Ejemplo n.º 1
0
    def test_mean_recombination_rate(self):
        # Some quick sanity checks.
        recomb_map = msprime.RecombinationMap([0, 1], [1, 0])
        mean_rr = recomb_map.mean_recombination_rate
        self.assertEqual(mean_rr, 1.0)

        recomb_map = msprime.RecombinationMap([0, 1, 2], [1, 0, 0])
        mean_rr = recomb_map.mean_recombination_rate
        self.assertEqual(mean_rr, 0.5)

        recomb_map = msprime.RecombinationMap([0, 1, 2], [0, 0, 0])
        mean_rr = recomb_map.mean_recombination_rate
        self.assertEqual(mean_rr, 0.0)
Ejemplo n.º 2
0
    def test_old_recapitation(self, recipe):
        ts = recipe["ts"]
        if ts.num_populations <= 2:
            # if not we need migration rates
            recomb_rate = 1.0 / ts.sequence_length
            with pytest.warns(FutureWarning):
                recap = ts.recapitate(recombination_rate=recomb_rate)
            # there should be no new mutations
            assert ts.num_mutations == recap.num_mutations
            assert ts.num_sites == recap.num_sites
            assert list(ts.tables.sites.position) == list(recap.tables.sites.position)
            self.check_recap_consistency(ts, recap)

            with pytest.warns(FutureWarning):
                recap = ts.recapitate(recombination_rate=recomb_rate, Ne=1e-6)
            self.check_recap_consistency(ts, recap)
            if ts.metadata['SLiM']['generation'] < 200:
                for t in recap.trees():
                    assert abs(recap.node(t.root).time - recap.metadata['SLiM']['generation']) < 1e-4

            # test with passing in a recombination map
            recombination_map = msprime.RecombinationMap(
                       positions = [0.0, ts.sequence_length],
                       rates = [recomb_rate, 0.0],
                       num_loci=int(ts.sequence_length))
            with pytest.warns(FutureWarning):
                    recap = ts.recapitate(recombination_map=recombination_map, Ne=1e-6)
            self.check_recap_consistency(ts, recap)
Ejemplo n.º 3
0
    def test_slice(self):
        # test RecombinationMap.slice(..., trim=False)
        a = msprime.RecombinationMap([0, 100, 200, 300, 400], [0, 1, 2, 3, 0])
        b = a.slice()
        self.assertEqual(a.get_sequence_length(), b.get_sequence_length())
        self.assertTrue(np.array_equal(a.get_positions(), b.get_positions()))
        self.assertTrue(np.array_equal(a.get_rates(), b.get_rates()))

        b = a.slice(start=50)
        self.assertEqual(a.get_sequence_length(), b.get_sequence_length())
        self.assertTrue(
            np.array_equal([0, 100, 200, 300, 400], b.get_positions()))
        self.assertTrue(np.array_equal([0, 1, 2, 3, 0], b.get_rates()))

        b = a.slice(start=100)
        self.assertEqual(a.get_sequence_length(), b.get_sequence_length())
        self.assertTrue(
            np.array_equal([0, 100, 200, 300, 400], b.get_positions()))
        self.assertTrue(np.array_equal([0, 1, 2, 3, 0], b.get_rates()))

        b = a.slice(start=150)
        self.assertEqual(a.get_sequence_length(), b.get_sequence_length())
        self.assertTrue(
            np.array_equal([0, 150, 200, 300, 400], b.get_positions()))
        self.assertTrue(np.array_equal([0, 1, 2, 3, 0], b.get_rates()))

        b = a.slice(end=300)
        self.assertEqual(a.get_sequence_length(), b.get_sequence_length())
        self.assertTrue(
            np.array_equal([0, 100, 200, 300, 400], b.get_positions()))
        self.assertTrue(np.array_equal([0, 1, 2, 0, 0], b.get_rates()))

        b = a.slice(end=250)
        self.assertEqual(a.get_sequence_length(), b.get_sequence_length())
        self.assertTrue(
            np.array_equal([0, 100, 200, 250, 400], b.get_positions()))
        self.assertTrue(np.array_equal([0, 1, 2, 0, 0], b.get_rates()))

        b = a.slice(start=50, end=300)
        self.assertEqual(a.get_sequence_length(), b.get_sequence_length())
        self.assertTrue(
            np.array_equal([0, 100, 200, 300, 400], b.get_positions()))
        self.assertTrue(np.array_equal([0, 1, 2, 0, 0], b.get_rates()))

        b = a.slice(start=150, end=250)
        self.assertEqual(a.get_sequence_length(), b.get_sequence_length())
        self.assertTrue(
            np.array_equal([0, 150, 200, 250, 400], b.get_positions()))
        self.assertTrue(np.array_equal([0, 1, 2, 0, 0], b.get_rates()))

        b = a.slice(start=150, end=300)
        self.assertEqual(a.get_sequence_length(), b.get_sequence_length())
        self.assertTrue(
            np.array_equal([0, 150, 200, 300, 400], b.get_positions()))
        self.assertTrue(np.array_equal([0, 1, 2, 0, 0], b.get_rates()))

        b = a.slice(start=150, end=160)
        self.assertEqual(a.get_sequence_length(), b.get_sequence_length())
        self.assertTrue(np.array_equal([0, 150, 160, 400], b.get_positions()))
        self.assertTrue(np.array_equal([0, 1, 0, 0], b.get_rates()))
