Beispiel #1
0
 def test_many_sweeps(self):
     sweep_models = [
         msprime.SweepGenicSelection(
             reference_size=0.25,
             position=j,
             start_frequency=0.69,
             end_frequency=0.7,
             alpha=1e-5,
             dt=0.1,
         ) for j in range(10)
     ]
     ts = msprime.simulate(
         10,
         Ne=0.25,
         length=10,
         recombination_rate=0.2,
         demographic_events=[
             msprime.SimulationModelChange(0.01, sweep_models[0])
         ] + [
             msprime.SimulationModelChange(None, model)
             for model in sweep_models
         ] + [msprime.SimulationModelChange()],
         random_seed=2,
     )
     self.assertTrue(all(tree.num_roots == 1 for tree in ts.trees()))
Beispiel #2
0
 def test_too_many_sweeps(self):
     # What happens when we have loads of sweeps
     demographic_events = []
     for j in range(1000):
         sweep_model = msprime.SweepGenicSelection(
             position=0.5,
             start_frequency=0.69,
             end_frequency=0.7,
             alpha=1000,
             dt=0.0125,
         )
         # Start the sweep after 0.1 generations of Hudson
         demographic_events.append(
             msprime.SimulationModelChange(time=lambda t: t + 0.1,
                                           model=sweep_model))
         # Revert back to Hudson until the next sweep
         demographic_events.append(msprime.SimulationModelChange())
     ts = msprime.simulate(
         10,
         Ne=0.25,
         length=10,
         recombination_rate=0.2,
         demographic_events=demographic_events,
         random_seed=2,
     )
     self.assertTrue(all(tree.num_roots == 1 for tree in ts.trees()))
Beispiel #3
0
    def test_sweep_model_change_time_complete(self):
        # Short sweep that doesn't coalesce followed
        # by Hudson phase to finish up coalescent
        sweep_model = msprime.SweepGenicSelection(
            position=0.5, start_frequency=0.69, end_frequency=0.7, alpha=1e5, dt=1
        )
        ts = msprime.simulate(
            10,
            Ne=0.25,
            recombination_rate=2,
            model=[sweep_model, (None, None)],
            random_seed=2,
        )
        assert all(tree.num_roots == 1 for tree in ts.trees())

        # Returning None from a function should be identical
        ts2 = msprime.simulate(
            10,
            Ne=0.25,
            recombination_rate=2,
            model=[
                sweep_model,
                msprime.SimulationModelChange(lambda t: None, "hudson"),
            ],
            random_seed=2,
        )
        self.assertTreeSequencesEqual(ts, ts2)

        # Make sure that the Hudson phase did something.
        ts = msprime.simulate(
            10, Ne=0.25, recombination_rate=2, model=sweep_model, random_seed=2
        )
        assert any(tree.num_roots > 1 for tree in ts.trees())
Beispiel #4
0
 def test_sweep_coalescence_same_seed(self):
     model = msprime.SweepGenicSelection(
         position=0.5, start_frequency=0.6, end_frequency=0.7, alpha=1000, dt=1e4
     )
     ts1 = msprime.simulate(5, model=model, random_seed=2)
     ts2 = msprime.simulate(5, model=model, random_seed=2)
     self.assertTreeSequencesEqual(ts1, ts2)
Beispiel #5
0
    def test_sweep_model_change_time_complete(self):
        # Short sweep that doesn't coalesce followed
        # by Hudson phase to finish up coalescent
        sweep_model = msprime.SweepGenicSelection(position=5,
                                                  start_frequency=0.69,
                                                  end_frequency=0.72,
                                                  s=0.1,
                                                  dt=1e-6)
        ts = msprime.sim_ancestry(
            10,
            population_size=1000,
            sequence_length=10,
            recombination_rate=2,
            model=[sweep_model, "hudson"],
            random_seed=2,
        )
        assert all(tree.num_roots == 1 for tree in ts.trees())

        ts = msprime.sim_ancestry(
            10,
            population_size=1000,
            recombination_rate=2,
            model=sweep_model,
            random_seed=2,
            sequence_length=10,
            discrete_genome=False,
        )
        assert any(tree.num_roots > 1 for tree in ts.trees())
Beispiel #6
0
 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())
