2 * getlineagecommand.cpp
5 * Created by westcott on 9/24/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "getlineagecommand.h"
11 #include "sequence.hpp"
12 #include "listvector.hpp"
14 //**********************************************************************************************************************
15 vector<string> GetLineageCommand::setParameters(){
17 CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "FNGLT", "none",false,false); parameters.push_back(pfasta);
18 CommandParameter pname("name", "InputTypes", "", "", "none", "FNGLT", "none",false,false); parameters.push_back(pname);
19 CommandParameter pgroup("group", "InputTypes", "", "", "none", "FNGLT", "none",false,false); parameters.push_back(pgroup);
20 CommandParameter plist("list", "InputTypes", "", "", "none", "FNGLT", "none",false,false); parameters.push_back(plist);
21 CommandParameter ptaxonomy("taxonomy", "InputTypes", "", "", "none", "FNGLT", "none",false,true); parameters.push_back(ptaxonomy);
22 CommandParameter palignreport("alignreport", "InputTypes", "", "", "none", "FNGLT", "none",false,false); parameters.push_back(palignreport);
23 CommandParameter ptaxon("taxon", "String", "", "", "", "", "",false,true); parameters.push_back(ptaxon);
24 CommandParameter pdups("dups", "Boolean", "", "T", "", "", "",false,false); parameters.push_back(pdups);
25 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
26 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
28 vector<string> myArray;
29 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
33 m->errorOut(e, "GetLineageCommand", "setParameters");
37 //**********************************************************************************************************************
38 string GetLineageCommand::getHelpString(){
40 string helpString = "";
41 helpString += "The get.lineage command reads a taxonomy file and any of the following file types: fasta, name, group, list or alignreport file.\n";
42 helpString += "It outputs a file containing only the sequences from the taxonomy file that are from the taxon requested.\n";
43 helpString += "The get.lineage command parameters are taxon, fasta, name, group, list, taxonomy, alignreport and dups. You must provide taxonomy unless you have a valid current taxonomy file.\n";
44 helpString += "The dups parameter allows you to add the entire line from a name file if you add any name from the line. default=false. \n";
45 helpString += "The taxon parameter allows you to select the taxons you would like to get and is required.\n";
46 helpString += "You may enter your taxons with confidence scores, doing so will get only those sequences that belong to the taxonomy and whose cofidence scores is above the scores you give.\n";
47 helpString += "If they belong to the taxonomy and have confidences below those you provide the sequence will not be selected.\n";
48 helpString += "The get.lineage command should be in the following format: get.lineage(taxonomy=yourTaxonomyFile, taxon=yourTaxons).\n";
49 helpString += "Example get.lineage(taxonomy=amazon.silva.taxonomy, taxon=Bacteria;Firmicutes;Bacilli;Lactobacillales;).\n";
50 helpString += "Note: If you are running mothur in script mode you must wrap the taxon in ' characters so mothur will ignore the ; in the taxon.\n";
51 helpString += "Example get.lineage(taxonomy=amazon.silva.taxonomy, taxon='Bacteria;Firmicutes;Bacilli;Lactobacillales;').\n";
52 helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n";
56 m->errorOut(e, "GetLineageCommand", "getHelpString");
61 //**********************************************************************************************************************
62 GetLineageCommand::GetLineageCommand(){
64 abort = true; calledHelp = true;
66 vector<string> tempOutNames;
67 outputTypes["fasta"] = tempOutNames;
68 outputTypes["taxonomy"] = tempOutNames;
69 outputTypes["name"] = tempOutNames;
70 outputTypes["group"] = tempOutNames;
71 outputTypes["alignreport"] = tempOutNames;
72 outputTypes["list"] = tempOutNames;
75 m->errorOut(e, "GetLineageCommand", "GetLineageCommand");
79 //**********************************************************************************************************************
80 GetLineageCommand::GetLineageCommand(string option) {
82 abort = false; calledHelp = false;
84 //allow user to run help
85 if(option == "help") { help(); abort = true; calledHelp = true; }
88 vector<string> myArray = setParameters();
90 OptionParser parser(option);
91 map<string,string> parameters = parser.getParameters();
93 ValidParameters validParameter;
94 map<string,string>::iterator it;
96 //check to make sure all parameters are valid for command
97 for (it = parameters.begin(); it != parameters.end(); it++) {
98 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
101 //initialize outputTypes
102 vector<string> tempOutNames;
103 outputTypes["fasta"] = tempOutNames;
104 outputTypes["taxonomy"] = tempOutNames;
105 outputTypes["name"] = tempOutNames;
106 outputTypes["group"] = tempOutNames;
107 outputTypes["alignreport"] = tempOutNames;
108 outputTypes["list"] = tempOutNames;
110 //if the user changes the output directory command factory will send this info to us in the output parameter
111 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
113 //if the user changes the input directory command factory will send this info to us in the output parameter
114 string inputDir = validParameter.validFile(parameters, "inputdir", false);
115 if (inputDir == "not found"){ inputDir = ""; }
118 it = parameters.find("alignreport");
119 //user has given a template file
120 if(it != parameters.end()){
121 path = m->hasPath(it->second);
122 //if the user has not given a path then, add inputdir. else leave path alone.
123 if (path == "") { parameters["alignreport"] = inputDir + it->second; }
126 it = parameters.find("fasta");
127 //user has given a template file
128 if(it != parameters.end()){
129 path = m->hasPath(it->second);
130 //if the user has not given a path then, add inputdir. else leave path alone.
131 if (path == "") { parameters["fasta"] = inputDir + it->second; }
134 it = parameters.find("list");
135 //user has given a template file
136 if(it != parameters.end()){
137 path = m->hasPath(it->second);
138 //if the user has not given a path then, add inputdir. else leave path alone.
139 if (path == "") { parameters["list"] = inputDir + it->second; }
142 it = parameters.find("name");
143 //user has given a template file
144 if(it != parameters.end()){
145 path = m->hasPath(it->second);
146 //if the user has not given a path then, add inputdir. else leave path alone.
147 if (path == "") { parameters["name"] = inputDir + it->second; }
150 it = parameters.find("group");
151 //user has given a template file
152 if(it != parameters.end()){
153 path = m->hasPath(it->second);
154 //if the user has not given a path then, add inputdir. else leave path alone.
155 if (path == "") { parameters["group"] = inputDir + it->second; }
158 it = parameters.find("taxonomy");
159 //user has given a template file
160 if(it != parameters.end()){
161 path = m->hasPath(it->second);
162 //if the user has not given a path then, add inputdir. else leave path alone.
163 if (path == "") { parameters["taxonomy"] = inputDir + it->second; }
168 //check for required parameters
169 fastafile = validParameter.validFile(parameters, "fasta", true);
170 if (fastafile == "not open") { abort = true; }
171 else if (fastafile == "not found") { fastafile = ""; }
173 namefile = validParameter.validFile(parameters, "name", true);
174 if (namefile == "not open") { abort = true; }
175 else if (namefile == "not found") { namefile = ""; }
177 groupfile = validParameter.validFile(parameters, "group", true);
178 if (groupfile == "not open") { abort = true; }
179 else if (groupfile == "not found") { groupfile = ""; }
181 alignfile = validParameter.validFile(parameters, "alignreport", true);
182 if (alignfile == "not open") { abort = true; }
183 else if (alignfile == "not found") { alignfile = ""; }
185 listfile = validParameter.validFile(parameters, "list", true);
186 if (listfile == "not open") { abort = true; }
187 else if (listfile == "not found") { listfile = ""; }
189 taxfile = validParameter.validFile(parameters, "taxonomy", true);
190 if (taxfile == "not open") { abort = true; }
191 else if (taxfile == "not found") {
192 taxfile = m->getTaxonomyFile();
193 if (taxfile != "") { m->mothurOut("Using " + taxfile + " as input file for the taxonomy parameter."); m->mothurOutEndLine(); }
194 else { m->mothurOut("You have no current taxonomy file and the taxonomy parameter is required."); m->mothurOutEndLine(); abort = true; }
197 string usedDups = "true";
198 string temp = validParameter.validFile(parameters, "dups", false);
199 if (temp == "not found") {
200 if (namefile != "") { temp = "true"; }
201 else { temp = "false"; usedDups = ""; }
203 dups = m->isTrue(temp);
205 taxons = validParameter.validFile(parameters, "taxon", false);
206 if (taxons == "not found") { taxons = ""; m->mothurOut("No taxons given, please correct."); m->mothurOutEndLine(); abort = true; }
209 if (taxons[0] == '\'') { taxons = taxons.substr(1); }
210 if (taxons[(taxons.length()-1)] == '\'') { taxons = taxons.substr(0, (taxons.length()-1)); }
214 if ((fastafile == "") && (namefile == "") && (groupfile == "") && (alignfile == "") && (listfile == "") && (taxfile == "")) { m->mothurOut("You must provide one of the following: fasta, name, group, alignreport, taxonomy or listfile."); m->mothurOutEndLine(); abort = true; }
218 catch(exception& e) {
219 m->errorOut(e, "GetLineageCommand", "GetLineageCommand");
223 //**********************************************************************************************************************
225 int GetLineageCommand::execute(){
228 if (abort == true) { if (calledHelp) { return 0; } return 2; }
230 if (m->control_pressed) { return 0; }
232 //read through the correct file and output lines you want to keep
233 if (taxfile != "") { readTax(); } //fills the set of names to get
234 if (namefile != "") { readName(); }
235 if (fastafile != "") { readFasta(); }
236 if (groupfile != "") { readGroup(); }
237 if (alignfile != "") { readAlign(); }
238 if (listfile != "") { readList(); }
241 if (m->control_pressed) { outputTypes.clear(); for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
243 if (outputNames.size() != 0) {
244 m->mothurOutEndLine();
245 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
246 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
247 m->mothurOutEndLine();
249 //set fasta file as new current fastafile
251 itTypes = outputTypes.find("fasta");
252 if (itTypes != outputTypes.end()) {
253 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
256 itTypes = outputTypes.find("name");
257 if (itTypes != outputTypes.end()) {
258 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); }
261 itTypes = outputTypes.find("group");
262 if (itTypes != outputTypes.end()) {
263 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setGroupFile(current); }
266 itTypes = outputTypes.find("list");
267 if (itTypes != outputTypes.end()) {
268 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setListFile(current); }
271 itTypes = outputTypes.find("taxonomy");
272 if (itTypes != outputTypes.end()) {
273 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setTaxonomyFile(current); }
280 catch(exception& e) {
281 m->errorOut(e, "GetLineageCommand", "execute");
286 //**********************************************************************************************************************
287 int GetLineageCommand::readFasta(){
289 string thisOutputDir = outputDir;
290 if (outputDir == "") { thisOutputDir += m->hasPath(fastafile); }
291 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(fastafile)) + "pick" + m->getExtension(fastafile);
293 m->openOutputFile(outputFileName, out);
297 m->openInputFile(fastafile, in);
300 bool wroteSomething = false;
304 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
306 Sequence currSeq(in);
307 name = currSeq.getName();
310 //if this name is in the accnos file
311 if (names.count(name) != 0) {
312 wroteSomething = true;
314 currSeq.printSequence(out);
322 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
323 outputNames.push_back(outputFileName); outputTypes["fasta"].push_back(outputFileName);
328 catch(exception& e) {
329 m->errorOut(e, "GetLineageCommand", "readFasta");
333 //**********************************************************************************************************************
334 int GetLineageCommand::readList(){
336 string thisOutputDir = outputDir;
337 if (outputDir == "") { thisOutputDir += m->hasPath(listfile); }
338 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(listfile)) + "pick" + m->getExtension(listfile);
340 m->openOutputFile(outputFileName, out);
343 m->openInputFile(listfile, in);
345 bool wroteSomething = false;
349 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
351 //read in list vector
354 //make a new list vector
356 newList.setLabel(list.getLabel());
359 for (int i = 0; i < list.getNumBins(); i++) {
361 //parse out names that are in accnos file
362 string binnames = list.get(i);
364 string newNames = "";
365 while (binnames.find_first_of(',') != -1) {
366 string name = binnames.substr(0,binnames.find_first_of(','));
367 binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
369 //if that name is in the .accnos file, add it
370 if (names.count(name) != 0) { newNames += name + ","; }
374 if (names.count(binnames) != 0) { newNames += binnames + ","; }
376 //if there are names in this bin add to new list
377 if (newNames != "") {
378 newNames = newNames.substr(0, newNames.length()-1); //rip off extra comma
379 newList.push_back(newNames);
383 //print new listvector
384 if (newList.getNumBins() != 0) {
385 wroteSomething = true;
394 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
395 outputNames.push_back(outputFileName); outputTypes["list"].push_back(outputFileName);
400 catch(exception& e) {
401 m->errorOut(e, "GetLineageCommand", "readList");
405 //**********************************************************************************************************************
406 int GetLineageCommand::readName(){
408 string thisOutputDir = outputDir;
409 if (outputDir == "") { thisOutputDir += m->hasPath(namefile); }
410 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(namefile)) + "pick" + m->getExtension(namefile);
412 m->openOutputFile(outputFileName, out);
416 m->openInputFile(namefile, in);
417 string name, firstCol, secondCol;
419 bool wroteSomething = false;
424 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
430 if (dups) { hold = secondCol; }
432 vector<string> parsedNames;
433 m->splitAtComma(secondCol, parsedNames);
435 vector<string> validSecond;
436 for (int i = 0; i < parsedNames.size(); i++) {
437 if (names.count(parsedNames[i]) != 0) {
438 validSecond.push_back(parsedNames[i]);
442 if ((dups) && (validSecond.size() != 0)) { //dups = true and we want to add someone, then add everyone
443 for (int i = 0; i < parsedNames.size(); i++) { names.insert(parsedNames[i]); }
444 out << firstCol << '\t' << hold << endl;
445 wroteSomething = true;
447 //if the name in the first column is in the set then print it and any other names in second column also in set
448 if (names.count(firstCol) != 0) {
450 wroteSomething = true;
452 out << firstCol << '\t';
454 //you know you have at least one valid second since first column is valid
455 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
456 out << validSecond[validSecond.size()-1] << endl;
459 //make first name in set you come to first column and then add the remaining names to second column
461 //you want part of this row
462 if (validSecond.size() != 0) {
464 wroteSomething = true;
466 out << validSecond[0] << '\t';
468 //you know you have at least one valid second since first column is valid
469 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
470 out << validSecond[validSecond.size()-1] << endl;
479 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
480 outputNames.push_back(outputFileName); outputTypes["name"].push_back(outputFileName);
485 catch(exception& e) {
486 m->errorOut(e, "GetLineageCommand", "readName");
491 //**********************************************************************************************************************
492 int GetLineageCommand::readGroup(){
494 string thisOutputDir = outputDir;
495 if (outputDir == "") { thisOutputDir += m->hasPath(groupfile); }
496 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(groupfile)) + "pick" + m->getExtension(groupfile);
498 m->openOutputFile(outputFileName, out);
502 m->openInputFile(groupfile, in);
505 bool wroteSomething = false;
509 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
512 in >> name; //read from first column
513 in >> group; //read from second column
515 //if this name is in the accnos file
516 if (names.count(name) != 0) {
517 wroteSomething = true;
519 out << name << '\t' << group << endl;
527 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
528 outputNames.push_back(outputFileName); outputTypes["group"].push_back(outputFileName);
533 catch(exception& e) {
534 m->errorOut(e, "GetLineageCommand", "readGroup");
538 //**********************************************************************************************************************
539 int GetLineageCommand::readTax(){
541 string thisOutputDir = outputDir;
542 if (outputDir == "") { thisOutputDir += m->hasPath(taxfile); }
543 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(taxfile)) + "pick" + m->getExtension(taxfile);
545 m->openOutputFile(outputFileName, out);
548 m->openInputFile(taxfile, in);
551 //bool wroteSomething = false;
553 bool taxonsHasConfidence = false;
554 vector< map<string, float> > searchTaxons;
555 string noConfidenceTaxons = taxons;
556 int hasConPos = taxons.find_first_of('(');
557 if (hasConPos != string::npos) {
558 taxonsHasConfidence = true;
559 searchTaxons = getTaxons(taxons);
560 noConfidenceTaxons = removeConfidences(taxons);
566 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
568 in >> name; //read from first column
569 in >> tax; //read from second column
574 //if the users file contains confidence scores we want to ignore them when searching for the taxons, unless the taxon has them
575 if (!taxonsHasConfidence) {
576 int hasConfidences = tax.find_first_of('(');
577 if (hasConfidences != string::npos) {
578 newtax = removeConfidences(tax);
581 int pos = newtax.find(taxons);
583 if (pos != string::npos) { //this sequence contains the taxon the user wants
585 out << name << '\t' << tax << endl;
588 }else{//if taxons has them and you don't them remove taxons
589 int hasConfidences = tax.find_first_of('(');
590 if (hasConfidences == string::npos) {
592 int pos = newtax.find(noConfidenceTaxons);
594 if (pos != string::npos) { //this sequence contains the taxon the user wants
596 out << name << '\t' << tax << endl;
598 }else { //both have confidences so we want to make sure the users confidences are greater then or equal to the taxons
599 //first remove confidences from both and see if the taxonomy exists
601 string noNewTax = tax;
602 int hasConfidences = tax.find_first_of('(');
603 if (hasConfidences != string::npos) {
604 noNewTax = removeConfidences(tax);
607 int pos = noNewTax.find(noConfidenceTaxons);
609 if (pos != string::npos) { //if yes, then are the confidences okay
612 vector< map<string, float> > usersTaxon = getTaxons(newtax);
614 //the usersTaxon is most likely longer than the searchTaxons, and searchTaxon[0] may relate to userTaxon[4]
615 //we want to "line them up", so we will find the the index where the searchstring starts
617 for (int i = 0; i < usersTaxon.size(); i++) {
619 if (usersTaxon[i].begin()->first == searchTaxons[0].begin()->first) {
622 bool goodspot = true;
623 //is this really the start, or are we dealing with a taxon of the same name?
624 while ((spot < searchTaxons.size()) && ((i+spot) < usersTaxon.size())) {
625 if (usersTaxon[i+spot].begin()->first != searchTaxons[spot].begin()->first) { goodspot = false; break; }
629 if (goodspot) { break; }
633 for (int i = 0; i < searchTaxons.size(); i++) {
635 if ((i+index) < usersTaxon.size()) { //just in case, should never be false
636 if (usersTaxon[i+index].begin()->second < searchTaxons[i].begin()->second) { //is the users cutoff less than the search taxons
646 //passed the test so add you
649 out << name << '\t' << tax << endl;
662 if (names.size() == 0) { m->mothurOut("Your taxonomy file does not contain any sequences from " + taxons + "."); m->mothurOutEndLine(); }
663 outputNames.push_back(outputFileName); outputTypes["taxonomy"].push_back(outputFileName);
668 catch(exception& e) {
669 m->errorOut(e, "GetLineageCommand", "readTax");
673 /**************************************************************************************************/
674 vector< map<string, float> > GetLineageCommand::getTaxons(string tax) {
677 vector< map<string, float> > t;
679 int taxLength = tax.length();
680 for(int i=0;i<taxLength;i++){
683 int openParen = taxon.find_first_of('(');
684 int closeParen = taxon.find_last_of(')');
686 string newtaxon, confidence;
687 if ((openParen != string::npos) && (closeParen != string::npos)) {
688 newtaxon = taxon.substr(0, openParen); //rip off confidence
689 confidence = taxon.substr((openParen+1), (closeParen-openParen-1));
695 convert(confidence, con);
697 map<string, float> temp;
698 temp[newtaxon] = con;
710 catch(exception& e) {
711 m->errorOut(e, "GetLineageCommand", "getTaxons");
715 /**************************************************************************************************/
716 string GetLineageCommand::removeConfidences(string tax) {
720 int taxLength = tax.length();
721 for(int i=0;i<taxLength;i++){
723 taxon = taxon.substr(0, taxon.find_first_of('(')); //rip off confidence
733 catch(exception& e) {
734 m->errorOut(e, "GetLineageCommand", "removeConfidences");
738 //**********************************************************************************************************************
739 //alignreport file has a column header line then all other lines contain 16 columns. we just want the first column since that contains the name
740 int GetLineageCommand::readAlign(){
742 string thisOutputDir = outputDir;
743 if (outputDir == "") { thisOutputDir += m->hasPath(alignfile); }
744 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(alignfile)) + "pick.align.report";
746 m->openOutputFile(outputFileName, out);
750 m->openInputFile(alignfile, in);
753 bool wroteSomething = false;
755 //read column headers
756 for (int i = 0; i < 16; i++) {
757 if (!in.eof()) { in >> junk; out << junk << '\t'; }
764 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
767 in >> name; //read from first column
769 //if this name is in the accnos file
770 if (names.count(name) != 0) {
771 wroteSomething = true;
776 for (int i = 0; i < 15; i++) {
777 if (!in.eof()) { in >> junk; out << junk << '\t'; }
782 }else {//still read just don't do anything with it
784 for (int i = 0; i < 15; i++) {
785 if (!in.eof()) { in >> junk; }
795 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
796 outputNames.push_back(outputFileName); outputTypes["alignreport"].push_back(outputFileName);
801 catch(exception& e) {
802 m->errorOut(e, "GetLineageCommand", "readAlign");
806 //**********************************************************************************************************************