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") {
229 //are you trying to cluster before you have read something
230 if ((commandName == "cluster") && (globaldata->getSparseMatrix() == NULL) ||
231 (commandName == "cluster") && (globaldata->getListVector() == NULL)) {
232 cout << "Before you use the cluster command, you first need to read in a distance matrix." << endl;
236 //check for valid method
237 if (commandName == "cluster") {
238 if ((method == "furthest") || (method == "nearest") || (method == "average")) { }
239 else {cout << "Not a valid clustering method. Valid clustering algorithms are furthest, nearest or average." << endl; return false; }
242 if ((commandName == "collect.single") || (commandName == "rarefaction.single") || (commandName == "summary.single") ){
243 if ((globaldata->getListFile() == "") && (globaldata->getRabundFile() == "") && (globaldata->getSabundFile() == "")) { cout << "You must read a listfile, sabundfile or rabundfile before you can use the collect.single, rarefaction.single or summary.single commands." << endl; return false; }
246 if ((commandName == "collect.shared") || (commandName == "rarefaction.shared") || (commandName == "summary.shared") || (commandName == "shared") ){
247 if (globaldata->getListFile() == "") { cout << "You must read a listfile and a groupfile before you can use the collect.shared, rarefaction.shared, summary.shared or shared commands." << endl; return false; }
248 else if (globaldata->getGroupFile() == "") { cout << "You must read a listfile and a groupfile before you can use the collect.shared, rarefaction.shared, summary.shared or shared commands." << endl; return false; }
255 /*******************************************************/
257 /******************************************************/
258 //This function checks to make sure the user entered a file to
259 // read and that the file exists and can be opened.
260 void ErrorCheck::validateReadFiles() {
262 //Validating files for read
266 //are we reading a phylipfile
267 if (phylipfile != "") {
268 ableToOpen = openInputFile(phylipfile, filehandle);
271 if (ableToOpen == 1) { errorFree = false; }
272 else { globaldata->inputFileName = phylipfile; }
273 //are we reading a phylipfile
274 }else if (columnfile != "") {
275 ableToOpen = openInputFile(columnfile, filehandle);
278 if (ableToOpen == 1) { errorFree = false; }
279 else { globaldata->inputFileName = columnfile; }
280 //are we reading a listfile
281 }else if (listfile!= "") {
282 ableToOpen = openInputFile(listfile, filehandle);
285 if (ableToOpen == 1) { errorFree = false; }
286 else { globaldata->inputFileName = listfile; }
287 //are we reading a rabundfile
288 }else if (rabundfile != "") {
289 ableToOpen = openInputFile(rabundfile, filehandle);
292 if (ableToOpen == 1) { errorFree = false; }
293 else { globaldata->inputFileName = rabundfile; }
294 //are we reading a sabundfile
295 }else if (sabundfile != "") {
296 ableToOpen = openInputFile(sabundfile, filehandle);
299 if (ableToOpen == 1) { errorFree = false; }
300 else { globaldata->inputFileName = sabundfile; }
301 }else if (fastafile != "") {
302 ableToOpen = openInputFile(fastafile, filehandle);
305 if (ableToOpen == 1) { errorFree = false; }
306 else { globaldata->inputFileName = fastafile; }
307 }else{ //no file given
311 catch(exception& e) {
312 cout << "Standard Error: " << e.what() << " has occurred in the ErrorCheck class Function validateReadFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
316 cout << "An unknown error has occurred in the ErrorCheck class function validateReadFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
321 /*******************************************************/
323 /******************************************************/
324 //This function checks to make sure the user entered appropriate
325 // format parameters on a distfile read
326 void ErrorCheck::validateReadDist() {
331 if ((phylipfile == "") && (columnfile == "")) { cout << "When executing a read.dist you must enter a phylipfile or a columnfile." << endl; errorFree = false; }
332 else if ((phylipfile != "") && (columnfile != "")) { cout << "When executing a read.dist you must enter ONLY ONE of the following: phylipfile or columnfile." << endl; errorFree = false; }
334 if (columnfile != "") {
335 if (namefile == "") {
336 cout << "You need to provide a namefile name if you are going to use the column format." << endl;
339 ableToOpen = openInputFile(namefile, filehandle);
342 if (ableToOpen == 1) { errorFree = false; }
346 catch(exception& e) {
347 cout << "Standard Error: " << e.what() << " has occurred in the ErrorCheck class Function validateReadDist. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
351 cout << "An unknown error has occurred in the ErrorCheck class function validateReadDist. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
355 /*******************************************************/
357 /******************************************************/
358 //This function checks to make sure the user entered appropriate
359 // format parameters on a parselistcommand
360 void ErrorCheck::validateParseFiles() {
365 //checks for valid files
367 if (listfile == "") { cout << "When executing a read.list you must enter a listfile and a groupfile." << endl; errorFree = false; }
368 else if (groupfile == "") { cout << "When executing a read.list you must enter a listfile and a groupfile." << endl; errorFree = false; }
370 //checks parameters on the read command
371 if (listfile != "") {
372 ableToOpen = openInputFile(listfile, filehandle);
374 if (ableToOpen == 1) { //unable to open
377 if (groupfile != "") {
378 ableToOpen = openInputFile(groupfile, filehandle);
380 if (ableToOpen == 1) { //unable to open
386 catch(exception& e) {
387 cout << "Standard Error: " << e.what() << " has occurred in the ErrorCheck class Function validateReadPhil. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
391 cout << "An unknown error has occurred in the ErrorCheck class function validateReadPhil. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
395 /*******************************************************/
397 /******************************************************/
398 //This function checks to make sure the user entered appropriate
399 // format parameters on a parselistcommand
400 void ErrorCheck::validateTreeFiles() {
405 //checks for valid files
407 if (treefile == "") { cout << "When executing a read.tree you must enter a treefile and a groupfile." << endl; errorFree = false; }
408 else if (groupfile == "") { cout << "When executing a read.tree you must enter a treefile and a groupfile." << endl; errorFree = false; }
410 //checks parameters on the read command
411 if (treefile != "") {
412 ableToOpen = openInputFile(treefile, filehandle);
414 if (ableToOpen == 1) { //unable to open
417 if (groupfile != "") {
418 ableToOpen = openInputFile(groupfile, filehandle);
420 if (ableToOpen == 1) { //unable to open
426 catch(exception& e) {
427 cout << "Standard Error: " << e.what() << " has occurred in the ErrorCheck class Function validateTreeFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
431 cout << "An unknown error has occurred in the ErrorCheck class function validateTreeFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
436 /*******************************************************/
438 /******************************************************/
439 //This function checks to make sure the user entered appropriate
440 // format parameters on a distfile read
441 void ErrorCheck::validateReadPhil() {
446 //checks to make sure only one file type is given
447 if (listfile != "") {
448 if ((rabundfile != "") || (sabundfile != "")) {
449 cout << "When executing a read.otu you must enter ONLY ONE of the following: listfile, rabundfile or sabundfile." << endl; errorFree = false; }
450 }else if (rabundfile != "") {
451 if ((listfile != "") || (sabundfile != "")) {
452 cout << "When executing a read.otu you must enter ONLY ONE of the following: listfile, rabundfile or sabundfile." << endl; errorFree = false; }
453 }else if (sabundfile != "") {
454 if ((listfile != "") || (rabundfile != "")) {
455 cout << "When executing a read.otu you must enter ONLY ONE of the following: listfile, rabundfile or sabundfile." << endl; errorFree = false; }
456 }else if ((listfile == "") && (rabundfile == "") && (sabundfile == "")) {
457 cout << "When executing a read.otu you must enter one of the following: listfile, rabundfile or sabundfile." << endl; errorFree = false;
460 //checks parameters on the read command
461 if (orderfile != "") {
462 ableToOpen = openInputFile(orderfile, filehandle);
464 if (ableToOpen == 1) { //unable to open
469 catch(exception& e) {
470 cout << "Standard Error: " << e.what() << " has occurred in the ErrorCheck class Function validateReadPhil. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
474 cout << "An unknown error has occurred in the ErrorCheck class function validateReadPhil. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
478 /*******************************************************/
480 /******************************************************/
482 void ErrorCheck::clear() {
483 //option definitions should go here...
496 /*******************************************************/
498 /******************************************************/