| 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.tree.MutableTree; |
| 29 |
import dr.evolution.tree.NodeRef; |
| 30 |
import dr.evolution.tree.Tree; |
| 31 |
import dr.evolution.util.Taxon; |
| 32 |
import dr.evomodel.tree.TreeModel; |
| 33 |
import dr.evomodel.alloppnet.parsers.MulSpeciesBindingsParser; |
| 34 |
import dr.inference.loggers.LogColumn; |
| 35 |
import dr.inference.loggers.Loggable; |
| 36 |
import dr.inference.model.*; |
| 37 |
import dr.math.MathUtils; |
| 38 |
import dr.util.HeapSort; |
| 39 |
import jebl.util.FixedBitSet; |
| 40 |
|
| 41 |
import java.util.Arrays; |
| 42 |
import java.util.HashMap; |
| 43 |
import java.util.HashSet; |
| 44 |
import java.util.Map; |
| 45 |
import java.util.Set; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
public class MulSpeciesBindings extends AbstractModel implements Loggable { |
| 60 |
|
| 61 |
private final GeneTreeInfo[] geneTreeInfos; |
| 62 |
|
| 63 |
|
| 64 |
private final AlloppSpeciesBindings.ApSpInfo[] apspecies; |
| 65 |
private final Taxon[] taxa; |
| 66 |
private final Map<Taxon, Integer> taxon2index = new HashMap<Taxon, Integer>(); |
| 67 |
private final int spsq[][]; |
| 68 |
private final int numberOfSpSeqs; |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
private final double[][] popTimesPair; |
| 73 |
private boolean dirty_pp; |
| 74 |
|
| 75 |
private final double[][] popTimesSingle; |
| 76 |
private boolean dirty_sg; |
| 77 |
private final boolean verbose = false; |
| 78 |
|
| 79 |
|
| 80 |
private class SpeciesIndivPair { |
| 81 |
public int spIndex; |
| 82 |
public int ivIndex; |
| 83 |
|
| 84 |
public SpeciesIndivPair(int spIndex, int ivIndex) { |
| 85 |
this.spIndex = spIndex; |
| 86 |
this.ivIndex = ivIndex; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
public MulSpeciesBindings(AlloppSpeciesBindings.ApSpInfo[] apspecies, TreeModel[] geneTrees, double[] popFactors) { |
| 93 |
super(MulSpeciesBindingsParser.MUL_SPECIES); |
| 94 |
|
| 95 |
this.apspecies = apspecies; |
| 96 |
|
| 97 |
|
| 98 |
int n = 0; |
| 99 |
for (AlloppSpeciesBindings.ApSpInfo apspi : apspecies) { |
| 100 |
n += apspi.individuals.length; |
| 101 |
} |
| 102 |
AlloppSpeciesBindings.Individual [] indivs = new AlloppSpeciesBindings.Individual[n]; |
| 103 |
n = 0; |
| 104 |
for (AlloppSpeciesBindings.ApSpInfo apspi : apspecies) { |
| 105 |
for (int i = 0; i < apspi.individuals.length; i++, n++) { |
| 106 |
indivs[n] = apspi.individuals[i]; |
| 107 |
} |
| 108 |
} |
| 109 |
int t = 0; |
| 110 |
for (AlloppSpeciesBindings.Individual indiv : indivs) { |
| 111 |
t += indiv.taxa.length; |
| 112 |
} |
| 113 |
taxa = new Taxon[t]; |
| 114 |
t = 0; |
| 115 |
for (AlloppSpeciesBindings.Individual indiv : indivs) { |
| 116 |
for (int j = 0; j < indiv.taxa.length; j++, t++) { |
| 117 |
taxa[t] = indiv.taxa[j]; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
for (int i = 0; i < taxa.length; i++) { |
| 122 |
taxon2index.put(taxa[i], i); |
| 123 |
} |
| 124 |
spsq = new int[apspecies.length][]; |
| 125 |
int spsqindex = 0; |
| 126 |
for (int sp = 0; sp < apspecies.length; sp++) { |
| 127 |
spsq[sp] = new int[apspecies[sp].ploidylevel/2]; |
| 128 |
for (int seq = 0; seq < spsq[sp].length; seq++, spsqindex++) { |
| 129 |
spsq[sp][seq] = spsqindex; |
| 130 |
} |
| 131 |
} |
| 132 |
numberOfSpSeqs = spsqindex; |
| 133 |
|
| 134 |
geneTreeInfos = new GeneTreeInfo[geneTrees.length]; |
| 135 |
for (int i = 0; i < geneTrees.length; i++) { |
| 136 |
final TreeModel gtm = geneTrees[i]; |
| 137 |
addModel(gtm); |
| 138 |
geneTreeInfos[i] = new GeneTreeInfo(gtm, popFactors[i]); |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
popTimesSingle = new double[numberOfSpSeqs][]; |
| 143 |
for (int ns = 0; ns < popTimesSingle.length; ++ns) { |
| 144 |
popTimesSingle[ns] = new double[allCoalPointsCount(ns)]; |
| 145 |
} |
| 146 |
dirty_sg = true; |
| 147 |
|
| 148 |
popTimesPair = new double[(numberOfSpSeqs * (numberOfSpSeqs - 1)) / 2][]; |
| 149 |
{ |
| 150 |
final int nps = allPairCoalPointsCount(); |
| 151 |
for (int ns = 0; ns < popTimesPair.length; ++ns) { |
| 152 |
popTimesPair[ns] = new double[nps]; |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
dirty_pp = true; |
| 157 |
|
| 158 |
addStatistic(new SpeciesLimits()); |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
public int numberOfGeneTrees() { |
| 168 |
return geneTreeInfos.length; |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
public int nSpSeqs() { |
| 174 |
return numberOfSpSeqs; |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
public String apspeciesName(int i) { |
| 179 |
return apspecies[i].name; |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
public int spseqindex2sp(int spsqindex) { |
| 185 |
return spseqindex2spandseq(spsqindex)[0]; |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
public int spseqindex2seq(int spsqindex) { |
| 190 |
return spseqindex2spandseq(spsqindex)[1]; |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
public SpeciesIndivPair apspeciesId2speciesindiv(String apspId) { |
| 197 |
int sp = -1; |
| 198 |
int iv = -1; |
| 199 |
for (int s = 0; s < apspecies.length; s++) { |
| 200 |
for (int i = 0; i < apspecies[s].individuals.length; i++) { |
| 201 |
for (int t = 0; t < apspecies[s].individuals[i].taxa.length; t++) { |
| 202 |
Taxon taxon = apspecies[s].individuals[i].taxa[t]; |
| 203 |
if (taxon.getId().compareTo(apspId) == 0) { |
| 204 |
sp = s; |
| 205 |
iv = i; |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
} |
| 210 |
assert sp != -1; |
| 211 |
SpeciesIndivPair x = new SpeciesIndivPair(sp, iv); |
| 212 |
|
| 213 |
return x; |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
public void permuteOneSpeciesOneIndivForOneGene() { |
| 218 |
int i = MathUtils.nextInt(geneTreeInfos.length); |
| 219 |
geneTreeInfos[i].permuteOneSpeciesOneIndiv(); |
| 220 |
geneTreeInfos[i].wasChanged(); |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
public void permuteSetOfIndivsForOneGene() { |
| 226 |
int i = MathUtils.nextInt(geneTreeInfos.length); |
| 227 |
geneTreeInfos[i].permuteSetOfIndivs(); |
| 228 |
geneTreeInfos[i].wasChanged(); |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
public String seqassignsAsText(int g) { |
| 234 |
return geneTreeInfos[g].seqassignsAsText(); |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
public String genetreeAsText(int g) { |
| 239 |
return geneTreeInfos[g].genetreeAsText(); |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
public double[][] getPopTimesSingle() { |
| 252 |
if (dirty_sg) { |
| 253 |
for (int ns = 0; ns < popTimesSingle.length; ++ns) { |
| 254 |
getAllCoalPoints(ns, popTimesSingle[ns]); |
| 255 |
} |
| 256 |
dirty_sg = false; |
| 257 |
} |
| 258 |
return popTimesSingle; |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
public double[][] getPopTimesPair() { |
| 263 |
if (dirty_pp) { |
| 264 |
final int nsp = nSpSeqs(); |
| 265 |
for (int ns1 = 0; ns1 < nsp - 1; ++ns1) { |
| 266 |
final int z = (ns1 * (2 * nsp - ns1 - 3)) / 2 - 1; |
| 267 |
|
| 268 |
for (int ns2 = ns1 + 1; ns2 < nsp; ++ns2) { |
| 269 |
getAllPairCoalPoints(ns1, ns2, popTimesPair[z + ns2]); |
| 270 |
} |
| 271 |
} |
| 272 |
} |
| 273 |
return popTimesPair; |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
private void getAllPairCoalPoints(int ns1, int ns2, double[] popTimes) { |
| 279 |
|
| 280 |
for (int i = 0; i < geneTreeInfos.length; i++) { |
| 281 |
for (CoalInfo ci : geneTreeInfos[i].getCoalInfo()) { |
| 282 |
if ((ci.sinfo[0].contains(ns1) && ci.sinfo[1].contains(ns2)) || |
| 283 |
(ci.sinfo[1].contains(ns1) && ci.sinfo[0].contains(ns2))) { |
| 284 |
popTimes[i] = ci.ctime; |
| 285 |
break; |
| 286 |
} |
| 287 |
} |
| 288 |
} |
| 289 |
HeapSort.sort(popTimes); |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
private int allCoalPointsCount(int spseqIndex) { |
| 295 |
int tot = 0; |
| 296 |
for (GeneTreeInfo t : geneTreeInfos) { |
| 297 |
if (t.nLineages(spseqIndex) > 0) { |
| 298 |
tot += t.nLineages(spseqIndex) - 1; |
| 299 |
} |
| 300 |
} |
| 301 |
return tot; |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
void getAllCoalPoints(int spseqIndex, double[] points) { |
| 308 |
|
| 309 |
int k = 0; |
| 310 |
for (GeneTreeInfo t : geneTreeInfos) { |
| 311 |
final int totCoalEvents = t.nLineages(spseqIndex) - 1; |
| 312 |
int savek = k; |
| 313 |
for (CoalInfo ci : t.getCoalInfo()) { |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
if (ci.allHas(spseqIndex)) { |
| 318 |
points[k] = ci.ctime; |
| 319 |
++k; |
| 320 |
} |
| 321 |
} |
| 322 |
if (!(totCoalEvents >= 0 && savek + totCoalEvents == k) || (totCoalEvents < 0 && savek == k)) { |
| 323 |
System.err.println(totCoalEvents); |
| 324 |
} |
| 325 |
assert (totCoalEvents >= 0 && savek + totCoalEvents == k) || (totCoalEvents < 0 && savek == k); |
| 326 |
} |
| 327 |
assert k == points.length; |
| 328 |
HeapSort.sort(points); |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
private int allPairCoalPointsCount() { |
| 334 |
return geneTreeInfos.length; |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
public double speciationUpperBound(FixedBitSet sub1, FixedBitSet sub2) { |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
double bound = Double.MAX_VALUE; |
| 344 |
for (GeneTreeInfo g : getGeneTrees()) { |
| 345 |
for (CoalInfo ci : g.getCoalInfo()) { |
| 346 |
|
| 347 |
if (ci.ctime >= bound) { |
| 348 |
break; |
| 349 |
} |
| 350 |
if ((ci.sinfo[0].intersectCardinality(sub1) > 0 && ci.sinfo[1].intersectCardinality(sub2) > 0) |
| 351 |
|| |
| 352 |
(ci.sinfo[0].intersectCardinality(sub2) > 0 && ci.sinfo[1].intersectCardinality(sub1) > 0)) { |
| 353 |
bound = ci.ctime; |
| 354 |
break; |
| 355 |
} |
| 356 |
} |
| 357 |
} |
| 358 |
return bound; |
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
public void makeCompatible(double rootHeight) { |
| 364 |
for( GeneTreeInfo t : getGeneTrees() ) { |
| 365 |
|
| 366 |
MutableTree tree = t.tree; |
| 367 |
|
| 368 |
for (int i = 0; i < tree.getExternalNodeCount(); i++) { |
| 369 |
final NodeRef node = tree.getExternalNode(i); |
| 370 |
final NodeRef p = tree.getParent(node); |
| 371 |
tree.setNodeHeight(p, rootHeight + tree.getNodeHeight(p)); |
| 372 |
} |
| 373 |
MutableTree.Utils.correctHeightsForTips(tree); |
| 374 |
|
| 375 |
t.wasChanged(); |
| 376 |
t.getCoalInfo(); |
| 377 |
t.wasBacked = false; |
| 378 |
|
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
class CoalInfo implements Comparable<CoalInfo> { |
| 385 |
|
| 386 |
final double ctime; |
| 387 |
|
| 388 |
final FixedBitSet[] sinfo; |
| 389 |
|
| 390 |
CoalInfo(double t, int nc) { |
| 391 |
ctime = t; |
| 392 |
sinfo = new FixedBitSet[nc]; |
| 393 |
} |
| 394 |
|
| 395 |
public int compareTo(CoalInfo o) { |
| 396 |
return o.ctime < ctime ? +1 : (o.ctime > ctime ? -1 : 0); |
| 397 |
} |
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
public boolean allHas(int s) { |
| 404 |
for (FixedBitSet b : sinfo) { |
| 405 |
if (!b.contains(s)) { |
| 406 |
return false; |
| 407 |
} |
| 408 |
} |
| 409 |
return true; |
| 410 |
} |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
private int collectCoalInfo(Tree tree, NodeRef node, |
| 425 |
GeneTreeInfo.SequenceAssignment[] seqassigns, int loc, CoalInfo[] info) { |
| 426 |
|
| 427 |
info[loc] = new CoalInfo(tree.getNodeHeight(node), tree.getChildCount(node)); |
| 428 |
|
| 429 |
int newLoc = loc - 1; |
| 430 |
for (int i = 0; i < 2; i++) { |
| 431 |
NodeRef child = tree.getChild(node, i); |
| 432 |
info[loc].sinfo[i] = new FixedBitSet(nSpSeqs()); |
| 433 |
|
| 434 |
if (tree.isExternal(child)) { |
| 435 |
Taxon taxon = tree.getNodeTaxon(child); |
| 436 |
int ti = taxon2index.get(taxon); |
| 437 |
int spseq = spsq[seqassigns[ti].spIndex][seqassigns[ti].seqIndex]; |
| 438 |
info[loc].sinfo[i].set(spseq); |
| 439 |
|
| 440 |
assert tree.getNodeHeight(child) == 0; |
| 441 |
} else { |
| 442 |
final int used = collectCoalInfo(tree, child, seqassigns, newLoc, info); |
| 443 |
for (int j = 0; j < info[newLoc].sinfo.length; ++j) { |
| 444 |
info[loc].sinfo[i].union(info[newLoc].sinfo[j]); |
| 445 |
} |
| 446 |
newLoc = used; |
| 447 |
} |
| 448 |
} |
| 449 |
return newLoc; |
| 450 |
} |
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
public class GeneTreeInfo { |
| 455 |
public final TreeModel tree; |
| 456 |
private SequenceAssignment seqassigns[]; |
| 457 |
private SequenceAssignment oldseqassigns[]; |
| 458 |
private final int[] lineagesCount; |
| 459 |
private CoalInfo[] cList; |
| 460 |
private CoalInfo[] savedcList; |
| 461 |
private boolean dirty; |
| 462 |
private boolean wasBacked; |
| 463 |
private final double popFactor; |
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
private class SequenceAssignment { |
| 480 |
public int spIndex; |
| 481 |
public int seqIndex; |
| 482 |
|
| 483 |
public SequenceAssignment(int spIndex, int seqIndex) { |
| 484 |
this.spIndex = spIndex; |
| 485 |
this.seqIndex = seqIndex; |
| 486 |
} |
| 487 |
|
| 488 |
public String toString() { |
| 489 |
String s = "" + seqIndex; |
| 490 |
return s; |
| 491 |
} |
| 492 |
} |
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
GeneTreeInfo(TreeModel tree, double popFactor) { |
| 498 |
this.tree = tree; |
| 499 |
this.popFactor = popFactor; |
| 500 |
seqassigns = new SequenceAssignment[taxa.length]; |
| 501 |
oldseqassigns = new SequenceAssignment[taxa.length]; |
| 502 |
|
| 503 |
|
| 504 |
for (int s = 0; s < apspecies.length; s++) { |
| 505 |
for (int i = 0; i < apspecies[s].individuals.length; i++) { |
| 506 |
int nseqs = apspecies[s].individuals[i].taxa.length; |
| 507 |
int asgns[] = new int [nseqs]; |
| 508 |
for (int x = 0; x < nseqs; x++) { |
| 509 |
asgns[x] = x; |
| 510 |
} |
| 511 |
MathUtils.permute(asgns); |
| 512 |
for (int x = 0; x < nseqs; x++) { |
| 513 |
int t = taxon2index.get(apspecies[s].individuals[i].taxa[x]); |
| 514 |
seqassigns[t] = new SequenceAssignment(s, asgns[x]); |
| 515 |
oldseqassigns[t] = new SequenceAssignment(s, asgns[x]); |
| 516 |
} |
| 517 |
} |
| 518 |
} |
| 519 |
lineagesCount = new int[nSpSeqs()]; |
| 520 |
Arrays.fill(lineagesCount, 0); |
| 521 |
|
| 522 |
for (int nl = 0; nl < lineagesCount.length; ++nl) { |
| 523 |
int sp = spseqindex2sp(nl); |
| 524 |
for (AlloppSpeciesBindings.Individual indiv : apspecies[sp].individuals) { |
| 525 |
boolean got = false; |
| 526 |
for (Taxon t : indiv.taxa) { |
| 527 |
if (tree.getTaxonIndex(t) >= 0) { |
| 528 |
got = true; |
| 529 |
} |
| 530 |
} |
| 531 |
for (Taxon t : indiv.taxa) { |
| 532 |
assert (tree.getTaxonIndex(t) >= 0) == got; |
| 533 |
} |
| 534 |
assert got; |
| 535 |
if (got) { |
| 536 |
++lineagesCount[nl]; |
| 537 |
} |
| 538 |
} |
| 539 |
} |
| 540 |
|
| 541 |
|
| 542 |
cList = new CoalInfo[tree.getExternalNodeCount() - 1]; |
| 543 |
savedcList = new CoalInfo[cList.length]; |
| 544 |
wasChanged(); |
| 545 |
getCoalInfo(); |
| 546 |
wasBacked = false; |
| 547 |
} |
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
public String seqassignsAsText() { |
| 552 |
String s = "Sequence assignments" + System.getProperty("line.separator"); |
| 553 |
for (int tx = 0; tx < seqassigns.length; tx++) { |
| 554 |
s += taxa[tx]; |
| 555 |
s += ":"; |
| 556 |
s += seqassigns[tx].seqIndex; |
| 557 |
if (tx+1 < seqassigns.length && seqassigns[tx].spIndex != seqassigns[tx+1].spIndex) { |
| 558 |
s += System.getProperty("line.separator"); |
| 559 |
} else { |
| 560 |
s += " "; |
| 561 |
} |
| 562 |
} |
| 563 |
return s; |
| 564 |
} |
| 565 |
|
| 566 |
|
| 567 |
public String genetreeAsText() { |
| 568 |
return tree.getNewick(); |
| 569 |
} |
| 570 |
|
| 571 |
|
| 572 |
public SequenceAssignment getSeqassigns(int tx) { |
| 573 |
return seqassigns[tx]; |
| 574 |
} |
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
public void storeSequenceAssignments() { |
| 579 |
for (int i = 0; i < seqassigns.length; i++) { |
| 580 |
oldseqassigns[i].seqIndex = seqassigns[i].seqIndex; |
| 581 |
} |
| 582 |
} |
| 583 |
|
| 584 |
|
| 585 |
public void restoreSequenceAssignments() { |
| 586 |
for (int i = 0; i < seqassigns.length; i++) { |
| 587 |
seqassigns[i].seqIndex = oldseqassigns[i].seqIndex; |
| 588 |
} |
| 589 |
} |
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
public void permuteOneSpeciesOneIndiv() { |
| 594 |
int sp = MathUtils.nextInt(apspecies.length); |
| 595 |
int iv = MathUtils.nextInt(apspecies[sp].individuals.length); |
| 596 |
permuteOneAssignment(sp, iv); |
| 597 |
} |
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
public void permuteSetOfIndivs() { |
| 619 |
int num = tree.getInternalNodeCount(); |
| 620 |
int i = MathUtils.nextInt(num); |
| 621 |
NodeRef node = tree.getInternalNode(i); |
| 622 |
Set<SpeciesIndivPair> spivs = new HashSet<SpeciesIndivPair>(); |
| 623 |
collectIndivsOfNode(node, spivs); |
| 624 |
for (SpeciesIndivPair spiv : spivs) { |
| 625 |
permuteOneAssignment(spiv.spIndex, spiv.ivIndex); |
| 626 |
} |
| 627 |
} |
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
int nLineages(int spseqIndex) { |
| 632 |
return lineagesCount[spseqIndex]; |
| 633 |
} |
| 634 |
|
| 635 |
|
| 636 |
public CoalInfo[] getCoalInfo() { |
| 637 |
if (dirty) { |
| 638 |
swap(); |
| 639 |
|
| 640 |
collectCoalInfo(tree, tree.getRoot(), seqassigns, cList.length - 1, cList); |
| 641 |
HeapSort.sort(cList); |
| 642 |
dirty = false; |
| 643 |
wasBacked = true; |
| 644 |
} |
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
return cList; |
| 662 |
} |
| 663 |
|
| 664 |
|
| 665 |
private void swap() { |
| 666 |
CoalInfo[] tmp = cList; |
| 667 |
cList = savedcList; |
| 668 |
savedcList = tmp; |
| 669 |
} |
| 670 |
|
| 671 |
|
| 672 |
void wasChanged() { |
| 673 |
dirty = true; |
| 674 |
wasBacked = false; |
| 675 |
} |
| 676 |
|
| 677 |
|
| 678 |
boolean restore() { |
| 679 |
if (verbose) System.out.println(" SP binding: restore " + tree.getId() + " (" + wasBacked + ")"); |
| 680 |
if (wasBacked) { |
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
swap(); |
| 692 |
wasBacked = false; |
| 693 |
dirty = false; |
| 694 |
return true; |
| 695 |
} |
| 696 |
return false; |
| 697 |
} |
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
void accept() { |
| 702 |
if (verbose) System.out.println(" SP binding: accept " + tree.getId()); |
| 703 |
|
| 704 |
wasBacked = false; |
| 705 |
} |
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
public double popFactor() { |
| 710 |
return popFactor; |
| 711 |
} |
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
private void collectIndivsOfNode(NodeRef node, Set<SpeciesIndivPair> spivs) { |
| 716 |
if (tree.isExternal(node)) { |
| 717 |
SpeciesIndivPair x = apspeciesId2speciesindiv(tree.getNodeTaxon(node).getId()); |
| 718 |
spivs.add(x); |
| 719 |
} else { |
| 720 |
collectIndivsOfNode(tree.getChild(node, 0), spivs); |
| 721 |
collectIndivsOfNode(tree.getChild(node, 1), spivs); |
| 722 |
} |
| 723 |
} |
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
private void permuteOneAssignment(int sp, int iv) { |
| 728 |
|
| 729 |
int tx; |
| 730 |
if (apspecies[sp].individuals[iv].taxa.length == 2) { |
| 731 |
tx = taxon2index.get(apspecies[sp].individuals[iv].taxa[0]); |
| 732 |
seqassigns[tx].seqIndex = 1 - seqassigns[tx].seqIndex; |
| 733 |
tx = taxon2index.get(apspecies[sp].individuals[iv].taxa[1]); |
| 734 |
seqassigns[tx].seqIndex = 1 - seqassigns[tx].seqIndex; |
| 735 |
} |
| 736 |
} |
| 737 |
|
| 738 |
} |
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
public GeneTreeInfo[] getGeneTrees() { |
| 743 |
return geneTreeInfos; |
| 744 |
} |
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
protected void handleModelChangedEvent(Model model, Object object, int index) { |
| 749 |
if (verbose) System.out.println(" SP binding: model changed " + model.getId()); |
| 750 |
|
| 751 |
dirty_sg = true; |
| 752 |
dirty_pp = true; |
| 753 |
|
| 754 |
for (GeneTreeInfo g : geneTreeInfos) { |
| 755 |
if (g.tree == model) { |
| 756 |
g.wasChanged(); |
| 757 |
break; |
| 758 |
} |
| 759 |
} |
| 760 |
fireModelChanged(object, index); |
| 761 |
} |
| 762 |
|
| 763 |
|
| 764 |
protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) { |
| 765 |
assert false; |
| 766 |
} |
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
protected void storeState() { |
| 771 |
for (GeneTreeInfo gti : geneTreeInfos) { |
| 772 |
gti.storeSequenceAssignments(); |
| 773 |
} |
| 774 |
if (MulSpeciesTreeModel.DBUGTUNE) |
| 775 |
System.err.println("MulSpeciesBindings.storeState()"); |
| 776 |
} |
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
protected void restoreState() { |
| 783 |
for (GeneTreeInfo gti : geneTreeInfos) { |
| 784 |
gti.restoreSequenceAssignments(); |
| 785 |
} |
| 786 |
for (GeneTreeInfo g : geneTreeInfos) { |
| 787 |
if (g.restore()) { |
| 788 |
dirty_sg = true; |
| 789 |
dirty_pp = true; |
| 790 |
} |
| 791 |
} |
| 792 |
if (MulSpeciesTreeModel.DBUGTUNE) |
| 793 |
System.err.println("MulSpeciesBindings.restoreState()"); |
| 794 |
} |
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
protected void acceptState() { |
| 799 |
for (GeneTreeInfo g : geneTreeInfos) { |
| 800 |
g.accept(); |
| 801 |
} |
| 802 |
} |
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
public LogColumn[] getColumns() { |
| 807 |
int ncols = geneTreeInfos.length * taxa.length; |
| 808 |
LogColumn[] columns = new LogColumn[ncols]; |
| 809 |
for (int g = 0, i = 0; g < geneTreeInfos.length; g++) { |
| 810 |
for (int tx = 0; tx < taxa.length; tx++, i++) { |
| 811 |
GeneTreeInfo.SequenceAssignment sqa = geneTreeInfos[g].getSeqassigns(tx); |
| 812 |
String header = "Gene" + g + "taxon" + tx; |
| 813 |
columns[i] = new LogColumn.Default(header, sqa); |
| 814 |
} |
| 815 |
|
| 816 |
} |
| 817 |
|
| 818 |
return columns; |
| 819 |
} |
| 820 |
|
| 821 |
|
| 822 |
public class SpeciesLimits extends Statistic.Abstract { |
| 823 |
int nDim; |
| 824 |
int c[][]; |
| 825 |
|
| 826 |
SpeciesLimits() { |
| 827 |
super("SpeciationBounds"); |
| 828 |
|
| 829 |
nDim = 0; |
| 830 |
|
| 831 |
final int nsp = nSpSeqs(); |
| 832 |
|
| 833 |
c = new int[nsp + 1][nsp + 1]; |
| 834 |
for(int k = 0; k < nsp + 1; ++k) { |
| 835 |
c[k][0] = 1; |
| 836 |
c[k][k] = 1; |
| 837 |
} |
| 838 |
for(int k = 0; k < nsp + 1; ++k) { |
| 839 |
for(int j = 1; j < k; ++j) { |
| 840 |
c[k][j] = c[k - 1][j - 1] + c[k - 1][j]; |
| 841 |
} |
| 842 |
} |
| 843 |
|
| 844 |
for(int k = 0; k <= (int) (nsp / 2); ++k) { |
| 845 |
nDim += c[nsp][k]; |
| 846 |
} |
| 847 |
|
| 848 |
} |
| 849 |
|
| 850 |
|
| 851 |
public int getDimension() { |
| 852 |
return nDim; |
| 853 |
} |
| 854 |
|
| 855 |
|
| 856 |
private double boundOnRoot() { |
| 857 |
double bound = Double.MAX_VALUE; |
| 858 |
final int nsp = nSpSeqs(); |
| 859 |
for(GeneTreeInfo g : getGeneTrees()) { |
| 860 |
for(CoalInfo ci : g.getCoalInfo()) { |
| 861 |
if( ci.sinfo[0].cardinality() == nsp || ci.sinfo[1].cardinality() == nsp ) { |
| 862 |
bound = Math.min(bound, ci.ctime); |
| 863 |
break; |
| 864 |
} |
| 865 |
} |
| 866 |
} |
| 867 |
return bound; |
| 868 |
} |
| 869 |
|
| 870 |
|
| 871 |
public double getStatisticValue(int dim) { |
| 872 |
if( dim == 0 ) { |
| 873 |
return boundOnRoot(); |
| 874 |
} |
| 875 |
|
| 876 |
final int nsp = nSpSeqs(); |
| 877 |
int r = 0; |
| 878 |
int k; |
| 879 |
for(k = 0; k <= (int) (nsp / 2); ++k) { |
| 880 |
final int i = c[nsp][k]; |
| 881 |
if( dim < r + i ) { |
| 882 |
break; |
| 883 |
} |
| 884 |
r += i; |
| 885 |
} |
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
int n = dim - r; |
| 891 |
FixedBitSet in = new FixedBitSet(nsp), |
| 892 |
out = new FixedBitSet(nsp); |
| 893 |
int fr = nsp; |
| 894 |
for(int i = 0; i < nsp; ++i) { |
| 895 |
if( k == 0 ) { |
| 896 |
out.set(i); |
| 897 |
} else { |
| 898 |
if( n < c[fr - 1][k - 1] ) { |
| 899 |
in.set(i); |
| 900 |
k -= 1; |
| 901 |
} else { |
| 902 |
out.set(i); |
| 903 |
n -= c[fr - 1][k]; |
| 904 |
} |
| 905 |
fr -= 1; |
| 906 |
} |
| 907 |
} |
| 908 |
return speciationUpperBound(in, out); |
| 909 |
} |
| 910 |
} |
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
private int[] spseqindex2spandseq(int spsqindex) { |
| 916 |
int indexp = -1; |
| 917 |
int indexq = -1; |
| 918 |
for (int p = 0; p < spsq.length; p++) { |
| 919 |
for (int q = 0; q < spsq[p].length; q++) { |
| 920 |
if (spsq[p][q] == spsqindex) { |
| 921 |
assert indexp == -1; |
| 922 |
assert indexq == -1; |
| 923 |
indexp = p; |
| 924 |
indexq = q; |
| 925 |
} |
| 926 |
} |
| 927 |
} |
| 928 |
assert indexp != -1; |
| 929 |
assert indexq != -1; |
| 930 |
int[] pq = new int[2]; |
| 931 |
pq[0] = indexp; |
| 932 |
pq[1] = indexq; |
| 933 |
return pq; |
| 934 |
} |
| 935 |
|
| 936 |
} |