5 * Created by Sarah Westcott on 1/2/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "errorchecking.h"
12 /*******************************************************/
14 /******************************************************/
16 ErrorCheck::ErrorCheck() {
17 globaldata = GlobalData::getInstance();
18 validCommand = new ValidCommands();
19 validParameter = new ValidParameters();
20 validCalculator = new ValidCalculators();
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();
40 randomtree = globaldata->getRandomTree();
43 /*******************************************************/
45 /******************************************************/
47 ErrorCheck::~ErrorCheck() {}
49 /*******************************************************/
51 /******************************************************/
53 bool ErrorCheck::checkInput(string input) {
60 //get command name and parameters
61 int openParen = input.find_first_of('(');
62 int closeParen = input.find_last_of(')');
64 if(openParen != -1 && closeParen != -1){
65 commandName = input.substr(0, openParen); //commandName contains everything before "("
66 optionText = input.substr(openParen+1, closeParen-openParen-1); //optionString contains everything between "(" and ")".
67 }else if (openParen == -1) { //there is no parenthesis
68 cout << input << " is not a valid command. You are missing the ()." << endl;
72 //is it a valid command
73 if (validCommand->isValidCommand(commandName) != true) { return false; }
75 string parameter, value;
76 //reads in parameters and values
77 if((optionText != "") && (commandName != "help")){
78 while((optionText.find_first_of(',') != -1) && (errorFree)) { //while there are parameters
79 globaldata->splitAtComma(value, optionText);
80 globaldata->splitAtEquals(parameter, value);
82 //is it a valid parameter
83 if (validParameter->isValidParameter(parameter) != true) { return false; }
85 if (parameter == "phylip" ) { phylipfile = value; }
86 if (parameter == "column" ) { columnfile = value; }
87 if (parameter == "list" ) { listfile = value; }
88 if (parameter == "rabund" ) { rabundfile = value; }
89 if (parameter == "sabund" ) { sabundfile = value; }
90 if (parameter == "name" ) { namefile = value; }
91 if (parameter == "order" ) { orderfile = value; }
92 if (parameter == "fasta" ) { fastafile = value; }
93 if (parameter == "tree" ) { treefile = value; }
94 if (parameter == "group" ) { groupfile = value; }
95 if (parameter == "cutoff" ) { cutoff = value; }
96 if (parameter == "precision" ) { precision = value; }
97 if (parameter == "iters" ) { iters = value; }
98 if (parameter == "jumble" ) { jumble = value; }
99 if (parameter == "freq" ) { freq = value; }
100 if (parameter == "method" ) { method = value; }
101 if (parameter == "fileroot" ) { fileroot = value; }
102 if (parameter == "line" ) { line = value; }
103 if (parameter == "label" ) { label = value; }
104 if (parameter == "randomtree" ) { randomtree = value; }
106 if (parameter == "single") {//stores estimators in a vector
107 singleEsimators.clear(); //clears out old values
108 globaldata->splitAtDash(value, singleEsimators);
109 for (int i = 0; i < singleEsimators.size(); i++) { //loop through estimators
110 //is it a valid calculator
111 if (validCalculator->isValidCalculator(parameter, singleEsimators[i]) != true) { return false; }
114 if (parameter == "rarefaction") {//stores estimators in a vector
115 rareEstimators.clear(); //clears out old values
116 globaldata->splitAtDash(value, rareEstimators);
117 for (int i = 0; i < rareEstimators.size(); i++) { //loop through estimators
118 //is it a valid calculator
119 if (validCalculator->isValidCalculator(parameter, rareEstimators[i]) != true) { return false; }
122 if (parameter == "shared") {//stores estimators in a vector
123 sharedEstimators.clear(); //clears out old values
124 globaldata->splitAtDash(value, sharedEstimators);
125 for (int i = 0; i < sharedEstimators.size(); i++) { //loop through estimators
126 //is it a valid calculator
127 if (validCalculator->isValidCalculator(parameter, sharedEstimators[i]) != true) { return false; }
130 if (parameter == "summary") { //stores summaries to be used in a vector
131 summaryEstimators.clear(); //clears out old values
132 globaldata->splitAtDash(value, summaryEstimators);
133 for (int i = 0; i < summaryEstimators.size(); i++) { //loop through estimators
134 //is it a valid calculator
135 if (validCalculator->isValidCalculator(parameter, summaryEstimators[i]) != true) { return false; }
138 if (parameter == "sharedrarefaction") { //stores summaries to be used in a vector
139 sharedRareEstimators.clear(); //clears out old values
140 globaldata->splitAtDash(value, sharedRareEstimators);
141 for (int i = 0; i < sharedRareEstimators.size(); i++) { //loop through estimators
142 //is it a valid calculator
143 if (validCalculator->isValidCalculator(parameter, sharedRareEstimators[i]) != true) { return false; }
148 //gets the last parameter and value
149 if (errorFree) { //gets the last parameter and value
151 globaldata->splitAtEquals(parameter, value);
152 //is it a valid parameter
153 if (validParameter->isValidParameter(parameter) != true) { return false; }
155 if (parameter == "phylip" ) { phylipfile = value; }
156 if (parameter == "column" ) { columnfile = value; }
157 if (parameter == "list" ) { listfile = value; }
158 if (parameter == "rabund" ) { rabundfile = value; }
159 if (parameter == "sabund" ) { sabundfile = value; }
160 if (parameter == "name" ) { namefile = value; }
161 if (parameter == "order" ) { orderfile = value; }
162 if (parameter == "group" ) { groupfile = value; }
163 if (parameter == "fasta" ) { fastafile = value; }
164 if (parameter == "tree" ) { treefile = value; }
165 if (parameter == "cutoff" ) { cutoff = value; }
166 if (parameter == "precision" ) { precision = value; }
167 if (parameter == "iters" ) { iters = value; }
168 if (parameter == "jumble" ) { jumble = value; }
169 if (parameter == "freq" ) { freq = value; }
170 if (parameter == "method" ) { method = value; }
171 if (parameter == "fileroot" ) { fileroot = value; }
172 if (parameter == "line" ) { line = value; }
173 if (parameter == "label" ) { label = value; }
174 if (parameter == "randomtree" ) { randomtree = value; }
176 if (parameter == "single") {//stores estimators in a vector
177 singleEsimators.clear(); //clears out old values
178 globaldata->splitAtDash(value, singleEsimators);
179 for (int i = 0; i < singleEsimators.size(); i++) { //loop through estimators
180 //is it a valid calculator
181 if (validCalculator->isValidCalculator(parameter, singleEsimators[i]) != true) { return false; }
184 if (parameter == "rarefaction") {//stores estimators in a vector
185 rareEstimators.clear(); //clears out old values
186 globaldata->splitAtDash(value, rareEstimators);
187 for (int i = 0; i < rareEstimators.size(); i++) { //loop through estimators
188 //is it a valid calculator
189 if (validCalculator->isValidCalculator(parameter, rareEstimators[i]) != true) { return false; }
192 if (parameter == "shared") {//stores estimators in a vector
193 sharedEstimators.clear(); //clears out old values
194 globaldata->splitAtDash(value, sharedEstimators);
195 for (int i = 0; i < sharedEstimators.size(); i++) { //loop through estimators
196 //is it a valid calculator
197 if (validCalculator->isValidCalculator(parameter, sharedEstimators[i]) != true) { return false; }
200 if (parameter == "summary") { //stores summaries to be used in a vector
201 summaryEstimators.clear(); //clears out old values
202 globaldata->splitAtDash(value, summaryEstimators);
203 for (int i = 0; i < summaryEstimators.size(); i++) { //loop through estimators
204 //is it a valid calculator
205 if (validCalculator->isValidCalculator(parameter, summaryEstimators[i]) != true) { return false; }
208 if (parameter == "sharedrarefaction") { //stores summaries to be used in a vector
209 sharedRareEstimators.clear(); //clears out old values
210 globaldata->splitAtDash(value, sharedRareEstimators);
211 for (int i = 0; i < sharedRareEstimators.size(); i++) { //loop through estimators
212 //is it a valid calculator
213 if (validCalculator->isValidCalculator(parameter, sharedRareEstimators[i]) != true) { return false; }
220 //make sure the user does not use both the line and label parameters
221 if ((line != "") && (label != "")) { cout << "You may use either the line or label parameters, but not both." << endl; return false; }
224 if (commandName == "read.dist") {
227 }else if (commandName == "read.otu") {
228 //you want to do shared commands
229 if ((listfile != "") && (groupfile != "")) {
230 validateParseFiles(); //checks the listfile and groupfile parameters
231 }else { //you want to do single commands
235 }else if (commandName == "read.tree") {
236 validateTreeFiles(); //checks the treefile and groupfile parameters
237 }else if (commandName == "deconvolute") {
238 if (fastafile == "") { cout << "You must enter a fastafile with the deconvolute() command." << endl; return false; }
242 //are you trying to cluster before you have read something
243 if ((commandName == "cluster") && (globaldata->getSparseMatrix() == NULL) ||
244 (commandName == "cluster") && (globaldata->getListVector() == NULL)) {
245 cout << "Before you use the cluster command, you first need to read in a distance matrix." << endl;
249 if (commandName == "parsimony") {
250 //are you trying to use parsimony without reading a tree or saying you want random distribution
251 if (randomtree == "0") {
252 if ((globaldata->getTreeFile() == "") || (globaldata->getGroupFile() == "")) {
253 cout << "You must read a treefile and a groupfile or set the randomtree parameter to 1, before you may execute the parsimony command." << endl; return false; }
257 if ((commandName == "unifrac.weighted") || (commandName == "unifrac.unweighted")) {
258 if (globaldata->gTree.size() == 0) {//no trees were read
259 cout << "You must execute the read.tree command, before you may execute the unifrac.weighted or unifrac.unweighted command." << endl; return false; }
262 //check for valid method
263 if (commandName == "cluster") {
264 if ((method == "furthest") || (method == "nearest") || (method == "average")) { }
265 else {cout << "Not a valid clustering method. Valid clustering algorithms are furthest, nearest or average." << endl; return false; }
268 if ((commandName == "collect.single") || (commandName == "rarefaction.single") || (commandName == "summary.single") ){
269 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; }
272 if ((commandName == "collect.shared") || (commandName == "rarefaction.shared") || (commandName == "summary.shared") ){
273 if (globaldata->getListFile() == "") { cout << "You must read a list and a group before you can use the collect.shared, rarefaction.shared or summary.shared commands." << endl; return false; }
274 else if (globaldata->getGroupFile() == "") { cout << "You must read a list and a group before you can use the collect.shared, rarefaction.shared or summary.shared commands." << endl; return false; }
281 /*******************************************************/
283 /******************************************************/
284 //This function checks to make sure the user entered a file to
285 // read and that the file exists and can be opened.
286 void ErrorCheck::validateReadFiles() {
288 //Validating files for read
292 //are we reading a phylipfile
293 if (phylipfile != "") {
294 ableToOpen = openInputFile(phylipfile, filehandle);
297 if (ableToOpen == 1) { errorFree = false; }
298 else { globaldata->inputFileName = phylipfile; }
299 //are we reading a phylipfile
300 }else if (columnfile != "") {
301 ableToOpen = openInputFile(columnfile, filehandle);
304 if (ableToOpen == 1) { errorFree = false; }
305 else { globaldata->inputFileName = columnfile; }
306 //are we reading a listfile
307 }else if (listfile!= "") {
308 ableToOpen = openInputFile(listfile, filehandle);
311 if (ableToOpen == 1) { errorFree = false; }
312 else { globaldata->inputFileName = listfile; }
313 //are we reading a rabundfile
314 }else if (rabundfile != "") {
315 ableToOpen = openInputFile(rabundfile, filehandle);
318 if (ableToOpen == 1) { errorFree = false; }
319 else { globaldata->inputFileName = rabundfile; }
320 //are we reading a sabundfile
321 }else if (sabundfile != "") {
322 ableToOpen = openInputFile(sabundfile, filehandle);
325 if (ableToOpen == 1) { errorFree = false; }
326 else { globaldata->inputFileName = sabundfile; }
327 }else if (fastafile != "") {
328 ableToOpen = openInputFile(fastafile, filehandle);
331 if (ableToOpen == 1) { errorFree = false; }
332 else { globaldata->inputFileName = fastafile; }
333 }else{ //no file given
337 catch(exception& e) {
338 cout << "Standard Error: " << e.what() << " has occurred in the ErrorCheck class Function validateReadFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
342 cout << "An unknown error has occurred in the ErrorCheck class function validateReadFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
347 /*******************************************************/
349 /******************************************************/
350 //This function checks to make sure the user entered appropriate
351 // format parameters on a distfile read
352 void ErrorCheck::validateReadDist() {
357 if ((phylipfile == "") && (columnfile == "")) { cout << "When executing a read.dist you must enter a phylip or a column." << endl; errorFree = false; }
358 else if ((phylipfile != "") && (columnfile != "")) { cout << "When executing a read.dist you must enter ONLY ONE of the following: phylip or column." << endl; errorFree = false; }
360 if (columnfile != "") {
361 if (namefile == "") {
362 cout << "You need to provide a namefile if you are going to use the column format." << endl;
365 ableToOpen = openInputFile(namefile, filehandle);
368 if (ableToOpen == 1) { errorFree = false; }
372 catch(exception& e) {
373 cout << "Standard Error: " << e.what() << " has occurred in the ErrorCheck class Function validateReadDist. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
377 cout << "An unknown error has occurred in the ErrorCheck class function validateReadDist. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
381 /*******************************************************/
383 /******************************************************/
384 //This function checks to make sure the user entered appropriate
385 // format parameters on a parselistcommand
386 void ErrorCheck::validateParseFiles() {
391 //checks for valid files
393 if (listfile == "") { cout << "When executing a read.otu for groups you must enter a list and a group." << endl; errorFree = false; }
394 else if (groupfile == "") { cout << "When executing a read.otu for groups you must enter a list and a group." << endl; errorFree = false; }
396 //checks parameters on the read command
397 if (listfile != "") {
398 ableToOpen = openInputFile(listfile, filehandle);
400 if (ableToOpen == 1) { //unable to open
403 if (groupfile != "") {
404 ableToOpen = openInputFile(groupfile, filehandle);
406 if (ableToOpen == 1) { //unable to open
412 catch(exception& e) {
413 cout << "Standard Error: " << e.what() << " has occurred in the ErrorCheck class Function validateReadPhil. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
417 cout << "An unknown error has occurred in the ErrorCheck class function validateReadPhil. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
421 /*******************************************************/
423 /******************************************************/
424 //This function checks to make sure the user entered appropriate
425 // format parameters on a parselistcommand
426 void ErrorCheck::validateTreeFiles() {
431 //checks for valid files
433 if (treefile == "") { cout << "When executing a read.tree you must enter a treefile and a groupfile." << endl; errorFree = false; }
434 else if (groupfile == "") { cout << "When executing a read.tree you must enter a treefile and a groupfile." << endl; errorFree = false; }
436 //checks parameters on the read command
437 if (treefile != "") {
438 ableToOpen = openInputFile(treefile, filehandle);
440 if (ableToOpen == 1) { //unable to open
443 if (groupfile != "") {
444 ableToOpen = openInputFile(groupfile, filehandle);
446 if (ableToOpen == 1) { //unable to open
452 catch(exception& e) {
453 cout << "Standard Error: " << e.what() << " has occurred in the ErrorCheck class Function validateTreeFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
457 cout << "An unknown error has occurred in the ErrorCheck class function validateTreeFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
462 /*******************************************************/
464 /******************************************************/
465 //This function checks to make sure the user entered appropriate
466 // format parameters on a distfile read
467 void ErrorCheck::validateReadPhil() {
472 //checks to make sure only one file type is given
473 if (listfile != "") {
474 if ((rabundfile != "") || (sabundfile != "")) {
475 cout << "When executing a read.otu you must enter ONLY ONE of the following: list, rabund or sabund." << endl; errorFree = false; }
476 }else if (rabundfile != "") {
477 if ((listfile != "") || (sabundfile != "")) {
478 cout << "When executing a read.otu you must enter ONLY ONE of the following: list, rabund or sabund." << endl; errorFree = false; }
479 }else if (sabundfile != "") {
480 if ((listfile != "") || (rabundfile != "")) {
481 cout << "When executing a read.otu you must enter ONLY ONE of the following: list, rabund or sabund." << endl; errorFree = false; }
482 }else if ((listfile == "") && (rabundfile == "") && (sabundfile == "")) {
483 cout << "When executing a read.otu you must enter one of the following: list, rabund or sabund." << endl; errorFree = false;
486 //checks parameters on the read command
487 if (orderfile != "") {
488 ableToOpen = openInputFile(orderfile, filehandle);
490 if (ableToOpen == 1) { //unable to open
495 catch(exception& e) {
496 cout << "Standard Error: " << e.what() << " has occurred in the ErrorCheck class Function validateReadPhil. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
500 cout << "An unknown error has occurred in the ErrorCheck class function validateReadPhil. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
504 /*******************************************************/
506 /******************************************************/
508 void ErrorCheck::clear() {
509 //option definitions should go here...
522 /*******************************************************/
524 /******************************************************/