How to use the msprime.PopulationConfiguration function in msprime

To help you get started, we’ve selected a few msprime examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github tskit-dev / msprime / tskit_tests / test_highlevel.py View on Github external
def get_tree_sequence(self, num_demes=4):
        n = 40
        return msprime.simulate(
            samples=[
                msprime.Sample(time=0, population=j % num_demes) for j in range(n)],
            population_configurations=[
                msprime.PopulationConfiguration() for _ in range(num_demes)],
            migration_matrix=[
                [int(j != k) for j in range(num_demes)] for k in range(num_demes)],
            random_seed=1,
            mutation_rate=10)
github tskit-dev / msprime / tests / test_demography.py View on Github external
def test_equal_after(self):
        population_configurations = [
            msprime.PopulationConfiguration(sample_size=10),
            msprime.PopulationConfiguration(sample_size=20)]
        msprime.DemographyDebugger(
            population_configurations=population_configurations)
        self.assertEqual(population_configurations[0].sample_size, 10)
        self.assertEqual(population_configurations[1].sample_size, 20)
github tskit-dev / msprime / tests / test_demography.py View on Github external
def test_full_arg_migration(self):
        population_configurations = [
            msprime.PopulationConfiguration(10),
            msprime.PopulationConfiguration(10),
        ]
        ts = msprime.simulate(
            population_configurations=population_configurations,
            migration_matrix=[
                [0, 1],
                [1, 0]],
            random_seed=1, recombination_rate=0.1,
            record_migrations=True, record_full_arg=True)
        self.verify_two_pops_full_arg(ts)
github tskit-dev / msprime / tests / test_mutations.py View on Github external
def test_populations(self):
        ts = msprime.simulate(
            population_configurations=[
                msprime.PopulationConfiguration(10),
                msprime.PopulationConfiguration(10)],
            migration_matrix=[[0, 1], [1, 0]],
            record_migrations=True,
            random_seed=1)
        mutated = msprime.mutate(ts, 0)
        t1 = ts.dump_tables()
        self.assertEqual(len(t1.populations), 2)
        self.assertGreater(len(t1.migrations), 0)
        t2 = mutated.dump_tables()
        self.verify_topology(t1, t2)
        self.verify_provenance(t1, t2)
        self.assertEqual(t1.sites, t2.sites)
        self.assertEqual(t1.mutations, t2.mutations)
github tskit-dev / msprime / tests / test_demography.py View on Github external
def test_simple_case(self):
        md = {"x": "y"}
        ts = msprime.simulate(
            population_configurations=[
                msprime.PopulationConfiguration(2, metadata=md)],
            random_seed=1)
        self.assertEqual(ts.num_populations, 1)
        pop = ts.population(0)
        self.assertEqual(md, json.loads(pop.metadata.decode()))
github tskit-dev / msprime / tskit_tests / test_highlevel.py View on Github external
def test_simplify_migrations_fails(self):
        ts = msprime.simulate(
            population_configurations=[
                msprime.PopulationConfiguration(10),
                msprime.PopulationConfiguration(10)],
            migration_matrix=[[0, 1], [1, 0]],
            random_seed=2,
            record_migrations=True)
        self.assertGreater(ts.num_migrations, 0)
        # We don't support simplify with migrations, so should fail.
        with self.assertRaises(_tskit.LibraryError):
            ts.simplify()
github tskit-dev / msprime / verification.py View on Github external
self.check_slim_version()
        assert len(sample_sizes) == len(initial_sizes)

        num_pops = len(sample_sizes)
        slim_args = {}

        if num_replicates is None:
            num_replicates = 200

        slim_args['sample_sizes'] = sample_sizes

        population_configurations = []
        slim_args['POP_STRS'] = ''
        for i in range(len(sample_sizes)):
            population_configurations.append(
                    msprime.PopulationConfiguration(
                        sample_size=sample_sizes[i],
                        initial_size=initial_sizes[i],
                        growth_rate=0
                        )
                    )
            slim_args['POP_STRS'] += "sim.addSubpop('p{i}', {N});\n".format(
                    i=i, N=initial_sizes[i])

        if migration_matrix is None:
            default_mig_rate = 0.01
            migration_matrix = []
            for i in range(num_pops):
                row = [default_mig_rate] * num_pops
                row[i] = 0
                migration_matrix.append(row)
