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)
55 #define exp(x) (exp((double) x))
56 #define sqrt(x) (sqrt((double) x))
57 #define log10(x) (log10((double) x))
58 #define log2(x) (log10(x)/log10(2))
59 #define isnan(x) ((x) != (x))
60 #define isinf(x) (fabs(x) == std::numeric_limits<double>::infinity())
63 typedef unsigned long ull;
80 /***********************************************************************/
82 // snagged from http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.2
83 // works for now, but there should be a way to do it without killing the whole program
85 class BadConversion : public runtime_error {
87 BadConversion(const string& s) : runtime_error(s){ }
90 //**********************************************************************************************************************
93 inline void convert(const string& s, T& x, bool failIfLeftoverChars = true){
96 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
97 throw BadConversion(s);
100 //**********************************************************************************************************************
103 inline bool convertTestFloat(const string& s, T& x, bool failIfLeftoverChars = true){
106 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
113 //**********************************************************************************************************************
116 inline bool convertTest(const string& s, T& x, bool failIfLeftoverChars = true){
119 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
121 cout << "unable to be converted into an integer.\n" << endl;
127 //**********************************************************************************************************************
130 string toString(const T&x){
136 //**********************************************************************************************************************
139 string toHex(const T&x){
146 //**********************************************************************************************************************
149 string toString(const T&x, int i){
153 output << fixed << x;
157 /***********************************************************************/
159 inline int openOutputFileAppend(string fileName, ofstream& fileHandle){
161 fileHandle.open(fileName.c_str(), ios::app);
163 cout << "Error: Could not open " << fileName << endl;
171 /***********************************************************************/
173 inline string getline(ifstream& fileHandle) {
178 while (!fileHandle.eof()) {
180 char c = fileHandle.get();
182 //are you at the end of the line
183 if ((c == '\n') || (c == '\r') || (c == '\f')){ break; }
190 catch(exception& e) {
191 cout << "Error in mothur function getline" << endl;
196 /**************************************************************************************************/
198 inline void mothurOut(string message) {
201 string logFileName = "mothur.logFile";
202 openOutputFileAppend(logFileName, out);
209 catch(exception& e) {
210 cout << "Error in mothur class mothurOut" << endl;
214 /**************************************************************************************************/
216 inline void mothurOut(string message, string precision) {
219 string logFileName = "mothur.logFile";
220 openOutputFileAppend(logFileName, out);
222 cout << precision << message;
223 out << precision << message;
227 catch(exception& e) {
228 cout << "Error in mothur class mothurOut" << endl;
233 /**************************************************************************************************/
235 inline void mothurOutEndLine() {
238 string logFileName = "mothur.logFile";
239 openOutputFileAppend(logFileName, out);
246 catch(exception& e) {
247 cout << "error in mothur mothurOutEndLine" << endl;
253 /**************************************************************************************************/
255 inline void errorOut(exception& e, string object, string function) {
257 mothurOut("Error: ");
258 mothurOut(toString(e.what()));
259 mothurOut(" has occurred in the " + object + " class function " + function + ". Please contact Pat Schloss at pschloss@microbio.umass.edu, and be sure to include the mothur.logFile with your inquiry.");
267 /***********************************************************************/
269 inline void gobble(istream& f){
272 while(isspace(d=f.get())) {;}
276 /***********************************************************************/
278 inline bool isTrue(string f){
280 if ((f == "TRUE") || (f == "T") || (f == "true") || (f == "t")) { return true; }
281 else { return false; }
284 /***********************************************************************/
286 inline float roundDist(float dist, int precision){
288 return int(dist * precision + 0.5)/float(precision);
292 /***********************************************************************/
294 inline int getNumNames(string names){
300 for(int i=0;i<names.size();i++){
311 /**************************************************************************************************/
313 inline vector<vector<double> > binomial(int maxOrder){
315 vector<vector<double> > binomial(maxOrder+1);
317 for(int i=0;i<=maxOrder;i++){
318 binomial[i].resize(maxOrder+1);
327 for(int i=2;i<=maxOrder;i++){
331 for(int i=2;i<=maxOrder;i++){
332 for(int j=1;j<=maxOrder;j++){
333 if(i==j){ binomial[i][j]=1; }
334 if(j>i) { binomial[i][j]=0; }
335 else { binomial[i][j]=binomial[i-1][j-1]+binomial[i-1][j]; }
342 /***********************************************************************/
344 inline string getRootName(string longName){
346 string rootName = longName;
348 if(longName.find_last_of(".") != longName.npos){
349 int pos = longName.find_last_of('.')+1;
350 rootName = longName.substr(0, pos);
355 /***********************************************************************/
357 inline string getSimpleName(string longName){
359 string simpleName = longName;
361 if(longName.find_last_of("/") != longName.npos){
362 int pos = longName.find_last_of('/')+1;
363 simpleName = longName.substr(pos, longName.length());
369 /***********************************************************************/
371 inline int factorial(int num){
374 for (int i = 1; i <= num; i++) {
380 /**************************************************************************************************
382 double min(double x, double y)
388 /***********************************************************************/
390 inline string getPathName(string longName){
392 string rootPathName = longName;
394 if(longName.find_last_of('/') != longName.npos){
395 int pos = longName.find_last_of('/')+1;
396 rootPathName = longName.substr(0, pos);
402 /***********************************************************************/
404 inline string getExtension(string longName){
406 string extension = longName;
408 if(longName.find_last_of('.') != longName.npos){
409 int pos = longName.find_last_of('.');
410 extension = longName.substr(pos, longName.length());
416 /***********************************************************************/
418 inline int openInputFile(string fileName, ifstream& fileHandle){
420 fileHandle.open(fileName.c_str());
422 mothurOut("Error: Could not open " + fileName); mothurOutEndLine();
431 /***********************************************************************/
433 inline int openOutputFile(string fileName, ofstream& fileHandle){
435 fileHandle.open(fileName.c_str(), ios::trunc);
437 mothurOut("Error: Could not open " + fileName); mothurOutEndLine();
446 /***********************************************************************/
448 inline int getNumSeqs(ifstream& file){
450 int numSeqs = count(istreambuf_iterator<char>(file),istreambuf_iterator<char>(), '>');
456 /***********************************************************************/
458 //This function parses the estimator options and puts them in a vector
459 inline void splitAtDash(string& estim, vector<string>& container) {
463 while (estim.find_first_of('-') != -1) {
464 individual = estim.substr(0,estim.find_first_of('-'));
465 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
466 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
467 container.push_back(individual);
471 container.push_back(estim);
473 catch(exception& e) {
474 errorOut(e, "mothur", "splitAtDash");
479 /***********************************************************************/
480 //This function parses the label options and puts them in a set
481 inline void splitAtDash(string& estim, set<string>& container) {
485 while (estim.find_first_of('-') != -1) {
486 individual = estim.substr(0,estim.find_first_of('-'));
487 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
488 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
489 container.insert(individual);
493 container.insert(estim);
495 catch(exception& e) {
496 errorOut(e, "mothur", "splitAtDash");
500 /***********************************************************************/
501 //This function parses the line options and puts them in a set
502 inline void splitAtDash(string& estim, set<int>& container) {
507 while (estim.find_first_of('-') != -1) {
508 individual = estim.substr(0,estim.find_first_of('-'));
509 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
510 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
511 convert(individual, lineNum); //convert the string to int
512 container.insert(lineNum);
516 convert(estim, lineNum); //convert the string to int
517 container.insert(lineNum);
519 catch(exception& e) {
520 errorOut(e, "mothur", "splitAtDash");
524 /***********************************************************************/
525 //This function parses the a string and puts peices in a vector
526 inline void splitAtComma(string& estim, vector<string>& container) {
530 while (estim.find_first_of(',') != -1) {
531 individual = estim.substr(0,estim.find_first_of(','));
532 if ((estim.find_first_of(',')+1) <= estim.length()) { //checks to make sure you don't have comma at end of string
533 estim = estim.substr(estim.find_first_of(',')+1, estim.length());
534 container.push_back(individual);
538 container.push_back(estim);
540 catch(exception& e) {
541 errorOut(e, "mothur", "splitAtComma");
545 /***********************************************************************/
547 //This function splits up the various option parameters
548 inline void splitAtComma(string& prefix, string& suffix){
550 prefix = suffix.substr(0,suffix.find_first_of(','));
551 if ((suffix.find_first_of(',')+2) <= suffix.length()) { //checks to make sure you don't have comma at end of string
552 suffix = suffix.substr(suffix.find_first_of(',')+1, suffix.length());
554 while(suffix.at(0) == ' ')
555 suffix = suffix.substr(1, suffix.length());
559 catch(exception& e) {
560 errorOut(e, "mothur", "splitAtComma");
564 /***********************************************************************/
566 //This function separates the key value from the option value i.e. dist=96_...
567 inline void splitAtEquals(string& key, string& value){
569 if(value.find_first_of('=') != -1){
570 key = value.substr(0,value.find_first_of('='));
571 if ((value.find_first_of('=')+1) <= value.length()) {
572 value = value.substr(value.find_first_of('=')+1, value.length());
579 catch(exception& e) {
580 errorOut(e, "mothur", "splitAtEquals");
584 /**************************************************************************************************/
586 inline bool inUsersGroups(string groupname, vector<string> Groups) {
588 for (int i = 0; i < Groups.size(); i++) {
589 if (groupname == Groups[i]) { return true; }
593 catch(exception& e) {
594 errorOut(e, "mothur", "inUsersGroups");
599 /**************************************************************************************************/
601 inline void mothurOutJustToLog(string message) {
604 string logFileName = "mothur.logFile";
605 openOutputFileAppend(logFileName, out);
611 catch(exception& e) {
612 errorOut(e, "mothur", "mothurOutJustToLog");
618 /**************************************************************************************************/
620 inline void mothurOut(float num) {
623 string logFileName = "mothur.logFile";
624 openOutputFileAppend(logFileName, out);
631 catch(exception& e) {
632 cout << "Error in mothur class mothurOut float" << endl;
636 /***********************************************************************/
637 inline void mothurOut(double value) {
640 string logFileName = "mothur.logFile";
641 openOutputFileAppend(logFileName, out);
648 catch(exception& e) {
649 cout << "Error in mothur class mothurOut double" << endl;
654 /***********************************************************************/
655 //this function determines if the user has given us labels that are smaller than the given label.
656 //if so then it returns true so that the calling function can run the previous valid distance.
657 //it's a "smart" distance function. It also checks for invalid labels.
658 inline bool anyLabelsToProcess(string label, set<string>& userLabels, string errorOff) {
660 set<string>::iterator it;
661 vector<float> orderFloat;
662 map<string, float> userMap; //the conversion process removes trailing 0's which we need to put back
663 map<string, float>::iterator it2;
665 bool smaller = false;
667 //unique is the smallest line
668 if (label == "unique") { return false; }
669 else { convert(label, labelFloat); }
671 //go through users set and make them floats
672 for(it = userLabels.begin(); it != userLabels.end(); ++it) {
675 if ((*it != "unique") && (convertTestFloat(*it, temp) == true)){
677 orderFloat.push_back(temp);
679 }else if (*it == "unique") {
680 orderFloat.push_back(-1.0);
681 userMap["unique"] = -1.0;
683 if (errorOff == "") { mothurOut(*it + " is not a valid label."); mothurOutEndLine(); }
684 userLabels.erase(*it);
690 sort(orderFloat.begin(), orderFloat.end());
692 /*************************************************/
693 //is this label bigger than any of the users labels
694 /*************************************************/
696 //loop through order until you find a label greater than label
697 for (int i = 0; i < orderFloat.size(); i++) {
698 if (orderFloat[i] < labelFloat) {
700 if (orderFloat[i] == -1) {
701 if (errorOff == "") { mothurOut("Your file does not include the label unique."); mothurOutEndLine(); }
702 userLabels.erase("unique");
705 if (errorOff == "") { mothurOut("Your file does not include the label "); mothurOutEndLine(); }
707 for (it2 = userMap.begin(); it2!= userMap.end(); it2++) {
708 if (it2->second == orderFloat[i]) {
710 //remove small labels
715 if (errorOff == "") { mothurOut(s + ". I will use the next smallest distance. "); mothurOutEndLine(); }
717 //since they are sorted once you find a bigger one stop looking
724 catch(exception& e) {
725 errorOut(e, "mothur", "anyLabelsToProcess");
730 /**************************************************************************************************/