]> git.donarmstrong.com Git - mothur.git/blob - deuniquetreecommand.cpp
Merge remote-tracking branch 'mothur/master'
[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 DeuniqueTreeCommand::DeuniqueTreeCommand(){     
47         try {
48                 abort = true; calledHelp = true; 
49                 setParameters();
50                 vector<string> tempOutNames;
51                 outputTypes["tree"] = tempOutNames;
52         }
53         catch(exception& e) {
54                 m->errorOut(e, "DeuniqueTreeCommand", "DeuniqueTreeCommand");
55                 exit(1);
56         }
57 }
58 /***********************************************************/
59 DeuniqueTreeCommand::DeuniqueTreeCommand(string option)  {
60         try {
61                 abort = false; calledHelp = false;   
62                 
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;}
66                 
67                 else {
68                         vector<string> myArray = setParameters();
69                         
70                         OptionParser parser(option);
71                         map<string,string> parameters = parser.getParameters();
72                         map<string,string>::iterator it;
73                         
74                         ValidParameters validParameter;
75                         
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;  }
79                         }
80                         
81                         //initialize outputTypes
82                         vector<string> tempOutNames;
83                         outputTypes["tree"] = tempOutNames;
84                                                 
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 = "";          }
88                         else {
89                                 string path;
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;             }
96                                 }
97                                 
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;             }
104                                 }
105                         }
106                         
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); }     
115                         
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); }
123                         
124                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = m->hasPath(treefile);       }
125                 }
126                 
127         }
128         catch(exception& e) {
129                 m->errorOut(e, "DeuniqueTreeCommand", "DeuniqueTreeCommand");
130                 exit(1);
131         }
132 }
133
134 /***********************************************************/
135 int DeuniqueTreeCommand::execute() {
136         try {
137                 
138                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
139                 
140                 m->setTreeFile(treefile);
141                 
142                 TreeReader* reader = new TreeReader(treefile, "", namefile);
143         vector<Tree*> T = reader->getTrees();
144         map<string, string> nameMap = reader->getNameMap();
145         delete reader;          
146                 
147                 //print new Tree
148                 string outputFile = outputDir + m->getRootName(m->getSimpleName(treefile)) + "deunique.tre";
149                 outputNames.push_back(outputFile); outputTypes["tree"].push_back(outputFile);
150                 ofstream out;
151                 m->openOutputFile(outputFile, out);
152                 T[0]->print(out, nameMap);
153                 out.close();
154                 
155         delete (T[0]->getTreeMap());
156                 for (int i = 0; i < T.size(); i++) { delete T[i]; }
157                                 
158                 //set phylip file as new current phylipfile
159                 string current = "";
160                 itTypes = outputTypes.find("tree");
161                 if (itTypes != outputTypes.end()) {
162                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setTreeFile(current); }
163                 }
164                 
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();
169                 
170                 return 0;
171                 
172         }
173         catch(exception& e) {
174                 m->errorOut(e, "DeuniqueTreeCommand", "execute");
175                 exit(1);
176         }
177 }
178 /***********************************************************/
179
180
181
182