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 string getline(ifstream& fileHandle) {
179 while (!fileHandle.eof()) {
181 char c = fileHandle.get();
183 //are you at the end of the line
184 if ((c == '\n') || (c == '\r') || (c == '\f')){ break; }
191 catch(exception& e) {
192 cout << "Error in mothur function getline" << endl;
197 /**************************************************************************************************/
199 inline void mothurOut(string message) {
202 string logFileName = "mothur.logFile";
203 openOutputFileAppend(logFileName, out);
210 catch(exception& e) {
211 cout << "Error in mothur class mothurOut" << endl;
215 /**************************************************************************************************/
217 inline void mothurOut(string message, string precision) {
220 string logFileName = "mothur.logFile";
221 openOutputFileAppend(logFileName, out);
223 cout << precision << message;
224 out << precision << message;
228 catch(exception& e) {
229 cout << "Error in mothur class mothurOut" << endl;
234 /**************************************************************************************************/
236 inline void mothurOutEndLine() {
239 string logFileName = "mothur.logFile";
240 openOutputFileAppend(logFileName, out);
247 catch(exception& e) {
248 cout << "error in mothur mothurOutEndLine" << endl;
254 /**************************************************************************************************/
256 inline void errorOut(exception& e, string object, string function) {
258 mothurOut("Error: ");
259 mothurOut(toString(e.what()));
260 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.");
268 /***********************************************************************/
270 inline void gobble(istream& f){
273 while(isspace(d=f.get())) {;}
277 /***********************************************************************/
279 inline bool isTrue(string f){
281 if ((f == "TRUE") || (f == "T") || (f == "true") || (f == "t")) { return true; }
282 else { return false; }
285 /***********************************************************************/
287 inline float roundDist(float dist, int precision){
289 return int(dist * precision + 0.5)/float(precision);
293 /***********************************************************************/
295 inline int getNumNames(string names){
301 for(int i=0;i<names.size();i++){
312 /**************************************************************************************************/
314 inline vector<vector<double> > binomial(int maxOrder){
316 vector<vector<double> > binomial(maxOrder+1);
318 for(int i=0;i<=maxOrder;i++){
319 binomial[i].resize(maxOrder+1);
328 for(int i=2;i<=maxOrder;i++){
332 for(int i=2;i<=maxOrder;i++){
333 for(int j=1;j<=maxOrder;j++){
334 if(i==j){ binomial[i][j]=1; }
335 if(j>i) { binomial[i][j]=0; }
336 else { binomial[i][j]=binomial[i-1][j-1]+binomial[i-1][j]; }
343 /***********************************************************************/
345 inline string getRootName(string longName){
347 string rootName = longName;
349 if(longName.find_last_of(".") != longName.npos){
350 int pos = longName.find_last_of('.')+1;
351 rootName = longName.substr(0, pos);
356 /***********************************************************************/
358 inline string getSimpleName(string longName){
360 string simpleName = longName;
362 if(longName.find_last_of("/") != longName.npos){
363 int pos = longName.find_last_of('/')+1;
364 simpleName = longName.substr(pos, longName.length());
370 /***********************************************************************/
372 inline int factorial(int num){
375 for (int i = 1; i <= num; i++) {
381 /**************************************************************************************************
383 double min(double x, double y)
389 /***********************************************************************/
391 inline string getPathName(string longName){
393 string rootPathName = longName;
395 if(longName.find_last_of('/') != longName.npos){
396 int pos = longName.find_last_of('/')+1;
397 rootPathName = longName.substr(0, pos);
403 /***********************************************************************/
405 inline string getExtension(string longName){
407 string extension = longName;
409 if(longName.find_last_of('.') != longName.npos){
410 int pos = longName.find_last_of('.');
411 extension = longName.substr(pos, longName.length());
417 /***********************************************************************/
419 inline int openInputFile(string fileName, ifstream& fileHandle){
421 fileHandle.open(fileName.c_str());
423 mothurOut("Error: Could not open " + fileName); mothurOutEndLine();
432 /***********************************************************************/
434 inline int openOutputFile(string fileName, ofstream& fileHandle){
436 fileHandle.open(fileName.c_str(), ios::trunc);
438 mothurOut("Error: Could not open " + fileName); mothurOutEndLine();
447 /***********************************************************************/
449 inline int getNumSeqs(ifstream& file){
451 int numSeqs = count(istreambuf_iterator<char>(file),istreambuf_iterator<char>(), '>');
457 /***********************************************************************/
459 //This function parses the estimator options and puts them in a vector
460 inline void splitAtDash(string& estim, vector<string>& container) {
464 while (estim.find_first_of('-') != -1) {
465 individual = estim.substr(0,estim.find_first_of('-'));
466 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
467 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
468 container.push_back(individual);
472 container.push_back(estim);
474 catch(exception& e) {
475 errorOut(e, "mothur", "splitAtDash");
480 /***********************************************************************/
481 //This function parses the label options and puts them in a set
482 inline void splitAtDash(string& estim, set<string>& container) {
486 while (estim.find_first_of('-') != -1) {
487 individual = estim.substr(0,estim.find_first_of('-'));
488 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
489 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
490 container.insert(individual);
494 container.insert(estim);
496 catch(exception& e) {
497 errorOut(e, "mothur", "splitAtDash");
501 /***********************************************************************/
502 //This function parses the line options and puts them in a set
503 inline void splitAtDash(string& estim, set<int>& container) {
508 while (estim.find_first_of('-') != -1) {
509 individual = estim.substr(0,estim.find_first_of('-'));
510 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
511 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
512 convert(individual, lineNum); //convert the string to int
513 container.insert(lineNum);
517 convert(estim, lineNum); //convert the string to int
518 container.insert(lineNum);
520 catch(exception& e) {
521 errorOut(e, "mothur", "splitAtDash");
525 /***********************************************************************/
526 //This function parses the a string and puts peices in a vector
527 inline void splitAtComma(string& estim, vector<string>& container) {
531 while (estim.find_first_of(',') != -1) {
532 individual = estim.substr(0,estim.find_first_of(','));
533 if ((estim.find_first_of(',')+1) <= estim.length()) { //checks to make sure you don't have comma at end of string
534 estim = estim.substr(estim.find_first_of(',')+1, estim.length());
535 container.push_back(individual);
539 container.push_back(estim);
541 catch(exception& e) {
542 errorOut(e, "mothur", "splitAtComma");
546 /***********************************************************************/
548 //This function splits up the various option parameters
549 inline void splitAtComma(string& prefix, string& suffix){
551 prefix = suffix.substr(0,suffix.find_first_of(','));
552 if ((suffix.find_first_of(',')+2) <= suffix.length()) { //checks to make sure you don't have comma at end of string
553 suffix = suffix.substr(suffix.find_first_of(',')+1, suffix.length());
555 while(suffix.at(0) == ' ')
556 suffix = suffix.substr(1, suffix.length());
560 catch(exception& e) {
561 errorOut(e, "mothur", "splitAtComma");
565 /***********************************************************************/
567 //This function separates the key value from the option value i.e. dist=96_...
568 inline void splitAtEquals(string& key, string& value){
570 if(value.find_first_of('=') != -1){
571 key = value.substr(0,value.find_first_of('='));
572 if ((value.find_first_of('=')+1) <= value.length()) {
573 value = value.substr(value.find_first_of('=')+1, value.length());
580 catch(exception& e) {
581 errorOut(e, "mothur", "splitAtEquals");
585 /**************************************************************************************************/
587 inline bool inUsersGroups(string groupname, vector<string> Groups) {
589 for (int i = 0; i < Groups.size(); i++) {
590 if (groupname == Groups[i]) { return true; }
594 catch(exception& e) {
595 errorOut(e, "mothur", "inUsersGroups");
600 /**************************************************************************************************/
602 inline void mothurOutJustToLog(string message) {
605 string logFileName = "mothur.logFile";
606 openOutputFileAppend(logFileName, out);
612 catch(exception& e) {
613 errorOut(e, "mothur", "mothurOutJustToLog");
619 /**************************************************************************************************/
621 inline void mothurOut(float num) {
624 string logFileName = "mothur.logFile";
625 openOutputFileAppend(logFileName, out);
632 catch(exception& e) {
633 cout << "Error in mothur class mothurOut float" << endl;
637 /***********************************************************************/
638 inline void mothurOut(double value) {
641 string logFileName = "mothur.logFile";
642 openOutputFileAppend(logFileName, out);
649 catch(exception& e) {
650 cout << "Error in mothur class mothurOut double" << endl;
655 /***********************************************************************/
656 //this function determines if the user has given us labels that are smaller than the given label.
657 //if so then it returns true so that the calling function can run the previous valid distance.
658 //it's a "smart" distance function. It also checks for invalid labels.
659 inline bool anyLabelsToProcess(string label, set<string>& userLabels, string errorOff) {
661 set<string>::iterator it;
662 vector<float> orderFloat;
663 map<string, float> userMap; //the conversion process removes trailing 0's which we need to put back
664 map<string, float>::iterator it2;
666 bool smaller = false;
668 //unique is the smallest line
669 if (label == "unique") { return false; }
670 else { convert(label, labelFloat); }
672 //go through users set and make them floats
673 for(it = userLabels.begin(); it != userLabels.end(); ++it) {
676 if ((*it != "unique") && (convertTestFloat(*it, temp) == true)){
678 orderFloat.push_back(temp);
680 }else if (*it == "unique") {
681 orderFloat.push_back(-1.0);
682 userMap["unique"] = -1.0;
684 if (errorOff == "") { mothurOut(*it + " is not a valid label."); mothurOutEndLine(); }
685 userLabels.erase(*it);
691 sort(orderFloat.begin(), orderFloat.end());
693 /*************************************************/
694 //is this label bigger than any of the users labels
695 /*************************************************/
697 //loop through order until you find a label greater than label
698 for (int i = 0; i < orderFloat.size(); i++) {
699 if (orderFloat[i] < labelFloat) {
701 if (orderFloat[i] == -1) {
702 if (errorOff == "") { mothurOut("Your file does not include the label unique."); mothurOutEndLine(); }
703 userLabels.erase("unique");
706 if (errorOff == "") { mothurOut("Your file does not include the label "); mothurOutEndLine(); }
708 for (it2 = userMap.begin(); it2!= userMap.end(); it2++) {
709 if (it2->second == orderFloat[i]) {
711 //remove small labels
716 if (errorOff == "") { mothurOut(s + ". I will use the next smallest distance. "); mothurOutEndLine(); }
718 //since they are sorted once you find a bigger one stop looking
725 catch(exception& e) {
726 errorOut(e, "mothur", "anyLabelsToProcess");
731 /**************************************************************************************************/