Ejemplo n.º 4
0
 def test_unsupported_methods(self):
     recomb_map = msprime.RecombinationMap([0, 10], [0.2, 0])
     self.assertRaises(ValueError, recomb_map.get_num_loci)
     self.assertRaises(ValueError, recomb_map.physical_to_discrete_genetic,
                       8)
     self.assertRaises(ValueError,
                       recomb_map.get_per_locus_recombination_rate)
Ejemplo n.º 5
0
    def verify_coordinate_conversion(self, positions, rates):
        """
        Verifies coordinate conversions by the specified RecombinationMap
        instance.
        """
        L = positions[-1]
        rm = msprime.RecombinationMap(positions, rates)
        other_rm = PythonRecombinationMap(positions, rates)

        assert (rm.get_total_recombination_rate() ==
                other_rm.get_total_recombination_rate())
        num_random_trials = 10
        num_systematic_trials = 10
        values = [L * random.random() for j in range(num_random_trials)]
        for j in range(num_systematic_trials):
            values.append(L * j / num_systematic_trials)
        values += positions
        for x in values:
            # x is a physical coordinate
            y = rm.physical_to_genetic(x)
            self.assertAlmostEqual(y,
                                   other_rm.physical_to_genetic(x),
                                   delta=1e-10)
            assert 0 <= y <= rm.get_total_recombination_rate()
            # Check if we can round trip approximately in real coordinates.
            xp = rm.genetic_to_physical(y)
            self.assertAlmostEqual(x, xp)
            # The different implementations might differ by very small amounts.
            self.assertAlmostEqual(xp, other_rm.genetic_to_physical(y))
Ejemplo n.º 6
0
    def recapitate(self,
                   recombination_rate,
                   keep_first_generation=False,
                   population_configurations=None,
                   **kwargs):
        '''
        Returns a "recapitated" tree sequence, by using msprime to run a
        coalescent simulation from the "top" of this tree sequence, i.e.,
        allowing any uncoalesced lineages to coalesce. 

        To allow this process, the first generation of the SLiM simulation has been
        recorded in the tree sequence, but are not currently marked as samples,
        so this process (or, simplify()) will remove any of these that are not needed.
        If you want to keep them, then set ``keep_first_generation`` to True;
        although this will make more work here.

        Note that ``Ne`` is not set automatically, so defaults to ``1.0``; you probably
        want to set it explicitly.  Similarly, migration is not set up
        automatically, so that if there are uncoalesced lineages in more than
        one population, you will need to pass in a migration matrix to allow
        coalescence. In both cases, remember that population IDs in ``tskit`` begin
        with 0, so that if your SLiM simulation has populations ``p1`` and ``p2``,
        then the tree sequence will have three populations (but with no nodes
        assigned to population 0), so that migration rate of 1.0 between ``p1`` and
        ``p2`` needs a migration matrix of
           [[0.0, 0.0, 0.0], [0.0, 0.0, 1.0], [0.0, 1.0, 0.0]]

        :param float recombination_rate: The recombination rate - only a constant 
            recombination rate is allowed.
        :param bool keep_first_generation: Whether to keep the individuals (and genomes)
            corresponding to the first SLiM generation in the resulting tree sequence
        :param list population_configurations: See :meth:`msprime.simulate()` for
            this argument; if not provided, each population will have zero growth rate
            and the same effective population size.
        :param dict kwargs: Any other arguments to :meth:`msprime.simulate()`.
        '''
        recomb = msprime.RecombinationMap(
            positions=[0.0, self.sequence_length],
            rates=[recombination_rate, 0.0],
            num_loci=int(self.sequence_length))

        if population_configurations is None:
            population_configurations = [
                msprime.PopulationConfiguration()
                for _ in range(self.num_populations)
            ]

        if keep_first_generation:
            ts = self._mark_first_generation()
        else:
            ts = self

        recap = msprime.simulate(
            from_ts=ts,
            population_configurations=population_configurations,
            recombination_map=recomb,
            start_time=self.slim_generation,
            **kwargs)
        ts = SlimTreeSequence.load_tables(recap.tables)
        return ts
