]> git.donarmstrong.com Git - mothur.git/blob - removeseqscommand.cpp
added set.current and get.current commands and modified existing commands to update...
[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                 while(!in.eof()){
518                         if (m->control_pressed) { in.close();  out.close();  remove(outputFileName.c_str());  return 0; }
519
520                         in >> firstCol;         m->gobble(in);          
521                         in >> secondCol;                        
522
523                         vector<string> parsedNames;
524                         //parse second column saving each name
525                         while (secondCol.find_first_of(',') != -1) { 
526                                 name = secondCol.substr(0,secondCol.find_first_of(','));
527                                 secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
528                                 parsedNames.push_back(name);
529                         }
530                         
531                         //get name after last ,
532                         parsedNames.push_back(secondCol);
533
534                         vector<string> validSecond;  validSecond.clear();
535                         for (int i = 0; i < parsedNames.size(); i++) {
536                                 if (names.count(parsedNames[i]) == 0) {
537                                         validSecond.push_back(parsedNames[i]);
538                                 }
539                         }
540                         
541                         if ((dups) && (validSecond.size() != parsedNames.size())) {  //if dups is true and we want to get rid of anyone, get rid of everyone
542                                 for (int i = 0; i < parsedNames.size(); i++) {  names.insert(parsedNames[i]);  }
543                         }else {
544                                         //if the name in the first column is in the set then print it and any other names in second column also in set
545                                 if (names.count(firstCol) == 0) {
546                                         
547                                         wroteSomething = true;
548                                         
549                                         out << firstCol << '\t';
550                                         
551                                         //you know you have at least one valid second since first column is valid
552                                         for (int i = 0; i < validSecond.size()-1; i++) {  out << validSecond[i] << ',';  }
553                                         out << validSecond[validSecond.size()-1] << endl;
554                                         
555                                         //make first name in set you come to first column and then add the remaining names to second column
556                                 }else {
557                                         
558                                         //you want part of this row
559                                         if (validSecond.size() != 0) {
560                                                 
561                                                 wroteSomething = true;
562                                                 
563                                                 out << validSecond[0] << '\t';
564                                                 
565                                                 //you know you have at least one valid second since first column is valid
566                                                 for (int i = 0; i < validSecond.size()-1; i++) {  out << validSecond[i] << ',';  }
567                                                 out << validSecond[validSecond.size()-1] << endl;
568                                         }
569                                 }
570                         }
571                         m->gobble(in);
572                 }
573                 in.close();
574                 out.close();
575
576                 if (wroteSomething == false) {  m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine();  }
577                 outputTypes["name"].push_back(outputFileName); outputNames.push_back(outputFileName);
578                                 
579                 return 0;
580         }
581         catch(exception& e) {
582                 m->errorOut(e, "RemoveSeqsCommand", "readName");
583                 exit(1);
584         }
585 }
586
587 //**********************************************************************************************************************
588 int RemoveSeqsCommand::readGroup(){
589         try {
590                 string thisOutputDir = outputDir;
591                 if (outputDir == "") {  thisOutputDir += m->hasPath(groupfile);  }
592                 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(groupfile)) + "pick" + m->getExtension(groupfile);
593                 
594                 ofstream out;
595                 m->openOutputFile(outputFileName, out);
596
597                 ifstream in;
598                 m->openInputFile(groupfile, in);
599                 string name, group;
600                 
601                 bool wroteSomething = false;
602                 
603                 while(!in.eof()){
604                         if (m->control_pressed) { in.close();  out.close();  remove(outputFileName.c_str());  return 0; }
605                         
606                         in >> name;                             //read from first column
607                         in >> group;                    //read from second column
608                         
609                         //if this name is in the accnos file
610                         if (names.count(name) == 0) {
611                                 wroteSomething = true;
612                                 out << name << '\t' << group << endl;
613                         }
614                                         
615                         m->gobble(in);
616                 }
617                 in.close();
618                 out.close();
619                 
620                 if (wroteSomething == false) {  m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine();  }
621                 outputTypes["group"].push_back(outputFileName); outputNames.push_back(outputFileName);
622                 
623                 return 0;
624         }
625         catch(exception& e) {
626                 m->errorOut(e, "RemoveSeqsCommand", "readGroup");
627                 exit(1);
628         }
629 }
630 //**********************************************************************************************************************
631 int RemoveSeqsCommand::readTax(){
632         try {
633                 string thisOutputDir = outputDir;
634                 if (outputDir == "") {  thisOutputDir += m->hasPath(taxfile);  }
635                 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(taxfile)) + "pick" + m->getExtension(taxfile);
636                 ofstream out;
637                 m->openOutputFile(outputFileName, out);
638
639                 ifstream in;
640                 m->openInputFile(taxfile, in);
641                 string name, tax;
642                 
643                 bool wroteSomething = false;
644                 
645                 while(!in.eof()){
646                         if (m->control_pressed) { in.close();  out.close();  remove(outputFileName.c_str());  return 0; }
647                         
648                         in >> name;                             //read from first column
649                         in >> tax;                      //read from second column
650                         
651                         //if this name is in the accnos file
652                         if (names.count(name) == 0) {
653                                 wroteSomething = true;
654                                 out << name << '\t' << tax << endl;
655                         }
656                                         
657                         m->gobble(in);
658                 }
659                 in.close();
660                 out.close();
661                 
662                 if (wroteSomething == false) {  m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine();  }
663                 outputTypes["taxonomy"].push_back(outputFileName); outputNames.push_back(outputFileName);
664                 
665                 return 0;
666         }
667         catch(exception& e) {
668                 m->errorOut(e, "RemoveSeqsCommand", "readTax");
669                 exit(1);
670         }
671 }
672 //**********************************************************************************************************************
673 //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
674 int RemoveSeqsCommand::readAlign(){
675         try {
676                 string thisOutputDir = outputDir;
677                 if (outputDir == "") {  thisOutputDir += m->hasPath(alignfile);  }
678                 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(alignfile)) + "pick.align.report";
679                 
680                 ofstream out;
681                 m->openOutputFile(outputFileName, out);
682
683                 ifstream in;
684                 m->openInputFile(alignfile, in);
685                 string name, junk;
686                 
687                 bool wroteSomething = false;
688                 
689                 //read column headers
690                 for (int i = 0; i < 16; i++) {  
691                         if (!in.eof())  {       in >> junk;      out << junk << '\t';   }
692                         else                    {       break;                  }
693                 }
694                 out << endl;
695                 
696                 while(!in.eof()){
697                         if (m->control_pressed) { in.close();  out.close();  remove(outputFileName.c_str());  return 0; }
698                         
699                         in >> name;                             //read from first column
700                         
701                         //if this name is in the accnos file
702                         if (names.count(name) == 0) {
703                                 wroteSomething = true;
704                                 
705                                 out << name << '\t';
706                                 
707                                 //read rest
708                                 for (int i = 0; i < 15; i++) {  
709                                         if (!in.eof())  {       in >> junk;      out << junk << '\t';   }
710                                         else                    {       break;                  }
711                                 }
712                                 out << endl;
713                                 
714                         }else {//still read just don't do anything with it
715                                 
716                                 //read rest
717                                 for (int i = 0; i < 15; i++) {  
718                                         if (!in.eof())  {       in >> junk;             }
719                                         else                    {       break;                  }
720                                 }
721                         }
722                         
723                         m->gobble(in);
724                 }
725                 in.close();
726                 out.close();
727                 
728                 if (wroteSomething == false) {  m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine();  }
729                 outputTypes["alignreport"].push_back(outputFileName); outputNames.push_back(outputFileName);
730                 
731                 return 0;
732                 
733         }
734         catch(exception& e) {
735                 m->errorOut(e, "RemoveSeqsCommand", "readAlign");
736                 exit(1);
737         }
738 }
739 //**********************************************************************************************************************
740 void RemoveSeqsCommand::readAccnos(){
741         try {
742                 
743                 ifstream in;
744                 m->openInputFile(accnosfile, in);
745                 string name;
746                 
747                 while(!in.eof()){
748                         in >> name;
749                                                 
750                         names.insert(name);
751                         
752                         m->gobble(in);
753                 }
754                 in.close();             
755
756         }
757         catch(exception& e) {
758                 m->errorOut(e, "RemoveSeqsCommand", "readAccnos");
759                 exit(1);
760         }
761 }
762
763 //**********************************************************************************************************************
764
765