Debian Codegraph @

src/dr/evomodel/coalescent/AbstractCoalescentLikelihood.java

raw symbols SCIP
1 /*
2 * AbstractCoalescentLikelihood.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.coalescent;
27
28 import dr.evolution.coalescent.IntervalList;
29 import dr.evolution.coalescent.IntervalType;
30 import dr.evolution.coalescent.Intervals;
31 import dr.evolution.tree.NodeRef;
32 import dr.evolution.tree.Tree;
33 import dr.evolution.tree.TreeUtils;
34 import dr.evolution.util.TaxonList;
35 import dr.evolution.util.Units;
36 import dr.evomodel.tree.TreeModel;
37 import dr.inference.model.*;
38
39 import java.util.HashSet;
40 import java.util.List;
41 import java.util.Set;
42
43
44 /**
45 * Forms a base class for a number of coalescent likelihood calculators.
46 *
47 * @author Andrew Rambaut
48 * @author Alexei Drummond
49 * @version $Id: CoalescentLikelihood.java,v 1.43 2006/07/28 11:27:32 rambaut Exp $
50 */
51 public abstract class AbstractCoalescentLikelihood extends AbstractModelLikelihood implements Units, CoalescentIntervalProvider {
52
53 // PUBLIC STUFF
54
55 public AbstractCoalescentLikelihood(
56 String name,
57 Tree tree,
58 TaxonList includeSubtree,
59 List<TaxonList> excludeSubtrees) throws TreeUtils.MissingTaxonException {
60
61 super(name);
62
63 this.tree = tree;
64
65 if (includeSubtree != null) {
66 includedLeafSet = TreeUtils.getLeavesForTaxa(tree, includeSubtree);
67 } else {
68 includedLeafSet = null;
69 }
70
71 if (excludeSubtrees != null) {
72 excludedLeafSets = new Set[excludeSubtrees.size()];
73 for (int i = 0; i < excludeSubtrees.size(); i++) {
74 excludedLeafSets[i] = TreeUtils.getLeavesForTaxa(tree, excludeSubtrees.get(i));
75 }
76 } else {
77 excludedLeafSets = new Set[0];
78 }
79
80 if (tree instanceof TreeModel) {
81 addModel((TreeModel) tree);
82 }
83
84 intervals = new Intervals(tree.getNodeCount());
85 storedIntervals = new Intervals(tree.getNodeCount());
86 eventsKnown = false;
87
88 this.coalescentEventStatisticValues = new double[getNumberOfCoalescentEvents()];
89
90 addStatistic(new DeltaStatistic());
91
92 likelihoodKnown = false;
93 }
94
95 // **************************************************************
96 // ModelListener IMPLEMENTATION
97 // **************************************************************
98
99 protected final void handleModelChangedEvent(Model model, Object object, int index) {
100 if (model == tree) {
101 // treeModel has changed so recalculate the intervals
102 eventsKnown = false;
103 }
104
105 likelihoodKnown = false;
106 }
107
108 // **************************************************************
109 // VariableListener IMPLEMENTATION
110 // **************************************************************
111
112 protected void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
113 } // No parameters to respond to
114
115 // **************************************************************
116 // Model IMPLEMENTATION
117 // **************************************************************
118
119 /**
120 * Stores the precalculated state: in this case the intervals
121 */
122 protected final void storeState() {
123 // copy the intervals into the storedIntervals
124 storedIntervals.copyIntervals(intervals);
125
126 storedEventsKnown = eventsKnown;
127 storedLikelihoodKnown = likelihoodKnown;
128 storedLogLikelihood = logLikelihood;
129 }
130
131 /**
132 * Restores the precalculated state: that is the intervals of the tree.
133 */
134 protected final void restoreState() {
135 // swap the intervals back
136 Intervals tmp = storedIntervals;
137 storedIntervals = intervals;
138 intervals = tmp;
139
140 eventsKnown = storedEventsKnown;
141 likelihoodKnown = storedLikelihoodKnown;
142 logLikelihood = storedLogLikelihood;
143 }
144
145 protected final void acceptState() {
146 } // nothing to do
147
148 // **************************************************************
149 // Likelihood IMPLEMENTATION
150 // **************************************************************
151
152 public final Model getModel() {
153 return this;
154 }
155
156 public final double getLogLikelihood() {
157 if (!eventsKnown) {
158 setupIntervals();
159 }
160
161 if (!likelihoodKnown) {
162 logLikelihood = calculateLogLikelihood();
163 likelihoodKnown = true;
164 }
165
166 return logLikelihood;
167 }
168
169 public final void makeDirty() {
170 likelihoodKnown = false;
171 eventsKnown = false;
172 }
173
174 /**
175 * Calculates the log likelihood of this set of coalescent intervals,
176 * given a demographic model.
177 */
178 public abstract double calculateLogLikelihood();
179
180 /**
181 * @return the node ref of the MRCA of this coalescent prior in the given tree.
182 */
183 protected NodeRef getIncludedMRCA(Tree tree) {
184 if (includedLeafSet != null) {
185 return TreeUtils.getCommonAncestorNode(tree, includedLeafSet);
186 } else {
187 return tree.getRoot();
188 }
189 }
190
191 /**
192 * @return an array of noderefs that represent the MRCAs of subtrees to exclude from coalescent prior.
193 * May return null if no subtrees should be excluded.
194 */
195 protected Set<NodeRef> getExcludedMRCAs(Tree tree) {
196
197 if (excludedLeafSets.length == 0) return null;
198
199 Set<NodeRef> excludeNodesBelow = new HashSet<NodeRef>();
200 for (Set<String> excludedLeafSet : excludedLeafSets) {
201 excludeNodesBelow.add(TreeUtils.getCommonAncestorNode(tree, excludedLeafSet));
202 }
203 return excludeNodesBelow;
204 }
205
206 public Tree getTree() {
207 return tree;
208 }
209
210 public IntervalList getIntervals() {
211 return intervals;
212 }
213
214 /**
215 * Recalculates all the intervals from the tree model.
216 */
217 protected final void setupIntervals() {
218
219 intervals.resetEvents();
220 collectTimes(tree, getIncludedMRCA(tree), getExcludedMRCAs(tree), intervals);
221 // force a calculation of the intervals...
222 intervals.getIntervalCount();
223
224 eventsKnown = true;
225 likelihoodKnown = false;
226 }
227
228
229 /**
230 * extract coalescent times and tip information into ArrayList times from tree.
231 *
232 * @param tree the tree
233 * @param node the node to start from
234 * @param intervals the intervals object to store the events
235 */
236 private void collectTimes(Tree tree, NodeRef node, Set<NodeRef> excludeNodesBelow, Intervals intervals) {
237
238 intervals.addCoalescentEvent(tree.getNodeHeight(node));
239
240 for (int i = 0; i < tree.getChildCount(node); i++) {
241 NodeRef child = tree.getChild(node, i);
242
243 // check if this subtree is included in the coalescent density
244 boolean include = true;
245
246 if (excludeNodesBelow != null && excludeNodesBelow.contains(child)) {
247 include = false;
248 }
249
250 if (!include || tree.isExternal(child)) {
251 intervals.addSampleEvent(tree.getNodeHeight(child));
252 } else {
253 collectTimes(tree, child, excludeNodesBelow, intervals);
254 }
255 }
256
257 }
258
259 public double getCoalescentInterval(int i) {
260 if (!eventsKnown) {
261 setupIntervals();
262 }
263 return intervals.getInterval(i);
264 }
265
266 public int getCoalescentIntervalDimension() {
267 if (!eventsKnown) {
268 setupIntervals();
269 }
270 return intervals.getIntervalCount();
271 }
272
273 public int getNumberOfCoalescentEvents() {
274 return tree.getExternalNodeCount()-1;
275 }
276
277 public int getCoalescentIntervalLineageCount(int i) {
278 if (!eventsKnown) {
279 setupIntervals();
280 }
281 return intervals.getLineageCount(i);
282 }
283
284 public IntervalType getCoalescentIntervalType(int i) {
285 if (!eventsKnown) {
286 setupIntervals();
287 }
288 return intervals.getIntervalType(i);
289 }
290
291 public double getCoalescentEventsStatisticValue(int i) {
292 if (i == 0) {
293 for (int j = 0; j < coalescentEventStatisticValues.length; j++) {
294 coalescentEventStatisticValues[j] = 0.0;
295 }
296 int counter = 0;
297 for (int j = 0; j < getCoalescentIntervalDimension(); j++) {
298 if (getCoalescentIntervalType(j) == IntervalType.COALESCENT) {
299 this.coalescentEventStatisticValues[counter] += getCoalescentInterval(j) * (getCoalescentIntervalLineageCount(j) * (getCoalescentIntervalLineageCount(j) - 1.0)) / 2.0;
300 counter++;
301 } else {
302 this.coalescentEventStatisticValues[counter] += getCoalescentInterval(j) * (getCoalescentIntervalLineageCount(j) * (getCoalescentIntervalLineageCount(j) - 1.0)) / 2.0;
303 }
304 }
305 }
306 return coalescentEventStatisticValues[i];
307 }
308
309 public String toString() {
310 return Double.toString(logLikelihood);
311
312 }
313
314 // ****************************************************************
315 // Inner classes
316 // ****************************************************************
317
318 public class DeltaStatistic extends Statistic.Abstract {
319
320 public DeltaStatistic() {
321 super("delta");
322 }
323
324 public int getDimension() {
325 return 1;
326 }
327
328 public double getStatisticValue(int i) {
329 throw new RuntimeException("Not implemented");
330 // return IntervalList.Utils.getDelta(intervals);
331 }
332
333 }
334
335 // ****************************************************************
336 // Private and protected stuff
337 // ****************************************************************
338
339 /**
340 * The tree.
341 */
342 private Tree tree = null;
343 private final Set<String> includedLeafSet;
344 private final Set[] excludedLeafSets;
345
346 /**
347 * The intervals.
348 */
349 private Intervals intervals = null;
350
351 /**
352 * The stored values for intervals.
353 */
354 private Intervals storedIntervals = null;
355
356 private boolean eventsKnown = false;
357 private boolean storedEventsKnown = false;
358
359 private double logLikelihood;
360 private double storedLogLikelihood;
361 protected boolean likelihoodKnown = false;
362 private boolean storedLikelihoodKnown = false;
363
364 private double[] coalescentEventStatisticValues;
365 }