Coverage for src/python/ensembl/vep/runner/catalogue/add.py: 95%
48 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 catalogue term addition."""
16from __future__ import annotations
18import logging
19from pathlib import Path
20from typing import ClassVar
22from ensembl.vep.annotators.consequence.rules.editing import apply_add
23from ensembl.vep.annotators.consequence.rules.validation import ValidationReport
24from ensembl.vep.config import Config
25from ensembl.vep.runner.base import ExitCode
26from ensembl.vep.runner.catalogue.common import (
27 destination_for,
28 log_diff,
29 log_validation_report,
30 read_payload,
31 write_payload,
32)
33from ensembl.vep.runner.registry import register_runner
35__all__ = ["Runner"]
37logger = logging.getLogger(__name__)
40@register_runner("catalogue.add")
41class Runner:
42 """Runner for adding and validating a consequence term."""
44 name: ClassVar[str] = "catalogue.add"
45 sections: ClassVar[tuple[str, ...]] = ("logging", "catalogue", "catalogue_write", "catalogue_add")
46 description: ClassVar[str] = "add a term to the consequence catalogue"
48 def __init__(self, config: Config) -> None:
49 """Hold the configuration."""
50 self.config = config
52 def run(self) -> ExitCode:
53 """Add the term, or report why it was refused."""
54 entry = self.config.catalogue_add
55 if entry.term is None:
56 logger.error("nothing to add: give --term")
57 return ExitCode.USAGE
59 baseline, payload = read_payload(self.config)
60 try:
61 destination = destination_for(self.config) if not self.config.catalogue_write.dry_run else None
62 result = apply_add(
63 payload,
64 term=entry.term,
65 accession=entry.accession,
66 impact=entry.impact,
67 rank=entry.rank,
68 tier=entry.tier,
69 so_parent=entry.so_parent,
70 subsumes=entry.subsumes,
71 no_derive_subsumes=entry.no_derive_subsumes,
72 when=entry.when,
73 allow_missing_when=entry.allow_missing_when,
74 notes=entry.notes,
75 )
76 except ValueError as error:
77 logger.error("%s", error)
78 return ExitCode.USAGE
80 if result is None:
81 return ExitCode.FAILURE
82 updated, messages = result
83 for message in messages:
84 logger.info("%s", message)
86 report = ValidationReport.for_catalogue_entries(updated["consequences"], baseline["consequences"])
87 log_validation_report(report)
88 if report.has_errors: 88 ↛ 89line 88 didn't jump to line 89 because the condition on line 88 was never true
89 logger.error("refusing to write: validation reported errors")
90 return ExitCode.FAILURE
92 if destination is None:
93 log_diff(baseline, updated, self.config.catalogue.catalogue_file or Path("catalogue.json"))
94 logger.info("dry run: nothing written")
95 return ExitCode.OK
97 log_diff(baseline, updated, destination)
98 write_payload(updated, destination)
99 return ExitCode.OK