5 * Created by westcott on 5/11/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "clearcutcommand.h"
18 //**********************************************************************************************************************
19 vector<string> ClearcutCommand::getValidParameters(){
21 string AlignArray[] = {"fasta","phylip","version","verbose","quiet","seed","norandom","shuffle","neighbor","expblen",
22 "expdist","ntrees","matrixout","stdout","kimura","jukes","protein","DNA","outputdir","inputdir"};
23 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
27 m->errorOut(e, "ClearcutCommand", "getValidParameters");
31 //**********************************************************************************************************************
32 ClearcutCommand::ClearcutCommand(){
34 abort = true; calledHelp = true;
35 vector<string> tempOutNames;
36 outputTypes["tree"] = tempOutNames;
37 outputTypes["matrixout"] = tempOutNames;
40 m->errorOut(e, "ClearcutCommand", "ClearcutCommand");
43 }//**********************************************************************************************************************
44 vector<string> ClearcutCommand::getRequiredParameters(){
46 string Array[] = {"fasta","phylip","or"};
47 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
51 m->errorOut(e, "ClearcutCommand", "getRequiredParameters");
55 //**********************************************************************************************************************
56 vector<string> ClearcutCommand::getRequiredFiles(){
58 vector<string> myArray;
62 m->errorOut(e, "ClearcutCommand", "getRequiredFiles");
66 /**************************************************************************************/
67 ClearcutCommand::ClearcutCommand(string option) {
69 abort = false; calledHelp = false;
71 //allow user to run help
72 if(option == "help") { help(); abort = true; calledHelp = true; }
75 //valid paramters for this command
76 string Array[] = {"fasta","phylip","version","verbose","quiet","seed","norandom","shuffle","neighbor","expblen",
77 "expdist","ntrees","matrixout","stdout","kimura","jukes","protein","DNA","outputdir","inputdir"};
78 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
80 OptionParser parser(option);
81 map<string,string> parameters = parser.getParameters();
83 ValidParameters validParameter;
84 map<string, string>::iterator it;
86 //check to make sure all parameters are valid for command
87 for (it = parameters.begin(); it != parameters.end(); it++) {
88 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
91 //initialize outputTypes
92 vector<string> tempOutNames;
93 outputTypes["tree"] = tempOutNames;
94 outputTypes["matrixout"] = tempOutNames;
96 //if the user changes the input directory command factory will send this info to us in the output parameter
97 string inputDir = validParameter.validFile(parameters, "inputdir", false);
98 if (inputDir == "not found"){ inputDir = ""; }
101 it = parameters.find("fasta");
102 //user has given a template file
103 if(it != parameters.end()){
104 path = m->hasPath(it->second);
105 //if the user has not given a path then, add inputdir. else leave path alone.
106 if (path == "") { parameters["fasta"] = inputDir + it->second; }
109 it = parameters.find("phylip");
110 //user has given a template file
111 if(it != parameters.end()){
112 path = m->hasPath(it->second);
113 //if the user has not given a path then, add inputdir. else leave path alone.
114 if (path == "") { parameters["phylip"] = inputDir + it->second; }
118 //check for required parameters
119 fastafile = validParameter.validFile(parameters, "fasta", true);
120 if (fastafile == "not open") { fastafile = ""; abort = true; }
121 else if (fastafile == "not found") { fastafile = ""; }
122 else { inputFile = fastafile; }
124 phylipfile = validParameter.validFile(parameters, "phylip", true);
125 if (phylipfile == "not open") { phylipfile = ""; abort = true; }
126 else if (phylipfile == "not found") { phylipfile = ""; }
127 else { inputFile = phylipfile; }
129 if ((phylipfile == "") && (fastafile == "")) { m->mothurOut("You must provide either a phylip formatted distance matrix or an aligned fasta file."); m->mothurOutEndLine(); abort=true; }
130 if ((phylipfile != "") && (fastafile != "")) { m->mothurOut("You must provide either a phylip formatted distance matrix or an aligned fasta file, not BOTH."); m->mothurOutEndLine(); abort=true; }
133 //if the user changes the output directory command factory will send this info to us in the output parameter
134 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(inputFile); }
137 temp = validParameter.validFile(parameters, "version", false); if (temp == "not found"){ temp = "F"; }
138 version = m->isTrue(temp);
140 temp = validParameter.validFile(parameters, "verbose", false); if (temp == "not found"){ temp = "F"; }
141 verbose = m->isTrue(temp);
143 temp = validParameter.validFile(parameters, "quiet", false); if (temp == "not found"){ temp = "F"; }
144 quiet = m->isTrue(temp);
146 seed = validParameter.validFile(parameters, "seed", false); if (seed == "not found"){ seed = "*"; }
148 temp = validParameter.validFile(parameters, "norandom", false); if (temp == "not found"){ temp = "F"; }
149 norandom = m->isTrue(temp);
151 temp = validParameter.validFile(parameters, "shuffle", false); if (temp == "not found"){ temp = "F"; }
152 shuffle = m->isTrue(temp);
154 temp = validParameter.validFile(parameters, "neighbor", false); if (temp == "not found"){ temp = "T"; }
155 neighbor = m->isTrue(temp);
157 temp = validParameter.validFile(parameters, "DNA", false); if (temp == "not found"){ temp = "F"; }
158 DNA = m->isTrue(temp);
160 temp = validParameter.validFile(parameters, "protein", false); if (temp == "not found"){ temp = "F"; }
161 protein = m->isTrue(temp);
163 temp = validParameter.validFile(parameters, "jukes", false); if (temp == "not found"){ temp = "F"; }
164 jukes = m->isTrue(temp);
166 temp = validParameter.validFile(parameters, "kimura", false); if (temp == "not found"){ temp = "F"; }
167 kimura = m->isTrue(temp);
169 temp = validParameter.validFile(parameters, "stdout", false); if (temp == "not found"){ temp = "F"; }
170 stdoutWanted = m->isTrue(temp);
172 matrixout = validParameter.validFile(parameters, "matrixout", false); if (matrixout == "not found"){ matrixout = ""; }
174 ntrees = validParameter.validFile(parameters, "ntrees", false); if (ntrees == "not found"){ ntrees = "1"; }
176 temp = validParameter.validFile(parameters, "expblen", false); if (temp == "not found"){ temp = "F"; }
177 expblen = m->isTrue(temp);
179 temp = validParameter.validFile(parameters, "expdist", false); if (temp == "not found"){ temp = "F"; }
180 expdist = m->isTrue(temp);
182 if ((fastafile != "") && ((!DNA) && (!protein))) { m->mothurOut("You must specify the type of sequences you are using: DNA or protein"); m->mothurOutEndLine(); abort=true; }
186 catch(exception& e) {
187 m->errorOut(e, "ClearcutCommand", "ClearcutCommand");
191 //**********************************************************************************************************************
193 void ClearcutCommand::help(){
195 m->mothurOut("The clearcut command interfaces mothur with the clearcut program written by Initiative for Bioinformatics and Evolutionary Studies (IBEST) at the University of Idaho.\n");
196 m->mothurOut("For more information about clearcut refer to http://bioinformatics.hungry.com/clearcut/ \n");
197 m->mothurOut("The clearcut command parameters are phylip, fasta, version, verbose, quiet, seed, norandom, shuffle, neighbor, expblen, expdist, ntrees, matrixout, stdout, kimura, jukes, protein, DNA. \n");
198 m->mothurOut("The phylip parameter allows you to enter your phylip formatted distance matrix. \n");
199 m->mothurOut("The fasta parameter allows you to enter your aligned fasta file, if you enter a fastafile you specify if the sequences are DNA or protein using the DNA or protein parameters. \n");
201 m->mothurOut("The version parameter prints out the version of clearcut you are using, default=F. \n");
202 m->mothurOut("The verbose parameter prints out more output from clearcut, default=F. \n");
203 m->mothurOut("The quiet parameter turns on silent operation mode, default=F. \n");
204 m->mothurOut("The seed parameter allows you to explicitly set the PRNG seed to a specific value. \n");
205 m->mothurOut("The norandom parameter allows you to attempt joins deterministically, default=F. \n");
206 m->mothurOut("The shuffle parameter allows you to randomly shuffle the distance matrix, default=F. \n");
207 m->mothurOut("The neighbor parameter allows you to use traditional Neighbor-Joining algorithm, default=T. \n");
209 m->mothurOut("The DNA parameter allows you to indicate your fasta file contains DNA sequences, default=F. \n");
210 m->mothurOut("The protein parameter allows you to indicate your fasta file contains protein sequences, default=F. \n");
212 m->mothurOut("The stdout parameter outputs your tree to STDOUT, default=F. \n");
213 m->mothurOut("The matrixout parameter allows you to specify a filename to output a distance matrix to. \n");
214 m->mothurOut("The ntrees parameter allows you to specify the number of output trees, default=1. \n");
215 m->mothurOut("The expblen parameter allows you to use exponential notation for branch lengths, default=F. \n");
216 m->mothurOut("The expdist parameter allows you to use exponential notation for distance outputs, default=F. \n");
218 m->mothurOut("The clearcut command should be in the following format: \n");
219 m->mothurOut("clearcut(phylip=yourDistanceFile) \n");
220 m->mothurOut("Example: clearcut(phylip=abrecovery.phylip.dist) \n");
223 catch(exception& e) {
224 m->errorOut(e, "ClearcutCommand", "help");
229 /**************************************************************************************/
230 int ClearcutCommand::execute() {
233 if (abort == true) { if (calledHelp) { return 0; } return 2; }
236 string outputName = outputDir + m->getRootName(m->getSimpleName(inputFile)) + "tre";
237 outputNames.push_back(outputName); outputTypes["tree"].push_back(outputName);
241 char* tempClearcut = new char[8];
242 strcpy(tempClearcut, "clearcut");
243 cPara.push_back(tempClearcut);
245 //you gave us a distance matrix
246 if (phylipfile != "") { char* temp = new char[10]; strcpy(temp, "--distance"); cPara.push_back(temp); }
248 //you gave us a fastafile
249 if (fastafile != "") { char* temp = new char[11]; strcpy(temp, "--alignment"); cPara.push_back(temp); }
251 if (version) { char* temp = new char[9]; strcpy(temp, "--version"); cPara.push_back(temp); }
252 if (verbose) { char* temp = new char[9]; strcpy(temp, "--verbose"); cPara.push_back(temp); }
253 if (quiet) { char* temp = new char[7]; strcpy(temp, "--quiet"); cPara.push_back(temp); }
255 string tempSeed = "--seed=" + seed;
256 char* temp = new char[tempSeed.length()];
257 strcpy(temp, tempSeed.c_str());
258 cPara.push_back(temp);
260 if (norandom) { char* temp = new char[10]; strcpy(temp, "--norandom"); cPara.push_back(temp); }
261 if (shuffle) { char* temp = new char[9]; strcpy(temp, "--shuffle"); cPara.push_back(temp); }
262 if (neighbor) { char* temp = new char[10]; strcpy(temp, "--neighbor"); cPara.push_back(temp); }
264 string tempIn = "--in=" + inputFile;
265 char* tempI = new char[tempIn.length()];
266 strcpy(tempI, tempIn.c_str());
267 cPara.push_back(tempI);
269 if (stdoutWanted) { char* temp = new char[8]; strcpy(temp, "--stdout"); cPara.push_back(temp); }
271 string tempOut = "--out=" + outputName;
273 char* temp = new char[tempOut.length()];
274 strcpy(temp, tempOut.c_str());
275 cPara.push_back(temp);
278 if (DNA) { char* temp = new char[5]; strcpy(temp, "--DNA"); cPara.push_back(temp); }
279 if (protein) { char* temp = new char[9]; strcpy(temp, "--protein"); cPara.push_back(temp); }
280 if (jukes) { char* temp = new char[7]; strcpy(temp, "--jukes"); cPara.push_back(temp); }
281 if (kimura) { char* temp = new char[8]; strcpy(temp, "--kimura"); cPara.push_back(temp); }
282 if (matrixout != "") {
283 string tempMatrix = "--matrixout=" + outputDir + matrixout;
284 char* temp = new char[tempMatrix.length()];
285 strcpy(temp, tempMatrix.c_str());
286 cPara.push_back(temp);
287 outputNames.push_back((outputDir + matrixout));
288 outputTypes["matrixout"].push_back((outputDir + matrixout));
292 string tempNtrees = "--ntrees=" + ntrees;
293 char* temp = new char[tempNtrees.length()];
294 strcpy(temp, tempNtrees.c_str());
295 cPara.push_back(temp);
298 if (expblen) { char* temp = new char[9]; strcpy(temp, "--expblen"); cPara.push_back(temp); }
299 if (expdist) { char* temp = new char[9]; strcpy(temp, "--expdist"); cPara.push_back(temp); }
301 char** clearcutParameters;
302 clearcutParameters = new char*[cPara.size()];
303 for (int i = 0; i < cPara.size(); i++) { clearcutParameters[i] = cPara[i]; }
304 int numArgs = cPara.size();
306 clearcut_main(numArgs, clearcutParameters);
309 for(int i = 0; i < cPara.size(); i++) { delete[] cPara[i]; }
310 delete[] clearcutParameters;
313 m->mothurOutEndLine();
314 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
315 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
316 m->mothurOutEndLine();
321 catch(exception& e) {
322 m->errorOut(e, "ClearcutCommand", "execute");
326 /**************************************************************************************/