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