]> git.donarmstrong.com Git - mothur.git/blobdiff - cluster.cpp
added parse.list command
[mothur.git] / cluster.cpp
index 6fd463116e65becc0ef2a969c08d5bfaf6dfea15..c6a9ca4059b0fafb4dc2529ccf12bf3f134eb69e 100644 (file)
@@ -14,8 +14,8 @@
 
 /***********************************************************************/
 
-Cluster::Cluster(RAbundVector* rav, ListVector* lv, SparseMatrix* dm) :
-rabund(rav), list(lv), dMatrix(dm)
+Cluster::Cluster(RAbundVector* rav, ListVector* lv, SparseMatrix* dm, float c, string m) :
+rabund(rav), list(lv), dMatrix(dm), method(m)
 {
 /*
        cout << "sizeof(MatData): " << sizeof(MatData) << endl;
@@ -55,6 +55,10 @@ rabund(rav), list(lv), dMatrix(dm)
                seqVec[currentCell->row].push_back(currentCell);
                seqVec[currentCell->column].push_back(currentCell);
        }
+       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;
 }
 
 /***********************************************************************/
@@ -78,7 +82,7 @@ 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)
@@ -150,7 +154,8 @@ void Cluster::clusterBins(){
 void Cluster::clusterNames(){
        try {
        //      cout << smallCol << '\t' << smallRow << '\t' << smallDist << '\t' << list->get(smallRow) << '\t' << list->get(smallCol);
-
+               if (mapWanted) {  updateMap();  }
+               
                list->set(smallCol, list->get(smallRow)+','+list->get(smallCol));
                list->set(smallRow, "");        
                list->setLabel(toString(smallDist));
@@ -168,11 +173,12 @@ void Cluster::clusterNames(){
 //This function clusters based on the method of the derived class
 //At the moment only average and complete linkage are covered, because
 //single linkage uses a different approach.
-void Cluster::update(){
+void Cluster::update(double& cutOFF){
        try {
                getRowColCells();       
        
-               vector<int> found(nColCells, 0);
+               vector<int> foundCol(nColCells, 0);
+
                int search;
                bool changed;
 
@@ -185,11 +191,13 @@ void Cluster::update(){
                                } else {
                                        search = rowCells[i]->row;
                                }
-               
+                               
+                               bool merged = false;
                                for (int j=0;j<nColCells;j++) {
-                                       if (!((colCells[j]->row == smallRow) && (colCells[j]->column == smallCol))) {
+                                       if (!((colCells[j]->row == smallRow) && (colCells[j]->column == smallCol))) { //if you are not hte smallest distance
                                                if (colCells[j]->row == search || colCells[j]->column == search) {
-                                                       found[j] = 1;
+                                                       foundCol[j] = 1;
+                                                       merged = true;
                                                        changed = updateDistance(colCells[j], rowCells[i]);
                                                        // If the cell's distance changed and it had the same distance as 
                                                        // the smallest distance, invalidate the mins vector in SparseMatrix
@@ -201,9 +209,19 @@ void Cluster::update(){
                                                        }
                                                        break;
                                                }
+                                       }               
+                               }
+                               //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(); 
                                        }
+
                                }
-                               removeCell(rowCells[i], i , -1);
+                               removeCell(rowCells[i], i , -1);  
+                               
                        }
                }
                clusterBins();
@@ -212,7 +230,16 @@ void Cluster::update(){
                // Special handling for singlelinkage case, not sure whether this
                // could be avoided
                for (int i=nColCells-1;i>=0;i--) {
-                       if (found[i] == 0) {
+                       if (foundCol[i] == 0) {
+                               if (method == "average") {
+                                       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(); 
+                                               }
+                                       }
+                               }
                                removeCell(colCells[i], -1, i);
                        }
                }
@@ -222,7 +249,57 @@ void Cluster::update(){
                exit(1);
        }
 }
+/***********************************************************************/
+void Cluster::setMapWanted(bool m)  {  
+       try {
+               mapWanted = m;
+               
+               //initialize map
+               for (int i = 0; i < list->getNumBins(); i++) {
+                       
+                       //parse bin 
+                       string names = list->get(i);
+                       while (names.find_first_of(',') != -1) { 
+                               //get name from bin
+                               string name = names.substr(0,names.find_first_of(','));
+                               //save name and bin number
+                               seq2Bin[name] = i;
+                               names = names.substr(names.find_first_of(',')+1, names.length());
+                       }
+                       
+                       //get last name
+                       seq2Bin[names] = i;
+               }
+               
+       }
+       catch(exception& e) {
+               errorOut(e, "Cluster", "setMapWanted");
+               exit(1);
+       }
+}
+/***********************************************************************/
+void Cluster::updateMap() {
+try {
+               //update location of seqs in smallRow since they move to smallCol now
+               string names = list->get(smallRow);
+               while (names.find_first_of(',') != -1) { 
+                       //get name from bin
+                       string name = names.substr(0,names.find_first_of(','));
+                       //save name and bin number
+                       seq2Bin[name] = smallCol;
+                       names = names.substr(names.find_first_of(',')+1, names.length());
+               }
+                       
+               //get last name
+               seq2Bin[names] = smallCol;
+               
+       }
+       catch(exception& e) {
+               errorOut(e, "Cluster", "updateMap");
+               exit(1);
+       }
+}
+/***********************************************************************/
 
 
-/***********************************************************************/