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