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