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