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