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