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