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