Ejemplo n.º 7
0
    def test_getitem_slice_with_negative_indexes_and_floats(self):
        # test RecombinationMap slice syntax with negative indexes and floats
        a = msprime.RecombinationMap([0, 100, 200, 300, 400], [0, 1, 2, 3, 0])

        b = a[150:250]
        c = a[150:-150]
        self.assertTrue(np.array_equal(b.get_positions(), c.get_positions()))
        self.assertTrue(np.array_equal(b.get_rates(), c.get_rates()))

        b = a[150:250]
        c = a[-250:250]
        self.assertTrue(np.array_equal(b.get_positions(), c.get_positions()))
        self.assertTrue(np.array_equal(b.get_rates(), c.get_rates()))

        b = a[150:250]
        c = a[-250:-150]
        self.assertTrue(np.array_equal(b.get_positions(), c.get_positions()))
        self.assertTrue(np.array_equal(b.get_rates(), c.get_rates()))

        b = a[:-np.pi]
        c = a[:400 - np.pi]
        self.assertTrue(np.array_equal(b.get_positions(), c.get_positions()))
        self.assertTrue(np.array_equal(b.get_rates(), c.get_rates()))

        b = a[-50 * np.pi:-np.pi]
        c = a[400 - 50 * np.pi:400 - np.pi]
        self.assertTrue(np.array_equal(b.get_positions(), c.get_positions()))
        self.assertTrue(np.array_equal(b.get_rates(), c.get_rates()))
Ejemplo n.º 8
0
 def test_unsupported_methods(self):
     recomb_map = msprime.RecombinationMap([0, 10], [0.2, 0])
     with pytest.raises(ValueError):
         recomb_map.get_num_loci()
     with pytest.raises(ValueError):
         recomb_map.physical_to_discrete_genetic(8)
     with pytest.raises(ValueError):
         recomb_map.get_per_locus_recombination_rate()
Ejemplo n.º 9
0
 def test_one_rate(self):
     for rate in [0.1, 1.0, 10]:
         for L in [0.1, 1, 10, 1024, 1e6]:
             positions = [0, L]
             rates = [rate, 0]
             rm = msprime.RecombinationMap(positions, rates)
             assert rate * L == rm.get_total_recombination_rate()
             self.verify_coordinate_conversion(positions, rates)
Ejemplo n.º 10
0
def no_hybridization(positions, rates, mu=7.7e-07):
    """
    num    cyd  mel-W mel-E
     |      |     |     |
     |      |     |     |
     |      |     |-----|
     |      |           |
     |      |           |
     |      |-----------|
     |                  |
     |                  |
     |                  |
     |                  |
     |------------------|
               |


    """
    # Coalescent units are the number of 2N generations
    time_units = 2.0 * 2000.0

    # Divergence times
    tau1 = 0.5
    tau2 = 1.5
    tau3 = 4.0

    # Recombination rates
    rmap = msp.RecombinationMap(positions, rates)

    # Set up population configurations
    population_configurations = [
        msp.PopulationConfiguration(sample_size=10, initial_size=2000),
        msp.PopulationConfiguration(sample_size=10, initial_size=2000),
        msp.PopulationConfiguration(sample_size=10, initial_size=2000),
        msp.PopulationConfiguration(sample_size=4, initial_size=2000)
    ]

    # Set up demographic events
    demographic_events = [
        msp.MassMigration(time=tau1 * time_units,
                          source=0,
                          destination=1,
                          proportion=1.0),
        msp.MassMigration(time=tau2 * time_units,
                          source=1,
                          destination=2,
                          proportion=1.0),
        msp.MassMigration(time=tau3 * time_units,
                          source=2,
                          destination=3,
                          proportion=1.0)
    ]

    return ([time_units, tau1, tau2, tau3, mu],
            msp.simulate(mutation_rate=mu,
                         recombination_map=rmap,
                         population_configurations=population_configurations,
                         demographic_events=demographic_events))
