File: validparameter.cpp

package info (click to toggle)
mothur 1.44.3-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 13,328 kB
  • sloc: cpp: 158,612; makefile: 119; sh: 10
file content (363 lines) | stat: -rw-r--r-- 12,489 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*
 *  validparameter.cpp
 *  Dotur
 *
 *  Created by Sarah Westcott on 1/5/09.
 *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
 *
 */

#include "validparameter.h"

/***********************************************************************/

ValidParameters::ValidParameters() {
	try {
		m = MothurOut::getInstance();
        current = CurrentFile::getInstance();
        locations = current->getLocations();
		initParameterRanges();
	}
	catch(exception& e) {
		m->errorOut(e, "ValidParameters", "ValidParameters");
		exit(1);
	}
}
/***********************************************************************/

ValidParameters::~ValidParameters() {}

/***********************************************************************/
bool ValidParameters::isValidParameter(string parameter, vector<string> cParams, string value) {
	try {	
		bool valid = false;
		//vector<string> cParams = commandParameters[command];
		int numParams = cParams.size(); 
		for(int i = 0; i < numParams; i++) {
			if(cParams.at(i).compare(parameter) == 0) {
				valid = true;
                break;
			}
		}
		if(!valid) {
			m->mothurOut("[WARNING]: " + parameter + " is not a valid parameter, ignoring.\n");
			m->mothurOut("The valid parameters are: ");
			for(int i = 0; i < numParams-1; i++)
				m->mothurOut(cParams.at(i) + ", ");
			m->mothurOut("and " + cParams.at(numParams-1) + ".\n");
			return false;
		}
		
        if(parameterRanges.count(parameter) != 1) { return true; }
	
		int pVal;
		double piSentinel = 3.14159;
		vector<string> range = parameterRanges[parameter];
		
		vector<string> values;
        Utils util; util.splitAtDash(value, values);
		
		for(int i = 0; i < values.size(); i++) {
			value = values.at(i);
			valid = convertTest(value, pVal);
		
			if(!valid)
				return false;
			
			
			
			/********************************************************************************************************
				   Special Cases
			*********************************************************************************************************/
			
			if(parameter.compare("precision") == 0) {
				double logNum = log10((double)pVal);
				double diff = (double)((int)logNum - logNum);
				if(!util.isEqual(diff, 0)) {
					m->mothurOut("The precision parameter can only take powers of 10 as a value (e.g. 10,1000,1000, etc.)\n");
					return false;
				}
			}
			
			/************************************************************************************************************/
			
			
			
			double a,b,c,d,e;
			
			if(range.at(1).compare("NA") == 0)
				a = piSentinel;
			else
				a = atoi(range.at(1).c_str()); 
				
			if(range.at(3).compare("NA") == 0)
				b = piSentinel;
			else
				b = atoi(range.at(3).c_str()); 
						
			if(range.at(4).compare("between") == 0)
				c = 0;
			else if(range.at(4).compare("only") == 0)
				c = 1;
			else {
				m->mothurOut("The range can only be 'between' or 'only' the bounding numbers.\n");
				return false;
			}
			
			if(range.at(0).compare(">") == 0)
				d = 0;
			else if(range.at(0).compare(">=") == 0 || range[3].compare("=>") == 0)
				d = 1;
			else {
				m->mothurOut("The parameter value can only be '>', '>=', or '=>' the lower bounding number.\n");
				return false;
			}
			
			if(range.at(2).compare("<") == 0)
				e = 0;
			else if(range.at(2).compare("<=") == 0 || range[4].compare("=<") == 0)
				e = 1;
			else {
				m->mothurOut("The parameter value can only be '<', '<=', or '=<' the upper bounding number.\n");
				return false;
			}
			
			bool a0 = pVal > a;
			bool a1 = pVal >= a;
			bool b0 = pVal < b;
			bool b1 = pVal <= b;
			
			if(c != 1) {
				if(!util.isEqual(a, piSentinel) && util.isEqual(b, piSentinel)) {
					if(util.isEqual(d, 0))
						valid = a0;
					else
						valid = a1;
				}
				else if(util.isEqual(a, piSentinel) && !util.isEqual(b, piSentinel)) {
					if(e == 0)
						valid = b0;
					else
						valid = b1;
				}
				else {
					if(util.isEqual(d, 0) && util.isEqual(e, 0))
						valid = (a0 && b0);
					else if(util.isEqual(d, 0) && util.isEqual(e, 1))
						valid = (a0 && b1);
					else if(util.isEqual(d, 1) && util.isEqual(e, 0))
						valid = (a1 && b0);
					else
						valid = (a1 && b1);
				}
			}
			else {
				if(!util.isEqual(a, piSentinel) && util.isEqual(b, piSentinel))
					valid = (pVal == a);
				else if(util.isEqual(a, piSentinel) && !util.isEqual(b, piSentinel))
					valid = (pVal == b);
				else
					valid = (pVal == a || pVal == b);
			}
			
			
			if(!valid) {
				m->mothurOut("The '" + parameter + "' parameter needs to be ");
				if(util.isEqual(c, 1))
					m->mothurOut("either '" + toString(a) + "' or '" + toString(b) + "'.\n");
				else {
					if(!util.isEqual(a, piSentinel)) {
						m->mothurOut(">");
						if(!util.isEqual(d, 0))
							m->mothurOut("=");
						m->mothurOut(" '" + toString(a) + "'");
					}
					if(util.isEqual(b, piSentinel))
						m->mothurOut( "'.\n");
					else if(!util.isEqual(a, piSentinel))
						m->mothurOut(" and ");
					if(!util.isEqual(b, piSentinel)) {
						m->mothurOut("<");
						if(!util.isEqual(e, 0))
							m->mothurOut("=");
						m->mothurOut(" '" + toString(b) + "'.\n");
					}
				}
				return false;
			}
		}
		return true;
	}
	catch(exception& e) {
		m->errorOut(e, "ValidParameters", "isValidParameters");
		exit(1);
	}
}
/*******************************************************/

