2 * deuniquetreecommand.cpp
5 * Created by westcott on 5/27/11.
6 * Copyright 2011 Schloss Lab. All rights reserved.
10 #include "deuniquetreecommand.h"
12 //**********************************************************************************************************************
13 vector<string> DeuniqueTreeCommand::setParameters(){
15 CommandParameter ptree("tree", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(ptree);
16 CommandParameter pname("name", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pname);
17 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
18 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
20 vector<string> myArray;
21 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
25 m->errorOut(e, "DeuniqueTreeCommand", "setParameters");
29 //**********************************************************************************************************************
30 string DeuniqueTreeCommand::getHelpString(){
32 string helpString = "";
33 helpString += "The deunique.tree command parameters are tree and name. Both parameters are required unless you have valid current files.\n";
34 helpString += "The deunique.tree command should be in the following format: deunique.tree(tree=yourTreeFile, name=yourNameFile).\n";
35 helpString += "Example deunique.tree(tree=abrecovery.tree, name=abrecovery.names).\n";
36 helpString += "Note: No spaces between parameter labels (i.e. tree), '=' and parameters (i.e.yourTreeFile).\n";
40 m->errorOut(e, "DeuniqueTreeCommand", "getHelpString");
44 //**********************************************************************************************************************
45 DeuniqueTreeCommand::DeuniqueTreeCommand(){
47 abort = true; calledHelp = true;
49 vector<string> tempOutNames;
50 outputTypes["tree"] = tempOutNames;
53 m->errorOut(e, "DeuniqueTreeCommand", "DeuniqueTreeCommand");
57 /***********************************************************/
58 DeuniqueTreeCommand::DeuniqueTreeCommand(string option) {
60 abort = false; calledHelp = false;
62 //allow user to run help
63 if(option == "help") { help(); abort = true; calledHelp = true; }
64 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
67 vector<string> myArray = setParameters();
69 OptionParser parser(option);
70 map<string,string> parameters = parser.getParameters();
71 map<string,string>::iterator it;
73 ValidParameters validParameter;
75 //check to make sure all parameters are valid for command
76 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
77 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
80 //initialize outputTypes
81 vector<string> tempOutNames;
82 outputTypes["tree"] = tempOutNames;
84 //if the user changes the input directory command factory will send this info to us in the output parameter
85 string inputDir = validParameter.validFile(parameters, "inputdir", false);
86 if (inputDir == "not found"){ inputDir = ""; }
89 it = parameters.find("tree");
90 //user has given a template file
91 if(it != parameters.end()){
92 path = m->hasPath(it->second);
93 //if the user has not given a path then, add inputdir. else leave path alone.
94 if (path == "") { parameters["tree"] = inputDir + it->second; }
97 it = parameters.find("name");
98 //user has given a template file
99 if(it != parameters.end()){
100 path = m->hasPath(it->second);
101 //if the user has not given a path then, add inputdir. else leave path alone.
102 if (path == "") { parameters["name"] = inputDir + it->second; }
108 m->namesOfGroups.clear();
109 m->Treenames.clear();
112 //check for required parameters
113 treefile = validParameter.validFile(parameters, "tree", true);
114 if (treefile == "not open") { abort = true; }
115 else if (treefile == "not found") { //if there is a current design file, use it
116 treefile = m->getTreeFile();
117 if (treefile != "") { m->mothurOut("Using " + treefile + " as input file for the tree parameter."); m->mothurOutEndLine(); }
118 else { m->mothurOut("You have no current tree file and the tree parameter is required."); m->mothurOutEndLine(); abort = true; }
119 }else { m->setTreeFile(treefile); }
121 namefile = validParameter.validFile(parameters, "name", true);
122 if (namefile == "not open") { abort = true; }
123 else if (namefile == "not found") { //if there is a current design file, use it
124 namefile = m->getNameFile();
125 if (namefile != "") { m->mothurOut("Using " + namefile + " as input file for the name parameter."); m->mothurOutEndLine(); }
126 else { m->mothurOut("You have no current name file and the name parameter is required."); m->mothurOutEndLine(); abort = true; }
127 }else { m->setNameFile(namefile); }
129 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(treefile); }
133 catch(exception& e) {
134 m->errorOut(e, "DeuniqueTreeCommand", "DeuniqueTreeCommand");
139 /***********************************************************/
140 int DeuniqueTreeCommand::execute() {
143 if (abort == true) { if (calledHelp) { return 0; } return 2; }
145 m->setTreeFile(treefile);
147 //extracts names from tree to make faked out groupmap
148 Tree* tree = new Tree(treefile); delete tree;
149 tmap = new TreeMap();
150 for (int i = 0; i < m->Treenames.size(); i++) { tmap->addSeq(m->Treenames[i], "Group1"); }
152 if (m->control_pressed) { delete tmap; return 0; }
156 if (m->control_pressed) { delete tmap; return 0; }
158 ReadTree* read = new ReadNewickTree(treefile);
159 int readOk = read->read(tmap);
160 if (readOk != 0) { m->mothurOut("Read Terminated."); m->mothurOutEndLine(); delete tmap; delete read; return 0; }
162 read->AssembleTrees();
163 vector<Tree*> T = read->getTrees();
166 //make sure all files match
167 //if you provide a namefile we will use the numNames in the namefile as long as the number of unique match the tree names size.
169 if (numUniquesInName == m->Treenames.size()) { numNamesInTree = nameMap.size(); }
170 else { numNamesInTree = m->Treenames.size(); }
172 //output any names that are in group file but not in tree
173 if (numNamesInTree < tmap->getNumSeqs()) {
174 for (int i = 0; i < tmap->namesOfSeqs.size(); i++) {
175 //is that name in the tree?
177 for (int j = 0; j < m->Treenames.size(); j++) {
178 if (tmap->namesOfSeqs[i] == m->Treenames[j]) { break; } //found it
182 if (m->control_pressed) {
183 delete tmap; for (int i = 0; i < T.size(); i++) { delete T[i]; }
184 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } outputTypes.clear();
189 //then you did not find it so report it
190 if (count == m->Treenames.size()) {
191 //if it is in your namefile then don't remove
192 map<string, string>::iterator it = nameMap.find(tmap->namesOfSeqs[i]);
194 if (it == nameMap.end()) {
195 m->mothurOut(tmap->namesOfSeqs[i] + " is in your groupfile and not in your tree. It will be disregarded."); m->mothurOutEndLine();
196 tmap->removeSeq(tmap->namesOfSeqs[i]);
197 i--; //need this because removeSeq removes name from namesOfSeqs
205 string outputFile = outputDir + m->getRootName(m->getSimpleName(treefile)) + "deunique.tre";
206 outputNames.push_back(outputFile); outputTypes["tree"].push_back(outputFile);
208 m->openOutputFile(outputFile, out);
209 T[0]->print(out, "deunique");
212 delete tmap; for (int i = 0; i < T.size(); i++) { delete T[i]; }
214 //set phylip file as new current phylipfile
216 itTypes = outputTypes.find("tree");
217 if (itTypes != outputTypes.end()) {
218 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setTreeFile(current); }
221 m->mothurOutEndLine();
222 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
223 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
224 m->mothurOutEndLine();
229 catch(exception& e) {
230 m->errorOut(e, "DeuniqueTreeCommand", "execute");
234 /*****************************************************************/
235 int DeuniqueTreeCommand::readNamesFile() {
238 numUniquesInName = 0;
241 m->openInputFile(namefile, in);
243 string first, second;
244 map<string, string>::iterator itNames;
247 in >> first >> second; m->gobble(in);
251 itNames = m->names.find(first);
252 if (itNames == m->names.end()) {
253 m->names[first] = second;
255 //we need a list of names in your namefile to use above when removing extra seqs above so we don't remove them
256 vector<string> dupNames;
257 m->splitAtComma(second, dupNames);
259 for (int i = 0; i < dupNames.size(); i++) {
260 nameMap[dupNames[i]] = dupNames[i];
261 if (i != 0) { tmap->addSeq(dupNames[i], "Group1"); }
263 }else { m->mothurOut(first + " has already been seen in namefile, aborting."); m->mothurOutEndLine(); in.close(); m->names.clear(); m->control_pressed = true; return 1; }
269 catch(exception& e) {
270 m->errorOut(e, "DeuniqueTreeCommand", "readNamesFile");
274 /***********************************************************/