Ejemplo n.º 11
0
    def test_mean_recombination_rate(self):
        # Some quick sanity checks.
        recomb_map = msprime.RecombinationMap([0, 1], [1, 0])
        mean_rr = recomb_map.mean_recombination_rate
        assert mean_rr == 1.0

        recomb_map = msprime.RecombinationMap([0, 1, 2], [1, 0, 0])
        mean_rr = recomb_map.mean_recombination_rate
        assert mean_rr == 0.5

        recomb_map = msprime.RecombinationMap([0, 1, 2], [0, 0, 0])
        mean_rr = recomb_map.mean_recombination_rate
        assert mean_rr == 0.0

        # Test mean_recombination_rate is correct after reading from
        # a hapmap file. read_hapmap() ignores the cM
        # field, so here we test against using the cM field directly.
        def hapmap_rr(hapmap_file):
            first_pos = 0
            with open(hapmap_file) as f:
                next(f)  # skip header
                for line in f:
                    pos, rate, cM = map(float, line.split()[1:4])
                    if cM == 0:
                        first_pos = pos
            return cM / 100 / (pos - first_pos)

        hapmap = """chr pos        rate                    cM
                    1   4283592    3.79115663174456        0
                    1   4361401    0.0664276817058413      0.294986106359414
                    1   7979763   10.9082897515584         0.535345505591925
                    1   8007051    0.0976780648822495      0.833010916332456
                    1   8762788    0.0899929572085616      0.906829844052373
                    1   9477943    0.0864382908650907      0.971188757364862
                    1   9696341    4.76495005895746        0.990066707213216
                    1   9752154    0.0864316558730679      1.25601286485381
                    1   9881751    0.0                     1.26721414815999"""
        with tempfile.TemporaryDirectory() as temp_dir:
            hapfile = os.path.join(temp_dir, "hapmap.txt")
            with open(hapfile, "w") as f:
                f.write(hapmap)
            recomb_map = msprime.RateMap.read_hapmap(f.name)
            mean_rr = recomb_map.mean_rate
            mean_rr2 = hapmap_rr(hapfile)
        self.assertAlmostEqual(mean_rr, mean_rr2, places=15)
Ejemplo n.º 12
0
 def test_one_rate(self):
     for num_loci in [1, 10, 1024, 2**31 - 1]:
         for rate in [0.1, 1.0, 10]:
             for L in [0.1, 1, 10, 1024, 1e6]:
                 positions = [0, L]
                 rates = [rate, 0]
                 rm = msprime.RecombinationMap(positions, rates, num_loci)
                 self.assertEqual(rate * L, rm.get_total_recombination_rate())
                 self.verify_coordinate_conversion(positions, rates, num_loci)
Ejemplo n.º 13
0
def get_recombination_map(num_chroms, rho=1e-8):
    positions, rates = get_positions_rates(num_chroms, rho)

    ## HACK: Discretization hack to avoid overflow for WG
    num_loci = int(positions[-1] / 100)

    recombination_map = msprime.RecombinationMap(positions, rates, num_loci)

    return recombination_map
Ejemplo n.º 14
0
 def test_random_recombination_map(self):
     np.random.seed(100)
     k = 15
     position = np.random.random(k) * 100
     position[0] = 0
     position.sort()
     rate = np.random.random(k)
     rate[-1] = 0
     recomb_map = msprime.RecombinationMap(list(position), list(rate))
     self.verify_simple_model(10, 23, recombination_map=recomb_map)
