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. */
43 typedef unsigned long long ull;
46 /***********************************************************************/
48 // snagged from http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.2
49 // works for now, but there should be a way to do it without killing the whole program
51 class BadConversion : public runtime_error {
53 BadConversion(const string& s) : runtime_error(s){ }
56 //**********************************************************************************************************************
59 inline void convert(const string& s, T& x, bool failIfLeftoverChars = true){
62 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
63 throw BadConversion(s);
65 //**********************************************************************************************************************
68 inline bool convertTest(const string& s, T& x, bool failIfLeftoverChars = true){
71 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
73 cout << "'" << s << "' is unable to be converted into an integer.\n";
79 //**********************************************************************************************************************
82 string toString(const T&x){
88 //**********************************************************************************************************************
91 string toHex(const T&x){
99 //**********************************************************************************************************************
102 string toString(const T&x, int i){
106 output << fixed << x;
112 /***********************************************************************/
114 inline void gobble(istream& f){
117 while(isspace(d=f.get())) {;}
122 /***********************************************************************/
124 inline float roundDist(float dist, int precision){
126 return int(dist * precision + 0.5)/float(precision);
130 /***********************************************************************/
132 inline int getNumNames(string names){
138 for(int i=0;i<names.size();i++){
149 /**************************************************************************************************/
151 inline vector<vector<double> > binomial(int maxOrder){
153 vector<vector<double> > binomial(maxOrder+1);
155 for(int i=0;i<=maxOrder;i++){
156 binomial[i].resize(maxOrder+1);
165 for(int i=2;i<=maxOrder;i++){
169 for(int i=2;i<=maxOrder;i++){
170 for(int j=1;j<=maxOrder;j++){
171 if(i==j){ binomial[i][j]=1; }
172 if(j>i) { binomial[i][j]=0; }
173 else { binomial[i][j]=binomial[i-1][j-1]+binomial[i-1][j]; }
180 /***********************************************************************/
182 inline string getRootName(string longName){
184 string rootName = longName;
186 if(longName.find_last_of(".") != longName.npos){
187 int pos = longName.find_last_of('.')+1;
188 rootName = longName.substr(0, pos);
193 /***********************************************************************/
195 inline string getSimpleName(string longName){
197 string simpleName = longName;
199 if(longName.find_last_of("/") != longName.npos){
200 int pos = longName.find_last_of('/')+1;
201 simpleName = longName.substr(pos, longName.length());
207 /***********************************************************************/
209 inline string getPathName(string longName){
211 string rootPathName = longName;
213 if(longName.find_last_of("/") != longName.npos){
214 int pos = longName.find_last_of('/')+1;
215 rootPathName = longName.substr(0, pos);
221 /***********************************************************************/
223 inline int openInputFile(string fileName, ifstream& fileHandle){
225 fileHandle.open(fileName.c_str());
227 cerr << "Error: Could not open " << fileName << endl;
236 /***********************************************************************/
238 inline int openOutputFile(string fileName, ofstream& fileHandle){
240 fileHandle.open(fileName.c_str(), ios::trunc);
242 cerr << "Error: Could not open " << fileName << endl;
251 /***********************************************************************/
253 //This function parses the estimator options and puts them in a vector
254 inline void splitAtDash(string& estim, vector<string>& container) {
258 while (estim.find_first_of('-') != -1) {
259 individual = estim.substr(0,estim.find_first_of('-'));
260 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
261 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
262 container.push_back(individual);
266 container.push_back(estim);
268 catch(exception& e) {
269 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
273 cout << "An unknown error has occurred in the mothur class function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
279 /***********************************************************************/
280 //This function parses the label options and puts them in a set
281 inline void splitAtDash(string& estim, set<string>& container) {
285 while (estim.find_first_of('-') != -1) {
286 individual = estim.substr(0,estim.find_first_of('-'));
287 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
288 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
289 container.insert(individual);
293 container.insert(estim);
295 catch(exception& e) {
296 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
300 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 line options and puts them in a set
307 inline void splitAtDash(string& estim, set<int>& container) {
312 while (estim.find_first_of('-') != -1) {
313 individual = estim.substr(0,estim.find_first_of('-'));
314 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
315 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
316 convert(individual, lineNum); //convert the string to int
317 container.insert(lineNum);
321 convert(estim, lineNum); //convert the string to int
322 container.insert(lineNum);
324 catch(exception& e) {
325 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
329 cout << "An unknown error has occurred in the mothur class function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
334 /***********************************************************************/
335 //This function parses the a string and puts peices in a vector
336 inline void splitAtComma(string& estim, vector<string>& container) {
340 while (estim.find_first_of(',') != -1) {
341 individual = estim.substr(0,estim.find_first_of(','));
342 if ((estim.find_first_of(',')+1) <= estim.length()) { //checks to make sure you don't have comma at end of string
343 estim = estim.substr(estim.find_first_of(',')+1, estim.length());
344 container.push_back(individual);
348 container.push_back(estim);
350 catch(exception& e) {
351 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtComma. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
355 cout << "An unknown error has occurred in the mothur class function splitAtComma. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
359 /***********************************************************************/
361 //This function splits up the various option parameters
362 inline void splitAtComma(string& prefix, string& suffix){
364 prefix = suffix.substr(0,suffix.find_first_of(','));
365 if ((suffix.find_first_of(',')+2) <= suffix.length()) { //checks to make sure you don't have comma at end of string
366 suffix = suffix.substr(suffix.find_first_of(',')+1, suffix.length());
368 while(suffix.at(0) == ' ')
369 suffix = suffix.substr(1, suffix.length());
373 catch(exception& e) {
374 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtComma. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
378 cout << "An unknown error has occurred in the mothur class function splitAtComma. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
383 /***********************************************************************/
385 //This function separates the key value from the option value i.e. dist=96_...
386 inline void splitAtEquals(string& key, string& value){
388 if(value.find_first_of('=') != -1){
389 key = value.substr(0,value.find_first_of('='));
390 if ((value.find_first_of('=')+1) <= value.length()) {
391 value = value.substr(value.find_first_of('=')+1, value.length());
398 catch(exception& e) {
399 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtEquals. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
403 cout << "An unknown error has occurred in the mothur class function splitAtEquals. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
408 /**************************************************************************************************/
410 inline bool inUsersGroups(string groupname, vector<string> Groups) {
412 for (int i = 0; i < Groups.size(); i++) {
413 if (groupname == Groups[i]) { return true; }
417 catch(exception& e) {
418 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function inUsersGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
422 cout << "An unknown error has occurred in the mothur class function inUsersGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
427 /**************************************************************************************************/