8 typedef unsigned long long ull;
10 /***********************************************************************/
12 // snagged from http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.2
13 // works for now, but there should be a way to do it without killing the whole program
15 class BadConversion : public runtime_error {
17 BadConversion(const string& s) : runtime_error(s){ }
20 //**********************************************************************************************************************
23 inline void convert(const string& s, T& x, bool failIfLeftoverChars = true){
26 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
27 throw BadConversion(s);
29 //**********************************************************************************************************************
32 inline bool convertTest(const string& s, T& x, bool failIfLeftoverChars = true){
35 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
37 cout << "'" << s << "' is unable to be converted into an integer.\n";
43 //**********************************************************************************************************************
46 string toString(const T&x){
52 //**********************************************************************************************************************
55 string toString(const T&x, int i){
65 /***********************************************************************/
67 inline void gobble(istream& f){
70 while(isspace(d=f.get())) {;}
75 /***********************************************************************/
77 inline float roundDist(float dist, int precision){
79 return int(dist * precision + 0.5)/float(precision);
83 /***********************************************************************/
85 inline int getNumNames(string names){
91 for(int i=0;i<names.size();i++){
102 /**************************************************************************************************/
104 inline vector<vector<double> > binomial(int maxOrder){
106 vector<vector<double> > binomial(maxOrder+1);
108 for(int i=0;i<=maxOrder;i++){
109 binomial[i].resize(maxOrder+1);
118 for(int i=2;i<=maxOrder;i++){
122 for(int i=2;i<=maxOrder;i++){
123 for(int j=1;j<=maxOrder;j++){
124 if(i==j){ binomial[i][j]=1; }
125 if(j>i) { binomial[i][j]=0; }
126 else { binomial[i][j]=binomial[i-1][j-1]+binomial[i-1][j]; }
133 /***********************************************************************/
135 inline string getRootName(string longName){
137 string rootName = longName;
139 if(longName.find_last_of(".") != longName.npos){
140 int pos = longName.find_last_of('.')+1;
141 rootName = longName.substr(0, pos);
146 /***********************************************************************/
148 inline string getSimpleName(string longName){
150 string simpleName = longName;
152 if(longName.find_last_of("/") != longName.npos){
153 int pos = longName.find_last_of('/')+1;
154 simpleName = longName.substr(pos, longName.length());
160 /***********************************************************************/
162 inline string getPathName(string longName){
164 string rootPathName = longName;
166 if(longName.find_last_of("/") != longName.npos){
167 int pos = longName.find_last_of('/')+1;
168 rootPathName = longName.substr(0, pos);
174 /***********************************************************************/
176 inline int openInputFile(string fileName, ifstream& fileHandle){
178 fileHandle.open(fileName.c_str());
180 cerr << "Error: Could not open " << fileName << endl;
189 /***********************************************************************/
191 inline int openOutputFile(string fileName, ofstream& fileHandle){
193 fileHandle.open(fileName.c_str(), ios::trunc);
195 cerr << "Error: Could not open " << fileName << endl;
204 /***********************************************************************/
206 //This function parses the estimator options and puts them in a vector
207 inline void splitAtDash(string& estim, vector<string>& container) {
211 while (estim.find_first_of('-') != -1) {
212 individual = estim.substr(0,estim.find_first_of('-'));
213 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
214 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
215 container.push_back(individual);
219 container.push_back(estim);
221 catch(exception& e) {
222 cout << "Standard Error: " << e.what() << " has occurred in the utilities class Function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
226 cout << "An unknown error has occurred in the utilities class function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
232 /***********************************************************************/
233 //This function parses the label options and puts them in a set
234 inline void splitAtDash(string& estim, set<string>& container) {
238 while (estim.find_first_of('-') != -1) {
239 individual = estim.substr(0,estim.find_first_of('-'));
240 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
241 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
242 container.insert(individual);
246 container.insert(estim);
248 catch(exception& e) {
249 cout << "Standard Error: " << e.what() << " has occurred in the utilities class Function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
253 cout << "An unknown error has occurred in the utilities class function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
258 /***********************************************************************/
259 //This function parses the line options and puts them in a set
260 inline void splitAtDash(string& estim, set<int>& container) {
265 while (estim.find_first_of('-') != -1) {
266 individual = estim.substr(0,estim.find_first_of('-'));
267 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
268 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
269 convert(individual, lineNum); //convert the string to int
270 container.insert(lineNum);
274 convert(estim, lineNum); //convert the string to int
275 container.insert(lineNum);
277 catch(exception& e) {
278 cout << "Standard Error: " << e.what() << " has occurred in the utilities class Function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
282 cout << "An unknown error has occurred in the utilities class function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
287 /***********************************************************************/
289 //This function splits up the various option parameters
290 inline void splitAtComma(string& prefix, string& suffix){
292 prefix = suffix.substr(0,suffix.find_first_of(','));
293 if ((suffix.find_first_of(',')+2) <= suffix.length()) { //checks to make sure you don't have comma at end of string
294 suffix = suffix.substr(suffix.find_first_of(',')+1, suffix.length());
296 while(suffix.at(0) == ' ')
297 suffix = suffix.substr(1, suffix.length());
301 catch(exception& e) {
302 cout << "Standard Error: " << e.what() << " has occurred in the utilities class Function splitAtComma. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
306 cout << "An unknown error has occurred in the utilities class function splitAtComma. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
311 /***********************************************************************/
313 //This function separates the key value from the option value i.e. dist=96_...
314 inline void splitAtEquals(string& key, string& value){
316 if(value.find_first_of('=') != -1){
317 key = value.substr(0,value.find_first_of('='));
318 if ((value.find_first_of('=')+1) <= value.length()) {
319 value = value.substr(value.find_first_of('=')+1, value.length());
326 catch(exception& e) {
327 cout << "Standard Error: " << e.what() << " has occurred in the utilities class Function splitAtEquals. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
331 cout << "An unknown error has occurred in the utilities class function splitAtEquals. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
336 /*******************************************************/