]> git.donarmstrong.com Git - mothur.git/blob - getgroupscommand.cpp
finished shhh.seqs command, fixed bug with remove.groups and get.groups that caused...
[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") { abort = true; }
194                         else if (groupfile == "not found") {  
195                                 //if there is a current group file, use it
196                                 groupfile = m->getGroupFile(); 
197                                 if (groupfile != "") { m->mothurOut("Using " + groupfile + " as input file for the group parameter."); m->mothurOutEndLine(); }
198                                 else {  m->mothurOut("You have no current groupfile and the group parameter is required."); m->mothurOutEndLine(); abort = true; }
199                         }else { m->setGroupFile(groupfile); }   
200                         
201                         listfile = validParameter.validFile(parameters, "list", true);
202                         if (listfile == "not open") { abort = true; }
203                         else if (listfile == "not found") {  listfile = "";  }
204                         else { m->setListFile(listfile); }
205                         
206                         taxfile = validParameter.validFile(parameters, "taxonomy", true);
207                         if (taxfile == "not open") { abort = true; }
208                         else if (taxfile == "not found") {  taxfile = "";  }
209                         else { m->setTaxonomyFile(taxfile); }
210                         
211                         groups = validParameter.validFile(parameters, "groups", false);                 
212                         if (groups == "not found") { groups = ""; }
213                         else {
214                                 m->splitAtDash(groups, Groups);
215                                 m->setGroups(Groups);
216                         }
217                         
218                         sharedfile = validParameter.validFile(parameters, "shared", true);
219                         if (sharedfile == "not open") { sharedfile = ""; abort = true; }
220                         else if (sharedfile == "not found") {  sharedfile = "";  }
221                         else { m->setSharedFile(sharedfile); }
222                         
223                         groupfile = validParameter.validFile(parameters, "group", true);
224                         if (groupfile == "not open") { groupfile = ""; abort = true; }
225                         else if (groupfile == "not found") {    groupfile = ""; }
226                         else { m->setGroupFile(groupfile); }    
227                         
228                         if ((sharedfile == "") && (groupfile == "")) { 
229                                 //is there are current file available for any of these?
230                                 if ((namefile != "") || (fastafile != "") || (listfile != "") || (taxfile != "")) {
231                                         //give priority to group, then shared
232                                         groupfile = m->getGroupFile(); 
233                                         if (groupfile != "") {  m->mothurOut("Using " + groupfile + " as input file for the group parameter."); m->mothurOutEndLine(); }
234                                         else { 
235                                                 sharedfile = m->getSharedFile(); 
236                                                 if (sharedfile != "") { m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
237                                                 else { 
238                                                         m->mothurOut("You have no current groupfile or sharedfile and one is required."); m->mothurOutEndLine(); abort = true;
239                                                 }
240                                         }
241                                 }else {
242                                         //give priority to shared, then group
243                                         sharedfile = m->getSharedFile(); 
244                                         if (sharedfile != "") {  m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
245                                         else { 
246                                                 groupfile = m->getGroupFile(); 
247                                                 if (groupfile != "") { m->mothurOut("Using " + groupfile + " as input file for the group parameter."); m->mothurOutEndLine(); }
248                                                 else { 
249                                                         m->mothurOut("You have no current groupfile or sharedfile and one is required."); m->mothurOutEndLine(); abort = true;
250                                                 }
251                                         }
252                                 }
253                         }
254                         
255                         if ((accnosfile == "") && (Groups.size() == 0)) { m->mothurOut("You must provide an accnos file or specify groups using the groups parameter."); m->mothurOutEndLine(); abort = true; }
256                         
257                         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; }
258                         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; }
259
260                 }
261                 
262         }
263         catch(exception& e) {
264                 m->errorOut(e, "GetGroupsCommand", "GetGroupsCommand");
265                 exit(1);
266         }
267 }
268 //**********************************************************************************************************************
269
270 int GetGroupsCommand::execute(){
271         try {
272                 
273                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
274                 
275                 //get groups you want to remove
276                 if (accnosfile != "") { readAccnos(); }
277                 
278                 if (groupfile != "") {
279                         groupMap = new GroupMap(groupfile);
280                         groupMap->readMap();
281                         
282                         //make sure groups are valid
283                         //takes care of user setting groupNames that are invalid or setting groups=all
284                         SharedUtil* util = new SharedUtil();
285                         vector<string> gNamesOfGroups = groupMap->getNamesOfGroups();
286                         util->setGroups(Groups, gNamesOfGroups);
287                         groupMap->setNamesOfGroups(gNamesOfGroups);
288                         delete util;
289                         
290                         //fill names with names of sequences that are from the groups we want to remove 
291                         fillNames();
292                         
293                         delete groupMap;
294                 }
295                 
296                 if (m->control_pressed) { return 0; }
297                 
298                 //read through the correct file and output lines you want to keep
299                 if (namefile != "")                     {               readName();             }
300                 if (fastafile != "")            {               readFasta();    }
301                 if (groupfile != "")            {               readGroup();    }
302                 if (listfile != "")                     {               readList();             }
303                 if (taxfile != "")                      {               readTax();              }
304                 if (sharedfile != "")           {               readShared();   }
305                 
306                 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        m->mothurRemove(outputNames[i]); } return 0; }
307                 
308                 
309                 if (outputNames.size() != 0) {
310                         m->mothurOutEndLine();
311                         m->mothurOut("Output File names: "); m->mothurOutEndLine();
312                         for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
313                         m->mothurOutEndLine();
314                         
315                         //set fasta file as new current fastafile
316                         string current = "";
317                         itTypes = outputTypes.find("fasta");
318                         if (itTypes != outputTypes.end()) {
319                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
320                         }
321                         
322                         itTypes = outputTypes.find("name");
323                         if (itTypes != outputTypes.end()) {
324                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); }
325                         }
326                         
327                         itTypes = outputTypes.find("group");
328                         if (itTypes != outputTypes.end()) {
329                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setGroupFile(current); }
330                         }
331                         
332                         itTypes = outputTypes.find("list");
333                         if (itTypes != outputTypes.end()) {
334                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setListFile(current); }
335                         }
336                         
337                         itTypes = outputTypes.find("taxonomy");
338                         if (itTypes != outputTypes.end()) {
339                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setTaxonomyFile(current); }
340                         }
341                         
342                         itTypes = outputTypes.find("shared");
343                         if (itTypes != outputTypes.end()) {
344                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setSharedFile(current); }
345                         }
346                 }
347                 
348                 return 0;               
349         }
350         
351         catch(exception& e) {
352                 m->errorOut(e, "GetGroupsCommand", "execute");
353                 exit(1);
354         }
355 }
356
357 //**********************************************************************************************************************
358 int GetGroupsCommand::readFasta(){
359         try {
360                 string thisOutputDir = outputDir;
361                 if (outputDir == "") {  thisOutputDir += m->hasPath(fastafile);  }
362                 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(fastafile)) + "pick" + m->getExtension(fastafile);
363                 
364                 ofstream out;
365                 m->openOutputFile(outputFileName, out);
366                 
367                 ifstream in;
368                 m->openInputFile(fastafile, in);
369                 string name;
370                 
371                 bool wroteSomething = false;
372                 int selectedCount = 0;
373                 
374                 while(!in.eof()){
375                         if (m->control_pressed) { in.close();  out.close();  m->mothurRemove(outputFileName);  return 0; }
376                         
377                         Sequence currSeq(in);
378                         name = currSeq.getName();
379                         
380                         if (name != "") {
381                                 //if this name is in the accnos file
382                                 if (names.count(name) != 0) {
383                                         wroteSomething = true;
384                                         
385                                         currSeq.printSequence(out);
386                                         selectedCount++;
387                                 }else{
388                                         //if you are not in the accnos file check if you are a name that needs to be changed
389                                         map<string, string>::iterator it = uniqueToRedundant.find(name);
390                                         if (it != uniqueToRedundant.end()) {
391                                                 wroteSomething = true;
392                                                 currSeq.setName(it->second);
393                                                 currSeq.printSequence(out);
394                                                 selectedCount++;
395                                         }
396                                 }
397                         }
398                         m->gobble(in);
399                 }
400                 in.close();     
401                 out.close();
402                 
403                 if (wroteSomething == false) {  m->mothurOut("Your file does NOT contain sequences from the groups you wish to get."); m->mothurOutEndLine();  }
404                 outputTypes["fasta"].push_back(outputFileName);  outputNames.push_back(outputFileName);
405                 
406                 m->mothurOut("Selected " + toString(selectedCount) + " sequences from your fasta file."); m->mothurOutEndLine();
407
408                 
409                 return 0;
410                 
411         }
412         catch(exception& e) {
413                 m->errorOut(e, "GetGroupsCommand", "readFasta");
414                 exit(1);
415         }
416 }
417 //**********************************************************************************************************************
418 int GetGroupsCommand::readShared(){
419         try {
420                 string thisOutputDir = outputDir;
421                 if (outputDir == "") {  thisOutputDir += m->hasPath(sharedfile);  }
422                 
423                 InputData input(sharedfile, "sharedfile");
424                 vector<SharedRAbundVector*> lookup = input.getSharedRAbundVectors();
425                 
426                 bool wroteSomething = false;
427                 
428                 while(lookup[0] != NULL) {
429                         
430                         string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(sharedfile)) + lookup[0]->getLabel() + ".pick" + m->getExtension(sharedfile);
431                         ofstream out;
432                         m->openOutputFile(outputFileName, out);
433                         outputTypes["shared"].push_back(outputFileName);  outputNames.push_back(outputFileName);
434                         
435                         if (m->control_pressed) { out.close();  m->mothurRemove(outputFileName);  for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } return 0; }
436                         
437                         lookup[0]->printHeaders(out); 
438                         
439                         for (int i = 0; i < lookup.size(); i++) {
440                                 out << lookup[i]->getLabel() << '\t' << lookup[i]->getGroup() << '\t';
441                                 lookup[i]->print(out);
442                                 wroteSomething = true;
443                                 
444                         }                       
445                         
446                         //get next line to process
447                         //prevent memory leak
448                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
449                         lookup = input.getSharedRAbundVectors();
450                         
451                         out.close();
452                 }
453                 
454                 if (wroteSomething == false) {  m->mothurOut("Your file contains only the groups you wish to remove."); m->mothurOutEndLine();  }
455                 
456                 string groupsString = "";
457                 for (int i = 0; i < Groups.size()-1; i++) {     groupsString += Groups[i] + ", "; }
458                 groupsString += Groups[Groups.size()-1];
459                 
460                 m->mothurOut("Selected groups: " + groupsString + " from your shared file."); m->mothurOutEndLine();
461                 
462                 return 0;
463                 
464         }
465         catch(exception& e) {
466                 m->errorOut(e, "GetGroupsCommand", "readShared");
467                 exit(1);
468         }
469 }
470 //**********************************************************************************************************************
471 int GetGroupsCommand::readList(){
472         try {
473                 string thisOutputDir = outputDir;
474                 if (outputDir == "") {  thisOutputDir += m->hasPath(listfile);  }
475                 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(listfile)) + "pick" +  m->getExtension(listfile);
476                 
477                 ofstream out;
478                 m->openOutputFile(outputFileName, out);
479                 
480                 ifstream in;
481                 m->openInputFile(listfile, in);
482                 
483                 bool wroteSomething = false;
484                 int selectedCount = 0;
485                 
486                 while(!in.eof()){
487                         
488                         selectedCount = 0;
489                         
490                         //read in list vector
491                         ListVector list(in);
492                         
493                         //make a new list vector
494                         ListVector newList;
495                         newList.setLabel(list.getLabel());
496                         
497                         //for each bin
498                         for (int i = 0; i < list.getNumBins(); i++) {
499                                 if (m->control_pressed) { in.close();  out.close();  m->mothurRemove(outputFileName);  return 0; }
500                                 
501                                 //parse out names that are in accnos file
502                                 string binnames = list.get(i);
503                                 
504                                 string newNames = "";
505                                 while (binnames.find_first_of(',') != -1) { 
506                                         string name = binnames.substr(0,binnames.find_first_of(','));
507                                         binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
508                                         
509                                         //if that name is in the .accnos file, add it
510                                         if (names.count(name) != 0) {  newNames += name + ",";  selectedCount++;  }
511                                         else{
512                                                 //if you are not in the accnos file check if you are a name that needs to be changed
513                                                 map<string, string>::iterator it = uniqueToRedundant.find(name);
514                                                 if (it != uniqueToRedundant.end()) {
515                                                         newNames += it->second + ",";
516                                                         selectedCount++;
517                                                 }
518                                         }
519                                 }
520                                 
521                                 //get last name
522                                 if (names.count(binnames) != 0) {  newNames += binnames + ",";  selectedCount++;  }
523                                 else{
524                                         //if you are not in the accnos file check if you are a name that needs to be changed
525                                         map<string, string>::iterator it = uniqueToRedundant.find(binnames);
526                                         if (it != uniqueToRedundant.end()) {
527                                                 newNames += it->second + ",";
528                                                 selectedCount++;
529                                         }
530                                 }
531                                 
532                                 //if there are names in this bin add to new list
533                                 if (newNames != "") {  
534                                         newNames = newNames.substr(0, newNames.length()-1); //rip off extra comma
535                                         newList.push_back(newNames);    
536                                 }
537                         }
538                         
539                         //print new listvector
540                         if (newList.getNumBins() != 0) {
541                                 wroteSomething = true;
542                                 newList.print(out);
543                         }
544                         
545                         m->gobble(in);
546                 }
547                 in.close();     
548                 out.close();
549                 
550                 if (wroteSomething == false) {  m->mothurOut("Your file does NOT contain sequences from the groups you wish to get."); m->mothurOutEndLine();  }
551                 outputTypes["list"].push_back(outputFileName); outputNames.push_back(outputFileName);
552                 
553                 m->mothurOut("Selected " + toString(selectedCount) + " sequences from your list file."); m->mothurOutEndLine();
554                 
555                 return 0;
556                 
557         }
558         catch(exception& e) {
559                 m->errorOut(e, "GetGroupsCommand", "readList");
560                 exit(1);
561         }
562 }
563 //**********************************************************************************************************************
564 int GetGroupsCommand::readName(){
565         try {
566                 string thisOutputDir = outputDir;
567                 if (outputDir == "") {  thisOutputDir += m->hasPath(namefile);  }
568                 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(namefile)) + "pick" + m->getExtension(namefile);
569                 
570                 ofstream out;
571                 m->openOutputFile(outputFileName, out);
572                 
573                 ifstream in;
574                 m->openInputFile(namefile, in);
575                 string name, firstCol, secondCol;
576                 
577                 bool wroteSomething = false;
578                 int selectedCount = 0;
579                 
580                 while(!in.eof()){
581                         if (m->control_pressed) { in.close();  out.close();  m->mothurRemove(outputFileName);  return 0; }
582                         
583                         in >> firstCol;         m->gobble(in);          
584                         in >> secondCol;                        
585                         
586                         vector<string> parsedNames;
587                         m->splitAtComma(secondCol, parsedNames);
588                         
589                         vector<string> validSecond;  validSecond.clear();
590                         for (int i = 0; i < parsedNames.size(); i++) {
591                                 if (names.count(parsedNames[i]) != 0) {
592                                         validSecond.push_back(parsedNames[i]);
593                                 }
594                         }
595                         
596                         selectedCount += validSecond.size();
597                         
598                         //if the name in the first column is in the set then print it and any other names in second column also in set
599                         if (names.count(firstCol) != 0) {
600                                 
601                                 wroteSomething = true;
602                                 
603                                 out << firstCol << '\t';
604                                 
605                                 //you know you have at least one valid second since first column is valid
606                                 for (int i = 0; i < validSecond.size()-1; i++) {  out << validSecond[i] << ',';  }
607                                 out << validSecond[validSecond.size()-1] << endl;
608                                 
609                                 //make first name in set you come to first column and then add the remaining names to second column
610                         }else {
611                                 
612                                 //you want part of this row
613                                 if (validSecond.size() != 0) {
614                                         
615                                         wroteSomething = true;
616                                         
617                                         out << validSecond[0] << '\t';
618                                         
619                                         //you know you have at least one valid second since first column is valid
620                                         for (int i = 0; i < validSecond.size()-1; i++) {  out << validSecond[i] << ',';  }
621                                         out << validSecond[validSecond.size()-1] << endl;
622                                         uniqueToRedundant[firstCol] = validSecond[0];
623                                 }
624                         }
625                         
626                         m->gobble(in);
627                 }
628                 in.close();
629                 out.close();
630                 
631                 if (wroteSomething == false) {  m->mothurOut("Your file does NOT contain sequences from the groups you wish to get."); m->mothurOutEndLine();  }
632                 outputTypes["name"].push_back(outputFileName); outputNames.push_back(outputFileName);
633                 
634                 m->mothurOut("Selected " + toString(selectedCount) + " sequences from your name file."); m->mothurOutEndLine();
635
636                 return 0;
637         }
638         catch(exception& e) {
639                 m->errorOut(e, "GetGroupsCommand", "readName");
640                 exit(1);
641         }
642 }
643
644 //**********************************************************************************************************************
645 int GetGroupsCommand::readGroup(){
646         try {
647                 string thisOutputDir = outputDir;
648                 if (outputDir == "") {  thisOutputDir += m->hasPath(groupfile);  }
649                 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(groupfile)) + "pick" + m->getExtension(groupfile);
650                 
651                 ofstream out;
652                 m->openOutputFile(outputFileName, out);
653                 
654                 ifstream in;
655                 m->openInputFile(groupfile, in);
656                 string name, group;
657                 
658                 bool wroteSomething = false;
659                 int selectedCount = 0;
660                 
661                 while(!in.eof()){
662                         if (m->control_pressed) { in.close();  out.close();  m->mothurRemove(outputFileName);  return 0; }
663                         
664                         in >> name;                             //read from first column
665                         in >> group;                    //read from second column
666                         
667                         //if this name is in the accnos file
668                         if (names.count(name) != 0) {
669                                 wroteSomething = true;
670                                 out << name << '\t' << group << endl;
671                                 selectedCount++;
672                         }
673                         
674                         m->gobble(in);
675                 }
676                 in.close();
677                 out.close();
678                 
679                 if (wroteSomething == false) {  m->mothurOut("Your file does NOT contain sequences from the groups you wish to get."); m->mothurOutEndLine();  }
680                 outputTypes["group"].push_back(outputFileName); outputNames.push_back(outputFileName);
681                 
682                 m->mothurOut("Selected " + toString(selectedCount) + " sequences from your group file."); m->mothurOutEndLine();
683
684                 return 0;
685         }
686         catch(exception& e) {
687                 m->errorOut(e, "GetGroupsCommand", "readGroup");
688                 exit(1);
689         }
690 }
691 //**********************************************************************************************************************
692 int GetGroupsCommand::readTax(){
693         try {
694                 string thisOutputDir = outputDir;
695                 if (outputDir == "") {  thisOutputDir += m->hasPath(taxfile);  }
696                 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(taxfile)) + "pick" + m->getExtension(taxfile);
697                 ofstream out;
698                 m->openOutputFile(outputFileName, out);
699                 
700                 ifstream in;
701                 m->openInputFile(taxfile, in);
702                 string name, tax;
703                 
704                 bool wroteSomething = false;
705                 
706                 while(!in.eof()){
707                         if (m->control_pressed) { in.close();  out.close();  m->mothurRemove(outputFileName);  return 0; }
708                         
709                         in >> name;                             //read from first column
710                         in >> tax;                      //read from second column
711                         
712                         //if this name is in the accnos file
713                         if (names.count(name) != 0) {
714                                 wroteSomething = true;
715                                 out << name << '\t' << tax << endl;
716                         }else{
717                                 //if you are not in the accnos file check if you are a name that needs to be changed
718                                 map<string, string>::iterator it = uniqueToRedundant.find(name);
719                                 if (it != uniqueToRedundant.end()) {
720                                         wroteSomething = true;
721                                         out << it->second << '\t' << tax << endl;
722                                 }
723                         }
724                         
725                         m->gobble(in);
726                 }
727                 in.close();
728                 out.close();
729                 
730                 if (wroteSomething == false) {  m->mothurOut("Your file does NOT contain sequences from the groups you wish to get."); m->mothurOutEndLine();  }
731                 outputTypes["taxonomy"].push_back(outputFileName); outputNames.push_back(outputFileName);
732                 
733                 return 0;
734         }
735         catch(exception& e) {
736                 m->errorOut(e, "GetGroupsCommand", "readTax");
737                 exit(1);
738         }
739 }
740 //**********************************************************************************************************************
741 void GetGroupsCommand::readAccnos(){
742         try {
743                 Groups.clear();
744                 
745                 ifstream in;
746                 m->openInputFile(accnosfile, in);
747                 string name;
748                 
749                 while(!in.eof()){
750                         in >> name;
751                         
752                         Groups.push_back(name);
753                         
754                         m->gobble(in);
755                 }
756                 in.close();             
757                 
758                 m->setGroups(Groups);
759                 
760         }
761         catch(exception& e) {
762                 m->errorOut(e, "GetGroupsCommand", "readAccnos");
763                 exit(1);
764         }
765 }
766 //**********************************************************************************************************************
767 int GetGroupsCommand::fillNames(){
768         try {
769                 vector<string> seqs = groupMap->getNamesSeqs();
770                 
771                 for (int i = 0; i < seqs.size(); i++) {
772                         
773                         if (m->control_pressed) { return 0; }
774                         
775                         string group = groupMap->getGroup(seqs[i]);
776                         
777                         if (m->inUsersGroups(group, Groups)) {
778                                 names.insert(seqs[i]);
779                         }
780                 }
781                 
782                 return 0;
783         }
784         catch(exception& e) {
785                 m->errorOut(e, "GetGroupsCommand", "fillNames");
786                 exit(1);
787         }
788 }
789
790 //**********************************************************************************************************************
791
792