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 DeuniqueTreeCommand::DeuniqueTreeCommand(){
48 abort = true; calledHelp = true;
50 vector<string> tempOutNames;
51 outputTypes["tree"] = tempOutNames;
54 m->errorOut(e, "DeuniqueTreeCommand", "DeuniqueTreeCommand");
58 /***********************************************************/
59 DeuniqueTreeCommand::DeuniqueTreeCommand(string option) {
61 abort = false; calledHelp = false;
63 //allow user to run help
64 if(option == "help") { help(); abort = true; calledHelp = true; }
65 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
68 vector<string> myArray = setParameters();
70 OptionParser parser(option);
71 map<string,string> parameters = parser.getParameters();
72 map<string,string>::iterator it;
74 ValidParameters validParameter;
76 //check to make sure all parameters are valid for command
77 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
78 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
81 //initialize outputTypes
82 vector<string> tempOutNames;
83 outputTypes["tree"] = tempOutNames;
85 //if the user changes the input directory command factory will send this info to us in the output parameter
86 string inputDir = validParameter.validFile(parameters, "inputdir", false);
87 if (inputDir == "not found"){ inputDir = ""; }
90 it = parameters.find("tree");
91 //user has given a template file
92 if(it != parameters.end()){
93 path = m->hasPath(it->second);
94 //if the user has not given a path then, add inputdir. else leave path alone.
95 if (path == "") { parameters["tree"] = inputDir + it->second; }
98 it = parameters.find("name");
99 //user has given a template file
100 if(it != parameters.end()){
101 path = m->hasPath(it->second);
102 //if the user has not given a path then, add inputdir. else leave path alone.
103 if (path == "") { parameters["name"] = inputDir + it->second; }
107 //check for required parameters
108 treefile = validParameter.validFile(parameters, "tree", true);
109 if (treefile == "not open") { abort = true; }
110 else if (treefile == "not found") { //if there is a current design file, use it
111 treefile = m->getTreeFile();
112 if (treefile != "") { m->mothurOut("Using " + treefile + " as input file for the tree parameter."); m->mothurOutEndLine(); }
113 else { m->mothurOut("You have no current tree file and the tree parameter is required."); m->mothurOutEndLine(); abort = true; }
114 }else { m->setTreeFile(treefile); }
116 namefile = validParameter.validFile(parameters, "name", true);
117 if (namefile == "not open") { abort = true; }
118 else if (namefile == "not found") { //if there is a current design file, use it
119 namefile = m->getNameFile();
120 if (namefile != "") { m->mothurOut("Using " + namefile + " as input file for the name parameter."); m->mothurOutEndLine(); }
121 else { m->mothurOut("You have no current name file and the name parameter is required."); m->mothurOutEndLine(); abort = true; }
122 }else { m->setNameFile(namefile); }
124 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(treefile); }
128 catch(exception& e) {
129 m->errorOut(e, "DeuniqueTreeCommand", "DeuniqueTreeCommand");
134 /***********************************************************/
135 int DeuniqueTreeCommand::execute() {
138 if (abort == true) { if (calledHelp) { return 0; } return 2; }
140 m->setTreeFile(treefile);
142 TreeReader* reader = new TreeReader(treefile, "", namefile);
143 vector<Tree*> T = reader->getTrees();
144 map<string, string> nameMap = reader->getNameMap();
148 string outputFile = outputDir + m->getRootName(m->getSimpleName(treefile)) + "deunique.tre";
149 outputNames.push_back(outputFile); outputTypes["tree"].push_back(outputFile);
151 m->openOutputFile(outputFile, out);
152 T[0]->print(out, nameMap);
155 delete (T[0]->getTreeMap());
156 for (int i = 0; i < T.size(); i++) { delete T[i]; }
158 //set phylip file as new current phylipfile
160 itTypes = outputTypes.find("tree");
161 if (itTypes != outputTypes.end()) {
162 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setTreeFile(current); }
165 m->mothurOutEndLine();
166 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
167 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
168 m->mothurOutEndLine();
173 catch(exception& e) {
174 m->errorOut(e, "DeuniqueTreeCommand", "execute");
178 /***********************************************************/