5 * Created by westcott on 5/11/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "clearcutcommand.h"
11 #include "globaldata.hpp"
13 /**************************************************************************************/
14 ClearcutCommand::ClearcutCommand(string option) {
18 //allow user to run help
19 if(option == "help") { help(); abort = true; }
22 //valid paramters for this command
23 string Array[] = {"fasta","phylip","version","verbose","quiet","seed","norandom","shuffle","neighbor","expblen",
24 "expdist","ntrees","matrixout","stdout","kimura","jukes","protein","DNA","stdin","outputdir","inputdir"};
25 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
27 OptionParser parser(option);
28 map<string,string> parameters = parser.getParameters();
30 ValidParameters validParameter;
31 map<string, string>::iterator it;
33 //check to make sure all parameters are valid for command
34 for (it = parameters.begin(); it != parameters.end(); it++) {
35 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
38 //if the user changes the input directory command factory will send this info to us in the output parameter
39 string inputDir = validParameter.validFile(parameters, "inputdir", false);
40 if (inputDir == "not found"){ inputDir = ""; }
43 it = parameters.find("fasta");
44 //user has given a template file
45 if(it != parameters.end()){
46 path = hasPath(it->second);
47 //if the user has not given a path then, add inputdir. else leave path alone.
48 if (path == "") { parameters["fasta"] = inputDir + it->second; }
51 it = parameters.find("phylip");
52 //user has given a template file
53 if(it != parameters.end()){
54 path = hasPath(it->second);
55 //if the user has not given a path then, add inputdir. else leave path alone.
56 if (path == "") { parameters["phylip"] = inputDir + it->second; }
60 //check for required parameters
61 fastafile = validParameter.validFile(parameters, "fasta", true);
62 if (fastafile == "not open") { fastafile = ""; abort = true; }
63 else if (fastafile == "not found") { fastafile = ""; }
64 else { inputFile = fastafile; }
66 phylipfile = validParameter.validFile(parameters, "phylip", true);
67 if (phylipfile == "not open") { phylipfile = ""; abort = true; }
68 else if (phylipfile == "not found") { phylipfile = ""; }
69 else { inputFile = phylipfile; }
71 if ((phylipfile == "") && (fastafile == "")) { m->mothurOut("You must provide either a phylip formatted distance matrix or an aligned fasta file."); m->mothurOutEndLine(); abort=true; }
72 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; }
75 //if the user changes the output directory command factory will send this info to us in the output parameter
76 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
79 temp = validParameter.validFile(parameters, "version", false); if (temp == "not found"){ temp = "F"; }
80 version = isTrue(temp);
82 temp = validParameter.validFile(parameters, "verbose", false); if (temp == "not found"){ temp = "F"; }
83 verbose = isTrue(temp);
85 temp = validParameter.validFile(parameters, "quiet", false); if (temp == "not found"){ temp = "F"; }
88 seed = validParameter.validFile(parameters, "seed", false); if (seed == "not found"){ seed = "*"; }
90 temp = validParameter.validFile(parameters, "norandom", false); if (temp == "not found"){ temp = "F"; }
91 norandom = isTrue(temp);
93 temp = validParameter.validFile(parameters, "shuffle", false); if (temp == "not found"){ temp = "F"; }
94 shuffle = isTrue(temp);
96 temp = validParameter.validFile(parameters, "neighbor", false); if (temp == "not found"){ temp = "F"; }
97 neighbor = isTrue(temp);
99 temp = validParameter.validFile(parameters, "stdin", false); if (temp == "not found"){ temp = "F"; }
100 stdin = isTrue(temp);
102 temp = validParameter.validFile(parameters, "DNA", false); if (temp == "not found"){ temp = "F"; }
105 temp = validParameter.validFile(parameters, "protein", false); if (temp == "not found"){ temp = "F"; }
106 protein = isTrue(temp);
108 temp = validParameter.validFile(parameters, "jukes", false); if (temp == "not found"){ temp = "F"; }
109 jukes = isTrue(temp);
111 temp = validParameter.validFile(parameters, "kimura", false); if (temp == "not found"){ temp = "F"; }
112 kimura = isTrue(temp);
114 temp = validParameter.validFile(parameters, "stdout", false); if (temp == "not found"){ temp = "F"; }
115 stdout = isTrue(temp);
117 matrixout = validParameter.validFile(parameters, "matrixout", false); if (matrixout == "not found"){ matrixout = ""; }
119 ntrees = validParameter.validFile(parameters, "ntrees", false); if (ntrees == "not found"){ ntrees = "1"; }
121 temp = validParameter.validFile(parameters, "expblen", false); if (temp == "not found"){ temp = "F"; }
122 expblen = isTrue(temp);
124 temp = validParameter.validFile(parameters, "expdist", false); if (temp == "not found"){ temp = "F"; }
125 expdist = isTrue(temp);
127 if ((fastafile != "") && ((!DNA) && (!protein))) { m->mothurOut("You must specify the type of sequences you are using: DNA or protein"); m->mothurOutEndLine(); abort=true; }
131 catch(exception& e) {
132 m->errorOut(e, "ClearcutCommand", "ClearcutCommand");
136 //**********************************************************************************************************************
138 void ClearcutCommand::help(){
140 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");
141 m->mothurOut("For more information about clearcut refer to http://bioinformatics.hungry.com/clearcut/ \n");
142 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, stdin. \n");
143 m->mothurOut("The phylip parameter allows you to enter your phylip formatted distance matrix. \n");
144 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");
146 m->mothurOut("The version parameter prints out the version of clearcut you are using, default=F. \n");
147 m->mothurOut("The verbose parameter prints out more output from clearcut, default=F. \n");
148 m->mothurOut("The quiet parameter turns on silent operation mode, default=F. \n");
149 m->mothurOut("The seed parameter allows you to explicitly set the PRNG seed to a specific value. \n");
150 m->mothurOut("The norandom parameter allows you to attempt joins deterministically, default=F. \n");
151 m->mothurOut("The shuffle parameter allows you to randomly shuffle the distance matrix, default=F. \n");
152 m->mothurOut("The neighbor parameter allows you to use traditional Neighbor-Joining algorithm, default=F. \n");
154 m->mothurOut("The stdin parameter reads input from STDIN, default=F. \n");
155 m->mothurOut("The DNA parameter allows you to indicate your fasta file contains DNA sequences, default=F. \n");
156 m->mothurOut("The protein parameter allows you to indicate your fasta file contains protein sequences, default=F. \n");
158 m->mothurOut("The stdout parameter outputs your tree to STDOUT, default=F. \n");
159 m->mothurOut("The matrixout parameter allows you to specify a filename to output a distance matrix to. \n");
160 m->mothurOut("The ntrees parameter allows you to specify the number of output trees, default=1. \n");
161 m->mothurOut("The expblen parameter allows you to use exponential notation for branch lengths, default=F. \n");
162 m->mothurOut("The expdist parameter allows you to use exponential notation for distance outputs, default=F. \n");
164 m->mothurOut("The clearcut command should be in the following format: \n");
165 m->mothurOut("clearcut(phylip=yourDistanceFile) \n");
166 m->mothurOut("Example: clearcut(phylip=abrecovery.phylip.dist) \n");
168 catch(exception& e) {
169 m->errorOut(e, "ClearcutCommand", "help");
174 /**************************************************************************************/
175 int ClearcutCommand::execute() {
178 if (abort == true) { return 0; }
181 string outputName = outputDir + getRootName(getSimpleName(inputFile)) + "tre";
183 //get location of clearcut
184 GlobalData* globaldata = GlobalData::getInstance();
185 string path = globaldata->argv;
186 path = path.substr(0, (path.find_last_of('m')));
188 string clearcutCommand = path + "clearcut/clearcut ";
190 //you gave us a distance matrix
191 if (phylipfile != "") { clearcutCommand += "--distance "; }
193 //you gave us a fastafile
194 if (fastafile != "") { clearcutCommand += "--alignment "; }
196 if (version) { clearcutCommand += "--version "; }
197 if (verbose) { clearcutCommand += "--verbose "; }
198 if (quiet) { clearcutCommand += "--quiet "; }
199 if (seed != "*") { clearcutCommand += "--seed=" + seed + " "; }
200 if (norandom) { clearcutCommand += "--norandom "; }
201 if (shuffle) { clearcutCommand += "--shuffle "; }
202 if (neighbor) { clearcutCommand += "--neighbor "; }
204 if (stdin) { clearcutCommand += "--stdin "; }
205 else { clearcutCommand += "--in=" + inputFile + " "; }
206 if (stdout) { clearcutCommand += "--stdout "; }
207 else { clearcutCommand += "--out=" + outputName + " "; }
209 if (DNA) { clearcutCommand += "--DNA "; }
210 if (protein) { clearcutCommand += "--protein "; }
211 if (jukes) { clearcutCommand += "--jukes "; }
212 if (kimura) { clearcutCommand += "--kimura "; }
213 if (matrixout != "") { clearcutCommand += "--matrixout=" + matrixout + " "; }
214 if (ntrees != "1") { clearcutCommand += "--ntrees=" + ntrees + " "; }
215 if (expblen) { clearcutCommand += "--expblen "; }
216 if (expdist) { clearcutCommand += "--expdist "; }
219 system(clearcutCommand.c_str());
222 m->mothurOutEndLine();
223 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
224 m->mothurOut(outputName); m->mothurOutEndLine();
225 m->mothurOutEndLine();
230 catch(exception& e) {
231 m->errorOut(e, "ClearcutCommand", "execute");
235 /**************************************************************************************/