]> git.donarmstrong.com Git - mothur.git/blob - errorchecking.cpp
adding treeclimber and unifrac pieces
[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 == "treefile" )           { 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 == "treefile" )           { 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                 //check for valid method
258                 if (commandName == "cluster") {
259                         if ((method == "furthest") || (method == "nearest") || (method == "average")) { }
260                         else {cout << "Not a valid clustering method.  Valid clustering algorithms are furthest, nearest or average." << endl; return false; }
261                 }
262                 
263                 if ((commandName == "collect.single") || (commandName == "rarefaction.single") || (commandName == "summary.single") ){ 
264                         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; }
265                 }
266                 
267                 if ((commandName == "collect.shared") || (commandName == "rarefaction.shared") || (commandName == "summary.shared") ){ 
268                         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; }
269                         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; }
270                 }
271  
272                 
273                 return errorFree;
274 }
275
276 /*******************************************************/
277
278 /******************************************************/
279 //This function checks to make sure the user entered a file to 
280 // read and that the file exists and can be opened.
281 void ErrorCheck::validateReadFiles() {
282         try {
283                 //Validating files for read
284                 ifstream filehandle;
285                 int ableToOpen;
286         
287                 //are we reading a phylipfile
288                 if (phylipfile != "") {
289                         ableToOpen = openInputFile(phylipfile, filehandle);
290                         filehandle.close();
291                         //unable to open
292                         if (ableToOpen == 1) { errorFree = false; }
293                         else { globaldata->inputFileName = phylipfile; }
294                 //are we reading a phylipfile
295                 }else if (columnfile != "") {
296                         ableToOpen = openInputFile(columnfile, filehandle);
297                         filehandle.close();
298                         //unable to open
299                         if (ableToOpen == 1) { errorFree = false; }
300                         else { globaldata->inputFileName = columnfile; }
301                 //are we reading a listfile
302                 }else if (listfile!= "") {
303                         ableToOpen = openInputFile(listfile, filehandle);
304                         filehandle.close();
305                         //unable to open
306                         if (ableToOpen == 1) {  errorFree = false; }
307                         else { globaldata->inputFileName = listfile; }
308                 //are we reading a rabundfile
309                 }else if (rabundfile != "") {
310                         ableToOpen = openInputFile(rabundfile, filehandle);
311                         filehandle.close();
312                         //unable to open
313                         if (ableToOpen == 1) {  errorFree = false; }
314                         else { globaldata->inputFileName = rabundfile; }
315                 //are we reading a sabundfile
316                 }else if (sabundfile != "") {
317                         ableToOpen = openInputFile(sabundfile, filehandle);
318                         filehandle.close();
319                         //unable to open
320                         if (ableToOpen == 1) {  errorFree = false; }
321                         else { globaldata->inputFileName = sabundfile; }
322                 }else if (fastafile != "") {
323                         ableToOpen = openInputFile(fastafile, filehandle);
324                         filehandle.close();
325                         //unable to open
326                         if (ableToOpen == 1) {  errorFree = false; }
327                         else { globaldata->inputFileName = fastafile; }
328                 }else{ //no file given
329                         errorFree = false;
330                 }
331         }
332         catch(exception& e) {
333                 cout << "Standard Error: " << e.what() << " has occurred in the ErrorCheck class Function validateReadFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
334                 exit(1);
335         }
336         catch(...) {
337                 cout << "An unknown error has occurred in the ErrorCheck class function validateReadFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
338                 exit(1);
339         }
340         
341 }
342 /*******************************************************/
343
344 /******************************************************/
345 //This function checks to make sure the user entered appropriate
346 // format parameters on a distfile read
347 void ErrorCheck::validateReadDist() {
348         try {
349                 ifstream filehandle;
350                 int ableToOpen;
351                 
352                 if ((phylipfile == "") && (columnfile == "")) { cout << "When executing a read.dist you must enter a phylip or a column." << endl; errorFree = false; }
353                 else if ((phylipfile != "") && (columnfile != "")) { cout << "When executing a read.dist you must enter ONLY ONE of the following: phylip or column." << endl; errorFree = false; }
354                 
355                 if (columnfile != "") {
356                         if (namefile == "") {
357                                 cout << "You need to provide a namefile if you are going to use the column format." << endl;
358                                 errorFree = false; 
359                         }else {
360                                 ableToOpen = openInputFile(namefile, filehandle);
361                                 filehandle.close();
362                                 //unable to open
363                                 if (ableToOpen == 1) { errorFree = false; }
364                         }
365                 }
366         }
367         catch(exception& e) {
368                 cout << "Standard Error: " << e.what() << " has occurred in the ErrorCheck class Function validateReadDist. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
369                 exit(1);
370         }
371         catch(...) {
372                 cout << "An unknown error has occurred in the ErrorCheck class function validateReadDist. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
373                 exit(1);
374         }
375 }
376 /*******************************************************/
377
378 /******************************************************/
379 //This function checks to make sure the user entered appropriate
380 // format parameters on a parselistcommand
381 void ErrorCheck::validateParseFiles() {
382         try {
383                 ifstream filehandle;
384                 int ableToOpen;
385                 
386                 //checks for valid files
387         
388                 if (listfile == "") { cout << "When executing a read.otu for groups you must enter a list and a group." << endl; errorFree = false; }
389                 else if (groupfile == "") { cout << "When executing a read.otu for groups you must enter a list and a group." << endl; errorFree = false; }
390         
391                 //checks parameters on the read command
392                 if (listfile != "") {
393                         ableToOpen = openInputFile(listfile, filehandle);
394                         filehandle.close();
395                         if (ableToOpen == 1) { //unable to open
396                                 errorFree = false;
397                         }
398                         if (groupfile != "") {
399                                 ableToOpen = openInputFile(groupfile, filehandle);
400                                 filehandle.close();
401                                 if (ableToOpen == 1) { //unable to open
402                                         errorFree = false;;
403                                 }
404                         }
405                 }
406         }
407         catch(exception& e) {
408                 cout << "Standard Error: " << e.what() << " has occurred in the ErrorCheck class Function validateReadPhil. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
409                 exit(1);
410         }
411         catch(...) {
412                 cout << "An unknown error has occurred in the ErrorCheck class function validateReadPhil. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
413                 exit(1);
414         }
415 }
416 /*******************************************************/
417
418 /******************************************************/
419 //This function checks to make sure the user entered appropriate
420 // format parameters on a parselistcommand
421 void ErrorCheck::validateTreeFiles() {
422         try {
423                 ifstream filehandle;
424                 int ableToOpen;
425                 
426                 //checks for valid files
427         
428                 if (treefile == "") { cout << "When executing a read.tree you must enter a treefile and a groupfile." << endl; errorFree = false; }
429                 else if (groupfile == "") { cout << "When executing a read.tree you must enter a treefile and a groupfile." << endl; errorFree = false; }
430         
431                 //checks parameters on the read command
432                 if (treefile != "") {
433                         ableToOpen = openInputFile(treefile, filehandle);
434                         filehandle.close();
435                         if (ableToOpen == 1) { //unable to open
436                                 errorFree = false;
437                         }
438                         if (groupfile != "") {
439                                 ableToOpen = openInputFile(groupfile, filehandle);
440                                 filehandle.close();
441                                 if (ableToOpen == 1) { //unable to open
442                                         errorFree = false;;
443                                 }
444                         }
445                 }
446         }
447         catch(exception& e) {
448                 cout << "Standard Error: " << e.what() << " has occurred in the ErrorCheck class Function validateTreeFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
449                 exit(1);
450         }
451         catch(...) {
452                 cout << "An unknown error has occurred in the ErrorCheck class function validateTreeFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
453                 exit(1);
454         }
455 }
456
457 /*******************************************************/
458
459 /******************************************************/
460 //This function checks to make sure the user entered appropriate
461 // format parameters on a distfile read
462 void ErrorCheck::validateReadPhil() {
463         try {
464                 ifstream filehandle;
465                 int ableToOpen;
466                 
467                 //checks to make sure only one file type is given
468                 if (listfile != "") { 
469                         if ((rabundfile != "") || (sabundfile != "")) { 
470                                 cout << "When executing a read.otu you must enter ONLY ONE of the following: list, rabund or sabund." << endl; errorFree = false; }
471                 }else if (rabundfile != "") { 
472                         if ((listfile != "") || (sabundfile != "")) { 
473                                 cout << "When executing a read.otu you must enter ONLY ONE of the following: list, rabund or sabund." << endl; errorFree = false; }
474                 }else if (sabundfile != "") { 
475                         if ((listfile != "") || (rabundfile != "")) { 
476                                 cout << "When executing a read.otu you must enter ONLY ONE of the following: list, rabund or sabund." << endl; errorFree = false; }
477                 }else if ((listfile == "") && (rabundfile == "") && (sabundfile == "")) {
478                             cout << "When executing a read.otu you must enter one of the following: list, rabund or sabund." << endl; errorFree = false; 
479                 }
480                 
481                 //checks parameters on the read command
482                 if (orderfile != "") {
483                         ableToOpen = openInputFile(orderfile, filehandle);
484                         filehandle.close();
485                         if (ableToOpen == 1) { //unable to open
486                                 errorFree = false;
487                         }
488                 }       
489         }
490         catch(exception& e) {
491                 cout << "Standard Error: " << e.what() << " has occurred in the ErrorCheck class Function validateReadPhil. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
492                 exit(1);
493         }
494         catch(...) {
495                 cout << "An unknown error has occurred in the ErrorCheck class function validateReadPhil. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
496                 exit(1);
497         }
498 }
499 /*******************************************************/
500
501 /******************************************************/
502
503 void ErrorCheck::clear() {
504         //option definitions should go here...
505         phylipfile              =       "";
506         columnfile              =       "";
507         listfile                =       "";
508         rabundfile              =       "";
509         sabundfile              =       "";
510         namefile                =       "";
511         groupfile               =       ""; 
512         orderfile               =       "";
513         line                    =       "";
514         label                   =       "";
515         method                  =   "furthest";
516 }
517 /*******************************************************/
518
519 /******************************************************/
520