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