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