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, float cs) :
17 rabund(rav), list(lv), dMatrix(dm), method(f), adjust(cs)
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) { //we don't have a distance for this cell
89 if (adjust != -1.0) { //adjust
91 PDistCell value(search, adjust); //create a distance for the missing value
92 int location = dMatrix->addCellSorted(smallCol, value);
93 changed = updateDistance(dMatrix->seqVec[smallCol][location], dMatrix->seqVec[smallRow][i]);
94 dMatrix->updateCellCompliment(smallCol, location);
96 foundCol.push_back(0); //add a new found column
98 for (int k = foundCol.size()-1; k > location; k--) { foundCol[k] = foundCol[k-1]; }
99 foundCol[location] = 1;
105 //if not merged it you need it for warning
106 if ((!merged) && (method == "average" || method == "weighted")) {
107 if (cutOFF > dMatrix->seqVec[smallRow][i].dist) {
108 cutOFF = dMatrix->seqVec[smallRow][i].dist;
109 //cout << "changing cutoff to " << cutOFF << endl;
113 dMatrix->rmCell(smallRow, i);
119 // Special handling for singlelinkage case, not sure whether this
121 for (int i=nColCells-1;i>=0;i--) {
122 if (foundCol[i] == 0) {
123 if (adjust != -1.0) { //adjust
124 PDistCell value(smallCol, adjust); //create a distance for the missing value
125 changed = updateDistance(dMatrix->seqVec[smallCol][i], value);
126 dMatrix->updateCellCompliment(smallCol, i);
128 if (method == "average" || method == "weighted") {
129 if (dMatrix->seqVec[smallCol][i].index != smallRow) { //if you are not hte smallest distance
130 if (cutOFF > dMatrix->seqVec[smallCol][i].dist) {
131 cutOFF = dMatrix->seqVec[smallCol][i].dist;
136 dMatrix->rmCell(smallCol, i);
141 catch(exception& e) {
142 m->errorOut(e, "Cluster", "update");
146 /***********************************************************************/
147 void Cluster::setMapWanted(bool f) {
152 for (int k = 0; k < list->getNumBins(); k++) {
154 string names = list->get(k);
157 string individual = "";
158 int binNameslength = names.size();
159 for(int j=0;j<binNameslength;j++){
161 seq2Bin[individual] = k;
164 else{ individual += names[j]; }
167 seq2Bin[individual] = k;
171 catch(exception& e) {
172 m->errorOut(e, "Cluster", "setMapWanted");
176 /***********************************************************************/
177 void Cluster::updateMap() {
179 //update location of seqs in smallRow since they move to smallCol now
180 string names = list->get(smallRow);
182 string individual = "";
183 int binNameslength = names.size();
184 for(int j=0;j<binNameslength;j++){
186 seq2Bin[individual] = smallCol;
189 else{ individual += names[j]; }
192 seq2Bin[individual] = smallCol;
195 catch(exception& e) {
196 m->errorOut(e, "Cluster", "updateMap");
200 /***********************************************************************/