| 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.speciation; |
| 27 |
|
| 28 |
import dr.evolution.io.Importer; |
| 29 |
import dr.evolution.io.NewickImporter; |
| 30 |
import dr.evolution.tree.Tree; |
| 31 |
import dr.evolution.util.Taxon; |
| 32 |
import dr.inference.model.Parameter; |
| 33 |
import dr.inference.model.Variable; |
| 34 |
|
| 35 |
import java.io.IOException; |
| 36 |
import java.util.Arrays; |
| 37 |
import java.util.Set; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public class BirthDeathSerialSkylineModel extends SpeciationModel { |
| 49 |
|
| 50 |
|
| 51 |
Variable<Double> times; |
| 52 |
|
| 53 |
|
| 54 |
Variable<Double> lambda; |
| 55 |
|
| 56 |
|
| 57 |
Variable<Double> mu; |
| 58 |
|
| 59 |
|
| 60 |
Variable<Double> psi; |
| 61 |
|
| 62 |
|
| 63 |
Variable<Double> p; |
| 64 |
|
| 65 |
Variable<Double> origin; |
| 66 |
|
| 67 |
|
| 68 |
boolean relativeDeath = false; |
| 69 |
|
| 70 |
|
| 71 |
int size = 1; |
| 72 |
|
| 73 |
double t_root; |
| 74 |
double x0; |
| 75 |
protected double[] p0_iMinus1; |
| 76 |
protected double[] Ai; |
| 77 |
protected double[] Bi; |
| 78 |
|
| 79 |
protected boolean birthChanges = true; |
| 80 |
protected boolean deathChanges = true; |
| 81 |
protected boolean samplingChanges = true; |
| 82 |
|
| 83 |
protected boolean timesStartFromOrigin = true; |
| 84 |
protected double[] timesFromTips; |
| 85 |
|
| 86 |
public BirthDeathSerialSkylineModel( |
| 87 |
Variable<Double> times, |
| 88 |
Variable<Double> lambda, |
| 89 |
Variable<Double> mu, |
| 90 |
Variable<Double> psi, |
| 91 |
Variable<Double> p, |
| 92 |
Variable<Double> origin, |
| 93 |
boolean relativeDeath, |
| 94 |
boolean sampledIndividualsRemainInfectious, |
| 95 |
boolean timesStartFromOrigin, |
| 96 |
Type units) { |
| 97 |
|
| 98 |
this("birthDeathSerialSamplingModel", times, lambda, mu, psi, p, origin, relativeDeath, |
| 99 |
sampledIndividualsRemainInfectious, timesStartFromOrigin, units); |
| 100 |
} |
| 101 |
|
| 102 |
public BirthDeathSerialSkylineModel( |
| 103 |
String modelName, |
| 104 |
Variable<Double> times, |
| 105 |
Variable<Double> lambda, |
| 106 |
Variable<Double> mu, |
| 107 |
Variable<Double> psi, |
| 108 |
Variable<Double> p, |
| 109 |
Variable<Double> origin, |
| 110 |
boolean relativeDeath, |
| 111 |
boolean sampledIndividualsRemainInfectious, |
| 112 |
boolean timesStartFromOrigin, |
| 113 |
Type units) { |
| 114 |
|
| 115 |
super(modelName, units); |
| 116 |
|
| 117 |
this.size = times.getSize(); |
| 118 |
|
| 119 |
if (lambda.getSize() != 1 && lambda.getSize() != size) |
| 120 |
throw new RuntimeException("Length of Lambda parameter should be one or equal to the size of time parameter (size = " + size + ")"); |
| 121 |
|
| 122 |
if (mu.getSize() != 1 && mu.getSize() != size) |
| 123 |
throw new RuntimeException("Length of mu parameter should be one or equal to the size of time parameter (size = " + size + ")"); |
| 124 |
|
| 125 |
this.timesStartFromOrigin = timesStartFromOrigin; |
| 126 |
|
| 127 |
this.times = times; |
| 128 |
addVariable(times); |
| 129 |
times.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, times.getSize())); |
| 130 |
|
| 131 |
this.lambda = lambda; |
| 132 |
addVariable(lambda); |
| 133 |
lambda.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, lambda.getSize())); |
| 134 |
|
| 135 |
this.mu = mu; |
| 136 |
addVariable(mu); |
| 137 |
mu.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, mu.getSize())); |
| 138 |
|
| 139 |
this.p = p; |
| 140 |
addVariable(p); |
| 141 |
p.addBounds(new Parameter.DefaultBounds(1.0, 0.0, p.getSize())); |
| 142 |
|
| 143 |
this.origin = origin; |
| 144 |
addVariable(origin); |
| 145 |
p.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, origin.getSize())); |
| 146 |
|
| 147 |
this.psi = psi; |
| 148 |
addVariable(psi); |
| 149 |
psi.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, psi.getSize())); |
| 150 |
|
| 151 |
|
| 152 |
this.relativeDeath = relativeDeath; |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
public int lineageCountAtTime(double time, Tree tree) { |
| 161 |
|
| 162 |
int count = 1; |
| 163 |
for (int i = 0; i < tree.getInternalNodeCount(); i++) { |
| 164 |
if (tree.getNodeHeight(tree.getInternalNode(i)) > time) count += 1; |
| 165 |
} |
| 166 |
for (int i = 0; i < tree.getExternalNodeCount(); i++) { |
| 167 |
if (tree.getNodeHeight(tree.getExternalNode(i)) > time) count -= 1; |
| 168 |
} |
| 169 |
return count; |
| 170 |
} |
| 171 |
|
| 172 |
public double Ai(double b, double g, double psi) { |
| 173 |
|
| 174 |
return Math.sqrt((b - g - psi) * (b - g - psi) + 4.0 * b * psi); |
| 175 |
} |
| 176 |
|
| 177 |
public double Bi(double b, double g, double psi, double A, double p0) { |
| 178 |
|
| 179 |
return (-((1.0 - 2.0 * p0) * b + g + psi) / A); |
| 180 |
} |
| 181 |
|
| 182 |
public double p0(int index, double t, double ti) { |
| 183 |
|
| 184 |
return p0(birth(birthChanges ? index : 0), death(deathChanges ? index : 0), psi(samplingChanges ? index : 0), Ai[index], Bi[index], t, ti); |
| 185 |
} |
| 186 |
|
| 187 |
public double p0(double b, double g, double psi, double A, double B, double t, double ti) { |
| 188 |
|
| 189 |
return ((b + g + psi - A * ((Math.exp(A * (t - ti)) * (1.0 - B) - (1.0 + B))) / (Math.exp(A * (t - ti)) * (1.0 - B) + (1.0 + B))) / (2.0 * b)); |
| 190 |
} |
| 191 |
|
| 192 |
public double g(int index, double t, double ti) { |
| 193 |
|
| 194 |
double oneMinusBiSq = (1.0 - Bi[index]) * (1.0 - Bi[index]); |
| 195 |
double onePlusBiSq = (1.0 + Bi[index]) * (1.0 + Bi[index]); |
| 196 |
|
| 197 |
return 4.0 / (2.0 * (1.0 - Bi[index] * Bi[index]) + Math.exp(Ai[index] * (t - ti)) * |
| 198 |
oneMinusBiSq + Math.exp(-Ai[index] * (t - ti)) * onePlusBiSq); |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
public double t(int i) { |
| 208 |
return timesFromTips[i]; |
| 209 |
} |
| 210 |
|
| 211 |
public double birth(int i) { |
| 212 |
return lambda.getValue(i); |
| 213 |
} |
| 214 |
|
| 215 |
public double death(int i) { |
| 216 |
return relativeDeath ? mu.getValue(i) * birth(i) : mu.getValue(i); |
| 217 |
} |
| 218 |
|
| 219 |
public double psi(int i) { |
| 220 |
return psi.getValue(i); |
| 221 |
} |
| 222 |
|
| 223 |
public double p() { |
| 224 |
return p.getValue(0); |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
public double lambda(double t) { |
| 232 |
|
| 233 |
return lambda.getValue(index(t)); |
| 234 |
|
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
public double mu(double t) { |
| 242 |
|
| 243 |
return mu.getValue(index(t)); |
| 244 |
} |
| 245 |
|
| 246 |
public int index(double t) { |
| 247 |
|
| 248 |
int epoch = Arrays.binarySearch(timesFromTips, t); |
| 249 |
|
| 250 |
if (epoch < 0) { |
| 251 |
epoch = -epoch - 1; |
| 252 |
} |
| 253 |
return Math.max(epoch - 1, 0); |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
public void preCalculation(Tree tree) { |
| 258 |
|
| 259 |
t_root = tree.getNodeHeight(tree.getRoot()); |
| 260 |
x0 = t_root + origin.getValue(0); |
| 261 |
|
| 262 |
|
| 263 |
if (timesFromTips == null) { |
| 264 |
timesFromTips = new double[times.getSize()]; |
| 265 |
} |
| 266 |
if (timesStartFromOrigin) { |
| 267 |
timesFromTips[0] = 0; |
| 268 |
for (int i = 1; i < timesFromTips.length; i++) { |
| 269 |
timesFromTips[i] = Math.max(0, x0 - times.getValue(timesFromTips.length - i)); |
| 270 |
} |
| 271 |
|
| 272 |
} else { |
| 273 |
for (int i = 0; i < timesFromTips.length; i++) { |
| 274 |
timesFromTips[i] = times.getValue(i); |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
Ai = new double[size]; |
| 279 |
Bi = new double[size]; |
| 280 |
p0_iMinus1 = new double[size]; |
| 281 |
|
| 282 |
for (int i = 0; i < size; i++) { |
| 283 |
Ai[i] = Ai(birth(birthChanges ? i : 0), death(deathChanges ? i : 0), psi(samplingChanges ? i : 0)); |
| 284 |
|
| 285 |
|
| 286 |
} |
| 287 |
|
| 288 |
Bi[0] = Bi(birth(0), death(0), psi(0), Ai[0], 1); |
| 289 |
|
| 290 |
for (int i = 1; i < size; i++) { |
| 291 |
p0_iMinus1[i - 1] = p0(birth(birthChanges ? (i - 1) : 0), death(deathChanges ? (i - 1) : 0), psi(samplingChanges ? (i - 1) : 0), Ai[i - 1], Bi[i - 1], t(i), t(i - 1)); |
| 292 |
Bi[i] = Bi(birth(birthChanges ? i : 0), death(deathChanges ? i : 0), psi(samplingChanges ? i : 0), Ai[i], p0_iMinus1[i - 1]); |
| 293 |
|
| 294 |
|
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
public final double calculateTreeLogLikelihood(Tree tree) { |
| 305 |
|
| 306 |
|
| 307 |
int[] n = new int[size]; |
| 308 |
int nTips = tree.getExternalNodeCount(); |
| 309 |
preCalculation(tree); |
| 310 |
|
| 311 |
int index = size - 1; |
| 312 |
|
| 313 |
double t = t(index); |
| 314 |
double g = g(index, x0, t); |
| 315 |
|
| 316 |
double logP = Math.log(g); |
| 317 |
|
| 318 |
|
| 319 |
for (int i = 0; i < tree.getInternalNodeCount(); i++) { |
| 320 |
|
| 321 |
double x = tree.getNodeHeight(tree.getInternalNode(i)); |
| 322 |
index = index(x); |
| 323 |
|
| 324 |
double contrib = Math.log(birth(birthChanges ? index : 0) * g(index, x, t(index))); |
| 325 |
|
| 326 |
logP += contrib; |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
t = t(index); |
| 331 |
g = g(index, x, t); |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
for (int i = 0; i < nTips; i++) { |
| 339 |
|
| 340 |
double y = tree.getNodeHeight(tree.getExternalNode(i)); |
| 341 |
index = index(y); |
| 342 |
|
| 343 |
double contrib = Math.log(psi(samplingChanges ? index : 0)) - Math.log(g(index, y, t(index))); |
| 344 |
; |
| 345 |
logP += contrib; |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
for (int j = 0; j < size - 1; j++) { |
| 353 |
|
| 354 |
double contrib = 0; |
| 355 |
|
| 356 |
double time = t(j + 1); |
| 357 |
n[j] = lineageCountAtTime(time, tree); |
| 358 |
if (n[j] > 0) { |
| 359 |
contrib += n[j] * Math.log(g(j, time, t(j))); |
| 360 |
|
| 361 |
} |
| 362 |
|
| 363 |
logP += contrib; |
| 364 |
|
| 365 |
} |
| 366 |
return logP; |
| 367 |
} |
| 368 |
|
| 369 |
public double calculateTreeLogLikelihood(Tree tree, Set<Taxon> exclude) { |
| 370 |
if (exclude.size() == 0) return calculateTreeLogLikelihood(tree); |
| 371 |
throw new RuntimeException("Not implemented!"); |
| 372 |
} |
| 373 |
|
| 374 |
public static void main(String[] args) throws IOException, Importer.ImportException { |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
Variable<Double> times = new Variable.D(1, 10); |
| 379 |
|
| 380 |
Variable<Double> mu = new Variable.D(1, 10); |
| 381 |
for (int i = 0; i < mu.getSize(); i++) { |
| 382 |
times.setValue(i, (i + 1) * 2.0); |
| 383 |
mu.setValue(i, i + 1.0); |
| 384 |
} |
| 385 |
|
| 386 |
Variable<Double> lambda = new Variable.D(1, 10); |
| 387 |
Variable<Double> psi = new Variable.D(0.5, 1); |
| 388 |
Variable<Double> p = new Variable.D(0.5, 1); |
| 389 |
Variable<Double> origin = new Variable.D(0.5, 1); |
| 390 |
boolean relativeDeath = false; |
| 391 |
boolean sampledIndividualsRemainInfectious = false; |
| 392 |
boolean timesStartFromOrigin = false; |
| 393 |
|
| 394 |
BirthDeathSerialSkylineModel model = |
| 395 |
new BirthDeathSerialSkylineModel(times, lambda, mu, psi, p, origin, relativeDeath, |
| 396 |
sampledIndividualsRemainInfectious, timesStartFromOrigin, Type.SUBSTITUTIONS); |
| 397 |
|
| 398 |
NewickImporter importer = new NewickImporter("((A:6,B:5):4,(C:3,D:2):1);"); |
| 399 |
Tree tree = importer.importNextTree(); |
| 400 |
|
| 401 |
model.calculateTreeLogLikelihood(tree); |
| 402 |
|
| 403 |
for (int i = 0; i < times.getSize(); i += 1) { |
| 404 |
System.out.println("mu at time " + i + " is " + model.mu(i)); |
| 405 |
System.out.println("p0 at time " + i + " is " + model.p0(0, i, i)); |
| 406 |
} |
| 407 |
|
| 408 |
} |
| 409 |
} |