Coverage for src/python/ensembl/vep/config/sections/common/annotation.py: 100%
34 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"""Annotation strategy and cache options.
16The default is the columnar staged strategy with every cache off. The zone map, codon and
17mapper caches are all opt-in, because their benefit depends on the workload -- the zone map
18pays for itself once a transcript is touched by more than one variant, which dense or targeted
19panels do and sparse genome-wide input does not. ``readthrough`` changes the modelled answer;
20the remaining options only affect the cost of reaching it.
21"""
23from __future__ import annotations
25from typing import ClassVar
27from attrs import define, field
29from ensembl.vep.config.option_spec import option
30from ensembl.vep.config.sections.base import ConfigSection
32__all__ = ["AnnotationConfig"]
34READTHROUGH_LEVELS = ("none", "utr", "beyond_transcript")
36# Mirrors ensembl.vep.caches.zone_map.DEFAULT_ZONE_MAP_BYTES (the source of truth every other
37# call site imports). Not imported here so this config layer stays free of the domain-layer
38# dependencies caches.zone_map pulls in transitively (annotators.consequence.*, models.*) -- the
39# same reason _annotator_names() below resolves its own dependency lazily rather than at import
40# time.
41_DEFAULT_ZONE_MAP_BYTES = 256 * 1024 * 1024
44def _annotator_names() -> tuple[str, ...]:
45 """Return registered annotator names.
47 Resolved lazily so this module never imports ensembl.vep.annotators, which would make the
48 configuration layer depend on the domain it configures.
50 Returns:
51 tuple[str, ...]: Every registered annotator name, sorted.
52 """
53 # pylint: disable=import-outside-toplevel
54 from ensembl.vep.annotators.registry import available_annotators
56 return available_annotators()
59@define(frozen=True, kw_only=True, auto_attribs=True)
60class AnnotationConfig(ConfigSection):
61 """Which annotator to run and which caches to give it.
63 Attributes:
64 annotator (str): Registered annotator name. Defaults to the columnar staged strategy.
65 batch_size (int): Overlaps per chunk, for the batch-capable strategies.
66 zone_map (bool): Enable the per-transcript positional-fact zone map. Disabled by default;
67 worth enabling for inputs with many variants per transcript.
68 zone_map_bytes (int): Byte budget for the zone map cache.
69 codon_cache (bool): Enable the shared-codon edit cache.
70 codon_cache_entries (int): Maximum codon cache entries.
71 mapper_cache (bool): Enable the genomic-to-CDS mapper index cache.
72 mapper_cache_bytes (int): Byte budget for the mapper index cache.
73 readthrough (str): How far translation may run past a destroyed stop codon.
74 early_subsumption_skip (bool): Skip rules already suppressed by a hit, for the staged
75 strategy. Measured a small net loss; off by default.
76 fact_cost_override (bool): Use the packaged measured fact costs instead of declared tiers.
77 """
79 section_name: ClassVar[str] = "annotation"
81 annotator: str = field(
82 default="consequence.columnar-staged",
83 metadata=option("annotator to run", choices=_annotator_names, metavar="NAME"),
84 )
85 batch_size: int = field(default=256, metadata=option("overlaps per batch", metavar="N"))
86 zone_map: bool = field(
87 default=False,
88 metadata=option("enable the positional zone map for inputs with many variants per transcript"),
89 )
90 zone_map_bytes: int = field(
91 default=_DEFAULT_ZONE_MAP_BYTES, metadata=option("zone map byte budget", metavar="BYTES")
92 )
93 codon_cache: bool = field(default=False, metadata=option("enable the shared-codon cache"))
94 codon_cache_entries: int = field(default=65536, metadata=option("codon cache size", metavar="N"))
95 mapper_cache: bool = field(default=False, metadata=option("enable the mapper index cache"))
96 mapper_cache_bytes: int = field(
97 default=64 * 1024 * 1024, metadata=option("mapper cache byte budget", metavar="BYTES")
98 )
99 readthrough: str = field(
100 default="none",
101 metadata=option("how far to translate past a destroyed stop codon", choices=READTHROUGH_LEVELS),
102 )
103 early_subsumption_skip: bool = field(
104 default=False, metadata=option("skip rules already suppressed by a hit (staged only)")
105 )
106 fact_cost_override: bool = field(
107 default=False,
108 metadata=option("use measured fact costs", env="ENSEMBL_VEP_FACT_COST_OVERRIDE"),
109 )
111 def __attrs_post_init__(self) -> None:
112 """Reject batch and enabled-cache limits that cannot be used."""
114 if self.batch_size < 1:
115 raise ValueError("batch_size must be at least one")
116 if self.zone_map and self.zone_map_bytes < 1:
117 raise ValueError("zone_map_bytes must be at least one when zone_map is enabled")
118 if self.codon_cache and self.codon_cache_entries < 1:
119 raise ValueError("codon_cache_entries must be at least one when codon_cache is enabled")
120 if self.mapper_cache and self.mapper_cache_bytes < 1:
121 raise ValueError("mapper_cache_bytes must be at least one when mapper_cache is enabled")