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