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