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