Debian Codegraph @

src/dr/evomodel/coalescent/structure/ColourSamplerModel.java

raw symbols SCIP
1 /*
2 * ColourSamplerModel.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.structure;
27
28 import dr.evolution.alignment.Alignment;
29 import dr.evolution.colouring.*;
30 import dr.evolution.tree.Tree;
31 import dr.evolution.util.Taxa;
32 import dr.evomodel.tree.TreeModel;
33 import dr.inference.model.*;
34 import dr.xml.*;
35
36 import java.util.logging.Logger;
37
38 /**
39 * @author Alexei Drummond
40 * @version $Id: ColourSamplerModel.java,v 1.14 2006/09/11 09:33:01 gerton Exp $
41 */
42
43 @Deprecated
44 public class ColourSamplerModel extends AbstractModel implements TreeColouringProvider, ModelListener, StatisticList {
45
46 public static final String COLOUR_SAMPLER_MODEL = "colourSamplerModel";
47 public static final String STRUCTURED_SAMPLER = "structuredSampler";
48 public static final String NODE_BIAS = "nodeBias";
49 public static final String BRANCH_BIAS = "branchBias";
50 public static final String SECOND_ITERATION = "secondIteration";
51
52 public ColourSamplerModel(TreeModel treeModel, ColourSampler colourSampler, MigrationModel migrationModel, MetaPopulationModel metaPopulationModel) {
53
54 super(COLOUR_SAMPLER_MODEL);
55
56 this.treeModel = treeModel;
57 addModel(treeModel);
58
59 this.migrationModel = migrationModel;
60 addModel(migrationModel);
61
62 this.metaPopulationModel = metaPopulationModel;
63 // This is unusual because we don't want to addModel(metaPopulationModel) -
64 // this is because the population sizes are just used as a bias in the proposal
65 // distribution - a change in population size shouldn't force a recolouring by
66 // this mechanism.
67
68 addStatistic(migrationEventStatistic);
69 //addStatistic(debugMigrationEventStatistic);
70
71 addStatistic(rootColourStatistic);
72
73 this.colourSampler = colourSampler;
74
75 }
76
77 public final TreeColouring getTreeColouring(Tree tree) {
78 return getTreeColouring();
79 }
80
81 public final DefaultTreeColouring getTreeColouring() {
82 if (treeColouring == null) {
83 sample();
84 }
85 return treeColouring;
86 }
87
88 public final int[] getLeafColourCounts() {
89 return colourSampler.getLeafColourCounts();
90 }
91
92 /**
93 * Returns treeColouring and ensures that a proposal probability density has been assigned to it.
94 *
95 * @return treeColouring
96 */
97 public final DefaultTreeColouring getTreeColouringWithProbability() {
98
99 DefaultTreeColouring tc = getTreeColouring();
100
101 if (tc.hasProbability()) {
102 return tc;
103 }
104
105 // We have a colouring, but it hasn't got a valid proposal probability. This happens when parameters influencing the
106 // proposal distribution have changed, or when local re-colouring moves have occurred. Re-compute the proposal probability
107
108 // TODO HACK HACK HACK -- only using modern day population size
109 // NOTE: If this is changed, StructuredCoalescentSampler should change too! (GAL)
110 //double[] N = metaPopulationModel.getPopulationSizes(0);
111
112 double logP = colourSampler.getProposalProbability(tc, treeModel, migrationModel.getMigrationMatrix(), metaPopulationModel);
113 tc.setLogProbabilityDensity(logP);
114 return tc;
115 }
116
117 public final void resample() {
118 treeColouring = null;
119 }
120
121 /**
122 * Keeps the colouring, but reset its proposal probability (in response to a change in parameters)
123 */
124 public final void invalidateProposalProbability() {
125 treeColouring = new DefaultTreeColouring(treeColouring);
126 }
127
128 /**
129 * Colours the tree probabilistically with the given migration rates
130 */
131 private void sample() {
132
133 // TODO HACK HACK HACK -- only using modern day population size
134 // NOTE: If this is changed, StructuredCoalescentSampler should change too! (GAL)
135 //double[] N = metaPopulationModel.getPopulationSizes(0);
136
137 treeColouring = colourSampler.sampleTreeColouring(treeModel, migrationModel.getMigrationMatrix(), metaPopulationModel);
138
139 // StructuredCoalescent sc = new StructuredCoalescent();
140 // StructuredIntervalList list = new ColouredTreeIntervals(treeModel, treeColouring);
141 // double logL = sc.calculateLogLikelihood(treeColouring, list, migrationModel.getMigrationMatrix(), N);
142 // double logP = treeColouring.getLogProbabilityDensity();
143
144 //System.out.println("sampled tree colouring: " + logP + ", " + logL + " diff=" + (logL-logP));
145
146 // treeColouring.checkColouring();
147
148 listenerHelper.fireModelChanged(this);
149 }
150
151 /**
152 * This function should be called to store the state of the
153 * entire model. This makes the model state invalid until either
154 * an acceptModelState or restoreModelState is called.
155 */
156 public void storeState() {
157 storedTreeColouring = treeColouring;
158 }
159
160 /**
161 * This function should be called to restore the state of the entire model.
162 */
163 public void restoreState() {
164 treeColouring = storedTreeColouring;
165 }
166
167 /**
168 * This function should be called to accept the state of the entire model
169 */
170 public void acceptState() {
171 // Do nothing?
172 }
173
174 // **************************************************************
175 // ModelListener IMPLEMENTATION
176 // **************************************************************
177
178 /**
179 * Handles model changed events from the submodels.
180 */
181 protected void handleModelChangedEvent(Model model, Object object, int index) {
182 resample();
183 }
184
185 protected void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
186 // do nothing
187 }
188
189 // ****************************************************************
190 // Private and protected stuff
191 // ****************************************************************
192
193 private final Statistic migrationEventStatistic = new Statistic.Abstract() {
194
195 public String getStatisticName() {
196 return "migrationEvents";
197 }
198
199 public int getDimension() {
200 return 1;
201 }
202
203 public double getStatisticValue(int dim) {
204 TreeColouring colouring = getTreeColouringWithProbability();
205 return colouring.getColourChangeCount();
206 }
207
208 };
209
210 private final Statistic rootColourStatistic = new Statistic.Abstract() {
211
212 public String getStatisticName() {
213 return "rootColour";
214 }
215
216 public int getDimension() {
217 return 1;
218 }
219
220 public double getStatisticValue(int dim) {
221 TreeColouring colouring = getTreeColouringWithProbability();
222 return colouring.getNodeColour(colouring.getTree().getRoot());
223 }
224
225 };
226
227 /* for debugging
228 private Statistic debugMigrationEventStatistic = new Statistic.Abstract() {
229
230 public String getStatisticName() {
231 return "migrationEvents";
232 }
233
234 public int getDimension() { return 4; }
235
236 public double getStatisticValue(int dim) {
237 TreeColouring colouring = getTreeColouringWithProbability();
238
239 if (dim == 0) return colouring.getColourChangeCount();
240
241 if (dim == 1) return colouring.getLogProbabilityDensity();
242
243 StructuredCoalescent sc = new StructuredCoalescent();
244
245 StructuredIntervalList list = new ColouredTreeIntervals(treeModel, colouring);
246
247 //double[] N = metaPopulationModel.getPopulationSizes(0);
248
249 double logL = sc.calculateLogLikelihood(colouring, list, migrationModel.getMigrationMatrix(), metaPopulationModel);
250
251 if (dim == 2) return logL;
252
253 return logL-colouring.getLogProbabilityDensity();
254 }
255
256 };
257 */
258
259 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
260
261 public String getParserName() {
262 return COLOUR_SAMPLER_MODEL;
263 }
264
265 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
266
267 TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class);
268
269 MigrationModel migrationModel = (MigrationModel) xo.getChild(MigrationModel.class);
270
271 MetaPopulationModel metaPopulationModel = (MetaPopulationModel) xo.getChild(MetaPopulationModel.class);
272
273 boolean structuredSampler = xo.getAttribute(STRUCTURED_SAMPLER, true);
274 boolean branchBias = xo.getAttribute(BRANCH_BIAS, true);
275 boolean nodeBias = xo.getAttribute(NODE_BIAS, true);
276 boolean secondIteration = xo.getAttribute(SECOND_ITERATION, false);
277
278 ColourSampler colourSampler;
279 if (xo.hasChildNamed("colours")) {
280 XMLObject cxo = xo.getChild("colours");
281
282 Taxa[] colourTaxa = new Taxa[cxo.getChildCount()];
283 for (int i = 0; i < cxo.getChildCount(); i++) {
284 colourTaxa[i] = (Taxa) cxo.getChild(i);
285 }
286
287 if (structuredSampler) {
288 colourSampler = new StructuredColourSampler(colourTaxa, treeModel, nodeBias, branchBias, secondIteration);
289 } else {
290 colourSampler = new BasicColourSampler(colourTaxa, treeModel);
291 }
292 } else {
293 Alignment alignment = (Alignment) xo.getChild(Alignment.class);
294 if (structuredSampler) {
295 colourSampler = new StructuredColourSampler(alignment, treeModel, nodeBias, branchBias, secondIteration);
296 } else {
297 colourSampler = new BasicColourSampler(alignment, treeModel);
298 }
299 }
300
301 ColourSamplerModel colourSamplerModel = new ColourSamplerModel(treeModel, colourSampler, migrationModel, metaPopulationModel);
302
303 if (structuredSampler) {
304 Logger.getLogger("dr.evomodel").info("Creating colour sampler model with 2 colours");
305 if (!nodeBias) {
306 Logger.getLogger("dr.evomodel").info(" Colour sampler has node biases switched off");
307 }
308 if (!branchBias) {
309 Logger.getLogger("dr.evomodel").info(" Colour sampler has branch biases switched off");
310 }
311 } else {
312 Logger.getLogger("dr.evomodel").info("Creating basic 2-colour sampler");
313 }
314
315 return colourSamplerModel;
316 }
317
318 //************************************************************************
319 // AbstractXMLObjectParser implementation
320 //************************************************************************
321
322 public String getParserDescription() {
323 return "This element represents a likelihood function for transmission.";
324 }
325
326 public Class getReturnType() {
327 return StructuredCoalescentLikelihood.class;
328 }
329
330 public XMLSyntaxRule[] getSyntaxRules() {
331 return rules;
332 }
333
334 private final XMLSyntaxRule[] rules = {
335 new XORRule(
336 new ElementRule("colours", new XMLSyntaxRule[]{
337 new ElementRule(Taxa.class, "Taxa for each subsequent colour (after 0).", 1, Integer.MAX_VALUE),
338 }),
339 new ElementRule(Alignment.class, "The alignment.")),
340 new ElementRule(TreeModel.class, "The tree."),
341 new ElementRule(MigrationModel.class, "The migration model."),
342 new ElementRule(MetaPopulationModel.class, "The metapopulation model,")
343 };
344 };
345
346
347 private final MetaPopulationModel metaPopulationModel;
348 private final MigrationModel migrationModel;
349 private final ColourSampler colourSampler;
350 private final TreeModel treeModel;
351
352 private DefaultTreeColouring treeColouring = null;
353 private DefaultTreeColouring storedTreeColouring = null;
354
355 }