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();
21 /*******************************************************/
23 /******************************************************/
25 void ErrorCheck::refresh() {
26 columnfile = globaldata->getColumnFile();
27 phylipfile = globaldata->getPhylipFile();
28 listfile = globaldata->getListFile();
29 rabundfile = globaldata->getRabundFile();
30 sabundfile = globaldata->getSabundFile();
31 namefile = globaldata->getNameFile();
32 groupfile = globaldata->getGroupFile();
33 orderfile = globaldata->getOrderFile();
34 fastafile = globaldata->getFastaFile();
35 treefile = globaldata->getTreeFile();
36 cutoff = globaldata->getCutOff();
37 format = globaldata->getFormat();
38 method = globaldata->getMethod();
39 randomtree = globaldata->getRandomTree();
40 sharedfile = globaldata->getSharedFile();
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 splitAtComma(value, optionText);
80 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 == "shared" ) { sharedfile = value; }
96 if (parameter == "cutoff" ) { cutoff = value; }
97 if (parameter == "precision" ) { precision = value; }
98 if (parameter == "iters" ) { iters = value; }
99 if (parameter == "jumble" ) { jumble = value; }
100 if (parameter == "freq" ) { freq = value; }
101 if (parameter == "method" ) { method = value; }
102 if (parameter == "fileroot" ) { fileroot = value; }
103 if (parameter == "line" ) { line = value; }
104 if (parameter == "label" ) { label = value; }
105 if (parameter == "randomtree" ) { randomtree = value; }
109 //gets the last parameter and value
110 if (errorFree) { //gets the last parameter and value
112 splitAtEquals(parameter, value);
113 //is it a valid parameter
114 if (validParameter->isValidParameter(parameter) != true) { return false; }
116 if (parameter == "phylip" ) { phylipfile = value; }
117 if (parameter == "column" ) { columnfile = value; }
118 if (parameter == "list" ) { listfile = value; }
119 if (parameter == "rabund" ) { rabundfile = value; }
120 if (parameter == "sabund" ) { sabundfile = value; }
121 if (parameter == "name" ) { namefile = value; }
122 if (parameter == "order" ) { orderfile = value; }
123 if (parameter == "group" ) { groupfile = value; }
124 if (parameter == "shared" ) { sharedfile = value; }
125 if (parameter == "fasta" ) { fastafile = value; }
126 if (parameter == "tree" ) { treefile = value; }
127 if (parameter == "cutoff" ) { cutoff = value; }
128 if (parameter == "precision" ) { precision = value; }
129 if (parameter == "iters" ) { iters = value; }
130 if (parameter == "jumble" ) { jumble = value; }
131 if (parameter == "freq" ) { freq = value; }
132 if (parameter == "method" ) { method = value; }
133 if (parameter == "fileroot" ) { fileroot = value; }
134 if (parameter == "line" ) { line = value; }
135 if (parameter == "label" ) { label = value; }
136 if (parameter == "randomtree" ) { randomtree = value; }
141 //make sure the user does not use both the line and label parameters
142 if ((line != "") && (label != "")) { cout << "You may use either the line or label parameters, but not both." << endl; return false; }
145 if (commandName == "read.dist") {
148 }else if (commandName == "read.otu") {
149 //you want to do shared commands
150 if ((listfile != "") && (groupfile != "")) {
151 validateParseFiles(); //checks the listfile and groupfile parameters
152 }else { //you want to do single commands
156 }else if (commandName == "read.shared") {
157 //you want to do shared commands with just the shared file
159 }else if (commandName == "read.tree") {
160 validateTreeFiles(); //checks the treefile and groupfile parameters
161 }else if (commandName == "deconvolute") {
162 if (fastafile == "") { cout << "You must enter a fastafile with the deconvolute() command." << endl; return false; }
166 //are you trying to cluster before you have read something
167 if ((commandName == "cluster") && (globaldata->getSparseMatrix() == NULL) ||
168 (commandName == "cluster") && (globaldata->getListVector() == NULL)) {
169 cout << "Before you use the cluster command, you first need to read in a distance matrix." << endl;
173 if (commandName == "parsimony") {
174 //are you trying to use parsimony without reading a tree or saying you want random distribution
175 if (randomtree == "0") {
176 if ((globaldata->getTreeFile() == "") || (globaldata->getGroupFile() == "")) {
177 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; }
181 if ((commandName == "unifrac.weighted") || (commandName == "unifrac.unweighted")) {
182 if (globaldata->gTree.size() == 0) {//no trees were read
183 cout << "You must execute the read.tree command, before you may execute the unifrac.weighted or unifrac.unweighted command." << endl; return false; }
186 //check for valid method
187 if (commandName == "cluster") {
188 if ((method == "furthest") || (method == "nearest") || (method == "average")) { }
189 else {cout << "Not a valid clustering method. Valid clustering algorithms are furthest, nearest or average." << endl; return false; }
192 if ((commandName == "collect.single") || (commandName == "rarefaction.single") || (commandName == "summary.single") ){
193 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; }
196 if ((commandName == "collect.shared") || (commandName == "rarefaction.shared") || (commandName == "summary.shared") ){
197 if (globaldata->getSharedFile() == "") {
198 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; }
199 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; }
206 /*******************************************************/
208 /******************************************************/
209 //This function checks to make sure the user entered a file to
210 // read and that the file exists and can be opened.
211 void ErrorCheck::validateReadFiles() {
213 //Validating files for read
217 //are we reading a phylipfile
218 if (phylipfile != "") {
219 ableToOpen = openInputFile(phylipfile, filehandle);
222 if (ableToOpen == 1) { errorFree = false; }
223 else { globaldata->inputFileName = phylipfile; }
224 //are we reading a phylipfile
225 }else if (columnfile != "") {
226 ableToOpen = openInputFile(columnfile, filehandle);
229 if (ableToOpen == 1) { errorFree = false; }
230 else { globaldata->inputFileName = columnfile; }
231 //are we reading a listfile
232 }else if (listfile!= "") {
233 ableToOpen = openInputFile(listfile, filehandle);
236 if (ableToOpen == 1) { errorFree = false; }
237 else { globaldata->inputFileName = listfile; }
238 //are we reading a rabundfile
239 }else if (rabundfile != "") {
240 ableToOpen = openInputFile(rabundfile, filehandle);
243 if (ableToOpen == 1) { errorFree = false; }
244 else { globaldata->inputFileName = rabundfile; }
245 //are we reading a sabundfile
246 }else if (sabundfile != "") {
247 ableToOpen = openInputFile(sabundfile, filehandle);
250 if (ableToOpen == 1) { errorFree = false; }
251 else { globaldata->inputFileName = sabundfile; }
252 }else if (fastafile != "") {
253 ableToOpen = openInputFile(fastafile, filehandle);
256 if (ableToOpen == 1) { errorFree = false; }
257 else { globaldata->inputFileName = fastafile; }
258 }else if (sharedfile != "") {
259 ableToOpen = openInputFile(sharedfile, filehandle);
262 if (ableToOpen == 1) { errorFree = false; }
263 else { globaldata->inputFileName = sharedfile; }
264 }else{ //no file given
268 catch(exception& e) {
269 cout << "Standard Error: " << e.what() << " has occurred in the ErrorCheck class Function validateReadFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
273 cout << "An unknown error has occurred in the ErrorCheck class function validateReadFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
278 /*******************************************************/
280 /******************************************************/
281 //This function checks to make sure the user entered appropriate
282 // format parameters on a distfile read
283 void ErrorCheck::validateReadDist() {
288 if ((phylipfile == "") && (columnfile == "")) { cout << "When executing a read.dist you must enter a phylip or a column." << endl; errorFree = false; }
289 else if ((phylipfile != "") && (columnfile != "")) { cout << "When executing a read.dist you must enter ONLY ONE of the following: phylip or column." << endl; errorFree = false; }
291 if (columnfile != "") {
292 if (namefile == "") {
293 cout << "You need to provide a namefile if you are going to use the column format." << endl;
296 ableToOpen = openInputFile(namefile, filehandle);
299 if (ableToOpen == 1) { errorFree = false; }
303 catch(exception& e) {
304 cout << "Standard Error: " << e.what() << " has occurred in the ErrorCheck class Function validateReadDist. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
308 cout << "An unknown error has occurred in the ErrorCheck class function validateReadDist. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
312 /*******************************************************/
314 /******************************************************/
315 //This function checks to make sure the user entered appropriate
316 // format parameters on a parselistcommand
317 void ErrorCheck::validateParseFiles() {
322 //checks for valid files
324 if (listfile == "") { cout << "When executing a read.otu for groups you must enter a list and a group." << endl; errorFree = false; }
325 else if (groupfile == "") { cout << "When executing a read.otu for groups you must enter a list and a group." << endl; errorFree = false; }
327 //checks parameters on the read command
328 if (listfile != "") {
329 ableToOpen = openInputFile(listfile, filehandle);
331 if (ableToOpen == 1) { //unable to open
334 if (groupfile != "") {
335 ableToOpen = openInputFile(groupfile, filehandle);
337 if (ableToOpen == 1) { //unable to open
343 catch(exception& e) {
344 cout << "Standard Error: " << e.what() << " has occurred in the ErrorCheck class Function validateReadPhil. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
348 cout << "An unknown error has occurred in the ErrorCheck class function validateReadPhil. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
352 /*******************************************************/
354 /******************************************************/
355 //This function checks to make sure the user entered appropriate
356 // format parameters on a parselistcommand
357 void ErrorCheck::validateTreeFiles() {
362 //checks for valid files
364 if (treefile == "") { cout << "When executing a read.tree you must enter a treefile and a groupfile." << endl; errorFree = false; }
365 else if (groupfile == "") { cout << "When executing a read.tree you must enter a treefile and a groupfile." << endl; errorFree = false; }
367 //checks parameters on the read command
368 if (treefile != "") {
369 ableToOpen = openInputFile(treefile, filehandle);
371 if (ableToOpen == 1) { //unable to open
374 if (groupfile != "") {
375 ableToOpen = openInputFile(groupfile, filehandle);
377 if (ableToOpen == 1) { //unable to open
383 catch(exception& e) {
384 cout << "Standard Error: " << e.what() << " has occurred in the ErrorCheck class Function validateTreeFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
388 cout << "An unknown error has occurred in the ErrorCheck class function validateTreeFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
393 /*******************************************************/
395 /******************************************************/
396 //This function checks to make sure the user entered appropriate
397 // format parameters on a distfile read
398 void ErrorCheck::validateReadPhil() {
403 //checks to make sure only one file type is given
404 if (listfile != "") {
405 if ((rabundfile != "") || (sabundfile != "")) {
406 cout << "When executing a read.otu you must enter ONLY ONE of the following: list, rabund or sabund." << endl; errorFree = false; }
407 }else if (rabundfile != "") {
408 if ((listfile != "") || (sabundfile != "")) {
409 cout << "When executing a read.otu you must enter ONLY ONE of the following: list, rabund or sabund." << endl; errorFree = false; }
410 }else if (sabundfile != "") {
411 if ((listfile != "") || (rabundfile != "")) {
412 cout << "When executing a read.otu you must enter ONLY ONE of the following: list, rabund or sabund." << endl; errorFree = false; }
413 }else if ((listfile == "") && (rabundfile == "") && (sabundfile == "")) {
414 cout << "When executing a read.otu you must enter one of the following: list, rabund or sabund." << endl; errorFree = false;
417 //checks parameters on the read command
418 if (orderfile != "") {
419 ableToOpen = openInputFile(orderfile, filehandle);
421 if (ableToOpen == 1) { //unable to open
426 catch(exception& e) {
427 cout << "Standard Error: " << e.what() << " has occurred in the ErrorCheck class Function validateReadPhil. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
431 cout << "An unknown error has occurred in the ErrorCheck class function validateReadPhil. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
435 /*******************************************************/
437 /******************************************************/
439 void ErrorCheck::clear() {
440 //option definitions should go here...
454 /*******************************************************/
456 /******************************************************/