1
1
import msprime
2
+ import numpy as np
2
3
import pytest
3
4
import tskit
4
5
import tszip
9
10
10
11
11
12
def test_model (tmpdir ):
13
+ # Generate a tree sequence with populations and migrations
14
+ N = 1000
15
+ demography = msprime .Demography ()
16
+ demography .add_population (name = "pop1" , initial_size = N )
17
+ demography .add_population (name = "pop2" , initial_size = N )
18
+ demography .add_population (name = "ancestral" , initial_size = N )
19
+ demography .set_symmetric_migration_rate (["pop1" , "pop2" ], 0.01 )
20
+ demography .add_population_split (
21
+ time = 1000 , derived = ["pop1" , "pop2" ], ancestral = "ancestral"
22
+ )
12
23
ts = msprime .sim_ancestry (
13
- recombination_rate = 1e-3 , samples = 10 , sequence_length = 1_000 , random_seed = 42
24
+ samples = {"pop1" : 5 , "pop2" : 5 },
25
+ demography = demography ,
26
+ sequence_length = 1e4 ,
27
+ record_migrations = True ,
28
+ random_seed = 42 ,
14
29
)
15
- ts = msprime .sim_mutations (ts , rate = 1e-2 , random_seed = 43 )
30
+ ts = msprime .sim_mutations (ts , rate = 1e-8 , random_seed = 42 )
31
+ assert ts .num_populations > 0
32
+ assert ts .num_sites > 0
33
+ assert ts .num_migrations > 0
34
+ assert ts .num_mutations > 0
35
+
16
36
tables = ts .tables
17
37
tables .nodes .metadata_schema = tskit .MetadataSchema ({"codec" : "json" })
38
+
39
+ # Give each individual a location
40
+ indiv_copy = tables .individuals .copy ()
41
+ tables .individuals .clear ()
42
+ for i , ind in enumerate (indiv_copy ):
43
+ tables .individuals .append (ind .replace (location = [i / 2 , i + 1 ]))
44
+
18
45
ts = tables .tree_sequence ()
19
46
20
47
tszip .compress (ts , tmpdir / "test.tszip" )
@@ -25,11 +52,44 @@ def test_model(tmpdir):
25
52
assert tsm .name == "test"
26
53
assert tsm .file_uuid == ts .file_uuid
27
54
assert len (tsm .summary_df ) == 9
28
- assert len (tsm .edges_df ) == ts .num_edges
29
55
assert len (tsm .trees_df ) == ts .num_trees
56
+
57
+ assert len (tsm .edges_df ) == ts .num_edges
58
+ for col in ["left" , "right" , "parent" , "child" ]:
59
+ assert np .array_equal (tsm .edges_df [col ].values , getattr (ts .tables .edges , col ))
60
+
30
61
assert len (tsm .mutations_df ) == ts .num_mutations
62
+ for m1 , m2 in zip (ts .mutations (), tsm .mutations_df .to_dict ("records" )):
63
+ assert m1 .derived_state == m2 ["derived_state" ]
64
+ assert m1 .site == m2 ["site" ]
65
+ assert m1 .node == m2 ["node" ]
66
+ assert m1 .parent == m2 ["parent" ]
67
+ assert m1 .time == m2 ["time" ]
68
+
31
69
assert len (tsm .nodes_df ) == ts .num_nodes
70
+ for col in ["time" , "flags" , "population" , "individual" ]:
71
+ assert np .array_equal (tsm .nodes_df [col ].values , getattr (ts .tables .nodes , col ))
72
+
32
73
assert len (tsm .sites_df ) == ts .num_sites
74
+ for m1 , m2 in zip (ts .sites (), tsm .sites_df .to_dict ("records" )):
75
+ assert m1 .ancestral_state == m2 ["ancestral_state" ]
76
+ assert m1 .position == m2 ["position" ]
77
+
78
+ assert len (tsm .individuals_df ) == ts .num_individuals
79
+ for m1 , m2 in zip (ts .individuals (), tsm .individuals_df .to_dict ("records" )):
80
+ assert m1 .flags == m2 ["flags" ]
81
+ assert np .array_equal (m1 .location , m2 ["location" ])
82
+ assert np .array_equal (m1 .parents , m2 ["parents" ])
83
+
84
+ assert len (tsm .populations_df ) == ts .num_populations
85
+ for m1 , m2 in zip (ts .populations (), tsm .populations_df .to_dict ("records" )):
86
+ assert m1 .metadata == m2 ["metadata" ]
87
+
88
+ assert len (tsm .migrations_df ) == ts .num_migrations
89
+ for col in ["left" , "right" , "node" , "source" , "dest" , "time" ]:
90
+ assert np .array_equal (
91
+ tsm .migrations_df [col ].values , getattr (ts .tables .migrations , col )
92
+ )
33
93
34
94
35
95
def test_model_errors (tmpdir ):
0 commit comments