/******************************************************/
string ValidParameters::valid(map<string, string>& container, string parameter) {
    try {
        map<string, string>::iterator it;
        
        it = container.find(parameter);
        if(it != container.end()){ }
        else { return "not found"; }
        
        return it->second;
    }
    catch(exception& e) {
        m->errorOut(e, "ValidParameters", "valid");
        exit(1);
    }
}
/******************************************************/
vector<string> ValidParameters::validFiles(map<string, string>& container, string parameter) {
    try {
        vector<string> vFiles;
        Utils util;
        bool openedAtLeastOne = false;

        map<string, string>::iterator it = container.find(parameter);
        if(it != container.end()){ //no parameter given
            if ((it->second == "NONE") || (it->second == "none")) {it->second = "NONE";}//ignore
            else {
                
                vector<string> files; util.splitAtDash(it->second, files);
                
                for (int i = 0; i < files.size(); i++) {
                    files[i] = util.removeQuotes(files[i]);
                    string filename = files[i];
                    if (util.checkLocations(filename, current->getLocations())) { vFiles.push_back(filename); container[parameter] = filename; openedAtLeastOne = true; }
                    else { m->mothurOut("Unable to open " + filename + ", skipping.\n");  }
                    
                    //check phylip file to make sure its really phylip and not column
                    if ((it->first == "phylip") && (openedAtLeastOne)) {
                        ifstream inPhylip;
                        util.openInputFile(filename, inPhylip);
                        
                        string numTest, name;
                        inPhylip >> numTest >> name;
                        inPhylip.close();
                        
                        if (!util.isContainingOnlyDigits(numTest)) { m->mothurOut("[ERROR]: expected a number and got " + numTest + ". I suspect you entered a column formatted file as a phylip file, aborting.\n"); return nullVector; }
                    }
                    
                    //check for blank file
                    if (openedAtLeastOne) {
                        if (util.isBlank(container[parameter])) { m->mothurOut("[ERROR]: " + filename + " is blank, skipping.\n");  }
                    }
                }
                
                if (!openedAtLeastOne) { vFiles.push_back("not open"); }
            }
        }else { return vFiles; }
        
        return vFiles;
        
    }
    catch(exception& e) {
        m->errorOut(e, "ValidParameters", "validFile");
        exit(1);
    }
}
/******************************************************/
string ValidParameters::validFile(map<string, string>& container, string parameter) {
    try {
        bool ableToOpen = false;
        Utils util;
        
        map<string, string>::iterator it;
        
        it = container.find(parameter);
        if(it != container.end()){ //no parameter given
            if ((it->second == "NONE") || (it->second == "none")) {it->second = "NONE";}//ignore
            else {
                it->second = util.removeQuotes(it->second);
                string filename = it->second;
                if (util.checkLocations(filename, current->getLocations())) { container[parameter] = filename; ableToOpen = true; }
                else { m->mothurOut("Unable to open " + container[parameter]); m->mothurOutEndLine(); return "not open";  }
                
                //check phylip file to make sure its really phylip and not column
                if ((it->first == "phylip") && (ableToOpen)) {
                    ifstream inPhylip;
                    util.openInputFile(it->second, inPhylip);
                    
                    string numTest, name;
                    inPhylip >> numTest >> name;
                    inPhylip.close();
                    
                    if (!util.isContainingOnlyDigits(numTest)) { m->mothurOut("[ERROR]: expected a number and got " + numTest + ". I suspect you entered a column formatted file as a phylip file, aborting.\n");  return "not found"; }
                }
                
                //check for blank file
                if (ableToOpen) {
                    if (util.isBlank(container[parameter])) {
                        m->mothurOut("[ERROR]: " + container[parameter] + " is blank, aborting.\n");  return "not found";
                    }
                }
            }
        }else { return "not found"; }
        
        return it->second;
        
    }
    catch(exception& e) {
        m->errorOut(e, "ValidParameters", "validFile");
        exit(1);
    }
}
/***********************************************************************/

