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