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 convertTest(const string& s, T& x, bool failIfLeftoverChars = true){
80 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
82 cout << "'" << s << "' is unable to be converted into an integer.\n";
88 //**********************************************************************************************************************
91 string toString(const T&x){
97 //**********************************************************************************************************************
100 string toHex(const T&x){
107 //**********************************************************************************************************************
110 string toString(const T&x, int i){
114 output << fixed << x;
120 /***********************************************************************/
122 inline void gobble(istream& f){
125 while(isspace(d=f.get())) {;}
130 /***********************************************************************/
132 inline float roundDist(float dist, int precision){
134 return int(dist * precision + 0.5)/float(precision);
138 /***********************************************************************/
140 inline int getNumNames(string names){
146 for(int i=0;i<names.size();i++){
157 /**************************************************************************************************/
159 inline vector<vector<double> > binomial(int maxOrder){
161 vector<vector<double> > binomial(maxOrder+1);
163 for(int i=0;i<=maxOrder;i++){
164 binomial[i].resize(maxOrder+1);
173 for(int i=2;i<=maxOrder;i++){
177 for(int i=2;i<=maxOrder;i++){
178 for(int j=1;j<=maxOrder;j++){
179 if(i==j){ binomial[i][j]=1; }
180 if(j>i) { binomial[i][j]=0; }
181 else { binomial[i][j]=binomial[i-1][j-1]+binomial[i-1][j]; }
188 /***********************************************************************/
190 inline string getRootName(string longName){
192 string rootName = longName;
194 if(longName.find_last_of(".") != longName.npos){
195 int pos = longName.find_last_of('.')+1;
196 rootName = longName.substr(0, pos);
201 /***********************************************************************/
203 inline string getSimpleName(string longName){
205 string simpleName = longName;
207 if(longName.find_last_of("/") != longName.npos){
208 int pos = longName.find_last_of('/')+1;
209 simpleName = longName.substr(pos, longName.length());
214 /***********************************************************************/
216 inline int factorial(int num){
219 for (int i = 1; i <= num; i++) {
226 /***********************************************************************/
228 inline string getPathName(string longName){
230 string rootPathName = longName;
232 if(longName.find_last_of("/") != longName.npos){
233 int pos = longName.find_last_of('/')+1;
234 rootPathName = longName.substr(0, pos);
240 /***********************************************************************/
242 inline int openInputFile(string fileName, ifstream& fileHandle){
244 fileHandle.open(fileName.c_str());
246 cerr << "Error: Could not open " << fileName << endl;
255 /***********************************************************************/
257 inline int openOutputFile(string fileName, ofstream& fileHandle){
259 fileHandle.open(fileName.c_str(), ios::trunc);
261 cerr << "Error: Could not open " << fileName << endl;
270 /***********************************************************************/
272 //This function parses the estimator options and puts them in a vector
273 inline void splitAtDash(string& estim, vector<string>& container) {
277 while (estim.find_first_of('-') != -1) {
278 individual = estim.substr(0,estim.find_first_of('-'));
279 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
280 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
281 container.push_back(individual);
285 container.push_back(estim);
287 catch(exception& e) {
288 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
292 cout << "An unknown error has occurred in the mothur class function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
298 /***********************************************************************/
299 //This function parses the label options and puts them in a set
300 inline void splitAtDash(string& estim, set<string>& container) {
304 while (estim.find_first_of('-') != -1) {
305 individual = estim.substr(0,estim.find_first_of('-'));
306 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
307 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
308 container.insert(individual);
312 container.insert(estim);
314 catch(exception& e) {
315 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
319 cout << "An unknown error has occurred in the mothur class function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
324 /***********************************************************************/
325 //This function parses the line options and puts them in a set
326 inline void splitAtDash(string& estim, set<int>& container) {
331 while (estim.find_first_of('-') != -1) {
332 individual = estim.substr(0,estim.find_first_of('-'));
333 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
334 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
335 convert(individual, lineNum); //convert the string to int
336 container.insert(lineNum);
340 convert(estim, lineNum); //convert the string to int
341 container.insert(lineNum);
343 catch(exception& e) {
344 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
348 cout << "An unknown error has occurred in the mothur class function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
353 /***********************************************************************/
354 //This function parses the a string and puts peices in a vector
355 inline void splitAtComma(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 comma 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 splitAtComma. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
374 cout << "An unknown error has occurred in the mothur class function splitAtComma. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
378 /***********************************************************************/
380 //This function splits up the various option parameters
381 inline void splitAtComma(string& prefix, string& suffix){
383 prefix = suffix.substr(0,suffix.find_first_of(','));
384 if ((suffix.find_first_of(',')+2) <= suffix.length()) { //checks to make sure you don't have comma at end of string
385 suffix = suffix.substr(suffix.find_first_of(',')+1, suffix.length());
387 while(suffix.at(0) == ' ')
388 suffix = suffix.substr(1, suffix.length());
392 catch(exception& e) {
393 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtComma. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
397 cout << "An unknown error has occurred in the mothur class function splitAtComma. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
402 /***********************************************************************/
404 //This function separates the key value from the option value i.e. dist=96_...
405 inline void splitAtEquals(string& key, string& value){
407 if(value.find_first_of('=') != -1){
408 key = value.substr(0,value.find_first_of('='));
409 if ((value.find_first_of('=')+1) <= value.length()) {
410 value = value.substr(value.find_first_of('=')+1, value.length());
417 catch(exception& e) {
418 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtEquals. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
422 cout << "An unknown error has occurred in the mothur class function splitAtEquals. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
427 /**************************************************************************************************/
429 inline bool inUsersGroups(string groupname, vector<string> Groups) {
431 for (int i = 0; i < Groups.size(); i++) {
432 if (groupname == Groups[i]) { return true; }
436 catch(exception& e) {
437 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function inUsersGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
441 cout << "An unknown error has occurred in the mothur class function inUsersGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
446 /**************************************************************************************************/