5 * Created by westcott on 10/13/09.
6 * Copyright 2009 Schloss Lab. All rights reserved.
11 #include "rabundvector.hpp"
12 #include "listvector.hpp"
13 #include "sparsematrix.hpp"
15 /***********************************************************************/
17 HCluster::HCluster(RAbundVector* rav, ListVector* lv) : rabund(rav), list(lv){
20 numSeqs = list->getNumSeqs();
22 //initialize cluster array
23 for (int i = 0; i < numSeqs; i++) {
24 clusterNode temp(1, -1, i);
25 clusterArray.push_back(temp);
30 errorOut(e, "HCluster", "HCluster");
34 /***********************************************************************/
36 void HCluster::clusterBins(){
38 //cout << smallCol << '\t' << smallRow << '\t' << smallDist << '\t' << rabund->get(clusterArray[smallRow].smallChild) << '\t' << rabund->get(clusterArray[smallCol].smallChild);
40 rabund->set(clusterArray[smallCol].smallChild, rabund->get(clusterArray[smallRow].smallChild)+rabund->get(clusterArray[smallCol].smallChild));
41 rabund->set(clusterArray[smallRow].smallChild, 0);
42 rabund->setLabel(toString(smallDist));
44 //cout << '\t' << rabund->get(clusterArray[smallRow].smallChild) << '\t' << rabund->get(clusterArray[smallCol].smallChild) << endl;
47 errorOut(e, "HCluster", "clusterBins");
54 /***********************************************************************/
56 void HCluster::clusterNames(){
58 ///cout << smallCol << '\t' << smallRow << '\t' << smallDist << '\t' << list->get(clusterArray[smallRow].smallChild) << '\t' << list->get(clusterArray[smallCol].smallChild);
60 list->set(clusterArray[smallCol].smallChild, list->get(clusterArray[smallRow].smallChild)+','+list->get(clusterArray[smallCol].smallChild));
61 list->set(clusterArray[smallRow].smallChild, "");
62 list->setLabel(toString(smallDist));
64 //cout << '\t' << list->get(clusterArray[smallRow].smallChild) << '\t' << list->get(clusterArray[smallCol].smallChild) << endl;
68 errorOut(e, "HCluster", "clusterNames");
73 /***********************************************************************/
74 int HCluster::getUpmostParent(int node){
77 while (clusterArray[node].parent != -1) {
78 node = clusterArray[node].parent;
84 errorOut(e, "HCluster", "getUpmostParent");
88 /***********************************************************************/
89 void HCluster::printInfo(){
92 cout << "link table" << endl;
93 for (it = activeLinks.begin(); it!= activeLinks.end(); it++) {
94 cout << it->first << " = " << it->second << endl;
97 for (int i = 0; i < linkTable.size(); i++) {
98 for (it = linkTable[i].begin(); it != linkTable[i].end(); it++) {
99 cout << it->first << '\t' << it->second << '\t' << '\t';
103 cout << endl << "clusterArray" << endl;
105 for (int i = 0; i < clusterArray.size(); i++) {
106 cout << i << '\t' << clusterArray[i].numSeq << '\t' << clusterArray[i].parent << '\t' << clusterArray[i].smallChild << endl;
112 catch(exception& e) {
113 errorOut(e, "HCluster", "getUpmostParent");
117 /***********************************************************************/
118 int HCluster::makeActive() {
122 //cout << "active - here" << endl;
123 it = activeLinks.find(smallRow);
124 it2 = activeLinks.find(smallCol);
126 if ((it == activeLinks.end()) && (it2 == activeLinks.end())) { //both are not active so add them
127 int size = linkTable.size();
128 map<int, int> temp; map<int, int> temp2;
130 //add link to eachother
131 temp[smallRow] = 1; // 1 2
132 temp2[smallCol] = 1; // 1 0 1
134 linkTable.push_back(temp);
135 linkTable.push_back(temp2);
138 activeLinks[smallRow] = size;
139 activeLinks[smallCol] = size+1;
140 //cout << "active - here1" << endl;
141 }else if ((it != activeLinks.end()) && (it2 == activeLinks.end())) { //smallRow is active, smallCol is not
142 int size = linkTable.size();
143 int alreadyActiveRow = it->second;
146 //add link to eachother
147 temp[smallRow] = 1; // 6 2 3 5
148 linkTable.push_back(temp); // 6 0 1 2 0
149 linkTable[alreadyActiveRow][smallCol] = 1; // 2 1 0 1 1
153 activeLinks[smallCol] = size;
154 //cout << "active - here2" << endl;
155 }else if ((it == activeLinks.end()) && (it2 != activeLinks.end())) { //smallCol is active, smallRow is not
156 int size = linkTable.size();
157 int alreadyActiveCol = it2->second;
160 //add link to eachother
161 temp[smallCol] = 1; // 6 2 3 5
162 linkTable.push_back(temp); // 6 0 1 2 0
163 linkTable[alreadyActiveCol][smallRow] = 1; // 2 1 0 1 1
167 activeLinks[smallRow] = size;
168 //cout << "active - here3" << endl;
169 }else { //both are active so add one
170 int row = it->second;
171 int col = it2->second;
172 //cout << row << '\t' << col << endl;
174 linkTable[row][smallCol]++;
175 linkTable[col][smallRow]++;
176 linkValue = linkTable[row][smallCol];
177 //cout << "active - here4" << endl;
182 catch(exception& e) {
183 errorOut(e, "HCluster", "makeActive");
187 /***********************************************************************/
188 void HCluster::updateArrayandLinkTable() {
190 //if cluster was made update clusterArray and linkTable
191 int size = clusterArray.size();
194 clusterNode temp(clusterArray[smallRow].numSeq + clusterArray[smallCol].numSeq, -1, clusterArray[smallCol].smallChild);
195 clusterArray.push_back(temp);
198 clusterArray[smallRow].parent = size;
199 clusterArray[smallCol].parent = size;
201 //update linkTable by merging clustered rows and columns
202 int rowSpot = activeLinks[smallRow];
203 int colSpot = activeLinks[smallCol];
204 //cout << "here" << endl;
206 for (int i = 0; i < linkTable.size(); i++) {
207 //check if they are in map
208 it = linkTable[i].find(smallRow);
209 it2 = linkTable[i].find(smallCol);
211 if ((it!=linkTable[i].end()) && (it2!=linkTable[i].end())) { //they are both there
212 linkTable[i][size] = linkTable[i][smallRow]+linkTable[i][smallCol];
213 linkTable[i].erase(smallCol); //delete col row
214 linkTable[i].erase(smallRow); //delete col row
215 }else if ((it==linkTable[i].end()) && (it2!=linkTable[i].end())) { //only col
216 linkTable[i][size] = linkTable[i][smallCol];
217 linkTable[i].erase(smallCol); //delete col
218 }else if ((it!=linkTable[i].end()) && (it2==linkTable[i].end())) { //only row
219 linkTable[i][size] = linkTable[i][smallRow];
220 linkTable[i].erase(smallRow); //delete col
224 //cout << "here2" << endl;
226 for (it = linkTable[rowSpot].begin(); it != linkTable[rowSpot].end(); it++) {
227 it2 = linkTable[colSpot].find(it->first); //does the col also have this
229 if (it2 == linkTable[colSpot].end()) { //not there so add it
230 linkTable[colSpot][it->first] = it->second;
232 linkTable[colSpot][it->first] = it->second+it2->second;
235 //cout << "here3" << endl;
236 linkTable[colSpot].erase(size);
237 linkTable.erase(linkTable.begin()+rowSpot); //delete col
240 activeLinks.erase(smallRow);
241 activeLinks.erase(smallCol);
243 if(rowSpot>colSpot) { activeLinks[size] = colSpot; }
244 else{ activeLinks[size] = colSpot-1; }
246 //adjust everybody elses spot since you deleted - time vs. space
247 for (it = activeLinks.begin(); it != activeLinks.end(); it++) {
248 if (it->second > rowSpot) { activeLinks[it->first]--; }
252 //cout << "here4" << endl;
255 catch(exception& e) {
256 errorOut(e, "HCluster", "updateArrayandLinkTable");
260 /***********************************************************************/
261 bool HCluster::update(int row, int col, float distance){
266 smallDist = distance;
267 bool clustered = false;
269 //find upmost parent of row and col
270 smallRow = getUpmostParent(smallRow);
271 smallCol = getUpmostParent(smallCol);
272 //cout << "smallRow = " << smallRow << " smallCol = " << smallCol << endl;
274 //are they active in the link table
275 int linkValue = makeActive(); //after this point this nodes info is active in linkTable
277 //cout << "linkValue = " << linkValue << " times = " << (clusterArray[smallRow].numSeq * clusterArray[smallCol].numSeq) << endl;
279 if (linkValue == (clusterArray[smallRow].numSeq * clusterArray[smallCol].numSeq)) {
280 updateArrayandLinkTable();
290 catch(exception& e) {
291 errorOut(e, "HCluster", "update");
297 /***********************************************************************/