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 PhylotypeCommand::PhylotypeCommand(string option){
21 //allow user to run help
22 if(option == "help") { help(); abort = true; }
26 //valid paramters for this command
27 string AlignArray[] = {"taxonomy","cutoff","label","outputdir","inputdir"};
28 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
30 OptionParser parser(option);
31 map<string, string> parameters = parser.getParameters();
33 ValidParameters validParameter;
34 map<string, string>::iterator it;
36 //check to make sure all parameters are valid for command
37 for (it = parameters.begin(); it != parameters.end(); it++) {
38 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
41 //if the user changes the input directory command factory will send this info to us in the output parameter
42 string inputDir = validParameter.validFile(parameters, "inputdir", false);
43 if (inputDir == "not found"){ inputDir = ""; }
46 it = parameters.find("taxonomy");
47 //user has given a template file
48 if(it != parameters.end()){
49 path = hasPath(it->second);
50 //if the user has not given a path then, add inputdir. else leave path alone.
51 if (path == "") { parameters["taxonomy"] = inputDir + it->second; }
55 taxonomyFileName = validParameter.validFile(parameters, "taxonomy", true);
56 if (taxonomyFileName == "not found") {
57 mothurOut("taxonomy is a required parameter for the phylotype command.");
60 }else if (taxonomyFileName == "not open") { abort = true; }
62 //if the user changes the output directory command factory will send this info to us in the output parameter
63 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
65 outputDir += hasPath(taxonomyFileName); //if user entered a file with a path then preserve it
68 string temp = validParameter.validFile(parameters, "cutoff", false);
69 if (temp == "not found") { temp = "-1"; }
70 convert(temp, cutoff);
72 label = validParameter.validFile(parameters, "label", false);
73 if (label == "not found") { label = ""; allLines = 1; }
75 if(label != "all") { splitAtDash(label, labels); allLines = 0; }
76 else { allLines = 1; }
82 errorOut(e, "PhylotypeCommand", "PhylotypeCommand");
86 /**********************************************************************************************************************/
88 void PhylotypeCommand::help(){
90 mothurOut("The phylotype command reads a taxonomy file and outputs a .list, .rabund and .sabund file. \n");
91 mothurOut("The phylotype command parameter options are taxonomy, cutoff and label. The taxonomy parameter is required.\n");
92 mothurOut("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");
93 mothurOut("For example: taxonomy = Bacteria;Bacteroidetes-Chlorobi;Bacteroidetes; - cutoff=2, would truncate the taxonomy to Bacteria;Bacteroidetes-Chlorobi; \n");
94 mothurOut("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");
95 mothurOut("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");
96 mothurOut("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");
97 mothurOut("The phylotype command should be in the following format: \n");
98 mothurOut("phylotype(taxonomy=yourTaxonomyFile, cutoff=yourCutoff, label=yourLabels) \n");
99 mothurOut("Eaxample: phylotype(taxonomy=amazon.taxonomy, cutoff=5, label=1-3-5).\n\n");
101 catch(exception& e) {
102 errorOut(e, "PhylotypeCommand", "help");
106 /**********************************************************************************************************************/
108 PhylotypeCommand::~PhylotypeCommand(){}
110 /**********************************************************************************************************************/
112 int PhylotypeCommand::execute(){
115 if (abort == true) { return 0; }
117 //reads in taxonomy file and makes all the taxonomies the same length
118 //by appending the last taxon to a given taxonomy as many times as needed to
119 //make it as long as the longest taxonomy in the file
120 TaxEqualizer* taxEqual = new TaxEqualizer(taxonomyFileName, cutoff);
122 string equalizedTaxFile = taxEqual->getEqualizedTaxFile();
126 //build taxonomy tree from equalized file
127 PhyloTree* tree = new PhyloTree(equalizedTaxFile);
128 vector<int> leaves = tree->getGenusNodes();
130 //store leaf nodes in current map
131 for (int i = 0; i < leaves.size(); i++) { currentNodes[leaves[i]] = leaves[i]; }
134 if (tree->get(leaves[0]).parent == -1) { mothurOut("Empty Tree"); mothurOutEndLine(); done = true; }
136 string fileroot = outputDir + getRootName(getSimpleName(taxonomyFileName));
139 string outputListFile = fileroot + "tx.list";
140 openOutputFile(outputListFile, outList);
142 string outputSabundFile = fileroot + "tx.sabund";
143 openOutputFile(outputSabundFile, outSabund);
145 string outputRabundFile = fileroot + "tx.rabund";
146 openOutputFile(outputRabundFile, outRabund);
149 //start at leaves of tree and work towards root, processing the labels the user wants
150 while((!done) && ((allLines == 1) || (labels.size() != 0))) {
152 string level = toString(count);
155 //is this a level the user want output for
156 if(allLines == 1 || labels.count(level) == 1){
159 mothurOut(level); mothurOutEndLine();
162 list.setLabel(level);
163 //go through nodes and build listvector
164 for (itCurrent = currentNodes.begin(); itCurrent != currentNodes.end(); itCurrent++) {
167 TaxNode node = tree->get(itCurrent->first);
168 parentNodes[node.parent] = node.parent;
170 vector<string> names = node.accessions;
172 //make the names compatable with listvector
174 for (int i = 0; i < names.size(); i++) { name += names[i] + ","; }
175 name = name.substr(0, name.length()-1); //rip off extra ','
177 //add bin to list vector
178 list.push_back(name);
184 list.getRAbundVector().print(outRabund);
186 list.getSAbundVector().print(outSabund);
193 for (itCurrent = currentNodes.begin(); itCurrent != currentNodes.end(); itCurrent++) {
194 int parent = tree->get(itCurrent->first).parent;
195 parentNodes[parent] = parent;
200 currentNodes = parentNodes;
203 //have we reached the rootnode
204 if (tree->get(currentNodes.begin()->first).parent == -1) { done = true; }
216 catch(exception& e) {
217 errorOut(e, "PhylotypeCommand", "execute");
221 /**********************************************************************************************************************/