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