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