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 cutoff = globaldata->getCutOff();
30 format = globaldata->getFormat();
31 method = globaldata->getMethod();
34 /*******************************************************/
36 /******************************************************/
38 ErrorCheck::~ErrorCheck() {}
40 /*******************************************************/
42 /******************************************************/
44 bool ErrorCheck::checkInput(string input) {
48 //get command name and parameters
49 int openParen = input.find_first_of('(');
50 int closeParen = input.find_last_of(')');
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;
60 //is it a valid command
61 if (validCommand->isValidCommand(commandName) != true) { return false; }
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);
70 //is it a valid parameter
71 if (validParameter->isValidParameter(parameter) != true) { return false; }
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; }
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; }
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; }
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; }
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; }
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; }
133 //gets the last parameter and value
134 if (errorFree) { //gets the last parameter and value
136 globaldata->splitAtEquals(parameter, value);
137 //is it a valid parameter
138 if (validParameter->isValidParameter(parameter) != true) { return false; }
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; }
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; }
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; }
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; }
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; }
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; }
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; }
206 if (commandName == "read.dist") {
209 }else if (commandName == "read.otu") {
212 }else if (commandName == "read.list") {
213 validateParseFiles(); //checks the listfile and groupfile parameters
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;
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; }
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; }
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; }
242 /*******************************************************/
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() {
249 //Validating files for read
253 //are we reading a phylipfile
254 if (phylipfile != "") {
255 ableToOpen = openInputFile(phylipfile, filehandle);
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);
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);
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);
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);
286 if (ableToOpen == 1) { errorFree = false; }
287 else { globaldata->inputFileName = sabundfile; }
288 }else{ //no file given
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";
297 cout << "An unknown error has occurred in the ErrorCheck class function validateReadFiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
302 /*******************************************************/
304 /******************************************************/
305 //This function checks to make sure the user entered appropriate
306 // format parameters on a distfile read
307 void ErrorCheck::validateReadDist() {
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; }
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;
320 ableToOpen = openInputFile(namefile, filehandle);
323 if (ableToOpen == 1) { errorFree = false; }
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";
332 cout << "An unknown error has occurred in the ErrorCheck class function validateReadDist. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
336 /*******************************************************/
338 /******************************************************/
339 //This function checks to make sure the user entered appropriate
340 // format parameters on a parselistcommand
341 void ErrorCheck::validateParseFiles() {
346 //checks for valid files
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; }
351 //checks parameters on the read command
352 if (listfile != "") {
353 ableToOpen = openInputFile(listfile, filehandle);
355 if (ableToOpen == 1) { //unable to open
358 if (groupfile != "") {
359 ableToOpen = openInputFile(groupfile, filehandle);
361 if (ableToOpen == 1) { //unable to open
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";
372 cout << "An unknown error has occurred in the ErrorCheck class function validateReadPhil. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
377 /*******************************************************/
379 /******************************************************/
380 //This function checks to make sure the user entered appropriate
381 // format parameters on a distfile read
382 void ErrorCheck::validateReadPhil() {
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;
401 //checks parameters on the read command
402 if (orderfile != "") {
403 ableToOpen = openInputFile(orderfile, filehandle);
405 if (ableToOpen == 1) { //unable to open
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";
415 cout << "An unknown error has occurred in the ErrorCheck class function validateReadPhil. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
419 /*******************************************************/
421 /******************************************************/
423 void ErrorCheck::clear() {
424 //option definitions should go here...
437 /*******************************************************/
439 /******************************************************/