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 toHex(const T&x){
64 //**********************************************************************************************************************
67 string toString(const T&x, int i){
77 /***********************************************************************/
79 inline void gobble(istream& f){
82 while(isspace(d=f.get())) {;}
87 /***********************************************************************/
89 inline float roundDist(float dist, int precision){
91 return int(dist * precision + 0.5)/float(precision);
95 /***********************************************************************/
97 inline int getNumNames(string names){
103 for(int i=0;i<names.size();i++){
114 /**************************************************************************************************/
116 inline vector<vector<double> > binomial(int maxOrder){
118 vector<vector<double> > binomial(maxOrder+1);
120 for(int i=0;i<=maxOrder;i++){
121 binomial[i].resize(maxOrder+1);
130 for(int i=2;i<=maxOrder;i++){
134 for(int i=2;i<=maxOrder;i++){
135 for(int j=1;j<=maxOrder;j++){
136 if(i==j){ binomial[i][j]=1; }
137 if(j>i) { binomial[i][j]=0; }
138 else { binomial[i][j]=binomial[i-1][j-1]+binomial[i-1][j]; }
145 /***********************************************************************/
147 inline string getRootName(string longName){
149 string rootName = longName;
151 if(longName.find_last_of(".") != longName.npos){
152 int pos = longName.find_last_of('.')+1;
153 rootName = longName.substr(0, pos);
158 /***********************************************************************/
160 inline string getSimpleName(string longName){
162 string simpleName = longName;
164 if(longName.find_last_of("/") != longName.npos){
165 int pos = longName.find_last_of('/')+1;
166 simpleName = longName.substr(pos, longName.length());
172 /***********************************************************************/
174 inline string getPathName(string longName){
176 string rootPathName = longName;
178 if(longName.find_last_of("/") != longName.npos){
179 int pos = longName.find_last_of('/')+1;
180 rootPathName = longName.substr(0, pos);
186 /***********************************************************************/
188 inline int openInputFile(string fileName, ifstream& fileHandle){
190 fileHandle.open(fileName.c_str());
192 cerr << "Error: Could not open " << fileName << endl;
201 /***********************************************************************/
203 inline int openOutputFile(string fileName, ofstream& fileHandle){
205 fileHandle.open(fileName.c_str(), ios::trunc);
207 cerr << "Error: Could not open " << fileName << endl;
216 /***********************************************************************/
218 //This function parses the estimator options and puts them in a vector
219 inline void splitAtDash(string& estim, vector<string>& container) {
223 while (estim.find_first_of('-') != -1) {
224 individual = estim.substr(0,estim.find_first_of('-'));
225 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
226 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
227 container.push_back(individual);
231 container.push_back(estim);
233 catch(exception& e) {
234 cout << "Standard Error: " << e.what() << " has occurred in the utilities class Function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
238 cout << "An unknown error has occurred in the utilities class function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
244 /***********************************************************************/
245 //This function parses the label options and puts them in a set
246 inline void splitAtDash(string& estim, set<string>& container) {
250 while (estim.find_first_of('-') != -1) {
251 individual = estim.substr(0,estim.find_first_of('-'));
252 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
253 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
254 container.insert(individual);
258 container.insert(estim);
260 catch(exception& e) {
261 cout << "Standard Error: " << e.what() << " has occurred in the utilities class Function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
265 cout << "An unknown error has occurred in the utilities class function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
270 /***********************************************************************/
271 //This function parses the line options and puts them in a set
272 inline void splitAtDash(string& estim, set<int>& 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 convert(individual, lineNum); //convert the string to int
282 container.insert(lineNum);
286 convert(estim, lineNum); //convert the string to int
287 container.insert(lineNum);
289 catch(exception& e) {
290 cout << "Standard Error: " << e.what() << " has occurred in the utilities class Function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
294 cout << "An unknown error has occurred in the utilities class function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
299 /***********************************************************************/
301 //This function splits up the various option parameters
302 inline void splitAtComma(string& prefix, string& suffix){
304 prefix = suffix.substr(0,suffix.find_first_of(','));
305 if ((suffix.find_first_of(',')+2) <= suffix.length()) { //checks to make sure you don't have comma at end of string
306 suffix = suffix.substr(suffix.find_first_of(',')+1, suffix.length());
308 while(suffix.at(0) == ' ')
309 suffix = suffix.substr(1, suffix.length());
313 catch(exception& e) {
314 cout << "Standard Error: " << e.what() << " has occurred in the utilities class Function splitAtComma. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
318 cout << "An unknown error has occurred in the utilities class function splitAtComma. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
323 /***********************************************************************/
325 //This function separates the key value from the option value i.e. dist=96_...
326 inline void splitAtEquals(string& key, string& value){
328 if(value.find_first_of('=') != -1){
329 key = value.substr(0,value.find_first_of('='));
330 if ((value.find_first_of('=')+1) <= value.length()) {
331 value = value.substr(value.find_first_of('=')+1, value.length());
338 catch(exception& e) {
339 cout << "Standard Error: " << e.what() << " has occurred in the utilities class Function splitAtEquals. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
343 cout << "An unknown error has occurred in the utilities class function splitAtEquals. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
348 /**************************************************************************************************/
350 inline bool inUsersGroups(string groupname, vector<string> Groups) {
352 for (int i = 0; i < Groups.size(); i++) {
353 if (groupname == Groups[i]) { return true; }
357 catch(exception& e) {
358 cout << "Standard Error: " << e.what() << " has occurred in the utilities class Function inUsersGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
362 cout << "An unknown error has occurred in the utilities class function inUsersGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";