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