5 * Created by westcott on 10/29/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "clusterclassic.h"
11 #include "progress.hpp"
13 /***********************************************************************/
14 ClusterClassic::ClusterClassic(float c, string f) : method(f), smallDist(1e6), nseqs(0) {
16 mapWanted = false; //set to true by mgcluster to speed up overlap merge
18 //save so you can modify as it changes in average neighbor
20 aboveCutoff = cutoff + 10000.0;
21 m = MothurOut::getInstance();
22 globaldata = GlobalData::getInstance();
25 m->errorOut(e, "ClusterClassic", "ClusterClassic");
29 /***********************************************************************/
30 int ClusterClassic::readPhylipFile(string filename, NameAssignment* nameMap) {
35 vector<string> matrixNames;
38 m->openInputFile(filename, fileHandle);
40 fileHandle >> nseqs >> name;
42 matrixNames.push_back(name);
45 list = new ListVector(nseqs);
49 list = new ListVector(nameMap->getListVector());
50 if(nameMap->count(name)==0){ m->mothurOut("Error: Sequence '" + name + "' was not found in the names file, please correct"); m->mothurOutEndLine(); }
53 //initialize distance matrix to cutoff
54 dMatrix.resize(nseqs);
55 //colDist temp(0, 0, aboveCutoff);
56 //rowSmallDists.resize(nseqs, temp);
57 for (int i = 1; i < nseqs; i++) {
58 dMatrix[i].resize(i, aboveCutoff);
63 while((d=fileHandle.get()) != EOF){
67 fileHandle.putback(d);
68 for(int i=0;i<nseqs;i++){
69 fileHandle >> distance;
83 reading = new Progress("Reading matrix: ", nseqs * (nseqs - 1) / 2);
87 for(int i=1;i<nseqs;i++){
88 if (m->control_pressed) { fileHandle.close(); delete reading; return 0; }
91 matrixNames.push_back(name);
94 //there's A LOT of repeated code throughout this method...
100 if (m->control_pressed) { delete reading; fileHandle.close(); return 0; }
102 fileHandle >> distance;
104 if (distance == -1) { distance = 1000000; }
105 else if (globaldata->sim) { distance = 1.0 - distance; } //user has entered a sim matrix that we need to convert.
107 //if(distance < cutoff){
108 dMatrix[i][j] = distance;
109 if (distance < smallDist) { smallDist = distance; }
110 //if (rowSmallDists[i].dist > distance) { rowSmallDists[i].dist = distance; rowSmallDists[i].col = j; rowSmallDists[i].row = i; }
111 //if (rowSmallDists[j].dist > distance) { rowSmallDists[j].dist = distance; rowSmallDists[j].col = i; rowSmallDists[j].row = j; }
114 reading->update(index);
119 if(nameMap->count(name)==0){ m->mothurOut("Error: Sequence '" + name + "' was not found in the names file, please correct"); m->mothurOutEndLine(); }
121 for(int j=0;j<i;j++){
122 fileHandle >> distance;
124 if (m->control_pressed) { delete reading; fileHandle.close(); return 0; }
126 if (distance == -1) { distance = 1000000; }
127 else if (globaldata->sim) { distance = 1.0 - distance; } //user has entered a sim matrix that we need to convert.
129 //if(distance < cutoff){
130 if (distance < smallDist) { smallDist = distance; }
132 int row = nameMap->get(matrixNames[i]);
133 int col = nameMap->get(matrixNames[j]);
135 if (row < col) { dMatrix[col][row] = distance; }
136 else { dMatrix[row][col] = distance; }
138 //if (rowSmallDists[row].dist > distance) { rowSmallDists[row].dist = distance; rowSmallDists[row].col = col; rowSmallDists[row].row = row; }
139 //if (rowSmallDists[col].dist > distance) { rowSmallDists[col].dist = distance; rowSmallDists[col].col = row; rowSmallDists[col].row = col; }
142 reading->update(index);
149 reading = new Progress("Reading matrix: ", nseqs * nseqs);
153 for(int i=1;i<nseqs;i++){
155 matrixNames.push_back(name);
159 for(int j=0;j<nseqs;j++){
160 fileHandle >> distance;
162 if (m->control_pressed) { fileHandle.close(); delete reading; return 0; }
164 if (distance == -1) { distance = 1000000; }
165 else if (globaldata->sim) { distance = 1.0 - distance; } //user has entered a sim matrix that we need to convert.
168 if (distance < smallDist) { smallDist = distance; }
170 dMatrix[i][j] = distance;
171 //if (rowSmallDists[i].dist > distance) { rowSmallDists[i].dist = distance; rowSmallDists[i].col = j; rowSmallDists[i].row = i; }
172 //if (rowSmallDists[j].dist > distance) { rowSmallDists[j].dist = distance; rowSmallDists[j].col = i; rowSmallDists[j].row = j; }
175 reading->update(index);
180 if(nameMap->count(name)==0){ m->mothurOut("Error: Sequence '" + name + "' was not found in the names file, please correct"); m->mothurOutEndLine(); }
182 for(int j=0;j<nseqs;j++){
183 fileHandle >> distance;
185 if (m->control_pressed) { fileHandle.close(); delete reading; return 0; }
187 if (distance == -1) { distance = 1000000; }
188 else if (globaldata->sim) { distance = 1.0 - distance; } //user has entered a sim matrix that we need to convert.
191 if (distance < smallDist) { smallDist = distance; }
193 int row = nameMap->get(matrixNames[i]);
194 int col = nameMap->get(matrixNames[j]);
196 if (row < col) { dMatrix[col][row] = distance; }
197 else { dMatrix[row][col] = distance; }
199 //if (rowSmallDists[row].dist > distance) { rowSmallDists[row].dist = distance; rowSmallDists[row].col = col; rowSmallDists[row].row = row; }
200 //if (rowSmallDists[col].dist > distance) { rowSmallDists[col].dist = distance; rowSmallDists[col].col = row; rowSmallDists[col].row = col; }
203 reading->update(index);
209 if (m->control_pressed) { fileHandle.close(); delete reading; return 0; }
215 rabund = new RAbundVector(list->getRAbundVector());
221 catch(exception& e) {
222 m->errorOut(e, "ClusterClassic", "readPhylipFile");
227 /***********************************************************************/
228 //sets smallCol and smallRow, returns distance
229 double ClusterClassic::getSmallCell() {
232 smallDist = aboveCutoff;
236 vector<colDist> mins;
238 for(int i=1;i<nseqs;i++){
239 for(int j=0;j<i;j++){
240 if (dMatrix[i][j] < smallDist) {
242 colDist temp(i, j, dMatrix[i][j]);
243 mins.push_back(temp);
244 smallDist = dMatrix[i][j];
245 }else if (dMatrix[i][j] == smallDist) {
246 colDist temp(i, j, dMatrix[i][j]);
247 mins.push_back(temp);
253 if (mins.size() > 0) {
255 if (mins.size() > 1) {
256 //pick random number between 0 and mins.size()
257 zrand = (int)((float)(rand()) / (RAND_MAX / (mins.size()-1) + 1));
260 smallRow = mins[zrand].row;
261 smallCol = mins[zrand].col;
264 //cout << smallRow << '\t' << smallCol << '\t' << smallDist << endl;
265 //eliminate smallCell
266 if (smallRow < smallCol) { dMatrix[smallCol][smallRow] = aboveCutoff; }
267 else { dMatrix[smallRow][smallCol] = aboveCutoff; }
272 catch(exception& e) {
273 m->errorOut(e, "ClusterClassic", "getSmallCell");
277 /***********************************************************************/
278 void ClusterClassic::clusterBins(){
280 // cout << smallCol << '\t' << smallRow << '\t' << smallDist << '\t' << rabund->get(smallRow) << '\t' << rabund->get(smallCol);
282 rabund->set(smallRow, rabund->get(smallRow)+rabund->get(smallCol));
283 rabund->set(smallCol, 0);
284 /*for (int i = smallCol+1; i < rabund->size(); i++) {
285 rabund->set((i-1), rabund->get(i));
287 rabund->resize((rabund->size()-1));*/
288 rabund->setLabel(toString(smallDist));
290 // cout << '\t' << rabund->get(smallRow) << '\t' << rabund->get(smallCol) << endl;
292 catch(exception& e) {
293 m->errorOut(e, "ClusterClassic", "clusterBins");
297 /***********************************************************************/
298 void ClusterClassic::clusterNames(){
300 // cout << smallCol << '\t' << smallRow << '\t' << smallDist << '\t' << list->get(smallRow) << '\t' << list->get(smallCol);
301 if (mapWanted) { updateMap(); }
303 list->set(smallRow, list->get(smallRow)+','+list->get(smallCol));
304 list->set(smallCol, "");
305 /*for (int i = smallCol+1; i < list->size(); i++) {
306 list->set((i-1), list->get(i));
308 list->resize((list->size()-1));*/
309 list->setLabel(toString(smallDist));
311 // cout << '\t' << list->get(smallRow) << '\t' << list->get(smallCol) << endl;
313 catch(exception& e) {
314 m->errorOut(e, "ClusterClassic", "clusterNames");
318 /***********************************************************************/
319 void ClusterClassic::update(double& cutOFF){
325 r = smallRow; c = smallCol;
327 for(int i=0;i<nseqs;i++){
328 if(i != r && i != c){
329 double distRow, distCol, newDist;
330 if (i > r) { distRow = dMatrix[i][r]; }
331 else { distRow = dMatrix[r][i]; }
333 if (i > c) { distCol = dMatrix[i][c]; dMatrix[i][c] = aboveCutoff; } //like removeCell
334 else { distCol = dMatrix[c][i]; dMatrix[c][i] = aboveCutoff; }
336 if(method == "furthest"){
337 newDist = max(distRow, distCol);
339 else if (method == "average"){
340 int rowBin = rabund->get(r);
341 int colBin = rabund->get(c);
342 newDist = (colBin * distCol + rowBin * distRow) / (rowBin + colBin);
344 else if (method == "weighted"){
345 newDist = (distCol + distRow) / 2.0;
347 else if (method == "nearest"){
348 newDist = min(distRow, distCol);
350 //cout << "newDist = " << newDist << endl;
351 if (i > r) { dMatrix[i][r] = newDist; }
352 else { dMatrix[r][i] = newDist; }
361 /*for(int i=0;i<nseqs;i++){
362 for(int j=c+1;j<dMatrix[i].size();j++){
363 dMatrix[i][j-1]=dMatrix[i][j];
368 for(int i=c+1;i<nseqs;i++){
369 for(int j=0;j < dMatrix[i-1].size();j++){
370 dMatrix[i-1][j]=dMatrix[i][j];
375 dMatrix.pop_back();*/
378 catch(exception& e) {
379 m->errorOut(e, "ClusterClassic", "update");
383 /***********************************************************************/
384 void ClusterClassic::setMapWanted(bool f) {
389 for (int i = 0; i < list->getNumBins(); i++) {
392 string names = list->get(i);
393 while (names.find_first_of(',') != -1) {
395 string name = names.substr(0,names.find_first_of(','));
396 //save name and bin number
398 names = names.substr(names.find_first_of(',')+1, names.length());
406 catch(exception& e) {
407 m->errorOut(e, "ClusterClassic", "setMapWanted");
411 /***********************************************************************/
412 void ClusterClassic::updateMap() {
414 //update location of seqs in smallRow since they move to smallCol now
415 string names = list->get(smallRow);
416 while (names.find_first_of(',') != -1) {
418 string name = names.substr(0,names.find_first_of(','));
419 //save name and bin number
420 seq2Bin[name] = smallCol;
421 names = names.substr(names.find_first_of(',')+1, names.length());
425 seq2Bin[names] = smallCol;
428 catch(exception& e) {
429 m->errorOut(e, "ClusterClassic", "updateMap");
433 /***********************************************************************/
434 void ClusterClassic::print() {
436 //update location of seqs in smallRow since they move to smallCol now
437 for (int i = 0; i < dMatrix.size(); i++) {
438 cout << "row = " << i << '\t';
439 for (int j = 0; j < dMatrix[i].size(); j++) {
440 cout << dMatrix[i][j] << '\t';
445 catch(exception& e) {
446 m->errorOut(e, "ClusterClassic", "updateMap");
450 /***********************************************************************/