Skip to content

Commit

Permalink
warn user if the data module loaded zero variants
Browse files Browse the repository at this point in the history
  • Loading branch information
aryarm committed Apr 2, 2022
1 parent 0756290 commit dc7cad0
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions haptools/data/genotypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ def read(self, region: str = None, samples: list[str] = None):
"""
Read genotypes from a VCF into a numpy matrix stored in :py:attr:`~.Genotypes.data`
Raises
------
ValueError
If the genotypes array is empty
Parameters
----------
region : str, optional
Expand Down Expand Up @@ -110,6 +115,11 @@ def read(self, region: str = None, samples: list[str] = None):
self.data = np.array(
[variant.genotypes for variant in variants], dtype=np.uint8
)
if self.data.shape == (0, 0, 0):
raise ValueError(
"Failed to load genotypes. If you specified a region, check that the"
" contig name matches! For example, double-check the 'chr' prefix."
)
# transpose the GT matrix so that samples are rows and variants are columns
self.data = self.data.transpose((1, 0, 2))

Expand Down Expand Up @@ -144,8 +154,10 @@ def check_biallelic(self, discard_also=False):
self.variants = np.delete(self.variants, variant_idx)
else:
raise ValueError(
"Variant with ID {} at POS {}:{} is multiallelic for sample {}".format(
*tuple(self.variants[variant_idx[0]])[:3], self.samples[samp_idx[0]]
"Variant with ID {} at POS {}:{} is multiallelic for sample {}"
.format(
*tuple(self.variants[variant_idx[0]])[:3],
self.samples[samp_idx[0]],
)
)
self.data = self.data.astype(np.bool_)
Expand Down

0 comments on commit dc7cad0

Please sign in to comment.