]> git.donarmstrong.com Git - mothur.git/blob - removegroupscommand.cpp
Merge remote-tracking branch 'mothur/master'
[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->names.clear();
469                 m->saveNextLabel = "";
470                 m->printedHeaders = false;
471                 m->currentBinLabels.clear();
472                 m->binLabelsInFile.clear();
473                 
474                 InputData input(sharedfile, "sharedfile");
475                 lookup = input.getSharedRAbundVectors();
476
477                 bool wroteSomething = false;
478                 
479                 while(lookup[0] != NULL) {
480                         
481                         string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(sharedfile)) + lookup[0]->getLabel() + ".pick" + m->getExtension(sharedfile);
482                         ofstream out;
483                         m->openOutputFile(outputFileName, out);
484                         outputTypes["shared"].push_back(outputFileName);  outputNames.push_back(outputFileName);
485                         
486                         if (m->control_pressed) { out.close();  m->mothurRemove(outputFileName);  for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } return 0; }
487                         
488                         lookup[0]->printHeaders(out); 
489                         
490                         for (int i = 0; i < lookup.size(); i++) {
491                                 out << lookup[i]->getLabel() << '\t' << lookup[i]->getGroup() << '\t';
492                                 lookup[i]->print(out);
493                                 wroteSomething = true;
494                                 
495                         }                       
496                         
497                         //get next line to process
498                         //prevent memory leak
499                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
500                         lookup = input.getSharedRAbundVectors();
501                         
502                         out.close();
503                 }
504                 
505                 
506                 m->setGroups(mothurOutGroups);
507                 
508                 if (wroteSomething == false) {  m->mothurOut("Your file contains only the groups you wish to remove."); m->mothurOutEndLine();  }
509                 
510                 string groupsString = "";
511                 for (int i = 0; i < Groups.size()-1; i++) {     groupsString += Groups[i] + ", "; }
512                 groupsString += Groups[Groups.size()-1];
513                 
514                 m->mothurOut("Removed groups: " + groupsString + " from your shared file."); m->mothurOutEndLine();
515                 
516                 return 0;
517                 
518         }
519         catch(exception& e) {
520                 m->errorOut(e, "RemoveGroupsCommand", "readShared");
521                 exit(1);
522         }
523 }
524 //**********************************************************************************************************************
525 int RemoveGroupsCommand::readList(){
526         try {
527                 string thisOutputDir = outputDir;
528                 if (outputDir == "") {  thisOutputDir += m->hasPath(listfile);  }
529                 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(listfile)) + "pick" +  m->getExtension(listfile);
530                 
531                 ofstream out;
532                 m->openOutputFile(outputFileName, out);
533                 
534                 ifstream in;
535                 m->openInputFile(listfile, in);
536                 
537                 bool wroteSomething = false;
538                 int removedCount = 0;
539                 
540                 while(!in.eof()){
541                         
542                         removedCount = 0;
543                         
544                         //read in list vector
545                         ListVector list(in);
546                         
547                         //make a new list vector
548                         ListVector newList;
549                         newList.setLabel(list.getLabel());
550                         
551                         //for each bin
552                         for (int i = 0; i < list.getNumBins(); i++) {
553                                 if (m->control_pressed) { in.close();  out.close();  m->mothurRemove(outputFileName);  return 0; }
554                                 
555                                 //parse out names that are in accnos file
556                                 string binnames = list.get(i);
557                                 
558                                 string newNames = "";
559                                 while (binnames.find_first_of(',') != -1) { 
560                                         string name = binnames.substr(0,binnames.find_first_of(','));
561                                         binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
562                                         
563                                         //if that name is in the .accnos file, add it
564                                         if (names.count(name) == 0) {  newNames += name + ",";  }
565                                         else {
566                                                 //if you are not in the accnos file check if you are a name that needs to be changed
567                                                 map<string, string>::iterator it = uniqueToRedundant.find(name);
568                                                 if (it != uniqueToRedundant.end()) {
569                                                         newNames += it->second + ",";
570                                                 }else { removedCount++; }
571                                         }
572                                 }
573                                 
574                                 //get last name
575                                 if (names.count(binnames) == 0) {  newNames += binnames + ",";  }
576                                 else { //if you are not in the accnos file check if you are a name that needs to be changed
577                                         map<string, string>::iterator it = uniqueToRedundant.find(binnames);
578                                         if (it != uniqueToRedundant.end()) {
579                                                 newNames += it->second + ",";
580                                         }else { removedCount++; }
581                                 }
582                                 
583                                 //if there are names in this bin add to new list
584                                 if (newNames != "") {  
585                                         newNames = newNames.substr(0, newNames.length()-1); //rip off extra comma
586                                         newList.push_back(newNames);    
587                                 }
588                         }
589                         
590                         //print new listvector
591                         if (newList.getNumBins() != 0) {
592                                 wroteSomething = true;
593                                 newList.print(out);
594                         }
595                         
596                         m->gobble(in);
597                 }
598                 in.close();     
599                 out.close();
600                 
601                 if (wroteSomething == false) {  m->mothurOut("Your file contains only sequences from the groups you wish to remove."); m->mothurOutEndLine();  }
602                 outputTypes["list"].push_back(outputFileName); outputNames.push_back(outputFileName);
603                 
604                 m->mothurOut("Removed " + toString(removedCount) + " sequences from your list file."); m->mothurOutEndLine();
605                 
606                 return 0;
607                 
608         }
609         catch(exception& e) {
610                 m->errorOut(e, "RemoveGroupsCommand", "readList");
611                 exit(1);
612         }
613 }
614 //**********************************************************************************************************************
615 int RemoveGroupsCommand::readName(){
616         try {
617                 string thisOutputDir = outputDir;
618                 if (outputDir == "") {  thisOutputDir += m->hasPath(namefile);  }
619                 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(namefile)) + "pick" + m->getExtension(namefile);
620                 
621                 ofstream out;
622                 m->openOutputFile(outputFileName, out);
623                 
624                 ifstream in;
625                 m->openInputFile(namefile, in);
626                 string name, firstCol, secondCol;
627                 
628                 bool wroteSomething = false;
629                 int removedCount = 0;
630                 
631                 while(!in.eof()){
632                         if (m->control_pressed) { in.close();  out.close();  m->mothurRemove(outputFileName);  return 0; }
633                         
634                         in >> firstCol;         m->gobble(in);          
635                         in >> secondCol;                        
636                         
637                         vector<string> parsedNames;
638                         m->splitAtComma(secondCol, parsedNames);
639                                                 
640                         vector<string> validSecond;  validSecond.clear();
641                         for (int i = 0; i < parsedNames.size(); i++) {
642                                 if (names.count(parsedNames[i]) == 0) {
643                                         validSecond.push_back(parsedNames[i]);
644                                 }
645                         }
646                         
647                         removedCount += parsedNames.size()-validSecond.size();
648                         
649                         //if the name in the first column is in the set then print it and any other names in second column also in set
650                         if (names.count(firstCol) == 0) {
651                                 
652                                 wroteSomething = true;
653                                 
654                                 out << firstCol << '\t';
655                                 
656                                 //you know you have at least one valid second since first column is valid
657                                 for (int i = 0; i < validSecond.size()-1; i++) {  out << validSecond[i] << ',';  }
658                                 out << validSecond[validSecond.size()-1] << endl;
659                                 
660                                 //make first name in set you come to first column and then add the remaining names to second column
661                         }else {
662                                 
663                                 //you want part of this row
664                                 if (validSecond.size() != 0) {
665                                         
666                                         wroteSomething = true;
667                                         
668                                         out << validSecond[0] << '\t';
669                                         
670                                         //you know you have at least one valid second since first column is valid
671                                         for (int i = 0; i < validSecond.size()-1; i++) {  out << validSecond[i] << ',';  }
672                                         out << validSecond[validSecond.size()-1] << endl;
673                                         uniqueToRedundant[firstCol] = validSecond[0];
674                                 }
675                         }
676                         
677                         m->gobble(in);
678                 }
679                 in.close();
680                 out.close();
681                 
682                 if (wroteSomething == false) {  m->mothurOut("Your file contains only sequences from the groups you wish to remove."); m->mothurOutEndLine();  }
683                 outputTypes["name"].push_back(outputFileName); outputNames.push_back(outputFileName);
684                 
685                 m->mothurOut("Removed " + toString(removedCount) + " sequences from your name file."); m->mothurOutEndLine();
686                 
687                 return 0;
688         }
689         catch(exception& e) {
690                 m->errorOut(e, "RemoveGroupsCommand", "readName");
691                 exit(1);
692         }
693 }
694
695 //**********************************************************************************************************************
696 int RemoveGroupsCommand::readGroup(){
697         try {
698                 string thisOutputDir = outputDir;
699                 if (outputDir == "") {  thisOutputDir += m->hasPath(groupfile);  }
700                 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(groupfile)) + "pick" + m->getExtension(groupfile);
701                 
702                 ofstream out;
703                 m->openOutputFile(outputFileName, out);
704                 
705                 ifstream in;
706                 m->openInputFile(groupfile, in);
707                 string name, group;
708                 
709                 bool wroteSomething = false;
710                 int removedCount = 0;
711                 
712                 while(!in.eof()){
713                         if (m->control_pressed) { in.close();  out.close();  m->mothurRemove(outputFileName);  return 0; }
714                         
715                         in >> name;                             //read from first column
716                         in >> group;                    //read from second column
717                         
718                         //if this name is in the accnos file
719                         if (names.count(name) == 0) {
720                                 wroteSomething = true;
721                                 out << name << '\t' << group << endl;
722                         }else {  removedCount++;  }
723                         
724                         m->gobble(in);
725                 }
726                 in.close();
727                 out.close();
728                 
729                 if (wroteSomething == false) {  m->mothurOut("Your file contains only sequences from the groups you wish to remove."); m->mothurOutEndLine();  }
730                 outputTypes["group"].push_back(outputFileName); outputNames.push_back(outputFileName);
731                 
732                 m->mothurOut("Removed " + toString(removedCount) + " sequences from your group file."); m->mothurOutEndLine();
733
734                 
735                 return 0;
736         }
737         catch(exception& e) {
738                 m->errorOut(e, "RemoveGroupsCommand", "readGroup");
739                 exit(1);
740         }
741 }
742 //**********************************************************************************************************************
743 int RemoveGroupsCommand::readDesign(){
744         try {
745                 string thisOutputDir = outputDir;
746                 if (outputDir == "") {  thisOutputDir += m->hasPath(designfile);  }
747                 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(designfile)) + "pick" + m->getExtension(designfile);
748                 
749                 ofstream out;
750                 m->openOutputFile(outputFileName, out);
751                 
752                 ifstream in;
753                 m->openInputFile(designfile, in);
754                 string name, group;
755                 
756                 bool wroteSomething = false;
757                 int removedCount = 0;
758                 
759                 while(!in.eof()){
760                         if (m->control_pressed) { in.close();  out.close();  m->mothurRemove(outputFileName);  return 0; }
761                         
762                         in >> name;                             //read from first column
763                         in >> group;                    //read from second column
764                         
765                         //if this name is in the accnos file
766                         if (!(m->inUsersGroups(name, Groups))) {
767                                 wroteSomething = true;
768                                 out << name << '\t' << group << endl;
769                         }else {  removedCount++;  }
770                         
771                         m->gobble(in);
772                 }
773                 in.close();
774                 out.close();
775                 
776                 if (wroteSomething == false) {  m->mothurOut("Your file contains only groups from the groups you wish to remove."); m->mothurOutEndLine();  }
777                 outputTypes["design"].push_back(outputFileName); outputNames.push_back(outputFileName);
778                 
779                 m->mothurOut("Removed " + toString(removedCount) + " groups from your design file."); m->mothurOutEndLine();
780         
781                 
782                 return 0;
783         }
784         catch(exception& e) {
785                 m->errorOut(e, "RemoveGroupsCommand", "readDesign");
786                 exit(1);
787         }
788 }
789
790 //**********************************************************************************************************************
791 int RemoveGroupsCommand::readTax(){
792         try {
793                 string thisOutputDir = outputDir;
794                 if (outputDir == "") {  thisOutputDir += m->hasPath(taxfile);  }
795                 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(taxfile)) + "pick" + m->getExtension(taxfile);
796                 ofstream out;
797                 m->openOutputFile(outputFileName, out);
798                 
799                 ifstream in;
800                 m->openInputFile(taxfile, in);
801                 string name, tax;
802                 
803                 bool wroteSomething = false;
804                 int removedCount = 0;
805                 
806                 while(!in.eof()){
807                         if (m->control_pressed) { in.close();  out.close();  m->mothurRemove(outputFileName);  return 0; }
808                         
809                         in >> name;                             //read from first column
810                         in >> tax;                      //read from second column
811                         
812                         //if this name is in the accnos file
813                         if (names.count(name) == 0) {
814                                 wroteSomething = true;
815                                 out << name << '\t' << tax << endl;
816                         }else {  //if you are not in the accnos file check if you are a name that needs to be changed
817                                 map<string, string>::iterator it = uniqueToRedundant.find(name);
818                                 if (it != uniqueToRedundant.end()) {
819                                         wroteSomething = true;
820                                         out << it->second << '\t' << tax << endl;
821                                 }else { removedCount++; }  }
822                         
823                         m->gobble(in);
824                 }
825                 in.close();
826                 out.close();
827                 
828                 if (wroteSomething == false) {  m->mothurOut("Your file contains only sequences from the groups you wish to remove."); m->mothurOutEndLine();  }
829                 outputTypes["taxonomy"].push_back(outputFileName); outputNames.push_back(outputFileName);
830                 
831                 m->mothurOut("Removed " + toString(removedCount) + " sequences from your taxonomy file."); m->mothurOutEndLine();
832                 
833                 return 0;
834         }
835         catch(exception& e) {
836                 m->errorOut(e, "RemoveGroupsCommand", "readTax");
837                 exit(1);
838         }
839 }
840 //**********************************************************************************************************************
841 void RemoveGroupsCommand::readAccnos(){
842         try {
843                 Groups.clear();
844                 
845                 ifstream in;
846                 m->openInputFile(accnosfile, in);
847                 string name;
848                 
849                 while(!in.eof()){
850                         in >> name;
851                         
852                         Groups.push_back(name);
853                         
854                         m->gobble(in);
855                 }
856                 in.close();     
857                 
858                 m->setGroups(Groups);
859                 
860         }
861         catch(exception& e) {
862                 m->errorOut(e, "RemoveGroupsCommand", "readAccnos");
863                 exit(1);
864         }
865 }
866 //**********************************************************************************************************************
867 int RemoveGroupsCommand::fillNames(){
868         try {
869                 vector<string> seqs = groupMap->getNamesSeqs();
870                 
871                 for (int i = 0; i < seqs.size(); i++) {
872                         
873                         if (m->control_pressed) { return 0; }
874                         
875                         string group = groupMap->getGroup(seqs[i]);
876                         
877                         if (m->inUsersGroups(group, Groups)) {
878                                 names.insert(seqs[i]);
879                         }
880                 }
881                 
882                 return 0;
883         }
884         catch(exception& e) {
885                 m->errorOut(e, "RemoveGroupsCommand", "fillNames");
886                 exit(1);
887         }
888 }
889
890 //**********************************************************************************************************************
891
892
893