]> git.donarmstrong.com Git - mothur.git/blob - errorchecking.cpp
067d246d4c50f1427840c3879be04bac2ff7cdf8
[mothur.git] / errorchecking.cpp
1 /*
2  *  errorchecking.cpp
3  *  Dotur
4  *
5  *  Created by Sarah Westcott on 1/2/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "errorchecking.h"
11
12 /*******************************************************/
13
14 /******************************************************/
15
16 ErrorCheck::ErrorCheck() {
17         globaldata = GlobalData::getInstance();
18         validCommand = new ValidCommands();
19         validParameter = new ValidParameters();
20         validCalculator = new ValidCalculators();
21 }
22 /*******************************************************/
23
24 /******************************************************/
25
26 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 }
42
43 /*******************************************************/
44
45 /******************************************************/
46
47 ErrorCheck::~ErrorCheck() {}
48
49 /*******************************************************/
50
51 /******************************************************/
52
53 bool ErrorCheck::checkInput(string input) {
54                 errorFree = true;
55                 clear();
56                 
57                 //refresh variable
58                 refresh();
59                 
60                 //get command name and parameters
61                 int openParen = input.find_first_of('(');
62                 int closeParen = input.find_last_of(')');
63
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;
69                         return false;
70                 }
71                 
72                 //is it a valid command
73                 if (validCommand->isValidCommand(commandName) != true) { return false; }
74                 
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                                 globaldata->splitAtComma(value, optionText);
80                                 globaldata->splitAtEquals(parameter, value);
81                                 
82                                 //is it a valid parameter
83                                 if (validParameter->isValidParameter(parameter) != true) { return false; }
84                                 
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 == "cutoff" )                     { cutoff = value; }
96                                 if (parameter == "precision" )          { precision = value; }
97                                 if (parameter == "iters" )                      { iters = value; }
98                                 if (parameter == "jumble" )                     { jumble = value; }
99                                 if (parameter == "freq" )                       { freq = value; }
100                                 if (parameter == "method" )                     { method = value; }
101                                 if (parameter == "fileroot" )           { fileroot = value; }
102                                 if (parameter == "line" )                       { line = value; }
103                                 if (parameter == "label" )                      { label = value; }
104                                 if (parameter == "randomtree" )         { randomtree = value;   }
105
106                                 if (parameter == "single") {//stores estimators in a vector
107                                         singleEsimators.clear(); //clears out old values
108                                         globaldata->splitAtDash(value, singleEsimators);
109                                         for (int i = 0; i < singleEsimators.size(); i++) { //loop through estimators
110                                                 //is it a valid calculator
111                                                 if (validCalculator->isValidCalculator(parameter, singleEsimators[i]) != true) { return false; }
112                                         }
113                                 }
114                                 if (parameter == "rarefaction") {//stores estimators in a vector
115                                         rareEstimators.clear(); //clears out old values
116                                         globaldata->splitAtDash(value, rareEstimators);
117                                         for (int i = 0; i < rareEstimators.size(); i++) { //loop through estimators
118                                                 //is it a valid calculator
119                                                 if (validCalculator->isValidCalculator(parameter, rareEstimators[i]) != true) { return false; }
120                                         }
121                                 }
122                                 if (parameter == "shared") {//stores estimators in a vector
123                                         sharedEstimators.clear(); //clears out old values
124                                         globaldata->splitAtDash(value, sharedEstimators);
125                                         for (int i = 0; i < sharedEstimators.size(); i++) { //loop through estimators
126                                                 //is it a valid calculator
127                                                 if (validCalculator->isValidCalculator(parameter, sharedEstimators[i]) != true) { return false; }
128                                         }
129                                 }
130                                 if (parameter == "summary") { //stores summaries to be used in a vector
131                                         summaryEstimators.clear(); //clears out old values
132                                         globaldata->splitAtDash(value, summaryEstimators);
133                                         for (int i = 0; i < summaryEstimators.size(); i++) { //loop through estimators
134                                                 //is it a valid calculator
135                                                 if (validCalculator->isValidCalculator(parameter, summaryEstimators[i]) != true) { return false; }
136                                         }
137                                 }
138                                 if (parameter == "sharedrarefaction") { //stores summaries to be used in a vector
139                                         sharedRareEstimators.clear(); //clears out old values
140                                         globaldata->splitAtDash(value, sharedRareEstimators);
141                                         for (int i = 0; i < sharedRareEstimators.size(); i++) { //loop through estimators
142                                                 //is it a valid calculator
143                                                 if (validCalculator->isValidCalculator(parameter, sharedRareEstimators[i]) != true) { return false; }
144                                         }
145                                 }
146                         }
147                         
148                         //gets the last parameter and value
149                         if (errorFree)  { //gets the last parameter and value
150                                 value = optionText;
151                                 globaldata->splitAtEquals(parameter, value);
152                                 //is it a valid parameter
153                                 if (validParameter->isValidParameter(parameter) != true) { return false; }
154                                 
155                                 if (parameter == "phylip" )             { phylipfile = value; }
156                                 if (parameter == "column" )             { columnfile = value; }                         
157                                 if (parameter == "list" )               { listfile = value; }
158                                 if (parameter == "rabund" )             { rabundfile = value; }
159                                 if (parameter == "sabund" )             { sabundfile = value; }
160                                 if (parameter == "name" )               { namefile = value; }
161                                 if (parameter == "order" )              { orderfile = value; }
162                                 if (parameter == "group" )              { groupfile = value; }
163                                 if (parameter == "fasta" )              { fastafile = value; }
164                                 if (parameter == "tree" )               { treefile = value; }
165                                 if (parameter == "cutoff" )                     { cutoff = value; }
166                                 if (parameter == "precision" )          { precision = value; }
167                                 if (parameter == "iters" )                      { iters = value; }
168                                 if (parameter == "jumble" )                     { jumble = value; }
169                                 if (parameter == "freq" )                       { freq = value; }
170                                 if (parameter == "method" )                     { method = value; }
171                                 if (parameter == "fileroot" )           { fileroot = value; }
172                                 if (parameter == "line" )                       { line = value; }
173                                 if (parameter == "label" )                      { label = value; }
174                                 if (parameter == "randomtree" )         { randomtree = value;   }
175
176                                 if (parameter == "single") {//stores estimators in a vector
177                                         singleEsimators.clear(); //clears out old values
178                                         globaldata->splitAtDash(value, singleEsimators);
179                                         for (int i = 0; i < singleEsimators.size(); i++) { //loop through estimators
180                                                 //is it a valid calculator
181                                                 if (validCalculator->isValidCalculator(parameter, singleEsimators[i]) != true) { return false; }
182                                         }
183                                 }
184                                 if (parameter == "rarefaction") {//stores estimators in a vector
185                                         rareEstimators.clear(); //clears out old values
186                                         globaldata->splitAtDash(value, rareEstimators);
187                                         for (int i = 0; i < rareEstimators.size(); i++) { //loop through estimators
188                                                 //is it a valid calculator
189                                                 if (validCalculator->isValidCalculator(parameter, rareEstimators[i]) != true) { return false; }
190                                         }
191                                 }
192                                 if (parameter == "shared") {//stores estimators in a vector
193                                         sharedEstimators.clear(); //clears out old values
194                                         globaldata->splitAtDash(value, sharedEstimators);
195                                         for (int i = 0; i < sharedEstimators.size(); i++) { //loop through estimators
196                                                 //is it a valid calculator
197                                                 if (validCalculator->isValidCalculator(parameter, sharedEstimators[i]) != true) { return false; }
198                                         }
199                                 }
200                                 if (parameter == "summary") { //stores summaries to be used in a vector
201                                         summaryEstimators.clear(); //clears out old values
202                                         globaldata->splitAtDash(value, summaryEstimators);
203                                         for (int i = 0; i < summaryEstimators.size(); i++) { //loop through estimators
204                                                 //is it a valid calculator
205                                                 if (validCalculator->isValidCalculator(parameter, summaryEstimators[i]) != true) { return false; }
206                                         }
207                                 }
208                                 if (parameter == "sharedrarefaction") { //stores summaries to be used in a vector
209                                         sharedRareEstimators.clear(); //clears out old values
210                                         globaldata->splitAtDash(value, sharedRareEstimators);
211                                         for (int i = 0; i < sharedRareEstimators.size(); i++) { //loop through estimators
212                                                 //is it a valid calculator
213                                                 if (validCalculator->isValidCalculator(parameter, sharedRareEstimators[i]) != true) { return false; }
214                                         }
215                                 }
216
217                         }
218                 }
219                 
220                 //make sure the user does not use both the line and label parameters
221                 if ((line != "") && (label != "")) { cout << "You may use either the line or label parameters, but not both." << endl; return false; }
222         
223                 
224                 if (commandName == "read.dist") { 
225                         validateReadFiles();
226                         validateReadDist();
227                 }else if (commandName == "read.otu") { 
228                         //you want to do shared commands
229                         if ((listfile != "") && (groupfile != ""))      {
230                                 validateParseFiles(); //checks the listfile and groupfile parameters
231                         }else { //you want to do single commands
232                                 validateReadFiles();
233                                 validateReadPhil();
234                         }
235                 }else if (commandName == "read.tree") { 
236                         validateTreeFiles(); //checks the treefile and groupfile parameters
237                 }else if (commandName == "deconvolute") {
238                         if (fastafile == "") { cout << "You must enter a fastafile with the deconvolute() command." << endl; return false; }
239                         validateReadFiles();
240                 }
241                 
242                 //are you trying to cluster before you have read something                      
243                 if ((commandName == "cluster") && (globaldata->getSparseMatrix() == NULL) ||
244                         (commandName == "cluster") && (globaldata->getListVector() == NULL)) {
245                                 cout << "Before you use the cluster command, you first need to read in a distance matrix." << endl; 
246                                 errorFree = false;
247                 } 
248                 
249                 if (commandName == "parsimony") {
250                         //are you trying to use parsimony without reading a tree or saying you want random distribution
251                         if (randomtree == "0")  {
252                                 if ((globaldata->getTreeFile() == "") || (globaldata->getGroupFile() == "")) {
253                                         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;  }
254                         }
255                 }
256                 
257                 if ((commandName == "unifrac.weighted") || (commandName == "unifrac.unweighted")) {
258                         if (globaldata->gTree.size() == 0) {//no trees were read
259                                 cout << "You must execute the read.tree command, before you may execute the unifrac.weighted or unifrac.unweighted command." << endl; return false;  }
260                 }
261                 
262                 //check for valid method
263                 if (commandName == "cluster") {
264                         if ((method == "furthest") || (method == "nearest") || (method == "average")) { }
265                         else {cout << "Not a valid clustering method.  Valid clustering algorithms are furthest, nearest or average." << endl; return false; }
266                 }
267                 
268                 if ((commandName == "collect.single") || (commandName == "rarefaction.single") || (commandName == "summary.single") ){ 
269                         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; }
270                 }
271                 
272                 if ((commandName == "collect.shared") || (commandName == "rarefaction.shared") || (commandName == "summary.shared") ){ 
273                         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; }
274                         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; }
275                 }
276  
277                 
278                 return errorFree;
279 }
280
281 /*******************************************************/
282
283 /******************************************************/
284 //This function checks to make sure the user entered a file to 
285 // read and that the file exists and can be opened.
286 void ErrorCheck::validateReadFiles() {
287         try {
288                 //Validating files for read
289                 ifstream filehandle;
290                 int ableToOpen;
291         
292                 //are we reading a phylipfile
293                 if (phylipfile != "") {
294                         ableToOpen = openInputFile(phylipfile, filehandle);
295                         filehandle.close();
296                         //unable to open
297                         if (ableToOpen == 1) { errorFree = false; }
298                         else { globaldata->inputFileName = phylipfile; }
299                 //are we reading a phylipfile
300                 }else if (columnfile != "") {
301                         ableToOpen = openInputFile(columnfile, filehandle);
302                         filehandle.close();
303                         //unable to open
304                         if (ableToOpen == 1) { errorFree = false; }
305                         else { globaldata->inputFileName = columnfile; }
306                 //are we reading a listfile
307                 }else if (listfile!= "") {
308                         ableToOpen = openInputFile(listfile, filehandle);
309                         filehandle.close();
310                         //unable to open
311                         if (ableToOpen == 1) {  errorFree = false; }
312                         else { globaldata->inputFileName = listfile; }
313                 //are we reading a rabundfile
314                 }else if (rabundfile != "") {
315                         ableToOpen = openInputFile(rabundfile, filehandle);
316                         filehandle.close();
317                         //unable to open
318                         if (ableToOpen == 1) {  errorFree = false; }
319                         else { globaldata->inputFileName = rabundfile; }
320                 //are we reading a sabundfile
321                 }else if (sabundfile != "") {
322                         ableToOpen = openInputFile(sabundfile, filehandle);
323                         filehandle.close();
324                         //unable to open
325                         if (ableToOpen == 1) {  errorFree = false; }
326                         else { globaldata->inputFileName = sabundfile; }
327                 }else if (fastafile != "") {
328                         ableToOpen = openInputFile(fastafile, filehandle);
329                         filehandle.close();
330                         //unable to open
331                         if (ableToOpen == 1) {  errorFree = false; }
332                         else { globaldata->inputFileName = fastafile; }
333                 }else{ //no file given
334                         errorFree = false;
335                 }
336         }
337         catch(exception& e) {
338                 cout << "Standard Error: " << e.what() << " has occurred in the ErrorCheck class Function validateReadFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
339                 exit(1);
340         }
341         catch(...) {
342                 cout << "An unknown error has occurred in the ErrorCheck class function validateReadFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
343                 exit(1);
344         }
345         
346 }
347 /*******************************************************/
348
349 /******************************************************/
350 //This function checks to make sure the user entered appropriate
351 // format parameters on a distfile read
352 void ErrorCheck::validateReadDist() {
353         try {
354                 ifstream filehandle;
355                 int ableToOpen;
356                 
357                 if ((phylipfile == "") && (columnfile == "")) { cout << "When executing a read.dist you must enter a phylip or a column." << endl; errorFree = false; }
358                 else if ((phylipfile != "") && (columnfile != "")) { cout << "When executing a read.dist you must enter ONLY ONE of the following: phylip or column." << endl; errorFree = false; }
359                 
360                 if (columnfile != "") {
361                         if (namefile == "") {
362                                 cout << "You need to provide a namefile if you are going to use the column format." << endl;
363                                 errorFree = false; 
364                         }else {
365                                 ableToOpen = openInputFile(namefile, filehandle);
366                                 filehandle.close();
367                                 //unable to open
368                                 if (ableToOpen == 1) { errorFree = false; }
369                         }
370                 }
371         }
372         catch(exception& e) {
373                 cout << "Standard Error: " << e.what() << " has occurred in the ErrorCheck class Function validateReadDist. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
374                 exit(1);
375         }
376         catch(...) {
377                 cout << "An unknown error has occurred in the ErrorCheck class function validateReadDist. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
378                 exit(1);
379         }
380 }
381 /*******************************************************/
382
383 /******************************************************/
384 //This function checks to make sure the user entered appropriate
385 // format parameters on a parselistcommand
386 void ErrorCheck::validateParseFiles() {
387         try {
388                 ifstream filehandle;
389                 int ableToOpen;
390                 
391                 //checks for valid files
392         
393                 if (listfile == "") { cout << "When executing a read.otu for groups you must enter a list and a group." << endl; errorFree = false; }
394                 else if (groupfile == "") { cout << "When executing a read.otu for groups you must enter a list and a group." << endl; errorFree = false; }
395         
396                 //checks parameters on the read command
397                 if (listfile != "") {
398                         ableToOpen = openInputFile(listfile, filehandle);
399                         filehandle.close();
400                         if (ableToOpen == 1) { //unable to open
401                                 errorFree = false;
402                         }
403                         if (groupfile != "") {
404                                 ableToOpen = openInputFile(groupfile, filehandle);
405                                 filehandle.close();
406                                 if (ableToOpen == 1) { //unable to open
407                                         errorFree = false;;
408                                 }
409                         }
410                 }
411         }
412         catch(exception& e) {
413                 cout << "Standard Error: " << e.what() << " has occurred in the ErrorCheck class Function validateReadPhil. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
414                 exit(1);
415         }
416         catch(...) {
417                 cout << "An unknown error has occurred in the ErrorCheck class function validateReadPhil. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
418                 exit(1);
419         }
420 }
421 /*******************************************************/
422
423 /******************************************************/
424 //This function checks to make sure the user entered appropriate
425 // format parameters on a parselistcommand
426 void ErrorCheck::validateTreeFiles() {
427         try {
428                 ifstream filehandle;
429                 int ableToOpen;
430                 
431                 //checks for valid files
432         
433                 if (treefile == "") { cout << "When executing a read.tree you must enter a treefile and a groupfile." << endl; errorFree = false; }
434                 else if (groupfile == "") { cout << "When executing a read.tree you must enter a treefile and a groupfile." << endl; errorFree = false; }
435         
436                 //checks parameters on the read command
437                 if (treefile != "") {
438                         ableToOpen = openInputFile(treefile, filehandle);
439                         filehandle.close();
440                         if (ableToOpen == 1) { //unable to open
441                                 errorFree = false;
442                         }
443                         if (groupfile != "") {
444                                 ableToOpen = openInputFile(groupfile, filehandle);
445                                 filehandle.close();
446                                 if (ableToOpen == 1) { //unable to open
447                                         errorFree = false;;
448                                 }
449                         }
450                 }
451         }
452         catch(exception& e) {
453                 cout << "Standard Error: " << e.what() << " has occurred in the ErrorCheck class Function validateTreeFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
454                 exit(1);
455         }
456         catch(...) {
457                 cout << "An unknown error has occurred in the ErrorCheck class function validateTreeFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
458                 exit(1);
459         }
460 }
461
462 /*******************************************************/
463
464 /******************************************************/
465 //This function checks to make sure the user entered appropriate
466 // format parameters on a distfile read
467 void ErrorCheck::validateReadPhil() {
468         try {
469                 ifstream filehandle;
470                 int ableToOpen;
471                 
472                 //checks to make sure only one file type is given
473                 if (listfile != "") { 
474                         if ((rabundfile != "") || (sabundfile != "")) { 
475                                 cout << "When executing a read.otu you must enter ONLY ONE of the following: list, rabund or sabund." << endl; errorFree = false; }
476                 }else if (rabundfile != "") { 
477                         if ((listfile != "") || (sabundfile != "")) { 
478                                 cout << "When executing a read.otu you must enter ONLY ONE of the following: list, rabund or sabund." << endl; errorFree = false; }
479                 }else if (sabundfile != "") { 
480                         if ((listfile != "") || (rabundfile != "")) { 
481                                 cout << "When executing a read.otu you must enter ONLY ONE of the following: list, rabund or sabund." << endl; errorFree = false; }
482                 }else if ((listfile == "") && (rabundfile == "") && (sabundfile == "")) {
483                             cout << "When executing a read.otu you must enter one of the following: list, rabund or sabund." << endl; errorFree = false; 
484                 }
485                 
486                 //checks parameters on the read command
487                 if (orderfile != "") {
488                         ableToOpen = openInputFile(orderfile, filehandle);
489                         filehandle.close();
490                         if (ableToOpen == 1) { //unable to open
491                                 errorFree = false;
492                         }
493                 }       
494         }
495         catch(exception& e) {
496                 cout << "Standard Error: " << e.what() << " has occurred in the ErrorCheck class Function validateReadPhil. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
497                 exit(1);
498         }
499         catch(...) {
500                 cout << "An unknown error has occurred in the ErrorCheck class function validateReadPhil. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
501                 exit(1);
502         }
503 }
504 /*******************************************************/
505
506 /******************************************************/
507
508 void ErrorCheck::clear() {
509         //option definitions should go here...
510         phylipfile              =       "";
511         columnfile              =       "";
512         listfile                =       "";
513         rabundfile              =       "";
514         sabundfile              =       "";
515         namefile                =       "";
516         groupfile               =       ""; 
517         orderfile               =       "";
518         line                    =       "";
519         label                   =       "";
520         method                  =   "furthest";
521 }
522 /*******************************************************/
523
524 /******************************************************/
525