10 * Created by Sarah Westcott on 2/19/09.
11 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
15 /* This file contains all the standard incudes we use in the project as well as some common utilities. */
49 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
56 #define exp(x) (exp((double) x))
57 #define sqrt(x) (sqrt((double) x))
58 #define log10(x) (log10((double) x))
59 #define log2(x) (log10(x)/log10(2))
60 #define isnan(x) ((x) != (x))
61 #define isinf(x) (fabs(x) == std::numeric_limits<double>::infinity())
64 typedef unsigned long ull;
81 /***********************************************************************/
83 // snagged from http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.2
84 // works for now, but there should be a way to do it without killing the whole program
86 class BadConversion : public runtime_error {
88 BadConversion(const string& s) : runtime_error(s){ }
91 //**********************************************************************************************************************
94 inline void convert(const string& s, T& x, bool failIfLeftoverChars = true){
97 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
98 throw BadConversion(s);
101 //**********************************************************************************************************************
104 inline bool convertTestFloat(const string& s, T& x, bool failIfLeftoverChars = true){
107 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
114 //**********************************************************************************************************************
117 inline bool convertTest(const string& s, T& x, bool failIfLeftoverChars = true){
120 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
122 cout << "unable to be converted into an integer.\n" << endl;
128 //**********************************************************************************************************************
131 string toString(const T&x){
137 //**********************************************************************************************************************
140 string toHex(const T&x){
147 //**********************************************************************************************************************
150 string toString(const T&x, int i){
154 output << fixed << x;
158 /***********************************************************************/
160 inline int openOutputFileAppend(string fileName, ofstream& fileHandle){
162 fileHandle.open(fileName.c_str(), ios::app);
164 cout << "Error: Could not open " << fileName << endl;
172 /***********************************************************************/
174 inline void gobble(istream& f){
177 while(isspace(d=f.get())) {;}
182 /***********************************************************************/
184 inline string getline(ifstream& fileHandle) {
189 while (!fileHandle.eof()) {
191 char c = fileHandle.get();
193 //are you at the end of the line
194 if ((c == '\n') || (c == '\r') || (c == '\f')){ break; }
201 catch(exception& e) {
202 cout << "Error in mothur function getline" << endl;
207 /**************************************************************************************************/
209 inline void mothurOut(string message) {
212 string logFileName = "mothur.logFile";
213 openOutputFileAppend(logFileName, out);
220 catch(exception& e) {
221 cout << "Error in mothur class mothurOut" << endl;
225 /**************************************************************************************************/
227 inline void mothurOut(string message, string precision) {
230 string logFileName = "mothur.logFile";
231 openOutputFileAppend(logFileName, out);
233 cout << precision << message;
234 out << precision << message;
238 catch(exception& e) {
239 cout << "Error in mothur class mothurOut" << endl;
244 /**************************************************************************************************/
246 inline void mothurOutEndLine() {
249 string logFileName = "mothur.logFile";
250 openOutputFileAppend(logFileName, out);
257 catch(exception& e) {
258 cout << "error in mothur mothurOutEndLine" << endl;
264 /**************************************************************************************************/
266 inline void errorOut(exception& e, string object, string function) {
268 mothurOut("Error: ");
269 mothurOut(toString(e.what()));
270 mothurOut(" has occurred in the " + object + " class function " + function + ". Please contact Pat Schloss at pschloss@umich.edu, and be sure to include the mothur.logFile with your inquiry.");
278 /***********************************************************************/
280 inline bool isTrue(string f){
282 if ((f == "TRUE") || (f == "T") || (f == "true") || (f == "t")) { return true; }
283 else { return false; }
286 /***********************************************************************/
288 inline float roundDist(float dist, int precision){
290 return int(dist * precision + 0.5)/float(precision);
294 /***********************************************************************/
296 inline int getNumNames(string names){
302 for(int i=0;i<names.size();i++){
313 /**************************************************************************************************/
315 inline vector<vector<double> > binomial(int maxOrder){
317 vector<vector<double> > binomial(maxOrder+1);
319 for(int i=0;i<=maxOrder;i++){
320 binomial[i].resize(maxOrder+1);
329 for(int i=2;i<=maxOrder;i++){
333 for(int i=2;i<=maxOrder;i++){
334 for(int j=1;j<=maxOrder;j++){
335 if(i==j){ binomial[i][j]=1; }
336 if(j>i) { binomial[i][j]=0; }
337 else { binomial[i][j]=binomial[i-1][j-1]+binomial[i-1][j]; }
344 /***********************************************************************/
346 inline string getRootName(string longName){
348 string rootName = longName;
350 if(longName.find_last_of(".") != longName.npos){
351 int pos = longName.find_last_of('.')+1;
352 rootName = longName.substr(0, pos);
357 /***********************************************************************/
359 inline string getSimpleName(string longName){
361 string simpleName = longName;
363 if(longName.find_last_of("/") != longName.npos){
364 int pos = longName.find_last_of('/')+1;
365 simpleName = longName.substr(pos, longName.length());
371 /***********************************************************************/
373 inline int factorial(int num){
376 for (int i = 1; i <= num; i++) {
382 /**************************************************************************************************
384 double min(double x, double y)
390 /***********************************************************************/
392 inline string getPathName(string longName){
394 string rootPathName = longName;
396 if(longName.find_last_of('/') != longName.npos){
397 int pos = longName.find_last_of('/')+1;
398 rootPathName = longName.substr(0, pos);
404 /***********************************************************************/
406 inline string getExtension(string longName){
408 string extension = longName;
410 if(longName.find_last_of('.') != longName.npos){
411 int pos = longName.find_last_of('.');
412 extension = longName.substr(pos, longName.length());
418 /***********************************************************************/
420 inline int openInputFile(string fileName, ifstream& fileHandle){
422 fileHandle.open(fileName.c_str());
424 mothurOut("Error: Could not open " + fileName); mothurOutEndLine();
428 //check for blank file
430 if (fileHandle.eof()) { mothurOut(fileName + " is blank. Please correct."); mothurOutEndLine(); return 1; }
437 /***********************************************************************/
439 inline int openOutputFile(string fileName, ofstream& fileHandle){
441 fileHandle.open(fileName.c_str(), ios::trunc);
443 mothurOut("Error: Could not open " + fileName); mothurOutEndLine();
452 /***********************************************************************/
454 inline int getNumSeqs(ifstream& file){
456 int numSeqs = count(istreambuf_iterator<char>(file),istreambuf_iterator<char>(), '>');
462 /***********************************************************************/
464 //This function parses the estimator options and puts them in a vector
465 inline void splitAtDash(string& estim, vector<string>& container) {
469 while (estim.find_first_of('-') != -1) {
470 individual = estim.substr(0,estim.find_first_of('-'));
471 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
472 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
473 container.push_back(individual);
477 container.push_back(estim);
479 catch(exception& e) {
480 errorOut(e, "mothur", "splitAtDash");
485 /***********************************************************************/
486 //This function parses the label options and puts them in a set
487 inline void splitAtDash(string& estim, set<string>& container) {
491 while (estim.find_first_of('-') != -1) {
492 individual = estim.substr(0,estim.find_first_of('-'));
493 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
494 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
495 container.insert(individual);
499 container.insert(estim);
501 catch(exception& e) {
502 errorOut(e, "mothur", "splitAtDash");
506 /***********************************************************************/
507 //This function parses the line options and puts them in a set
508 inline void splitAtDash(string& estim, set<int>& container) {
513 while (estim.find_first_of('-') != -1) {
514 individual = estim.substr(0,estim.find_first_of('-'));
515 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
516 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
517 convert(individual, lineNum); //convert the string to int
518 container.insert(lineNum);
522 convert(estim, lineNum); //convert the string to int
523 container.insert(lineNum);
525 catch(exception& e) {
526 errorOut(e, "mothur", "splitAtDash");
530 /***********************************************************************/
531 //This function parses the a string and puts peices in a vector
532 inline void splitAtComma(string& estim, vector<string>& container) {
536 while (estim.find_first_of(',') != -1) {
537 individual = estim.substr(0,estim.find_first_of(','));
538 if ((estim.find_first_of(',')+1) <= estim.length()) { //checks to make sure you don't have comma at end of string
539 estim = estim.substr(estim.find_first_of(',')+1, estim.length());
540 container.push_back(individual);
544 container.push_back(estim);
546 catch(exception& e) {
547 errorOut(e, "mothur", "splitAtComma");
551 /***********************************************************************/
553 //This function splits up the various option parameters
554 inline void splitAtComma(string& prefix, string& suffix){
556 prefix = suffix.substr(0,suffix.find_first_of(','));
557 if ((suffix.find_first_of(',')+2) <= suffix.length()) { //checks to make sure you don't have comma at end of string
558 suffix = suffix.substr(suffix.find_first_of(',')+1, suffix.length());
560 while(suffix.at(0) == ' ')
561 suffix = suffix.substr(1, suffix.length());
565 catch(exception& e) {
566 errorOut(e, "mothur", "splitAtComma");
570 /***********************************************************************/
572 //This function separates the key value from the option value i.e. dist=96_...
573 inline void splitAtEquals(string& key, string& value){
575 if(value.find_first_of('=') != -1){
576 key = value.substr(0,value.find_first_of('='));
577 if ((value.find_first_of('=')+1) <= value.length()) {
578 value = value.substr(value.find_first_of('=')+1, value.length());
585 catch(exception& e) {
586 errorOut(e, "mothur", "splitAtEquals");
590 /**************************************************************************************************/
592 inline bool inUsersGroups(string groupname, vector<string> Groups) {
594 for (int i = 0; i < Groups.size(); i++) {
595 if (groupname == Groups[i]) { return true; }
599 catch(exception& e) {
600 errorOut(e, "mothur", "inUsersGroups");
605 /**************************************************************************************************/
607 inline void mothurOutJustToLog(string message) {
610 string logFileName = "mothur.logFile";
611 openOutputFileAppend(logFileName, out);
617 catch(exception& e) {
618 errorOut(e, "mothur", "mothurOutJustToLog");
624 /**************************************************************************************************/
626 inline void mothurOut(float num) {
629 string logFileName = "mothur.logFile";
630 openOutputFileAppend(logFileName, out);
637 catch(exception& e) {
638 cout << "Error in mothur class mothurOut float" << endl;
642 /***********************************************************************/
643 inline void mothurOut(double value) {
646 string logFileName = "mothur.logFile";
647 openOutputFileAppend(logFileName, out);
654 catch(exception& e) {
655 cout << "Error in mothur class mothurOut double" << endl;
660 /***********************************************************************/
661 //this function determines if the user has given us labels that are smaller than the given label.
662 //if so then it returns true so that the calling function can run the previous valid distance.
663 //it's a "smart" distance function. It also checks for invalid labels.
664 inline bool anyLabelsToProcess(string label, set<string>& userLabels, string errorOff) {
666 set<string>::iterator it;
667 vector<float> orderFloat;
668 map<string, float> userMap; //the conversion process removes trailing 0's which we need to put back
669 map<string, float>::iterator it2;
671 bool smaller = false;
673 //unique is the smallest line
674 if (label == "unique") { return false; }
675 else { convert(label, labelFloat); }
677 //go through users set and make them floats
678 for(it = userLabels.begin(); it != userLabels.end(); ++it) {
681 if ((*it != "unique") && (convertTestFloat(*it, temp) == true)){
683 orderFloat.push_back(temp);
685 }else if (*it == "unique") {
686 orderFloat.push_back(-1.0);
687 userMap["unique"] = -1.0;
689 if (errorOff == "") { mothurOut(*it + " is not a valid label."); mothurOutEndLine(); }
690 userLabels.erase(*it);
696 sort(orderFloat.begin(), orderFloat.end());
698 /*************************************************/
699 //is this label bigger than any of the users labels
700 /*************************************************/
702 //loop through order until you find a label greater than label
703 for (int i = 0; i < orderFloat.size(); i++) {
704 if (orderFloat[i] < labelFloat) {
706 if (orderFloat[i] == -1) {
707 if (errorOff == "") { mothurOut("Your file does not include the label unique."); mothurOutEndLine(); }
708 userLabels.erase("unique");
711 if (errorOff == "") { mothurOut("Your file does not include the label "); mothurOutEndLine(); }
713 for (it2 = userMap.begin(); it2!= userMap.end(); it2++) {
714 if (it2->second == orderFloat[i]) {
716 //remove small labels
721 if (errorOff == "") { mothurOut(s + ". I will use the next smallest distance. "); mothurOutEndLine(); }
723 //since they are sorted once you find a bigger one stop looking
730 catch(exception& e) {
731 errorOut(e, "mothur", "anyLabelsToProcess");
736 /**************************************************************************************************/