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();
21 columnfile = globaldata->getColumnFile();
22 phylipfile = globaldata->getPhylipFile();
23 listfile = globaldata->getListFile();
24 rabundfile = globaldata->getRabundFile();
25 sabundfile = globaldata->getSabundFile();
26 namefile = globaldata->getNameFile();
27 groupfile = globaldata->getGroupFile();
28 orderfile = globaldata->getOrderFile();
29 fastafile = globaldata->getFastaFile();
30 treefile = globaldata->getTreeFile();
31 cutoff = globaldata->getCutOff();
32 format = globaldata->getFormat();
33 method = globaldata->getMethod();
36 /*******************************************************/
38 /******************************************************/
40 ErrorCheck::~ErrorCheck() {}
42 /*******************************************************/
44 /******************************************************/
46 bool ErrorCheck::checkInput(string input) {
50 //get command name and parameters
51 int openParen = input.find_first_of('(');
52 int closeParen = input.find_last_of(')');
54 if(openParen != -1 && closeParen != -1){
55 commandName = input.substr(0, openParen); //commandName contains everything before "("
56 optionText = input.substr(openParen+1, closeParen-openParen-1); //optionString contains everything between "(" and ")".
57 }else if (openParen == -1) { //there is no parenthesis
58 cout << input << " is not a valid command. You are missing the ()." << endl;
62 //is it a valid command
63 if (validCommand->isValidCommand(commandName) != true) { return false; }
65 string parameter, value;
66 //reads in parameters and values
67 if((optionText != "") && (commandName != "help")){
68 while((optionText.find_first_of(',') != -1) && (errorFree)) { //while there are parameters
69 globaldata->splitAtComma(value, optionText);
70 globaldata->splitAtEquals(parameter, value);
72 //is it a valid parameter
73 if (validParameter->isValidParameter(parameter) != true) { return false; }
75 if (parameter == "phylip" ) { phylipfile = value; }
76 if (parameter == "column" ) { columnfile = value; }
77 if (parameter == "list" ) { listfile = value; }
78 if (parameter == "rabund" ) { rabundfile = value; }
79 if (parameter == "sabund" ) { sabundfile = value; }
80 if (parameter == "name" ) { namefile = value; }
81 if (parameter == "order" ) { orderfile = value; }
82 if (parameter == "fasta" ) { fastafile = value; }
83 if (parameter == "treefile" ) { treefile = value; }
84 if (parameter == "group" ) { groupfile = value; }
85 if (parameter == "cutoff" ) { cutoff = value; }
86 if (parameter == "precision" ) { precision = value; }
87 if (parameter == "iters" ) { iters = value; }
88 if (parameter == "jumble" ) { jumble = value; }
89 if (parameter == "freq" ) { freq = value; }
90 if (parameter == "method" ) { method = value; }
91 if (parameter == "fileroot" ) { fileroot = value; }
92 if (parameter == "line" ) { line = value; }
93 if (parameter == "label" ) { label = value; }
95 if (parameter == "single") {//stores estimators in a vector
96 singleEsimators.clear(); //clears out old values
97 globaldata->splitAtDash(value, singleEsimators);
98 for (int i = 0; i < singleEsimators.size(); i++) { //loop through estimators
99 //is it a valid calculator
100 if (validCalculator->isValidCalculator(parameter, singleEsimators[i]) != true) { return false; }
103 if (parameter == "rarefaction") {//stores estimators in a vector
104 rareEstimators.clear(); //clears out old values
105 globaldata->splitAtDash(value, rareEstimators);
106 for (int i = 0; i < rareEstimators.size(); i++) { //loop through estimators
107 //is it a valid calculator
108 if (validCalculator->isValidCalculator(parameter, rareEstimators[i]) != true) { return false; }
111 if (parameter == "shared") {//stores estimators in a vector
112 sharedEstimators.clear(); //clears out old values
113 globaldata->splitAtDash(value, sharedEstimators);
114 for (int i = 0; i < sharedEstimators.size(); i++) { //loop through estimators
115 //is it a valid calculator
116 if (validCalculator->isValidCalculator(parameter, sharedEstimators[i]) != true) { return false; }
119 if (parameter == "summary") { //stores summaries to be used in a vector
120 summaryEstimators.clear(); //clears out old values
121 globaldata->splitAtDash(value, summaryEstimators);
122 for (int i = 0; i < summaryEstimators.size(); i++) { //loop through estimators
123 //is it a valid calculator
124 if (validCalculator->isValidCalculator(parameter, summaryEstimators[i]) != true) { return false; }
127 if (parameter == "sharedrarefaction") { //stores summaries to be used in a vector
128 sharedRareEstimators.clear(); //clears out old values
129 globaldata->splitAtDash(value, sharedRareEstimators);
130 for (int i = 0; i < sharedRareEstimators.size(); i++) { //loop through estimators
131 //is it a valid calculator
132 if (validCalculator->isValidCalculator(parameter, sharedRareEstimators[i]) != true) { return false; }
137 //gets the last parameter and value
138 if (errorFree) { //gets the last parameter and value
140 globaldata->splitAtEquals(parameter, value);
141 //is it a valid parameter
142 if (validParameter->isValidParameter(parameter) != true) { return false; }
144 if (parameter == "phylip" ) { phylipfile = value; }
145 if (parameter == "column" ) { columnfile = value; }
146 if (parameter == "list" ) { listfile = value; }
147 if (parameter == "rabund" ) { rabundfile = value; }
148 if (parameter == "sabund" ) { sabundfile = value; }
149 if (parameter == "name" ) { namefile = value; }
150 if (parameter == "order" ) { orderfile = value; }
151 if (parameter == "group" ) { groupfile = value; }
152 if (parameter == "fasta" ) { fastafile = value; }
153 if (parameter == "treefile" ) { treefile = value; }
154 if (parameter == "cutoff" ) { cutoff = value; }
155 if (parameter == "precision" ) { precision = value; }
156 if (parameter == "iters" ) { iters = value; }
157 if (parameter == "jumble" ) { jumble = value; }
158 if (parameter == "freq" ) { freq = value; }
159 if (parameter == "method" ) { method = value; }
160 if (parameter == "fileroot" ) { fileroot = value; }
161 if (parameter == "line" ) { line = value; }
162 if (parameter == "label" ) { label = value; }
164 if (parameter == "single") {//stores estimators in a vector
165 singleEsimators.clear(); //clears out old values
166 globaldata->splitAtDash(value, singleEsimators);
167 for (int i = 0; i < singleEsimators.size(); i++) { //loop through estimators
168 //is it a valid calculator
169 if (validCalculator->isValidCalculator(parameter, singleEsimators[i]) != true) { return false; }
172 if (parameter == "rarefaction") {//stores estimators in a vector
173 rareEstimators.clear(); //clears out old values
174 globaldata->splitAtDash(value, rareEstimators);
175 for (int i = 0; i < rareEstimators.size(); i++) { //loop through estimators
176 //is it a valid calculator
177 if (validCalculator->isValidCalculator(parameter, rareEstimators[i]) != true) { return false; }
180 if (parameter == "shared") {//stores estimators in a vector
181 sharedEstimators.clear(); //clears out old values
182 globaldata->splitAtDash(value, sharedEstimators);
183 for (int i = 0; i < sharedEstimators.size(); i++) { //loop through estimators
184 //is it a valid calculator
185 if (validCalculator->isValidCalculator(parameter, sharedEstimators[i]) != true) { return false; }
188 if (parameter == "summary") { //stores summaries to be used in a vector
189 summaryEstimators.clear(); //clears out old values
190 globaldata->splitAtDash(value, summaryEstimators);
191 for (int i = 0; i < summaryEstimators.size(); i++) { //loop through estimators
192 //is it a valid calculator
193 if (validCalculator->isValidCalculator(parameter, summaryEstimators[i]) != true) { return false; }
196 if (parameter == "sharedrarefaction") { //stores summaries to be used in a vector
197 sharedRareEstimators.clear(); //clears out old values
198 globaldata->splitAtDash(value, sharedRareEstimators);
199 for (int i = 0; i < sharedRareEstimators.size(); i++) { //loop through estimators
200 //is it a valid calculator
201 if (validCalculator->isValidCalculator(parameter, sharedRareEstimators[i]) != true) { return false; }
208 //make sure the user does not use both the line and label parameters
209 if ((line != "") && (label != "")) { cout << "You may use either the line or label parameters, but not both." << endl; return false; }
212 if (commandName == "read.dist") {
215 }else if (commandName == "read.otu") {
216 //you want to do shared commands
217 if ((listfile != "") && (groupfile != "")) {
218 validateParseFiles(); //checks the listfile and groupfile parameters
219 }else { //you want to do single commands
223 }else if (commandName == "read.tree") {
224 validateTreeFiles(); //checks the treefile and groupfile parameters
225 }else if (commandName == "deconvolute") {
226 if (fastafile == "") { cout << "You must enter a fastafile with the deconvolute() command." << endl; return false; }
230 //are you trying to cluster before you have read something
231 if ((commandName == "cluster") && (globaldata->getSparseMatrix() == NULL) ||
232 (commandName == "cluster") && (globaldata->getListVector() == NULL)) {
233 cout << "Before you use the cluster command, you first need to read in a distance matrix." << endl;
237 //check for valid method
238 if (commandName == "cluster") {
239 if ((method == "furthest") || (method == "nearest") || (method == "average")) { }
240 else {cout << "Not a valid clustering method. Valid clustering algorithms are furthest, nearest or average." << endl; return false; }
243 if ((commandName == "collect.single") || (commandName == "rarefaction.single") || (commandName == "summary.single") ){
244 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; }
247 if ((commandName == "collect.shared") || (commandName == "rarefaction.shared") || (commandName == "summary.shared") ){
248 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; }
249 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; }
256 /*******************************************************/
258 /******************************************************/
259 //This function checks to make sure the user entered a file to
260 // read and that the file exists and can be opened.
261 void ErrorCheck::validateReadFiles() {
263 //Validating files for read
267 //are we reading a phylipfile
268 if (phylipfile != "") {
269 ableToOpen = openInputFile(phylipfile, filehandle);
272 if (ableToOpen == 1) { errorFree = false; }
273 else { globaldata->inputFileName = phylipfile; }
274 //are we reading a phylipfile
275 }else if (columnfile != "") {
276 ableToOpen = openInputFile(columnfile, filehandle);
279 if (ableToOpen == 1) { errorFree = false; }
280 else { globaldata->inputFileName = columnfile; }
281 //are we reading a listfile
282 }else if (listfile!= "") {
283 ableToOpen = openInputFile(listfile, filehandle);
286 if (ableToOpen == 1) { errorFree = false; }
287 else { globaldata->inputFileName = listfile; }
288 //are we reading a rabundfile
289 }else if (rabundfile != "") {
290 ableToOpen = openInputFile(rabundfile, filehandle);
293 if (ableToOpen == 1) { errorFree = false; }
294 else { globaldata->inputFileName = rabundfile; }
295 //are we reading a sabundfile
296 }else if (sabundfile != "") {
297 ableToOpen = openInputFile(sabundfile, filehandle);
300 if (ableToOpen == 1) { errorFree = false; }
301 else { globaldata->inputFileName = sabundfile; }
302 }else if (fastafile != "") {
303 ableToOpen = openInputFile(fastafile, filehandle);
306 if (ableToOpen == 1) { errorFree = false; }
307 else { globaldata->inputFileName = fastafile; }
308 }else{ //no file given
312 catch(exception& e) {
313 cout << "Standard Error: " << e.what() << " has occurred in the ErrorCheck class Function validateReadFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
317 cout << "An unknown error has occurred in the ErrorCheck class function validateReadFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
322 /*******************************************************/
324 /******************************************************/
325 //This function checks to make sure the user entered appropriate
326 // format parameters on a distfile read
327 void ErrorCheck::validateReadDist() {
332 if ((phylipfile == "") && (columnfile == "")) { cout << "When executing a read.dist you must enter a phylip or a column." << endl; errorFree = false; }
333 else if ((phylipfile != "") && (columnfile != "")) { cout << "When executing a read.dist you must enter ONLY ONE of the following: phylip or column." << endl; errorFree = false; }
335 if (columnfile != "") {
336 if (namefile == "") {
337 cout << "You need to provide a namefile if you are going to use the column format." << endl;
340 ableToOpen = openInputFile(namefile, filehandle);
343 if (ableToOpen == 1) { errorFree = false; }
347 catch(exception& e) {
348 cout << "Standard Error: " << e.what() << " has occurred in the ErrorCheck class Function validateReadDist. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
352 cout << "An unknown error has occurred in the ErrorCheck class function validateReadDist. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
356 /*******************************************************/
358 /******************************************************/
359 //This function checks to make sure the user entered appropriate
360 // format parameters on a parselistcommand
361 void ErrorCheck::validateParseFiles() {
366 //checks for valid files
368 if (listfile == "") { cout << "When executing a read.otu for groups you must enter a list and a group." << endl; errorFree = false; }
369 else if (groupfile == "") { cout << "When executing a read.otu for groups you must enter a list and a group." << endl; errorFree = false; }
371 //checks parameters on the read command
372 if (listfile != "") {
373 ableToOpen = openInputFile(listfile, filehandle);
375 if (ableToOpen == 1) { //unable to open
378 if (groupfile != "") {
379 ableToOpen = openInputFile(groupfile, filehandle);
381 if (ableToOpen == 1) { //unable to open
387 catch(exception& e) {
388 cout << "Standard Error: " << e.what() << " has occurred in the ErrorCheck class Function validateReadPhil. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
392 cout << "An unknown error has occurred in the ErrorCheck class function validateReadPhil. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
396 /*******************************************************/
398 /******************************************************/
399 //This function checks to make sure the user entered appropriate
400 // format parameters on a parselistcommand
401 void ErrorCheck::validateTreeFiles() {
406 //checks for valid files
408 if (treefile == "") { cout << "When executing a read.tree you must enter a treefile and a groupfile." << endl; errorFree = false; }
409 else if (groupfile == "") { cout << "When executing a read.tree you must enter a treefile and a groupfile." << endl; errorFree = false; }
411 //checks parameters on the read command
412 if (treefile != "") {
413 ableToOpen = openInputFile(treefile, filehandle);
415 if (ableToOpen == 1) { //unable to open
418 if (groupfile != "") {
419 ableToOpen = openInputFile(groupfile, filehandle);
421 if (ableToOpen == 1) { //unable to open
427 catch(exception& e) {
428 cout << "Standard Error: " << e.what() << " has occurred in the ErrorCheck class Function validateTreeFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
432 cout << "An unknown error has occurred in the ErrorCheck class function validateTreeFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
437 /*******************************************************/
439 /******************************************************/
440 //This function checks to make sure the user entered appropriate
441 // format parameters on a distfile read
442 void ErrorCheck::validateReadPhil() {
447 //checks to make sure only one file type is given
448 if (listfile != "") {
449 if ((rabundfile != "") || (sabundfile != "")) {
450 cout << "When executing a read.otu you must enter ONLY ONE of the following: list, rabund or sabund." << endl; errorFree = false; }
451 }else if (rabundfile != "") {
452 if ((listfile != "") || (sabundfile != "")) {
453 cout << "When executing a read.otu you must enter ONLY ONE of the following: list, rabund or sabund." << endl; errorFree = false; }
454 }else if (sabundfile != "") {
455 if ((listfile != "") || (rabundfile != "")) {
456 cout << "When executing a read.otu you must enter ONLY ONE of the following: list, rabund or sabund." << endl; errorFree = false; }
457 }else if ((listfile == "") && (rabundfile == "") && (sabundfile == "")) {
458 cout << "When executing a read.otu you must enter one of the following: list, rabund or sabund." << endl; errorFree = false;
461 //checks parameters on the read command
462 if (orderfile != "") {
463 ableToOpen = openInputFile(orderfile, filehandle);
465 if (ableToOpen == 1) { //unable to open
470 catch(exception& e) {
471 cout << "Standard Error: " << e.what() << " has occurred in the ErrorCheck class Function validateReadPhil. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
475 cout << "An unknown error has occurred in the ErrorCheck class function validateReadPhil. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
479 /*******************************************************/
481 /******************************************************/
483 void ErrorCheck::clear() {
484 //option definitions should go here...
497 /*******************************************************/
499 /******************************************************/