github mcveanlab / treeseq-inference / src / plots.py View on Github external
r_EU = 0.004
            r_AS = 0.0055
            N_EU = N_EU0 / math.exp(-r_EU * T_EU_AS)
            N_AS = N_AS0 / math.exp(-r_AS * T_EU_AS)
            # Migration rates during the various epochs.
            m_AF_B = 25e-5
            m_AF_EU = 3e-5
            m_AF_AS = 1.9e-5
            m_EU_AS = 9.6e-5
            # Population IDs correspond to their indexes in the population
            # configuration array. Therefore, we have 0=YRI, 1=CEU and 2=CHB
            # initially.
            population_configurations = [
                msprime.PopulationConfiguration(
                    sample_size= nYRI, initial_size=N_AF),
                msprime.PopulationConfiguration(
                    sample_size= nCEU, initial_size=N_EU, growth_rate=r_EU),
                msprime.PopulationConfiguration(
                    sample_size= nCHB, initial_size=N_AS, growth_rate=r_AS)
            ]
            migration_matrix = [
                [      0, m_AF_EU, m_AF_AS],
                [m_AF_EU,       0, m_EU_AS],
                [m_AF_AS, m_EU_AS,       0],
            ]
            demographic_events = [
                # CEU and CHB merge into B with rate changes at T_EU_AS
                msprime.MassMigration(
                    time=T_EU_AS, source=2, destination=1, proportion=1.0),
                msprime.MigrationRateChange(time=T_EU_AS, rate=0),
                msprime.MigrationRateChange(
                    time=T_EU_AS, rate=m_AF_B, matrix_index=(0, 1)),
github tskit-dev / msprime / dev.py View on Github external
def pop_example():
    if False:
        t = 100
        ts = msprime.simulate(
            Ne=10**4,
            population_configurations=[
                msprime.PopulationConfiguration(sample_size=1000),
                msprime.PopulationConfiguration(sample_size=1000),
                msprime.PopulationConfiguration(sample_size=1000),
                msprime.PopulationConfiguration(sample_size=1000),
                msprime.PopulationConfiguration(sample_size=1000)],
            demographic_events=[
                msprime.MassMigration(time=t, source=1, destination=0),
                msprime.MassMigration(time=t, source=2, destination=0),
                msprime.MassMigration(time=t, source=3, destination=0),
                msprime.MassMigration(time=t, source=4, destination=0)],
            length=100 * 1e6,
            recombination_rate=2e-8,
            mutation_rate=2e-8,
            random_seed=1)
        ts.dump("populations.hdf5")
        print(
            ts.get_sample_size(), ts.get_num_trees(),
            ts.get_num_mutations())
    else:
github popgenmethods / momi2 / momi / demography.py View on Github external
sampled_t = self.sampled_t
        if sampled_t is None:
            sampled_t = 0.0
        sampled_t = np.array(sampled_t) * np.ones(len(self.sampled_pops))

        pops = {p: i for i, p in enumerate(self.sampled_pops)}

        demographic_events = []
        for e in self._G.graph["events"]:
            e = e.get_msprime_event(self._G.graph["params"], pops)
            if e is not None:
                demographic_events.append(e)

        return msprime.simulate(
            population_configurations=[
                msprime.PopulationConfiguration()
                for _ in range(len(pops))],
            Ne=self.default_N / 4,
            demographic_events=demographic_events,
            samples=[
                msprime.Sample(population=pops[p], time=t)
                for p, t, n in zip(
                        self.sampled_pops, self.sampled_t,
                        self.sampled_n)
                for _ in range(n)],
            **kwargs)