Skip to content

sim_ancestry(initial_state=ts) does not use population names #1528

Closed

Description

@petrelharp

Right now, it looks like sim_ancestry(initial_state=ts, demography=d, ...) matches up the populations in the Demography object, in order, to the populations in the initial state. The population table in the output is the same as in the initial state (this is good!). However, there's the possibility for confusion here, because names are not matched. For instance:

t = tskit.TableCollection(sequence_length=1)
t.populations.metadata_schema = tskit.MetadataSchema({ "codec": "json" })
t.populations.add_row(metadata={'name' : 'A'})
t.populations.add_row(metadata={'name' : 'B'})
t.nodes.add_row(time=0, flags=1, population=0)
t.nodes.add_row(time=0, flags=1, population=0)
t.nodes.add_row(time=0, flags=1, population=1)
t.nodes.add_row(time=0, flags=1, population=1)

d = msprime.Demography()
d.add_population(initial_size=0.001, name='B')
d.add_population(initial_size=1e6, name='A')

ts = msprime.sim_ancestry(
        initial_state=t.tree_sequence(),
        demography=d,
        end_time=1)

print(ts.first().draw(format='unicode'))

shows that 0 and 1, the samples in A, have coalesced, and so have been assigned to what we called B in the Demography.

 5  7 6
 ┃  ┃ ┃
 4  ┃ ┃
┏┻┓ ┃ ┃
0 1 2 3

(and, the populations are still labeled A and B, as before).

BTW, if the demography has one more population than the tables, we get this error:

msprime._msprime.InputError: Input error in population configuration: Population ID out of bounds.

and if the tables have one more population than the demography, we get:

ValueError: migration matrix must be a N x N square matrix encoded as a list-of-lists or numpy array, where N is the number of populations defined in the population_configurations. The diagonal elements of this matrix must be zero. For example, a valid matrix for a 3 population system is [[0, 1, 1], [1, 0, 1], [1, 1, 0]]

Activity

  1. jeromekelleher commented on Mar 12, 2021

    @jeromekelleher
    Member

    Hmm, very nice @petrelharp. Let's see.

    What if we had a Demography.from_tree_sequence() that returned a demography reflecting the populations in the tree sequence, which tries to recover as much as it can from the metadata? At a minimum it will have the right number of populations and if names/descriptions exist in the metadata it'll use those too. (No reason we couldn't read initial_size etc as well, but let's leave that for now)

    Then, the way to do things would be

    demography = msprime.Demography.from_tree_sequence(slim_ts)
    # e.g.
    demography["p1"].initial_size = 10000
    demography["p2"].initial_size = 20000
    ts = msprime.sim_ancestry(initial_state=slim_ts, demography=demography)

    Would that be any better?

  2. petrelharp commented on Mar 12, 2021

    @petrelharp
    ContributorAuthor

    I like that idea (it would solve tskit-dev/pyslim#106, in particular). It doesn't totally address this, though - would we then just throw an informative error message if populations don't match?

  3. jeromekelleher commented on Mar 12, 2021

    @jeromekelleher
    Member

    It doesn't totally address this, though - would we then just throw an informative error message if populations don't match?

    Yes, I think we could be stricter then about what we accept as an input demography. I guess what we'd do is

    demography_check = Demography.from_tree_sequence(initial_state)
    if demography_check.num_populations != demography.num_populations:
        raise ValueError("Blah blah")
    for check_pop, pop in zip(demography_check.populations, demography.populations):
        if check_pop.name != pop.name:
               raise ValueError("Incompatible population names")
    

    So, if you follow the pattern above as a user this error should never be thrown (since both are using from_tree_sequence to derive the names), and it should catch errors otherwise.

    What do you think?

  4. jeromekelleher commented on Mar 12, 2021

    @jeromekelleher
    Member

    Gives a nice way to inspect the populations in the ts too.

  5. petrelharp commented on Mar 12, 2021

    @petrelharp
    ContributorAuthor

    Sounds good!

  6. petrelharp commented on Mar 12, 2021

    @petrelharp
    ContributorAuthor

    What will populations not having any lineages in them look like in the result, btw? Can we make it clear that those won't be used?

  7. petrelharp commented on Mar 12, 2021

    @petrelharp
    ContributorAuthor

    Ah: suppose we've got a tree sequence with samples in two populations, but want to simulate them having come from a population split some time in the past. This requires adding another population that isn't in the tree sequence we've passed in, like

    d = Demography.from_tree_sequence(initial_state)
    d.add_population(initial_size=100, name='ancestral')
    

    ... but currently, sim_ancestry(initial_state=ts, demograph=d) won't add populations.

  8. jeromekelleher commented on Mar 12, 2021

    @jeromekelleher
    Member

    Yeah, good point. So, we require that the first initial_state.num_populations pops are equivalent to the ones from initial_state, and we add populations to the tables for any additional populations from the demography.

    That should allow us do stuff like

    demography = msprime.Demography.from_tree_sequence(slim_ts)
    # e.g.
    demography["p1"].initial_size = 10000
    demography["p2"].initial_size = 20000
    demography.add_population(name="ancestor", initial_size=100
    demography.add_population_split(time=1000, derived=["p1", "p2"], ancestral="ancestor")
    ts = msprime.sim_ancestry(initial_state=slim_ts, demography=demography)

    What will populations not having any lineages in them look like in the result, btw? Can we make it clear that those won't be used?

    We won't know that just by inspecting the population table, we'd have to something fancy like create a simulator from the initial state and then inspect it's state. We could then mark those pops that have no lineages initially as "inactive", but it might get complicated.

    Probably not necessary for a first pass though?

  9. petrelharp commented on Mar 12, 2021

    @petrelharp
    ContributorAuthor

    Probably not necessary for a first pass though?

    Totally. Sounds good.

  10. added this to the Release 1.0 milestone on Mar 12, 2021
  11. jeromekelleher commented on Mar 12, 2021

    @jeromekelleher
    Member

    OK, will do this once the current batch of docs updates is done.

  12. added 7 commits that reference this issue on Mar 15, 2021
    14254ca
    f14b173
    16c07ba
    89d00af
    82bb7d3
    3d8c0f8
    91ae2c3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions