| 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; |
| 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 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public abstract class AbstractCoalescentLikelihood extends AbstractModelLikelihood implements Units, CoalescentIntervalProvider { |
| 52 |
|
| 53 |
|
| 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 |
|
| 97 |
|
| 98 |
|
| 99 |
protected final void handleModelChangedEvent(Model model, Object object, int index) { |
| 100 |
if (model == tree) { |
| 101 |
|
| 102 |
eventsKnown = false; |
| 103 |
} |
| 104 |
|
| 105 |
likelihoodKnown = false; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
protected void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) { |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
protected final void storeState() { |
| 123 |
|
| 124 |
storedIntervals.copyIntervals(intervals); |
| 125 |
|
| 126 |
storedEventsKnown = eventsKnown; |
| 127 |
storedLikelihoodKnown = likelihoodKnown; |
| 128 |
storedLogLikelihood = logLikelihood; |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
protected final void restoreState() { |
| 135 |
|
| 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 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 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 |
|
| 176 |
|
| 177 |
|
| 178 |
public abstract double calculateLogLikelihood(); |
| 179 |
|
| 180 |
|
| 181 |
|
| 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 |
|
| 193 |
|
| 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 |
|
| 216 |
|
| 217 |
protected final void setupIntervals() { |
| 218 |
|
| 219 |
intervals.resetEvents(); |
| 220 |
collectTimes(tree, getIncludedMRCA(tree), getExcludedMRCAs(tree), intervals); |
| 221 |
|
| 222 |
intervals.getIntervalCount(); |
| 223 |
|
| 224 |
eventsKnown = true; |
| 225 |
likelihoodKnown = false; |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 331 |
} |
| 332 |
|
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
private Tree tree = null; |
| 343 |
private final Set<String> includedLeafSet; |
| 344 |
private final Set[] excludedLeafSets; |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
private Intervals intervals = null; |
| 350 |
|
| 351 |
|
| 352 |
|
| 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 |
} |