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++) {
225 /**************************************************************************************************
227 double min(double x, double y)
233 /***********************************************************************/
235 inline string getPathName(string longName){
237 string rootPathName = longName;
239 if(longName.find_last_of("/") != longName.npos){
240 int pos = longName.find_last_of('/')+1;
241 rootPathName = longName.substr(0, pos);
247 /***********************************************************************/
249 inline int openInputFile(string fileName, ifstream& fileHandle){
251 fileHandle.open(fileName.c_str());
253 cerr << "Error: Could not open " << fileName << endl;
262 /***********************************************************************/
264 inline int openOutputFile(string fileName, ofstream& fileHandle){
266 fileHandle.open(fileName.c_str(), ios::trunc);
268 cerr << "Error: Could not open " << fileName << endl;
277 /***********************************************************************/
279 //This function parses the estimator options and puts them in a vector
280 inline void splitAtDash(string& estim, vector<string>& container) {
284 while (estim.find_first_of('-') != -1) {
285 individual = estim.substr(0,estim.find_first_of('-'));
286 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
287 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
288 container.push_back(individual);
292 container.push_back(estim);
294 catch(exception& e) {
295 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
299 cout << "An unknown error has occurred in the mothur class function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
305 /***********************************************************************/
306 //This function parses the label options and puts them in a set
307 inline void splitAtDash(string& estim, set<string>& container) {
311 while (estim.find_first_of('-') != -1) {
312 individual = estim.substr(0,estim.find_first_of('-'));
313 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
314 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
315 container.insert(individual);
319 container.insert(estim);
321 catch(exception& e) {
322 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
326 cout << "An unknown error has occurred in the mothur class function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
331 /***********************************************************************/
332 //This function parses the line options and puts them in a set
333 inline void splitAtDash(string& estim, set<int>& container) {
338 while (estim.find_first_of('-') != -1) {
339 individual = estim.substr(0,estim.find_first_of('-'));
340 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
341 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
342 convert(individual, lineNum); //convert the string to int
343 container.insert(lineNum);
347 convert(estim, lineNum); //convert the string to int
348 container.insert(lineNum);
350 catch(exception& e) {
351 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
355 cout << "An unknown error has occurred in the mothur class function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
360 /***********************************************************************/
361 //This function parses the a string and puts peices in a vector
362 inline void splitAtComma(string& estim, vector<string>& container) {
366 while (estim.find_first_of(',') != -1) {
367 individual = estim.substr(0,estim.find_first_of(','));
368 if ((estim.find_first_of(',')+1) <= estim.length()) { //checks to make sure you don't have comma at end of string
369 estim = estim.substr(estim.find_first_of(',')+1, estim.length());
370 container.push_back(individual);
374 container.push_back(estim);
376 catch(exception& e) {
377 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtComma. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
381 cout << "An unknown error has occurred in the mothur class function splitAtComma. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
385 /***********************************************************************/
387 //This function splits up the various option parameters
388 inline void splitAtComma(string& prefix, string& suffix){
390 prefix = suffix.substr(0,suffix.find_first_of(','));
391 if ((suffix.find_first_of(',')+2) <= suffix.length()) { //checks to make sure you don't have comma at end of string
392 suffix = suffix.substr(suffix.find_first_of(',')+1, suffix.length());
394 while(suffix.at(0) == ' ')
395 suffix = suffix.substr(1, suffix.length());
399 catch(exception& e) {
400 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtComma. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
404 cout << "An unknown error has occurred in the mothur class function splitAtComma. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
409 /***********************************************************************/
411 //This function separates the key value from the option value i.e. dist=96_...
412 inline void splitAtEquals(string& key, string& value){
414 if(value.find_first_of('=') != -1){
415 key = value.substr(0,value.find_first_of('='));
416 if ((value.find_first_of('=')+1) <= value.length()) {
417 value = value.substr(value.find_first_of('=')+1, value.length());
424 catch(exception& e) {
425 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtEquals. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
429 cout << "An unknown error has occurred in the mothur class function splitAtEquals. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
434 /**************************************************************************************************/
436 inline bool inUsersGroups(string groupname, vector<string> Groups) {
438 for (int i = 0; i < Groups.size(); i++) {
439 if (groupname == Groups[i]) { return true; }
443 catch(exception& e) {
444 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function inUsersGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
448 cout << "An unknown error has occurred in the mothur class function inUsersGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
453 /**************************************************************************************************/