Example #1
0
def test_sparsealleles_copyspan():
    a = SparseAlleles(np.array([1, 1, 1, 1, 1, 1, 1], dtype=np.int), refcode=1)
    b = SparseAlleles(np.array([2, 2, 2, 2, 2, 2, 2], dtype=np.int), refcode=1)

    a.copy_span(b, 2, 6)
    assert all(a.todense() == np.array([1, 1, 2, 2, 2, 2, 1]))

    c = SparseAlleles(np.array([1, 1, 1, 1, 1, 1, 1], dtype=np.int), refcode=1)
    a.copy_span(c, 2, 6)
    assert all(a.todense() == np.array([1, 1, 1, 1, 1, 1, 1]))
Example #2
0
def test_sparsealleles_meaninglesscomparisions():
    # Comparsions like >, <, >=, <= aren't meaningful for genotypes
    a = SparseAlleles([1, 2, 3, -1])
    b = SparseAlleles([1, 3, 2, -1])

    # Can't test an expression so we make a throwaway function to test
    assert_raises(NotMeaningfulError, lambda x, y: x < y, a, b)
    assert_raises(NotMeaningfulError, lambda x, y: x > y, a, b)
    assert_raises(NotMeaningfulError, lambda x, y: x >= y, a, b)
    assert_raises(NotMeaningfulError, lambda x, y: x <= y, a, b)
Example #3
0
def test_sparsealleles():
    a = SparseAlleles([1, 2, 3, -1], refcode=1)
    b = SparseAlleles([1, 3, 2, -1], refcode=1)

    assert a.nmark() == b.nmark() == 4
    assert (a.missing == np.array([False, False, False, True])).all()
    assert (a.missing == b.missing).all()

    # Test todense
    assert (a.todense() == [1, 2, 3, -1]).all()
    assert isinstance(a.todense(), Alleles)

    a = SparseAlleles([0, 0, 1, 0, -1, 1], refcode=0)
    a[0] = a.missingcode
    assert list(a.missing) == [True, False, False, False, True, False]
Example #4
0
def test_sparsealleles_copyspan():
    a = SparseAlleles(np.array([1,1,1,1,1,1,1], dtype=np.int), refcode=1)
    b = SparseAlleles(np.array([2,2,2,2,2,2,2], dtype=np.int), refcode=1)

    a.copy_span(b, 2, 6)
    assert all(a.todense() == np.array([1,1,2,2,2,2,1]))

    c = SparseAlleles(np.array([1,1,1,1,1,1,1], dtype=np.int), refcode=1)
    a.copy_span(c, 2, 6)
    assert all(a.todense() == np.array([1,1,1,1,1,1,1]))
Example #5
0
def test_sparsealleles():
    a = SparseAlleles([1,2,3,-1], refcode=1)
    b = SparseAlleles([1,3,2,-1], refcode=1)

    assert a.nmark() == b.nmark() == 4
    assert (a.missing == np.array([False, False, False, True])).all()
    assert (a.missing == b.missing).all()

    # Test todense
    assert (a.todense() == [1, 2, 3, -1]).all()
    assert isinstance(a.todense(), Alleles)

    a = SparseAlleles([0,0,1, 0,-1, 1], refcode=0)
    a[0] = a.missingcode
    assert list(a.missing) == [True, False, False, False, True, False] 
Example #6
0
    def empty_chromosome(self, dtype=np.uint8, sparse=False, refcode=None):
        """
        Produces a completely empty chromosome associated with this template.

        :param sparse: Should a SparseAlleles object be returned
        :type sparse: bool
        :param refcode: if sparse, what should the refcode be?
        :type refcode: int8_t
        
        :returns: empty alleles container 
        """
        if sparse:
            return SparseAlleles(size=self.nmark(),
                                 template=self,
                                 refcode=refcode)
        else:
            return Alleles(np.zeros(self.nmark(), dtype=dtype), template=self)
Example #7
0
    def linkageequilibrium_chromosome(self, sparse=False):
        """ 
        Returns a randomly generated chromosome in linage equilibrium 

        :param sparse: Should the output be sparse
        :type sparse: bool

        :returns: random chromosome
        :rtype: Alleles or SparseAlleles 
        """
        if (self.frequencies < 0).any():
            raise ValueError('Not all frequencies are specified')
        r = np.random.random(self.nmark())
        r = np.array(r < self.frequencies, dtype=np.int8) + 1

        if sparse:
            return SparseAlleles(r - 1, refcode=0, template=self)
        else:
            return Alleles(r, template=self)
Example #8
0
def test_sparsealleles_emptylike():
    a = SparseAlleles([1,2,3,4])
    e = a.empty_like()
    assert ((not e.container.any()) and 
            (e.refcode == a.refcode) and 
            (a.nmark() == e.nmark()))
Example #9
0
def test_sparsealleles_emptylike():
    a = SparseAlleles([1,2,3,4])
    e = a.empty_like()
    assert (e.container == e.refcode).all()
Example #10
0
def test_sparsealleles_emptylike():
    a = SparseAlleles([1, 2, 3, 4])
    e = a.empty_like()
    assert ((not e.container.any()) and (e.refcode == a.refcode)
            and (a.nmark() == e.nmark()))