]> git.donarmstrong.com Git - mothur.git/blob - getlineagecommand.cpp
paralellized seq.error and dist.shared added some error checks to libshuff and dist...
[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                         m->splitAtChar(taxons, listOfTaxons, '-');
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                 vector<bool> taxonsHasConfidence; taxonsHasConfidence.resize(listOfTaxons.size(), false);
554                 vector< vector< map<string, float> > > searchTaxons; searchTaxons.resize(listOfTaxons.size());
555                 vector<string> noConfidenceTaxons; noConfidenceTaxons.resize(listOfTaxons.size(), "");
556                 
557                 for (int i = 0; i < listOfTaxons.size(); i++) {
558                         noConfidenceTaxons[i] = listOfTaxons[i];
559                         int hasConPos = listOfTaxons[i].find_first_of('(');
560                         if (hasConPos != string::npos) {  
561                                 taxonsHasConfidence[i] = true; 
562                                 searchTaxons[i] = getTaxons(listOfTaxons[i]); 
563                                 noConfidenceTaxons[i] = removeConfidences(listOfTaxons[i]);
564                         }
565                 }
566                 
567                 
568                 while(!in.eof()){
569
570                         if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str());  return 0; }
571
572                         in >> name;                             //read from first column
573                         in >> tax;                      //read from second column
574                         
575                         for (int j = 0; j < listOfTaxons.size(); j++) {
576                                                         
577                                 string newtax = tax;
578                         
579                                 //if the users file contains confidence scores we want to ignore them when searching for the taxons, unless the taxon has them
580                                 if (!taxonsHasConfidence[j]) {
581                                         int hasConfidences = tax.find_first_of('(');
582                                         if (hasConfidences != string::npos) { 
583                                                 newtax = removeConfidences(tax);
584                                         }
585                                 
586                                         int pos = newtax.find(noConfidenceTaxons[j]);
587                                 
588                                         if (pos != string::npos) { //this sequence contains the taxon the user wants
589                                                 names.insert(name); 
590                                                 out << name << '\t' << tax << endl;
591                                                 //since you belong to at least one of the taxons we want you are included so no need to search for other
592                                                 break;
593                                         }
594                                 }else{//if listOfTaxons[i] has them and you don't them remove taxons
595                                         int hasConfidences = tax.find_first_of('(');
596                                         if (hasConfidences == string::npos) { 
597                                         
598                                                 int pos = newtax.find(noConfidenceTaxons[j]);
599                                         
600                                                 if (pos != string::npos) { //this sequence contains the taxon the user wants
601                                                         names.insert(name);
602                                                         out << name << '\t' << tax << endl;
603                                                         //since you belong to at least one of the taxons we want you are included so no need to search for other
604                                                         break;
605                                                 }
606                                         }else { //both have confidences so we want to make sure the users confidences are greater then or equal to the taxons
607                                                 //first remove confidences from both and see if the taxonomy exists
608                                         
609                                                 string noNewTax = tax;
610                                                 int hasConfidences = tax.find_first_of('(');
611                                                 if (hasConfidences != string::npos) { 
612                                                         noNewTax = removeConfidences(tax);
613                                                 }
614                                         
615                                                 int pos = noNewTax.find(noConfidenceTaxons[j]);
616                                         
617                                                 if (pos != string::npos) { //if yes, then are the confidences okay
618                                                 
619                                                         bool good = true;
620                                                         vector< map<string, float> > usersTaxon = getTaxons(newtax);
621                                                 
622                                                         //the usersTaxon is most likely longer than the searchTaxons, and searchTaxon[0] may relate to userTaxon[4]
623                                                         //we want to "line them up", so we will find the the index where the searchstring starts
624                                                         int index = 0;
625                                                         for (int i = 0; i < usersTaxon.size(); i++) {
626                                                         
627                                                                 if (usersTaxon[i].begin()->first == searchTaxons[j][0].begin()->first) { 
628                                                                         index = i;  
629                                                                         int spot = 0;
630                                                                         bool goodspot = true;
631                                                                         //is this really the start, or are we dealing with a taxon of the same name?
632                                                                         while ((spot < searchTaxons[j].size()) && ((i+spot) < usersTaxon.size())) {
633                                                                                 if (usersTaxon[i+spot].begin()->first != searchTaxons[j][spot].begin()->first) { goodspot = false; break; }
634                                                                                 else { spot++; }
635                                                                         }
636                                                                 
637                                                                         if (goodspot) { break; }
638                                                                 }
639                                                         }
640                                                 
641                                                         for (int i = 0; i < searchTaxons[j].size(); i++) {
642                                                         
643                                                                 if ((i+index) < usersTaxon.size()) { //just in case, should never be false
644                                                                         if (usersTaxon[i+index].begin()->second < searchTaxons[j][i].begin()->second) { //is the users cutoff less than the search taxons
645                                                                                 good = false;
646                                                                                 break;
647                                                                         }
648                                                                 }else {
649                                                                         good = false;
650                                                                         break;
651                                                                 }
652                                                         }
653                                                 
654                                                         //passed the test so add you
655                                                         if (good) {
656                                                                 names.insert(name);
657                                                                 out << name << '\t' << tax << endl;
658                                                                 break;
659                                                         }
660                                                 }
661                                         }
662                                 }
663                         
664                         }
665                         
666                         m->gobble(in);
667                 }
668                 in.close();
669                 out.close();
670                 
671                 if (names.size() == 0) { m->mothurOut("Your taxonomy file does not contain any sequences from " + taxons + "."); m->mothurOutEndLine();  }
672                 outputNames.push_back(outputFileName); outputTypes["taxonomy"].push_back(outputFileName);
673                         
674                 return 0;
675
676         }
677         catch(exception& e) {
678                 m->errorOut(e, "GetLineageCommand", "readTax");
679                 exit(1);
680         }
681 }
682 /**************************************************************************************************/
683 vector< map<string, float> > GetLineageCommand::getTaxons(string tax) {
684         try {
685                 
686                 vector< map<string, float> > t;
687                 string taxon = "";
688                 int taxLength = tax.length();
689                 for(int i=0;i<taxLength;i++){
690                         if(tax[i] == ';'){
691                 
692                                 int openParen = taxon.find_first_of('(');
693                                 int closeParen = taxon.find_last_of(')');
694                                 
695                                 string newtaxon, confidence;
696                                 if ((openParen != string::npos) && (closeParen != string::npos)) {
697                                         newtaxon = taxon.substr(0, openParen); //rip off confidence
698                                         confidence = taxon.substr((openParen+1), (closeParen-openParen-1));  
699                                 }else{
700                                         newtaxon = taxon;
701                                         confidence = "0";
702                                 } 
703                                 float con = 0;
704                                 convert(confidence, con);
705                                 
706                                 map<string, float> temp;
707                                 temp[newtaxon] = con;
708                                 t.push_back(temp);
709                                 
710                                 taxon = "";
711                         }
712                         else{
713                                 taxon += tax[i];
714                         }
715                 }
716                 
717                 return t;
718         }
719         catch(exception& e) {
720                 m->errorOut(e, "GetLineageCommand", "getTaxons");
721                 exit(1);
722         }
723 }
724 /**************************************************************************************************/
725 string GetLineageCommand::removeConfidences(string tax) {
726         try {
727                 
728                 string taxon = "";
729                 int taxLength = tax.length();
730                 for(int i=0;i<taxLength;i++){
731                         if(tax[i] == ';'){
732                                 taxon = taxon.substr(0, taxon.find_first_of('(')); //rip off confidence
733                                 taxon += ";";
734                         }
735                         else{
736                                 taxon += tax[i];
737                         }
738                 }
739                                 
740                 return taxon;
741         }
742         catch(exception& e) {
743                 m->errorOut(e, "GetLineageCommand", "removeConfidences");
744                 exit(1);
745         }
746 }
747 //**********************************************************************************************************************
748 //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
749 int GetLineageCommand::readAlign(){
750         try {
751                 string thisOutputDir = outputDir;
752                 if (outputDir == "") {  thisOutputDir += m->hasPath(alignfile);  }
753                 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(alignfile)) + "pick.align.report";
754                 ofstream out;
755                 m->openOutputFile(outputFileName, out);
756                 
757
758                 ifstream in;
759                 m->openInputFile(alignfile, in);
760                 string name, junk;
761                 
762                 bool wroteSomething = false;
763                 
764                 //read column headers
765                 for (int i = 0; i < 16; i++) {  
766                         if (!in.eof())  {       in >> junk;      out << junk << '\t';   }
767                         else                    {       break;                  }
768                 }
769                 out << endl;
770                 
771                 while(!in.eof()){
772                 
773                         if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str());  return 0; }
774
775
776                         in >> name;                             //read from first column
777                         
778                         //if this name is in the accnos file
779                         if (names.count(name) != 0) {
780                                 wroteSomething = true;
781                                 
782                                 out << name << '\t';
783                                 
784                                 //read rest
785                                 for (int i = 0; i < 15; i++) {  
786                                         if (!in.eof())  {       in >> junk;      out << junk << '\t';   }
787                                         else                    {       break;                  }
788                                 }
789                                 out << endl;
790                                 
791                         }else {//still read just don't do anything with it
792                                 //read rest
793                                 for (int i = 0; i < 15; i++) {  
794                                         if (!in.eof())  {       in >> junk;             }
795                                         else                    {       break;                  }
796                                 }
797                         }
798                         
799                         m->gobble(in);
800                 }
801                 in.close();
802                 out.close();
803                 
804                 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();  }
805                 outputNames.push_back(outputFileName); outputTypes["alignreport"].push_back(outputFileName);
806                 
807                 return 0;
808                 
809         }
810         catch(exception& e) {
811                 m->errorOut(e, "GetLineageCommand", "readAlign");
812                 exit(1);
813         }
814 }
815 //**********************************************************************************************************************
816
817
818
819