Coverage for src/python/ensembl/vep/runner/catalogue/validate.py: 72%
60 statements
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-18 14:41 +0000
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-18 14:41 +0000
1# See the NOTICE file distributed with this work for additional information
2# regarding copyright ownership.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14"""Consequence rule-set validation."""
16from __future__ import annotations
18import logging
19from typing import ClassVar
21from ensembl.vep.annotators.consequence.context.fact import FactContext
22from ensembl.vep.annotators.consequence.facts.base import verify_requires
23from ensembl.vep.annotators.consequence.rules.rulesets import FactRuleSet, build_fact_ruleset
24from ensembl.vep.annotators.consequence.rules.validation import ValidationReport
25from ensembl.vep.config import Config
26from ensembl.vep.corpus.registry import CorpusOptionsError, build_corpus_from_options
27from ensembl.vep.runner.base import ExitCode
28from ensembl.vep.runner.catalogue.common import log_validation_report
29from ensembl.vep.runner.registry import register_runner
31__all__ = ["Runner"]
33logger = logging.getLogger(__name__)
36@register_runner("catalogue.validate")
37class Runner:
38 """Runner for configured catalogue validation tiers.
40 A non-zero exit is a real failure: the checked-in catalogue passes today.
41 """
43 name: ClassVar[str] = "catalogue.validate"
44 sections: ClassVar[tuple[str, ...]] = ("logging", "catalogue", "catalogue_validate", "corpus")
45 description: ClassVar[str] = "validate the consequence rule set"
47 def __init__(self, config: Config) -> None:
48 """Hold the configuration."""
49 self.config = config
51 def run(self) -> ExitCode:
52 """Report every finding, and fail if any is build-breaking."""
53 ruleset = build_fact_ruleset()
54 report = ValidationReport.for_ruleset(ruleset)
55 logger.info("checked %d rules", len(ruleset.rules))
56 log_validation_report(report)
57 exit_code = ExitCode.FAILURE if report.has_errors else ExitCode.OK
59 if self.config.catalogue_validate.verify_requires:
60 try:
61 if self._report_requires_problems(): 61 ↛ 62line 61 didn't jump to line 62 because the condition on line 61 was never true
62 exit_code = ExitCode.FAILURE
63 except CorpusOptionsError as unusable:
64 logger.error("%s", unusable)
65 return ExitCode.USAGE
67 if self.config.catalogue_validate.solver == "z3" and self._report_solver_findings(ruleset): 67 ↛ 68line 67 didn't jump to line 68 because the condition on line 67 was never true
68 exit_code = ExitCode.FAILURE
70 return exit_code
72 def _report_requires_problems(self) -> bool:
73 """Check every declared ``requires`` against real contexts, and return whether any failed."""
74 with build_corpus_from_options(self.config.corpus) as corpus:
75 contexts = FactContext.for_overlaps([overlap for _name, overlap in corpus.named_overlaps])
76 problems = verify_requires(contexts)
77 description = corpus.description
78 logger.info("verify_requires over %s: %d problem(s)", description, len(problems))
79 for problem in problems: 79 ↛ 80line 79 didn't jump to line 80 because the loop on line 79 never started
80 logger.error("ERROR: %s", problem)
81 return bool(problems)
83 def _report_solver_findings(self, ruleset: FactRuleSet) -> bool:
84 """Run the whole-ruleset SAT queries, and return whether any rule was unreachable."""
85 # pylint: disable=import-outside-toplevel
86 from ensembl.vep.annotators.consequence.rules.validation import SolverChecks
88 try:
89 findings = SolverChecks.findings(ruleset)
90 except RuntimeError as unavailable:
91 logger.warning("solver skipped: %s", unavailable)
92 return False
94 unreachable = False
95 for finding in findings:
96 if finding.startswith("UNREACHABLE"):
97 logger.error("ERROR: %s", finding)
98 unreachable = True
99 else:
100 logger.info("%s", finding)
101 return unreachable