/***********************************************************************/
void ValidParameters::initParameterRanges() {
	try {	
		int rangeSize = 5;

		/**************************************************************************************************************
			{">=" or "=>" or ">" if the value should be greater than or equal to or just greater than the lower bound,
		    A number representing the lower bound ("NA" if there is no lower bound), 
		   "<=" or "=<" or "<" if the value shoud be less than or equal to or just less than the upper bound,
		    A number representing the upper bound ("NA" if there is no lower bound),
		   "between" if between lower and upper bounds or "only" if exactly one of the bounds};
		   
		   # = parameter
		   # (>, >=) lower bound, # (<, <=) upperbound, # should be (between, only) lower and upper bounds.
		   ***********************************************************************************************************/
		
		string precisionArray[] = {">=","10", "<","NA", "between"};
		parameterRanges["precision"] = addParameters(precisionArray, rangeSize);
		
		string itersArray[] = {">=","1", "<","NA", "between"};
		parameterRanges["iters"] = addParameters(itersArray, rangeSize);

		string abundArray[] = {">=","5", "<","NA", "between"};
		parameterRanges["abund"] = addParameters(abundArray, rangeSize);
		
		string softArray[] = {">=","0", "<=","100", "between"};
		parameterRanges["soft"] = addParameters(softArray, rangeSize);
		
		string sizeArray[] = {">=","1", "<","NA", "between"};
		parameterRanges["size"] = addParameters(sizeArray, rangeSize);
	}
	catch(exception& e) {
		m->errorOut(e, "ValidParameters", "initParameterRanges");
		exit(1);
	}
}

/***********************************************************************/

/***********************************************************************/
vector<string> ValidParameters::addParameters(string parameters[], int size) {
	try {	
		vector<string> pVector (parameters, parameters+size); 
		return pVector;
	}
	catch(exception& e) {
		m->errorOut(e, "ValidParameters", "addParameters");
		exit(1);
	}
}
/***********************************************************************/