Beispiel #7
0
    def test_new_old_style_model_changes_equal(self):
        models = [
            msprime.SweepGenicSelection(
                position=j,
                start_frequency=j,
                end_frequency=j,
                alpha=j,
                dt=j,
            ) for j in range(1, 10)
        ]
        # Old style
        sim = msprime.simulator_factory(
            sample_size=2,
            Ne=10,
            demographic_events=[
                msprime.SimulationModelChange(None, model) for model in models
            ],
        )
        self.assertEqual(len(sim.model_change_events), len(models))
        for event, model in zip(sim.model_change_events, models):
            self.assertEqual(event.model, model)

        sim2 = msprime.simulator_factory(
            sample_size=2,
            Ne=10,
            model=[None] +
            [msprime.SimulationModelChange(None, model) for model in models],
        )
        self.assertEqual(sim.model_change_events, sim2.model_change_events)
Beispiel #8
0
 def test_incorrect_num_labels(self):
     model = msprime.SweepGenicSelection(
         position=0.5, start_frequency=0.1, end_frequency=0.9, alpha=0.1,
         dt=0.01)
     for num_labels in [1, 3, 10]:
         with self.assertRaises(_msprime.LibraryError):
             msprime.simulate(
                 10, recombination_rate=1, model=model, num_labels=num_labels)
Beispiel #9
0
 def test_sweep_coalescence_no_recomb(self):
     model = msprime.SweepGenicSelection(
         position=0.5, start_frequency=0.6, end_frequency=0.7, alpha=0.01,
         dt=0.1)
     ts = msprime.simulate(10, model=model, num_labels=2, random_seed=2)
     self.assertEqual(ts.num_trees, 1)
     for tree in ts.trees():
         self.assertEqual(tree.num_roots, 1)
Beispiel #10
0
 def test_sweep_coalescence_same_seed(self):
     model = msprime.SweepGenicSelection(position=0.5,
                                         start_frequency=0.6,
                                         end_frequency=0.7,
                                         s=0.1,
                                         dt=1e-6)
     ts1 = msprime.sim_ancestry(5, model=model, random_seed=2)
     ts2 = msprime.sim_ancestry(5, model=model, random_seed=2)
     assert ts1.equals(ts2, ignore_provenance=True)
Beispiel #11
0
 def test_sweep_genic_selection(self):
     model = msprime.SweepGenicSelection(position=1,
                                         start_frequency=0.5,
                                         end_frequency=0.9,
                                         s=0.1,
                                         dt=0.01)
     repr_s = ("SweepGenicSelection(position=1, start_frequency=0.5, "
               "end_frequency=0.9, s=0.1, dt=0.01)")
     assert repr(model) == repr_s
     assert str(model) == repr_s
Beispiel #12
0
 def test_sweep_genic_selection(self):
     model = msprime.SweepGenicSelection(position=1,
                                         start_frequency=0.5,
                                         end_frequency=0.9,
                                         alpha=1,
                                         dt=0.01)
     repr_s = ("SweepGenicSelection(position=1, start_frequency=0.5, "
               "end_frequency=0.9, alpha=1, dt=0.01)")
     self.assertEqual(repr(model), repr_s)
     self.assertEqual(str(model), repr_s)
Beispiel #13
0
 def test_model_end_broken(self):
     # Checking that we're correctly detecting the fact that
     # sweeps are non renentrant.
     model = msprime.SweepGenicSelection(position=0.5,
                                         start_frequency=0.1,
                                         end_frequency=0.9,
                                         s=0.01,
                                         dt=0.01)
     with pytest.raises(RuntimeError,
                        match="does not support interruption"):
         msprime.sim_ancestry(10, model=model, end_time=0.0001)
Beispiel #14
0
 def test_sweep_coalescence_recomb(self):
     N = 1e6
     model = msprime.SweepGenicSelection(
         position=0.5,
         start_frequency=1.0 / (2 * N),
         end_frequency=1.0 - (1.0 / (2 * N)),
         alpha=1000,
         dt=1e-6,
     )
     ts = msprime.simulate(10, model=model, recombination_rate=1, random_seed=2)
     assert ts.num_trees > 1
Beispiel #15
0
 def test_sweep_coalescence_same_seed(self):
     model = msprime.SweepGenicSelection(
         position=0.5, start_frequency=0.6, end_frequency=0.7, alpha=0.01,
         dt=0.1)
     ts1 = msprime.simulate(5, model=model, num_labels=2, random_seed=2)
     ts2 = msprime.simulate(5, model=model, num_labels=2, random_seed=2)
     t1 = ts1.dump_tables()
     t2 = ts2.dump_tables()
     t1.provenances.clear()
     t2.provenances.clear()
     self.assertEqual(t1, t2)
