Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * TN93.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.evolution.datatype.Nucleotides;
32 import dr.inference.model.Parameter;
33 import dr.util.Author;
34 import dr.util.Citable;
35 import dr.util.Citation;
36
37 import java.util.Collections;
38 import java.util.List;
39
40 /**
41 * Tamura-Nei model of nucleotide evolution
42 *
43 * @author Marc A. Suchard
44 */
45 public class TN93 extends BaseSubstitutionModel implements Citable {
46
47 private Parameter kappaParameter1 = null;
48 private Parameter kappaParameter2 = null;
49
50 public TN93(Parameter kappaParameter1, Parameter kappaParameter2, FrequencyModel freqModel) {
51
52 super("TN93", Nucleotides.INSTANCE, freqModel);
53
54 this.kappaParameter1 = kappaParameter1;
55 addVariable(kappaParameter1);
56 kappaParameter1.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));
57
58 this.kappaParameter2 = kappaParameter2;
59 addVariable(kappaParameter2);
60 kappaParameter2.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));
61 }
62
63 public final double getKappa1() {
64 return kappaParameter1.getParameterValue(0);
65 }
66
67 public final double getKappa2() {
68 return kappaParameter2.getParameterValue(0);
69 }
70
71 protected void frequenciesChanged() {
72 }
73
74 protected void ratesChanged() {
75 }
76
77 protected void setupRelativeRates(double[] rates) {
78 double kappa1 = getKappa1();
79 double kappa2 = getKappa2();
80 rates[0] = 1.0;
81 rates[1] = kappa1;
82 rates[2] = 1.0;
83 rates[3] = 1.0;
84 rates[4] = kappa2;
85 rates[5] = 1.0;
86 }
87
88 public synchronized EigenDecomposition getEigenDecomposition() {
89
90 if (eigenDecomposition == null) {
91 double[] evec = new double[stateCount * stateCount];
92 double[] ivec = new double[stateCount * stateCount];
93 double[] eval = new double[stateCount];
94 eigenDecomposition = new EigenDecomposition(evec, ivec, eval);
95
96 ivec[2 * stateCount + 1] = 1; // left eigenvectors 3 = (0,1,0,-1); 4 = (1,0,-1,0)
97 ivec[2 * stateCount + 3] = -1;
98
99 ivec[3 * stateCount + 0] = 1;
100 ivec[3 * stateCount + 2] = -1;
101
102 evec[0 * stateCount + 0] = 1; // right eigenvector 1 = (1,1,1,1)'
103 evec[1 * stateCount + 0] = 1;
104 evec[2 * stateCount + 0] = 1;
105 evec[3 * stateCount + 0] = 1;
106
107 }
108
109 if (updateMatrix) {
110
111 double[] evec = eigenDecomposition.getEigenVectors();
112 double[] ivec = eigenDecomposition.getInverseEigenVectors();
113 double[] pi = freqModel.getFrequencies();
114 double piR = pi[0] + pi[2];
115 double piY = pi[1] + pi[3];
116
117 // left eigenvector #1
118 ivec[0 * stateCount + 0] = pi[0]; // or, evec[0] = pi;
119 ivec[0 * stateCount + 1] = pi[1];
120 ivec[0 * stateCount + 2] = pi[2];
121 ivec[0 * stateCount + 3] = pi[3];
122
123 // left eigenvector #2
124 ivec[1 * stateCount + 0] = pi[0] * piY;
125 ivec[1 * stateCount + 1] = -pi[1] * piR;
126 ivec[1 * stateCount + 2] = pi[2] * piY;
127 ivec[1 * stateCount + 3] = -pi[3] * piR;
128
129 // right eigenvector #2
130 evec[0 * stateCount + 1] = 1.0 / piR;
131 evec[1 * stateCount + 1] = -1.0 / piY;
132 evec[2 * stateCount + 1] = 1.0 / piR;
133 evec[3 * stateCount + 1] = -1.0 / piY;
134
135 // right eigenvector #3
136 evec[1 * stateCount + 2] = pi[3] / piY;
137 evec[3 * stateCount + 2] = -pi[1] / piY;
138
139 // right eigenvector #4
140 evec[0 * stateCount + 3] = pi[2] / piR;
141 evec[2 * stateCount + 3] = -pi[0] / piR;
142
143 // eigenvectors
144 double[] eval = eigenDecomposition.getEigenValues();
145
146 final double kappa1 = getKappa1();
147 final double kappa2 = getKappa2();
148 final double beta = -1.0 / (2.0 * (piR * piY + kappa1 * pi[0] * pi[2] + kappa2 * pi[1] * pi[3]));
149 final double A_R = 1.0 + piR * (kappa1 - 1);
150 final double A_Y = 1.0 + piY * (kappa2 - 1);
151
152 eval[1] = beta;
153 eval[2] = beta * A_Y;
154 eval[3] = beta * A_R;
155
156 updateMatrix = false;
157 }
158
159 return eigenDecomposition;
160 }
161
162 @Override
163 public Citation.Category getCategory() {
164 return Citation.Category.SUBSTITUTION_MODELS;
165 }
166
167 @Override
168 public String getDescription() {
169 return "Tamura-Nei nucleotide substitution model";
170 }
171
172 @Override
173 public List<Citation> getCitations() {
174 return Collections.singletonList(CITATION);
175 }
176
177 public static Citation CITATION = new Citation(
178 new Author[]{
179 new Author("K", "Tamura"),
180 new Author("M", "Nei")
181 },
182 "Estimation of the number of nucleotide substitutions in the control region of mitochondrial DNA in humans and chimpanzees",
183 1993,
184 "Mol Biol Evol",
185 10, 512, 526
186 );
187 }