Skip to content

Add MutationTable.time column #513

Closed

Description

@jeromekelleher

Currently we have no way of recording the precise age of a mutation. All that we know is that it occured somewhere on a branch. Most of the time this isn't an issue, but there are situtations in which we need to record this precise time (see, tskit-dev/msprime#946, e.g.).

We can add a MutationTable.time column easily enough. If the mutation time is not specified (the default) then we set it to the time of the mutation's node. If multiple mutations are added on the same branch without times being specified, then they will have the same time (oh well, no big deal I guess).

In terms of the file format, we make this optional in the same way that Edge metadata is optional (it would be simplest to call set_columns to implement this though). It might be best to do #506 first, therefore.

As this is backward compatible, we don't need to increment the file version number.

We will break some C code since mutation_table_add_row et al will need a new parameter, which most naturally comes between the current parent and derived_state. I guess providing TSK_NULL indicates you want the default for this. Since we're already breaking some C code, this seems fine.

@benjeffery, any thoughts here?

Activity

  1. petrelharp commented on Apr 6, 2020

    @petrelharp
    Contributor

    Supporting information that this is a good idea: SLiM already records the time, but in metadata.

  2. molpopgen commented on Apr 6, 2020

    @molpopgen
    Member

    fwdpy11 does the same thing. If you keep tables in the native (fwdpp) format, the mutation's origin time is with respect to time moving forwards. If you dump to the tskit format, then origin times are changed to be pastwards from the last time point simulated.

  3. jeromekelleher commented on Apr 7, 2020

    @jeromekelleher
    MemberAuthor

    I'm reassured we all think this is a good idea!

  4. benjeffery commented on Apr 7, 2020

    @benjeffery
    Member

    Do we know why SLiM includes this in metadata? It seems to be a core enough property of a mutation to warrant inclusion as a column.
    I agree on getting #506 done first and making this backwards compatible. This will be the first "non-trivial" default value so care will need to be taken in documenting the behaviour.

  5. jeromekelleher commented on Apr 7, 2020

    @jeromekelleher
    MemberAuthor

    Do we know why SLiM includes this in metadata? It seems to be a core enough property of a mutation to warrant inclusion as a column.

    It was just because it wasn't implemented in tskit and we decided there were other things to be getting on with, I'd imagine.

  6. added this to the Python 0.2.4 milestone on Apr 24, 2020
  7. petrelharp commented on May 26, 2020

    @petrelharp
    Contributor

    A though: once this is implemented, we should have sort_tables sort mutations by time, which satisfies the current requirement that parents come before children, but would make the ordering produced by sort_tables less ambiguous (not totally, if in discrete time, though). Perhaps we should make "sorted by time" a requirement of the mutation table, also.

  8. benjeffery commented on Jun 3, 2020

    @benjeffery
    Member

    I've started working on this. Above it says that if TSK_NULL is passed as the time to tsk_mutation_table_add_row then it should default to the node time. However I don't see how tsk_mutation_table_add_row has access to the node time as it only has the node id.

  9. benjeffery commented on Jun 3, 2020

    @benjeffery
    Member

    Just had a discussion with @jeromekelleher and @awohns on this issue.

    Any way of loading an old TS that doesn't have the mutation time column seems complex and a kludge. The mutation loading code would need to have the node table and do lots of look ups. It is also not clear how tsk_mutation_table_add_row could provide a default.

    It seems a cleaner path to make mutation time a mandatory column (bumping the major file version). Then tskit upgrade would add mutation times by spreading them equally across each edge in a one-off procedure. Mutation time would be mandatory in add_row. @petrelharp thoughts?

  10. petrelharp commented on Jun 3, 2020

    @petrelharp
    Contributor

    I agree - making mutation time mandatory sounds like the right way to go. Hm: it seems like we'll really want to get msprime and SLiM producing mutation times right away, to avoid a period of awkwardness? This will be easy to add in SLiM, but would require a release to get adoption. I guess what I'm saying is that a lot of people might be encountering this.

    About spreading the times out over the branch - also, agree! At first I was nervous about adding extra fake information, but we're not using mutation times currently, so I think it's good.

    Hm - it occurs to me that some methods - eg tsinfer? - don't have any natural way to assign times along a branch, so it could be useful to allow clients to ignore times in tables (eg leave them all NULL), and provide a method to assign times uniformly along a branch, much like compute_mutation_parents and deduplicate_sites? Since we need to write this method anyhow? (to be clear, I think the time column should make sense for a valid tree sequence, I'm talking about 'just a collection of tables')

    This will be great to get in there for the future "time slice" statistics!

  11. jeromekelleher commented on Jun 3, 2020

    @jeromekelleher
    MemberAuthor

    Hm - it occurs to me that some methods - eg tsinfer? - don't have any natural way to assign times along a branch, so it could be useful to allow clients to ignore times in tables (eg leave them all NULL), and provide a method to assign times uniformly along a branch, much like compute_mutation_parents and deduplicate_sites? Since we need to write this method anyhow? (to be clear, I think the time column should make sense for a valid tree sequence, I'm talking about 'just a collection of tables')

    Great idea @petrelharp, let's do this.

  12. benjeffery commented on Jun 4, 2020

    @benjeffery
    Member

    I've done most of the work to put mutation time through the code. I've also added checks so that for a valid tree sequence times need to be not null, greater or equal to the node time and less than or equal to the parent mutation if there is one. I think I need to add less than or equal to the node time above, right if no parent? That means following the edge which means a lookup in the edge table.

    I'm now working on the method to put times on existing, timeless mutations, which is needed to get existing tests working so has to be in the same PR. I think I need to check integrity (with time checking turned off) before adding times, right? Or for efficiency do we assume a valid tree sequence is passed to these kinds of methods?

  13. 7 remaining items

  14. petrelharp commented on Jun 5, 2020

    @petrelharp
    Contributor

    Oh - ps - is the idea to assign them exactly uniformly, or randomly uniformly? I guess I vote for the latter - more useful, anyhow.

  15. benjeffery commented on Jun 5, 2020

    @benjeffery
    Member

    Currently doing exactly uniformly (i.e. a single mutation gets put on the edge mid-time). But easily adjust.

  16. petrelharp commented on Jun 5, 2020

    @petrelharp
    Contributor

    Well, if it was randomly uniformly and there was a public method to re-compute it, then that would be very useful for averaging over mutation placement on the branches!

  17. jeromekelleher commented on Jun 5, 2020

    @jeromekelleher
    MemberAuthor

    Oh - ps - is the idea to assign them exactly uniformly, or randomly uniformly? I guess I vote for the latter - more useful, anyhow.

    I would be very much in favour of exactly uniformly! Where do we get the randomness from, otherwise? We can't use GSL, libc's RNGs are rubbish, and there's the seeding headache to worry about. This would greatly complicate things.

  18. jeromekelleher commented on Jun 5, 2020

    @jeromekelleher
    MemberAuthor

    Might be worth making a Python version of this and opening a PR @benjeffery? We'll need it for testing anyway, and that way we can feed in concretely.

  19. molpopgen commented on Jun 5, 2020

    @molpopgen
    Member

    Oh - ps - is the idea to assign them exactly uniformly, or randomly uniformly? I guess I vote for the latter - more useful, anyhow.

    I would be very much in favour of exactly uniformly! Where do we get the randomness from, otherwise? We can't use GSL, libc's RNGs are rubbish, and there's the seeding headache to worry about. This would greatly complicate things.

    I guess a callback receiving the two node times and returning a double could allow external libs to generate randomness?

  20. petrelharp commented on Jun 5, 2020

    @petrelharp
    Contributor

    I would be very much in favour of exactly uniformly! Where do we get the randomness from, otherwise? We can't use GSL, libc's RNGs are rubbish, and there's the seeding headache to worry about. This would greatly complicate things.

    Darn, because averaging over uniform random mutation placement is something that I want to do.

  21. petrelharp commented on Jun 5, 2020

    @petrelharp
    Contributor

    Alternatively, if there was just a method to obtain the edge, or parent node time, then this randomization would be easy enough to do in pytohn.

  22. jeromekelleher commented on Jun 5, 2020

    @jeromekelleher
    MemberAuthor

    This is a lot of extra complexity to take on now, and I feel like we'll get bogged down in it. How about this: we add an extra argument called random which if not NULL should be an array of at least num_mutations doubles between 0 and 1. Then, if random is specified we use one of those random values per mutation to put it in the right place. We could work out the details later, but for now only do the non-random case.

    Callbacks etc are a pain, and don't scale when working with Python.

  23. jeromekelleher commented on Jun 5, 2020

    @jeromekelleher
    MemberAuthor

    Alternatively, if there was just a method to obtain the edge, or parent node time, then this randomization would be easy enough to do in pytohn.

    Yes, that's better! If we know the edge then we could do this in Python with the tables API. Probably wouldn't be as fast as the approach above though, if you wanted to do it many times.

  24. petrelharp commented on Jun 5, 2020

    @petrelharp
    Contributor

    Probably wouldn't be as fast as the approach above though, if you wanted to do it many times.

    It'd just be some numpy vector addition/multiplication and a call to set_columns?

  25. jeromekelleher commented on Jun 5, 2020

    @jeromekelleher
    MemberAuthor

    It'd just be some numpy vector addition/multiplication and a call to set_columns?

    It's not that easy if you have multiple mutations on an edge though, is it? You'd end up having to loop over the sites, I'd have thought.

  26. benjeffery commented on Jun 5, 2020

    @benjeffery
    Member

    I like the "passing doubles in" approach. I'll implement that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions