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