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;
54 /***********************************************************************/
56 // snagged from http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.2
57 // works for now, but there should be a way to do it without killing the whole program
59 class BadConversion : public runtime_error {
61 BadConversion(const string& s) : runtime_error(s){ }
64 //**********************************************************************************************************************
67 inline void convert(const string& s, T& x, bool failIfLeftoverChars = true){
70 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
71 throw BadConversion(s);
73 //**********************************************************************************************************************
76 inline bool convertTest(const string& s, T& x, bool failIfLeftoverChars = true){
79 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
81 cout << "'" << s << "' is unable to be converted into an integer.\n";
87 //**********************************************************************************************************************
90 string toString(const T&x){
96 //**********************************************************************************************************************
99 string toHex(const T&x){
106 //**********************************************************************************************************************
109 string toString(const T&x, int i){
113 output << fixed << x;
119 /***********************************************************************/
121 inline void gobble(istream& f){
124 while(isspace(d=f.get())) {;}
129 /***********************************************************************/
131 inline float roundDist(float dist, int precision){
133 return int(dist * precision + 0.5)/float(precision);
137 /***********************************************************************/
139 inline int getNumNames(string names){
145 for(int i=0;i<names.size();i++){
156 /**************************************************************************************************/
158 inline vector<vector<double> > binomial(int maxOrder){
160 vector<vector<double> > binomial(maxOrder+1);
162 for(int i=0;i<=maxOrder;i++){
163 binomial[i].resize(maxOrder+1);
172 for(int i=2;i<=maxOrder;i++){
176 for(int i=2;i<=maxOrder;i++){
177 for(int j=1;j<=maxOrder;j++){
178 if(i==j){ binomial[i][j]=1; }
179 if(j>i) { binomial[i][j]=0; }
180 else { binomial[i][j]=binomial[i-1][j-1]+binomial[i-1][j]; }
187 /***********************************************************************/
189 inline string getRootName(string longName){
191 string rootName = longName;
193 if(longName.find_last_of(".") != longName.npos){
194 int pos = longName.find_last_of('.')+1;
195 rootName = longName.substr(0, pos);
200 /***********************************************************************/
202 inline string getSimpleName(string longName){
204 string simpleName = longName;
206 if(longName.find_last_of("/") != longName.npos){
207 int pos = longName.find_last_of('/')+1;
208 simpleName = longName.substr(pos, longName.length());
213 /***********************************************************************/
215 inline int factorial(int num){
218 for (int i = 1; i <= num; i++) {
225 /***********************************************************************/
227 inline string getPathName(string longName){
229 string rootPathName = longName;
231 if(longName.find_last_of("/") != longName.npos){
232 int pos = longName.find_last_of('/')+1;
233 rootPathName = longName.substr(0, pos);
239 /***********************************************************************/
241 inline int openInputFile(string fileName, ifstream& fileHandle){
243 fileHandle.open(fileName.c_str());
245 cerr << "Error: Could not open " << fileName << endl;
254 /***********************************************************************/
256 inline int openOutputFile(string fileName, ofstream& fileHandle){
258 fileHandle.open(fileName.c_str(), ios::trunc);
260 cerr << "Error: Could not open " << fileName << endl;
269 /***********************************************************************/
271 //This function parses the estimator options and puts them in a vector
272 inline void splitAtDash(string& estim, vector<string>& container) {
276 while (estim.find_first_of('-') != -1) {
277 individual = estim.substr(0,estim.find_first_of('-'));
278 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
279 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
280 container.push_back(individual);
284 container.push_back(estim);
286 catch(exception& e) {
287 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
291 cout << "An unknown error has occurred in the mothur class function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
297 /***********************************************************************/
298 //This function parses the label options and puts them in a set
299 inline void splitAtDash(string& estim, set<string>& container) {
303 while (estim.find_first_of('-') != -1) {
304 individual = estim.substr(0,estim.find_first_of('-'));
305 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
306 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
307 container.insert(individual);
311 container.insert(estim);
313 catch(exception& e) {
314 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
318 cout << "An unknown error has occurred in the mothur class function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
323 /***********************************************************************/
324 //This function parses the line options and puts them in a set
325 inline void splitAtDash(string& estim, set<int>& container) {
330 while (estim.find_first_of('-') != -1) {
331 individual = estim.substr(0,estim.find_first_of('-'));
332 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
333 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
334 convert(individual, lineNum); //convert the string to int
335 container.insert(lineNum);
339 convert(estim, lineNum); //convert the string to int
340 container.insert(lineNum);
342 catch(exception& e) {
343 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
347 cout << "An unknown error has occurred in the mothur class function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
352 /***********************************************************************/
353 //This function parses the a string and puts peices in a vector
354 inline void splitAtComma(string& estim, vector<string>& container) {
358 while (estim.find_first_of(',') != -1) {
359 individual = estim.substr(0,estim.find_first_of(','));
360 if ((estim.find_first_of(',')+1) <= estim.length()) { //checks to make sure you don't have comma at end of string
361 estim = estim.substr(estim.find_first_of(',')+1, estim.length());
362 container.push_back(individual);
366 container.push_back(estim);
368 catch(exception& e) {
369 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtComma. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
373 cout << "An unknown error has occurred in the mothur class function splitAtComma. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
377 /***********************************************************************/
379 //This function splits up the various option parameters
380 inline void splitAtComma(string& prefix, string& suffix){
382 prefix = suffix.substr(0,suffix.find_first_of(','));
383 if ((suffix.find_first_of(',')+2) <= suffix.length()) { //checks to make sure you don't have comma at end of string
384 suffix = suffix.substr(suffix.find_first_of(',')+1, suffix.length());
386 while(suffix.at(0) == ' ')
387 suffix = suffix.substr(1, suffix.length());
391 catch(exception& e) {
392 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtComma. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
396 cout << "An unknown error has occurred in the mothur class function splitAtComma. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
401 /***********************************************************************/
403 //This function separates the key value from the option value i.e. dist=96_...
404 inline void splitAtEquals(string& key, string& value){
406 if(value.find_first_of('=') != -1){
407 key = value.substr(0,value.find_first_of('='));
408 if ((value.find_first_of('=')+1) <= value.length()) {
409 value = value.substr(value.find_first_of('=')+1, value.length());
416 catch(exception& e) {
417 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtEquals. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
421 cout << "An unknown error has occurred in the mothur class function splitAtEquals. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
426 /**************************************************************************************************/
428 inline bool inUsersGroups(string groupname, vector<string> Groups) {
430 for (int i = 0; i < Groups.size(); i++) {
431 if (groupname == Groups[i]) { return true; }
435 catch(exception& e) {
436 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function inUsersGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
440 cout << "An unknown error has occurred in the mothur class function inUsersGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
445 /**************************************************************************************************/