Debian Codegraph @

src/dr/oldevomodel/substmodel/AbstractCovarionDNAModel.java

raw symbols SCIP
1 /*
2 * AbstractCovarionDNAModel.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.evolution.datatype.OldHiddenNucleotides;
29 import dr.inference.model.Parameter;
30
31 /**
32 * A general time reversible model of nucleotide evolution with
33 * covarion hidden rate categories.
34 *
35 * @author Alexei Drummond
36 * @version $Id$
37 */
38 abstract public class AbstractCovarionDNAModel extends AbstractSubstitutionModel {
39
40 public static final String HIDDEN_CLASS_RATES = "hiddenClassRates";
41 public static final String SWITCHING_RATES = "switchingRates";
42 public static final String FREQUENCIES = "frequencies";
43
44 /**
45 * @param name the name of the covarion substitution model
46 * @param dataType the data type
47 * @param freqModel the equlibrium frequencies
48 * @param hiddenClassRates the relative rates of the hidden categories
49 * (first hidden category has rate 1.0 so this parameter
50 * has dimension one less than number of hidden categories.
51 * each hidden category.
52 * @param switchingRates rate of switching between hidden categories
53 */
54 public AbstractCovarionDNAModel(String name,
55 OldHiddenNucleotides dataType,
56 Parameter hiddenClassRates,
57 Parameter switchingRates,
58 FrequencyModel freqModel) {
59
60 super(name, dataType, freqModel);
61
62 hiddenClassCount = dataType.getHiddenClassCount();
63
64 this.hiddenClassRates = hiddenClassRates;
65 this.switchingRates = switchingRates;
66
67 assert hiddenClassRates.getDimension() == hiddenClassCount - 1;
68
69 int hiddenClassCount = getHiddenClassCount();
70
71 int switchingClassCount = hiddenClassCount * (hiddenClassCount - 1) / 2;
72
73 if (switchingRates.getDimension() != switchingClassCount) {
74 throw new IllegalArgumentException("switching rate parameter must have " +
75 switchingClassCount + " rates for " + hiddenClassCount + " classes");
76 }
77 addVariable(switchingRates);
78 addVariable(hiddenClassRates);
79 constructRateMatrixMap();
80 }
81
82 /**
83 * @return the relative rates of A<->C, A<->G, A<->T, C<->G, C<->T and G<->T substitutions
84 */
85 abstract double[] getRelativeDNARates();
86
87 /**
88 * @return the number of hidden classes in this covarion model.
89 */
90 public final int getHiddenClassCount() {
91 return hiddenClassCount;
92 }
93
94 public void frequenciesChanged() {
95 // DO NOTHING
96 }
97
98 public void ratesChanged() {
99 setupRelativeRates();
100 }
101
102 protected void setupRelativeRates() {
103
104 double[] phi = switchingRates.getParameterValues();
105 double[] rr = getRelativeDNARates();
106 double[] hiddenRates = hiddenClassRates.getParameterValues();
107
108 for (int i = 0; i < rateCount; i++) {
109 if (rateMatrixMap[i] == 0) {
110 relativeRates[i] = 0.0;
111 } else if (rateMatrixMap[i] < 7) {
112 if (hiddenClassMap[i] == 0) {
113 relativeRates[i] = rr[rateMatrixMap[i] - 1];
114 } else {
115 relativeRates[i] = rr[rateMatrixMap[i] - 1] * hiddenRates[hiddenClassMap[i] - 1];
116 }
117 } else {
118 relativeRates[i] = phi[rateMatrixMap[i] - 7];
119 }
120 }
121 }
122
123 /**
124 * Construct a map of the rate classes in the rate matrix:
125 * 0: class and nucleotide change
126 * 1: A <-> C
127 * 2: A <-> G
128 * 3: A <-> T
129 * 4: C <-> G
130 * 5: C <-> T
131 * 6: G <-> T
132 * 7: 0 <-> 1 class change
133 * 8: 0 <-> 2 class change
134 * et cetera
135 */
136 private void constructRateMatrixMap() {
137
138 byte rateClass;
139 int fromNuc, toNuc;
140 int fromRate, toRate;
141 int count = 0;
142
143 rateMatrixMap = new byte[rateCount];
144 hiddenClassMap = new byte[rateCount];
145
146 for (int i = 0; i < stateCount; i++) {
147
148 for (int j = i + 1; j < stateCount; j++) {
149
150 fromNuc = i % 4;
151 toNuc = j % 4;
152 fromRate = i / 4;
153 toRate = j / 4;
154
155 if (fromNuc == toNuc) {
156 // rate transition
157 if (fromRate == toRate) {
158 throw new RuntimeException("Shouldn't be possible");
159 }
160
161 rateClass = (byte) (7 + getIndex(fromRate, toRate, hiddenClassCount));
162 } else if (fromRate != toRate) {
163 rateClass = 0;
164 } else {
165 rateClass = (byte) (1 + getIndex(fromNuc, toNuc, 4));
166 }
167
168 rateMatrixMap[count] = rateClass;
169 hiddenClassMap[count] = (byte) fromRate;
170 count++;
171 }
172 }
173 }
174
175 private int getIndex(int from, int to, int size) {
176 int index = 0;
177
178 int f = from;
179 while (f > 0) {
180 index += size - 1;
181 f -= 1;
182 size -= 1;
183 }
184 index += to - from - 1;
185
186 return index;
187 }
188
189 /**
190 * Normalize rate matrix to one expected substitution per unit time
191 *
192 * @param matrix the matrix to normalize to one expected substitution
193 * @param pi the equilibrium distribution of states
194 */
195 void normalize(double[][] matrix, double[] pi) {
196 double subst = 0.0;
197 int dimension = pi.length;
198
199 for (int i = 0; i < dimension; i++) {
200 subst += -matrix[i][i] * pi[i];
201 }
202
203 // normalize, including switches
204 for (int i = 0; i < dimension; i++) {
205 for (int j = 0; j < dimension; j++) {
206 matrix[i][j] = matrix[i][j] / subst;
207 }
208 }
209
210 double switchingProportion = 0.0;
211 for (int i = 0; i < hiddenClassCount; i++) {
212 for (int j = i + 1; j < hiddenClassCount; j++) {
213 for (int l = 0; l < 4; l++) {
214
215 int x = i * 4 + l;
216 int y = j * 4 + l;
217
218 switchingProportion += matrix[x][y] * pi[y];
219 switchingProportion += matrix[y][x] * pi[x];
220 }
221 }
222 }
223 // normalize, removing switches
224 for (int i = 0; i < dimension; i++) {
225 for (int j = 0; j < dimension; j++) {
226 matrix[i][j] = matrix[i][j] / (1.0 - switchingProportion);
227 }
228 }
229 }
230
231 Parameter switchingRates;
232 Parameter hiddenClassRates;
233
234 byte[] rateMatrixMap;
235 byte[] hiddenClassMap;
236
237 private int hiddenClassCount;
238 }