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 << "'" << s << "' is unable to be converted into an integer.\n";
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;
158 /***********************************************************************/
160 inline void gobble(istream& f){
163 while(isspace(d=f.get())) {;}
167 /***********************************************************************/
169 inline bool isTrue(string f){
171 if ((f == "TRUE") || (f == "T") || (f == "true") || (f == "t")) { return true; }
172 else { return false; }
175 /***********************************************************************/
177 inline float roundDist(float dist, int precision){
179 return int(dist * precision + 0.5)/float(precision);
183 /***********************************************************************/
185 inline int getNumNames(string names){
191 for(int i=0;i<names.size();i++){
202 /**************************************************************************************************/
204 inline vector<vector<double> > binomial(int maxOrder){
206 vector<vector<double> > binomial(maxOrder+1);
208 for(int i=0;i<=maxOrder;i++){
209 binomial[i].resize(maxOrder+1);
218 for(int i=2;i<=maxOrder;i++){
222 for(int i=2;i<=maxOrder;i++){
223 for(int j=1;j<=maxOrder;j++){
224 if(i==j){ binomial[i][j]=1; }
225 if(j>i) { binomial[i][j]=0; }
226 else { binomial[i][j]=binomial[i-1][j-1]+binomial[i-1][j]; }
233 /***********************************************************************/
235 inline string getRootName(string longName){
237 string rootName = longName;
239 if(longName.find_last_of(".") != longName.npos){
240 int pos = longName.find_last_of('.')+1;
241 rootName = longName.substr(0, pos);
246 /***********************************************************************/
248 inline string getSimpleName(string longName){
250 string simpleName = longName;
252 if(longName.find_last_of("/") != longName.npos){
253 int pos = longName.find_last_of('/')+1;
254 simpleName = longName.substr(pos, longName.length());
260 /***********************************************************************/
262 inline int factorial(int num){
265 for (int i = 1; i <= num; i++) {
271 /**************************************************************************************************
273 double min(double x, double y)
279 /***********************************************************************/
281 inline string getPathName(string longName){
283 string rootPathName = longName;
285 if(longName.find_last_of('/') != longName.npos){
286 int pos = longName.find_last_of('/')+1;
287 rootPathName = longName.substr(0, pos);
293 /***********************************************************************/
295 inline string getExtension(string longName){
297 string extension = longName;
299 if(longName.find_last_of('.') != longName.npos){
300 int pos = longName.find_last_of('.');
301 extension = longName.substr(pos, longName.length());
307 /***********************************************************************/
309 inline int openInputFile(string fileName, ifstream& fileHandle){
311 fileHandle.open(fileName.c_str());
313 cerr << "Error: Could not open " << fileName << endl;
322 /***********************************************************************/
324 inline int openOutputFile(string fileName, ofstream& fileHandle){
326 fileHandle.open(fileName.c_str(), ios::trunc);
328 cerr << "Error: Could not open " << fileName << endl;
336 /***********************************************************************/
338 inline int openOutputFileAppend(string fileName, ofstream& fileHandle){
340 fileHandle.open(fileName.c_str(), ios::app);
342 cerr << "Error: Could not open " << fileName << endl;
352 /***********************************************************************/
354 inline int getNumSeqs(ifstream& file){
356 int numSeqs = count(istreambuf_iterator<char>(file),istreambuf_iterator<char>(), '>');
362 /***********************************************************************/
364 //This function parses the estimator options and puts them in a vector
365 inline void splitAtDash(string& estim, vector<string>& container) {
369 while (estim.find_first_of('-') != -1) {
370 individual = estim.substr(0,estim.find_first_of('-'));
371 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
372 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
373 container.push_back(individual);
377 container.push_back(estim);
379 catch(exception& e) {
380 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
384 cout << "An unknown error has occurred in the mothur class function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
390 /***********************************************************************/
391 //This function parses the label options and puts them in a set
392 inline void splitAtDash(string& estim, set<string>& container) {
396 while (estim.find_first_of('-') != -1) {
397 individual = estim.substr(0,estim.find_first_of('-'));
398 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
399 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
400 container.insert(individual);
404 container.insert(estim);
406 catch(exception& e) {
407 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
411 cout << "An unknown error has occurred in the mothur class function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
416 /***********************************************************************/
417 //This function parses the line options and puts them in a set
418 inline void splitAtDash(string& estim, set<int>& container) {
423 while (estim.find_first_of('-') != -1) {
424 individual = estim.substr(0,estim.find_first_of('-'));
425 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
426 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
427 convert(individual, lineNum); //convert the string to int
428 container.insert(lineNum);
432 convert(estim, lineNum); //convert the string to int
433 container.insert(lineNum);
435 catch(exception& e) {
436 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
440 cout << "An unknown error has occurred in the mothur class function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
445 /***********************************************************************/
446 //This function parses the a string and puts peices in a vector
447 inline void splitAtComma(string& estim, vector<string>& container) {
451 while (estim.find_first_of(',') != -1) {
452 individual = estim.substr(0,estim.find_first_of(','));
453 if ((estim.find_first_of(',')+1) <= estim.length()) { //checks to make sure you don't have comma at end of string
454 estim = estim.substr(estim.find_first_of(',')+1, estim.length());
455 container.push_back(individual);
459 container.push_back(estim);
461 catch(exception& e) {
462 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtComma. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
466 cout << "An unknown error has occurred in the mothur class function splitAtComma. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
470 /***********************************************************************/
472 //This function splits up the various option parameters
473 inline void splitAtComma(string& prefix, string& suffix){
475 prefix = suffix.substr(0,suffix.find_first_of(','));
476 if ((suffix.find_first_of(',')+2) <= suffix.length()) { //checks to make sure you don't have comma at end of string
477 suffix = suffix.substr(suffix.find_first_of(',')+1, suffix.length());
479 while(suffix.at(0) == ' ')
480 suffix = suffix.substr(1, suffix.length());
484 catch(exception& e) {
485 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtComma. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
489 cout << "An unknown error has occurred in the mothur class function splitAtComma. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
494 /***********************************************************************/
496 //This function separates the key value from the option value i.e. dist=96_...
497 inline void splitAtEquals(string& key, string& value){
499 if(value.find_first_of('=') != -1){
500 key = value.substr(0,value.find_first_of('='));
501 if ((value.find_first_of('=')+1) <= value.length()) {
502 value = value.substr(value.find_first_of('=')+1, value.length());
509 catch(exception& e) {
510 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtEquals. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
514 cout << "An unknown error has occurred in the mothur class function splitAtEquals. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
519 /**************************************************************************************************/
521 inline bool inUsersGroups(string groupname, vector<string> Groups) {
523 for (int i = 0; i < Groups.size(); i++) {
524 if (groupname == Groups[i]) { return true; }
528 catch(exception& e) {
529 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function inUsersGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
533 cout << "An unknown error has occurred in the mothur class function inUsersGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
538 /***********************************************************************/
539 //this function determines if the user has given us labels that are smaller than the given label.
540 //if so then it returns true so that the calling function can run the previous valid distance.
541 //it's a "smart" distance function. It also checks for invalid labels.
542 inline bool anyLabelsToProcess(string label, set<string>& userLabels, string errorOff) {
544 set<string>::iterator it;
545 vector<float> orderFloat;
546 map<string, float> userMap; //the conversion process removes trailing 0's which we need to put back
547 map<string, float>::iterator it2;
549 bool smaller = false;
551 //unique is the smallest line
552 if (label == "unique") { return false; }
553 else { convert(label, labelFloat); }
555 //go through users set and make them floats
556 for(it = userLabels.begin(); it != userLabels.end(); ++it) {
559 if ((*it != "unique") && (convertTestFloat(*it, temp) == true)){
561 orderFloat.push_back(temp);
563 }else if (*it == "unique") {
564 orderFloat.push_back(-1.0);
565 userMap["unique"] = -1.0;
567 if (errorOff == "") { cout << *it << " is not a valid label." << endl; }
568 userLabels.erase(*it);
574 sort(orderFloat.begin(), orderFloat.end());
576 /*************************************************/
577 //is this label bigger than any of the users labels
578 /*************************************************/
580 //loop through order until you find a label greater than label
581 for (int i = 0; i < orderFloat.size(); i++) {
582 if (orderFloat[i] < labelFloat) {
584 if (orderFloat[i] == -1) {
585 if (errorOff == "") { cout << "Your file does not include the label unique." << endl; }
586 userLabels.erase("unique");
589 if (errorOff == "") { cout << "Your file does not include the label "; }
591 for (it2 = userMap.begin(); it2!= userMap.end(); it2++) {
592 if (it2->second == orderFloat[i]) {
594 //remove small labels
599 if (errorOff == "") { cout << s << ". I will use the next smallest distance. " << endl; }
601 //since they are sorted once you find a bigger one stop looking
608 catch(exception& e) {
609 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function anyLabelsToProcess. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
613 cout << "An unknown error has occurred in the mothur class function anyLabelsToProcess. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
619 /**************************************************************************************************/