| 1 | /* |
| 2 | * UPGMATree.java |
| 3 | * |
| 4 | * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard |
| 5 | * |
| 6 | * This file is part of BEAST. |
| 7 | * See the NOTICE file distributed with this work for additional |
| 8 | * information regarding copyright ownership and licensing. |
| 9 | * |
| 10 | * BEAST is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU Lesser General Public License as |
| 12 | * published by the Free Software Foundation; either version 2 |
| 13 | * of the License, or (at your option) any later version. |
| 14 | * |
| 15 | * BEAST is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU Lesser General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU Lesser General Public |
| 21 | * License along with BEAST; if not, write to the |
| 22 | * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, |
| 23 | * Boston, MA 02110-1301 USA |
| 24 | */ |
| 25 | |
| 26 | package dr.evolution.tree; |
| 27 | |
| 28 | import dr.evolution.distance.DistanceMatrix; |
| 29 | |
| 30 | /** |
| 31 | * constructs a UPGMA tree from pairwise distances |
| 32 | * |
| 33 | * @version $Id: UPGMATree.java,v 1.14 2005/05/24 20:25:57 rambaut Exp $ |
| 34 | * |
| 35 | * @author Andrew Rambaut |
| 36 | * @author Alexei Drummond |
| 37 | */ |
| 38 | public class UPGMATree extends ClusteringTree { |
| 39 | |
| 40 | /** |
| 41 | * constructor UPGMA tree |
| 42 | * |
| 43 | * @param distanceMatrix distance matrix |
| 44 | */ |
| 45 | public UPGMATree(DistanceMatrix distanceMatrix) { |
| 46 | |
| 47 | super(distanceMatrix, 2); |
| 48 | } |
| 49 | |
| 50 | // |
| 51 | // Protected and Private stuff |
| 52 | // |
| 53 | |
| 54 | protected void findNextPair() { |
| 55 | |
| 56 | besti = 0; |
| 57 | bestj = 1; |
| 58 | double dmin = getDist(0, 1); |
| 59 | for (int i = 0; i < numClusters-1; i++) { |
| 60 | |
| 61 | for (int j = i+1; j < numClusters; j++) { |
| 62 | |
| 63 | if (getDist(i, j) < dmin) { |
| 64 | |
| 65 | dmin = getDist(i, j); |
| 66 | besti = i; |
| 67 | bestj = j; |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | abi = alias[besti]; |
| 72 | abj = alias[bestj]; |
| 73 | } |
| 74 | |
| 75 | protected double newNodeHeight() { |
| 76 | return getDist(besti, bestj) / 2.0; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * compute updated distance between the new cluster (i,j) |
| 81 | * to any other cluster k |
| 82 | */ |
| 83 | protected double updatedDistance(int i, int j, int k) |
| 84 | { |
| 85 | int ai = alias[i]; |
| 86 | int aj = alias[j]; |
| 87 | |
| 88 | double tipSum = (double) (tipCount[ai] + tipCount[aj]); |
| 89 | |
| 90 | return (((double)tipCount[ai]) / tipSum) * getDist(k, i) + |
| 91 | (((double)tipCount[aj]) / tipSum) * getDist(k, j); |
| 92 | } |
| 93 | } |