Example #1
0
    def test_windowed_tajima_d(self):
        from allel import windowed_tajima_d

        pos = np.array([1, 11, 21, 31, 41])

        # example with calculable value
        ac = AlleleCountsArray([[1, 3], [2, 2], [3, 1], [1, 3], [2, 2]])
        expect = np.array([0.168] * 3)
        actual, _, _ = windowed_tajima_d(pos, ac, size=25, step=10)
        assert_array_almost_equal(expect, actual, decimal=3)

        # too few sites
        actual, _, _ = windowed_tajima_d(pos, ac, size=15, step=10)
        assert 4 == len(actual)
        assert np.all(np.isnan(actual))

        # too few segregating sites
        ac = AlleleCountsArray([[4, 0], [2, 2], [3, 1], [4, 0], [2, 2]])
        actual, _, _ = windowed_tajima_d(pos, ac, size=25, step=10)
        assert 3 == len(actual)
        assert np.all(np.isnan(actual))
        # allow people to override if they really want to
        expect = np.array([0.592] * 3)
        actual, _, _ = windowed_tajima_d(pos,
                                         ac,
                                         size=25,
                                         step=10,
                                         min_sites=2)
        assert_array_almost_equal(expect, actual, decimal=3)
Example #2
0
    def test_moving_tajima_d(self):
        from allel import moving_tajima_d

        # example with calculable value
        ac = AlleleCountsArray([[1, 3],
                                [2, 2],
                                [3, 1],
                                [1, 3],
                                [2, 2]])
        expect = np.array([0.168] * 3)
        actual = moving_tajima_d(ac, size=3, step=1)
        assert_array_almost_equal(expect, actual, decimal=3)

        # too few sites
        actual = moving_tajima_d(ac, size=2, step=1)
        assert 4 == len(actual)
        assert np.all(np.isnan(actual))

        # too few segregating sites
        ac = AlleleCountsArray([[4, 0],
                                [2, 2],
                                [3, 1],
                                [4, 0],
                                [2, 2]])
        actual = moving_tajima_d(ac, size=3, step=1)
        assert 3 == len(actual)
        assert np.all(np.isnan(actual))
        # allow people to override if they really want to
        expect = np.array([0.592] * 3)
        actual = moving_tajima_d(ac, size=3, step=1, min_sites=2)
        assert_array_almost_equal(expect, actual, decimal=3)
Example #3
0
    def test_slice_types(self):
        ac = AlleleCountsArray(allele_counts_data, dtype='u1')

        # total slice
        s = ac[:]
        assert isinstance(s, AlleleCountsArray)

        # row slice
        s = ac[1:]
        assert isinstance(s, AlleleCountsArray)

        # col slice
        s = ac[:, 1:]
        assert isinstance(s, np.ndarray)

        # row index
        s = ac[0]
        assert isinstance(s, np.ndarray)

        # col index
        s = ac[:, 0]
        assert isinstance(s, np.ndarray)

        # item
        s = ac[0, 0]
        assert isinstance(s, np.uint8)
Example #4
0
    def test_tajima_d(self):
        from allel import tajima_d

        # example with calculable value
        ac = AlleleCountsArray([[1, 3], [2, 2], [3, 1]])
        expect = approx(0.168, 0.01)
        actual = tajima_d(ac)
        assert expect == actual

        # too few sites
        ac = AlleleCountsArray([[2, 2], [3, 1]])
        assert np.nan is tajima_d(ac)

        # too few segregating sites
        ac = AlleleCountsArray([[4, 0], [2, 2], [3, 1]])
        assert np.nan is tajima_d(ac)
        # allow people to override if they really want to
        assert approx(0.592, 0.01) == tajima_d(ac, min_sites=2)
Example #5
0
    def test_sequence_divergence(self):
        from allel import sequence_divergence
        pos = [2, 4, 8]
        ac1 = AlleleCountsArray([[2, 0], [2, 0], [2, 0]])
        ac2 = AlleleCountsArray([[0, 2], [0, 2], [0, 2]])

        # all variants
        e = 3 / 7
        a = sequence_divergence(pos, ac1, ac2)
        assert e == a

        # start/stop
        e = 2 / 6
        a = sequence_divergence(pos, ac1, ac2, start=0, stop=5)
        assert e == a

        # start/stop, an provided
        an1 = ac1.sum(axis=1)
        an2 = ac2.sum(axis=1)
        e = 2 / 6
        a = sequence_divergence(pos,
                                ac1,
                                ac2,
                                start=0,
                                stop=5,
                                an1=an1,
                                an2=an2)
        assert e == a
Example #6
0
    def test_constructor(self):

        # missing data arg
        with pytest.raises(TypeError):
            # noinspection PyArgumentList
            AlleleCountsArray()

        # data has wrong dtype
        data = 'foo bar'
        with pytest.raises(TypeError):
            AlleleCountsArray(data)

        # data has wrong dtype
        data = [4., 5., 3.7]
        with pytest.raises(TypeError):
            AlleleCountsArray(data)

        # data has wrong dimensions
        data = [1, 2, 3]
        with pytest.raises(TypeError):
            AlleleCountsArray(data)

        # data has wrong dimensions
        data = diploid_genotype_data
        with pytest.raises(TypeError):
            AlleleCountsArray(data)

        # valid data (typed)
        ac = AlleleCountsArray(allele_counts_data, dtype='u1')
        aeq(allele_counts_data, ac)
        assert np.uint8 == ac.dtype
Example #7
0
    def test_reduce_types(self):
        ac = AlleleCountsArray(allele_counts_data, dtype='u1')

        for m in 'sum', 'max', 'argmax':
            x = getattr(ac, m)(axis=1)
            assert isinstance(x, np.ndarray)
Example #8
0
 def setup_instance(self, data):
     return AlleleCountsArray(data)