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