]> git.donarmstrong.com Git - mothur.git/blob - deuniquetreecommand.cpp
sffinfo bug with flow grams right index when clipQualRight=0
[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","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);
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::getOutputPattern(string type) {
47     try {
48         string pattern = "";
49         
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;  }
52         
53         return pattern;
54     }
55     catch(exception& e) {
56         m->errorOut(e, "DeuniqueTreeCommand", "getOutputPattern");
57         exit(1);
58     }
59 }
60 //**********************************************************************************************************************
61 DeuniqueTreeCommand::DeuniqueTreeCommand(){     
62         try {
63                 abort = true; calledHelp = true; 
64                 setParameters();
65                 vector<string> tempOutNames;
66                 outputTypes["tree"] = tempOutNames;
67         }
68         catch(exception& e) {
69                 m->errorOut(e, "DeuniqueTreeCommand", "DeuniqueTreeCommand");
70                 exit(1);
71         }
72 }
73 /***********************************************************/
74 DeuniqueTreeCommand::DeuniqueTreeCommand(string option)  {
75         try {
76                 abort = false; calledHelp = false;   
77                 
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;}
81                 
82                 else {
83                         vector<string> myArray = setParameters();
84                         
85                         OptionParser parser(option);
86                         map<string,string> parameters = parser.getParameters();
87                         map<string,string>::iterator it;
88                         
89                         ValidParameters validParameter;
90                         
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;  }
94                         }
95                         
96                         //initialize outputTypes
97                         vector<string> tempOutNames;
98                         outputTypes["tree"] = tempOutNames;
99                                                 
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 = "";          }
103                         else {
104                                 string path;
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;             }
111                                 }
112                                 
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;             }
119                                 }
120                         }
121                         
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); }     
130                         
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); }
138                         
139                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = m->hasPath(treefile);       }
140                 }
141                 
142         }
143         catch(exception& e) {
144                 m->errorOut(e, "DeuniqueTreeCommand", "DeuniqueTreeCommand");
145                 exit(1);
146         }
147 }
148
149 /***********************************************************/
150 int DeuniqueTreeCommand::execute() {
151         try {
152                 
153                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
154                 
155                 m->setTreeFile(treefile);
156                 
157                 TreeReader* reader = new TreeReader(treefile, "", namefile);
158         vector<Tree*> T = reader->getTrees();
159         map<string, string> nameMap;
160         m->readNames(namefile, nameMap);
161         delete reader;          
162                 
163                 //print new Tree
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);
168                 ofstream out;
169                 m->openOutputFile(outputFile, out);
170                 T[0]->print(out, nameMap);
171                 out.close();
172                 
173         delete (T[0]->getCountTable());
174                 for (int i = 0; i < T.size(); i++) { delete T[i]; }
175                                 
176                 //set phylip file as new current phylipfile
177                 string current = "";
178                 itTypes = outputTypes.find("tree");
179                 if (itTypes != outputTypes.end()) {
180                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setTreeFile(current); }
181                 }
182                 
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();
187                 
188                 return 0;
189                 
190         }
191         catch(exception& e) {
192                 m->errorOut(e, "DeuniqueTreeCommand", "execute");
193                 exit(1);
194         }
195 }
196 /***********************************************************/
197
198
199
200