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