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;
64 /***********************************************************************/
66 // snagged from http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.2
67 // works for now, but there should be a way to do it without killing the whole program
69 class BadConversion : public runtime_error {
71 BadConversion(const string& s) : runtime_error(s){ }
74 //**********************************************************************************************************************
77 inline void convert(const string& s, T& x, bool failIfLeftoverChars = true){
80 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
81 throw BadConversion(s);
83 //**********************************************************************************************************************
86 inline bool convertTestFloat(const string& s, T& x, bool failIfLeftoverChars = true){
89 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
96 //**********************************************************************************************************************
99 inline bool convertTest(const string& s, T& x, bool failIfLeftoverChars = true){
102 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
104 cout << "'" << s << "' is unable to be converted into an integer.\n";
110 //**********************************************************************************************************************
113 string toString(const T&x){
119 //**********************************************************************************************************************
122 string toHex(const T&x){
129 //**********************************************************************************************************************
132 string toString(const T&x, int i){
136 output << fixed << x;
142 /***********************************************************************/
144 inline void gobble(istream& f){
147 while(isspace(d=f.get())) {;}
151 /***********************************************************************/
153 inline bool isTrue(string f){
155 if ((f == "TRUE") || (f == "T") || (f == "true") || (f == "t")) { return true; }
156 else { return false; }
159 /***********************************************************************/
161 inline float roundDist(float dist, int precision){
163 return int(dist * precision + 0.5)/float(precision);
167 /***********************************************************************/
169 inline int getNumNames(string names){
175 for(int i=0;i<names.size();i++){
186 /**************************************************************************************************/
188 inline vector<vector<double> > binomial(int maxOrder){
190 vector<vector<double> > binomial(maxOrder+1);
192 for(int i=0;i<=maxOrder;i++){
193 binomial[i].resize(maxOrder+1);
202 for(int i=2;i<=maxOrder;i++){
206 for(int i=2;i<=maxOrder;i++){
207 for(int j=1;j<=maxOrder;j++){
208 if(i==j){ binomial[i][j]=1; }
209 if(j>i) { binomial[i][j]=0; }
210 else { binomial[i][j]=binomial[i-1][j-1]+binomial[i-1][j]; }
217 /***********************************************************************/
219 inline string getRootName(string longName){
221 string rootName = longName;
223 if(longName.find_last_of(".") != longName.npos){
224 int pos = longName.find_last_of('.')+1;
225 rootName = longName.substr(0, pos);
230 /***********************************************************************/
232 inline string getSimpleName(string longName){
234 string simpleName = longName;
236 if(longName.find_last_of("/") != longName.npos){
237 int pos = longName.find_last_of('/')+1;
238 simpleName = longName.substr(pos, longName.length());
243 /***********************************************************************/
245 inline int factorial(int num){
248 for (int i = 1; i <= num; i++) {
254 /**************************************************************************************************
256 double min(double x, double y)
262 /***********************************************************************/
264 inline string getPathName(string longName){
266 string rootPathName = longName;
268 if(longName.find_last_of('/') != longName.npos){
269 int pos = longName.find_last_of('/')+1;
270 rootPathName = longName.substr(0, pos);
276 /***********************************************************************/
278 inline string getExtension(string longName){
280 string extension = longName;
282 if(longName.find_last_of('.') != longName.npos){
283 int pos = longName.find_last_of('.');
284 extension = longName.substr(pos, longName.length());
290 /***********************************************************************/
292 inline int openInputFile(string fileName, ifstream& fileHandle){
294 fileHandle.open(fileName.c_str());
296 cerr << "Error: Could not open " << fileName << endl;
305 /***********************************************************************/
307 inline int openOutputFile(string fileName, ofstream& fileHandle){
309 fileHandle.open(fileName.c_str(), ios::trunc);
311 cerr << "Error: Could not open " << fileName << endl;
319 /***********************************************************************/
321 inline int openOutputFileAppend(string fileName, ofstream& fileHandle){
323 fileHandle.open(fileName.c_str(), ios::app);
325 cerr << "Error: Could not open " << fileName << endl;
335 /***********************************************************************/
337 inline int getNumSeqs(ifstream& file){
339 int numSeqs = count(istreambuf_iterator<char>(file),istreambuf_iterator<char>(), '>');
345 /***********************************************************************/
347 //This function parses the estimator options and puts them in a vector
348 inline void splitAtDash(string& estim, vector<string>& container) {
352 while (estim.find_first_of('-') != -1) {
353 individual = estim.substr(0,estim.find_first_of('-'));
354 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
355 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
356 container.push_back(individual);
360 container.push_back(estim);
362 catch(exception& e) {
363 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
367 cout << "An unknown error has occurred in the mothur class function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
373 /***********************************************************************/
374 //This function parses the label options and puts them in a set
375 inline void splitAtDash(string& estim, set<string>& container) {
379 while (estim.find_first_of('-') != -1) {
380 individual = estim.substr(0,estim.find_first_of('-'));
381 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
382 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
383 container.insert(individual);
387 container.insert(estim);
389 catch(exception& e) {
390 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
394 cout << "An unknown error has occurred in the mothur class function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
399 /***********************************************************************/
400 //This function parses the line options and puts them in a set
401 inline void splitAtDash(string& estim, set<int>& container) {
406 while (estim.find_first_of('-') != -1) {
407 individual = estim.substr(0,estim.find_first_of('-'));
408 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
409 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
410 convert(individual, lineNum); //convert the string to int
411 container.insert(lineNum);
415 convert(estim, lineNum); //convert the string to int
416 container.insert(lineNum);
418 catch(exception& e) {
419 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
423 cout << "An unknown error has occurred in the mothur class function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
428 /***********************************************************************/
429 //This function parses the a string and puts peices in a vector
430 inline void splitAtComma(string& estim, vector<string>& container) {
434 while (estim.find_first_of(',') != -1) {
435 individual = estim.substr(0,estim.find_first_of(','));
436 if ((estim.find_first_of(',')+1) <= estim.length()) { //checks to make sure you don't have comma at end of string
437 estim = estim.substr(estim.find_first_of(',')+1, estim.length());
438 container.push_back(individual);
442 container.push_back(estim);
444 catch(exception& e) {
445 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtComma. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
449 cout << "An unknown error has occurred in the mothur class function splitAtComma. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
453 /***********************************************************************/
455 //This function splits up the various option parameters
456 inline void splitAtComma(string& prefix, string& suffix){
458 prefix = suffix.substr(0,suffix.find_first_of(','));
459 if ((suffix.find_first_of(',')+2) <= suffix.length()) { //checks to make sure you don't have comma at end of string
460 suffix = suffix.substr(suffix.find_first_of(',')+1, suffix.length());
462 while(suffix.at(0) == ' ')
463 suffix = suffix.substr(1, suffix.length());
467 catch(exception& e) {
468 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtComma. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
472 cout << "An unknown error has occurred in the mothur class function splitAtComma. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
477 /***********************************************************************/
479 //This function separates the key value from the option value i.e. dist=96_...
480 inline void splitAtEquals(string& key, string& value){
482 if(value.find_first_of('=') != -1){
483 key = value.substr(0,value.find_first_of('='));
484 if ((value.find_first_of('=')+1) <= value.length()) {
485 value = value.substr(value.find_first_of('=')+1, value.length());
492 catch(exception& e) {
493 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtEquals. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
497 cout << "An unknown error has occurred in the mothur class function splitAtEquals. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
502 /**************************************************************************************************/
504 inline bool inUsersGroups(string groupname, vector<string> Groups) {
506 for (int i = 0; i < Groups.size(); i++) {
507 if (groupname == Groups[i]) { return true; }
511 catch(exception& e) {
512 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function inUsersGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
516 cout << "An unknown error has occurred in the mothur class function inUsersGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
521 /***********************************************************************/
522 //this function determines if the user has given us labels that are smaller than the given label.
523 //if so then it returns true so that the calling function can run the previous valid distance.
524 //it's a "smart" distance function. It also checks for invalid labels.
525 inline bool anyLabelsToProcess(string label, set<string>& userLabels, string errorOff) {
527 set<string>::iterator it;
528 vector<float> orderFloat;
529 map<string, float> userMap; //the conversion process removes trailing 0's which we need to put back
530 map<string, float>::iterator it2;
532 bool smaller = false;
534 //unique is the smallest line
535 if (label == "unique") { return false; }
536 else { convert(label, labelFloat); }
538 //go through users set and make them floats
539 for(it = userLabels.begin(); it != userLabels.end(); ++it) {
542 if ((*it != "unique") && (convertTestFloat(*it, temp) == true)){
544 orderFloat.push_back(temp);
546 }else if (*it == "unique") {
547 orderFloat.push_back(-1.0);
548 userMap["unique"] = -1.0;
550 if (errorOff == "") { cout << *it << " is not a valid label." << endl; }
551 userLabels.erase(*it);
557 sort(orderFloat.begin(), orderFloat.end());
559 /*************************************************/
560 //is this label bigger than any of the users labels
561 /*************************************************/
563 //loop through order until you find a label greater than label
564 for (int i = 0; i < orderFloat.size(); i++) {
565 if (orderFloat[i] < labelFloat) {
567 if (orderFloat[i] == -1) {
568 if (errorOff == "") { cout << "Your file does not include the label unique." << endl; }
569 userLabels.erase("unique");
572 if (errorOff == "") { cout << "Your file does not include the label "; }
574 for (it2 = userMap.begin(); it2!= userMap.end(); it2++) {
575 if (it2->second == orderFloat[i]) {
577 //remove small labels
582 if (errorOff == "") { cout << s << ". I will use the next smallest distance. " << endl; }
584 //since they are sorted once you find a bigger one stop looking
591 catch(exception& e) {
592 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function anyLabelsToProcess. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
596 cout << "An unknown error has occurred in the mothur class function anyLabelsToProcess. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
602 /**************************************************************************************************/