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