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("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");
73 mothurOut("The phylotype command should be in the following format: \n");
74 mothurOut("phylotype(taxonomy=yourTaxonomyFile, cutoff=yourCutoff, label=yourLabels) \n");
75 mothurOut("Eaxample: phylotype(taxonomy=amazon.taxonomy, cutoff=5, label=1-3-5).\n\n");
78 errorOut(e, "PhylotypeCommand", "help");
82 /**********************************************************************************************************************/
84 PhylotypeCommand::~PhylotypeCommand(){}
86 /**********************************************************************************************************************/
88 int PhylotypeCommand::execute(){
91 if (abort == true) { return 0; }
93 //reads in taxonomy file and makes all the taxonomies the same length
94 //by appending the last taxon to a given taxonomy as many times as needed to
95 //make it as long as the longest taxonomy in the file
96 TaxEqualizer* taxEqual = new TaxEqualizer(taxonomyFileName, cutoff);
98 string equalizedTaxFile = taxEqual->getEqualizedTaxFile();
102 //build taxonomy tree from equalized file
103 PhyloTree* tree = new PhyloTree(equalizedTaxFile);
104 vector<int> leaves = tree->getGenusNodes();
106 //store leaf nodes in current map
107 for (int i = 0; i < leaves.size(); i++) { currentNodes[leaves[i]] = leaves[i]; }
110 if (tree->get(leaves[0]).parent == -1) { mothurOut("Empty Tree"); mothurOutEndLine(); done = true; }
113 string outputListFile = getRootName(taxonomyFileName) + "tax.list";
114 openOutputFile(outputListFile, outList);
116 string outputSabundFile = getRootName(taxonomyFileName) + "tax.sabund";
117 openOutputFile(outputSabundFile, outSabund);
119 string outputRabundFile = getRootName(taxonomyFileName) + "tax.rabund";
120 openOutputFile(outputRabundFile, outRabund);
123 //start at leaves of tree and work towards root, processing the labels the user wants
124 while((!done) && ((allLines == 1) || (labels.size() != 0))) {
126 string level = toString(tree->get(currentNodes.begin()->first).level);
128 //is this a level the user want output for
129 if(allLines == 1 || labels.count(level) == 1){
132 mothurOut(level); mothurOutEndLine();
135 list.setLabel(level);
136 //go through nodes and build listvector
137 for (itCurrent = currentNodes.begin(); itCurrent != currentNodes.end(); itCurrent++) {
140 TaxNode node = tree->get(itCurrent->first);
141 parentNodes[node.parent] = node.parent;
143 vector<string> names = node.accessions;
145 //make the names compatable with listvector
147 for (int i = 0; i < names.size(); i++) { name += names[i] + ","; }
148 name = name.substr(0, name.length()-1); //rip off extra ','
150 //add bin to list vector
151 list.push_back(name);
157 list.getRAbundVector().print(outRabund);
159 list.getSAbundVector().print(outSabund);
166 for (itCurrent = currentNodes.begin(); itCurrent != currentNodes.end(); itCurrent++) {
167 int parent = tree->get(itCurrent->first).parent;
168 parentNodes[parent] = parent;
173 currentNodes = parentNodes;
176 //have we reached the rootnode
177 if (tree->get(currentNodes.begin()->first).parent == -1) { done = true; }
189 catch(exception& e) {
190 errorOut(e, "PhylotypeCommand", "execute");
194 /**********************************************************************************************************************/