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