Debian Codegraph @

src/dr/oldevomodel/substmodel/YangCodonModel.java

raw symbols SCIP
1 /*
2 * YangCodonModel.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.AminoAcids;
29 import dr.evolution.datatype.Codons;
30 import dr.evolution.datatype.Nucleotides;
31 import dr.oldevomodelxml.substmodel.YangCodonModelParser;
32 import dr.inference.model.Parameter;
33 import dr.inference.model.Statistic;
34
35 /**
36 * Yang model of codon evolution
37 *
38 * @version $Id: YangCodonModel.java,v 1.21 2005/05/24 20:25:58 rambaut Exp $
39 *
40 * @author Andrew Rambaut
41 * @author Alexei Drummond
42 */
43 public class YangCodonModel extends AbstractCodonModel {
44 /** kappa */
45 protected Parameter kappaParameter;
46
47 /** omega */
48 protected Parameter omegaParameter;
49
50 protected byte[] rateMap;
51
52 /**
53 * Constructor
54 */
55 public YangCodonModel(Codons codonDataType,
56 Parameter omegaParameter,
57 Parameter kappaParameter,
58 FrequencyModel freqModel)
59 {
60 super(YangCodonModelParser.YANG_CODON_MODEL, codonDataType, freqModel);
61
62 this.omegaParameter = omegaParameter;
63 addVariable(omegaParameter);
64 omegaParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0,
65 omegaParameter.getDimension()));
66
67 this.kappaParameter = kappaParameter;
68 addVariable(kappaParameter);
69 kappaParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0,
70 kappaParameter.getDimension()));
71
72 constructRateMap();
73
74 addStatistic(synonymousRateStatistic);
75 }
76
77 /**
78 * set kappa
79 */
80 public void setKappa(double kappa) {
81 kappaParameter.setParameterValue(0, kappa);
82 updateMatrix = true;
83 }
84
85 /**
86 * @return kappa
87 */
88 public double getKappa() { return kappaParameter.getParameterValue(0); }
89
90 /**
91 * set dN/dS
92 */
93 public void setOmega(double omega) {
94 omegaParameter.setParameterValue(0, omega);
95 updateMatrix = true;
96 }
97
98
99 /**
100 * @return dN/dS
101 */
102 public double getOmega() { return omegaParameter.getParameterValue(0); }
103
104 public double getSynonymousRate() {
105 double k = getKappa();
106 double o = getOmega();
107 return ((31.0 * k) + 36.0) / ((31.0 * k) + 36.0 + (138.0 * o) + (58.0 * o * k));
108 }
109
110 public double getNonSynonymousRate() {
111 return 0;
112 }
113
114 /**
115 * setup substitution matrix
116 */
117 public void setupRelativeRates()
118 {
119 double kappa = getKappa();
120 double omega = getOmega();
121 for (int i = 0; i < rateCount; i++) {
122 switch (rateMap[i]) {
123 case 0: relativeRates[i] = 0.0; break; // codon changes in more than one codon position
124 case 1: relativeRates[i] = kappa; break; // synonymous transition
125 case 2: relativeRates[i] = 1.0; break; // synonymous transversion
126 case 3: relativeRates[i] = kappa * omega; break;// non-synonymous transition
127 case 4: relativeRates[i] = omega; break; // non-synonymous transversion
128 }
129 }
130
131
132 }
133
134 /**
135 * Construct a map of the rate classes in the rate matrix using the current
136 * genetic code. Classes:
137 * 0: codon changes in more than one codon position (or stop codons)
138 * 1: synonymous transition
139 * 2: synonymous transversion
140 * 3: non-synonymous transition
141 * 4: non-synonymous transversion
142 */
143 protected void constructRateMap()
144 {
145 int u, v, i1, j1, k1, i2, j2, k2;
146 byte rateClass;
147 int[] codon;
148 int cs1, cs2, aa1, aa2;
149
150 int i = 0;
151
152 rateMap = new byte[rateCount];
153
154 for (u = 0; u < stateCount; u++) {
155
156 codon = codonDataType.getTripletStates(u);
157 i1 = codon[0];
158 j1 = codon[1];
159 k1 = codon[2];
160
161 cs1 = codonDataType.getState(i1, j1, k1);
162 aa1 = geneticCode.getAminoAcidState(codonDataType.getCanonicalState(cs1));
163
164 for (v = u + 1; v < stateCount; v++) {
165
166 codon = codonDataType.getTripletStates(v);
167 i2 = codon[0];
168 j2 = codon[1];
169 k2 = codon[2];
170
171 cs2 = codonDataType.getState(i2, j2, k2);
172 aa2 = geneticCode.getAminoAcidState(codonDataType.getCanonicalState(cs2));
173
174 rateClass = -1;
175 if (i1 != i2) {
176 if ( (i1 == 0 && i2 == 2) || (i1 == 2 && i2 == 0) || // A <-> G
177 (i1 == 1 && i2 == 3) || (i1 == 3 && i2 == 1) ) { // C <-> T
178 rateClass = 1; // Transition at position 1
179 } else {
180 rateClass = 2; // Transversion at position 1
181 }
182 }
183 if (j1 != j2) {
184 if (rateClass == -1) {
185 if ( (j1 == 0 && j2 == 2) || (j1 == 2 && j2 == 0) || // A <-> G
186 (j1 == 1 && j2 == 3) || (j1 == 3 && j2 == 1) ) { // C <-> T
187 rateClass = 1; // Transition
188 } else {
189 rateClass = 2; // Transversion
190 }
191 } else
192 rateClass = 0; // Codon changes at more than one position
193 }
194 if (k1 != k2) {
195 if (rateClass == -1) {
196 if ( (k1 == 0 && k2 == 2) || (k1 == 2 && k2 == 0) || // A <-> G
197 (k1 == 1 && k2 == 3) || (k1 == 3 && k2 == 1) ) { // C <-> T
198 rateClass = 1; // Transition
199 } else {
200 rateClass = 2; // Transversion
201 }
202 } else
203 rateClass = 0; // Codon changes at more than one position
204 }
205
206 if (rateClass != 0) {
207 if (aa1 != aa2) {
208 rateClass += 2; // Is a non-synonymous change
209 }
210 }
211
212 rateMap[i] = rateClass;
213 i++;
214 }
215
216 }
217 }
218
219 public void printRateMap()
220 {
221 int u, v, i1, j1, k1, i2, j2, k2;
222 byte rateClass;
223 int[] codon;
224 int cs1, cs2, aa1, aa2;
225
226 System.out.print("\t");
227 for (v = 0; v < stateCount; v++) {
228 codon = codonDataType.getTripletStates(v);
229 i2 = codon[0];
230 j2 = codon[1];
231 k2 = codon[2];
232
233 System.out.print("\t" + Nucleotides.INSTANCE.getChar(i2));
234 System.out.print(Nucleotides.INSTANCE.getChar(j2));
235 System.out.print(Nucleotides.INSTANCE.getChar(k2));
236 }
237 System.out.println();
238
239 System.out.print("\t");
240 for (v = 0; v < stateCount; v++) {
241 codon = codonDataType.getTripletStates(v);
242 i2 = codon[0];
243 j2 = codon[1];
244 k2 = codon[2];
245
246 cs2 = codonDataType.getState(i2, j2, k2);
247 aa2 = geneticCode.getAminoAcidState(codonDataType.getCanonicalState(cs2));
248 System.out.print("\t" + AminoAcids.INSTANCE.getChar(aa2));
249 }
250 System.out.println();
251
252 for (u = 0; u < stateCount; u++) {
253
254 codon = codonDataType.getTripletStates(u);
255 i1 = codon[0];
256 j1 = codon[1];
257 k1 = codon[2];
258
259 System.out.print(Nucleotides.INSTANCE.getChar(i1));
260 System.out.print(Nucleotides.INSTANCE.getChar(j1));
261 System.out.print(Nucleotides.INSTANCE.getChar(k1));
262
263 cs1 = codonDataType.getState(i1, j1, k1);
264 aa1 = geneticCode.getAminoAcidState(codonDataType.getCanonicalState(cs1));
265
266 System.out.print("\t" + AminoAcids.INSTANCE.getChar(aa1));
267
268 for (v = 0; v < stateCount; v++) {
269
270 codon = codonDataType.getTripletStates(v);
271 i2 = codon[0];
272 j2 = codon[1];
273 k2 = codon[2];
274
275 cs2 = codonDataType.getState(i2, j2, k2);
276 aa2 = geneticCode.getAminoAcidState(codonDataType.getCanonicalState(cs2));
277
278 rateClass = -1;
279 if (i1 != i2) {
280 if ( (i1 == 0 && i2 == 2) || (i1 == 2 && i2 == 0) || // A <-> G
281 (i1 == 1 && i2 == 3) || (i1 == 3 && i2 == 1) ) { // C <-> T
282 rateClass = 1; // Transition at position 1
283 } else {
284 rateClass = 2; // Transversion at position 1
285 }
286 }
287 if (j1 != j2) {
288 if (rateClass == -1) {
289 if ( (j1 == 0 && j2 == 2) || (j1 == 2 && j2 == 0) || // A <-> G
290 (j1 == 1 && j2 == 3) || (j1 == 3 && j2 == 1) ) { // C <-> T
291 rateClass = 1; // Transition
292 } else {
293 rateClass = 2; // Transversion
294 }
295 } else
296 rateClass = 0; // Codon changes at more than one position
297 }
298 if (k1 != k2) {
299 if (rateClass == -1) {
300 if ( (k1 == 0 && k2 == 2) || (k1 == 2 && k2 == 0) || // A <-> G
301 (k1 == 1 && k2 == 3) || (k1 == 3 && k2 == 1) ) { // C <-> T
302 rateClass = 1; // Transition
303 } else {
304 rateClass = 2; // Transversion
305 }
306 } else
307 rateClass = 0; // Codon changes at more than one position
308 }
309
310 if (rateClass != 0) {
311 if (aa1 != aa2) {
312 rateClass += 2; // Is a non-synonymous change
313 }
314 }
315
316 System.out.print("\t" + rateClass);
317
318 }
319 System.out.println();
320
321 }
322 }
323
324 // **************************************************************
325 // XHTMLable IMPLEMENTATION
326 // **************************************************************
327
328 public String toXHTML() {
329 StringBuffer buffer = new StringBuffer();
330
331 buffer.append("<em>Yang Codon Model</em> kappa = ");
332 buffer.append(getKappa());
333 buffer.append(", omega = ");
334 buffer.append(getOmega());
335
336 return buffer.toString();
337 }
338
339 private Statistic synonymousRateStatistic = new Statistic.Abstract() {
340
341 public String getStatisticName() {
342 return "synonymousRate";
343 }
344
345 public int getDimension() { return 1; }
346
347 public double getStatisticValue(int dim) {
348 return getSynonymousRate();
349 }
350
351 };
352
353 /* private Statistic nonsynonymousRateStatistic = new Statistic.Abstract() {
354
355 public String getStatisticName() {
356 return "nonSynonymousRate";
357 }
358
359 public int getDimension() { return 1; }
360
361 public double getStatisticValue(int dim) {
362 return getNonSynonymousRate();
363 }
364
365 };*/
366 }