Debian Codegraph @

src/dr/oldevomodel/substmodel/PseudoCodonModel.java

raw symbols SCIP
1 /*
2 * PseudoCodonModel.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.Nucleotides;
29 import dr.oldevomodel.sitemodel.GammaSiteModel;
30 import dr.inference.model.AbstractModel;
31 import dr.inference.model.Model;
32 import dr.inference.model.Parameter;
33 import dr.inference.model.Variable;
34 import dr.xml.*;
35 import org.w3c.dom.Document;
36 import org.w3c.dom.Element;
37
38 /**
39 * PseudoCodonModel - provides an approximation to the codon substitution model.
40 *
41 * @author Alexei Drummond
42 * @author Andrew Rambaut
43 * @version $Id: PseudoCodonModel.java,v 1.9 2005/05/24 20:25:58 rambaut Exp $
44 */
45
46 public class PseudoCodonModel extends AbstractModel {
47
48 //
49 // Public stuff
50 //
51
52 public static final String PSEUDO_CODON_MODEL = "pseudoCodonModel";
53 public static final String MU = "mu";
54 public static final String OMEGA = "omega";
55 public static final String KAPPA = "kappa";
56 public static final String FIRST_POSITION = "firstPosition";
57 public static final String SECOND_POSITION = "secondPosition";
58 public static final String THIRD_POSITION = "thirdPosition";
59
60 /**
61 * Constructor
62 */
63 public PseudoCodonModel(GammaSiteModel siteModel1,
64 GammaSiteModel siteModel2,
65 GammaSiteModel siteModel3,
66 Parameter muParameter,
67 Parameter omegaParameter,
68 Parameter kappaParameter,
69 FrequencyModel freqModel) {
70
71 super(PSEUDO_CODON_MODEL);
72
73 this.gtr1 = (GTR) siteModel1.getSubstitutionModel();
74 this.siteModel1 = siteModel1;
75
76 this.gtr2 = (GTR) siteModel2.getSubstitutionModel();
77 this.siteModel2 = siteModel2;
78
79 this.gtr3 = (GTR) siteModel3.getSubstitutionModel();
80 this.siteModel3 = siteModel3;
81
82 if (freqModel.getDataType() != Nucleotides.INSTANCE) {
83 throw new IllegalArgumentException("Datatypes do not match!");
84 }
85
86 this.frequencyModel = freqModel;
87 addModel(frequencyModel);
88
89 this.muParameter = muParameter;
90 muParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));
91 addVariable(muParameter);
92
93 this.omegaParameter = omegaParameter;
94 omegaParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));
95 addVariable(omegaParameter);
96
97 this.kappaParameter = kappaParameter;
98 kappaParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));
99 addVariable(kappaParameter);
100
101 excludeStopCodons = true;
102
103 calculateSubstitutionModel();
104 }
105
106 // **************************************************************
107 // Model IMPLEMENTATION
108 // **************************************************************
109
110 protected void handleModelChangedEvent(Model model, Object object, int index) {
111 // frequencyModel changed!
112 calculateSubstitutionModel();
113 }
114
115 protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
116 // parameter changed!
117 calculateSubstitutionModel();
118 }
119
120 protected void storeState() {
121 } // no additional state needs storing
122
123 protected void restoreState() {
124 } // no additional state needs restoring
125
126 protected void acceptState() {
127 } // no additional state needs accepting
128
129 //
130 // Private stuff
131 //
132
133 private void calculateSubstitutionModel() {
134 double omega = omegaParameter.getParameterValue(0);
135 double kappa = kappaParameter.getParameterValue(0);
136 double mu = muParameter.getParameterValue(0);
137
138 double[] frequencies = frequencyModel.getFrequencies();
139 double piAC = frequencies[0] * frequencies[1];
140 double piAG = frequencies[0] * frequencies[2];
141 double piAT = frequencies[0] * frequencies[3];
142 double piCG = frequencies[1] * frequencies[2];
143 double piCT = frequencies[1] * frequencies[3];
144 double piGT = frequencies[2] * frequencies[3];
145
146 double[][] rates = new double[3][6];
147
148 if (excludeStopCodons) {
149 // A-C
150 rates[0][0] = ((omega * 14.0 / 16.0) + (2.0 / 16.0));
151 rates[1][0] = omega;
152 rates[2][0] = ((omega * 5.0 / 14.0) + (9.0 / 14.0));
153
154 // A-G
155 rates[0][1] = omega * kappa;
156 rates[1][1] = rates[0][1];
157 rates[2][1] = ((omega / 14.0) + (13.0 / 14.0)) * kappa;
158
159 // A-T
160 rates[0][2] = omega;
161 rates[1][2] = rates[0][2];
162 rates[2][2] = ((omega * 5.0 / 14.0) + (9.0 / 14.0));
163
164 // C-G
165 rates[0][3] = omega;
166 rates[1][3] = rates[0][3];
167 rates[2][3] = ((omega * 7.0 / 15.0) + (8.0 / 15.0));
168
169 // C-T
170 rates[0][4] = ((omega * 11.0 / 13.0) + (2.0 / 13.0)) * kappa;
171 rates[1][4] = omega * kappa;
172 rates[2][4] = kappa;
173
174 // G-T
175 rates[0][5] = omega;
176 rates[1][5] = rates[0][5];
177 rates[2][5] = ((omega * 7.0 / 15.0) + (8.0 / 15.0));
178 } else {
179 // A-C
180 rates[0][0] = ((omega * 14.0 / 16.0) + (2.0 / 16.0));
181 rates[1][0] = omega;
182 rates[2][0] = ((omega * 5.0 / 16.0) + (9.0 / 16.0));
183
184 // A-G
185 rates[0][1] = omega * kappa;
186 rates[1][1] = rates[0][1];
187 rates[2][1] = ((omega / 16.0) + (13.0 / 16.0)) * kappa;
188
189 // A-T
190 rates[0][2] = (omega * 13.0 / 16.0);
191 rates[1][2] = omega;
192 rates[2][2] = ((omega * 5.0 / 16.0) + (9.0 / 16.0));
193
194 // C-G
195 rates[0][3] = omega;
196 rates[1][3] = rates[0][3];
197 rates[2][3] = ((omega * 7.0 / 16.0) + (8.0 / 16.0));
198
199 // C-T
200 rates[0][4] = ((omega * 11.0 / 16.0) + (2.0 / 16.0)) * kappa;
201 rates[1][4] = omega * kappa;
202 rates[2][4] = kappa;
203
204 // G-T
205 rates[0][5] = (omega * 13.0 / 16.0);
206 rates[1][5] = omega;
207 rates[2][5] = ((omega * 7.0 / 16.0) + (8.0 / 16.0));
208 }
209
210 double sumRates1 = (rates[0][0] * piAC) +
211 (rates[0][1] * piAG) +
212 (rates[0][2] * piAT) +
213 (rates[0][3] * piCG) +
214 (rates[0][4] * piCT) +
215 (rates[0][5] * piGT);
216 double sumRates2 = (rates[1][0] * piAC) +
217 (rates[1][1] * piAG) +
218 (rates[1][2] * piAT) +
219 (rates[1][3] * piCG) +
220 (rates[1][4] * piCT) +
221 (rates[1][5] * piGT);
222 double sumRates3 = (rates[2][0] * piAC) +
223 (rates[2][1] * piAG) +
224 (rates[2][2] * piAT) +
225 (rates[2][3] * piCG) +
226 (rates[2][4] * piCT) +
227 (rates[2][5] * piGT);
228
229 double f = (mu * 3.0) / (sumRates1 + sumRates2 + sumRates3);
230
231 double mu1 = f * sumRates1;
232 double mu2 = f * sumRates2;
233 double mu3 = f * sumRates3;
234
235 gtr1.setAbsoluteRates(rates[0], 4);
236 siteModel1.setMu(mu1);
237 gtr2.setAbsoluteRates(rates[1], 4);
238 siteModel2.setMu(mu2);
239 gtr3.setAbsoluteRates(rates[2], 4);
240 siteModel3.setMu(mu3);
241 }
242
243 // **************************************************************
244 // XMLElement IMPLEMENTATION
245 // **************************************************************
246
247 public Element createElement(Document d) {
248 throw new RuntimeException("createElement not implemented");
249 }
250
251 public static XMLObjectParser PSEUDO_CODON_MODEL_PARSER = new AbstractXMLObjectParser() {
252
253 public String getParserName() {
254 return PSEUDO_CODON_MODEL;
255 }
256
257 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
258
259 Parameter muParam = (Parameter) xo.getElementFirstChild(MU);
260 Parameter omegaParam = (Parameter) xo.getElementFirstChild(OMEGA);
261 Parameter kappaParam = (Parameter) xo.getElementFirstChild(KAPPA);
262
263 GammaSiteModel siteModel1 = (GammaSiteModel) xo.getElementFirstChild(FIRST_POSITION);
264 GammaSiteModel siteModel2 = (GammaSiteModel) xo.getElementFirstChild(SECOND_POSITION);
265 GammaSiteModel siteModel3 = (GammaSiteModel) xo.getElementFirstChild(THIRD_POSITION);
266
267 if (!(siteModel1.getSubstitutionModel() instanceof GTR) ||
268 !(siteModel2.getSubstitutionModel() instanceof GTR) ||
269 !(siteModel3.getSubstitutionModel() instanceof GTR)) {
270 throw new XMLParseException("Substitution models in " + getParserName() + " elements must be GTRs");
271 }
272
273 GTR gtr = (GTR) siteModel1.getSubstitutionModel();
274 FrequencyModel freqModel = gtr.getFrequencyModel();
275
276 return new PseudoCodonModel(siteModel1, siteModel2, siteModel3, muParam, omegaParam, kappaParam, freqModel);
277 }
278 //************************************************************************
279 // AbstractXMLObjectParser implementation
280 //************************************************************************
281
282 public String getParserDescription() {
283 return "This element represents the Pseudo-Codon model of nucleotide evolution.";
284 }
285
286 public Class getReturnType() {
287 return PseudoCodonModel.class;
288 }
289
290 public XMLSyntaxRule[] getSyntaxRules() {
291 return rules;
292 }
293
294 private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{
295 new ElementRule(MU, Parameter.class),
296 new ElementRule(OMEGA, Parameter.class),
297 new ElementRule(KAPPA, Parameter.class),
298 new ElementRule(FIRST_POSITION, GammaSiteModel.class),
299 new ElementRule(SECOND_POSITION, GammaSiteModel.class),
300 new ElementRule(THIRD_POSITION, GammaSiteModel.class)
301 };
302 };
303
304 // **************************************************************
305 // INSTANCE VARIABLES
306 // **************************************************************
307
308 /**
309 * the site models
310 */
311 private GammaSiteModel siteModel1, siteModel2, siteModel3;
312 private GTR gtr1, gtr2, gtr3;
313 private FrequencyModel frequencyModel;
314 private boolean excludeStopCodons;
315
316 protected Parameter muParameter = null;
317 protected Parameter omegaParameter = null;
318 protected Parameter kappaParameter = null;
319 }