You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Keeping specific unary nodes though simplify #1113
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.
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.
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.
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.
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?
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?
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?
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?
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).
Activity
jeromekelleher commentedon Jan 13, 2021
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.
jeromekelleher commentedon Jan 13, 2021
cc @petrelharp, @bhaller, @molpopgen
hyanwong commentedon Jan 13, 2021
Yes, that's a good point. I agree with that.
That sounds good to me.
bhaller commentedon Jan 13, 2021
Seems good to me, and easy enough to expose as a pref in SLiM.
petrelharp commentedon Jan 13, 2021
We can already do this, with
keep_unary? Oh, you mean only those nodes with individuals (and associated individuals)?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.
jeromekelleher commentedon Jan 13, 2021
The
TSK_KEEP_ANCESTRAL_INDIVIDUALSflag 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?
jeromekelleher commentedon Jan 13, 2021
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.
petrelharp commentedon Jan 13, 2021
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?
hyanwong commentedon Jan 13, 2021
And if they are on the ancestry.
hyanwong commentedon Jan 16, 2021
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 havesimplify(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 tosimplify(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?
hyanwong commentedon Jan 16, 2021
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?molpopgen commentedon Jan 16, 2021
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.
jeromekelleher commentedon Jan 18, 2021
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_unarymore 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.
hyanwong commentedon Jan 18, 2021
Yep, doing this now
Should it be singular or plural:
TSK_KEEP_UNARY_IN_INDIVIDUALSorTSK_KEEP_UNARY_IN_INDIVIDUAL?jeromekelleher commentedon Jan 18, 2021
Plural, I'd say, unless this breaks some existing conventions.