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