def validate_nonbinary_trees(self, n): demographic_events = [ msprime.SimpleBottleneck(0.02, 0, proportion=0.25), msprime.SimpleBottleneck(0.2, 0, proportion=1)] for seed in range(1, 10): ts = msprime.simulate( n, random_seed=seed, demographic_events=demographic_events) # Check if this is really nonbinary found = False for edgeset in ts.edgesets(): if len(edgeset.children) > 2: found = True break self.assertTrue(found) tree1 = ts.first() ts = msprime.simulate( n, random_seed=seed + 1, demographic_events=demographic_events) tree2 = ts.first() self.assertAlmostEqual( tsinfer.kc_distance(tree1, tree2), kc_distance(tree1, tree2)) self.assertAlmostEqual( tsinfer.kc_distance(tree2, tree1), kc_distance(tree2, tree1)) # compare to a binary tree also tree2 = msprime.simulate(n, random_seed=seed + 1).first() self.assertAlmostEqual( tsinfer.kc_distance(tree1, tree2), kc_distance(tree1, tree2)) self.assertAlmostEqual( tsinfer.kc_distance(tree2, tree1), kc_distance(tree2, tree1))
def test_nonbinary_trees(self): bottlenecks = [ msprime.SimpleBottleneck(0.01, 0, proportion=0.05), msprime.SimpleBottleneck(0.02, 0, proportion=0.25), msprime.SimpleBottleneck(0.03, 0, proportion=1)] ts = msprime.simulate( 10, length=100, recombination_rate=1, demographic_events=bottlenecks, random_seed=1) self.verify_tree_sequence(ts)
def test_three_populations_no_migration_recombination(self): seed = 1234 ts1 = msprime.simulate( population_configurations=[ msprime.PopulationConfiguration(10), msprime.PopulationConfiguration(10), msprime.PopulationConfiguration(10), ], migration_matrix=np.zeros((3, 3)), recombination_rate=0.5, end_time=0.1, random_seed=seed, ) ts2 = msprime.simulate( population_configurations=[ msprime.PopulationConfiguration(), msprime.PopulationConfiguration(), msprime.PopulationConfiguration(), ], migration_matrix=np.zeros((3, 3)), from_ts=ts1, recombination_rate=0.5, demographic_events=[ msprime.SimpleBottleneck(time=0.5, population=0, proportion=1.0), msprime.SimpleBottleneck(time=0.5, population=1, proportion=1.0), msprime.SimpleBottleneck(time=0.5, population=2, proportion=1.0), msprime.MassMigration(0.61, 1, 0, 1.0), msprime.MassMigration(0.61, 2, 0, 1.0), msprime.SimpleBottleneck(0.61, population=0, proportion=1.0), ], random_seed=seed, ) self.assertGreater(ts2.num_trees, 1) for tree in ts2.trees(): # We should have three children at the root, and every node below # should be in one population. root_children = tree.children(tree.root) self.assertEqual(len(root_children), 3) populations = {ts2.node(u).population: u for u in root_children} self.assertEqual(len(populations), 3) for pop in [0, 1, 2]: for node in tree.nodes(populations[pop]): self.assertEqual(ts2.node(node).population, pop)
def get_bottleneck_examples(): """ Returns an iterator of example tree sequences with nonbinary trees. """ bottlenecks = [ msprime.SimpleBottleneck(0.01, 0, proportion=0.05), msprime.SimpleBottleneck(0.02, 0, proportion=0.25), msprime.SimpleBottleneck(0.03, 0, proportion=1)] for n in [3, 10, 100]: ts = msprime.simulate( n, length=100, recombination_rate=1, demographic_events=bottlenecks, random_seed=n) yield ts
def subset_samples(n, samples): demographic_events = [msprime.SimpleBottleneck(1000, 0.15)] demographic_events = [] ts = msprime.simulate(sample_size=n, Ne=1e4, length=1e4, recombination_rate=5e-8, mutation_rate=2e-8, random_seed=5, demographic_events=demographic_events) # ts = msprime.load(sys.argv[1]) subset = ts.subset(samples) # print("starting subsetting", len(samples)) # before = time.clock() # subset = ts.subset(samples) # duration = time.clock() - before # print("Subsetting done", duration) all_trees = ts.trees() full_tree = next(all_trees) for subset_tree in subset.trees(): while full_tree.get_interval()[1] <= subset_tree.get_interval()[1]: # print(full_tree.get_interval(), subset_tree.get_interval()) for u, v in itertools.combinations(range(len(samples)), 2): # print(u, v, samples[u], samples[v]) t_mrca1 = full_tree.get_tmrca(samples[u], samples[v]) t_mrca2 = subset_tree.get_tmrca(u, v) assert t_mrca1 == t_mrca2 full_tree = next(all_trees, None) if full_tree is None: break
def test_v2_non_binary_records(self): demographic_events = [ msprime.SimpleBottleneck(time=0.01, population=0, proportion=1) ] ts = msprime.simulate(sample_size=10, demographic_events=demographic_events, random_seed=1) self.assertRaises(ValueError, tskit.dump_legacy, ts, self.temp_file, 2)
def bottleneck_example(): return msprime.simulate( random_seed=1, sample_size=100, recombination_rate=0.1, length=10, demographic_events=[ msprime.SimpleBottleneck(time=0.01, proportion=0.75)])
def test_smc_bottlenecks(self): # TODO we should have a better exception here. for model in ["smc", "smc_prime"]: self.assertRaises( _msprime.InputError, msprime.simulate, 10, model=model, demographic_events=[msprime.SimpleBottleneck(1)]) self.assertRaises( _msprime.InputError, msprime.simulate, 10, model=model, demographic_events=[msprime.InstantaneousBottleneck(1)])
def get_nonbinary_tree(self): demographic_events = [ msprime.SimpleBottleneck(time=0.1, population=0, proportion=0.5)] ts = msprime.simulate( 10, recombination_rate=5, mutation_rate=10, demographic_events=demographic_events, random_seed=1) for t in ts.trees(): for u in t.nodes(): if len(t.children(u)) > 2: return t assert False
def test_v2_non_binary_records(self): demographic_events = [ msprime.SimpleBottleneck(time=0.01, proportion=1) ] ts = msprime.simulate( sample_size=10, demographic_events=demographic_events, random_seed=1) with tempfile.NamedTemporaryFile() as f: self.assertRaises( ValueError, msprime.dump_legacy, ts, f.name)
def test_different_types(self): events = [ msprime.PopulationParametersChange(time=1, initial_size=1), msprime.MigrationRateChange(time=1, rate=1), msprime.MassMigration(time=1, source=1), msprime.SimpleBottleneck(time=1) ] for a, b in itertools.combinations(events, 2): self.assertFalse(models.demographic_events_equal([a], [b], 1)) self.assertFalse(models.demographic_events_equal([b], [a], 1)) self.assertTrue(models.demographic_events_equal([a], [a], 1)) self.assertTrue(models.demographic_events_equal([b], [b], 1))
def get_nonbinary_example(self): ts = msprime.simulate( sample_size=20, recombination_rate=10, random_seed=self.random_seed, demographic_events=[ msprime.SimpleBottleneck(time=0.5, population=0, proportion=1)]) # Make sure this really has some non-binary nodes found = False for e in ts.edgesets(): if len(e.children) > 2: found = True break self.assertTrue(found) return ts
def get_non_binary_tree(n): demographic_events = [ msprime.SimpleBottleneck(time=0.1, population=0, proportion=0.5) ] ts = msprime.simulate(n, demographic_events=demographic_events, random_seed=3) tree = ts.first() found = False for u in tree.nodes(): if len(tree.children(u)) > 2: found = True assert found return tree
def test_models_demographic_events(self): Ne = 10000 ts = msprime.simulate( Ne=Ne, sample_size=10, recombination_rate=0.1, demographic_events=[ msprime.SimulationModelChange(10, msprime.StandardCoalescent(Ne)), msprime.SimpleBottleneck(11, population=0, proportion=1.0), ], random_seed=10, ) for tree in ts.trees(): self.assertEqual(tree.num_roots, 1) self.assertEqual(ts.node(tree.root).time, 11)
def f(time=1, population=1, proportion=1): return msprime.SimpleBottleneck( time=time, population=population, proportion=proportion)
def test_simple_bottleneck(self): examples = [ msprime.SimpleBottleneck(time=10, population=2), msprime.SimpleBottleneck(time=10, population=2, proportion=0.5), ] self.assert_repr_round_trip(examples)