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