5 * Created by westcott on 5/11/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "clearcutcommand.h"
12 /**************************************************************************************/
13 ClearcutCommand::ClearcutCommand(string option) {
17 //allow user to run help
18 if(option == "help") { help(); abort = true; }
21 //valid paramters for this command
22 string Array[] = {"fasta","phylip","version","verbose","quiet","seed","norandom","shuffle","neighbor","expblen",
23 "expdist","ntrees","matrixout","stdout","kimura","jukes","protein","DNA","outputdir","inputdir"};
24 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
26 OptionParser parser(option);
27 map<string,string> parameters = parser.getParameters();
29 ValidParameters validParameter;
30 map<string, string>::iterator it;
32 //check to make sure all parameters are valid for command
33 for (it = parameters.begin(); it != parameters.end(); it++) {
34 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
37 //if the user changes the input directory command factory will send this info to us in the output parameter
38 string inputDir = validParameter.validFile(parameters, "inputdir", false);
39 if (inputDir == "not found"){ inputDir = ""; }
42 it = parameters.find("fasta");
43 //user has given a template file
44 if(it != parameters.end()){
45 path = hasPath(it->second);
46 //if the user has not given a path then, add inputdir. else leave path alone.
47 if (path == "") { parameters["fasta"] = inputDir + it->second; }
50 it = parameters.find("phylip");
51 //user has given a template file
52 if(it != parameters.end()){
53 path = hasPath(it->second);
54 //if the user has not given a path then, add inputdir. else leave path alone.
55 if (path == "") { parameters["phylip"] = inputDir + it->second; }
59 //check for required parameters
60 fastafile = validParameter.validFile(parameters, "fasta", true);
61 if (fastafile == "not open") { fastafile = ""; abort = true; }
62 else if (fastafile == "not found") { fastafile = ""; }
63 else { inputFile = fastafile; }
65 phylipfile = validParameter.validFile(parameters, "phylip", true);
66 if (phylipfile == "not open") { phylipfile = ""; abort = true; }
67 else if (phylipfile == "not found") { phylipfile = ""; }
68 else { inputFile = phylipfile; }
70 if ((phylipfile == "") && (fastafile == "")) { m->mothurOut("You must provide either a phylip formatted distance matrix or an aligned fasta file."); m->mothurOutEndLine(); abort=true; }
71 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; }
74 //if the user changes the output directory command factory will send this info to us in the output parameter
75 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
78 temp = validParameter.validFile(parameters, "version", false); if (temp == "not found"){ temp = "F"; }
79 version = isTrue(temp);
81 temp = validParameter.validFile(parameters, "verbose", false); if (temp == "not found"){ temp = "F"; }
82 verbose = isTrue(temp);
84 temp = validParameter.validFile(parameters, "quiet", false); if (temp == "not found"){ temp = "F"; }
87 seed = validParameter.validFile(parameters, "seed", false); if (seed == "not found"){ seed = "*"; }
89 temp = validParameter.validFile(parameters, "norandom", false); if (temp == "not found"){ temp = "F"; }
90 norandom = isTrue(temp);
92 temp = validParameter.validFile(parameters, "shuffle", false); if (temp == "not found"){ temp = "F"; }
93 shuffle = isTrue(temp);
95 temp = validParameter.validFile(parameters, "neighbor", false); if (temp == "not found"){ temp = "F"; }
96 neighbor = isTrue(temp);
98 temp = validParameter.validFile(parameters, "DNA", false); if (temp == "not found"){ temp = "F"; }
101 temp = validParameter.validFile(parameters, "protein", false); if (temp == "not found"){ temp = "F"; }
102 protein = isTrue(temp);
104 temp = validParameter.validFile(parameters, "jukes", false); if (temp == "not found"){ temp = "F"; }
105 jukes = isTrue(temp);
107 temp = validParameter.validFile(parameters, "kimura", false); if (temp == "not found"){ temp = "F"; }
108 kimura = isTrue(temp);
110 temp = validParameter.validFile(parameters, "stdout", false); if (temp == "not found"){ temp = "F"; }
111 stdoutWanted = isTrue(temp);
113 matrixout = validParameter.validFile(parameters, "matrixout", false); if (matrixout == "not found"){ matrixout = ""; }
115 ntrees = validParameter.validFile(parameters, "ntrees", false); if (ntrees == "not found"){ ntrees = "1"; }
117 temp = validParameter.validFile(parameters, "expblen", false); if (temp == "not found"){ temp = "F"; }
118 expblen = isTrue(temp);
120 temp = validParameter.validFile(parameters, "expdist", false); if (temp == "not found"){ temp = "F"; }
121 expdist = isTrue(temp);
123 if ((fastafile != "") && ((!DNA) && (!protein))) { m->mothurOut("You must specify the type of sequences you are using: DNA or protein"); m->mothurOutEndLine(); abort=true; }
127 catch(exception& e) {
128 m->errorOut(e, "ClearcutCommand", "ClearcutCommand");
132 //**********************************************************************************************************************
134 void ClearcutCommand::help(){
136 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");
137 m->mothurOut("For more information about clearcut refer to http://bioinformatics.hungry.com/clearcut/ \n");
138 m->mothurOut("The clearcut executable must be in a folder called clearcut in the same folder as your mothur executable, similar to mothur's requirements for using blast. \n");
139 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");
140 m->mothurOut("The phylip parameter allows you to enter your phylip formatted distance matrix. \n");
141 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");
143 m->mothurOut("The version parameter prints out the version of clearcut you are using, default=F. \n");
144 m->mothurOut("The verbose parameter prints out more output from clearcut, default=F. \n");
145 m->mothurOut("The quiet parameter turns on silent operation mode, default=F. \n");
146 m->mothurOut("The seed parameter allows you to explicitly set the PRNG seed to a specific value. \n");
147 m->mothurOut("The norandom parameter allows you to attempt joins deterministically, default=F. \n");
148 m->mothurOut("The shuffle parameter allows you to randomly shuffle the distance matrix, default=F. \n");
149 m->mothurOut("The neighbor parameter allows you to use traditional Neighbor-Joining algorithm, default=F. \n");
151 m->mothurOut("The DNA parameter allows you to indicate your fasta file contains DNA sequences, default=F. \n");
152 m->mothurOut("The protein parameter allows you to indicate your fasta file contains protein sequences, default=F. \n");
154 m->mothurOut("The stdout parameter outputs your tree to STDOUT, default=F. \n");
155 m->mothurOut("The matrixout parameter allows you to specify a filename to output a distance matrix to. \n");
156 m->mothurOut("The ntrees parameter allows you to specify the number of output trees, default=1. \n");
157 m->mothurOut("The expblen parameter allows you to use exponential notation for branch lengths, default=F. \n");
158 m->mothurOut("The expdist parameter allows you to use exponential notation for distance outputs, default=F. \n");
160 m->mothurOut("The clearcut command should be in the following format: \n");
161 m->mothurOut("clearcut(phylip=yourDistanceFile) \n");
162 m->mothurOut("Example: clearcut(phylip=abrecovery.phylip.dist) \n");
165 catch(exception& e) {
166 m->errorOut(e, "ClearcutCommand", "help");
171 /**************************************************************************************/
172 int ClearcutCommand::execute() {
175 if (abort == true) { return 0; }
178 string outputName = outputDir + getRootName(getSimpleName(inputFile)) + "tre";
180 //get location of clearcut
181 GlobalData* globaldata = GlobalData::getInstance();
182 string path = globaldata->argv;
183 path = path.substr(0, (path.find_last_of('m')));
185 string clearcutCommand = "";
186 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
187 clearcutCommand = path + "clearcut/clearcut ";
189 clearcutCommand = path + "clearcut\\clearcut ";
192 //you gave us a distance matrix
193 if (phylipfile != "") { clearcutCommand += "--distance "; }
195 //you gave us a fastafile
196 if (fastafile != "") { clearcutCommand += "--alignment "; }
198 if (version) { clearcutCommand += "--version "; }
199 if (verbose) { clearcutCommand += "--verbose "; }
200 if (quiet) { clearcutCommand += "--quiet "; }
201 if (seed != "*") { clearcutCommand += "--seed=" + seed + " "; }
202 if (norandom) { clearcutCommand += "--norandom "; }
203 if (shuffle) { clearcutCommand += "--shuffle "; }
204 if (neighbor) { clearcutCommand += "--neighbor "; }
206 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
207 clearcutCommand += "--in=" + inputFile + " ";
209 clearcutCommand += "--in=\"" + inputFile + "\" ";
212 if (stdoutWanted) { clearcutCommand += "--stdout "; }
214 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
215 clearcutCommand += "--out=" + outputName + " "; }
217 clearcutCommand += "--out=\"" + outputName + "\" "; }
220 if (DNA) { clearcutCommand += "--DNA "; }
221 if (protein) { clearcutCommand += "--protein "; }
222 if (jukes) { clearcutCommand += "--jukes "; }
223 if (kimura) { clearcutCommand += "--kimura "; }
224 if (matrixout != "") { clearcutCommand += "--matrixout=" + matrixout + " "; }
225 if (ntrees != "1") { clearcutCommand += "--ntrees=" + ntrees + " "; }
226 if (expblen) { clearcutCommand += "--expblen "; }
227 if (expdist) { clearcutCommand += "--expdist "; }
230 system(clearcutCommand.c_str());
233 m->mothurOutEndLine();
234 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
235 m->mothurOut(outputName); m->mothurOutEndLine();
236 if (matrixout != "") { m->mothurOut(matrixout); m->mothurOutEndLine(); }
237 m->mothurOutEndLine();
242 catch(exception& e) {
243 m->errorOut(e, "ClearcutCommand", "execute");
247 /**************************************************************************************/