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 /************************************************************/
85 int smallChild; //used to make linkTable work with list and rabund. represents bin number of this cluster node
86 clusterNode(int num, int par, int kid) : numSeq(num), parent(par), smallChild(kid) {};
89 /***********************************************************************/
91 // snagged from http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.2
92 // works for now, but there should be a way to do it without killing the whole program
94 class BadConversion : public runtime_error {
96 BadConversion(const string& s) : runtime_error(s){ }
99 //**********************************************************************************************************************
102 inline void convert(const string& s, T& x, bool failIfLeftoverChars = true){
105 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
106 throw BadConversion(s);
109 //**********************************************************************************************************************
112 inline bool convertTestFloat(const string& s, T& x, bool failIfLeftoverChars = true){
115 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
122 //**********************************************************************************************************************
125 inline bool convertTest(const string& s, T& x, bool failIfLeftoverChars = true){
128 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
130 cout << "unable to be converted into an integer.\n" << endl;
136 //**********************************************************************************************************************
139 string toString(const T&x){
145 //**********************************************************************************************************************
148 string toHex(const T&x){
155 //**********************************************************************************************************************
158 string toString(const T&x, int i){
162 output << fixed << x;
166 /***********************************************************************/
168 inline int openOutputFileAppend(string fileName, ofstream& fileHandle){
170 fileHandle.open(fileName.c_str(), ios::app);
172 cout << "Error: Could not open " << fileName << endl;
180 /***********************************************************************/
182 inline void gobble(istream& f){
185 while(isspace(d=f.get())) {;}
190 /***********************************************************************/
192 inline string getline(ifstream& fileHandle) {
197 while (!fileHandle.eof()) {
199 char c = fileHandle.get();
201 //are you at the end of the line
202 if ((c == '\n') || (c == '\r') || (c == '\f')){ break; }
209 catch(exception& e) {
210 cout << "Error in mothur function getline" << endl;
215 /**************************************************************************************************/
217 inline void mothurOut(string message) {
220 string logFileName = "mothur.logFile";
221 openOutputFileAppend(logFileName, out);
228 catch(exception& e) {
229 cout << "Error in mothur class mothurOut" << endl;
233 /**************************************************************************************************/
235 inline void mothurOut(string message, string precision) {
238 string logFileName = "mothur.logFile";
239 openOutputFileAppend(logFileName, out);
241 cout << precision << message;
242 out << precision << message;
246 catch(exception& e) {
247 cout << "Error in mothur class mothurOut" << endl;
252 /**************************************************************************************************/
254 inline void mothurOutEndLine() {
257 string logFileName = "mothur.logFile";
258 openOutputFileAppend(logFileName, out);
265 catch(exception& e) {
266 cout << "error in mothur mothurOutEndLine" << endl;
272 /**************************************************************************************************/
274 inline void errorOut(exception& e, string object, string function) {
276 mothurOut("Error: ");
277 mothurOut(toString(e.what()));
278 mothurOut(" has occurred in the " + object + " class function " + function + ". Please contact Pat Schloss at mothur.bugs@gmail.com, and be sure to include the mothur.logFile with your inquiry.");
286 /***********************************************************************/
288 inline bool isTrue(string f){
290 if ((f == "TRUE") || (f == "T") || (f == "true") || (f == "t")) { return true; }
291 else { return false; }
294 /***********************************************************************/
296 inline float roundDist(float dist, int precision){
298 return int(dist * precision + 0.5)/float(precision);
302 /***********************************************************************/
304 inline int getNumNames(string names){
310 for(int i=0;i<names.size();i++){
321 /**************************************************************************************************/
323 inline vector<vector<double> > binomial(int maxOrder){
325 vector<vector<double> > binomial(maxOrder+1);
327 for(int i=0;i<=maxOrder;i++){
328 binomial[i].resize(maxOrder+1);
337 for(int i=2;i<=maxOrder;i++){
341 for(int i=2;i<=maxOrder;i++){
342 for(int j=1;j<=maxOrder;j++){
343 if(i==j){ binomial[i][j]=1; }
344 if(j>i) { binomial[i][j]=0; }
345 else { binomial[i][j]=binomial[i-1][j-1]+binomial[i-1][j]; }
352 /***********************************************************************/
354 inline string getRootName(string longName){
356 string rootName = longName;
358 if(longName.find_last_of(".") != longName.npos){
359 int pos = longName.find_last_of('.')+1;
360 rootName = longName.substr(0, pos);
365 /***********************************************************************/
367 inline string getSimpleName(string longName){
369 string simpleName = longName;
371 if(longName.find_last_of("/") != longName.npos){
372 int pos = longName.find_last_of('/')+1;
373 simpleName = longName.substr(pos, longName.length());
379 /***********************************************************************/
381 inline int factorial(int num){
384 for (int i = 1; i <= num; i++) {
390 /**************************************************************************************************
392 double min(double x, double y)
398 /***********************************************************************/
400 inline string getPathName(string longName){
402 string rootPathName = longName;
404 if(longName.find_last_of('/') != longName.npos){
405 int pos = longName.find_last_of('/')+1;
406 rootPathName = longName.substr(0, pos);
412 /***********************************************************************/
414 inline string getExtension(string longName){
416 string extension = longName;
418 if(longName.find_last_of('.') != longName.npos){
419 int pos = longName.find_last_of('.');
420 extension = longName.substr(pos, longName.length());
426 /***********************************************************************/
428 inline int openInputFile(string fileName, ifstream& fileHandle){
430 fileHandle.open(fileName.c_str());
432 mothurOut("Error: Could not open " + fileName); mothurOutEndLine();
436 //check for blank file
438 if (fileHandle.eof()) { mothurOut(fileName + " is blank. Please correct."); mothurOutEndLine(); return 1; }
445 /***********************************************************************/
447 inline int openOutputFile(string fileName, ofstream& fileHandle){
449 fileHandle.open(fileName.c_str(), ios::trunc);
451 mothurOut("Error: Could not open " + fileName); mothurOutEndLine();
460 /***********************************************************************/
462 inline int getNumSeqs(ifstream& file){
464 int numSeqs = count(istreambuf_iterator<char>(file),istreambuf_iterator<char>(), '>');
470 /***********************************************************************/
472 //This function parses the estimator options and puts them in a vector
473 inline void splitAtDash(string& estim, vector<string>& container) {
477 while (estim.find_first_of('-') != -1) {
478 individual = estim.substr(0,estim.find_first_of('-'));
479 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
480 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
481 container.push_back(individual);
485 container.push_back(estim);
487 catch(exception& e) {
488 errorOut(e, "mothur", "splitAtDash");
493 /***********************************************************************/
494 //This function parses the label options and puts them in a set
495 inline void splitAtDash(string& estim, set<string>& container) {
499 while (estim.find_first_of('-') != -1) {
500 individual = estim.substr(0,estim.find_first_of('-'));
501 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
502 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
503 container.insert(individual);
507 container.insert(estim);
509 catch(exception& e) {
510 errorOut(e, "mothur", "splitAtDash");
514 /***********************************************************************/
515 //This function parses the line options and puts them in a set
516 inline void splitAtDash(string& estim, set<int>& container) {
521 while (estim.find_first_of('-') != -1) {
522 individual = estim.substr(0,estim.find_first_of('-'));
523 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
524 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
525 convert(individual, lineNum); //convert the string to int
526 container.insert(lineNum);
530 convert(estim, lineNum); //convert the string to int
531 container.insert(lineNum);
533 catch(exception& e) {
534 errorOut(e, "mothur", "splitAtDash");
538 /***********************************************************************/
539 //This function parses the a string and puts peices in a vector
540 inline void splitAtComma(string& estim, vector<string>& container) {
544 while (estim.find_first_of(',') != -1) {
545 individual = estim.substr(0,estim.find_first_of(','));
546 if ((estim.find_first_of(',')+1) <= estim.length()) { //checks to make sure you don't have comma at end of string
547 estim = estim.substr(estim.find_first_of(',')+1, estim.length());
548 container.push_back(individual);
552 container.push_back(estim);
554 catch(exception& e) {
555 errorOut(e, "mothur", "splitAtComma");
559 /***********************************************************************/
561 //This function splits up the various option parameters
562 inline void splitAtComma(string& prefix, string& suffix){
564 prefix = suffix.substr(0,suffix.find_first_of(','));
565 if ((suffix.find_first_of(',')+2) <= suffix.length()) { //checks to make sure you don't have comma at end of string
566 suffix = suffix.substr(suffix.find_first_of(',')+1, suffix.length());
568 while(suffix.at(0) == ' ')
569 suffix = suffix.substr(1, suffix.length());
573 catch(exception& e) {
574 errorOut(e, "mothur", "splitAtComma");
578 /***********************************************************************/
580 //This function separates the key value from the option value i.e. dist=96_...
581 inline void splitAtEquals(string& key, string& value){
583 if(value.find_first_of('=') != -1){
584 key = value.substr(0,value.find_first_of('='));
585 if ((value.find_first_of('=')+1) <= value.length()) {
586 value = value.substr(value.find_first_of('=')+1, value.length());
593 catch(exception& e) {
594 errorOut(e, "mothur", "splitAtEquals");
598 /**************************************************************************************************/
600 inline bool inUsersGroups(string groupname, vector<string> Groups) {
602 for (int i = 0; i < Groups.size(); i++) {
603 if (groupname == Groups[i]) { return true; }
607 catch(exception& e) {
608 errorOut(e, "mothur", "inUsersGroups");
613 /**************************************************************************************************/
615 inline void mothurOutJustToLog(string message) {
618 string logFileName = "mothur.logFile";
619 openOutputFileAppend(logFileName, out);
625 catch(exception& e) {
626 errorOut(e, "mothur", "mothurOutJustToLog");
632 /**************************************************************************************************/
634 inline void mothurOut(float num) {
637 string logFileName = "mothur.logFile";
638 openOutputFileAppend(logFileName, out);
645 catch(exception& e) {
646 cout << "Error in mothur class mothurOut float" << endl;
650 /***********************************************************************/
651 inline void mothurOut(double value) {
654 string logFileName = "mothur.logFile";
655 openOutputFileAppend(logFileName, out);
662 catch(exception& e) {
663 cout << "Error in mothur class mothurOut double" << endl;
668 /***********************************************************************/
669 //this function determines if the user has given us labels that are smaller than the given label.
670 //if so then it returns true so that the calling function can run the previous valid distance.
671 //it's a "smart" distance function. It also checks for invalid labels.
672 inline bool anyLabelsToProcess(string label, set<string>& userLabels, string errorOff) {
674 set<string>::iterator it;
675 vector<float> orderFloat;
676 map<string, float> userMap; //the conversion process removes trailing 0's which we need to put back
677 map<string, float>::iterator it2;
679 bool smaller = false;
681 //unique is the smallest line
682 if (label == "unique") { return false; }
683 else { convert(label, labelFloat); }
685 //go through users set and make them floats
686 for(it = userLabels.begin(); it != userLabels.end(); ++it) {
689 if ((*it != "unique") && (convertTestFloat(*it, temp) == true)){
691 orderFloat.push_back(temp);
693 }else if (*it == "unique") {
694 orderFloat.push_back(-1.0);
695 userMap["unique"] = -1.0;
697 if (errorOff == "") { mothurOut(*it + " is not a valid label."); mothurOutEndLine(); }
698 userLabels.erase(*it);
704 sort(orderFloat.begin(), orderFloat.end());
706 /*************************************************/
707 //is this label bigger than any of the users labels
708 /*************************************************/
710 //loop through order until you find a label greater than label
711 for (int i = 0; i < orderFloat.size(); i++) {
712 if (orderFloat[i] < labelFloat) {
714 if (orderFloat[i] == -1) {
715 if (errorOff == "") { mothurOut("Your file does not include the label unique."); mothurOutEndLine(); }
716 userLabels.erase("unique");
719 if (errorOff == "") { mothurOut("Your file does not include the label "); mothurOutEndLine(); }
721 for (it2 = userMap.begin(); it2!= userMap.end(); it2++) {
722 if (it2->second == orderFloat[i]) {
724 //remove small labels
729 if (errorOff == "") { mothurOut(s + ". I will use the next smallest distance. "); mothurOutEndLine(); }
731 //since they are sorted once you find a bigger one stop looking
738 catch(exception& e) {
739 errorOut(e, "mothur", "anyLabelsToProcess");
744 /**************************************************************************************************/