- stringstream output;
-
- output.precision(i);
- output << fixed << x;
-
- return output.str();
-}
-/***********************************************************************/
-
-inline int openOutputFileAppend(string fileName, ofstream& fileHandle){
-
- fileHandle.open(fileName.c_str(), ios::app);
- if(!fileHandle) {
- cout << "Error: Could not open " << fileName << endl;
- return 1;
- }
- else {
- return 0;
- }
-
-}
-/***********************************************************************/
-
-inline void gobble(istream& f){
-
- char d;
- while(isspace(d=f.get())) {;}
- f.putback(d);
-
-}
-/***********************************************************************/
-
-inline string getline(ifstream& fileHandle) {
- try {
-
- string line = "";
-
- while (!fileHandle.eof()) {
- //get next character
- char c = fileHandle.get();
-
- //are you at the end of the line
- if ((c == '\n') || (c == '\r') || (c == '\f')){ break; }
- else { line += c; }
- }
-
- return line;
-
- }
- catch(exception& e) {
- cout << "Error in mothur function getline" << endl;
- exit(1);
- }
-}
-
-/**************************************************************************************************/
-
-inline void mothurOut(string message) {
- try{
- ofstream out;
- string logFileName = "mothur.logFile";
- openOutputFileAppend(logFileName, out);
-
- cout << message;
- out << message;
-
- out.close();
- }
- catch(exception& e) {
- cout << "Error in mothur class mothurOut" << endl;
- exit(1);
- }
-}
-/**************************************************************************************************/
-
-inline void mothurOut(string message, string precision) {
- try{
- ofstream out;
- string logFileName = "mothur.logFile";
- openOutputFileAppend(logFileName, out);
-
- cout << precision << message;
- out << precision << message;
-
- out.close();
- }
- catch(exception& e) {
- cout << "Error in mothur class mothurOut" << endl;
- exit(1);
- }
-}
-
-/**************************************************************************************************/
-
-inline void mothurOutEndLine() {
- try {
- ofstream out;
- string logFileName = "mothur.logFile";
- openOutputFileAppend(logFileName, out);
-
- cout << endl;
- out << endl;
-
- out.close();
- }
- catch(exception& e) {
- cout << "error in mothur mothurOutEndLine" << endl;
- exit(1);
- }
-}
-
-
-/**************************************************************************************************/
-
-inline void errorOut(exception& e, string object, string function) {
-
- mothurOut("Error: ");
- mothurOut(toString(e.what()));
- mothurOut(" has occurred in the " + object + " class function " + function + ". Please contact Pat Schloss at mothur.bugs@gmail.com, and be sure to include the mothur.logFile with your inquiry.");
- mothurOutEndLine();
-
-}
-
-/***********************************************************************/
-
-inline bool isTrue(string f){
-
- if ((f == "TRUE") || (f == "T") || (f == "true") || (f == "t")) { return true; }
- else { return false; }
-}
-
-/***********************************************************************/
-
-inline float roundDist(float dist, int precision){
-
- return int(dist * precision + 0.5)/float(precision);
-
-}
-
-/***********************************************************************/
-
-inline int getNumNames(string names){
-
- int count = 0;
-
- if(names != ""){
- count = 1;
- for(int i=0;i<names.size();i++){
- if(names[i] == ','){
- count++;
- }
- }
- }
-
- return count;
-
-}
-
-/**************************************************************************************************/
-
-inline vector<vector<double> > binomial(int maxOrder){
-
- vector<vector<double> > binomial(maxOrder+1);
-
- for(int i=0;i<=maxOrder;i++){
- binomial[i].resize(maxOrder+1);
- binomial[i][0]=1;
- binomial[0][i]=0;
- }
- binomial[0][0]=1;
-
- binomial[1][0]=1;
- binomial[1][1]=1;
-
- for(int i=2;i<=maxOrder;i++){
- binomial[1][i]=0;
- }
-
- for(int i=2;i<=maxOrder;i++){
- for(int j=1;j<=maxOrder;j++){
- if(i==j){ binomial[i][j]=1; }
- if(j>i) { binomial[i][j]=0; }
- else { binomial[i][j]=binomial[i-1][j-1]+binomial[i-1][j]; }
- }
- }
-
- return binomial;
-}
-
-/***********************************************************************/
-
-inline string getRootName(string longName){
-
- string rootName = longName;
-
- if(longName.find_last_of(".") != longName.npos){
- int pos = longName.find_last_of('.')+1;
- rootName = longName.substr(0, pos);
- }
-
- return rootName;
-}
-/***********************************************************************/
-
-inline string getSimpleName(string longName){
-
- string simpleName = longName;
-
- size_t found;
- found=longName.find_last_of("/\\");
-
- if(found != longName.npos){
- simpleName = longName.substr(found+1);
- }
-
- //if(longName.find_last_of("/") != longName.npos){
- // int pos = longName.find_last_of('/')+1;
- // simpleName = longName.substr(pos, longName.length());
- //}
-
- return simpleName;
-}
-
-/***********************************************************************/
-
-inline int factorial(int num){
- int total = 1;
-
- for (int i = 1; i <= num; i++) {
- total *= i;
- }
-
- return total;
-}
-/**************************************************************************************************
-
-double min(double x, double y)
-{
- if(x<y){ return x; }
- else { return y; }
-}
-
-/***********************************************************************/
-
-inline string getPathName(string longName){
-
- string rootPathName = longName;
-
- if(longName.find_last_of("/\\") != longName.npos){
- int pos = longName.find_last_of("/\\")+1;
- rootPathName = longName.substr(0, pos);
- }
-
- return rootPathName;
-}
-/***********************************************************************/
-
-inline string hasPath(string longName){
-
- string path = "";
-
- size_t found;
- found=longName.find_last_of("/\\");
-
- if(found != longName.npos){
- path = longName.substr(0, found+1);
- }
-
- return path;
-}
-
-/***********************************************************************/
-
-inline string getExtension(string longName){
-
- string extension = longName;
-
- if(longName.find_last_of('.') != longName.npos){
- int pos = longName.find_last_of('.');
- extension = longName.substr(pos, longName.length());
- }
-
- return extension;
-}
-/***********************************************************************/
-inline bool isBlank(string fileName){
-
- ifstream fileHandle;
- fileHandle.open(fileName.c_str());
- if(!fileHandle) {
- mothurOut("Error: Could not open " + fileName); mothurOutEndLine();
- return false;
- }else {
- //check for blank file
- gobble(fileHandle);
- if (fileHandle.eof()) { fileHandle.close(); return true; }
- }
- return false;
-}
-/***********************************************************************/
-
-inline string getFullPathName(string fileName){
- try{
- string path = hasPath(fileName);
- string newFileName;
- int pos;
-
- if (path == "") { return fileName; } //its a simple name
- else { //we need to complete the pathname
- // ex. ../../../filename
- // cwd = /user/work/desktop
-
- string cwd;
- //get current working directory
- #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
- if (path.rfind("./") == -1) { return fileName; } //already complete name
- else { newFileName = fileName.substr(fileName.rfind("./")+2); } //save the complete part of the name
-
- char* cwdpath = new char[1024];
-
- size_t size;
- cwdpath=getcwd(cwdpath,size);
-
- cwd = cwdpath;
-
- //rip off first '/'
- string simpleCWD;
- if (cwd.length() > 0) { simpleCWD = cwd.substr(1); }
-
- //break apart the current working directory
- vector<string> dirs;
- while (simpleCWD.find_first_of('/') != -1) {
- string dir = simpleCWD.substr(0,simpleCWD.find_first_of('/'));
- simpleCWD = simpleCWD.substr(simpleCWD.find_first_of('/')+1, simpleCWD.length());
- dirs.push_back(dir);
- }
- //get last one // ex. ../../../filename = /user/work/desktop/filename
- dirs.push_back(simpleCWD); //ex. dirs[0] = user, dirs[1] = work, dirs[2] = desktop
-
-
- int index = dirs.size()-1;
-
- while((pos = path.rfind("./")) != -1) { //while you don't have a complete path
- if (path[(pos-1)] == '.') { //you want your parent directory ../
- path = path.substr(0, pos-1);
- index--;
- if (index == 0) { break; }
- }else if (path[(pos-1)] == '/') { //you want the current working dir ./
- path = path.substr(0, pos);
- }else if (pos == 1) { break;
- }else { mothurOut("cannot resolve path for " + fileName); mothurOutEndLine(); return fileName; }
- }
-
- for (int i = index; i >= 0; i--) {
- newFileName = dirs[i] + "/" + newFileName;
- }
-
- newFileName = "/" + newFileName;
- return newFileName;
-
- #else
- if (path.rfind(".\\") == -1) { return fileName; } //already complete name
- else { newFileName = fileName.substr(fileName.rfind(".\\")+2); } //save the complete part of the name
-
- char *cwdpath = NULL;
- cwdpath = getcwd(NULL, 0); // or _getcwd
- if ( cwdpath != NULL) { cwd = cwdpath; }
- else { cwd = ""; }
-
- //break apart the current working directory
- vector<string> dirs;
- while (cwd.find_first_of('\\') != -1) {
- string dir = cwd.substr(0,cwd.find_first_of('\\'));
- cwd = cwd.substr(cwd.find_first_of('\\')+1, cwd.length());
- dirs.push_back(dir);