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. */
52 #define exp(x) (exp((double) x))
53 #define sqrt(x) (sqrt((double) x))
54 #define log10(x) (log10((double) x))
55 #define log2(x) (log10(x)/log10(2))
56 #define isnan(x) ((x) != (x))
57 #define isinf(x) (fabs(x) == std::numeric_limits<double>::infinity())
60 typedef unsigned long ull;
77 /***********************************************************************/
79 // snagged from http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.2
80 // works for now, but there should be a way to do it without killing the whole program
82 class BadConversion : public runtime_error {
84 BadConversion(const string& s) : runtime_error(s){ }
87 //**********************************************************************************************************************
90 inline void convert(const string& s, T& x, bool failIfLeftoverChars = true){
93 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
94 throw BadConversion(s);
97 //**********************************************************************************************************************
100 inline bool convertTestFloat(const string& s, T& x, bool failIfLeftoverChars = true){
103 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
110 //**********************************************************************************************************************
113 inline bool convertTest(const string& s, T& x, bool failIfLeftoverChars = true){
116 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
118 cout << "unable to be converted into an integer.\n" << endl;
124 //**********************************************************************************************************************
127 string toString(const T&x){
133 //**********************************************************************************************************************
136 string toHex(const T&x){
143 //**********************************************************************************************************************
146 string toString(const T&x, int i){
150 output << fixed << x;
154 /***********************************************************************/
156 inline int openOutputFileAppend(string fileName, ofstream& fileHandle){
158 fileHandle.open(fileName.c_str(), ios::app);
160 cout << "Error: Could not open " << fileName << endl;
168 /***********************************************************************/
170 inline string getline(ifstream& fileHandle) {
175 while (!fileHandle.eof()) {
177 char c = fileHandle.get();
179 //are you at the end of the line
180 if (c == 10 || c == 13){ break; }
187 catch(exception& e) {
188 cout << "Error in mothur function getline" << endl;
193 /**************************************************************************************************/
195 inline void mothurOut(string message) {
198 string logFileName = "mothur.logFile";
199 openOutputFileAppend(logFileName, out);
206 catch(exception& e) {
207 cout << "Error in mothur class mothurOut" << endl;
211 /**************************************************************************************************/
213 inline void mothurOut(string message, string precision) {
216 string logFileName = "mothur.logFile";
217 openOutputFileAppend(logFileName, out);
219 cout << precision << message;
220 out << precision << message;
224 catch(exception& e) {
225 cout << "Error in mothur class mothurOut" << endl;
230 /**************************************************************************************************/
232 inline void mothurOutEndLine() {
235 string logFileName = "mothur.logFile";
236 openOutputFileAppend(logFileName, out);
243 catch(exception& e) {
244 cout << "error in mothur mothurOutEndLine" << endl;
250 /**************************************************************************************************/
252 inline void errorOut(exception& e, string object, string function) {
254 mothurOut("Error: ");
255 mothurOut(toString(e.what()));
256 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.");
264 /***********************************************************************/
266 inline void gobble(istream& f){
269 while(isspace(d=f.get())) {;}
273 /***********************************************************************/
275 inline bool isTrue(string f){
277 if ((f == "TRUE") || (f == "T") || (f == "true") || (f == "t")) { return true; }
278 else { return false; }
281 /***********************************************************************/
283 inline float roundDist(float dist, int precision){
285 return int(dist * precision + 0.5)/float(precision);
289 /***********************************************************************/
291 inline int getNumNames(string names){
297 for(int i=0;i<names.size();i++){
308 /**************************************************************************************************/
310 inline vector<vector<double> > binomial(int maxOrder){
312 vector<vector<double> > binomial(maxOrder+1);
314 for(int i=0;i<=maxOrder;i++){
315 binomial[i].resize(maxOrder+1);
324 for(int i=2;i<=maxOrder;i++){
328 for(int i=2;i<=maxOrder;i++){
329 for(int j=1;j<=maxOrder;j++){
330 if(i==j){ binomial[i][j]=1; }
331 if(j>i) { binomial[i][j]=0; }
332 else { binomial[i][j]=binomial[i-1][j-1]+binomial[i-1][j]; }
339 /***********************************************************************/
341 inline string getRootName(string longName){
343 string rootName = longName;
345 if(longName.find_last_of(".") != longName.npos){
346 int pos = longName.find_last_of('.')+1;
347 rootName = longName.substr(0, pos);
352 /***********************************************************************/
354 inline string getSimpleName(string longName){
356 string simpleName = longName;
358 if(longName.find_last_of("/") != longName.npos){
359 int pos = longName.find_last_of('/')+1;
360 simpleName = longName.substr(pos, longName.length());
366 /***********************************************************************/
368 inline int factorial(int num){
371 for (int i = 1; i <= num; i++) {
377 /**************************************************************************************************
379 double min(double x, double y)
385 /***********************************************************************/
387 inline string getPathName(string longName){
389 string rootPathName = longName;
391 if(longName.find_last_of('/') != longName.npos){
392 int pos = longName.find_last_of('/')+1;
393 rootPathName = longName.substr(0, pos);
399 /***********************************************************************/
401 inline string getExtension(string longName){
403 string extension = longName;
405 if(longName.find_last_of('.') != longName.npos){
406 int pos = longName.find_last_of('.');
407 extension = longName.substr(pos, longName.length());
413 /***********************************************************************/
415 inline int openInputFile(string fileName, ifstream& fileHandle){
417 fileHandle.open(fileName.c_str());
419 mothurOut("Error: Could not open " + fileName); mothurOutEndLine();
428 /***********************************************************************/
430 inline int openOutputFile(string fileName, ofstream& fileHandle){
432 fileHandle.open(fileName.c_str(), ios::trunc);
434 mothurOut("Error: Could not open " + fileName); mothurOutEndLine();
443 /***********************************************************************/
445 inline int getNumSeqs(ifstream& file){
447 int numSeqs = count(istreambuf_iterator<char>(file),istreambuf_iterator<char>(), '>');
453 /***********************************************************************/
455 //This function parses the estimator options and puts them in a vector
456 inline void splitAtDash(string& estim, vector<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.push_back(individual);
468 container.push_back(estim);
470 catch(exception& e) {
471 errorOut(e, "mothur", "splitAtDash");
476 /***********************************************************************/
477 //This function parses the label options and puts them in a set
478 inline void splitAtDash(string& estim, set<string>& 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 container.insert(individual);
490 container.insert(estim);
492 catch(exception& e) {
493 errorOut(e, "mothur", "splitAtDash");
497 /***********************************************************************/
498 //This function parses the line options and puts them in a set
499 inline void splitAtDash(string& estim, set<int>& container) {
504 while (estim.find_first_of('-') != -1) {
505 individual = estim.substr(0,estim.find_first_of('-'));
506 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
507 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
508 convert(individual, lineNum); //convert the string to int
509 container.insert(lineNum);
513 convert(estim, lineNum); //convert the string to int
514 container.insert(lineNum);
516 catch(exception& e) {
517 errorOut(e, "mothur", "splitAtDash");
521 /***********************************************************************/
522 //This function parses the a string and puts peices in a vector
523 inline void splitAtComma(string& estim, vector<string>& container) {
527 while (estim.find_first_of(',') != -1) {
528 individual = estim.substr(0,estim.find_first_of(','));
529 if ((estim.find_first_of(',')+1) <= estim.length()) { //checks to make sure you don't have comma at end of string
530 estim = estim.substr(estim.find_first_of(',')+1, estim.length());
531 container.push_back(individual);
535 container.push_back(estim);
537 catch(exception& e) {
538 errorOut(e, "mothur", "splitAtComma");
542 /***********************************************************************/
544 //This function splits up the various option parameters
545 inline void splitAtComma(string& prefix, string& suffix){
547 prefix = suffix.substr(0,suffix.find_first_of(','));
548 if ((suffix.find_first_of(',')+2) <= suffix.length()) { //checks to make sure you don't have comma at end of string
549 suffix = suffix.substr(suffix.find_first_of(',')+1, suffix.length());
551 while(suffix.at(0) == ' ')
552 suffix = suffix.substr(1, suffix.length());
556 catch(exception& e) {
557 errorOut(e, "mothur", "splitAtComma");
561 /***********************************************************************/
563 //This function separates the key value from the option value i.e. dist=96_...
564 inline void splitAtEquals(string& key, string& value){
566 if(value.find_first_of('=') != -1){
567 key = value.substr(0,value.find_first_of('='));
568 if ((value.find_first_of('=')+1) <= value.length()) {
569 value = value.substr(value.find_first_of('=')+1, value.length());
576 catch(exception& e) {
577 errorOut(e, "mothur", "splitAtEquals");
581 /**************************************************************************************************/
583 inline bool inUsersGroups(string groupname, vector<string> Groups) {
585 for (int i = 0; i < Groups.size(); i++) {
586 if (groupname == Groups[i]) { return true; }
590 catch(exception& e) {
591 errorOut(e, "mothur", "inUsersGroups");
596 /**************************************************************************************************/
598 inline void mothurOutJustToLog(string message) {
601 string logFileName = "mothur.logFile";
602 openOutputFileAppend(logFileName, out);
608 catch(exception& e) {
609 errorOut(e, "mothur", "mothurOutJustToLog");
615 /**************************************************************************************************/
617 inline void mothurOut(float num) {
620 string logFileName = "mothur.logFile";
621 openOutputFileAppend(logFileName, out);
628 catch(exception& e) {
629 cout << "Error in mothur class mothurOut float" << endl;
633 /***********************************************************************/
634 inline void mothurOut(double value) {
637 string logFileName = "mothur.logFile";
638 openOutputFileAppend(logFileName, out);
645 catch(exception& e) {
646 cout << "Error in mothur class mothurOut double" << endl;
651 /***********************************************************************/
652 //this function determines if the user has given us labels that are smaller than the given label.
653 //if so then it returns true so that the calling function can run the previous valid distance.
654 //it's a "smart" distance function. It also checks for invalid labels.
655 inline bool anyLabelsToProcess(string label, set<string>& userLabels, string errorOff) {
657 set<string>::iterator it;
658 vector<float> orderFloat;
659 map<string, float> userMap; //the conversion process removes trailing 0's which we need to put back
660 map<string, float>::iterator it2;
662 bool smaller = false;
664 //unique is the smallest line
665 if (label == "unique") { return false; }
666 else { convert(label, labelFloat); }
668 //go through users set and make them floats
669 for(it = userLabels.begin(); it != userLabels.end(); ++it) {
672 if ((*it != "unique") && (convertTestFloat(*it, temp) == true)){
674 orderFloat.push_back(temp);
676 }else if (*it == "unique") {
677 orderFloat.push_back(-1.0);
678 userMap["unique"] = -1.0;
680 if (errorOff == "") { mothurOut(*it + " is not a valid label."); mothurOutEndLine(); }
681 userLabels.erase(*it);
687 sort(orderFloat.begin(), orderFloat.end());
689 /*************************************************/
690 //is this label bigger than any of the users labels
691 /*************************************************/
693 //loop through order until you find a label greater than label
694 for (int i = 0; i < orderFloat.size(); i++) {
695 if (orderFloat[i] < labelFloat) {
697 if (orderFloat[i] == -1) {
698 if (errorOff == "") { mothurOut("Your file does not include the label unique."); mothurOutEndLine(); }
699 userLabels.erase("unique");
702 if (errorOff == "") { mothurOut("Your file does not include the label "); mothurOutEndLine(); }
704 for (it2 = userMap.begin(); it2!= userMap.end(); it2++) {
705 if (it2->second == orderFloat[i]) {
707 //remove small labels
712 if (errorOff == "") { mothurOut(s + ". I will use the next smallest distance. "); mothurOutEndLine(); }
714 //since they are sorted once you find a bigger one stop looking
721 catch(exception& e) {
722 errorOut(e, "mothur", "anyLabelsToProcess");
727 /**************************************************************************************************/