| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 40 |
|
| 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 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
addStatistic(migrationEventStatistic); |
| 69 |
|
| 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 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
public final DefaultTreeColouring getTreeColouringWithProbability() { |
| 98 |
|
| 99 |
DefaultTreeColouring tc = getTreeColouring(); |
| 100 |
|
| 101 |
if (tc.hasProbability()) { |
| 102 |
return tc; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 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 |
|
| 123 |
|
| 124 |
public final void invalidateProposalProbability() { |
| 125 |
treeColouring = new DefaultTreeColouring(treeColouring); |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
private void sample() { |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
treeColouring = colourSampler.sampleTreeColouring(treeModel, migrationModel.getMigrationMatrix(), metaPopulationModel); |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
listenerHelper.fireModelChanged(this); |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
public void storeState() { |
| 157 |
storedTreeColouring = treeColouring; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
public void restoreState() { |
| 164 |
treeColouring = storedTreeColouring; |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
public void acceptState() { |
| 171 |
|
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 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 |
|
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 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 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 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 |
|
| 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 |
} |