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() {
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();
41 //sharedfile = globaldata->getSharedFile();
45 /*******************************************************/
47 /******************************************************/
49 ErrorCheck::~ErrorCheck() {
51 delete validParameter;
54 /*******************************************************/
56 /******************************************************/
58 bool ErrorCheck::checkInput(string input) {
65 //get command name and parameters
66 int openParen = input.find_first_of('(');
67 int closeParen = input.find_last_of(')');
69 if(openParen != -1 && closeParen != -1){
70 commandName = input.substr(0, openParen); //commandName contains everything before "("
71 optionText = input.substr(openParen+1, closeParen-openParen-1); //optionString contains everything between "(" and ")".
72 }else if (openParen == -1) { //there is no parenthesis
73 cout << input << " is not a valid command. You are missing the ()." << endl;
77 //is it a valid command
78 if (validCommand->isValidCommand(commandName) != true) { return false; }
79 string parameter, value;
81 //reads in parameters and values
82 if((optionText != "") && (commandName != "help")){
83 while((optionText.find_first_of(',') != -1) && (errorFree)) { //while there are parameters
84 splitAtComma(value, optionText);
85 splitAtEquals(parameter, value);
87 //is it a valid parameter
88 if (validParameter->isValidParameter(parameter, commandName, value) != true) { return false; }
90 if (parameter == "phylip" ) { phylipfile = value; }
91 if (parameter == "column" ) { columnfile = value; }
92 if (parameter == "list" ) { listfile = value; }
93 if (parameter == "rabund" ) { rabundfile = value; }
94 if (parameter == "sabund" ) { sabundfile = value; }
95 if (parameter == "name" ) { namefile = value; }
96 if (parameter == "order" ) { orderfile = value; }
97 if (parameter == "fasta" ) { fastafile = value; }
98 if (parameter == "tree" ) { treefile = value; }
99 if (parameter == "group" ) { groupfile = value; }
100 if (parameter == "shared" ) { sharedfile = value; }
101 if (parameter == "cutoff" ) { cutoff = value; }
102 if (parameter == "precision" ) { precision = value; }
103 if (parameter == "iters" ) { iters = value; }
104 if (parameter == "jumble" ) { jumble = value; }
105 if (parameter == "freq" ) { freq = value; }
106 if (parameter == "method" ) { method = value; }
107 if (parameter == "fileroot" ) { fileroot = value; }
108 if (parameter == "line" ) { line = value; }
109 if (parameter == "label" ) { label = value; }
110 if (parameter == "abund" ) { abund = value; }
111 if (parameter == "random" ) { randomtree = value; }
112 if (parameter == "sorted" ) { sorted = value; }
113 if (parameter == "trump" ) { trump = value; }
114 if (parameter == "soft" ) { soft = value; }
115 if (parameter == "filter" ) { filter = value; }
116 if (parameter == "scale" ) { scale = value; }
117 if (parameter == "countends" ) { countends = value; }
118 if (parameter == "processors" ) { processors = value; }
119 if (parameter == "size" ) { size = value; }
120 if (parameter == "candidate") { candidatefile = value; }
121 if (parameter == "search") { search = value; }
122 if (parameter == "ksize") { ksize = value; }
123 if (parameter == "align") { align = value; }
124 if (parameter == "match") { match = value; }
125 if (parameter == "mismatch") { mismatch = value; }
126 if (parameter == "gapopen") { gapopen = value; }
127 if (parameter == "gapextend" ) { gapextend = value; }
130 //gets the last parameter and value
131 if (errorFree) { //gets the last parameter and value
133 splitAtEquals(parameter, value);
134 //is it a valid parameter
135 if (validParameter->isValidParameter(parameter, commandName, value) != true) { return false; }
138 if (parameter == "phylip" ) { phylipfile = value; }
139 if (parameter == "column" ) { columnfile = value; }
140 if (parameter == "list" ) { listfile = value; }
141 if (parameter == "rabund" ) { rabundfile = value; }
142 if (parameter == "sabund" ) { sabundfile = value; }
143 if (parameter == "name" ) { namefile = value; }
144 if (parameter == "order" ) { orderfile = value; }
145 if (parameter == "group" ) { groupfile = value; }
146 if (parameter == "shared" ) { sharedfile = value; }
147 if (parameter == "fasta" ) { fastafile = value; }
148 if (parameter == "tree" ) { treefile = value; }
149 if (parameter == "cutoff" ) { cutoff = value; }
150 if (parameter == "precision" ) { precision = value; }
151 if (parameter == "iters" ) { iters = value; }
152 if (parameter == "jumble" ) { jumble = value; }
153 if (parameter == "freq" ) { freq = value; }
154 if (parameter == "method" ) { method = value; }
155 if (parameter == "fileroot" ) { fileroot = value; }
156 if (parameter == "line" ) { line = value; }
157 if (parameter == "label" ) { label = value; }
158 if (parameter == "random" ) { randomtree = value; }
159 if (parameter == "abund" ) { abund = value; }
160 if (parameter == "sorted" ) { sorted = value; }
161 if (parameter == "trump" ) { trump = value; }
162 if (parameter == "soft" ) { soft = value; }
163 if (parameter == "filter" ) { filter = value; }
164 if (parameter == "scale" ) { scale = value; }
165 if (parameter == "countends" ) { countends = value; }
166 if (parameter == "processors" ) { processors = value; }
167 if (parameter == "size" ) { size = value; }
168 if (parameter == "candidate") { candidatefile = value; }
169 if (parameter == "search") { search = value; }
170 if (parameter == "ksize") { ksize = value; }
171 if (parameter == "align") { align = value; }
172 if (parameter == "match") { match = value; }
173 if (parameter == "mismatch") { mismatch = value; }
174 if (parameter == "gapopen") { gapopen = value; }
175 if (parameter == "gapextend" ) { gapextend = value; }
180 //make sure the user does not use both the line and label parameters
181 if ((line != "") && (label != "")) { cout << "You may use either the line or label parameters, but not both." << endl; return false; }
183 //check for valid files
184 if (commandName == "read.dist") {
187 }else if (commandName == "read.otu") {
188 //you want to do shared commands
189 if ((listfile != "") && (groupfile != "")) {
190 validateParseFiles(); //checks the listfile and groupfile parameters
191 //you want to do single commands
192 }else if ((listfile != "") || (rabundfile != "") || (sabundfile != "")){
195 //you have not given a file
196 }else if ((listfile == "") && (sharedfile == "") && (rabundfile == "") && (sabundfile == "")) {
197 cout << "You must enter either a listfile, rabundfile, sabundfile or a sharedfile with the read.otu command. " << endl; return false;
198 //you want to do shared commands with a shared file
199 }else if (sharedfile != "") {//you are reading a shared file
202 }else if (commandName == "read.tree") {
203 validateTreeFiles(); //checks the treefile and groupfile parameters
204 }else if (commandName == "unique.seqs") {
205 if (fastafile == "") { cout << "You must enter a fastafile with the unique.seqs() command." << endl; return false; }
209 //are you trying to cluster before you have read something
210 if (((commandName == "cluster") && (globaldata->gSparseMatrix == NULL)) ||
211 ((commandName == "cluster") && (globaldata->gListVector == NULL))) {
212 cout << "Before you use the cluster command, you first need to read in a distance matrix." << endl;
216 if ((commandName == "libshuff") && ((globaldata->gMatrix == NULL) || (globaldata->gGroupmap == NULL))) {
217 cout << "You must read in a matrix and groupfile using the read.dist command, before you use the libshuff command. " << endl; return false;
220 if (commandName == "parsimony") {
221 //are you trying to use parsimony without reading a tree or saying you want random distribution
222 if (randomtree == "") {
223 if (globaldata->gTree.size() == 0) {
224 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; }
228 if ((commandName == "unifrac.weighted") || (commandName == "unifrac.unweighted") || (commandName == "concensus")) {
229 if (globaldata->gTree.size() == 0) {//no trees were read
230 cout << "You must execute the read.tree command, before you may execute the unifrac.weighted, unifrac.unweighted or concensus command." << endl; return false; }
233 //check for valid method
234 if(commandName == "get.group") {
235 if ((globaldata->getSharedFile() == "")) { cout << "You must read a groupfile or a sharedfile before you can use the get.group command." << endl; return false; }
237 if (commandName == "get.label" || commandName == "get.line") {
238 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; }
240 if (commandName == "cluster") {
241 if ((method == "furthest") || (method == "nearest") || (method == "average")) { }
242 else {cout << "Not a valid clustering method. Valid clustering algorithms are furthest, nearest or average." << endl; return false; }
245 if ((commandName == "collect.single") || (commandName == "rarefaction.single") || (commandName == "summary.single")){
246 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; }
249 if (commandName == "get.rabund") {
250 if (globaldata->getListFile() == "") { cout << "You must read a listfile before you can use the get.rabund command." << endl; return false; }
253 if (commandName == "get.sabund") {
254 if ((globaldata->getListFile() == "") && (globaldata->getRabundFile() == "")) { cout << "You must read a list or rabund before you can use the get.sabund command." << endl; return false; }
257 if ((commandName == "collect.shared") || (commandName == "rarefaction.shared") || (commandName == "summary.shared") || (commandName == "bootstrap.shared") || (commandName == "dist.shared")){
258 if (globaldata->getSharedFile() == "") {
259 if (globaldata->getListFile() == "") { cout << "You must read a list and a group, or a shared before you can use the collect.shared, rarefaction.shared, summary.shared, tree.shared, bootstrap.shared or dist.shared commands." << endl; return false; }
260 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, summary.shared, tree.shared, bootstrap.shared or dist.shared commands." << endl; return false; }
264 if (commandName == "tree.shared") {
266 if ((globaldata->getSharedFile() == "") && ((phylipfile == "") && (columnfile == ""))) { cout << "You must run the read.otu command or provide a distance file before running the tree.shared command." << endl; return false; }
267 //you want to do single commands
268 else if ((globaldata->getSharedFile() == "") && ((phylipfile != "") || (columnfile != ""))) {
273 if ((commandName == "heatmap") || (commandName == "venn")) {
274 if ((globaldata->getListFile() == "") && (globaldata->getSharedFile() == "")) {
275 cout << "You must read a list, or a list and a group, or a shared before you can use the heatmap or venn commands." << endl; return false;
279 if ((commandName == "filter.seqs") || (commandName == "dist.seqs")) {
280 if (fastafile == "") {
281 cout << "You must enter either a fasta file before you can use the filter.seqs or dist.seqs command." << endl; return false;
286 if (commandName == "align.seqs") {
287 if ((fastafile == "") || (candidatefile == "")) {
288 cout << "You must enter fasta and a candidate file to use the align.seqs command." << endl; return false;
293 int ableToOpen = openInputFile(candidatefile, filehandle);
296 if (ableToOpen == 1) { return false; }
299 if ((commandName == "bin.seqs")) {
300 if ((globaldata->getListFile() == "")) { cout << "You must read a list file before you can use the bin.seqs commands." << endl; return false; }
305 if ((commandName == "get.oturep")) {
306 if ((globaldata->gSparseMatrix == NULL) || (globaldata->gListVector == NULL)) {
307 cout << "Before you use the get.oturep command, you first need to read in a distance matrix." << endl;
310 if (listfile == "") { cout << "list is a required parameter for the get.oturep command." << endl; errorFree = false; }
311 if (fastafile == "") { cout << "fasta is a required parameter for the get.oturep command." << endl; errorFree = false; }
319 /*******************************************************/
321 /******************************************************/
322 //This function checks to make sure the user entered a file to
323 // read and that the file exists and can be opened.
324 void ErrorCheck::validateReadFiles() {
326 //Validating files for read
330 //are we reading a phylipfile
331 if (phylipfile != "") {
332 ableToOpen = openInputFile(phylipfile, filehandle);
335 if (ableToOpen == 1) { errorFree = false; }
336 else { globaldata->inputFileName = phylipfile; }
337 //are we reading a columnfile
338 }else if (columnfile != "") {
339 ableToOpen = openInputFile(columnfile, filehandle);
342 if (ableToOpen == 1) { errorFree = false; }
343 else { globaldata->inputFileName = columnfile; }
344 //are we reading a listfile
345 }else if (listfile!= "") {
346 ableToOpen = openInputFile(listfile, filehandle);
349 if (ableToOpen == 1) { errorFree = false; }
350 else { globaldata->inputFileName = listfile; }
351 //are we reading a rabundfile
352 }else if (rabundfile != "") {
353 ableToOpen = openInputFile(rabundfile, filehandle);
356 if (ableToOpen == 1) { errorFree = false; }
357 else { globaldata->inputFileName = rabundfile; }
358 //are we reading a sabundfile
359 }else if (sabundfile != "") {
360 ableToOpen = openInputFile(sabundfile, filehandle);
363 if (ableToOpen == 1) { errorFree = false; }
364 else { globaldata->inputFileName = sabundfile; }
365 }else if (fastafile != "") {
366 ableToOpen = openInputFile(fastafile, filehandle);
369 if (ableToOpen == 1) { errorFree = false; }
370 else { globaldata->inputFileName = fastafile; }
371 }else if (sharedfile != "") {
372 ableToOpen = openInputFile(sharedfile, filehandle);
375 if (ableToOpen == 1) { errorFree = false; }
376 else { globaldata->inputFileName = sharedfile; }
377 }else if (groupfile != "") {
378 ableToOpen = openInputFile(groupfile, filehandle);
380 if (ableToOpen == 1) { //unable to open
383 }else{ //no file given
387 catch(exception& e) {
388 cout << "Standard Error: " << e.what() << " has occurred in the ErrorCheck class Function validateReadFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
392 cout << "An unknown error has occurred in the ErrorCheck class function validateReadFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
397 /*******************************************************/
399 /******************************************************/
400 //This function checks to make sure the user entered appropriate
401 // format parameters on a distfile read
402 void ErrorCheck::validateReadDist() {
407 if (groupfile != "") {
408 ableToOpen = openInputFile(groupfile, filehandle);
411 if (ableToOpen == 1) { errorFree = false; }
414 if ((phylipfile == "") && (columnfile == "")) { cout << "When executing a read.dist or a tree.shared command with a distance file you must enter a phylip or a column." << endl; errorFree = false; }
415 else if ((phylipfile != "") && (columnfile != "")) { cout << "When executing a read.dist or a tree.shared command with a distance file you must enter ONLY ONE of the following: phylip or column." << endl; errorFree = false; }
417 if (columnfile != "") {
418 if (namefile == "") {
419 cout << "You need to provide a namefile if you are going to use the column format." << endl;
422 ableToOpen = openInputFile(namefile, filehandle);
425 if (ableToOpen == 1) { errorFree = false; }
429 catch(exception& e) {
430 cout << "Standard Error: " << e.what() << " has occurred in the ErrorCheck class Function validateReadDist. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
434 cout << "An unknown error has occurred in the ErrorCheck class function validateReadDist. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
438 /*******************************************************/
440 /******************************************************/
441 //This function checks to make sure the user entered appropriate
442 // format parameters on a parselistcommand
443 void ErrorCheck::validateParseFiles() {
448 //checks for valid files
450 if (listfile == "") { cout << "When executing a read.otu for groups you must enter a list and a group." << endl; errorFree = false; }
451 else if (groupfile == "") { cout << "When executing a read.otu for groups you must enter a list and a group." << endl; errorFree = false; }
453 //checks parameters on the read command
454 if (listfile != "") {
455 ableToOpen = openInputFile(listfile, filehandle);
457 if (ableToOpen == 1) { //unable to open
460 if (groupfile != "") {
461 ableToOpen = openInputFile(groupfile, filehandle);
463 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 /******************************************************/
481 //This function checks to make sure the user entered appropriate
482 // format parameters on a parselistcommand
483 void ErrorCheck::validateTreeFiles() {
488 //checks for valid files
490 if (treefile == "") { cout << "When executing a read.tree you must enter a treefile and a groupfile." << endl; errorFree = false; }
491 else if (groupfile == "") { cout << "When executing a read.tree you must enter a treefile and a groupfile." << endl; errorFree = false; }
493 //checks parameters on the read command
494 if (treefile != "") {
495 ableToOpen = openInputFile(treefile, filehandle);
497 if (ableToOpen == 1) { //unable to open
500 if (groupfile != "") {
501 ableToOpen = openInputFile(groupfile, filehandle);
503 if (ableToOpen == 1) { //unable to open
509 catch(exception& e) {
510 cout << "Standard Error: " << e.what() << " has occurred in the ErrorCheck class Function validateTreeFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
514 cout << "An unknown error has occurred in the ErrorCheck class function validateTreeFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
519 /*******************************************************/
521 /******************************************************/
522 //This function checks to make sure the user entered appropriate
523 // format parameters on a distfile read
524 void ErrorCheck::validateReadPhil() {
529 //checks to make sure only one file type is given
530 if (listfile != "") {
531 if ((rabundfile != "") || (sabundfile != "")) {
532 cout << "When executing a read.otu you must enter ONLY ONE of the following: list, rabund or sabund." << endl; errorFree = false; }
533 }else if (rabundfile != "") {
534 if ((listfile != "") || (sabundfile != "")) {
535 cout << "When executing a read.otu you must enter ONLY ONE of the following: list, rabund or sabund." << endl; errorFree = false; }
536 }else if (sabundfile != "") {
537 if ((listfile != "") || (rabundfile != "")) {
538 cout << "When executing a read.otu you must enter ONLY ONE of the following: list, rabund or sabund." << endl; errorFree = false; }
539 }else if ((listfile == "") && (rabundfile == "") && (sabundfile == "") && (sharedfile == "")) {
540 cout << "When executing a read.otu you must enter one of the following: list, rabund or sabund." << endl; errorFree = false;
543 //checks parameters on the read command
544 if (orderfile != "") {
545 ableToOpen = openInputFile(orderfile, filehandle);
547 if (ableToOpen == 1) { //unable to open
552 catch(exception& e) {
553 cout << "Standard Error: " << e.what() << " has occurred in the ErrorCheck class Function validateReadPhil. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
557 cout << "An unknown error has occurred in the ErrorCheck class function validateReadPhil. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
561 /*******************************************************/
563 /******************************************************/
564 //This function checks to make sure the user entered appropriate
565 // format parameters on a bin.seq command
566 void ErrorCheck::validateBinFiles() {
571 if (fastafile == "") {
572 cout << "fasta is a required parameter for bin.seqs, get.oturep and get.repseqs commands." << endl; errorFree = false;
573 }else if (fastafile != "") {
574 //is it a valid filename'
575 ableToOpen = openInputFile(fastafile, filehandle);
578 if (ableToOpen == 1) { errorFree = false; }
579 }else if (listfile != "") {
580 //is it a valid filename'
581 ableToOpen = openInputFile(listfile, filehandle);
584 if (ableToOpen == 1) { errorFree = false; }
585 }else if (globaldata->getNameFile() != "") {
586 //is it a valid filename'
588 int ableToOpen = openInputFile(globaldata->getNameFile(), filehandle);
591 if (ableToOpen == 1) { errorFree = false; }
592 }else if (namefile != "") {
593 //is it a valid filename'
595 int ableToOpen = openInputFile(namefile, filehandle);
598 if (ableToOpen == 1) { errorFree = false; }
599 }else if (groupfile != "") {
600 //is it a valid filename'
602 int ableToOpen = openInputFile(groupfile, filehandle);
605 if (ableToOpen == 1) { errorFree = false; }
608 catch(exception& e) {
609 cout << "Standard Error: " << e.what() << " has occurred in the ErrorCheck class Function validateBinFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
613 cout << "An unknown error has occurred in the ErrorCheck class function validateBinFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
618 /*******************************************************/
620 /******************************************************/
622 void ErrorCheck::clear() {
623 //option definitions should go here...
639 /*******************************************************/
641 /******************************************************/