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