Getting samples from tree sequence outputted by SLiM for time before roots

20 views
Skip to first unread message

Sohan Alleshwaram

unread,
Oct 25, 2025, 10:47:59 PMOct 25
to slim-discuss
Hi all,

I have a script in slim which sets up a neutral SLiM simulation with many subpopulations that runs for ~200 generations which outputs a tree sequence. Using pyslim and msprime, I recapitate the sequence until coalescence and then add mutations using msprime.sim_mutations. 

My goal is to sample this history at a couple of points in time at subsets of the populations I have set up in SLiM using TreeSequence.samples(population, time) and calculate diversities at these sites. This works when time is 0 for all the sites I am sampling, but it suddenly stops working when I set time to anything greater than 0. Specifically when I run something like:
 pi = ts.diversity([ts.samples(population=idx, time=8)])
I get this error:
raise ValueError("Sample sets must contain at least one element")
on the call for diversity().

Does anyone know why this might be? Is there something about the way tree sequences are stored that I'm not understanding?

I know this is more of a tskit question and now a SLiM one, but I didn't really know where else to ask this, sorry about that.

Thanks,
Sohan Alleshwaram


Ben Haller

unread,
Oct 25, 2025, 10:55:22 PMOct 25
to Sohan Alleshwaram, slim-discuss
Hi Sohan!

No worries, I imagine Peter Ralph will see your question and answer it.  :->  I'm not sure how Peter prefers to receive questions like this about the Python side of things, but I don't mind them being on slim-discuss.

Cheers,
-B.

Benjamin C. Haller
Messer Lab
Cornell University


'Sohan Alleshwaram' via slim-discuss wrote on 10/25/25 10:47 PM:
--
SLiM forward genetic simulation: http://messerlab.org/slim/
Before posting, please read http://benhaller.com/slim/bugs_and_questions.html
---
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 visit https://groups.google.com/d/msgid/slim-discuss/6c75d5ba-46ea-48fd-bc38-6c80bc504818n%40googlegroups.com.

Peter Ralph

unread,
Oct 27, 2025, 1:12:38 AMOct 27
to slim-discuss, Sohan Alleshwaram
Hello! The problem is that you don't have any samples at anything except the end of the simulation (which is time=0, since that's "time ago"). Thus "must contain at least one element", since ts.samples(time=8) has zero elements. What you need to do is to use treeSeqRememberIndividuals( ) to keep around (samples from) the population at whatever past time points you're going to want to compute things for. So for instance if you did
  sim.treeSeqRememberIndividuals(sim.subpopulations.individuals);
at the tick which is 8 generations before the end of the simulation, then your code should work. (See the pyslim manual for converting between slim time and tskit time, however.)

--peter

From: 'Sohan Alleshwaram' via slim-discuss <slim-d...@googlegroups.com>
Sent: Saturday, October 25, 2025 7:47 PM
To: slim-discuss <slim-d...@googlegroups.com>
Subject: Getting samples from tree sequence outputted by SLiM for time before roots
 
Hi all, I have a script in slim which sets up a neutral SLiM simulation with many subpopulations that runs for ~200 generations which outputs a tree sequence. Using pyslim and msprime, I recapitate the sequence until coalescence and then add
ZjQcmQRYFpfptBannerStart
This message originated outside the UO email ecosystem.
Use caution with links and attachments. Learn more about this email warning tag.
 
ZjQcmQRYFpfptBannerEnd

Sohan Alleshwaram

unread,
Oct 27, 2025, 5:56:11 PMOct 27
to slim-discuss
Hi Peter,

Thanks! This seems to have solved it!

Best,
Sohan Alleshwaram
Reply all
Reply to author
Forward
0 new messages