Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: load an empty array if there are no variants in GenotypesVCF.read #257

Merged
merged 2 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions haptools/data/genotypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,11 @@ def read(
"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.log.info(f"Transposing genotype matrix of size {self.data.shape}")
self.data = self.data.transpose((1, 0, 2))
self.data = np.empty(shape=(0, 0, 0), dtype=self.data.dtype)
else:
# transpose the GT matrix so that samples are rows and variants are columns
self.log.info(f"Transposing genotype matrix of size {self.data.shape}")
self.data = self.data.transpose((1, 0, 2))

def _variant_arr(self, record: Variant):
"""
Expand Down
7 changes: 7 additions & 0 deletions tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,13 @@ def test_load_genotypes_subset(self):
np.testing.assert_allclose(gts.data, expected)
assert gts.samples == ("HG00096", "HG00097", "HG00099", "HG00100", "HG00101")

# what happens if we ask for a region with no variants?
gts = Genotypes(DATADIR / "simple.vcf.gz")
gts.read(region="1:10125-10140")
expected_empty = np.empty((0, 0, 0), dtype=np.uint8)
np.testing.assert_allclose(gts.data, expected_empty)
assert gts.samples == ("HG00096", "HG00097", "HG00099", "HG00100", "HG00101")

# subset for just the samples we want
expected = expected[[1, 3]]

Expand Down
3 changes: 2 additions & 1 deletion tests/test_simphenotype.py
Original file line number Diff line number Diff line change
Expand Up @@ -677,9 +677,10 @@ def test_repeat_with_hapgts(self, capfd):
gt_file = DATADIR / "simple_tr.vcf"
hp_file = DATADIR / "simple_tr.hap"

# simulate from a mix of one haplotype and one repeat
cmd = (
f"simphenotype --repeats {gt_file} --id 1:10114:GTT "
f"{tmp_transform} {hp_file}"
f"--id H1 {tmp_transform} {hp_file}"
)
runner = CliRunner()
result = runner.invoke(main, cmd.split(" "), catch_exceptions=False)
Expand Down
Loading