Coverage for src/python/ensembl/vep/annotators/consequence/strategies/base.py: 100%

28 statements  

« 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"""Shared consequence annotator implementation.""" 

15 

16from __future__ import annotations 

17 

18from abc import ABC, abstractmethod 

19from typing import TYPE_CHECKING, Generic, Sequence, TypeVar 

20 

21from ensembl.vep.annotators.consequence.context import AnnotatorContext 

22from ensembl.vep.common import Readthrough 

23from ensembl.vep.models.overlap import FeatureOverlap, OverlapConsequence 

24 

25if TYPE_CHECKING: 

26 from ensembl.vep.trace import AnnotationTrace 

27 from ensembl.vep.caches.codon_cache import CodonCache 

28 from ensembl.vep.caches.zone_map import ZoneMapCache 

29 

30__all__ = ["ConsequenceAnnotator", "RulesetT"] 

31 

32RulesetT = TypeVar("RulesetT") 

33 

34 

35class ConsequenceAnnotator(ABC, Generic[RulesetT]): 

36 """Implement shared behaviour for compiled consequence-rule annotators. 

37 

38 Attributes: 

39 name (str): The registry name this annotator is registered under. 

40 ruleset (RulesetT): The compiled rule set this annotator evaluates. 

41 last_trace (AnnotationTrace | None): The derivation trace recorded by the most recent 

42 ``annotate_feature_overlap`` call, or None if that call did not trace (the annotators 

43 did not opt-in). The native ``rust.*`` annotators never set this. 

44 annotator_context (AnnotatorContext): The run-scoped caches this annotator evaluates 

45 against. 

46 """ 

47 

48 name: str = "base" 

49 

50 def __init__( 

51 self, 

52 ruleset: RulesetT, 

53 annotator_context: AnnotatorContext | None = None, 

54 *, 

55 zone_map: ZoneMapCache | None = None, 

56 codon_cache: CodonCache | None = None, 

57 readthrough: Readthrough | None = None, 

58 ) -> None: 

59 """Initialize the annotator with a compiled rule set and its optional caches. 

60 

61 The ``zone_map`` and ``codon_cache`` can be either coming from annotator context or supplied when 

62 creating the annotator. 

63 

64 Args: 

65 ruleset (RulesetT): The compiled rule set this annotator will evaluate. 

66 annotator_context (AnnotatorContext | None): Run-scoped caches to evaluate against, 

67 or None (the default) for a fresh, empty one owned by this annotator alone. 

68 zone_map (ZoneMapCache | None): Zone-map cache to enable. Overrides whatever 

69 ``annotator_context`` carried; None leaves that untouched. 

70 codon_cache (CodonCache | None): Shared-codon cache to enable. Overrides whatever 

71 ``annotator_context`` carried; None leaves that untouched. 

72 readthrough (Readthrough | None): Stop-codon readthrough level for this run. Overrides 

73 whatever ``annotator_context`` carried; None leaves that untouched. 

74 """ 

75 self.ruleset = ruleset 

76 self.annotator_context = annotator_context if annotator_context is not None else AnnotatorContext() 

77 if zone_map is not None: 

78 self.annotator_context.zone_map_cache = zone_map 

79 if codon_cache is not None: 

80 self.annotator_context.codon_cache = codon_cache 

81 if readthrough is not None: 

82 self.annotator_context.readthrough = readthrough 

83 self.last_trace: AnnotationTrace | None = None 

84 

85 @property 

86 def cache_hits(self) -> int: 

87 """Return candidate-cache hits recorded on this annotator's context. 

88 

89 Returns: 

90 int: The hit count. Shared with any other annotator given the same context. 

91 """ 

92 return self.annotator_context.cache_hits 

93 

94 @property 

95 def cache_misses(self) -> int: 

96 """Return candidate-cache misses recorded on this annotator's context. 

97 

98 Returns: 

99 int: The miss count. Shared with any other annotator given the same context. 

100 """ 

101 return self.annotator_context.cache_misses 

102 

103 @abstractmethod 

104 def annotate_feature_overlaps(self, overlaps: Sequence[FeatureOverlap]) -> list[list[OverlapConsequence]]: 

105 """Calculate consequences for a batch of overlaps at once. 

106 

107 Args: 

108 overlaps (Sequence[FeatureOverlap]): The overlaps to annotate, in order. 

109 

110 Returns: 

111 list[list[OverlapConsequence]]: One consequence list per overlap, same order. 

112 """ 

113 raise NotImplementedError 

114 

115 def annotate_feature_overlap(self, overlap: FeatureOverlap) -> list[OverlapConsequence]: 

116 """Calculate consequences for a feature overlap. 

117 

118 Supplied in terms of the batch method, so implementing ``annotate_feature_overlaps`` is 

119 enough to get both halves of the contract. 

120 

121 **If your batch method is a loop over this one, you must override this one too.** A 

122 strategy that evaluates per overlap (``linear``/``staged``) implements the real work here 

123 and a plain loop there. Implementing *only* a looping batch method leaves this delegation 

124 pointing back at it, and the pair recurses until RecursionError. 

125 

126 Args: 

127 overlap (FeatureOverlap): The overlap to annotate. 

128 

129 Returns: 

130 list[OverlapConsequence]: The consequences that apply. May be empty. 

131 """ 

132 return self.annotate_feature_overlaps([overlap])[0]