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