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