Skip to content

Commit

Permalink
Fail with helpful error message if 'region_index' is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
tomwhite committed Aug 27, 2024
1 parent 44ba5d0 commit 4cb2e26
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
13 changes: 13 additions & 0 deletions tests/test_stats.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import pathlib
from io import StringIO

import pytest
from bio2zarr import vcf2zarr

from vcztools.stats import nrecords, stats

from .utils import vcz_path_cache
Expand Down Expand Up @@ -29,3 +32,13 @@ def test_stats():
X . 1
"""
)


def test_stats__no_index(tmp_path):
original = pathlib.Path("tests/data/vcf") / "sample.vcf.gz"
# don't use cache here since we wnat to make sure vcz is not indexed
vcz = tmp_path.joinpath("intermediate.vcz")
vcf2zarr.convert([original], vcz, worker_processes=0, local_alleles=False)

with pytest.raises(ValueError, match="Could not load 'region_index' variable."):
stats(vcz, StringIO())
6 changes: 6 additions & 0 deletions vcztools/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ def nrecords(vcz, output):
def stats(vcz, output):
root = zarr.open(vcz, mode="r")

if "region_index" not in root:
raise ValueError(
"Could not load 'region_index' variable. "
"Use 'vcztools index' to create an index."
)

with open_file_like(output) as output:
contigs = root["contig_id"][:].astype("U").tolist()
if "contig_length" in root:
Expand Down

0 comments on commit 4cb2e26

Please sign in to comment.