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 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;
171 /**************************************************************************************************/
173 inline void mothurOut(string message) {
176 string logFileName = "mothur.logFile";
177 openOutputFileAppend(logFileName, out);
184 catch(exception& e) {
185 cout << "Error in mothur class mothurOut" << endl;
189 /**************************************************************************************************/
191 inline void mothurOut(string message, string precision) {
194 string logFileName = "mothur.logFile";
195 openOutputFileAppend(logFileName, out);
197 cout << precision << message;
198 out << precision << message;
202 catch(exception& e) {
203 cout << "Error in mothur class mothurOut" << endl;
208 /**************************************************************************************************/
210 inline void mothurOutEndLine() {
213 string logFileName = "mothur.logFile";
214 openOutputFileAppend(logFileName, out);
221 catch(exception& e) {
222 cout << "error in mothur mothurOutEndLine" << endl;
228 /**************************************************************************************************/
230 inline void errorOut(exception& e, string object, string function) {
232 mothurOut("Error: ");
233 mothurOut(toString(e.what()));
234 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.");
242 /***********************************************************************/
244 inline void gobble(istream& f){
247 while(isspace(d=f.get())) {;}
251 /***********************************************************************/
253 inline bool isTrue(string f){
255 if ((f == "TRUE") || (f == "T") || (f == "true") || (f == "t")) { return true; }
256 else { return false; }
259 /***********************************************************************/
261 inline float roundDist(float dist, int precision){
263 return int(dist * precision + 0.5)/float(precision);
267 /***********************************************************************/
269 inline int getNumNames(string names){
275 for(int i=0;i<names.size();i++){
286 /**************************************************************************************************/
288 inline vector<vector<double> > binomial(int maxOrder){
290 vector<vector<double> > binomial(maxOrder+1);
292 for(int i=0;i<=maxOrder;i++){
293 binomial[i].resize(maxOrder+1);
302 for(int i=2;i<=maxOrder;i++){
306 for(int i=2;i<=maxOrder;i++){
307 for(int j=1;j<=maxOrder;j++){
308 if(i==j){ binomial[i][j]=1; }
309 if(j>i) { binomial[i][j]=0; }
310 else { binomial[i][j]=binomial[i-1][j-1]+binomial[i-1][j]; }
317 /***********************************************************************/
319 inline string getRootName(string longName){
321 string rootName = longName;
323 if(longName.find_last_of(".") != longName.npos){
324 int pos = longName.find_last_of('.')+1;
325 rootName = longName.substr(0, pos);
330 /***********************************************************************/
332 inline string getSimpleName(string longName){
334 string simpleName = longName;
336 if(longName.find_last_of("/") != longName.npos){
337 int pos = longName.find_last_of('/')+1;
338 simpleName = longName.substr(pos, longName.length());
344 /***********************************************************************/
346 inline int factorial(int num){
349 for (int i = 1; i <= num; i++) {
355 /**************************************************************************************************
357 double min(double x, double y)
363 /***********************************************************************/
365 inline string getPathName(string longName){
367 string rootPathName = longName;
369 if(longName.find_last_of('/') != longName.npos){
370 int pos = longName.find_last_of('/')+1;
371 rootPathName = longName.substr(0, pos);
377 /***********************************************************************/
379 inline string getExtension(string longName){
381 string extension = longName;
383 if(longName.find_last_of('.') != longName.npos){
384 int pos = longName.find_last_of('.');
385 extension = longName.substr(pos, longName.length());
391 /***********************************************************************/
393 inline int openInputFile(string fileName, ifstream& fileHandle){
395 fileHandle.open(fileName.c_str());
397 mothurOut("Error: Could not open " + fileName); mothurOutEndLine();
406 /***********************************************************************/
408 inline int openOutputFile(string fileName, ofstream& fileHandle){
410 fileHandle.open(fileName.c_str(), ios::trunc);
412 mothurOut("Error: Could not open " + fileName); mothurOutEndLine();
421 /***********************************************************************/
423 inline int getNumSeqs(ifstream& file){
425 int numSeqs = count(istreambuf_iterator<char>(file),istreambuf_iterator<char>(), '>');
431 /***********************************************************************/
433 //This function parses the estimator options and puts them in a vector
434 inline void splitAtDash(string& estim, vector<string>& container) {
438 while (estim.find_first_of('-') != -1) {
439 individual = estim.substr(0,estim.find_first_of('-'));
440 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
441 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
442 container.push_back(individual);
446 container.push_back(estim);
448 catch(exception& e) {
449 errorOut(e, "mothur", "splitAtDash");
454 /***********************************************************************/
455 //This function parses the label options and puts them in a set
456 inline void splitAtDash(string& estim, set<string>& container) {
460 while (estim.find_first_of('-') != -1) {
461 individual = estim.substr(0,estim.find_first_of('-'));
462 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
463 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
464 container.insert(individual);
468 container.insert(estim);
470 catch(exception& e) {
471 errorOut(e, "mothur", "splitAtDash");
475 /***********************************************************************/
476 //This function parses the line options and puts them in a set
477 inline void splitAtDash(string& estim, set<int>& container) {
482 while (estim.find_first_of('-') != -1) {
483 individual = estim.substr(0,estim.find_first_of('-'));
484 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
485 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
486 convert(individual, lineNum); //convert the string to int
487 container.insert(lineNum);
491 convert(estim, lineNum); //convert the string to int
492 container.insert(lineNum);
494 catch(exception& e) {
495 errorOut(e, "mothur", "splitAtDash");
499 /***********************************************************************/
500 //This function parses the a string and puts peices in a vector
501 inline void splitAtComma(string& estim, vector<string>& container) {
505 while (estim.find_first_of(',') != -1) {
506 individual = estim.substr(0,estim.find_first_of(','));
507 if ((estim.find_first_of(',')+1) <= estim.length()) { //checks to make sure you don't have comma at end of string
508 estim = estim.substr(estim.find_first_of(',')+1, estim.length());
509 container.push_back(individual);
513 container.push_back(estim);
515 catch(exception& e) {
516 errorOut(e, "mothur", "splitAtComma");
520 /***********************************************************************/
522 //This function splits up the various option parameters
523 inline void splitAtComma(string& prefix, string& suffix){
525 prefix = suffix.substr(0,suffix.find_first_of(','));
526 if ((suffix.find_first_of(',')+2) <= suffix.length()) { //checks to make sure you don't have comma at end of string
527 suffix = suffix.substr(suffix.find_first_of(',')+1, suffix.length());
529 while(suffix.at(0) == ' ')
530 suffix = suffix.substr(1, suffix.length());
534 catch(exception& e) {
535 errorOut(e, "mothur", "splitAtComma");
539 /***********************************************************************/
541 //This function separates the key value from the option value i.e. dist=96_...
542 inline void splitAtEquals(string& key, string& value){
544 if(value.find_first_of('=') != -1){
545 key = value.substr(0,value.find_first_of('='));
546 if ((value.find_first_of('=')+1) <= value.length()) {
547 value = value.substr(value.find_first_of('=')+1, value.length());
554 catch(exception& e) {
555 errorOut(e, "mothur", "splitAtEquals");
559 /**************************************************************************************************/
561 inline bool inUsersGroups(string groupname, vector<string> Groups) {
563 for (int i = 0; i < Groups.size(); i++) {
564 if (groupname == Groups[i]) { return true; }
568 catch(exception& e) {
569 errorOut(e, "mothur", "inUsersGroups");
574 /**************************************************************************************************/
576 inline void mothurOutJustToLog(string message) {
579 string logFileName = "mothur.logFile";
580 openOutputFileAppend(logFileName, out);
586 catch(exception& e) {
587 errorOut(e, "mothur", "mothurOutJustToLog");
593 /**************************************************************************************************/
595 inline void mothurOut(float num) {
598 string logFileName = "mothur.logFile";
599 openOutputFileAppend(logFileName, out);
606 catch(exception& e) {
607 cout << "Error in mothur class mothurOut float" << endl;
611 /***********************************************************************/
612 inline void mothurOut(double value) {
615 string logFileName = "mothur.logFile";
616 openOutputFileAppend(logFileName, out);
623 catch(exception& e) {
624 cout << "Error in mothur class mothurOut double" << endl;
629 /***********************************************************************/
630 //this function determines if the user has given us labels that are smaller than the given label.
631 //if so then it returns true so that the calling function can run the previous valid distance.
632 //it's a "smart" distance function. It also checks for invalid labels.
633 inline bool anyLabelsToProcess(string label, set<string>& userLabels, string errorOff) {
635 set<string>::iterator it;
636 vector<float> orderFloat;
637 map<string, float> userMap; //the conversion process removes trailing 0's which we need to put back
638 map<string, float>::iterator it2;
640 bool smaller = false;
642 //unique is the smallest line
643 if (label == "unique") { return false; }
644 else { convert(label, labelFloat); }
646 //go through users set and make them floats
647 for(it = userLabels.begin(); it != userLabels.end(); ++it) {
650 if ((*it != "unique") && (convertTestFloat(*it, temp) == true)){
652 orderFloat.push_back(temp);
654 }else if (*it == "unique") {
655 orderFloat.push_back(-1.0);
656 userMap["unique"] = -1.0;
658 if (errorOff == "") { mothurOut(*it + " is not a valid label."); mothurOutEndLine(); }
659 userLabels.erase(*it);
665 sort(orderFloat.begin(), orderFloat.end());
667 /*************************************************/
668 //is this label bigger than any of the users labels
669 /*************************************************/
671 //loop through order until you find a label greater than label
672 for (int i = 0; i < orderFloat.size(); i++) {
673 if (orderFloat[i] < labelFloat) {
675 if (orderFloat[i] == -1) {
676 if (errorOff == "") { mothurOut("Your file does not include the label unique."); mothurOutEndLine(); }
677 userLabels.erase("unique");
680 if (errorOff == "") { mothurOut("Your file does not include the label "); mothurOutEndLine(); }
682 for (it2 = userMap.begin(); it2!= userMap.end(); it2++) {
683 if (it2->second == orderFloat[i]) {
685 //remove small labels
690 if (errorOff == "") { mothurOut(s + ". I will use the next smallest distance. "); mothurOutEndLine(); }
692 //since they are sorted once you find a bigger one stop looking
699 catch(exception& e) {
700 errorOut(e, "mothur", "anyLabelsToProcess");
705 /**************************************************************************************************/