]> git.donarmstrong.com Git - mothur.git/blob - getgroupscommand.cpp
changed rarefaction.single output
[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", "FNGLT", "none",false,false); parameters.push_back(pfasta);
20                 CommandParameter pshared("shared", "InputTypes", "", "", "none", "FNGLT-sharedGroup", "none",false,false); parameters.push_back(pshared);
21                 CommandParameter pname("name", "InputTypes", "", "", "none", "FNGLT", "none",false,false); parameters.push_back(pname);
22                 CommandParameter pgroup("group", "InputTypes", "", "", "none", "FNGLT-sharedGroup", "none",false,false); parameters.push_back(pgroup);
23                 CommandParameter plist("list", "InputTypes", "", "", "none", "FNGLT", "none",false,false); parameters.push_back(plist);
24                 CommandParameter ptaxonomy("taxonomy", "InputTypes", "", "", "none", "FNGLT", "none",false,false); parameters.push_back(ptaxonomy);
25                 CommandParameter paccnos("accnos", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(paccnos);
26                 CommandParameter pgroups("groups", "String", "", "", "", "", "",false,false); parameters.push_back(pgroups);
27                 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
28                 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
29                 
30                 vector<string> myArray;
31                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
32                 return myArray;
33         }
34         catch(exception& e) {
35                 m->errorOut(e, "GetGroupsCommand", "setParameters");
36                 exit(1);
37         }
38 }
39 //**********************************************************************************************************************
40 string GetGroupsCommand::getHelpString(){       
41         try {
42                 string helpString = "";
43                 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 or shared file.\n";
44                 helpString += "It outputs a file containing the sequences in the those specified groups, or a sharedfile containing only those groups.\n";
45                 helpString += "The get.groups command parameters are accnos, fasta, name, group, list, taxonomy, shared and groups. The group parameter is required, unless you have a current group file, or are using a shared file.\n";
46                 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";
47                 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";
48                 helpString += "The get.groups command should be in the following format: get.groups(accnos=yourAccnos, fasta=yourFasta, group=yourGroupFile).\n";
49                 helpString += "Example get.groups(accnos=amazon.accnos, fasta=amazon.fasta, group=amazon.groups).\n";
50                 helpString += "or get.groups(groups=pasture, fasta=amazon.fasta, group=amazon.groups).\n";
51                 helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n";
52                 return helpString;
53         }
54         catch(exception& e) {
55                 m->errorOut(e, "GetGroupsCommand", "getHelpString");
56                 exit(1);
57         }
58 }
59 //**********************************************************************************************************************
60 GetGroupsCommand::GetGroupsCommand(){   
61         try {
62                 abort = true; calledHelp = true;
63                 setParameters();
64                 vector<string> tempOutNames;
65                 outputTypes["fasta"] = tempOutNames;
66                 outputTypes["taxonomy"] = tempOutNames;
67                 outputTypes["name"] = tempOutNames;
68                 outputTypes["group"] = tempOutNames;
69                 outputTypes["list"] = tempOutNames;
70                 outputTypes["shared"] = tempOutNames;
71         }
72         catch(exception& e) {
73                 m->errorOut(e, "GetGroupsCommand", "GetGroupsCommand");
74                 exit(1);
75         }
76 }
77 //**********************************************************************************************************************
78 GetGroupsCommand::GetGroupsCommand(string option)  {
79         try {
80                 abort = false; calledHelp = false;   
81                 
82                 //allow user to run help
83                 if(option == "help") { help(); abort = true; calledHelp = true; }
84                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
85                 
86                 else {
87                         vector<string> myArray = setParameters();
88                         
89                         OptionParser parser(option);
90                         map<string,string> parameters = parser.getParameters();
91                         
92                         ValidParameters validParameter;
93                         map<string,string>::iterator it;
94                         
95                         //check to make sure all parameters are valid for command
96                         for (it = parameters.begin(); it != parameters.end(); it++) { 
97                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
98                         }
99                         
100                         //initialize outputTypes
101                         vector<string> tempOutNames;
102                         outputTypes["fasta"] = tempOutNames;
103                         outputTypes["taxonomy"] = tempOutNames;
104                         outputTypes["name"] = tempOutNames;
105                         outputTypes["group"] = tempOutNames;
106                         outputTypes["list"] = tempOutNames;
107                         outputTypes["shared"] = tempOutNames;
108                         
109                         
110                         //if the user changes the output directory command factory will send this info to us in the output parameter 
111                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = "";         }
112                         
113                         //if the user changes the input directory command factory will send this info to us in the output parameter 
114                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
115                         if (inputDir == "not found"){   inputDir = "";          }
116                         else {
117                                 string path;
118                                 it = parameters.find("fasta");
119                                 //user has given a template file
120                                 if(it != parameters.end()){ 
121                                         path = m->hasPath(it->second);
122                                         //if the user has not given a path then, add inputdir. else leave path alone.
123                                         if (path == "") {       parameters["fasta"] = inputDir + it->second;            }
124                                 }
125                                 
126                                 it = parameters.find("accnos");
127                                 //user has given a template file
128                                 if(it != parameters.end()){ 
129                                         path = m->hasPath(it->second);
130                                         //if the user has not given a path then, add inputdir. else leave path alone.
131                                         if (path == "") {       parameters["accnos"] = inputDir + it->second;           }
132                                 }
133                                 
134                                 it = parameters.find("list");
135                                 //user has given a template file
136                                 if(it != parameters.end()){ 
137                                         path = m->hasPath(it->second);
138                                         //if the user has not given a path then, add inputdir. else leave path alone.
139                                         if (path == "") {       parameters["list"] = inputDir + it->second;             }
140                                 }
141                                 
142                                 it = parameters.find("name");
143                                 //user has given a template file
144                                 if(it != parameters.end()){ 
145                                         path = m->hasPath(it->second);
146                                         //if the user has not given a path then, add inputdir. else leave path alone.
147                                         if (path == "") {       parameters["name"] = inputDir + it->second;             }
148                                 }
149                                 
150                                 it = parameters.find("group");
151                                 //user has given a template file
152                                 if(it != parameters.end()){ 
153                                         path = m->hasPath(it->second);
154                                         //if the user has not given a path then, add inputdir. else leave path alone.
155                                         if (path == "") {       parameters["group"] = inputDir + it->second;            }
156                                 }
157                                 
158                                 it = parameters.find("taxonomy");
159                                 //user has given a template file
160                                 if(it != parameters.end()){ 
161                                         path = m->hasPath(it->second);
162                                         //if the user has not given a path then, add inputdir. else leave path alone.
163                                         if (path == "") {       parameters["taxonomy"] = inputDir + it->second;         }
164                                 }
165                                 
166                                 it = parameters.find("shared");
167                                 //user has given a template file
168                                 if(it != parameters.end()){ 
169                                         path = m->hasPath(it->second);
170                                         //if the user has not given a path then, add inputdir. else leave path alone.
171                                         if (path == "") {       parameters["shared"] = inputDir + it->second;           }
172                                 }
173                         }
174                         
175                         
176                         //check for required parameters
177                         accnosfile = validParameter.validFile(parameters, "accnos", true);
178                         if (accnosfile == "not open") { abort = true; }
179                         else if (accnosfile == "not found") {  accnosfile = ""; }
180                         else { m->setAccnosFile(accnosfile); }
181                         
182                         fastafile = validParameter.validFile(parameters, "fasta", true);
183                         if (fastafile == "not open") { abort = true; }
184                         else if (fastafile == "not found") {  fastafile = "";  }
185                         else { m->setFastaFile(fastafile); }
186                         
187                         namefile = validParameter.validFile(parameters, "name", true);
188                         if (namefile == "not open") { abort = true; }
189                         else if (namefile == "not found") {  namefile = "";  }  
190                         else { m->setNameFile(namefile); }
191                         
192                         groupfile = validParameter.validFile(parameters, "group", true);
193                         if (groupfile == "not open") { groupfile = ""; abort = true; }
194                         else if (groupfile == "not found") {  groupfile = "";                   }
195                         else { m->setGroupFile(groupfile); }    
196                         
197                         listfile = validParameter.validFile(parameters, "list", true);
198                         if (listfile == "not open") { abort = true; }
199                         else if (listfile == "not found") {  listfile = "";  }
200                         else { m->setListFile(listfile); }
201                         
202                         taxfile = validParameter.validFile(parameters, "taxonomy", true);
203                         if (taxfile == "not open") { abort = true; }
204                         else if (taxfile == "not found") {  taxfile = "";  }
205                         else { m->setTaxonomyFile(taxfile); }
206                         
207                         groups = validParameter.validFile(parameters, "groups", false);                 
208                         if (groups == "not found") { groups = ""; }
209                         else {
210                                 m->splitAtDash(groups, Groups);
211                                 m->setGroups(Groups);
212                         }
213                         
214                         sharedfile = validParameter.validFile(parameters, "shared", true);
215                         if (sharedfile == "not open") { sharedfile = ""; abort = true; }
216                         else if (sharedfile == "not found") {  sharedfile = "";  }
217                         else { m->setSharedFile(sharedfile); }
218                         
219                         groupfile = validParameter.validFile(parameters, "group", true);
220                         if (groupfile == "not open") { groupfile = ""; abort = true; }
221                         else if (groupfile == "not found") {    groupfile = ""; }
222                         else { m->setGroupFile(groupfile); }    
223                         
224                         if ((sharedfile == "") && (groupfile == "")) { 
225                                 //is there are current file available for any of these?
226                                 if ((namefile != "") || (fastafile != "") || (listfile != "") || (taxfile != "")) {
227                                         //give priority to group, then shared
228                                         groupfile = m->getGroupFile(); 
229                                         if (groupfile != "") {  m->mothurOut("Using " + groupfile + " as input file for the group parameter."); m->mothurOutEndLine(); }
230                                         else { 
231                                                 sharedfile = m->getSharedFile(); 
232                                                 if (sharedfile != "") { m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
233                                                 else { 
234                                                         m->mothurOut("You have no current groupfile or sharedfile and one is required."); m->mothurOutEndLine(); abort = true;
235                                                 }
236                                         }
237                                 }else {
238                                         //give priority to shared, then group
239                                         sharedfile = m->getSharedFile(); 
240                                         if (sharedfile != "") {  m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
241                                         else { 
242                                                 groupfile = m->getGroupFile(); 
243                                                 if (groupfile != "") { m->mothurOut("Using " + groupfile + " as input file for the group parameter."); m->mothurOutEndLine(); }
244                                                 else { 
245                                                         m->mothurOut("You have no current groupfile or sharedfile and one is required."); m->mothurOutEndLine(); abort = true;
246                                                 }
247                                         }
248                                 }
249                         }
250                         
251                         if ((accnosfile == "") && (Groups.size() == 0)) { m->mothurOut("You must provide an accnos file or specify groups using the groups parameter."); m->mothurOutEndLine(); abort = true; }
252                         
253                         if ((fastafile == "") && (namefile == "") && (groupfile == "")  && (sharedfile == "") && (listfile == "") && (taxfile == ""))  { m->mothurOut("You must provide at least one of the following: fasta, name, taxonomy, group, shared or list."); m->mothurOutEndLine(); abort = true; }
254                         if ((groupfile == "") && ((namefile != "") || (fastafile != "") || (listfile != "") || (taxfile != "")))  { m->mothurOut("If using a fasta, name, taxonomy, group or list, then you must provide a group file."); m->mothurOutEndLine(); abort = true; }
255
256                 }
257                 
258         }
259         catch(exception& e) {
260                 m->errorOut(e, "GetGroupsCommand", "GetGroupsCommand");
261                 exit(1);
262         }
263 }
264 //**********************************************************************************************************************
265
266 int GetGroupsCommand::execute(){
267         try {
268                 
269                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
270                 
271                 //get groups you want to remove
272                 if (accnosfile != "") { readAccnos(); }
273                 
274                 if (groupfile != "") {
275                         groupMap = new GroupMap(groupfile);
276                         groupMap->readMap();
277                         
278                         //make sure groups are valid
279                         //takes care of user setting groupNames that are invalid or setting groups=all
280                         SharedUtil* util = new SharedUtil();
281                         vector<string> gNamesOfGroups = groupMap->getNamesOfGroups();
282                         util->setGroups(Groups, gNamesOfGroups);
283                         groupMap->setNamesOfGroups(gNamesOfGroups);
284                         delete util;
285                         
286                         //fill names with names of sequences that are from the groups we want to remove 
287                         fillNames();
288                         
289                         delete groupMap;
290                 }
291                 
292                 if (m->control_pressed) { return 0; }
293                 
294                 //read through the correct file and output lines you want to keep
295                 if (namefile != "")                     {               readName();             }
296                 if (fastafile != "")            {               readFasta();    }
297                 if (groupfile != "")            {               readGroup();    }
298                 if (listfile != "")                     {               readList();             }
299                 if (taxfile != "")                      {               readTax();              }
300                 if (sharedfile != "")           {               readShared();   }
301                 
302                 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        m->mothurRemove(outputNames[i]); } return 0; }
303                 
304                 
305                 if (outputNames.size() != 0) {
306                         m->mothurOutEndLine();
307                         m->mothurOut("Output File names: "); m->mothurOutEndLine();
308                         for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
309                         m->mothurOutEndLine();
310                         
311                         //set fasta file as new current fastafile
312                         string current = "";
313                         itTypes = outputTypes.find("fasta");
314                         if (itTypes != outputTypes.end()) {
315                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
316                         }
317                         
318                         itTypes = outputTypes.find("name");
319                         if (itTypes != outputTypes.end()) {
320                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); }
321                         }
322                         
323                         itTypes = outputTypes.find("group");
324                         if (itTypes != outputTypes.end()) {
325                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setGroupFile(current); }
326                         }
327                         
328                         itTypes = outputTypes.find("list");
329                         if (itTypes != outputTypes.end()) {
330                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setListFile(current); }
331                         }
332                         
333                         itTypes = outputTypes.find("taxonomy");
334                         if (itTypes != outputTypes.end()) {
335                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setTaxonomyFile(current); }
336                         }
337                         
338                         itTypes = outputTypes.find("shared");
339                         if (itTypes != outputTypes.end()) {
340                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setSharedFile(current); }
341                         }
342                 }
343                 
344                 return 0;               
345         }
346         
347         catch(exception& e) {
348                 m->errorOut(e, "GetGroupsCommand", "execute");
349                 exit(1);
350         }
351 }
352
353 //**********************************************************************************************************************
354 int GetGroupsCommand::readFasta(){
355         try {
356                 string thisOutputDir = outputDir;
357                 if (outputDir == "") {  thisOutputDir += m->hasPath(fastafile);  }
358                 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(fastafile)) + "pick" + m->getExtension(fastafile);
359                 
360                 ofstream out;
361                 m->openOutputFile(outputFileName, out);
362                 
363                 ifstream in;
364                 m->openInputFile(fastafile, in);
365                 string name;
366                 
367                 bool wroteSomething = false;
368                 int selectedCount = 0;
369                 
370                 while(!in.eof()){
371                         if (m->control_pressed) { in.close();  out.close();  m->mothurRemove(outputFileName);  return 0; }
372                         
373                         Sequence currSeq(in);
374                         name = currSeq.getName();
375                         
376                         if (name != "") {
377                                 //if this name is in the accnos file
378                                 if (names.count(name) != 0) {
379                                         wroteSomething = true;
380                                         
381                                         currSeq.printSequence(out);
382                                         selectedCount++;
383                                 }else{
384                                         //if you are not in the accnos file check if you are a name that needs to be changed
385                                         map<string, string>::iterator it = uniqueToRedundant.find(name);
386                                         if (it != uniqueToRedundant.end()) {
387                                                 wroteSomething = true;
388                                                 currSeq.setName(it->second);
389                                                 currSeq.printSequence(out);
390                                                 selectedCount++;
391                                         }
392                                 }
393                         }
394                         m->gobble(in);
395                 }
396                 in.close();     
397                 out.close();
398                 
399                 if (wroteSomething == false) {  m->mothurOut("Your file does NOT contain sequences from the groups you wish to get."); m->mothurOutEndLine();  }
400                 outputTypes["fasta"].push_back(outputFileName);  outputNames.push_back(outputFileName);
401                 
402                 m->mothurOut("Selected " + toString(selectedCount) + " sequences from your fasta file."); m->mothurOutEndLine();
403
404                 
405                 return 0;
406                 
407         }
408         catch(exception& e) {
409                 m->errorOut(e, "GetGroupsCommand", "readFasta");
410                 exit(1);
411         }
412 }
413 //**********************************************************************************************************************
414 int GetGroupsCommand::readShared(){
415         try {
416                 string thisOutputDir = outputDir;
417                 if (outputDir == "") {  thisOutputDir += m->hasPath(sharedfile);  }
418                 
419                 InputData input(sharedfile, "sharedfile");
420                 vector<SharedRAbundVector*> lookup = input.getSharedRAbundVectors();
421                 
422                 bool wroteSomething = false;
423                 
424                 while(lookup[0] != NULL) {
425                         
426                         string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(sharedfile)) + lookup[0]->getLabel() + ".pick" + m->getExtension(sharedfile);
427                         ofstream out;
428                         m->openOutputFile(outputFileName, out);
429                         outputTypes["shared"].push_back(outputFileName);  outputNames.push_back(outputFileName);
430                         
431                         if (m->control_pressed) { out.close();  m->mothurRemove(outputFileName);  for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } return 0; }
432                         
433                         lookup[0]->printHeaders(out); 
434                         
435                         for (int i = 0; i < lookup.size(); i++) {
436                                 out << lookup[i]->getLabel() << '\t' << lookup[i]->getGroup() << '\t';
437                                 lookup[i]->print(out);
438                                 wroteSomething = true;
439                                 
440                         }                       
441                         
442                         //get next line to process
443                         //prevent memory leak
444                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
445                         lookup = input.getSharedRAbundVectors();
446                         
447                         out.close();
448                 }
449                 
450                 if (wroteSomething == false) {  m->mothurOut("Your file contains only the groups you wish to remove."); m->mothurOutEndLine();  }
451                 
452                 string groupsString = "";
453                 for (int i = 0; i < Groups.size()-1; i++) {     groupsString += Groups[i] + ", "; }
454                 groupsString += Groups[Groups.size()-1];
455                 
456                 m->mothurOut("Selected groups: " + groupsString + " from your shared file."); m->mothurOutEndLine();
457                 
458                 return 0;
459                 
460         }
461         catch(exception& e) {
462                 m->errorOut(e, "GetGroupsCommand", "readShared");
463                 exit(1);
464         }
465 }
466 //**********************************************************************************************************************
467 int GetGroupsCommand::readList(){
468         try {
469                 string thisOutputDir = outputDir;
470                 if (outputDir == "") {  thisOutputDir += m->hasPath(listfile);  }
471                 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(listfile)) + "pick" +  m->getExtension(listfile);
472                 
473                 ofstream out;
474                 m->openOutputFile(outputFileName, out);
475                 
476                 ifstream in;
477                 m->openInputFile(listfile, in);
478                 
479                 bool wroteSomething = false;
480                 int selectedCount = 0;
481                 
482                 while(!in.eof()){
483                         
484                         selectedCount = 0;
485                         
486                         //read in list vector
487                         ListVector list(in);
488                         
489                         //make a new list vector
490                         ListVector newList;
491                         newList.setLabel(list.getLabel());
492                         
493                         //for each bin
494                         for (int i = 0; i < list.getNumBins(); i++) {
495                                 if (m->control_pressed) { in.close();  out.close();  m->mothurRemove(outputFileName);  return 0; }
496                                 
497                                 //parse out names that are in accnos file
498                                 string binnames = list.get(i);
499                                 
500                                 string newNames = "";
501                                 while (binnames.find_first_of(',') != -1) { 
502                                         string name = binnames.substr(0,binnames.find_first_of(','));
503                                         binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
504                                         
505                                         //if that name is in the .accnos file, add it
506                                         if (names.count(name) != 0) {  newNames += name + ",";  selectedCount++;  }
507                                         else{
508                                                 //if you are not in the accnos file check if you are a name that needs to be changed
509                                                 map<string, string>::iterator it = uniqueToRedundant.find(name);
510                                                 if (it != uniqueToRedundant.end()) {
511                                                         newNames += it->second + ",";
512                                                         selectedCount++;
513                                                 }
514                                         }
515                                 }
516                                 
517                                 //get last name
518                                 if (names.count(binnames) != 0) {  newNames += binnames + ",";  selectedCount++;  }
519                                 else{
520                                         //if you are not in the accnos file check if you are a name that needs to be changed
521                                         map<string, string>::iterator it = uniqueToRedundant.find(binnames);
522                                         if (it != uniqueToRedundant.end()) {
523                                                 newNames += it->second + ",";
524                                                 selectedCount++;
525                                         }
526                                 }
527                                 
528                                 //if there are names in this bin add to new list
529                                 if (newNames != "") {  
530                                         newNames = newNames.substr(0, newNames.length()-1); //rip off extra comma
531                                         newList.push_back(newNames);    
532                                 }
533                         }
534                         
535                         //print new listvector
536                         if (newList.getNumBins() != 0) {
537                                 wroteSomething = true;
538                                 newList.print(out);
539                         }
540                         
541                         m->gobble(in);
542                 }
543                 in.close();     
544                 out.close();
545                 
546                 if (wroteSomething == false) {  m->mothurOut("Your file does NOT contain sequences from the groups you wish to get."); m->mothurOutEndLine();  }
547                 outputTypes["list"].push_back(outputFileName); outputNames.push_back(outputFileName);
548                 
549                 m->mothurOut("Selected " + toString(selectedCount) + " sequences from your list file."); m->mothurOutEndLine();
550                 
551                 return 0;
552                 
553         }
554         catch(exception& e) {
555                 m->errorOut(e, "GetGroupsCommand", "readList");
556                 exit(1);
557         }
558 }
559 //**********************************************************************************************************************
560 int GetGroupsCommand::readName(){
561         try {
562                 string thisOutputDir = outputDir;
563                 if (outputDir == "") {  thisOutputDir += m->hasPath(namefile);  }
564                 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(namefile)) + "pick" + m->getExtension(namefile);
565                 
566                 ofstream out;
567                 m->openOutputFile(outputFileName, out);
568                 
569                 ifstream in;
570                 m->openInputFile(namefile, in);
571                 string name, firstCol, secondCol;
572                 
573                 bool wroteSomething = false;
574                 int selectedCount = 0;
575                 
576                 while(!in.eof()){
577                         if (m->control_pressed) { in.close();  out.close();  m->mothurRemove(outputFileName);  return 0; }
578                         
579                         in >> firstCol;         m->gobble(in);          
580                         in >> secondCol;                        
581                         
582                         vector<string> parsedNames;
583                         m->splitAtComma(secondCol, parsedNames);
584                         
585                         vector<string> validSecond;  validSecond.clear();
586                         for (int i = 0; i < parsedNames.size(); i++) {
587                                 if (names.count(parsedNames[i]) != 0) {
588                                         validSecond.push_back(parsedNames[i]);
589                                 }
590                         }
591                         
592                         selectedCount += validSecond.size();
593                         
594                         //if the name in the first column is in the set then print it and any other names in second column also in set
595                         if (names.count(firstCol) != 0) {
596                                 
597                                 wroteSomething = true;
598                                 
599                                 out << firstCol << '\t';
600                                 
601                                 //you know you have at least one valid second since first column is valid
602                                 for (int i = 0; i < validSecond.size()-1; i++) {  out << validSecond[i] << ',';  }
603                                 out << validSecond[validSecond.size()-1] << endl;
604                                 
605                                 //make first name in set you come to first column and then add the remaining names to second column
606                         }else {
607                                 
608                                 //you want part of this row
609                                 if (validSecond.size() != 0) {
610                                         
611                                         wroteSomething = true;
612                                         
613                                         out << validSecond[0] << '\t';
614                                         
615                                         //you know you have at least one valid second since first column is valid
616                                         for (int i = 0; i < validSecond.size()-1; i++) {  out << validSecond[i] << ',';  }
617                                         out << validSecond[validSecond.size()-1] << endl;
618                                         uniqueToRedundant[firstCol] = validSecond[0];
619                                 }
620                         }
621                         
622                         m->gobble(in);
623                 }
624                 in.close();
625                 out.close();
626                 
627                 if (wroteSomething == false) {  m->mothurOut("Your file does NOT contain sequences from the groups you wish to get."); m->mothurOutEndLine();  }
628                 outputTypes["name"].push_back(outputFileName); outputNames.push_back(outputFileName);
629                 
630                 m->mothurOut("Selected " + toString(selectedCount) + " sequences from your name file."); m->mothurOutEndLine();
631
632                 return 0;
633         }
634         catch(exception& e) {
635                 m->errorOut(e, "GetGroupsCommand", "readName");
636                 exit(1);
637         }
638 }
639
640 //**********************************************************************************************************************
641 int GetGroupsCommand::readGroup(){
642         try {
643                 string thisOutputDir = outputDir;
644                 if (outputDir == "") {  thisOutputDir += m->hasPath(groupfile);  }
645                 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(groupfile)) + "pick" + m->getExtension(groupfile);
646                 
647                 ofstream out;
648                 m->openOutputFile(outputFileName, out);
649                 
650                 ifstream in;
651                 m->openInputFile(groupfile, in);
652                 string name, group;
653                 
654                 bool wroteSomething = false;
655                 int selectedCount = 0;
656                 
657                 while(!in.eof()){
658                         if (m->control_pressed) { in.close();  out.close();  m->mothurRemove(outputFileName);  return 0; }
659                         
660                         in >> name;                             //read from first column
661                         in >> group;                    //read from second column
662                         
663                         //if this name is in the accnos file
664                         if (names.count(name) != 0) {
665                                 wroteSomething = true;
666                                 out << name << '\t' << group << endl;
667                                 selectedCount++;
668                         }
669                         
670                         m->gobble(in);
671                 }
672                 in.close();
673                 out.close();
674                 
675                 if (wroteSomething == false) {  m->mothurOut("Your file does NOT contain sequences from the groups you wish to get."); m->mothurOutEndLine();  }
676                 outputTypes["group"].push_back(outputFileName); outputNames.push_back(outputFileName);
677                 
678                 m->mothurOut("Selected " + toString(selectedCount) + " sequences from your group file."); m->mothurOutEndLine();
679
680                 return 0;
681         }
682         catch(exception& e) {
683                 m->errorOut(e, "GetGroupsCommand", "readGroup");
684                 exit(1);
685         }
686 }
687 //**********************************************************************************************************************
688 int GetGroupsCommand::readTax(){
689         try {
690                 string thisOutputDir = outputDir;
691                 if (outputDir == "") {  thisOutputDir += m->hasPath(taxfile);  }
692                 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(taxfile)) + "pick" + m->getExtension(taxfile);
693                 ofstream out;
694                 m->openOutputFile(outputFileName, out);
695                 
696                 ifstream in;
697                 m->openInputFile(taxfile, in);
698                 string name, tax;
699                 
700                 bool wroteSomething = false;
701                 
702                 while(!in.eof()){
703                         if (m->control_pressed) { in.close();  out.close();  m->mothurRemove(outputFileName);  return 0; }
704                         
705                         in >> name;                             //read from first column
706                         in >> tax;                      //read from second column
707                         
708                         //if this name is in the accnos file
709                         if (names.count(name) != 0) {
710                                 wroteSomething = true;
711                                 out << name << '\t' << tax << endl;
712                         }else{
713                                 //if you are not in the accnos file check if you are a name that needs to be changed
714                                 map<string, string>::iterator it = uniqueToRedundant.find(name);
715                                 if (it != uniqueToRedundant.end()) {
716                                         wroteSomething = true;
717                                         out << it->second << '\t' << tax << endl;
718                                 }
719                         }
720                         
721                         m->gobble(in);
722                 }
723                 in.close();
724                 out.close();
725                 
726                 if (wroteSomething == false) {  m->mothurOut("Your file does NOT contain sequences from the groups you wish to get."); m->mothurOutEndLine();  }
727                 outputTypes["taxonomy"].push_back(outputFileName); outputNames.push_back(outputFileName);
728                 
729                 return 0;
730         }
731         catch(exception& e) {
732                 m->errorOut(e, "GetGroupsCommand", "readTax");
733                 exit(1);
734         }
735 }
736 //**********************************************************************************************************************
737 void GetGroupsCommand::readAccnos(){
738         try {
739                 Groups.clear();
740                 
741                 ifstream in;
742                 m->openInputFile(accnosfile, in);
743                 string name;
744                 
745                 while(!in.eof()){
746                         in >> name;
747                         
748                         Groups.push_back(name);
749                         
750                         m->gobble(in);
751                 }
752                 in.close();             
753                 
754                 m->setGroups(Groups);
755                 
756         }
757         catch(exception& e) {
758                 m->errorOut(e, "GetGroupsCommand", "readAccnos");
759                 exit(1);
760         }
761 }
762 //**********************************************************************************************************************
763 int GetGroupsCommand::fillNames(){
764         try {
765                 vector<string> seqs = groupMap->getNamesSeqs();
766                 
767                 for (int i = 0; i < seqs.size(); i++) {
768                         
769                         if (m->control_pressed) { return 0; }
770                         
771                         string group = groupMap->getGroup(seqs[i]);
772                         
773                         if (m->inUsersGroups(group, Groups)) {
774                                 names.insert(seqs[i]);
775                         }
776                 }
777                 
778                 return 0;
779         }
780         catch(exception& e) {
781                 m->errorOut(e, "GetGroupsCommand", "fillNames");
782                 exit(1);
783         }
784 }
785
786 //**********************************************************************************************************************
787
788