Skip to content

Add requirement that mutations are sorted by time #651

Closed

Description

@jeromekelleher

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.

Originally posted by @petrelharp in #513 (comment)

Activity

  1. added this to the C API 0.99.3 milestone on May 26, 2020
  2. jeromekelleher commented on May 26, 2020

    @jeromekelleher
    MemberAuthor

    This seems like a sensible thing to do, to me, and we'll regret it if we miss the opportunity to set this constraint while we're adding the field. We just need to think through the consequences.

  3. petrelharp commented on May 27, 2020

    @petrelharp
    Contributor

    Ah, this would close #27 .

  4. benjeffery commented on Jul 17, 2020

    @benjeffery
    Member

    Started looking into this. One question is how we deal with unknowns - based on the "parents before children" requirement we have to include unknown times in a place that is consistent with that requirement. That means doing more than a simple comparison of time, but instead following each mutations parents to see which one is in the parent chain of the other. Am I getting that right?

  5. mufernando commented on Jul 17, 2020

    @mufernando
    Member

    @benjeffery , I started looking into this in #715 (see also issue #705). Could use your help there, I've been busy with some other things this week.

  6. jeromekelleher commented on Jul 17, 2020

    @jeromekelleher
    MemberAuthor

    Started looking into this. One question is how we deal with unknowns - based on the "parents before children" requirement we have to include unknown times in a place that is consistent with that requirement. That means doing more than a simple comparison of time, but instead following each mutations parents to see which one is in the parent chain of the other. Am I getting that right?

    I think to the thing to do with UNKNOWN_TIME is to duplicate the old behaviour. So, the goal would be to keep the code exactly the same semantically if an old tree sequence was read in (which didn't have times), and then only have different behaviour when we do know the times. This should be easy enough to do within the comparison func. The only tricky bit is dealing with how to compare known and unknown times - but I guess the only thing to do there is to treat them both as unknown and not compare the time at all.

  7. benjeffery commented on Jul 17, 2020

    @benjeffery
    Member

    Thanks for pointing that out @mufernando. I think it's best to do each change to the sorting requirements as a separate sub-PR to #705. This prevents PRs and their discussions getting too large. I'll do a PR for this mutation time issue if that's good with you?

  8. mufernando commented on Jul 17, 2020

    @mufernando
    Member

    Sounds good to me! Thanks!

  9. petrelharp commented on Jul 17, 2020

    @petrelharp
    Contributor

    Actually, we realized in #705 that the only thing we needed to do was to sort the mutations, so we can close that one in favor of this one. Let's see, here's the things we have available to sort on (after sorting by site; the discussion is how to order the mutations that all have the same site):

    • time (maybe)
    • node
    • parent mutation id
    • original mutation id (ie order in the table before sorting)

    Observations:

    1. Sorting on (time, parent), with missing times incomparable, would suffice to ensure that things are ordered by time, and that parents come before children even if times are missing, as long as mutation parents are present.
    2. Sorting on (time, original id) would also ensure this, as long as mutation parents came before children in the original table; and what we did before is just maintain original order, so sort on just original id.
    3. Neither of these give you a canonical ordering; so we're proposing in make sort stricter #705 sorting on (time, parent, node) to make it canonical.

    So I guess that here you could sort on (time, original id) to keep it as close to the original as possible. But I think we do want to move to (time, parent, node), so why not do that now?

  10. benjeffery commented on Jul 17, 2020

    @benjeffery
    Member

    @jeromekelleher and I had a chat about this earlier and for basic mutation time sorting settled on your (2) as the simplest option. To be explicit I think cmp_mutation would look like this:

        int ret = (ia->site > ib->site) - (ia->site < ib->site);
        if (ret == 0 && !tsk_is_unknown_time(ia->time) && !tsk_is_unknown_time(ib->time)) {
            ret = (ia->time > ib->time) - (ia->time < ib->time);
        }
        if (ret == 0) {
            ret = (ia->id > ib->id) - (ia->id < ib->id);
        }
        return ret;

    Whereas for your option (3), if I understand correctly would be:

        int ret = (ia->site > ib->site) - (ia->site < ib->site);
        if (ret == 0 && !tsk_is_unknown_time(ia->time) && !tsk_is_unknown_time(ib->time)) {
            ret = (ia->time > ib->time) - (ia->time < ib->time);
        }
        if (ret == 0) {
            ret = (ia->parent > ib->parent) - (ia->parent < ib->parent);
        }
        if (ret == 0) {
            ret = (ia->node > ib->node) - (ia->node < ib->node);
        }
        return ret;

    You can only use parent to sort if the ids are already sorted correctly with respect to parenthood, right? Is that a safe assumption?

    I'm all for doing (3) now if that's what we want to end up with.

  11. mufernando commented on Jul 17, 2020

    @mufernando
    Member

    I would think that is a safe assumption because the current sorting behavior (on site id only) would not work otherwise, no?

  12. petrelharp commented on Jul 17, 2020

    @petrelharp
    Contributor

    That's right, those are options (2) and (3).

    You can only use parent to sort if the ids are already sorted correctly with respect to parenthood, right? Is that a safe assumption?

    Not sure what you mean here? If you have correct parent ids already, then proposal (3) will put them in the correct order, regardless of whether beforehand parents came before children. (you gotta recompute mutation parents after sorting, though!)

    Maybe this is what you mean? If mutations previously had parents coming before children, but did not have mutation parents computed, then (2) would preserve that (and thus compute_mutation_parents would still work), while (3) would break it. I think that's OK, since once we have mutation time, we don't have to rely on this relatively fragile ordering to keep that information. Edit: this is wrong - we still need to rely on it - because times may not be unique.

  13. jeromekelleher commented on Jul 20, 2020

    @jeromekelleher
    MemberAuthor

    It's not clear to me that we do want to canonicalise the ordering of mutations here. Wouldn't it be easier to think about a canonical ordering when we're guaranteed that all the elements of the data model are present and correct? As it is with sorting, we have to be robust to parents being wrong (i.e., the parent is pointing to NULL, but it actually does have a parent). So, perhaps it would be easiest to do option (2) for the regular sorting algorithm, and deal with canonicalisation separately, preconditioned on a full TSK_CHECK_TREES integrity check?

  14. benjeffery commented on Jul 20, 2020

    @benjeffery
    Member

    In working out the tests for option 2 I realised that if you have two out-of-time-order mutations then putting an unknown time between them satisfies the order checking as it just looks at neighbouring mutations. Is this ok as mixed known/unknown is a fringe use case, or do we need to do more intensive checking for "monotonically decreasing known times per site"?

  15. jeromekelleher commented on Jul 20, 2020

    @jeromekelleher
    MemberAuthor

    In working out the tests for option 2 I realised that if you have two out-of-time-order mutations then putting an unknown time between them satisfies the order checking as it just looks at neighbouring mutations. Is this ok as mixed known/unknown is a fringe use case, or do we need to do more intensive checking for "monotonically decreasing known times per site"?

    I guess it's simpler to actually check this. We just need to remember the last_known_time as we go through the mutations for a site, right?

  16. petrelharp commented on Jul 20, 2020

    @petrelharp
    Contributor

    I agree with doing (2) after all: SLiM will want mutations with the same time (and node) to keep their relative order, since we won't have parent, and that's the only way we preserve that information. Sorry - I hadn't thought carefully enough about nonuniqueness of times before.

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