Skip to content

Commit 65d6e7b

Browse files
tomwhitejeromekelleher
authored andcommitted
Handle case where FILTER in filtering expression is not in header
1 parent b5868de commit 65d6e7b

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

tests/test_bcftools_validation.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -285,20 +285,25 @@ def test_query_logic_precendence(tmp_path, expr, expected):
285285
assert num_lines == int(expected)
286286

287287

288+
# fmt: off
288289
@pytest.mark.parametrize(
289-
("args", "vcf_name"),
290+
("args", "vcf_name", "bcftools_error_string"),
290291
[
291-
("index -ns", "sample.vcf.gz"),
292-
("query -f '%POS\n' -i 'INFO/DP > 10' -e 'INFO/DP < 50'", "sample.vcf.gz"),
293-
("view -i 'INFO/DP > 10' -e 'INFO/DP < 50'", "sample.vcf.gz"),
292+
("index -ns", "sample.vcf.gz", True),
293+
("query -f '%POS\n' -i 'INFO/DP > 10' -e 'INFO/DP < 50'", "sample.vcf.gz", True), # noqa: E501
294+
("view -i 'INFO/DP > 10' -e 'INFO/DP < 50'", "sample.vcf.gz", True),
295+
# bcftools output does not start with "Error"
296+
("view -i 'FILTER=\"F\"'", "sample.vcf.gz", False),
294297
],
295298
)
296-
def test_error(tmp_path, args, vcf_name):
299+
# fmt: on
300+
def test_error(tmp_path, args, vcf_name, bcftools_error_string):
297301
vcf_path = pathlib.Path("tests/data/vcf") / vcf_name
298302
vcz_path = vcz_path_cache(vcf_path)
299303

300304
_, bcftools_error = run_bcftools(f"{args} {vcf_path}", expect_error=True)
301-
assert bcftools_error.startswith("Error:") or bcftools_error.startswith("[E::")
305+
if bcftools_error_string:
306+
assert bcftools_error.startswith("Error:") or bcftools_error.startswith("[E::")
302307

303308
_, vcztools_error = run_vcztools(f"{args} {vcz_path}", expect_error=True)
304309
assert "Error:" in vcztools_error

vcztools/filter.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,9 @@ def eval(self, data):
311311
if self.tokens == ".":
312312
return np.zeros_like(data["filter_id"], dtype=bool)
313313
filters = self.tokens.split(";")
314+
for filter in filters:
315+
if filter not in data["filter_id"]:
316+
raise ValueError(f'The filter "{filter}" is not present in header')
314317
return np.isin(data["filter_id"], filters)
315318

316319
def referenced_fields(self):

0 commit comments

Comments
 (0)