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