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