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::getValidParameters(){
19 string Array[] = {"taxonomy","cutoff","label","name","outputdir","inputdir"};
20 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
24 m->errorOut(e, "PhylotypeCommand", "getValidParameters");
28 //**********************************************************************************************************************
29 PhylotypeCommand::PhylotypeCommand(){
32 //initialize outputTypes
33 vector<string> tempOutNames;
34 outputTypes["list"] = tempOutNames;
35 outputTypes["sabund"] = tempOutNames;
36 outputTypes["rabund"] = tempOutNames;
39 m->errorOut(e, "PhylotypeCommand", "PhylotypeCommand");
43 //**********************************************************************************************************************
44 vector<string> PhylotypeCommand::getRequiredParameters(){
46 string Array[] = {"taxonomy"};
47 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
51 m->errorOut(e, "PhylotypeCommand", "getRequiredParameters");
55 //**********************************************************************************************************************
56 vector<string> PhylotypeCommand::getRequiredFiles(){
58 vector<string> myArray;
62 m->errorOut(e, "PhylotypeCommand", "getRequiredFiles");
66 /**********************************************************************************************************************/
67 PhylotypeCommand::PhylotypeCommand(string option) {
71 //allow user to run help
72 if(option == "help") { help(); abort = true; }
76 //valid paramters for this command
77 string AlignArray[] = {"taxonomy","cutoff","label","name","outputdir","inputdir"};
78 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
80 OptionParser parser(option);
81 map<string, string> parameters = parser.getParameters();
83 ValidParameters validParameter;
84 map<string, string>::iterator it;
86 //check to make sure all parameters are valid for command
87 for (it = parameters.begin(); it != parameters.end(); it++) {
88 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
91 //initialize outputTypes
92 vector<string> tempOutNames;
93 outputTypes["list"] = tempOutNames;
94 outputTypes["sabund"] = tempOutNames;
95 outputTypes["rabund"] = tempOutNames;
97 //if the user changes the input directory command factory will send this info to us in the output parameter
98 string inputDir = validParameter.validFile(parameters, "inputdir", false);
99 if (inputDir == "not found"){ inputDir = ""; }
102 it = parameters.find("taxonomy");
103 //user has given a template file
104 if(it != parameters.end()){
105 path = m->hasPath(it->second);
106 //if the user has not given a path then, add inputdir. else leave path alone.
107 if (path == "") { parameters["taxonomy"] = inputDir + it->second; }
110 it = parameters.find("name");
111 //user has given a template file
112 if(it != parameters.end()){
113 path = m->hasPath(it->second);
114 //if the user has not given a path then, add inputdir. else leave path alone.
115 if (path == "") { parameters["name"] = inputDir + it->second; }
119 taxonomyFileName = validParameter.validFile(parameters, "taxonomy", true);
120 if (taxonomyFileName == "not found") {
121 m->mothurOut("taxonomy is a required parameter for the phylotype command.");
122 m->mothurOutEndLine();
124 }else if (taxonomyFileName == "not open") { abort = true; }
126 namefile = validParameter.validFile(parameters, "name", true);
127 if (namefile == "not open") { abort = true; }
128 else if (namefile == "not found") { namefile = ""; }
129 else { readNamesFile(); }
131 //if the user changes the output directory command factory will send this info to us in the output parameter
132 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
134 outputDir += m->hasPath(taxonomyFileName); //if user entered a file with a path then preserve it
137 string temp = validParameter.validFile(parameters, "cutoff", false);
138 if (temp == "not found") { temp = "-1"; }
139 convert(temp, cutoff);
141 label = validParameter.validFile(parameters, "label", false);
142 if (label == "not found") { label = ""; allLines = 1; }
144 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
145 else { allLines = 1; }
150 catch(exception& e) {
151 m->errorOut(e, "PhylotypeCommand", "PhylotypeCommand");
155 /**********************************************************************************************************************/
157 void PhylotypeCommand::help(){
159 m->mothurOut("The phylotype command reads a taxonomy file and outputs a .list, .rabund and .sabund file. \n");
160 m->mothurOut("The phylotype command parameter options are taxonomy, cutoff and label. The taxonomy parameter is required.\n");
161 m->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");
162 m->mothurOut("For example: taxonomy = Bacteria;Bacteroidetes-Chlorobi;Bacteroidetes; - cutoff=2, would truncate the taxonomy to Bacteria;Bacteroidetes-Chlorobi; \n");
163 m->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");
164 m->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");
165 m->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");
166 m->mothurOut("The phylotype command should be in the following format: \n");
167 m->mothurOut("phylotype(taxonomy=yourTaxonomyFile, cutoff=yourCutoff, label=yourLabels) \n");
168 m->mothurOut("Eaxample: phylotype(taxonomy=amazon.taxonomy, cutoff=5, label=1-3-5).\n\n");
170 catch(exception& e) {
171 m->errorOut(e, "PhylotypeCommand", "help");
175 /**********************************************************************************************************************/
177 PhylotypeCommand::~PhylotypeCommand(){}
179 /**********************************************************************************************************************/
181 int PhylotypeCommand::execute(){
184 if (abort == true) { return 0; }
186 //reads in taxonomy file and makes all the taxonomies the same length
187 //by appending the last taxon to a given taxonomy as many times as needed to
188 //make it as long as the longest taxonomy in the file
189 TaxEqualizer* taxEqual = new TaxEqualizer(taxonomyFileName, cutoff, outputDir);
191 if (m->control_pressed) { delete taxEqual; return 0; }
193 string equalizedTaxFile = taxEqual->getEqualizedTaxFile();
197 //build taxonomy tree from equalized file
198 PhyloTree* tree = new PhyloTree(equalizedTaxFile);
199 vector<int> leaves = tree->getGenusNodes();
201 //store leaf nodes in current map
202 for (int i = 0; i < leaves.size(); i++) { currentNodes[leaves[i]] = leaves[i]; }
205 if (tree->get(leaves[0]).parent == -1) { m->mothurOut("Empty Tree"); m->mothurOutEndLine(); done = true; }
207 if (m->control_pressed) { delete tree; return 0; }
209 string fileroot = outputDir + m->getRootName(m->getSimpleName(taxonomyFileName));
212 string outputListFile = fileroot + "tx.list";
213 m->openOutputFile(outputListFile, outList);
215 string outputSabundFile = fileroot + "tx.sabund";
216 m->openOutputFile(outputSabundFile, outSabund);
218 string outputRabundFile = fileroot + "tx.rabund";
219 m->openOutputFile(outputRabundFile, outRabund);
221 outputNames.push_back(outputListFile); outputTypes["list"].push_back(outputListFile);
222 outputNames.push_back(outputSabundFile); outputTypes["sabund"].push_back(outputSabundFile);
223 outputNames.push_back(outputRabundFile); outputTypes["rabund"].push_back(outputRabundFile);
226 //start at leaves of tree and work towards root, processing the labels the user wants
227 while((!done) && ((allLines == 1) || (labels.size() != 0))) {
229 string level = toString(count);
232 if (m->control_pressed) {
233 outRabund.close(); outSabund.close(); outList.close();
234 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
235 delete tree; return 0;
238 //is this a level the user want output for
239 if(allLines == 1 || labels.count(level) == 1){
242 m->mothurOut(level); m->mothurOutEndLine();
245 list.setLabel(level);
246 //go through nodes and build listvector
247 for (itCurrent = currentNodes.begin(); itCurrent != currentNodes.end(); itCurrent++) {
250 TaxNode node = tree->get(itCurrent->first);
251 parentNodes[node.parent] = node.parent;
253 vector<string> names = node.accessions;
255 //make the names compatable with listvector
257 for (int i = 0; i < names.size(); i++) {
258 if (namefile != "") {
259 map<string, string>::iterator itNames = namemap.find(names[i]); //make sure this name is in namefile
261 if (itNames != namemap.end()) { name += namemap[names[i]] + ","; } //you found it in namefile
262 else { m->mothurOut(names[i] + " is not in your namefile, please correct."); m->mothurOutEndLine(); exit(1); }
264 }else{ name += names[i] + ","; }
266 name = name.substr(0, name.length()-1); //rip off extra ','
268 //add bin to list vector
269 list.push_back(name);
275 list.getRAbundVector().print(outRabund);
277 list.getSAbundVector().print(outSabund);
284 for (itCurrent = currentNodes.begin(); itCurrent != currentNodes.end(); itCurrent++) {
285 int parent = tree->get(itCurrent->first).parent;
286 parentNodes[parent] = parent;
291 currentNodes = parentNodes;
294 //have we reached the rootnode
295 if (tree->get(currentNodes.begin()->first).parent == -1) { done = true; }
304 if (m->control_pressed) {
305 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
309 m->mothurOutEndLine();
310 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
311 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
312 m->mothurOutEndLine();
317 catch(exception& e) {
318 m->errorOut(e, "PhylotypeCommand", "execute");
322 /*****************************************************************/
323 int PhylotypeCommand::readNamesFile() {
327 m->openInputFile(namefile, in);
329 string first, second;
330 map<string, string>::iterator itNames;
333 in >> first >> second; m->gobble(in);
335 itNames = namemap.find(first);
336 if (itNames == namemap.end()) {
337 namemap[first] = second;
338 }else { m->mothurOut(first + " has already been seen in namefile, disregarding names file."); m->mothurOutEndLine(); in.close(); namemap.clear(); namefile = ""; return 1; }
344 catch(exception& e) {
345 m->errorOut(e, "PhylotypeCommand", "readNamesFile");
350 /**********************************************************************************************************************/