]> git.donarmstrong.com Git - mothur.git/blob - validparameter.cpp
added bootstrap.shared command and fixed some bugs with heatmap
[mothur.git] / validparameter.cpp
1 /*
2  *  validparameter.cpp
3  *  Dotur
4  *
5  *  Created by Sarah Westcott on 1/5/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "validparameter.h"
11
12 /***********************************************************************/
13
14 ValidParameters::ValidParameters() {
15         try {
16                 initCommandParameters();                
17                 initParameterRanges();
18
19         }
20         catch(exception& e) {
21                 cout << "Standard Error: " << e.what() << " has occurred in the ValidParameters class Function ValidParameters. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
22                 exit(1);
23         }
24         catch(...) {
25                 cout << "An unknown error has occurred in the ValidParameters class function ValidParameters. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
26                 exit(1);
27         }       
28 }
29
30 /***********************************************************************/
31
32 ValidParameters::~ValidParameters() {}
33
34 /***********************************************************************/
35 bool ValidParameters::isValidParameter(string parameter, string command, string value) {
36         try {   
37                 bool valid = false;
38                 vector<string> cParams = commandParameters[command];
39                 int numParams = cParams.size(); 
40                 for(int i = 0; i < numParams; i++)
41                 {
42                         if(cParams.at(i).compare(parameter) == 0)
43                         {
44                                 valid = true;
45                                 i = numParams;
46                         }
47                 }
48                 if(!valid)
49                 {
50                         cout << "'" << parameter << "' is not a valid parameter for the " << command << " command.\n";
51                         cout << "The valid paramters for the " << command << " command are: ";
52                         for(int i = 0; i < numParams-1; i++)
53                                 cout << cParams.at(i) << ", ";
54                         cout << "and " << cParams.at(numParams-1) << ".\n";
55                         return false;
56                 }
57                 
58                 if(parameterRanges.count(parameter) != 1)
59                         return true;
60         
61                 int pVal;
62                 double piSentinel = 3.14159;
63                 vector<string> range = parameterRanges[parameter];
64                 
65                 vector<string> values;
66                 splitAtDash(value, values);
67                 
68                 for(int i = 0; i < values.size(); i++)
69                 {
70                         value = values.at(i);
71                         valid = convertTest(value, pVal);
72                 
73                         if(!valid)
74                                 return false;
75                         
76                         
77                         
78                         /********************************************************************************************************
79                                    Special Cases
80                         *********************************************************************************************************/
81                         
82                         if(parameter.compare("precision") == 0)
83                         {
84                                 double logNum = log10((double)pVal);
85                                 double diff = (double)((int)logNum - logNum);
86                                 if(diff != 0)
87                                 {
88                                         cout << "The precision parameter can only take powers of 10 as a value (e.g. 10,1000,1000, etc.)\n";
89                                         return false;
90                                 }
91                         }
92                         
93                         /************************************************************************************************************/
94                         
95                         
96                         
97                         double a,b,c,d,e;
98                         
99                         if(range.at(1).compare("NA") == 0)
100                                 a = piSentinel;
101                         else
102                                 a = atoi(range.at(1).c_str()); 
103                                 
104                         if(range.at(3).compare("NA") == 0)
105                                 b = piSentinel;
106                         else
107                                 b = atoi(range.at(3).c_str()); 
108                                                 
109                         if(range.at(4).compare("between") == 0)
110                                 c = 0;
111                         else if(range.at(4).compare("only") == 0)
112                                 c = 1;
113                         else
114                         {
115                                 cout << "The range can only be 'between' or 'only' the bounding numbers.\n";
116                                 return false;
117                         }
118                         
119                         if(range.at(0).compare(">") == 0)
120                                 d = 0;
121                         else if(range.at(0).compare(">=") == 0 || range[3].compare("=>") == 0)
122                                 d = 1;
123                         else
124                         {
125                                 cout << "The parameter value can only be '>', '>=', or '=>' the lower bounding number.\n";
126                                 return false;
127                         }
128                         
129                         if(range.at(2).compare("<") == 0)
130                                 e = 0;
131                         else if(range.at(2).compare("<=") == 0 || range[4].compare("=<") == 0)
132                                 e = 1;
133                         else
134                         {
135                                 cout << "The parameter value can only be '<', '<=', or '=<' the upper bounding number.\n";
136                                 return false;
137                         }
138                         
139                         bool a0 = pVal > a;
140                         bool a1 = pVal >= a;
141                         bool b0 = pVal < b;
142                         bool b1 = pVal <= b;
143                         
144                         if(c != 1)
145                         {
146                                 if(a != piSentinel && b == piSentinel)
147                                 {
148                                         if(d == 0)
149                                                 valid = a0;
150                                         else
151                                                 valid = a1;
152                                 }
153                                 else if(a == piSentinel && b != piSentinel)
154                                 {
155                                         if(e == 0)
156                                                 valid = b0;
157                                         else
158                                                 valid = b1;
159                                 }
160                                 else
161                                 {
162                                         if(d == 0 && e == 0)
163                                                 valid = (a0 && b0);
164                                         else if(d == 0 && e == 1)
165                                                 valid = (a0 && b1);
166                                         else if(d == 1 && e == 0)
167                                                 valid = (a1 && b0);
168                                         else
169                                                 valid = (a1 && b1);
170                                 }
171                         }
172                         else
173                         {
174                                 if(a != piSentinel && b == piSentinel)
175                                         valid = (pVal == a);
176                                 else if(a == piSentinel && b != piSentinel)
177                                         valid = (pVal == b);
178                                 else
179                                         valid = (pVal == a || pVal == b);
180                         }
181                         
182                         
183                         if(!valid)
184                         {
185                                 cout << "The '" << parameter << "' parameter needs to be ";
186                                 if(c == 1)
187                                         cout << "either '" << a << "' or '" << b << "'.\n";
188                                 else
189                                 {
190                                         if(a != piSentinel)
191                                         {
192                                                 cout << ">";
193                                                 if(d != 0)
194                                                         cout << "=";
195                                                 cout << " '" << a << "'";
196                                         }
197                                         if(b == piSentinel)
198                                                 cout << ".\n";
199                                         else if(a != piSentinel)
200                                                 cout << " and ";
201                                         if(b != piSentinel)
202                                         {
203                                                 cout << "<";
204                                                 if(e != 0)
205                                                         cout << "=";
206                                                 cout << " '" << b << ".\n";
207                                         }
208                                 }
209                                 return false;
210                         }
211                 }
212                 return true;
213         }
214         catch(exception& e) {
215                 cout << "Standard Error: " << e.what() << " has occurred in the ValidParameters class Function isValidParameter. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
216                 exit(1);
217         }
218         catch(...) {
219                 cout << "An unknown error has occurred in the ValidParameters class function isValidParameter. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
220                 exit(1);
221         }
222 }
223
224 /***********************************************************************/
225
226 /***********************************************************************/
227 void ValidParameters::initCommandParameters() {
228         try {   
229                 //{"parameter1","parameter2",...,"last parameter"};
230                 
231                 string readdistArray[] = {"phylip","column", "name","cutoff","precision", "group"};
232
233                 commandParameters["read.dist"] = addParameters(readdistArray, sizeof(readdistArray)/sizeof(string));
234
235                 string readotuArray[] =  {"list","order","shared", "line", "label","group","sabund", "rabund"};
236                 commandParameters["read.otu"] = addParameters(readotuArray, sizeof(readotuArray)/sizeof(string));
237                 
238                 string readtreeArray[] = {"tree","group"};
239                 commandParameters["read.tree"] = addParameters(readtreeArray, sizeof(readtreeArray)/sizeof(string));
240                 
241                 string clusterArray[] =  {"cutoff","precision","method"};
242                 commandParameters["cluster"] = addParameters(clusterArray, sizeof(clusterArray)/sizeof(string));
243                 
244                 string deconvoluteArray[] =  {"fasta"};
245                 commandParameters["deconvolute"] = addParameters(deconvoluteArray, sizeof(deconvoluteArray)/sizeof(string));
246                 
247                 string collectsingleArray[] =  {"freq","line","label","calc","abund"};
248                 commandParameters["collect.single"] = addParameters(collectsingleArray, sizeof(collectsingleArray)/sizeof(string));
249
250                 string collectsharedArray[] =  {"jumble","freq","line","label","calc","groups"};
251                 commandParameters["collect.shared"] = addParameters(collectsharedArray, sizeof(collectsharedArray)/sizeof(string));
252
253                 string getgroupArray[] =  {};
254                 commandParameters["get.group"]   = addParameters(getgroupArray, sizeof(getgroupArray)/sizeof(string));
255                 
256                 string getlabelArray[] =  {};
257                 commandParameters["get.label"]  = addParameters(getlabelArray, sizeof(getlabelArray)/sizeof(string));
258
259                 string getlineArray[] =  {};
260                 commandParameters["get.line"] = addParameters(getlineArray, sizeof(getlineArray)/sizeof(string));
261
262                 string rarefactionsingleArray[] =  {"iters","freq","line","label","calc","abund"};
263                 commandParameters["rarefaction.single"] = addParameters(rarefactionsingleArray, sizeof(rarefactionsingleArray)/sizeof(string));
264
265                 string rarefactionsharedArray[] =  {"iters","jumble","line","label","calc","groups"};
266                 commandParameters["rarefaction.shared"] = addParameters(rarefactionsharedArray, sizeof(rarefactionsharedArray)/sizeof(string));
267                 
268                 string libshuffArray[] =  {"iters","groups","step","form","cutoff"};
269                 commandParameters["libshuff"] = addParameters(libshuffArray, sizeof(libshuffArray)/sizeof(string));
270                 
271                 string summarysingleArray[] =  {"line","label","calc","abund"};
272                 commandParameters["summary.single"] = addParameters(summarysingleArray, sizeof(summarysingleArray)/sizeof(string));
273
274                 string summarysharedArray[] =  {"jumble","line","label","calc","groups"};
275                 commandParameters["summary.shared"] = addParameters(summarysharedArray, sizeof(summarysharedArray)/sizeof(string));
276
277                 string parsimonyArray[] =  {"random","groups","iters"};
278                 commandParameters["parsimony"] = addParameters(parsimonyArray, sizeof(parsimonyArray)/sizeof(string));
279
280                 string unifracWeightedArray[] =  {"groups","iters"};
281                 commandParameters["unifrac.weighted"] = addParameters(unifracWeightedArray, sizeof(unifracWeightedArray)/sizeof(string));
282
283                 string unifracUnweightedArray[] =  {"groups","iters"};
284                 commandParameters["unifrac.unweighted"] = addParameters(unifracUnweightedArray, sizeof(unifracUnweightedArray)/sizeof(string));
285
286                 string heatmapArray[] =  {"groups","line","label","sorted","scale"};
287                 commandParameters["heatmap"] = addParameters(heatmapArray, sizeof(heatmapArray)/sizeof(string));
288
289                 string vennArray[] =  {"groups","line","label","calc"};
290                 commandParameters["venn"] = addParameters(vennArray, sizeof(vennArray)/sizeof(string));
291                 
292                 string binseqsArray[] =  {"fasta","line","label","name"};
293                 commandParameters["bin.seqs"] = addParameters(binseqsArray, sizeof(binseqsArray)/sizeof(string));
294                 
295                 string getOTURepArray[] =  {"fasta","list","line","label","name"};
296                 commandParameters["get.oturep"] = addParameters(getOTURepArray, sizeof(getOTURepArray)/sizeof(string));
297                 
298                 string treeGroupsArray[] =  {"line","label","calc","groups"};
299                 commandParameters["tree.shared"] = addParameters(treeGroupsArray, sizeof(treeGroupsArray)/sizeof(string));
300                 
301                 string bootstrapArray[] =  {"line","label","calc","groups","iters"};
302                 commandParameters["bootstrap.shared"] = addParameters(bootstrapArray, sizeof(bootstrapArray)/sizeof(string));
303                 
304                 string quitArray[] = {};
305                 commandParameters["quit"] = addParameters(quitArray, sizeof(quitArray)/sizeof(string));
306
307         }
308         catch(exception& e) {
309                 cout << "Standard Error: " << e.what() << " has occurred in the ValidParameters class Function isValidParameter. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
310                 exit(1);
311         }
312         catch(...) {
313                 cout << "An unknown error has occurred in the ValidParameters class function isValidParameter. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
314                 exit(1);
315         }
316 }
317
318 /***********************************************************************/
319
320 /***********************************************************************/
321 void ValidParameters::initParameterRanges() {
322         try {   
323                 int rangeSize = 5;
324
325                 /**************************************************************************************************************
326                         {">=" or "=>" or ">" if the value should be greater than or equal to or just greater than the lower bound,
327                     A number representing the lower bound ("NA" if there is no lower bound), 
328                    "<=" or "=<" or "<" if the value shoud be less than or equal to or just less than the upper bound,
329                     A number representing the upper bound ("NA" if there is no lower bound),
330                    "between" if between lower and upper bounds or "only" if exactly one of the bounds};
331                    
332                    # = parameter
333                    # (>, >=) lower bound, # (<, <=) upperbound, # should be (between, only) lower and upper bounds.
334                    ***********************************************************************************************************/
335                 
336                 string precisionArray[] = {">=","10", "<","NA", "between"};
337                 parameterRanges["precision"] = addParameters(precisionArray, rangeSize);
338                 
339                 string itersArray[] = {">=","10", "<","NA", "between"};
340                 parameterRanges["iters"] = addParameters(itersArray, rangeSize);
341
342                 string jumbleArray[] = {">","0", "<","1", "only"};
343                 parameterRanges["jumble"] = addParameters(jumbleArray, rangeSize);
344
345                 string freqArray[] = {">","1", "<","NA", "between"};
346                 parameterRanges["freq"] = addParameters(freqArray, rangeSize);
347
348                 //string lineArray[] = {">=","1", "<","NA", "between"};
349                 //parameterRanges["line"] = addParameters(lineArray, rangeSize);
350
351                 string abundArray[] = {">=","5", "<","NA", "between"};
352                 parameterRanges["abund"] = addParameters(abundArray, rangeSize);
353         }
354         catch(exception& e) {
355                 cout << "Standard Error: " << e.what() << " has occurred in the ValidParameters class Function isValidParameter. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
356                 exit(1);
357         }
358         catch(...) {
359                 cout << "An unknown error has occurred in the ValidParameters class function isValidParameter. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
360                 exit(1);
361         }
362 }
363
364 /***********************************************************************/
365
366 /***********************************************************************/
367 vector<string> ValidParameters::addParameters(string parameters[], int size) {
368         try {   
369                 vector<string> pVector (parameters, parameters+size); 
370                 return pVector;
371         }
372         catch(exception& e) {
373                 cout << "Standard Error: " << e.what() << " has occurred in the ValidParameters class Function isValidParameter. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
374                 exit(1);
375         }
376         catch(...) {
377                 cout << "An unknown error has occurred in the ValidParameters class function isValidParameter. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
378                 exit(1);
379         }
380 }
381