11 * Created by Sarah Westcott on 2/19/09.
12 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
16 /* This file contains all the standard incudes we use in the project as well as some common utilities. */
46 #define exp(x) (exp((double) x))
47 #define sqrt(x) (sqrt((double) x))
48 #define log10(x) (log10((double) x))
49 #define log2(x) (log10(x)/log10(2))
53 typedef unsigned long long ull;
70 /***********************************************************************/
72 // snagged from http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.2
73 // works for now, but there should be a way to do it without killing the whole program
75 class BadConversion : public runtime_error {
77 BadConversion(const string& s) : runtime_error(s){ }
80 //**********************************************************************************************************************
83 inline void convert(const string& s, T& x, bool failIfLeftoverChars = true){
86 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
87 throw BadConversion(s);
89 //**********************************************************************************************************************
92 inline bool convertTestFloat(const string& s, T& x, bool failIfLeftoverChars = true){
95 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
102 //**********************************************************************************************************************
105 inline bool convertTest(const string& s, T& x, bool failIfLeftoverChars = true){
108 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
110 cout << "'" << s << "' is unable to be converted into an integer.\n";
116 //**********************************************************************************************************************
119 string toString(const T&x){
125 //**********************************************************************************************************************
128 string toHex(const T&x){
135 //**********************************************************************************************************************
138 string toString(const T&x, int i){
142 output << fixed << x;
148 /***********************************************************************/
150 inline void gobble(istream& f){
153 while(isspace(d=f.get())) {;}
157 /***********************************************************************/
159 inline bool isTrue(string f){
161 if ((f == "TRUE") || (f == "T") || (f == "true") || (f == "t")) { return true; }
162 else { return false; }
165 /***********************************************************************/
167 inline float roundDist(float dist, int precision){
169 return int(dist * precision + 0.5)/float(precision);
173 /***********************************************************************/
175 inline int getNumNames(string names){
181 for(int i=0;i<names.size();i++){
192 /**************************************************************************************************/
194 inline vector<vector<double> > binomial(int maxOrder){
196 vector<vector<double> > binomial(maxOrder+1);
198 for(int i=0;i<=maxOrder;i++){
199 binomial[i].resize(maxOrder+1);
208 for(int i=2;i<=maxOrder;i++){
212 for(int i=2;i<=maxOrder;i++){
213 for(int j=1;j<=maxOrder;j++){
214 if(i==j){ binomial[i][j]=1; }
215 if(j>i) { binomial[i][j]=0; }
216 else { binomial[i][j]=binomial[i-1][j-1]+binomial[i-1][j]; }
223 /***********************************************************************/
225 inline string getRootName(string longName){
227 string rootName = longName;
229 if(longName.find_last_of(".") != longName.npos){
230 int pos = longName.find_last_of('.')+1;
231 rootName = longName.substr(0, pos);
236 /***********************************************************************/
238 inline string getSimpleName(string longName){
240 string simpleName = longName;
242 if(longName.find_last_of("/") != longName.npos){
243 int pos = longName.find_last_of('/')+1;
244 simpleName = longName.substr(pos, longName.length());
250 /***********************************************************************/
252 inline int factorial(int num){
255 for (int i = 1; i <= num; i++) {
261 /**************************************************************************************************
263 double min(double x, double y)
269 /***********************************************************************/
271 inline string getPathName(string longName){
273 string rootPathName = longName;
275 if(longName.find_last_of('/') != longName.npos){
276 int pos = longName.find_last_of('/')+1;
277 rootPathName = longName.substr(0, pos);
283 /***********************************************************************/
285 inline string getExtension(string longName){
287 string extension = longName;
289 if(longName.find_last_of('.') != longName.npos){
290 int pos = longName.find_last_of('.');
291 extension = longName.substr(pos, longName.length());
297 /***********************************************************************/
299 inline int openInputFile(string fileName, ifstream& fileHandle){
301 fileHandle.open(fileName.c_str());
303 cerr << "Error: Could not open " << fileName << endl;
312 /***********************************************************************/
314 inline int openOutputFile(string fileName, ofstream& fileHandle){
316 fileHandle.open(fileName.c_str(), ios::trunc);
318 cerr << "Error: Could not open " << fileName << endl;
326 /***********************************************************************/
328 inline int openOutputFileAppend(string fileName, ofstream& fileHandle){
330 fileHandle.open(fileName.c_str(), ios::app);
332 cerr << "Error: Could not open " << fileName << endl;
342 /***********************************************************************/
344 inline int getNumSeqs(ifstream& file){
346 int numSeqs = count(istreambuf_iterator<char>(file),istreambuf_iterator<char>(), '>');
352 /***********************************************************************/
354 //This function parses the estimator options and puts them in a vector
355 inline void splitAtDash(string& estim, vector<string>& container) {
359 while (estim.find_first_of('-') != -1) {
360 individual = estim.substr(0,estim.find_first_of('-'));
361 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
362 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
363 container.push_back(individual);
367 container.push_back(estim);
369 catch(exception& e) {
370 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
374 cout << "An unknown error has occurred in the mothur class function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
380 /***********************************************************************/
381 //This function parses the label options and puts them in a set
382 inline void splitAtDash(string& estim, set<string>& container) {
386 while (estim.find_first_of('-') != -1) {
387 individual = estim.substr(0,estim.find_first_of('-'));
388 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
389 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
390 container.insert(individual);
394 container.insert(estim);
396 catch(exception& e) {
397 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
401 cout << "An unknown error has occurred in the mothur class function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
406 /***********************************************************************/
407 //This function parses the line options and puts them in a set
408 inline void splitAtDash(string& estim, set<int>& container) {
413 while (estim.find_first_of('-') != -1) {
414 individual = estim.substr(0,estim.find_first_of('-'));
415 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
416 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
417 convert(individual, lineNum); //convert the string to int
418 container.insert(lineNum);
422 convert(estim, lineNum); //convert the string to int
423 container.insert(lineNum);
425 catch(exception& e) {
426 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
430 cout << "An unknown error has occurred in the mothur class function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
435 /***********************************************************************/
436 //This function parses the a string and puts peices in a vector
437 inline void splitAtComma(string& estim, vector<string>& container) {
441 while (estim.find_first_of(',') != -1) {
442 individual = estim.substr(0,estim.find_first_of(','));
443 if ((estim.find_first_of(',')+1) <= estim.length()) { //checks to make sure you don't have comma at end of string
444 estim = estim.substr(estim.find_first_of(',')+1, estim.length());
445 container.push_back(individual);
449 container.push_back(estim);
451 catch(exception& e) {
452 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtComma. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
456 cout << "An unknown error has occurred in the mothur class function splitAtComma. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
460 /***********************************************************************/
462 //This function splits up the various option parameters
463 inline void splitAtComma(string& prefix, string& suffix){
465 prefix = suffix.substr(0,suffix.find_first_of(','));
466 if ((suffix.find_first_of(',')+2) <= suffix.length()) { //checks to make sure you don't have comma at end of string
467 suffix = suffix.substr(suffix.find_first_of(',')+1, suffix.length());
469 while(suffix.at(0) == ' ')
470 suffix = suffix.substr(1, suffix.length());
474 catch(exception& e) {
475 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtComma. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
479 cout << "An unknown error has occurred in the mothur class function splitAtComma. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
484 /***********************************************************************/
486 //This function separates the key value from the option value i.e. dist=96_...
487 inline void splitAtEquals(string& key, string& value){
489 if(value.find_first_of('=') != -1){
490 key = value.substr(0,value.find_first_of('='));
491 if ((value.find_first_of('=')+1) <= value.length()) {
492 value = value.substr(value.find_first_of('=')+1, value.length());
499 catch(exception& e) {
500 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtEquals. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
504 cout << "An unknown error has occurred in the mothur class function splitAtEquals. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
509 /**************************************************************************************************/
511 inline bool inUsersGroups(string groupname, vector<string> Groups) {
513 for (int i = 0; i < Groups.size(); i++) {
514 if (groupname == Groups[i]) { return true; }
518 catch(exception& e) {
519 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function inUsersGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
523 cout << "An unknown error has occurred in the mothur class function inUsersGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
528 /***********************************************************************/
529 //this function determines if the user has given us labels that are smaller than the given label.
530 //if so then it returns true so that the calling function can run the previous valid distance.
531 //it's a "smart" distance function. It also checks for invalid labels.
532 inline bool anyLabelsToProcess(string label, set<string>& userLabels, string errorOff) {
534 set<string>::iterator it;
535 vector<float> orderFloat;
536 map<string, float> userMap; //the conversion process removes trailing 0's which we need to put back
537 map<string, float>::iterator it2;
539 bool smaller = false;
541 //unique is the smallest line
542 if (label == "unique") { return false; }
543 else { convert(label, labelFloat); }
545 //go through users set and make them floats
546 for(it = userLabels.begin(); it != userLabels.end(); ++it) {
549 if ((*it != "unique") && (convertTestFloat(*it, temp) == true)){
551 orderFloat.push_back(temp);
553 }else if (*it == "unique") {
554 orderFloat.push_back(-1.0);
555 userMap["unique"] = -1.0;
557 if (errorOff == "") { cout << *it << " is not a valid label." << endl; }
558 userLabels.erase(*it);
564 sort(orderFloat.begin(), orderFloat.end());
566 /*************************************************/
567 //is this label bigger than any of the users labels
568 /*************************************************/
570 //loop through order until you find a label greater than label
571 for (int i = 0; i < orderFloat.size(); i++) {
572 if (orderFloat[i] < labelFloat) {
574 if (orderFloat[i] == -1) {
575 if (errorOff == "") { cout << "Your file does not include the label unique." << endl; }
576 userLabels.erase("unique");
579 if (errorOff == "") { cout << "Your file does not include the label "; }
581 for (it2 = userMap.begin(); it2!= userMap.end(); it2++) {
582 if (it2->second == orderFloat[i]) {
584 //remove small labels
589 if (errorOff == "") { cout << s << ". I will use the next smallest distance. " << endl; }
591 //since they are sorted once you find a bigger one stop looking
598 catch(exception& e) {
599 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function anyLabelsToProcess. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
603 cout << "An unknown error has occurred in the mothur class function anyLabelsToProcess. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
609 /**************************************************************************************************/