]> git.donarmstrong.com Git - mothur.git/blob - averagelinkage.cpp
db2c51edc5b182b52dcf540456baad7b5782c61e
[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, float c) :
15         Cluster(rav, lv, dm, c)
16 {
17         saveRow = -1;
18         saveCol = -1;
19 }
20
21
22 /***********************************************************************/
23 //This function returns the tag of the method.
24 string AverageLinkage::getTag() {
25         return("an");
26 }
27
28
29 /***********************************************************************/
30 //This function updates the distance based on the average linkage method.
31 bool AverageLinkage::updateDistance(MatData& colCell, MatData& rowCell) {
32         try {
33                 if ((saveRow != smallRow) || (saveCol != smallCol)) {
34                         rowBin = rabund->get(smallRow);
35                         colBin = rabund->get(smallCol);
36                         totalBin = rowBin + colBin;
37                         saveRow = smallRow;
38                         saveCol = smallCol;
39                 }
40                 
41                 float oldColCell = colCell->dist;
42                 
43                 colCell->dist = (colBin * colCell->dist + rowBin * rowCell->dist) / totalBin;
44                 
45                 //warn user if merge with value above cutoff produces a value below cutoff
46                 if ((colCell->dist < cutoff) && ((oldColCell > cutoff) || (rowCell->dist > cutoff)) ) {
47                         mothurOut("Warning: merging " + toString(oldColCell) + " with " + toString(rowCell->dist) + ", new value = " + toString(colCell->dist) + ". Results will differ from those if cutoff was used in the read.dist command."); mothurOutEndLine();
48                 }
49
50                 return(true);
51         }
52         catch(exception& e) {
53                 errorOut(e, "AverageLinkage", "updateDistance");
54                 exit(1);
55         }
56 }
57
58 /***********************************************************************/
59
60
61 /***********************************************************************/
62
63 #endif