]> git.donarmstrong.com Git - mothur.git/blob - clearcutcommand.cpp
added set.current and get.current commands and modified existing commands to update...
[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 vector<string> ClearcutCommand::getValidParameters(){   
20         try {
21                 string AlignArray[] =  {"fasta","phylip","version","verbose","quiet","seed","norandom","shuffle","neighbor","expblen",
22                                                                 "expdist","ntrees","matrixout","stdout","kimura","jukes","protein","DNA","outputdir","inputdir"};
23                 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
24                 return myArray;
25         }
26         catch(exception& e) {
27                 m->errorOut(e, "ClearcutCommand", "getValidParameters");
28                 exit(1);
29         }
30 }
31 //**********************************************************************************************************************
32 ClearcutCommand::ClearcutCommand(){     
33         try {
34                 abort = true; calledHelp = true; 
35                 vector<string> tempOutNames;
36                 outputTypes["tree"] = tempOutNames;
37                 outputTypes["matrixout"] = tempOutNames;
38         }
39         catch(exception& e) {
40                 m->errorOut(e, "ClearcutCommand", "ClearcutCommand");
41                 exit(1);
42         }
43 }//**********************************************************************************************************************
44 vector<string> ClearcutCommand::getRequiredParameters(){        
45         try {
46                 string Array[] =  {"fasta","phylip","or"};
47                 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
48                 return myArray;
49         }
50         catch(exception& e) {
51                 m->errorOut(e, "ClearcutCommand", "getRequiredParameters");
52                 exit(1);
53         }
54 }
55 //**********************************************************************************************************************
56 vector<string> ClearcutCommand::getRequiredFiles(){     
57         try {
58                 vector<string> myArray;
59                 return myArray;
60         }
61         catch(exception& e) {
62                 m->errorOut(e, "ClearcutCommand", "getRequiredFiles");
63                 exit(1);
64         }
65 }
66 /**************************************************************************************/
67 ClearcutCommand::ClearcutCommand(string option)  {      
68         try {
69                 abort = false; calledHelp = false;   
70                 
71                 //allow user to run help
72                 if(option == "help") { help(); abort = true; calledHelp = true; }
73                 
74                 else {
75                         //valid paramters for this command
76                         string Array[] =  {"fasta","phylip","version","verbose","quiet","seed","norandom","shuffle","neighbor","expblen",
77                                                                 "expdist","ntrees","matrixout","stdout","kimura","jukes","protein","DNA","outputdir","inputdir"};
78                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
79                         
80                         OptionParser parser(option);
81                         map<string,string> parameters = parser.getParameters();
82                         
83                         ValidParameters validParameter;
84                         map<string, string>::iterator it;
85                 
86                         //check to make sure all parameters are valid for command
87                         for (it = parameters.begin(); it != parameters.end(); it++) { 
88                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
89                         }
90                         
91                         //initialize outputTypes
92                         vector<string> tempOutNames;
93                         outputTypes["tree"] = tempOutNames;
94                         outputTypes["matrixout"] = tempOutNames;
95
96                         //if the user changes the input directory command factory will send this info to us in the output parameter 
97                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
98                         if (inputDir == "not found"){   inputDir = "";          }
99                         else {
100                                 string path;
101                                 it = parameters.find("fasta");
102                                 //user has given a template file
103                                 if(it != parameters.end()){ 
104                                         path = m->hasPath(it->second);
105                                         //if the user has not given a path then, add inputdir. else leave path alone.
106                                         if (path == "") {       parameters["fasta"] = inputDir + it->second;            }
107                                 }
108                                 
109                                 it = parameters.find("phylip");
110                                 //user has given a template file
111                                 if(it != parameters.end()){ 
112                                         path = m->hasPath(it->second);
113                                         //if the user has not given a path then, add inputdir. else leave path alone.
114                                         if (path == "") {       parameters["phylip"] = inputDir + it->second;           }
115                                 }
116                         }
117
118                         //check for required parameters
119                         fastafile = validParameter.validFile(parameters, "fasta", true);
120                         if (fastafile == "not open") { fastafile = ""; abort = true; }
121                         else if (fastafile == "not found") { fastafile = ""; }  
122                         else { inputFile = fastafile;  }
123                         
124                         phylipfile = validParameter.validFile(parameters, "phylip", true);
125                         if (phylipfile == "not open") { phylipfile = ""; abort = true; }
126                         else if (phylipfile == "not found") { phylipfile = ""; }
127                         else { inputFile = phylipfile;  }
128                                 
129                         if ((phylipfile == "") && (fastafile == "")) {  m->mothurOut("You must provide either a phylip formatted distance matrix or an aligned fasta file."); m->mothurOutEndLine(); abort=true; }
130                         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; }
131
132                         
133                         //if the user changes the output directory command factory will send this info to us in the output parameter 
134                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = m->hasPath(inputFile);      }
135                         
136                         string temp;
137                         temp = validParameter.validFile(parameters, "version", false);          if (temp == "not found"){       temp = "F";                     }
138                         version = m->isTrue(temp);
139                         
140                         temp = validParameter.validFile(parameters, "verbose", false);          if (temp == "not found"){       temp = "F";                     }
141                         verbose = m->isTrue(temp); 
142                         
143                         temp = validParameter.validFile(parameters, "quiet", false);            if (temp == "not found"){       temp = "F";                     }
144                         quiet = m->isTrue(temp); 
145                         
146                         seed = validParameter.validFile(parameters, "seed", false);                     if (seed == "not found"){       seed = "*";                     }
147                         
148                         temp = validParameter.validFile(parameters, "norandom", false);         if (temp == "not found"){       temp = "F";                     }
149                         norandom = m->isTrue(temp); 
150                         
151                         temp = validParameter.validFile(parameters, "shuffle", false);          if (temp == "not found"){       temp = "F";                     }
152                         shuffle = m->isTrue(temp); 
153                         
154                         temp = validParameter.validFile(parameters, "neighbor", false);         if (temp == "not found"){       temp = "T";                     }
155                         neighbor = m->isTrue(temp); 
156                         
157                         temp = validParameter.validFile(parameters, "DNA", false);                      if (temp == "not found"){       temp = "F";                     }
158                         DNA = m->isTrue(temp);
159                         
160                         temp = validParameter.validFile(parameters, "protein", false);          if (temp == "not found"){       temp = "F";                     }
161                         protein = m->isTrue(temp);
162                         
163                         temp = validParameter.validFile(parameters, "jukes", false);            if (temp == "not found"){       temp = "F";                     }
164                         jukes = m->isTrue(temp);
165                         
166                         temp = validParameter.validFile(parameters, "kimura", false);           if (temp == "not found"){       temp = "F";                     }
167                         kimura = m->isTrue(temp);
168                         
169                         temp = validParameter.validFile(parameters, "stdout", false);           if (temp == "not found"){       temp = "F";                     }
170                         stdoutWanted = m->isTrue(temp); 
171                         
172                         matrixout = validParameter.validFile(parameters, "matrixout", false);   if (matrixout == "not found"){  matrixout = "";         }
173                         
174                         ntrees = validParameter.validFile(parameters, "ntrees", false);         if (ntrees == "not found"){     ntrees = "1";           }
175                         
176                         temp = validParameter.validFile(parameters, "expblen", false);          if (temp == "not found"){       temp = "F";                     }
177                         expblen = m->isTrue(temp);
178                         
179                         temp = validParameter.validFile(parameters, "expdist", false);          if (temp == "not found"){       temp = "F";                     }
180                         expdist = m->isTrue(temp);
181                         
182                         if ((fastafile != "") && ((!DNA) && (!protein))) { m->mothurOut("You must specify the type of sequences you are using: DNA or protein"); m->mothurOutEndLine(); abort=true; }
183                 }
184
185         }
186         catch(exception& e) {
187                 m->errorOut(e, "ClearcutCommand", "ClearcutCommand");
188                 exit(1);
189         }
190 }
191 //**********************************************************************************************************************
192
193 void ClearcutCommand::help(){
194         try {
195                 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");
196                 m->mothurOut("For more information about clearcut refer to http://bioinformatics.hungry.com/clearcut/ \n");
197                 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");
198                 m->mothurOut("The phylip parameter allows you to enter your phylip formatted distance matrix. \n");
199                 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");
200                 
201                 m->mothurOut("The version parameter prints out the version of clearcut you are using, default=F. \n");
202                 m->mothurOut("The verbose parameter prints out more output from clearcut, default=F. \n");
203                 m->mothurOut("The quiet parameter turns on silent operation mode, default=F. \n");
204                 m->mothurOut("The seed parameter allows you to explicitly set the PRNG seed to a specific value. \n");
205                 m->mothurOut("The norandom parameter allows you to attempt joins deterministically, default=F. \n");
206                 m->mothurOut("The shuffle parameter allows you to randomly shuffle the distance matrix, default=F. \n");
207                 m->mothurOut("The neighbor parameter allows you to use traditional Neighbor-Joining algorithm, default=T. \n");
208                 
209                 m->mothurOut("The DNA parameter allows you to indicate your fasta file contains DNA sequences, default=F. \n");
210                 m->mothurOut("The protein parameter allows you to indicate your fasta file contains protein sequences, default=F. \n");
211                 
212                 m->mothurOut("The stdout parameter outputs your tree to STDOUT, default=F. \n");
213                 m->mothurOut("The matrixout parameter allows you to specify a filename to output a distance matrix to. \n");
214                 m->mothurOut("The ntrees parameter allows you to specify the number of output trees, default=1. \n");
215                 m->mothurOut("The expblen parameter allows you to use exponential notation for branch lengths, default=F. \n");
216                 m->mothurOut("The expdist parameter allows you to use exponential notation for distance outputs, default=F. \n");
217
218                 m->mothurOut("The clearcut command should be in the following format: \n");
219                 m->mothurOut("clearcut(phylip=yourDistanceFile) \n");
220                 m->mothurOut("Example: clearcut(phylip=abrecovery.phylip.dist) \n");    
221                 
222         }
223         catch(exception& e) {
224                 m->errorOut(e, "ClearcutCommand", "help");
225                 exit(1);
226         }
227 }
228
229 /**************************************************************************************/
230 int ClearcutCommand::execute() {        
231         try {
232                 
233                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
234                 
235                 //prepare filename
236                 string outputName = outputDir + m->getRootName(m->getSimpleName(inputFile)) + "tre";
237                 outputNames.push_back(outputName); outputTypes["tree"].push_back(outputName);
238                 
239                 vector<char*> cPara;
240                 
241                 char* tempClearcut = new char[8];  
242                 strcpy(tempClearcut, "clearcut"); 
243                 cPara.push_back(tempClearcut);
244                                 
245                 //you gave us a distance matrix
246                 if (phylipfile != "") {  char* temp = new char[10];  strcpy(temp, "--distance");  cPara.push_back(temp);        }
247                 
248                 //you gave us a fastafile
249                 if (fastafile != "") { char* temp = new char[11];  strcpy(temp, "--alignment");  cPara.push_back(temp);         }
250                 
251                 if (version)                    {  char* temp = new char[9];  strcpy(temp, "--version");  cPara.push_back(temp);        }
252                 if (verbose)                    {  char* temp = new char[9];  strcpy(temp, "--verbose");  cPara.push_back(temp);        }
253                 if (quiet)                              {  char* temp = new char[7];  strcpy(temp, "--quiet");  cPara.push_back(temp);  }
254                 if (seed != "*")                {  
255                         string tempSeed = "--seed=" + seed;
256                         char* temp = new char[tempSeed.length()];
257                         strcpy(temp, tempSeed.c_str());
258                         cPara.push_back(temp);
259                 }
260                 if (norandom)                   {  char* temp = new char[10];  strcpy(temp, "--norandom");  cPara.push_back(temp);      }
261                 if (shuffle)                    {  char* temp = new char[9];  strcpy(temp, "--shuffle");  cPara.push_back(temp);        }
262                 if (neighbor)                   {  char* temp = new char[10];  strcpy(temp, "--neighbor");  cPara.push_back(temp);      }
263                 
264                 string tempIn = "--in=" + inputFile;  
265                 char* tempI = new char[tempIn.length()];
266                 strcpy(tempI, tempIn.c_str());
267                 cPara.push_back(tempI);
268                 
269                 if (stdoutWanted)               {  char* temp = new char[8];  strcpy(temp, "--stdout");  cPara.push_back(temp); }
270                 else{  
271                         string tempOut = "--out=" + outputName;  
272                         
273                         char* temp = new char[tempOut.length()];
274                         strcpy(temp, tempOut.c_str());
275                         cPara.push_back(temp);
276                 }
277                         
278                 if (DNA)                                {  char* temp = new char[5];  strcpy(temp, "--DNA");  cPara.push_back(temp);            }
279                 if (protein)                    {  char* temp = new char[9];  strcpy(temp, "--protein");  cPara.push_back(temp);        }
280                 if (jukes)                              {  char* temp = new char[7];  strcpy(temp, "--jukes");  cPara.push_back(temp);          }
281                 if (kimura)                             { char* temp = new char[8];  strcpy(temp, "--kimura");  cPara.push_back(temp);          }
282                 if (matrixout != "")    {  
283                         string tempMatrix =  "--matrixout=" + outputDir + matrixout; 
284                         char* temp = new char[tempMatrix.length()];
285                         strcpy(temp, tempMatrix.c_str());
286                         cPara.push_back(temp);
287                         outputNames.push_back((outputDir + matrixout));
288                         outputTypes["matrixout"].push_back((outputDir + matrixout));
289                 }
290
291                 if (ntrees != "1")              {  
292                         string tempNtrees = "--ntrees=" + ntrees; 
293                         char* temp = new char[tempNtrees.length()];
294                         strcpy(temp, tempNtrees.c_str());
295                         cPara.push_back(temp);
296                 }
297
298                 if (expblen)                    { char* temp = new char[9];  strcpy(temp, "--expblen");  cPara.push_back(temp);         }
299                 if (expdist)                    { char* temp = new char[9];  strcpy(temp, "--expdist");  cPara.push_back(temp); }
300                 
301                 char** clearcutParameters;
302                 clearcutParameters = new char*[cPara.size()];
303                 for (int i = 0; i < cPara.size(); i++) {  clearcutParameters[i] = cPara[i];  }
304                 int numArgs = cPara.size();
305                 
306                 clearcut_main(numArgs, clearcutParameters); 
307                 
308                 //free memory
309                 for(int i = 0; i < cPara.size(); i++)  {  delete[] cPara[i];  }
310                 delete[] clearcutParameters; 
311                 
312                 if (!stdoutWanted) {    
313                         
314                         //set first tree file as new current treefile
315                         string currentTree = "";
316                         itTypes = outputTypes.find("tree");
317                         if (itTypes != outputTypes.end()) {
318                                 if ((itTypes->second).size() != 0) { currentTree = (itTypes->second)[0]; m->setTreeFile(currentTree); }
319                         }
320                         
321                         m->mothurOutEndLine();
322                         m->mothurOut("Output File Names: "); m->mothurOutEndLine();
323                         for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
324                         m->mothurOutEndLine();
325                 }
326
327                 return 0;
328         }
329         catch(exception& e) {
330                 m->errorOut(e, "ClearcutCommand", "execute");
331                 exit(1);
332         }
333 }
334 /**************************************************************************************/
335
336
337
338