]> git.donarmstrong.com Git - mothur.git/blobdiff - cluster.cpp
fixed io
[mothur.git] / cluster.cpp
index e360624dbfc599f94901fe12c1518fe8c11e877d..ac9f4482da12110796c54f15c4137681a3d29038 100644 (file)
 
 /***********************************************************************/
 
-Cluster::Cluster(RAbundVector* rav, ListVector* lv, SparseMatrix* dm, float c, string m) :
-rabund(rav), list(lv), dMatrix(dm), method(m)
+Cluster::Cluster(RAbundVector* rav, ListVector* lv, SparseMatrix* dm, float c, string f) :
+rabund(rav), list(lv), dMatrix(dm), method(f)
 {
+       try {
 /*
        cout << "sizeof(MatData): " << sizeof(MatData) << endl;
        cout << "sizeof(PCell*): " << sizeof(PCell*) << endl;
@@ -50,15 +51,28 @@ rabund(rav), list(lv), dMatrix(dm), method(m)
        // a list contains pointers (iterators) to the all distances related
        // to a certain sequence. The Vector is accessed via the index of a 
        // sequence in the distance matrix.
+//ofstream outtemp;
+//string temp = "temp";
+//m->openOutputFile(temp, outtemp);    
+//cout << lv->size() << endl;
        seqVec = vector<MatVec>(lv->size());
        for (MatData currentCell = dMatrix->begin(); currentCell != dMatrix->end(); currentCell++) {
+//outtemp << currentCell->row << '\t' << currentCell->column  << '\t' << currentCell->dist << endl;
                seqVec[currentCell->row].push_back(currentCell);
                seqVec[currentCell->column].push_back(currentCell);
        }
+//outtemp.close();
        mapWanted = false;  //set to true by mgcluster to speed up overlap merge
        
        //save so you can modify as it changes in average neighbor
        cutoff = c;
+       m = MothurOut::getInstance();
+       
+       }
+       catch(exception& e) {
+               m->errorOut(e, "Cluster", "Cluster");
+               exit(1);
+       }
 }
 
 /***********************************************************************/
@@ -70,14 +84,19 @@ void Cluster::getRowColCells() {
                smallRow = smallCell->row;              // get its row
                smallCol = smallCell->column;   // get its column
                smallDist = smallCell->dist;    // get the smallest distance
-       
+       //cout << "small row = " << smallRow << "small col = " << smallCol << "small dist = " << smallDist << endl;
+        
                rowCells = seqVec[smallRow];    // all distances related to the row index
                colCells = seqVec[smallCol];    // all distances related to the column index
                nRowCells = rowCells.size();
                nColCells = colCells.size();
+//cout << "num rows = " << nRowCells << "num col = " << nColCells << endl;
+               
+               //for (int i = 0; i < nColCells; i++) { cout << colCells[i]->row << '\t' << colCells[i]->column << endl;  }
+               //for (int i = 0; i < nRowCells; i++) { cout << rowCells[i]->row << '\t' << rowCells[i]->column << endl;  }
        }
        catch(exception& e) {
-               errorOut(e, "Cluster", "getRowColCells");
+               m->errorOut(e, "Cluster", "getRowColCells");
                exit(1);
        }
 
@@ -85,50 +104,60 @@ void Cluster::getRowColCells() {
 /***********************************************************************/
 // Remove the specified cell from the seqVec and from the sparse
 // matrix
-void Cluster::removeCell(const MatData& cell, int vrow, int vcol, bool rmMatrix)
-{
-       ull drow = cell->row;
-       ull dcol = cell->column;
-       if (((vrow >=0) && (drow != smallRow)) ||
-               ((vcol >=0) && (dcol != smallCol))) {
-               ull dtemp = drow;
-               drow = dcol;
-               dcol = dtemp;
-       }
+void Cluster::removeCell(const MatData& cell, int vrow, int vcol, bool rmMatrix){
+       try {
+       
+               ull drow = cell->row;
+                       ull dcol = cell->column;
+                       if (((vrow >=0) && (drow != smallRow)) ||
+                               ((vcol >=0) && (dcol != smallCol))) {
+                               ull dtemp = drow;
+                               drow = dcol;
+                               dcol = dtemp;
+                       }
 
-       ull crow;
-       ull ccol;
-       int nCells;
-       if (vrow < 0) {
-               nCells = seqVec[drow].size();
-               for (vrow=0; vrow<nCells;vrow++) {
-                       crow = seqVec[drow][vrow]->row;
-                       ccol = seqVec[drow][vrow]->column;
-                       if (((crow == drow) && (ccol == dcol)) ||
-                               ((ccol == drow) && (crow == dcol))) {
-                               break;
+                       ull crow;
+                       ull ccol;
+                       int nCells;
+                       if (vrow < 0) {
+                               nCells = seqVec[drow].size();
+                               for (vrow=0; vrow<nCells;vrow++) {
+                                       crow = seqVec[drow][vrow]->row;
+                                       ccol = seqVec[drow][vrow]->column;
+                                       if (((crow == drow) && (ccol == dcol)) ||
+                                               ((ccol == drow) && (crow == dcol))) {
+                                               break;
+                                       }
+                               }
                        }
-               }
-       }
-       seqVec[drow].erase(seqVec[drow].begin()+vrow);
-       if (vcol < 0) {
-               nCells = seqVec[dcol].size();
-               for (vcol=0; vcol<nCells;vcol++) {
-                       crow = seqVec[dcol][vcol]->row;
-                       ccol = seqVec[dcol][vcol]->column;
-                       if (((crow == drow) && (ccol == dcol)) ||
-                               ((ccol == drow) && (crow == dcol))) {
-                               break;
+
+                       seqVec[drow].erase(seqVec[drow].begin()+vrow);
+                       if (vcol < 0) {
+                               nCells = seqVec[dcol].size();
+                               for (vcol=0; vcol<nCells;vcol++) {
+                                       crow = seqVec[dcol][vcol]->row;
+                                       ccol = seqVec[dcol][vcol]->column;
+                                       if (((crow == drow) && (ccol == dcol)) ||
+                                               ((ccol == drow) && (crow == dcol))) {
+                                               break;
+                                       }
+                               }
                        }
-               }
+               
+                       seqVec[dcol].erase(seqVec[dcol].begin()+vcol);
+               
+                       if (rmMatrix) {
+                       //cout << " removing = " << cell->row << '\t' << cell->column  << '\t' << cell->dist << endl;
+                               dMatrix->rmCell(cell);
+               //      cout << "done" << endl;
+                       }
+               
        }
-       seqVec[dcol].erase(seqVec[dcol].begin()+vcol);
-       if (rmMatrix) {
-               dMatrix->rmCell(cell);
+       catch(exception& e) {
+               m->errorOut(e, "Cluster", "removeCell");
+               exit(1);
        }
 }
-
-
 /***********************************************************************/
 
 void Cluster::clusterBins(){
@@ -142,7 +171,7 @@ void Cluster::clusterBins(){
        //      cout << '\t' << rabund->get(smallRow) << '\t' << rabund->get(smallCol) << endl;
        }
        catch(exception& e) {
-               errorOut(e, "Cluster", "clusterBins");
+               m->errorOut(e, "Cluster", "clusterBins");
                exit(1);
        }
 
@@ -163,7 +192,7 @@ void Cluster::clusterNames(){
        //      cout << '\t' << list->get(smallRow) << '\t' << list->get(smallCol) << endl;
     }
        catch(exception& e) {
-               errorOut(e, "Cluster", "clusterNames");
+               m->errorOut(e, "Cluster", "clusterNames");
                exit(1);
        }
 
@@ -176,7 +205,7 @@ void Cluster::clusterNames(){
 void Cluster::update(double& cutOFF){
        try {
                getRowColCells();       
-       
+
                vector<int> foundCol(nColCells, 0);
 
                int search;
@@ -185,6 +214,7 @@ void Cluster::update(double& cutOFF){
                // The vector has to be traversed in reverse order to preserve the index
                // for faster removal in removeCell()
                for (int i=nRowCells-1;i>=0;i--) {
+                       //if you are not the smallCell
                        if (!((rowCells[i]->row == smallRow) && (rowCells[i]->column == smallCol))) {
                                if (rowCells[i]->row == smallRow) {
                                        search = rowCells[i]->column;
@@ -212,9 +242,12 @@ void Cluster::update(double& cutOFF){
                                        }               
                                }
                                //if not merged it you need it for warning 
-                               if ((!merged) && (method == "average")) {  
-                                       mothurOut("Warning: trying to merge cell " + toString(rowCells[i]->row+1) + " " + toString(rowCells[i]->column+1) + " distance " + toString(rowCells[i]->dist) + " with value above cutoff. Results may vary from using cutoff at cluster command instead of read.dist."); mothurOutEndLine(); 
-                                       if (cutOFF > rowCells[i]->dist) {  cutOFF = rowCells[i]->dist;  mothurOut("changing cutoff to " + toString(cutOFF));  mothurOutEndLine(); }
+                               if ((!merged) && (method == "average" || method == "weighted")) {  
+                                       //m->mothurOut("Warning: trying to merge cell " + toString(rowCells[i]->row+1) + " " + toString(rowCells[i]->column+1) + " distance " + toString(rowCells[i]->dist) + " with value above cutoff. Results may vary from using cutoff at cluster command instead of read.dist."); m->mothurOutEndLine(); 
+                                       if (cutOFF > rowCells[i]->dist) {  
+                                               cutOFF = rowCells[i]->dist;  
+                                               //m->mothurOut("changing cutoff to " + toString(cutOFF));  m->mothurOutEndLine(); 
+                                       }
 
                                }
                                removeCell(rowCells[i], i , -1);  
@@ -228,10 +261,13 @@ void Cluster::update(double& cutOFF){
                // could be avoided
                for (int i=nColCells-1;i>=0;i--) {
                        if (foundCol[i] == 0) {
-                               if (method == "average") {
+                               if (method == "average" || method == "weighted") {
                                        if (!((colCells[i]->row == smallRow) && (colCells[i]->column == smallCol))) {
-                                               mothurOut("Warning: merging cell " + toString(colCells[i]->row+1) + " " + toString(colCells[i]->column+1) + " distance " + toString(colCells[i]->dist) + " value above cutoff. Results may vary from using cutoff at cluster command instead of read.dist."); mothurOutEndLine();
-                                               if (cutOFF > colCells[i]->dist) {  cutOFF = colCells[i]->dist;  mothurOut("changing cutoff to " + toString(cutOFF));  mothurOutEndLine(); }
+                                               //m->mothurOut("Warning: merging cell " + toString(colCells[i]->row+1) + " " + toString(colCells[i]->column+1) + " distance " + toString(colCells[i]->dist) + " value above cutoff. Results may vary from using cutoff at cluster command instead of read.dist."); m->mothurOutEndLine();
+                                               if (cutOFF > colCells[i]->dist) {  
+                                                       cutOFF = colCells[i]->dist;  
+                                                       //m->mothurOut("changing cutoff to " + toString(cutOFF));  m->mothurOutEndLine(); 
+                                               }
                                        }
                                }
                                removeCell(colCells[i], -1, i);
@@ -239,14 +275,14 @@ void Cluster::update(double& cutOFF){
                }
        }
        catch(exception& e) {
-               errorOut(e, "Cluster", "update");
+               m->errorOut(e, "Cluster", "update");
                exit(1);
        }
 }
 /***********************************************************************/
-void Cluster::setMapWanted(bool m)  {  
+void Cluster::setMapWanted(bool f)  {  
        try {
-               mapWanted = m;
+               mapWanted = f;
                
                //initialize map
                for (int i = 0; i < list->getNumBins(); i++) {
@@ -267,7 +303,7 @@ void Cluster::setMapWanted(bool m)  {
                
        }
        catch(exception& e) {
-               errorOut(e, "Cluster", "setMapWanted");
+               m->errorOut(e, "Cluster", "setMapWanted");
                exit(1);
        }
 }
@@ -289,7 +325,7 @@ try {
                
        }
        catch(exception& e) {
-               errorOut(e, "Cluster", "updateMap");
+               m->errorOut(e, "Cluster", "updateMap");
                exit(1);
        }
 }