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 void gobble(istream& f){
177 while(isspace(d=f.get())) {;}
182 /***********************************************************************/
184 inline string getline(ifstream& fileHandle) {
189 while (!fileHandle.eof()) {
191 char c = fileHandle.get();
193 //are you at the end of the line
194 if ((c == '\n') || (c == '\r') || (c == '\f')){ break; }
201 catch(exception& e) {
202 cout << "Error in mothur function getline" << endl;
207 /**************************************************************************************************/
209 inline void mothurOut(string message) {
212 string logFileName = "mothur.logFile";
213 openOutputFileAppend(logFileName, out);
220 catch(exception& e) {
221 cout << "Error in mothur class mothurOut" << endl;
225 /**************************************************************************************************/
227 inline void mothurOut(string message, string precision) {
230 string logFileName = "mothur.logFile";
231 openOutputFileAppend(logFileName, out);
233 cout << precision << message;
234 out << precision << message;
238 catch(exception& e) {
239 cout << "Error in mothur class mothurOut" << endl;
244 /**************************************************************************************************/
246 inline void mothurOutEndLine() {
249 string logFileName = "mothur.logFile";
250 openOutputFileAppend(logFileName, out);
257 catch(exception& e) {
258 cout << "error in mothur mothurOutEndLine" << endl;
264 /**************************************************************************************************/
266 inline void errorOut(exception& e, string object, string function) {
268 mothurOut("Error: ");
269 mothurOut(toString(e.what()));
270 mothurOut(" has occurred in the " + object + " class function " + function + ". Please contact Pat Schloss at pschloss@umich.edu, and be sure to include the mothur.logFile with your inquiry.");
278 /***********************************************************************/
280 inline bool isTrue(string f){
282 if ((f == "TRUE") || (f == "T") || (f == "true") || (f == "t")) { return true; }
283 else { return false; }
286 /***********************************************************************/
288 inline float roundDist(float dist, int precision){
290 return int(dist * precision + 0.5)/float(precision);
294 /***********************************************************************/
296 inline int getNumNames(string names){
302 for(int i=0;i<names.size();i++){
313 /**************************************************************************************************/
315 inline vector<vector<double> > binomial(int maxOrder){
317 vector<vector<double> > binomial(maxOrder+1);
319 for(int i=0;i<=maxOrder;i++){
320 binomial[i].resize(maxOrder+1);
329 for(int i=2;i<=maxOrder;i++){
333 for(int i=2;i<=maxOrder;i++){
334 for(int j=1;j<=maxOrder;j++){
335 if(i==j){ binomial[i][j]=1; }
336 if(j>i) { binomial[i][j]=0; }
337 else { binomial[i][j]=binomial[i-1][j-1]+binomial[i-1][j]; }
344 /***********************************************************************/
346 inline string getRootName(string longName){
348 string rootName = longName;
350 if(longName.find_last_of(".") != longName.npos){
351 int pos = longName.find_last_of('.')+1;
352 rootName = longName.substr(0, pos);
357 /***********************************************************************/
359 inline string getSimpleName(string longName){
361 string simpleName = longName;
363 if(longName.find_last_of("/") != longName.npos){
364 int pos = longName.find_last_of('/')+1;
365 simpleName = longName.substr(pos, longName.length());
371 /***********************************************************************/
373 inline int factorial(int num){
376 for (int i = 1; i <= num; i++) {
382 /**************************************************************************************************
384 double min(double x, double y)
390 /***********************************************************************/
392 inline string getPathName(string longName){
394 string rootPathName = longName;
396 if(longName.find_last_of('/') != longName.npos){
397 int pos = longName.find_last_of('/')+1;
398 rootPathName = longName.substr(0, pos);
404 /***********************************************************************/
406 inline string getExtension(string longName){
408 string extension = longName;
410 if(longName.find_last_of('.') != longName.npos){
411 int pos = longName.find_last_of('.');
412 extension = longName.substr(pos, longName.length());
418 /***********************************************************************/
420 inline int openInputFile(string fileName, ifstream& fileHandle){
422 fileHandle.open(fileName.c_str());
424 mothurOut("Error: Could not open " + fileName); mothurOutEndLine();
433 /***********************************************************************/
435 inline int openOutputFile(string fileName, ofstream& fileHandle){
437 fileHandle.open(fileName.c_str(), ios::trunc);
439 mothurOut("Error: Could not open " + fileName); mothurOutEndLine();
448 /***********************************************************************/
450 inline int getNumSeqs(ifstream& file){
452 int numSeqs = count(istreambuf_iterator<char>(file),istreambuf_iterator<char>(), '>');
458 /***********************************************************************/
460 //This function parses the estimator options and puts them in a vector
461 inline void splitAtDash(string& estim, vector<string>& container) {
465 while (estim.find_first_of('-') != -1) {
466 individual = estim.substr(0,estim.find_first_of('-'));
467 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
468 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
469 container.push_back(individual);
473 container.push_back(estim);
475 catch(exception& e) {
476 errorOut(e, "mothur", "splitAtDash");
481 /***********************************************************************/
482 //This function parses the label options and puts them in a set
483 inline void splitAtDash(string& estim, set<string>& container) {
487 while (estim.find_first_of('-') != -1) {
488 individual = estim.substr(0,estim.find_first_of('-'));
489 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
490 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
491 container.insert(individual);
495 container.insert(estim);
497 catch(exception& e) {
498 errorOut(e, "mothur", "splitAtDash");
502 /***********************************************************************/
503 //This function parses the line options and puts them in a set
504 inline void splitAtDash(string& estim, set<int>& container) {
509 while (estim.find_first_of('-') != -1) {
510 individual = estim.substr(0,estim.find_first_of('-'));
511 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
512 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
513 convert(individual, lineNum); //convert the string to int
514 container.insert(lineNum);
518 convert(estim, lineNum); //convert the string to int
519 container.insert(lineNum);
521 catch(exception& e) {
522 errorOut(e, "mothur", "splitAtDash");
526 /***********************************************************************/
527 //This function parses the a string and puts peices in a vector
528 inline void splitAtComma(string& estim, vector<string>& container) {
532 while (estim.find_first_of(',') != -1) {
533 individual = estim.substr(0,estim.find_first_of(','));
534 if ((estim.find_first_of(',')+1) <= estim.length()) { //checks to make sure you don't have comma at end of string
535 estim = estim.substr(estim.find_first_of(',')+1, estim.length());
536 container.push_back(individual);
540 container.push_back(estim);
542 catch(exception& e) {
543 errorOut(e, "mothur", "splitAtComma");
547 /***********************************************************************/
549 //This function splits up the various option parameters
550 inline void splitAtComma(string& prefix, string& suffix){
552 prefix = suffix.substr(0,suffix.find_first_of(','));
553 if ((suffix.find_first_of(',')+2) <= suffix.length()) { //checks to make sure you don't have comma at end of string
554 suffix = suffix.substr(suffix.find_first_of(',')+1, suffix.length());
556 while(suffix.at(0) == ' ')
557 suffix = suffix.substr(1, suffix.length());
561 catch(exception& e) {
562 errorOut(e, "mothur", "splitAtComma");
566 /***********************************************************************/
568 //This function separates the key value from the option value i.e. dist=96_...
569 inline void splitAtEquals(string& key, string& value){
571 if(value.find_first_of('=') != -1){
572 key = value.substr(0,value.find_first_of('='));
573 if ((value.find_first_of('=')+1) <= value.length()) {
574 value = value.substr(value.find_first_of('=')+1, value.length());
581 catch(exception& e) {
582 errorOut(e, "mothur", "splitAtEquals");
586 /**************************************************************************************************/
588 inline bool inUsersGroups(string groupname, vector<string> Groups) {
590 for (int i = 0; i < Groups.size(); i++) {
591 if (groupname == Groups[i]) { return true; }
595 catch(exception& e) {
596 errorOut(e, "mothur", "inUsersGroups");
601 /**************************************************************************************************/
603 inline void mothurOutJustToLog(string message) {
606 string logFileName = "mothur.logFile";
607 openOutputFileAppend(logFileName, out);
613 catch(exception& e) {
614 errorOut(e, "mothur", "mothurOutJustToLog");
620 /**************************************************************************************************/
622 inline void mothurOut(float num) {
625 string logFileName = "mothur.logFile";
626 openOutputFileAppend(logFileName, out);
633 catch(exception& e) {
634 cout << "Error in mothur class mothurOut float" << endl;
638 /***********************************************************************/
639 inline void mothurOut(double value) {
642 string logFileName = "mothur.logFile";
643 openOutputFileAppend(logFileName, out);
650 catch(exception& e) {
651 cout << "Error in mothur class mothurOut double" << endl;
656 /***********************************************************************/
657 //this function determines if the user has given us labels that are smaller than the given label.
658 //if so then it returns true so that the calling function can run the previous valid distance.
659 //it's a "smart" distance function. It also checks for invalid labels.
660 inline bool anyLabelsToProcess(string label, set<string>& userLabels, string errorOff) {
662 set<string>::iterator it;
663 vector<float> orderFloat;
664 map<string, float> userMap; //the conversion process removes trailing 0's which we need to put back
665 map<string, float>::iterator it2;
667 bool smaller = false;
669 //unique is the smallest line
670 if (label == "unique") { return false; }
671 else { convert(label, labelFloat); }
673 //go through users set and make them floats
674 for(it = userLabels.begin(); it != userLabels.end(); ++it) {
677 if ((*it != "unique") && (convertTestFloat(*it, temp) == true)){
679 orderFloat.push_back(temp);
681 }else if (*it == "unique") {
682 orderFloat.push_back(-1.0);
683 userMap["unique"] = -1.0;
685 if (errorOff == "") { mothurOut(*it + " is not a valid label."); mothurOutEndLine(); }
686 userLabels.erase(*it);
692 sort(orderFloat.begin(), orderFloat.end());
694 /*************************************************/
695 //is this label bigger than any of the users labels
696 /*************************************************/
698 //loop through order until you find a label greater than label
699 for (int i = 0; i < orderFloat.size(); i++) {
700 if (orderFloat[i] < labelFloat) {
702 if (orderFloat[i] == -1) {
703 if (errorOff == "") { mothurOut("Your file does not include the label unique."); mothurOutEndLine(); }
704 userLabels.erase("unique");
707 if (errorOff == "") { mothurOut("Your file does not include the label "); mothurOutEndLine(); }
709 for (it2 = userMap.begin(); it2!= userMap.end(); it2++) {
710 if (it2->second == orderFloat[i]) {
712 //remove small labels
717 if (errorOff == "") { mothurOut(s + ". I will use the next smallest distance. "); mothurOutEndLine(); }
719 //since they are sorted once you find a bigger one stop looking
726 catch(exception& e) {
727 errorOut(e, "mothur", "anyLabelsToProcess");
732 /**************************************************************************************************/