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