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"};
28 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
30 OptionParser parser(option);
31 map<string, string> parameters = parser.getParameters();
33 ValidParameters validParameter;
35 //check to make sure all parameters are valid for command
36 for (map<string, string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
37 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
40 taxonomyFileName = validParameter.validFile(parameters, "taxonomy", true);
41 if (taxonomyFileName == "not found") {
42 mothurOut("taxonomy is a required parameter for the phylotype command.");
45 }else if (taxonomyFileName == "not open") { abort = true; }
47 string temp = validParameter.validFile(parameters, "cutoff", false);
48 if (temp == "not found") { temp = "-1"; }
49 convert(temp, cutoff);
51 label = validParameter.validFile(parameters, "label", false);
52 if (label == "not found") { label = ""; allLines = 1; }
54 if(label != "all") { splitAtDash(label, labels); allLines = 0; }
55 else { allLines = 1; }
61 errorOut(e, "PhylotypeCommand", "PhylotypeCommand");
65 /**********************************************************************************************************************/
67 void PhylotypeCommand::help(){
69 mothurOut("The phylotype command reads a taxonomy file and outputs a .list, .rabund and .sabund file. \n");
70 mothurOut("The phylotype command parameter options are taxonomy, cutoff and label. The taxonomy parameter is required.\n");
71 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");
72 mothurOut("For example: taxonomy = Bacteria;Bacteroidetes-Chlorobi;Bacteroidetes; - cutoff=2, would truncate the taxonomy to Bacteria;Bacteroidetes-Chlorobi; \n");
73 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");
74 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");
75 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");
76 mothurOut("The phylotype command should be in the following format: \n");
77 mothurOut("phylotype(taxonomy=yourTaxonomyFile, cutoff=yourCutoff, label=yourLabels) \n");
78 mothurOut("Eaxample: phylotype(taxonomy=amazon.taxonomy, cutoff=5, label=1-3-5).\n\n");
81 errorOut(e, "PhylotypeCommand", "help");
85 /**********************************************************************************************************************/
87 PhylotypeCommand::~PhylotypeCommand(){}
89 /**********************************************************************************************************************/
91 int PhylotypeCommand::execute(){
94 if (abort == true) { return 0; }
96 //reads in taxonomy file and makes all the taxonomies the same length
97 //by appending the last taxon to a given taxonomy as many times as needed to
98 //make it as long as the longest taxonomy in the file
99 TaxEqualizer* taxEqual = new TaxEqualizer(taxonomyFileName, cutoff);
101 string equalizedTaxFile = taxEqual->getEqualizedTaxFile();
105 //build taxonomy tree from equalized file
106 PhyloTree* tree = new PhyloTree(equalizedTaxFile);
107 vector<int> leaves = tree->getGenusNodes();
109 //store leaf nodes in current map
110 for (int i = 0; i < leaves.size(); i++) { currentNodes[leaves[i]] = leaves[i]; }
113 if (tree->get(leaves[0]).parent == -1) { mothurOut("Empty Tree"); mothurOutEndLine(); done = true; }
116 string outputListFile = getRootName(taxonomyFileName) + "tx.list";
117 openOutputFile(outputListFile, outList);
119 string outputSabundFile = getRootName(taxonomyFileName) + "tx.sabund";
120 openOutputFile(outputSabundFile, outSabund);
122 string outputRabundFile = getRootName(taxonomyFileName) + "tx.rabund";
123 openOutputFile(outputRabundFile, outRabund);
126 //start at leaves of tree and work towards root, processing the labels the user wants
127 while((!done) && ((allLines == 1) || (labels.size() != 0))) {
129 string level = toString(count);
132 //is this a level the user want output for
133 if(allLines == 1 || labels.count(level) == 1){
136 mothurOut(level); mothurOutEndLine();
139 list.setLabel(level);
140 //go through nodes and build listvector
141 for (itCurrent = currentNodes.begin(); itCurrent != currentNodes.end(); itCurrent++) {
144 TaxNode node = tree->get(itCurrent->first);
145 parentNodes[node.parent] = node.parent;
147 vector<string> names = node.accessions;
149 //make the names compatable with listvector
151 for (int i = 0; i < names.size(); i++) { name += names[i] + ","; }
152 name = name.substr(0, name.length()-1); //rip off extra ','
154 //add bin to list vector
155 list.push_back(name);
161 list.getRAbundVector().print(outRabund);
163 list.getSAbundVector().print(outSabund);
170 for (itCurrent = currentNodes.begin(); itCurrent != currentNodes.end(); itCurrent++) {
171 int parent = tree->get(itCurrent->first).parent;
172 parentNodes[parent] = parent;
177 currentNodes = parentNodes;
180 //have we reached the rootnode
181 if (tree->get(currentNodes.begin()->first).parent == -1) { done = true; }
193 catch(exception& e) {
194 errorOut(e, "PhylotypeCommand", "execute");
198 /**********************************************************************************************************************/