]> git.donarmstrong.com Git - mothur.git/blob - getgroupscommand.cpp
changed random forest output filename
[mothur.git] / getgroupscommand.cpp
1 /*
2  *  getgroupscommand.cpp
3  *  Mothur
4  *
5  *  Created by westcott on 11/10/10.
6  *  Copyright 2010 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "getgroupscommand.h"
11 #include "sequence.hpp"
12 #include "listvector.hpp"
13 #include "sharedutilities.h"
14 #include "inputdata.h"
15
16 //**********************************************************************************************************************
17 vector<string> GetGroupsCommand::setParameters(){       
18         try {
19                 CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "none", "FNGLT","fasta",false,false, true); parameters.push_back(pfasta);
20                 CommandParameter pshared("shared", "InputTypes", "", "", "none", "sharedGroup", "none","shared",false,false, true); parameters.push_back(pshared);
21         CommandParameter pname("name", "InputTypes", "", "", "NameCount", "none", "none","name",false,false, true); parameters.push_back(pname);
22         CommandParameter pcount("count", "InputTypes", "", "", "NameCount-CountGroup", "none", "none","count",false,false, true); parameters.push_back(pcount);
23                 CommandParameter pgroup("group", "InputTypes", "", "", "CountGroup", "sharedGroup", "FNGLT","group",false,false, true); parameters.push_back(pgroup);           
24         CommandParameter pdesign("design", "InputTypes", "", "", "none", "sharedGroup", "FNGLT","design",false,false, true); parameters.push_back(pdesign);
25                 CommandParameter plist("list", "InputTypes", "", "", "none", "none", "FNGLT","list",false,false, true); parameters.push_back(plist);
26                 CommandParameter ptaxonomy("taxonomy", "InputTypes", "", "", "none", "none", "FNGLT","taxonomy",false,false, true); parameters.push_back(ptaxonomy);
27                 CommandParameter paccnos("accnos", "InputTypes", "", "", "none", "none", "none","",false,false); parameters.push_back(paccnos);
28                 CommandParameter pgroups("groups", "String", "", "", "", "", "","",false,false); parameters.push_back(pgroups);
29                 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
30                 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
31                 
32                 vector<string> myArray;
33                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
34                 return myArray;
35         }
36         catch(exception& e) {
37                 m->errorOut(e, "GetGroupsCommand", "setParameters");
38                 exit(1);
39         }
40 }
41 //**********************************************************************************************************************
42 string GetGroupsCommand::getHelpString(){       
43         try {
44                 string helpString = "";
45                 helpString += "The get.groups command selects sequences from a specfic group or set of groups from the following file types: fasta, name, group, list, taxonomy, design or shared file.\n";
46                 helpString += "It outputs a file containing the sequences in the those specified groups, or a sharedfile containing only those groups.\n";
47                 helpString += "The get.groups command parameters are accnos, fasta, name, group, list, taxonomy, shared, design and groups. The group or count parameter is required, unless you have a current group or count file, or are using a shared file.\n";
48                 helpString += "You must also provide an accnos containing the list of groups to get or set the groups parameter to the groups you wish to select.\n";
49                 helpString += "The groups parameter allows you to specify which of the groups in your groupfile you would like.  You can separate group names with dashes.\n";
50                 helpString += "The get.groups command should be in the following format: get.groups(accnos=yourAccnos, fasta=yourFasta, group=yourGroupFile).\n";
51                 helpString += "Example get.groups(accnos=amazon.accnos, fasta=amazon.fasta, group=amazon.groups).\n";
52                 helpString += "or get.groups(groups=pasture, fasta=amazon.fasta, group=amazon.groups).\n";
53                 helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n";
54                 return helpString;
55         }
56         catch(exception& e) {
57                 m->errorOut(e, "GetGroupsCommand", "getHelpString");
58                 exit(1);
59         }
60 }
61 //**********************************************************************************************************************
62 string GetGroupsCommand::getOutputPattern(string type) {
63     try {
64         string pattern = "";
65         
66         if (type == "fasta")            {   pattern = "[filename],pick,[extension]";    }
67         else if (type == "taxonomy")    {   pattern = "[filename],pick,[extension]";    }
68         else if (type == "name")        {   pattern = "[filename],pick,[extension]";    }
69         else if (type == "group")       {   pattern = "[filename],pick,[extension]";    }
70         else if (type == "count")       {   pattern = "[filename],pick,[extension]";    }
71         else if (type == "list")        {   pattern = "[filename],pick,[extension]";    }
72         else if (type == "shared")      {   pattern = "[filename],[tag],pick,[extension]";    }
73         else if (type == "design")      {   pattern = "[filename],pick,[extension]";    }
74         else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true;  }
75         
76         return pattern;
77     }
78     catch(exception& e) {
79         m->errorOut(e, "GetGroupsCommand", "getOutputPattern");
80         exit(1);
81     }
82 }
83 //**********************************************************************************************************************
84 GetGroupsCommand::GetGroupsCommand(){   
85         try {
86                 abort = true; calledHelp = true;
87                 setParameters();
88                 vector<string> tempOutNames;
89                 outputTypes["fasta"] = tempOutNames;
90                 outputTypes["taxonomy"] = tempOutNames;
91                 outputTypes["name"] = tempOutNames;
92                 outputTypes["group"] = tempOutNames;
93                 outputTypes["list"] = tempOutNames;
94                 outputTypes["shared"] = tempOutNames;
95         outputTypes["design"] = tempOutNames;
96         outputTypes["count"] = tempOutNames;
97         }
98         catch(exception& e) {
99                 m->errorOut(e, "GetGroupsCommand", "GetGroupsCommand");
100                 exit(1);
101         }
102 }
103 //**********************************************************************************************************************
104 GetGroupsCommand::GetGroupsCommand(string option)  {
105         try {
106                 abort = false; calledHelp = false;   
107                 
108                 //allow user to run help
109                 if(option == "help") { help(); abort = true; calledHelp = true; }
110                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
111                 
112                 else {
113                         vector<string> myArray = setParameters();
114                         
115                         OptionParser parser(option);
116                         map<string,string> parameters = parser.getParameters();
117                         
118                         ValidParameters validParameter;
119                         map<string,string>::iterator it;
120                         
121                         //check to make sure all parameters are valid for command
122                         for (it = parameters.begin(); it != parameters.end(); it++) { 
123                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
124                         }
125                         
126                         //initialize outputTypes
127                         vector<string> tempOutNames;
128                         outputTypes["fasta"] = tempOutNames;
129                         outputTypes["taxonomy"] = tempOutNames;
130                         outputTypes["name"] = tempOutNames;
131                         outputTypes["group"] = tempOutNames;
132                         outputTypes["list"] = tempOutNames;
133                         outputTypes["shared"] = tempOutNames;
134             outputTypes["design"] = tempOutNames;
135             outputTypes["count"] = tempOutNames;
136                         
137                         
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 = "";         }
140                         
141                         //if the user changes the input directory command factory will send this info to us in the output parameter 
142                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
143                         if (inputDir == "not found"){   inputDir = "";          }
144                         else {
145                                 string path;
146                                 it = parameters.find("fasta");
147                                 //user has given a template file
148                                 if(it != parameters.end()){ 
149                                         path = m->hasPath(it->second);
150                                         //if the user has not given a path then, add inputdir. else leave path alone.
151                                         if (path == "") {       parameters["fasta"] = inputDir + it->second;            }
152                                 }
153                                 
154                                 it = parameters.find("accnos");
155                                 //user has given a template file
156                                 if(it != parameters.end()){ 
157                                         path = m->hasPath(it->second);
158                                         //if the user has not given a path then, add inputdir. else leave path alone.
159                                         if (path == "") {       parameters["accnos"] = inputDir + it->second;           }
160                                 }
161                                 
162                                 it = parameters.find("list");
163                                 //user has given a template file
164                                 if(it != parameters.end()){ 
165                                         path = m->hasPath(it->second);
166                                         //if the user has not given a path then, add inputdir. else leave path alone.
167                                         if (path == "") {       parameters["list"] = inputDir + it->second;             }
168                                 }
169                                 
170                                 it = parameters.find("name");
171                                 //user has given a template file
172                                 if(it != parameters.end()){ 
173                                         path = m->hasPath(it->second);
174                                         //if the user has not given a path then, add inputdir. else leave path alone.
175                                         if (path == "") {       parameters["name"] = inputDir + it->second;             }
176                                 }
177                                 
178                                 it = parameters.find("group");
179                                 //user has given a template file
180                                 if(it != parameters.end()){ 
181                                         path = m->hasPath(it->second);
182                                         //if the user has not given a path then, add inputdir. else leave path alone.
183                                         if (path == "") {       parameters["group"] = inputDir + it->second;            }
184                                 }
185                                 
186                                 it = parameters.find("taxonomy");
187                                 //user has given a template file
188                                 if(it != parameters.end()){ 
189                                         path = m->hasPath(it->second);
190                                         //if the user has not given a path then, add inputdir. else leave path alone.
191                                         if (path == "") {       parameters["taxonomy"] = inputDir + it->second;         }
192                                 }
193                                 
194                                 it = parameters.find("shared");
195                                 //user has given a template file
196                                 if(it != parameters.end()){ 
197                                         path = m->hasPath(it->second);
198                                         //if the user has not given a path then, add inputdir. else leave path alone.
199                                         if (path == "") {       parameters["shared"] = inputDir + it->second;           }
200                                 }
201                 
202                 it = parameters.find("design");
203                                 //user has given a template file
204                                 if(it != parameters.end()){ 
205                                         path = m->hasPath(it->second);
206                                         //if the user has not given a path then, add inputdir. else leave path alone.
207                                         if (path == "") {       parameters["design"] = inputDir + it->second;           }
208                                 }
209                 
210                 it = parameters.find("count");
211                                 //user has given a template file
212                                 if(it != parameters.end()){ 
213                                         path = m->hasPath(it->second);
214                                         //if the user has not given a path then, add inputdir. else leave path alone.
215                                         if (path == "") {       parameters["count"] = inputDir + it->second;            }
216                                 }
217                         }
218                         
219                         
220                         //check for required parameters
221                         accnosfile = validParameter.validFile(parameters, "accnos", true);
222                         if (accnosfile == "not open") { abort = true; }
223                         else if (accnosfile == "not found") {  accnosfile = ""; }
224                         else { m->setAccnosFile(accnosfile); }
225                         
226                         fastafile = validParameter.validFile(parameters, "fasta", true);
227                         if (fastafile == "not open") { fastafile = ""; abort = true; }
228                         else if (fastafile == "not found") {  fastafile = "";  }
229                         else { m->setFastaFile(fastafile); }
230                         
231                         namefile = validParameter.validFile(parameters, "name", true);
232                         if (namefile == "not open") { namefile = ""; abort = true; }
233                         else if (namefile == "not found") {  namefile = "";  }  
234                         else { m->setNameFile(namefile); }
235                         
236                         listfile = validParameter.validFile(parameters, "list", true);
237                         if (listfile == "not open") { abort = true; }
238                         else if (listfile == "not found") {  listfile = "";  }
239                         else { m->setListFile(listfile); }
240                         
241                         taxfile = validParameter.validFile(parameters, "taxonomy", true);
242                         if (taxfile == "not open") { taxfile = ""; abort = true; }
243                         else if (taxfile == "not found") {  taxfile = "";  }
244                         else { m->setTaxonomyFile(taxfile); }
245                         
246                         groups = validParameter.validFile(parameters, "groups", false);                 
247                         if (groups == "not found") { groups = ""; }
248                         else {
249                                 m->splitAtDash(groups, Groups);
250                                 m->setGroups(Groups);
251                         }
252                         
253                         sharedfile = validParameter.validFile(parameters, "shared", true);
254                         if (sharedfile == "not open") { sharedfile = ""; abort = true; }
255                         else if (sharedfile == "not found") {  sharedfile = "";  }
256                         else { m->setSharedFile(sharedfile); }
257                         
258                         groupfile = validParameter.validFile(parameters, "group", true);
259                         if (groupfile == "not open") { groupfile = ""; abort = true; }
260                         else if (groupfile == "not found") {    groupfile = ""; }
261                         else { m->setGroupFile(groupfile); }
262             
263             designfile = validParameter.validFile(parameters, "design", true);
264                         if (designfile == "not open") { designfile = ""; abort = true; }
265                         else if (designfile == "not found") {   designfile = "";        }
266                         else { m->setDesignFile(designfile); }
267             
268             countfile = validParameter.validFile(parameters, "count", true);
269             if (countfile == "not open") { countfile = ""; abort = true; }
270             else if (countfile == "not found") { countfile = "";  }     
271             else { m->setCountTableFile(countfile); }
272             
273             if ((namefile != "") && (countfile != "")) {
274                 m->mothurOut("[ERROR]: you may only use one of the following: name or count."); m->mothurOutEndLine(); abort = true;
275             }
276             
277             if ((groupfile != "") && (countfile != "")) {
278                 m->mothurOut("[ERROR]: you may only use one of the following: group or count."); m->mothurOutEndLine(); abort=true;
279             }
280
281                         
282                         if ((sharedfile == "") && (groupfile == "") && (designfile == "") && (countfile == "")) { 
283                                 //is there are current file available for any of these?
284                                 if ((namefile != "") || (fastafile != "") || (listfile != "") || (taxfile != "")) {
285                                         //give priority to group, then shared
286                                         groupfile = m->getGroupFile(); 
287                                         if (groupfile != "") {  m->mothurOut("Using " + groupfile + " as input file for the group parameter."); m->mothurOutEndLine(); }
288                                         else { 
289                                                 sharedfile = m->getSharedFile(); 
290                                                 if (sharedfile != "") { m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
291                                                 else { 
292                                                         countfile = m->getCountTableFile(); 
293                             if (countfile != "") { m->mothurOut("Using " + countfile + " as input file for the count parameter."); m->mothurOutEndLine(); }
294                             else { 
295                                 m->mothurOut("You have no current groupfile, countfile or sharedfile and one is required."); m->mothurOutEndLine(); abort = true;
296                             }
297                                                 }
298                                         }
299                                 }else {
300                                         //give priority to shared, then group
301                                         sharedfile = m->getSharedFile(); 
302                                         if (sharedfile != "") {  m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
303                                         else { 
304                                                 groupfile = m->getGroupFile(); 
305                                                 if (groupfile != "") { m->mothurOut("Using " + groupfile + " as input file for the group parameter."); m->mothurOutEndLine(); }
306                                                 else { 
307                                                         designfile = m->getDesignFile(); 
308                             if (designfile != "") { m->mothurOut("Using " + designfile + " as input file for the design parameter."); m->mothurOutEndLine(); }
309                             else { 
310                                 countfile = m->getCountTableFile(); 
311                                 if (countfile != "") { m->mothurOut("Using " + countfile + " as input file for the count parameter."); m->mothurOutEndLine(); }
312                                 else { 
313                                     m->mothurOut("You have no current groupfile, designfile, countfile or sharedfile and one is required."); m->mothurOutEndLine(); abort = true;
314                                 }
315
316                             }
317                                                 }
318                                         }
319                                 }
320                         }
321                         
322                         if ((accnosfile == "") && (Groups.size() == 0)) { m->mothurOut("You must provide an accnos file or specify groups using the groups parameter."); m->mothurOutEndLine(); abort = true; }
323                         
324                         if ((fastafile == "") && (namefile == "") && (countfile == "") && (groupfile == "")  && (designfile == "") && (sharedfile == "") && (listfile == "") && (taxfile == ""))  { m->mothurOut("You must provide at least one of the following: fasta, name, taxonomy, group, shared, design, count or list."); m->mothurOutEndLine(); abort = true; }
325                         if (((groupfile == "") && (countfile == "")) && ((namefile != "") || (fastafile != "") || (listfile != "") || (taxfile != "")))  { m->mothurOut("If using a fasta, name, taxonomy, group or list, then you must provide a group or count file."); m->mothurOutEndLine(); abort = true; }
326             
327             if (countfile == "") {
328                 if ((namefile == "") && ((fastafile != "") || (taxfile != ""))){
329                     vector<string> files; files.push_back(fastafile); files.push_back(taxfile);
330                     parser.getNameFile(files);
331                 }
332             }
333                 }
334                 
335         }
336         catch(exception& e) {
337                 m->errorOut(e, "GetGroupsCommand", "GetGroupsCommand");
338                 exit(1);
339         }
340 }
341 //**********************************************************************************************************************
342
343 int GetGroupsCommand::execute(){
344         try {
345                 
346                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
347                 
348                 //get groups you want to remove
349                 if (accnosfile != "") { m->readAccnos(accnosfile, Groups); m->setGroups(Groups); }
350                 
351                 if (groupfile != "") {
352                         groupMap = new GroupMap(groupfile);
353                         groupMap->readMap();
354                         
355                         //make sure groups are valid
356                         //takes care of user setting groupNames that are invalid or setting groups=all
357                         SharedUtil* util = new SharedUtil();
358                         vector<string> gNamesOfGroups = groupMap->getNamesOfGroups();
359                         util->setGroups(Groups, gNamesOfGroups);
360             m->setGroups(Groups);
361                         groupMap->setNamesOfGroups(gNamesOfGroups);
362                         delete util;
363                         
364                         //fill names with names of sequences that are from the groups we want to remove 
365                         fillNames();
366                         
367                         delete groupMap;
368                 }else if (countfile != ""){
369             if ((fastafile != "") || (listfile != "") || (taxfile != "")) { 
370                 m->mothurOut("\n[NOTE]: The count file should contain only unique names, so mothur assumes your fasta, list and taxonomy files also contain only uniques.\n\n");
371             }
372             CountTable ct;
373             ct.readTable(countfile, true);
374             if (!ct.hasGroupInfo()) { m->mothurOut("[ERROR]: your count file does not contain group info, aborting.\n"); return 0; }
375                 
376             vector<string> gNamesOfGroups = ct.getNamesOfGroups();
377             SharedUtil util;
378             util.setGroups(Groups, gNamesOfGroups);
379             m->setGroups(Groups);
380             for (int i = 0; i < Groups.size(); i++) {
381                 vector<string> thisGroupsSeqs = ct.getNamesOfSeqs(Groups[i]);
382                 for (int j = 0; j < thisGroupsSeqs.size(); j++) { names.insert(thisGroupsSeqs[j]); }
383             }
384         }
385                 
386                 if (m->control_pressed) { return 0; }
387                 
388                 //read through the correct file and output lines you want to keep
389                 if (namefile != "")                     {               readName();             }
390                 if (fastafile != "")            {               readFasta();    }
391                 if (groupfile != "")            {               readGroup();    }
392         if (countfile != "")            {               readCount();    }
393                 if (listfile != "")                     {               readList();             }
394                 if (taxfile != "")                      {               readTax();              }
395                 if (sharedfile != "")           {               readShared();   }
396         if (designfile != "")           {               readDesign();   }
397                 
398                 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        m->mothurRemove(outputNames[i]); } return 0; }
399                 
400                 
401                 if (outputNames.size() != 0) {
402                         m->mothurOutEndLine();
403                         m->mothurOut("Output File names: "); m->mothurOutEndLine();
404                         for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
405                         m->mothurOutEndLine();
406                         
407                         //set fasta file as new current fastafile
408                         string current = "";
409                         itTypes = outputTypes.find("fasta");
410                         if (itTypes != outputTypes.end()) {
411                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
412                         }
413                         
414                         itTypes = outputTypes.find("name");
415                         if (itTypes != outputTypes.end()) {
416                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); }
417                         }
418                         
419                         itTypes = outputTypes.find("group");
420                         if (itTypes != outputTypes.end()) {
421                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setGroupFile(current); }
422                         }
423                         
424                         itTypes = outputTypes.find("list");
425                         if (itTypes != outputTypes.end()) {
426                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setListFile(current); }
427                         }
428                         
429                         itTypes = outputTypes.find("taxonomy");
430                         if (itTypes != outputTypes.end()) {
431                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setTaxonomyFile(current); }
432                         }
433                         
434                         itTypes = outputTypes.find("shared");
435                         if (itTypes != outputTypes.end()) {
436                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setSharedFile(current); }
437                         }
438             
439             itTypes = outputTypes.find("design");
440                         if (itTypes != outputTypes.end()) {
441                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setDesignFile(current); }
442                         }
443             
444             itTypes = outputTypes.find("count");
445                         if (itTypes != outputTypes.end()) {
446                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setCountTableFile(current); }
447                         }
448                 }
449                 
450                 return 0;               
451         }
452         
453         catch(exception& e) {
454                 m->errorOut(e, "GetGroupsCommand", "execute");
455                 exit(1);
456         }
457 }
458
459 //**********************************************************************************************************************
460 int GetGroupsCommand::readFasta(){
461         try {
462                 string thisOutputDir = outputDir;
463                 if (outputDir == "") {  thisOutputDir += m->hasPath(fastafile);  }
464         map<string, string> variables; 
465         variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(fastafile));
466         variables["[extension]"] = m->getExtension(fastafile);
467                 string outputFileName = getOutputFileName("fasta", variables);
468                 
469                 ofstream out;
470                 m->openOutputFile(outputFileName, out);
471                 
472                 ifstream in;
473                 m->openInputFile(fastafile, in);
474                 string name;
475                 
476                 bool wroteSomething = false;
477                 int selectedCount = 0;
478                 
479                 while(!in.eof()){
480                         if (m->control_pressed) { in.close();  out.close();  m->mothurRemove(outputFileName);  return 0; }
481                         
482                         Sequence currSeq(in);
483                         name = currSeq.getName();
484                         
485                         if (name != "") {
486                                 //if this name is in the accnos file
487                                 if (names.count(name) != 0) {
488                                         wroteSomething = true;
489                                         
490                                         currSeq.printSequence(out);
491                                         selectedCount++;
492                                 }else{
493                                         //if you are not in the accnos file check if you are a name that needs to be changed
494                                         map<string, string>::iterator it = uniqueToRedundant.find(name);
495                                         if (it != uniqueToRedundant.end()) {
496                                                 wroteSomething = true;
497                                                 currSeq.setName(it->second);
498                                                 currSeq.printSequence(out);
499                                                 selectedCount++;
500                                         }
501                                 }
502                         }
503                         m->gobble(in);
504                 }
505                 in.close();     
506                 out.close();
507                 
508                 if (wroteSomething == false) {  m->mothurOut("Your file does NOT contain sequences from the groups you wish to get."); m->mothurOutEndLine();  }
509                 outputTypes["fasta"].push_back(outputFileName);  outputNames.push_back(outputFileName);
510                 
511                 m->mothurOut("Selected " + toString(selectedCount) + " sequences from your fasta file."); m->mothurOutEndLine();
512
513                 
514                 return 0;
515                 
516         }
517         catch(exception& e) {
518                 m->errorOut(e, "GetGroupsCommand", "readFasta");
519                 exit(1);
520         }
521 }
522 //**********************************************************************************************************************
523 int GetGroupsCommand::readShared(){
524         try {
525                 string thisOutputDir = outputDir;
526                 if (outputDir == "") {  thisOutputDir += m->hasPath(sharedfile);  }
527                 
528                 InputData input(sharedfile, "sharedfile");
529                 vector<SharedRAbundVector*> lookup = input.getSharedRAbundVectors();
530         map<string, string> variables; 
531         variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(sharedfile));
532         variables["[extension]"] = m->getExtension(sharedfile);
533                 
534                 bool wroteSomething = false;
535                 
536                 while(lookup[0] != NULL) {
537                         
538             variables["[tag]"] = lookup[0]->getLabel();
539             string outputFileName = getOutputFileName("shared", variables);
540                         
541                         ofstream out;
542                         m->openOutputFile(outputFileName, out);
543                         outputTypes["shared"].push_back(outputFileName);  outputNames.push_back(outputFileName);
544                         
545                         if (m->control_pressed) { out.close();  m->mothurRemove(outputFileName);  for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } return 0; }
546                         
547                         lookup[0]->printHeaders(out); 
548                         
549                         for (int i = 0; i < lookup.size(); i++) {
550                                 out << lookup[i]->getLabel() << '\t' << lookup[i]->getGroup() << '\t';
551                                 lookup[i]->print(out);
552                                 wroteSomething = true;
553                                 
554                         }                       
555                         
556                         //get next line to process
557                         //prevent memory leak
558                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
559                         lookup = input.getSharedRAbundVectors();
560                         
561                         out.close();
562                 }
563                 
564                 if (wroteSomething == false) {  m->mothurOut("Your file contains only the groups you wish to remove."); m->mothurOutEndLine();  }
565                 
566                 string groupsString = "";
567                 for (int i = 0; i < Groups.size()-1; i++) {     groupsString += Groups[i] + ", "; }
568                 groupsString += Groups[Groups.size()-1];
569                 
570                 m->mothurOut("Selected groups: " + groupsString + " from your shared file."); m->mothurOutEndLine();
571                 
572                 return 0;
573                 
574         }
575         catch(exception& e) {
576                 m->errorOut(e, "GetGroupsCommand", "readShared");
577                 exit(1);
578         }
579 }
580 //**********************************************************************************************************************
581 int GetGroupsCommand::readList(){
582         try {
583                 string thisOutputDir = outputDir;
584                 if (outputDir == "") {  thisOutputDir += m->hasPath(listfile);  }
585         map<string, string> variables; 
586         variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(listfile));
587         variables["[extension]"] = m->getExtension(listfile);
588                 string outputFileName = getOutputFileName("list", variables);
589                 
590                 ofstream out;
591                 m->openOutputFile(outputFileName, out);
592                 
593                 ifstream in;
594                 m->openInputFile(listfile, in);
595                 
596                 bool wroteSomething = false;
597                 int selectedCount = 0;
598                 
599                 while(!in.eof()){
600                         
601                         selectedCount = 0;
602                         
603                         //read in list vector
604                         ListVector list(in);
605                         
606                         //make a new list vector
607                         ListVector newList;
608                         newList.setLabel(list.getLabel());
609                         
610                         //for each bin
611                         for (int i = 0; i < list.getNumBins(); i++) {
612                                 if (m->control_pressed) { in.close();  out.close();  m->mothurRemove(outputFileName);  return 0; }
613                                 
614                                 //parse out names that are in accnos file
615                                 string binnames = list.get(i);
616                                 
617                                 string newNames = "";
618                                 while (binnames.find_first_of(',') != -1) { 
619                                         string name = binnames.substr(0,binnames.find_first_of(','));
620                                         binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
621                                         
622                                         //if that name is in the .accnos file, add it
623                                         if (names.count(name) != 0) {  newNames += name + ",";  selectedCount++;  }
624                                         else{
625                                                 //if you are not in the accnos file check if you are a name that needs to be changed
626                                                 map<string, string>::iterator it = uniqueToRedundant.find(name);
627                                                 if (it != uniqueToRedundant.end()) {
628                                                         newNames += it->second + ",";
629                                                         selectedCount++;
630                                                 }
631                                         }
632                                 }
633                                 
634                                 //get last name
635                                 if (names.count(binnames) != 0) {  newNames += binnames + ",";  selectedCount++;  }
636                                 else{
637                                         //if you are not in the accnos file check if you are a name that needs to be changed
638                                         map<string, string>::iterator it = uniqueToRedundant.find(binnames);
639                                         if (it != uniqueToRedundant.end()) {
640                                                 newNames += it->second + ",";
641                                                 selectedCount++;
642                                         }
643                                 }
644                                 
645                                 //if there are names in this bin add to new list
646                                 if (newNames != "") {  
647                                         newNames = newNames.substr(0, newNames.length()-1); //rip off extra comma
648                                         newList.push_back(newNames);    
649                                 }
650                         }
651                         
652                         //print new listvector
653                         if (newList.getNumBins() != 0) {
654                                 wroteSomething = true;
655                                 newList.print(out);
656                         }
657                         
658                         m->gobble(in);
659                 }
660                 in.close();     
661                 out.close();
662                 
663                 if (wroteSomething == false) {  m->mothurOut("Your file does NOT contain sequences from the groups you wish to get."); m->mothurOutEndLine();  }
664                 outputTypes["list"].push_back(outputFileName); outputNames.push_back(outputFileName);
665                 
666                 m->mothurOut("Selected " + toString(selectedCount) + " sequences from your list file."); m->mothurOutEndLine();
667                 
668                 return 0;
669                 
670         }
671         catch(exception& e) {
672                 m->errorOut(e, "GetGroupsCommand", "readList");
673                 exit(1);
674         }
675 }
676 //**********************************************************************************************************************
677 int GetGroupsCommand::readName(){
678         try {
679                 string thisOutputDir = outputDir;
680                 if (outputDir == "") {  thisOutputDir += m->hasPath(namefile);  }
681         map<string, string> variables; 
682                 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(namefile));
683         variables["[extension]"] = m->getExtension(namefile);
684                 string outputFileName = getOutputFileName("name", variables);
685                 
686                 ofstream out;
687                 m->openOutputFile(outputFileName, out);
688                 
689                 ifstream in;
690                 m->openInputFile(namefile, in);
691                 string name, firstCol, secondCol;
692                 
693                 bool wroteSomething = false;
694                 int selectedCount = 0;
695                 
696                 while(!in.eof()){
697                         if (m->control_pressed) { in.close();  out.close();  m->mothurRemove(outputFileName);  return 0; }
698                         
699                         in >> firstCol;         m->gobble(in);          
700                         in >> secondCol;                        
701                         
702                         vector<string> parsedNames;
703                         m->splitAtComma(secondCol, parsedNames);
704                         
705                         vector<string> validSecond;  validSecond.clear();
706                         for (int i = 0; i < parsedNames.size(); i++) {
707                                 if (names.count(parsedNames[i]) != 0) {
708                                         validSecond.push_back(parsedNames[i]);
709                                 }
710                         }
711                         
712                         selectedCount += validSecond.size();
713                         
714                         //if the name in the first column is in the set then print it and any other names in second column also in set
715                         if (names.count(firstCol) != 0) {
716                                 
717                                 wroteSomething = true;
718                                 
719                                 out << firstCol << '\t';
720                                 
721                                 //you know you have at least one valid second since first column is valid
722                                 for (int i = 0; i < validSecond.size()-1; i++) {  out << validSecond[i] << ',';  }
723                                 out << validSecond[validSecond.size()-1] << endl;
724                                 
725                                 //make first name in set you come to first column and then add the remaining names to second column
726                         }else {
727                                 
728                                 //you want part of this row
729                                 if (validSecond.size() != 0) {
730                                         
731                                         wroteSomething = true;
732                                         
733                                         out << validSecond[0] << '\t';
734                                         
735                                         //you know you have at least one valid second since first column is valid
736                                         for (int i = 0; i < validSecond.size()-1; i++) {  out << validSecond[i] << ',';  }
737                                         out << validSecond[validSecond.size()-1] << endl;
738                                         uniqueToRedundant[firstCol] = validSecond[0];
739                                 }
740                         }
741                         
742                         m->gobble(in);
743                 }
744                 in.close();
745                 out.close();
746                 
747                 if (wroteSomething == false) {  m->mothurOut("Your file does NOT contain sequences from the groups you wish to get."); m->mothurOutEndLine();  }
748                 outputTypes["name"].push_back(outputFileName); outputNames.push_back(outputFileName);
749                 
750                 m->mothurOut("Selected " + toString(selectedCount) + " sequences from your name file."); m->mothurOutEndLine();
751
752                 return 0;
753         }
754         catch(exception& e) {
755                 m->errorOut(e, "GetGroupsCommand", "readName");
756                 exit(1);
757         }
758 }
759
760 //**********************************************************************************************************************
761 int GetGroupsCommand::readGroup(){
762         try {
763                 string thisOutputDir = outputDir;
764                 if (outputDir == "") {  thisOutputDir += m->hasPath(groupfile);  }
765         map<string, string> variables; 
766                 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(groupfile));
767         variables["[extension]"] = m->getExtension(groupfile);
768                 string outputFileName = getOutputFileName("group", variables);
769                 
770                 ofstream out;
771                 m->openOutputFile(outputFileName, out);
772                 
773                 ifstream in;
774                 m->openInputFile(groupfile, in);
775                 string name, group;
776                 
777                 bool wroteSomething = false;
778                 int selectedCount = 0;
779                 
780                 while(!in.eof()){
781                         if (m->control_pressed) { in.close();  out.close();  m->mothurRemove(outputFileName);  return 0; }
782                         
783                         in >> name;                             //read from first column
784                         in >> group;                    //read from second column
785                         
786                         //if this name is in the accnos file
787                         if (names.count(name) != 0) {
788                                 wroteSomething = true;
789                                 out << name << '\t' << group << endl;
790                                 selectedCount++;
791                         }
792                         
793                         m->gobble(in);
794                 }
795                 in.close();
796                 out.close();
797                 
798                 if (wroteSomething == false) {  m->mothurOut("Your file does NOT contain sequences from the groups you wish to get."); m->mothurOutEndLine();  }
799                 outputTypes["group"].push_back(outputFileName); outputNames.push_back(outputFileName);
800                 
801                 m->mothurOut("Selected " + toString(selectedCount) + " sequences from your group file."); m->mothurOutEndLine();
802
803                 return 0;
804         }
805         catch(exception& e) {
806                 m->errorOut(e, "GetGroupsCommand", "readGroup");
807                 exit(1);
808         }
809 }
810 //**********************************************************************************************************************
811 int GetGroupsCommand::readCount(){
812         try {
813                 string thisOutputDir = outputDir;
814                 if (outputDir == "") {  thisOutputDir += m->hasPath(countfile);  }
815         map<string, string> variables; 
816                 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(countfile));
817         variables["[extension]"] = m->getExtension(countfile);
818                 string outputFileName = getOutputFileName("count", variables);
819                 
820                 ofstream out;
821                 m->openOutputFile(outputFileName, out);
822                 
823                 ifstream in;
824                 m->openInputFile(countfile, in);
825                 
826                 bool wroteSomething = false;
827                 int selectedCount = 0;
828                 
829         string headers = m->getline(in); m->gobble(in);
830         vector<string> columnHeaders = m->splitWhiteSpace(headers);
831         
832         vector<string> groups;
833         map<int, string> originalGroupIndexes;
834         map<string, int> GroupIndexes;
835         set<int> indexOfGroupsChosen;
836         for (int i = 2; i < columnHeaders.size(); i++) {  groups.push_back(columnHeaders[i]);  originalGroupIndexes[i-2] = columnHeaders[i]; }
837         //sort groups to keep consistent with how we store the groups in groupmap
838         sort(groups.begin(), groups.end());
839         for (int i = 0; i < groups.size(); i++) {  GroupIndexes[groups[i]] = i; }
840         sort(Groups.begin(), Groups.end());
841         out << "Representative_Sequence\ttotal\t";
842         for (int i = 0; i < Groups.size(); i++) { out << Groups[i] << '\t'; indexOfGroupsChosen.insert(GroupIndexes[Groups[i]]); }
843         out << endl;
844         
845         string name; int oldTotal;
846         while (!in.eof()) {
847             
848             if (m->control_pressed) { in.close();  out.close();  m->mothurRemove(outputFileName);  return 0; }
849             
850             in >> name; m->gobble(in); in >> oldTotal; m->gobble(in);
851             if (m->debug) { m->mothurOut("[DEBUG]: " + name + '\t' + toString(oldTotal) + "\n"); }
852             
853             if (names.count(name) != 0) {
854                 //if group info, then read it
855                 vector<int> selectedCounts; int thisTotal = 0; int temp;
856                 for (int i = 0; i < groups.size(); i++) {  
857                     int thisIndex = GroupIndexes[originalGroupIndexes[i]]; 
858                     in >> temp;  m->gobble(in);
859                     if (indexOfGroupsChosen.count(thisIndex) != 0) { //we want this group
860                         selectedCounts.push_back(temp); thisTotal += temp;
861                     }
862                 }
863
864                 out << name << '\t' << thisTotal << '\t';
865                 for (int i = 0; i < selectedCounts.size(); i++) {  out << selectedCounts[i] << '\t'; }
866                 out << endl;
867                 
868                 wroteSomething = true;
869                 selectedCount+= thisTotal;
870             }else {  m->getline(in); }
871             
872             m->gobble(in);
873         }
874         in.close();
875                 out.close();
876                 
877                 if (wroteSomething == false) {  m->mothurOut("Your file does NOT contain sequences from the groups you wish to get."); m->mothurOutEndLine();  }
878                 outputTypes["count"].push_back(outputFileName); outputNames.push_back(outputFileName);
879                 
880                 m->mothurOut("Selected " + toString(selectedCount) + " sequences from your count file."); m->mothurOutEndLine();
881         
882                 return 0;
883         }
884         catch(exception& e) {
885                 m->errorOut(e, "GetGroupsCommand", "readCount");
886                 exit(1);
887         }
888 }
889 //**********************************************************************************************************************
890 int GetGroupsCommand::readDesign(){
891         try {
892                 string thisOutputDir = outputDir;
893                 if (outputDir == "") {  thisOutputDir += m->hasPath(designfile);  }
894         map<string, string> variables; 
895                 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(designfile));
896         variables["[extension]"] = m->getExtension(designfile);
897                 string outputFileName = getOutputFileName("design", variables);
898                 
899                 ofstream out;
900                 m->openOutputFile(outputFileName, out);
901                 
902                 ifstream in;
903                 m->openInputFile(designfile, in);
904                 string name, group;
905                 
906                 bool wroteSomething = false;
907                 int selectedCount = 0;
908                 
909                 while(!in.eof()){
910                         if (m->control_pressed) { in.close();  out.close();  m->mothurRemove(outputFileName);  return 0; }
911                         
912                         in >> name;                             //read from first column
913                         in >> group;                    //read from second column
914                         
915                         //if this name is in the accnos file
916                         if (m->inUsersGroups(name, Groups)) {
917                                 wroteSomething = true;
918                                 out << name << '\t' << group << endl;
919                 selectedCount++;
920                         }
921                         
922                         m->gobble(in);
923                 }
924                 in.close();
925                 out.close();
926                 
927                 if (wroteSomething == false) {  m->mothurOut("Your file does NOT contain groups from the groups you wish to get."); m->mothurOutEndLine();  }
928                 outputTypes["design"].push_back(outputFileName); outputNames.push_back(outputFileName);
929                 
930                 m->mothurOut("Selected " + toString(selectedCount) + " groups from your design file."); m->mothurOutEndLine();
931         
932                 
933                 return 0;
934         }
935         catch(exception& e) {
936                 m->errorOut(e, "GetGroupsCommand", "readDesign");
937                 exit(1);
938         }
939 }
940
941
942 //**********************************************************************************************************************
943 int GetGroupsCommand::readTax(){
944         try {
945                 string thisOutputDir = outputDir;
946                 if (outputDir == "") {  thisOutputDir += m->hasPath(taxfile);  }
947         map<string, string> variables; 
948                 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(taxfile));
949         variables["[extension]"] = m->getExtension(taxfile);
950                 string outputFileName = getOutputFileName("taxonomy", variables);
951                 
952                 ofstream out;
953                 m->openOutputFile(outputFileName, out);
954                 
955                 ifstream in;
956                 m->openInputFile(taxfile, in);
957                 string name, tax;
958                 
959                 bool wroteSomething = false;
960                 
961                 while(!in.eof()){
962                         if (m->control_pressed) { in.close();  out.close();  m->mothurRemove(outputFileName);  return 0; }
963                         
964                         in >> name;                             //read from first column
965                         in >> tax;                      //read from second column
966                         
967                         //if this name is in the accnos file
968                         if (names.count(name) != 0) {
969                                 wroteSomething = true;
970                                 out << name << '\t' << tax << endl;
971                         }else{
972                                 //if you are not in the accnos file check if you are a name that needs to be changed
973                                 map<string, string>::iterator it = uniqueToRedundant.find(name);
974                                 if (it != uniqueToRedundant.end()) {
975                                         wroteSomething = true;
976                                         out << it->second << '\t' << tax << endl;
977                                 }
978                         }
979                         
980                         m->gobble(in);
981                 }
982                 in.close();
983                 out.close();
984                 
985                 if (wroteSomething == false) {  m->mothurOut("Your file does NOT contain sequences from the groups you wish to get."); m->mothurOutEndLine();  }
986                 outputTypes["taxonomy"].push_back(outputFileName); outputNames.push_back(outputFileName);
987                 
988                 return 0;
989         }
990         catch(exception& e) {
991                 m->errorOut(e, "GetGroupsCommand", "readTax");
992                 exit(1);
993         }
994 }
995 //**********************************************************************************************************************
996 int GetGroupsCommand::fillNames(){
997         try {
998                 vector<string> seqs = groupMap->getNamesSeqs();
999                 
1000                 for (int i = 0; i < seqs.size(); i++) {
1001                         
1002                         if (m->control_pressed) { return 0; }
1003                         
1004                         string group = groupMap->getGroup(seqs[i]);
1005                         
1006                         if (m->inUsersGroups(group, Groups)) {
1007                                 names.insert(seqs[i]);
1008                         }
1009                 }
1010                 
1011                 return 0;
1012         }
1013         catch(exception& e) {
1014                 m->errorOut(e, "GetGroupsCommand", "fillNames");
1015                 exit(1);
1016         }
1017 }
1018
1019 //**********************************************************************************************************************
1020
1021