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, bool s) : method(f), smallDist(1e6), nseqs(0), sim(s) {
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 if(method == "furthest") { tag = "fn"; }
23 else if (method == "average") { tag = "an"; }
24 else if (method == "weighted") { tag = "wn"; }
25 else if (method == "nearest") { tag = "nn"; }
28 m->errorOut(e, "ClusterClassic", "ClusterClassic");
32 /***********************************************************************/
33 int ClusterClassic::readPhylipFile(string filename, NameAssignment* nameMap) {
38 vector<string> matrixNames;
41 m->openInputFile(filename, fileHandle);
44 fileHandle >> numTest >> name;
46 if (!m->isContainingOnlyDigits(numTest)) { m->mothurOut("[ERROR]: expected a number and got " + numTest + ", quitting."); m->mothurOutEndLine(); exit(1); }
47 else { convert(numTest, nseqs); }
50 matrixNames.push_back(name);
53 list = new ListVector(nseqs);
57 list = new ListVector(nameMap->getListVector());
58 if(nameMap->count(name)==0){ m->mothurOut("Error: Sequence '" + name + "' was not found in the names file, please correct"); m->mothurOutEndLine(); }
61 //initialize distance matrix to cutoff
62 dMatrix.resize(nseqs);
63 //colDist temp(0, 0, aboveCutoff);
64 //rowSmallDists.resize(nseqs, temp);
65 for (int i = 1; i < nseqs; i++) {
66 dMatrix[i].resize(i, aboveCutoff);
71 while((d=fileHandle.get()) != EOF){
75 fileHandle.putback(d);
76 for(int i=0;i<nseqs;i++){
77 fileHandle >> distance;
91 reading = new Progress("Reading matrix: ", nseqs * (nseqs - 1) / 2);
95 for(int i=1;i<nseqs;i++){
96 if (m->control_pressed) { fileHandle.close(); delete reading; return 0; }
99 matrixNames.push_back(name);
102 //there's A LOT of repeated code throughout this method...
106 for(int j=0;j<i;j++){
108 if (m->control_pressed) { delete reading; fileHandle.close(); return 0; }
110 fileHandle >> distance;
112 if (distance == -1) { distance = 1000000; }
113 else if (sim) { distance = 1.0 - distance; } //user has entered a sim matrix that we need to convert.
115 //if(distance < cutoff){
116 dMatrix[i][j] = distance;
117 if (distance < smallDist) { smallDist = distance; }
118 //if (rowSmallDists[i].dist > distance) { rowSmallDists[i].dist = distance; rowSmallDists[i].col = j; rowSmallDists[i].row = i; }
119 //if (rowSmallDists[j].dist > distance) { rowSmallDists[j].dist = distance; rowSmallDists[j].col = i; rowSmallDists[j].row = j; }
122 reading->update(index);
127 if(nameMap->count(name)==0){ m->mothurOut("Error: Sequence '" + name + "' was not found in the names file, please correct"); m->mothurOutEndLine(); }
129 for(int j=0;j<i;j++){
130 fileHandle >> distance;
132 if (m->control_pressed) { delete reading; fileHandle.close(); return 0; }
134 if (distance == -1) { distance = 1000000; }
135 else if (sim) { distance = 1.0 - distance; } //user has entered a sim matrix that we need to convert.
137 //if(distance < cutoff){
138 if (distance < smallDist) { smallDist = distance; }
140 int row = nameMap->get(matrixNames[i]);
141 int col = nameMap->get(matrixNames[j]);
143 if (row < col) { dMatrix[col][row] = distance; }
144 else { dMatrix[row][col] = distance; }
146 //if (rowSmallDists[row].dist > distance) { rowSmallDists[row].dist = distance; rowSmallDists[row].col = col; rowSmallDists[row].row = row; }
147 //if (rowSmallDists[col].dist > distance) { rowSmallDists[col].dist = distance; rowSmallDists[col].col = row; rowSmallDists[col].row = col; }
150 reading->update(index);
157 reading = new Progress("Reading matrix: ", nseqs * nseqs);
161 for(int i=1;i<nseqs;i++){
163 matrixNames.push_back(name);
167 for(int j=0;j<nseqs;j++){
168 fileHandle >> distance;
170 if (m->control_pressed) { fileHandle.close(); delete reading; return 0; }
172 if (distance == -1) { distance = 1000000; }
173 else if (sim) { distance = 1.0 - distance; } //user has entered a sim matrix that we need to convert.
176 if (distance < smallDist) { smallDist = distance; }
178 dMatrix[i][j] = distance;
179 //if (rowSmallDists[i].dist > distance) { rowSmallDists[i].dist = distance; rowSmallDists[i].col = j; rowSmallDists[i].row = i; }
180 //if (rowSmallDists[j].dist > distance) { rowSmallDists[j].dist = distance; rowSmallDists[j].col = i; rowSmallDists[j].row = j; }
183 reading->update(index);
188 if(nameMap->count(name)==0){ m->mothurOut("Error: Sequence '" + name + "' was not found in the names file, please correct"); m->mothurOutEndLine(); }
190 for(int j=0;j<nseqs;j++){
191 fileHandle >> distance;
193 if (m->control_pressed) { fileHandle.close(); delete reading; return 0; }
195 if (distance == -1) { distance = 1000000; }
196 else if (sim) { distance = 1.0 - distance; } //user has entered a sim matrix that we need to convert.
199 if (distance < smallDist) { smallDist = distance; }
201 int row = nameMap->get(matrixNames[i]);
202 int col = nameMap->get(matrixNames[j]);
204 if (row < col) { dMatrix[col][row] = distance; }
205 else { dMatrix[row][col] = distance; }
207 //if (rowSmallDists[row].dist > distance) { rowSmallDists[row].dist = distance; rowSmallDists[row].col = col; rowSmallDists[row].row = row; }
208 //if (rowSmallDists[col].dist > distance) { rowSmallDists[col].dist = distance; rowSmallDists[col].col = row; rowSmallDists[col].row = col; }
211 reading->update(index);
217 if (m->control_pressed) { fileHandle.close(); delete reading; return 0; }
223 rabund = new RAbundVector(list->getRAbundVector());
229 catch(exception& e) {
230 m->errorOut(e, "ClusterClassic", "readPhylipFile");
235 /***********************************************************************/
236 //sets smallCol and smallRow, returns distance
237 double ClusterClassic::getSmallCell() {
240 smallDist = aboveCutoff;
244 vector<colDist> mins;
246 for(int i=1;i<nseqs;i++){
247 for(int j=0;j<i;j++){
248 if (dMatrix[i][j] < smallDist) {
250 colDist temp(i, j, dMatrix[i][j]);
251 mins.push_back(temp);
252 smallDist = dMatrix[i][j];
253 }else if (dMatrix[i][j] == smallDist) {
254 colDist temp(i, j, dMatrix[i][j]);
255 mins.push_back(temp);
261 if (mins.size() > 0) {
263 if (mins.size() > 1) {
264 //pick random number between 0 and mins.size()
265 zrand = (int)((float)(rand()) / (RAND_MAX / (mins.size()-1) + 1));
268 smallRow = mins[zrand].row;
269 smallCol = mins[zrand].col;
272 //cout << smallRow << '\t' << smallCol << '\t' << smallDist << endl;
273 //eliminate smallCell
274 if (smallRow < smallCol) { dMatrix[smallCol][smallRow] = aboveCutoff; }
275 else { dMatrix[smallRow][smallCol] = aboveCutoff; }
280 catch(exception& e) {
281 m->errorOut(e, "ClusterClassic", "getSmallCell");
285 /***********************************************************************/
286 void ClusterClassic::clusterBins(){
288 // cout << smallCol << '\t' << smallRow << '\t' << smallDist << '\t' << rabund->get(smallRow) << '\t' << rabund->get(smallCol);
290 rabund->set(smallRow, rabund->get(smallRow)+rabund->get(smallCol));
291 rabund->set(smallCol, 0);
292 /*for (int i = smallCol+1; i < rabund->size(); i++) {
293 rabund->set((i-1), rabund->get(i));
295 rabund->resize((rabund->size()-1));*/
296 rabund->setLabel(toString(smallDist));
298 // cout << '\t' << rabund->get(smallRow) << '\t' << rabund->get(smallCol) << endl;
300 catch(exception& e) {
301 m->errorOut(e, "ClusterClassic", "clusterBins");
305 /***********************************************************************/
306 void ClusterClassic::clusterNames(){
308 // cout << smallCol << '\t' << smallRow << '\t' << smallDist << '\t' << list->get(smallRow) << '\t' << list->get(smallCol);
309 if (mapWanted) { updateMap(); }
311 list->set(smallRow, list->get(smallRow)+','+list->get(smallCol));
312 list->set(smallCol, "");
313 /*for (int i = smallCol+1; i < list->size(); i++) {
314 list->set((i-1), list->get(i));
316 list->resize((list->size()-1));*/
317 list->setLabel(toString(smallDist));
319 // cout << '\t' << list->get(smallRow) << '\t' << list->get(smallCol) << endl;
321 catch(exception& e) {
322 m->errorOut(e, "ClusterClassic", "clusterNames");
326 /***********************************************************************/
327 void ClusterClassic::update(double& cutOFF){
333 r = smallRow; c = smallCol;
335 for(int i=0;i<nseqs;i++){
336 if(i != r && i != c){
337 double distRow, distCol, newDist;
338 if (i > r) { distRow = dMatrix[i][r]; }
339 else { distRow = dMatrix[r][i]; }
341 if (i > c) { distCol = dMatrix[i][c]; dMatrix[i][c] = aboveCutoff; } //like removeCell
342 else { distCol = dMatrix[c][i]; dMatrix[c][i] = aboveCutoff; }
344 if(method == "furthest"){
345 newDist = max(distRow, distCol);
347 else if (method == "average"){
348 int rowBin = rabund->get(r);
349 int colBin = rabund->get(c);
350 newDist = (colBin * distCol + rowBin * distRow) / (rowBin + colBin);
352 else if (method == "weighted"){
353 newDist = (distCol + distRow) / 2.0;
355 else if (method == "nearest"){
356 newDist = min(distRow, distCol);
358 //cout << "newDist = " << newDist << endl;
359 if (i > r) { dMatrix[i][r] = newDist; }
360 else { dMatrix[r][i] = newDist; }
369 /*for(int i=0;i<nseqs;i++){
370 for(int j=c+1;j<dMatrix[i].size();j++){
371 dMatrix[i][j-1]=dMatrix[i][j];
376 for(int i=c+1;i<nseqs;i++){
377 for(int j=0;j < dMatrix[i-1].size();j++){
378 dMatrix[i-1][j]=dMatrix[i][j];
383 dMatrix.pop_back();*/
386 catch(exception& e) {
387 m->errorOut(e, "ClusterClassic", "update");
391 /***********************************************************************/
392 void ClusterClassic::setMapWanted(bool f) {
397 for (int i = 0; i < list->getNumBins(); i++) {
400 string names = list->get(i);
401 while (names.find_first_of(',') != -1) {
403 string name = names.substr(0,names.find_first_of(','));
404 //save name and bin number
406 names = names.substr(names.find_first_of(',')+1, names.length());
414 catch(exception& e) {
415 m->errorOut(e, "ClusterClassic", "setMapWanted");
419 /***********************************************************************/
420 void ClusterClassic::updateMap() {
422 //update location of seqs in smallRow since they move to smallCol now
423 string names = list->get(smallRow);
424 while (names.find_first_of(',') != -1) {
426 string name = names.substr(0,names.find_first_of(','));
427 //save name and bin number
428 seq2Bin[name] = smallCol;
429 names = names.substr(names.find_first_of(',')+1, names.length());
433 seq2Bin[names] = smallCol;
436 catch(exception& e) {
437 m->errorOut(e, "ClusterClassic", "updateMap");
441 /***********************************************************************/
442 void ClusterClassic::print() {
444 //update location of seqs in smallRow since they move to smallCol now
445 for (int i = 0; i < dMatrix.size(); i++) {
446 m->mothurOut("row = " + toString(i) + "\t");
447 for (int j = 0; j < dMatrix[i].size(); j++) {
448 m->mothurOut(toString(dMatrix[i][j]) + "\t");
450 m->mothurOutEndLine();
453 catch(exception& e) {
454 m->errorOut(e, "ClusterClassic", "updateMap");
458 /***********************************************************************/