Ejemplo n.º 15
0
    def test_getitem_slice(self):
        """
        test RecombinationMap slice syntax
        """
        a = msprime.RecombinationMap([0, 100, 200, 300, 400], [0, 1, 2, 3, 0])
        b = a[:]
        self.assertEqual(a.get_sequence_length(), b.get_sequence_length())
        self.assertTrue(np.array_equal(a.get_positions(), b.get_positions()))
        self.assertTrue(np.array_equal(a.get_rates(), b.get_rates()))

        b = a[50:]
        self.assertEqual(350, b.get_sequence_length())
        self.assertTrue(
            np.array_equal([0, 50, 150, 250, 350], b.get_positions()))
        self.assertTrue(np.array_equal([0, 1, 2, 3, 0], b.get_rates()))

        b = a[100:]
        self.assertEqual(300, b.get_sequence_length())
        self.assertTrue(np.array_equal([0, 100, 200, 300], b.get_positions()))
        self.assertTrue(np.array_equal([1, 2, 3, 0], b.get_rates()))

        b = a[150:]
        self.assertEqual(250, b.get_sequence_length())
        self.assertTrue(np.array_equal([0, 50, 150, 250], b.get_positions()))
        self.assertTrue(np.array_equal([1, 2, 3, 0], b.get_rates()))

        b = a[:300]
        self.assertEqual(300, b.get_sequence_length())
        self.assertTrue(np.array_equal([0, 100, 200, 300], b.get_positions()))
        self.assertTrue(np.array_equal([0, 1, 2, 0], b.get_rates()))

        b = a[:250]
        self.assertEqual(250, b.get_sequence_length())
        self.assertTrue(np.array_equal([0, 100, 200, 250], b.get_positions()))
        self.assertTrue(np.array_equal([0, 1, 2, 0], b.get_rates()))

        b = a[50:300]
        self.assertEqual(250, b.get_sequence_length())
        self.assertTrue(np.array_equal([0, 50, 150, 250], b.get_positions()))
        self.assertTrue(np.array_equal([0, 1, 2, 0], b.get_rates()))

        b = a[100:300]
        self.assertEqual(200, b.get_sequence_length())
        self.assertTrue(np.array_equal([0, 100, 200], b.get_positions()))
        self.assertTrue(np.array_equal([1, 2, 0], b.get_rates()))

        b = a[150:250]
        self.assertEqual(100, b.get_sequence_length())
        self.assertTrue(np.array_equal([0, 50, 100], b.get_positions()))
        self.assertTrue(np.array_equal([1, 2, 0], b.get_rates()))

        b = a[150:160]
        self.assertEqual(10, b.get_sequence_length())
        self.assertTrue(np.array_equal([0, 10], b.get_positions()))
        self.assertTrue(np.array_equal([1, 0], b.get_rates()))
Ejemplo n.º 16
0
def sample_rates(mu_rho, nsite):
    currpos = 0
    positions = []
    rho = []
    mu = []
    ## ensure that we don't "wrap around"; find the last index that we can start from
    last_idx = len(mu_rho)
    len_end = 0
    while (len_end < nsite):
        last_idx -= 1
        len_end += (mu_rho.end[last_idx] - mu_rho.start[last_idx])
    idx = random.randrange(last_idx)
    print(idx, last_idx, len_end)
    used_rates = pandas.DataFrame(
        columns=["chrom", "start", "end", "mu", "rho"])
    while (currpos < nsite):
        if options.recombRate != None:
            rho.append(options.recombRate)
        else:
            rho.append(mu_rho.rho[idx])
        mu.append(mu_rho.mu[idx])
        positions.append(currpos)
        df2 = pandas.DataFrame([[
            mu_rho.chrom[idx], mu_rho.start[idx], mu_rho.end[idx],
            mu_rho.mu[idx], mu_rho.rho[idx]
        ]],
                               columns=["chrom", "start", "end", "mu", "rho"])
        used_rates = used_rates.append(df2)
        currpos += (mu_rho.end[idx] - mu_rho.start[idx])
        idx += 1
        if (idx == len(mu_rho)):
            print("Error! sample_rates should not wrap around")
            exit
    positions.append(nsite)
    rho.append(0)
    mu.append(0)
    used_rates.to_csv(options.outdir + "/used_rates.bed",
                      sep="\t",
                      header=False,
                      index=False)
    return (msprime.RecombinationMap(positions=positions, rates=mu),
            msprime.RecombinationMap(positions=positions, rates=rho))
