Coverage for src/python/ensembl/vep/annotators/factory.py: 100%
28 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"""Build annotators and run-scoped context from plain values.
16Registered strategies accept different keyword arguments, so construction derives supported
17options from each factory signature instead of maintaining a duplicate table. That is what lets a
18caller spread a whole configuration section over ``build_annotator``: the options the chosen
19strategy declares reach it, and the rest are warned about and dropped.
20"""
22from __future__ import annotations
24import inspect
25import logging
26from typing import Any
28from ensembl.vep.annotators.base import Annotator
29from ensembl.vep.annotators.consequence.context.annotator import AnnotatorContext
30from ensembl.vep.annotators.consequence.facts import apply_packaged_fact_cost_overrides
31from ensembl.vep.annotators.registry import annotator_factory, get_annotator
32from ensembl.vep.caches.codon_cache import CodonEditLRUCache
33from ensembl.vep.caches.mapper_cache import MapperIndexLRUCache
34from ensembl.vep.caches.zone_map import DEFAULT_ZONE_MAP_BYTES, ZoneMapCache
35from ensembl.vep.common import DEFAULT_DISTANCE, Readthrough
36from ensembl.vep.models.overlap import OverlapConsequence
38__all__ = ["build_annotator", "build_annotator_context"]
40logger = logging.getLogger(__name__)
43def build_annotator_context(
44 *,
45 zone_map: bool = False,
46 zone_map_bytes: int = DEFAULT_ZONE_MAP_BYTES,
47 codon_cache: bool = False,
48 codon_cache_entries: int = 65536,
49 mapper_cache: bool = False,
50 mapper_cache_bytes: int = 64 * 1024 * 1024,
51 readthrough: str | Readthrough = Readthrough.NONE,
52 fact_cost_override: bool = False,
53 upstream_distance: int = DEFAULT_DISTANCE,
54 downstream_distance: int = DEFAULT_DISTANCE,
55) -> AnnotatorContext:
56 """Return the run-scoped context described by a set of cache choices.
58 Args:
59 zone_map (bool): Enable the per-transcript positional-fact zone map.
60 zone_map_bytes (int): Byte budget for the zone map cache.
61 codon_cache (bool): Enable the shared-codon edit cache.
62 codon_cache_entries (int): Maximum codon cache entries.
63 mapper_cache (bool): Enable the genomic-to-CDS mapper index cache.
64 mapper_cache_bytes (int): Byte budget for the mapper index cache.
65 readthrough (str | Readthrough): How far translation may run past a destroyed stop codon,
66 as a level or its lowercased name.
67 fact_cost_override (bool): Order fact evaluation by the packaged measured costs instead of the
68 declared tiers. The table is process-wide, so this changes every annotator built after it.
69 upstream_distance (int): Bases before a feature's 5' end that still count as upstream.
70 downstream_distance (int): Bases past a feature's 3' end that still count as downstream.
72 Returns:
73 AnnotatorContext: The context, with only the enabled caches attached.
74 """
75 if isinstance(readthrough, str):
76 readthrough = Readthrough[readthrough.upper()]
77 if fact_cost_override:
78 apply_packaged_fact_cost_overrides()
79 return AnnotatorContext(
80 zone_map_cache=(
81 ZoneMapCache(
82 max_bytes=zone_map_bytes,
83 upstream_distance=upstream_distance,
84 downstream_distance=downstream_distance,
85 )
86 if zone_map
87 else None
88 ),
89 codon_cache=CodonEditLRUCache(max_entries=codon_cache_entries) if codon_cache else None,
90 mapper_index_cache=MapperIndexLRUCache(max_bytes=mapper_cache_bytes) if mapper_cache else None,
91 readthrough=readthrough,
92 upstream_distance=upstream_distance,
93 downstream_distance=downstream_distance,
94 )
97def build_annotator(
98 *,
99 annotator: str = "consequence.linear",
100 batch_size: int = 256,
101 early_subsumption_skip: bool = False,
102 zone_map: bool = False,
103 zone_map_bytes: int = DEFAULT_ZONE_MAP_BYTES,
104 codon_cache: bool = False,
105 codon_cache_entries: int = 65536,
106 mapper_cache: bool = False,
107 mapper_cache_bytes: int = 64 * 1024 * 1024,
108 readthrough: str | Readthrough = Readthrough.NONE,
109 fact_cost_override: bool = False,
110 upstream_distance: int = DEFAULT_DISTANCE,
111 downstream_distance: int = DEFAULT_DISTANCE,
112 annotator_context: AnnotatorContext | None = None,
113 **options: Any,
114) -> Annotator[OverlapConsequence]:
115 """Return a configured annotator, with its caches attached.
117 Args:
118 annotator (str): Registered annotator name.
119 batch_size (int): Overlaps per chunk, for the strategies that batch.
120 early_subsumption_skip (bool): Skip rules already suppressed by a hit, where supported.
121 zone_map (bool): Enable the positional-fact zone map.
122 zone_map_bytes (int): Byte budget for the zone map cache.
123 codon_cache (bool): Enable the shared-codon edit cache.
124 codon_cache_entries (int): Maximum codon cache entries.
125 mapper_cache (bool): Enable the mapper index cache.
126 mapper_cache_bytes (int): Byte budget for the mapper index cache.
127 readthrough (str | Readthrough): How far translation may run past a destroyed stop codon.
128 fact_cost_override (bool): Use the packaged measured fact costs, when building the context.
129 upstream_distance (int): Bases before a feature's 5' end that still count as upstream.
130 downstream_distance (int): Bases past a feature's 3' end that still count as downstream.
131 annotator_context (AnnotatorContext | None): A context to share across annotators, or None
132 to build one from the cache arguments.
133 **options: Further options for the chosen strategy, such as the ones another annotation
134 kind declares. Each is passed on only if that strategy's factory accepts it; unsupported
135 options are logged as warnings.
137 Returns:
138 Annotator[OverlapConsequence]: The annotator instance.
140 Raises:
141 KeyError: If no annotator is registered under ``annotator``.
142 """
143 context = annotator_context or build_annotator_context(
144 zone_map=zone_map,
145 zone_map_bytes=zone_map_bytes,
146 codon_cache=codon_cache,
147 codon_cache_entries=codon_cache_entries,
148 mapper_cache=mapper_cache,
149 mapper_cache_bytes=mapper_cache_bytes,
150 readthrough=readthrough,
151 fact_cost_override=fact_cost_override,
152 upstream_distance=upstream_distance,
153 downstream_distance=downstream_distance,
154 )
155 candidate_kwargs = {
156 **options,
157 "annotator_context": context,
158 "batch_size": batch_size,
159 "early_subsumption_skip": early_subsumption_skip,
160 }
161 accepted = inspect.signature(annotator_factory(annotator)).parameters
162 for name in sorted(set(options).difference(accepted)):
163 logger.warning(f"Ignoring unsupported option {name!r} for annotator {annotator!r}")
164 return get_annotator(
165 annotator,
166 **{name: value for name, value in candidate_kwargs.items() if name in accepted},
167 )