| 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.ConstantPopulation; |
| 29 |
import dr.evolution.tree.Tree; |
| 30 |
import dr.evolution.util.Units; |
| 31 |
import dr.evomodel.tree.TreeModel; |
| 32 |
import dr.evomodelxml.coalescent.VariableSkylineLikelihoodParser; |
| 33 |
import dr.inference.model.Parameter; |
| 34 |
import dr.inference.model.Variable; |
| 35 |
import dr.util.Author; |
| 36 |
import dr.util.Citable; |
| 37 |
import dr.util.Citation; |
| 38 |
|
| 39 |
import java.util.ArrayList; |
| 40 |
import java.util.Collections; |
| 41 |
import java.util.List; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public class VariableSkylineLikelihood extends OldAbstractCoalescentLikelihood implements Citable { |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public enum Type { |
| 54 |
STEPWISE, |
| 55 |
LINEAR, |
| 56 |
EXPONENTIAL |
| 57 |
} |
| 58 |
|
| 59 |
public VariableSkylineLikelihood(Tree tree, Parameter popSizeParameter, Parameter indicatorParameter, |
| 60 |
Type type, boolean logSpace) { |
| 61 |
super(VariableSkylineLikelihoodParser.SKYLINE_LIKELIHOOD); |
| 62 |
|
| 63 |
this.popSizeParameter = popSizeParameter; |
| 64 |
this.indicatorParameter = indicatorParameter; |
| 65 |
final int redcueDim = type == Type.STEPWISE ? 1 : 0; |
| 66 |
final int events = tree.getExternalNodeCount() - redcueDim; |
| 67 |
final int paramDim1 = popSizeParameter.getDimension(); |
| 68 |
final int paramDim2 = indicatorParameter.getDimension(); |
| 69 |
this.type = type; |
| 70 |
this.logSpace = logSpace; |
| 71 |
|
| 72 |
if (paramDim1 != events) { |
| 73 |
throw new IllegalArgumentException("Dimension of population parameter (" + paramDim1 + ") must be the same as the number of internal nodes in the tree (" |
| 74 |
+ events + ")."); |
| 75 |
} |
| 76 |
if (paramDim2 != events - 1) { |
| 77 |
throw new IllegalArgumentException("Dimension of indicator parameter must one less than the number of internal nodes in the tree. (" |
| 78 |
+ paramDim2 + " != " + (events - 1) + ")"); |
| 79 |
} |
| 80 |
|
| 81 |
this.tree = tree; |
| 82 |
if (tree instanceof TreeModel) { |
| 83 |
addModel((TreeModel) tree); |
| 84 |
} |
| 85 |
|
| 86 |
addVariable(indicatorParameter); |
| 87 |
addVariable(popSizeParameter); |
| 88 |
|
| 89 |
setupIntervals(); |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public synchronized double getLogLikelihood() { |
| 101 |
|
| 102 |
insureValid(); |
| 103 |
|
| 104 |
double logL = 0.0; |
| 105 |
|
| 106 |
double currentTime = 0.0; |
| 107 |
|
| 108 |
|
| 109 |
int groupIndex = 0; |
| 110 |
|
| 111 |
|
| 112 |
int subIndex = 0; |
| 113 |
ConstantPopulation cp = new ConstantPopulation(Units.Type.YEARS); |
| 114 |
|
| 115 |
final double[] pop = new double[1]; |
| 116 |
if (type == Type.LINEAR) { |
| 117 |
cp = new ConstantPopulation(Units.Type.YEARS) { |
| 118 |
public double getDemographic(double t) { |
| 119 |
return pop[0]; |
| 120 |
} |
| 121 |
}; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
for (int j = 0; j < intervalCount; j++) { |
| 127 |
|
| 128 |
|
| 129 |
if (type == Type.LINEAR) { |
| 130 |
pop[0] = getPopSize(groupIndex, currentTime + intervals[j]); |
| 131 |
} |
| 132 |
cp.setN0(getPopSize(groupIndex, currentTime + (intervals[j] / 2.0))); |
| 133 |
final CoalescentEventType iType = getIntervalType(j); |
| 134 |
if (iType == CoalescentEventType.COALESCENT) { |
| 135 |
subIndex += 1; |
| 136 |
if (subIndex >= groupSizes.get(groupIndex)) { |
| 137 |
groupIndex += 1; |
| 138 |
subIndex = 0; |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
logL += calculateIntervalLikelihood(cp, intervals[j], currentTime, lineageCounts[j], iType); |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
if (logL > 0 && j > 1) { |
| 147 |
System.out.println(logL); |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
final int diff = getCoalescentEvents(j) - 1; |
| 152 |
for (int k = 0; k < diff; k++) { |
| 153 |
|
| 154 |
pop[0] = getPopSize(groupIndex, currentTime); |
| 155 |
cp.setN0(pop[0]); |
| 156 |
logL += calculateIntervalLikelihood(cp, 0.0, currentTime, lineageCounts[j] - k - 1, |
| 157 |
CoalescentEventType.COALESCENT); |
| 158 |
subIndex += 1; |
| 159 |
if (subIndex >= groupSizes.get(groupIndex)) { |
| 160 |
groupIndex += 1; |
| 161 |
subIndex = 0; |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
currentTime += intervals[j]; |
| 166 |
} |
| 167 |
|
| 168 |
return logL; |
| 169 |
} |
| 170 |
|
| 171 |
private void insureValid() { |
| 172 |
if (!intervalsKnown) { |
| 173 |
|
| 174 |
|
| 175 |
setupIntervals(); |
| 176 |
groupsValid = false; |
| 177 |
} |
| 178 |
|
| 179 |
if (!groupsValid) { |
| 180 |
calculateGroupSizesHeightsAndEnds(); |
| 181 |
popPoints = null; |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
private double[] popPoints = null; |
| 186 |
|
| 187 |
public double[] getPopulation(int dim) { |
| 188 |
insureValid(); |
| 189 |
|
| 190 |
if (popPoints != null && popPoints.length == dim) { |
| 191 |
return popPoints; |
| 192 |
} |
| 193 |
|
| 194 |
if (popPoints == null || popPoints.length != dim) { |
| 195 |
popPoints = new double[dim]; |
| 196 |
} |
| 197 |
|
| 198 |
final double height = tree.getNodeHeight(tree.getRoot()); |
| 199 |
double hstep = height / popPoints.length; |
| 200 |
|
| 201 |
int groupIndex = 0; |
| 202 |
double currentTime = 0.0; |
| 203 |
double switchAt = groupEnds.get(groupIndex); |
| 204 |
for (int k = 0; k < popPoints.length - 1; ++k) { |
| 205 |
popPoints[k] = getPopSize(groupIndex, currentTime); |
| 206 |
currentTime += hstep; |
| 207 |
while (currentTime >= switchAt) { |
| 208 |
++groupIndex; |
| 209 |
switchAt = groupEnds.get(groupIndex); |
| 210 |
} |
| 211 |
} |
| 212 |
popPoints[popPoints.length - 1] = getPopSize(groupIndex, currentTime); |
| 213 |
return popPoints; |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
private double getPopSize(int groupIndex, double atTime) { |
| 223 |
|
| 224 |
final double startGroupTime = groupIndex > 0 ? groupEnds.get(groupIndex - 1) : 0.0; |
| 225 |
final double endGroupTime = groupEnds.get(groupIndex); |
| 226 |
final double startGroupPopSize = groupHeights.get(groupIndex); |
| 227 |
|
| 228 |
if (!(startGroupTime <= atTime && |
| 229 |
((type != Type.STEPWISE && atTime <= endGroupTime) || |
| 230 |
(type == Type.STEPWISE && atTime < endGroupTime)))) { |
| 231 |
System.out.println(atTime + " " + startGroupTime + "/" + endGroupTime); |
| 232 |
} |
| 233 |
|
| 234 |
switch (type) { |
| 235 |
case EXPONENTIAL: { |
| 236 |
throw new UnsupportedOperationException("Exponential Skyline Plot not implemented yet"); |
| 237 |
} |
| 238 |
case LINEAR: { |
| 239 |
if (groupIndex + 1 >= groupHeights.size()) { |
| 240 |
return startGroupPopSize; |
| 241 |
} |
| 242 |
|
| 243 |
final double endGroupPopSize = groupHeights.get(groupIndex + 1); |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
final double a = (atTime - startGroupTime) / (endGroupTime - startGroupTime); |
| 248 |
|
| 249 |
|
| 250 |
double v = (1 - a) * startGroupPopSize + a * endGroupPopSize; |
| 251 |
if (v <= 0) { |
| 252 |
|
| 253 |
if (a == 1) { |
| 254 |
assert endGroupPopSize > 0; |
| 255 |
return endGroupPopSize; |
| 256 |
} |
| 257 |
System.out.println(v); |
| 258 |
assert v > 0; |
| 259 |
} |
| 260 |
return v; |
| 261 |
} |
| 262 |
case STEPWISE: { |
| 263 |
return startGroupPopSize; |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
return -1; |
| 268 |
} |
| 269 |
|
| 270 |
private void calculateGroupSizesHeightsAndEnds() { |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
groupSizes.clear(); |
| 279 |
groupHeights.clear(); |
| 280 |
groupEnds.clear(); |
| 281 |
|
| 282 |
int groupSize = 1; |
| 283 |
int nextPopSizeIndex = 0; |
| 284 |
double height = 0.0; |
| 285 |
for (int i = 0; i < indicatorParameter.getDimension(); i++) { |
| 286 |
height += intervals[i]; |
| 287 |
if (indicatorParameter.getParameterValue(i) != 0.0) { |
| 288 |
groupSizes.add(groupSize); |
| 289 |
groupHeights.add(popSizeParameter.getParameterValue(nextPopSizeIndex)); |
| 290 |
groupSize = 1; |
| 291 |
nextPopSizeIndex = i + 1; |
| 292 |
|
| 293 |
groupEnds.add(height); |
| 294 |
|
| 295 |
} else { |
| 296 |
groupSize += 1; |
| 297 |
} |
| 298 |
} |
| 299 |
height += intervals[indicatorParameter.getDimension()]; |
| 300 |
|
| 301 |
groupSizes.add(groupSize); |
| 302 |
groupHeights.add(popSizeParameter.getParameterValue(nextPopSizeIndex)); |
| 303 |
groupEnds.add(height); |
| 304 |
|
| 305 |
if (logSpace) { |
| 306 |
for (int i = 0; i < groupHeights.size(); i++) { |
| 307 |
groupHeights.set(i, Math.exp(groupHeights.get(i))); |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
groupsValid = true; |
| 312 |
} |
| 313 |
|
| 314 |
protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) { |
| 315 |
super.handleVariableChangedEvent(variable, index, type); |
| 316 |
|
| 317 |
groupsValid = false; |
| 318 |
|
| 319 |
} |
| 320 |
|
| 321 |
protected void storeState() { |
| 322 |
super.storeState(); |
| 323 |
storeSizes.clear(); |
| 324 |
storeSizes.addAll(groupSizes); |
| 325 |
storeHeights.clear(); |
| 326 |
storeHeights.addAll(groupHeights); |
| 327 |
storeEnds.clear(); |
| 328 |
storeEnds.addAll(groupEnds); |
| 329 |
storeValid = groupsValid; |
| 330 |
} |
| 331 |
|
| 332 |
protected void restoreState() { |
| 333 |
super.restoreState(); |
| 334 |
groupsValid = storeValid; |
| 335 |
groupSizes.clear(); |
| 336 |
groupSizes.addAll(storeSizes); |
| 337 |
groupHeights.clear(); |
| 338 |
groupHeights.addAll(storeHeights); |
| 339 |
groupEnds.clear(); |
| 340 |
groupEnds.addAll(storeEnds); |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
private final Parameter popSizeParameter; |
| 351 |
|
| 352 |
private final Parameter indicatorParameter; |
| 353 |
|
| 354 |
private boolean groupsValid = false; |
| 355 |
|
| 356 |
List<Integer> groupSizes = new ArrayList<Integer>(); |
| 357 |
List<Double> groupHeights = new ArrayList<Double>(); |
| 358 |
List<Double> groupEnds = new ArrayList<Double>(); |
| 359 |
|
| 360 |
private final ArrayList<Integer> storeSizes = new ArrayList<Integer>(); |
| 361 |
private final ArrayList<Double> storeHeights = new ArrayList<Double>(); |
| 362 |
private final ArrayList<Double> storeEnds = new ArrayList<Double>(); |
| 363 |
private boolean storeValid; |
| 364 |
|
| 365 |
private final Type type; |
| 366 |
|
| 367 |
private boolean logSpace = false; |
| 368 |
|
| 369 |
@Override |
| 370 |
public Citation.Category getCategory() { |
| 371 |
return Citation.Category.TREE_PRIORS; |
| 372 |
} |
| 373 |
|
| 374 |
@Override |
| 375 |
public String getDescription() { |
| 376 |
return "Extended Skyline Coalescent"; |
| 377 |
} |
| 378 |
|
| 379 |
@Override |
| 380 |
public List<Citation> getCitations() { |
| 381 |
return Collections.singletonList(CITATION); |
| 382 |
} |
| 383 |
|
| 384 |
public static Citation CITATION = new Citation( |
| 385 |
new Author[]{ |
| 386 |
new Author("J", "Heled"), |
| 387 |
new Author("AJ", "Drummond") |
| 388 |
}, |
| 389 |
"", |
| 390 |
0, |
| 391 |
"", |
| 392 |
0, 0, 0, |
| 393 |
"" |
| 394 |
); |
| 395 |
} |