Debian Codegraph @

src/dr/oldevomodel/substmodel/HKY.java

raw symbols SCIP
1 /*
2 * HKY.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.inference.model.Parameter;
29 import dr.inference.model.Statistic;
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 /**
40 * Hasegawa-Kishino-Yano model of nucleotide evolution
41 *
42 * @author Alexei Drummond
43 * @author Andrew Rambaut
44 * @version $Id: HKY.java,v 1.42 2005/09/23 13:17:59 rambaut Exp $
45 */
46 public class HKY extends AbstractNucleotideModel implements Citable {
47
48 /**
49 * tsTv
50 */
51 private double tsTv;
52
53 private Variable<Double> kappaParameter = null;
54
55 private boolean updateIntermediates = true;
56
57 /**
58 * Used for precalculations
59 */
60 private double beta, A_R, A_Y;
61 private double tab1A, tab2A, tab3A;
62 private double tab1C, tab2C, tab3C;
63 private double tab1G, tab2G, tab3G;
64 private double tab1T, tab2T, tab3T;
65
66 /**
67 * A constructor which allows a more programmatic approach with
68 * fixed kappa.
69 * @param kappa
70 * @param freqModel
71 */
72 public HKY(double kappa, FrequencyModel freqModel) {
73 this(new Parameter.Default(kappa), freqModel);
74 }
75 /**
76 * Constructor
77 */
78 public HKY(Variable kappaParameter, FrequencyModel freqModel) {
79
80 super(NucModelType.HKY.getXMLName(), freqModel);
81 this.kappaParameter = kappaParameter;
82 addVariable(kappaParameter);
83 kappaParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));
84 updateIntermediates = true;
85
86 addStatistic(tsTvStatistic);
87 }
88
89 /**
90 * set kappa
91 */
92 public void setKappa(double kappa) {
93 kappaParameter.setValue(0, kappa);
94 updateMatrix = true;
95 }
96
97 /**
98 * @return kappa
99 */
100 public final double getKappa() {
101 return kappaParameter.getValue(0);
102 }
103
104 /**
105 * set ts/tv
106 */
107 public void setTsTv(double tsTv) {
108 this.tsTv = tsTv;
109 calculateFreqRY();
110 setKappa((tsTv * freqR * freqY) / (freqA * freqG + freqC * freqT));
111 }
112
113 /**
114 * @return tsTv
115 */
116 public double getTsTv() {
117 calculateFreqRY();
118 tsTv = (getKappa() * (freqA * freqG + freqC * freqT)) / (freqR * freqY);
119
120 return tsTv;
121 }
122
123 protected void frequenciesChanged() {
124 // frequencyModel changed
125 updateIntermediates = true;
126 }
127
128 private void calculateIntermediates() {
129
130 calculateFreqRY();
131
132 // small speed up - reduce calculations. Comments show original code
133
134 // (C+T) / (A+G)
135 final double r1 = (1 / freqR) - 1;
136 tab1A = freqA * r1;
137
138 tab3A = freqA / freqR;
139 tab2A = 1 - tab3A; // (freqR-freqA)/freqR;
140
141 final double r2 = 1 / r1; // ((1 / freqY) - 1);
142 tab1C = freqC * r2;
143
144 tab3C = freqC / freqY;
145 tab2C = 1 - tab3C; // (freqY-freqC)/freqY; assert tab2C + tab3C == 1.0;
146
147 tab1G = freqG * r1;
148 tab3G = tab2A; // 1 - tab3A; // freqG/freqR;
149 tab2G = tab3A; // 1 - tab3G; // (freqR-freqG)/freqR;
150
151 tab1T = freqT * r2;
152
153 tab3T = tab2C; // 1 - tab3C; // freqT/freqY;
154 tab2T = tab3C; // 1 - tab3T; // (freqY-freqT)/freqY; //assert tab2T + tab3T == 1.0 ;
155
156 updateMatrix = true;
157 updateIntermediates = false;
158 }
159
160 /**
161 * get the complete transition probability matrix for the given distance
162 *
163 * @param distance the expected number of substitutions
164 * @param matrix an array to store the matrix
165 */
166 public void getTransitionProbabilities(double distance, double[] matrix) {
167 synchronized (this) {
168 if (updateIntermediates) {
169 calculateIntermediates();
170 }
171
172 if (updateMatrix) {
173 setupMatrix();
174 }
175 }
176
177 final double xx = beta * distance;
178 final double bbR = Math.exp(xx * A_R);
179 final double bbY = Math.exp(xx * A_Y);
180
181 final double aa = Math.exp(xx);
182 final double oneminusa = 1 - aa;
183
184 final double t1Aaa = (tab1A * aa);
185 matrix[0] = freqA + t1Aaa + (tab2A * bbR);
186
187 matrix[1] = freqC * oneminusa;
188 final double t1Gaa = (tab1G * aa);
189 matrix[2] = freqG + t1Gaa - (tab3G * bbR);
190 matrix[3] = freqT * oneminusa;
191
192 matrix[4] = freqA * oneminusa;
193 final double t1Caa = (tab1C * aa);
194 matrix[5] = freqC + t1Caa + (tab2C * bbY);
195 matrix[6] = freqG * oneminusa;
196 final double t1Taa = (tab1T * aa);
197 matrix[7] = freqT + t1Taa - (tab3T * bbY);
198
199 matrix[8] = freqA + t1Aaa - (tab3A * bbR);
200 matrix[9] = matrix[1];
201 matrix[10] = freqG + t1Gaa + (tab2G * bbR);
202 matrix[11] = matrix[3];
203
204 matrix[12] = matrix[4];
205 matrix[13] = freqC + t1Caa - (tab3C * bbY);
206 matrix[14] = matrix[6];
207 matrix[15] = freqT + t1Taa + (tab2T * bbY);
208 }
209
210 /**
211 * setup substitution matrix
212 */
213 public void setupMatrix() {
214 final double kappa = getKappa();
215 beta = -1.0 / (2.0 * (freqR * freqY + kappa * (freqA * freqG + freqC * freqT)));
216
217 A_R = 1.0 + freqR * (kappa - 1);
218 A_Y = 1.0 + freqY * (kappa - 1);
219
220 updateMatrix = false;
221 }
222
223 protected void setupRelativeRates() {
224 }
225
226 /**
227 * This function returns the Eigen vectors.
228 *
229 * @return the array
230 */
231 public double[][] getEigenVectors() {
232 synchronized (this) {
233 if (updateMatrix) {
234 setupEigenSystem();
235 }
236 }
237 return Evec;
238 }
239
240 /**
241 * This function returns the inverse Eigen vectors.
242 *
243 * @return the array
244 */
245 public double[][] getInverseEigenVectors() {
246 synchronized (this) {
247 if (updateMatrix) {
248 setupEigenSystem();
249 }
250 }
251 return Ievc;
252 }
253
254 /**
255 * This function returns the Eigen values.
256 */
257 public double[] getEigenValues() {
258 synchronized (this) {
259 if (updateMatrix) {
260 setupEigenSystem();
261 }
262 }
263 return Eval;
264 }
265
266 /**
267 * setup substitution matrix
268 */
269 protected void setupEigenSystem() {
270 if (!eigenInitialised)
271 initialiseEigen();
272
273 final double kappa = getKappa();
274
275 // left eigenvector #1
276 Ievc[0][0] = freqA; // or, evec[0] = pi;
277 Ievc[0][1] = freqC;
278 Ievc[0][2] = freqG;
279 Ievc[0][3] = freqT;
280
281 // left eigenvector #2
282 Ievc[1][0] = freqA * freqY;
283 Ievc[1][1] = -freqC * freqR;
284 Ievc[1][2] = freqG * freqY;
285 Ievc[1][3] = -freqT * freqR;
286
287 Ievc[2][1] = 1; // left eigenvectors 3 = (0,1,0,-1); 4 = (1,0,-1,0)
288 Ievc[2][3] = -1;
289
290 Ievc[3][0] = 1;
291 Ievc[3][2] = -1;
292
293 Evec[0][0] = 1; // right eigenvector 1 = (1,1,1,1)'
294 Evec[1][0] = 1;
295 Evec[2][0] = 1;
296 Evec[3][0] = 1;
297
298 // right eigenvector #2
299 Evec[0][1] = 1.0/freqR;
300 Evec[1][1] = -1.0/freqY;
301 Evec[2][1] = 1.0/freqR;
302 Evec[3][1] = -1.0/freqY;
303
304 // right eigenvector #3
305 Evec[1][2] = freqT / freqY;
306 Evec[3][2] = -freqC / freqY;
307
308 // right eigenvector #4
309 Evec[0][3] = freqG / freqR;
310 Evec[2][3] = -freqA / freqR;
311
312 // eigenvectors
313 beta = -1.0 / (2.0 * (freqR * freqY + kappa * (freqA * freqG + freqC * freqT)));
314
315 A_R = 1.0 + freqR * (kappa - 1);
316 A_Y = 1.0 + freqY * (kappa - 1);
317
318 Eval[1] = beta;
319 Eval[2] = beta*A_Y;
320 Eval[3] = beta*A_R;
321
322 updateMatrix = false;
323 }
324
325 /**
326 * allocate memory for the Eigen routines
327 */
328 protected void initialiseEigen() {
329
330 Eval = new double[stateCount];
331 Evec = new double[stateCount][stateCount];
332 Ievc = new double[stateCount][stateCount];
333
334 eigenInitialised = true;
335 updateMatrix = true;
336 }
337 // *****************************************************************
338 // Interface Model
339 // *****************************************************************
340
341 /**
342 * Restore the stored state
343 */
344 public void restoreState() {
345 super.restoreState();
346 updateIntermediates = true;
347 }
348
349 // **************************************************************
350 // XHTMLable IMPLEMENTATION
351 // **************************************************************
352
353 public String toXHTML() {
354 StringBuffer buffer = new StringBuffer();
355
356 buffer.append("<em>HKY Model</em> Ts/Tv = ");
357 buffer.append(getTsTv());
358 buffer.append(" (kappa = ");
359 buffer.append(getKappa());
360 buffer.append(")");
361
362 return buffer.toString();
363 }
364
365 //
366 // Private stuff
367 //
368
369 private final Statistic tsTvStatistic = new Statistic.Abstract() {
370
371 public String getStatisticName() {
372 return "tsTv";
373 }
374
375 public int getDimension() {
376 return 1;
377 }
378
379 public double getStatisticValue(int dim) {
380 return getTsTv();
381 }
382
383 };
384
385 @Override
386 public Citation.Category getCategory() {
387 return Citation.Category.SUBSTITUTION_MODELS;
388 }
389
390 @Override
391 public String getDescription() {
392 return "HKY nucleotide substitution model";
393 }
394
395 @Override
396 public List<Citation> getCitations() {
397 return Collections.singletonList(CITATION);
398 }
399
400
401 public static Citation CITATION = new Citation(
402 new Author[]{
403 new Author("M", "Hasegawa"),
404 new Author("H", "Kishino"),
405 new Author("T", "Yano")
406 },
407 "Dating the human-ape splitting by a molecular clock of mitochondrial DNA",
408 1985,
409 "J. Mol. Evol.",
410 22,
411 160, 174,
412 Citation.Status.PUBLISHED
413 );
414 }