Skip to content

Keeping specific unary nodes though simplify #1113

Closed

Description

@hyanwong

There are some cases where, during simplify, we might want to leave specific unary nodes in the TS, as long as they are on the lineage to a sample node. At the moment, we can only retain unary nodes by a. specifying keep_unary=T in which case all unary nodes are kept but risk being lost on the next simplify or b. marking the node as a "sample node" but then the node is always retained even if it is not on the lineage from another sample node. The specific example I have encountered with @mmosborn is where a "census" is performed at a certain time, and the nodes at that time, and the individuals associated with them, need to be retained as long as they are in the ancestry. This allows us to get information about individuals and nodes in the past (for example, the spatial location of the individual) even though the nodes aren't strictly "samples" because we don't think we have full genomic information about them.

@jeromekelleher suggests that a way to do this is via an option to simplify which says "keep unary nodes associated with an individual, unless they aren't on a path from a sample". I find that a little contorted, but my suggestion of flagging up the required unary nodes using a flag like NODE_IS_SAMPLE has other problems because it associates a permanent state with the node in question. Other ideas welcome below.

Activity

  1. jeromekelleher commented on Jan 13, 2021

    @jeromekelleher
    Member

    I think being able to keep all individuals that are ancestral to a sample is an important application for forward simulations, and a flag to simplify which enables this would be very useful. It may turn out that this is too many individuals and you only actually want to keep those that you census at specific times, but we should do the simple thing first.

    How about TSK_KEEP_ANCESTRAL_INDIVIDUALS? The implementation is easy enough I think, we just keep any unary nodes that have non-null individual references. The rest sorts itself out.

  2. jeromekelleher commented on Jan 13, 2021

    @jeromekelleher
    Member
  3. hyanwong commented on Jan 13, 2021

    @hyanwong
    MemberAuthor

    I think being able to keep all individuals that are ancestral to a sample is an important application for forward simulations, and a flag to simplify which enables this would be very useful. It may turn out that this is too many individuals and you only actually want to keep those that you census at specific times, but we should do the simple thing first.

    Yes, that's a good point. I agree with that.

    How about TSK_KEEP_ANCESTRAL_INDIVIDUALS? The implementation is easy enough I think, we just keep any unary nodes that have non-null individual references. The rest sorts itself out.

    That sounds good to me.

  4. bhaller commented on Jan 13, 2021

    @bhaller

    Seems good to me, and easy enough to expose as a pref in SLiM.

  5. petrelharp commented on Jan 13, 2021

    @petrelharp
    Contributor

    I think being able to keep all individuals that are ancestral to a sample is an important application for forward simulations,

    We can already do this, with keep_unary? Oh, you mean only those nodes with individuals (and associated individuals)?

    we just keep any unary nodes that have non-null individual references.

    Nice idea. This would actually allow in SLiM the censusing that @hyanwong is asking for, since individuals only go in the table if you specifically ask for it; so if you wanted to keep everyone who's ancestral, but only at a particular time, then you'd just add the individuals at that time.

    This strikes me as a bit odd, because we're using presence-of-individuals to convey a certain kind of information, which is right for this use case, but may not generalize to anything else. I'm also not sure if this is going to fix anyone's bottleneck in practice. But I'm happy to see it go through, if others are convinced.

  6. jeromekelleher commented on Jan 13, 2021

    @jeromekelleher
    Member

    The TSK_KEEP_ANCESTRAL_INDIVIDUALS flag seems logical enough though, doesn't it? As in, we keep any individuals that are linked to nodes that are ancestral to the input sample nodes, and we also keep any unary nodes that go with these individuals. I agree this probably will result in keeping a lot of individuals/nodes, but I can also imagine (cool) visualisations where we trace the location of every individual all the way back to the root. There's no way to do this without keeping all ancestral individuals, at least.

    It's surely orders of magnitude less storage than needed to store every individual, right?

  7. jeromekelleher commented on Jan 13, 2021

    @jeromekelleher
    Member

    We can already do this, with keep_unary? Oh, you mean only those nodes with individuals (and associated individuals)?

    Ah, but I guess KEEP_UNARY would already keep all the intermediate nodes and the associated individuals. So, we can already do what I'm talking about.

  8. petrelharp commented on Jan 13, 2021

    @petrelharp
    Contributor

    Right, maybe we're not talking about the same thing. KEEP_UNARY would keep ancestral nodes and associated individuals; I thought you meant to add a flag that would keep ancestral nodes only if they have an individual. Is that what you meant?

  9. hyanwong commented on Jan 13, 2021

    @hyanwong
    MemberAuthor

    I thought you meant to add a flag that would keep ancestral nodes only if they have an individual

    And if they are on the ancestry.

  10. hyanwong commented on Jan 16, 2021

    @hyanwong
    MemberAuthor

    I've been pondering this. I suspect the way to think about it is as more specific version of KEEP_UNARY, because it's only unary nodes that are affected by this. It's a bit like, instead of simplify(keep_unary=True|False), we have simplify(keep_unary="Never"|"Always"|"OnlyIfInIndividual"), right?

    So, assuming we don't actually want to change the parameter type (and I'm sure we don't), it's really it's just a question of finding a name that reflects this. I think TSK_KEEP_ANCESTRAL_INDIVIDUALS is a bit confusing, because it focusses on the individuals and not the unary nodes. For example, if we do simplify(filter_individuals=False, keep_ancestral_individuals=False), it does something different to simplify(filter_individuals=False, keep_ancestral_individuals=True), even though it sounds like they should do identical things. In the first case simplify throws away all unary nodes, whereas in the second it keeps some of them (those in an individual).

    I don't have a brilliant naming idea, though. TSK_KEEP_UNARY_IN_INDIVIDUALS springs to mind, but that doesn't immediately indicate that they are only kept if on the ancestry. OTOH, the normal KEEP_UNARY parameter doesn't say that in the name either, although it's equally true, so perhaps that's fine?

  11. hyanwong commented on Jan 16, 2021

    @hyanwong
    MemberAuthor

    I would very much like to add this to a released version of tskit, so that SLiM can take advantage of it. Are we agreed that it is a useful thing, that we want to do this by an extra option to simplify(), and the only issue is the name?

  12. molpopgen commented on Jan 16, 2021

    @molpopgen
    Member

    I'll admit to not fully following the discussion over at slim, but I think I get the gist of it here. Makes sense to support this.

  13. jeromekelleher commented on Jan 18, 2021

    @jeromekelleher
    Member

    I don't have a brilliant naming idea, though. TSK_KEEP_UNARY_IN_INDIVIDUALS springs to mind, but that doesn't immediately indicate that they are only kept if on the ancestry. OTOH, the normal KEEP_UNARY parameter doesn't say that in the name either, although it's equally true, so perhaps that's fine?

    I think this is a good name for the C flag. We can figure out the right way of calling it from Python later, since we only need this in the C API to support it in SLiM (your idea of making keep_unary more flexible seems good, at first glance, though).

    Do you want to have a stab at at @hyanwong?

    I think TSK_KEEP_UNARY_IN_INDIVIDUALS should be mutually exclusive with KEEP_UNARY.

  14. hyanwong commented on Jan 18, 2021

    @hyanwong
    MemberAuthor

    Do you want to have a stab at at @hyanwong?

    Yep, doing this now

    I think TSK_KEEP_UNARY_IN_INDIVIDUALS should be mutually exclusive with KEEP_UNARY.

    Should it be singular or plural: TSK_KEEP_UNARY_IN_INDIVIDUALS or TSK_KEEP_UNARY_IN_INDIVIDUAL?

  15. jeromekelleher commented on Jan 18, 2021

    @jeromekelleher
    Member

    Plural, I'd say, unless this breaks some existing conventions.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions