Debian Codegraph @

src/dr/evomodel/branchratemodel/DecayingRateModel.java

raw symbols SCIP
1 /*
2 * DecayingRateModel.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.evomodel.branchratemodel;
27
28 import dr.evolution.tree.NodeRef;
29 import dr.evolution.tree.Tree;
30 import dr.evomodel.tree.TreeModel;
31 import dr.inference.model.Model;
32 import dr.inference.model.Parameter;
33 import dr.inference.model.Variable;
34 import dr.xml.*;
35
36 import java.util.logging.Logger;
37
38 /**
39 * @author Andrew Rambaut
40 * @version $Id: DecayingRateModel.java,v 1.3 2006/01/10 16:48:27 rambaut Exp $
41 */
42 public class DecayingRateModel extends AbstractBranchRateModel {
43
44 public static final String DECAYING_RATE_MODEL = "decayingRateModel";
45 public static final String MUTATION_RATE = "mutationRate";
46 public static final String HALF_LIFE = "halfLife";
47 public static final String SUBSTITUTION_RATE = "substitutionRate";
48 public static final String PROPORTION = "proportion";
49 public static final String AVERAGE = "average";
50
51 private final Parameter mutationRateParameter;
52 private final Parameter substitutionRateParameter;
53 private final Parameter proportionParameter;
54 private final Parameter halfLifeParameter;
55
56 private final boolean useAveraging;
57
58 private final double[] rates;
59 private boolean ratesCalculated = false;
60
61 public DecayingRateModel(TreeModel treeModel, Parameter mutationRateParameter, Parameter substitutionRateParameter,
62 Parameter proportionParameter, Parameter halfLifeParameter, boolean useAveraging) {
63
64 super(DECAYING_RATE_MODEL);
65
66 rates = new double[treeModel.getNodeCount()];
67
68 addModel(treeModel);
69
70 this.mutationRateParameter = mutationRateParameter;
71 this.substitutionRateParameter = substitutionRateParameter;
72 this.proportionParameter = proportionParameter;
73 this.halfLifeParameter = halfLifeParameter;
74
75 addVariable(mutationRateParameter);
76 if (proportionParameter != null) {
77 addVariable(proportionParameter);
78 } else {
79 addVariable(substitutionRateParameter);
80 }
81 addVariable(halfLifeParameter);
82
83 this.useAveraging = useAveraging;
84 }
85
86 public void handleModelChangedEvent(Model model, Object object, int index) {
87 // TreeModel has changed...
88 ratesCalculated = false;
89 fireModelChanged();
90 }
91
92 protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
93
94 // Parameters have changed
95 ratesCalculated = false;
96 fireModelChanged();
97 }
98
99 protected void storeState() {
100 // nothing to do
101 }
102
103 protected void restoreState() {
104 ratesCalculated = false;
105 // nothing to do
106 }
107
108 protected void acceptState() {
109 // nothing to do
110 }
111
112 public double getBranchRate(final Tree tree, final NodeRef node) {
113
114 if (!ratesCalculated) {
115 double mu = mutationRateParameter.getParameterValue(0);
116 double k;
117 if (proportionParameter != null) {
118 k = mu * proportionParameter.getParameterValue(0);
119 } else {
120 k = substitutionRateParameter.getParameterValue(0);
121 }
122
123 double lambda = Math.log(2) / halfLifeParameter.getParameterValue(0);
124
125 calculateNodeRates(tree, tree.getRoot(), mu, k, lambda);
126
127 ratesCalculated = true;
128 }
129
130 return rates[node.getNumber()];
131 }
132
133 /**
134 * Traverse the tree calculating partial likelihoods.
135 *
136 * @return whether the partials for this node were recalculated.
137 */
138 private final double calculateNodeRates(Tree tree, NodeRef node, double mu, double k, double lambda) {
139
140 NodeRef parent = tree.getParent(node);
141
142 double time0 = 0.0;
143
144 if (!tree.isExternal(node)) {
145
146 // Traverse down the two child nodes
147 NodeRef child1 = tree.getChild(node, 0);
148 double t1 = calculateNodeRates(tree, child1, mu, k, lambda);
149
150 NodeRef child2 = tree.getChild(node, 1);
151 double t2 = calculateNodeRates(tree, child2, mu, k, lambda);
152
153 if (useAveraging) {
154 time0 = (t1 + t2) / 2.0;
155 } else {
156 // pick larger of the two
157 if (t1 > t2) {
158 time0 = t1;
159 } else {
160 time0 = t2;
161 }
162 }
163 }
164
165 // don't bother if you are at the root because rate at root is ignored
166 if (parent == null) return 0;
167
168 double branchTime = tree.getNodeHeight(parent) - tree.getNodeHeight(node);
169 double time1 = time0 + branchTime;
170
171 double branchRate = rateIntegral(time1, mu, k, lambda);
172
173 if (time0 > 0.0) {
174 branchRate -= rateIntegral(time0, mu, k, lambda);
175 }
176
177 rates[node.getNumber()] = branchRate / branchTime;
178
179 return time1;
180 }
181
182 private static double rateIntegral(double time, double mu, double k, double lambda) {
183 return ((k * time) + (((mu - k) / lambda) * (1.0 - Math.exp(-lambda * time))));
184 }
185
186 private static double rate(double time, double mu, double k, double lambda) {
187 return k + ((mu - k) * Math.exp(-lambda * time));
188 }
189
190 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
191
192 public String getParserName() {
193 return DECAYING_RATE_MODEL;
194 }
195
196 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
197
198 boolean useAveraging = xo.getBooleanAttribute(AVERAGE);
199
200 TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class);
201
202 Parameter mutationRateParameter = (Parameter) xo.getElementFirstChild(MUTATION_RATE);
203
204 Parameter proportionParameter = null;
205 Parameter substitutionRateParameter = null;
206
207 if (xo.hasChildNamed(PROPORTION)) {
208 proportionParameter = (Parameter) xo.getElementFirstChild(PROPORTION);
209 } else {
210 substitutionRateParameter = (Parameter) xo.getElementFirstChild(SUBSTITUTION_RATE);
211 }
212 Parameter halfLifeParameter = (Parameter) xo.getElementFirstChild(HALF_LIFE);
213
214 Logger.getLogger("dr.evomodel").info("Using decaying-rate clock model.");
215
216 return new DecayingRateModel(treeModel, mutationRateParameter, substitutionRateParameter, proportionParameter, halfLifeParameter, useAveraging);
217 }
218
219 //************************************************************************
220 // AbstractXMLObjectParser implementation
221 //************************************************************************
222
223 public String getParserDescription() {
224 return
225 "This element provides a clock model in which the rate of " +
226 "molecular evolution decays to a substitution rate in the past.";
227 }
228
229 public Class getReturnType() {
230 return DecayingRateModel.class;
231 }
232
233 public XMLSyntaxRule[] getSyntaxRules() {
234 return rules;
235 }
236
237 private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{
238 AttributeRule.newBooleanRule(AVERAGE, false),
239 new ElementRule(TreeModel.class, "The tree model"),
240 new ElementRule(MUTATION_RATE, Parameter.class, "The mutation rate parameter", false),
241 new XORRule(
242 new ElementRule(PROPORTION, Parameter.class, "The proportion of neutral mutations", false),
243 new ElementRule(SUBSTITUTION_RATE, Parameter.class, "The long-term substitution", false)
244 ),
245 new ElementRule(HALF_LIFE, Parameter.class, "The half-life of a deleterious mutation", false)
246 };
247 };
248
249 public static void main(String[] argv) {
250 double mu = 1E-4;
251 double k = 1E-5;
252 double lambda = Math.log(2) / 1.0;
253
254 double time1 = 0.000001;
255 for (int i = 0; i < 100; i++) {
256 System.out.println(time1 + "\t" + rate(time1, mu, k, lambda) + "\t" + (rateIntegral(time1, mu, k, lambda)) / time1);
257 time1 += 1.0;
258 }
259 }
260 }