Skip to content

Commit

Permalink
Add fill argument to Genotype.array (#173)
Browse files Browse the repository at this point in the history
  • Loading branch information
timothymillar authored Oct 7, 2020
1 parent 995a6b6 commit 1152d8d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
9 changes: 5 additions & 4 deletions cyvcf2/cyvcf2.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1024,13 +1024,14 @@ cdef class Genotypes(object):
result.append((v >> 1) - 1)
return result

def array(Genotypes self):
def array(Genotypes self, int fill=-2):
"""
array returns an int16 numpy array of shape n_samples, (ploidy + 1).
The last column indicates phased (1 is phased, 0 is unphased).
The other columns indicate the alleles, e.g. [0, 1, 1] is 0|1.
Unknown alleles are represented by -1 and mixed-ploidy arrays
are padded with -2 to indicate non-alleles.
Unknown alleles are represented by -1.
If a mixture of ploidy levels are present then the array is padded
with the `fill` value (default = -2) to indicate non-alleles.
"""
cdef np.ndarray[np.int16_t, ndim=2] to_return = np.zeros((self.n_samples, self.ploidy + 1),
dtype=np.int16)
Expand All @@ -1044,7 +1045,7 @@ cdef class Genotypes(object):
for allele in range(self.ploidy):
raw = self._raw[ind * self.ploidy + allele]
if raw == bcf_int32_vector_end:
to_return[ind, allele] = -2
to_return[ind, allele] = fill
else:
to_return[ind, allele] = (raw >> 1) - 1
to_return[ind, self.ploidy] = (self._raw[ind * self.ploidy + 1] & 1) == 1
Expand Down
6 changes: 6 additions & 0 deletions cyvcf2/tests/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,12 @@ def test_access_genotype_array():
np.array([[-1, -2, 1], [0, 2, 1]], dtype=np.int16)
)

# test fill value
np.testing.assert_array_equal(
v.genotype.array(fill=-9),
np.array([[-1, -9, 1], [0, 2, 1]], dtype=np.int16)
)

def test_alt_homozygous_gt():
vcf = VCF(os.path.join(HERE, "test-multiallelic-homozygous-alt.vcf.gz"))
assert vcf is not None
Expand Down

0 comments on commit 1152d8d

Please sign in to comment.