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