]> git.donarmstrong.com Git - mothur.git/blob - clearcutcommand.cpp
Merge remote-tracking branch 'mothur/master'
[mothur.git] / clearcutcommand.cpp
1 /*
2  *  clearcutcommand.cpp
3  *  Mothur
4  *
5  *  Created by westcott on 5/11/10.
6  *  Copyright 2010 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "clearcutcommand.h"
11 #ifdef __cplusplus
12 extern "C" {
13 #endif
14 #include "clearcut.h"
15 #ifdef __cplusplus
16 }
17 #endif
18
19 //**********************************************************************************************************************
20 vector<string> ClearcutCommand::setParameters(){        
21         try {
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);
42                 
43                 vector<string> myArray;
44                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
45                 return myArray;
46         }
47         catch(exception& e) {
48                 m->errorOut(e, "ClearcutCommand", "setParameters");
49                 exit(1);
50         }
51 }
52 //**********************************************************************************************************************
53 string ClearcutCommand::getHelpString(){        
54         try {
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";
61                 
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";
69                 
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";
72                 
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";
78                 
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";    
82                 return helpString;
83         }
84         catch(exception& e) {
85                 m->errorOut(e, "ClearcutCommand", "getHelpString");
86                 exit(1);
87         }
88 }
89 //**********************************************************************************************************************
90 string ClearcutCommand::getOutputFileNameTag(string type, string inputName=""){ 
91         try {
92         string outputFileName = "";
93                 map<string, vector<string> >::iterator it;
94         
95         //is this a type this command creates
96         it = outputTypes.find(type);
97         if (it == outputTypes.end()) {  m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); }
98         else {
99             if (type == "tree") {  outputFileName =  "tre"; }
100             else if (type == "matrixout") {  outputFileName =  ""; }
101             else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true;  }
102         }
103         return outputFileName;
104         }
105         catch(exception& e) {
106                 m->errorOut(e, "ClearcutCommand", "getOutputFileNameTag");
107                 exit(1);
108         }
109 }
110 //**********************************************************************************************************************
111 ClearcutCommand::ClearcutCommand(){     
112         try {
113                 abort = true; calledHelp = true; 
114                 setParameters();
115                 vector<string> tempOutNames;
116                 outputTypes["tree"] = tempOutNames;
117                 outputTypes["matrixout"] = tempOutNames;
118         }
119         catch(exception& e) {
120                 m->errorOut(e, "ClearcutCommand", "ClearcutCommand");
121                 exit(1);
122         }
123 }
124 /**************************************************************************************/
125 ClearcutCommand::ClearcutCommand(string option)  {      
126         try {
127                 abort = false; calledHelp = false;   
128                 
129                 //allow user to run help
130                 if(option == "help") { help(); abort = true; calledHelp = true; }
131                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
132                 
133                 else {
134                         vector<string> myArray = setParameters();
135                         
136                         OptionParser parser(option);
137                         map<string,string> parameters = parser.getParameters();
138                         
139                         ValidParameters validParameter;
140                         map<string, string>::iterator it;
141                 
142                         //check to make sure all parameters are valid for command
143                         for (it = parameters.begin(); it != parameters.end(); it++) { 
144                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
145                         }
146                         
147                         //initialize outputTypes
148                         vector<string> tempOutNames;
149                         outputTypes["tree"] = tempOutNames;
150                         outputTypes["matrixout"] = tempOutNames;
151
152                         //if the user changes the input directory command factory will send this info to us in the output parameter 
153                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
154                         if (inputDir == "not found"){   inputDir = "";          }
155                         else {
156                                 string path;
157                                 it = parameters.find("fasta");
158                                 //user has given a template file
159                                 if(it != parameters.end()){ 
160                                         path = m->hasPath(it->second);
161                                         //if the user has not given a path then, add inputdir. else leave path alone.
162                                         if (path == "") {       parameters["fasta"] = inputDir + it->second;            }
163                                 }
164                                 
165                                 it = parameters.find("phylip");
166                                 //user has given a template file
167                                 if(it != parameters.end()){ 
168                                         path = m->hasPath(it->second);
169                                         //if the user has not given a path then, add inputdir. else leave path alone.
170                                         if (path == "") {       parameters["phylip"] = inputDir + it->second;           }
171                                 }
172                         }
173
174                         //check for required parameters
175                         fastafile = validParameter.validFile(parameters, "fasta", true);
176                         if (fastafile == "not open") { fastafile = ""; abort = true; }
177                         else if (fastafile == "not found") { fastafile = ""; }  
178                         else { inputFile = fastafile;  m->setFastaFile(fastafile); }
179                         
180                         phylipfile = validParameter.validFile(parameters, "phylip", true);
181                         if (phylipfile == "not open") { phylipfile = ""; abort = true; }
182                         else if (phylipfile == "not found") { phylipfile = ""; }
183                         else { inputFile = phylipfile;  m->setPhylipFile(phylipfile); }
184                                 
185                         if ((phylipfile == "") && (fastafile == "")) {  
186                                 //is there are current file available for either of these?
187                                 //give priority to phylip, then fasta
188                                 phylipfile = m->getPhylipFile(); 
189                                 if (phylipfile != "") {  inputFile = phylipfile; m->mothurOut("Using " + phylipfile + " as input file for the phylip parameter."); m->mothurOutEndLine(); }
190                                 else { 
191                                         fastafile = m->getFastaFile(); 
192                                         if (fastafile != "") { inputFile = fastafile;  m->mothurOut("Using " + fastafile + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
193                                         else { 
194                                                 m->mothurOut("No valid current files. You must provide a phylip or fasta file before you can use the clearcut command."); m->mothurOutEndLine(); 
195                                                 abort = true;
196                                         }
197                                 }
198                         }
199                         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; }
200
201                         
202                         //if the user changes the output directory command factory will send this info to us in the output parameter 
203                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = m->hasPath(inputFile);      }
204                         
205                         string temp;
206                         temp = validParameter.validFile(parameters, "version", false);          if (temp == "not found"){       temp = "F";                     }
207                         version = m->isTrue(temp);
208                         
209                         temp = validParameter.validFile(parameters, "verbose", false);          if (temp == "not found"){       temp = "F";                     }
210                         verbose = m->isTrue(temp); 
211                         
212                         temp = validParameter.validFile(parameters, "quiet", false);            if (temp == "not found"){       temp = "F";                     }
213                         quiet = m->isTrue(temp); 
214                         
215                         seed = validParameter.validFile(parameters, "seed", false);                     if (seed == "not found"){       seed = "*";                     }
216                         
217                         temp = validParameter.validFile(parameters, "norandom", false);         if (temp == "not found"){       temp = "F";                     }
218                         norandom = m->isTrue(temp); 
219                         
220                         temp = validParameter.validFile(parameters, "shuffle", false);          if (temp == "not found"){       temp = "F";                     }
221                         shuffle = m->isTrue(temp); 
222                         
223                         temp = validParameter.validFile(parameters, "neighbor", false);         if (temp == "not found"){       temp = "T";                     }
224                         neighbor = m->isTrue(temp); 
225                         
226                         temp = validParameter.validFile(parameters, "DNA", false);                      if (temp == "not found"){       temp = "F";                     }
227                         DNA = m->isTrue(temp);
228                         
229                         temp = validParameter.validFile(parameters, "protein", false);          if (temp == "not found"){       temp = "F";                     }
230                         protein = m->isTrue(temp);
231                         
232                         temp = validParameter.validFile(parameters, "jukes", false);            if (temp == "not found"){       temp = "F";                     }
233                         jukes = m->isTrue(temp);
234                         
235                         temp = validParameter.validFile(parameters, "kimura", false);           if (temp == "not found"){       temp = "F";                     }
236                         kimura = m->isTrue(temp);
237                         
238                         temp = validParameter.validFile(parameters, "stdout", false);           if (temp == "not found"){       temp = "F";                     }
239                         stdoutWanted = m->isTrue(temp); 
240                         
241                         matrixout = validParameter.validFile(parameters, "matrixout", false);   if (matrixout == "not found"){  matrixout = "";         }
242                         
243                         ntrees = validParameter.validFile(parameters, "ntrees", false);         if (ntrees == "not found"){     ntrees = "1";           }
244                         
245                         temp = validParameter.validFile(parameters, "expblen", false);          if (temp == "not found"){       temp = "F";                     }
246                         expblen = m->isTrue(temp);
247                         
248                         temp = validParameter.validFile(parameters, "expdist", false);          if (temp == "not found"){       temp = "F";                     }
249                         expdist = m->isTrue(temp);
250                         
251                         if ((fastafile != "") && ((!DNA) && (!protein))) { m->mothurOut("You must specify the type of sequences you are using: DNA or protein"); m->mothurOutEndLine(); abort=true; }
252                 }
253
254         }
255         catch(exception& e) {
256                 m->errorOut(e, "ClearcutCommand", "ClearcutCommand");
257                 exit(1);
258         }
259 }
260 /**************************************************************************************/
261 int ClearcutCommand::execute() {        
262         try {
263                 
264                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
265                 
266                 //prepare filename
267                 string outputName = outputDir + m->getRootName(m->getSimpleName(inputFile)) + getOutputFileNameTag("tree");
268                 outputNames.push_back(outputName); outputTypes["tree"].push_back(outputName);
269                 
270                 vector<char*> cPara;
271                 
272                 char* tempClearcut = new char[8];  
273                 strcpy(tempClearcut, "clearcut"); 
274                 cPara.push_back(tempClearcut);
275                                 
276                 //you gave us a distance matrix
277                 if (phylipfile != "") {  char* temp = new char[10];  strcpy(temp, "--distance");  cPara.push_back(temp);        }
278                 
279                 //you gave us a fastafile
280                 if (fastafile != "") { char* temp = new char[11];  strcpy(temp, "--alignment");  cPara.push_back(temp);         }
281                 
282                 if (version)                    {  char* temp = new char[9];  strcpy(temp, "--version");  cPara.push_back(temp);        }
283                 if (verbose)                    {  char* temp = new char[9];  strcpy(temp, "--verbose");  cPara.push_back(temp);        }
284                 if (quiet)                              {  char* temp = new char[7];  strcpy(temp, "--quiet");  cPara.push_back(temp);  }
285                 if (seed != "*")                {  
286                         string tempSeed = "--seed=" + seed;
287                         char* temp = new char[tempSeed.length()];
288                         strcpy(temp, tempSeed.c_str());
289                         cPara.push_back(temp);
290                 }
291                 if (norandom)                   {  char* temp = new char[10];  strcpy(temp, "--norandom");  cPara.push_back(temp);      }
292                 if (shuffle)                    {  char* temp = new char[9];  strcpy(temp, "--shuffle");  cPara.push_back(temp);        }
293                 if (neighbor)                   {  char* temp = new char[10];  strcpy(temp, "--neighbor");  cPara.push_back(temp);      }
294                 
295                 string tempIn = "--in=" + inputFile;  
296                 char* tempI = new char[tempIn.length()];
297                 strcpy(tempI, tempIn.c_str());
298                 cPara.push_back(tempI);
299                 
300                 if (stdoutWanted)               {  char* temp = new char[8];  strcpy(temp, "--stdout");  cPara.push_back(temp); }
301                 else{  
302                         string tempOut = "--out=" + outputName;  
303                         
304                         char* temp = new char[tempOut.length()];
305                         strcpy(temp, tempOut.c_str());
306                         cPara.push_back(temp);
307                 }
308                         
309                 if (DNA)                                {  char* temp = new char[5];  strcpy(temp, "--DNA");  cPara.push_back(temp);            }
310                 if (protein)                    {  char* temp = new char[9];  strcpy(temp, "--protein");  cPara.push_back(temp);        }
311                 if (jukes)                              {  char* temp = new char[7];  strcpy(temp, "--jukes");  cPara.push_back(temp);          }
312                 if (kimura)                             { char* temp = new char[8];  strcpy(temp, "--kimura");  cPara.push_back(temp);          }
313                 if (matrixout != "")    {  
314                         string tempMatrix =  "--matrixout=" + outputDir + matrixout; 
315                         char* temp = new char[tempMatrix.length()];
316                         strcpy(temp, tempMatrix.c_str());
317                         cPara.push_back(temp);
318                         outputNames.push_back((outputDir + matrixout));
319                         outputTypes["matrixout"].push_back((outputDir + matrixout));
320                 }
321
322                 if (ntrees != "1")              {  
323                         string tempNtrees = "--ntrees=" + ntrees; 
324                         char* temp = new char[tempNtrees.length()];
325                         strcpy(temp, tempNtrees.c_str());
326                         cPara.push_back(temp);
327                 }
328
329                 if (expblen)                    { char* temp = new char[9];  strcpy(temp, "--expblen");  cPara.push_back(temp);         }
330                 if (expdist)                    { char* temp = new char[9];  strcpy(temp, "--expdist");  cPara.push_back(temp); }
331                 
332                 char** clearcutParameters;
333                 clearcutParameters = new char*[cPara.size()];
334                 for (int i = 0; i < cPara.size(); i++) {  clearcutParameters[i] = cPara[i];  }
335                 int numArgs = cPara.size();
336                 
337                 clearcut_main(numArgs, clearcutParameters); 
338                 
339                 //free memory
340                 for(int i = 0; i < cPara.size(); i++)  {  delete[] cPara[i];  }
341                 delete[] clearcutParameters; 
342                 
343                 if (!stdoutWanted) {    
344                         
345                         //set first tree file as new current treefile
346                         string currentTree = "";
347                         itTypes = outputTypes.find("tree");
348                         if (itTypes != outputTypes.end()) {
349                                 if ((itTypes->second).size() != 0) { currentTree = (itTypes->second)[0]; m->setTreeFile(currentTree); }
350                         }
351                         
352                         m->mothurOutEndLine();
353                         m->mothurOut("Output File Names: "); m->mothurOutEndLine();
354                         for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
355                         m->mothurOutEndLine();
356                 }
357
358                 return 0;
359         }
360         catch(exception& e) {
361                 m->errorOut(e, "ClearcutCommand", "execute");
362                 exit(1);
363         }
364 }
365 /**************************************************************************************/
366
367
368
369