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 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();
35 /*******************************************************/
37 /******************************************************/
39 ErrorCheck::~ErrorCheck() {}
41 /*******************************************************/
43 /******************************************************/
45 bool ErrorCheck::checkInput(string input) {
49 //get command name and parameters
50 int openParen = input.find_first_of('(');
51 int closeParen = input.find_last_of(')');
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;
61 //is it a valid command
62 if (validCommand->isValidCommand(commandName) != true) { return false; }
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);
71 //is it a valid parameter
72 if (validParameter->isValidParameter(parameter) != true) { return false; }
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; }
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; }
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; }
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; }
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; }
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; }
135 //gets the last parameter and value
136 if (errorFree) { //gets the last parameter and value
138 globaldata->splitAtEquals(parameter, value);
139 //is it a valid parameter
140 if (validParameter->isValidParameter(parameter) != true) { return false; }
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; }
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; }
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; }
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; }
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; }
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; }
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; }
209 if (commandName == "read.dist") {
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
220 }else if (commandName == "deconvolute") {
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;
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; }
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; }
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; }
250 /*******************************************************/
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() {
257 //Validating files for read
261 //are we reading a phylipfile
262 if (phylipfile != "") {
263 ableToOpen = openInputFile(phylipfile, filehandle);
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);
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);
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);
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);
294 if (ableToOpen == 1) { errorFree = false; }
295 else { globaldata->inputFileName = sabundfile; }
296 }else if (fastafile != "") {
297 ableToOpen = openInputFile(fastafile, filehandle);
300 if (ableToOpen == 1) { errorFree = false; }
301 else { globaldata->inputFileName = fastafile; }
302 }else{ //no file given
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";
311 cout << "An unknown error has occurred in the ErrorCheck class function validateReadFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
316 /*******************************************************/
318 /******************************************************/
319 //This function checks to make sure the user entered appropriate
320 // format parameters on a distfile read
321 void ErrorCheck::validateReadDist() {
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; }
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;
334 ableToOpen = openInputFile(namefile, filehandle);
337 if (ableToOpen == 1) { errorFree = false; }
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";
346 cout << "An unknown error has occurred in the ErrorCheck class function validateReadDist. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
350 /*******************************************************/
352 /******************************************************/
353 //This function checks to make sure the user entered appropriate
354 // format parameters on a parselistcommand
355 void ErrorCheck::validateParseFiles() {
360 //checks for valid files
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; }
365 //checks parameters on the read command
366 if (listfile != "") {
367 ableToOpen = openInputFile(listfile, filehandle);
369 if (ableToOpen == 1) { //unable to open
372 if (groupfile != "") {
373 ableToOpen = openInputFile(groupfile, filehandle);
375 if (ableToOpen == 1) { //unable to open
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";
386 cout << "An unknown error has occurred in the ErrorCheck class function validateReadPhil. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
391 /*******************************************************/
393 /******************************************************/
394 //This function checks to make sure the user entered appropriate
395 // format parameters on a distfile read
396 void ErrorCheck::validateReadPhil() {
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;
415 //checks parameters on the read command
416 if (orderfile != "") {
417 ableToOpen = openInputFile(orderfile, filehandle);
419 if (ableToOpen == 1) { //unable to open
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";
429 cout << "An unknown error has occurred in the ErrorCheck class function validateReadPhil. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
433 /*******************************************************/
435 /******************************************************/
437 void ErrorCheck::clear() {
438 //option definitions should go here...
451 /*******************************************************/
453 /******************************************************/