]> git.donarmstrong.com Git - mothur.git/blob - removegroupscommand.cpp
fixed bug with shhh.flow from file path name in write functions, added "smart" featur...
[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 plist("list", "InputTypes", "", "", "none", "none", "FNGLT",false,false); parameters.push_back(plist);
24                 CommandParameter ptaxonomy("taxonomy", "InputTypes", "", "", "none", "none", "FNGLT",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                         if ((namefile == "") && ((fastafile != "") || (taxfile != ""))){
258                                 vector<string> files; files.push_back(fastafile); files.push_back(taxfile);
259                                 parser.getNameFile(files);
260                         }
261                 
262                 }
263                 
264         }
265         catch(exception& e) {
266                 m->errorOut(e, "RemoveGroupsCommand", "RemoveGroupsCommand");
267                 exit(1);
268         }
269 }
270 //**********************************************************************************************************************
271
272 int RemoveGroupsCommand::execute(){
273         try {
274                 
275                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
276                 
277                 //get groups you want to remove
278                 if (accnosfile != "") { readAccnos(); }
279                 
280                 if (groupfile != "") {
281                         groupMap = new GroupMap(groupfile);
282                         groupMap->readMap();
283                         
284                         //make sure groups are valid
285                         //takes care of user setting groupNames that are invalid or setting groups=all
286                         SharedUtil* util = new SharedUtil();
287                         vector<string> namesGroups = groupMap->getNamesOfGroups();
288                         util->setGroups(Groups, namesGroups);
289                         delete util;
290                         
291                         //fill names with names of sequences that are from the groups we want to remove 
292                         fillNames();
293                         
294                         delete groupMap;
295                 }
296                                 
297                 if (m->control_pressed) { return 0; }
298                 
299                 //read through the correct file and output lines you want to keep
300                 if (namefile != "")                     {               readName();             }
301                 if (fastafile != "")            {               readFasta();    }
302                 if (groupfile != "")            {               readGroup();    }
303                 if (listfile != "")                     {               readList();             }
304                 if (taxfile != "")                      {               readTax();              }
305                 if (sharedfile != "")           {               readShared();   }
306                 
307                 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        m->mothurRemove(outputNames[i]); } return 0; }
308                                 
309                 if (outputNames.size() != 0) {
310                         m->mothurOutEndLine();
311                         m->mothurOut("Output File names: "); m->mothurOutEndLine();
312                         for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
313                         m->mothurOutEndLine();
314                         
315                         //set fasta file as new current fastafile
316                         string current = "";
317                         itTypes = outputTypes.find("fasta");
318                         if (itTypes != outputTypes.end()) {
319                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
320                         }
321                         
322                         itTypes = outputTypes.find("name");
323                         if (itTypes != outputTypes.end()) {
324                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); }
325                         }
326                         
327                         itTypes = outputTypes.find("group");
328                         if (itTypes != outputTypes.end()) {
329                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setGroupFile(current); }
330                         }
331                         
332                         itTypes = outputTypes.find("list");
333                         if (itTypes != outputTypes.end()) {
334                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setListFile(current); }
335                         }
336                         
337                         itTypes = outputTypes.find("taxonomy");
338                         if (itTypes != outputTypes.end()) {
339                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setTaxonomyFile(current); }
340                         }
341                         
342                         itTypes = outputTypes.find("shared");
343                         if (itTypes != outputTypes.end()) {
344                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setSharedFile(current); }
345                         }
346                 }
347                 
348                 return 0;               
349         }
350         
351         catch(exception& e) {
352                 m->errorOut(e, "RemoveGroupsCommand", "execute");
353                 exit(1);
354         }
355 }
356
357 //**********************************************************************************************************************
358 int RemoveGroupsCommand::readFasta(){
359         try {
360                 string thisOutputDir = outputDir;
361                 if (outputDir == "") {  thisOutputDir += m->hasPath(fastafile);  }
362                 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(fastafile)) + "pick" + m->getExtension(fastafile);
363                 
364                 ofstream out;
365                 m->openOutputFile(outputFileName, out);
366                 
367                 ifstream in;
368                 m->openInputFile(fastafile, in);
369                 string name;
370                 
371                 bool wroteSomething = false;
372                 int removedCount = 0;
373                 
374                 while(!in.eof()){
375                         if (m->control_pressed) { in.close();  out.close();  m->mothurRemove(outputFileName);  return 0; }
376                         
377                         Sequence currSeq(in);
378                         name = currSeq.getName();
379                         
380                         if (name != "") {
381                                 //if this name is in the accnos file
382                                 if (names.count(name) == 0) {
383                                         wroteSomething = true;
384                                         currSeq.printSequence(out); 
385                                 }else { 
386                                         //if you are not in the accnos file check if you are a name that needs to be changed
387                                         map<string, string>::iterator it = uniqueToRedundant.find(name);
388                                         if (it != uniqueToRedundant.end()) {
389                                                 wroteSomething = true;
390                                                 currSeq.setName(it->second);
391                                                 currSeq.printSequence(out);
392                                         }else { removedCount++; }
393                                 }
394                         }
395                         m->gobble(in);
396                 }
397                 in.close();     
398                 out.close();
399                 
400                 if (wroteSomething == false) {  m->mothurOut("Your file contains only sequences from the groups you wish to remove."); m->mothurOutEndLine();  }
401                 outputTypes["fasta"].push_back(outputFileName);  outputNames.push_back(outputFileName);
402                 
403                 m->mothurOut("Removed " + toString(removedCount) + " sequences from your fasta file."); m->mothurOutEndLine();
404                 
405                 return 0;
406                 
407         }
408         catch(exception& e) {
409                 m->errorOut(e, "RemoveGroupsCommand", "readFasta");
410                 exit(1);
411         }
412 }
413 //**********************************************************************************************************************
414 int RemoveGroupsCommand::readShared(){
415         try {
416                 string thisOutputDir = outputDir;
417                 if (outputDir == "") {  thisOutputDir += m->hasPath(sharedfile);  }
418                 
419                 //get group names from sharedfile so we can set Groups to the groupNames we want to keep
420                 //that way we can take advantage of the reads in inputdata and sharedRabundVector
421                 InputData* tempInput = new InputData(sharedfile, "sharedfile");
422                 vector<SharedRAbundVector*> lookup = tempInput->getSharedRAbundVectors();
423         
424                 //save m->Groups
425                 vector<string> allGroupsNames = m->getAllGroups();
426                 vector<string> mothurOutGroups = m->getGroups();
427                 
428                 vector<string> groupsToKeep;
429                 for (int i = 0; i < allGroupsNames.size(); i++) {
430                         if (!m->inUsersGroups(allGroupsNames[i], m->getGroups())) {
431                                 groupsToKeep.push_back(allGroupsNames[i]);
432                         }
433                 }
434                 
435                 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; }
436                 
437                 //reset read 
438                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
439                 delete tempInput;
440                 m->setGroups(groupsToKeep);
441                 m->clearAllGroups();
442                 m->names.clear();
443                 m->saveNextLabel = "";
444                 m->printedHeaders = false;
445                 m->currentBinLabels.clear();
446                 m->binLabelsInFile.clear();
447                 
448                 InputData input(sharedfile, "sharedfile");
449                 lookup = input.getSharedRAbundVectors();
450
451                 bool wroteSomething = false;
452                 
453                 while(lookup[0] != NULL) {
454                         
455                         string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(sharedfile)) + lookup[0]->getLabel() + ".pick" + m->getExtension(sharedfile);
456                         ofstream out;
457                         m->openOutputFile(outputFileName, out);
458                         outputTypes["shared"].push_back(outputFileName);  outputNames.push_back(outputFileName);
459                         
460                         if (m->control_pressed) { out.close();  m->mothurRemove(outputFileName);  for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } return 0; }
461                         
462                         lookup[0]->printHeaders(out); 
463                         
464                         for (int i = 0; i < lookup.size(); i++) {
465                                 out << lookup[i]->getLabel() << '\t' << lookup[i]->getGroup() << '\t';
466                                 lookup[i]->print(out);
467                                 wroteSomething = true;
468                                 
469                         }                       
470                         
471                         //get next line to process
472                         //prevent memory leak
473                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
474                         lookup = input.getSharedRAbundVectors();
475                         
476                         out.close();
477                 }
478                 
479                 
480                 m->setGroups(mothurOutGroups);
481                 
482                 if (wroteSomething == false) {  m->mothurOut("Your file contains only the groups you wish to remove."); m->mothurOutEndLine();  }
483                 
484                 string groupsString = "";
485                 for (int i = 0; i < Groups.size()-1; i++) {     groupsString += Groups[i] + ", "; }
486                 groupsString += Groups[Groups.size()-1];
487                 
488                 m->mothurOut("Removed groups: " + groupsString + " from your shared file."); m->mothurOutEndLine();
489                 
490                 return 0;
491                 
492         }
493         catch(exception& e) {
494                 m->errorOut(e, "RemoveGroupsCommand", "readShared");
495                 exit(1);
496         }
497 }
498 //**********************************************************************************************************************
499 int RemoveGroupsCommand::readList(){
500         try {
501                 string thisOutputDir = outputDir;
502                 if (outputDir == "") {  thisOutputDir += m->hasPath(listfile);  }
503                 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(listfile)) + "pick" +  m->getExtension(listfile);
504                 
505                 ofstream out;
506                 m->openOutputFile(outputFileName, out);
507                 
508                 ifstream in;
509                 m->openInputFile(listfile, in);
510                 
511                 bool wroteSomething = false;
512                 int removedCount = 0;
513                 
514                 while(!in.eof()){
515                         
516                         removedCount = 0;
517                         
518                         //read in list vector
519                         ListVector list(in);
520                         
521                         //make a new list vector
522                         ListVector newList;
523                         newList.setLabel(list.getLabel());
524                         
525                         //for each bin
526                         for (int i = 0; i < list.getNumBins(); i++) {
527                                 if (m->control_pressed) { in.close();  out.close();  m->mothurRemove(outputFileName);  return 0; }
528                                 
529                                 //parse out names that are in accnos file
530                                 string binnames = list.get(i);
531                                 
532                                 string newNames = "";
533                                 while (binnames.find_first_of(',') != -1) { 
534                                         string name = binnames.substr(0,binnames.find_first_of(','));
535                                         binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
536                                         
537                                         //if that name is in the .accnos file, add it
538                                         if (names.count(name) == 0) {  newNames += name + ",";  }
539                                         else {
540                                                 //if you are not in the accnos file check if you are a name that needs to be changed
541                                                 map<string, string>::iterator it = uniqueToRedundant.find(name);
542                                                 if (it != uniqueToRedundant.end()) {
543                                                         newNames += it->second + ",";
544                                                 }else { removedCount++; }
545                                         }
546                                 }
547                                 
548                                 //get last name
549                                 if (names.count(binnames) == 0) {  newNames += binnames + ",";  }
550                                 else { //if you are not in the accnos file check if you are a name that needs to be changed
551                                         map<string, string>::iterator it = uniqueToRedundant.find(binnames);
552                                         if (it != uniqueToRedundant.end()) {
553                                                 newNames += it->second + ",";
554                                         }else { removedCount++; }
555                                 }
556                                 
557                                 //if there are names in this bin add to new list
558                                 if (newNames != "") {  
559                                         newNames = newNames.substr(0, newNames.length()-1); //rip off extra comma
560                                         newList.push_back(newNames);    
561                                 }
562                         }
563                         
564                         //print new listvector
565                         if (newList.getNumBins() != 0) {
566                                 wroteSomething = true;
567                                 newList.print(out);
568                         }
569                         
570                         m->gobble(in);
571                 }
572                 in.close();     
573                 out.close();
574                 
575                 if (wroteSomething == false) {  m->mothurOut("Your file contains only sequences from the groups you wish to remove."); m->mothurOutEndLine();  }
576                 outputTypes["list"].push_back(outputFileName); outputNames.push_back(outputFileName);
577                 
578                 m->mothurOut("Removed " + toString(removedCount) + " sequences from your list file."); m->mothurOutEndLine();
579                 
580                 return 0;
581                 
582         }
583         catch(exception& e) {
584                 m->errorOut(e, "RemoveGroupsCommand", "readList");
585                 exit(1);
586         }
587 }
588 //**********************************************************************************************************************
589 int RemoveGroupsCommand::readName(){
590         try {
591                 string thisOutputDir = outputDir;
592                 if (outputDir == "") {  thisOutputDir += m->hasPath(namefile);  }
593                 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(namefile)) + "pick" + m->getExtension(namefile);
594                 
595                 ofstream out;
596                 m->openOutputFile(outputFileName, out);
597                 
598                 ifstream in;
599                 m->openInputFile(namefile, in);
600                 string name, firstCol, secondCol;
601                 
602                 bool wroteSomething = false;
603                 int removedCount = 0;
604                 
605                 while(!in.eof()){
606                         if (m->control_pressed) { in.close();  out.close();  m->mothurRemove(outputFileName);  return 0; }
607                         
608                         in >> firstCol;         m->gobble(in);          
609                         in >> secondCol;                        
610                         
611                         vector<string> parsedNames;
612                         m->splitAtComma(secondCol, parsedNames);
613                                                 
614                         vector<string> validSecond;  validSecond.clear();
615                         for (int i = 0; i < parsedNames.size(); i++) {
616                                 if (names.count(parsedNames[i]) == 0) {
617                                         validSecond.push_back(parsedNames[i]);
618                                 }
619                         }
620                         
621                         removedCount += parsedNames.size()-validSecond.size();
622                         
623                         //if the name in the first column is in the set then print it and any other names in second column also in set
624                         if (names.count(firstCol) == 0) {
625                                 
626                                 wroteSomething = true;
627                                 
628                                 out << firstCol << '\t';
629                                 
630                                 //you know you have at least one valid second since first column is valid
631                                 for (int i = 0; i < validSecond.size()-1; i++) {  out << validSecond[i] << ',';  }
632                                 out << validSecond[validSecond.size()-1] << endl;
633                                 
634                                 //make first name in set you come to first column and then add the remaining names to second column
635                         }else {
636                                 
637                                 //you want part of this row
638                                 if (validSecond.size() != 0) {
639                                         
640                                         wroteSomething = true;
641                                         
642                                         out << validSecond[0] << '\t';
643                                         
644                                         //you know you have at least one valid second since first column is valid
645                                         for (int i = 0; i < validSecond.size()-1; i++) {  out << validSecond[i] << ',';  }
646                                         out << validSecond[validSecond.size()-1] << endl;
647                                         uniqueToRedundant[firstCol] = validSecond[0];
648                                 }
649                         }
650                         
651                         m->gobble(in);
652                 }
653                 in.close();
654                 out.close();
655                 
656                 if (wroteSomething == false) {  m->mothurOut("Your file contains only sequences from the groups you wish to remove."); m->mothurOutEndLine();  }
657                 outputTypes["name"].push_back(outputFileName); outputNames.push_back(outputFileName);
658                 
659                 m->mothurOut("Removed " + toString(removedCount) + " sequences from your name file."); m->mothurOutEndLine();
660                 
661                 return 0;
662         }
663         catch(exception& e) {
664                 m->errorOut(e, "RemoveGroupsCommand", "readName");
665                 exit(1);
666         }
667 }
668
669 //**********************************************************************************************************************
670 int RemoveGroupsCommand::readGroup(){
671         try {
672                 string thisOutputDir = outputDir;
673                 if (outputDir == "") {  thisOutputDir += m->hasPath(groupfile);  }
674                 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(groupfile)) + "pick" + m->getExtension(groupfile);
675                 
676                 ofstream out;
677                 m->openOutputFile(outputFileName, out);
678                 
679                 ifstream in;
680                 m->openInputFile(groupfile, in);
681                 string name, group;
682                 
683                 bool wroteSomething = false;
684                 int removedCount = 0;
685                 
686                 while(!in.eof()){
687                         if (m->control_pressed) { in.close();  out.close();  m->mothurRemove(outputFileName);  return 0; }
688                         
689                         in >> name;                             //read from first column
690                         in >> group;                    //read from second column
691                         
692                         //if this name is in the accnos file
693                         if (names.count(name) == 0) {
694                                 wroteSomething = true;
695                                 out << name << '\t' << group << endl;
696                         }else {  removedCount++;  }
697                         
698                         m->gobble(in);
699                 }
700                 in.close();
701                 out.close();
702                 
703                 if (wroteSomething == false) {  m->mothurOut("Your file contains only sequences from the groups you wish to remove."); m->mothurOutEndLine();  }
704                 outputTypes["group"].push_back(outputFileName); outputNames.push_back(outputFileName);
705                 
706                 m->mothurOut("Removed " + toString(removedCount) + " sequences from your group file."); m->mothurOutEndLine();
707
708                 
709                 return 0;
710         }
711         catch(exception& e) {
712                 m->errorOut(e, "RemoveGroupsCommand", "readGroup");
713                 exit(1);
714         }
715 }
716 //**********************************************************************************************************************
717 int RemoveGroupsCommand::readTax(){
718         try {
719                 string thisOutputDir = outputDir;
720                 if (outputDir == "") {  thisOutputDir += m->hasPath(taxfile);  }
721                 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(taxfile)) + "pick" + m->getExtension(taxfile);
722                 ofstream out;
723                 m->openOutputFile(outputFileName, out);
724                 
725                 ifstream in;
726                 m->openInputFile(taxfile, in);
727                 string name, tax;
728                 
729                 bool wroteSomething = false;
730                 int removedCount = 0;
731                 
732                 while(!in.eof()){
733                         if (m->control_pressed) { in.close();  out.close();  m->mothurRemove(outputFileName);  return 0; }
734                         
735                         in >> name;                             //read from first column
736                         in >> tax;                      //read from second column
737                         
738                         //if this name is in the accnos file
739                         if (names.count(name) == 0) {
740                                 wroteSomething = true;
741                                 out << name << '\t' << tax << endl;
742                         }else {  //if you are not in the accnos file check if you are a name that needs to be changed
743                                 map<string, string>::iterator it = uniqueToRedundant.find(name);
744                                 if (it != uniqueToRedundant.end()) {
745                                         wroteSomething = true;
746                                         out << it->second << '\t' << tax << endl;
747                                 }else { removedCount++; }  }
748                         
749                         m->gobble(in);
750                 }
751                 in.close();
752                 out.close();
753                 
754                 if (wroteSomething == false) {  m->mothurOut("Your file contains only sequences from the groups you wish to remove."); m->mothurOutEndLine();  }
755                 outputTypes["taxonomy"].push_back(outputFileName); outputNames.push_back(outputFileName);
756                 
757                 m->mothurOut("Removed " + toString(removedCount) + " sequences from your taxonomy file."); m->mothurOutEndLine();
758                 
759                 return 0;
760         }
761         catch(exception& e) {
762                 m->errorOut(e, "RemoveGroupsCommand", "readTax");
763                 exit(1);
764         }
765 }
766 //**********************************************************************************************************************
767 void RemoveGroupsCommand::readAccnos(){
768         try {
769                 Groups.clear();
770                 
771                 ifstream in;
772                 m->openInputFile(accnosfile, in);
773                 string name;
774                 
775                 while(!in.eof()){
776                         in >> name;
777                         
778                         Groups.push_back(name);
779                         
780                         m->gobble(in);
781                 }
782                 in.close();     
783                 
784                 m->setGroups(Groups);
785                 
786         }
787         catch(exception& e) {
788                 m->errorOut(e, "RemoveGroupsCommand", "readAccnos");
789                 exit(1);
790         }
791 }
792 //**********************************************************************************************************************
793 int RemoveGroupsCommand::fillNames(){
794         try {
795                 vector<string> seqs = groupMap->getNamesSeqs();
796                 
797                 for (int i = 0; i < seqs.size(); i++) {
798                         
799                         if (m->control_pressed) { return 0; }
800                         
801                         string group = groupMap->getGroup(seqs[i]);
802                         
803                         if (m->inUsersGroups(group, Groups)) {
804                                 names.insert(seqs[i]);
805                         }
806                 }
807                 
808                 return 0;
809         }
810         catch(exception& e) {
811                 m->errorOut(e, "RemoveGroupsCommand", "fillNames");
812                 exit(1);
813         }
814 }
815
816 //**********************************************************************************************************************
817
818
819