5 * Created by Sarah Westcott on 1/2/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "errorchecking.h"
12 /*******************************************************/
14 /******************************************************/
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();
33 /*******************************************************/
35 /******************************************************/
37 ErrorCheck::~ErrorCheck() {}
39 /*******************************************************/
41 /******************************************************/
43 bool ErrorCheck::checkInput(string input) {
47 //get command name and parameters
48 int openParen = input.find_first_of('(');
49 int closeParen = input.find_last_of(')');
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;
59 //is it a valid command
60 if (validCommand->isValidCommand(commandName) != true) { return false; }
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);
69 //is it a valid parameter
70 if (validParameter->isValidParameter(parameter) != true) { return false; }
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; }
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; }
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; }
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; }
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; }
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; }
131 //gets the last parameter and value
132 if (errorFree) { //gets the last parameter and value
134 globaldata->splitAtEquals(parameter, value);
135 //is it a valid parameter
136 if (validParameter->isValidParameter(parameter) != true) { return false; }
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; }
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; }
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; }
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; }
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; }
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; }
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; }
203 if (commandName == "parselist") {
204 validateParseFiles(commandName);
206 if ((commandName == "read.phylip") || (commandName == "read.column")) {
209 }else if ((commandName == "read.list") || (commandName == "read.rabund") || (commandName == "read.sabund")){
211 validateReadPhil(commandName);
212 }else if (commandName == "read.shared") {
214 validateParseFiles(commandName); //checks the listfile and groupfile parameters
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;
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; }
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; }
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; }
243 /*******************************************************/
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() {
250 //Validating files for read
254 //are we reading a distfile
255 if (distfile != "") {
256 ableToOpen = openInputFile(distfile, filehandle);
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);
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);
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);
280 if (ableToOpen == 1) { errorFree = false; }
281 else { globaldata->inputFileName = sabundfile; }
282 }else{ //no file given
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";
291 cout << "An unknown error has occurred in the ErrorCheck class function validateReadFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
296 /*******************************************************/
298 /******************************************************/
299 //This function checks to make sure the user entered appropriate
300 // format parameters on a distfile read
301 void ErrorCheck::validateReadDist() {
306 if (distfile == "") { cout << "When executing a read.phylip or read.column you must enter a distfile." << endl; errorFree = false; }
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;
313 ableToOpen = openInputFile(namefile, filehandle);
316 if (ableToOpen == 1) { errorFree = false; }
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";
325 cout << "An unknown error has occurred in the ErrorCheck class function validateReadDist. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
329 /*******************************************************/
331 /******************************************************/
332 //This function checks to make sure the user entered appropriate
333 // format parameters on a parselistcommand
334 void ErrorCheck::validateParseFiles(string command) {
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; }
348 //checks parameters on the read command
349 if (listfile != "") {
350 ableToOpen = openInputFile(listfile, filehandle);
352 if (ableToOpen == 1) { //unable to open
355 if (groupfile != "") {
356 ableToOpen = openInputFile(groupfile, filehandle);
358 if (ableToOpen == 1) { //unable to open
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";
369 cout << "An unknown error has occurred in the ErrorCheck class function validateReadPhil. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
374 /*******************************************************/
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) {
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; }
393 //checks parameters on the read command
394 if (orderfile != "") {
395 ableToOpen = openInputFile(orderfile, filehandle);
397 if (ableToOpen == 1) { //unable to open
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";
407 cout << "An unknown error has occurred in the ErrorCheck class function validateReadPhil. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
411 /*******************************************************/
413 /******************************************************/
415 void ErrorCheck::clear() {
416 //option definitions should go here...
428 /*******************************************************/
430 /******************************************************/