Debian Codegraph @

src/dr/evomodel/treelikelihood/ApproximatePoissonTreeLikelihood.java

raw symbols SCIP
1 /*
2 * AbstractTreeLikelihood.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.treelikelihood;
27
28 import dr.evolution.tree.NodeRef;
29 import dr.evolution.tree.Tree;
30 import dr.evolution.tree.TreeUtils;
31 import dr.evomodel.branchratemodel.BranchRateModel;
32 import dr.evomodel.tree.TreeChangedEvent;
33 import dr.evomodel.tree.TreeModel;
34 import dr.inference.model.AbstractModelLikelihood;
35 import dr.inference.model.Model;
36 import dr.inference.model.Parameter;
37 import dr.inference.model.Variable;
38 import dr.math.distributions.GammaDistribution;
39 import dr.xml.Reportable;
40
41 /**
42 * ApproximatePoissonTreeLikelihood - a tree likelihood which uses an ML tree as expected number
43 * of substitutions and assumes that these are drawn from a gamma (with mean == variance).
44 *
45 * This is similar to work by Jeff Thorne and colleagues:
46 *
47 *
48 * And more recently Didelot et al (2018) BioRxiv
49 *
50 * @author Andrew Rambaut
51 */
52
53 public class ApproximatePoissonTreeLikelihood extends AbstractModelLikelihood implements Reportable {
54
55 public ApproximatePoissonTreeLikelihood(String name, Tree dataTree, int sequenceLength, TreeModel treeModel, BranchRateModel branchRateModel) {
56
57 super(name);
58
59 this.sequenceLength = sequenceLength;
60
61 this.treeModel = treeModel;
62 addModel(treeModel);
63
64 this.branchRateModel = branchRateModel;
65 addModel(branchRateModel);
66
67 updateNode = new boolean[treeModel.getNodeCount()];
68 branchLengths = new double[treeModel.getNodeCount()];
69
70 for (int i = 0; i < treeModel.getNodeCount(); i++) {
71 updateNode[i] = true;
72
73 if (!dataTree.isRoot(dataTree.getNode(i))) {
74 // Adding a small epsilon to avoid zeros.
75 branchLengths[i] = dataTree.getBranchLength(dataTree.getNode(i)) * sequenceLength + 1.0E-8;
76 }
77 }
78
79 distanceMatrix = new double[treeModel.getExternalNodeCount()][];
80 for (int i = 0; i < treeModel.getExternalNodeCount(); i++) {
81 distanceMatrix[i] = new double[treeModel.getExternalNodeCount()];
82 for (int j = i + 1; j < treeModel.getExternalNodeCount(); j++) {
83 distanceMatrix[i][j] = distanceMatrix[j][i] = TreeUtils.getPathLength(dataTree, dataTree.getNode(i), dataTree.getNode(j));
84 }
85 }
86
87 branchLogL = new double[treeModel.getNodeCount()];
88 storedBranchLogL = new double[treeModel.getNodeCount()];
89
90 likelihoodKnown = false;
91
92 gamma = new GammaDistribution(1.0, 1.0);
93 }
94
95 /**
96 * Set update flag for a node only
97 */
98 protected void updateNode(NodeRef node) {
99 updateNode[node.getNumber()] = true;
100 likelihoodKnown = false;
101 }
102
103 /**
104 * Set update flag for a node and its direct children
105 */
106 protected void updateNodeAndChildren(NodeRef node) {
107 updateNode[node.getNumber()] = true;
108
109 for (int i = 0; i < treeModel.getChildCount(node); i++) {
110 NodeRef child = treeModel.getChild(node, i);
111 updateNode[child.getNumber()] = true;
112 }
113 likelihoodKnown = false;
114 }
115
116 /**
117 * Set update flag for a node and all its descendents
118 */
119 protected void updateNodeAndDescendents(NodeRef node) {
120 updateNode[node.getNumber()] = true;
121
122 for (int i = 0; i < treeModel.getChildCount(node); i++) {
123 NodeRef child = treeModel.getChild(node, i);
124 updateNodeAndDescendents(child);
125 }
126
127 likelihoodKnown = false;
128 }
129
130 /**
131 * Set update flag for all nodes
132 */
133 protected void updateAllNodes() {
134 for (int i = 0; i < treeModel.getNodeCount(); i++) {
135 updateNode[i] = true;
136 }
137 likelihoodKnown = false;
138 }
139
140 // **************************************************************
141 // VariableListener IMPLEMENTATION
142 // **************************************************************
143
144 protected void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
145 // do nothing
146 }
147
148 // **************************************************************
149 // ModelListener IMPLEMENTATION
150 // **************************************************************
151
152 /**
153 * Handles model changed events from the submodels.
154 */
155 protected void handleModelChangedEvent(Model model, Object object, int index) {
156
157 fireModelChanged();
158
159 if (model == treeModel) {
160 if (object instanceof TreeChangedEvent) {
161
162 if (((TreeChangedEvent) object).isNodeChanged()) {
163 // If a node event occurs the node and its two child nodes
164 // are flagged for updating (this will result in everything
165 // above being updated as well. Node events occur when a node
166 // is added to a branch, removed from a branch or its height or
167 // rate changes.
168 updateNodeAndChildren(((TreeChangedEvent) object).getNode());
169
170 } else if (((TreeChangedEvent) object).isTreeChanged()) {
171 // Full tree events result in a complete updating of the tree likelihood
172 // This event type is now used for EmpiricalTreeDistributions.
173 // System.err.println("Full tree update event - these events currently aren't used\n" +
174 // "so either this is in error or a new feature is using them so remove this message.");
175 updateAllNodes();
176 } else {
177 // Other event types are ignored (probably trait changes).
178 //System.err.println("Another tree event has occured (possibly a trait change).");
179 }
180 }
181
182 } else if (model == branchRateModel) {
183 if (index == -1) {
184 updateAllNodes();
185 } else {
186 updateNode(treeModel.getNode(index));
187 }
188
189 } else {
190
191 throw new RuntimeException("Unknown componentChangedEvent");
192 }
193 }
194
195 /**
196 * Stores the additional state other than model components
197 */
198 protected void storeState() {
199 storedLikelihoodKnown = likelihoodKnown;
200 storedLogLikelihood = logLikelihood;
201
202 System.arraycopy(branchLogL, 0, storedBranchLogL, 0, branchLogL.length);
203 }
204
205 /**
206 * Restore the additional stored state
207 */
208 protected void restoreState() {
209
210 likelihoodKnown = storedLikelihoodKnown;
211 logLikelihood = storedLogLikelihood;
212
213 System.arraycopy(storedBranchLogL, 0, branchLogL, 0, branchLogL.length);
214 }
215
216 protected void acceptState() {
217 } // nothing to do
218
219 // **************************************************************
220 // Likelihood IMPLEMENTATION
221 // **************************************************************
222
223 private double calculateLogLikelihood() {
224
225 // makeDirty();
226
227 int root = treeModel.getRoot().getNumber();
228 int rootChild1 = treeModel.getChild(treeModel.getRoot(), 0).getNumber();
229 int rootChild2 = treeModel.getChild(treeModel.getRoot(), 1).getNumber();
230
231 //
232 updateNode[rootChild1] = updateNode[rootChild1] || updateNode[rootChild2];
233
234 double logL = 0.0;
235 for (int i = 0; i < treeModel.getNodeCount(); i++) {
236 if (updateNode[i] && i != root && i != rootChild2) {
237 NodeRef node = treeModel.getNode(i);
238 // skip the root and the second child of the root (this is added to the first child)
239
240 double shape = treeModel.getBranchLength(node) * branchRateModel.getBranchRate(treeModel, node);
241 double x = branchLengths[i];
242
243 if (i == rootChild1) {
244 // sum the branches on both sides of the root
245 NodeRef node2 = treeModel.getNode(rootChild2);
246 shape += treeModel.getBranchLength(node2) * branchRateModel.getBranchRate(treeModel, node2);
247 x += branchLengths[rootChild2];
248 }
249 gamma.setShape(shape * sequenceLength);
250 gamma.setScale(1.0);
251 branchLogL[i] = gamma.logPdf(x);
252 }
253 updateNode[i] = false;
254 logL += branchLogL[i];
255 }
256
257 return logL;
258 }
259
260 private void calculateLogLikelihood(NodeRef node) {
261 NodeRef c1 = treeModel.getChild(node, 0);
262 if (!treeModel.isExternal(c1)) {
263 calculateLogLikelihood(c1);
264 }
265 NodeRef c2 = treeModel.getChild(node, 1);
266 if (!treeModel.isExternal(c2)) {
267 calculateLogLikelihood(c2);
268 }
269
270
271 }
272
273 public final Model getModel() {
274 return this;
275 }
276
277 public final double getLogLikelihood() {
278 if (!likelihoodKnown) {
279 logLikelihood = calculateLogLikelihood();
280 likelihoodKnown = true;
281 }
282 return logLikelihood;
283 }
284
285 /**
286 * Forces a complete recalculation of the likelihood next time getLikelihood is called
287 */
288 public void makeDirty() {
289 likelihoodKnown = false;
290 updateAllNodes();
291 }
292
293 public String getReport() {
294 return getClass().getName() + "(" + getLogLikelihood() + ")";
295 }
296
297 // **************************************************************
298 // INSTANCE VARIABLES
299 // **************************************************************
300
301 /**
302 * the tree
303 */
304 private final TreeModel treeModel;
305
306 private final BranchRateModel branchRateModel;
307
308 private final int sequenceLength;
309
310 private final double[] branchLengths;
311 private final double[][] distanceMatrix;
312
313 private final GammaDistribution gamma;
314
315 /**
316 * Flags to specify which nodes are to be updated
317 */
318 protected boolean[] updateNode;
319
320 private double logLikelihood;
321 private double storedLogLikelihood;
322 private boolean likelihoodKnown;
323 private boolean storedLikelihoodKnown = false;
324
325 private double[] branchLogL;
326 private double[] storedBranchLogL;
327
328
329 }