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