Skip to content

Commit c61a151

Browse files
authored
Merge pull request #250 from hyanwong/completing-fwd-simpler
Make WF example simpler
2 parents 974c3da + 567bc3b commit c61a151

1 file changed

Lines changed: 8 additions & 17 deletions

File tree

completing_forward_sims.md

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,22 @@ import numpy as np
4040
4141
def wright_fisher(N, T, L=100, random_seed=None):
4242
"""
43-
Simulate a Wright-Fisher population of N haploid individuals with L
44-
discrete loci for T generations. Based on Algorithm W from
45-
https://doi.org/10.1371/journal.pcbi.1006581
43+
Simulate a Wright-Fisher population of N haploid individuals with L discrete
44+
loci for T generations, with one recombination per transmission event
45+
Based on Algorithm W from https://doi.org/10.1371/journal.pcbi.1006581
4646
"""
4747
random.seed(random_seed)
4848
tables = tskit.TableCollection(L)
4949
tables.populations.add_row()
5050
P = np.arange(N, dtype=int)
5151
for _ in range(N):
52-
tables.nodes.add_row(time=T, flags=0, population=0)
52+
tables.nodes.add_row(time=T, population=0)
5353
t = T
5454
while t > 0:
5555
t -= 1
5656
Pp = P.copy()
5757
for j in range(N):
58-
u = tables.nodes.add_row(time=t, flags=0, population=0)
58+
u = tables.nodes.add_row(time=t, population=0)
5959
Pp[j] = u
6060
a = random.randint(0, N - 1)
6161
b = random.randint(0, N - 1)
@@ -64,19 +64,10 @@ def wright_fisher(N, T, L=100, random_seed=None):
6464
tables.edges.add_row(x, L, P[b], u)
6565
P = Pp
6666
67-
# Now do some table manipulations to ensure that the tree sequence
68-
# that we output has the form that msprime needs to finish the
69-
# simulation. (Note: table columns are not modified in place because the
70-
# tables API does not currently allow direct access to memory.)
71-
72-
# Mark the extant population as samples.
73-
flags = tables.nodes.flags
74-
flags[P] = tskit.NODE_IS_SAMPLE
75-
tables.nodes.flags = flags
7667
tables.sort()
77-
# Simplify with respect to the current generation, but ensuring we keep the
78-
# ancient nodes from the initial population.
79-
tables.simplify(keep_input_roots=True)
68+
# Simplify with respect to nodes at time zero (the current generation), using
69+
# `keep_input_roots`` to keep the ancient nodes from the initial population.
70+
tables.simplify(np.where(tables.nodes.time == 0)[0], keep_input_roots=True)
8071
return tables.tree_sequence()
8172
```
8273

0 commit comments

Comments
 (0)