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: 1) improve error in transform when providing an empty hap file and a --region and 2) allow for calling write() on Genotypes objects without variants #264

Merged
merged 9 commits into from
Dec 11, 2024
Prev Previous commit
Next Next commit
raise ValueError for empty hap file in transform
aryarm authored Dec 1, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 525fd4463fa1bfe3099819ee015d27267ad0d455
2 changes: 2 additions & 0 deletions haptools/transform.py
Original file line number Diff line number Diff line change
@@ -593,6 +593,8 @@ def transform_haps(
"that the IDs in your .hap file correspond with those you provided. "
f"Here are the first few missing haplotypes: {diff[:first_few]}"
)
if len(hp.data) == 0:
raise ValueError("Didn't load any haplotypes from the .hap file")

log.info("Extracting variants from haplotypes")
variants = {vr.id for id in hp.type_ids["H"] for vr in hp.data[id].variants}
11 changes: 5 additions & 6 deletions tests/test_transform.py
Original file line number Diff line number Diff line change
@@ -386,10 +386,10 @@ def test_transform_empty_hap(capfd):
# can we run transform with the empty hap file?
cmd = f"transform --region 1:10116-10122 {gt_file} {hp_file}"
runner = CliRunner()
result = runner.invoke(main, cmd.split(" "), catch_exceptions=False)
result = runner.invoke(main, cmd.split(" "))
captured = capfd.readouterr()
assert all(line for line in captured.out.split("\n") if line.startswith("#"))
assert result.exit_code == 0
assert result.exit_code != 0

# now, index the empty hap file and try again
cmd = f"index {hp_file}"
@@ -400,13 +400,12 @@ def test_transform_empty_hap(capfd):
assert hp_file_gz.exists()
assert hp_file_idx.exists()

# what about now? does it still work?
# what about now? does it still fail?
cmd = f"transform --region 1:10116-10122 {gt_file} {hp_file_gz}"
runner = CliRunner()
result = runner.invoke(main, cmd.split(" "), catch_exceptions=False)
result = runner.invoke(main, cmd.split(" "))
captured = capfd.readouterr()
assert all(line for line in captured.out.split("\n") if line.startswith("#"))
assert result.exit_code == 0
assert result.exit_code != 0

hp_file.unlink()
hp_file_gz.unlink()