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