| 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.coalescent.DemographicFunction; |
| 29 |
import dr.evolution.io.NewickImporter; |
| 30 |
import dr.evolution.tree.*; |
| 31 |
import dr.evolution.util.MutableTaxonListListener; |
| 32 |
import dr.evolution.util.Taxon; |
| 33 |
import dr.evomodel.coalescent.VDdemographicFunction; |
| 34 |
import dr.evomodel.operators.TreeNodeSlide; |
| 35 |
import dr.evomodel.tree.TreeLogger; |
| 36 |
import dr.evomodelxml.speciation.SpeciesTreeModelParser; |
| 37 |
import dr.inference.model.AbstractModel; |
| 38 |
import dr.inference.model.Model; |
| 39 |
import dr.inference.model.Parameter; |
| 40 |
import dr.inference.model.Variable; |
| 41 |
import dr.inference.operators.Scalable; |
| 42 |
import dr.util.Author; |
| 43 |
import dr.util.Citable; |
| 44 |
import dr.util.Citation; |
| 45 |
import dr.util.HeapSort; |
| 46 |
import jebl.util.FixedBitSet; |
| 47 |
|
| 48 |
import java.util.*; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public class SpeciesTreeModel extends AbstractModel implements |
| 57 |
MutableTree, TreeTraitProvider, TreeLogger.LogUpon, Scalable, Citable { |
| 58 |
private final SimpleTree spTree; |
| 59 |
private final SpeciesBindings species; |
| 60 |
private final Map<NodeRef, NodeProperties> props = new HashMap<NodeRef, NodeProperties>(); |
| 61 |
public final Parameter sppSplitPopulations; |
| 62 |
private int[] singleStartPoints; |
| 63 |
private int[] pairStartPoints; |
| 64 |
|
| 65 |
private final Parameter coalPointsPops; |
| 66 |
private final Parameter coalPointsIndicator; |
| 67 |
private boolean nodePropsReady; |
| 68 |
|
| 69 |
private final NodeRef[] children; |
| 70 |
private final double[] heights; |
| 71 |
|
| 72 |
|
| 73 |
private boolean anyChange; |
| 74 |
|
| 75 |
private boolean treeChanged; |
| 76 |
|
| 77 |
private final String spIndexAttrName = "spi"; |
| 78 |
private final boolean bmp; |
| 79 |
private final boolean nonConstRootPopulation; |
| 80 |
private final boolean constantPopulation; |
| 81 |
|
| 82 |
private class NodeProperties { |
| 83 |
final int speciesIndex; |
| 84 |
public VDdemographicFunction demogf; |
| 85 |
FixedBitSet spSet; |
| 86 |
|
| 87 |
public NodeProperties(int n) { |
| 88 |
speciesIndex = n; |
| 89 |
demogf = null; |
| 90 |
spSet = new FixedBitSet(species.nSpecies()); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
public SpeciesTreeModel(SpeciesBindings species, Parameter sppSplitPopulations, |
| 95 |
Parameter coalPointsPops, Parameter coalPointsIndicator, Tree startTree, |
| 96 |
boolean bmp, boolean nonConstRootPopulation, boolean constantPopulation) { |
| 97 |
super(SpeciesTreeModelParser.SPECIES_TREE); |
| 98 |
|
| 99 |
this.species = species; |
| 100 |
|
| 101 |
this.sppSplitPopulations = sppSplitPopulations; |
| 102 |
this.coalPointsPops = coalPointsPops; |
| 103 |
this.coalPointsIndicator = coalPointsIndicator; |
| 104 |
|
| 105 |
this.bmp = bmp; |
| 106 |
this.nonConstRootPopulation = nonConstRootPopulation; |
| 107 |
this.constantPopulation = constantPopulation; |
| 108 |
|
| 109 |
addVariable(sppSplitPopulations); |
| 110 |
|
| 111 |
addModel(species); |
| 112 |
|
| 113 |
if (coalPointsPops != null) { |
| 114 |
assert coalPointsIndicator != null; |
| 115 |
assert !constantPopulation; |
| 116 |
|
| 117 |
addVariable(coalPointsPops); |
| 118 |
addVariable(coalPointsIndicator); |
| 119 |
|
| 120 |
final double[][] pts = species.getPopTimesSingle(); |
| 121 |
|
| 122 |
int start = 0; |
| 123 |
singleStartPoints = new int[pts.length]; |
| 124 |
for (int i = 0; i < pts.length; i++) { |
| 125 |
singleStartPoints[i] = start; |
| 126 |
start += pts[i].length; |
| 127 |
} |
| 128 |
|
| 129 |
if (!bmp) { |
| 130 |
final double[][] ptp = species.getPopTimesPair(); |
| 131 |
pairStartPoints = new int[ptp.length]; |
| 132 |
for (int i = 0; i < ptp.length; i++) { |
| 133 |
pairStartPoints[i] = start; |
| 134 |
start += ptp[i].length; |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
spTree = compatibleUninformedSpeciesTree(startTree); |
| 141 |
|
| 142 |
|
| 143 |
assert TreeUtils.isBinary(spTree); |
| 144 |
|
| 145 |
final int nNodes = spTree.getNodeCount(); |
| 146 |
heights = new double[nNodes]; |
| 147 |
children = new NodeRef[2 * nNodes + 1]; |
| 148 |
|
| 149 |
|
| 150 |
for (int k = 0; k < getExternalNodeCount(); ++k) { |
| 151 |
final NodeRef nodeRef = getExternalNode(k); |
| 152 |
final int n = (Integer) getNodeAttribute(nodeRef, spIndexAttrName); |
| 153 |
final NodeProperties np = new NodeProperties(n); |
| 154 |
props.put(nodeRef, np); |
| 155 |
np.spSet.set(n); |
| 156 |
} |
| 157 |
|
| 158 |
for (int k = 0; k < getInternalNodeCount(); ++k) { |
| 159 |
final NodeRef nodeRef = getInternalNode(k); |
| 160 |
props.put(nodeRef, new NodeProperties(-1)); |
| 161 |
} |
| 162 |
|
| 163 |
nodePropsReady = false; |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
boolean check = spTree.getAttribute("check") != null; |
| 168 |
spTree.setAttribute("check", null); |
| 169 |
|
| 170 |
if (check) { |
| 171 |
|
| 172 |
|
| 173 |
for (SpeciesBindings.GeneTreeInfo gt : species.getGeneTrees()) { |
| 174 |
if (!isCompatible(gt)) { |
| 175 |
|
| 176 |
species.makeCompatible(spTree.getRootHeight()); |
| 177 |
for (SpeciesBindings.GeneTreeInfo t : species.getGeneTrees()) { |
| 178 |
assert isCompatible(t); |
| 179 |
} |
| 180 |
anyChange = false; |
| 181 |
break; |
| 182 |
} |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
public boolean constPopulation() { |
| 188 |
return constantPopulation; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
public boolean isCompatible(SpeciesBindings.GeneTreeInfo geneTreeInfo) { |
| 194 |
|
| 195 |
if (!nodePropsReady) { |
| 196 |
setSPsets(getRoot()); |
| 197 |
} |
| 198 |
return isCompatible(getRoot(), geneTreeInfo.getCoalInfo(), 0) >= 0; |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
private int isCompatible(NodeRef node, SpeciesBindings.CoalInfo[] cList, int loc) { |
| 204 |
if (!isExternal(node)) { |
| 205 |
int l = -1; |
| 206 |
for (int nc = 0; nc < getChildCount(node); ++nc) { |
| 207 |
int l1 = isCompatible(getChild(node, nc), cList, loc); |
| 208 |
if (l1 < 0) { |
| 209 |
return -1; |
| 210 |
} |
| 211 |
assert l == -1 || l1 == l; |
| 212 |
|
| 213 |
l = l1; |
| 214 |
} |
| 215 |
loc = l; |
| 216 |
|
| 217 |
assert cList[loc].ctime >= getNodeHeight(node); |
| 218 |
} |
| 219 |
|
| 220 |
if (node == getRoot()) { |
| 221 |
return cList.length; |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
final FixedBitSet nodeSps = props.get(node).spSet; |
| 226 |
|
| 227 |
final double limit = getNodeHeight(getParent(node)); |
| 228 |
|
| 229 |
while (loc < cList.length) { |
| 230 |
final SpeciesBindings.CoalInfo ci = cList[loc]; |
| 231 |
if (ci.ctime >= limit) { |
| 232 |
break; |
| 233 |
} |
| 234 |
boolean allIn = true, noneIn = true; |
| 235 |
|
| 236 |
for (int i = 0; i < 2; ++i) { |
| 237 |
final FixedBitSet s = ci.sinfo[i]; |
| 238 |
|
| 239 |
final int in1 = s.intersectCardinality(nodeSps); |
| 240 |
if (in1 > 0) { |
| 241 |
noneIn = false; |
| 242 |
} |
| 243 |
if (s.cardinality() != in1) { |
| 244 |
allIn = false; |
| 245 |
} |
| 246 |
} |
| 247 |
if (!(allIn || noneIn)) { |
| 248 |
return -1; |
| 249 |
} |
| 250 |
++loc; |
| 251 |
} |
| 252 |
return loc; |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
private static double |
| 257 |
fp(double val, double low, double[][] tt, int[] ii) { |
| 258 |
for (int k = 0; k < ii.length; ++k) { |
| 259 |
int ip = ii[k]; |
| 260 |
if (ip == tt[k].length || val <= tt[k][ip]) { |
| 261 |
--ip; |
| 262 |
while (ip >= 0 && val <= tt[k][ip]) { |
| 263 |
--ip; |
| 264 |
} |
| 265 |
assert ((ip < 0) || (tt[k][ip] < val)) && ((ip + 1 == tt[k].length) || (val <= tt[k][ip + 1])); |
| 266 |
if (ip >= 0) { |
| 267 |
low = Math.max(low, tt[k][ip]); |
| 268 |
} |
| 269 |
} else { |
| 270 |
++ip; |
| 271 |
while (ip < tt[k].length && val > tt[k][ip]) { |
| 272 |
++ip; |
| 273 |
} |
| 274 |
assert tt[k][ip - 1] < val && ((ip == tt[k].length) || (val <= tt[k][ip])); |
| 275 |
low = Math.max(low, tt[k][ip - 1]); |
| 276 |
} |
| 277 |
} |
| 278 |
return low; |
| 279 |
} |
| 280 |
|
| 281 |
private interface SimpleDemographicFunction { |
| 282 |
double population(double t); |
| 283 |
|
| 284 |
double upperBound(); |
| 285 |
} |
| 286 |
|
| 287 |
private class PLSD implements SimpleDemographicFunction { |
| 288 |
private final double[] pops; |
| 289 |
private final double[] times; |
| 290 |
|
| 291 |
public PLSD(double[] pops, double[] times) { |
| 292 |
assert pops.length == times.length + 1; |
| 293 |
|
| 294 |
this.pops = pops; |
| 295 |
this.times = times; |
| 296 |
} |
| 297 |
|
| 298 |
public double population(double t) { |
| 299 |
if (t >= upperBound()) { |
| 300 |
return pops[pops.length - 1]; |
| 301 |
} |
| 302 |
|
| 303 |
int k = 0; |
| 304 |
while (t > times[k]) { |
| 305 |
t -= times[k]; |
| 306 |
++k; |
| 307 |
} |
| 308 |
double a = t / (times[k] - (k > 0 ? times[k - 1] : 0)); |
| 309 |
return a * pops[k] + (1 - a) * pops[k + 1]; |
| 310 |
} |
| 311 |
|
| 312 |
public double upperBound() { |
| 313 |
return times[times.length - 1]; |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
private class Args { |
| 319 |
final double[][] cps = species.getPopTimesSingle(); |
| 320 |
final double[][] cpp; |
| 321 |
final int[] iSingle = new int[cps.length]; |
| 322 |
final int[] iPair; |
| 323 |
|
| 324 |
final double[] indicators = ((Parameter.Default) coalPointsIndicator).inspectParameterValues(); |
| 325 |
final double[] pops = ((Parameter.Default) coalPointsPops).inspectParameterValues(); |
| 326 |
final SimpleDemographicFunction[] dms; |
| 327 |
|
| 328 |
Args(Boolean bmp) { |
| 329 |
if (!bmp) { |
| 330 |
cpp = species.getPopTimesPair(); |
| 331 |
iPair = new int[cpp.length]; |
| 332 |
dms = null; |
| 333 |
} else { |
| 334 |
cpp = null; |
| 335 |
iPair = null; |
| 336 |
int nsps = cps.length; |
| 337 |
dms = new SimpleDemographicFunction[nsps]; |
| 338 |
for (int nsp = 0; nsp < nsps; ++nsp) { |
| 339 |
final int start = singleStartPoints[nsp]; |
| 340 |
final int stop = nsp < nsps - 1 ? singleStartPoints[nsp + 1] : pops.length; |
| 341 |
|
| 342 |
double[] pop = new double[1 + stop - start]; |
| 343 |
pop[0] = sppSplitPopulations.getParameterValue(nsp); |
| 344 |
|
| 345 |
for (int k = 0; k < stop - start; ++k) { |
| 346 |
pop[k + 1] = pops[start + k]; |
| 347 |
} |
| 348 |
dms[nsp] = new PLSD(pop, cps[nsp]); |
| 349 |
} |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
private double findPrev(double val, double low) { |
| 354 |
low = fp(val, low, cps, iSingle); |
| 355 |
low = fp(val, low, cpp, iPair); |
| 356 |
|
| 357 |
return low; |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
class RawPopulationHelper { |
| 362 |
final int[] preOrderIndices = new int[getNodeCount()]; |
| 363 |
final double[] pops = ((Parameter.Default) sppSplitPopulations).inspectParameterValues(); |
| 364 |
final int nsp = species.nSpecies(); |
| 365 |
final Args args = coalPointsPops != null ? new Args(bmp) : null; |
| 366 |
|
| 367 |
RawPopulationHelper() { |
| 368 |
setPreorderIndices(preOrderIndices); |
| 369 |
} |
| 370 |
|
| 371 |
public void getPopulations(NodeRef n, int nc, double[] p) { |
| 372 |
p[1] = pops[nsp + 2 * preOrderIndices[n.getNumber()] + nc]; |
| 373 |
|
| 374 |
final NodeRef child = getChild(n, nc); |
| 375 |
if (isExternal(child)) { |
| 376 |
p[0] = pops[props.get(child).speciesIndex]; |
| 377 |
} else { |
| 378 |
int k = nsp + 2 * preOrderIndices[child.getNumber()]; |
| 379 |
p[0] = pops[k] + pops[k + 1]; |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
public double tipPopulation(NodeRef tip) { |
| 384 |
return pops[props.get(tip).speciesIndex]; |
| 385 |
} |
| 386 |
|
| 387 |
public int nSpecies() { |
| 388 |
return species.nSpecies(); |
| 389 |
} |
| 390 |
|
| 391 |
public boolean perSpeciesPopulation() { |
| 392 |
return args != null; |
| 393 |
} |
| 394 |
|
| 395 |
public double[] getTimes(int ns) { |
| 396 |
return ((PLSD) args.dms[ns]).times; |
| 397 |
} |
| 398 |
|
| 399 |
public double[] getPops(int ns) { |
| 400 |
return ((PLSD) args.dms[ns]).pops; |
| 401 |
} |
| 402 |
|
| 403 |
public void getRootPopulations(double[] p) { |
| 404 |
int k = nsp + 2 * preOrderIndices[getRoot().getNumber()]; |
| 405 |
p[0] = pops[k] + pops[k + 1]; |
| 406 |
p[1] = nonConstRootPopulation ? pops[pops.length - 1] : p[0]; |
| 407 |
} |
| 408 |
|
| 409 |
public double geneTreesRootHeight() { |
| 410 |
|
| 411 |
double h = -1; |
| 412 |
for (SpeciesBindings.GeneTreeInfo t : species.getGeneTrees()) { |
| 413 |
h = Math.max(h, t.tree.getNodeHeight(t.tree.getRoot())); |
| 414 |
} |
| 415 |
return h; |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
RawPopulationHelper getPopulationHelper() { |
| 420 |
return new RawPopulationHelper(); |
| 421 |
} |
| 422 |
|
| 423 |
static private class Points implements Comparable<Points> { |
| 424 |
final double time; |
| 425 |
double population; |
| 426 |
final boolean use; |
| 427 |
|
| 428 |
Points(double t, double p) { |
| 429 |
time = t; |
| 430 |
population = p; |
| 431 |
use = true; |
| 432 |
} |
| 433 |
|
| 434 |
Points(double t, boolean u) { |
| 435 |
time = t; |
| 436 |
population = 0; |
| 437 |
use = u; |
| 438 |
} |
| 439 |
|
| 440 |
public int compareTo(Points points) { |
| 441 |
return time < points.time ? -1 : (time > points.time ? 1 : 0); |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
private NodeProperties |
| 446 |
setSPsets(NodeRef nodeID) { |
| 447 |
final NodeProperties nprop = props.get(nodeID); |
| 448 |
|
| 449 |
if (!isExternal(nodeID)) { |
| 450 |
nprop.spSet = new FixedBitSet(species.nSpecies()); |
| 451 |
for (int nc = 0; nc < getChildCount(nodeID); ++nc) { |
| 452 |
NodeProperties p = setSPsets(getChild(nodeID, nc)); |
| 453 |
nprop.spSet.union(p.spSet); |
| 454 |
} |
| 455 |
} |
| 456 |
return nprop; |
| 457 |
} |
| 458 |
|
| 459 |
private int ti2f(int i, int j) { |
| 460 |
return (i == 0) ? j : 2 * i + j + 1; |
| 461 |
} |
| 462 |
|
| 463 |
private VDdemographicFunction |
| 464 |
bestLinearFit(double[] xs, double[] ys, boolean[] use) { |
| 465 |
|
| 466 |
assert (xs.length + 1) == ys.length; |
| 467 |
assert ys.length == use.length + 2 || ys.length == use.length + 1; |
| 468 |
|
| 469 |
int N = ys.length; |
| 470 |
if (N == 2) { |
| 471 |
|
| 472 |
return new VDdemographicFunction(xs, ys, getUnits()); |
| 473 |
} |
| 474 |
|
| 475 |
List<Integer> iv = new ArrayList<Integer>(2); |
| 476 |
iv.add(0); |
| 477 |
for (int k = 0; k < N - 2; ++k) { |
| 478 |
if (use[k]) { |
| 479 |
iv.add(k + 1); |
| 480 |
} |
| 481 |
} |
| 482 |
iv.add(N - 1); |
| 483 |
|
| 484 |
double[] ati = new double[xs.length + 1]; |
| 485 |
ati[0] = 0.0; |
| 486 |
System.arraycopy(xs, 0, ati, 1, xs.length); |
| 487 |
int n = iv.size(); |
| 488 |
|
| 489 |
double[] a = new double[3 * n]; |
| 490 |
double[] v = new double[n]; |
| 491 |
|
| 492 |
for (int k = 0; k < n - 1; ++k) { |
| 493 |
int i0 = iv.get(k); |
| 494 |
int i1 = iv.get(k + 1); |
| 495 |
|
| 496 |
double u0 = ati[i0]; |
| 497 |
double u1 = ati[i1] - ati[i0]; |
| 498 |
|
| 499 |
if (i1 == N - 1) { |
| 500 |
i1 += 1; |
| 501 |
} |
| 502 |
|
| 503 |
final int l = ti2f(k, k); |
| 504 |
final int l1 = ti2f(k + 1, k); |
| 505 |
|
| 506 |
for (int j = i0; j < i1; ++j) { |
| 507 |
double t = ati[j]; |
| 508 |
double y = ys[j]; |
| 509 |
|
| 510 |
double z = (t - u0) / u1; |
| 511 |
v[k] += y * (1 - z); |
| 512 |
|
| 513 |
a[l] += (1 - z) * (1 - z); |
| 514 |
a[l + 1] += z * (1 - z); |
| 515 |
|
| 516 |
a[l1] += z * (1 - z); |
| 517 |
a[l1 + 1] += z * z; |
| 518 |
v[k + 1] += y * z; |
| 519 |
} |
| 520 |
} |
| 521 |
|
| 522 |
for (int k = 0; k < n - 1; ++k) { |
| 523 |
|
| 524 |
final double r = a[ti2f(k + 1, k)] / a[ti2f(k, k)]; |
| 525 |
for (int j = k; j < k + 3; ++j) { |
| 526 |
a[ti2f((k + 1), j)] -= a[ti2f(k, j)] * r; |
| 527 |
} |
| 528 |
v[k + 1] -= v[k] * r; |
| 529 |
} |
| 530 |
|
| 531 |
double[] z = new double[n]; |
| 532 |
for (int k = n - 1; k > 0; --k) { |
| 533 |
z[k] = v[k] / a[ti2f(k, k)]; |
| 534 |
v[k - 1] -= a[ti2f((k - 1), k)] * z[k]; |
| 535 |
} |
| 536 |
|
| 537 |
z[0] = v[0] / a[ti2f(0, 0)]; |
| 538 |
double[] t = new double[iv.size() - 1]; |
| 539 |
for (int j = 0; j < t.length; ++j) { |
| 540 |
t[j] = ati[iv.get(j + 1)]; |
| 541 |
} |
| 542 |
return new VDdemographicFunction(t, z, getUnits()); |
| 543 |
} |
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
private NodeProperties getDemographicPoints(final NodeRef nodeID, Args args, Points[][] pointsList) { |
| 553 |
|
| 554 |
final NodeProperties nprop = props.get(nodeID); |
| 555 |
final int nSpecies = species.nSpecies(); |
| 556 |
|
| 557 |
|
| 558 |
if (!isExternal(nodeID)) { |
| 559 |
nprop.spSet = new FixedBitSet(nSpecies); |
| 560 |
for (int nc = 0; nc < getChildCount(nodeID); ++nc) { |
| 561 |
final NodeProperties p = getDemographicPoints(getChild(nodeID, nc), args, pointsList); |
| 562 |
nprop.spSet.union(p.spSet); |
| 563 |
} |
| 564 |
} |
| 565 |
|
| 566 |
if (args == null) { |
| 567 |
return nprop; |
| 568 |
} |
| 569 |
|
| 570 |
|
| 571 |
final double cHeight = nodeID != getRoot() ? getNodeHeight(getParent(nodeID)) : Double.MAX_VALUE; |
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
List<Points> allPoints = new ArrayList<Points>(5); |
| 576 |
|
| 577 |
if (bmp) { |
| 578 |
for (int isp = nprop.spSet.nextOnBit(0); isp >= 0; isp = nprop.spSet.nextOnBit(isp + 1)) { |
| 579 |
final double[] cp = args.cps[isp]; |
| 580 |
final int upi = singleStartPoints[isp]; |
| 581 |
|
| 582 |
int i = args.iSingle[isp]; |
| 583 |
for (; i < cp.length && cp[i] < cHeight; ++i) { |
| 584 |
allPoints.add(new Points(cp[i], args.indicators[upi + i] > 0)); |
| 585 |
} |
| 586 |
args.iSingle[isp] = i; |
| 587 |
} |
| 588 |
} else { |
| 589 |
|
| 590 |
for (int isp = nprop.spSet.nextOnBit(0); isp >= 0; isp = nprop.spSet.nextOnBit(isp + 1)) { |
| 591 |
final double nodeHeight = spTree.getNodeHeight(nodeID); |
| 592 |
{ |
| 593 |
double[] cp = args.cps[isp]; |
| 594 |
final int upi = singleStartPoints[isp]; |
| 595 |
|
| 596 |
int i = args.iSingle[isp]; |
| 597 |
|
| 598 |
while (i < cp.length && cp[i] < cHeight) { |
| 599 |
if (args.indicators[upi + i] > 0) { |
| 600 |
|
| 601 |
args.iSingle[isp] = i; |
| 602 |
double prev = args.findPrev(cp[i], nodeHeight); |
| 603 |
double mid = (prev + cp[i]) / 2.0; |
| 604 |
assert nodeHeight < mid; |
| 605 |
allPoints.add(new Points(mid, args.pops[upi + i])); |
| 606 |
} |
| 607 |
++i; |
| 608 |
} |
| 609 |
args.iSingle[isp] = i; |
| 610 |
} |
| 611 |
|
| 612 |
final int kx = (isp * (2 * nSpecies - isp - 3)) / 2 - 1; |
| 613 |
for (int y = nprop.spSet.nextOnBit(isp + 1); y >= 0; y = nprop.spSet.nextOnBit(y + 1)) { |
| 614 |
|
| 615 |
assert isp < y; |
| 616 |
int k = kx + y; |
| 617 |
|
| 618 |
double[] cp = args.cpp[k]; |
| 619 |
int i = args.iPair[k]; |
| 620 |
final int upi = pairStartPoints[k]; |
| 621 |
|
| 622 |
while (i < cp.length && cp[i] < cHeight) { |
| 623 |
if (args.indicators[upi + i] > 0) { |
| 624 |
|
| 625 |
args.iPair[k] = i; |
| 626 |
final double prev = args.findPrev(cp[i], nodeHeight); |
| 627 |
double mid = (prev + cp[i]) / 2.0; |
| 628 |
assert nodeHeight < mid; |
| 629 |
allPoints.add(new Points(mid, args.pops[upi + i])); |
| 630 |
} |
| 631 |
++i; |
| 632 |
} |
| 633 |
args.iPair[k] = i; |
| 634 |
} |
| 635 |
} |
| 636 |
} |
| 637 |
|
| 638 |
Points[] all = null; |
| 639 |
|
| 640 |
if (allPoints.size() > 0) { |
| 641 |
all = allPoints.toArray(new Points[allPoints.size()]); |
| 642 |
if (all.length > 1) { |
| 643 |
HeapSort.sort(all); |
| 644 |
} |
| 645 |
|
| 646 |
int len = all.length; |
| 647 |
|
| 648 |
if (bmp) { |
| 649 |
int k = 0; |
| 650 |
while (k + 1 < len) { |
| 651 |
final double t = all[k].time; |
| 652 |
if (t == all[k + 1].time) { |
| 653 |
int j = k + 2; |
| 654 |
boolean use = all[k].use || all[k + 1].use; |
| 655 |
|
| 656 |
while (j < len && t == all[j].time) { |
| 657 |
use = use || all[j].use; |
| 658 |
j += 1; |
| 659 |
} |
| 660 |
int removed = (j - k - 1); |
| 661 |
all[k] = new Points(t, use); |
| 662 |
for (int i = k + 1; i < len - removed; ++i) { |
| 663 |
all[i] = all[i + removed]; |
| 664 |
} |
| 665 |
len -= removed; |
| 666 |
} |
| 667 |
++k; |
| 668 |
} |
| 669 |
} else { |
| 670 |
|
| 671 |
|
| 672 |
int k = 0; |
| 673 |
while (k + 1 < len) { |
| 674 |
double t = all[k].time; |
| 675 |
if (t == all[k + 1].time) { |
| 676 |
int j = k + 2; |
| 677 |
double v = all[k].population + all[k + 1].population; |
| 678 |
while (j < len && t == all[j].time) { |
| 679 |
v += all[j].population; |
| 680 |
j += 1; |
| 681 |
} |
| 682 |
int removed = (j - k - 1); |
| 683 |
all[k] = new Points(t, v / (removed + 1)); |
| 684 |
for (int i = k + 1; i < len - removed; ++i) { |
| 685 |
all[i] = all[i + removed]; |
| 686 |
} |
| 687 |
|
| 688 |
len -= removed; |
| 689 |
} |
| 690 |
++k; |
| 691 |
} |
| 692 |
} |
| 693 |
|
| 694 |
if (len != all.length) { |
| 695 |
Points[] a = new Points[len]; |
| 696 |
System.arraycopy(all, 0, a, 0, len); |
| 697 |
all = a; |
| 698 |
} |
| 699 |
|
| 700 |
if (bmp) { |
| 701 |
for (Points p : all) { |
| 702 |
double t = p.time; |
| 703 |
assert p.population == 0; |
| 704 |
for (int isp = nprop.spSet.nextOnBit(0); isp >= 0; isp = nprop.spSet.nextOnBit(isp + 1)) { |
| 705 |
SimpleDemographicFunction d = args.dms[isp]; |
| 706 |
if (t <= d.upperBound()) { |
| 707 |
p.population += d.population(t); |
| 708 |
} |
| 709 |
} |
| 710 |
} |
| 711 |
} |
| 712 |
} |
| 713 |
|
| 714 |
pointsList[nodeID.getNumber()] = all; |
| 715 |
|
| 716 |
return nprop; |
| 717 |
} |
| 718 |
|
| 719 |
private int setDemographics(NodeRef nodeID, int pStart, int side, double[] pops, Points[][] pointsList) { |
| 720 |
|
| 721 |
final int nSpecies = species.nSpecies(); |
| 722 |
final NodeProperties nprop = props.get(nodeID); |
| 723 |
int pEnd; |
| 724 |
double p0; |
| 725 |
|
| 726 |
if (isExternal(nodeID)) { |
| 727 |
final int sps = nprop.speciesIndex; |
| 728 |
p0 = pops[sps]; |
| 729 |
pEnd = pStart; |
| 730 |
} else { |
| 731 |
assert getChildCount(nodeID) == 2; |
| 732 |
|
| 733 |
final int iHere = setDemographics(getChild(nodeID, 0), pStart, 0, pops, pointsList); |
| 734 |
pEnd = setDemographics(getChild(nodeID, 1), iHere + 1, 1, pops, pointsList); |
| 735 |
if (constantPopulation) { |
| 736 |
final int i = nSpecies + iHere; |
| 737 |
p0 = pops[i]; |
| 738 |
} else { |
| 739 |
final int i = nSpecies + iHere * 2; |
| 740 |
p0 = pops[i] + pops[i + 1]; |
| 741 |
} |
| 742 |
} |
| 743 |
|
| 744 |
if (constantPopulation) { |
| 745 |
double[] xs = {}; |
| 746 |
double[] ys = {p0}; |
| 747 |
|
| 748 |
nprop.demogf = new VDdemographicFunction(xs, ys, getUnits()); |
| 749 |
|
| 750 |
} else { |
| 751 |
final double t0 = getNodeHeight(nodeID); |
| 752 |
|
| 753 |
Points[] p = pointsList != null ? pointsList[nodeID.getNumber()] : null; |
| 754 |
final int plen = p == null ? 0 : p.length; |
| 755 |
|
| 756 |
final boolean isRoot = nodeID == getRoot(); |
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
final boolean useBMP = bmp && pointsList != null; |
| 761 |
|
| 762 |
|
| 763 |
final int len = plen + (useBMP ? (!isRoot ? 1 : 0) : 1); |
| 764 |
double[] xs = new double[len]; |
| 765 |
double[] ys = new double[len + 1]; |
| 766 |
boolean[] use = new boolean[len]; |
| 767 |
ys[0] = p0; |
| 768 |
for (int i = 0; i < plen; ++i) { |
| 769 |
xs[i] = p[i].time - t0; |
| 770 |
ys[i + 1] = p[i].population; |
| 771 |
use[i] = p[i].use; |
| 772 |
} |
| 773 |
|
| 774 |
if (!isRoot) { |
| 775 |
final int anccIndex = (side == 0) ? pEnd : pStart - 1; |
| 776 |
final double pe = pops[nSpecies + anccIndex * 2 + side]; |
| 777 |
final double b = getBranchLength(nodeID); |
| 778 |
|
| 779 |
xs[xs.length - 1] = b; |
| 780 |
ys[ys.length - 1] = pe; |
| 781 |
} |
| 782 |
|
| 783 |
if (useBMP) { |
| 784 |
nprop.demogf = bestLinearFit(xs, ys, use); |
| 785 |
} else { |
| 786 |
|
| 787 |
if (isRoot) { |
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
double h = -1; |
| 792 |
for (SpeciesBindings.GeneTreeInfo t : species.getGeneTrees()) { |
| 793 |
h = Math.max(h, t.tree.getNodeHeight(t.tree.getRoot())); |
| 794 |
} |
| 795 |
|
| 796 |
final double rh = h - t0; |
| 797 |
xs[xs.length - 1] = rh; |
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
ys[ys.length - 1] = pointsList != null ? ys[ys.length - 2] : |
| 802 |
(nonConstRootPopulation ? pops[pops.length - 1] : ys[ys.length - 2]); |
| 803 |
} |
| 804 |
|
| 805 |
nprop.demogf = new VDdemographicFunction(xs, ys, getUnits()); |
| 806 |
} |
| 807 |
} |
| 808 |
return pEnd; |
| 809 |
} |
| 810 |
|
| 811 |
private void setNodeProperties() { |
| 812 |
Points[][] perBranchPoints = null; |
| 813 |
|
| 814 |
if (coalPointsPops != null) { |
| 815 |
final Args args = new Args(bmp); |
| 816 |
perBranchPoints = new Points[getNodeCount()][]; |
| 817 |
getDemographicPoints(getRoot(), args, perBranchPoints); |
| 818 |
} else { |
| 819 |
|
| 820 |
getDemographicPoints(getRoot(), null, null); |
| 821 |
} |
| 822 |
|
| 823 |
setDemographics(getRoot(), 0, -1, ((Parameter.Default) sppSplitPopulations).inspectParameterValues(), perBranchPoints); |
| 824 |
} |
| 825 |
|
| 826 |
private Map<NodeRef, NodeProperties> getProps() { |
| 827 |
if (!nodePropsReady) { |
| 828 |
setNodeProperties(); |
| 829 |
nodePropsReady = true; |
| 830 |
} |
| 831 |
return props; |
| 832 |
} |
| 833 |
|
| 834 |
public DemographicFunction getNodeDemographic(NodeRef node) { |
| 835 |
return getProps().get(node).demogf; |
| 836 |
} |
| 837 |
|
| 838 |
public FixedBitSet spSet(NodeRef node) { |
| 839 |
return getProps().get(node).spSet; |
| 840 |
} |
| 841 |
|
| 842 |
public int speciesIndex(NodeRef tip) { |
| 843 |
assert isExternal(tip); |
| 844 |
|
| 845 |
|
| 846 |
return props.get(tip).speciesIndex; |
| 847 |
} |
| 848 |
|
| 849 |
private Double setInitialSplitPopulations(FlexibleTree startTree, NodeRef node, int pos[]) { |
| 850 |
if (!startTree.isExternal(node)) { |
| 851 |
int loc = -1; |
| 852 |
for (int nc = 0; nc < startTree.getChildCount(node); ++nc) { |
| 853 |
final Double p = setInitialSplitPopulations(startTree, startTree.getChild(node, nc), pos); |
| 854 |
if (!constantPopulation && nc == 0) { |
| 855 |
loc = pos[0]; |
| 856 |
pos[0] += 1; |
| 857 |
} |
| 858 |
if (p != null) { |
| 859 |
if (constantPopulation) { |
| 860 |
|
| 861 |
} else { |
| 862 |
sppSplitPopulations.setParameterValueQuietly(species.nSpecies() + 2 * loc + nc, p); |
| 863 |
} |
| 864 |
} |
| 865 |
} |
| 866 |
} |
| 867 |
|
| 868 |
final String comment = (String) startTree.getNodeAttribute(node, NewickImporter.COMMENT); |
| 869 |
Double p0 = null; |
| 870 |
if (comment != null) { |
| 871 |
StringTokenizer st = new StringTokenizer(comment); |
| 872 |
|
| 873 |
p0 = Double.parseDouble(st.nextToken()); |
| 874 |
if (startTree.isExternal(node)) { |
| 875 |
int ns = (Integer) startTree.getNodeAttribute(node, spIndexAttrName); |
| 876 |
sppSplitPopulations.setParameterValueQuietly(ns, p0); |
| 877 |
} else if (constantPopulation) { |
| 878 |
|
| 879 |
sppSplitPopulations.setParameterValueQuietly(species.nSpecies() + pos[0], p0); |
| 880 |
pos[0] += 1; |
| 881 |
} |
| 882 |
|
| 883 |
|
| 884 |
if (st.hasMoreTokens()) { |
| 885 |
p0 = Double.parseDouble(st.nextToken()); |
| 886 |
} |
| 887 |
} |
| 888 |
return !constantPopulation ? p0 : null; |
| 889 |
} |
| 890 |
|
| 891 |
private SimpleTree compatibleUninformedSpeciesTree(Tree startTree) { |
| 892 |
double rootHeight = Double.MAX_VALUE; |
| 893 |
|
| 894 |
for (SpeciesBindings.GeneTreeInfo t : species.getGeneTrees()) { |
| 895 |
rootHeight = Math.min(rootHeight, t.getCoalInfo()[0].ctime); |
| 896 |
} |
| 897 |
|
| 898 |
final SpeciesBindings.SPinfo[] spp = species.species; |
| 899 |
|
| 900 |
if (startTree != null) { |
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
if (startTree.getExternalNodeCount() != spp.length) { |
| 905 |
throw new Error("Start tree error - different number of tips"); |
| 906 |
} |
| 907 |
|
| 908 |
final FlexibleTree tree = new FlexibleTree(startTree, true); |
| 909 |
tree.resolveTree(); |
| 910 |
final double treeHeight = tree.getRootHeight(); |
| 911 |
if (treeHeight <= 0) { |
| 912 |
tree.setRootHeight(1.0); |
| 913 |
MutableTree.Utils.correctHeightsForTips(tree); |
| 914 |
SimpleTree.Utils.scaleNodeHeights(tree, rootHeight / tree.getRootHeight()); |
| 915 |
} |
| 916 |
|
| 917 |
SimpleTree sTree = new SimpleTree(tree); |
| 918 |
for (int ns = 0; ns < spp.length; ns++) { |
| 919 |
SpeciesBindings.SPinfo sp = spp[ns]; |
| 920 |
final int i = sTree.getTaxonIndex(sp.name); |
| 921 |
if (i < 0) { |
| 922 |
throw new Error(sp.name + " is not present in the start tree"); |
| 923 |
} |
| 924 |
final SimpleNode node = sTree.getExternalNode(i); |
| 925 |
node.setAttribute(spIndexAttrName, ns); |
| 926 |
|
| 927 |
|
| 928 |
tree.setNodeAttribute(tree.getNode(tree.getTaxonIndex(sp.name)), spIndexAttrName, ns); |
| 929 |
} |
| 930 |
|
| 931 |
if (treeHeight > 0) { |
| 932 |
sTree.setAttribute("check", new Double(rootHeight)); |
| 933 |
} |
| 934 |
|
| 935 |
{ |
| 936 |
|
| 937 |
int[] pos = {0}; |
| 938 |
setInitialSplitPopulations(tree, tree.getRoot(), pos); |
| 939 |
} |
| 940 |
|
| 941 |
return sTree; |
| 942 |
} |
| 943 |
|
| 944 |
|
| 945 |
final double delta = rootHeight / (spp.length + 1); |
| 946 |
double cTime = delta; |
| 947 |
|
| 948 |
List<SimpleNode> subs = new ArrayList<SimpleNode>(spp.length); |
| 949 |
|
| 950 |
for (int ns = 0; ns < spp.length; ns++) { |
| 951 |
SpeciesBindings.SPinfo sp = spp[ns]; |
| 952 |
final SimpleNode node = new SimpleNode(); |
| 953 |
node.setTaxon(new Taxon(sp.name)); |
| 954 |
subs.add(node); |
| 955 |
|
| 956 |
node.setAttribute(spIndexAttrName, ns); |
| 957 |
} |
| 958 |
|
| 959 |
while (subs.size() > 1) { |
| 960 |
final SimpleNode node = new SimpleNode(); |
| 961 |
int i = 0, j = 1; |
| 962 |
node.addChild(subs.get(i)); |
| 963 |
node.addChild(subs.get(j)); |
| 964 |
node.setHeight(cTime); |
| 965 |
cTime += delta; |
| 966 |
subs.set(j, node); |
| 967 |
subs.remove(i); |
| 968 |
} |
| 969 |
|
| 970 |
return new SimpleTree(subs.get(0)); |
| 971 |
} |
| 972 |
|
| 973 |
public void setPreorderIndices(int[] indices) { |
| 974 |
setPreorderIndices(getRoot(), 0, indices); |
| 975 |
} |
| 976 |
|
| 977 |
private int setPreorderIndices(NodeRef node, int loc, int[] indices) { |
| 978 |
if (!isExternal(node)) { |
| 979 |
int l = setPreorderIndices(getChild(node, 0), loc, indices); |
| 980 |
indices[node.getNumber()] = l; |
| 981 |
loc = setPreorderIndices(getChild(node, 1), l + 1, indices); |
| 982 |
} |
| 983 |
return loc; |
| 984 |
} |
| 985 |
|
| 986 |
public String getName() { |
| 987 |
return getModelName(); |
| 988 |
} |
| 989 |
|
| 990 |
static private TreeNodeSlide internalTreeOP = null; |
| 991 |
|
| 992 |
public int scale(double scaleFactor, int nDims, boolean testBounds) { |
| 993 |
assert scaleFactor > 0; |
| 994 |
if (nDims <= 0) { |
| 995 |
|
| 996 |
|
| 997 |
|
| 998 |
beginTreeEdit(); |
| 999 |
final int count = getInternalNodeCount(); |
| 1000 |
for (int i = 0; i < count; ++i) { |
| 1001 |
final NodeRef n = getInternalNode(i); |
| 1002 |
setNodeHeight(n, getNodeHeight(n) * scaleFactor); |
| 1003 |
} |
| 1004 |
endTreeEdit(); |
| 1005 |
fireModelChanged(this, 1); |
| 1006 |
return count; |
| 1007 |
} else { |
| 1008 |
if (nDims != 1) { |
| 1009 |
throw new UnsupportedOperationException("not implemented for count != 1"); |
| 1010 |
} |
| 1011 |
if (internalTreeOP == null) { |
| 1012 |
internalTreeOP = new TreeNodeSlide(this, species, 1); |
| 1013 |
} |
| 1014 |
|
| 1015 |
internalTreeOP.operateOneNode(scaleFactor); |
| 1016 |
fireModelChanged(this, 1); |
| 1017 |
return nDims; |
| 1018 |
} |
| 1019 |
} |
| 1020 |
|
| 1021 |
@Override |
| 1022 |
public boolean testBounds() { |
| 1023 |
return true; |
| 1024 |
} |
| 1025 |
|
| 1026 |
private final boolean verbose = false; |
| 1027 |
|
| 1028 |
protected void handleModelChangedEvent(Model model, Object object, int index) { |
| 1029 |
if (verbose) System.out.println(" SPtree: model changed " + model.getId()); |
| 1030 |
|
| 1031 |
nodePropsReady = false; |
| 1032 |
anyChange = true; |
| 1033 |
|
| 1034 |
fireModelChanged(); |
| 1035 |
} |
| 1036 |
|
| 1037 |
protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) { |
| 1038 |
if (verbose) System.out.println(" SPtree: parameter changed " + variable.getId()); |
| 1039 |
|
| 1040 |
nodePropsReady = false; |
| 1041 |
anyChange = true; |
| 1042 |
} |
| 1043 |
|
| 1044 |
protected void storeState() { |
| 1045 |
assert !treeChanged; |
| 1046 |
assert !anyChange; |
| 1047 |
} |
| 1048 |
|
| 1049 |
protected void restoreState() { |
| 1050 |
if (verbose) System.out.println(" SPtree: restore (" + treeChanged + "," + anyChange + ")"); |
| 1051 |
|
| 1052 |
if (treeChanged) { |
| 1053 |
|
| 1054 |
spTree.beginTreeEdit(); |
| 1055 |
|
| 1056 |
for (int k = 0; k < getInternalNodeCount(); ++k) { |
| 1057 |
final NodeRef node = getInternalNode(k); |
| 1058 |
final int index = node.getNumber(); |
| 1059 |
final double h = heights[index]; |
| 1060 |
if (getNodeHeight(node) != h) { |
| 1061 |
setNodeHeight(node, h); |
| 1062 |
} |
| 1063 |
for (int nc = 0; nc < 2; ++nc) { |
| 1064 |
final NodeRef child = getChild(node, nc); |
| 1065 |
|
| 1066 |
final NodeRef child1 = children[2 * index + nc]; |
| 1067 |
if (child != child1) { |
| 1068 |
replaceChild(node, child, child1); |
| 1069 |
} |
| 1070 |
assert getParent(child1) == node; |
| 1071 |
} |
| 1072 |
} |
| 1073 |
setRoot(children[children.length - 1]); |
| 1074 |
|
| 1075 |
if (verbose) System.out.println(" restored to: " + spTree); |
| 1076 |
|
| 1077 |
spTree.endTreeEdit(); |
| 1078 |
} |
| 1079 |
if (treeChanged || anyChange) { |
| 1080 |
setNodeProperties(); |
| 1081 |
} |
| 1082 |
treeChanged = false; |
| 1083 |
anyChange = false; |
| 1084 |
} |
| 1085 |
|
| 1086 |
protected void acceptState() { |
| 1087 |
if (verbose) System.out.println(" SPtree: accept"); |
| 1088 |
|
| 1089 |
treeChanged = false; |
| 1090 |
anyChange = false; |
| 1091 |
} |
| 1092 |
|
| 1093 |
String previousTopology = null; |
| 1094 |
|
| 1095 |
public boolean logNow(long state) { |
| 1096 |
final String curTop = TreeUtils.uniqueNewick(spTree, spTree.getRoot()); |
| 1097 |
if (state == 0 || !curTop.equals(previousTopology)) { |
| 1098 |
previousTopology = curTop; |
| 1099 |
return true; |
| 1100 |
} |
| 1101 |
return false; |
| 1102 |
} |
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
|
| 1114 |
|
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
|
| 1121 |
|
| 1122 |
|
| 1123 |
TreeTrait dmt = new TreeTrait.DA() { |
| 1124 |
public String getTraitName() { |
| 1125 |
return "dmt"; |
| 1126 |
} |
| 1127 |
|
| 1128 |
public Intent getIntent() { |
| 1129 |
return Intent.NODE; |
| 1130 |
} |
| 1131 |
|
| 1132 |
public double[] getTrait(Tree tree, NodeRef node) { |
| 1133 |
assert tree == SpeciesTreeModel.this; |
| 1134 |
|
| 1135 |
final VDdemographicFunction df = (VDdemographicFunction) getNodeDemographic(node); |
| 1136 |
return df.times(); |
| 1137 |
} |
| 1138 |
}; |
| 1139 |
|
| 1140 |
TreeTrait dmv = new TreeTrait.DA() { |
| 1141 |
public String getTraitName() { |
| 1142 |
return "dmv"; |
| 1143 |
} |
| 1144 |
|
| 1145 |
public Intent getIntent() { |
| 1146 |
return Intent.NODE; |
| 1147 |
} |
| 1148 |
|
| 1149 |
public double[] getTrait(Tree tree, NodeRef node) { |
| 1150 |
assert tree == SpeciesTreeModel.this; |
| 1151 |
|
| 1152 |
final VDdemographicFunction df = (VDdemographicFunction) getNodeDemographic(node); |
| 1153 |
return df.values(); |
| 1154 |
} |
| 1155 |
}; |
| 1156 |
|
| 1157 |
|
| 1158 |
public TreeTrait[] getTreeTraits() { |
| 1159 |
return new TreeTrait[]{dmt, dmv}; |
| 1160 |
} |
| 1161 |
|
| 1162 |
public TreeTrait getTreeTrait(String key) { |
| 1163 |
if (key.equals(dmt.getTraitName())) { |
| 1164 |
return dmt; |
| 1165 |
} else if (key.equals(dmv.getTraitName())) { |
| 1166 |
return dmv; |
| 1167 |
} |
| 1168 |
throw new IllegalArgumentException(); |
| 1169 |
} |
| 1170 |
|
| 1171 |
|
| 1172 |
|
| 1173 |
|
| 1174 |
public SimpleTree getSimpleTree() { |
| 1175 |
return spTree; |
| 1176 |
} |
| 1177 |
|
| 1178 |
public Tree getCopy() { |
| 1179 |
return spTree.getCopy(); |
| 1180 |
} |
| 1181 |
|
| 1182 |
public Type getUnits() { |
| 1183 |
return spTree.getUnits(); |
| 1184 |
} |
| 1185 |
|
| 1186 |
public void setUnits(Type units) { |
| 1187 |
spTree.setUnits(units); |
| 1188 |
} |
| 1189 |
|
| 1190 |
public int getNodeCount() { |
| 1191 |
return spTree.getNodeCount(); |
| 1192 |
} |
| 1193 |
|
| 1194 |
public boolean hasNodeHeights() { |
| 1195 |
return spTree.hasNodeHeights(); |
| 1196 |
} |
| 1197 |
|
| 1198 |
public double getNodeHeight(NodeRef node) { |
| 1199 |
return spTree.getNodeHeight(node); |
| 1200 |
} |
| 1201 |
|
| 1202 |
public double getNodeRate(NodeRef node) { |
| 1203 |
return spTree.getNodeRate(node); |
| 1204 |
} |
| 1205 |
|
| 1206 |
public Taxon getNodeTaxon(NodeRef node) { |
| 1207 |
return spTree.getNodeTaxon(node); |
| 1208 |
} |
| 1209 |
|
| 1210 |
public int getChildCount(NodeRef node) { |
| 1211 |
return spTree.getChildCount(node); |
| 1212 |
} |
| 1213 |
|
| 1214 |
public boolean isExternal(NodeRef node) { |
| 1215 |
return spTree.isExternal(node); |
| 1216 |
} |
| 1217 |
|
| 1218 |
public boolean isRoot(NodeRef node) { |
| 1219 |
return spTree.isRoot(node); |
| 1220 |
} |
| 1221 |
|
| 1222 |
public NodeRef getChild(NodeRef node, int i) { |
| 1223 |
return spTree.getChild(node, i); |
| 1224 |
} |
| 1225 |
|
| 1226 |
public NodeRef getParent(NodeRef node) { |
| 1227 |
return spTree.getParent(node); |
| 1228 |
} |
| 1229 |
|
| 1230 |
public boolean hasBranchLengths() { |
| 1231 |
return spTree.hasBranchLengths(); |
| 1232 |
} |
| 1233 |
|
| 1234 |
public double getBranchLength(NodeRef node) { |
| 1235 |
return spTree.getBranchLength(node); |
| 1236 |
} |
| 1237 |
|
| 1238 |
public void setBranchLength(NodeRef node, double length) { |
| 1239 |
spTree.setBranchLength(node, length); |
| 1240 |
} |
| 1241 |
|
| 1242 |
public NodeRef getExternalNode(int i) { |
| 1243 |
return spTree.getExternalNode(i); |
| 1244 |
} |
| 1245 |
|
| 1246 |
public NodeRef getInternalNode(int i) { |
| 1247 |
return spTree.getInternalNode(i); |
| 1248 |
} |
| 1249 |
|
| 1250 |
public NodeRef getNode(int i) { |
| 1251 |
return spTree.getNode(i); |
| 1252 |
} |
| 1253 |
|
| 1254 |
public int getExternalNodeCount() { |
| 1255 |
return spTree.getExternalNodeCount(); |
| 1256 |
} |
| 1257 |
|
| 1258 |
public int getInternalNodeCount() { |
| 1259 |
return spTree.getInternalNodeCount(); |
| 1260 |
} |
| 1261 |
|
| 1262 |
public NodeRef getRoot() { |
| 1263 |
return spTree.getRoot(); |
| 1264 |
} |
| 1265 |
|
| 1266 |
public void setRoot(NodeRef r) { |
| 1267 |
spTree.setRoot(r); |
| 1268 |
} |
| 1269 |
|
| 1270 |
public void addChild(NodeRef p, NodeRef c) { |
| 1271 |
spTree.addChild(p, c); |
| 1272 |
} |
| 1273 |
|
| 1274 |
public void removeChild(NodeRef p, NodeRef c) { |
| 1275 |
spTree.removeChild(p, c); |
| 1276 |
} |
| 1277 |
|
| 1278 |
public void replaceChild(NodeRef node, NodeRef child, NodeRef newChild) { |
| 1279 |
spTree.replaceChild(node, child, newChild); |
| 1280 |
} |
| 1281 |
|
| 1282 |
public boolean beginTreeEdit() { |
| 1283 |
boolean beingEdited = spTree.beginTreeEdit(); |
| 1284 |
if (!beingEdited) { |
| 1285 |
|
| 1286 |
for (int n = 0; n < getInternalNodeCount(); ++n) { |
| 1287 |
final NodeRef node = getInternalNode(n); |
| 1288 |
final int k = node.getNumber(); |
| 1289 |
children[2 * k] = getChild(node, 0); |
| 1290 |
children[2 * k + 1] = getChild(node, 1); |
| 1291 |
heights[k] = getNodeHeight(node); |
| 1292 |
} |
| 1293 |
children[children.length - 1] = getRoot(); |
| 1294 |
|
| 1295 |
treeChanged = true; |
| 1296 |
nodePropsReady = false; |
| 1297 |
|
| 1298 |
} |
| 1299 |
return beingEdited; |
| 1300 |
} |
| 1301 |
|
| 1302 |
public void endTreeEdit() { |
| 1303 |
spTree.endTreeEdit(); |
| 1304 |
fireModelChanged(); |
| 1305 |
} |
| 1306 |
|
| 1307 |
public void setNodeHeight(NodeRef n, double height) { |
| 1308 |
spTree.setNodeHeight(n, height); |
| 1309 |
} |
| 1310 |
|
| 1311 |
public void setNodeRate(NodeRef n, double rate) { |
| 1312 |
spTree.setNodeRate(n, rate); |
| 1313 |
} |
| 1314 |
|
| 1315 |
public void setNodeAttribute(NodeRef node, String name, Object value) { |
| 1316 |
spTree.setNodeAttribute(node, name, value); |
| 1317 |
} |
| 1318 |
|
| 1319 |
public Object getNodeAttribute(NodeRef node, String name) { |
| 1320 |
return spTree.getNodeAttribute(node, name); |
| 1321 |
} |
| 1322 |
|
| 1323 |
public Iterator getNodeAttributeNames(NodeRef node) { |
| 1324 |
return spTree.getNodeAttributeNames(node); |
| 1325 |
} |
| 1326 |
|
| 1327 |
public int getTaxonCount() { |
| 1328 |
return spTree.getTaxonCount(); |
| 1329 |
} |
| 1330 |
|
| 1331 |
public Taxon getTaxon(int taxonIndex) { |
| 1332 |
return spTree.getTaxon(taxonIndex); |
| 1333 |
} |
| 1334 |
|
| 1335 |
public String getTaxonId(int taxonIndex) { |
| 1336 |
return spTree.getTaxonId(taxonIndex); |
| 1337 |
} |
| 1338 |
|
| 1339 |
public int getTaxonIndex(String id) { |
| 1340 |
return spTree.getTaxonIndex(id); |
| 1341 |
} |
| 1342 |
|
| 1343 |
public int getTaxonIndex(Taxon taxon) { |
| 1344 |
|
| 1345 |
return getTaxonIndex(taxon.getId()); |
| 1346 |
} |
| 1347 |
|
| 1348 |
public List<Taxon> asList() { |
| 1349 |
return spTree.asList(); |
| 1350 |
} |
| 1351 |
|
| 1352 |
public Iterator<Taxon> iterator() { |
| 1353 |
return spTree.iterator(); |
| 1354 |
} |
| 1355 |
|
| 1356 |
public Object getTaxonAttribute(int taxonIndex, String name) { |
| 1357 |
return spTree.getTaxonAttribute(taxonIndex, name); |
| 1358 |
} |
| 1359 |
|
| 1360 |
public int addTaxon(Taxon taxon) { |
| 1361 |
return spTree.addTaxon(taxon); |
| 1362 |
} |
| 1363 |
|
| 1364 |
public boolean removeTaxon(Taxon taxon) { |
| 1365 |
return spTree.removeTaxon(taxon); |
| 1366 |
} |
| 1367 |
|
| 1368 |
public void setTaxonId(int taxonIndex, String id) { |
| 1369 |
spTree.setTaxonId(taxonIndex, id); |
| 1370 |
} |
| 1371 |
|
| 1372 |
public void setTaxonAttribute(int taxonIndex, String name, Object value) { |
| 1373 |
spTree.setTaxonAttribute(taxonIndex, name, value); |
| 1374 |
} |
| 1375 |
|
| 1376 |
public String getId() { |
| 1377 |
return spTree.getId(); |
| 1378 |
} |
| 1379 |
|
| 1380 |
public void setId(String id) { |
| 1381 |
spTree.setId(id); |
| 1382 |
} |
| 1383 |
|
| 1384 |
public void setAttribute(String name, Object value) { |
| 1385 |
spTree.setAttribute(name, value); |
| 1386 |
} |
| 1387 |
|
| 1388 |
public Object getAttribute(String name) { |
| 1389 |
return spTree.getAttribute(name); |
| 1390 |
} |
| 1391 |
|
| 1392 |
public Iterator<String> getAttributeNames() { |
| 1393 |
return spTree.getAttributeNames(); |
| 1394 |
} |
| 1395 |
|
| 1396 |
public void addMutableTreeListener(MutableTreeListener listener) { |
| 1397 |
spTree.addMutableTreeListener(listener); |
| 1398 |
} |
| 1399 |
|
| 1400 |
public void addMutableTaxonListListener(MutableTaxonListListener listener) { |
| 1401 |
spTree.addMutableTaxonListListener(listener); |
| 1402 |
} |
| 1403 |
|
| 1404 |
public static Parameter createCoalPointsPopParameter(SpeciesBindings spb, Double value, Boolean bmp) { |
| 1405 |
int dim = 0; |
| 1406 |
for (double[] d : spb.getPopTimesSingle()) { |
| 1407 |
dim += d.length; |
| 1408 |
} |
| 1409 |
|
| 1410 |
if (!bmp) { |
| 1411 |
for (double[] d : spb.getPopTimesPair()) { |
| 1412 |
dim += d.length; |
| 1413 |
} |
| 1414 |
} |
| 1415 |
|
| 1416 |
return new Parameter.Default(dim, value); |
| 1417 |
} |
| 1418 |
|
| 1419 |
public static Parameter createSplitPopulationsParameter(SpeciesBindings spb, double value, boolean root, boolean constPop) { |
| 1420 |
int dim; |
| 1421 |
if (constPop) { |
| 1422 |
|
| 1423 |
dim = 2 * spb.nSpecies() - 1; |
| 1424 |
} else { |
| 1425 |
|
| 1426 |
dim = 3 * spb.nSpecies() - 2 + (root ? 1 : 0); |
| 1427 |
} |
| 1428 |
return new Parameter.Default(dim, value); |
| 1429 |
} |
| 1430 |
|
| 1431 |
@Override |
| 1432 |
public Citation.Category getCategory() { |
| 1433 |
return Citation.Category.SPECIES_MODELS; |
| 1434 |
} |
| 1435 |
|
| 1436 |
@Override |
| 1437 |
public String getDescription() { |
| 1438 |
return "StarBEAST multi-locus species tree inference"; |
| 1439 |
} |
| 1440 |
|
| 1441 |
@Override |
| 1442 |
public List<Citation> getCitations() { |
| 1443 |
return Collections.singletonList(new Citation( |
| 1444 |
new Author[]{ |
| 1445 |
new Author("J", "Heled"), |
| 1446 |
new Author("AJ", "Drummond"), |
| 1447 |
}, |
| 1448 |
"Bayesian Inference of Species Trees from Multilocus Data", |
| 1449 |
2010, |
| 1450 |
"Mol Biol Evol", |
| 1451 |
27, 570, 580, |
| 1452 |
"10.1093/molbev/msp274" |
| 1453 |
)); |
| 1454 |
} |
| 1455 |
|
| 1456 |
} |