]> git.donarmstrong.com Git - mothur.git/blob - validparameter.cpp
broke up globaldata and moved error checking and help into commands
[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                 initParameterRanges();
17         }
18         catch(exception& e) {
19                 cout << "Standard Error: " << e.what() << " has occurred in the ValidParameters class Function ValidParameters. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
20                 exit(1);
21         }
22         catch(...) {
23                 cout << "An unknown error has occurred in the ValidParameters class function ValidParameters. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
24                 exit(1);
25         }       
26 }
27
28 /***********************************************************************/
29
30 ValidParameters::~ValidParameters() {}
31
32 /***********************************************************************/
33 bool ValidParameters::isValidParameter(string parameter, vector<string> cParams, string value) {
34         try {   
35                 bool valid = false;
36                 //vector<string> cParams = commandParameters[command];
37                 int numParams = cParams.size(); 
38                 for(int i = 0; i < numParams; i++) {
39                         if(cParams.at(i).compare(parameter) == 0) {
40                                 valid = true;
41                                 i = numParams;
42                         }
43                 }
44                 if(!valid) {
45                         cout << "'" << parameter << "' is not a valid parameter." << endl;
46                         cout << "The valid parameters are: ";
47                         for(int i = 0; i < numParams-1; i++)
48                                 cout << cParams.at(i) << ", ";
49                         cout << "and " << cParams.at(numParams-1) << ".\n";
50                         return false;
51                 }
52                 
53                 if(parameterRanges.count(parameter) != 1)
54                         return true;
55         
56                 int pVal;
57                 double piSentinel = 3.14159;
58                 vector<string> range = parameterRanges[parameter];
59                 
60                 vector<string> values;
61                 splitAtDash(value, values);
62                 
63                 for(int i = 0; i < values.size(); i++) {
64                         value = values.at(i);
65                         valid = convertTest(value, pVal);
66                 
67                         if(!valid)
68                                 return false;
69                         
70                         
71                         
72                         /********************************************************************************************************
73                                    Special Cases
74                         *********************************************************************************************************/
75                         
76                         if(parameter.compare("precision") == 0) {
77                                 double logNum = log10((double)pVal);
78                                 double diff = (double)((int)logNum - logNum);
79                                 if(diff != 0) {
80                                         cout << "The precision parameter can only take powers of 10 as a value (e.g. 10,1000,1000, etc.)\n";
81                                         return false;
82                                 }
83                         }
84                         
85                         /************************************************************************************************************/
86                         
87                         
88                         
89                         double a,b,c,d,e;
90                         
91                         if(range.at(1).compare("NA") == 0)
92                                 a = piSentinel;
93                         else
94                                 a = atoi(range.at(1).c_str()); 
95                                 
96                         if(range.at(3).compare("NA") == 0)
97                                 b = piSentinel;
98                         else
99                                 b = atoi(range.at(3).c_str()); 
100                                                 
101                         if(range.at(4).compare("between") == 0)
102                                 c = 0;
103                         else if(range.at(4).compare("only") == 0)
104                                 c = 1;
105                         else {
106                                 cout << "The range can only be 'between' or 'only' the bounding numbers.\n";
107                                 return false;
108                         }
109                         
110                         if(range.at(0).compare(">") == 0)
111                                 d = 0;
112                         else if(range.at(0).compare(">=") == 0 || range[3].compare("=>") == 0)
113                                 d = 1;
114                         else {
115                                 cout << "The parameter value can only be '>', '>=', or '=>' the lower bounding number.\n";
116                                 return false;
117                         }
118                         
119                         if(range.at(2).compare("<") == 0)
120                                 e = 0;
121                         else if(range.at(2).compare("<=") == 0 || range[4].compare("=<") == 0)
122                                 e = 1;
123                         else {
124                                 cout << "The parameter value can only be '<', '<=', or '=<' the upper bounding number.\n";
125                                 return false;
126                         }
127                         
128                         bool a0 = pVal > a;
129                         bool a1 = pVal >= a;
130                         bool b0 = pVal < b;
131                         bool b1 = pVal <= b;
132                         
133                         if(c != 1) {
134                                 if(a != piSentinel && b == piSentinel) {
135                                         if(d == 0)
136                                                 valid = a0;
137                                         else
138                                                 valid = a1;
139                                 }
140                                 else if(a == piSentinel && b != piSentinel) {
141                                         if(e == 0)
142                                                 valid = b0;
143                                         else
144                                                 valid = b1;
145                                 }
146                                 else {
147                                         if(d == 0 && e == 0)
148                                                 valid = (a0 && b0);
149                                         else if(d == 0 && e == 1)
150                                                 valid = (a0 && b1);
151                                         else if(d == 1 && e == 0)
152                                                 valid = (a1 && b0);
153                                         else
154                                                 valid = (a1 && b1);
155                                 }
156                         }
157                         else {
158                                 if(a != piSentinel && b == piSentinel)
159                                         valid = (pVal == a);
160                                 else if(a == piSentinel && b != piSentinel)
161                                         valid = (pVal == b);
162                                 else
163                                         valid = (pVal == a || pVal == b);
164                         }
165                         
166                         
167                         if(!valid) {
168                                 cout << "The '" << parameter << "' parameter needs to be ";
169                                 if(c == 1)
170                                         cout << "either '" << a << "' or '" << b << "'.\n";
171                                 else {
172                                         if(a != piSentinel) {
173                                                 cout << ">";
174                                                 if(d != 0)
175                                                         cout << "=";
176                                                 cout << " '" << a << "'";
177                                         }
178                                         if(b == piSentinel)
179                                                 cout << "'.\n";
180                                         else if(a != piSentinel)
181                                                 cout << " and ";
182                                         if(b != piSentinel) {
183                                                 cout << "<";
184                                                 if(e != 0)
185                                                         cout << "=";
186                                                 cout << " '" << b << "'.\n";
187                                         }
188                                 }
189                                 return false;
190                         }
191                 }
192                 return true;
193         }
194         catch(exception& e) {
195                 cout << "Standard Error: " << e.what() << " has occurred in the ValidParameters class Function isValidParameter. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
196                 exit(1);
197         }
198         catch(...) {
199                 cout << "An unknown error has occurred in the ValidParameters class function isValidParameter. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
200                 exit(1);
201         }
202 }
203 /*******************************************************/
204
205 /******************************************************/
206
207 string ValidParameters::validFile(map<string, string> container, string parameter, bool isFile) {
208         try {
209                 int ableToOpen;
210                 ifstream in;
211                 map<string, string>::iterator it;
212                 
213                 it = container.find(parameter);
214                 if(it != container.end()){ //no parameter given
215                         if(isFile == true) {
216                                 ableToOpen = openInputFile(it->second, in);
217                                 if (ableToOpen == 1) { return "not open"; }
218                                 in.close();
219                         }
220                 }else { return "not found"; }
221                 
222                 return it->second;
223         
224         }
225         catch(exception& e) {
226                 cout << "Standard Error: " << e.what() << " has occurred in the ValidParameters class Function validFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
227                 exit(1);
228         }
229         catch(...) {
230                 cout << "An unknown error has occurred in the ValidParameters class function validFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
231                 exit(1);
232         }
233 }
234
235 /***********************************************************************/
236
237 /***********************************************************************/
238 void ValidParameters::initParameterRanges() {
239         try {   
240                 int rangeSize = 5;
241
242                 /**************************************************************************************************************
243                         {">=" or "=>" or ">" if the value should be greater than or equal to or just greater than the lower bound,
244                     A number representing the lower bound ("NA" if there is no lower bound), 
245                    "<=" or "=<" or "<" if the value shoud be less than or equal to or just less than the upper bound,
246                     A number representing the upper bound ("NA" if there is no lower bound),
247                    "between" if between lower and upper bounds or "only" if exactly one of the bounds};
248                    
249                    # = parameter
250                    # (>, >=) lower bound, # (<, <=) upperbound, # should be (between, only) lower and upper bounds.
251                    ***********************************************************************************************************/
252                 
253                 string precisionArray[] = {">=","10", "<","NA", "between"};
254                 parameterRanges["precision"] = addParameters(precisionArray, rangeSize);
255                 
256                 string itersArray[] = {">=","10", "<","NA", "between"};
257                 parameterRanges["iters"] = addParameters(itersArray, rangeSize);
258
259                 string freqArray[] = {">=","1", "<","NA", "between"};
260                 parameterRanges["freq"] = addParameters(freqArray, rangeSize);
261
262                 //string lineArray[] = {">=","1", "<","NA", "between"};
263                 //parameterRanges["line"] = addParameters(lineArray, rangeSize);
264
265                 string abundArray[] = {">=","5", "<","NA", "between"};
266                 parameterRanges["abund"] = addParameters(abundArray, rangeSize);
267                 
268                 string softArray[] = {">=","0", "<=","100", "between"};
269                 parameterRanges["soft"] = addParameters(softArray, rangeSize);
270                 
271                 string sizeArray[] = {">=","1", "<","NA", "between"};
272                 parameterRanges["size"] = addParameters(sizeArray, rangeSize);
273         }
274         catch(exception& e) {
275                 cout << "Standard Error: " << e.what() << " has occurred in the ValidParameters class Function isValidParameter. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
276                 exit(1);
277         }
278         catch(...) {
279                 cout << "An unknown error has occurred in the ValidParameters class function isValidParameter. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
280                 exit(1);
281         }
282 }
283
284 /***********************************************************************/
285
286 /***********************************************************************/
287 vector<string> ValidParameters::addParameters(string parameters[], int size) {
288         try {   
289                 vector<string> pVector (parameters, parameters+size); 
290                 return pVector;
291         }
292         catch(exception& e) {
293                 cout << "Standard Error: " << e.what() << " has occurred in the ValidParameters class Function isValidParameter. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
294                 exit(1);
295         }
296         catch(...) {
297                 cout << "An unknown error has occurred in the ValidParameters class function isValidParameter. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
298                 exit(1);
299         }
300 }
301