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
Allow keep_unary to be passed to simplification funcs #139
I can imagine that it would be very handy to be able to output in tree sequence all the intermediate nodes produced by SLiM - i.e. not just the coalescence points, but all the "unary nodes" too. This would be somewhat intermediate between "saving everybody" and the normal output. Individuals would still not be saved, of course, unless they happen to be "retained" - this is just about saving the node info (genomes) themselves.
The way I imagine this working would be to add an optional keep_unary arg to treeSeqSimplify(), which could be T, F, or NULL (default). There would also be a keep_unary option to initializeTreeSeq() that would default to F and would set the default keep_unary flag during automatic simplification. This value would be the default used when calling treeSeqSimplify() with no params.
What do you think @petrelharp ? I guess you're the one doing "remember everyone" simulations.
changed the title [-]Allow `keep_unary` to be passed to simplification funds[/-][+]Allow `keep_unary` to be passed to simplification func[/+]on Jan 11, 2021
changed the title [-]Allow `keep_unary` to be passed to simplification func[/-][+]Allow `keep_unary` to be passed to simplification funcs[/+]on Jan 11, 2021
As I mentioned in the review on #25, let's try to envision the ideal final design for this stuff. Is it a keepUnary=T option on initializeTreeSeq() and a permanent=F option on treeSeqRememberIndividuals()? In other words, are these two features completely orthogonal? Or is there some interaction between them, and some better API design? Because keepUnary=T strikes me as a kind of "remembering" or "retaining" too, but we're proposing handling it in a very different way. It could be handled instead as another type of flag passed to treeSeqRememberIndividuals(), right? A flag saying "mark these individuals as being kept even if their nodes are unary"? I don't see a clear problem with what has been proposed, but I want to make sure we're going to arrive at a final design that we won't regret later.
I see them as completely orthogonal, which is why I opened up a new issue. I guess you could think of this as a sort of "remembering", but of nodes not individuals. The distinction is very clear in tree-sequence world, and I think that the "keep_unary" function should automatically play nicely (and logically) with the permanent=F option in #25 . But let's see what @petrelharp thinks too?
Not even so much orthogonal, but copmplementary: if you do keep_unary=T and you Retain all the individuals, then you'll get the individuals corresponding to retained unary nodes as well. (However, I'd imagine that would be nearly the same as just Remembering everyone.)
Yes, bad phrasing on my part. That's what I meant by "play nicely together".
(However, I'd imagine that would be nearly the same as just Remembering everyone.)
I think it would be for short simulations. For long running ones, I suspect that there will be lots of ancestors in the distant past that are genealogical ancestors but not genetic ancestors anywhere (unless e.g. there is loads of gene conversion)
Oh, one extra wrinkle I just thought of. There's not much point to allowing treeSeqSimplify(keep_unary=T) if by default the next time that simplification is done automatically, it removes all the unary nodes anyway, because we started the simulation with initializeTreeSeq(keep_unary=F). But unless anyone has a better suggestion, I guess there's no harm in allowing it, as long as it's documented that subsequent auto-simplification could remove the unary nodes.
Oh, one extra wrinkle I just thought of. There's not much point to allowing treeSeqSimplify(keep_unary=T) if by default the next time that simplification is done automatically, it removes all the unary nodes anyway, because we started the simulation with initializeTreeSeq(keep_unary=F). But unless anyone has a better suggestion, I guess there's no harm in allowing it, as long as it's documented that subsequent auto-simplification could remove the unary nodes.
Why even have it as a parameter to treeSeqSimplify()? Why not just use the flag that was given to initializeTreeSeq() for all simplifications?
(However, I'd imagine that would be nearly the same as just Remembering everyone.)
I think it would be for short simulations. For long running ones, I suspect that there will be lots of ancestors in the distant past that are genealogical ancestors but not genetic ancestors anywhere (unless e.g. there is loads of gene conversion)
OK, probably I'm just confused, but. Remembering all individuals would mean that whole evolutionary branches that eventually went extinct would still be preserved in the tree sequence, right? Every individual would be remembered. Whereas retaining every individual and setting keepUnary=T would keep every individual that is genetically ancestral to the extant generation, but it would allow the evolutionary branches that went extinct to be pruned away by simplify, right? So isn't there a huge difference between the two, given that most evolutionary branches eventually go extinct? Or what am I misunderstanding?
If we keepUnary, then we'll prune out only individuals who leave behind no genome to the present day. If you're doing a long chromosome, that's nearly the same as individuals who have no pedigree ancestors in the present day. The number of pedigree descndants of an individual in a WF population acts, for a while anyhow, like a branching process with a Poisson(2) offspring distribution, whose probability of extinction is about 20%. So, I'd expect if you're doing a simulation with a long genome (like, 1Morgan long), that keepUnary would end up with about 80% as many individuals as compared to if you Remembered everyone. If you're doing a short genome though, then I agree, the number would be a lot smaller.
If we keepUnary, then we'll prune out only individuals who leave behind no genome to the present day. If you're doing a long chromosome, that's nearly the same as individuals who have no pedigree ancestors in the present day.
Yes – but it's much, much smaller than the total number of individuals that have ever lived, right? Which is what just remembering every individual in every generation would do, right? I mean, this is the main point of simplification, isn't it? That over time, most evolutionary branches go completely extinct: most individuals leave no pedigree descendants over the long run. But remembering every individual in every generation keeps all of those extinct branches, and effectively turns off simplification, doesn't it? Or am I misremembering how "remembering" works?
Yes – but it's much, much smaller than the total number of individuals that have ever lived, right?
Well, this is an empirical question, but my calculation above suggests that the number of individuals with no pedigree descendants is about 80% of the total number of individuals ever.
I think you're getting mixed up by the fact that the tree sequence only has to keep track of nodes with coalescences in extant material, not all nodes that are ancestors. So the condition for appearing in a tree sequence is "I'm the MRCA of at least two samples at some locus", which is a lot stricter than "I'm an ancestor of at least one sample at some locus".
Well, this is an empirical question, but my calculation above suggests that the number of individuals with no pedigree descendants is about 80% of the total number of individuals ever.
I think it's a relatively standard result that the pedigree ancestors encompass about 80% of an entire panmictic population (I think Richard Dawkins and I were the first to write about this in a popular science book, based on Chang's calculations, around the year 2000!). But that's not the same as the percentage of genetic (not pedigree) ancestors, which are the ones we would keep if we did keep_unary. Are pedigree and genetic ancestors really the same order of magnitude as @petrelharp says? I can see that's the case as the chromosomes get very long, but I'm not sure it is in e.g. humans. Graham Coop has a nice blog post about this, ISTR, and I asked him about it (?on twitter), assuming that far enough into the past, the genetic lineages get cut up into such fine pieces that they basically pass through all the pedigree ancestors. But he said that doesn't happen - something to do with the asymptotic effect of recombination.
I guess if we implement this feature, it'll be easy to find out!
Yes – but it's much, much smaller than the total number of individuals that have ever lived, right?
Well, this is an empirical question, but my calculation above suggests that the number of individuals with no pedigree descendants is about 80% of the total number of individuals ever.
I think you're getting mixed up by the fact that the tree sequence only has to keep track of nodes with coalescences in extant material, not all nodes that are ancestors. So the condition for appearing in a tree sequence is "I'm the MRCA of at least two samples at some locus", which is a lot stricter than "I'm an ancestor of at least one sample at some locus".
No, that's not the point of confusion for me, I get that. The point of confusion for me is that I can't reconcile what you're saying with, e.g., this claim: "Of all species that have existed on Earth, 99.9 percent are now extinct." (https://www.pbs.org/wgbh/evolution/extinction/massext/statement_03.html). In the long run, as I understand it, the vast, vast majority of evolutionary branches go completely extinct; no individual in those branches has any descendant whatsoever. If you took a complete tree sequence of the entire history of life on Earth and simplified it, pruning away those extinct branches would get rid of 99.9% of the individuals. (Well, a few of the extinct species would be ancestral to present-day species and so some of their individuals would be retained; but most would be pruned.) You would then get additional savings by pruning away all the nodes that don't involve coalescences, etc., as you say; but let's ignore that for now, since that's just gravy on top of the 99.9% pruning. But if you had done a "remember" on every individual in every generation, simplify() wouldn't get rid of any of the individuals at all, as I understand it.
So how does your claim that 80% of the individuals would be kept square with this perspective that suggests to me that 99.9% of the individuals would be pruned away? What am I missing? (Apologies for the digression. :->)
OK, so. I'm good with keepAllAncestralRetained=F if you guys like that. Shall I do a commit that adds this parameter to initializeTreeSeq(), and puts the value into a flag in SLiMSim for your use, and adds the doc above to the online doc in SLiMgui?
I'll let @hyanwong answer this... but, probably?
Sorry not to get round to this (it was late for me). I'm wondering if it is clearer to reverse the logic, so instead of keepAllAncestralRetained=F we have retainCoalescentOnly=T as the default. To me it seems a little less convoluted to explain that way. Then we can say:
Individuals can be temporarily retained using treeSeqRememberIndividuals(permanent=F), which means they are only output if their genomes remain in the tree sequence. Genomes are always removed if not ancestral to the sample; if retainCoalescentOnly=T (the default) they are also removed if no longer a branch point (coalescent node, or MRCA) in the tree sequence. In some cases it may be useful to retain a node and its associated individual when it is simply an intermediate node in the ancestry (i.e. in the middle of a branch). This can be done by setting retainCoalescentOnly=F. In this case ancestral nodes that are intermediate ("unary nodes") and within a retained individual are kept, along with the retained individual itself."
I think @bhaller is best placed to adjudicate on which name and explanation is clearer to the average SLiM user, though. Happy to go with @petrelharp's suggestion instead if that's deemed clearer.
I do like retainCoalescentOnly=T; the way it has "retain" as the active verb, and describes what that verb implies, is more direct to me. I guess the equivalent with the opposite sense would be retainAllAncestral=F. But that latter flag is less descriptive; it immediately begs the question "as opposed to retaining... who, exactly?", whereas retainCoalescentOnly=T has a more obvious opposite, "as opposed to retaining everybody". So I do lean toward retainCoalescentOnly=T for that reason.
However, I disagree that I'm the one with their finger on the pulse of the average SLiM user of tree-seq recording; I think that'd be @petrelharp. :-> What do you think, Peter?
Well, if nothing well be broken, then go ahead with the code as you like, Ben.
And, the other suggestion for the flag name is fine, as long as it's clear right away from the docs that the ordinary use doesn't need to think about it. What I'd like to avoid is people without a strong mental model of simplification, etcetera thinking they need to start Retaining individuals to get what they need at the end, when all they need is the standard tree sequence. (This will happen!)
OK, @hyanwong and @petrelharp, I have just committed the addition of [l$ retainCoalescentOnly=T] to initializeTreeSeq(). There is now a corresponding flag in SLiMSim, retain_coalescent_only_, that can be used when simplify is called; I guess it will translate (inversely?) to the new tskit flag.
The doc I wrote can be revised, of course. For initializeTreeSeq() I wrote:
The retainCoalescentOnly parameter controls how, exactly, simplification of the tree-sequence data is performed in SLiM (both for auto-simplification and for calls to treeSeqSimplify()). The default of T should work for almost all users of tree-sequence recording; changing it to F may dramatically increase memory usage and runtime, without benefit for most users. More specifically, this parameter controls the behavior of simplification for individuals and genomes that have been “retained” by calling treeSeqRememberIndividuals() with the parameter permanent=F. See the documentation of treeSeqRememberIndividuals() for further discussion (section 24.13.2).
For the treeSeqRememberIndividuals() doc I adapted Yan's proposal. Here's the full doc for the function, since I edited other parts of it to fit these changes too:
– (void)treeSeqRememberIndividuals(object individuals, [logical$ permanent = T])
Mark the individuals specified by individuals to be kept across tree sequence table simplification. This method may only be called if tree sequence recording has been turned on with initializeTreeSeq(). All currently living individuals are always kept across simplification; this method does not need to be called, and indeed should not be called, for that purpose. Instead, treeSeqRememberIndividuals() allows any individual, including dead individuals, to be kept in the final tree sequence. Typically this would be used, for example, to keep particular individuals that you wanted to be able to trace ancestry back to in later analysis. However, this is not the typical usage pattern for tree sequence recording; most models will not need to call this method.
There are two ways to keep individuals across simplification. If permanent is T (the default), then the specified individuals will be permanently remembered: their genomes will be added to the current sample, and they will always be present in the tree sequence. Permanently remembering a large number of individuals will, of course, markedly increase memory usage and runtime.
Supplying F for permanent will instead mark the individuals only for (temporary) retention: their genomes will not be added to the sample, and they will appear in the final tree sequence only if one of their genomes is retained across simplification. In other words, the rule of thumb for retained individuals is simple: if a genome is kept by simplification, the genome’s corresponding individual is kept also, if it is retained. Note that permanent remembering takes priority; calling this function with permanent=F on an individual that has previously been permanently remembered will not remove it from the sample.
The behavior of simplification for individuals retained with permanent=F depends upon the value of the retainCoalescentOnly flag passed to initializeTreeSeq(); here we will discuss the behavior of that flag in detail. First of all, genomes are always removed by simplification if they are not ancestral to the sample, unless their individual has been permanently remembered. If retainCoalescentOnly is T (the default), they are also removed if they are not a branch point (i.e., a coalescent node or most recent common ancestor) in the tree sequence. In some cases it may be useful to retain a genome and its associated individual when it is simply an intermediate node in the ancestry (i.e., in the middle of a branch). This can be done by setting retainCoalescentOnly to F in your call to initializeTreeSeq(). In this case, ancestral genomes that are intermediate (“unary nodes”, in tskit parlance) and are within a retained individual are kept, along with the retained individual itself. Since setting retainCoalescentOnly to F will prevent the unary nodes for retained individuals from being pruned, simplification may often be unable to prune very much at all from the tree sequence, and memory usage and runtime may increase rapidly. This setting should therefore be used only with caution; it is rarely necessary. See the pyslim documentation for further discussion of retaining and remembering individuals and the effects of the retainCoalescentOnly flag.
The metadata (age, location, etc) that are stored in the resulting tree sequence are those values present at either (a) the final generation, if the individual is alive at the end of the simulation, or (b) the last time that the individual was remembered, if not. Calling treeSeqRememberIndividuals() on an individual that is already remembered will cause the archived information about the remembered individual to be updated to reflect the individual’s current state; care should be taken to remember individuals at a point in time when their state is valid. A case where this is particularly important is for the spatial location of individuals in continuous-space models. SLiM automatically retains the portions of the genomes that comprise the first generation of any new subpopulation created with addSubpop() that are inherited by extant individuals, for easy recapitation and other analysis (see sections 17.2 and 17.10). However, the individuals of the first generation are not remembered automatically, only their needed genomic information.
The tskit merge ought to build without code changes once Castedo's PR is accepted (which I see just happened a moment ago) and that version of tskit gets merged in, since it will add vg->samples back in. I will go in and clean up the vargen usage as discussed later; that is orthogonal to this work, you can forget about that issue completely once you merge in the new tskit C API release that Ben is about to tag.
OK, @hyanwong and @petrelharp, I have just committed the addition of [l$ retainCoalescentOnly=T] to initializeTreeSeq()
Thanks so much for this Ben.
The doc I wrote can be revised, of course. For initializeTreeSeq() I wrote:
The default of T should work for almost all users of tree-sequence recording;
I'm not sure this is true, sadly. I think it is true if you are remembering (or retaining) everyone, or retaining every generation. If you are just retaining a few individuals, or only retaining every (say) 1000 generations, you probably want retainCoalescentOnly=F. So how about:
The retainCoalescentOnly parameter controls how, exactly, simplification of the tree-sequence data is performed in SLiM (both for auto-simplification and for calls to treeSeqSimplify()). More specifically, this parameter controls the behavior of simplification for individuals and genomes that have been “retained” by calling treeSeqRememberIndividuals() with the parameter permanent=F. The default of retainCoalescentOnly=T helps to keep the number of retained individuals relatively small, which is helpful if your simulation regularly flags many individuals for retaining. In this case, changing retainCoalescentOnly to F may dramatically increase memory usage and runtime, in a similar way to permanently Remembering all the individuals. See the documentation of treeSeqRememberIndividuals() for further discussion (section 24.13.2).
Also in the treeSeqRememberIndividuals() doc, how about:
... Since setting retainCoalescentOnly to F will prevent the unary nodes for retained individuals from being pruned, simplification may often be unable to prune very much at all from the tree sequence, and memory usage and runtime may increase rapidly. If you are retaining many individuals, this setting should therefore be used only with caution; it is not necessary if you are purely interested in the most recent common ancestors. See the pyslim documentation for further discussion of retaining and remembering individuals and the effects of the retainCoalescentOnly flag.
I can change the docs, but I thought I'd ask here if these changes seem sensible. I do think that "rarely needed" is a bit of an overstatement: after all, for a while we did consider making retainCoalescentOnly=F the default, so it can't be that rare of a use case for individual retention. I think most people will be in the mindset of "retain a few individuals to peer into the past at certain times", rather than in @petrelharp 's realm of "retain everyone, every generation, but let the software work out the ones that I really need to know about:"
OK, @hyanwong and @petrelharp, I have just committed the addition of [l$ retainCoalescentOnly=T] to initializeTreeSeq()
Thanks so much for this Ben.
The doc I wrote can be revised, of course. For initializeTreeSeq() I wrote:
The default of T should work for almost all users of tree-sequence recording;
I'm not sure this is true, sadly. I think it is true if you are remembering (or retaining) everyone, or retaining every generation. If you are just retaining a few individuals, or only retaining every (say) 1000 generations, you probably want retainCoalescentOnly=F. So how about: ...
Well, I think it is true that the default of T should work for almost all users of tree-sequence recording, because my guess is that almost all users of tree-sequence recording won't be retaining any individuals at all – so the value of this flag is irrelevant. (That's why having the default be flipped would also be reasonable – for most users it actually doesn't matter at all, and it's just a question of what the better default behavior is for the small minority of users who do actually retain individuals.) My goal was just to warn users as strongly as seemed reasonable, because of the concern @petrelharp expressed earlier.
That said, your edits seem clarifying and good, so the above point is moot; I just wanted to explain why I wrote it as I did. I'll incorporate your changes now in a commit (I'll do it on my end given the way the docs get generated :->). Thanks!
First of all, genomes are always removed by simplification if they are not ancestral to the sample, unless their individual has been permanently remembered.
to
First of all, genomes are always removed by simplification if they are not ancestral to the the final generation
and/or any permanently remembered genomes.
First of all, genomes are always removed by simplification if they are not ancestral to the sample, unless their individual has been permanently remembered.
to
First of all, genomes are always removed by simplification if they are not ancestral to the the final generation
and/or any permanently remembered genomes.
Otherwise, looks good!
Only problem with this edit is: is an individual ancestral to itself? If not (and my gut says no), this rewrite doesn't cover the case where the genome belongs to a remembered individual, but isn't ancestral to the final generation or any remembered individual.
So now I've got:
First of all, genomes are always removed by simplification if they are not ancestral to the final generation or any permanently remembered genomes, unless their individual has been permanently remembered.
But this is confusing and ugly. I'm not seeing a simpler rewrite, though. Any suggestions?
First of all, genomes are always removed by simplification unless they are (a) part of the final generation, (b) ancestral to the final generation, (c) a genome of a permanently remembered individual, or (d) ancestral to a permanently remembered individual.
OK, here's my proposed revision for this paragraph:
The behavior of simplification for individuals retained with permanent=F depends upon the value of the retainCoalescentOnly flag passed to initializeTreeSeq(); here we will discuss the behavior of that flag in detail. First of all, genomes are always removed by simplification unless they are (a) part of the final generation, (b) ancestral to the final generation, (c) a genome of a permanently remembered individual, or (d) ancestral to a permanently remembered individual. If retainCoalescentOnly is T (the default), they are also always removed if they are not a branch point (i.e., a coalescent node or most recent common ancestor) in the tree sequence. In some cases it may be useful to retain a genome and its associated individual when it is simply an intermediate node in the ancestry (i.e., in the middle of a branch). This can be enabled by setting retainCoalescentOnly to F in your call to initializeTreeSeq(). In this case, ancestral genomes that are intermediate (“unary nodes”, in tskit parlance) and are within an individual that has been retained using the permanent=F flag here are kept, along with the retained individual itself. Since setting retainCoalescentOnly to F will prevent the unary nodes for retained individuals from being pruned, simplification may often be unable to prune very much at all from the tree sequence, and memory usage and runtime may increase rapidly. If you are retaining many individuals, this setting should therefore be used only with caution; it is not necessary if you are purely interested in the most recent common ancestors. See the pyslim documentation for further discussion of retaining and remembering individuals and the effects of the retainCoalescentOnly flag.
I revised it a little in a few other spots, with the goal of clarifying that (a) retainCoalescentOnly=F does not, on its own, cause any additional genomes to be kept by simplification (you must also retain genomes), and (b) genomes (and individuals) get "retained" by passing permanent=F, a point that is made earlier too, but seems worth reinforcing here. Hopefully this is good. These concepts are very subtle and hard to explain accurately; diagrams will be worth their weight in gold. I'll commit this doc change now.
Hi @hyanwong. I guess this is tied up together with the improvements to the testing framework? And then there was a need for a small bug-fix in tskit that needed to get pulled into SLiM, too, right? How is all that coming along? Just checking in. :->
Activity
[-]Allow `keep_unary` to be passed to simplification funds[/-][+]Allow `keep_unary` to be passed to simplification func[/+][-]Allow `keep_unary` to be passed to simplification func[/-][+]Allow `keep_unary` to be passed to simplification funcs[/+]bhaller commentedon Jan 11, 2021
As I mentioned in the review on #25, let's try to envision the ideal final design for this stuff. Is it a
keepUnary=Toption oninitializeTreeSeq()and apermanent=Foption ontreeSeqRememberIndividuals()? In other words, are these two features completely orthogonal? Or is there some interaction between them, and some better API design? BecausekeepUnary=Tstrikes me as a kind of "remembering" or "retaining" too, but we're proposing handling it in a very different way. It could be handled instead as another type of flag passed totreeSeqRememberIndividuals(), right? A flag saying "mark these individuals as being kept even if their nodes are unary"? I don't see a clear problem with what has been proposed, but I want to make sure we're going to arrive at a final design that we won't regret later.hyanwong commentedon Jan 11, 2021
I see them as completely orthogonal, which is why I opened up a new issue. I guess you could think of this as a sort of "remembering", but of nodes not individuals. The distinction is very clear in tree-sequence world, and I think that the "keep_unary" function should automatically play nicely (and logically) with the
permanent=Foption in #25 . But let's see what @petrelharp thinks too?petrelharp commentedon Jan 11, 2021
Not even so much orthogonal, but copmplementary: if you do
keep_unary=Tand you Retain all the individuals, then you'll get the individuals corresponding to retained unary nodes as well. (However, I'd imagine that would be nearly the same as just Remembering everyone.)hyanwong commentedon Jan 11, 2021
Yes, bad phrasing on my part. That's what I meant by "play nicely together".
I think it would be for short simulations. For long running ones, I suspect that there will be lots of ancestors in the distant past that are genealogical ancestors but not genetic ancestors anywhere (unless e.g. there is loads of gene conversion)
hyanwong commentedon Jan 11, 2021
Oh, one extra wrinkle I just thought of. There's not much point to allowing
treeSeqSimplify(keep_unary=T)if by default the next time that simplification is done automatically, it removes all the unary nodes anyway, because we started the simulation withinitializeTreeSeq(keep_unary=F). But unless anyone has a better suggestion, I guess there's no harm in allowing it, as long as it's documented that subsequent auto-simplification could remove the unary nodes.bhaller commentedon Jan 11, 2021
Why even have it as a parameter to
treeSeqSimplify()? Why not just use the flag that was given toinitializeTreeSeq()for all simplifications?bhaller commentedon Jan 11, 2021
OK, probably I'm just confused, but. Remembering all individuals would mean that whole evolutionary branches that eventually went extinct would still be preserved in the tree sequence, right? Every individual would be remembered. Whereas retaining every individual and setting
keepUnary=Twould keep every individual that is genetically ancestral to the extant generation, but it would allow the evolutionary branches that went extinct to be pruned away by simplify, right? So isn't there a huge difference between the two, given that most evolutionary branches eventually go extinct? Or what am I misunderstanding?petrelharp commentedon Jan 11, 2021
If we
keepUnary, then we'll prune out only individuals who leave behind no genome to the present day. If you're doing a long chromosome, that's nearly the same as individuals who have no pedigree ancestors in the present day. The number of pedigree descndants of an individual in a WF population acts, for a while anyhow, like a branching process with a Poisson(2) offspring distribution, whose probability of extinction is about 20%. So, I'd expect if you're doing a simulation with a long genome (like, 1Morgan long), that keepUnary would end up with about 80% as many individuals as compared to if you Remembered everyone. If you're doing a short genome though, then I agree, the number would be a lot smaller.bhaller commentedon Jan 11, 2021
Yes – but it's much, much smaller than the total number of individuals that have ever lived, right? Which is what just remembering every individual in every generation would do, right? I mean, this is the main point of simplification, isn't it? That over time, most evolutionary branches go completely extinct: most individuals leave no pedigree descendants over the long run. But remembering every individual in every generation keeps all of those extinct branches, and effectively turns off simplification, doesn't it? Or am I misremembering how "remembering" works?
petrelharp commentedon Jan 11, 2021
Well, this is an empirical question, but my calculation above suggests that the number of individuals with no pedigree descendants is about 80% of the total number of individuals ever.
I think you're getting mixed up by the fact that the tree sequence only has to keep track of nodes with coalescences in extant material, not all nodes that are ancestors. So the condition for appearing in a tree sequence is "I'm the MRCA of at least two samples at some locus", which is a lot stricter than "I'm an ancestor of at least one sample at some locus".
petrelharp commentedon Jan 11, 2021
Just to be clear, I'm fine with this proposed change, but I suspect that in practice I'm going to keep on remembering everyone.
hyanwong commentedon Jan 11, 2021
I think it's a relatively standard result that the pedigree ancestors encompass about 80% of an entire panmictic population (I think Richard Dawkins and I were the first to write about this in a popular science book, based on Chang's calculations, around the year 2000!). But that's not the same as the percentage of genetic (not pedigree) ancestors, which are the ones we would keep if we did
keep_unary. Are pedigree and genetic ancestors really the same order of magnitude as @petrelharp says? I can see that's the case as the chromosomes get very long, but I'm not sure it is in e.g. humans. Graham Coop has a nice blog post about this, ISTR, and I asked him about it (?on twitter), assuming that far enough into the past, the genetic lineages get cut up into such fine pieces that they basically pass through all the pedigree ancestors. But he said that doesn't happen - something to do with the asymptotic effect of recombination.I guess if we implement this feature, it'll be easy to find out!
bhaller commentedon Jan 11, 2021
No, that's not the point of confusion for me, I get that. The point of confusion for me is that I can't reconcile what you're saying with, e.g., this claim: "Of all species that have existed on Earth, 99.9 percent are now extinct." (https://www.pbs.org/wgbh/evolution/extinction/massext/statement_03.html). In the long run, as I understand it, the vast, vast majority of evolutionary branches go completely extinct; no individual in those branches has any descendant whatsoever. If you took a complete tree sequence of the entire history of life on Earth and simplified it, pruning away those extinct branches would get rid of 99.9% of the individuals. (Well, a few of the extinct species would be ancestral to present-day species and so some of their individuals would be retained; but most would be pruned.) You would then get additional savings by pruning away all the nodes that don't involve coalescences, etc., as you say; but let's ignore that for now, since that's just gravy on top of the 99.9% pruning. But if you had done a "remember" on every individual in every generation, simplify() wouldn't get rid of any of the individuals at all, as I understand it.
So how does your claim that 80% of the individuals would be kept square with this perspective that suggests to me that 99.9% of the individuals would be pruned away? What am I missing? (Apologies for the digression. :->)
27 remaining items
hyanwong commentedon Jan 23, 2021
Sorry not to get round to this (it was late for me). I'm wondering if it is clearer to reverse the logic, so instead of
keepAllAncestralRetained=Fwe haveretainCoalescentOnly=Tas the default. To me it seems a little less convoluted to explain that way. Then we can say:I think @bhaller is best placed to adjudicate on which name and explanation is clearer to the average SLiM user, though. Happy to go with @petrelharp's suggestion instead if that's deemed clearer.
bhaller commentedon Jan 23, 2021
I do like
retainCoalescentOnly=T; the way it has "retain" as the active verb, and describes what that verb implies, is more direct to me. I guess the equivalent with the opposite sense would beretainAllAncestral=F. But that latter flag is less descriptive; it immediately begs the question "as opposed to retaining... who, exactly?", whereasretainCoalescentOnly=Thas a more obvious opposite, "as opposed to retaining everybody". So I do lean towardretainCoalescentOnly=Tfor that reason.However, I disagree that I'm the one with their finger on the pulse of the average SLiM user of tree-seq recording; I think that'd be @petrelharp. :-> What do you think, Peter?
petrelharp commentedon Jan 24, 2021
Well, if nothing well be broken, then go ahead with the code as you like, Ben.
And, the other suggestion for the flag name is fine, as long as it's clear right away from the docs that the ordinary use doesn't need to think about it. What I'd like to avoid is people without a strong mental model of simplification, etcetera thinking they need to start Retaining individuals to get what they need at the end, when all they need is the standard tree sequence. (This will happen!)
bhaller commentedon Jan 25, 2021
OK, @hyanwong and @petrelharp, I have just committed the addition of
[l$ retainCoalescentOnly=T]toinitializeTreeSeq(). There is now a corresponding flag inSLiMSim,retain_coalescent_only_, that can be used when simplify is called; I guess it will translate (inversely?) to the new tskit flag.The doc I wrote can be revised, of course. For initializeTreeSeq() I wrote:
For the treeSeqRememberIndividuals() doc I adapted Yan's proposal. Here's the full doc for the function, since I edited other parts of it to fit these changes too:
The tskit merge ought to build without code changes once Castedo's PR is accepted (which I see just happened a moment ago) and that version of tskit gets merged in, since it will add
vg->samplesback in. I will go in and clean up the vargen usage as discussed later; that is orthogonal to this work, you can forget about that issue completely once you merge in the new tskit C API release that Ben is about to tag.Progress! :->
hyanwong commentedon Jan 26, 2021
Thanks so much for this Ben.
I'm not sure this is true, sadly. I think it is true if you are remembering (or retaining) everyone, or retaining every generation. If you are just retaining a few individuals, or only retaining every (say) 1000 generations, you probably want
retainCoalescentOnly=F. So how about:Also in the
treeSeqRememberIndividuals()doc, how about:I can change the docs, but I thought I'd ask here if these changes seem sensible. I do think that "rarely needed" is a bit of an overstatement: after all, for a while we did consider making
retainCoalescentOnly=Fthe default, so it can't be that rare of a use case for individual retention. I think most people will be in the mindset of "retain a few individuals to peer into the past at certain times", rather than in @petrelharp 's realm of "retain everyone, every generation, but let the software work out the ones that I really need to know about:"bhaller commentedon Jan 26, 2021
Well, I think it is true that the default of T should work for almost all users of tree-sequence recording, because my guess is that almost all users of tree-sequence recording won't be retaining any individuals at all – so the value of this flag is irrelevant. (That's why having the default be flipped would also be reasonable – for most users it actually doesn't matter at all, and it's just a question of what the better default behavior is for the small minority of users who do actually retain individuals.) My goal was just to warn users as strongly as seemed reasonable, because of the concern @petrelharp expressed earlier.
That said, your edits seem clarifying and good, so the above point is moot; I just wanted to explain why I wrote it as I did. I'll incorporate your changes now in a commit (I'll do it on my end given the way the docs get generated :->). Thanks!
bhaller commentedon Jan 26, 2021
(done)
hyanwong commentedon Jan 26, 2021
Thanks. I'm working up a PR that actually implements the behaviour in simplify (trivial), with some tests (less trivial!)
petrelharp commentedon Jan 26, 2021
Late arrival, but how about changing
Otherwise, looks good!
bhaller commentedon Jan 26, 2021
Only problem with this edit is: is an individual ancestral to itself? If not (and my gut says no), this rewrite doesn't cover the case where the genome belongs to a remembered individual, but isn't ancestral to the final generation or any remembered individual.
So now I've got:
First of all, genomes are always removed by simplification if they are not ancestral to the final generation or any permanently remembered genomes, unless their individual has been permanently remembered.
But this is confusing and ugly. I'm not seeing a simpler rewrite, though. Any suggestions?
bhaller commentedon Jan 26, 2021
How about:
First of all, genomes are always removed by simplification unless they are (a) part of the final generation, (b) ancestral to the final generation, (c) a genome of a permanently remembered individual, or (d) ancestral to a permanently remembered individual.
At least it's clear...
bhaller commentedon Jan 27, 2021
OK, here's my proposed revision for this paragraph:
I revised it a little in a few other spots, with the goal of clarifying that (a) retainCoalescentOnly=F does not, on its own, cause any additional genomes to be kept by simplification (you must also retain genomes), and (b) genomes (and individuals) get "retained" by passing permanent=F, a point that is made earlier too, but seems worth reinforcing here. Hopefully this is good. These concepts are very subtle and hard to explain accurately; diagrams will be worth their weight in gold. I'll commit this doc change now.
bhaller commentedon Feb 2, 2021
Hi @hyanwong. I guess this is tied up together with the improvements to the testing framework? And then there was a need for a small bug-fix in tskit that needed to get pulled into SLiM, too, right? How is all that coming along? Just checking in. :->
petrelharp commentedon Feb 20, 2021
Closed in #158!