]> git.donarmstrong.com Git - mothur.git/blobdiff - mothur.h
precluster command finished
[mothur.git] / mothur.h
index 20d3b4b64725d0428248b47b22a3146a0a3a52c9..cf6ae69dc85fc866c34588118575222772897dcf 100644 (file)
--- a/mothur.h
+++ b/mothur.h
@@ -21,6 +21,7 @@
 #include <iomanip>
 #include <fstream>
 #include <sstream>
+#include <signal.h>
 
 //exception
 #include <stdexcept>
 #include <ctime>
 #include <limits>
 
-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<double>::infinity())
-#else
+#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
        #include <sys/wait.h>
        #include <unistd.h>
+       
+       #ifdef USE_READLINE
+               #include <readline/readline.h>
+               #include <readline/history.h>
+       #endif
+
+       //#include <readline/readline.h>
+       //#include <readline/history.h>
+#else
+       #include <conio.h> //allows unbuffered screen capture from stdin
 #endif
 
+using namespace std;
+
+#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<double>::infinity())
+
 
-typedef unsigned long long ull;
+typedef unsigned long ull;
 
 struct IntNode {
        int lvalue;
@@ -69,6 +82,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 +93,22 @@ 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() {}
+};
 /***********************************************************************/
 
 // snagged from http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.2
@@ -96,6 +128,7 @@ inline void convert(const string& s, T& x, bool failIfLeftoverChars = true){
        if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
                throw BadConversion(s);
 }
+
 //**********************************************************************************************************************
 
 template<typename T>
@@ -153,14 +186,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 +200,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 +297,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 +441,35 @@ 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){
 
        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 +481,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 +499,16 @@ inline int getNumSeqs(ifstream& file){
        return numSeqs;
 
 }
+/***********************************************************************/
 
+inline bool inVector(string member, vector<string> 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 +783,92 @@ inline bool anyLabelsToProcess(string label, set<string>& 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