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