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