@@ -108,11 +108,13 @@ and so our simulation would have less genetic variation than it should have
108108Doing this is as simple as:
109109
110110``` {code-cell}
111- orig_ts = pyslim .load("example_sim.trees")
112- rts = orig_ts .recapitate(
111+ orig_ts = tskit .load("example_sim.trees")
112+ rts = pyslim .recapitate(orig_ts,
113113 recombination_rate=1e-8,
114- Ne =200, random_seed=5)
114+ ancestral_Ne =200, random_seed=5)
115115```
116+ The warning is harmless; it is reminding us to think about generation time
117+ when recapitating a nonWF simulation (a topic we'll deal with later).
116118
117119We can check that this worked as expected, by verifying that after recapitation
118120all trees have only one root:
@@ -124,14 +126,14 @@ print(f"Maximum number of roots before recapitation: {orig_max_roots}\n"
124126 f"After recapitation: {recap_max_roots}")
125127```
126128
127- The {meth} ` .SlimTreeSequence .recapitate` method
129+ The {func} ` .recapitate ` method
128130is just a thin wrapper around {func}` msprime.sim_ancestry ` ,
129131and you need to set up demography explicitly - for instance, in the example above
130132we've simulated from an ancestral population of `` Ne=200 `` diploids.
131133If you have more than one population,
132134you must set migration rates or else coalescence will never happen
133135(see {ref}` sec_recapitate_with_migration ` for an example,
134- and {meth} ` .SlimTreeSequence .recapitate` for more).
136+ and {func} ` .recapitate ` for more).
135137
136138
137139#### Recapitation with a nonuniform recombination map
@@ -223,9 +225,9 @@ positions[-1] += 1
223225assert positions[-1] == orig_ts.sequence_length
224226
225227recomb_map = msprime.RateMap(position=positions, rate=rates)
226- rts = orig_ts .recapitate(
227- recombination_map =recomb_map,
228- Ne =200, random_seed=7)
228+ rts = pyslim .recapitate(orig_ts,
229+ recombination_rate =recomb_map,
230+ ancestral_Ne =200, random_seed=7)
229231assert(max([t.num_roots for t in rts.trees()]) == 1)
230232```
231233(As before, you should * not* usually explicitly set
@@ -301,7 +303,7 @@ which would be inconsistent with the SLiM simulation.
301303
302304After recapitation,
303305simplification to the history of 100 individuals alive today
304- can be done with the {meth}` .SlimTreeSequence .simplify` method:
306+ can be done with the {meth}` tskit.TreeSequence .simplify` method:
305307
306308``` {code-cell}
307309import numpy as np
@@ -423,7 +425,9 @@ and write their SNPs to a VCF is:
423425``` {code-cell}
424426np.random.seed(1)
425427keep_indivs = np.random.choice(alive, 100, replace=False)
426- ts = pyslim.SlimTreeSequence(msprime.mutate(orig_ts, rate=1e-8, random_seed=1))
428+ ts = pyslim.SlimTreeSequence(
429+ msprime.sim_mutations(orig_ts, rate=1e-8, random_seed=1)
430+ )
427431with open("example_snps.vcf", "w") as vcffile:
428432 ts.write_vcf(vcffile, individuals=keep_indivs)
429433```
@@ -452,7 +456,9 @@ keep_nodes = []
452456for i in keep_indivs:
453457 keep_nodes.extend(orig_ts.individual(i).nodes)
454458sts = rts.simplify(keep_nodes)
455- ts = pyslim.SlimTreeSequence(msprime.mutate(sts, rate=1e-8, random_seed=1))
459+ ts = pyslim.SlimTreeSequence(
460+ msprime.sim_mutations(sts, rate=1e-8, random_seed=1)
461+ )
456462```
457463Individuals are retained by simplify if any of their nodes are,
458464so we would get an alive individual without sample nodes if, for instance,
@@ -533,27 +539,43 @@ so there is an empty "population 0" in a SLiM-produced tree sequence.
533539
534540(sec_recapitate_with_migration)=
535541
536- ## Recapitation with more than one population
542+ ## Recapitation with migration between more than one population
537543
538544Following on the last example,
539545let's recapitate and mutate the tree sequence.
540- Recapitation takes a bit more thought, because we have to specify a migration matrix
541- (or else it will run forever, unable to coalesce).
546+ Recall that this recipe had two populations, `` p1 `` and `` p2 `` ,
547+ each of size 1000.
548+ Recapitation takes a bit more thought, because if the two populations stay separate,
549+ it will run forever, unable to coalesce.
550+ By default, :func:` .recapitate ` * merges* the two populations into a single
551+ one of size `` ancestral_Ne `` .
552+ But, if we'd like them to stay separate, we need to inclue migration between them.
553+ Here's how we set up the demography using msprime's tools:
542554
543555``` {code-cell}
544- pop_configs = [msprime.PopulationConfiguration(initial_size=1000)
545- for _ in range(orig_ts.num_populations)]
546- rts = orig_ts.recapitate(population_configurations=pop_configs,
547- migration_matrix=[[0.0, 0.0, 0.0],
548- [0.0, 0.0, 0.1],
549- [0.0, 0.1, 0.0]],
556+ demography = msprime.Demography.from_tree_sequence(orig_ts)
557+ for pop in demography.populations:
558+ # must set their effective population sizes
559+ pop.initial_size = 1000
560+
561+ demography.add_migration_rate_change(
562+ time=orig_ts.metadata['SLiM']['generation'],
563+ rate=0.1, source="p1", dest="p2",
564+ )
565+ demography.add_migration_rate_change(
566+ time=orig_ts.metadata['SLiM']['generation'],
567+ rate=0.1, source="p2", dest="p1",
568+ )
569+ rts = pyslim.recapitate(orig_ts, demography=demography,
550570 recombination_rate=1e-8,
551- random_seed=4)
571+ random_seed=4
572+ )
552573ts = pyslim.SlimTreeSequence(
553574 msprime.sim_mutations(
554575 rts, rate=1e-8,
555576 model=msprime.SLiMMutationModel(type=0),
556- random_seed=7))
577+ random_seed=7)
578+ )
557579```
558580
559581Again, there are * three* populations because SLiM starts counting at 1;
0 commit comments