def test_many_models(self): Ne = 10000 ts = msprime.simulate( Ne=Ne, sample_size=10, recombination_rate=0.1, model=[ "hudson", msprime.SimulationModelChange(10, msprime.StandardCoalescent()), msprime.SimulationModelChange(20, msprime.SmcApproxCoalescent()), msprime.SimulationModelChange( 30, msprime.SmcPrimeApproxCoalescent()), msprime.SimulationModelChange( 40, msprime.DiscreteTimeWrightFisher()), msprime.SimulationModelChange( 50, msprime.BetaCoalescent(alpha=1.1, truncation_point=1), ), msprime.SimulationModelChange(60, msprime.StandardCoalescent()), ], random_seed=10, ) for tree in ts.trees(): self.assertEqual(tree.num_roots, 1)
def test_encode_simulation_models(self): models = [ msprime.StandardCoalescent(duration=10), msprime.DiscreteTimeWrightFisher(duration=10), msprime.SmcApproxCoalescent(duration=10), msprime.StandardCoalescent(), ] ts = msprime.sim_ancestry(10, model=models, random_seed=1234) decoded = self.decode(ts.provenance(0).record) parameters = decoded.parameters assert parameters.model[0] == { "__class__": "msprime.ancestry.StandardCoalescent", "duration": 10, } assert parameters.model[1] == { "__class__": "msprime.ancestry.DiscreteTimeWrightFisher", "duration": 10, } assert parameters.model[2] == { "__class__": "msprime.ancestry.SmcApproxCoalescent", "duration": 10, } assert parameters.model[3] == { "__class__": "msprime.ancestry.StandardCoalescent", "duration": None, }
def test_one_event(self): expected_event = msprime.AncestryModelChange( time=1.33, model=msprime.StandardCoalescent()) model, events = ancestry._parse_model_arg(["dtwf", (1.33, "hudson")]) assert model == msprime.DiscreteTimeWrightFisher() assert events == [expected_event] model, events = ancestry._parse_model_arg(["dtwf", (1.33, None)]) assert model == msprime.DiscreteTimeWrightFisher() assert events == [expected_event] model, events = ancestry._parse_model_arg( ["dtwf", (1.33, msprime.StandardCoalescent())]) assert model == msprime.DiscreteTimeWrightFisher() assert events == [expected_event] model, events = ancestry._parse_model_arg(["dtwf", expected_event]) assert model == msprime.DiscreteTimeWrightFisher() assert events == [expected_event] # We should take a copy of the event. assert events[0] is not expected_event model, events = ancestry._parse_model_arg(["dtwf", (None, None)]) assert model == msprime.DiscreteTimeWrightFisher() assert events == [ msprime.AncestryModelChange(time=None, model=msprime.StandardCoalescent()) ]
def test_single_model_list(self): model, events = ancestry._parse_model_arg([None]) assert model == msprime.StandardCoalescent() assert events == [] # Tuples are also accepted as input. model, events = ancestry._parse_model_arg((None, )) assert model == msprime.StandardCoalescent() assert events == [] model, events = ancestry._parse_model_arg(["hudson"]) assert model == msprime.StandardCoalescent() assert events == [] model, events = ancestry._parse_model_arg( [msprime.StandardCoalescent()]) assert model == msprime.StandardCoalescent() assert events == [] model, events = ancestry._parse_model_arg(["dtwf"]) assert model == msprime.DiscreteTimeWrightFisher() assert events == [] model, events = ancestry._parse_model_arg( [msprime.DiscreteTimeWrightFisher()]) assert model == msprime.DiscreteTimeWrightFisher() assert events == []
def test_one_event(self): expected_event = msprime.SimulationModelChange( time=1.33, model=msprime.StandardCoalescent()) model, events = msprime.parse_model_arg(["dtwf", (1.33, "hudson")]) self.assertEqual(model, msprime.DiscreteTimeWrightFisher()) self.assertEqual(events, [expected_event]) model, events = msprime.parse_model_arg(["dtwf", (1.33, None)]) self.assertEqual(model, msprime.DiscreteTimeWrightFisher()) self.assertEqual(events, [expected_event]) model, events = msprime.parse_model_arg( ["dtwf", (1.33, msprime.StandardCoalescent())]) self.assertEqual(model, msprime.DiscreteTimeWrightFisher()) self.assertEqual(events, [expected_event]) model, events = msprime.parse_model_arg(["dtwf", expected_event]) self.assertEqual(model, msprime.DiscreteTimeWrightFisher()) self.assertEqual(events, [expected_event]) # We should take a copy of the event. self.assertIsNot(events[0], expected_event) model, events = msprime.parse_model_arg(["dtwf", (None, None)]) self.assertEqual(model, msprime.DiscreteTimeWrightFisher()) self.assertEqual( events, [ msprime.SimulationModelChange( time=None, model=msprime.StandardCoalescent()) ], )
def test_many_models_simulate(self): Ne = 10000 ts = msprime.simulate( Ne=Ne, sample_size=10, # Use the old-style SimulationModelChange model=[ "hudson", msprime.SimulationModelChange(10, msprime.StandardCoalescent()), msprime.SimulationModelChange(20, msprime.SmcApproxCoalescent()), msprime.SimulationModelChange( 30, msprime.SmcPrimeApproxCoalescent()), msprime.SimulationModelChange( 40, msprime.DiscreteTimeWrightFisher()), msprime.SimulationModelChange( 50, msprime.BetaCoalescent(alpha=1.1)), msprime.SimulationModelChange(60, msprime.StandardCoalescent()), ], random_seed=10, ) for tree in ts.trees(): assert tree.num_roots == 1
def test_single_model_list(self): model, events = msprime.parse_model_arg([None]) self.assertEqual(model, msprime.StandardCoalescent()) self.assertEqual(events, []) # Tuples are also accepted as input. model, events = msprime.parse_model_arg((None, )) self.assertEqual(model, msprime.StandardCoalescent()) self.assertEqual(events, []) model, events = msprime.parse_model_arg(["hudson"]) self.assertEqual(model, msprime.StandardCoalescent()) self.assertEqual(events, []) model, events = msprime.parse_model_arg([msprime.StandardCoalescent()]) self.assertEqual(model, msprime.StandardCoalescent()) self.assertEqual(events, []) model, events = msprime.parse_model_arg(["dtwf"]) self.assertEqual(model, msprime.DiscreteTimeWrightFisher()) self.assertEqual(events, []) model, events = msprime.parse_model_arg( [msprime.DiscreteTimeWrightFisher()]) self.assertEqual(model, msprime.DiscreteTimeWrightFisher()) self.assertEqual(events, [])
def test_two_changes(self): models = ancestry._parse_model_arg([ msprime.DiscreteTimeWrightFisher(duration=1), msprime.StandardCoalescent(duration=2), msprime.SmcApproxCoalescent(duration=3), ]) assert models == [ msprime.DiscreteTimeWrightFisher(duration=1), msprime.StandardCoalescent(duration=2), msprime.SmcApproxCoalescent(duration=3), ]
def test_single_model(self): models = ancestry._parse_model_arg("hudson") assert models == [msprime.StandardCoalescent()] models = ancestry._parse_model_arg(msprime.StandardCoalescent()) assert models == [msprime.StandardCoalescent()] models = ancestry._parse_model_arg("dtwf") assert models == [msprime.DiscreteTimeWrightFisher()] models = ancestry._parse_model_arg(msprime.DiscreteTimeWrightFisher()) assert models == [msprime.DiscreteTimeWrightFisher()]
def test_reference_size_instance(self): for size in range(1, 10): existing_model = msprime.StandardCoalescent() model = msprime.model_factory(existing_model, reference_size=size) self.assertEqual(model.reference_size, size) existing_model = msprime.StandardCoalescent(None) model = msprime.model_factory(existing_model, reference_size=size) self.assertEqual(model.reference_size, size) # If the size is already set, the model isn't changed. existing_model = msprime.StandardCoalescent(10**4) model = msprime.model_factory(existing_model, reference_size=size) self.assertEqual(model.reference_size, 10**4)
def test_one_change(self): models = ancestry._parse_model_arg( [msprime.DiscreteTimeWrightFisher(duration=1.33), "hudson"]) assert models == [ msprime.DiscreteTimeWrightFisher(duration=1.33), msprime.StandardCoalescent(), ] models = ancestry._parse_model_arg( [msprime.DiscreteTimeWrightFisher(duration=1.33), None]) assert models == [ msprime.DiscreteTimeWrightFisher(duration=1.33), msprime.StandardCoalescent(), ]
def test_wf_hudson_back_and_forth(self): Ne = 100 t1 = 100 t2 = 200 ts = msprime.simulate( sample_size=10, model=msprime.DiscreteTimeWrightFisher(Ne), recombination_rate=0.1, demographic_events=[ msprime.SimulationModelChange(t1, msprime.StandardCoalescent(Ne)), msprime.SimulationModelChange( t2, msprime.DiscreteTimeWrightFisher(Ne)), ], random_seed=2, ) tree = ts.first() self.assertEqual(tree.num_roots, 1) times = ts.tables.nodes.time dtwf_times = times[np.logical_and(times > 0, times < t1, times > t2)] self.assertGreater(dtwf_times.shape[0], 0) self.assertTrue(np.all(dtwf_times == np.floor(dtwf_times))) coalescent_times = times[np.logical_and(times > t1, times < t2)] self.assertGreater(coalescent_times.shape[0], 0) self.assertTrue(np.all(coalescent_times != np.floor(coalescent_times)))
def test_many_models_sim_ancestry(self): ts = msprime.sim_ancestry( samples=10, population_size=10_000, model=[ msprime.StandardCoalescent(duration=10), msprime.SmcApproxCoalescent(duration=10), msprime.SmcPrimeApproxCoalescent(duration=10), msprime.DiscreteTimeWrightFisher(duration=10), msprime.BetaCoalescent(alpha=1.1, duration=10), msprime.StandardCoalescent(), ], random_seed=10, ) for tree in ts.trees(): assert tree.num_roots == 1
def test_wf_hudson_different_specifications(self): Ne = 100 t = 100 ts1 = msprime.simulate( sample_size=10, model=msprime.DiscreteTimeWrightFisher(Ne), recombination_rate=0.1, demographic_events=[ msprime.SimulationModelChange(t, msprime.StandardCoalescent(Ne))], random_seed=2) ts2 = msprime.simulate( sample_size=10, recombination_rate=0.1, Ne=Ne, model="dtwf", demographic_events=[msprime.SimulationModelChange(t, "hudson")], random_seed=2) ts3 = msprime.simulate( sample_size=10, recombination_rate=0.1, Ne=Ne, model="dtwf", demographic_events=[msprime.SimulationModelChange(t)], random_seed=2) t1 = ts1.dump_tables() t2 = ts2.dump_tables() t3 = ts3.dump_tables() t1.provenances.clear() t2.provenances.clear() t3.provenances.clear() self.assertEqual(t1, t2) self.assertEqual(t1, t3)
def test_many_sweeps_regular_times_model_change(self): models = [] for j in range(0, 10): models.extend([ # Start each sweep after 0.01 generations of Hudson msprime.StandardCoalescent(duration=0.01), msprime.SweepGenicSelection( position=j, start_frequency=0.69, end_frequency=0.7, s=0.1, dt=1e-6, ), ]) # Complete the simulation with Hudson models.append("hudson") ts = msprime.sim_ancestry( 3, population_size=1000, sequence_length=10, recombination_rate=0.2, model=models, random_seed=2, ) assert all(tree.num_roots == 1 for tree in ts.trees())
def test_two_events(self): expected_events = [ msprime.SimulationModelChange(time=1, model=msprime.StandardCoalescent()), msprime.SimulationModelChange(time=2, model=msprime.SmcApproxCoalescent()), ] model, events = msprime.parse_model_arg( ["dtwf", (1, "hudson"), (2, "smc")]) self.assertEqual(model, msprime.DiscreteTimeWrightFisher()) self.assertEqual(events, expected_events) model, events = msprime.parse_model_arg( ["dtwf", (1, None), (2, msprime.SmcApproxCoalescent())]) self.assertEqual(model, msprime.DiscreteTimeWrightFisher()) self.assertEqual(events, expected_events) model, events = msprime.parse_model_arg( ["dtwf", expected_events[0], (2, msprime.SmcApproxCoalescent())]) self.assertEqual(model, msprime.DiscreteTimeWrightFisher()) self.assertEqual(events, expected_events) model, events = msprime.parse_model_arg( ["dtwf", expected_events[0], (2, msprime.SmcApproxCoalescent())]) self.assertEqual(model, msprime.DiscreteTimeWrightFisher()) self.assertEqual(events, expected_events) self.assertIsNot(events[0], expected_events[0]) model, events = msprime.parse_model_arg(["dtwf"] + expected_events) self.assertEqual(model, msprime.DiscreteTimeWrightFisher()) self.assertEqual(events, expected_events) self.assertIsNot(events[0], expected_events[0]) self.assertIsNot(events[1], expected_events[1])
def test_two_events(self): expected_events = [ msprime.AncestryModelChange(time=1, model=msprime.StandardCoalescent()), msprime.AncestryModelChange(time=2, model=msprime.SmcApproxCoalescent()), ] model, events = ancestry._parse_model_arg( ["dtwf", (1, "hudson"), (2, "smc")]) assert model == msprime.DiscreteTimeWrightFisher() assert events == expected_events model, events = ancestry._parse_model_arg( ["dtwf", (1, None), (2, msprime.SmcApproxCoalescent())]) assert model == msprime.DiscreteTimeWrightFisher() assert events == expected_events model, events = ancestry._parse_model_arg( ["dtwf", expected_events[0], (2, msprime.SmcApproxCoalescent())]) assert model == msprime.DiscreteTimeWrightFisher() assert events == expected_events model, events = ancestry._parse_model_arg( ["dtwf", expected_events[0], (2, msprime.SmcApproxCoalescent())]) assert model == msprime.DiscreteTimeWrightFisher() assert events == expected_events assert events[0] is not expected_events[0] model, events = ancestry._parse_model_arg(["dtwf"] + expected_events) assert model == msprime.DiscreteTimeWrightFisher() assert events == expected_events assert events[0] is not expected_events[0] assert events[1] is not expected_events[1]
def test_many_models(self): # What happens when we have loads of models models = [ msprime.StandardCoalescent(duration=0.1), msprime.SmcApproxCoalescent(duration=0.1), ] ts = msprime.sim_ancestry(10, model=models * 1000, random_seed=2) assert all(tree.num_roots == 1 for tree in ts.trees())
def test_single_model(self): model, events = msprime.parse_model_arg("hudson") self.assertEqual(model, msprime.StandardCoalescent()) self.assertEqual(events, []) model, events = msprime.parse_model_arg(msprime.StandardCoalescent()) self.assertEqual(model, msprime.StandardCoalescent()) self.assertEqual(events, []) model, events = msprime.parse_model_arg("dtwf") self.assertEqual(model, msprime.DiscreteTimeWrightFisher()) self.assertEqual(events, []) model, events = msprime.parse_model_arg( msprime.DiscreteTimeWrightFisher()) self.assertEqual(model, msprime.DiscreteTimeWrightFisher()) self.assertEqual(events, [])
def sim_msprime_hybrid(sample_size, chrom_length_mb): return sim_msprime( sample_size, chrom_length_mb, model=[ msprime.DiscreteTimeWrightFisher(duration=100), msprime.StandardCoalescent(), ], )
def test_many_models(self): Ne = 10000 ts = msprime.simulate( Ne=Ne, sample_size=10, recombination_rate=0.1, demographic_events=[ msprime.SimulationModelChange(10, msprime.StandardCoalescent(Ne)), msprime.SimulationModelChange(20, msprime.SmcApproxCoalescent(Ne)), msprime.SimulationModelChange(30, msprime.SmcPrimeApproxCoalescent(Ne)), msprime.SimulationModelChange( 40, msprime.DiscreteTimeWrightFisher(100)), msprime.SimulationModelChange( 50, msprime.BetaCoalescent(reference_size=10)), msprime.SimulationModelChange(60, msprime.StandardCoalescent(0.1))], random_seed=10) for tree in ts.trees(): self.assertEqual(tree.num_roots, 1)
def test_many_models_sim_ancestry(self): ts = msprime.sim_ancestry( samples=10, population_size=10_000, model=[ "hudson", msprime.AncestryModelChange(10, msprime.StandardCoalescent()), msprime.AncestryModelChange(20, msprime.SmcApproxCoalescent()), msprime.AncestryModelChange( 30, msprime.SmcPrimeApproxCoalescent()), msprime.AncestryModelChange( 40, msprime.DiscreteTimeWrightFisher()), msprime.AncestryModelChange(50, msprime.BetaCoalescent(alpha=1.1)), msprime.AncestryModelChange(60, msprime.StandardCoalescent()), ], random_seed=10, ) for tree in ts.trees(): assert tree.num_roots == 1
def test_mixed_models_no_recombination(self): ts = msprime.simulate(10, Ne=10, model="dtwf", random_seed=2, __tmp_max_time=100, demographic_events=[ msprime.SimulationModelChange( 10, msprime.StandardCoalescent(1)) ]) self.verify(ts)
def test_encode_simulation_models(self): simple_model = ["hudson", [10, "dtwf"], [20, "smc"], [None, None]] ts = msprime.simulate(10, model=simple_model) decoded = self.decode(ts.provenance(0).record) parameters = decoded.parameters self.assertEqual(parameters.sample_size, 10) self.assertEqual(list(parameters.model), simple_model) model_instances = [ msprime.StandardCoalescent(), msprime.SimulationModelChange(10, msprime.DiscreteTimeWrightFisher()), msprime.SimulationModelChange(20, msprime.SmcApproxCoalescent()), msprime.SimulationModelChange(30, msprime.BetaCoalescent(alpha=1.1)), ] ts = msprime.simulate(10, model=model_instances) decoded = self.decode(ts.provenance(0).record) parameters = decoded.parameters self.assertEqual(parameters.sample_size, 10) self.assertEqual(parameters.model[0], {"__class__": "msprime.ancestry.StandardCoalescent"}) self.assertDictEqual( parameters.model[1], { "__class__": "msprime.ancestry.SimulationModelChange", "model": { "__class__": "msprime.ancestry.DiscreteTimeWrightFisher" }, "time": 10, }, ) self.assertDictEqual( parameters.model[2], { "__class__": "msprime.ancestry.SimulationModelChange", "model": { "__class__": "msprime.ancestry.SmcApproxCoalescent" }, "time": 20, }, ) self.assertDictEqual( parameters.model[3], { "__class__": "msprime.ancestry.SimulationModelChange", "model": { "__class__": "msprime.ancestry.BetaCoalescent", "alpha": 1.1, "truncation_point": 1.0, }, "time": 30, }, )
def test_simulation_models(self): simple_model = ["hudson", [10, "dtwf"], [20, "smc"]] ts = msprime.simulate(10, model=simple_model) self.verify(ts) model_instances = [ msprime.StandardCoalescent(), msprime.SimulationModelChange(10, msprime.DiscreteTimeWrightFisher()), msprime.SimulationModelChange(20, msprime.SmcApproxCoalescent()), msprime.SimulationModelChange(30, msprime.BetaCoalescent(alpha=1.1)), ] ts = msprime.simulate(10, model=model_instances) self.verify(ts)
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 test_model_instances(self): models = [ msprime.StandardCoalescent(100), msprime.SmcApproxCoalescent(30), msprime.SmcPrimeApproxCoalescent(2132), msprime.DiscreteTimeWrightFisher(500), msprime.SweepGenicSelection( reference_size=500, position=0.5, start_frequency=0.1, end_frequency=0.9, alpha=0.1, dt=0.01), msprime.DiracCoalescent(), msprime.BetaCoalescent(), ] for model in models: new_model = msprime.model_factory(model=model) self.assertFalse(new_model is model) self.assertEqual(new_model.__dict__, model.__dict__)
def test_model_instances(self): for bad_type in [1234, {}]: self.assertRaises(TypeError, msprime.simulator_factory, sample_size=2, model=bad_type) models = [ msprime.StandardCoalescent(), msprime.SmcApproxCoalescent(), msprime.SmcPrimeApproxCoalescent(), msprime.BetaCoalescent(), msprime.DiracCoalescent(), ] for model in models: sim = msprime.simulator_factory(sample_size=10, model=model) self.assertEqual(sim.get_model(), model)
def test_sweep_start_time_incomplete(self): # Short sweep that doesn't make complete coalescence. sweep_model = msprime.SweepGenicSelection(position=0.5, start_frequency=0.69, end_frequency=0.7, s=0.01, dt=1e-6) ts = msprime.sim_ancestry( 10, population_size=1000, recombination_rate=2, sequence_length=10, model=[msprime.StandardCoalescent(duration=0.1), sweep_model], random_seed=3, ) assert any(tree.num_roots > 1 for tree in ts.trees())
def test_wf_hudson_single_locus(self): Ne = 100 t = 10 ts = msprime.simulate( sample_size=10, model=msprime.DiscreteTimeWrightFisher(Ne), demographic_events=[ msprime.SimulationModelChange(t, msprime.StandardCoalescent(Ne))], random_seed=2) tree = ts.first() self.assertEqual(tree.num_roots, 1) times = ts.tables.nodes.time dtwf_times = times[np.logical_and(times > 0, times < t)] self.assertGreater(dtwf_times.shape[0], 0) self.assertTrue(np.all(dtwf_times == np.floor(dtwf_times))) coalescent_times = times[times > t] self.assertGreater(coalescent_times.shape[0], 0)