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