Skip to content

Commit

Permalink
do not simulate null ancestry; closes tskit-dev#357
Browse files Browse the repository at this point in the history
  • Loading branch information
petrelharp committed Jan 3, 2025
1 parent 46809d4 commit d9219a3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
9 changes: 9 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@
UPCOMING
***************************

**Bugfixes**:

- Recapitation on tree sequences with null genomes would attempt to simulate
the history of those null genomes; this would in all but exceptional cases
fail with an error ("not all roots are at the time expected"). Now,
`recapitate` removes the "sample" flag from all such null genomes in
sampled individuals. (:user: `petrelharp`, :pr:`358`)



***************************
[1.0.4] - 2023-08-01
Expand Down
25 changes: 24 additions & 1 deletion pyslim/methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,36 @@ def recapitate(ts,
that if neither ``recombination_rate`` or a ``recombination_map`` are
provided, there will be *no* recombination.
.. warning:
If the tree sequence contains (SLiM-specified) "null" genomes, as for
instance in a haploid or haplodiploid simulation, then these genomes
will remain in the tree sequence but no longer be marked as samples,
so loading back into SLiM will not work. If this is a problem, please
open an issue on github.
:param tskit.TreeSequence ts: The tree sequence to transform.
:param float ancestral_Ne: If specified, then will simulate from a single
ancestral population of this size. It is an error to specify this
as well as ``demography``.
:param dict kwargs: Any other arguments to :func:`msprime.sim_ancestry`.
'''
is_current_version(ts, _warn=True)
has_null = False
for n in ts.samples():
if n.metadata['is_null']:
has_null = True
break
if has_null:
# we need to mark NULL genomes to be *not* samples
# so we don't simulate their ancestry
tables = ts.tables
tables.nodes.clear()
for n in ts.nodes():
if n.metadata['is_null']:
n = n.replace(flags=n.flags & ~tskit.NODE_IS_SAMPLE)
tables.nodes.append(n)
ts = tables.tree_sequence()
if ancestral_Ne is not None:
if "demography" in kwargs:
raise ValueError("You cannot specify both `demography` and `ancestral_Ne`.")
Expand All @@ -66,7 +89,7 @@ def recapitate(ts,
"Not all roots of the provided tree sequence are at the time expected "
"by recapitate(). This could happen if you've simplified in "
"python before recapitating (fix: don't simplify first). "
"If could also happen in other situations, e.g., "
"It could also happen in other situations, e.g., "
"you added new individuals without parents in SLiM "
"during the course of the simulation with sim.addSubPop(), "
"in which case you will probably need to recapitate with "
Expand Down

0 comments on commit d9219a3

Please sign in to comment.