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