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