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"
15 //**********************************************************************************************************************
16 vector<string> GetLineageCommand::getValidParameters(){
18 string Array[] = {"fasta","name", "group", "alignreport", "taxon", "dups", "list","taxonomy","outputdir","inputdir"};
19 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
23 m->errorOut(e, "GetLineageCommand", "getValidParameters");
27 //**********************************************************************************************************************
28 GetLineageCommand::GetLineageCommand(){
30 //initialize outputTypes
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;
40 m->errorOut(e, "GetLineageCommand", "GetLineageCommand");
44 //**********************************************************************************************************************
45 vector<string> GetLineageCommand::getRequiredParameters(){
47 string Array[] = {"taxonomy"};
48 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
52 m->errorOut(e, "GetLineageCommand", "getRequiredParameters");
56 //**********************************************************************************************************************
57 vector<string> GetLineageCommand::getRequiredFiles(){
59 vector<string> myArray;
63 m->errorOut(e, "GetLineageCommand", "getRequiredFiles");
67 //**********************************************************************************************************************
68 GetLineageCommand::GetLineageCommand(string option) {
72 //allow user to run help
73 if(option == "help") { help(); abort = true; }
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)));
80 OptionParser parser(option);
81 map<string,string> parameters = parser.getParameters();
83 ValidParameters validParameter;
84 map<string,string>::iterator it;
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; }
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;
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 = ""; }
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 = ""; }
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; }
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; }
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; }
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; }
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; }
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; }
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 = ""; }
163 namefile = validParameter.validFile(parameters, "name", true);
164 if (namefile == "not open") { abort = true; }
165 else if (namefile == "not found") { namefile = ""; }
167 groupfile = validParameter.validFile(parameters, "group", true);
168 if (groupfile == "not open") { abort = true; }
169 else if (groupfile == "not found") { groupfile = ""; }
171 alignfile = validParameter.validFile(parameters, "alignreport", true);
172 if (alignfile == "not open") { abort = true; }
173 else if (alignfile == "not found") { alignfile = ""; }
175 listfile = validParameter.validFile(parameters, "list", true);
176 if (listfile == "not open") { abort = true; }
177 else if (listfile == "not found") { listfile = ""; }
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; }
183 string usedDups = "true";
184 string temp = validParameter.validFile(parameters, "dups", false); if (temp == "not found") { temp = "false"; usedDups = ""; }
185 dups = m->isTrue(temp);
187 taxons = validParameter.validFile(parameters, "taxon", false);
188 if (taxons == "not found") { taxons = ""; m->mothurOut("No taxons given, please correct."); m->mothurOutEndLine(); abort = true; }
191 if (taxons[0] == '\'') { taxons = taxons.substr(1); }
192 if (taxons[(taxons.length()-1)] == '\'') { taxons = taxons.substr(0, (taxons.length()-1)); }
196 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; }
198 if ((usedDups != "") && (namefile == "")) { m->mothurOut("You may only use dups with the name option."); m->mothurOutEndLine(); abort = true; }
203 catch(exception& e) {
204 m->errorOut(e, "GetLineageCommand", "GetLineageCommand");
208 //**********************************************************************************************************************
210 void GetLineageCommand::help(){
212 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");
213 m->mothurOut("It outputs a file containing only the sequences from the taxonomy file that are from the taxon requested.\n");
214 m->mothurOut("The get.lineage command parameters are taxon, fasta, name, group, list, taxonomy, alignreport and dups. You must provide taxonomy and taxon.\n");
215 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");
216 m->mothurOut("The taxon parameter allows you to select the taxons you would like to get.\n");
217 m->mothurOut("The get.lineage command should be in the following format: get.lineage(taxonomy=yourTaxonomyFile, taxon=yourTaxons).\n");
218 m->mothurOut("Example get.lineage(taxonomy=amazon.silva.taxonomy, taxon=Bacteria;Firmicutes;Bacilli;Lactobacillales;).\n");
219 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");
220 m->mothurOut("Example get.lineage(taxonomy=amazon.silva.taxonomy, taxon='Bacteria;Firmicutes;Bacilli;Lactobacillales;').\n");
221 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
223 catch(exception& e) {
224 m->errorOut(e, "GetLineageCommand", "help");
229 //**********************************************************************************************************************
231 int GetLineageCommand::execute(){
234 if (abort == true) { return 0; }
236 if (m->control_pressed) { return 0; }
238 //read through the correct file and output lines you want to keep
239 if (taxfile != "") { readTax(); } //fills the set of names to get
240 if (namefile != "") { readName(); }
241 if (fastafile != "") { readFasta(); }
242 if (groupfile != "") { readGroup(); }
243 if (alignfile != "") { readAlign(); }
244 if (listfile != "") { readList(); }
247 if (m->control_pressed) { outputTypes.clear(); for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
249 if (outputNames.size() != 0) {
250 m->mothurOutEndLine();
251 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
252 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
253 m->mothurOutEndLine();
259 catch(exception& e) {
260 m->errorOut(e, "GetLineageCommand", "execute");
265 //**********************************************************************************************************************
266 int GetLineageCommand::readFasta(){
268 string thisOutputDir = outputDir;
269 if (outputDir == "") { thisOutputDir += m->hasPath(fastafile); }
270 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(fastafile)) + "pick" + m->getExtension(fastafile);
272 m->openOutputFile(outputFileName, out);
276 m->openInputFile(fastafile, in);
279 bool wroteSomething = false;
283 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
285 Sequence currSeq(in);
286 name = currSeq.getName();
289 //if this name is in the accnos file
290 if (names.count(name) != 0) {
291 wroteSomething = true;
293 currSeq.printSequence(out);
301 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
302 outputNames.push_back(outputFileName); outputTypes["fasta"].push_back(outputFileName);
307 catch(exception& e) {
308 m->errorOut(e, "GetLineageCommand", "readFasta");
312 //**********************************************************************************************************************
313 int GetLineageCommand::readList(){
315 string thisOutputDir = outputDir;
316 if (outputDir == "") { thisOutputDir += m->hasPath(listfile); }
317 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(listfile)) + "pick" + m->getExtension(listfile);
319 m->openOutputFile(outputFileName, out);
322 m->openInputFile(listfile, in);
324 bool wroteSomething = false;
328 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
330 //read in list vector
333 //make a new list vector
335 newList.setLabel(list.getLabel());
338 for (int i = 0; i < list.getNumBins(); i++) {
340 //parse out names that are in accnos file
341 string binnames = list.get(i);
343 string newNames = "";
344 while (binnames.find_first_of(',') != -1) {
345 string name = binnames.substr(0,binnames.find_first_of(','));
346 binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
348 //if that name is in the .accnos file, add it
349 if (names.count(name) != 0) { newNames += name + ","; }
353 if (names.count(binnames) != 0) { newNames += binnames + ","; }
355 //if there are names in this bin add to new list
356 if (newNames != "") {
357 newNames = newNames.substr(0, newNames.length()-1); //rip off extra comma
358 newList.push_back(newNames);
362 //print new listvector
363 if (newList.getNumBins() != 0) {
364 wroteSomething = true;
373 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
374 outputNames.push_back(outputFileName); outputTypes["list"].push_back(outputFileName);
379 catch(exception& e) {
380 m->errorOut(e, "GetLineageCommand", "readList");
384 //**********************************************************************************************************************
385 int GetLineageCommand::readName(){
387 string thisOutputDir = outputDir;
388 if (outputDir == "") { thisOutputDir += m->hasPath(namefile); }
389 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(namefile)) + "pick" + m->getExtension(namefile);
391 m->openOutputFile(outputFileName, out);
395 m->openInputFile(namefile, in);
396 string name, firstCol, secondCol;
398 bool wroteSomething = false;
403 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
409 if (dups) { hold = secondCol; }
411 vector<string> parsedNames;
412 //parse second column saving each name
413 while (secondCol.find_first_of(',') != -1) {
414 name = secondCol.substr(0,secondCol.find_first_of(','));
415 secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
416 parsedNames.push_back(name);
419 //get name after last ,
420 parsedNames.push_back(secondCol);
422 vector<string> validSecond;
423 for (int i = 0; i < parsedNames.size(); i++) {
424 if (names.count(parsedNames[i]) != 0) {
425 validSecond.push_back(parsedNames[i]);
429 if ((dups) && (validSecond.size() != 0)) { //dups = true and we want to add someone, then add everyone
430 for (int i = 0; i < parsedNames.size(); i++) { names.insert(parsedNames[i]); }
431 out << firstCol << '\t' << hold << endl;
432 wroteSomething = true;
434 //if the name in the first column is in the set then print it and any other names in second column also in set
435 if (names.count(firstCol) != 0) {
437 wroteSomething = true;
439 out << firstCol << '\t';
441 //you know you have at least one valid second since first column is valid
442 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
443 out << validSecond[validSecond.size()-1] << endl;
446 //make first name in set you come to first column and then add the remaining names to second column
448 //you want part of this row
449 if (validSecond.size() != 0) {
451 wroteSomething = true;
453 out << validSecond[0] << '\t';
455 //you know you have at least one valid second since first column is valid
456 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
457 out << validSecond[validSecond.size()-1] << endl;
466 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
467 outputNames.push_back(outputFileName); outputTypes["name"].push_back(outputFileName);
472 catch(exception& e) {
473 m->errorOut(e, "GetLineageCommand", "readName");
478 //**********************************************************************************************************************
479 int GetLineageCommand::readGroup(){
481 string thisOutputDir = outputDir;
482 if (outputDir == "") { thisOutputDir += m->hasPath(groupfile); }
483 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(groupfile)) + "pick" + m->getExtension(groupfile);
485 m->openOutputFile(outputFileName, out);
489 m->openInputFile(groupfile, in);
492 bool wroteSomething = false;
496 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
499 in >> name; //read from first column
500 in >> group; //read from second column
502 //if this name is in the accnos file
503 if (names.count(name) != 0) {
504 wroteSomething = true;
506 out << name << '\t' << group << endl;
514 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
515 outputNames.push_back(outputFileName); outputTypes["group"].push_back(outputFileName);
520 catch(exception& e) {
521 m->errorOut(e, "GetLineageCommand", "readGroup");
525 //**********************************************************************************************************************
526 int GetLineageCommand::readTax(){
528 string thisOutputDir = outputDir;
529 if (outputDir == "") { thisOutputDir += m->hasPath(taxfile); }
530 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(taxfile)) + "pick" + m->getExtension(taxfile);
532 m->openOutputFile(outputFileName, out);
535 m->openInputFile(taxfile, in);
538 bool wroteSomething = false;
542 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
544 in >> name; //read from first column
545 in >> tax; //read from second column
549 //if the users file contains confidence scores we want to ignore them when searching for the taxons
550 int hasConfidences = tax.find_first_of('(');
551 if (hasConfidences != string::npos) {
552 newtax = removeConfidences(tax);
555 int pos = newtax.find(taxons);
557 if (pos != string::npos) { //this sequence contains the taxon the user wants
559 out << name << '\t' << tax << endl;
567 if (names.size() == 0) { m->mothurOut("Your taxonomy file does not contain any sequences from " + taxons + "."); m->mothurOutEndLine(); }
568 outputNames.push_back(outputFileName); outputTypes["taxonomy"].push_back(outputFileName);
573 catch(exception& e) {
574 m->errorOut(e, "GetLineageCommand", "readTax");
578 /**************************************************************************************************/
579 string GetLineageCommand::removeConfidences(string tax) {
583 int taxLength = tax.length();
584 for(int i=0;i<taxLength;i++){
586 taxon = taxon.substr(0, taxon.find_first_of('(')); //rip off confidence
596 catch(exception& e) {
597 m->errorOut(e, "GetLineageCommand", "removeConfidences");
601 //**********************************************************************************************************************
602 //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
603 int GetLineageCommand::readAlign(){
605 string thisOutputDir = outputDir;
606 if (outputDir == "") { thisOutputDir += m->hasPath(alignfile); }
607 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(alignfile)) + "pick.align.report";
609 m->openOutputFile(outputFileName, out);
613 m->openInputFile(alignfile, in);
616 bool wroteSomething = false;
618 //read column headers
619 for (int i = 0; i < 16; i++) {
620 if (!in.eof()) { in >> junk; out << junk << '\t'; }
627 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
630 in >> name; //read from first column
632 //if this name is in the accnos file
633 if (names.count(name) != 0) {
634 wroteSomething = true;
639 for (int i = 0; i < 15; i++) {
640 if (!in.eof()) { in >> junk; out << junk << '\t'; }
645 }else {//still read just don't do anything with it
647 for (int i = 0; i < 15; i++) {
648 if (!in.eof()) { in >> junk; }
658 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
659 outputNames.push_back(outputFileName); outputTypes["alignreport"].push_back(outputFileName);
664 catch(exception& e) {
665 m->errorOut(e, "GetLineageCommand", "readAlign");
669 //**********************************************************************************************************************