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