Re: Modeling discrete migration events in WF model

11 views
Skip to first unread message

Ben Haller

unread,
Oct 29, 2025, 12:51:00 PM (13 days ago) Oct 29
to C Henry, slim-discuss
Hi C Henry,

I've just opened an issue about this at https://github.com/MesserLab/SLiM/issues/570.  The setMigrationRates() method is being a bit too fussy.

Given that, you'll need to clear out the migration with a loop like this:

    subpops = sim.subpopulations;
    for (subpop in subpops)
        subpop.setMigrationRates(subpops[subpops != subpop], rep(0, size(subpops) - 1));

To set up the migration pattern you want, you'll probably need to write a similar loop.  You write:

> it is difficult to use a function like p3.setMigrationRates(p1, 0.0);, since the identity of p3 and p1 changes every time

Note that you can loop up subpopulations from a vector like subpops (above) using integer indices, and also that setMigrationRates() will accept an integer subpop id instead of a Subpopulation object, and also that the Community class supports looking up subpopulations by id with the subpopulationsWithIDs() method.  With some mix of those approaches, you should be able to do what you want to do pretty easily.  :->

I hope this helps.  Happy modeling!

Cheers,
-B.

Benjamin C. Haller
Messer Lab
Cornell University


C Henry wrote on 10/27/25 9:38 PM:
 I accidentally posted this before I meant to (sorry!). The point is that I have a nonWF model that uses takeMigrants() to easily control migration in a discrete fashion (i.e., I can use takeMigrants() every 30 ticks). In the WF model, the function setMigrationRate() is a probability that is set across ticks. I was testing out whether or not I could convert my simulation to a WF model that would be less computationally expensive, especially since WF slim 5 has so many great haploid features now. Basically, what I am trying to do in the script excerpt (shown in my original post) is calculate a per-subpopulation probability that it receives migrants, and then randomly sample a source deme. If it does, ~0.5 of the offspring in the following generation will be from the randomly drawn deme. But I only want migration to occur in the following generation, not as a constant rate. For reference, every 30 ticks this repeats and demes are resampled.

Since I have ~100+ demes that stochastically undergo migration on tick 30 (using a sim.cycle % 30 ==, etc), it is difficult to use a function like p3.setMigrationRates(p1, 0.0);, since the identity of p3 and p1 changes every time (and there are oftentimes multiple demes that undergo migration), and I only want migration between demes to happen once every 30 generations. I was wondering if it was possible to either A) do a global rate like subpops.setMigrationRates(0.0 -- which unfortunately didn't work) in the following generation after migration occurs.. so I didn't have to somehow call the randomly selected demes. Or, use a function like self.active = 0 that would basically reset the migration rates back to zero once it has happened.

Thank you so much for your help! I hope that explanation somewhat makes sense.

On Monday, October 27, 2025 at 5:58:52 PM UTC-4 C Henry wrote:
Hello!

I wanted to ask a hopefully simple question about functions in the WF model. I have been making a nonWF model to do a metapopulation haploid model (w/ a bunch of other complicated features), which has made it extremely expensive and slow, although I appreciate how much direct control I had in my functions. I was toying around with the WF model, and it seems like I am able to create a much faster but similar version using some of the SLIM 5/5.1 features. 

The population size, cloning rate, etc of my 100 demes. are determined using the modulus operator. Every "x" ticks, there is a per deme probability that it goes extinct (setSubpopulationSize(0)). Then, it is "recolonized" with subpopSplit, although it is technically a different deme (side note: I have been calling the newly split populations by the max subpop id + 1 -- is there really no way to call another deme by the same name after it has been removed? Just wondering, because I worry that i will end up with "p1e6" eventually even though I have a constant number of demes).

Where I am getting stuck specifically is the migration part:

seq(33, n_cycles, by=cycle_length) late() {

(....)

migrating_subpops = non_extinct_subpops[runif(size(non_extinct_subpops)) < migrationProb]; 

for (migrating_subpop in migrating_subpops) {

source_subpop = sample(non_extinct_subpops[non_extinct_subpops != migrating_subpop], 1);

migrating_subpop.setMigrationRates(source_subpop, 0.5);

}


I am generating a number to determine the migration rate, sampling a random subpopulation that has not gone extinct, and then I would like to have a single movement of migration into the deme, where the next generation of offspring will have 50% parents from one, and 50% from the other. The model is clonal up until this point, and then it has a single generation of recombination after migration occurs. My thought process is somehow defining a global migration variable, or only having "setmigrationrates" locally set within said block. 



--
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/3e04e296-8519-425b-8932-57ade3d394b6n%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages