5 * Created by westcott on 5/11/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "clearcutcommand.h"
14 /**************************************************************************************/
15 ClearcutCommand::ClearcutCommand(string option) {
19 //allow user to run help
20 if(option == "help") { help(); abort = true; }
23 //valid paramters for this command
24 string Array[] = {"fasta","phylip","version","verbose","quiet","seed","norandom","shuffle","neighbor","expblen",
25 "expdist","ntrees","matrixout","stdout","kimura","jukes","protein","DNA","outputdir","inputdir"};
26 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
28 OptionParser parser(option);
29 map<string,string> parameters = parser.getParameters();
31 ValidParameters validParameter;
32 map<string, string>::iterator it;
34 //check to make sure all parameters are valid for command
35 for (it = parameters.begin(); it != parameters.end(); it++) {
36 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
39 //if the user changes the input directory command factory will send this info to us in the output parameter
40 string inputDir = validParameter.validFile(parameters, "inputdir", false);
41 if (inputDir == "not found"){ inputDir = ""; }
44 it = parameters.find("fasta");
45 //user has given a template file
46 if(it != parameters.end()){
47 path = m->hasPath(it->second);
48 //if the user has not given a path then, add inputdir. else leave path alone.
49 if (path == "") { parameters["fasta"] = inputDir + it->second; }
52 it = parameters.find("phylip");
53 //user has given a template file
54 if(it != parameters.end()){
55 path = m->hasPath(it->second);
56 //if the user has not given a path then, add inputdir. else leave path alone.
57 if (path == "") { parameters["phylip"] = inputDir + it->second; }
61 //check for required parameters
62 fastafile = validParameter.validFile(parameters, "fasta", true);
63 if (fastafile == "not open") { fastafile = ""; abort = true; }
64 else if (fastafile == "not found") { fastafile = ""; }
65 else { inputFile = fastafile; }
67 phylipfile = validParameter.validFile(parameters, "phylip", true);
68 if (phylipfile == "not open") { phylipfile = ""; abort = true; }
69 else if (phylipfile == "not found") { phylipfile = ""; }
70 else { inputFile = phylipfile; }
72 if ((phylipfile == "") && (fastafile == "")) { m->mothurOut("You must provide either a phylip formatted distance matrix or an aligned fasta file."); m->mothurOutEndLine(); abort=true; }
73 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; }
76 //if the user changes the output directory command factory will send this info to us in the output parameter
77 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(inputFile); }
80 temp = validParameter.validFile(parameters, "version", false); if (temp == "not found"){ temp = "F"; }
81 version = m->isTrue(temp);
83 temp = validParameter.validFile(parameters, "verbose", false); if (temp == "not found"){ temp = "F"; }
84 verbose = m->isTrue(temp);
86 temp = validParameter.validFile(parameters, "quiet", false); if (temp == "not found"){ temp = "F"; }
87 quiet = m->isTrue(temp);
89 seed = validParameter.validFile(parameters, "seed", false); if (seed == "not found"){ seed = "*"; }
91 temp = validParameter.validFile(parameters, "norandom", false); if (temp == "not found"){ temp = "F"; }
92 norandom = m->isTrue(temp);
94 temp = validParameter.validFile(parameters, "shuffle", false); if (temp == "not found"){ temp = "F"; }
95 shuffle = m->isTrue(temp);
97 temp = validParameter.validFile(parameters, "neighbor", false); if (temp == "not found"){ temp = "T"; }
98 neighbor = m->isTrue(temp);
100 temp = validParameter.validFile(parameters, "DNA", false); if (temp == "not found"){ temp = "F"; }
101 DNA = m->isTrue(temp);
103 temp = validParameter.validFile(parameters, "protein", false); if (temp == "not found"){ temp = "F"; }
104 protein = m->isTrue(temp);
106 temp = validParameter.validFile(parameters, "jukes", false); if (temp == "not found"){ temp = "F"; }
107 jukes = m->isTrue(temp);
109 temp = validParameter.validFile(parameters, "kimura", false); if (temp == "not found"){ temp = "F"; }
110 kimura = m->isTrue(temp);
112 temp = validParameter.validFile(parameters, "stdout", false); if (temp == "not found"){ temp = "F"; }
113 stdoutWanted = m->isTrue(temp);
115 matrixout = validParameter.validFile(parameters, "matrixout", false); if (matrixout == "not found"){ matrixout = ""; }
117 ntrees = validParameter.validFile(parameters, "ntrees", false); if (ntrees == "not found"){ ntrees = "1"; }
119 temp = validParameter.validFile(parameters, "expblen", false); if (temp == "not found"){ temp = "F"; }
120 expblen = m->isTrue(temp);
122 temp = validParameter.validFile(parameters, "expdist", false); if (temp == "not found"){ temp = "F"; }
123 expdist = m->isTrue(temp);
125 if ((fastafile != "") && ((!DNA) && (!protein))) { m->mothurOut("You must specify the type of sequences you are using: DNA or protein"); m->mothurOutEndLine(); abort=true; }
129 catch(exception& e) {
130 m->errorOut(e, "ClearcutCommand", "ClearcutCommand");
134 //**********************************************************************************************************************
136 void ClearcutCommand::help(){
138 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");
139 m->mothurOut("For more information about clearcut refer to http://bioinformatics.hungry.com/clearcut/ \n");
140 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");
141 m->mothurOut("The phylip parameter allows you to enter your phylip formatted distance matrix. \n");
142 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");
144 m->mothurOut("The version parameter prints out the version of clearcut you are using, default=F. \n");
145 m->mothurOut("The verbose parameter prints out more output from clearcut, default=F. \n");
146 m->mothurOut("The quiet parameter turns on silent operation mode, default=F. \n");
147 m->mothurOut("The seed parameter allows you to explicitly set the PRNG seed to a specific value. \n");
148 m->mothurOut("The norandom parameter allows you to attempt joins deterministically, default=F. \n");
149 m->mothurOut("The shuffle parameter allows you to randomly shuffle the distance matrix, default=F. \n");
150 m->mothurOut("The neighbor parameter allows you to use traditional Neighbor-Joining algorithm, default=T. \n");
152 m->mothurOut("The DNA parameter allows you to indicate your fasta file contains DNA sequences, default=F. \n");
153 m->mothurOut("The protein parameter allows you to indicate your fasta file contains protein sequences, default=F. \n");
155 m->mothurOut("The stdout parameter outputs your tree to STDOUT, default=F. \n");
156 m->mothurOut("The matrixout parameter allows you to specify a filename to output a distance matrix to. \n");
157 m->mothurOut("The ntrees parameter allows you to specify the number of output trees, default=1. \n");
158 m->mothurOut("The expblen parameter allows you to use exponential notation for branch lengths, default=F. \n");
159 m->mothurOut("The expdist parameter allows you to use exponential notation for distance outputs, default=F. \n");
161 m->mothurOut("The clearcut command should be in the following format: \n");
162 m->mothurOut("clearcut(phylip=yourDistanceFile) \n");
163 m->mothurOut("Example: clearcut(phylip=abrecovery.phylip.dist) \n");
166 catch(exception& e) {
167 m->errorOut(e, "ClearcutCommand", "help");
172 /**************************************************************************************/
173 int ClearcutCommand::execute() {
176 if (abort == true) { return 0; }
179 string outputName = outputDir + m->getRootName(m->getSimpleName(inputFile)) + "tre";
183 char* tempClearcut = new char[8];
184 strcpy(tempClearcut, "clearcut");
185 cPara.push_back(tempClearcut);
187 //you gave us a distance matrix
188 if (phylipfile != "") { char* temp = new char[10]; strcpy(temp, "--distance"); cPara.push_back(temp); }
190 //you gave us a fastafile
191 if (fastafile != "") { char* temp = new char[11]; strcpy(temp, "--alignment"); cPara.push_back(temp); }
193 if (version) { char* temp = new char[9]; strcpy(temp, "--version"); cPara.push_back(temp); }
194 if (verbose) { char* temp = new char[9]; strcpy(temp, "--verbose"); cPara.push_back(temp); }
195 if (quiet) { char* temp = new char[7]; strcpy(temp, "--quiet"); cPara.push_back(temp); }
197 string tempSeed = "--seed=" + seed;
198 char* temp = new char[tempSeed.length()];
199 strcpy(temp, tempSeed.c_str());
200 cPara.push_back(temp);
202 if (norandom) { char* temp = new char[10]; strcpy(temp, "--norandom"); cPara.push_back(temp); }
203 if (shuffle) { char* temp = new char[9]; strcpy(temp, "--shuffle"); cPara.push_back(temp); }
204 if (neighbor) { char* temp = new char[10]; strcpy(temp, "--neighbor"); cPara.push_back(temp); }
206 string tempIn = "--in=" + inputFile;
207 char* tempI = new char[tempIn.length()];
208 strcpy(tempI, tempIn.c_str());
209 cPara.push_back(tempI);
211 if (stdoutWanted) { char* temp = new char[8]; strcpy(temp, "--stdout"); cPara.push_back(temp); }
213 string tempOut = "--out=" + outputName;
215 char* temp = new char[tempOut.length()];
216 strcpy(temp, tempOut.c_str());
217 cPara.push_back(temp);
220 if (DNA) { char* temp = new char[5]; strcpy(temp, "--DNA"); cPara.push_back(temp); }
221 if (protein) { char* temp = new char[9]; strcpy(temp, "--protein"); cPara.push_back(temp); }
222 if (jukes) { char* temp = new char[7]; strcpy(temp, "--jukes"); cPara.push_back(temp); }
223 if (kimura) { char* temp = new char[8]; strcpy(temp, "--kimura"); cPara.push_back(temp); }
224 if (matrixout != "") {
225 string tempMatrix = "--matrixout=" + outputDir + matrixout;
226 char* temp = new char[tempMatrix.length()];
227 strcpy(temp, tempMatrix.c_str());
228 cPara.push_back(temp);
232 string tempNtrees = "--ntrees=" + ntrees;
233 char* temp = new char[tempNtrees.length()];
234 strcpy(temp, tempNtrees.c_str());
235 cPara.push_back(temp);
238 if (expblen) { char* temp = new char[9]; strcpy(temp, "--expblen"); cPara.push_back(temp); }
239 if (expdist) { char* temp = new char[9]; strcpy(temp, "--expdist"); cPara.push_back(temp); }
241 char** clearcutParameters;
242 clearcutParameters = new char*[cPara.size()];
243 for (int i = 0; i < cPara.size(); i++) { clearcutParameters[i] = cPara[i]; }
244 int numArgs = cPara.size();
246 clearcut_main(numArgs, clearcutParameters);
249 for(int i = 0; i < cPara.size(); i++) { delete[] cPara[i]; }
250 delete[] clearcutParameters;
253 m->mothurOutEndLine();
254 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
255 m->mothurOut(outputName); m->mothurOutEndLine();
256 if (matrixout != "") { m->mothurOut(outputDir+matrixout); m->mothurOutEndLine(); }
257 m->mothurOutEndLine();
262 catch(exception& e) {
263 m->errorOut(e, "ClearcutCommand", "execute");
267 /**************************************************************************************/