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. */
50 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
57 #define exp(x) (exp((double) x))
58 #define sqrt(x) (sqrt((double) x))
59 #define log10(x) (log10((double) x))
60 #define log2(x) (log10(x)/log10(2))
61 #define isnan(x) ((x) != (x))
62 #define isinf(x) (fabs(x) == std::numeric_limits<double>::infinity())
65 typedef unsigned long ull;
75 IntNode(int lv, int rv, IntNode* l, IntNode* r) : lvalue(lv), rvalue(rv), left(l), right(r) {};
85 /************************************************************/
89 int smallChild; //used to make linkTable work with list and rabund. represents bin number of this cluster node
90 clusterNode(int num, int par, int kid) : numSeq(num), parent(par), smallChild(kid) {};
93 /***********************************************************************/
95 // snagged from http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.2
96 // works for now, but there should be a way to do it without killing the whole program
98 class BadConversion : public runtime_error {
100 BadConversion(const string& s) : runtime_error(s){ }
103 //**********************************************************************************************************************
106 inline void convert(const string& s, T& x, bool failIfLeftoverChars = true){
109 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
110 throw BadConversion(s);
113 //**********************************************************************************************************************
116 inline bool convertTestFloat(const string& s, T& x, bool failIfLeftoverChars = true){
119 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
126 //**********************************************************************************************************************
129 inline bool convertTest(const string& s, T& x, bool failIfLeftoverChars = true){
132 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
134 cout << "unable to be converted into an integer.\n" << endl;
140 //**********************************************************************************************************************
143 string toString(const T&x){
149 //**********************************************************************************************************************
152 string toHex(const T&x){
159 //**********************************************************************************************************************
162 string toString(const T&x, int i){
166 output << fixed << x;
170 /***********************************************************************/
172 inline int openOutputFileAppend(string fileName, ofstream& fileHandle){
174 fileHandle.open(fileName.c_str(), ios::app);
176 cout << "Error: Could not open " << fileName << endl;
184 /***********************************************************************/
186 inline void gobble(istream& f){
189 while(isspace(d=f.get())) {;}
194 /***********************************************************************/
196 inline string getline(ifstream& fileHandle) {
201 while (!fileHandle.eof()) {
203 char c = fileHandle.get();
205 //are you at the end of the line
206 if ((c == '\n') || (c == '\r') || (c == '\f')){ break; }
213 catch(exception& e) {
214 cout << "Error in mothur function getline" << endl;
219 /**************************************************************************************************/
221 inline void mothurOut(string message) {
224 string logFileName = "mothur.logFile";
225 openOutputFileAppend(logFileName, out);
232 catch(exception& e) {
233 cout << "Error in mothur class mothurOut" << endl;
237 /**************************************************************************************************/
239 inline void mothurOut(string message, string precision) {
242 string logFileName = "mothur.logFile";
243 openOutputFileAppend(logFileName, out);
245 cout << precision << message;
246 out << precision << message;
250 catch(exception& e) {
251 cout << "Error in mothur class mothurOut" << endl;
256 /**************************************************************************************************/
258 inline void mothurOutEndLine() {
261 string logFileName = "mothur.logFile";
262 openOutputFileAppend(logFileName, out);
269 catch(exception& e) {
270 cout << "error in mothur mothurOutEndLine" << endl;
276 /**************************************************************************************************/
278 inline void errorOut(exception& e, string object, string function) {
280 mothurOut("Error: ");
281 mothurOut(toString(e.what()));
282 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.");
287 /***********************************************************************/
289 inline bool isTrue(string f){
291 if ((f == "TRUE") || (f == "T") || (f == "true") || (f == "t")) { return true; }
292 else { return false; }
295 /***********************************************************************/
297 inline float roundDist(float dist, int precision){
299 return int(dist * precision + 0.5)/float(precision);
303 /***********************************************************************/
305 inline int getNumNames(string names){
311 for(int i=0;i<names.size();i++){
322 /**************************************************************************************************/
324 inline vector<vector<double> > binomial(int maxOrder){
326 vector<vector<double> > binomial(maxOrder+1);
328 for(int i=0;i<=maxOrder;i++){
329 binomial[i].resize(maxOrder+1);
338 for(int i=2;i<=maxOrder;i++){
342 for(int i=2;i<=maxOrder;i++){
343 for(int j=1;j<=maxOrder;j++){
344 if(i==j){ binomial[i][j]=1; }
345 if(j>i) { binomial[i][j]=0; }
346 else { binomial[i][j]=binomial[i-1][j-1]+binomial[i-1][j]; }
353 /***********************************************************************/
355 inline string getRootName(string longName){
357 string rootName = longName;
359 if(longName.find_last_of(".") != longName.npos){
360 int pos = longName.find_last_of('.')+1;
361 rootName = longName.substr(0, pos);
366 /***********************************************************************/
368 inline string getSimpleName(string longName){
370 string simpleName = longName;
372 if(longName.find_last_of("/") != longName.npos){
373 int pos = longName.find_last_of('/')+1;
374 simpleName = longName.substr(pos, longName.length());
380 /***********************************************************************/
382 inline int factorial(int num){
385 for (int i = 1; i <= num; i++) {
391 /**************************************************************************************************
393 double min(double x, double y)
399 /***********************************************************************/
401 inline string getPathName(string longName){
403 string rootPathName = longName;
405 if(longName.find_last_of('/') != longName.npos){
406 int pos = longName.find_last_of('/')+1;
407 rootPathName = longName.substr(0, pos);
413 /***********************************************************************/
415 inline string getExtension(string longName){
417 string extension = longName;
419 if(longName.find_last_of('.') != longName.npos){
420 int pos = longName.find_last_of('.');
421 extension = longName.substr(pos, longName.length());
426 /***********************************************************************/
427 inline bool isBlank(string fileName){
430 fileHandle.open(fileName.c_str());
432 mothurOut("Error: Could not open " + fileName); mothurOutEndLine();
435 //check for blank file
437 if (fileHandle.eof()) { fileHandle.close(); return true; }
441 /***********************************************************************/
443 inline int openInputFile(string fileName, ifstream& fileHandle){
445 fileHandle.open(fileName.c_str());
447 mothurOut("Error: Could not open " + fileName); mothurOutEndLine();
451 //check for blank file
453 if (fileHandle.eof()) { mothurOut(fileName + " is blank. Please correct."); mothurOutEndLine(); return 1; }
460 /***********************************************************************/
462 inline int openOutputFile(string fileName, ofstream& fileHandle){
464 fileHandle.open(fileName.c_str(), ios::trunc);
466 mothurOut("Error: Could not open " + fileName); mothurOutEndLine();
475 /***********************************************************************/
477 inline int getNumSeqs(ifstream& file){
479 int numSeqs = count(istreambuf_iterator<char>(file),istreambuf_iterator<char>(), '>');
485 /***********************************************************************/
487 //This function parses the estimator options and puts them in a vector
488 inline void splitAtDash(string& estim, vector<string>& container) {
492 while (estim.find_first_of('-') != -1) {
493 individual = estim.substr(0,estim.find_first_of('-'));
494 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
495 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
496 container.push_back(individual);
500 container.push_back(estim);
502 catch(exception& e) {
503 errorOut(e, "mothur", "splitAtDash");
508 /***********************************************************************/
509 //This function parses the label options and puts them in a set
510 inline void splitAtDash(string& estim, set<string>& container) {
514 while (estim.find_first_of('-') != -1) {
515 individual = estim.substr(0,estim.find_first_of('-'));
516 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
517 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
518 container.insert(individual);
522 container.insert(estim);
524 catch(exception& e) {
525 errorOut(e, "mothur", "splitAtDash");
529 /***********************************************************************/
530 //This function parses the line options and puts them in a set
531 inline void splitAtDash(string& estim, set<int>& 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 dash at end of string
539 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
540 convert(individual, lineNum); //convert the string to int
541 container.insert(lineNum);
545 convert(estim, lineNum); //convert the string to int
546 container.insert(lineNum);
548 catch(exception& e) {
549 errorOut(e, "mothur", "splitAtDash");
553 /***********************************************************************/
554 //This function parses the a string and puts peices in a vector
555 inline void splitAtComma(string& estim, vector<string>& container) {
559 while (estim.find_first_of(',') != -1) {
560 individual = estim.substr(0,estim.find_first_of(','));
561 if ((estim.find_first_of(',')+1) <= estim.length()) { //checks to make sure you don't have comma at end of string
562 estim = estim.substr(estim.find_first_of(',')+1, estim.length());
563 container.push_back(individual);
567 container.push_back(estim);
569 catch(exception& e) {
570 errorOut(e, "mothur", "splitAtComma");
574 /***********************************************************************/
576 //This function splits up the various option parameters
577 inline void splitAtComma(string& prefix, string& suffix){
579 prefix = suffix.substr(0,suffix.find_first_of(','));
580 if ((suffix.find_first_of(',')+2) <= suffix.length()) { //checks to make sure you don't have comma at end of string
581 suffix = suffix.substr(suffix.find_first_of(',')+1, suffix.length());
583 while(suffix.at(0) == ' ')
584 suffix = suffix.substr(1, suffix.length());
588 catch(exception& e) {
589 errorOut(e, "mothur", "splitAtComma");
593 /***********************************************************************/
595 //This function separates the key value from the option value i.e. dist=96_...
596 inline void splitAtEquals(string& key, string& value){
598 if(value.find_first_of('=') != -1){
599 key = value.substr(0,value.find_first_of('='));
600 if ((value.find_first_of('=')+1) <= value.length()) {
601 value = value.substr(value.find_first_of('=')+1, value.length());
608 catch(exception& e) {
609 errorOut(e, "mothur", "splitAtEquals");
613 /**************************************************************************************************/
615 inline bool inUsersGroups(string groupname, vector<string> Groups) {
617 for (int i = 0; i < Groups.size(); i++) {
618 if (groupname == Groups[i]) { return true; }
622 catch(exception& e) {
623 errorOut(e, "mothur", "inUsersGroups");
628 /**************************************************************************************************/
630 inline void mothurOutJustToLog(string message) {
633 string logFileName = "mothur.logFile";
634 openOutputFileAppend(logFileName, out);
640 catch(exception& e) {
641 errorOut(e, "mothur", "mothurOutJustToLog");
647 /**************************************************************************************************/
649 inline void mothurOut(float num) {
652 string logFileName = "mothur.logFile";
653 openOutputFileAppend(logFileName, out);
660 catch(exception& e) {
661 cout << "Error in mothur class mothurOut float" << endl;
665 /***********************************************************************/
666 inline void mothurOut(double value) {
669 string logFileName = "mothur.logFile";
670 openOutputFileAppend(logFileName, out);
677 catch(exception& e) {
678 cout << "Error in mothur class mothurOut double" << endl;
683 /***********************************************************************/
684 //this function determines if the user has given us labels that are smaller than the given label.
685 //if so then it returns true so that the calling function can run the previous valid distance.
686 //it's a "smart" distance function. It also checks for invalid labels.
687 inline bool anyLabelsToProcess(string label, set<string>& userLabels, string errorOff) {
689 set<string>::iterator it;
690 vector<float> orderFloat;
691 map<string, float> userMap; //the conversion process removes trailing 0's which we need to put back
692 map<string, float>::iterator it2;
694 bool smaller = false;
696 //unique is the smallest line
697 if (label == "unique") { return false; }
698 else { convert(label, labelFloat); }
700 //go through users set and make them floats
701 for(it = userLabels.begin(); it != userLabels.end(); ++it) {
704 if ((*it != "unique") && (convertTestFloat(*it, temp) == true)){
706 orderFloat.push_back(temp);
708 }else if (*it == "unique") {
709 orderFloat.push_back(-1.0);
710 userMap["unique"] = -1.0;
712 if (errorOff == "") { mothurOut(*it + " is not a valid label."); mothurOutEndLine(); }
713 userLabels.erase(*it);
719 sort(orderFloat.begin(), orderFloat.end());
721 /*************************************************/
722 //is this label bigger than any of the users labels
723 /*************************************************/
725 //loop through order until you find a label greater than label
726 for (int i = 0; i < orderFloat.size(); i++) {
727 if (orderFloat[i] < labelFloat) {
729 if (orderFloat[i] == -1) {
730 if (errorOff == "") { mothurOut("Your file does not include the label unique."); mothurOutEndLine(); }
731 userLabels.erase("unique");
734 if (errorOff == "") { mothurOut("Your file does not include the label "); mothurOutEndLine(); }
736 for (it2 = userMap.begin(); it2!= userMap.end(); it2++) {
737 if (it2->second == orderFloat[i]) {
739 //remove small labels
744 if (errorOff == "") { mothurOut(s + ". I will use the next smallest distance. "); mothurOutEndLine(); }
746 //since they are sorted once you find a bigger one stop looking
753 catch(exception& e) {
754 errorOut(e, "mothur", "anyLabelsToProcess");
759 /**************************************************************************************************/