| 1 | /* |
| 2 | * JukesCantorDistanceMatrix.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.distance; |
| 27 | |
| 28 | import dr.evolution.alignment.PatternList; |
| 29 | |
| 30 | /** |
| 31 | * compute jukes-cantor corrected distance matrix |
| 32 | * |
| 33 | * @version $Id: JukesCantorDistanceMatrix.java,v 1.4 2005/05/24 20:25:56 rambaut Exp $ |
| 34 | * |
| 35 | * @author Andrew Rambaut |
| 36 | * @author Korbinian Strimmer |
| 37 | */ |
| 38 | public class JukesCantorDistanceMatrix extends DistanceMatrix |
| 39 | { |
| 40 | // |
| 41 | // Public stuff |
| 42 | // |
| 43 | |
| 44 | /** constructor */ |
| 45 | public JukesCantorDistanceMatrix() |
| 46 | { |
| 47 | super(); |
| 48 | } |
| 49 | |
| 50 | /** constructor taking a pattern source */ |
| 51 | public JukesCantorDistanceMatrix(PatternList patterns) |
| 52 | { |
| 53 | super(patterns); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * set the pattern source |
| 58 | */ |
| 59 | public void setPatterns(PatternList patterns) { |
| 60 | super.setPatterns(patterns); |
| 61 | |
| 62 | final int stateCount = patterns.getStateCount(); |
| 63 | |
| 64 | const1 = ((double) stateCount - 1) / stateCount; |
| 65 | const2 = ((double) stateCount) / (stateCount - 1) ; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Calculate a pairwise distance |
| 70 | */ |
| 71 | protected double calculatePairwiseDistance(int i, int j) { |
| 72 | final double obsDist = super.calculatePairwiseDistance(i, j); |
| 73 | |
| 74 | if (obsDist == 0.0) return 0.0; |
| 75 | |
| 76 | if (obsDist >= const1) { |
| 77 | return MAX_DISTANCE; |
| 78 | } |
| 79 | |
| 80 | final double expDist = -const1 * Math.log(1.0 - (const2 * obsDist)); |
| 81 | |
| 82 | if (expDist < MAX_DISTANCE) { |
| 83 | return expDist; |
| 84 | } else { |
| 85 | return MAX_DISTANCE; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | // |
| 90 | // Private stuff |
| 91 | // |
| 92 | |
| 93 | //used in correction formula |
| 94 | private double const1, const2; |
| 95 | } |