2 * splitgroupscommand.cpp
5 * Created by westcott on 9/20/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "splitgroupscommand.h"
11 #include "sharedutilities.h"
13 //**********************************************************************************************************************
14 vector<string> SplitGroupCommand::getValidParameters(){
16 string Array[] = {"name","group","groups","fasta","outputdir","inputdir"};
17 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
21 m->errorOut(e, "SplitGroupCommand", "getValidParameters");
25 //**********************************************************************************************************************
26 vector<string> SplitGroupCommand::getRequiredParameters(){
28 string Array[] = {"fasta","group"};
29 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
33 m->errorOut(e, "SplitGroupCommand", "getRequiredParameters");
37 //**********************************************************************************************************************
38 vector<string> SplitGroupCommand::getRequiredFiles(){
40 vector<string> myArray;
44 m->errorOut(e, "SplitGroupCommand", "getRequiredFiles");
48 //**********************************************************************************************************************
49 SplitGroupCommand::SplitGroupCommand(){
51 abort = true; calledHelp = true;
52 vector<string> tempOutNames;
53 outputTypes["fasta"] = tempOutNames;
54 outputTypes["name"] = tempOutNames;
57 m->errorOut(e, "SplitGroupCommand", "SplitGroupCommand");
61 //**********************************************************************************************************************
62 SplitGroupCommand::SplitGroupCommand(string option) {
64 abort = false; calledHelp = false;
66 //allow user to run help
67 if(option == "help") { help(); abort = true; calledHelp = true; }
70 //valid paramters for this command
71 string Array[] = {"name","group","groups","fasta","outputdir","inputdir"};
72 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
74 OptionParser parser(option);
75 map<string, string> parameters = parser.getParameters();
77 ValidParameters validParameter;
78 map<string, string>::iterator it;
80 //check to make sure all parameters are valid for command
81 for (it = parameters.begin(); it != parameters.end(); it++) {
82 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
85 //initialize outputTypes
86 vector<string> tempOutNames;
87 outputTypes["fasta"] = tempOutNames;
88 outputTypes["name"] = tempOutNames;
90 //if the user changes the input directory command factory will send this info to us in the output parameter
91 string inputDir = validParameter.validFile(parameters, "inputdir", false);
92 if (inputDir == "not found"){ inputDir = ""; }
95 it = parameters.find("group");
96 //user has given a template file
97 if(it != parameters.end()){
98 path = m->hasPath(it->second);
99 //if the user has not given a path then, add inputdir. else leave path alone.
100 if (path == "") { parameters["group"] = inputDir + it->second; }
103 it = parameters.find("fasta");
104 //user has given a template file
105 if(it != parameters.end()){
106 path = m->hasPath(it->second);
107 //if the user has not given a path then, add inputdir. else leave path alone.
108 if (path == "") { parameters["fasta"] = inputDir + it->second; }
111 it = parameters.find("name");
112 //user has given a template file
113 if(it != parameters.end()){
114 path = m->hasPath(it->second);
115 //if the user has not given a path then, add inputdir. else leave path alone.
116 if (path == "") { parameters["name"] = inputDir + it->second; }
122 namefile = validParameter.validFile(parameters, "name", true);
123 if (namefile == "not open") { abort = true; }
124 else if (namefile == "not found") { namefile = ""; }
126 fastafile = validParameter.validFile(parameters, "fasta", true);
127 if (fastafile == "not open") { abort = true; }
128 else if (fastafile == "not found") { fastafile = ""; m->mothurOut("fasta is a required parameter for the split.group command. "); m->mothurOutEndLine(); abort = true; }
130 groupfile = validParameter.validFile(parameters, "group", true);
131 if (groupfile == "not open") { groupfile = ""; abort = true; }
132 else if (groupfile == "not found") { groupfile = ""; m->mothurOut("group is a required parameter for the split.group command. "); m->mothurOutEndLine(); abort = true; }
134 groups = validParameter.validFile(parameters, "groups", false);
135 if (groups == "not found") { groups = ""; }
136 else { m->splitAtDash(groups, Groups); }
138 //if the user changes the output directory command factory will send this info to us in the output parameter
139 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(groupfile); }
143 catch(exception& e) {
144 m->errorOut(e, "SplitGroupCommand", "SplitAbundCommand");
148 //**********************************************************************************************************************
149 void SplitGroupCommand::help(){
151 m->mothurOut("The split.group command reads a group file, and parses your fasta and names files by groups. \n");
152 m->mothurOut("The split.group command parameters are fasta, name, group and groups.\n");
153 m->mothurOut("The fasta and group parameters are required.\n");
154 m->mothurOut("The groups parameter allows you to select groups to create files for. \n");
155 m->mothurOut("For example if you set groups=A-B-C, you will get a .A.fasta, .A.names, .B.fasta, .B.names, .C.fasta, .C.names files. \n");
156 m->mothurOut("If you want .fasta and .names files for all groups, set groups=all. \n");
157 m->mothurOut("The split.group command should be used in the following format: split.group(fasta=yourFasta, group=yourGroupFile).\n");
158 m->mothurOut("Example: split.group(fasta=abrecovery.fasta, group=abrecovery.groups).\n");
159 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
162 catch(exception& e) {
163 m->errorOut(e, "SplitGroupCommand", "help");
167 //**********************************************************************************************************************
168 SplitGroupCommand::~SplitGroupCommand(){ }
169 //**********************************************************************************************************************
170 int SplitGroupCommand::execute(){
173 if (abort == true) { if (calledHelp) { return 0; } return 2; }
175 groupMap = new GroupMap(groupfile);
178 SharedUtil util; util.setGroups(Groups, groupMap->namesOfGroups);
180 if (namefile != "") { readNames(); }
185 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
187 m->mothurOutEndLine();
188 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
189 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
190 m->mothurOutEndLine();
194 catch(exception& e) {
195 m->errorOut(e, "SplitGroupCommand", "execute");
199 /**********************************************************************************************************************/
200 int SplitGroupCommand::readNames() {
204 m->openInputFile(namefile, in);
207 if (m->control_pressed) { break; }
209 string firstCol, secondCol;
210 in >> firstCol >> secondCol; m->gobble(in);
212 vector<string> names;
213 m->splitAtComma(secondCol, names);
215 nameMap[firstCol] = names;
222 catch(exception& e) {
223 m->errorOut(e, "SplitGroupCommand", "readNames");
228 /**********************************************************************************************************************/
229 int SplitGroupCommand::splitFasta() {
232 string filerootFasta = outputDir + m->getRootName(m->getSimpleName(fastafile));
233 string filerootName = outputDir + m->getRootName(m->getSimpleName(namefile));
236 map<string, ofstream*> filehandles;
237 map<string, ofstream*>::iterator it3;
239 for (int i=0; i<Groups.size(); i++) {
241 filehandles[Groups[i]+"fasta"] = temp;
242 m->openOutputFile(filerootFasta + Groups[i] + ".fasta", *(filehandles[Groups[i]+"fasta"]));
243 outputNames.push_back(filerootFasta + Groups[i] + ".fasta"); outputTypes["fasta"].push_back(filerootFasta + Groups[i] + ".fasta");
245 if (namefile != "") {
246 temp2 = new ofstream;
247 filehandles[Groups[i]+"name"] = temp2;
248 m->openOutputFile(filerootName + Groups[i] + ".names", *(filehandles[Groups[i]+"name"]));
249 outputNames.push_back(filerootName + Groups[i] + ".names"); outputTypes["name"].push_back(filerootFasta + Groups[i] + ".names");
255 m->openInputFile(fastafile, in);
258 if (m->control_pressed) { break; }
260 Sequence seq(in); m->gobble(in);
262 if (seq.getName() != "") {
264 itNames = nameMap.find(seq.getName());
266 if (itNames == nameMap.end()) {
267 if (namefile != "") { m->mothurOut(seq.getName() + " is not in your namesfile, ignoring."); m->mothurOutEndLine(); }
268 else { //no names file given
269 string group = groupMap->getGroup(seq.getName());
271 if (m->inUsersGroups(group, Groups)) { //only add if this is in a group we want
272 seq.printSequence(*(filehandles[group+"fasta"]));
273 }else if(group == "not found") {
274 m->mothurOut(seq.getName() + " is not in your groupfile. Ignoring."); m->mothurOutEndLine();
278 vector<string> names = itNames->second;
279 map<string, string> group2Names;
280 map<string, string>::iterator it;
282 for (int i = 0; i < names.size(); i++) { //build strings for new group names file, will select rep below
283 string group = groupMap->getGroup(names[i]);
285 if (m->inUsersGroups(group, Groups)) { //only add if this is in a group we want
286 it = group2Names.find(group);
287 if (it == group2Names.end()) {
288 group2Names[group] = names[i] + ",";
290 group2Names[group] += names[i] + ",";
292 }else if(group == "not found") {
293 m->mothurOut(names[i] + " is not in your groupfile. Ignoring."); m->mothurOutEndLine();
297 for (map<string, string>::iterator itGroups = group2Names.begin(); itGroups != group2Names.end(); itGroups++) {
298 //edit names string, by grabbing the first guy as the rep and removing the last comma
299 string newNames = itGroups->second;
300 newNames = newNames.substr(0, newNames.length()-1);
303 int pos = newNames.find_first_of(',');
304 if (pos == newNames.npos) { //only one sequence so it represents itself
307 repName = newNames.substr(0, pos);
310 *(filehandles[itGroups->first+"name"]) << repName << '\t' << newNames << endl;
311 *(filehandles[itGroups->first+"fasta"]) << ">" << repName << endl << seq.getAligned() << endl;
319 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
320 (*(filehandles[it3->first])).close();
324 vector<string> newOutputNames;
326 for (int i = 0; i < outputNames.size(); i++) {
327 if (m->isBlank(outputNames[i])) {
328 remove(outputNames[i].c_str());
329 }else { newOutputNames.push_back(outputNames[i]); }
331 outputNames = newOutputNames;
336 catch(exception& e) {
337 m->errorOut(e, "SplitGroupCommand", "splitFasta");
341 /**********************************************************************************************************************/