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. */
44 typedef unsigned long long ull;
55 /***********************************************************************/
57 // snagged from http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.2
58 // works for now, but there should be a way to do it without killing the whole program
60 class BadConversion : public runtime_error {
62 BadConversion(const string& s) : runtime_error(s){ }
65 //**********************************************************************************************************************
68 inline void convert(const string& s, T& x, bool failIfLeftoverChars = true){
71 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
72 throw BadConversion(s);
74 //**********************************************************************************************************************
77 inline bool convertTestFloat(const string& s, T& x, bool failIfLeftoverChars = true){
80 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
87 //**********************************************************************************************************************
90 inline bool convertTest(const string& s, T& x, bool failIfLeftoverChars = true){
93 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
95 cout << "'" << s << "' is unable to be converted into an integer.\n";
101 //**********************************************************************************************************************
104 string toString(const T&x){
110 //**********************************************************************************************************************
113 string toHex(const T&x){
120 //**********************************************************************************************************************
123 string toString(const T&x, int i){
127 output << fixed << x;
133 /***********************************************************************/
135 inline void gobble(istream& f){
138 while(isspace(d=f.get())) {;}
143 /***********************************************************************/
145 inline float roundDist(float dist, int precision){
147 return int(dist * precision + 0.5)/float(precision);
151 /***********************************************************************/
153 inline int getNumNames(string names){
159 for(int i=0;i<names.size();i++){
170 /**************************************************************************************************/
172 inline vector<vector<double> > binomial(int maxOrder){
174 vector<vector<double> > binomial(maxOrder+1);
176 for(int i=0;i<=maxOrder;i++){
177 binomial[i].resize(maxOrder+1);
186 for(int i=2;i<=maxOrder;i++){
190 for(int i=2;i<=maxOrder;i++){
191 for(int j=1;j<=maxOrder;j++){
192 if(i==j){ binomial[i][j]=1; }
193 if(j>i) { binomial[i][j]=0; }
194 else { binomial[i][j]=binomial[i-1][j-1]+binomial[i-1][j]; }
201 /***********************************************************************/
203 inline string getRootName(string longName){
205 string rootName = longName;
207 if(longName.find_last_of(".") != longName.npos){
208 int pos = longName.find_last_of('.')+1;
209 rootName = longName.substr(0, pos);
214 /***********************************************************************/
216 inline string getSimpleName(string longName){
218 string simpleName = longName;
220 if(longName.find_last_of("/") != longName.npos){
221 int pos = longName.find_last_of('/')+1;
222 simpleName = longName.substr(pos, longName.length());
227 /***********************************************************************/
229 inline int factorial(int num){
232 for (int i = 1; i <= num; i++) {
238 /**************************************************************************************************
240 double min(double x, double y)
246 /***********************************************************************/
248 inline string getPathName(string longName){
250 string rootPathName = longName;
252 if(longName.find_last_of('/') != longName.npos){
253 int pos = longName.find_last_of('/')+1;
254 rootPathName = longName.substr(0, pos);
260 /***********************************************************************/
262 inline string getExtension(string longName){
264 string extension = longName;
266 if(longName.find_last_of('.') != longName.npos){
267 int pos = longName.find_last_of('.');
268 extension = longName.substr(pos, longName.length());
274 /***********************************************************************/
276 inline int openInputFile(string fileName, ifstream& fileHandle){
278 fileHandle.open(fileName.c_str());
280 cerr << "Error: Could not open " << fileName << endl;
289 /***********************************************************************/
291 inline int openOutputFile(string fileName, ofstream& fileHandle){
293 fileHandle.open(fileName.c_str(), ios::trunc);
295 cerr << "Error: Could not open " << fileName << endl;
303 /***********************************************************************/
305 inline int openOutputFileAppend(string fileName, ofstream& fileHandle){
307 fileHandle.open(fileName.c_str(), ios::app);
309 cerr << "Error: Could not open " << fileName << endl;
319 /***********************************************************************/
321 inline int getNumSeqs(ifstream& file){
323 int numSeqs = count(istreambuf_iterator<char>(file),istreambuf_iterator<char>(), '>');
329 /***********************************************************************/
331 //This function parses the estimator options and puts them in a vector
332 inline void splitAtDash(string& estim, vector<string>& container) {
336 while (estim.find_first_of('-') != -1) {
337 individual = estim.substr(0,estim.find_first_of('-'));
338 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
339 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
340 container.push_back(individual);
344 container.push_back(estim);
346 catch(exception& e) {
347 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
351 cout << "An unknown error has occurred in the mothur class function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
357 /***********************************************************************/
358 //This function parses the label options and puts them in a set
359 inline void splitAtDash(string& estim, set<string>& container) {
363 while (estim.find_first_of('-') != -1) {
364 individual = estim.substr(0,estim.find_first_of('-'));
365 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
366 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
367 container.insert(individual);
371 container.insert(estim);
373 catch(exception& e) {
374 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
378 cout << "An unknown error has occurred in the mothur class function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
383 /***********************************************************************/
384 //This function parses the line options and puts them in a set
385 inline void splitAtDash(string& estim, set<int>& container) {
390 while (estim.find_first_of('-') != -1) {
391 individual = estim.substr(0,estim.find_first_of('-'));
392 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
393 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
394 convert(individual, lineNum); //convert the string to int
395 container.insert(lineNum);
399 convert(estim, lineNum); //convert the string to int
400 container.insert(lineNum);
402 catch(exception& e) {
403 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
407 cout << "An unknown error has occurred in the mothur class function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
412 /***********************************************************************/
413 //This function parses the a string and puts peices in a vector
414 inline void splitAtComma(string& estim, vector<string>& container) {
418 while (estim.find_first_of(',') != -1) {
419 individual = estim.substr(0,estim.find_first_of(','));
420 if ((estim.find_first_of(',')+1) <= estim.length()) { //checks to make sure you don't have comma at end of string
421 estim = estim.substr(estim.find_first_of(',')+1, estim.length());
422 container.push_back(individual);
426 container.push_back(estim);
428 catch(exception& e) {
429 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtComma. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
433 cout << "An unknown error has occurred in the mothur class function splitAtComma. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
437 /***********************************************************************/
439 //This function splits up the various option parameters
440 inline void splitAtComma(string& prefix, string& suffix){
442 prefix = suffix.substr(0,suffix.find_first_of(','));
443 if ((suffix.find_first_of(',')+2) <= suffix.length()) { //checks to make sure you don't have comma at end of string
444 suffix = suffix.substr(suffix.find_first_of(',')+1, suffix.length());
446 while(suffix.at(0) == ' ')
447 suffix = suffix.substr(1, suffix.length());
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";
461 /***********************************************************************/
463 //This function separates the key value from the option value i.e. dist=96_...
464 inline void splitAtEquals(string& key, string& value){
466 if(value.find_first_of('=') != -1){
467 key = value.substr(0,value.find_first_of('='));
468 if ((value.find_first_of('=')+1) <= value.length()) {
469 value = value.substr(value.find_first_of('=')+1, value.length());
476 catch(exception& e) {
477 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtEquals. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
481 cout << "An unknown error has occurred in the mothur class function splitAtEquals. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
486 /**************************************************************************************************/
488 inline bool inUsersGroups(string groupname, vector<string> Groups) {
490 for (int i = 0; i < Groups.size(); i++) {
491 if (groupname == Groups[i]) { return true; }
495 catch(exception& e) {
496 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function inUsersGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
500 cout << "An unknown error has occurred in the mothur class function inUsersGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
505 /***********************************************************************/
506 //this function determines if the user has given us labels that are smaller than the given label.
507 //if so then it returns true so that the calling function can run the previous valid distance.
508 //it's a "smart" distance function. It also checks for invalid labels.
509 inline bool anyLabelsToProcess(string label, set<string>& userLabels, string errorOff) {
511 set<string>::iterator it;
512 vector<float> orderFloat;
513 map<string, float> userMap; //the conversion process removes trailing 0's which we need to put back
514 map<string, float>::iterator it2;
516 bool smaller = false;
518 //unique is the smallest line
519 if (label == "unique") { return false; }
520 else { convert(label, labelFloat); }
522 //go through users set and make them floats
523 for(it = userLabels.begin(); it != userLabels.end(); ++it) {
526 if ((*it != "unique") && (convertTestFloat(*it, temp) == true)){
528 orderFloat.push_back(temp);
530 }else if (*it == "unique") {
531 orderFloat.push_back(-1.0);
532 userMap["unique"] = -1.0;
534 if (errorOff == "") { cout << *it << " is not a valid label." << endl; }
535 userLabels.erase(*it);
541 sort(orderFloat.begin(), orderFloat.end());
543 /*************************************************/
544 //is this label bigger than any of the users labels
545 /*************************************************/
547 //loop through order until you find a label greater than label
548 for (int i = 0; i < orderFloat.size(); i++) {
549 if (orderFloat[i] < labelFloat) {
551 if (orderFloat[i] == -1) {
552 if (errorOff == "") { cout << "Your file does not include the label unique." << endl; }
553 userLabels.erase("unique");
556 if (errorOff == "") { cout << "Your file does not include the label "; }
558 for (it2 = userMap.begin(); it2!= userMap.end(); it2++) {
559 if (it2->second == orderFloat[i]) {
561 //remove small labels
566 if (errorOff == "") { cout << s << ". I will use the next smallest distance. " << endl; }
568 //since they are sorted once you find a bigger one stop looking
575 catch(exception& e) {
576 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function anyLabelsToProcess. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
580 cout << "An unknown error has occurred in the mothur class function anyLabelsToProcess. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
586 /**************************************************************************************************/