5 * Created by Sarah Westcott on 1/5/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "validparameter.h"
12 /***********************************************************************/
14 ValidParameters::ValidParameters() {
16 m = MothurOut::getInstance();
17 initParameterRanges();
21 m->errorOut(e, "ValidParameters", "ValidParameters");
25 /***********************************************************************/
27 ValidParameters::ValidParameters(string c) {
29 m = MothurOut::getInstance();
30 initParameterRanges();
34 m->errorOut(e, "ValidParameters", "ValidParameters");
39 /***********************************************************************/
41 ValidParameters::~ValidParameters() {}
43 /***********************************************************************/
44 bool ValidParameters::isValidParameter(string parameter, vector<string> cParams, string value) {
47 //vector<string> cParams = commandParameters[command];
48 int numParams = cParams.size();
49 for(int i = 0; i < numParams; i++) {
50 if(cParams.at(i).compare(parameter) == 0) {
56 m->mothurOut(parameter + " is not a valid parameter."); m->mothurOutEndLine();
57 m->mothurOut("The valid parameters are: ");
58 for(int i = 0; i < numParams-1; i++)
59 m->mothurOut(cParams.at(i) + ", ");
60 m->mothurOut("and " + cParams.at(numParams-1) + ".\n");
64 if(parameterRanges.count(parameter) != 1)
68 double piSentinel = 3.14159;
69 vector<string> range = parameterRanges[parameter];
71 vector<string> values;
72 m->splitAtDash(value, values);
74 for(int i = 0; i < values.size(); i++) {
76 valid = convertTest(value, pVal);
83 /********************************************************************************************************
85 *********************************************************************************************************/
87 if(parameter.compare("precision") == 0) {
88 double logNum = log10((double)pVal);
89 double diff = (double)((int)logNum - logNum);
91 m->mothurOut("The precision parameter can only take powers of 10 as a value (e.g. 10,1000,1000, etc.)\n");
96 /************************************************************************************************************/
102 if(range.at(1).compare("NA") == 0)
105 a = atoi(range.at(1).c_str());
107 if(range.at(3).compare("NA") == 0)
110 b = atoi(range.at(3).c_str());
112 if(range.at(4).compare("between") == 0)
114 else if(range.at(4).compare("only") == 0)
117 m->mothurOut("The range can only be 'between' or 'only' the bounding numbers.\n");
121 if(range.at(0).compare(">") == 0)
123 else if(range.at(0).compare(">=") == 0 || range[3].compare("=>") == 0)
126 m->mothurOut("The parameter value can only be '>', '>=', or '=>' the lower bounding number.\n");
130 if(range.at(2).compare("<") == 0)
132 else if(range.at(2).compare("<=") == 0 || range[4].compare("=<") == 0)
135 m->mothurOut("The parameter value can only be '<', '<=', or '=<' the upper bounding number.\n");
145 if(a != piSentinel && b == piSentinel) {
151 else if(a == piSentinel && b != piSentinel) {
160 else if(d == 0 && e == 1)
162 else if(d == 1 && e == 0)
169 if(a != piSentinel && b == piSentinel)
171 else if(a == piSentinel && b != piSentinel)
174 valid = (pVal == a || pVal == b);
179 m->mothurOut("The '" + parameter + "' parameter needs to be ");
181 m->mothurOut("either '" + toString(a) + "' or '" + toString(b) + "'.\n");
183 if(a != piSentinel) {
187 m->mothurOut(" '" + toString(a) + "'");
190 m->mothurOut( "'.\n");
191 else if(a != piSentinel)
192 m->mothurOut(" and ");
193 if(b != piSentinel) {
197 m->mothurOut(" '" + toString(b) + "'.\n");
205 catch(exception& e) {
206 m->errorOut(e, "ValidParameters", "isValidParameters");
210 /*******************************************************/
212 /******************************************************/
214 string ValidParameters::validFile(map<string, string>& container, string parameter, bool isFile) {
218 map<string, string>::iterator it;
220 it = container.find(parameter);
221 if(it != container.end()){ //no parameter given
225 int pos = (it->second).find(".tx.");
226 if (pos != string::npos) { m->sharedHeaderMode = "tax"; }
227 else { m->sharedHeaderMode = "otu"; }
232 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
233 MPI_Comm_size(MPI_COMM_WORLD, &processors);
235 if (commandName == "") { processors = 1; }
240 ableToOpen = m->openInputFile(it->second, in, "noerror");
243 //if you can't open it, try default location
244 if (ableToOpen == 1) {
245 if (m->getDefaultPath() != "") { //default path is set
246 string tryPath = m->getDefaultPath() + m->getSimpleName(it->second);
247 m->mothurOut("Unable to open " + it->second + ". Trying default " + tryPath); m->mothurOutEndLine();
249 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
251 container[parameter] = tryPath;
255 //if you can't open it, try default location
256 if (ableToOpen == 1) {
257 if (m->getOutputDir() != "") { //default path is set
258 string tryPath = m->getOutputDir() + m->getSimpleName(it->second);
259 m->mothurOut("Unable to open " + it->second + ". Trying output directory " + tryPath); m->mothurOutEndLine();
261 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
262 container[parameter] = tryPath;
270 for(int i = 1; i < processors; i++) {
271 MPI_Send(&ableToOpen, 1, MPI_INT, i, 2001, MPI_COMM_WORLD);
273 int length = container[parameter].length();
274 MPI_Send(&length, 1, MPI_INT, i, 2001, MPI_COMM_WORLD);
275 MPI_Send(&(container[parameter][0]), length, MPI_CHAR, i, 2001, MPI_COMM_WORLD);
279 MPI_Recv(&ableToOpen, 1, MPI_INT, 0, 2001, MPI_COMM_WORLD, &status);
282 MPI_Recv(&length, 1, MPI_INT, 0, 2001, MPI_COMM_WORLD, &status);
284 char* tempBuf = new char[length];
285 MPI_Recv(&tempBuf[0], length, MPI_CHAR, 0, 2001, MPI_COMM_WORLD, &status);
287 container[parameter] = tempBuf;
288 if (container[parameter].length() > length) { container[parameter] = container[parameter].substr(0, length); }
294 if (ableToOpen == 1) {
295 m->mothurOut("Unable to open " + container[parameter]); m->mothurOutEndLine();
299 //check phylip file to make sure its really phylip and not column
300 if ((it->first == "phylip") && (ableToOpen != 1)) {
302 m->openInputFile(it->second, inPhylip);
304 string numTest, name;
305 inPhylip >> numTest >> name;
308 if (!m->isContainingOnlyDigits(numTest)) { m->mothurOut("[ERROR]: expected a number and got " + numTest + ". I suspect you entered a column formatted file as a phylip file, aborting."); m->mothurOutEndLine(); return "not found"; }
311 //check for blank file
312 if (ableToOpen != 1) {
313 if (m->isBlank(container[parameter])) {
314 m->mothurOut("[ERROR]: " + container[parameter] + " is blank, aborting."); m->mothurOutEndLine(); return "not found";
319 }else { return "not found"; }
324 catch(exception& e) {
325 m->errorOut(e, "ValidParameters", "validFile");
330 /***********************************************************************/
332 /***********************************************************************/
333 void ValidParameters::initParameterRanges() {
337 /**************************************************************************************************************
338 {">=" or "=>" or ">" if the value should be greater than or equal to or just greater than the lower bound,
339 A number representing the lower bound ("NA" if there is no lower bound),
340 "<=" or "=<" or "<" if the value shoud be less than or equal to or just less than the upper bound,
341 A number representing the upper bound ("NA" if there is no lower bound),
342 "between" if between lower and upper bounds or "only" if exactly one of the bounds};
345 # (>, >=) lower bound, # (<, <=) upperbound, # should be (between, only) lower and upper bounds.
346 ***********************************************************************************************************/
348 string precisionArray[] = {">=","10", "<","NA", "between"};
349 parameterRanges["precision"] = addParameters(precisionArray, rangeSize);
351 string itersArray[] = {">=","1", "<","NA", "between"};
352 parameterRanges["iters"] = addParameters(itersArray, rangeSize);
354 string abundArray[] = {">=","5", "<","NA", "between"};
355 parameterRanges["abund"] = addParameters(abundArray, rangeSize);
357 string softArray[] = {">=","0", "<=","100", "between"};
358 parameterRanges["soft"] = addParameters(softArray, rangeSize);
360 string sizeArray[] = {">=","1", "<","NA", "between"};
361 parameterRanges["size"] = addParameters(sizeArray, rangeSize);
363 catch(exception& e) {
364 m->errorOut(e, "ValidParameters", "initParameterRanges");
369 /***********************************************************************/
371 /***********************************************************************/
372 vector<string> ValidParameters::addParameters(string parameters[], int size) {
374 vector<string> pVector (parameters, parameters+size);
377 catch(exception& e) {
378 m->errorOut(e, "ValidParameters", "addParameters");