]> git.donarmstrong.com Git - mothur.git/blob - averagelinkage.cpp
Initial revision
[mothur.git] / averagelinkage.cpp
1
2 #include "cluster.hpp"
3 #include "rabundvector.hpp"
4 #include "sparsematrix.hpp"
5 #include <exception>
6
7 /* This class implements the average UPGMA, average neighbor clustering algorithm */
8
9 /***********************************************************************/
10
11 AverageLinkage::AverageLinkage(RAbundVector* rav, ListVector* lv, SparseMatrix* dm) :
12 Cluster(rav, lv, dm)
13 {}
14
15 /***********************************************************************/
16 //THis function clusters based on the average method
17 void AverageLinkage::update(){
18         try{
19                 getRowColCells();               
20         
21                 vector<int> found(nColCells, 0);
22         
23                 int rowBin = rabund->get(smallRow);
24                 int colBin = rabund->get(smallCol);
25                 int totalBin = rowBin + colBin;
26         
27                 for(int i=1;i<nRowCells;i++){
28                 
29                         int search;
30                 
31                         if(rowCells[i]->row == smallRow){
32                                 search = rowCells[i]->column;
33                         }
34                         else{
35                                 search = rowCells[i]->row;
36                         }
37                 
38                         for(int j=1;j<nColCells;j++){
39                         
40                                 if(colCells[j]->row == search || colCells[j]->column == search){
41                                         colCells[j]->dist = (colBin * colCells[j]->dist + rowBin * rowCells[i]->dist) / totalBin;               
42                                 
43                                         found[j] = 1;
44                                 
45                                         if(colCells[j]->vectorMap != NULL){
46                                                 *(colCells[j]->vectorMap) = NULL;
47                                                 colCells[j]->vectorMap = NULL;
48                                         }
49                                 
50                                         break;
51                                 }
52                         
53                         }                       
54                         dMatrix->rmCell(rowCells[i]);
55                 }       
56         
57                 clusterBins();
58                 clusterNames();
59         
60                 for(int i=0;i<nColCells;i++){
61                         if(found[i] == 0){
62                                 dMatrix->rmCell(colCells[i]);
63                         }
64                 }
65         }
66         catch(exception& e) {
67                 cout << "Standard Error: " << e.what() << " has occurred in the AverageLinkage class Function update. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
68                 exit(1);
69         }
70         catch(...) {
71                 cout << "An unknown error has occurred in the AverageLinkage class function update. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
72                 exit(1);
73         }
74
75         
76 }
77
78 /***********************************************************************/