5 * Created by westcott on 5/11/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "clearcutcommand.h"
13 //**********************************************************************************************************************
14 vector<string> ClearcutCommand::getValidParameters(){
16 string AlignArray[] = {"fasta","phylip","version","verbose","quiet","seed","norandom","shuffle","neighbor","expblen",
17 "expdist","ntrees","matrixout","stdout","kimura","jukes","protein","DNA","outputdir","inputdir"};
18 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
22 m->errorOut(e, "ClearcutCommand", "getValidParameters");
26 //**********************************************************************************************************************
27 ClearcutCommand::ClearcutCommand(){
29 //initialize outputTypes
30 vector<string> tempOutNames;
31 outputTypes["tree"] = tempOutNames;
32 outputTypes["matrixout"] = tempOutNames;
35 m->errorOut(e, "ClearcutCommand", "ClearcutCommand");
38 }//**********************************************************************************************************************
39 vector<string> ClearcutCommand::getRequiredParameters(){
41 string Array[] = {"fasta","phylip","or"};
42 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
46 m->errorOut(e, "ClearcutCommand", "getRequiredParameters");
50 //**********************************************************************************************************************
51 vector<string> ClearcutCommand::getRequiredFiles(){
53 vector<string> myArray;
57 m->errorOut(e, "ClearcutCommand", "getRequiredFiles");
61 /**************************************************************************************/
62 ClearcutCommand::ClearcutCommand(string option) {
66 //allow user to run help
67 if(option == "help") { help(); abort = true; }
70 //valid paramters for this command
71 string Array[] = {"fasta","phylip","version","verbose","quiet","seed","norandom","shuffle","neighbor","expblen",
72 "expdist","ntrees","matrixout","stdout","kimura","jukes","protein","DNA","outputdir","inputdir"};
73 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
75 OptionParser parser(option);
76 map<string,string> parameters = parser.getParameters();
78 ValidParameters validParameter;
79 map<string, string>::iterator it;
81 //check to make sure all parameters are valid for command
82 for (it = parameters.begin(); it != parameters.end(); it++) {
83 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
86 //initialize outputTypes
87 vector<string> tempOutNames;
88 outputTypes["tree"] = tempOutNames;
89 outputTypes["matrixout"] = tempOutNames;
91 //if the user changes the input directory command factory will send this info to us in the output parameter
92 string inputDir = validParameter.validFile(parameters, "inputdir", false);
93 if (inputDir == "not found"){ inputDir = ""; }
96 it = parameters.find("fasta");
97 //user has given a template file
98 if(it != parameters.end()){
99 path = m->hasPath(it->second);
100 //if the user has not given a path then, add inputdir. else leave path alone.
101 if (path == "") { parameters["fasta"] = inputDir + it->second; }
104 it = parameters.find("phylip");
105 //user has given a template file
106 if(it != parameters.end()){
107 path = m->hasPath(it->second);
108 //if the user has not given a path then, add inputdir. else leave path alone.
109 if (path == "") { parameters["phylip"] = inputDir + it->second; }
113 //check for required parameters
114 fastafile = validParameter.validFile(parameters, "fasta", true);
115 if (fastafile == "not open") { fastafile = ""; abort = true; }
116 else if (fastafile == "not found") { fastafile = ""; }
117 else { inputFile = fastafile; }
119 phylipfile = validParameter.validFile(parameters, "phylip", true);
120 if (phylipfile == "not open") { phylipfile = ""; abort = true; }
121 else if (phylipfile == "not found") { phylipfile = ""; }
122 else { inputFile = phylipfile; }
124 if ((phylipfile == "") && (fastafile == "")) { m->mothurOut("You must provide either a phylip formatted distance matrix or an aligned fasta file."); m->mothurOutEndLine(); abort=true; }
125 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; }
128 //if the user changes the output directory command factory will send this info to us in the output parameter
129 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(inputFile); }
132 temp = validParameter.validFile(parameters, "version", false); if (temp == "not found"){ temp = "F"; }
133 version = m->isTrue(temp);
135 temp = validParameter.validFile(parameters, "verbose", false); if (temp == "not found"){ temp = "F"; }
136 verbose = m->isTrue(temp);
138 temp = validParameter.validFile(parameters, "quiet", false); if (temp == "not found"){ temp = "F"; }
139 quiet = m->isTrue(temp);
141 seed = validParameter.validFile(parameters, "seed", false); if (seed == "not found"){ seed = "*"; }
143 temp = validParameter.validFile(parameters, "norandom", false); if (temp == "not found"){ temp = "F"; }
144 norandom = m->isTrue(temp);
146 temp = validParameter.validFile(parameters, "shuffle", false); if (temp == "not found"){ temp = "F"; }
147 shuffle = m->isTrue(temp);
149 temp = validParameter.validFile(parameters, "neighbor", false); if (temp == "not found"){ temp = "T"; }
150 neighbor = m->isTrue(temp);
152 temp = validParameter.validFile(parameters, "DNA", false); if (temp == "not found"){ temp = "F"; }
153 DNA = m->isTrue(temp);
155 temp = validParameter.validFile(parameters, "protein", false); if (temp == "not found"){ temp = "F"; }
156 protein = m->isTrue(temp);
158 temp = validParameter.validFile(parameters, "jukes", false); if (temp == "not found"){ temp = "F"; }
159 jukes = m->isTrue(temp);
161 temp = validParameter.validFile(parameters, "kimura", false); if (temp == "not found"){ temp = "F"; }
162 kimura = m->isTrue(temp);
164 temp = validParameter.validFile(parameters, "stdout", false); if (temp == "not found"){ temp = "F"; }
165 stdoutWanted = m->isTrue(temp);
167 matrixout = validParameter.validFile(parameters, "matrixout", false); if (matrixout == "not found"){ matrixout = ""; }
169 ntrees = validParameter.validFile(parameters, "ntrees", false); if (ntrees == "not found"){ ntrees = "1"; }
171 temp = validParameter.validFile(parameters, "expblen", false); if (temp == "not found"){ temp = "F"; }
172 expblen = m->isTrue(temp);
174 temp = validParameter.validFile(parameters, "expdist", false); if (temp == "not found"){ temp = "F"; }
175 expdist = m->isTrue(temp);
177 if ((fastafile != "") && ((!DNA) && (!protein))) { m->mothurOut("You must specify the type of sequences you are using: DNA or protein"); m->mothurOutEndLine(); abort=true; }
181 catch(exception& e) {
182 m->errorOut(e, "ClearcutCommand", "ClearcutCommand");
186 //**********************************************************************************************************************
188 void ClearcutCommand::help(){
190 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");
191 m->mothurOut("For more information about clearcut refer to http://bioinformatics.hungry.com/clearcut/ \n");
192 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");
193 m->mothurOut("The phylip parameter allows you to enter your phylip formatted distance matrix. \n");
194 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");
196 m->mothurOut("The version parameter prints out the version of clearcut you are using, default=F. \n");
197 m->mothurOut("The verbose parameter prints out more output from clearcut, default=F. \n");
198 m->mothurOut("The quiet parameter turns on silent operation mode, default=F. \n");
199 m->mothurOut("The seed parameter allows you to explicitly set the PRNG seed to a specific value. \n");
200 m->mothurOut("The norandom parameter allows you to attempt joins deterministically, default=F. \n");
201 m->mothurOut("The shuffle parameter allows you to randomly shuffle the distance matrix, default=F. \n");
202 m->mothurOut("The neighbor parameter allows you to use traditional Neighbor-Joining algorithm, default=T. \n");
204 m->mothurOut("The DNA parameter allows you to indicate your fasta file contains DNA sequences, default=F. \n");
205 m->mothurOut("The protein parameter allows you to indicate your fasta file contains protein sequences, default=F. \n");
207 m->mothurOut("The stdout parameter outputs your tree to STDOUT, default=F. \n");
208 m->mothurOut("The matrixout parameter allows you to specify a filename to output a distance matrix to. \n");
209 m->mothurOut("The ntrees parameter allows you to specify the number of output trees, default=1. \n");
210 m->mothurOut("The expblen parameter allows you to use exponential notation for branch lengths, default=F. \n");
211 m->mothurOut("The expdist parameter allows you to use exponential notation for distance outputs, default=F. \n");
213 m->mothurOut("The clearcut command should be in the following format: \n");
214 m->mothurOut("clearcut(phylip=yourDistanceFile) \n");
215 m->mothurOut("Example: clearcut(phylip=abrecovery.phylip.dist) \n");
218 catch(exception& e) {
219 m->errorOut(e, "ClearcutCommand", "help");
224 /**************************************************************************************/
225 int ClearcutCommand::execute() {
228 if (abort == true) { return 0; }
231 string outputName = outputDir + m->getRootName(m->getSimpleName(inputFile)) + "tre";
232 outputNames.push_back(outputName); outputTypes["tree"].push_back(outputName);
236 char* tempClearcut = new char[8];
237 strcpy(tempClearcut, "clearcut");
238 cPara.push_back(tempClearcut);
240 //you gave us a distance matrix
241 if (phylipfile != "") { char* temp = new char[10]; strcpy(temp, "--distance"); cPara.push_back(temp); }
243 //you gave us a fastafile
244 if (fastafile != "") { char* temp = new char[11]; strcpy(temp, "--alignment"); cPara.push_back(temp); }
246 if (version) { char* temp = new char[9]; strcpy(temp, "--version"); cPara.push_back(temp); }
247 if (verbose) { char* temp = new char[9]; strcpy(temp, "--verbose"); cPara.push_back(temp); }
248 if (quiet) { char* temp = new char[7]; strcpy(temp, "--quiet"); cPara.push_back(temp); }
250 string tempSeed = "--seed=" + seed;
251 char* temp = new char[tempSeed.length()];
252 strcpy(temp, tempSeed.c_str());
253 cPara.push_back(temp);
255 if (norandom) { char* temp = new char[10]; strcpy(temp, "--norandom"); cPara.push_back(temp); }
256 if (shuffle) { char* temp = new char[9]; strcpy(temp, "--shuffle"); cPara.push_back(temp); }
257 if (neighbor) { char* temp = new char[10]; strcpy(temp, "--neighbor"); cPara.push_back(temp); }
259 string tempIn = "--in=" + inputFile;
260 char* tempI = new char[tempIn.length()];
261 strcpy(tempI, tempIn.c_str());
262 cPara.push_back(tempI);
264 if (stdoutWanted) { char* temp = new char[8]; strcpy(temp, "--stdout"); cPara.push_back(temp); }
266 string tempOut = "--out=" + outputName;
268 char* temp = new char[tempOut.length()];
269 strcpy(temp, tempOut.c_str());
270 cPara.push_back(temp);
273 if (DNA) { char* temp = new char[5]; strcpy(temp, "--DNA"); cPara.push_back(temp); }
274 if (protein) { char* temp = new char[9]; strcpy(temp, "--protein"); cPara.push_back(temp); }
275 if (jukes) { char* temp = new char[7]; strcpy(temp, "--jukes"); cPara.push_back(temp); }
276 if (kimura) { char* temp = new char[8]; strcpy(temp, "--kimura"); cPara.push_back(temp); }
277 if (matrixout != "") {
278 string tempMatrix = "--matrixout=" + outputDir + matrixout;
279 char* temp = new char[tempMatrix.length()];
280 strcpy(temp, tempMatrix.c_str());
281 cPara.push_back(temp);
282 outputNames.push_back((outputDir + matrixout));
283 outputTypes["matrixout"].push_back((outputDir + matrixout));
287 string tempNtrees = "--ntrees=" + ntrees;
288 char* temp = new char[tempNtrees.length()];
289 strcpy(temp, tempNtrees.c_str());
290 cPara.push_back(temp);
293 if (expblen) { char* temp = new char[9]; strcpy(temp, "--expblen"); cPara.push_back(temp); }
294 if (expdist) { char* temp = new char[9]; strcpy(temp, "--expdist"); cPara.push_back(temp); }
296 char** clearcutParameters;
297 clearcutParameters = new char*[cPara.size()];
298 for (int i = 0; i < cPara.size(); i++) { clearcutParameters[i] = cPara[i]; }
299 int numArgs = cPara.size();
301 clearcut_main(numArgs, clearcutParameters);
304 for(int i = 0; i < cPara.size(); i++) { delete[] cPara[i]; }
305 delete[] clearcutParameters;
308 m->mothurOutEndLine();
309 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
310 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
311 m->mothurOutEndLine();
316 catch(exception& e) {
317 m->errorOut(e, "ClearcutCommand", "execute");
321 /**************************************************************************************/