Ejemplo n.º 17
0
 def test_no_recombination_interval(self):
     positions = [0, 50, 100, 150, 200]
     rates = [0.01, 0.0, 0.1, 0.005, 0.0]
     recombination_map = msprime.RecombinationMap(positions, rates)
     ts = msprime.simulate(
         10, Ne=10, model="dtwf", random_seed=2, recombination_map=recombination_map
     )
     for tree in ts.trees():
         left, right = tree.interval
         assert left < 50 or left > 100
         assert right < 50 or right > 100
Ejemplo n.º 18
0
def make_rec_map(n_chrom, len_chrom, recombination_rate):
    n_chrom = int(n_chrom)
    len_chrom = int(len_chrom)
    A = [[x, x + 1]
         for x in range(int(len_chrom),
                        int(len_chrom) * (n_chrom + 1), int(len_chrom))]
    rm_pos = [0] + list(itertools.chain.from_iterable(A))
    B = [recombination_rate, 0.5] * n_chrom
    rec_rates = B + [0]
    rec_map = msprime.RecombinationMap(rm_pos, rec_rates)
    return (rec_map)
Ejemplo n.º 19
0
 def test_zero_recombination_map(self):
     # test that beginning and trailing zero recombination regions in the
     # recomb map are included in the sequence
     for n in range(3, 10):
         positions = list(range(n))
         rates = [0.0, 0.2] + [0.0] * (n - 2)
         recomb_map = msprime.RecombinationMap(positions, rates)
         ts = msprime.simulate(10, recombination_map=recomb_map)
         assert ts.sequence_length == n - 1
         assert min(ts.tables.edges.left) == 0.0
         assert max(ts.tables.edges.right) == n - 1.0
Ejemplo n.º 20
0
 def test_slice_error(self):
     recomb_map = msprime.RecombinationMap([0, 100], [1, 0])
     with self.assertRaises(IndexError):
         recomb_map.slice(start=-1)
     with self.assertRaises(IndexError):
         recomb_map.slice(end=-1)
     with self.assertRaises(IndexError):
         recomb_map.slice(start=200)
     with self.assertRaises(IndexError):
         recomb_map.slice(end=200)
     with self.assertRaises(IndexError):
         recomb_map.slice(start=20, end=10)
Ejemplo n.º 21
0
 def test_all_zero_rate(self):
     positions = [0, 100]
     rates = [0, 0]
     maps = [
         msprime.RecombinationMap(positions, rates),
         PythonRecombinationMap(positions, rates),
     ]
     for rm in maps:
         for x in [0, 50, 51, 55, 99, 100]:
             assert 0 == rm.physical_to_genetic(x)
             assert 0 == rm.genetic_to_physical(0)
             assert 100 == rm.genetic_to_physical(1)
Ejemplo n.º 22
0
 def test_combining_recomb_map_and_rate_length(self):
     recomb_map = msprime.RecombinationMap([0, 1], [1, 0])
     self.assertRaises(
         ValueError, msprime.simulator_factory, 10,
         recombination_map=recomb_map, length=1)
     self.assertRaises(
         ValueError, msprime.simulator_factory, 10,
         recombination_map=recomb_map, recombination_rate=100)
     self.assertRaises(
         ValueError, msprime.simulator_factory, 10,
         recombination_map=recomb_map, length=1,
         recombination_rate=1)
Ejemplo n.º 23
0
 def test_single_recombination(self):
     recombination_map = msprime.RecombinationMap([0, 100, 101, 200],
                                                  [0, 1, 0, 0],
                                                  discrete=True)
     ts = msprime.simulate(10,
                           Ne=10,
                           model="dtwf",
                           random_seed=2,
                           recombination_map=recombination_map)
     self.assertEqual(ts.num_trees, 2)
     trees = ts.trees()
     self.assertEqual(next(trees).interval, (0, 100))
     self.assertEqual(next(trees).interval, (100, 200))
