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","tree",false,true,true); parameters.push_back(ptree);
17 CommandParameter pname("name", "InputTypes", "", "", "none", "none", "none","",false,true,true); 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::getOutputPattern(string type) {
50 if (type == "tree") { pattern = "[filename],deunique.tre"; }
51 else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true; }
56 m->errorOut(e, "DeuniqueTreeCommand", "getOutputPattern");
60 //**********************************************************************************************************************
61 DeuniqueTreeCommand::DeuniqueTreeCommand(){
63 abort = true; calledHelp = true;
65 vector<string> tempOutNames;
66 outputTypes["tree"] = tempOutNames;
69 m->errorOut(e, "DeuniqueTreeCommand", "DeuniqueTreeCommand");
73 /***********************************************************/
74 DeuniqueTreeCommand::DeuniqueTreeCommand(string option) {
76 abort = false; calledHelp = false;
78 //allow user to run help
79 if(option == "help") { help(); abort = true; calledHelp = true; }
80 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
83 vector<string> myArray = setParameters();
85 OptionParser parser(option);
86 map<string,string> parameters = parser.getParameters();
87 map<string,string>::iterator it;
89 ValidParameters validParameter;
91 //check to make sure all parameters are valid for command
92 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
93 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
96 //initialize outputTypes
97 vector<string> tempOutNames;
98 outputTypes["tree"] = tempOutNames;
100 //if the user changes the input directory command factory will send this info to us in the output parameter
101 string inputDir = validParameter.validFile(parameters, "inputdir", false);
102 if (inputDir == "not found"){ inputDir = ""; }
105 it = parameters.find("tree");
106 //user has given a template file
107 if(it != parameters.end()){
108 path = m->hasPath(it->second);
109 //if the user has not given a path then, add inputdir. else leave path alone.
110 if (path == "") { parameters["tree"] = inputDir + it->second; }
113 it = parameters.find("name");
114 //user has given a template file
115 if(it != parameters.end()){
116 path = m->hasPath(it->second);
117 //if the user has not given a path then, add inputdir. else leave path alone.
118 if (path == "") { parameters["name"] = inputDir + it->second; }
122 //check for required parameters
123 treefile = validParameter.validFile(parameters, "tree", true);
124 if (treefile == "not open") { abort = true; }
125 else if (treefile == "not found") { //if there is a current design file, use it
126 treefile = m->getTreeFile();
127 if (treefile != "") { m->mothurOut("Using " + treefile + " as input file for the tree parameter."); m->mothurOutEndLine(); }
128 else { m->mothurOut("You have no current tree file and the tree parameter is required."); m->mothurOutEndLine(); abort = true; }
129 }else { m->setTreeFile(treefile); }
131 namefile = validParameter.validFile(parameters, "name", true);
132 if (namefile == "not open") { abort = true; }
133 else if (namefile == "not found") { //if there is a current design file, use it
134 namefile = m->getNameFile();
135 if (namefile != "") { m->mothurOut("Using " + namefile + " as input file for the name parameter."); m->mothurOutEndLine(); }
136 else { m->mothurOut("You have no current name file and the name parameter is required."); m->mothurOutEndLine(); abort = true; }
137 }else { m->setNameFile(namefile); }
139 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(treefile); }
143 catch(exception& e) {
144 m->errorOut(e, "DeuniqueTreeCommand", "DeuniqueTreeCommand");
149 /***********************************************************/
150 int DeuniqueTreeCommand::execute() {
153 if (abort == true) { if (calledHelp) { return 0; } return 2; }
155 m->setTreeFile(treefile);
157 TreeReader* reader = new TreeReader(treefile, "", namefile);
158 vector<Tree*> T = reader->getTrees();
159 map<string, string> nameMap;
160 m->readNames(namefile, nameMap);
164 map<string, string> variables;
165 variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(treefile));
166 string outputFile = getOutputFileName("tree", variables);
167 outputNames.push_back(outputFile); outputTypes["tree"].push_back(outputFile);
169 m->openOutputFile(outputFile, out);
170 T[0]->print(out, nameMap);
173 delete (T[0]->getCountTable());
174 for (int i = 0; i < T.size(); i++) { delete T[i]; }
176 //set phylip file as new current phylipfile
178 itTypes = outputTypes.find("tree");
179 if (itTypes != outputTypes.end()) {
180 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setTreeFile(current); }
183 m->mothurOutEndLine();
184 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
185 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
186 m->mothurOutEndLine();
191 catch(exception& e) {
192 m->errorOut(e, "DeuniqueTreeCommand", "execute");
196 /***********************************************************/