5 * Created by westcott on 5/11/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "clearcutcommand.h"
19 //**********************************************************************************************************************
20 vector<string> ClearcutCommand::setParameters(){
22 CommandParameter pphylip("phylip", "InputTypes", "", "", "FastaPhylip", "FastaPhylip", "none",false,false); parameters.push_back(pphylip);
23 CommandParameter pfasta("fasta", "InputTypes", "", "", "FastaPhylip", "FastaPhylip", "none",false,false); parameters.push_back(pfasta);
24 CommandParameter pverbose("verbose", "Boolean", "", "F", "", "", "",false,false); parameters.push_back(pverbose);
25 CommandParameter pquiet("quiet", "Boolean", "", "F", "", "", "",false,false); parameters.push_back(pquiet);
26 CommandParameter pversion("version", "Boolean", "", "F", "", "", "",false,false); parameters.push_back(pversion);
27 CommandParameter pseed("seed", "String", "", "", "*", "", "",false,false); parameters.push_back(pseed);
28 CommandParameter pnorandom("norandom", "Boolean", "", "F", "", "", "",false,false); parameters.push_back(pnorandom);
29 CommandParameter pshuffle("shuffle", "Boolean", "", "F", "", "", "",false,false); parameters.push_back(pshuffle);
30 CommandParameter pneighbor("neighbor", "Boolean", "", "T", "", "", "",false,false); parameters.push_back(pneighbor);
31 CommandParameter pexpblen("expblen", "Boolean", "", "F", "", "", "",false,false); parameters.push_back(pexpblen);
32 CommandParameter pexpdist("expdist", "Boolean", "", "F", "", "", "",false,false); parameters.push_back(pexpdist);
33 CommandParameter pDNA("DNA", "Boolean", "", "F", "", "", "",false,false); parameters.push_back(pDNA);
34 CommandParameter pprotein("protein", "Boolean", "", "F", "", "", "",false,false); parameters.push_back(pprotein);
35 CommandParameter pjukes("jukes", "Boolean", "", "F", "", "", "",false,false); parameters.push_back(pjukes);
36 CommandParameter pkimura("kimura", "Boolean", "", "F", "", "", "",false,false); parameters.push_back(pkimura);
37 CommandParameter pstdout("stdout", "Boolean", "", "F", "", "", "",false,false); parameters.push_back(pstdout);
38 CommandParameter pntrees("ntrees", "Number", "", "1", "", "", "",false,false); parameters.push_back(pntrees);
39 CommandParameter pmatrixout("matrixout", "String", "", "", "", "", "",false,false); parameters.push_back(pmatrixout);
40 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
41 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
43 vector<string> myArray;
44 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
48 m->errorOut(e, "ClearcutCommand", "setParameters");
52 //**********************************************************************************************************************
53 string ClearcutCommand::getHelpString(){
55 string helpString = "";
56 helpString += "The clearcut command interfaces mothur with the clearcut program written by Initiative for Bioinformatics and Evolutionary Studies (IBEST) at the University of Idaho.\n";
57 helpString += "For more information about clearcut refer to http://bioinformatics.hungry.com/clearcut/ \n";
58 helpString += "The clearcut command parameters are phylip, fasta, version, verbose, quiet, seed, norandom, shuffle, neighbor, expblen, expdist, ntrees, matrixout, stdout, kimura, jukes, protein, DNA. \n";
59 helpString += "The phylip parameter allows you to enter your phylip formatted distance matrix. \n";
60 helpString += "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";
62 helpString += "The version parameter prints out the version of clearcut you are using, default=F. \n";
63 helpString += "The verbose parameter prints out more output from clearcut, default=F. \n";
64 helpString += "The quiet parameter turns on silent operation mode, default=F. \n";
65 helpString += "The seed parameter allows you to explicitly set the PRNG seed to a specific value. \n";
66 helpString += "The norandom parameter allows you to attempt joins deterministically, default=F. \n";
67 helpString += "The shuffle parameter allows you to randomly shuffle the distance matrix, default=F. \n";
68 helpString += "The neighbor parameter allows you to use traditional Neighbor-Joining algorithm, default=T. \n";
70 helpString += "The DNA parameter allows you to indicate your fasta file contains DNA sequences, default=F. \n";
71 helpString += "The protein parameter allows you to indicate your fasta file contains protein sequences, default=F. \n";
73 helpString += "The stdout parameter outputs your tree to STDOUT, default=F. \n";
74 helpString += "The matrixout parameter allows you to specify a filename to output a distance matrix to. \n";
75 helpString += "The ntrees parameter allows you to specify the number of output trees, default=1. \n";
76 helpString += "The expblen parameter allows you to use exponential notation for branch lengths, default=F. \n";
77 helpString += "The expdist parameter allows you to use exponential notation for distance outputs, default=F. \n";
79 helpString += "The clearcut command should be in the following format: \n";
80 helpString += "clearcut(phylip=yourDistanceFile) \n";
81 helpString += "Example: clearcut(phylip=abrecovery.phylip.dist) \n";
85 m->errorOut(e, "ClearcutCommand", "getHelpString");
90 //**********************************************************************************************************************
91 ClearcutCommand::ClearcutCommand(){
93 abort = true; calledHelp = true;
95 vector<string> tempOutNames;
96 outputTypes["tree"] = tempOutNames;
97 outputTypes["matrixout"] = tempOutNames;
100 m->errorOut(e, "ClearcutCommand", "ClearcutCommand");
104 /**************************************************************************************/
105 ClearcutCommand::ClearcutCommand(string option) {
107 abort = false; calledHelp = false;
109 //allow user to run help
110 if(option == "help") { help(); abort = true; calledHelp = true; }
113 vector<string> myArray = setParameters();
115 OptionParser parser(option);
116 map<string,string> parameters = parser.getParameters();
118 ValidParameters validParameter;
119 map<string, string>::iterator it;
121 //check to make sure all parameters are valid for command
122 for (it = parameters.begin(); it != parameters.end(); it++) {
123 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
126 //initialize outputTypes
127 vector<string> tempOutNames;
128 outputTypes["tree"] = tempOutNames;
129 outputTypes["matrixout"] = tempOutNames;
131 //if the user changes the input directory command factory will send this info to us in the output parameter
132 string inputDir = validParameter.validFile(parameters, "inputdir", false);
133 if (inputDir == "not found"){ inputDir = ""; }
136 it = parameters.find("fasta");
137 //user has given a template file
138 if(it != parameters.end()){
139 path = m->hasPath(it->second);
140 //if the user has not given a path then, add inputdir. else leave path alone.
141 if (path == "") { parameters["fasta"] = inputDir + it->second; }
144 it = parameters.find("phylip");
145 //user has given a template file
146 if(it != parameters.end()){
147 path = m->hasPath(it->second);
148 //if the user has not given a path then, add inputdir. else leave path alone.
149 if (path == "") { parameters["phylip"] = inputDir + it->second; }
153 //check for required parameters
154 fastafile = validParameter.validFile(parameters, "fasta", true);
155 if (fastafile == "not open") { fastafile = ""; abort = true; }
156 else if (fastafile == "not found") { fastafile = ""; }
157 else { inputFile = fastafile; }
159 phylipfile = validParameter.validFile(parameters, "phylip", true);
160 if (phylipfile == "not open") { phylipfile = ""; abort = true; }
161 else if (phylipfile == "not found") { phylipfile = ""; }
162 else { inputFile = phylipfile; }
164 if ((phylipfile == "") && (fastafile == "")) {
165 //is there are current file available for either of these?
166 //give priority to phylip, then fasta
167 phylipfile = m->getPhylipFile();
168 if (phylipfile != "") { m->mothurOut("Using " + phylipfile + " as input file for the phylip parameter."); m->mothurOutEndLine(); }
170 fastafile = m->getFastaFile();
171 if (fastafile != "") { m->mothurOut("Using " + fastafile + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
173 m->mothurOut("No valid current files. You must provide a phylip or fasta file before you can use the clearcut command."); m->mothurOutEndLine();
178 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; }
181 //if the user changes the output directory command factory will send this info to us in the output parameter
182 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(inputFile); }
185 temp = validParameter.validFile(parameters, "version", false); if (temp == "not found"){ temp = "F"; }
186 version = m->isTrue(temp);
188 temp = validParameter.validFile(parameters, "verbose", false); if (temp == "not found"){ temp = "F"; }
189 verbose = m->isTrue(temp);
191 temp = validParameter.validFile(parameters, "quiet", false); if (temp == "not found"){ temp = "F"; }
192 quiet = m->isTrue(temp);
194 seed = validParameter.validFile(parameters, "seed", false); if (seed == "not found"){ seed = "*"; }
196 temp = validParameter.validFile(parameters, "norandom", false); if (temp == "not found"){ temp = "F"; }
197 norandom = m->isTrue(temp);
199 temp = validParameter.validFile(parameters, "shuffle", false); if (temp == "not found"){ temp = "F"; }
200 shuffle = m->isTrue(temp);
202 temp = validParameter.validFile(parameters, "neighbor", false); if (temp == "not found"){ temp = "T"; }
203 neighbor = m->isTrue(temp);
205 temp = validParameter.validFile(parameters, "DNA", false); if (temp == "not found"){ temp = "F"; }
206 DNA = m->isTrue(temp);
208 temp = validParameter.validFile(parameters, "protein", false); if (temp == "not found"){ temp = "F"; }
209 protein = m->isTrue(temp);
211 temp = validParameter.validFile(parameters, "jukes", false); if (temp == "not found"){ temp = "F"; }
212 jukes = m->isTrue(temp);
214 temp = validParameter.validFile(parameters, "kimura", false); if (temp == "not found"){ temp = "F"; }
215 kimura = m->isTrue(temp);
217 temp = validParameter.validFile(parameters, "stdout", false); if (temp == "not found"){ temp = "F"; }
218 stdoutWanted = m->isTrue(temp);
220 matrixout = validParameter.validFile(parameters, "matrixout", false); if (matrixout == "not found"){ matrixout = ""; }
222 ntrees = validParameter.validFile(parameters, "ntrees", false); if (ntrees == "not found"){ ntrees = "1"; }
224 temp = validParameter.validFile(parameters, "expblen", false); if (temp == "not found"){ temp = "F"; }
225 expblen = m->isTrue(temp);
227 temp = validParameter.validFile(parameters, "expdist", false); if (temp == "not found"){ temp = "F"; }
228 expdist = m->isTrue(temp);
230 if ((fastafile != "") && ((!DNA) && (!protein))) { m->mothurOut("You must specify the type of sequences you are using: DNA or protein"); m->mothurOutEndLine(); abort=true; }
234 catch(exception& e) {
235 m->errorOut(e, "ClearcutCommand", "ClearcutCommand");
239 /**************************************************************************************/
240 int ClearcutCommand::execute() {
243 if (abort == true) { if (calledHelp) { return 0; } return 2; }
246 string outputName = outputDir + m->getRootName(m->getSimpleName(inputFile)) + "tre";
247 outputNames.push_back(outputName); outputTypes["tree"].push_back(outputName);
251 char* tempClearcut = new char[8];
252 strcpy(tempClearcut, "clearcut");
253 cPara.push_back(tempClearcut);
255 //you gave us a distance matrix
256 if (phylipfile != "") { char* temp = new char[10]; strcpy(temp, "--distance"); cPara.push_back(temp); }
258 //you gave us a fastafile
259 if (fastafile != "") { char* temp = new char[11]; strcpy(temp, "--alignment"); cPara.push_back(temp); }
261 if (version) { char* temp = new char[9]; strcpy(temp, "--version"); cPara.push_back(temp); }
262 if (verbose) { char* temp = new char[9]; strcpy(temp, "--verbose"); cPara.push_back(temp); }
263 if (quiet) { char* temp = new char[7]; strcpy(temp, "--quiet"); cPara.push_back(temp); }
265 string tempSeed = "--seed=" + seed;
266 char* temp = new char[tempSeed.length()];
267 strcpy(temp, tempSeed.c_str());
268 cPara.push_back(temp);
270 if (norandom) { char* temp = new char[10]; strcpy(temp, "--norandom"); cPara.push_back(temp); }
271 if (shuffle) { char* temp = new char[9]; strcpy(temp, "--shuffle"); cPara.push_back(temp); }
272 if (neighbor) { char* temp = new char[10]; strcpy(temp, "--neighbor"); cPara.push_back(temp); }
274 string tempIn = "--in=" + inputFile;
275 char* tempI = new char[tempIn.length()];
276 strcpy(tempI, tempIn.c_str());
277 cPara.push_back(tempI);
279 if (stdoutWanted) { char* temp = new char[8]; strcpy(temp, "--stdout"); cPara.push_back(temp); }
281 string tempOut = "--out=" + outputName;
283 char* temp = new char[tempOut.length()];
284 strcpy(temp, tempOut.c_str());
285 cPara.push_back(temp);
288 if (DNA) { char* temp = new char[5]; strcpy(temp, "--DNA"); cPara.push_back(temp); }
289 if (protein) { char* temp = new char[9]; strcpy(temp, "--protein"); cPara.push_back(temp); }
290 if (jukes) { char* temp = new char[7]; strcpy(temp, "--jukes"); cPara.push_back(temp); }
291 if (kimura) { char* temp = new char[8]; strcpy(temp, "--kimura"); cPara.push_back(temp); }
292 if (matrixout != "") {
293 string tempMatrix = "--matrixout=" + outputDir + matrixout;
294 char* temp = new char[tempMatrix.length()];
295 strcpy(temp, tempMatrix.c_str());
296 cPara.push_back(temp);
297 outputNames.push_back((outputDir + matrixout));
298 outputTypes["matrixout"].push_back((outputDir + matrixout));
302 string tempNtrees = "--ntrees=" + ntrees;
303 char* temp = new char[tempNtrees.length()];
304 strcpy(temp, tempNtrees.c_str());
305 cPara.push_back(temp);
308 if (expblen) { char* temp = new char[9]; strcpy(temp, "--expblen"); cPara.push_back(temp); }
309 if (expdist) { char* temp = new char[9]; strcpy(temp, "--expdist"); cPara.push_back(temp); }
311 char** clearcutParameters;
312 clearcutParameters = new char*[cPara.size()];
313 for (int i = 0; i < cPara.size(); i++) { clearcutParameters[i] = cPara[i]; }
314 int numArgs = cPara.size();
316 clearcut_main(numArgs, clearcutParameters);
319 for(int i = 0; i < cPara.size(); i++) { delete[] cPara[i]; }
320 delete[] clearcutParameters;
324 //set first tree file as new current treefile
325 string currentTree = "";
326 itTypes = outputTypes.find("tree");
327 if (itTypes != outputTypes.end()) {
328 if ((itTypes->second).size() != 0) { currentTree = (itTypes->second)[0]; m->setTreeFile(currentTree); }
331 m->mothurOutEndLine();
332 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
333 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
334 m->mothurOutEndLine();
339 catch(exception& e) {
340 m->errorOut(e, "ClearcutCommand", "execute");
344 /**************************************************************************************/