library(slendr)
init_env()
outgroup <- population("outgroup", time = 7000000, N = 10000)
ancestor <- population("ancestor", time = 6000001, N = 1000, parent = outgroup, remove = 599999)
altai <- population("Altai", time = 600000, N = 1000, parent = ancestor, remove = 49999)
vindija <- population("Vindija", time = 200000, N = 1000, parent = altai, remove = 49999)
san <- population("San", time = 600000, N = 10000, parent = ancestor)
mbuti <- population("Mbuti", time = 350000, N = 10000, parent = san)
yoruba <- population("Yoruba", time = 220000, N = 10000, parent = mbuti)
dinka <- population("Dinka", time = 150000, N = 15000, parent = yoruba)
nonafr <- population("nonAfrican", time = 70000, N = 3000, parent = dinka)
gf <- gene_flow(from = vindija, to = nonafr, proportion = 0.03, start = 55000, end = 50000)
model <- compile_model(
populations = list(outgroup, ancestor, altai, vindija, san, mbuti, yoruba, dinka, nonafr),
gene_flow = gf,
generation_time = 30
)
# schedule recording of present-day individuals
schedule_present <- schedule_recording(
model, times = 0,
list(outgroup, 1), list(san, 1), list(mbuti, 1), list(yoruba, 1), list(dinka, 1), list(nonafr, 1)
)
# schedule recording of two Nenderthals
schedule_neand <- schedule_recording(model, times = 50000, list(altai, 1, "Altai"), list(vindija, 1, "Vindija"))
# schedule recording of Upper-Paleolithic modern humans
schedule_emh <- schedule_recording(model, times = runif(n = 50, min = 1000, max = 45000), list(nonafr, 1))
# bind the schedules into one
schedule <- rbind(schedule_present, schedule_neand, schedule_emh)
ts <-
msprime(model, sequence_length = 100e6, recombination_rate = 1e-8, schedule = schedule) %>%
ts_mutate(mutation_rate = 1e-8)
s <- ts_names(ts, split = TRUE)Neanderthal ancestry trajectory
In this case study, you will be reproducing an important result from my 2018 study on the limits of selection against Neanderthal introgression. In a very unplanned and unpredictable manner, this research ended up turning into a study on the importance of model assumptions on the robustness of inferences with \(f_4\) statistics and how they can produce entirely misleading inferences about ancient admixture events in human history.
I chose this case study for several reasons:
This research would’ve been absolutely impossible without simulations exactly like those you’ve learned to do in this workshop.
The limitations of computational tools I had available at the time required me to write hundreds of lines of code spread across shell scripts, Python scripts, and R scripts. Thanks to slendr, a project which required months of work and lots of coding is possible to do in a handful of lines of R.
It provides an excellent educational example of how slendr simulations can help you navigate practically any demographic scenario, gain confidence in your results, and aid in their interpretation.
Most importantly, the lessons of my paper apply in any context of using \(f_4\) statistics in ancient DNA research. The Neanderthal introgression case serves just as an illustrative example.
Preparation
To avoid too much frustration with transcribing very complex admixture graph model from yet another of my drawings, please copy the following code into a new script introgression.R. When you visualize the model, you should see a relatively detailed (yet still simplified) model of ancient human history including Neanderthal introgression into the ancestors of non-Africans. Again, please inspect this script to make sure you understand what it does—this is now even more important than before.
Please note that real-world simulations are rather computationally intensive, much more so than the modeling we did in our exercises. Generally speaking, most of simulation-based research happens on big computational servers. For our learning purposes here, if things take too long, just lower the amount of simulated sequence. However, be aware that this will, in turn, make statistics much more noisy (but that’s OK, we’re learning here).
Indirect \(f_4\)-ratio estimate of Neanderthal ancestry
The main controversy at the time I did this research was caused by the observation that ancient DNA time series of early modern humans (like the one you just simulated!) showed a clear decline in Neanderthal ancestry trajectory over the past ~45 thousand years.
The way the authors of the “Neanderthal decline” paper computed Neanderthal ancestry was using what I ended up naming as the “indirect \(f_4\)-ratio” estimate, described in panel B of Figure 1 in my paper (where “Dinka” typically represented the “East African” lineage, and “Yoruba” represented the “Central and West African” lineage, to follow the terminology in that schematic diagram). This “indirect” estimate is what showed clear evidence of the decline in Neanderthal ancestry over time.
Compute this “indirect \(f_4\)-ratio” estimate of Neanderthal ancestry from your simulated ts tree sequence for every one of your “nonAfrican” individuals. Then visualize the simulated Neanderthal ancestry trajectory as a time-series (i.e., your indirect ancestry estimate on the y-axis, time on the x-axis), similarly to what’s shown for the empirical data linked above. What kind of temporal trend do you observe?
Hint: You can either compute this “manually” using the path overlap principles described in the \(f\) statistics chapter earlier, or you can make your life a bit easier and use a dedicated slendr function ts_f4ratio(). That function follows the same nomenclature shown in my Figure 1B diagram above. If you use ts_f4ratio(), be aware that X can only always be one individual in this function, so you will want to compute this for every aDNA individual in the time-series separately.
Hint: If you need more help, see this example of how to compute the indirect \(f_4\)-ratio for a single individual.
Adding complex admixture during modern human prehistory
In your visualization of the indirect \(f_4\)-ratio trajectory, you should have noticed no trend at all. Indeed, under neutrality, Neanderthal ancestry is expected to constant throughout the millenia. However, the puzzle at the time was that the empirical data showed clear, significant temporal decline in this estimate over time!
The solution to the puzzle came out of (lots of) simulations and modeling, very similar to what you’ve done in this workshop so far. Let’s retrace my steps again.
What happens if you introduce additional gene flow events within the modern human population tree? For example, simulate admixture between the two African lineages used in your indirect \(f_4\)-ratio calculation, as well as “back-flow” admixture from the non-African population into both of those African lineages. Let’s say that all of these admixture events should happen at 5% proportion, the within-Africa gene flow should happen starting from 20 kya until present-day time 0, and the backflow nonAfrican admixture should happen from 8 kya to 5 kya.
None of those changes into the demographic model changes the amount of true Neanderthal ancestry in Europeans. But what does the trajectory of the indirect Neanderthal ancestry estimate look like when you compute it on this model with additional complex admixture within modern humans?
Hint: Feel free to make a copy of your introgression.R script to make the indicated changes, perhaps saving it as introgression_complex.R, just to make things clean and tidy.
Again, if you think your tree-sequence simulation takes too long or that your computer is struggling, feel free to use this chunk of code to read my own ts tree sequence from the internet, then continue working with it as if you simulated it yourself:
Direct \(f_4\)-ratio estimate of Neanderthal ancestry
The reason we had to compute Neanderthal ancestry using the “indirect” statistic is because we only had a single Neanderthal genome available at the time—the genome of the “Altai” Neanderthal.
Luckily, a few years later, a second Neanderthal was sequenced which allowed us to express the \(f_4\)-ratio estimate in a “direct” way using both Neanderthal genomes. We called this estimate the “direct \(f_4\)-ratio” estimate.
If you got to the relevant part of the \(f\)-statistics chapter, you may remember that we discovered multiple ways of computing the px ancestry—either coming from the p3 population (we could call it alpha), or as its complement coming from the p2 population (we could compute it as 1 - alpha). Well, the “indirect vs direct \(f_4\)-ratio” situation is exactly the same.
Using this idea, compute the “direct \(f_4\)-ratio” using the two Neanderthal genomes on the model which includes lots of complex admixture within modern humans. How does the direct \(f_4\)-ratio perform on such a model? Is it robust and can it infer the Neanderthal ancestry trajectory accurately, unlike the indirect estimate?
Hint: If you need help, see this example of how to compute the direct \(f_4\)-ratio for a single individual again.
In conclusion, the demographic simulation exploration in my paper took months of work (and a combination of Python simulations, shells scripts, and R data analysis scripts), resolving a problem which occupied the brightest minds of population genetics at the time. I hope that the fact that you’re able to reproduce the core aspects of this work this easily convinces you of the power of slendr modeling for this type of research!
Bonus exercise
Because I saved you a lot of work by providing the introgression admixture model already programmed, here’s a bonus challenge:
One question that the authors of the original draft Neanderthal genome paper had to answer was the issue of the direction of gene-flow.
How would you show that introgression happened from Neanderthals into non-Africans and not the other way around? Skim through the paper linked above, particularly focusing on the section called “Direction of gene flow” to get an idea of how did the authors distinguished the two scenarios using a \(D\) statistic (which, for our purposes, could be thought of as essentially equivalent to the \(f_4\) statistic as we’ve established it, so you can use the familiar \(f_4\) to answer this question).
Hint: They used logic to argue for what would the expected values of certain \(f_4\) statistics look like between two different models (the model of introgression from Neanderthals into non-Africans, and the model of introgression from non-Africans into Neanderthals). We want to use simulations to try to arrive at the same conclusions.




