Debian Codegraph @

src/dr/evolution/distance/F84DistanceMatrix.java

raw symbols SCIP
1 /*
2 * F84DistanceMatrix.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 HKY corrected distance matrix
32 *
33 * @version $Id: F84DistanceMatrix.java,v 1.3 2005/05/24 20:25:56 rambaut Exp $
34 *
35 * @author Andrew Rambaut
36 */
37 public class F84DistanceMatrix extends DistanceMatrix
38 {
39 //
40 // Public stuff
41 //
42
43 /** constructor */
44 public F84DistanceMatrix()
45 {
46 super();
47 }
48
49 /** constructor taking a pattern source */
50 public F84DistanceMatrix(PatternList patterns)
51 {
52 super(patterns);
53 }
54
55 /**
56 * set the pattern source
57 */
58 public void setPatterns(PatternList patterns) {
59 super.setPatterns(patterns);
60
61 double[] freqs = patterns.getStateFrequencies();
62 stateCount = patterns.getStateCount();
63 if (stateCount != 4) {
64 throw new IllegalArgumentException("F84DistanceMatrix must have nucleotide patterns");
65 }
66
67 double freqA = freqs[0];
68 double freqC = freqs[1];
69 double freqG = freqs[2];
70 double freqT = freqs[3];
71
72 double freqR = freqA + freqG;
73 double freqY = freqC + freqT;
74
75 constA = ((freqA * freqG) / freqR) + ((freqC * freqT) / freqY);
76 constB = (freqA * freqG) + (freqC * freqT);
77 constC = (freqR * freqY);
78 }
79
80 /**
81 * Calculate a pairwise distance
82 */
83 protected double calculatePairwiseDistance(int taxon1, int taxon2) {
84 int state1, state2;
85
86 int n = patterns.getPatternCount();
87 double weight, distance;
88 double sumTs = 0.0;
89 double sumTv = 0.0;
90 double sumWeight = 0.0;
91
92 int[] pattern;
93
94 for (int i = 0; i < n; i++) {
95 pattern = patterns.getPattern(i);
96
97 state1 = pattern[taxon1];
98 state2 = pattern[taxon2];
99
100 weight = patterns.getPatternWeight(i);
101 if (!dataType.isAmbiguousState(state1) && !dataType.isAmbiguousState(state2) && state1 != state2) {
102
103 if ((state1 == 0 && state2 == 2) || (state1 == 2 && state2 == 0)) {
104 // it's a transition
105 sumTs += weight;
106 } else {
107 // it's a transversion
108 sumTv += weight;
109 }
110 }
111 sumWeight += weight;
112 }
113
114 double P = sumTs / sumWeight;
115 double Q = sumTv / sumWeight;
116
117 double tmp1 = Math.log(1.0 - (P / (2.0 * constA)) -
118 (((constA - constB) * Q) / (2.0 * constA * constC)));
119
120 double tmp2 = Math.log(1.0 - (Q / (2.0 * constC)));
121
122 distance = -(2.0 * constA * tmp1) +
123 (2.0 * (constA - constB - constC) * tmp2);
124
125 if (distance < MAX_DISTANCE) {
126 return distance;
127 } else {
128 return MAX_DISTANCE;
129 }
130 }
131
132 //
133 // Private stuff
134 //
135
136 private int stateCount;
137
138 //used in correction formula
139 private double constA, constB, constC;
140
141 }