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