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