2 * deuniquetreecommand.cpp
5 * Created by westcott on 5/27/11.
6 * Copyright 2011 Schloss Lab. All rights reserved.
10 #include "deuniquetreecommand.h"
11 #include "treereader.h"
13 //**********************************************************************************************************************
14 vector<string> DeuniqueTreeCommand::setParameters(){
16 CommandParameter ptree("tree", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(ptree);
17 CommandParameter pname("name", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pname);
18 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
19 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
21 vector<string> myArray;
22 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
26 m->errorOut(e, "DeuniqueTreeCommand", "setParameters");
30 //**********************************************************************************************************************
31 string DeuniqueTreeCommand::getHelpString(){
33 string helpString = "";
34 helpString += "The deunique.tree command parameters are tree and name. Both parameters are required unless you have valid current files.\n";
35 helpString += "The deunique.tree command should be in the following format: deunique.tree(tree=yourTreeFile, name=yourNameFile).\n";
36 helpString += "Example deunique.tree(tree=abrecovery.tree, name=abrecovery.names).\n";
37 helpString += "Note: No spaces between parameter labels (i.e. tree), '=' and parameters (i.e.yourTreeFile).\n";
41 m->errorOut(e, "DeuniqueTreeCommand", "getHelpString");
45 //**********************************************************************************************************************
46 string DeuniqueTreeCommand::getOutputFileNameTag(string type, string inputName=""){
48 string outputFileName = "";
49 map<string, vector<string> >::iterator it;
51 //is this a type this command creates
52 it = outputTypes.find(type);
53 if (it == outputTypes.end()) { m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); }
55 if (type == "tree") { outputFileName = "deunique.tre"; }
56 else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true; }
58 return outputFileName;
61 m->errorOut(e, "DeuniqueTreeCommand", "getOutputFileNameTag");
65 //**********************************************************************************************************************
66 DeuniqueTreeCommand::DeuniqueTreeCommand(){
68 abort = true; calledHelp = true;
70 vector<string> tempOutNames;
71 outputTypes["tree"] = tempOutNames;
74 m->errorOut(e, "DeuniqueTreeCommand", "DeuniqueTreeCommand");
78 /***********************************************************/
79 DeuniqueTreeCommand::DeuniqueTreeCommand(string option) {
81 abort = false; calledHelp = false;
83 //allow user to run help
84 if(option == "help") { help(); abort = true; calledHelp = true; }
85 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
88 vector<string> myArray = setParameters();
90 OptionParser parser(option);
91 map<string,string> parameters = parser.getParameters();
92 map<string,string>::iterator it;
94 ValidParameters validParameter;
96 //check to make sure all parameters are valid for command
97 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
98 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
101 //initialize outputTypes
102 vector<string> tempOutNames;
103 outputTypes["tree"] = tempOutNames;
105 //if the user changes the input directory command factory will send this info to us in the output parameter
106 string inputDir = validParameter.validFile(parameters, "inputdir", false);
107 if (inputDir == "not found"){ inputDir = ""; }
110 it = parameters.find("tree");
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["tree"] = inputDir + it->second; }
118 it = parameters.find("name");
119 //user has given a template file
120 if(it != parameters.end()){
121 path = m->hasPath(it->second);
122 //if the user has not given a path then, add inputdir. else leave path alone.
123 if (path == "") { parameters["name"] = inputDir + it->second; }
127 //check for required parameters
128 treefile = validParameter.validFile(parameters, "tree", true);
129 if (treefile == "not open") { abort = true; }
130 else if (treefile == "not found") { //if there is a current design file, use it
131 treefile = m->getTreeFile();
132 if (treefile != "") { m->mothurOut("Using " + treefile + " as input file for the tree parameter."); m->mothurOutEndLine(); }
133 else { m->mothurOut("You have no current tree file and the tree parameter is required."); m->mothurOutEndLine(); abort = true; }
134 }else { m->setTreeFile(treefile); }
136 namefile = validParameter.validFile(parameters, "name", true);
137 if (namefile == "not open") { abort = true; }
138 else if (namefile == "not found") { //if there is a current design file, use it
139 namefile = m->getNameFile();
140 if (namefile != "") { m->mothurOut("Using " + namefile + " as input file for the name parameter."); m->mothurOutEndLine(); }
141 else { m->mothurOut("You have no current name file and the name parameter is required."); m->mothurOutEndLine(); abort = true; }
142 }else { m->setNameFile(namefile); }
144 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(treefile); }
148 catch(exception& e) {
149 m->errorOut(e, "DeuniqueTreeCommand", "DeuniqueTreeCommand");
154 /***********************************************************/
155 int DeuniqueTreeCommand::execute() {
158 if (abort == true) { if (calledHelp) { return 0; } return 2; }
160 m->setTreeFile(treefile);
162 TreeReader* reader = new TreeReader(treefile, "", namefile);
163 vector<Tree*> T = reader->getTrees();
164 map<string, string> nameMap;
165 m->readNames(namefile, nameMap);
169 string outputFile = outputDir + m->getRootName(m->getSimpleName(treefile)) + getOutputFileNameTag("tree");
170 outputNames.push_back(outputFile); outputTypes["tree"].push_back(outputFile);
172 m->openOutputFile(outputFile, out);
173 T[0]->print(out, nameMap);
176 delete (T[0]->getCountTable());
177 for (int i = 0; i < T.size(); i++) { delete T[i]; }
179 //set phylip file as new current phylipfile
181 itTypes = outputTypes.find("tree");
182 if (itTypes != outputTypes.end()) {
183 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setTreeFile(current); }
186 m->mothurOutEndLine();
187 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
188 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
189 m->mothurOutEndLine();
194 catch(exception& e) {
195 m->errorOut(e, "DeuniqueTreeCommand", "execute");
199 /***********************************************************/