Debian Codegraph @

src/dr/evomodel/substmodel/nucleotide/HKY.java

raw symbols SCIP
1 /*
2 * HKY.java
3 *
4 * Copyright (c) 2002-2016 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.evomodel.substmodel.nucleotide;
27
28 import dr.evomodel.substmodel.BaseSubstitutionModel;
29 import dr.evomodel.substmodel.EigenDecomposition;
30 import dr.evomodel.substmodel.FrequencyModel;
31 import dr.inference.model.Parameter;
32 import dr.inference.model.Statistic;
33 import dr.evolution.datatype.Nucleotides;
34 import dr.math.matrixAlgebra.Vector;
35 import dr.util.Author;
36 import dr.util.Citable;
37 import dr.util.Citation;
38
39 import java.util.Collections;
40 import java.util.List;
41
42
43 /**
44 * Hasegawa-Kishino-Yano model of nucleotide evolution
45 *
46 * @author Alexei Drummond
47 * @author Andrew Rambaut
48 * @author Marc A. Suchard
49 */
50 public class HKY extends BaseSubstitutionModel implements Citable {
51
52 private Parameter kappaParameter = null;
53
54 /**
55 * A constructor which allows a more programmatic approach with
56 * fixed kappa.
57 * @param kappa
58 * @param freqModel
59 */
60 public HKY(double kappa, FrequencyModel freqModel) {
61 this(new Parameter.Default(kappa), freqModel);
62 }
63 /**
64 * Constructor
65 * @param kappaParameter
66 * @param freqModel
67 */
68 public HKY(Parameter kappaParameter, FrequencyModel freqModel) {
69
70 super("HKY", Nucleotides.INSTANCE, freqModel);
71
72 this.kappaParameter = kappaParameter;
73 addVariable(kappaParameter);
74 kappaParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));
75
76 addStatistic(tsTvStatistic);
77 }
78
79 /**
80 * set kappa
81 * @param kappa
82 */
83 public void setKappa(double kappa) {
84 kappaParameter.setParameterValue(0, kappa);
85 updateMatrix = true;
86 }
87
88 /**
89 * @return kappa
90 */
91 public final double getKappa() {
92 return kappaParameter.getParameterValue(0);
93 }
94
95 /**
96 * set ts/tv
97 * @param tsTv
98 */
99 public void setTsTv(double tsTv) {
100 double freqA = freqModel.getFrequency(0);
101 double freqC = freqModel.getFrequency(1);
102 double freqG = freqModel.getFrequency(2);
103 double freqT = freqModel.getFrequency(3);
104 double freqR = freqA + freqG;
105 double freqY = freqC + freqT;
106 setKappa((tsTv * freqR * freqY) / (freqA * freqG + freqC * freqT));
107 }
108
109 /**
110 * @return tsTv
111 */
112 public double getTsTv() {
113 double freqA = freqModel.getFrequency(0);
114 double freqC = freqModel.getFrequency(1);
115 double freqG = freqModel.getFrequency(2);
116 double freqT = freqModel.getFrequency(3);
117 double freqR = freqA + freqG;
118 double freqY = freqC + freqT;
119 double tsTv = (getKappa() * (freqA * freqG + freqC * freqT)) / (freqR * freqY);
120
121 return tsTv;
122 }
123
124 protected void frequenciesChanged() {
125 }
126
127 protected void ratesChanged() {
128 }
129
130 protected void setupRelativeRates(double[] rates) {
131 double kappa = kappaParameter.getParameterValue(0);
132 rates[0] = 1.0;
133 rates[1] = kappa;
134 rates[2] = 1.0;
135 rates[3] = 1.0;
136 rates[4] = kappa;
137 rates[5] = 1.0;
138 }
139
140 public synchronized EigenDecomposition getEigenDecomposition() {
141
142 if (eigenDecomposition == null) {
143 double[] evec = new double[stateCount * stateCount];
144 double[] ivec = new double[stateCount * stateCount];
145 double[] eval = new double[stateCount];
146 eigenDecomposition = new EigenDecomposition(evec, ivec, eval);
147
148 ivec[2 * stateCount + 1] = 1; // left eigenvectors 3 = (0,1,0,-1); 4 = (1,0,-1,0)
149 ivec[2 * stateCount + 3] = -1;
150
151 ivec[3 * stateCount + 0] = 1;
152 ivec[3 * stateCount + 2] = -1;
153
154 evec[0 * stateCount + 0] = 1; // right eigenvector 1 = (1,1,1,1)'
155 evec[1 * stateCount + 0] = 1;
156 evec[2 * stateCount + 0] = 1;
157 evec[3 * stateCount + 0] = 1;
158
159 }
160
161 if (updateMatrix) {
162
163 double[] evec = eigenDecomposition.getEigenVectors();
164 double[] ivec = eigenDecomposition.getInverseEigenVectors();
165 double[] pi = freqModel.getFrequencies();
166 double piR = pi[0] + pi[2];
167 double piY = pi[1] + pi[3];
168
169 // left eigenvector #1
170 ivec[0 * stateCount + 0] = pi[0]; // or, evec[0] = pi;
171 ivec[0 * stateCount + 1] = pi[1];
172 ivec[0 * stateCount + 2] = pi[2];
173 ivec[0 * stateCount + 3] = pi[3];
174
175 // left eigenvector #2
176 ivec[1 * stateCount + 0] = pi[0]*piY;
177 ivec[1 * stateCount + 1] = -pi[1]*piR;
178 ivec[1 * stateCount + 2] = pi[2]*piY;
179 ivec[1 * stateCount + 3] = -pi[3]*piR;
180
181 // right eigenvector #2
182 evec[0 * stateCount + 1] = 1.0/piR;
183 evec[1 * stateCount + 1] = -1.0/piY;
184 evec[2 * stateCount + 1] = 1.0/piR;
185 evec[3 * stateCount + 1] = -1.0/piY;
186
187 // right eigenvector #3
188 evec[1 * stateCount + 2] = pi[3]/piY;
189 evec[3 * stateCount + 2] = -pi[1]/piY;
190
191 // right eigenvector #4
192 evec[0 * stateCount + 3] = pi[2]/piR;
193 evec[2 * stateCount + 3] = -pi[0]/piR;
194
195 // eigenvectors
196 double[] eval = eigenDecomposition.getEigenValues();
197 final double kappa = getKappa();
198
199 final double beta = -1.0 / (2.0 * (piR * piY + kappa * (pi[0] * pi[2] + pi[1] * pi[3])));
200 final double A_R = 1.0 + piR * (kappa - 1);
201 final double A_Y = 1.0 + piY * (kappa - 1);
202
203 eval[1] = beta;
204 eval[2] = beta*A_Y;
205 eval[3] = beta*A_R;
206
207 updateMatrix = false;
208
209 }
210
211 return eigenDecomposition;
212 }
213
214 //
215 // Private stuff
216 //
217
218 private Statistic tsTvStatistic = new Statistic.Abstract() {
219
220 public String getStatisticName() {
221 return "tsTv";
222 }
223
224 public int getDimension() {
225 return 1;
226 }
227
228 public double getStatisticValue(int dim) {
229 return getTsTv();
230 }
231
232 };
233
234 @Override
235 public Citation.Category getCategory() {
236 return Citation.Category.SUBSTITUTION_MODELS;
237 }
238
239 @Override
240 public String getDescription() {
241 return "HKY nucleotide substitution model";
242 }
243
244 @Override
245 public List<Citation> getCitations() {
246 return Collections.singletonList(CITATION);
247 }
248
249 public static Citation CITATION = new Citation(
250 new Author[]{
251 new Author("M", "Hasegawa"),
252 new Author("H", "Kishino"),
253 new Author("T", "Yano")
254 },
255 "Dating the human-ape splitting by a molecular clock of mitochondrial DNA",
256 1985,
257 "J. Mol. Evol.",
258 22,
259 160, 174,
260 Citation.Status.PUBLISHED
261 );
262
263
264 public static void main(String[] args) {
265 // double kappa = 2.0;
266 // double[] pi = new double[]{0.15,0.30,0.20,0.35};
267 // double time = 0.1;
268
269 double kappa = 1.0;
270 double[] pi = new double[]{0.25,0.25,0.25,0.25};
271 double time = 0.1;
272
273 FrequencyModel freqModel= new FrequencyModel(Nucleotides.INSTANCE, pi);
274 HKY hky = new HKY(kappa,freqModel);
275
276 EigenDecomposition decomp = hky.getEigenDecomposition();
277 // Matrix evec = new Matrix(decomp.getEigenVectors());
278 // Matrix ivec = new Matrix(decomp.getInverseEigenVectors());
279 // System.out.println("Evec =\n"+evec);
280 // System.out.println("Ivec =\n"+ivec);
281
282 Vector eval = new Vector(decomp.getEigenValues());
283 System.out.println("Eval = "+eval);
284
285 double[] probs = new double[16];
286 hky.getTransitionProbabilities(time,probs);
287 System.out.println("new probs = "+new Vector(probs));
288
289 // check against old implementation
290 dr.oldevomodel.substmodel.FrequencyModel oldFreq = new dr.oldevomodel.substmodel.FrequencyModel(Nucleotides.INSTANCE,pi);
291 dr.oldevomodel.substmodel.HKY oldHKY = new dr.oldevomodel.substmodel.HKY(kappa,oldFreq);
292 oldHKY.setKappa(kappa);
293
294 oldHKY.getTransitionProbabilities(time,probs);
295 System.out.println("old probs = "+new Vector(probs));
296
297 }
298 }