X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=mothur.h;h=8bd8d7f155ea7aaa28dc1b0d8b5bea2e9e4b43d6;hb=832d53a9dfac6b1795735eec643d8cf627b0d8e3;hp=20d3b4b64725d0428248b47b22a3146a0a3a52c9;hpb=510b1cfc25cd79391d6973ca20c5ec25fb1bb3b2;p=mothur.git diff --git a/mothur.h b/mothur.h index 20d3b4b..8bd8d7f 100644 --- a/mothur.h +++ b/mothur.h @@ -21,6 +21,7 @@ #include #include #include +#include //exception #include @@ -45,22 +46,33 @@ #include #include -using namespace std; +/***********************************************************************/ -#ifdef _WIN32 - #define exp(x) (exp((double) x)) - #define sqrt(x) (sqrt((double) x)) - #define log10(x) (log10((double) x)) - #define log2(x) (log10(x)/log10(2)) - #define isnan(x) ((x) != (x)) - #define isinf(x) (fabs(x) == std::numeric_limits::infinity()) -#else +#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) #include #include + + #ifdef USE_READLINE + #include + #include + #endif + + //#include + //#include +#else + #include //allows unbuffered screen capture from stdin #endif +using namespace std; -typedef unsigned long long ull; +#define exp(x) (exp((double) x)) +#define sqrt(x) (sqrt((double) x)) +#define log10(x) (log10((double) x)) +#define log2(x) (log10(x)/log10(2)) +#define isnan(x) ((x) != (x)) +#define isinf(x) (fabs(x) == std::numeric_limits::infinity()) + +typedef unsigned long ull; struct IntNode { int lvalue; @@ -69,6 +81,9 @@ struct IntNode { int rcoef; IntNode* left; IntNode* right; + + IntNode(int lv, int rv, IntNode* l, IntNode* r) : lvalue(lv), rvalue(rv), left(l), right(r) {}; + IntNode() {}; }; struct ThreadNode { @@ -77,6 +92,27 @@ struct ThreadNode { IntNode* right; }; +/************************************************************/ +struct clusterNode { + int numSeq; + int parent; + int smallChild; //used to make linkTable work with list and rabund. represents bin number of this cluster node + clusterNode(int num, int par, int kid) : numSeq(num), parent(par), smallChild(kid) {}; +}; +/************************************************************/ +struct seqDist { + int seq1; + int seq2; + float dist; + seqDist() {} + seqDist(int s1, int s2, float d) : seq1(s1), seq2(s2), dist(d) {} + ~seqDist() {} +}; +//******************************************************************************************************************** +//sorts lowest to highest +inline bool compareSequenceDistance(seqDist left, seqDist right){ + return (left.dist < right.dist); +} /***********************************************************************/ // snagged from http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.2 @@ -96,6 +132,7 @@ inline void convert(const string& s, T& x, bool failIfLeftoverChars = true){ if (!(i >> x) || (failIfLeftoverChars && i.get(c))) throw BadConversion(s); } + //********************************************************************************************************************** template @@ -153,14 +190,13 @@ string toString(const T&x, int i){ return output.str(); } - /***********************************************************************/ inline int openOutputFileAppend(string fileName, ofstream& fileHandle){ fileHandle.open(fileName.c_str(), ios::app); if(!fileHandle) { - cerr << "Error: Could not open " << fileName << endl; + cout << "Error: Could not open " << fileName << endl; return 1; } else { @@ -168,7 +204,39 @@ inline int openOutputFileAppend(string fileName, ofstream& fileHandle){ } } +/***********************************************************************/ +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); + } +} /**************************************************************************************************/ @@ -233,21 +301,11 @@ 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 pschloss@microbio.umass.edu, and be sure to include the mothur.logFile with your inquiry."); + 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 void gobble(istream& f){ - - char d; - while(isspace(d=f.get())) {;} - f.putback(d); - -} /***********************************************************************/ inline bool isTrue(string f){ @@ -387,17 +445,51 @@ inline string getExtension(string longName){ 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 int openInputFile(string fileName, ifstream& fileHandle, string m){ + fileHandle.open(fileName.c_str()); + if(!fileHandle) { + mothurOut("Error: Could not open " + fileName); mothurOutEndLine(); + return 1; + } + else { + //check for blank file + gobble(fileHandle); + return 0; + } + +} /***********************************************************************/ inline int openInputFile(string fileName, ifstream& fileHandle){ fileHandle.open(fileName.c_str()); if(!fileHandle) { - cerr << "Error: Could not open " << fileName << endl; + mothurOut("Error: Could not open " + fileName); mothurOutEndLine(); return 1; } else { + //check for blank file + gobble(fileHandle); + if (fileHandle.eof()) { mothurOut(fileName + " is blank. Please correct."); mothurOutEndLine(); return 1; } + return 0; } @@ -409,7 +501,7 @@ inline int openOutputFile(string fileName, ofstream& fileHandle){ fileHandle.open(fileName.c_str(), ios::trunc); if(!fileHandle) { - cerr << "Error: Could not open " << fileName << endl; + mothurOut("Error: Could not open " + fileName); mothurOutEndLine(); return 1; } else { @@ -427,7 +519,16 @@ inline int getNumSeqs(ifstream& file){ return numSeqs; } +/***********************************************************************/ +inline bool inVector(string member, vector group){ + + for (int i = 0; i < group.size(); i++) { + if (group[i] == member) { return true; } + } + + return false; +} /***********************************************************************/ //This function parses the estimator options and puts them in a vector @@ -702,6 +803,92 @@ inline bool anyLabelsToProcess(string label, set& userLabels, string err } } +/**************************************************************************************************/ +inline void appendFiles(string temp, string filename) { + try{ + ofstream output; + ifstream input; + + //open output file in append mode + openOutputFileAppend(filename, output); + openInputFile(temp, input); + + while(char c = input.get()){ + if(input.eof()) { break; } + else { output << c; } + } + + input.close(); + output.close(); + } + catch(exception& e) { + errorOut(e, "mothur", "appendFiles"); + exit(1); + } +} + +/**************************************************************************************************/ +inline string sortFile(string distFile){ + try { + string outfile = getRootName(distFile) + "sorted.dist"; + + //if you can, use the unix sort since its been optimized for years + #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) + string command = "sort -n -k +3 " + distFile + " -o " + outfile; + system(command.c_str()); + #else //you are stuck with my best attempt... + //windows sort does not have a way to specify a column, only a character in the line + //since we cannot assume that the distance will always be at the the same character location on each line + //due to variable sequence name lengths, I chose to force the distance into first position, then sort and then put it back. + + //read in file line by file and put distance first + string tempDistFile = distFile + ".temp"; + ifstream input; + ofstream output; + openInputFile(distFile, input); + openOutputFile(tempDistFile, output); + + string firstName, secondName; + float dist; + while (input) { + input >> firstName >> secondName >> dist; + output << dist << '\t' << firstName << '\t' << secondName << endl; + gobble(input); + } + input.close(); + output.close(); + + + //sort using windows sort + string tempOutfile = outfile + ".temp"; + string command = "sort " + tempDistFile + " /O " + tempOutfile; + system(command.c_str()); + + //read in sorted file and put distance at end again + ifstream input2; + openInputFile(tempOutfile, input2); + openOutputFile(outfile, output); + + while (input2) { + input2 >> dist >> firstName >> secondName; + output << firstName << '\t' << secondName << '\t' << dist << endl; + gobble(input2); + } + input2.close(); + output.close(); + + //remove temp files + remove(tempDistFile.c_str()); + remove(tempOutfile.c_str()); + #endif + + return outfile; + } + catch(exception& e) { + errorOut(e, "mothur", "sortFile"); + exit(1); + } +} /**************************************************************************************************/ #endif