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