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(){
35 //initialize outputTypes
36 vector<string> tempOutNames;
37 outputTypes["tree"] = tempOutNames;
38 outputTypes["matrixout"] = tempOutNames;
41 m->errorOut(e, "ClearcutCommand", "ClearcutCommand");
44 }//**********************************************************************************************************************
45 vector<string> ClearcutCommand::getRequiredParameters(){
47 string Array[] = {"fasta","phylip","or"};
48 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
52 m->errorOut(e, "ClearcutCommand", "getRequiredParameters");
56 //**********************************************************************************************************************
57 vector<string> ClearcutCommand::getRequiredFiles(){
59 vector<string> myArray;
63 m->errorOut(e, "ClearcutCommand", "getRequiredFiles");
67 /**************************************************************************************/
68 ClearcutCommand::ClearcutCommand(string option) {
72 //allow user to run help
73 if(option == "help") { help(); abort = true; }
76 //valid paramters for this command
77 string Array[] = {"fasta","phylip","version","verbose","quiet","seed","norandom","shuffle","neighbor","expblen",
78 "expdist","ntrees","matrixout","stdout","kimura","jukes","protein","DNA","outputdir","inputdir"};
79 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
81 OptionParser parser(option);
82 map<string,string> parameters = parser.getParameters();
84 ValidParameters validParameter;
85 map<string, string>::iterator it;
87 //check to make sure all parameters are valid for command
88 for (it = parameters.begin(); it != parameters.end(); it++) {
89 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
92 //initialize outputTypes
93 vector<string> tempOutNames;
94 outputTypes["tree"] = tempOutNames;
95 outputTypes["matrixout"] = tempOutNames;
97 //if the user changes the input directory command factory will send this info to us in the output parameter
98 string inputDir = validParameter.validFile(parameters, "inputdir", false);
99 if (inputDir == "not found"){ inputDir = ""; }
102 it = parameters.find("fasta");
103 //user has given a template file
104 if(it != parameters.end()){
105 path = m->hasPath(it->second);
106 //if the user has not given a path then, add inputdir. else leave path alone.
107 if (path == "") { parameters["fasta"] = inputDir + it->second; }
110 it = parameters.find("phylip");
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["phylip"] = inputDir + it->second; }
119 //check for required parameters
120 fastafile = validParameter.validFile(parameters, "fasta", true);
121 if (fastafile == "not open") { fastafile = ""; abort = true; }
122 else if (fastafile == "not found") { fastafile = ""; }
123 else { inputFile = fastafile; }
125 phylipfile = validParameter.validFile(parameters, "phylip", true);
126 if (phylipfile == "not open") { phylipfile = ""; abort = true; }
127 else if (phylipfile == "not found") { phylipfile = ""; }
128 else { inputFile = phylipfile; }
130 if ((phylipfile == "") && (fastafile == "")) { m->mothurOut("You must provide either a phylip formatted distance matrix or an aligned fasta file."); m->mothurOutEndLine(); abort=true; }
131 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; }
134 //if the user changes the output directory command factory will send this info to us in the output parameter
135 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(inputFile); }
138 temp = validParameter.validFile(parameters, "version", false); if (temp == "not found"){ temp = "F"; }
139 version = m->isTrue(temp);
141 temp = validParameter.validFile(parameters, "verbose", false); if (temp == "not found"){ temp = "F"; }
142 verbose = m->isTrue(temp);
144 temp = validParameter.validFile(parameters, "quiet", false); if (temp == "not found"){ temp = "F"; }
145 quiet = m->isTrue(temp);
147 seed = validParameter.validFile(parameters, "seed", false); if (seed == "not found"){ seed = "*"; }
149 temp = validParameter.validFile(parameters, "norandom", false); if (temp == "not found"){ temp = "F"; }
150 norandom = m->isTrue(temp);
152 temp = validParameter.validFile(parameters, "shuffle", false); if (temp == "not found"){ temp = "F"; }
153 shuffle = m->isTrue(temp);
155 temp = validParameter.validFile(parameters, "neighbor", false); if (temp == "not found"){ temp = "T"; }
156 neighbor = m->isTrue(temp);
158 temp = validParameter.validFile(parameters, "DNA", false); if (temp == "not found"){ temp = "F"; }
159 DNA = m->isTrue(temp);
161 temp = validParameter.validFile(parameters, "protein", false); if (temp == "not found"){ temp = "F"; }
162 protein = m->isTrue(temp);
164 temp = validParameter.validFile(parameters, "jukes", false); if (temp == "not found"){ temp = "F"; }
165 jukes = m->isTrue(temp);
167 temp = validParameter.validFile(parameters, "kimura", false); if (temp == "not found"){ temp = "F"; }
168 kimura = m->isTrue(temp);
170 temp = validParameter.validFile(parameters, "stdout", false); if (temp == "not found"){ temp = "F"; }
171 stdoutWanted = m->isTrue(temp);
173 matrixout = validParameter.validFile(parameters, "matrixout", false); if (matrixout == "not found"){ matrixout = ""; }
175 ntrees = validParameter.validFile(parameters, "ntrees", false); if (ntrees == "not found"){ ntrees = "1"; }
177 temp = validParameter.validFile(parameters, "expblen", false); if (temp == "not found"){ temp = "F"; }
178 expblen = m->isTrue(temp);
180 temp = validParameter.validFile(parameters, "expdist", false); if (temp == "not found"){ temp = "F"; }
181 expdist = m->isTrue(temp);
183 if ((fastafile != "") && ((!DNA) && (!protein))) { m->mothurOut("You must specify the type of sequences you are using: DNA or protein"); m->mothurOutEndLine(); abort=true; }
187 catch(exception& e) {
188 m->errorOut(e, "ClearcutCommand", "ClearcutCommand");
192 //**********************************************************************************************************************
194 void ClearcutCommand::help(){
196 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");
197 m->mothurOut("For more information about clearcut refer to http://bioinformatics.hungry.com/clearcut/ \n");
198 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");
199 m->mothurOut("The phylip parameter allows you to enter your phylip formatted distance matrix. \n");
200 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");
202 m->mothurOut("The version parameter prints out the version of clearcut you are using, default=F. \n");
203 m->mothurOut("The verbose parameter prints out more output from clearcut, default=F. \n");
204 m->mothurOut("The quiet parameter turns on silent operation mode, default=F. \n");
205 m->mothurOut("The seed parameter allows you to explicitly set the PRNG seed to a specific value. \n");
206 m->mothurOut("The norandom parameter allows you to attempt joins deterministically, default=F. \n");
207 m->mothurOut("The shuffle parameter allows you to randomly shuffle the distance matrix, default=F. \n");
208 m->mothurOut("The neighbor parameter allows you to use traditional Neighbor-Joining algorithm, default=T. \n");
210 m->mothurOut("The DNA parameter allows you to indicate your fasta file contains DNA sequences, default=F. \n");
211 m->mothurOut("The protein parameter allows you to indicate your fasta file contains protein sequences, default=F. \n");
213 m->mothurOut("The stdout parameter outputs your tree to STDOUT, default=F. \n");
214 m->mothurOut("The matrixout parameter allows you to specify a filename to output a distance matrix to. \n");
215 m->mothurOut("The ntrees parameter allows you to specify the number of output trees, default=1. \n");
216 m->mothurOut("The expblen parameter allows you to use exponential notation for branch lengths, default=F. \n");
217 m->mothurOut("The expdist parameter allows you to use exponential notation for distance outputs, default=F. \n");
219 m->mothurOut("The clearcut command should be in the following format: \n");
220 m->mothurOut("clearcut(phylip=yourDistanceFile) \n");
221 m->mothurOut("Example: clearcut(phylip=abrecovery.phylip.dist) \n");
224 catch(exception& e) {
225 m->errorOut(e, "ClearcutCommand", "help");
230 /**************************************************************************************/
231 int ClearcutCommand::execute() {
234 if (abort == true) { return 0; }
237 string outputName = outputDir + m->getRootName(m->getSimpleName(inputFile)) + "tre";
238 outputNames.push_back(outputName); outputTypes["tree"].push_back(outputName);
242 char* tempClearcut = new char[8];
243 strcpy(tempClearcut, "clearcut");
244 cPara.push_back(tempClearcut);
246 //you gave us a distance matrix
247 if (phylipfile != "") { char* temp = new char[10]; strcpy(temp, "--distance"); cPara.push_back(temp); }
249 //you gave us a fastafile
250 if (fastafile != "") { char* temp = new char[11]; strcpy(temp, "--alignment"); cPara.push_back(temp); }
252 if (version) { char* temp = new char[9]; strcpy(temp, "--version"); cPara.push_back(temp); }
253 if (verbose) { char* temp = new char[9]; strcpy(temp, "--verbose"); cPara.push_back(temp); }
254 if (quiet) { char* temp = new char[7]; strcpy(temp, "--quiet"); cPara.push_back(temp); }
256 string tempSeed = "--seed=" + seed;
257 char* temp = new char[tempSeed.length()];
258 strcpy(temp, tempSeed.c_str());
259 cPara.push_back(temp);
261 if (norandom) { char* temp = new char[10]; strcpy(temp, "--norandom"); cPara.push_back(temp); }
262 if (shuffle) { char* temp = new char[9]; strcpy(temp, "--shuffle"); cPara.push_back(temp); }
263 if (neighbor) { char* temp = new char[10]; strcpy(temp, "--neighbor"); cPara.push_back(temp); }
265 string tempIn = "--in=" + inputFile;
266 char* tempI = new char[tempIn.length()];
267 strcpy(tempI, tempIn.c_str());
268 cPara.push_back(tempI);
270 if (stdoutWanted) { char* temp = new char[8]; strcpy(temp, "--stdout"); cPara.push_back(temp); }
272 string tempOut = "--out=" + outputName;
274 char* temp = new char[tempOut.length()];
275 strcpy(temp, tempOut.c_str());
276 cPara.push_back(temp);
279 if (DNA) { char* temp = new char[5]; strcpy(temp, "--DNA"); cPara.push_back(temp); }
280 if (protein) { char* temp = new char[9]; strcpy(temp, "--protein"); cPara.push_back(temp); }
281 if (jukes) { char* temp = new char[7]; strcpy(temp, "--jukes"); cPara.push_back(temp); }
282 if (kimura) { char* temp = new char[8]; strcpy(temp, "--kimura"); cPara.push_back(temp); }
283 if (matrixout != "") {
284 string tempMatrix = "--matrixout=" + outputDir + matrixout;
285 char* temp = new char[tempMatrix.length()];
286 strcpy(temp, tempMatrix.c_str());
287 cPara.push_back(temp);
288 outputNames.push_back((outputDir + matrixout));
289 outputTypes["matrixout"].push_back((outputDir + matrixout));
293 string tempNtrees = "--ntrees=" + ntrees;
294 char* temp = new char[tempNtrees.length()];
295 strcpy(temp, tempNtrees.c_str());
296 cPara.push_back(temp);
299 if (expblen) { char* temp = new char[9]; strcpy(temp, "--expblen"); cPara.push_back(temp); }
300 if (expdist) { char* temp = new char[9]; strcpy(temp, "--expdist"); cPara.push_back(temp); }
302 char** clearcutParameters;
303 clearcutParameters = new char*[cPara.size()];
304 for (int i = 0; i < cPara.size(); i++) { clearcutParameters[i] = cPara[i]; }
305 int numArgs = cPara.size();
307 clearcut_main(numArgs, clearcutParameters);
310 for(int i = 0; i < cPara.size(); i++) { delete[] cPara[i]; }
311 delete[] clearcutParameters;
314 m->mothurOutEndLine();
315 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
316 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
317 m->mothurOutEndLine();
322 catch(exception& e) {
323 m->errorOut(e, "ClearcutCommand", "execute");
327 /**************************************************************************************/