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;
74 IntNode(int lv, int rv, IntNode* l, IntNode* r) : lvalue(lv), rvalue(rv), left(l), right(r) {};
84 /************************************************************/
88 int smallChild; //used to make linkTable work with list and rabund. represents bin number of this cluster node
89 clusterNode(int num, int par, int kid) : numSeq(num), parent(par), smallChild(kid) {};
92 /***********************************************************************/
94 // snagged from http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.2
95 // works for now, but there should be a way to do it without killing the whole program
97 class BadConversion : public runtime_error {
99 BadConversion(const string& s) : runtime_error(s){ }
102 //**********************************************************************************************************************
105 inline void convert(const string& s, T& x, bool failIfLeftoverChars = true){
108 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
109 throw BadConversion(s);
112 //**********************************************************************************************************************
115 inline bool convertTestFloat(const string& s, T& x, bool failIfLeftoverChars = true){
118 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
125 //**********************************************************************************************************************
128 inline bool convertTest(const string& s, T& x, bool failIfLeftoverChars = true){
131 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
133 cout << "unable to be converted into an integer.\n" << endl;
139 //**********************************************************************************************************************
142 string toString(const T&x){
148 //**********************************************************************************************************************
151 string toHex(const T&x){
158 //**********************************************************************************************************************
161 string toString(const T&x, int i){
165 output << fixed << x;
169 /***********************************************************************/
171 inline int openOutputFileAppend(string fileName, ofstream& fileHandle){
173 fileHandle.open(fileName.c_str(), ios::app);
175 cout << "Error: Could not open " << fileName << endl;
183 /***********************************************************************/
185 inline void gobble(istream& f){
188 while(isspace(d=f.get())) {;}
193 /***********************************************************************/
195 inline string getline(ifstream& fileHandle) {
200 while (!fileHandle.eof()) {
202 char c = fileHandle.get();
204 //are you at the end of the line
205 if ((c == '\n') || (c == '\r') || (c == '\f')){ break; }
212 catch(exception& e) {
213 cout << "Error in mothur function getline" << endl;
218 /**************************************************************************************************/
220 inline void mothurOut(string message) {
223 string logFileName = "mothur.logFile";
224 openOutputFileAppend(logFileName, out);
231 catch(exception& e) {
232 cout << "Error in mothur class mothurOut" << endl;
236 /**************************************************************************************************/
238 inline void mothurOut(string message, string precision) {
241 string logFileName = "mothur.logFile";
242 openOutputFileAppend(logFileName, out);
244 cout << precision << message;
245 out << precision << message;
249 catch(exception& e) {
250 cout << "Error in mothur class mothurOut" << endl;
255 /**************************************************************************************************/
257 inline void mothurOutEndLine() {
260 string logFileName = "mothur.logFile";
261 openOutputFileAppend(logFileName, out);
268 catch(exception& e) {
269 cout << "error in mothur mothurOutEndLine" << endl;
275 /**************************************************************************************************/
277 inline void errorOut(exception& e, string object, string function) {
279 mothurOut("Error: ");
280 mothurOut(toString(e.what()));
281 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.");
289 /***********************************************************************/
291 inline bool isTrue(string f){
293 if ((f == "TRUE") || (f == "T") || (f == "true") || (f == "t")) { return true; }
294 else { return false; }
297 /***********************************************************************/
299 inline float roundDist(float dist, int precision){
301 return int(dist * precision + 0.5)/float(precision);
305 /***********************************************************************/
307 inline int getNumNames(string names){
313 for(int i=0;i<names.size();i++){
324 /**************************************************************************************************/
326 inline vector<vector<double> > binomial(int maxOrder){
328 vector<vector<double> > binomial(maxOrder+1);
330 for(int i=0;i<=maxOrder;i++){
331 binomial[i].resize(maxOrder+1);
340 for(int i=2;i<=maxOrder;i++){
344 for(int i=2;i<=maxOrder;i++){
345 for(int j=1;j<=maxOrder;j++){
346 if(i==j){ binomial[i][j]=1; }
347 if(j>i) { binomial[i][j]=0; }
348 else { binomial[i][j]=binomial[i-1][j-1]+binomial[i-1][j]; }
355 /***********************************************************************/
357 inline string getRootName(string longName){
359 string rootName = longName;
361 if(longName.find_last_of(".") != longName.npos){
362 int pos = longName.find_last_of('.')+1;
363 rootName = longName.substr(0, pos);
368 /***********************************************************************/
370 inline string getSimpleName(string longName){
372 string simpleName = longName;
374 if(longName.find_last_of("/") != longName.npos){
375 int pos = longName.find_last_of('/')+1;
376 simpleName = longName.substr(pos, longName.length());
382 /***********************************************************************/
384 inline int factorial(int num){
387 for (int i = 1; i <= num; i++) {
393 /**************************************************************************************************
395 double min(double x, double y)
401 /***********************************************************************/
403 inline string getPathName(string longName){
405 string rootPathName = longName;
407 if(longName.find_last_of('/') != longName.npos){
408 int pos = longName.find_last_of('/')+1;
409 rootPathName = longName.substr(0, pos);
415 /***********************************************************************/
417 inline string getExtension(string longName){
419 string extension = longName;
421 if(longName.find_last_of('.') != longName.npos){
422 int pos = longName.find_last_of('.');
423 extension = longName.substr(pos, longName.length());
429 /***********************************************************************/
431 inline int openInputFile(string fileName, ifstream& fileHandle){
433 fileHandle.open(fileName.c_str());
435 mothurOut("Error: Could not open " + fileName); mothurOutEndLine();
439 //check for blank file
441 if (fileHandle.eof()) { mothurOut(fileName + " is blank. Please correct."); mothurOutEndLine(); return 1; }
448 /***********************************************************************/
450 inline int openOutputFile(string fileName, ofstream& fileHandle){
452 fileHandle.open(fileName.c_str(), ios::trunc);
454 mothurOut("Error: Could not open " + fileName); mothurOutEndLine();
463 /***********************************************************************/
465 inline int getNumSeqs(ifstream& file){
467 int numSeqs = count(istreambuf_iterator<char>(file),istreambuf_iterator<char>(), '>');
473 /***********************************************************************/
475 //This function parses the estimator options and puts them in a vector
476 inline void splitAtDash(string& estim, vector<string>& container) {
480 while (estim.find_first_of('-') != -1) {
481 individual = estim.substr(0,estim.find_first_of('-'));
482 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
483 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
484 container.push_back(individual);
488 container.push_back(estim);
490 catch(exception& e) {
491 errorOut(e, "mothur", "splitAtDash");
496 /***********************************************************************/
497 //This function parses the label options and puts them in a set
498 inline void splitAtDash(string& estim, set<string>& container) {
502 while (estim.find_first_of('-') != -1) {
503 individual = estim.substr(0,estim.find_first_of('-'));
504 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
505 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
506 container.insert(individual);
510 container.insert(estim);
512 catch(exception& e) {
513 errorOut(e, "mothur", "splitAtDash");
517 /***********************************************************************/
518 //This function parses the line options and puts them in a set
519 inline void splitAtDash(string& estim, set<int>& container) {
524 while (estim.find_first_of('-') != -1) {
525 individual = estim.substr(0,estim.find_first_of('-'));
526 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
527 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
528 convert(individual, lineNum); //convert the string to int
529 container.insert(lineNum);
533 convert(estim, lineNum); //convert the string to int
534 container.insert(lineNum);
536 catch(exception& e) {
537 errorOut(e, "mothur", "splitAtDash");
541 /***********************************************************************/
542 //This function parses the a string and puts peices in a vector
543 inline void splitAtComma(string& estim, vector<string>& container) {
547 while (estim.find_first_of(',') != -1) {
548 individual = estim.substr(0,estim.find_first_of(','));
549 if ((estim.find_first_of(',')+1) <= estim.length()) { //checks to make sure you don't have comma at end of string
550 estim = estim.substr(estim.find_first_of(',')+1, estim.length());
551 container.push_back(individual);
555 container.push_back(estim);
557 catch(exception& e) {
558 errorOut(e, "mothur", "splitAtComma");
562 /***********************************************************************/
564 //This function splits up the various option parameters
565 inline void splitAtComma(string& prefix, string& suffix){
567 prefix = suffix.substr(0,suffix.find_first_of(','));
568 if ((suffix.find_first_of(',')+2) <= suffix.length()) { //checks to make sure you don't have comma at end of string
569 suffix = suffix.substr(suffix.find_first_of(',')+1, suffix.length());
571 while(suffix.at(0) == ' ')
572 suffix = suffix.substr(1, suffix.length());
576 catch(exception& e) {
577 errorOut(e, "mothur", "splitAtComma");
581 /***********************************************************************/
583 //This function separates the key value from the option value i.e. dist=96_...
584 inline void splitAtEquals(string& key, string& value){
586 if(value.find_first_of('=') != -1){
587 key = value.substr(0,value.find_first_of('='));
588 if ((value.find_first_of('=')+1) <= value.length()) {
589 value = value.substr(value.find_first_of('=')+1, value.length());
596 catch(exception& e) {
597 errorOut(e, "mothur", "splitAtEquals");
601 /**************************************************************************************************/
603 inline bool inUsersGroups(string groupname, vector<string> Groups) {
605 for (int i = 0; i < Groups.size(); i++) {
606 if (groupname == Groups[i]) { return true; }
610 catch(exception& e) {
611 errorOut(e, "mothur", "inUsersGroups");
616 /**************************************************************************************************/
618 inline void mothurOutJustToLog(string message) {
621 string logFileName = "mothur.logFile";
622 openOutputFileAppend(logFileName, out);
628 catch(exception& e) {
629 errorOut(e, "mothur", "mothurOutJustToLog");
635 /**************************************************************************************************/
637 inline void mothurOut(float num) {
640 string logFileName = "mothur.logFile";
641 openOutputFileAppend(logFileName, out);
648 catch(exception& e) {
649 cout << "Error in mothur class mothurOut float" << endl;
653 /***********************************************************************/
654 inline void mothurOut(double value) {
657 string logFileName = "mothur.logFile";
658 openOutputFileAppend(logFileName, out);
665 catch(exception& e) {
666 cout << "Error in mothur class mothurOut double" << endl;
671 /***********************************************************************/
672 //this function determines if the user has given us labels that are smaller than the given label.
673 //if so then it returns true so that the calling function can run the previous valid distance.
674 //it's a "smart" distance function. It also checks for invalid labels.
675 inline bool anyLabelsToProcess(string label, set<string>& userLabels, string errorOff) {
677 set<string>::iterator it;
678 vector<float> orderFloat;
679 map<string, float> userMap; //the conversion process removes trailing 0's which we need to put back
680 map<string, float>::iterator it2;
682 bool smaller = false;
684 //unique is the smallest line
685 if (label == "unique") { return false; }
686 else { convert(label, labelFloat); }
688 //go through users set and make them floats
689 for(it = userLabels.begin(); it != userLabels.end(); ++it) {
692 if ((*it != "unique") && (convertTestFloat(*it, temp) == true)){
694 orderFloat.push_back(temp);
696 }else if (*it == "unique") {
697 orderFloat.push_back(-1.0);
698 userMap["unique"] = -1.0;
700 if (errorOff == "") { mothurOut(*it + " is not a valid label."); mothurOutEndLine(); }
701 userLabels.erase(*it);
707 sort(orderFloat.begin(), orderFloat.end());
709 /*************************************************/
710 //is this label bigger than any of the users labels
711 /*************************************************/
713 //loop through order until you find a label greater than label
714 for (int i = 0; i < orderFloat.size(); i++) {
715 if (orderFloat[i] < labelFloat) {
717 if (orderFloat[i] == -1) {
718 if (errorOff == "") { mothurOut("Your file does not include the label unique."); mothurOutEndLine(); }
719 userLabels.erase("unique");
722 if (errorOff == "") { mothurOut("Your file does not include the label "); mothurOutEndLine(); }
724 for (it2 = userMap.begin(); it2!= userMap.end(); it2++) {
725 if (it2->second == orderFloat[i]) {
727 //remove small labels
732 if (errorOff == "") { mothurOut(s + ". I will use the next smallest distance. "); mothurOutEndLine(); }
734 //since they are sorted once you find a bigger one stop looking
741 catch(exception& e) {
742 errorOut(e, "mothur", "anyLabelsToProcess");
747 /**************************************************************************************************/