]> git.donarmstrong.com Git - mothur.git/blob - validparameter.cpp
added help for alignment code and thomas' calcs to project
[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                 commandParameters["read.dist"] = addParameters(readdistArray, sizeof(readdistArray)/sizeof(string));
215
216                 string readotuArray[] =  {"list","order","shared", "line", "label","group","sabund", "rabund"};
217                 commandParameters["read.otu"] = addParameters(readotuArray, sizeof(readotuArray)/sizeof(string));
218                 
219                 string readtreeArray[] = {"tree","group"};
220                 commandParameters["read.tree"] = addParameters(readtreeArray, sizeof(readtreeArray)/sizeof(string));
221                 
222                 string clusterArray[] =  {"cutoff","precision","method"};
223                 commandParameters["cluster"] = addParameters(clusterArray, sizeof(clusterArray)/sizeof(string));
224                 
225                 string deconvoluteArray[] =  {"fasta"};
226                 commandParameters["deconvolute"] = addParameters(deconvoluteArray, sizeof(deconvoluteArray)/sizeof(string));
227                 
228                 string collectsingleArray[] =  {"freq","line","label","calc","abund","size"};
229                 commandParameters["collect.single"] = addParameters(collectsingleArray, sizeof(collectsingleArray)/sizeof(string));
230
231                 string collectsharedArray[] =  {"freq","line","label","calc","groups"};
232                 commandParameters["collect.shared"] = addParameters(collectsharedArray, sizeof(collectsharedArray)/sizeof(string));
233
234                 string getgroupArray[] =  {};
235                 commandParameters["get.group"]   = addParameters(getgroupArray, sizeof(getgroupArray)/sizeof(string));
236                 
237                 string getlabelArray[] =  {};
238                 commandParameters["get.label"]  = addParameters(getlabelArray, sizeof(getlabelArray)/sizeof(string));
239
240                 string getlineArray[] =  {};
241                 commandParameters["get.line"] = addParameters(getlineArray, sizeof(getlineArray)/sizeof(string));
242
243                 string rarefactionsingleArray[] =  {"iters","freq","line","label","calc","abund"};
244                 commandParameters["rarefaction.single"] = addParameters(rarefactionsingleArray, sizeof(rarefactionsingleArray)/sizeof(string));
245
246                 string rarefactionsharedArray[] =  {"iters","jumble","line","label","calc","groups"};
247                 commandParameters["rarefaction.shared"] = addParameters(rarefactionsharedArray, sizeof(rarefactionsharedArray)/sizeof(string));
248                 
249                 string libshuffArray[] =  {"iters","groups","step","form","cutoff"};
250                 commandParameters["libshuff"] = addParameters(libshuffArray, sizeof(libshuffArray)/sizeof(string));
251                 
252                 string summarysingleArray[] =  {"line","label","calc","abund","size"};
253                 commandParameters["summary.single"] = addParameters(summarysingleArray, sizeof(summarysingleArray)/sizeof(string));
254
255                 string summarysharedArray[] =  {"line","label","calc","groups"};
256                 commandParameters["summary.shared"] = addParameters(summarysharedArray, sizeof(summarysharedArray)/sizeof(string));
257
258                 string parsimonyArray[] =  {"random","groups","iters"};
259                 commandParameters["parsimony"] = addParameters(parsimonyArray, sizeof(parsimonyArray)/sizeof(string));
260
261                 string unifracWeightedArray[] =  {"groups","iters"};
262                 commandParameters["unifrac.weighted"] = addParameters(unifracWeightedArray, sizeof(unifracWeightedArray)/sizeof(string));
263
264                 string unifracUnweightedArray[] =  {"groups","iters"};
265                 commandParameters["unifrac.unweighted"] = addParameters(unifracUnweightedArray, sizeof(unifracUnweightedArray)/sizeof(string));
266
267                 string heatmapArray[] =  {"groups","line","label","sorted","scale"};
268                 commandParameters["heatmap"] = addParameters(heatmapArray, sizeof(heatmapArray)/sizeof(string));
269                 
270                 string filterseqsArray[] =  {"fasta","phylip","clustal","nexus", "trump", "soft", "filter"};
271                 commandParameters["filter.seqs"] = addParameters(filterseqsArray, sizeof(filterseqsArray)/sizeof(string));
272
273                 string vennArray[] =  {"groups","line","label","calc"};
274                 commandParameters["venn"] = addParameters(vennArray, sizeof(vennArray)/sizeof(string));
275                 
276                 string binseqsArray[] =  {"fasta","line","label","name"};
277                 commandParameters["bin.seqs"] = addParameters(binseqsArray, sizeof(binseqsArray)/sizeof(string));
278                 
279                 string getOTURepArray[] =  {"fasta","list","line","label","name"};
280                 commandParameters["get.oturep"] = addParameters(getOTURepArray, sizeof(getOTURepArray)/sizeof(string));
281                 
282                 string treeGroupsArray[] =  {"line","label","calc","groups"};
283                 commandParameters["tree.shared"] = addParameters(treeGroupsArray, sizeof(treeGroupsArray)/sizeof(string));
284                 
285                 string bootstrapArray[] =  {"line","label","calc","groups","iters"};
286                 commandParameters["bootstrap.shared"] = addParameters(bootstrapArray, sizeof(bootstrapArray)/sizeof(string));
287                 
288                 string concensusArray[] =  {};
289                 commandParameters["concensus"] = addParameters(concensusArray, sizeof(concensusArray)/sizeof(string));
290                 
291                 string distanceArray[] =  {"fasta","phylip","clustal","nexus", "calc", "ends", "cutoff", "processors"};
292                 commandParameters["dist.seqs"] = addParameters(distanceArray, sizeof(distanceArray)/sizeof(string));
293                 
294                 string AlignArray[] =  {"fasta","phylip","clustal","nexus", "template", "search", "ksize", "align", "match", "mismatch", "gapopen", "gapextend"};
295                 commandParameters["align.seqs"] = addParameters(AlignArray, sizeof(AlignArray)/sizeof(string));
296                 
297                 string quitArray[] = {};
298                 commandParameters["quit"] = addParameters(quitArray, sizeof(quitArray)/sizeof(string));
299
300         }
301         catch(exception& e) {
302                 cout << "Standard Error: " << e.what() << " has occurred in the ValidParameters class Function isValidParameter. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
303                 exit(1);
304         }
305         catch(...) {
306                 cout << "An unknown error has occurred in the ValidParameters class function isValidParameter. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
307                 exit(1);
308         }
309 }
310
311 /***********************************************************************/
312
313 /***********************************************************************/
314 void ValidParameters::initParameterRanges() {
315         try {   
316                 int rangeSize = 5;
317
318                 /**************************************************************************************************************
319                         {">=" or "=>" or ">" if the value should be greater than or equal to or just greater than the lower bound,
320                     A number representing the lower bound ("NA" if there is no lower bound), 
321                    "<=" or "=<" or "<" if the value shoud be less than or equal to or just less than the upper bound,
322                     A number representing the upper bound ("NA" if there is no lower bound),
323                    "between" if between lower and upper bounds or "only" if exactly one of the bounds};
324                    
325                    # = parameter
326                    # (>, >=) lower bound, # (<, <=) upperbound, # should be (between, only) lower and upper bounds.
327                    ***********************************************************************************************************/
328                 
329                 string precisionArray[] = {">=","10", "<","NA", "between"};
330                 parameterRanges["precision"] = addParameters(precisionArray, rangeSize);
331                 
332                 string itersArray[] = {">=","10", "<","NA", "between"};
333                 parameterRanges["iters"] = addParameters(itersArray, rangeSize);
334
335                 string jumbleArray[] = {">","0", "<","1", "only"};
336                 parameterRanges["jumble"] = addParameters(jumbleArray, rangeSize);
337
338                 string freqArray[] = {">=","1", "<","NA", "between"};
339                 parameterRanges["freq"] = addParameters(freqArray, rangeSize);
340
341                 //string lineArray[] = {">=","1", "<","NA", "between"};
342                 //parameterRanges["line"] = addParameters(lineArray, rangeSize);
343
344                 string abundArray[] = {">=","5", "<","NA", "between"};
345                 parameterRanges["abund"] = addParameters(abundArray, rangeSize);
346                 
347                 string softArray[] = {">=","0", "<=","100", "between"};
348                 parameterRanges["soft"] = addParameters(softArray, rangeSize);
349                 
350                 string sizeArray[] = {">=","1", "<","NA", "between"};
351                 parameterRanges["size"] = addParameters(sizeArray, rangeSize);
352         }
353         catch(exception& e) {
354                 cout << "Standard Error: " << e.what() << " has occurred in the ValidParameters class Function isValidParameter. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
355                 exit(1);
356         }
357         catch(...) {
358                 cout << "An unknown error has occurred in the ValidParameters class function isValidParameter. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
359                 exit(1);
360         }
361 }
362
363 /***********************************************************************/
364
365 /***********************************************************************/
366 vector<string> ValidParameters::addParameters(string parameters[], int size) {
367         try {   
368                 vector<string> pVector (parameters, parameters+size); 
369                 return pVector;
370         }
371         catch(exception& e) {
372                 cout << "Standard Error: " << e.what() << " has occurred in the ValidParameters class Function isValidParameter. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
373                 exit(1);
374         }
375         catch(...) {
376                 cout << "An unknown error has occurred in the ValidParameters class function isValidParameter. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
377                 exit(1);
378         }
379 }
380