Accidentally sampling genomes from killed individuals

18 views
Skip to first unread message

Dylan Clark

unread,
Jun 3, 2024, 11:36:12 PMJun 3
to slim-discuss
Hello,

In a non-WF model, I am attempting to keep the population at 10000 individuals if the population exceeds that value in the tick cycle. Importantly, I want the draw of individuals from the population >10000 to be weighted by fitness. This was my current idea of how to do this:

K is a constant set at the carrying capacity of 10,000.

//sample 10000 individuals for the next generation late() { if (p1.individualCount - K > 0) { numindividuals = p1.individualCount - K; sim.killIndividuals(p1.sampleIndividuals(numindividuals, replace=F)); } }

My main question is whether this approach would work across tick cycles, considering this information from the manual:


killed individuals are "kept in a temporary “graveyard” until they can be freed safely. It continues to be safe to use them and their genomes.


I'm wondering whether killed genomes could potentially be sampled as I have another late statement occurring after the first one in my script:

late() {

bottleneck_gens = seq(from = ∆T - 10, to = ∆T + 20, by = 1);

if (sim.cycle > 1500) {

if (sim.cycle % 100 == 0) {

sim.outputFixedMutations();

p1.outputSample(500);

}

}

if (any(sim.cycle == bottleneck_gens)) {

sim.outputFixedMutations();

p1.outputSample(500);

}

}

3000 late() {

//full output of slim simulation

sim.outputFull(filePath = paste0("~/test_output.txt"));

//finish simulation

sim.simulationFinished();

}


My concern is whether sampling of genomes for the slim output file occurring after the killIndividuals() could potentially result in killed individual's genomes being sampled and put into output files even if the individuals have been removed from the subpopulation, since their genomes remain accessible for a short period of time in the "graveyard". Is this an issue?

If yes, do you have another idea of how to sample the population to keep it at 10000 without using killIndividuals()?

Thanks!

Ben Haller

unread,
Jun 4, 2024, 6:35:33 AMJun 4
to Dylan Clark, slim-discuss
Hi Dylan!

No, this is not a concern with your code as designed.  As the doc for killIndividuals() says:

"Immediately kills the individuals in individuals.  This removes them from their subpopulation and gives them an index value of -1."

The only case where you might end up sampling/using individuals is if you continue to use a vector of individuals that you obtained prior to the killIndividuals() call, as here:

    inds = p1.individuals;
    to_kill = sample(inds, 10);
    sim.killIndividuals(to_kill);
    my_sample = sample(inds, 10);

The vector `inds` contains all the individuals that were originally in p1, and that fact does not change just because killIndividuals() was called.  The vector `my_sample` might therefore contain individuals that were killed.  (Sometimes this might be useful, in fact, which is why it is allowed.)

But your code (a) does not keep and reuse a local var, like `inds` above, that contains references to the killed individuals, and (b) takes a sample from the subpopulation itself, with outputSample(), not from a vector of individuals.  For both those reasons, your code is quite safe.  To fix code like the code above, you could simply re-fetch `inds`:

    inds = p1.individuals;
    to_kill = sample(inds, 10);
    sim.killIndividuals(to_kill);
   
    inds = p1.individuals;
    my_sample = sample(inds, 10);

Now my_sample will never contain a killed individual.  Using p1.sampleIndividuals() instead of sample() would also work.

If you ever want to check whether you have a reference to a killed individual accidentally, you can always just check for an `index` property value of -1; as the doc quoted above says, that property is set to -1 by killIndividuals(), providing a reliable way to diagnose that situation.

Happy modeling!

Cheers,
-B.

Benjamin C. Haller
Messer Lab
Cornell University


Dylan Clark wrote on 6/4/24 5:36 AM:
--
SLiM forward genetic simulation: http://messerlab.org/slim/
---
You received this message because you are subscribed to the Google Groups "slim-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to slim-discuss...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/slim-discuss/4ff614ad-7321-471a-9b96-de9b47e744fdn%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages