From 4cb2e26aaddfe25081395e9f9ac7f9bc48853f21 Mon Sep 17 00:00:00 2001 From: Tom White Date: Sun, 25 Aug 2024 17:38:51 +0100 Subject: [PATCH] Fail with helpful error message if 'region_index' is missing --- tests/test_stats.py | 13 +++++++++++++ vcztools/stats.py | 6 ++++++ 2 files changed, 19 insertions(+) diff --git a/tests/test_stats.py b/tests/test_stats.py index e705d83..91205c3 100644 --- a/tests/test_stats.py +++ b/tests/test_stats.py @@ -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 @@ -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()) diff --git a/vcztools/stats.py b/vcztools/stats.py index 25ee6bc..d1d750f 100644 --- a/vcztools/stats.py +++ b/vcztools/stats.py @@ -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: