]> git.donarmstrong.com Git - mothur.git/blob - errorchecking.cpp
cc6cbf9d8d96ac90a55dc68d16d2ffa157079cb1
[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 #include <math.h>
12
13 /*******************************************************/
14
15 /******************************************************/
16
17 ErrorCheck::ErrorCheck() {
18         globaldata = GlobalData::getInstance();
19         validCommand = new ValidCommands();
20         validParameter = new ValidParameters();
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
41         
42         string p[] = {
43                 "phylip",              //0
44                 "column",             //1
45                 "list",               //2
46                 "rabund",             //3
47                 "sabund",             //4
48                 "name",               //5
49                 "order",              //6
50                 "group",              //7
51                 "fasta",              //8
52                 "treefile",           //9
53                 "cutoff",             //10
54                 "precision",          //11
55                 "iters",              //12
56                 "jumble",             //13
57                 "freq",               //14
58                 "method",             //15
59                 "fileroot",           //16
60                 "line",               //17
61                 "label",              //18
62                 "single",             //19
63                 "rarefaction",        //20
64                 "shared",             //21
65                 "summary",            //22
66                 "sharedrarefaction",  //23
67                 "sharedsummary",      //24
68                 "comparegroups",      //25
69                 "abund",              //26
70                 };
71         
72         string c0[] = {p[0],p[5],p[10],p[11]};
73         string c1[] = {p[2],p[6],p[7]}; 
74         string c2[] = {p[10],p[11],p[15]}; 
75         string c3[] = {p[8]};  
76         string c4[] = {p[14],p[17],p[18],p[19],p[26]};
77         string c5[] = {p[13],p[14],p[17],p[18],p[21],p[25]};
78         string c6[] = {""}; 
79         string c7[] = {""}; 
80         string c8[] = {""}; 
81         string c9[] = {p[12],p[14],p[17],p[18],p[20],p[26]};
82         string c10[] = {p[12],p[13],p[17],p[18],p[23]};
83         string c11[] = {p[17],p[18],p[22],p[26]};   
84         string c12[] =  {p[13],p[17],p[18],p[24]}; 
85         string c13[] = {""};    
86         
87         vector<string> v0 (c0, c0+sizeof(c0)/sizeof(string)); 
88         vector<string> v1 (c1, c1+sizeof(c1)/sizeof(string));
89         vector<string> v2 (c2, c2+sizeof(c2)/sizeof(string));
90         vector<string> v3 (c3, c3+sizeof(c3)/sizeof(string));
91         vector<string> v4 (c4, c4+sizeof(c4)/sizeof(string));
92         vector<string> v5 (c5, c5+sizeof(c5)/sizeof(string));
93         vector<string> v6 (c6, c6+sizeof(c6)/sizeof(string));
94         vector<string> v7 (c7, c7+sizeof(c7)/sizeof(string));
95         vector<string> v8 (c8, c8+sizeof(c8)/sizeof(string));
96         vector<string> v9 (c9, c9+sizeof(c9)/sizeof(string));
97         vector<string> v10 (c10, c10+sizeof(c10)/sizeof(string));
98         vector<string> v11 (c11, c11+sizeof(c11)/sizeof(string));
99         vector<string> v12 (c12, c12+sizeof(c12)/sizeof(string));
100         vector<string> v13 (c13, c13+sizeof(c13)/sizeof(string));
101         
102         vector<vector<string> > allCommands;
103         allCommands.push_back(v0);
104         allCommands.push_back(v1);
105         allCommands.push_back(v2);
106         allCommands.push_back(v3);
107         allCommands.push_back(v4);
108         allCommands.push_back(v5);
109         allCommands.push_back(v6);
110         allCommands.push_back(v7);
111         allCommands.push_back(v8);
112         allCommands.push_back(v9);
113         allCommands.push_back(v10);
114         allCommands.push_back(v11);
115         allCommands.push_back(v12);
116         allCommands.push_back(v13);
117         
118         string commands[] = {
119         "read.dist",          //0
120         "read.otu",           //1
121         "cluster",            //2
122         "deconvolute",        //3
123         "collect.single",     //4
124         "collect.shared",     //5
125         "get.group",          //6
126         "get.label",          //7
127         "get.line",           //8
128         "rarefaction.single", //9
129         "rarefaction.shared", //10
130         "summary.single",     //11
131         "summary.shared",     //12
132         "quit"                //13
133         };
134         
135         for(int i = 0; i < allCommands.size(); i++)
136                 commandParameters[commands[i]] = allCommands.at(i);
137         
138          //{Lowerbound(piSent if no lowerbound), Upperbound(piSent if no upperbound), 1 if only the first 2 values, 0 if greater than, 0 if less than};
139         piSent = 3.14159;
140         double ip0[] = {10, piSent, 0, 1, 0};
141         double ip1[] = {10, piSent, 0, 1, 0};
142         double ip2[] = {0, 1, 1, 0, 0};
143         double ip3[] =  {1, piSent, 0, 0, 0};
144         double ip4[] = {1, piSent, 0, 1, 0};
145         double ip5[] = {5, piSent, 0, 1, 0};
146         
147         vector<double> ipv0 (ip0, ip0+sizeof(ip0)/sizeof(double)); 
148         vector<double> ipv1 (ip1, ip1+sizeof(ip1)/sizeof(double)); 
149         vector<double> ipv2 (ip2, ip2+sizeof(ip2)/sizeof(double)); 
150         vector<double> ipv3 (ip3, ip3+sizeof(ip3)/sizeof(double)); 
151         vector<double> ipv4 (ip4, ip4+sizeof(ip4)/sizeof(double)); 
152         vector<double> ipv5 (ip5, ip5+sizeof(ip5)/sizeof(double));
153
154         intParams[p[11]] = ipv0;
155         intParams[p[12]] = ipv1;
156         intParams[p[13]] = ipv2;
157         intParams[p[14]] = ipv3;
158         intParams[p[17]] = ipv4;
159         intParams[p[26]] = ipv5;
160         
161         randomtree = globaldata->getRandomTree();
162         sharedfile = globaldata->getSharedFile();
163 }
164
165 /*******************************************************/
166
167 /******************************************************/
168
169 ErrorCheck::~ErrorCheck() {}
170
171 /*******************************************************/
172
173 /******************************************************/
174
175 bool ErrorCheck::checkInput(string input) {
176                 errorFree = true;
177                 clear();
178                 
179                 //refresh variable
180                 refresh();
181                 
182                 //get command name and parameters
183                 int openParen = input.find_first_of('(');
184                 int closeParen = input.find_last_of(')');
185
186                 if(openParen != -1 && closeParen != -1){                        
187                         commandName = input.substr(0, openParen);   //commandName contains everything before "("
188                         optionText = input.substr(openParen+1, closeParen-openParen-1); //optionString contains everything between "(" and ")".
189                 }else if (openParen == -1) { //there is no parenthesis
190                         cout << input << " is not a valid command. You are missing the ()." << endl;
191                         return false;
192                 }
193                 
194                 //is it a valid command
195                 if (validCommand->isValidCommand(commandName) != true) { return false; }
196                 
197                 string parameter, value;
198                 
199                 //reads in parameters and values
200                 if((optionText != "") && (commandName != "help")){
201                         while((optionText.find_first_of(',') != -1) && (errorFree)) {  //while there are parameters
202                                 splitAtComma(value, optionText);
203                                 splitAtEquals(parameter, value);
204                                 
205                                 //is it a valid parameter
206                                 if (validParameter->isValidParameter(parameter) != true) { return false; }
207                                 if(!validCommandParameter(parameter,commandName)) { 
208                                         cout << "'" << parameter << "' is not a valid parameter for the " << commandName << " command.\n";
209                                         return false; 
210                                 }
211                                 if(!validParameterValue(value, parameter)) {
212                                         if(parameter.compare("precision") == 0)
213                                                 cout << "The precision parameter can only take powers of 10 as a value (e.g. 10,1000,1000, etc.)\n";
214                                         else {
215                                         vector<double> bounds = intParams[parameter];
216                                         double a = bounds.at(0);
217                                         double b = bounds.at(1);
218                                         double c = bounds.at(2);
219                                         double d = bounds.at(3);
220                                         double e = bounds.at(4);
221                                         cout << "The '" << parameter << "' parameter needs to be ";
222                                         if(c == 1)
223                                                         cout << "either '" << a << "' or '" << b << "'.\n";
224                                         else
225                                         {
226                                                 if(a != piSent)
227                                                 {
228                                                         cout << ">";
229                                                         if(d != 0)
230                                                                 cout << "=";
231                                                         cout << " '" << a << "'";
232                                                 }
233                                                 if(b == piSent)
234                                                         cout << ".\n";
235                                                 else if(a != piSent)
236                                                         cout << " and ";
237                                                 if(b != piSent)
238                                                 {
239                                                         cout << "<";
240                                                         if(e != 0)
241                                                                 cout << "=";
242                                                         cout << " '" << b << ".\n";
243                                                 }
244                                         }
245                                         }
246                                         return false;
247                                 }
248
249                                 if (parameter == "phylip" )             { phylipfile = value; }
250                                 if (parameter == "column" )             { columnfile = value; }
251                                 if (parameter == "list" )               { listfile = value; }
252                                 if (parameter == "rabund" )             { rabundfile = value; }
253                                 if (parameter == "sabund" )             { sabundfile = value; }
254                                 if (parameter == "name" )               { namefile = value; }
255                                 if (parameter == "order" )              { orderfile = value; }
256                                 if (parameter == "fasta" )              { fastafile = value; }
257                                 if (parameter == "tree" )               { treefile = value; }
258                                 if (parameter == "group" )              { groupfile = value; }
259                                 if (parameter == "shared" )             { sharedfile = value; }
260                                 if (parameter == "cutoff" )                     { cutoff = value; }
261                                 if (parameter == "precision" )          { precision = value; }
262                                 if (parameter == "iters" )                      { iters = value; }
263                                 if (parameter == "jumble" )                     { jumble = value; }
264                                 if (parameter == "freq" )                       { freq = value; }
265                                 if (parameter == "method" )                     { method = value; }
266                                 if (parameter == "fileroot" )           { fileroot = value; }
267                                 if (parameter == "line" )                       { line = value; }
268                                 if (parameter == "label" )                      { label = value; }
269                                 if (parameter == "abund" )          { abund = value; }
270                                 if (parameter == "random" )                     { randomtree = value;   }
271
272                                 if (parameter == "comparegroups") { //stores groups to be compared
273                                         sharedGroups.clear(); //clears out old values
274                                         globaldata->splitAtDash(value, sharedGroups);
275                                 }
276
277                         }
278                         
279                         //gets the last parameter and value
280                         if (errorFree)  { //gets the last parameter and value
281                                 value = optionText;
282                                 splitAtEquals(parameter, value);
283                                 //is it a valid parameter
284                                 if (validParameter->isValidParameter(parameter) != true) { return false; }
285                                 if(!validCommandParameter(parameter,commandName)) { 
286                                         cout << "'" << parameter << "' is not a valid parameter for the " << commandName << " command.\n";
287                                         return false; 
288                                 }
289                                 if(!validParameterValue(value, parameter)) {
290                                         if(parameter.compare("precision") == 0)
291                                                 cout << "The precision parameter can only take powers of 10 as a value (e.g. 10,1000,1000, etc.)\n";
292                                         else {
293                                         vector<double> bounds = intParams[parameter];
294                                         double a = bounds.at(0);
295                                         double b = bounds.at(1);
296                                         double c = bounds.at(2);
297                                         double d = bounds.at(3);
298                                         double e = bounds.at(4);
299                                         cout << "The '" << parameter << "' parameter needs to be ";
300                                         if(c == 1)
301                                                         cout << "either '" << a << "' or '" << b << "'.\n";
302                                         else
303                                         {
304                                                 if(a != piSent)
305                                                 {
306                                                         cout << ">";
307                                                         if(d != 0)
308                                                                 cout << "=";
309                                                         cout << " '" << a << "'";
310                                                 }
311                                                 if(b == piSent)
312                                                         cout << ".\n";
313                                                 else if(a != piSent)
314                                                         cout << " and ";
315                                                 if(b != piSent)
316                                                 {
317                                                         cout << "<";
318                                                         if(e != 0)
319                                                                 cout << "=";
320                                                         cout << " '" << b << ".\n";
321                                                 }
322                                         }
323                                         }
324                                         return false;
325                                 }
326                                 if (parameter == "phylip" )             { phylipfile = value; }
327                                 if (parameter == "column" )             { columnfile = value; }                         
328                                 if (parameter == "list" )               { listfile = value; }
329                                 if (parameter == "rabund" )             { rabundfile = value; }
330                                 if (parameter == "sabund" )             { sabundfile = value; }
331                                 if (parameter == "name" )               { namefile = value; }
332                                 if (parameter == "order" )              { orderfile = value; }
333                                 if (parameter == "group" )              { groupfile = value; }
334                                 if (parameter == "shared" )             { sharedfile = value; }
335                                 if (parameter == "fasta" )              { fastafile = value; }
336                                 if (parameter == "tree" )               { treefile = value; }
337                                 if (parameter == "cutoff" )                     { cutoff = value; }
338                                 if (parameter == "precision" )          { precision = value; }
339                                 if (parameter == "iters" )                      { iters = value; }
340                                 if (parameter == "jumble" )                     { jumble = value; }
341                                 if (parameter == "freq" )                       { freq = value; }
342                                 if (parameter == "method" )                     { method = value; }
343                                 if (parameter == "fileroot" )           { fileroot = value; }
344                                 if (parameter == "line" )                       { line = value; }
345                                 if (parameter == "label" )                      { label = value; }
346                                 if (parameter == "random" )                     { randomtree = value;   }
347                                 if (parameter == "abund" )          { abund = value; }
348
349                                 if (parameter == "comparegroups") { //stores groups to be compared
350                                         sharedGroups.clear(); //clears out old values
351                                         globaldata->splitAtDash(value, sharedGroups);
352                                 }
353                         }
354                 }
355                 
356                 //make sure the user does not use both the line and label parameters
357                 if ((line != "") && (label != "")) { cout << "You may use either the line or label parameters, but not both." << endl; return false; }
358                 
359                 if (commandName == "read.dist") { 
360                         validateReadFiles();
361                         validateReadDist();
362                 }else if (commandName == "read.otu") { 
363                         //you want to do shared commands
364                         if ((listfile != "") && (groupfile != ""))      {
365                                 validateParseFiles(); //checks the listfile and groupfile parameters
366                         }else { //you want to do single commands
367                                 validateReadFiles();
368                                 validateReadPhil();
369                         }
370                 }else if (commandName == "read.shared") { 
371                         //you want to do shared commands with just the shared file
372                         validateReadFiles();
373                 }else if (commandName == "read.tree") { 
374                         validateTreeFiles(); //checks the treefile and groupfile parameters
375                 }else if (commandName == "deconvolute") {
376                         if (fastafile == "") { cout << "You must enter a fastafile with the deconvolute() command." << endl; return false; }
377                         validateReadFiles();
378                 }
379                 
380                 //are you trying to cluster before you have read something                      
381                 if ((commandName == "cluster") && (globaldata->getSparseMatrix() == NULL) ||
382                         (commandName == "cluster") && (globaldata->getListVector() == NULL)) {
383                                 cout << "Before you use the cluster command, you first need to read in a distance matrix." << endl; 
384                                 errorFree = false;
385                 } 
386                 
387                 if (commandName == "parsimony") {
388                         //are you trying to use parsimony without reading a tree or saying you want random distribution
389                         if (randomtree == "")  {
390                                 if (globaldata->gTree.size() == 0) {
391                                         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;  }
392                         }
393                 }
394                 
395                 if ((commandName == "unifrac.weighted") || (commandName == "unifrac.unweighted")) {
396                         if (globaldata->gTree.size() == 0) {//no trees were read
397                                 cout << "You must execute the read.tree command, before you may execute the unifrac.weighted or unifrac.unweighted command." << endl; return false;  }
398                 }
399                 
400                 //check for valid method
401                 if(commandName == "get.group") {
402                         if ((globaldata->getGroupFile() == "")) { cout << "You must read a group before you can use the get.group command." << endl; return false; }
403                 }
404                 if (commandName == "get.label" || commandName == "get.line") {
405                         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; }
406                 }
407                 if (commandName == "cluster") {
408                         if ((method == "furthest") || (method == "nearest") || (method == "average")) { }
409                         else {cout << "Not a valid clustering method.  Valid clustering algorithms are furthest, nearest or average." << endl; return false; }
410                 }
411                 
412                 if ((commandName == "collect.single") || (commandName == "rarefaction.single") || (commandName == "summary.single") ){ 
413                         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; }
414                 }
415                 
416                 if ((commandName == "collect.shared") || (commandName == "rarefaction.shared") || (commandName == "summary.shared") ){ 
417                         if (globaldata->getSharedFile() == "") {
418                                 if (globaldata->getListFile() == "") { cout << "You must read a list and a group, or a shared before you can use the collect.shared, rarefaction.shared or summary.shared commands." << endl; return false; }
419                                 else if (globaldata->getGroupFile() == "") { cout << "You must read a list and a group, or a shared before you can use the collect.shared, rarefaction.shared or summary.shared commands." << endl; return false; }
420                         }
421                 }
422
423                 globaldata->clearAbund();
424
425                 return errorFree;
426 }
427
428 /*******************************************************/
429
430 /******************************************************/
431 //This function checks to make sure the user entered a file to 
432 // read and that the file exists and can be opened.
433 void ErrorCheck::validateReadFiles() {
434         try {
435                 //Validating files for read
436                 ifstream filehandle;
437                 int ableToOpen;
438         
439                 //are we reading a phylipfile
440                 if (phylipfile != "") {
441                         ableToOpen = openInputFile(phylipfile, filehandle);
442                         filehandle.close();
443                         //unable to open
444                         if (ableToOpen == 1) { errorFree = false; }
445                         else { globaldata->inputFileName = phylipfile; }
446                 //are we reading a phylipfile
447                 }else if (columnfile != "") {
448                         ableToOpen = openInputFile(columnfile, filehandle);
449                         filehandle.close();
450                         //unable to open
451                         if (ableToOpen == 1) { errorFree = false; }
452                         else { globaldata->inputFileName = columnfile; }
453                 //are we reading a listfile
454                 }else if (listfile!= "") {
455                         ableToOpen = openInputFile(listfile, filehandle);
456                         filehandle.close();
457                         //unable to open
458                         if (ableToOpen == 1) {  errorFree = false; }
459                         else { globaldata->inputFileName = listfile; }
460                 //are we reading a rabundfile
461                 }else if (rabundfile != "") {
462                         ableToOpen = openInputFile(rabundfile, filehandle);
463                         filehandle.close();
464                         //unable to open
465                         if (ableToOpen == 1) {  errorFree = false; }
466                         else { globaldata->inputFileName = rabundfile; }
467                 //are we reading a sabundfile
468                 }else if (sabundfile != "") {
469                         ableToOpen = openInputFile(sabundfile, filehandle);
470                         filehandle.close();
471                         //unable to open
472                         if (ableToOpen == 1) {  errorFree = false; }
473                         else { globaldata->inputFileName = sabundfile; }
474                 }else if (fastafile != "") {
475                         ableToOpen = openInputFile(fastafile, filehandle);
476                         filehandle.close();
477                         //unable to open
478                         if (ableToOpen == 1) {  errorFree = false; }
479                         else { globaldata->inputFileName = fastafile; }
480                 }else if (sharedfile != "") {
481                         ableToOpen = openInputFile(sharedfile, filehandle);
482                         filehandle.close();
483                         //unable to open
484                         if (ableToOpen == 1) {  errorFree = false; }
485                         else { globaldata->inputFileName = sharedfile; }
486                 }else{ //no file given
487                         errorFree = false;
488                 }
489         }
490         catch(exception& e) {
491                 cout << "Standard Error: " << e.what() << " has occurred in the ErrorCheck class Function validateReadFiles. 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 validateReadFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
496                 exit(1);
497         }
498         
499 }
500 /*******************************************************/
501
502 /******************************************************/
503 //This function checks to see if the given paramter
504 //is a valid paramter for the given command.
505 bool ErrorCheck::validCommandParameter(string parameter, string commandName) {
506         try {
507                 for(int i = 0; i < commandParameters[commandName].size(); i++)
508                         if(parameter.compare(commandParameters[commandName][i]) == 0)
509                                 return true;
510                 return false;
511         }
512                 
513         catch(exception& e) {
514                 cout << "Standard Error: " << e.what() << " has occurred in the ErrorCheck class Function validateReadFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
515                 exit(1);
516         }
517         catch(...) {
518                 cout << "An unknown error has occurred in the ErrorCheck class function validateReadFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
519                 exit(1);
520         }
521 }
522 /*******************************************************/
523
524 /******************************************************/
525 //This function checks to see if the given paramter value
526 //is convertable into an int if that parameter requires it.
527 bool ErrorCheck::validParameterValue(string value, string parameter) {
528         try {
529                 int pVal;
530                 if(intParams.count(parameter) == 1)
531                 {
532                         vector<double> bounds = intParams[parameter];
533                         bool valid = convertTest(value, pVal);
534                         if(!valid)
535                                 return false;
536                         if(parameter.compare("precision") == 0)
537                         {
538                                 double logNum = log10((double)pVal);
539                                 double diff = (double)((int)logNum - logNum);
540                                 if(diff != 0)
541                                         return false;
542                         }
543                         double a = bounds.at(0);
544                         double b = bounds.at(1);
545                         double c = bounds.at(2);
546                         double d = bounds.at(3);
547                         double e = bounds.at(4);
548                         bool a0 = pVal > a;
549                         bool a1 = pVal >= a;
550                         bool b0 = pVal < b;
551                         bool b1 = pVal <= b;
552                         
553                         if(c != 1)
554                         {
555                                 if(a == piSent && b == piSent)
556                                         return true;
557                                 if(a != piSent && b == piSent)
558                                 {
559                                         if(d == 0)
560                                                 return a0;
561                                         else
562                                                 return a1;
563                                 }
564                                 else if(a == piSent && b != piSent)
565                                 {
566                                         if(e == 0)
567                                                 return b0;
568                                         else
569                                                 return b1;
570                                 }
571                                 else
572                                 {
573                                         if(d == 0 && e == 0)
574                                                 return (a0 && b0);
575                                         else if(d == 0 && e == 1)
576                                                 return (a0 && b1);
577                                         else if(d == 1 && e == 0)
578                                                 return (a1 && b0);
579                                         else
580                                                 return (a1 && b1);
581                                 }
582                         }
583                         else
584                         {
585                                 if(a == piSent && b == piSent)
586                                         return true;
587                                 if(a != piSent && b == piSent)
588                                         return (pVal == a);
589                                 else if(a == piSent && b != piSent)
590                                         return (pVal == b);
591                                 else
592                                         return (pVal == a || pVal == b);
593                         }
594                 }
595                 return true;
596         }
597                 
598         catch(exception& e) {
599                 cout << "Standard Error: " << e.what() << " has occurred in the ErrorCheck class Function validateReadFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
600                 exit(1);
601         }
602         catch(...) {
603                 cout << "An unknown error has occurred in the ErrorCheck class function validateReadFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
604                 exit(1);
605         }
606 }               
607 /*******************************************************/
608
609 /******************************************************/
610 //This function checks to make sure the user entered appropriate
611 // format parameters on a distfile read
612 void ErrorCheck::validateReadDist() {
613         try {
614                 ifstream filehandle;
615                 int ableToOpen;
616                 
617                 if ((phylipfile == "") && (columnfile == "")) { cout << "When executing a read.dist you must enter a phylip or a column." << endl; errorFree = false; }
618                 else if ((phylipfile != "") && (columnfile != "")) { cout << "When executing a read.dist you must enter ONLY ONE of the following: phylip or column." << endl; errorFree = false; }
619                 
620                 if (columnfile != "") {
621                         if (namefile == "") {
622                                 cout << "You need to provide a namefile if you are going to use the column format." << endl;
623                                 errorFree = false; 
624                         }else {
625                                 ableToOpen = openInputFile(namefile, filehandle);
626                                 filehandle.close();
627                                 //unable to open
628                                 if (ableToOpen == 1) { errorFree = false; }
629                         }
630                 }
631         }
632         catch(exception& e) {
633                 cout << "Standard Error: " << e.what() << " has occurred in the ErrorCheck class Function validateReadDist. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
634                 exit(1);
635         }
636         catch(...) {
637                 cout << "An unknown error has occurred in the ErrorCheck class function validateReadDist. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
638                 exit(1);
639         }
640 }
641 /*******************************************************/
642
643 /******************************************************/
644 //This function checks to make sure the user entered appropriate
645 // format parameters on a parselistcommand
646 void ErrorCheck::validateParseFiles() {
647         try {
648                 ifstream filehandle;
649                 int ableToOpen;
650                 
651                 //checks for valid files
652         
653                 if (listfile == "") { cout << "When executing a read.otu for groups you must enter a list and a group." << endl; errorFree = false; }
654                 else if (groupfile == "") { cout << "When executing a read.otu for groups you must enter a list and a group." << endl; errorFree = false; }
655         
656                 //checks parameters on the read command
657                 if (listfile != "") {
658                         ableToOpen = openInputFile(listfile, filehandle);
659                         filehandle.close();
660                         if (ableToOpen == 1) { //unable to open
661                                 errorFree = false;
662                         }
663                         if (groupfile != "") {
664                                 ableToOpen = openInputFile(groupfile, filehandle);
665                                 filehandle.close();
666                                 if (ableToOpen == 1) { //unable to open
667                                         errorFree = false;;
668                                 }
669                         }
670                 }
671         }
672         catch(exception& e) {
673                 cout << "Standard Error: " << e.what() << " has occurred in the ErrorCheck class Function validateReadPhil. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
674                 exit(1);
675         }
676         catch(...) {
677                 cout << "An unknown error has occurred in the ErrorCheck class function validateReadPhil. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
678                 exit(1);
679         }
680 }
681 /*******************************************************/
682
683 /******************************************************/
684 //This function checks to make sure the user entered appropriate
685 // format parameters on a parselistcommand
686 void ErrorCheck::validateTreeFiles() {
687         try {
688                 ifstream filehandle;
689                 int ableToOpen;
690                 
691                 //checks for valid files
692         
693                 if (treefile == "") { cout << "When executing a read.tree you must enter a treefile and a groupfile." << endl; errorFree = false; }
694                 else if (groupfile == "") { cout << "When executing a read.tree you must enter a treefile and a groupfile." << endl; errorFree = false; }
695         
696                 //checks parameters on the read command
697                 if (treefile != "") {
698                         ableToOpen = openInputFile(treefile, filehandle);
699                         filehandle.close();
700                         if (ableToOpen == 1) { //unable to open
701                                 errorFree = false;
702                         }
703                         if (groupfile != "") {
704                                 ableToOpen = openInputFile(groupfile, filehandle);
705                                 filehandle.close();
706                                 if (ableToOpen == 1) { //unable to open
707                                         errorFree = false;;
708                                 }
709                         }
710                 }
711         }
712         catch(exception& e) {
713                 cout << "Standard Error: " << e.what() << " has occurred in the ErrorCheck class Function validateTreeFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
714                 exit(1);
715         }
716         catch(...) {
717                 cout << "An unknown error has occurred in the ErrorCheck class function validateTreeFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
718                 exit(1);
719         }
720 }
721
722 /*******************************************************/
723
724 /******************************************************/
725 //This function checks to make sure the user entered appropriate
726 // format parameters on a distfile read
727 void ErrorCheck::validateReadPhil() {
728         try {
729                 ifstream filehandle;
730                 int ableToOpen;
731                 
732                 //checks to make sure only one file type is given
733                 if (listfile != "") { 
734                         if ((rabundfile != "") || (sabundfile != "")) { 
735                                 cout << "When executing a read.otu you must enter ONLY ONE of the following: list, rabund or sabund." << endl; errorFree = false; }
736                 }else if (rabundfile != "") { 
737                         if ((listfile != "") || (sabundfile != "")) { 
738                                 cout << "When executing a read.otu you must enter ONLY ONE of the following: list, rabund or sabund." << endl; errorFree = false; }
739                 }else if (sabundfile != "") { 
740                         if ((listfile != "") || (rabundfile != "")) { 
741                                 cout << "When executing a read.otu you must enter ONLY ONE of the following: list, rabund or sabund." << endl; errorFree = false; }
742                 }else if ((listfile == "") && (rabundfile == "") && (sabundfile == "")) {
743                             cout << "When executing a read.otu you must enter one of the following: list, rabund or sabund." << endl; errorFree = false; 
744                 }
745                 
746                 //checks parameters on the read command
747                 if (orderfile != "") {
748                         ableToOpen = openInputFile(orderfile, filehandle);
749                         filehandle.close();
750                         if (ableToOpen == 1) { //unable to open
751                                 errorFree = false;
752                         }
753                 }       
754         }
755         catch(exception& e) {
756                 cout << "Standard Error: " << e.what() << " has occurred in the ErrorCheck class Function validateReadPhil. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
757                 exit(1);
758         }
759         catch(...) {
760                 cout << "An unknown error has occurred in the ErrorCheck class function validateReadPhil. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
761                 exit(1);
762         }
763 }
764 /*******************************************************/
765
766 /******************************************************/
767
768 void ErrorCheck::clear() {
769         //option definitions should go here...
770         phylipfile              =       "";
771         columnfile              =       "";
772         listfile                =       "";
773         rabundfile              =       "";
774         sabundfile              =       "";
775         namefile                =       "";
776         groupfile               =       ""; 
777         orderfile               =       "";
778         sharedfile              =       "";
779         line                    =       "";
780         label                   =       "";
781         method                  =   "furthest";
782 }
783 /*******************************************************/
784
785 /******************************************************/
786