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 //**********************************************************************************************************************
16 GetLineageCommand::GetLineageCommand(string option) {
20 //allow user to run help
21 if(option == "help") { help(); abort = true; }
24 //valid paramters for this command
25 string Array[] = {"fasta","name", "group", "alignreport", "taxon", "dups", "list","taxonomy","outputdir","inputdir"};
26 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
28 OptionParser parser(option);
29 map<string,string> parameters = parser.getParameters();
31 ValidParameters validParameter;
32 map<string,string>::iterator it;
34 //check to make sure all parameters are valid for command
35 for (it = parameters.begin(); it != parameters.end(); it++) {
36 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
39 //if the user changes the output directory command factory will send this info to us in the output parameter
40 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
42 //if the user changes the input directory command factory will send this info to us in the output parameter
43 string inputDir = validParameter.validFile(parameters, "inputdir", false);
44 if (inputDir == "not found"){ inputDir = ""; }
47 it = parameters.find("alignreport");
48 //user has given a template file
49 if(it != parameters.end()){
50 path = m->hasPath(it->second);
51 //if the user has not given a path then, add inputdir. else leave path alone.
52 if (path == "") { parameters["alignreport"] = inputDir + it->second; }
55 it = parameters.find("fasta");
56 //user has given a template file
57 if(it != parameters.end()){
58 path = m->hasPath(it->second);
59 //if the user has not given a path then, add inputdir. else leave path alone.
60 if (path == "") { parameters["fasta"] = inputDir + it->second; }
63 it = parameters.find("list");
64 //user has given a template file
65 if(it != parameters.end()){
66 path = m->hasPath(it->second);
67 //if the user has not given a path then, add inputdir. else leave path alone.
68 if (path == "") { parameters["list"] = inputDir + it->second; }
71 it = parameters.find("name");
72 //user has given a template file
73 if(it != parameters.end()){
74 path = m->hasPath(it->second);
75 //if the user has not given a path then, add inputdir. else leave path alone.
76 if (path == "") { parameters["name"] = inputDir + it->second; }
79 it = parameters.find("group");
80 //user has given a template file
81 if(it != parameters.end()){
82 path = m->hasPath(it->second);
83 //if the user has not given a path then, add inputdir. else leave path alone.
84 if (path == "") { parameters["group"] = inputDir + it->second; }
87 it = parameters.find("taxonomy");
88 //user has given a template file
89 if(it != parameters.end()){
90 path = m->hasPath(it->second);
91 //if the user has not given a path then, add inputdir. else leave path alone.
92 if (path == "") { parameters["taxonomy"] = inputDir + it->second; }
97 //check for required parameters
98 fastafile = validParameter.validFile(parameters, "fasta", true);
99 if (fastafile == "not open") { abort = true; }
100 else if (fastafile == "not found") { fastafile = ""; }
102 namefile = validParameter.validFile(parameters, "name", true);
103 if (namefile == "not open") { abort = true; }
104 else if (namefile == "not found") { namefile = ""; }
106 groupfile = validParameter.validFile(parameters, "group", true);
107 if (groupfile == "not open") { abort = true; }
108 else if (groupfile == "not found") { groupfile = ""; }
110 alignfile = validParameter.validFile(parameters, "alignreport", true);
111 if (alignfile == "not open") { abort = true; }
112 else if (alignfile == "not found") { alignfile = ""; }
114 listfile = validParameter.validFile(parameters, "list", true);
115 if (listfile == "not open") { abort = true; }
116 else if (listfile == "not found") { listfile = ""; }
118 taxfile = validParameter.validFile(parameters, "taxonomy", true);
119 if (taxfile == "not open") { abort = true; }
120 else if (taxfile == "not found") { taxfile = ""; m->mothurOut("The taxonomy parameter is required for the get.lineage command."); m->mothurOutEndLine(); abort = true; }
122 string usedDups = "true";
123 string temp = validParameter.validFile(parameters, "dups", false); if (temp == "not found") { temp = "false"; usedDups = ""; }
124 dups = m->isTrue(temp);
126 taxons = validParameter.validFile(parameters, "taxon", false);
127 if (taxons == "not found") { taxons = ""; m->mothurOut("No taxons given, please correct."); m->mothurOutEndLine(); abort = true; }
130 if (taxons[0] == '\'') { taxons = taxons.substr(1); }
131 if (taxons[(taxons.length()-1)] == '\'') { taxons = taxons.substr(0, (taxons.length()-1)); }
135 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; }
137 if ((usedDups != "") && (namefile == "")) { m->mothurOut("You may only use dups with the name option."); m->mothurOutEndLine(); abort = true; }
142 catch(exception& e) {
143 m->errorOut(e, "GetLineageCommand", "GetLineageCommand");
147 //**********************************************************************************************************************
149 void GetLineageCommand::help(){
151 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");
152 m->mothurOut("It outputs a file containing only the sequences from the taxonomy file that are from the taxon requested.\n");
153 m->mothurOut("The get.lineage command parameters are taxon, fasta, name, group, list, taxonomy, alignreport and dups. You must provide taxonomy and taxon.\n");
154 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");
155 m->mothurOut("The taxon parameter allows you to select the taxons you would like to get.\n");
156 m->mothurOut("The get.lineage command should be in the following format: get.lineage(taxonomy=yourTaxonomyFile, taxon=yourTaxons).\n");
157 m->mothurOut("Example get.lineage(taxonomy=amazon.silva.taxonomy, taxon=Bacteria;Firmicutes;Bacilli;Lactobacillales;).\n");
158 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");
159 m->mothurOut("Example get.lineage(taxonomy=amazon.silva.taxonomy, taxon='Bacteria;Firmicutes;Bacilli;Lactobacillales;').\n");
160 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
162 catch(exception& e) {
163 m->errorOut(e, "GetLineageCommand", "help");
168 //**********************************************************************************************************************
170 int GetLineageCommand::execute(){
173 if (abort == true) { return 0; }
175 if (m->control_pressed) { return 0; }
177 //read through the correct file and output lines you want to keep
178 if (taxfile != "") { readTax(); } //fills the set of names to get
179 if (namefile != "") { readName(); }
180 if (fastafile != "") { readFasta(); }
181 if (groupfile != "") { readGroup(); }
182 if (alignfile != "") { readAlign(); }
183 if (listfile != "") { readList(); }
186 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
188 if (outputNames.size() != 0) {
189 m->mothurOutEndLine();
190 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
191 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
192 m->mothurOutEndLine();
198 catch(exception& e) {
199 m->errorOut(e, "GetLineageCommand", "execute");
204 //**********************************************************************************************************************
205 int GetLineageCommand::readFasta(){
207 string thisOutputDir = outputDir;
208 if (outputDir == "") { thisOutputDir += m->hasPath(fastafile); }
209 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(fastafile)) + "pick" + m->getExtension(fastafile);
211 m->openOutputFile(outputFileName, out);
215 m->openInputFile(fastafile, in);
218 bool wroteSomething = false;
222 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
224 Sequence currSeq(in);
225 name = currSeq.getName();
228 //if this name is in the accnos file
229 if (names.count(name) != 0) {
230 wroteSomething = true;
232 currSeq.printSequence(out);
240 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
241 outputNames.push_back(outputFileName);
246 catch(exception& e) {
247 m->errorOut(e, "GetLineageCommand", "readFasta");
251 //**********************************************************************************************************************
252 int GetLineageCommand::readList(){
254 string thisOutputDir = outputDir;
255 if (outputDir == "") { thisOutputDir += m->hasPath(listfile); }
256 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(listfile)) + "pick" + m->getExtension(listfile);
258 m->openOutputFile(outputFileName, out);
261 m->openInputFile(listfile, in);
263 bool wroteSomething = false;
267 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
269 //read in list vector
272 //make a new list vector
274 newList.setLabel(list.getLabel());
277 for (int i = 0; i < list.getNumBins(); i++) {
279 //parse out names that are in accnos file
280 string binnames = list.get(i);
282 string newNames = "";
283 while (binnames.find_first_of(',') != -1) {
284 string name = binnames.substr(0,binnames.find_first_of(','));
285 binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
287 //if that name is in the .accnos file, add it
288 if (names.count(name) != 0) { newNames += name + ","; }
292 if (names.count(binnames) != 0) { newNames += binnames + ","; }
294 //if there are names in this bin add to new list
295 if (newNames != "") {
296 newNames = newNames.substr(0, newNames.length()-1); //rip off extra comma
297 newList.push_back(newNames);
301 //print new listvector
302 if (newList.getNumBins() != 0) {
303 wroteSomething = true;
312 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
313 outputNames.push_back(outputFileName);
318 catch(exception& e) {
319 m->errorOut(e, "GetLineageCommand", "readList");
323 //**********************************************************************************************************************
324 int GetLineageCommand::readName(){
326 string thisOutputDir = outputDir;
327 if (outputDir == "") { thisOutputDir += m->hasPath(namefile); }
328 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(namefile)) + "pick" + m->getExtension(namefile);
330 m->openOutputFile(outputFileName, out);
334 m->openInputFile(namefile, in);
335 string name, firstCol, secondCol;
337 bool wroteSomething = false;
342 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
348 if (dups) { hold = secondCol; }
350 vector<string> parsedNames;
351 //parse second column saving each name
352 while (secondCol.find_first_of(',') != -1) {
353 name = secondCol.substr(0,secondCol.find_first_of(','));
354 secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
355 parsedNames.push_back(name);
358 //get name after last ,
359 parsedNames.push_back(secondCol);
361 vector<string> validSecond;
362 for (int i = 0; i < parsedNames.size(); i++) {
363 if (names.count(parsedNames[i]) != 0) {
364 validSecond.push_back(parsedNames[i]);
368 if ((dups) && (validSecond.size() != 0)) { //dups = true and we want to add someone, then add everyone
369 for (int i = 0; i < parsedNames.size(); i++) { names.insert(parsedNames[i]); }
370 out << firstCol << '\t' << hold << endl;
371 wroteSomething = true;
373 //if the name in the first column is in the set then print it and any other names in second column also in set
374 if (names.count(firstCol) != 0) {
376 wroteSomething = true;
378 out << firstCol << '\t';
380 //you know you have at least one valid second since first column is valid
381 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
382 out << validSecond[validSecond.size()-1] << endl;
385 //make first name in set you come to first column and then add the remaining names to second column
387 //you want part of this row
388 if (validSecond.size() != 0) {
390 wroteSomething = true;
392 out << validSecond[0] << '\t';
394 //you know you have at least one valid second since first column is valid
395 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
396 out << validSecond[validSecond.size()-1] << endl;
405 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
406 outputNames.push_back(outputFileName);
411 catch(exception& e) {
412 m->errorOut(e, "GetLineageCommand", "readName");
417 //**********************************************************************************************************************
418 int GetLineageCommand::readGroup(){
420 string thisOutputDir = outputDir;
421 if (outputDir == "") { thisOutputDir += m->hasPath(groupfile); }
422 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(groupfile)) + "pick" + m->getExtension(groupfile);
424 m->openOutputFile(outputFileName, out);
428 m->openInputFile(groupfile, in);
431 bool wroteSomething = false;
435 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
438 in >> name; //read from first column
439 in >> group; //read from second column
441 //if this name is in the accnos file
442 if (names.count(name) != 0) {
443 wroteSomething = true;
445 out << name << '\t' << group << endl;
453 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
454 outputNames.push_back(outputFileName);
459 catch(exception& e) {
460 m->errorOut(e, "GetLineageCommand", "readGroup");
464 //**********************************************************************************************************************
465 int GetLineageCommand::readTax(){
467 string thisOutputDir = outputDir;
468 if (outputDir == "") { thisOutputDir += m->hasPath(taxfile); }
469 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(taxfile)) + "pick" + m->getExtension(taxfile);
471 m->openOutputFile(outputFileName, out);
474 m->openInputFile(taxfile, in);
477 bool wroteSomething = false;
481 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
483 in >> name; //read from first column
484 in >> tax; //read from second column
488 //if the users file contains confidence scores we want to ignore them when searching for the taxons
489 int hasConfidences = tax.find_first_of('(');
490 if (hasConfidences != string::npos) {
491 newtax = removeConfidences(tax);
494 int pos = newtax.find(taxons);
496 if (pos != string::npos) { //this sequence contains the taxon the user wants
498 out << name << '\t' << tax << endl;
506 if (names.size() == 0) { m->mothurOut("Your taxonomy file does not contain any sequences from " + taxons + "."); m->mothurOutEndLine(); }
507 outputNames.push_back(outputFileName);
512 catch(exception& e) {
513 m->errorOut(e, "GetLineageCommand", "readTax");
517 /**************************************************************************************************/
518 string GetLineageCommand::removeConfidences(string tax) {
522 int taxLength = tax.length();
523 for(int i=0;i<taxLength;i++){
525 taxon = taxon.substr(0, taxon.find_first_of('(')); //rip off confidence
535 catch(exception& e) {
536 m->errorOut(e, "GetLineageCommand", "removeConfidences");
540 //**********************************************************************************************************************
541 //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
542 int GetLineageCommand::readAlign(){
544 string thisOutputDir = outputDir;
545 if (outputDir == "") { thisOutputDir += m->hasPath(alignfile); }
546 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(alignfile)) + "pick.align.report";
548 m->openOutputFile(outputFileName, out);
552 m->openInputFile(alignfile, in);
555 bool wroteSomething = false;
557 //read column headers
558 for (int i = 0; i < 16; i++) {
559 if (!in.eof()) { in >> junk; out << junk << '\t'; }
566 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
569 in >> name; //read from first column
571 //if this name is in the accnos file
572 if (names.count(name) != 0) {
573 wroteSomething = true;
578 for (int i = 0; i < 15; i++) {
579 if (!in.eof()) { in >> junk; out << junk << '\t'; }
584 }else {//still read just don't do anything with it
586 for (int i = 0; i < 15; i++) {
587 if (!in.eof()) { in >> junk; }
597 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
598 outputNames.push_back(outputFileName);
603 catch(exception& e) {
604 m->errorOut(e, "GetLineageCommand", "readAlign");
608 //**********************************************************************************************************************