9 typedef unsigned long long ull;
11 /***********************************************************************/
13 // snagged from http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.2
14 // works for now, but there should be a way to do it without killing the whole program
16 class BadConversion : public runtime_error {
18 BadConversion(const string& s) : runtime_error(s){ }
21 //**********************************************************************************************************************
24 inline void convert(const string& s, T& x, bool failIfLeftoverChars = true){
27 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
28 throw BadConversion(s);
30 //**********************************************************************************************************************
33 inline bool convertTest(const string& s, T& x, bool failIfLeftoverChars = true){
36 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
38 cout << "'" << s << "' is unable to be converted into an integer.\n";
44 //**********************************************************************************************************************
47 string toString(const T&x){
53 //**********************************************************************************************************************
56 string toString(const T&x, int i){
66 /***********************************************************************/
68 inline void gobble(istream& f){
71 while(isspace(d=f.get())) {;}
76 /***********************************************************************/
78 inline float roundDist(float dist, int precision){
80 return int(dist * precision + 0.5)/float(precision);
84 /***********************************************************************/
86 inline int getNumNames(string names){
92 for(int i=0;i<names.size();i++){
103 /**************************************************************************************************/
105 inline vector<vector<double> > binomial(int maxOrder){
107 vector<vector<double> > binomial(maxOrder+1);
109 for(int i=0;i<=maxOrder;i++){
110 binomial[i].resize(maxOrder+1);
119 for(int i=2;i<=maxOrder;i++){
123 for(int i=2;i<=maxOrder;i++){
124 for(int j=1;j<=maxOrder;j++){
125 if(i==j){ binomial[i][j]=1; }
126 if(j>i) { binomial[i][j]=0; }
127 else { binomial[i][j]=binomial[i-1][j-1]+binomial[i-1][j]; }
134 /***********************************************************************/
136 inline string getRootName(string longName){
138 string rootName = longName;
140 if(longName.find_last_of(".") != longName.npos){
141 int pos = longName.find_last_of('.')+1;
142 rootName = longName.substr(0, pos);
147 /***********************************************************************/
149 inline string getSimpleName(string longName){
151 string simpleName = longName;
153 if(longName.find_last_of("/") != longName.npos){
154 int pos = longName.find_last_of('/')+1;
155 simpleName = longName.substr(pos, longName.length());
161 /***********************************************************************/
163 inline string getPathName(string longName){
165 string rootPathName = longName;
167 if(longName.find_last_of("/") != longName.npos){
168 int pos = longName.find_last_of('/')+1;
169 rootPathName = longName.substr(0, pos);
175 /***********************************************************************/
177 inline int openInputFile(string fileName, ifstream& fileHandle){
179 fileHandle.open(fileName.c_str());
181 cerr << "Error: Could not open " << fileName << endl;
190 /***********************************************************************/
192 inline int openOutputFile(string fileName, ofstream& fileHandle){
194 fileHandle.open(fileName.c_str(), ios::trunc);
196 cerr << "Error: Could not open " << fileName << endl;
205 /***********************************************************************/
207 //This function parses the estimator options and puts them in a vector
208 inline void splitAtDash(string& estim, vector<string>& container) {
212 while (estim.find_first_of('-') != -1) {
213 individual = estim.substr(0,estim.find_first_of('-'));
214 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
215 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
216 container.push_back(individual);
220 container.push_back(estim);
222 catch(exception& e) {
223 cout << "Standard Error: " << e.what() << " has occurred in the utilities class Function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
227 cout << "An unknown error has occurred in the utilities class function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
233 /***********************************************************************/
234 //This function parses the label options and puts them in a set
235 inline void splitAtDash(string& estim, set<string>& container) {
239 while (estim.find_first_of('-') != -1) {
240 individual = estim.substr(0,estim.find_first_of('-'));
241 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
242 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
243 container.insert(individual);
247 container.insert(estim);
249 catch(exception& e) {
250 cout << "Standard Error: " << e.what() << " has occurred in the utilities class Function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
254 cout << "An unknown error has occurred in the utilities class function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
259 /***********************************************************************/
260 //This function parses the line options and puts them in a set
261 inline void splitAtDash(string& estim, set<int>& container) {
266 while (estim.find_first_of('-') != -1) {
267 individual = estim.substr(0,estim.find_first_of('-'));
268 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
269 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
270 convert(individual, lineNum); //convert the string to int
271 container.insert(lineNum);
275 convert(estim, lineNum); //convert the string to int
276 container.insert(lineNum);
278 catch(exception& e) {
279 cout << "Standard Error: " << e.what() << " has occurred in the utilities class Function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
283 cout << "An unknown error has occurred in the utilities class function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
288 /***********************************************************************/
290 //This function splits up the various option parameters
291 inline void splitAtComma(string& prefix, string& suffix){
293 prefix = suffix.substr(0,suffix.find_first_of(','));
294 if ((suffix.find_first_of(',')+2) <= suffix.length()) { //checks to make sure you don't have comma at end of string
295 suffix = suffix.substr(suffix.find_first_of(',')+1, suffix.length());
297 while(suffix.at(0) == ' ')
298 suffix = suffix.substr(1, suffix.length());
302 catch(exception& e) {
303 cout << "Standard Error: " << e.what() << " has occurred in the utilities class Function splitAtComma. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
307 cout << "An unknown error has occurred in the utilities class function splitAtComma. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
312 /***********************************************************************/
314 //This function separates the key value from the option value i.e. dist=96_...
315 inline void splitAtEquals(string& key, string& value){
317 if(value.find_first_of('=') != -1){
318 key = value.substr(0,value.find_first_of('='));
319 if ((value.find_first_of('=')+1) <= value.length()) {
320 value = value.substr(value.find_first_of('=')+1, value.length());
327 catch(exception& e) {
328 cout << "Standard Error: " << e.what() << " has occurred in the utilities class Function splitAtEquals. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
332 cout << "An unknown error has occurred in the utilities class function splitAtEquals. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
337 /**************************************************************************************************/
339 inline bool inUsersGroups(string groupname, vector<string> Groups) {
341 for (int i = 0; i < Groups.size(); i++) {
342 if (groupname == Groups[i]) { return true; }
346 catch(exception& e) {
347 cout << "Standard Error: " << e.what() << " has occurred in the utilities class Function inUsersGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
351 cout << "An unknown error has occurred in the utilities class function inUsersGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";