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