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