Debian Codegraph @

src/dr/oldevomodel/substmodel/TN93.java

raw symbols SCIP
1 /*
2 * TN93.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.oldevomodel.substmodel;
27
28 import dr.oldevomodelxml.substmodel.TN93Parser;
29 import dr.inference.model.Parameter;
30 import dr.inference.model.Variable;
31 import dr.util.Author;
32 import dr.util.Citable;
33 import dr.util.Citation;
34
35 import java.util.Collections;
36 import java.util.List;
37
38 /**
39 * Tamura and Nei model of nucleotide evolution.
40 * <p/>
41 * <p/>
42 * <p/>
43 * pr = p[0]+p[1]
44 * py = 1 - pr
45 * <p/>
46 * eigen values
47 * <p/>
48 * [0, -1, -(k[0]*pr + py), -(k[1]*py + pr)]
49 * <p/>
50 * unnormalized eigen vectors
51 * [1,1,1,1],
52 * [1,1,-pr/py,-pr/py],
53 * [1, -p[0]/p[1], 0, 0],
54 * [0, 0, 1,-p[2]/p[3]]
55 *
56 * @author Joseph Heled
57 */
58 public class TN93 extends AbstractNucleotideModel implements Citable {
59
60 private Variable<Double> kappa1Variable = null;
61 private Variable<Double> kappa2Variable = null;
62
63 private boolean updateIntermediates = true;
64
65 /**
66 * Used for precalculations
67 */
68
69 private double p1a;
70 private double p0a;
71 private double p3b;
72 private double p2b;
73 private double a;
74 private double b;
75 private double p1aa;
76 private double p0aa;
77 private double p3bb;
78 private double p2bb;
79 private double p1aIsa;
80 private double p0aIsa;
81 private double p3bIsb;
82 private double p2bIsb;
83 private double k1g;
84 private double k1a;
85 private double k2t;
86 private double k2c;
87 private double subrateScale;
88
89 /**
90 * TN93
91 *
92 * @param kappa1Variable
93 * @param kappa2Variable
94 * @param freqModel
95 */
96 public TN93(Variable kappa1Variable, Variable kappa2Variable, FrequencyModel freqModel) {
97
98 super(TN93Parser.TN93_MODEL, freqModel);
99 this.kappa1Variable = kappa1Variable;
100 this.kappa2Variable = kappa2Variable;
101 addVariable(kappa1Variable);
102 addVariable(kappa2Variable);
103 kappa1Variable.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));
104 kappa2Variable.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));
105 updateIntermediates = true;
106 }
107
108 /**
109 * @return kappa1
110 */
111 public final double getKappa1() {
112 return kappa1Variable.getValue(0);
113 }
114
115 /**
116 * @return kappa2
117 */
118 public final double getKappa2() {
119 return kappa2Variable.getValue(0);
120 }
121
122
123 protected void frequenciesChanged() {
124 // frequencyModel changed
125 updateIntermediates = true;
126 }
127
128 // I am not sure how HKY works without this
129 // Comment this function out to get bug 138
130 protected void ratesChanged() {
131 // frequencyModel changed
132 updateIntermediates = true;
133 }
134
135 private void calculateIntermediates() {
136
137 calculateFreqRY();
138
139 double k1 = getKappa1();
140 double k2 = getKappa2();
141
142 // System.out.println(getModelName() + " Using " + k1 + " " + k2);
143 // A hack until I get right this boundary case. gives results accurate to 1e-8 in the P matrix
144 // so should be OK even like this.
145 if (k1 == 1) {
146 k1 += 1E-10;
147 }
148 if (k2 == 1) {
149 k2 += 1e-10;
150 }
151
152 double l1 = k1 * k1 * freqR + k1 * (2 * freqY - 1) - freqY;
153 double l2 = k2 * k2 * freqY + k2 * (2 * freqR - 1) - freqR;
154
155 p1a = freqG * l1;
156 p0a = freqA * l1;
157 p3b = freqT * l2;
158 p2b = freqC * l2;
159
160 a = -(k1 * freqR + freqY);
161 b = -(k2 * freqY + freqR);
162
163 p1aa = p1a / a;
164 p0aa = p0a / a;
165 p3bb = p3b / b;
166 p2bb = p2b / b;
167
168 p1aIsa = p1a / (1 + a);
169 p0aIsa = p0a / (1 + a);
170 p3bIsb = p3b / (1 + b);
171 p2bIsb = p2b / (1 + b);
172
173 k1g = k1 * freqG;
174 k1a = k1 * freqA;
175 k2t = k2 * freqT;
176 k2c = k2 * freqC;
177
178 subrateScale = 2 * (k1 * freqA * freqG + k2 * freqC * freqT + freqR * freqY);
179 // updateMatrix = true;
180 updateIntermediates = false;
181 }
182
183 /**
184 * get the complete transition probability matrix for the given distance.
185 * <p/>
186 * Based on work I did in my 691 project.
187 *
188 * @param distance the expected number of substitutions
189 * @param matrix an array to store the matrix
190 */
191 public void getTransitionProbabilities(double distance, double[] matrix) {
192 synchronized (this) {
193 if (updateIntermediates) {
194 calculateIntermediates();
195 }
196 }
197
198 distance /= subrateScale;
199
200 double[] q = {
201 0, k1g, freqC, freqT,
202 k1a, 0, freqC, freqT,
203 freqA, freqG, 0, k2t,
204 freqA, freqG, k2c, 0
205 };
206
207 q[0] = -(q[1] + q[2] + q[3]);
208 q[5] = -(q[4] + q[6] + q[7]);
209 q[10] = -(q[8] + q[9] + q[11]);
210 q[15] = -(q[12] + q[13] + q[14]);
211
212 double[] fa0 = {
213 1 + q[0] - p1aa, q[1] + p1aa, q[2], q[3],
214 q[4] + p0aa, 1 + q[5] - p0aa, q[6], q[7],
215 q[8], q[9], 1 + q[10] - p3bb, q[11] + p3bb,
216 q[12], q[13], q[14] + p2bb, 1 + q[15] - p2bb
217 };
218
219
220 double[] fa1 = {
221 -q[0] + p1aIsa, -q[1] - p1aIsa, -q[2], -q[3],
222 -q[4] - p0aIsa, -q[5] + p0aIsa, -q[6], -q[7],
223 -q[8], -q[9], -q[10] + p3bIsb, -q[11] - p3bIsb,
224 -q[12], -q[13], -q[14] - p2bIsb, -q[15] + p2bIsb};
225
226 double et = Math.exp(-distance);
227
228 for (int k = 0; k < 16; ++k) {
229 fa1[k] = fa1[k] * et + fa0[k];
230 }
231
232 final double eta = Math.exp(distance * a);
233 final double etb = Math.exp(distance * b);
234
235 double za = eta / (a * (1 + a));
236 double zb = etb / (b * (1 + b));
237 double u0 = p1a * za;
238 double u1 = p0a * za;
239 double u2 = p3b * zb;
240 double u3 = p2b * zb;
241
242 fa1[0] += u0;
243 fa1[1] -= u0;
244 fa1[4] -= u1;
245 fa1[5] += u1;
246
247 fa1[10] += u2;
248 fa1[11] -= u2;
249 fa1[14] -= u3;
250 fa1[15] += u3;
251
252 // transpose 2 middle rows and columns
253 matrix[0] = fa1[0];
254 matrix[1] = fa1[2];
255 matrix[2] = fa1[1];
256 matrix[3] = fa1[3];
257 matrix[4] = fa1[8];
258 matrix[5] = fa1[10];
259 matrix[6] = fa1[9];
260 matrix[7] = fa1[11];
261 matrix[8] = fa1[4];
262 matrix[9] = fa1[6];
263 matrix[10] = fa1[5];
264 matrix[11] = fa1[7];
265 matrix[12] = fa1[12];
266 matrix[13] = fa1[14];
267 matrix[14] = fa1[13];
268 matrix[15] = fa1[15];
269
270 //System.arraycopy(fa1, 0, matrix, 0, 16);
271 }
272
273 /**
274 * setup substitution matrix
275 */
276 public void setupMatrix() {
277 }
278
279 protected void setupRelativeRates() {
280 }
281
282 // untested
283 // public double[] getEigenValues() {
284 // final double k1 = getKappa1();
285 // final double k2 = getKappa2();
286 // calculateFreqRY();
287 //
288 // return new double[]{0, -1, -(k1*freqR + freqY), -(k2*freqY + freqR)};
289 // }
290 //
291 // public double[][] getEigenVectors() {
292 // calculateFreqRY();
293 //
294 // final double[][] emat = {
295 // {1., 1, 1., 1},
296 // {1, 1, -freqR / freqY, -freqR / freqY},
297 // {1, -freqA / freqC, 0, 0},
298 // {0, 0, 1, -freqG / freqT}};
299 //
300 // // make them norm 1
301 // for(int k = 0; k < 4; ++k) {
302 // double s = 0;
303 // for(int i = 0; i < 4; ++i) {
304 // s += emat[k][i];
305 // }
306 // s = 1.0/Math.sqrt(s);
307 // for(int i = 0; i < 4; ++i) {
308 // emat[k][i] *= s;
309 // }
310 // }
311 // return emat;
312 // }
313
314 // *****************************************************************
315 // Interface Model
316 // *****************************************************************
317
318 /**
319 * Restore the stored state
320 */
321 public void restoreState() {
322 super.restoreState();
323 updateIntermediates = true;
324 }
325
326 // **************************************************************
327 // XHTMLable IMPLEMENTATION
328 // **************************************************************
329
330 public String toXHTML() {
331 StringBuffer buffer = new StringBuffer();
332
333 buffer.append("<em>TN93 Model</em>");
334 buffer.append(" (kappa = ");
335 buffer.append(getKappa1()).append(",").append(getKappa2());
336 buffer.append(")");
337
338 return buffer.toString();
339 }
340
341 @Override
342 public Citation.Category getCategory() {
343 return Citation.Category.SUBSTITUTION_MODELS;
344 }
345
346 @Override
347 public String getDescription() {
348 return "Tamura-Nei nucleotide substitution model";
349 }
350
351 @Override
352 public List<Citation> getCitations() {
353 return Collections.singletonList(CITATION);
354 }
355
356 public static Citation CITATION = new Citation(
357 new Author[]{
358 new Author("K", "Tamura"),
359 new Author("M", "Nei")
360 },
361 "Estimation of the number of nucleotide substitutions in the control region of mitochondrial DNA in humans and chimpanzees",
362 1993,
363 "Mol Biol Evol",
364 10, 512, 526
365 );
366 }