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