]> git.donarmstrong.com Git - mothur.git/blobdiff - mothur.h
precluster command finished
[mothur.git] / mothur.h
index 5b519bb2111d27259d1969f81e41b805e4a33935..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 <cerrno>
 #include <ctime>
 #include <limits>
-#include <unistd.h>
+
+/***********************************************************************/
 
 #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;
@@ -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
@@ -170,6 +202,15 @@ 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 {
        
@@ -180,7 +221,7 @@ inline string getline(ifstream& fileHandle) {
                        char c = fileHandle.get(); 
                        
                        //are you at the end of the line
-                       if ((c == '\n') || (c == '\r') || (c == '\f')){   break;        }       
+                       if ((c == '\n') || (c == '\r') || (c == '\f')){  break; }       
                        else {          line += c;              }
                }
                
@@ -256,23 +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){
@@ -412,7 +441,21 @@ 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){
@@ -423,6 +466,10 @@ inline int openInputFile(string fileName, ifstream& fileHandle){
                return 1;
        }
        else {
+               //check for blank file
+               gobble(fileHandle);
+               if (fileHandle.eof()) { mothurOut(fileName + " is blank. Please correct."); mothurOutEndLine();  return 1;  }
+               
                return 0;
        }
        
@@ -452,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
@@ -727,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