]> git.donarmstrong.com Git - mothur.git/blobdiff - hcluster.cpp
moved utilities out of mothur.h and into mothurOut class.
[mothur.git] / hcluster.cpp
index ee6040bb8802b36a6728a8abcd7dee7c7e3f2001..e1830cd8fbaacaf518f17b74a8822b475e485b53 100644 (file)
@@ -13,8 +13,9 @@
 #include "sparsematrix.hpp"
 
 /***********************************************************************/
-HCluster::HCluster(RAbundVector* rav, ListVector* lv, string m, string d, NameAssignment* n, float c) :  rabund(rav), list(lv), method(m), distfile(d), nameMap(n), cutoff(c) {
+HCluster::HCluster(RAbundVector* rav, ListVector* lv, string ms, string d, NameAssignment* n, float c) :  rabund(rav), list(lv), method(ms), distfile(d), nameMap(n), cutoff(c) {
        try {
+               m = MothurOut::getInstance();
                mapWanted = false;
                exitedBreak = false; 
                numSeqs = list->getNumSeqs();
@@ -26,13 +27,13 @@ HCluster::HCluster(RAbundVector* rav, ListVector* lv, string m, string d, NameAs
                }
                
                if (method != "average") {
-                       openInputFile(distfile, filehandle);
+                       m->openInputFile(distfile, filehandle);
                }else{  
                        processFile();  
                }
        }
        catch(exception& e) {
-               errorOut(e, "HCluster", "HCluster");
+               m->errorOut(e, "HCluster", "HCluster");
                exit(1);
        }
 }
@@ -49,7 +50,7 @@ void HCluster::clusterBins(){
                //cout << '\t' << rabund->get(clusterArray[smallRow].smallChild) << '\t' << rabund->get(clusterArray[smallCol].smallChild) << endl;
        }
        catch(exception& e) {
-               errorOut(e, "HCluster", "clusterBins");
+               m->errorOut(e, "HCluster", "clusterBins");
                exit(1);
        }
 
@@ -71,7 +72,7 @@ void HCluster::clusterNames(){
 
     }
        catch(exception& e) {
-               errorOut(e, "HCluster", "clusterNames");
+               m->errorOut(e, "HCluster", "clusterNames");
                exit(1);
        }
 
@@ -86,7 +87,7 @@ int HCluster::getUpmostParent(int node){
                return node;
        }
        catch(exception& e) {
-               errorOut(e, "HCluster", "getUpmostParent");
+               m->errorOut(e, "HCluster", "getUpmostParent");
                exit(1);
        }
 }
@@ -116,7 +117,7 @@ void HCluster::printInfo(){
                
        }
        catch(exception& e) {
-               errorOut(e, "HCluster", "getUpmostParent");
+               m->errorOut(e, "HCluster", "getUpmostParent");
                exit(1);
        }
 }
@@ -184,7 +185,7 @@ int HCluster::makeActive() {
                return linkValue;
        }
        catch(exception& e) {
-               errorOut(e, "HCluster", "makeActive");
+               m->errorOut(e, "HCluster", "makeActive");
                exit(1);
        }
 }
@@ -253,7 +254,7 @@ void HCluster::updateArrayandLinkTable() {
                }
        }
        catch(exception& e) {
-               errorOut(e, "HCluster", "updateArrayandLinkTable");
+               m->errorOut(e, "HCluster", "updateArrayandLinkTable");
                exit(1);
        }
 }
@@ -299,14 +300,14 @@ bool HCluster::update(int row, int col, float distance){
                //printInfo();
        }
        catch(exception& e) {
-               errorOut(e, "HCluster", "update");
+               m->errorOut(e, "HCluster", "update");
                exit(1);
        }
 }
 /***********************************************************************/
-void HCluster::setMapWanted(bool m)  {  
+void HCluster::setMapWanted(bool ms)  {  
        try {
-               mapWanted = m;
+               mapWanted = ms;
                
                //initialize map
                for (int i = 0; i < list->getNumBins(); i++) {
@@ -327,7 +328,7 @@ void HCluster::setMapWanted(bool m)  {
                
        }
        catch(exception& e) {
-               errorOut(e, "HCluster", "setMapWanted");
+               m->errorOut(e, "HCluster", "setMapWanted");
                exit(1);
        }
 }
@@ -348,7 +349,7 @@ try {
                seq2Bin[names] = clusterArray[smallCol].smallChild;
        }
        catch(exception& e) {
-               errorOut(e, "HCluster", "updateMap");
+               m->errorOut(e, "HCluster", "updateMap");
                exit(1);
        }
 }
@@ -366,7 +367,7 @@ vector<seqDist> HCluster::getSeqs(){
                return sameSeqs;
        }
        catch(exception& e) {
-               errorOut(e, "HCluster", "getSeqs");
+               m->errorOut(e, "HCluster", "getSeqs");
                exit(1);
        }
 }
@@ -388,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; }
@@ -424,7 +425,7 @@ vector<seqDist> HCluster::getSeqsFNNN(){
                return sameSeqs;
        }
        catch(exception& e) {
-               errorOut(e, "HCluster", "getSeqsFNNN");
+               m->errorOut(e, "HCluster", "getSeqsFNNN");
                exit(1);
        }
 }
@@ -437,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;
@@ -445,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
@@ -468,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; }
                                
@@ -495,13 +496,13 @@ vector<seqDist> HCluster::getSeqsAN(){
                return temp;
        }
        catch(exception& e) {
-               errorOut(e, "HCluster", "getSeqsAN");
+               m->errorOut(e, "HCluster", "getSeqsAN");
                exit(1);
        }
 }
 
 /***********************************************************************/
-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;
@@ -510,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);
                
                int first, second;
                float dist;
@@ -549,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())  {
@@ -622,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
@@ -647,9 +650,11 @@ void HCluster::combineFile() {
 
                //sort merged values
                sort(mergedMin.begin(), mergedMin.end(), compareSequenceDistance);      
+               
+               return 0;
        }
        catch(exception& e) {
-               errorOut(e, "HCluster", "combineFile");
+               m->errorOut(e, "HCluster", "combineFile");
                exit(1);
        }
 }
@@ -683,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; }         
@@ -725,27 +730,28 @@ seqDist HCluster::getNextDist(char* buffer, int& index, int size){
                return next;
        }
        catch(exception& e) {
-               errorOut(e, "HCluster", "getNextDist");
+               m->errorOut(e, "HCluster", "getNextDist");
                exit(1);
        }
 }
 /***********************************************************************/
-void HCluster::processFile() {
+int HCluster::processFile() {
        try {
                string firstName, secondName;
                float distance;
                
                ifstream in;
-               openInputFile(distfile, in);
+               m->openInputFile(distfile, in);
                
                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);
@@ -765,9 +771,11 @@ void HCluster::processFile() {
                
                remove(distfile.c_str());
                rename(outTemp.c_str(), distfile.c_str());
+               
+               return 0;
        }
        catch(exception& e) {
-               errorOut(e, "HCluster", "processFile");
+               m->errorOut(e, "HCluster", "processFile");
                exit(1);
        }
 }