Ejemplo n.º 24
0
 def test_single_recombination(self):
     recombination_map = msprime.RecombinationMap([0, 100, 101, 200], [0, 1, 0, 0])
     ts = msprime.simulate(
         10,
         Ne=10,
         model="dtwf",
         random_seed=2,
         recombination_map=recombination_map,
         discrete_genome=True,
     )
     assert ts.num_trees == 2
     trees = ts.trees()
     assert next(trees).interval == (0, 100)
     assert next(trees).interval == (100, 200)
Ejemplo n.º 25
0
    def test_zero_rate_two_intervals(self):
        # When we have a zero rate in some interval we no longer have a
        # bijective function, since all the physical coordinates in this
        # interval map to a single genetic coordinate.
        positions = [0, 0.25, 0.5, 0.75, 1]
        rates = [1, 0, 1, 0, 0]
        num_loci = 100
        maps = [
            msprime.RecombinationMap(positions, rates, num_loci),
            PythonRecombinationMap(positions, rates, num_loci)
        ]
        for rm in maps:
            self.assertEqual(0.5, rm.get_total_recombination_rate())
            # Between 0 and 0.25 and 0.5 and 0.75 we should be able to map 1-1
            # in physical coordinates.
            for x in [0, 0.125, 0.25, 0.50001, 0.66, 0.74999]:
                y = rm.physical_to_genetic(x)
                self.assertTrue(0 <= y <= num_loci)
                z = rm.genetic_to_physical(y)
                self.assertAlmostEqual(x, z)
            self.assertEqual(0, rm.physical_to_discrete_genetic(0))
            self.assertEqual(25, rm.physical_to_discrete_genetic(0.125))
            self.assertEqual(50, rm.physical_to_discrete_genetic(0.25))
            # Everything withinin the 0.25 to 0.5 interval should map to 50
            self.assertEqual(50, rm.physical_to_discrete_genetic(0.2500001))
            self.assertEqual(50, rm.physical_to_discrete_genetic(0.4))
            self.assertEqual(50, rm.physical_to_discrete_genetic(0.4999999))
            # The discretisation means that we can push values on one side of the
            # interval back to the other side.
            self.assertEqual(50, rm.physical_to_discrete_genetic(0.5000))
            self.assertEqual(51, rm.physical_to_discrete_genetic(0.505))
            # Anything above or equalto 0.75 should map to 100
            self.assertEqual(100, rm.physical_to_discrete_genetic(0.75))
            self.assertEqual(100, rm.physical_to_discrete_genetic(0.751))
            self.assertEqual(100, rm.physical_to_discrete_genetic(0.999))
            self.assertEqual(100, rm.physical_to_discrete_genetic(1.0))

            # All physical coordinates within the first 0 region should map
            # down to the first point, but in the last interval should map
            # to the last point:
            for start, end, point in [(0.25, 0.5, 0.25), (0.75, 1, 1)]:
                for x in [start + delta for delta in [0, 0.01, 0.1]] + [end]:
                    y = rm.physical_to_genetic(x)
                    self.assertTrue(0 <= y <= num_loci)
                    z = rm.genetic_to_physical(y)
                    self.assertEqual(z, point)
                    # We should map exactly in discrete space.
                    k = rm.physical_to_discrete_genetic(x)
                    self.assertEqual(rm.genetic_to_physical(k), point)
