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();
24 m->errorOut(e, "ClusterClassic", "ClusterClassic");
28 /***********************************************************************/
29 int ClusterClassic::readPhylipFile(string filename, NameAssignment* nameMap) {
34 vector<string> matrixNames;
37 m->openInputFile(filename, fileHandle);
39 fileHandle >> nseqs >> name;
41 matrixNames.push_back(name);
44 list = new ListVector(nseqs);
48 list = new ListVector(nameMap->getListVector());
49 if(nameMap->count(name)==0){ m->mothurOut("Error: Sequence '" + name + "' was not found in the names file, please correct"); m->mothurOutEndLine(); }
52 //initialize distance matrix to cutoff
53 dMatrix.resize(nseqs);
54 //colDist temp(0, 0, aboveCutoff);
55 //rowSmallDists.resize(nseqs, temp);
56 for (int i = 1; i < nseqs; i++) {
57 dMatrix[i].resize(i, aboveCutoff);
62 while((d=fileHandle.get()) != EOF){
66 fileHandle.putback(d);
67 for(int i=0;i<nseqs;i++){
68 fileHandle >> distance;
82 reading = new Progress("Reading matrix: ", nseqs * (nseqs - 1) / 2);
86 for(int i=1;i<nseqs;i++){
87 if (m->control_pressed) { fileHandle.close(); delete reading; return 0; }
90 matrixNames.push_back(name);
93 //there's A LOT of repeated code throughout this method...
99 if (m->control_pressed) { delete reading; fileHandle.close(); return 0; }
101 fileHandle >> distance;
103 if (distance == -1) { distance = 1000000; }
104 else if (sim) { distance = 1.0 - distance; } //user has entered a sim matrix that we need to convert.
106 //if(distance < cutoff){
107 dMatrix[i][j] = distance;
108 if (distance < smallDist) { smallDist = distance; }
109 //if (rowSmallDists[i].dist > distance) { rowSmallDists[i].dist = distance; rowSmallDists[i].col = j; rowSmallDists[i].row = i; }
110 //if (rowSmallDists[j].dist > distance) { rowSmallDists[j].dist = distance; rowSmallDists[j].col = i; rowSmallDists[j].row = j; }
113 reading->update(index);
118 if(nameMap->count(name)==0){ m->mothurOut("Error: Sequence '" + name + "' was not found in the names file, please correct"); m->mothurOutEndLine(); }
120 for(int j=0;j<i;j++){
121 fileHandle >> distance;
123 if (m->control_pressed) { delete reading; fileHandle.close(); return 0; }
125 if (distance == -1) { distance = 1000000; }
126 else if (sim) { distance = 1.0 - distance; } //user has entered a sim matrix that we need to convert.
128 //if(distance < cutoff){
129 if (distance < smallDist) { smallDist = distance; }
131 int row = nameMap->get(matrixNames[i]);
132 int col = nameMap->get(matrixNames[j]);
134 if (row < col) { dMatrix[col][row] = distance; }
135 else { dMatrix[row][col] = distance; }
137 //if (rowSmallDists[row].dist > distance) { rowSmallDists[row].dist = distance; rowSmallDists[row].col = col; rowSmallDists[row].row = row; }
138 //if (rowSmallDists[col].dist > distance) { rowSmallDists[col].dist = distance; rowSmallDists[col].col = row; rowSmallDists[col].row = col; }
141 reading->update(index);
148 reading = new Progress("Reading matrix: ", nseqs * nseqs);
152 for(int i=1;i<nseqs;i++){
154 matrixNames.push_back(name);
158 for(int j=0;j<nseqs;j++){
159 fileHandle >> distance;
161 if (m->control_pressed) { fileHandle.close(); delete reading; return 0; }
163 if (distance == -1) { distance = 1000000; }
164 else if (sim) { distance = 1.0 - distance; } //user has entered a sim matrix that we need to convert.
167 if (distance < smallDist) { smallDist = distance; }
169 dMatrix[i][j] = distance;
170 //if (rowSmallDists[i].dist > distance) { rowSmallDists[i].dist = distance; rowSmallDists[i].col = j; rowSmallDists[i].row = i; }
171 //if (rowSmallDists[j].dist > distance) { rowSmallDists[j].dist = distance; rowSmallDists[j].col = i; rowSmallDists[j].row = j; }
174 reading->update(index);
179 if(nameMap->count(name)==0){ m->mothurOut("Error: Sequence '" + name + "' was not found in the names file, please correct"); m->mothurOutEndLine(); }
181 for(int j=0;j<nseqs;j++){
182 fileHandle >> distance;
184 if (m->control_pressed) { fileHandle.close(); delete reading; return 0; }
186 if (distance == -1) { distance = 1000000; }
187 else if (sim) { distance = 1.0 - distance; } //user has entered a sim matrix that we need to convert.
190 if (distance < smallDist) { smallDist = distance; }
192 int row = nameMap->get(matrixNames[i]);
193 int col = nameMap->get(matrixNames[j]);
195 if (row < col) { dMatrix[col][row] = distance; }
196 else { dMatrix[row][col] = distance; }
198 //if (rowSmallDists[row].dist > distance) { rowSmallDists[row].dist = distance; rowSmallDists[row].col = col; rowSmallDists[row].row = row; }
199 //if (rowSmallDists[col].dist > distance) { rowSmallDists[col].dist = distance; rowSmallDists[col].col = row; rowSmallDists[col].row = col; }
202 reading->update(index);
208 if (m->control_pressed) { fileHandle.close(); delete reading; return 0; }
214 rabund = new RAbundVector(list->getRAbundVector());
220 catch(exception& e) {
221 m->errorOut(e, "ClusterClassic", "readPhylipFile");
226 /***********************************************************************/
227 //sets smallCol and smallRow, returns distance
228 double ClusterClassic::getSmallCell() {
231 smallDist = aboveCutoff;
235 vector<colDist> mins;
237 for(int i=1;i<nseqs;i++){
238 for(int j=0;j<i;j++){
239 if (dMatrix[i][j] < smallDist) {
241 colDist temp(i, j, dMatrix[i][j]);
242 mins.push_back(temp);
243 smallDist = dMatrix[i][j];
244 }else if (dMatrix[i][j] == smallDist) {
245 colDist temp(i, j, dMatrix[i][j]);
246 mins.push_back(temp);
252 if (mins.size() > 0) {
254 if (mins.size() > 1) {
255 //pick random number between 0 and mins.size()
256 zrand = (int)((float)(rand()) / (RAND_MAX / (mins.size()-1) + 1));
259 smallRow = mins[zrand].row;
260 smallCol = mins[zrand].col;
263 //cout << smallRow << '\t' << smallCol << '\t' << smallDist << endl;
264 //eliminate smallCell
265 if (smallRow < smallCol) { dMatrix[smallCol][smallRow] = aboveCutoff; }
266 else { dMatrix[smallRow][smallCol] = aboveCutoff; }
271 catch(exception& e) {
272 m->errorOut(e, "ClusterClassic", "getSmallCell");
276 /***********************************************************************/
277 void ClusterClassic::clusterBins(){
279 // cout << smallCol << '\t' << smallRow << '\t' << smallDist << '\t' << rabund->get(smallRow) << '\t' << rabund->get(smallCol);
281 rabund->set(smallRow, rabund->get(smallRow)+rabund->get(smallCol));
282 rabund->set(smallCol, 0);
283 /*for (int i = smallCol+1; i < rabund->size(); i++) {
284 rabund->set((i-1), rabund->get(i));
286 rabund->resize((rabund->size()-1));*/
287 rabund->setLabel(toString(smallDist));
289 // cout << '\t' << rabund->get(smallRow) << '\t' << rabund->get(smallCol) << endl;
291 catch(exception& e) {
292 m->errorOut(e, "ClusterClassic", "clusterBins");
296 /***********************************************************************/
297 void ClusterClassic::clusterNames(){
299 // cout << smallCol << '\t' << smallRow << '\t' << smallDist << '\t' << list->get(smallRow) << '\t' << list->get(smallCol);
300 if (mapWanted) { updateMap(); }
302 list->set(smallRow, list->get(smallRow)+','+list->get(smallCol));
303 list->set(smallCol, "");
304 /*for (int i = smallCol+1; i < list->size(); i++) {
305 list->set((i-1), list->get(i));
307 list->resize((list->size()-1));*/
308 list->setLabel(toString(smallDist));
310 // cout << '\t' << list->get(smallRow) << '\t' << list->get(smallCol) << endl;
312 catch(exception& e) {
313 m->errorOut(e, "ClusterClassic", "clusterNames");
317 /***********************************************************************/
318 void ClusterClassic::update(double& cutOFF){
324 r = smallRow; c = smallCol;
326 for(int i=0;i<nseqs;i++){
327 if(i != r && i != c){
328 double distRow, distCol, newDist;
329 if (i > r) { distRow = dMatrix[i][r]; }
330 else { distRow = dMatrix[r][i]; }
332 if (i > c) { distCol = dMatrix[i][c]; dMatrix[i][c] = aboveCutoff; } //like removeCell
333 else { distCol = dMatrix[c][i]; dMatrix[c][i] = aboveCutoff; }
335 if(method == "furthest"){
336 newDist = max(distRow, distCol);
338 else if (method == "average"){
339 int rowBin = rabund->get(r);
340 int colBin = rabund->get(c);
341 newDist = (colBin * distCol + rowBin * distRow) / (rowBin + colBin);
343 else if (method == "weighted"){
344 newDist = (distCol + distRow) / 2.0;
346 else if (method == "nearest"){
347 newDist = min(distRow, distCol);
349 //cout << "newDist = " << newDist << endl;
350 if (i > r) { dMatrix[i][r] = newDist; }
351 else { dMatrix[r][i] = newDist; }
360 /*for(int i=0;i<nseqs;i++){
361 for(int j=c+1;j<dMatrix[i].size();j++){
362 dMatrix[i][j-1]=dMatrix[i][j];
367 for(int i=c+1;i<nseqs;i++){
368 for(int j=0;j < dMatrix[i-1].size();j++){
369 dMatrix[i-1][j]=dMatrix[i][j];
374 dMatrix.pop_back();*/
377 catch(exception& e) {
378 m->errorOut(e, "ClusterClassic", "update");
382 /***********************************************************************/
383 void ClusterClassic::setMapWanted(bool f) {
388 for (int i = 0; i < list->getNumBins(); i++) {
391 string names = list->get(i);
392 while (names.find_first_of(',') != -1) {
394 string name = names.substr(0,names.find_first_of(','));
395 //save name and bin number
397 names = names.substr(names.find_first_of(',')+1, names.length());
405 catch(exception& e) {
406 m->errorOut(e, "ClusterClassic", "setMapWanted");
410 /***********************************************************************/
411 void ClusterClassic::updateMap() {
413 //update location of seqs in smallRow since they move to smallCol now
414 string names = list->get(smallRow);
415 while (names.find_first_of(',') != -1) {
417 string name = names.substr(0,names.find_first_of(','));
418 //save name and bin number
419 seq2Bin[name] = smallCol;
420 names = names.substr(names.find_first_of(',')+1, names.length());
424 seq2Bin[names] = smallCol;
427 catch(exception& e) {
428 m->errorOut(e, "ClusterClassic", "updateMap");
432 /***********************************************************************/
433 void ClusterClassic::print() {
435 //update location of seqs in smallRow since they move to smallCol now
436 for (int i = 0; i < dMatrix.size(); i++) {
437 m->mothurOut("row = " + toString(i) + "\t");
438 for (int j = 0; j < dMatrix[i].size(); j++) {
439 m->mothurOut(toString(dMatrix[i][j]) + "\t");
441 m->mothurOutEndLine();
444 catch(exception& e) {
445 m->errorOut(e, "ClusterClassic", "updateMap");
449 /***********************************************************************/