Beispiel #16
0
 def test_sweep_start_time_complete(self):
     sweep_model = msprime.SweepGenicSelection(
         reference_size=0.25, position=0.5, start_frequency=0.6,
         end_frequency=0.7, alpha=0.9, dt=0.001)
     t_start = 0.1
     ts = msprime.simulate(
         10, Ne=0.25,
         recombination_rate=2,
         demographic_events=[
             msprime.SimulationModelChange(t_start, sweep_model)],
         num_labels=2, random_seed=2)
     self.assertTrue(all(tree.num_roots == 1 for tree in ts.trees()))
Beispiel #17
0
 def test_sweep_coalescence_recomb(self):
     model = msprime.SweepGenicSelection(position=0.5,
                                         start_frequency=0.01,
                                         end_frequency=0.999,
                                         alpha=1.0,
                                         dt=0.01)
     ts = msprime.simulate(10,
                           model=model,
                           recombination_rate=1,
                           random_seed=2)
     self.assertGreater(ts.num_trees, 1)
     for tree in ts.trees():
         self.assertEqual(tree.num_roots, 1)
Beispiel #18
0
 def test_sweep_start_time_incomplete(self):
     # Short sweep that doesn't make complete coalescence.
     sweep_model = msprime.SweepGenicSelection(
         reference_size=0.25, position=0.5, start_frequency=0.69,
         end_frequency=0.7, alpha=1e-5, dt=1)
     t_start = 0.1
     ts = msprime.simulate(
         10, Ne=0.25,
         recombination_rate=2,
         demographic_events=[
             msprime.SimulationModelChange(t_start, sweep_model)],
         num_labels=2, random_seed=2)
     self.assertTrue(any(tree.num_roots > 1 for tree in ts.trees()))
Beispiel #19
0
 def test_float_position_discrete_genome(self):
     # We can still specify a floating point position if we want, doesn't
     # make any difference.
     model = msprime.SweepGenicSelection(position=0.5,
                                         start_frequency=0.1,
                                         end_frequency=0.9,
                                         s=0.01,
                                         dt=0.01)
     ts = msprime.sim_ancestry(10,
                               sequence_length=1,
                               recombination_rate=20,
                               model=model)
     assert ts.num_trees == 1
Beispiel #20
0
    def test_sweep_genic_selection(self):
        model = msprime.SweepGenicSelection(position=1,
                                            start_frequency=0.5,
                                            end_frequency=0.9,
                                            s=0.1,
                                            dt=0.01)
        assert model.duration is None
        assert model.position == 1
        assert model.start_frequency == 0.5
        assert model.end_frequency == 0.9
        assert model.s == 0.1
        assert model.dt == 0.01

        model = msprime.SweepGenicSelection(
            position=2,
            start_frequency=0.5,
            end_frequency=0.9,
            s=0.1,
            dt=0.01,
            duration=1234,
        )
        assert model.duration == 1234
        assert model.position == 2
        assert model.start_frequency == 0.5
        assert model.end_frequency == 0.9
        assert model.s == 0.1
        assert model.dt == 0.01

        model = msprime.SweepGenicSelection()
        assert model.duration is None
        assert model.position is None
        assert model.start_frequency is None
        assert model.end_frequency is None
        assert model.s is None
        assert model.dt is None

        with pytest.raises(TypeError, match="takes 1 positional"):
            msprime.SweepGenicSelection(1)
Beispiel #21
0
 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, alpha=800, dt=0.001
     )
     t_start = 0.1
     ts = msprime.simulate(
         10,
         Ne=0.25,
         recombination_rate=2,
         model=["hudson", (t_start, sweep_model)],
         random_seed=2,
     )
     assert any(tree.num_roots > 1 for tree in ts.trees())
