Example #1
0
 def test_instantaneous_bottleneck(self):
     examples = [
         msprime.InstantaneousBottleneck(time=10, population=1),
         msprime.InstantaneousBottleneck(time=10, population=1,
                                         strength=10),
     ]
     self.assert_repr_round_trip(examples)
Example #2
0
 def test_instantaneous_bottleneck_locations(self):
     population_configurations = [
         msprime.PopulationConfiguration(100),
         msprime.PopulationConfiguration(100),
         msprime.PopulationConfiguration(100),
     ]
     strength = 1e6  # Make sure everyone coalescences.
     t1 = 0.0001
     t2 = 0.0002
     t3 = 0.0003
     t4 = 0.0004
     demographic_events = [
         msprime.InstantaneousBottleneck(time=t1, population_id=0, strength=strength),
         msprime.InstantaneousBottleneck(time=t2, population_id=1, strength=strength),
         msprime.InstantaneousBottleneck(time=t3, population_id=2, strength=strength),
         msprime.MassMigration(time=t4, source=2, destination=0),
         msprime.MassMigration(time=t4, source=1, destination=0)
     ]
     ts = msprime.simulate(
         population_configurations=population_configurations,
         demographic_events=demographic_events,
         random_seed=1)
     tree = next(ts.trees())
     self.assertGreater(tree.get_time(tree.get_root()), t4)
     self.assertEqual(tree.get_population(tree.get_root()), 0)
     # The parent of all the samples from each deme should be in that deme.
     for pop in range(3):
         parents = [
             tree.get_parent(u) for u in ts.get_samples(population_id=pop)]
         for v in parents:
             self.assertEqual(tree.get_population(v), pop)
Example #3
0
 def test_multimerger(self):
     rng = msprime.RandomGenerator(1234)
     sim = msprime.simulator_factory(
         100, recombination_rate=0.1, record_full_arg=True,
         random_generator=rng, demographic_events=[
             msprime.InstantaneousBottleneck(time=0.1, population=0, strength=5)])
     self.verify(sim, multiple_mergers=True)
Example #4
0
 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)])
Example #5
0
    def demographic_model(pop_size_domestic_2, pop_size_wild_2, div_time,
                          mig_rate_post_split, mig_length_post_split,
                          bottleneck_time_wild, bottleneck_strength_wild,
                          bottleneck_time_domestic,
                          bottleneck_strength_domestic):
        """Model for recapitation, including bottlenecks, population size changes and migration.
        Returns list of demographic events sorted in time order. Note that if parameters are drawn from priors this
        could have unexpected consequences on the demography. sim.utils.check_params() should mitigate this issue."""
        domestic, wild = 0, 1

        migration_time_2 = div_time - mig_length_post_split

        demographic_events = [
            msprime.PopulationParametersChange(
                time=bottleneck_time_domestic,
                initial_size=pop_size_domestic_2,
                population_id=domestic
            ),  # pop size change executes "before" bottleneck
            msprime.InstantaneousBottleneck(
                time=bottleneck_time_domestic,
                strength=bottleneck_strength_domestic,
                population_id=domestic),
            msprime.PopulationParametersChange(time=bottleneck_time_wild,
                                               initial_size=pop_size_wild_2,
                                               population_id=wild),
            msprime.InstantaneousBottleneck(time=bottleneck_time_wild,
                                            strength=bottleneck_strength_wild,
                                            population_id=wild),
            msprime.MigrationRateChange(time=migration_time_2,
                                        rate=mig_rate_post_split,
                                        matrix_index=(domestic, wild)),
            msprime.MigrationRateChange(time=migration_time_2,
                                        rate=mig_rate_post_split,
                                        matrix_index=(wild, domestic)),
            msprime.MassMigration(time=div_time,
                                  source=domestic,
                                  dest=wild,
                                  proportion=1)
        ]

        demographic_events.sort(
            key=lambda event: event.time,
            reverse=False)  # Ensure time sorted (required by msprime)
        return demographic_events
Example #6
0
 def test_instantaneous_bottleneck(self):
     g = 51
     for population in [0, 1, 5]:
         for strength in [0, 100, 1000, 1e9]:
             event = msprime.InstantaneousBottleneck(
                 time=g, population_id=population, strength=strength)
             d = event.get_ll_representation(1)
             dp = {
                 "time": g,
                 "type": "instantaneous_bottleneck",
                 "population_id": population,
                 "strength": strength}
             self.assertEqual(d, dp)
Example #7
0
File: dev.py Project: td329/msprime
def instantaneous_bottleneck_example():
    demographic_events = [
        msprime.InstantaneousBottleneck(time=1000, strength=1e4)
    ]
    ts = msprime.simulate(sample_size=10,
                          Ne=1e4,
                          recombination_rate=1e-5,
                          length=10,
                          demographic_events=demographic_events)
    for record in ts.records():
        print("{:.2f}-{:.2f}\t{:.1f}".format(record.left, record.right,
                                             record.time),
              record.node,
              record.children,
              sep="\t")
Example #8
0
 def test_demographic_events(self):
     population_configurations = [
         msprime.PopulationConfiguration(10),
         msprime.PopulationConfiguration(10)]
     migration_matrix = [[0, 0], [0, 0]]
     demographic_events = [
         msprime.PopulationParametersChange(0.1, initial_size=2),
         msprime.PopulationParametersChange(0.1, growth_rate=10),
         msprime.MassMigration(0.2, source=1, destination=0),
         msprime.MigrationRateChange(0.2, rate=0),
         msprime.MigrationRateChange(0.4, matrix_index=(0, 1), rate=1),
         msprime.MigrationRateChange(0.4, matrix_index=(1, 0), rate=1),
         msprime.InstantaneousBottleneck(0.5, strength=100)]
     self.verify_debug(
         population_configurations, migration_matrix, demographic_events)
Example #9
0
 def test_instantaneous_bottleneck(self):
     Ne = 0.5
     # Bottleneck occured 0.1 coalescent units ago
     t = 0.1
     population_configurations = [
         msprime.PopulationConfiguration(10),
         msprime.PopulationConfiguration(10),
     ]
     # At this time, we migrate the lineages in 1 to 0, and
     # have a very strong bottleneck, resulting in instant
     # coalescence.
     demographic_events = [
         msprime.MassMigration(time=t, source=1, destination=0),
         msprime.InstantaneousBottleneck(time=t, strength=100)
     ]
     reps = msprime.simulate(
         Ne=Ne,
         population_configurations=population_configurations,
         demographic_events=demographic_events,
         random_seed=1, num_replicates=10)
     for ts in reps:
         tree = next(ts.trees())
         self.assertAlmostEqual(t, tree.get_time(tree.get_root()), places=5)