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. */
51 #define exp(x) (exp((double) x))
52 #define sqrt(x) (sqrt((double) x))
53 #define log10(x) (log10((double) x))
54 #define log2(x) (log10(x)/log10(2))
55 #define isnan(x) ((x) != (x))
56 #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);
99 //**********************************************************************************************************************
102 inline bool convertTestFloat(const string& s, T& x, bool failIfLeftoverChars = true){
105 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
112 //**********************************************************************************************************************
115 inline bool convertTest(const string& s, T& x, bool failIfLeftoverChars = true){
118 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
120 cout << "unable to be converted into an integer.\n" << endl;
126 //**********************************************************************************************************************
129 string toString(const T&x){
135 //**********************************************************************************************************************
138 string toHex(const T&x){
145 //**********************************************************************************************************************
148 string toString(const T&x, int i){
152 output << fixed << x;
156 /***********************************************************************/
158 inline int openOutputFileAppend(string fileName, ofstream& fileHandle){
160 fileHandle.open(fileName.c_str(), ios::app);
162 cout << "Error: Could not open " << fileName << endl;
170 /***********************************************************************/
172 inline string getline(ifstream& fileHandle) {
177 while (!fileHandle.eof()) {
179 char c = fileHandle.get();
181 //are you at the end of the line
182 if (c == 10 || c == 13){ break; }
189 catch(exception& e) {
190 cout << "Error in mothur function getline" << endl;
195 /**************************************************************************************************/
197 inline void mothurOut(string message) {
200 string logFileName = "mothur.logFile";
201 openOutputFileAppend(logFileName, out);
208 catch(exception& e) {
209 cout << "Error in mothur class mothurOut" << endl;
213 /**************************************************************************************************/
215 inline void mothurOut(string message, string precision) {
218 string logFileName = "mothur.logFile";
219 openOutputFileAppend(logFileName, out);
221 cout << precision << message;
222 out << precision << message;
226 catch(exception& e) {
227 cout << "Error in mothur class mothurOut" << endl;
232 /**************************************************************************************************/
234 inline void mothurOutEndLine() {
237 string logFileName = "mothur.logFile";
238 openOutputFileAppend(logFileName, out);
245 catch(exception& e) {
246 cout << "error in mothur mothurOutEndLine" << endl;
252 /**************************************************************************************************/
254 inline void errorOut(exception& e, string object, string function) {
256 mothurOut("Error: ");
257 mothurOut(toString(e.what()));
258 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.");
266 /***********************************************************************/
268 inline void gobble(istream& f){
271 while(isspace(d=f.get())) {;}
275 /***********************************************************************/
277 inline bool isTrue(string f){
279 if ((f == "TRUE") || (f == "T") || (f == "true") || (f == "t")) { return true; }
280 else { return false; }
283 /***********************************************************************/
285 inline float roundDist(float dist, int precision){
287 return int(dist * precision + 0.5)/float(precision);
291 /***********************************************************************/
293 inline int getNumNames(string names){
299 for(int i=0;i<names.size();i++){
310 /**************************************************************************************************/
312 inline vector<vector<double> > binomial(int maxOrder){
314 vector<vector<double> > binomial(maxOrder+1);
316 for(int i=0;i<=maxOrder;i++){
317 binomial[i].resize(maxOrder+1);
326 for(int i=2;i<=maxOrder;i++){
330 for(int i=2;i<=maxOrder;i++){
331 for(int j=1;j<=maxOrder;j++){
332 if(i==j){ binomial[i][j]=1; }
333 if(j>i) { binomial[i][j]=0; }
334 else { binomial[i][j]=binomial[i-1][j-1]+binomial[i-1][j]; }
341 /***********************************************************************/
343 inline string getRootName(string longName){
345 string rootName = longName;
347 if(longName.find_last_of(".") != longName.npos){
348 int pos = longName.find_last_of('.')+1;
349 rootName = longName.substr(0, pos);
354 /***********************************************************************/
356 inline string getSimpleName(string longName){
358 string simpleName = longName;
360 if(longName.find_last_of("/") != longName.npos){
361 int pos = longName.find_last_of('/')+1;
362 simpleName = longName.substr(pos, longName.length());
368 /***********************************************************************/
370 inline int factorial(int num){
373 for (int i = 1; i <= num; i++) {
379 /**************************************************************************************************
381 double min(double x, double y)
387 /***********************************************************************/
389 inline string getPathName(string longName){
391 string rootPathName = longName;
393 if(longName.find_last_of('/') != longName.npos){
394 int pos = longName.find_last_of('/')+1;
395 rootPathName = longName.substr(0, pos);
401 /***********************************************************************/
403 inline string getExtension(string longName){
405 string extension = longName;
407 if(longName.find_last_of('.') != longName.npos){
408 int pos = longName.find_last_of('.');
409 extension = longName.substr(pos, longName.length());
415 /***********************************************************************/
417 inline int openInputFile(string fileName, ifstream& fileHandle){
419 fileHandle.open(fileName.c_str());
421 mothurOut("Error: Could not open " + fileName); mothurOutEndLine();
430 /***********************************************************************/
432 inline int openOutputFile(string fileName, ofstream& fileHandle){
434 fileHandle.open(fileName.c_str(), ios::trunc);
436 mothurOut("Error: Could not open " + fileName); mothurOutEndLine();
445 /***********************************************************************/
447 inline int getNumSeqs(ifstream& file){
449 int numSeqs = count(istreambuf_iterator<char>(file),istreambuf_iterator<char>(), '>');
455 /***********************************************************************/
457 //This function parses the estimator options and puts them in a vector
458 inline void splitAtDash(string& estim, vector<string>& container) {
462 while (estim.find_first_of('-') != -1) {
463 individual = estim.substr(0,estim.find_first_of('-'));
464 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
465 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
466 container.push_back(individual);
470 container.push_back(estim);
472 catch(exception& e) {
473 errorOut(e, "mothur", "splitAtDash");
478 /***********************************************************************/
479 //This function parses the label options and puts them in a set
480 inline void splitAtDash(string& estim, set<string>& container) {
484 while (estim.find_first_of('-') != -1) {
485 individual = estim.substr(0,estim.find_first_of('-'));
486 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
487 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
488 container.insert(individual);
492 container.insert(estim);
494 catch(exception& e) {
495 errorOut(e, "mothur", "splitAtDash");
499 /***********************************************************************/
500 //This function parses the line options and puts them in a set
501 inline void splitAtDash(string& estim, set<int>& container) {
506 while (estim.find_first_of('-') != -1) {
507 individual = estim.substr(0,estim.find_first_of('-'));
508 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
509 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
510 convert(individual, lineNum); //convert the string to int
511 container.insert(lineNum);
515 convert(estim, lineNum); //convert the string to int
516 container.insert(lineNum);
518 catch(exception& e) {
519 errorOut(e, "mothur", "splitAtDash");
523 /***********************************************************************/
524 //This function parses the a string and puts peices in a vector
525 inline void splitAtComma(string& estim, vector<string>& container) {
529 while (estim.find_first_of(',') != -1) {
530 individual = estim.substr(0,estim.find_first_of(','));
531 if ((estim.find_first_of(',')+1) <= estim.length()) { //checks to make sure you don't have comma at end of string
532 estim = estim.substr(estim.find_first_of(',')+1, estim.length());
533 container.push_back(individual);
537 container.push_back(estim);
539 catch(exception& e) {
540 errorOut(e, "mothur", "splitAtComma");
544 /***********************************************************************/
546 //This function splits up the various option parameters
547 inline void splitAtComma(string& prefix, string& suffix){
549 prefix = suffix.substr(0,suffix.find_first_of(','));
550 if ((suffix.find_first_of(',')+2) <= suffix.length()) { //checks to make sure you don't have comma at end of string
551 suffix = suffix.substr(suffix.find_first_of(',')+1, suffix.length());
553 while(suffix.at(0) == ' ')
554 suffix = suffix.substr(1, suffix.length());
558 catch(exception& e) {
559 errorOut(e, "mothur", "splitAtComma");
563 /***********************************************************************/
565 //This function separates the key value from the option value i.e. dist=96_...
566 inline void splitAtEquals(string& key, string& value){
568 if(value.find_first_of('=') != -1){
569 key = value.substr(0,value.find_first_of('='));
570 if ((value.find_first_of('=')+1) <= value.length()) {
571 value = value.substr(value.find_first_of('=')+1, value.length());
578 catch(exception& e) {
579 errorOut(e, "mothur", "splitAtEquals");
583 /**************************************************************************************************/
585 inline bool inUsersGroups(string groupname, vector<string> Groups) {
587 for (int i = 0; i < Groups.size(); i++) {
588 if (groupname == Groups[i]) { return true; }
592 catch(exception& e) {
593 errorOut(e, "mothur", "inUsersGroups");
598 /**************************************************************************************************/
600 inline void mothurOutJustToLog(string message) {
603 string logFileName = "mothur.logFile";
604 openOutputFileAppend(logFileName, out);
610 catch(exception& e) {
611 errorOut(e, "mothur", "mothurOutJustToLog");
617 /**************************************************************************************************/
619 inline void mothurOut(float num) {
622 string logFileName = "mothur.logFile";
623 openOutputFileAppend(logFileName, out);
630 catch(exception& e) {
631 cout << "Error in mothur class mothurOut float" << endl;
635 /***********************************************************************/
636 inline void mothurOut(double value) {
639 string logFileName = "mothur.logFile";
640 openOutputFileAppend(logFileName, out);
647 catch(exception& e) {
648 cout << "Error in mothur class mothurOut double" << endl;
653 /***********************************************************************/
654 //this function determines if the user has given us labels that are smaller than the given label.
655 //if so then it returns true so that the calling function can run the previous valid distance.
656 //it's a "smart" distance function. It also checks for invalid labels.
657 inline bool anyLabelsToProcess(string label, set<string>& userLabels, string errorOff) {
659 set<string>::iterator it;
660 vector<float> orderFloat;
661 map<string, float> userMap; //the conversion process removes trailing 0's which we need to put back
662 map<string, float>::iterator it2;
664 bool smaller = false;
666 //unique is the smallest line
667 if (label == "unique") { return false; }
668 else { convert(label, labelFloat); }
670 //go through users set and make them floats
671 for(it = userLabels.begin(); it != userLabels.end(); ++it) {
674 if ((*it != "unique") && (convertTestFloat(*it, temp) == true)){
676 orderFloat.push_back(temp);
678 }else if (*it == "unique") {
679 orderFloat.push_back(-1.0);
680 userMap["unique"] = -1.0;
682 if (errorOff == "") { mothurOut(*it + " is not a valid label."); mothurOutEndLine(); }
683 userLabels.erase(*it);
689 sort(orderFloat.begin(), orderFloat.end());
691 /*************************************************/
692 //is this label bigger than any of the users labels
693 /*************************************************/
695 //loop through order until you find a label greater than label
696 for (int i = 0; i < orderFloat.size(); i++) {
697 if (orderFloat[i] < labelFloat) {
699 if (orderFloat[i] == -1) {
700 if (errorOff == "") { mothurOut("Your file does not include the label unique."); mothurOutEndLine(); }
701 userLabels.erase("unique");
704 if (errorOff == "") { mothurOut("Your file does not include the label "); mothurOutEndLine(); }
706 for (it2 = userMap.begin(); it2!= userMap.end(); it2++) {
707 if (it2->second == orderFloat[i]) {
709 //remove small labels
714 if (errorOff == "") { mothurOut(s + ". I will use the next smallest distance. "); mothurOutEndLine(); }
716 //since they are sorted once you find a bigger one stop looking
723 catch(exception& e) {
724 errorOut(e, "mothur", "anyLabelsToProcess");
729 /**************************************************************************************************/