Beispiel #22
0
    def test_sweep_model_change_time_complete(self):
        # Short sweep that doesn't coalesce followed
        # by Hudson phase to finish up coalescent
        sweep_model = msprime.SweepGenicSelection(
            reference_size=0.25,
            position=0.5,
            start_frequency=0.69,
            end_frequency=0.7,
            alpha=1e-5,
            dt=1,
        )
        ts = msprime.simulate(
            10,
            Ne=0.25,
            recombination_rate=2,
            demographic_events=[
                msprime.SimulationModelChange(0, sweep_model),
                msprime.SimulationModelChange(None, "hudson"),
            ],
            random_seed=2,
        )
        self.assertTrue(all(tree.num_roots == 1 for tree in ts.trees()))

        # Returning None from a function should be identical
        ts2 = msprime.simulate(
            10,
            Ne=0.25,
            recombination_rate=2,
            demographic_events=[
                msprime.SimulationModelChange(0, sweep_model),
                msprime.SimulationModelChange(lambda t: None, "hudson"),
            ],
            random_seed=2,
        )
        t1 = ts.dump_tables()
        t2 = ts2.dump_tables()
        t1.provenances.clear()
        t2.provenances.clear()
        self.assertEqual(t1, t2)

        # Make sure that the Hudson phase did something.
        ts = msprime.simulate(
            10,
            Ne=0.25,
            recombination_rate=2,
            demographic_events=[msprime.SimulationModelChange(0, sweep_model)],
            random_seed=2,
        )
        self.assertTrue(any(tree.num_roots > 1 for tree in ts.trees()))
Beispiel #23
0
 def test_incorrect_num_labels(self):
     model = msprime.SweepGenicSelection(position=0.5,
                                         start_frequency=0.1,
                                         end_frequency=0.9,
                                         s=0.01,
                                         dt=0.01)
     for num_labels in [1, 3, 10]:
         # Not the best error, but this shouldn't be exposed to the user anyway.
         with pytest.raises(_msprime.LibraryError,
                            match="configuration is not supported"):
             msprime.sim_ancestry(
                 10,
                 model=model,
                 num_labels=num_labels,
             )
Beispiel #24
0
 def test_sweep_coalescence_no_recomb(self):
     N = 1e6
     model = msprime.SweepGenicSelection(
         position=0.5,
         start_frequency=1.0 / (2 * N),
         end_frequency=1.0 - (1.0 / (2 * N)),
         alpha=1000,
         dt=1e-6,
     )
     ts = msprime.simulate(10,
                           model=model,
                           recombination_rate=0,
                           random_seed=2)
     self.assertEqual(ts.num_trees, 1)
     for tree in ts.trees():
         self.assertEqual(tree.num_roots, 1)
Beispiel #25
0
 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__)
Beispiel #26
0
 def test_sweep_coalescence_no_recomb(self):
     N = 1e6
     model = msprime.SweepGenicSelection(
         position=0.5,
         start_frequency=1.0 / (2 * N),
         end_frequency=1.0 - (1.0 / (2 * N)),
         s=0.1,
         dt=1e-6,
     )
     ts = msprime.sim_ancestry(10,
                               model=model,
                               population_size=1000,
                               random_seed=2)
     assert ts.num_trees == 1
     for tree in ts.trees():
         assert tree.num_roots == 1
Beispiel #27
0
 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())
Beispiel #28
0
 def test_many_sweeps(self):
     sweep_models = [
         msprime.SweepGenicSelection(position=j,
                                     start_frequency=0.69,
                                     end_frequency=0.7,
                                     s=0.001,
                                     dt=1e-6) for j in range(10)
     ]
     ts = msprime.sim_ancestry(
         10,
         population_size=1000,
         sequence_length=10,
         recombination_rate=0.2,
         model=sweep_models + ["hudson"],
         random_seed=2,
     )
     assert all(tree.num_roots == 1 for tree in ts.trees())
Beispiel #29
0
 def test_sweep_start_time_complete(self):
     sweep_model = msprime.SweepGenicSelection(
         position=0.5,
         start_frequency=0.01,
         end_frequency=0.99,
         alpha=0.9,
         dt=0.001,
     )
     t_start = 0.1
     ts = msprime.simulate(
         10,
         Ne=0.25,
         recombination_rate=2,
         model=[None, (t_start, sweep_model)],
         random_seed=2,
     )
     self.assertTrue(all(tree.num_roots == 1 for tree in ts.trees()))
Beispiel #30
0
 def test_sweep_start_time_complete(self):
     sweep_model = msprime.SweepGenicSelection(
         position=0.5,
         start_frequency=0.01,
         end_frequency=0.99,
         s=0.25,
         dt=1e-6,
     )
     t_start = 0.1
     ts = msprime.simulate(
         10,
         Ne=1000,
         recombination_rate=2,
         model=[None, (t_start, sweep_model), (None, None)],
         random_seed=2,
     )
     assert all(tree.num_roots == 1 for tree in ts.trees())