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