How to use the msprime.Sample 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 / tests / test_zero_waiting_time.py View on Github external
def _ancient_sample_test(self,
            num_modern=1000, anc_pop=0, anc_num=1, anc_time=200,
            split_time_anc=400, Ne0=10000, Ne1=10000, length=1000):
        samples = [msp.Sample(population=0, time=0)]*num_modern
        samples.extend(
            [msp.Sample(population=anc_pop, time=anc_time)]*(2*anc_num))
        pop_config = [msp.PopulationConfiguration(
            initial_size=Ne0), msp.PopulationConfiguration(initial_size=Ne1)]
        divergence = [msp.MassMigration(
            time=split_time_anc, source=1, destination=0, proportion=1.0)]
        seed = 94320219
        sims = msp.simulate(
            samples=samples, Ne=Ne0, population_configurations=pop_config,
            demographic_events=divergence, length=length,
            random_seed=seed)
        return sims
github tskit-dev / msprime / tests / test_demography.py View on Github external
def test_two_samples_start_time(self):
        N = 10
        sampling_time = 10.01 * N
        for start_time in [0, sampling_time / 2, sampling_time, 10000 * sampling_time]:
            ts = msprime.simulate(
                Ne=N,
                start_time=start_time,
                model=self.model,
                random_seed=3,
                samples=[msprime.Sample(0, 0), msprime.Sample(0, sampling_time)])
            nodes = list(ts.nodes())
            self.assertEqual(ts.num_nodes, 3)
            self.assertEqual(nodes[0].time, 0)
            self.assertEqual(nodes[1].time, sampling_time)
            self.assertGreater(nodes[2].time, sampling_time)
            self.assertGreater(nodes[2].time, start_time)
github tskit-dev / msprime / tests / test_models.py View on Github external
def test_wf_hudson_ancient_samples(self):
        Ne = 10
        t = 10
        n = 20
        ts = msprime.simulate(
            samples=[msprime.Sample(time=j, population=0) for j in range(n)],
            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[ts.tables.nodes.flags == 0]
        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)
        self.assertTrue(np.all(coalescent_times != np.floor(coalescent_times)))
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_old_sampling_time(self):
        # This is an enormously long time in coalescent time, so we should
        # coalesce quickly after the samples are introduced.
        N = 100
        sampling_time = N * 100.01
        n = 5
        samples = [
            msprime.Sample(0, sampling_time) for j in range(n - 1)] + [
            msprime.Sample(0, 0)]
        ts = msprime.simulate(Ne=N, samples=samples, model=self.model, random_seed=4)
        time = [node.time for node in ts.nodes()]
        for j in range(n - 1):
            self.assertEqual(time[j], sampling_time)
        self.assertEqual(time[n - 1], 0)
        # Allow it to be within 10 coalescent time units.
        self.assertLess(time[-1], sampling_time + 10 * N)
github tskit-dev / msprime / docs / examples.py View on Github external
def historical_samples_example():
    samples = [
        msprime.Sample(population=0, time=0),
        msprime.Sample(0, 0),  # Or, we can use positional arguments.
        msprime.Sample(0, 1.0)
    ]
    tree_seq = msprime.simulate(samples=samples, random_seed=5)
    tree = next(tree_seq.trees())
    for u in range(tree_seq.get_num_nodes()):
        print(u, tree.get_parent(u), tree.get_time(u), sep="\t")
github tskit-dev / msprime / docs / examples.py View on Github external
def historical_samples_example():
    samples = [
        msprime.Sample(population=0, time=0),
        msprime.Sample(0, 0),  # Or, we can use positional arguments.
        msprime.Sample(0, 1.0)
    ]
    tree_seq = msprime.simulate(samples=samples, random_seed=5)
    tree = next(tree_seq.trees())
    for u in range(tree_seq.get_num_nodes()):
        print(u, tree.get_parent(u), tree.get_time(u), sep="\t")
github popgenmethods / momi2 / momi / demography.py View on Github external
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)