]> git.donarmstrong.com Git - mothur.git/blob - deuniquetreecommand.cpp
major change to the tree class to use the count table class instead of tree map....
[mothur.git] / deuniquetreecommand.cpp
1 /*
2  *  deuniquetreecommand.cpp
3  *  Mothur
4  *
5  *  Created by westcott on 5/27/11.
6  *  Copyright 2011 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "deuniquetreecommand.h"
11 #include "treereader.h"
12
13 //**********************************************************************************************************************
14 vector<string> DeuniqueTreeCommand::setParameters(){    
15         try {
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);
20                 
21                 vector<string> myArray;
22                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
23                 return myArray;
24         }
25         catch(exception& e) {
26                 m->errorOut(e, "DeuniqueTreeCommand", "setParameters");
27                 exit(1);
28         }
29 }
30 //**********************************************************************************************************************
31 string DeuniqueTreeCommand::getHelpString(){    
32         try {
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";
38                 return helpString;
39         }
40         catch(exception& e) {
41                 m->errorOut(e, "DeuniqueTreeCommand", "getHelpString");
42                 exit(1);
43         }
44 }
45 //**********************************************************************************************************************
46 string DeuniqueTreeCommand::getOutputFileNameTag(string type, string inputName=""){     
47         try {
48         string outputFileName = "";
49                 map<string, vector<string> >::iterator it;
50         
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"); }
54         else {
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;  }
57         }
58         return outputFileName;
59         }
60         catch(exception& e) {
61                 m->errorOut(e, "DeuniqueTreeCommand", "getOutputFileNameTag");
62                 exit(1);
63         }
64 }
65 //**********************************************************************************************************************
66 DeuniqueTreeCommand::DeuniqueTreeCommand(){     
67         try {
68                 abort = true; calledHelp = true; 
69                 setParameters();
70                 vector<string> tempOutNames;
71                 outputTypes["tree"] = tempOutNames;
72         }
73         catch(exception& e) {
74                 m->errorOut(e, "DeuniqueTreeCommand", "DeuniqueTreeCommand");
75                 exit(1);
76         }
77 }
78 /***********************************************************/
79 DeuniqueTreeCommand::DeuniqueTreeCommand(string option)  {
80         try {
81                 abort = false; calledHelp = false;   
82                 
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;}
86                 
87                 else {
88                         vector<string> myArray = setParameters();
89                         
90                         OptionParser parser(option);
91                         map<string,string> parameters = parser.getParameters();
92                         map<string,string>::iterator it;
93                         
94                         ValidParameters validParameter;
95                         
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;  }
99                         }
100                         
101                         //initialize outputTypes
102                         vector<string> tempOutNames;
103                         outputTypes["tree"] = tempOutNames;
104                                                 
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 = "";          }
108                         else {
109                                 string path;
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;             }
116                                 }
117                                 
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;             }
124                                 }
125                         }
126                         
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); }     
135                         
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); }
143                         
144                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = m->hasPath(treefile);       }
145                 }
146                 
147         }
148         catch(exception& e) {
149                 m->errorOut(e, "DeuniqueTreeCommand", "DeuniqueTreeCommand");
150                 exit(1);
151         }
152 }
153
154 /***********************************************************/
155 int DeuniqueTreeCommand::execute() {
156         try {
157                 
158                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
159                 
160                 m->setTreeFile(treefile);
161                 
162                 TreeReader* reader = new TreeReader(treefile, "", namefile);
163         vector<Tree*> T = reader->getTrees();
164         map<string, string> nameMap;
165         m->readNames(namefile, nameMap);
166         delete reader;          
167                 
168                 //print new Tree
169                 string outputFile = outputDir + m->getRootName(m->getSimpleName(treefile)) + getOutputFileNameTag("tree");
170                 outputNames.push_back(outputFile); outputTypes["tree"].push_back(outputFile);
171                 ofstream out;
172                 m->openOutputFile(outputFile, out);
173                 T[0]->print(out, nameMap);
174                 out.close();
175                 
176         delete (T[0]->getCountTable());
177                 for (int i = 0; i < T.size(); i++) { delete T[i]; }
178                                 
179                 //set phylip file as new current phylipfile
180                 string current = "";
181                 itTypes = outputTypes.find("tree");
182                 if (itTypes != outputTypes.end()) {
183                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setTreeFile(current); }
184                 }
185                 
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();
190                 
191                 return 0;
192                 
193         }
194         catch(exception& e) {
195                 m->errorOut(e, "DeuniqueTreeCommand", "execute");
196                 exit(1);
197         }
198 }
199 /***********************************************************/
200
201
202
203