5 * Created by Pat Schloss on 8/14/08.
6 * Copyright 2008 Patrick D. Schloss. All rights reserved.
10 #include "cluster.hpp"
11 #include "rabundvector.hpp"
12 #include "listvector.hpp"
14 /***********************************************************************/
16 Cluster::Cluster(RAbundVector* rav, ListVector* lv, SparseDistanceMatrix* dm, float c, string f) :
17 rabund(rav), list(lv), dMatrix(dm), method(f)
21 mapWanted = false; //set to true by mgcluster to speed up overlap merge
23 //save so you can modify as it changes in average neighbor
25 m = MothurOut::getInstance();
28 m->errorOut(e, "Cluster", "Cluster");
32 /***********************************************************************/
33 void Cluster::clusterBins(){
35 rabund->set(smallCol, rabund->get(smallRow)+rabund->get(smallCol));
36 rabund->set(smallRow, 0);
37 rabund->setLabel(toString(smallDist));
40 m->errorOut(e, "Cluster", "clusterBins");
44 /***********************************************************************/
46 void Cluster::clusterNames(){
48 if (mapWanted) { updateMap(); }
50 list->set(smallCol, list->get(smallRow)+','+list->get(smallCol));
51 list->set(smallRow, "");
52 list->setLabel(toString(smallDist));
55 m->errorOut(e, "Cluster", "clusterNames");
59 /***********************************************************************/
60 void Cluster::update(double& cutOFF){
62 smallCol = dMatrix->getSmallestCell(smallRow);
63 nColCells = dMatrix->seqVec[smallCol].size();
64 nRowCells = dMatrix->seqVec[smallRow].size();
66 vector<int> foundCol(nColCells, 0);
67 //cout << dMatrix->getNNodes() << " small cell: " << smallRow << '\t' << smallCol << endl;
71 for (int i=nRowCells-1;i>=0;i--) {
72 if (m->control_pressed) { break; }
74 //if you are not the smallCell
75 if (dMatrix->seqVec[smallRow][i].index != smallCol) {
76 search = dMatrix->seqVec[smallRow][i].index;
79 for (int j=0;j<nColCells;j++) {
81 if (dMatrix->seqVec[smallCol][j].index != smallRow) { //if you are not the smallest distance
82 if (dMatrix->seqVec[smallCol][j].index == search) {
85 changed = updateDistance(dMatrix->seqVec[smallCol][j], dMatrix->seqVec[smallRow][i]);
86 dMatrix->updateCellCompliment(smallCol, j);
88 }else if (dMatrix->seqVec[smallCol][j].index < search) { j+=nColCells; } //we don't have a distance for this cell
91 //if not merged it you need it for warning
92 if ((!merged) && (method == "average" || method == "weighted")) {
93 if (cutOFF > dMatrix->seqVec[smallRow][i].dist) {
94 cutOFF = dMatrix->seqVec[smallRow][i].dist;
95 //cout << "changing cutoff to " << cutOFF << endl;
99 dMatrix->rmCell(smallRow, i);
105 // Special handling for singlelinkage case, not sure whether this
107 for (int i=nColCells-1;i>=0;i--) {
108 if (foundCol[i] == 0) {
109 if (method == "average" || method == "weighted") {
110 if (dMatrix->seqVec[smallCol][i].index != smallRow) { //if you are not hte smallest distance
111 if (cutOFF > dMatrix->seqVec[smallCol][i].dist) {
112 cutOFF = dMatrix->seqVec[smallCol][i].dist;
116 dMatrix->rmCell(smallCol, i);
121 catch(exception& e) {
122 m->errorOut(e, "Cluster", "update");
126 /***********************************************************************/
127 void Cluster::setMapWanted(bool f) {
132 for (int k = 0; k < list->getNumBins(); k++) {
134 string names = list->get(k);
137 string individual = "";
138 int binNameslength = names.size();
139 for(int j=0;j<binNameslength;j++){
141 seq2Bin[individual] = k;
144 else{ individual += names[j]; }
147 seq2Bin[individual] = k;
151 catch(exception& e) {
152 m->errorOut(e, "Cluster", "setMapWanted");
156 /***********************************************************************/
157 void Cluster::updateMap() {
159 //update location of seqs in smallRow since they move to smallCol now
160 string names = list->get(smallRow);
162 string individual = "";
163 int binNameslength = names.size();
164 for(int j=0;j<binNameslength;j++){
166 seq2Bin[individual] = smallCol;
169 else{ individual += names[j]; }
172 seq2Bin[individual] = smallCol;
175 catch(exception& e) {
176 m->errorOut(e, "Cluster", "updateMap");
180 /***********************************************************************/