]> git.donarmstrong.com Git - mothur.git/blob - removeseqscommand.cpp
added getCommandInfoCommand for gui
[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                 
85                 else {
86                         vector<string> myArray = setParameters();
87                         
88                         OptionParser parser(option);
89                         map<string,string> parameters = parser.getParameters();
90                         
91                         ValidParameters validParameter;
92                         map<string,string>::iterator it;
93                         
94                         //check to make sure all parameters are valid for command
95                         for (it = parameters.begin(); it != parameters.end(); it++) { 
96                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
97                         }
98                         
99                         //initialize outputTypes
100                         vector<string> tempOutNames;
101                         outputTypes["fasta"] = tempOutNames;
102                         outputTypes["taxonomy"] = tempOutNames;
103                         outputTypes["name"] = tempOutNames;
104                         outputTypes["group"] = tempOutNames;
105                         outputTypes["alignreport"] = tempOutNames;
106                         outputTypes["list"] = tempOutNames;
107                         outputTypes["qfile"] = tempOutNames;
108                         
109                         //if the user changes the output directory command factory will send this info to us in the output parameter 
110                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = "";         }
111                         
112                         //if the user changes the input directory command factory will send this info to us in the output parameter 
113                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
114                         if (inputDir == "not found"){   inputDir = "";          }
115                         else {
116                                 string path;
117                                 it = parameters.find("alignreport");
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["alignreport"] = inputDir + it->second;              }
123                                 }
124                                 
125                                 it = parameters.find("fasta");
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["fasta"] = inputDir + it->second;            }
131                                 }
132                                 
133                                 it = parameters.find("accnos");
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["accnos"] = inputDir + it->second;           }
139                                 }
140                                 
141                                 it = parameters.find("list");
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["list"] = inputDir + it->second;             }
147                                 }
148                                 
149                                 it = parameters.find("name");
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["name"] = inputDir + it->second;             }
155                                 }
156                                 
157                                 it = parameters.find("group");
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["group"] = inputDir + it->second;            }
163                                 }
164                                 
165                                 it = parameters.find("taxonomy");
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["taxonomy"] = inputDir + it->second;         }
171                                 }
172                                 
173                                 it = parameters.find("qfile");
174                                 //user has given a template file
175                                 if(it != parameters.end()){ 
176                                         path = m->hasPath(it->second);
177                                         //if the user has not given a path then, add inputdir. else leave path alone.
178                                         if (path == "") {       parameters["qfile"] = inputDir + it->second;            }
179                                 }
180                         }
181
182                         
183                         //check for required parameters
184                         accnosfile = validParameter.validFile(parameters, "accnos", true);
185                         if (accnosfile == "not open") { abort = true; }
186                         else if (accnosfile == "not found") {  
187                                 accnosfile = m->getAccnosFile(); 
188                                 if (accnosfile != "") {  m->mothurOut("Using " + accnosfile + " as input file for the accnos parameter."); m->mothurOutEndLine(); }
189                                 else { 
190                                         m->mothurOut("You have no valid accnos file and accnos is required."); m->mothurOutEndLine(); 
191                                         abort = true;
192                                 }  
193                         }       
194                         
195                         fastafile = validParameter.validFile(parameters, "fasta", true);
196                         if (fastafile == "not open") { abort = true; }
197                         else if (fastafile == "not found") {  fastafile = "";  }        
198                         
199                         namefile = validParameter.validFile(parameters, "name", true);
200                         if (namefile == "not open") { abort = true; }
201                         else if (namefile == "not found") {  namefile = "";  }  
202                         
203                         groupfile = validParameter.validFile(parameters, "group", true);
204                         if (groupfile == "not open") { abort = true; }
205                         else if (groupfile == "not found") {  groupfile = "";  }        
206                         
207                         alignfile = validParameter.validFile(parameters, "alignreport", true);
208                         if (alignfile == "not open") { abort = true; }
209                         else if (alignfile == "not found") {  alignfile = "";  }
210                         
211                         listfile = validParameter.validFile(parameters, "list", true);
212                         if (listfile == "not open") { abort = true; }
213                         else if (listfile == "not found") {  listfile = "";  }
214                         
215                         taxfile = validParameter.validFile(parameters, "taxonomy", true);
216                         if (taxfile == "not open") { abort = true; }
217                         else if (taxfile == "not found") {  taxfile = "";  }
218                         
219                         qualfile = validParameter.validFile(parameters, "qfile", true);
220                         if (qualfile == "not open") { abort = true; }
221                         else if (qualfile == "not found") {  qualfile = "";  }                  
222
223                         
224                         string usedDups = "true";
225                         string temp = validParameter.validFile(parameters, "dups", false);      
226                         if (temp == "not found") { 
227                                 if (namefile != "") {  temp = "true";                                   }
228                                 else                            {  temp = "false"; usedDups = "";       }
229                         }
230                         dups = m->isTrue(temp);
231                         
232                         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; }
233                         
234                 }
235
236         }
237         catch(exception& e) {
238                 m->errorOut(e, "RemoveSeqsCommand", "RemoveSeqsCommand");
239                 exit(1);
240         }
241 }
242 //**********************************************************************************************************************
243
244 int RemoveSeqsCommand::execute(){
245         try {
246                 
247                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
248                 
249                 //get names you want to keep
250                 readAccnos();
251                 
252                 if (m->control_pressed) { return 0; }
253                 
254                 //read through the correct file and output lines you want to keep
255                 if (namefile != "")                     {               readName();             }
256                 if (fastafile != "")            {               readFasta();    }
257                 if (groupfile != "")            {               readGroup();    }
258                 if (alignfile != "")            {               readAlign();    }
259                 if (listfile != "")                     {               readList();             }
260                 if (taxfile != "")                      {               readTax();              }
261                 if (qualfile != "")                     {               readQual();             }
262                 
263                 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        remove(outputNames[i].c_str()); } return 0; }
264                 
265                 m->mothurOut("Removed " + toString(names.size()) + " sequences."); m->mothurOutEndLine();
266                 
267                 if (outputNames.size() != 0) {
268                         m->mothurOutEndLine();
269                         m->mothurOut("Output File Names: "); m->mothurOutEndLine();
270                         for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
271                         m->mothurOutEndLine();
272                         
273                         //set fasta file as new current fastafile
274                         string current = "";
275                         itTypes = outputTypes.find("fasta");
276                         if (itTypes != outputTypes.end()) {
277                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
278                         }
279                         
280                         itTypes = outputTypes.find("name");
281                         if (itTypes != outputTypes.end()) {
282                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); }
283                         }
284                         
285                         itTypes = outputTypes.find("group");
286                         if (itTypes != outputTypes.end()) {
287                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setGroupFile(current); }
288                         }
289                         
290                         itTypes = outputTypes.find("list");
291                         if (itTypes != outputTypes.end()) {
292                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setListFile(current); }
293                         }
294                         
295                         itTypes = outputTypes.find("taxonomy");
296                         if (itTypes != outputTypes.end()) {
297                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setTaxonomyFile(current); }
298                         }
299                         
300                         itTypes = outputTypes.find("qfile");
301                         if (itTypes != outputTypes.end()) {
302                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setQualFile(current); }
303                         }                       
304                 }
305                 
306                 return 0;               
307         }
308
309         catch(exception& e) {
310                 m->errorOut(e, "RemoveSeqsCommand", "execute");
311                 exit(1);
312         }
313 }
314
315 //**********************************************************************************************************************
316 int RemoveSeqsCommand::readFasta(){
317         try {
318                 string thisOutputDir = outputDir;
319                 if (outputDir == "") {  thisOutputDir += m->hasPath(fastafile);  }
320                 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(fastafile)) + "pick" + m->getExtension(fastafile);
321                 
322                 ofstream out;
323                 m->openOutputFile(outputFileName, out);
324                 
325                 ifstream in;
326                 m->openInputFile(fastafile, in);
327                 string name;
328                 
329                 bool wroteSomething = false;
330                 
331                 while(!in.eof()){
332                         if (m->control_pressed) { in.close();  out.close();  remove(outputFileName.c_str());  return 0; }
333                         
334                         Sequence currSeq(in);
335                         name = currSeq.getName();
336                         
337                         if (name != "") {
338                                 //if this name is in the accnos file
339                                 if (names.count(name) == 0) {
340                                         wroteSomething = true;
341                                         
342                                         currSeq.printSequence(out);
343                                 }
344                         }
345                         m->gobble(in);
346                 }
347                 in.close();     
348                 out.close();
349                 
350                 if (wroteSomething == false) {  m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine();  }
351                 outputTypes["fasta"].push_back(outputFileName);  outputNames.push_back(outputFileName);
352                 
353                 return 0;
354                 
355         }
356         catch(exception& e) {
357                 m->errorOut(e, "RemoveSeqsCommand", "readFasta");
358                 exit(1);
359         }
360 }
361 //**********************************************************************************************************************
362 int RemoveSeqsCommand::readQual(){
363         try {
364                 string thisOutputDir = outputDir;
365                 if (outputDir == "") {  thisOutputDir += m->hasPath(qualfile);  }
366                 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(qualfile)) + "pick" +  m->getExtension(qualfile);
367                 ofstream out;
368                 m->openOutputFile(outputFileName, out);
369                 
370                 
371                 ifstream in;
372                 m->openInputFile(qualfile, in);
373                 string name;
374                 
375                 bool wroteSomething = false;
376                 
377                 
378                 while(!in.eof()){       
379                         string saveName = "";
380                         string name = "";
381                         string scores = "";
382                         
383                         in >> name; 
384                         
385                         if (name.length() != 0) { 
386                                 saveName = name.substr(1);
387                                 while (!in.eof())       {       
388                                         char c = in.get(); 
389                                         if (c == 10 || c == 13){        break;  }
390                                         else { name += c; }     
391                                 } 
392                                 m->gobble(in);
393                         }
394                         
395                         while(in){
396                                 char letter= in.get();
397                                 if(letter == '>'){      in.putback(letter);     break;  }
398                                 else{ scores += letter; }
399                         }
400                         
401                         m->gobble(in);
402                         
403                         if (names.count(saveName) == 0) {
404                                 wroteSomething = true;
405                                 
406                                 out << name << endl << scores;
407                         }
408                         
409                         m->gobble(in);
410                 }
411                 in.close();
412                 out.close();
413                 
414                 
415                 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine();  }
416                 outputNames.push_back(outputFileName);  outputTypes["qfile"].push_back(outputFileName); 
417                 
418                 return 0;
419                 
420         }
421         catch(exception& e) {
422                 m->errorOut(e, "RemoveSeqsCommand", "readQual");
423                 exit(1);
424         }
425 }
426 //**********************************************************************************************************************
427 int RemoveSeqsCommand::readList(){
428         try {
429                 string thisOutputDir = outputDir;
430                 if (outputDir == "") {  thisOutputDir += m->hasPath(listfile);  }
431                 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(listfile)) + "pick" +  m->getExtension(listfile);
432                 
433                 ofstream out;
434                 m->openOutputFile(outputFileName, out);
435                 
436                 ifstream in;
437                 m->openInputFile(listfile, in);
438                 
439                 bool wroteSomething = false;
440                 
441                 while(!in.eof()){
442                         //read in list vector
443                         ListVector list(in);
444                         
445                         //make a new list vector
446                         ListVector newList;
447                         newList.setLabel(list.getLabel());
448                         
449                         //for each bin
450                         for (int i = 0; i < list.getNumBins(); i++) {
451                                 if (m->control_pressed) { in.close();  out.close();  remove(outputFileName.c_str());  return 0; }
452                         
453                                 //parse out names that are in accnos file
454                                 string binnames = list.get(i);
455                                 
456                                 string newNames = "";
457                                 while (binnames.find_first_of(',') != -1) { 
458                                         string name = binnames.substr(0,binnames.find_first_of(','));
459                                         binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
460                                         
461                                         //if that name is in the .accnos file, add it
462                                         if (names.count(name) == 0) {  newNames += name + ",";  }
463                                 }
464                         
465                                 //get last name
466                                 if (names.count(binnames) == 0) {  newNames += binnames + ",";  }
467
468                                 //if there are names in this bin add to new list
469                                 if (newNames != "") {  
470                                         newNames = newNames.substr(0, newNames.length()-1); //rip off extra comma
471                                         newList.push_back(newNames);    
472                                 }
473                         }
474                                 
475                         //print new listvector
476                         if (newList.getNumBins() != 0) {
477                                 wroteSomething = true;
478                                 newList.print(out);
479                         }
480                         
481                         m->gobble(in);
482                 }
483                 in.close();     
484                 out.close();
485                 
486                 if (wroteSomething == false) {  m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine();  }
487                 outputTypes["list"].push_back(outputFileName); outputNames.push_back(outputFileName);
488                                 
489                 return 0;
490
491         }
492         catch(exception& e) {
493                 m->errorOut(e, "RemoveSeqsCommand", "readList");
494                 exit(1);
495         }
496 }
497 //**********************************************************************************************************************
498 int RemoveSeqsCommand::readName(){
499         try {
500                 string thisOutputDir = outputDir;
501                 if (outputDir == "") {  thisOutputDir += m->hasPath(namefile);  }
502                 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(namefile)) + "pick" + m->getExtension(namefile);
503
504                 ofstream out;
505                 m->openOutputFile(outputFileName, out);
506
507                 ifstream in;
508                 m->openInputFile(namefile, in);
509                 string name, firstCol, secondCol;
510                 
511                 bool wroteSomething = false;
512                                 
513                 
514                 while(!in.eof()){
515                         if (m->control_pressed) { in.close();  out.close();  remove(outputFileName.c_str());  return 0; }
516                         
517                         in >> firstCol;         m->gobble(in);          
518                         in >> secondCol;                        
519                         
520                         vector<string> parsedNames;
521                         m->splitAtComma(secondCol, parsedNames);
522                         
523                         vector<string> validSecond;  validSecond.clear();
524                         for (int i = 0; i < parsedNames.size(); i++) {
525                                 if (names.count(parsedNames[i]) == 0) {
526                                         validSecond.push_back(parsedNames[i]);
527                                 }
528                         }
529                         
530                         if ((dups) && (validSecond.size() != parsedNames.size())) {  //if dups is true and we want to get rid of anyone, get rid of everyone
531                                 for (int i = 0; i < parsedNames.size(); i++) {  names.insert(parsedNames[i]);  }
532                         }else {
533                                 //if the name in the first column is in the set then print it and any other names in second column also in set
534                                 if (names.count(firstCol) == 0) {
535                                         
536                                         wroteSomething = true;
537                                         
538                                         out << firstCol << '\t';
539                                         
540                                         //you know you have at least one valid second since first column is valid
541                                         for (int i = 0; i < validSecond.size()-1; i++) {  out << validSecond[i] << ',';  }
542                                         out << validSecond[validSecond.size()-1] << endl;
543                                         
544                                         //make first name in set you come to first column and then add the remaining names to second column
545                                 }else {
546                                         
547                                         //you want part of this row
548                                         if (validSecond.size() != 0) {
549                                                 
550                                                 wroteSomething = true;
551                                                 
552                                                 out << validSecond[0] << '\t';
553                                                 
554                                                 //you know you have at least one valid second since first column is valid
555                                                 for (int i = 0; i < validSecond.size()-1; i++) {  out << validSecond[i] << ',';  }
556                                                 out << validSecond[validSecond.size()-1] << endl;
557                                         }
558                                 }
559                         }
560                         m->gobble(in);
561                 }
562                 in.close();
563                 out.close();
564                 
565                 if (wroteSomething == false) {  m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine();  }
566                 outputTypes["name"].push_back(outputFileName); outputNames.push_back(outputFileName);
567                 
568                 return 0;
569         }
570         catch(exception& e) {
571                 m->errorOut(e, "RemoveSeqsCommand", "readName");
572                 exit(1);
573         }
574 }
575
576 //**********************************************************************************************************************
577 int RemoveSeqsCommand::readGroup(){
578         try {
579                 string thisOutputDir = outputDir;
580                 if (outputDir == "") {  thisOutputDir += m->hasPath(groupfile);  }
581                 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(groupfile)) + "pick" + m->getExtension(groupfile);
582                 
583                 ofstream out;
584                 m->openOutputFile(outputFileName, out);
585
586                 ifstream in;
587                 m->openInputFile(groupfile, in);
588                 string name, group;
589                 
590                 bool wroteSomething = false;
591                 
592                 while(!in.eof()){
593                         if (m->control_pressed) { in.close();  out.close();  remove(outputFileName.c_str());  return 0; }
594                         
595                         in >> name;                             //read from first column
596                         in >> group;                    //read from second column
597                         
598                         //if this name is in the accnos file
599                         if (names.count(name) == 0) {
600                                 wroteSomething = true;
601                                 out << name << '\t' << group << endl;
602                         }
603                                         
604                         m->gobble(in);
605                 }
606                 in.close();
607                 out.close();
608                 
609                 if (wroteSomething == false) {  m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine();  }
610                 outputTypes["group"].push_back(outputFileName); outputNames.push_back(outputFileName);
611                 
612                 return 0;
613         }
614         catch(exception& e) {
615                 m->errorOut(e, "RemoveSeqsCommand", "readGroup");
616                 exit(1);
617         }
618 }
619 //**********************************************************************************************************************
620 int RemoveSeqsCommand::readTax(){
621         try {
622                 string thisOutputDir = outputDir;
623                 if (outputDir == "") {  thisOutputDir += m->hasPath(taxfile);  }
624                 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(taxfile)) + "pick" + m->getExtension(taxfile);
625                 ofstream out;
626                 m->openOutputFile(outputFileName, out);
627
628                 ifstream in;
629                 m->openInputFile(taxfile, in);
630                 string name, tax;
631                 
632                 bool wroteSomething = false;
633                 
634                 while(!in.eof()){
635                         if (m->control_pressed) { in.close();  out.close();  remove(outputFileName.c_str());  return 0; }
636                         
637                         in >> name;                             //read from first column
638                         in >> tax;                      //read from second column
639                         
640                         //if this name is in the accnos file
641                         if (names.count(name) == 0) {
642                                 wroteSomething = true;
643                                 out << name << '\t' << tax << endl;
644                         }
645                                         
646                         m->gobble(in);
647                 }
648                 in.close();
649                 out.close();
650                 
651                 if (wroteSomething == false) {  m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine();  }
652                 outputTypes["taxonomy"].push_back(outputFileName); outputNames.push_back(outputFileName);
653                 
654                 return 0;
655         }
656         catch(exception& e) {
657                 m->errorOut(e, "RemoveSeqsCommand", "readTax");
658                 exit(1);
659         }
660 }
661 //**********************************************************************************************************************
662 //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
663 int RemoveSeqsCommand::readAlign(){
664         try {
665                 string thisOutputDir = outputDir;
666                 if (outputDir == "") {  thisOutputDir += m->hasPath(alignfile);  }
667                 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(alignfile)) + "pick.align.report";
668                 
669                 ofstream out;
670                 m->openOutputFile(outputFileName, out);
671
672                 ifstream in;
673                 m->openInputFile(alignfile, in);
674                 string name, junk;
675                 
676                 bool wroteSomething = false;
677                 
678                 //read column headers
679                 for (int i = 0; i < 16; i++) {  
680                         if (!in.eof())  {       in >> junk;      out << junk << '\t';   }
681                         else                    {       break;                  }
682                 }
683                 out << endl;
684                 
685                 while(!in.eof()){
686                         if (m->control_pressed) { in.close();  out.close();  remove(outputFileName.c_str());  return 0; }
687                         
688                         in >> name;                             //read from first column
689                         
690                         //if this name is in the accnos file
691                         if (names.count(name) == 0) {
692                                 wroteSomething = true;
693                                 
694                                 out << name << '\t';
695                                 
696                                 //read rest
697                                 for (int i = 0; i < 15; i++) {  
698                                         if (!in.eof())  {       in >> junk;      out << junk << '\t';   }
699                                         else                    {       break;                  }
700                                 }
701                                 out << endl;
702                                 
703                         }else {//still read just don't do anything with it
704                                 
705                                 //read rest
706                                 for (int i = 0; i < 15; i++) {  
707                                         if (!in.eof())  {       in >> junk;             }
708                                         else                    {       break;                  }
709                                 }
710                         }
711                         
712                         m->gobble(in);
713                 }
714                 in.close();
715                 out.close();
716                 
717                 if (wroteSomething == false) {  m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine();  }
718                 outputTypes["alignreport"].push_back(outputFileName); outputNames.push_back(outputFileName);
719                 
720                 return 0;
721                 
722         }
723         catch(exception& e) {
724                 m->errorOut(e, "RemoveSeqsCommand", "readAlign");
725                 exit(1);
726         }
727 }
728 //**********************************************************************************************************************
729 void RemoveSeqsCommand::readAccnos(){
730         try {
731                 
732                 ifstream in;
733                 m->openInputFile(accnosfile, in);
734                 string name;
735                 
736                 while(!in.eof()){
737                         in >> name;
738                                                 
739                         names.insert(name);
740                         
741                         m->gobble(in);
742                 }
743                 in.close();             
744
745         }
746         catch(exception& e) {
747                 m->errorOut(e, "RemoveSeqsCommand", "readAccnos");
748                 exit(1);
749         }
750 }
751
752 //**********************************************************************************************************************
753
754