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 == "nexus" ) { nexusfile = value; }
99 if (parameter == "clustal" ) { clustalfile = value; }
100 if (parameter == "tree" ) { treefile = value; }
101 if (parameter == "group" ) { groupfile = value; }
102 if (parameter == "shared" ) { sharedfile = value; }
103 if (parameter == "cutoff" ) { cutoff = value; }
104 if (parameter == "precision" ) { precision = value; }
105 if (parameter == "iters" ) { iters = value; }
106 if (parameter == "jumble" ) { jumble = value; }
107 if (parameter == "freq" ) { freq = value; }
108 if (parameter == "method" ) { method = value; }
109 if (parameter == "fileroot" ) { fileroot = value; }
110 if (parameter == "line" ) { line = value; }
111 if (parameter == "label" ) { label = value; }
112 if (parameter == "abund" ) { abund = value; }
113 if (parameter == "random" ) { randomtree = value; }
114 if (parameter == "sorted" ) { sorted = value; }
115 if (parameter == "trump" ) { trump = value; }
116 if (parameter == "soft" ) { soft = value; }
117 if (parameter == "filter" ) { filter = value; }
118 if (parameter == "scale" ) { scale = value; }
119 if (parameter == "countends" ) { countends = value; }
120 if (parameter == "processors" ) { processors = value; }
121 if (parameter == "size" ) { size = value; }
122 if (parameter == "candidate") { candidatefile = value; }
123 if (parameter == "search") { search = value; }
124 if (parameter == "ksize") { ksize = value; }
125 if (parameter == "align") { align = value; }
126 if (parameter == "match") { match = value; }
127 if (parameter == "mismatch") { mismatch = value; }
128 if (parameter == "gapopen") { gapopen = value; }
129 if (parameter == "gapextend" ) { gapextend = value; }
132 //gets the last parameter and value
133 if (errorFree) { //gets the last parameter and value
135 splitAtEquals(parameter, value);
136 //is it a valid parameter
137 if (validParameter->isValidParameter(parameter, commandName, value) != true) { return false; }
140 if (parameter == "phylip" ) { phylipfile = value; }
141 if (parameter == "column" ) { columnfile = value; }
142 if (parameter == "list" ) { listfile = value; }
143 if (parameter == "rabund" ) { rabundfile = value; }
144 if (parameter == "sabund" ) { sabundfile = value; }
145 if (parameter == "name" ) { namefile = value; }
146 if (parameter == "order" ) { orderfile = value; }
147 if (parameter == "group" ) { groupfile = value; }
148 if (parameter == "shared" ) { sharedfile = value; }
149 if (parameter == "fasta" ) { fastafile = value; }
150 if (parameter == "nexus" ) { nexusfile = value; }
151 if (parameter == "clustal" ) { clustalfile = value; }
152 if (parameter == "tree" ) { treefile = value; }
153 if (parameter == "cutoff" ) { cutoff = value; }
154 if (parameter == "precision" ) { precision = value; }
155 if (parameter == "iters" ) { iters = value; }
156 if (parameter == "jumble" ) { jumble = value; }
157 if (parameter == "freq" ) { freq = value; }
158 if (parameter == "method" ) { method = value; }
159 if (parameter == "fileroot" ) { fileroot = value; }
160 if (parameter == "line" ) { line = value; }
161 if (parameter == "label" ) { label = value; }
162 if (parameter == "random" ) { randomtree = value; }
163 if (parameter == "abund" ) { abund = value; }
164 if (parameter == "sorted" ) { sorted = value; }
165 if (parameter == "trump" ) { trump = value; }
166 if (parameter == "soft" ) { soft = value; }
167 if (parameter == "filter" ) { filter = value; }
168 if (parameter == "scale" ) { scale = value; }
169 if (parameter == "countends" ) { countends = value; }
170 if (parameter == "processors" ) { processors = value; }
171 if (parameter == "size" ) { size = value; }
172 if (parameter == "candidate") { candidatefile = value; }
173 if (parameter == "search") { search = value; }
174 if (parameter == "ksize") { ksize = value; }
175 if (parameter == "align") { align = value; }
176 if (parameter == "match") { match = value; }
177 if (parameter == "mismatch") { mismatch = value; }
178 if (parameter == "gapopen") { gapopen = value; }
179 if (parameter == "gapextend" ) { gapextend = value; }
184 //make sure the user does not use both the line and label parameters
185 if ((line != "") && (label != "")) { cout << "You may use either the line or label parameters, but not both." << endl; return false; }
187 //check for valid files
188 if (commandName == "read.dist") {
191 }else if (commandName == "read.otu") {
192 //you want to do shared commands
193 if ((listfile != "") && (groupfile != "")) {
194 validateParseFiles(); //checks the listfile and groupfile parameters
195 //you want to do single commands
196 }else if ((listfile != "") || (rabundfile != "") || (sabundfile != "")){
199 //you have not given a file
200 }else if ((listfile == "") && (sharedfile == "") && (rabundfile == "") && (sabundfile == "")) {
201 cout << "You must enter either a listfile, rabundfile, sabundfile or a sharedfile with the read.otu command. " << endl; return false;
202 //you want to do shared commands with a shared file
203 }else if (sharedfile != "") {//you are reading a shared file
206 }else if (commandName == "read.tree") {
207 validateTreeFiles(); //checks the treefile and groupfile parameters
208 }else if (commandName == "deconvolute") {
209 if (fastafile == "") { cout << "You must enter a fastafile with the deconvolute() command." << endl; return false; }
213 //are you trying to cluster before you have read something
214 if (((commandName == "cluster") && (globaldata->gSparseMatrix == NULL)) ||
215 ((commandName == "cluster") && (globaldata->gListVector == NULL))) {
216 cout << "Before you use the cluster command, you first need to read in a distance matrix." << endl;
220 if ((commandName == "libshuff") && ((globaldata->gMatrix == NULL) || (globaldata->gGroupmap == NULL))) {
221 cout << "You must read in a matrix and groupfile using the read.dist command, before you use the libshuff command. " << endl; return false;
224 if (commandName == "parsimony") {
225 //are you trying to use parsimony without reading a tree or saying you want random distribution
226 if (randomtree == "") {
227 if (globaldata->gTree.size() == 0) {
228 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; }
232 if ((commandName == "unifrac.weighted") || (commandName == "unifrac.unweighted") || (commandName == "concensus")) {
233 if (globaldata->gTree.size() == 0) {//no trees were read
234 cout << "You must execute the read.tree command, before you may execute the unifrac.weighted, unifrac.unweighted or concensus command." << endl; return false; }
237 //check for valid method
238 if(commandName == "get.group") {
239 if ((globaldata->getSharedFile() == "")) { cout << "You must read a groupfile or a sharedfile before you can use the get.group command." << endl; return false; }
241 if (commandName == "get.label" || commandName == "get.line") {
242 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; }
244 if (commandName == "cluster") {
245 if ((method == "furthest") || (method == "nearest") || (method == "average")) { }
246 else {cout << "Not a valid clustering method. Valid clustering algorithms are furthest, nearest or average." << endl; return false; }
249 if ((commandName == "collect.single") || (commandName == "rarefaction.single") || (commandName == "summary.single") ){
250 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; }
253 if ((commandName == "collect.shared") || (commandName == "rarefaction.shared") || (commandName == "summary.shared") || (commandName == "bootstrap.shared") || (commandName == "dist.shared")){
254 if (globaldata->getSharedFile() == "") {
255 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; }
256 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; }
260 if (commandName == "tree.shared") {
262 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; }
263 //you want to do single commands
264 else if ((globaldata->getSharedFile() == "") && ((phylipfile != "") || (columnfile != ""))) {
269 if ((commandName == "heatmap") || (commandName == "venn")) {
270 if ((globaldata->getListFile() == "") && (globaldata->getSharedFile() == "")) {
271 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;
275 if ((commandName == "filter.seqs") || (commandName == "dist.seqs")) {
276 if ((fastafile == "") && (nexusfile == "") && (clustalfile == "") && (phylipfile == "")) {
277 cout << "You must enter either a fasta, nexus, clustal, or phylip file before you can use the filter.seqs or dist.seqs command." << endl; return false;
282 if (commandName == "align.seqs") {
283 if ((fastafile == "") || (candidatefile == "")) {
284 cout << "You must enter fasta and a candidate file to use the align.seqs command." << endl; return false;
289 if ((commandName == "bin.seqs")) {
290 if ((globaldata->getListFile() == "")) { cout << "You must read a list file before you can use the bin.seqs commands." << endl; return false; }
295 if ((commandName == "get.oturep")) {
296 if ((globaldata->gSparseMatrix == NULL) || (globaldata->gListVector == NULL)) {
297 cout << "Before you use the get.oturep command, you first need to read in a distance matrix." << endl;
300 if (listfile == "") { cout << "list is a required parameter for the get.oturep command." << endl; errorFree = false; }
301 if (fastafile == "") { cout << "fasta is a required parameter for the get.oturep command." << endl; errorFree = false; }
309 /*******************************************************/
311 /******************************************************/
312 //This function checks to make sure the user entered a file to
313 // read and that the file exists and can be opened.
314 void ErrorCheck::validateReadFiles() {
316 //Validating files for read
320 //are we reading a phylipfile
321 if (phylipfile != "") {
322 ableToOpen = openInputFile(phylipfile, filehandle);
325 if (ableToOpen == 1) { errorFree = false; }
326 else { globaldata->inputFileName = phylipfile; }
327 //are we reading a columnfile
328 }else if (columnfile != "") {
329 ableToOpen = openInputFile(columnfile, filehandle);
332 if (ableToOpen == 1) { errorFree = false; }
333 else { globaldata->inputFileName = columnfile; }
334 //are we reading a listfile
335 }else if (listfile!= "") {
336 ableToOpen = openInputFile(listfile, filehandle);
339 if (ableToOpen == 1) { errorFree = false; }
340 else { globaldata->inputFileName = listfile; }
341 //are we reading a rabundfile
342 }else if (rabundfile != "") {
343 ableToOpen = openInputFile(rabundfile, filehandle);
346 if (ableToOpen == 1) { errorFree = false; }
347 else { globaldata->inputFileName = rabundfile; }
348 //are we reading a sabundfile
349 }else if (sabundfile != "") {
350 ableToOpen = openInputFile(sabundfile, filehandle);
353 if (ableToOpen == 1) { errorFree = false; }
354 else { globaldata->inputFileName = sabundfile; }
355 }else if (fastafile != "") {
356 ableToOpen = openInputFile(fastafile, filehandle);
359 if (ableToOpen == 1) { errorFree = false; }
360 else { globaldata->inputFileName = fastafile; }
361 }else if (sharedfile != "") {
362 ableToOpen = openInputFile(sharedfile, filehandle);
365 if (ableToOpen == 1) { errorFree = false; }
366 else { globaldata->inputFileName = sharedfile; }
367 }else if (groupfile != "") {
368 ableToOpen = openInputFile(groupfile, filehandle);
370 if (ableToOpen == 1) { //unable to open
373 }else{ //no file given
377 catch(exception& e) {
378 cout << "Standard Error: " << e.what() << " has occurred in the ErrorCheck class Function validateReadFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
382 cout << "An unknown error has occurred in the ErrorCheck class function validateReadFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
387 /*******************************************************/
389 /******************************************************/
390 //This function checks to make sure the user entered appropriate
391 // format parameters on a distfile read
392 void ErrorCheck::validateReadDist() {
397 if (groupfile != "") {
398 ableToOpen = openInputFile(groupfile, filehandle);
401 if (ableToOpen == 1) { errorFree = false; }
404 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; }
405 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; }
407 if (columnfile != "") {
408 if (namefile == "") {
409 cout << "You need to provide a namefile if you are going to use the column format." << endl;
412 ableToOpen = openInputFile(namefile, filehandle);
415 if (ableToOpen == 1) { errorFree = false; }
419 catch(exception& e) {
420 cout << "Standard Error: " << e.what() << " has occurred in the ErrorCheck class Function validateReadDist. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
424 cout << "An unknown error has occurred in the ErrorCheck class function validateReadDist. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
428 /*******************************************************/
430 /******************************************************/
431 //This function checks to make sure the user entered appropriate
432 // format parameters on a parselistcommand
433 void ErrorCheck::validateParseFiles() {
438 //checks for valid files
440 if (listfile == "") { cout << "When executing a read.otu for groups you must enter a list and a group." << endl; errorFree = false; }
441 else if (groupfile == "") { cout << "When executing a read.otu for groups you must enter a list and a group." << endl; errorFree = false; }
443 //checks parameters on the read command
444 if (listfile != "") {
445 ableToOpen = openInputFile(listfile, filehandle);
447 if (ableToOpen == 1) { //unable to open
450 if (groupfile != "") {
451 ableToOpen = openInputFile(groupfile, filehandle);
453 if (ableToOpen == 1) { //unable to open
459 catch(exception& e) {
460 cout << "Standard Error: " << e.what() << " has occurred in the ErrorCheck class Function validateReadPhil. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
464 cout << "An unknown error has occurred in the ErrorCheck class function validateReadPhil. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
468 /*******************************************************/
470 /******************************************************/
471 //This function checks to make sure the user entered appropriate
472 // format parameters on a parselistcommand
473 void ErrorCheck::validateTreeFiles() {
478 //checks for valid files
480 if (treefile == "") { cout << "When executing a read.tree you must enter a treefile and a groupfile." << endl; errorFree = false; }
481 else if (groupfile == "") { cout << "When executing a read.tree you must enter a treefile and a groupfile." << endl; errorFree = false; }
483 //checks parameters on the read command
484 if (treefile != "") {
485 ableToOpen = openInputFile(treefile, filehandle);
487 if (ableToOpen == 1) { //unable to open
490 if (groupfile != "") {
491 ableToOpen = openInputFile(groupfile, filehandle);
493 if (ableToOpen == 1) { //unable to open
499 catch(exception& e) {
500 cout << "Standard Error: " << e.what() << " has occurred in the ErrorCheck class Function validateTreeFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
504 cout << "An unknown error has occurred in the ErrorCheck class function validateTreeFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
509 /*******************************************************/
511 /******************************************************/
512 //This function checks to make sure the user entered appropriate
513 // format parameters on a distfile read
514 void ErrorCheck::validateReadPhil() {
519 //checks to make sure only one file type is given
520 if (listfile != "") {
521 if ((rabundfile != "") || (sabundfile != "")) {
522 cout << "When executing a read.otu you must enter ONLY ONE of the following: list, rabund or sabund." << endl; errorFree = false; }
523 }else if (rabundfile != "") {
524 if ((listfile != "") || (sabundfile != "")) {
525 cout << "When executing a read.otu you must enter ONLY ONE of the following: list, rabund or sabund." << endl; errorFree = false; }
526 }else if (sabundfile != "") {
527 if ((listfile != "") || (rabundfile != "")) {
528 cout << "When executing a read.otu you must enter ONLY ONE of the following: list, rabund or sabund." << endl; errorFree = false; }
529 }else if ((listfile == "") && (rabundfile == "") && (sabundfile == "") && (sharedfile == "")) {
530 cout << "When executing a read.otu you must enter one of the following: list, rabund or sabund." << endl; errorFree = false;
533 //checks parameters on the read command
534 if (orderfile != "") {
535 ableToOpen = openInputFile(orderfile, filehandle);
537 if (ableToOpen == 1) { //unable to open
542 catch(exception& e) {
543 cout << "Standard Error: " << e.what() << " has occurred in the ErrorCheck class Function validateReadPhil. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
547 cout << "An unknown error has occurred in the ErrorCheck class function validateReadPhil. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
551 /*******************************************************/
553 /******************************************************/
554 //This function checks to make sure the user entered appropriate
555 // format parameters on a distfile read
556 void ErrorCheck::validateSeqsFiles() {
561 //checks to make sure only one file type is given
562 if (phylipfile != "") {
563 if ((nexusfile != "") || (fastafile != "") || (clustalfile != "")) {
564 cout << "You may enter ONLY ONE of the following: phylip, fasta, nexus or clustal." << endl; errorFree = false; }
566 ableToOpen = openInputFile(phylipfile, filehandle);
568 if (ableToOpen == 1) { //unable to open
572 }else if (nexusfile != "") {
573 if ((phylipfile != "") || (fastafile != "") || (clustalfile != "")) {
574 cout << "You may enter ONLY ONE of the following: phylip, fasta, nexus or clustal." << endl; errorFree = false; }
576 ableToOpen = openInputFile(nexusfile, filehandle);
578 if (ableToOpen == 1) { //unable to open
582 }else if (fastafile != "") {
583 if ((phylipfile != "") || (nexusfile != "") || (clustalfile != "")) {
584 cout << "You may enter ONLY ONE of the following: phylip, fasta, nexus or clustal." << endl; errorFree = false; }
586 ableToOpen = openInputFile(fastafile, filehandle);
588 if (ableToOpen == 1) { //unable to open
592 }else if (clustalfile != "") {
593 if ((phylipfile != "") || (nexusfile != "") || (fastafile != "")) {
594 cout << "You may enter ONLY ONE of the following: phylip, fasta, nexus or clustal." << endl; errorFree = false; }
596 ableToOpen = openInputFile(clustalfile, filehandle);
598 if (ableToOpen == 1) { //unable to open
602 }else if (candidatefile != "") {
603 ableToOpen = openInputFile(candidatefile, filehandle);
605 if (ableToOpen == 1) { //unable to open
612 catch(exception& e) {
613 cout << "Standard Error: " << e.what() << " has occurred in the ErrorCheck class Function validateSeqsFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
617 cout << "An unknown error has occurred in the ErrorCheck class function validateSeqsFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
622 /*******************************************************/
624 /******************************************************/
625 //This function checks to make sure the user entered appropriate
626 // format parameters on a bin.seq command
627 void ErrorCheck::validateBinFiles() {
632 if (fastafile == "") {
633 cout << "fasta is a required parameter for bin.seqs, get.oturep and get.repseqs commands." << endl; errorFree = false;
634 }else if (fastafile != "") {
635 //is it a valid filename'
636 ableToOpen = openInputFile(fastafile, filehandle);
639 if (ableToOpen == 1) { errorFree = false; }
640 }else if (listfile != "") {
641 //is it a valid filename'
642 ableToOpen = openInputFile(listfile, filehandle);
645 if (ableToOpen == 1) { errorFree = false; }
646 }else if (globaldata->getNameFile() != "") {
647 //is it a valid filename'
649 int ableToOpen = openInputFile(globaldata->getNameFile(), filehandle);
652 if (ableToOpen == 1) { errorFree = false; }
653 }else if (namefile != "") {
654 //is it a valid filename'
656 int ableToOpen = openInputFile(namefile, filehandle);
659 if (ableToOpen == 1) { errorFree = false; }
660 }else if (groupfile != "") {
661 //is it a valid filename'
663 int ableToOpen = openInputFile(groupfile, filehandle);
666 if (ableToOpen == 1) { errorFree = false; }
672 catch(exception& e) {
673 cout << "Standard Error: " << e.what() << " has occurred in the ErrorCheck class Function validateBinFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
677 cout << "An unknown error has occurred in the ErrorCheck class function validateBinFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
682 /*******************************************************/
684 /******************************************************/
686 void ErrorCheck::clear() {
687 //option definitions should go here...
705 /*******************************************************/
707 /******************************************************/