]> git.donarmstrong.com Git - mothur.git/blob - errorchecking.cpp
fixed summary.shared bug and set jumble default to 1.
[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 == "phylipfile" )         { phylipfile = value; }
76                                 if (parameter == "columnfile" )         { columnfile = value; }
77                                 if (parameter == "listfile" )           { listfile = value; }
78                                 if (parameter == "rabundfile" )         { rabundfile = value; }
79                                 if (parameter == "sabundfile" )         { sabundfile = value; }
80                                 if (parameter == "namefile" )           { namefile = value; }
81                                 if (parameter == "orderfile" )          { orderfile = value; }
82                                 if (parameter == "fastafile" )          { fastafile = value; }
83                                 if (parameter == "treefile" )           { treefile = value; }
84                                 if (parameter == "groupfile" )          { 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 == "phylipfile" )         { phylipfile = value; }
145                                 if (parameter == "columnfile" )         { columnfile = value; }                         
146                                 if (parameter == "listfile" )           { listfile = value; }
147                                 if (parameter == "rabundfile" )         { rabundfile = value; }
148                                 if (parameter == "sabundfile" )         { sabundfile = value; }
149                                 if (parameter == "namefile" )           { namefile = value; }
150                                 if (parameter == "orderfile" )          { orderfile = value; }
151                                 if (parameter == "groupfile" )          { groupfile = value; }
152                                 if (parameter == "fastafile" )          { 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                         validateReadFiles();
227                 }
228                 
229                 //are you trying to cluster before you have read something                      
230                 if ((commandName == "cluster") && (globaldata->getSparseMatrix() == NULL) ||
231                         (commandName == "cluster") && (globaldata->getListVector() == NULL)) {
232                                 cout << "Before you use the cluster command, you first need to read in a distance matrix." << endl; 
233                                 errorFree = false;
234                 } 
235                 
236                 //check for valid method
237                 if (commandName == "cluster") {
238                         if ((method == "furthest") || (method == "nearest") || (method == "average")) { }
239                         else {cout << "Not a valid clustering method.  Valid clustering algorithms are furthest, nearest or average." << endl; return false; }
240                 }
241                 
242                 if ((commandName == "collect.single") || (commandName == "rarefaction.single") || (commandName == "summary.single") ){ 
243                         if ((globaldata->getListFile() == "") && (globaldata->getRabundFile() == "") && (globaldata->getSabundFile() == "")) { cout << "You must read a listfile, sabundfile or rabundfile before you can use the collect.single, rarefaction.single or summary.single commands." << endl; return false; }
244                 }
245                 
246                 if ((commandName == "collect.shared") || (commandName == "rarefaction.shared") || (commandName == "summary.shared") || (commandName == "shared") ){ 
247                         if (globaldata->getListFile() == "") { cout << "You must read a listfile and a groupfile before you can use the collect.shared, rarefaction.shared, summary.shared or shared commands." << endl; return false; }
248                         else if (globaldata->getGroupFile() == "") { cout << "You must read a listfile and a groupfile before you can use the collect.shared, rarefaction.shared, summary.shared or shared commands." << endl; return false; }
249                 }
250  
251                 
252                 return errorFree;
253 }
254
255 /*******************************************************/
256
257 /******************************************************/
258 //This function checks to make sure the user entered a file to 
259 // read and that the file exists and can be opened.
260 void ErrorCheck::validateReadFiles() {
261         try {
262                 //Validating files for read
263                 ifstream filehandle;
264                 int ableToOpen;
265         
266                 //are we reading a phylipfile
267                 if (phylipfile != "") {
268                         ableToOpen = openInputFile(phylipfile, filehandle);
269                         filehandle.close();
270                         //unable to open
271                         if (ableToOpen == 1) { errorFree = false; }
272                         else { globaldata->inputFileName = phylipfile; }
273                 //are we reading a phylipfile
274                 }else if (columnfile != "") {
275                         ableToOpen = openInputFile(columnfile, filehandle);
276                         filehandle.close();
277                         //unable to open
278                         if (ableToOpen == 1) { errorFree = false; }
279                         else { globaldata->inputFileName = columnfile; }
280                 //are we reading a listfile
281                 }else if (listfile!= "") {
282                         ableToOpen = openInputFile(listfile, filehandle);
283                         filehandle.close();
284                         //unable to open
285                         if (ableToOpen == 1) {  errorFree = false; }
286                         else { globaldata->inputFileName = listfile; }
287                 //are we reading a rabundfile
288                 }else if (rabundfile != "") {
289                         ableToOpen = openInputFile(rabundfile, filehandle);
290                         filehandle.close();
291                         //unable to open
292                         if (ableToOpen == 1) {  errorFree = false; }
293                         else { globaldata->inputFileName = rabundfile; }
294                 //are we reading a sabundfile
295                 }else if (sabundfile != "") {
296                         ableToOpen = openInputFile(sabundfile, filehandle);
297                         filehandle.close();
298                         //unable to open
299                         if (ableToOpen == 1) {  errorFree = false; }
300                         else { globaldata->inputFileName = sabundfile; }
301                 }else if (fastafile != "") {
302                         ableToOpen = openInputFile(fastafile, filehandle);
303                         filehandle.close();
304                         //unable to open
305                         if (ableToOpen == 1) {  errorFree = false; }
306                         else { globaldata->inputFileName = fastafile; }
307                 }else{ //no file given
308                         errorFree = false;
309                 }
310         }
311         catch(exception& e) {
312                 cout << "Standard Error: " << e.what() << " has occurred in the ErrorCheck class Function validateReadFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
313                 exit(1);
314         }
315         catch(...) {
316                 cout << "An unknown error has occurred in the ErrorCheck class function validateReadFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
317                 exit(1);
318         }
319         
320 }
321 /*******************************************************/
322
323 /******************************************************/
324 //This function checks to make sure the user entered appropriate
325 // format parameters on a distfile read
326 void ErrorCheck::validateReadDist() {
327         try {
328                 ifstream filehandle;
329                 int ableToOpen;
330                 
331                 if ((phylipfile == "") && (columnfile == "")) { cout << "When executing a read.dist you must enter a phylipfile or a columnfile." << endl; errorFree = false; }
332                 else if ((phylipfile != "") && (columnfile != "")) { cout << "When executing a read.dist you must enter ONLY ONE of the following: phylipfile or columnfile." << endl; errorFree = false; }
333                 
334                 if (columnfile != "") {
335                         if (namefile == "") {
336                                 cout << "You need to provide a namefile name if you are going to use the column format." << endl;
337                                 errorFree = false; 
338                         }else {
339                                 ableToOpen = openInputFile(namefile, filehandle);
340                                 filehandle.close();
341                                 //unable to open
342                                 if (ableToOpen == 1) { errorFree = false; }
343                         }
344                 }
345         }
346         catch(exception& e) {
347                 cout << "Standard Error: " << e.what() << " has occurred in the ErrorCheck class Function validateReadDist. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
348                 exit(1);
349         }
350         catch(...) {
351                 cout << "An unknown error has occurred in the ErrorCheck class function validateReadDist. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
352                 exit(1);
353         }
354 }
355 /*******************************************************/
356
357 /******************************************************/
358 //This function checks to make sure the user entered appropriate
359 // format parameters on a parselistcommand
360 void ErrorCheck::validateParseFiles() {
361         try {
362                 ifstream filehandle;
363                 int ableToOpen;
364                 
365                 //checks for valid files
366         
367                 if (listfile == "") { cout << "When executing a read.list you must enter a listfile and a groupfile." << endl; errorFree = false; }
368                 else if (groupfile == "") { cout << "When executing a read.list you must enter a listfile and a groupfile." << endl; errorFree = false; }
369         
370                 //checks parameters on the read command
371                 if (listfile != "") {
372                         ableToOpen = openInputFile(listfile, filehandle);
373                         filehandle.close();
374                         if (ableToOpen == 1) { //unable to open
375                                 errorFree = false;
376                         }
377                         if (groupfile != "") {
378                                 ableToOpen = openInputFile(groupfile, filehandle);
379                                 filehandle.close();
380                                 if (ableToOpen == 1) { //unable to open
381                                         errorFree = false;;
382                                 }
383                         }
384                 }
385         }
386         catch(exception& e) {
387                 cout << "Standard Error: " << e.what() << " has occurred in the ErrorCheck class Function validateReadPhil. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
388                 exit(1);
389         }
390         catch(...) {
391                 cout << "An unknown error has occurred in the ErrorCheck class function validateReadPhil. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
392                 exit(1);
393         }
394 }
395 /*******************************************************/
396
397 /******************************************************/
398 //This function checks to make sure the user entered appropriate
399 // format parameters on a parselistcommand
400 void ErrorCheck::validateTreeFiles() {
401         try {
402                 ifstream filehandle;
403                 int ableToOpen;
404                 
405                 //checks for valid files
406         
407                 if (treefile == "") { cout << "When executing a read.tree you must enter a treefile and a groupfile." << endl; errorFree = false; }
408                 else if (groupfile == "") { cout << "When executing a read.tree you must enter a treefile and a groupfile." << endl; errorFree = false; }
409         
410                 //checks parameters on the read command
411                 if (treefile != "") {
412                         ableToOpen = openInputFile(treefile, filehandle);
413                         filehandle.close();
414                         if (ableToOpen == 1) { //unable to open
415                                 errorFree = false;
416                         }
417                         if (groupfile != "") {
418                                 ableToOpen = openInputFile(groupfile, filehandle);
419                                 filehandle.close();
420                                 if (ableToOpen == 1) { //unable to open
421                                         errorFree = false;;
422                                 }
423                         }
424                 }
425         }
426         catch(exception& e) {
427                 cout << "Standard Error: " << e.what() << " has occurred in the ErrorCheck class Function validateTreeFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
428                 exit(1);
429         }
430         catch(...) {
431                 cout << "An unknown error has occurred in the ErrorCheck class function validateTreeFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
432                 exit(1);
433         }
434 }
435
436 /*******************************************************/
437
438 /******************************************************/
439 //This function checks to make sure the user entered appropriate
440 // format parameters on a distfile read
441 void ErrorCheck::validateReadPhil() {
442         try {
443                 ifstream filehandle;
444                 int ableToOpen;
445                 
446                 //checks to make sure only one file type is given
447                 if (listfile != "") { 
448                         if ((rabundfile != "") || (sabundfile != "")) { 
449                                 cout << "When executing a read.otu you must enter ONLY ONE of the following: listfile, rabundfile or sabundfile." << endl; errorFree = false; }
450                 }else if (rabundfile != "") { 
451                         if ((listfile != "") || (sabundfile != "")) { 
452                                 cout << "When executing a read.otu you must enter ONLY ONE of the following: listfile, rabundfile or sabundfile." << endl; errorFree = false; }
453                 }else if (sabundfile != "") { 
454                         if ((listfile != "") || (rabundfile != "")) { 
455                                 cout << "When executing a read.otu you must enter ONLY ONE of the following: listfile, rabundfile or sabundfile." << endl; errorFree = false; }
456                 }else if ((listfile == "") && (rabundfile == "") && (sabundfile == "")) {
457                             cout << "When executing a read.otu you must enter one of the following: listfile, rabundfile or sabundfile." << endl; errorFree = false; 
458                 }
459                 
460                 //checks parameters on the read command
461                 if (orderfile != "") {
462                         ableToOpen = openInputFile(orderfile, filehandle);
463                         filehandle.close();
464                         if (ableToOpen == 1) { //unable to open
465                                 errorFree = false;
466                         }
467                 }       
468         }
469         catch(exception& e) {
470                 cout << "Standard Error: " << e.what() << " has occurred in the ErrorCheck class Function validateReadPhil. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
471                 exit(1);
472         }
473         catch(...) {
474                 cout << "An unknown error has occurred in the ErrorCheck class function validateReadPhil. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
475                 exit(1);
476         }
477 }
478 /*******************************************************/
479
480 /******************************************************/
481
482 void ErrorCheck::clear() {
483         //option definitions should go here...
484         phylipfile              =       "";
485         columnfile              =       "";
486         listfile                =       "";
487         rabundfile              =       "";
488         sabundfile              =       "";
489         namefile                =       "";
490         groupfile               =       ""; 
491         orderfile               =       "";
492         line                    =       "";
493         label                   =       "";
494         method                  =   "furthest";
495 }
496 /*******************************************************/
497
498 /******************************************************/
499