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