|
1 | 1 | import pathlib
|
2 | 2 |
|
3 | 3 | import numpy as np
|
| 4 | +import numpy.testing as nt |
4 | 5 | import pyparsing as pp
|
5 | 6 | import pytest
|
6 | 7 | import zarr
|
7 |
| -from numpy.testing import assert_array_equal |
8 | 8 |
|
9 | 9 | from tests.utils import vcz_path_cache
|
10 |
| -from vcztools.filter import FilterExpressionEvaluator, FilterExpressionParser |
| 10 | +from vcztools import filter as filter_mod |
11 | 11 |
|
12 | 12 |
|
13 | 13 | class TestFilterExpressionParser:
|
14 |
| - @pytest.fixture() |
15 |
| - def identifier_parser(self, parser): |
16 |
| - return parser._identifier_parser |
| 14 | + @pytest.mark.parametrize( |
| 15 | + "expression", |
| 16 | + [ |
| 17 | + "", |
| 18 | + "| |", |
| 19 | + "a +", |
| 20 | + '"stri + 2', |
| 21 | + ], |
| 22 | + ) |
| 23 | + def test_invalid_expressions(self, expression): |
| 24 | + parser = filter_mod.make_bcftools_filter_parser(map_vcf_identifiers=False) |
| 25 | + with pytest.raises(pp.ParseException): |
| 26 | + parser.parse_string(expression, parse_all=True) |
17 | 27 |
|
18 |
| - @pytest.fixture() |
19 |
| - def parser(self): |
20 |
| - return FilterExpressionParser() |
21 | 28 |
|
| 29 | +class TestFilterExpressionSample: |
22 | 30 | @pytest.mark.parametrize(
|
23 | 31 | ("expression", "expected_result"),
|
24 | 32 | [
|
25 |
| - ("1", [1]), |
26 |
| - ("1.0", [1.0]), |
27 |
| - ("1e-4", [0.0001]), |
28 |
| - ('"String"', ["String"]), |
29 |
| - ("POS", ["POS"]), |
30 |
| - ("INFO/DP", ["INFO/DP"]), |
31 |
| - ("FORMAT/GT", ["FORMAT/GT"]), |
32 |
| - ("FMT/GT", ["FMT/GT"]), |
33 |
| - ("GT", ["GT"]), |
| 33 | + ("POS < 1000", [1, 1, 0, 0, 0, 0, 0, 0, 1]), |
| 34 | + ("INFO/DP > 10", [0, 0, 1, 1, 0, 1, 0, 0, 0]), |
| 35 | + # Not supporting format fields for now: #180 |
| 36 | + # ("FMT/GQ > 20", [0, 0, 1, 1, 1, 1, 1, 0, 0]), |
| 37 | + # ("FMT/DP >= 5 && FMT/GQ > 10", [0, 0, 1, 1, 1, 0, 0, 0, 0]), |
| 38 | + # ("GT > 0", [1, 1, 1, 1, 1, 0, 1, 0, 1]), |
| 39 | + # ("GT > 0 & FMT/HQ >= 10", [0, 0, 1, 1, 1, 0, 0, 0, 0]), |
| 40 | + # ("FMT/DP >= 5 & FMT/GQ>10", [0, 0, 1, 0, 1, 0, 0, 0, 0]), |
| 41 | + # ("QUAL > 10 || FMT/GQ>10", [0, 0, 1, 1, 1, 1, 1, 0, 0]), |
| 42 | + # ("(QUAL > 10 || FMT/GQ>10) && POS > 100000", [0, 0, 0, 0, 1, 1, 1, 0, 0]), |
| 43 | + # ("(FMT/DP >= 8 | FMT/GQ>40) && POS > 100000", |
| 44 | + # [0, 0, 0, 0, 0, 1, 0, 0, 0]), |
34 | 45 | ],
|
35 | 46 | )
|
36 |
| - def test_valid_identifiers(self, identifier_parser, expression, expected_result): |
37 |
| - assert identifier_parser(expression).as_list() == expected_result |
| 47 | + def test(self, expression, expected_result): |
| 48 | + original = pathlib.Path("tests/data/vcf") / "sample.vcf.gz" |
| 49 | + vcz = vcz_path_cache(original) |
| 50 | + root = zarr.open(vcz, mode="r") |
| 51 | + data = {field: root[field][:] for field in root.keys()} |
| 52 | + filter_expr = filter_mod.FilterExpression( |
| 53 | + field_names=set(root), include=expression |
| 54 | + ) |
| 55 | + result = filter_expr.evaluate(data) |
| 56 | + nt.assert_array_equal(result, expected_result) |
| 57 | + |
| 58 | + filter_expr = filter_mod.FilterExpression( |
| 59 | + field_names=set(root), exclude=expression |
| 60 | + ) |
| 61 | + result = filter_expr.evaluate(data) |
| 62 | + nt.assert_array_equal(result, np.logical_not(expected_result)) |
38 | 63 |
|
| 64 | + |
| 65 | +def numpify_values(data): |
| 66 | + return {k: np.array(v) for k, v in data.items()} |
| 67 | + |
| 68 | + |
| 69 | +class TestFilterExpression: |
39 | 70 | @pytest.mark.parametrize(
|
40 |
| - "expression", |
| 71 | + ("expression", "data", "expected"), |
41 | 72 | [
|
42 |
| - "", |
43 |
| - "FORMAT/ GT", |
44 |
| - "format / GT", |
45 |
| - "fmt / GT", |
46 |
| - "info / DP", |
47 |
| - "'String'", |
| 73 | + ("POS<5", {"variant_position": [1, 5, 6, 10]}, [1, 0, 0, 0]), |
| 74 | + ("INFO/XX>=10", {"variant_XX": [1, 5, 6, 10]}, [0, 0, 0, 1]), |
| 75 | + ("INFO/XX / 2 >=5", {"variant_XX": [1, 5, 6, 10]}, [0, 0, 0, 1]), |
| 76 | + ("POS<5 | POS>8", {"variant_position": [1, 5, 6, 10]}, [1, 0, 0, 1]), |
| 77 | + ( |
| 78 | + "POS<0 & POS<1 & POS<2 & POS<3 & POS<4", |
| 79 | + {"variant_position": range(10)}, |
| 80 | + np.zeros(10, dtype=bool), |
| 81 | + ), |
48 | 82 | ],
|
49 | 83 | )
|
50 |
| - def test_invalid_identifiers(self, identifier_parser, expression): |
51 |
| - with pytest.raises(pp.ParseException): |
52 |
| - identifier_parser(expression) |
| 84 | + def test_evaluate(self, expression, data, expected): |
| 85 | + fee = filter_mod.FilterExpression(include=expression) |
| 86 | + result = fee.evaluate(numpify_values(data)) |
| 87 | + nt.assert_array_equal(result, expected) |
| 88 | + |
53 | 89 |
|
| 90 | +class TestBcftoolsParser: |
54 | 91 | @pytest.mark.parametrize(
|
55 |
| - ("expression", "expected_result"), |
| 92 | + "expr", |
56 | 93 | [
|
57 |
| - ("POS>=100", [["POS", ">=", 100]]), |
58 |
| - ( |
59 |
| - "FMT/DP>10 && FMT/GQ>10", |
60 |
| - [[["FMT/DP", ">", 10], "&&", ["FMT/GQ", ">", 10]]], |
61 |
| - ), |
62 |
| - ("QUAL>10 || FMT/GQ>10", [[["QUAL", ">", 10], "||", ["FMT/GQ", ">", 10]]]), |
63 |
| - ( |
64 |
| - "FMT/DP>10 && FMT/GQ>10 || QUAL > 10", |
65 |
| - [ |
66 |
| - [ |
67 |
| - [["FMT/DP", ">", 10], "&&", ["FMT/GQ", ">", 10]], |
68 |
| - "||", |
69 |
| - ["QUAL", ">", 10], |
70 |
| - ] |
71 |
| - ], |
72 |
| - ), |
73 |
| - ( |
74 |
| - "QUAL>10 || FMT/DP>10 && FMT/GQ>10", |
75 |
| - [ |
76 |
| - [ |
77 |
| - ["QUAL", ">", 10], |
78 |
| - "||", |
79 |
| - [["FMT/DP", ">", 10], "&&", ["FMT/GQ", ">", 10]], |
80 |
| - ] |
81 |
| - ], |
82 |
| - ), |
83 |
| - ( |
84 |
| - "QUAL>10 | FMT/DP>10 & FMT/GQ>10", |
85 |
| - [ |
86 |
| - [ |
87 |
| - ["QUAL", ">", 10], |
88 |
| - "|", |
89 |
| - [["FMT/DP", ">", 10], "&", ["FMT/GQ", ">", 10]], |
90 |
| - ], |
91 |
| - ], |
92 |
| - ), |
93 |
| - ( |
94 |
| - "(QUAL>10 || FMT/DP>10) && FMT/GQ>10", |
95 |
| - [ |
96 |
| - [ |
97 |
| - [["QUAL", ">", 10], "||", ["FMT/DP", ">", 10]], |
98 |
| - "&&", |
99 |
| - ["FMT/GQ", ">", 10], |
100 |
| - ] |
101 |
| - ], |
102 |
| - ), |
| 94 | + "2", |
| 95 | + '"x"', |
| 96 | + '"INFO/STRING"', |
| 97 | + "2 + 2", |
| 98 | + "(2 + 3) / 2", |
| 99 | + "2 / (2 + 3)", |
| 100 | + "1 + 1 + 1 + 1 + 1", |
| 101 | + "5 * (2 / 3)", |
| 102 | + "5 * 2 / 3", |
| 103 | + "1 + 2 - 3 / 4 * 5 + 6 * 7 / 8", |
| 104 | + "5 / (1 + 2 - 4) / (4 * 5 + 6 * 7 / 8)", |
| 105 | + "5 < 2", |
| 106 | + "5 > 2", |
| 107 | + "0 == 0", |
| 108 | + "0 != 0", |
| 109 | + "(1 + 2) == 0", |
| 110 | + "1 + 2 == 0", |
| 111 | + "1 + 2 == 1 + 2 + 3", |
| 112 | + "(1 + 2) == (1 + 2 + 3)", |
| 113 | + "(1 == 1) != (2 == 2)", |
| 114 | + '("x" == "x")', |
103 | 115 | ],
|
104 | 116 | )
|
105 |
| - def test_valid_expressions(self, parser, expression, expected_result): |
106 |
| - assert parser(expression=expression).as_list() == expected_result |
| 117 | + def test_python_arithmetic_expressions(self, expr): |
| 118 | + parser = filter_mod.make_bcftools_filter_parser() |
| 119 | + parsed = parser.parse_string(expr, parse_all=True) |
| 120 | + result = parsed[0].eval({}) |
| 121 | + assert result == eval(expr) |
107 | 122 |
|
| 123 | + @pytest.mark.parametrize( |
| 124 | + ("expr", "data"), |
| 125 | + [ |
| 126 | + ("a", {"a": 1}), |
| 127 | + ("a + a", {"a": 1}), |
| 128 | + ("a + 2 * a - 1", {"a": 7}), |
| 129 | + ("a - b < a + b", {"a": 7, "b": 6}), |
| 130 | + ("(a - b) < (a + b)", {"a": 7, "b": 6}), |
| 131 | + ("(a - b) < (a + b)", {"a": 7.0, "b": 6.666}), |
| 132 | + ("a == a", {"a": 1}), |
| 133 | + ('a == "string"', {"a": "string"}), |
| 134 | + ], |
| 135 | + ) |
| 136 | + def test_python_arithmetic_expressions_data(self, expr, data): |
| 137 | + parser = filter_mod.make_bcftools_filter_parser(map_vcf_identifiers=False) |
| 138 | + parsed = parser.parse_string(expr, parse_all=True) |
| 139 | + result = parsed[0].eval(data) |
| 140 | + assert result == eval(expr, data) |
108 | 141 |
|
109 |
| -class TestFilterExpressionEvaluator: |
110 | 142 | @pytest.mark.parametrize(
|
111 |
| - ("expression", "expected_result"), |
| 143 | + ("expr", "data"), |
112 | 144 | [
|
113 |
| - ("POS < 1000", [1, 1, 0, 0, 0, 0, 0, 0, 1]), |
114 |
| - ("FMT/GQ > 20", [0, 0, 1, 1, 1, 1, 1, 0, 0]), |
115 |
| - ("FMT/DP >= 5 && FMT/GQ > 10", [0, 0, 1, 1, 1, 0, 0, 0, 0]), |
116 |
| - ("FMT/DP >= 5 & FMT/GQ>10", [0, 0, 1, 0, 1, 0, 0, 0, 0]), |
117 |
| - ("QUAL > 10 || FMT/GQ>10", [0, 0, 1, 1, 1, 1, 1, 0, 0]), |
118 |
| - ("(QUAL > 10 || FMT/GQ>10) && POS > 100000", [0, 0, 0, 0, 1, 1, 1, 0, 0]), |
119 |
| - ("(FMT/DP >= 8 | FMT/GQ>40) && POS > 100000", [0, 0, 0, 0, 0, 1, 0, 0, 0]), |
120 |
| - ("INFO/DP > 10", [0, 0, 1, 1, 0, 1, 0, 0, 0]), |
121 |
| - ("GT > 0", [1, 1, 1, 1, 1, 0, 1, 0, 1]), |
122 |
| - ("GT > 0 & FMT/HQ >= 10", [0, 0, 1, 1, 1, 0, 0, 0, 0]), |
| 145 | + ("a", {"a": [1, 2, 3]}), |
| 146 | + ("a + a", {"a": [1, 2, 3]}), |
| 147 | + ("1 + a + a", {"a": [1, 2, 3]}), |
| 148 | + ("a + b", {"a": [1, 2, 3], "b": [5, 6, 7]}), |
| 149 | + ("(a + b) < c", {"a": [1, 2, 3], "b": [5, 6, 7], "c": [5, 10, 15]}), |
123 | 150 | ],
|
124 | 151 | )
|
125 |
| - def test(self, expression, expected_result): |
126 |
| - original = pathlib.Path("tests/data/vcf") / "sample.vcf.gz" |
127 |
| - vcz = vcz_path_cache(original) |
128 |
| - root = zarr.open(vcz, mode="r") |
| 152 | + def test_numpy_arithmetic_expressions_data(self, expr, data): |
| 153 | + parser = filter_mod.make_bcftools_filter_parser(map_vcf_identifiers=False) |
| 154 | + parsed = parser.parse_string(expr, parse_all=True) |
| 155 | + npdata = numpify_values(data) |
| 156 | + result = parsed[0].eval(npdata) |
| 157 | + evaled = eval(expr, npdata) |
| 158 | + nt.assert_array_equal(result, evaled) |
| 159 | + |
| 160 | + @pytest.mark.parametrize( |
| 161 | + ("expr", "expected"), |
| 162 | + [ |
| 163 | + ("1 & 1", True), |
| 164 | + ("0 & 1", False), |
| 165 | + ("1 & 0", False), |
| 166 | + ("0 & 0", False), |
| 167 | + ("1 | 1", True), |
| 168 | + ("0 | 1", True), |
| 169 | + ("1 | 0", True), |
| 170 | + ("0 | 0", False), |
| 171 | + ("(1 < 2) | 0", True), |
| 172 | + ("(1 < 2) & 0", False), |
| 173 | + ], |
| 174 | + ) |
| 175 | + def test_boolean_operator_expressions(self, expr, expected): |
| 176 | + parser = filter_mod.make_bcftools_filter_parser() |
| 177 | + parsed = parser.parse_string(expr, parse_all=True) |
| 178 | + result = parsed[0].eval({}) |
| 179 | + assert result == expected |
| 180 | + |
| 181 | + @pytest.mark.parametrize( |
| 182 | + ("expr", "data", "expected"), |
| 183 | + [ |
| 184 | + ("a == b", {"a": [0, 1], "b": [1, 1]}, [False, True]), |
| 185 | + ("a = b", {"a": [0, 1], "b": [1, 1]}, [False, True]), |
| 186 | + ("a & b", {"a": [0, 1], "b": [1, 1]}, [False, True]), |
| 187 | + ("a && b", {"a": [0, 1], "b": [1, 1]}, [False, True]), |
| 188 | + ("a | b", {"a": [0, 1], "b": [1, 1]}, [True, True]), |
| 189 | + ("a || b", {"a": [0, 1], "b": [1, 1]}, [True, True]), |
| 190 | + ("(a < 2) & (b > 1)", {"a": [0, 1], "b": [1, 2]}, [False, True]), |
| 191 | + ], |
| 192 | + ) |
| 193 | + def test_boolean_operator_expressions_data(self, expr, data, expected): |
| 194 | + parser = filter_mod.make_bcftools_filter_parser(map_vcf_identifiers=False) |
| 195 | + parsed = parser.parse_string(expr, parse_all=True) |
| 196 | + result = parsed[0].eval(numpify_values(data)) |
| 197 | + nt.assert_array_equal(result, expected) |
129 | 198 |
|
130 |
| - parser = FilterExpressionParser() |
131 |
| - parse_results = parser(expression)[0] |
132 |
| - evaluator = FilterExpressionEvaluator(parse_results) |
133 |
| - assert_array_equal(evaluator(root, 0), expected_result) |
134 | 199 |
|
135 |
| - invert_evaluator = FilterExpressionEvaluator(parse_results, invert=True) |
136 |
| - assert_array_equal(invert_evaluator(root, 0), np.logical_not(expected_result)) |
| 200 | +class TestAPIErrors: |
| 201 | + def test_include_and_exclude(self): |
| 202 | + with pytest.raises(ValueError, match="Cannot handle both an include "): |
| 203 | + filter_mod.FilterExpression(include="x", exclude="y") |
0 commit comments