]> git.donarmstrong.com Git - mothur.git/blobdiff - hcluster.cpp
fixed unifrac.weighted
[mothur.git] / hcluster.cpp
index 7da10a271125fadf7e4833ea37e68ce24cfcd16f..597aac889f9842fa87508dcde5cc319c5ff1b0f6 100644 (file)
@@ -27,7 +27,7 @@ HCluster::HCluster(RAbundVector* rav, ListVector* lv, string ms, string d, NameA
                }
                
                if (method != "average") {
-                       openInputFile(distfile, filehandle);
+                       m->openInputFile(distfile, filehandle);
                }else{  
                        processFile();  
                }
@@ -389,7 +389,7 @@ vector<seqDist> HCluster::getSeqsFNNN(){
                //get entry
                while (!filehandle.eof()) {
                        
-                       filehandle >> firstName >> secondName >> distance;    gobble(filehandle); 
+                       filehandle >> firstName >> secondName >> distance;    m->gobble(filehandle); 
        
                        //save first one
                        if (prevDistance == -1) { prevDistance = distance; }
@@ -438,7 +438,7 @@ vector<seqDist> HCluster::getSeqsAN(){
                vector<seqDist> sameSeqs;
                prevDistance = -1;
                
-               openInputFile(distfile, filehandle, "no error"); 
+               m->openInputFile(distfile, filehandle, "no error"); 
                
                //is the smallest value in mergedMin or the distfile?
                float mergedMinDist = 10000;
@@ -446,13 +446,13 @@ vector<seqDist> HCluster::getSeqsAN(){
                if (mergedMin.size() > 0) { mergedMinDist = mergedMin[0].dist;  }
                        
                if (!filehandle.eof()) {  
-                       filehandle >> firstName >> secondName >> distance;    gobble(filehandle);
+                       filehandle >> firstName >> secondName >> distance;    m->gobble(filehandle);
                        //save first one
                        if (prevDistance == -1) { prevDistance = distance; } 
                        if (distance != -1) { //-1 means skip me
                                seqDist temp(firstName, secondName, distance);
                                sameSeqs.push_back(temp);
-                       }
+                       }else{ distance = 10000; }
                }
                
                if (mergedMinDist < distance) { //get minimum distance from mergedMin
@@ -469,7 +469,7 @@ vector<seqDist> HCluster::getSeqsAN(){
                        //get entry
                        while (!filehandle.eof()) {
                                
-                               filehandle >> firstName >> secondName >> distance;    gobble(filehandle); 
+                               filehandle >> firstName >> secondName >> distance;    m->gobble(filehandle); 
                                
                                if (prevDistance == -1) { prevDistance = distance; }
                                
@@ -502,7 +502,7 @@ vector<seqDist> HCluster::getSeqsAN(){
 }
 
 /***********************************************************************/
-void HCluster::combineFile() {
+int HCluster::combineFile() {
        try {
                //int bufferSize = 64000;  //512k - this should be a variable that the user can set to optimize code to their hardware
                //char* inputBuffer;
@@ -511,13 +511,13 @@ void HCluster::combineFile() {
                
                string tempDistFile = distfile + ".temp";
                ofstream out;
-               openOutputFile(tempDistFile, out);
+               m->openOutputFile(tempDistFile, out);
                
                //FILE* in;
                //in = fopen(distfile.c_str(), "rb");
        
                ifstream in;
-               openInputFile(distfile, in);
+               m->openInputFile(distfile, in, "no error");
                
                int first, second;
                float dist;
@@ -550,7 +550,9 @@ void HCluster::combineFile() {
                           //since file is sorted and mergedMin is sorted 
                           //you can put the smallest distance from each through the code below and keep the file sorted
                           
-                          in >> first >> second >> dist; gobble(in);
+                          in >> first >> second >> dist; m->gobble(in);
+                          
+                          if (m->control_pressed) { in.close(); out.close(); remove(tempDistFile.c_str()); return 0; }
                           
                           //while there are still values in mergedMin that are smaller than the distance read from file
                           while (count < mergedMin.size())  {
@@ -623,8 +625,8 @@ void HCluster::combineFile() {
                mergedMin.clear();
                        
                //rename tempfile to distfile
-               int renameOK = remove(distfile.c_str());
-               int ok = rename(tempDistFile.c_str(), distfile.c_str());
+               remove(distfile.c_str());
+               rename(tempDistFile.c_str(), distfile.c_str());
 //cout << "remove = "<< renameOK << " rename = " << ok << endl;        
 
                //merge clustered rows averaging the distances
@@ -648,6 +650,8 @@ void HCluster::combineFile() {
 
                //sort merged values
                sort(mergedMin.begin(), mergedMin.end(), compareSequenceDistance);      
+               
+               return 0;
        }
        catch(exception& e) {
                m->errorOut(e, "HCluster", "combineFile");
@@ -684,7 +688,7 @@ seqDist HCluster::getNextDist(char* buffer, int& index, int size){
                        if ((buffer[index] == 10) || (buffer[index] == 13)) { //newline in unix or windows
                                gotDist = true;
                                
-                               //gobble space
+                               //m->gobble space
                                while (index < size) {          
                                        if (isspace(buffer[index])) { index++; }
                                        else { break; }         
@@ -731,22 +735,23 @@ seqDist HCluster::getNextDist(char* buffer, int& index, int size){
        }
 }
 /***********************************************************************/
-void HCluster::processFile() {
+int HCluster::processFile() {
        try {
                string firstName, secondName;
                float distance;
                
                ifstream in;
-               openInputFile(distfile, in);
+               m->openInputFile(distfile, in, "no error");
                
                ofstream out;
                string outTemp = distfile + ".temp";
-               openOutputFile(outTemp, out);
+               m->openOutputFile(outTemp, out);
        
                //get entry
                while (!in.eof()) {
+                       if (m->control_pressed) { in.close(); out.close(); remove(outTemp.c_str()); return 0; }
                        
-                       in >> firstName >> secondName >> distance;    gobble(in);               
+                       in >> firstName >> secondName >> distance;    m->gobble(in);            
                        
                        map<string,int>::iterator itA = nameMap->find(firstName);
                        map<string,int>::iterator itB = nameMap->find(secondName);
@@ -766,6 +771,8 @@ void HCluster::processFile() {
                
                remove(distfile.c_str());
                rename(outTemp.c_str(), distfile.c_str());
+               
+               return 0;
        }
        catch(exception& e) {
                m->errorOut(e, "HCluster", "processFile");