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