Ejemplo n.º 26
0
    def verify_coordinate_conversion(self, positions, rates, num_loci=10):
        """
        Verifies coordinate conversions by the specified RecombinationMap
        instance.
        """
        L = positions[-1]
        rm = msprime.RecombinationMap(positions, rates, num_loci)
        other_rm = PythonRecombinationMap(positions, rates, num_loci)
        if rm.get_size() == 2:
            # When we have very large numbers of loci, this calculations for
            # max distance is off by very small amounts, probably because of
            # machine precision. But if the expected diff is less than say, 10^-10
            # anyway, there's no point in worrying about it.
            max_discretisation_distance = max(1e-10, L / (2 * num_loci))
        else:
            # The above calculation works for a uniform map, but I couldn't
            # figure out how to generalise it. Cop out:
            max_discretisation_distance = L

        self.assertEqual(
            rm.get_total_recombination_rate(),
            other_rm.get_total_recombination_rate())
        num_random_trials = 10
        num_systematic_trials = 10
        values = [L * random.random() for j in range(num_random_trials)]
        for j in range(num_systematic_trials):
            values.append(L * j * 1 / num_systematic_trials)
        values += positions
        for x in values:
            # x is a physical coordinate
            y = rm.physical_to_genetic(x)
            self.assertEqual(y, other_rm.physical_to_genetic(x))
            self.assertTrue(0 <= y <= num_loci)
            # Check if we can round trip approximately in real coordinates.
            xp = rm.genetic_to_physical(y)
            self.assertAlmostEqual(x, xp)
            # The different implementations might differ by very small amounts.
            self.assertAlmostEqual(xp, other_rm.genetic_to_physical(y))

            # Verify the discrete coordinate conversion.
            k = other_rm.physical_to_discrete_genetic(x)
            if y != 0.5:
                # Yuck. Glibc and Python seem to disagree on which way to round
                # when the argument is 1/2. Easiest just skip.
                self.assertEqual(rm.physical_to_discrete_genetic(x), k)
            self.assertTrue(0 <= k <= num_loci)
            x_hat = other_rm.genetic_to_physical(k)
            delta = abs(x - x_hat)
            self.assertGreaterEqual(max_discretisation_distance, delta)
Ejemplo n.º 27
0
 def test_recombination_map(self):
     def f(recomb_map):
         return msprime.simulator_factory(10, recombination_map=recomb_map)
     self.assertRaises(TypeError, f, "wrong type")
     for n in range(2, 10):
         positions = list(range(n))
         rates = [0.1 * j for j in range(n - 1)] + [0.0]
         recomb_map = msprime.RecombinationMap(positions, rates)
         sim = msprime.simulator_factory(10, recombination_map=recomb_map)
         self.assertEqual(sim.recombination_map, recomb_map)
         self.assertEqual(recomb_map.get_positions(), positions)
         self.assertEqual(recomb_map.get_rates(), rates)
         self.assertEqual(sim.num_loci, recomb_map.get_num_loci())
         ll_sim = sim.create_ll_instance()
         self.assertEqual(ll_sim.get_num_loci(), recomb_map.get_num_loci())
Ejemplo n.º 28
0
 def test_nonuniform_recombination_map(self):
     positions = [0, 0.25, 0.5, 0.75, 1]
     rates = [1, 2, 1, 3, 0]
     recomb_map = msprime.RecombinationMap(positions, rates)
     from_ts = msprime.simulate(5,
                                end_time=0.1,
                                random_seed=5,
                                recombination_map=recomb_map)
     start_time = from_ts.tables.nodes.time.max()
     final_ts = msprime.simulate(from_ts=from_ts,
                                 start_time=start_time,
                                 random_seed=2,
                                 recombination_map=recomb_map)
     self.verify_from_tables(from_ts, final_ts, start_time)
     self.verify_simulation_completed(final_ts)
Ejemplo n.º 29
0
    def test_recombination_map(self):
        def f(recomb_map):
            return msprime.simulator_factory(10, recombination_map=recomb_map)

        self.assertRaises(TypeError, f, "wrong type")
        for n in range(2, 10):
            positions = list(range(n))
            rates = [0.1 * j for j in range(n - 1)] + [0.0]
            recomb_map = msprime.RecombinationMap(positions, rates)
            sim = msprime.simulator_factory(10, recombination_map=recomb_map)
            self.assertEqual(sim.recombination_map, recomb_map)
            self.assertEqual(recomb_map.get_positions(), positions)
            self.assertEqual(recomb_map.get_rates(), rates)
            self.assertEqual(sim.sequence_length,
                             recomb_map.get_sequence_length())
Ejemplo n.º 30
0
 def test_zero_rate_end(self):
     positions = [0, 50, 100]
     rates = [1, 0, 0]
     maps = [
         msprime.RecombinationMap(positions, rates),
         PythonRecombinationMap(positions, rates),
     ]
     for rm in maps:
         # Anything < 50 maps to x
         for x in [0, 10, 49]:
             assert x == rm.physical_to_genetic(x)
             assert x == rm.genetic_to_physical(x)
         # values >= 50 should map to 50
         for x in [50, 51, 55, 99, 100]:
             assert 50 == rm.physical_to_genetic(x)
         assert 50 == rm.genetic_to_physical(50)