5 * Created by Sarah Westcott on 1/2/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "errorchecking.h"
13 /*******************************************************/
15 /******************************************************/
17 ErrorCheck::ErrorCheck() {
18 globaldata = GlobalData::getInstance();
19 validCommand = new ValidCommands();
20 validParameter = new ValidParameters();
22 /*******************************************************/
24 /******************************************************/
26 void ErrorCheck::refresh() {
27 columnfile = globaldata->getColumnFile();
28 phylipfile = globaldata->getPhylipFile();
29 listfile = globaldata->getListFile();
30 rabundfile = globaldata->getRabundFile();
31 sabundfile = globaldata->getSabundFile();
32 namefile = globaldata->getNameFile();
33 groupfile = globaldata->getGroupFile();
34 orderfile = globaldata->getOrderFile();
35 fastafile = globaldata->getFastaFile();
36 treefile = globaldata->getTreeFile();
37 cutoff = globaldata->getCutOff();
38 format = globaldata->getFormat();
39 method = globaldata->getMethod();
66 "sharedrarefaction", //23
72 string c0[] = {p[0],p[5],p[10],p[11]};
73 string c1[] = {p[2],p[6],p[7]};
74 string c2[] = {p[10],p[11],p[15]};
76 string c4[] = {p[14],p[17],p[18],p[19],p[26]};
77 string c5[] = {p[13],p[14],p[17],p[18],p[21],p[25]};
81 string c9[] = {p[12],p[14],p[17],p[18],p[20],p[26]};
82 string c10[] = {p[12],p[13],p[17],p[18],p[23]};
83 string c11[] = {p[17],p[18],p[22],p[26]};
84 string c12[] = {p[13],p[17],p[18],p[24]};
87 vector<string> v0 (c0, c0+sizeof(c0)/sizeof(string));
88 vector<string> v1 (c1, c1+sizeof(c1)/sizeof(string));
89 vector<string> v2 (c2, c2+sizeof(c2)/sizeof(string));
90 vector<string> v3 (c3, c3+sizeof(c3)/sizeof(string));
91 vector<string> v4 (c4, c4+sizeof(c4)/sizeof(string));
92 vector<string> v5 (c5, c5+sizeof(c5)/sizeof(string));
93 vector<string> v6 (c6, c6+sizeof(c6)/sizeof(string));
94 vector<string> v7 (c7, c7+sizeof(c7)/sizeof(string));
95 vector<string> v8 (c8, c8+sizeof(c8)/sizeof(string));
96 vector<string> v9 (c9, c9+sizeof(c9)/sizeof(string));
97 vector<string> v10 (c10, c10+sizeof(c10)/sizeof(string));
98 vector<string> v11 (c11, c11+sizeof(c11)/sizeof(string));
99 vector<string> v12 (c12, c12+sizeof(c12)/sizeof(string));
100 vector<string> v13 (c13, c13+sizeof(c13)/sizeof(string));
102 vector<vector<string> > allCommands;
103 allCommands.push_back(v0);
104 allCommands.push_back(v1);
105 allCommands.push_back(v2);
106 allCommands.push_back(v3);
107 allCommands.push_back(v4);
108 allCommands.push_back(v5);
109 allCommands.push_back(v6);
110 allCommands.push_back(v7);
111 allCommands.push_back(v8);
112 allCommands.push_back(v9);
113 allCommands.push_back(v10);
114 allCommands.push_back(v11);
115 allCommands.push_back(v12);
116 allCommands.push_back(v13);
118 string commands[] = {
123 "collect.single", //4
124 "collect.shared", //5
128 "rarefaction.single", //9
129 "rarefaction.shared", //10
130 "summary.single", //11
131 "summary.shared", //12
135 for(int i = 0; i < allCommands.size(); i++)
136 commandParameters[commands[i]] = allCommands.at(i);
138 //{Lowerbound(piSent if no lowerbound), Upperbound(piSent if no upperbound), 1 if only the first 2 values, 0 if greater than, 0 if less than};
140 double ip0[] = {10, piSent, 0, 1, 0};
141 double ip1[] = {10, piSent, 0, 1, 0};
142 double ip2[] = {0, 1, 1, 0, 0};
143 double ip3[] = {1, piSent, 0, 0, 0};
144 double ip4[] = {1, piSent, 0, 1, 0};
145 double ip5[] = {5, piSent, 0, 1, 0};
147 vector<double> ipv0 (ip0, ip0+sizeof(ip0)/sizeof(double));
148 vector<double> ipv1 (ip1, ip1+sizeof(ip1)/sizeof(double));
149 vector<double> ipv2 (ip2, ip2+sizeof(ip2)/sizeof(double));
150 vector<double> ipv3 (ip3, ip3+sizeof(ip3)/sizeof(double));
151 vector<double> ipv4 (ip4, ip4+sizeof(ip4)/sizeof(double));
152 vector<double> ipv5 (ip5, ip5+sizeof(ip5)/sizeof(double));
154 intParams[p[11]] = ipv0;
155 intParams[p[12]] = ipv1;
156 intParams[p[13]] = ipv2;
157 intParams[p[14]] = ipv3;
158 intParams[p[17]] = ipv4;
159 intParams[p[26]] = ipv5;
161 randomtree = globaldata->getRandomTree();
162 sharedfile = globaldata->getSharedFile();
165 /*******************************************************/
167 /******************************************************/
169 ErrorCheck::~ErrorCheck() {}
171 /*******************************************************/
173 /******************************************************/
175 bool ErrorCheck::checkInput(string input) {
182 //get command name and parameters
183 int openParen = input.find_first_of('(');
184 int closeParen = input.find_last_of(')');
186 if(openParen != -1 && closeParen != -1){
187 commandName = input.substr(0, openParen); //commandName contains everything before "("
188 optionText = input.substr(openParen+1, closeParen-openParen-1); //optionString contains everything between "(" and ")".
189 }else if (openParen == -1) { //there is no parenthesis
190 cout << input << " is not a valid command. You are missing the ()." << endl;
194 //is it a valid command
195 if (validCommand->isValidCommand(commandName) != true) { return false; }
197 string parameter, value;
199 //reads in parameters and values
200 if((optionText != "") && (commandName != "help")){
201 while((optionText.find_first_of(',') != -1) && (errorFree)) { //while there are parameters
202 splitAtComma(value, optionText);
203 splitAtEquals(parameter, value);
205 //is it a valid parameter
206 if (validParameter->isValidParameter(parameter) != true) { return false; }
207 if(!validCommandParameter(parameter,commandName)) {
208 cout << "'" << parameter << "' is not a valid parameter for the " << commandName << " command.\n";
211 if(!validParameterValue(value, parameter)) {
212 if(parameter.compare("precision") == 0)
213 cout << "The precision parameter can only take powers of 10 as a value (e.g. 10,1000,1000, etc.)\n";
215 vector<double> bounds = intParams[parameter];
216 double a = bounds.at(0);
217 double b = bounds.at(1);
218 double c = bounds.at(2);
219 double d = bounds.at(3);
220 double e = bounds.at(4);
221 cout << "The '" << parameter << "' parameter needs to be ";
223 cout << "either '" << a << "' or '" << b << "'.\n";
231 cout << " '" << a << "'";
242 cout << " '" << b << ".\n";
249 if (parameter == "phylip" ) { phylipfile = value; }
250 if (parameter == "column" ) { columnfile = value; }
251 if (parameter == "list" ) { listfile = value; }
252 if (parameter == "rabund" ) { rabundfile = value; }
253 if (parameter == "sabund" ) { sabundfile = value; }
254 if (parameter == "name" ) { namefile = value; }
255 if (parameter == "order" ) { orderfile = value; }
256 if (parameter == "fasta" ) { fastafile = value; }
257 if (parameter == "tree" ) { treefile = value; }
258 if (parameter == "group" ) { groupfile = value; }
259 if (parameter == "shared" ) { sharedfile = value; }
260 if (parameter == "cutoff" ) { cutoff = value; }
261 if (parameter == "precision" ) { precision = value; }
262 if (parameter == "iters" ) { iters = value; }
263 if (parameter == "jumble" ) { jumble = value; }
264 if (parameter == "freq" ) { freq = value; }
265 if (parameter == "method" ) { method = value; }
266 if (parameter == "fileroot" ) { fileroot = value; }
267 if (parameter == "line" ) { line = value; }
268 if (parameter == "label" ) { label = value; }
269 if (parameter == "abund" ) { abund = value; }
270 if (parameter == "random" ) { randomtree = value; }
273 //gets the last parameter and value
274 if (errorFree) { //gets the last parameter and value
276 splitAtEquals(parameter, value);
277 //is it a valid parameter
278 if (validParameter->isValidParameter(parameter) != true) { return false; }
279 if(!validCommandParameter(parameter,commandName)) {
280 cout << "'" << parameter << "' is not a valid parameter for the " << commandName << " command.\n";
283 if(!validParameterValue(value, parameter)) {
284 if(parameter.compare("precision") == 0)
285 cout << "The precision parameter can only take powers of 10 as a value (e.g. 10,1000,1000, etc.)\n";
287 vector<double> bounds = intParams[parameter];
288 double a = bounds.at(0);
289 double b = bounds.at(1);
290 double c = bounds.at(2);
291 double d = bounds.at(3);
292 double e = bounds.at(4);
293 cout << "The '" << parameter << "' parameter needs to be ";
295 cout << "either '" << a << "' or '" << b << "'.\n";
303 cout << " '" << a << "'";
314 cout << " '" << b << ".\n";
320 if (parameter == "phylip" ) { phylipfile = value; }
321 if (parameter == "column" ) { columnfile = value; }
322 if (parameter == "list" ) { listfile = value; }
323 if (parameter == "rabund" ) { rabundfile = value; }
324 if (parameter == "sabund" ) { sabundfile = value; }
325 if (parameter == "name" ) { namefile = value; }
326 if (parameter == "order" ) { orderfile = value; }
327 if (parameter == "group" ) { groupfile = value; }
328 if (parameter == "shared" ) { sharedfile = value; }
329 if (parameter == "fasta" ) { fastafile = value; }
330 if (parameter == "tree" ) { treefile = value; }
331 if (parameter == "cutoff" ) { cutoff = value; }
332 if (parameter == "precision" ) { precision = value; }
333 if (parameter == "iters" ) { iters = value; }
334 if (parameter == "jumble" ) { jumble = value; }
335 if (parameter == "freq" ) { freq = value; }
336 if (parameter == "method" ) { method = value; }
337 if (parameter == "fileroot" ) { fileroot = value; }
338 if (parameter == "line" ) { line = value; }
339 if (parameter == "label" ) { label = value; }
340 if (parameter == "random" ) { randomtree = value; }
341 if (parameter == "abund" ) { abund = value; }
345 //make sure the user does not use both the line and label parameters
346 if ((line != "") && (label != "")) { cout << "You may use either the line or label parameters, but not both." << endl; return false; }
348 if (commandName == "read.dist") {
351 }else if (commandName == "read.otu") {
352 //you want to do shared commands
353 if ((listfile != "") && (groupfile != "")) {
354 validateParseFiles(); //checks the listfile and groupfile parameters
355 }else { //you want to do single commands
359 }else if (commandName == "read.shared") {
360 //you want to do shared commands with just the shared file
362 }else if (commandName == "read.tree") {
363 validateTreeFiles(); //checks the treefile and groupfile parameters
364 }else if (commandName == "deconvolute") {
365 if (fastafile == "") { cout << "You must enter a fastafile with the deconvolute() command." << endl; return false; }
369 //are you trying to cluster before you have read something
370 if ((commandName == "cluster") && (globaldata->getSparseMatrix() == NULL) ||
371 (commandName == "cluster") && (globaldata->getListVector() == NULL)) {
372 cout << "Before you use the cluster command, you first need to read in a distance matrix." << endl;
376 if (commandName == "parsimony") {
377 //are you trying to use parsimony without reading a tree or saying you want random distribution
378 if (randomtree == "") {
379 if (globaldata->gTree.size() == 0) {
380 cout << "You must read a treefile and a groupfile or set the randomtree parameter to the output filename you wish, before you may execute the parsimony command." << endl; return false; }
384 if ((commandName == "unifrac.weighted") || (commandName == "unifrac.unweighted")) {
385 if (globaldata->gTree.size() == 0) {//no trees were read
386 cout << "You must execute the read.tree command, before you may execute the unifrac.weighted or unifrac.unweighted command." << endl; return false; }
389 //check for valid method
390 if(commandName == "get.group") {
391 if ((globaldata->getGroupFile() == "")) { cout << "You must read a group before you can use the get.group command." << endl; return false; }
393 if (commandName == "get.label" || commandName == "get.line") {
394 if ((globaldata->getListFile() == "") && (globaldata->getRabundFile() == "") && (globaldata->getSabundFile() == "")) { cout << "You must read a list, sabund or rabund before you can use the get.label or get.line command." << endl; return false; }
396 if (commandName == "cluster") {
397 if ((method == "furthest") || (method == "nearest") || (method == "average")) { }
398 else {cout << "Not a valid clustering method. Valid clustering algorithms are furthest, nearest or average." << endl; return false; }
401 if ((commandName == "collect.single") || (commandName == "rarefaction.single") || (commandName == "summary.single") ){
402 if ((globaldata->getListFile() == "") && (globaldata->getRabundFile() == "") && (globaldata->getSabundFile() == "")) { cout << "You must read a list, sabund or rabund before you can use the collect.single, rarefaction.single or summary.single commands." << endl; return false; }
405 if ((commandName == "collect.shared") || (commandName == "rarefaction.shared") || (commandName == "summary.shared") ){
406 if (globaldata->getSharedFile() == "") {
407 if (globaldata->getListFile() == "") { cout << "You must read a list and a group, or a shared before you can use the collect.shared, rarefaction.shared or summary.shared commands." << endl; return false; }
408 else if (globaldata->getGroupFile() == "") { cout << "You must read a list and a group, or a shared before you can use the collect.shared, rarefaction.shared or summary.shared commands." << endl; return false; }
412 globaldata->clearAbund();
417 /*******************************************************/
419 /******************************************************/
420 //This function checks to make sure the user entered a file to
421 // read and that the file exists and can be opened.
422 void ErrorCheck::validateReadFiles() {
424 //Validating files for read
428 //are we reading a phylipfile
429 if (phylipfile != "") {
430 ableToOpen = openInputFile(phylipfile, filehandle);
433 if (ableToOpen == 1) { errorFree = false; }
434 else { globaldata->inputFileName = phylipfile; }
435 //are we reading a phylipfile
436 }else if (columnfile != "") {
437 ableToOpen = openInputFile(columnfile, filehandle);
440 if (ableToOpen == 1) { errorFree = false; }
441 else { globaldata->inputFileName = columnfile; }
442 //are we reading a listfile
443 }else if (listfile!= "") {
444 ableToOpen = openInputFile(listfile, filehandle);
447 if (ableToOpen == 1) { errorFree = false; }
448 else { globaldata->inputFileName = listfile; }
449 //are we reading a rabundfile
450 }else if (rabundfile != "") {
451 ableToOpen = openInputFile(rabundfile, filehandle);
454 if (ableToOpen == 1) { errorFree = false; }
455 else { globaldata->inputFileName = rabundfile; }
456 //are we reading a sabundfile
457 }else if (sabundfile != "") {
458 ableToOpen = openInputFile(sabundfile, filehandle);
461 if (ableToOpen == 1) { errorFree = false; }
462 else { globaldata->inputFileName = sabundfile; }
463 }else if (fastafile != "") {
464 ableToOpen = openInputFile(fastafile, filehandle);
467 if (ableToOpen == 1) { errorFree = false; }
468 else { globaldata->inputFileName = fastafile; }
469 }else if (sharedfile != "") {
470 ableToOpen = openInputFile(sharedfile, filehandle);
473 if (ableToOpen == 1) { errorFree = false; }
474 else { globaldata->inputFileName = sharedfile; }
475 }else{ //no file given
479 catch(exception& e) {
480 cout << "Standard Error: " << e.what() << " has occurred in the ErrorCheck class Function validateReadFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
484 cout << "An unknown error has occurred in the ErrorCheck class function validateReadFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
489 /*******************************************************/
491 /******************************************************/
492 //This function checks to see if the given paramter
493 //is a valid paramter for the given command.
494 bool ErrorCheck::validCommandParameter(string parameter, string commandName) {
496 for(int i = 0; i < commandParameters[commandName].size(); i++)
497 if(parameter.compare(commandParameters[commandName][i]) == 0)
502 catch(exception& e) {
503 cout << "Standard Error: " << e.what() << " has occurred in the ErrorCheck class Function validateReadFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
507 cout << "An unknown error has occurred in the ErrorCheck class function validateReadFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
511 /*******************************************************/
513 /******************************************************/
514 //This function checks to see if the given paramter value
515 //is convertable into an int if that parameter requires it.
516 bool ErrorCheck::validParameterValue(string value, string parameter) {
519 if(intParams.count(parameter) == 1)
521 vector<double> bounds = intParams[parameter];
522 bool valid = convertTest(value, pVal);
525 if(parameter.compare("precision") == 0)
527 double logNum = log10((double)pVal);
528 double diff = (double)((int)logNum - logNum);
532 double a = bounds.at(0);
533 double b = bounds.at(1);
534 double c = bounds.at(2);
535 double d = bounds.at(3);
536 double e = bounds.at(4);
544 if(a == piSent && b == piSent)
546 if(a != piSent && b == piSent)
553 else if(a == piSent && b != piSent)
564 else if(d == 0 && e == 1)
566 else if(d == 1 && e == 0)
574 if(a == piSent && b == piSent)
576 if(a != piSent && b == piSent)
578 else if(a == piSent && b != piSent)
581 return (pVal == a || pVal == b);
587 catch(exception& e) {
588 cout << "Standard Error: " << e.what() << " has occurred in the ErrorCheck class Function validateReadFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
592 cout << "An unknown error has occurred in the ErrorCheck class function validateReadFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
596 /*******************************************************/
598 /******************************************************/
599 //This function checks to make sure the user entered appropriate
600 // format parameters on a distfile read
601 void ErrorCheck::validateReadDist() {
606 if ((phylipfile == "") && (columnfile == "")) { cout << "When executing a read.dist you must enter a phylip or a column." << endl; errorFree = false; }
607 else if ((phylipfile != "") && (columnfile != "")) { cout << "When executing a read.dist you must enter ONLY ONE of the following: phylip or column." << endl; errorFree = false; }
609 if (columnfile != "") {
610 if (namefile == "") {
611 cout << "You need to provide a namefile if you are going to use the column format." << endl;
614 ableToOpen = openInputFile(namefile, filehandle);
617 if (ableToOpen == 1) { errorFree = false; }
621 catch(exception& e) {
622 cout << "Standard Error: " << e.what() << " has occurred in the ErrorCheck class Function validateReadDist. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
626 cout << "An unknown error has occurred in the ErrorCheck class function validateReadDist. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
630 /*******************************************************/
632 /******************************************************/
633 //This function checks to make sure the user entered appropriate
634 // format parameters on a parselistcommand
635 void ErrorCheck::validateParseFiles() {
640 //checks for valid files
642 if (listfile == "") { cout << "When executing a read.otu for groups you must enter a list and a group." << endl; errorFree = false; }
643 else if (groupfile == "") { cout << "When executing a read.otu for groups you must enter a list and a group." << endl; errorFree = false; }
645 //checks parameters on the read command
646 if (listfile != "") {
647 ableToOpen = openInputFile(listfile, filehandle);
649 if (ableToOpen == 1) { //unable to open
652 if (groupfile != "") {
653 ableToOpen = openInputFile(groupfile, filehandle);
655 if (ableToOpen == 1) { //unable to open
661 catch(exception& e) {
662 cout << "Standard Error: " << e.what() << " has occurred in the ErrorCheck class Function validateReadPhil. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
666 cout << "An unknown error has occurred in the ErrorCheck class function validateReadPhil. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
670 /*******************************************************/
672 /******************************************************/
673 //This function checks to make sure the user entered appropriate
674 // format parameters on a parselistcommand
675 void ErrorCheck::validateTreeFiles() {
680 //checks for valid files
682 if (treefile == "") { cout << "When executing a read.tree you must enter a treefile and a groupfile." << endl; errorFree = false; }
683 else if (groupfile == "") { cout << "When executing a read.tree you must enter a treefile and a groupfile." << endl; errorFree = false; }
685 //checks parameters on the read command
686 if (treefile != "") {
687 ableToOpen = openInputFile(treefile, filehandle);
689 if (ableToOpen == 1) { //unable to open
692 if (groupfile != "") {
693 ableToOpen = openInputFile(groupfile, filehandle);
695 if (ableToOpen == 1) { //unable to open
701 catch(exception& e) {
702 cout << "Standard Error: " << e.what() << " has occurred in the ErrorCheck class Function validateTreeFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
706 cout << "An unknown error has occurred in the ErrorCheck class function validateTreeFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
711 /*******************************************************/
713 /******************************************************/
714 //This function checks to make sure the user entered appropriate
715 // format parameters on a distfile read
716 void ErrorCheck::validateReadPhil() {
721 //checks to make sure only one file type is given
722 if (listfile != "") {
723 if ((rabundfile != "") || (sabundfile != "")) {
724 cout << "When executing a read.otu you must enter ONLY ONE of the following: list, rabund or sabund." << endl; errorFree = false; }
725 }else if (rabundfile != "") {
726 if ((listfile != "") || (sabundfile != "")) {
727 cout << "When executing a read.otu you must enter ONLY ONE of the following: list, rabund or sabund." << endl; errorFree = false; }
728 }else if (sabundfile != "") {
729 if ((listfile != "") || (rabundfile != "")) {
730 cout << "When executing a read.otu you must enter ONLY ONE of the following: list, rabund or sabund." << endl; errorFree = false; }
731 }else if ((listfile == "") && (rabundfile == "") && (sabundfile == "")) {
732 cout << "When executing a read.otu you must enter one of the following: list, rabund or sabund." << endl; errorFree = false;
735 //checks parameters on the read command
736 if (orderfile != "") {
737 ableToOpen = openInputFile(orderfile, filehandle);
739 if (ableToOpen == 1) { //unable to open
744 catch(exception& e) {
745 cout << "Standard Error: " << e.what() << " has occurred in the ErrorCheck class Function validateReadPhil. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
749 cout << "An unknown error has occurred in the ErrorCheck class function validateReadPhil. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
753 /*******************************************************/
755 /******************************************************/
757 void ErrorCheck::clear() {
758 //option definitions should go here...
772 /*******************************************************/
774 /******************************************************/