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