5 * Created by westcott on 11/20/09.
6 * Copyright 2009 Schloss Lab. All rights reserved.
10 #include "phylotypecommand.h"
11 #include "phylotree.h"
12 #include "listvector.hpp"
13 #include "rabundvector.hpp"
14 #include "sabundvector.hpp"
16 //**********************************************************************************************************************
17 vector<string> PhylotypeCommand::setParameters(){
19 CommandParameter ptaxonomy("taxonomy", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(ptaxonomy);
20 CommandParameter pname("name", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pname);
21 CommandParameter pcutoff("cutoff", "Number", "", "-1", "", "", "",false,false); parameters.push_back(pcutoff);
22 CommandParameter plabel("label", "String", "", "", "", "", "",false,false); parameters.push_back(plabel);
23 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
24 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
26 vector<string> myArray;
27 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
31 m->errorOut(e, "PhylotypeCommand", "setParameters");
35 //**********************************************************************************************************************
36 string PhylotypeCommand::getHelpString(){
38 string helpString = "";
39 helpString += "The phylotype command reads a taxonomy file and outputs a .list, .rabund and .sabund file. \n";
40 helpString += "The phylotype command parameter options are taxonomy, cutoff and label. The taxonomy parameter is required.\n";
41 helpString += "The cutoff parameter allows you to specify the level you want to stop at. The default is the highest level in your taxonomy file. \n";
42 helpString += "For example: taxonomy = Bacteria;Bacteroidetes-Chlorobi;Bacteroidetes; - cutoff=2, would truncate the taxonomy to Bacteria;Bacteroidetes-Chlorobi; \n";
43 helpString += "For the cutoff parameter levels count up from the root of the phylotree. This enables you to look at the grouping down to a specific resolution, say the genus level.\n";
44 helpString += "The label parameter allows you to specify which level you would like, and are separated by dashes. The default all levels in your taxonomy file. \n";
45 helpString += "For the label parameter, levels count down from the root to keep the output similiar to mothur's other commands which report information from finer resolution to coarser resolutions.\n";
46 helpString += "The phylotype command should be in the following format: \n";
47 helpString += "phylotype(taxonomy=yourTaxonomyFile, cutoff=yourCutoff, label=yourLabels) \n";
48 helpString += "Eaxample: phylotype(taxonomy=amazon.taxonomy, cutoff=5, label=1-3-5).\n";
52 m->errorOut(e, "PhylotypeCommand", "getHelpString");
57 //**********************************************************************************************************************
58 PhylotypeCommand::PhylotypeCommand(){
60 abort = true; calledHelp = true;
62 vector<string> tempOutNames;
63 outputTypes["list"] = tempOutNames;
64 outputTypes["sabund"] = tempOutNames;
65 outputTypes["rabund"] = tempOutNames;
68 m->errorOut(e, "PhylotypeCommand", "PhylotypeCommand");
72 /**********************************************************************************************************************/
73 PhylotypeCommand::PhylotypeCommand(string option) {
75 abort = false; calledHelp = false;
77 //allow user to run help
78 if(option == "help") { help(); abort = true; calledHelp = true; }
79 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
82 vector<string> myArray = setParameters();
84 OptionParser parser(option);
85 map<string, string> parameters = parser.getParameters();
87 ValidParameters validParameter;
88 map<string, string>::iterator it;
90 //check to make sure all parameters are valid for command
91 for (it = parameters.begin(); it != parameters.end(); it++) {
92 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
95 //initialize outputTypes
96 vector<string> tempOutNames;
97 outputTypes["list"] = tempOutNames;
98 outputTypes["sabund"] = tempOutNames;
99 outputTypes["rabund"] = tempOutNames;
101 //if the user changes the input directory command factory will send this info to us in the output parameter
102 string inputDir = validParameter.validFile(parameters, "inputdir", false);
103 if (inputDir == "not found"){ inputDir = ""; }
106 it = parameters.find("taxonomy");
107 //user has given a template file
108 if(it != parameters.end()){
109 path = m->hasPath(it->second);
110 //if the user has not given a path then, add inputdir. else leave path alone.
111 if (path == "") { parameters["taxonomy"] = inputDir + it->second; }
114 it = parameters.find("name");
115 //user has given a template file
116 if(it != parameters.end()){
117 path = m->hasPath(it->second);
118 //if the user has not given a path then, add inputdir. else leave path alone.
119 if (path == "") { parameters["name"] = inputDir + it->second; }
123 taxonomyFileName = validParameter.validFile(parameters, "taxonomy", true);
124 if (taxonomyFileName == "not found") {
125 taxonomyFileName = m->getTaxonomyFile();
126 if (taxonomyFileName != "") { m->mothurOut("Using " + taxonomyFileName + " as input file for the taxonomy parameter."); m->mothurOutEndLine(); }
128 m->mothurOut("No valid current files. taxonomy is a required parameter."); m->mothurOutEndLine();
131 }else if (taxonomyFileName == "not open") { taxonomyFileName = ""; abort = true; }
132 else { m->setTaxonomyFile(taxonomyFileName); }
134 namefile = validParameter.validFile(parameters, "name", true);
135 if (namefile == "not open") { namefile = ""; abort = true; }
136 else if (namefile == "not found") { namefile = ""; }
137 else { readNamesFile(); m->setNameFile(namefile); }
139 //if the user changes the output directory command factory will send this info to us in the output parameter
140 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
142 outputDir += m->hasPath(taxonomyFileName); //if user entered a file with a path then preserve it
145 string temp = validParameter.validFile(parameters, "cutoff", false);
146 if (temp == "not found") { temp = "-1"; }
147 m->mothurConvert(temp, cutoff);
149 label = validParameter.validFile(parameters, "label", false);
150 if (label == "not found") { label = ""; allLines = 1; }
152 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
153 else { allLines = 1; }
156 if (namefile == "") {
157 vector<string> files; files.push_back(taxonomyFileName);
158 parser.getNameFile(files);
163 catch(exception& e) {
164 m->errorOut(e, "PhylotypeCommand", "PhylotypeCommand");
168 /**********************************************************************************************************************/
170 int PhylotypeCommand::execute(){
173 if (abort == true) { if (calledHelp) { return 0; } return 2; }
175 //reads in taxonomy file and makes all the taxonomies the same length
176 //by appending the last taxon to a given taxonomy as many times as needed to
177 //make it as long as the longest taxonomy in the file
178 TaxEqualizer* taxEqual = new TaxEqualizer(taxonomyFileName, cutoff, outputDir);
180 if (m->control_pressed) { delete taxEqual; return 0; }
182 string equalizedTaxFile = taxEqual->getEqualizedTaxFile();
186 //build taxonomy tree from equalized file
187 PhyloTree* tree = new PhyloTree(equalizedTaxFile);
188 vector<int> leaves = tree->getGenusNodes();
190 //store leaf nodes in current map
191 for (int i = 0; i < leaves.size(); i++) { currentNodes[leaves[i]] = leaves[i]; }
194 if (tree->get(leaves[0]).parent == -1) { m->mothurOut("Empty Tree"); m->mothurOutEndLine(); done = true; }
196 if (m->control_pressed) { delete tree; return 0; }
198 string fileroot = outputDir + m->getRootName(m->getSimpleName(taxonomyFileName));
201 string outputListFile = fileroot + "tx.list";
202 m->openOutputFile(outputListFile, outList);
204 string outputSabundFile = fileroot + "tx.sabund";
205 m->openOutputFile(outputSabundFile, outSabund);
207 string outputRabundFile = fileroot + "tx.rabund";
208 m->openOutputFile(outputRabundFile, outRabund);
210 outputNames.push_back(outputListFile); outputTypes["list"].push_back(outputListFile);
211 outputNames.push_back(outputSabundFile); outputTypes["sabund"].push_back(outputSabundFile);
212 outputNames.push_back(outputRabundFile); outputTypes["rabund"].push_back(outputRabundFile);
215 //start at leaves of tree and work towards root, processing the labels the user wants
216 while((!done) && ((allLines == 1) || (labels.size() != 0))) {
218 string level = toString(count);
221 if (m->control_pressed) {
222 outRabund.close(); outSabund.close(); outList.close();
223 for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); }
224 delete tree; return 0;
227 //is this a level the user want output for
228 if(allLines == 1 || labels.count(level) == 1){
231 m->mothurOut(level); m->mothurOutEndLine();
234 list.setLabel(level);
236 //go through nodes and build listvector
237 for (itCurrent = currentNodes.begin(); itCurrent != currentNodes.end(); itCurrent++) {
240 TaxNode node = tree->get(itCurrent->first);
241 parentNodes[node.parent] = node.parent;
243 vector<string> names = node.accessions;
245 //make the names compatable with listvector
247 for (int i = 0; i < names.size(); i++) {
249 if (names[i] != "unknown") {
250 if (namefile != "") {
251 map<string, string>::iterator itNames = namemap.find(names[i]); //make sure this name is in namefile
253 if (itNames != namemap.end()) { name += namemap[names[i]] + ","; } //you found it in namefile
254 else { m->mothurOut(names[i] + " is not in your namefile, please correct."); m->mothurOutEndLine(); exit(1); }
256 }else{ name += names[i] + ","; }
259 name = name.substr(0, name.length()-1); //rip off extra ','
260 //add bin to list vector
261 if (name != "") { list.push_back(name); } //caused by unknown
267 list.getRAbundVector().print(outRabund);
269 list.getSAbundVector().print(outSabund);
276 for (itCurrent = currentNodes.begin(); itCurrent != currentNodes.end(); itCurrent++) {
277 int parent = tree->get(itCurrent->first).parent;
278 parentNodes[parent] = parent;
283 currentNodes = parentNodes;
286 //have we reached the rootnode
287 if (tree->get(currentNodes.begin()->first).parent == -1) { done = true; }
296 if (m->control_pressed) {
297 for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); }
301 //set list file as new current listfile
303 itTypes = outputTypes.find("list");
304 if (itTypes != outputTypes.end()) {
305 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setListFile(current); }
308 //set rabund file as new current rabundfile
309 itTypes = outputTypes.find("rabund");
310 if (itTypes != outputTypes.end()) {
311 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setRabundFile(current); }
314 //set sabund file as new current sabundfile
315 itTypes = outputTypes.find("sabund");
316 if (itTypes != outputTypes.end()) {
317 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setSabundFile(current); }
320 m->mothurOutEndLine();
321 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
322 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
323 m->mothurOutEndLine();
328 catch(exception& e) {
329 m->errorOut(e, "PhylotypeCommand", "execute");
333 /*****************************************************************/
334 int PhylotypeCommand::readNamesFile() {
338 m->openInputFile(namefile, in);
340 string first, second;
341 map<string, string>::iterator itNames;
344 in >> first >> second; m->gobble(in);
346 itNames = namemap.find(first);
347 if (itNames == namemap.end()) {
348 namemap[first] = second;
349 }else { m->mothurOut(first + " has already been seen in namefile, disregarding names file."); m->mothurOutEndLine(); in.close(); namemap.clear(); namefile = ""; return 1; }
355 catch(exception& e) {
356 m->errorOut(e, "PhylotypeCommand", "readNamesFile");
361 /**********************************************************************************************************************/