Coverage for src/python/ensembl/vep/annotators/consequence/facts/helper.py: 99%

67 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"""Unregistered helper functions used by consequence facts.""" 

15 

16from __future__ import annotations 

17 

18from typing import cast 

19 

20from ensembl.vep.annotators.consequence.facts.base import get_fact 

21from ensembl.vep.annotators.consequence.context.fact import FactContext 

22from ensembl.vep.common import DEFAULT_DISTANCE, ClassSOTerm, Strand 

23from ensembl.vep.common.constants import EMPTY_ALLELE 

24from ensembl.vep.models.genic import Intron 

25from ensembl.vep.models.variation import VariationAllele 

26from ensembl.vep.models.overlap import FeatureOverlap 

27 

28__all__ = [ 

29 "DOWNSTREAM_DISTANCE", 

30 "UPSTREAM_DISTANCE", 

31 "allele_length_difference", 

32 "alternate_allele", 

33 "alternate_allele_length", 

34 "downstream_distance", 

35 "genomic_overlap", 

36 "intron_offsets_to_genomic_span", 

37 "reference_allele", 

38 "reference_allele_length", 

39 "upstream_distance", 

40 "variant_genomic_span", 

41] 

42 

43UPSTREAM_DISTANCE = DOWNSTREAM_DISTANCE = DEFAULT_DISTANCE 

44"""The distances a run uses when it configures none, matching Perl VEP's ``--distance`` default.""" 

45 

46 

47def upstream_distance(fact_context: FactContext) -> int: 

48 """Return the run's upstream pairing distance, or the default outside a run. 

49 

50 Read from the annotator context because ``--distance`` is per run, and a fact evaluated 

51 standalone (a unit test, say) has no context to read. 

52 """ 

53 

54 context = fact_context.annotator_context 

55 return UPSTREAM_DISTANCE if context is None else context.upstream_distance 

56 

57 

58def downstream_distance(fact_context: FactContext) -> int: 

59 """Return the run's downstream pairing distance, or the default outside a run.""" 

60 

61 context = fact_context.annotator_context 

62 return DOWNSTREAM_DISTANCE if context is None else context.downstream_distance 

63 

64 

65# -------------------------------------------------------------------------- 

66# Span and overlaps 

67# -------------------------------------------------------------------------- 

68 

69 

70def variant_genomic_span(fact_context: FactContext[FeatureOverlap]) -> tuple[int, int]: 

71 """Return the variant's genomic ``(minimum, maximum)`` span. 

72 

73 Thin wrapper over ``FactContext.variant_span``, which memoises the result for the whole 

74 overlap -- kept as a free function because every position/sequence fact calls it by this name. 

75 """ 

76 return fact_context.variant_span 

77 

78 

79def genomic_overlap(range_start: int, range_end: int, other_start: int, other_end: int) -> bool: 

80 """Return whether two closed genomic intervals share a position.""" 

81 return range_start <= other_end and range_end >= other_start 

82 

83 

84# -------------------------------------------------------------------------- 

85# Intron offsets 

86# -------------------------------------------------------------------------- 

87 

88 

89def intron_offsets_to_genomic_span( 

90 intron: Intron, strand: Strand, low: int, high: int, from_donor: bool 

91) -> tuple[int, int]: 

92 """Return the genomic span of transcript-relative intron offsets. 

93 

94 As usual the genomic span is ascending. 

95 

96 Offsets are 1-based, transcript sense, measured from the intron's 5' end (donor) when 

97 ``from_donor`` is True, else from its 3' end (acceptor). The donor is ``intron.start`` on the 

98 forward strand and ``intron.end`` on the reverse strand (the intron's 5' end in transcript 

99 sense is the high genomic coordinate there). 

100 Positions are clipped to the start and end of the intron so that they are never outside an intron. 

101 """ 

102 if strand == Strand.FORWARD: 

103 five_prime_pos, three_prime_pos, step = intron.start, intron.end, 1 

104 else: 

105 five_prime_pos, three_prime_pos, step = intron.end, intron.start, -1 

106 anchor = five_prime_pos if from_donor else three_prime_pos 

107 direction = step if from_donor else -step 

108 position_a = anchor + direction * (low - 1) 

109 position_b = anchor + direction * (high - 1) 

110 if position_a > position_b: 

111 position_a, position_b = (position_b, position_a) 

112 # clip to intron 

113 position_a = max(position_a, intron.start) 

114 position_b = min(position_b, intron.end) 

115 return (position_a, position_b) 

116 

117 

118def intron_offset_hit( 

119 fact_context: FactContext[FeatureOverlap], low: int, high: int, from_donor: bool 

120) -> bool: 

121 """Return whether the variant hits the given offsets in any nearby intron. 

122 

123 Reads ``_nearby_introns`` via ``get_fact`` rather than calling it directly: the ``@fact`` 

124 decorator returns the original, unguarded, unmemoised function unchanged, so a bare call 

125 would silently skip both its `requires` guard and the memo table. 

126 """ 

127 variant_min, variant_max = variant_genomic_span(fact_context) 

128 strand = fact_context.overlap.feature.strand 

129 nearby_introns = cast("tuple[Intron, ...]", get_fact(fact_context, "_nearby_introns")) 

130 for intron in nearby_introns: 

131 span_start, span_end = intron_offsets_to_genomic_span(intron, strand, low, high, from_donor) 

132 if genomic_overlap(variant_min, variant_max, span_start, span_end): 

133 return True 

134 return False 

135 

136 

137# -------------------------------------------------------------------------- 

138# Alleles: reference and alternate allele (only relevant for short variants not structural) 

139# -------------------------------------------------------------------------- 

140 

141 

142def _variant(fact_context: FactContext[FeatureOverlap]) -> VariationAllele: 

143 """The overlapping variation allele, narrowed from ``Feature``. 

144 

145 A cast solely because ``FeatureOverlap.overlapping_feature`` is typed with wider ``Feature`` rather than 

146 ``VariationAllele``. 

147 

148 Args: 

149 fact_context (FactContext[FeatureOverlap]): The overlap being evaluated. 

150 

151 Returns: 

152 VariationAllele: The overlapping feature, as the variation allele it always is. 

153 """ 

154 return cast("VariationAllele", fact_context.overlap.overlapping_feature) 

155 

156 

157def reference_allele(fact_context: FactContext[FeatureOverlap]) -> str: 

158 """Return the reference allele, treating ``None`` as empty.""" 

159 return _variant(fact_context).reference_allele or "" 

160 

161 

162def alternate_allele(fact_context: FactContext[FeatureOverlap]) -> str: 

163 """Return the alternate allele, treating ``None`` as empty.""" 

164 return _variant(fact_context).alternate_allele or "" 

165 

166 

167def reference_allele_length(fact_context: FactContext[FeatureOverlap]) -> int: 

168 """Return the reference-allele length, treating empty spellings as zero.""" 

169 allele = _variant(fact_context).reference_allele or "" 

170 return len(allele.replace(EMPTY_ALLELE, "")) 

171 

172 

173def alternate_allele_length(fact_context: FactContext[FeatureOverlap]) -> int: 

174 """Return the alternate-allele length, treating empty spellings as zero.""" 

175 allele = _variant(fact_context).alternate_allele or "" 

176 return len(allele.replace(EMPTY_ALLELE, "")) 

177 

178 

179def allele_length_difference(fact_context: FactContext[FeatureOverlap]) -> int: 

180 """Return the length change from reference to alternate allele.""" 

181 return alternate_allele_length(fact_context) - reference_allele_length(fact_context) 

182 

183 

184# -------------------------------------------------------------------------- 

185# Alleles: codons, peptides and full CDS 

186# -------------------------------------------------------------------------- 

187 

188 

189def get_codon_alleles(fact_context: FactContext[FeatureOverlap]) -> tuple[str, str] | None: 

190 """Return the internal codon-allele fact with its concrete type.""" 

191 return cast("tuple[str, str] | None", get_fact(fact_context, "_codon_alleles")) 

192 

193 

194def get_peptide_alleles(fact_context: FactContext[FeatureOverlap]) -> tuple[str, str] | None: 

195 """Return the internal peptide-allele fact with its concrete type.""" 

196 return cast("tuple[str, str] | None", get_fact(fact_context, "_peptide_alleles")) 

197 

198 

199def get_full_cds_peptide_alleles(fact_context: FactContext[FeatureOverlap]) -> tuple[str, str] | None: 

200 """Return the internal full-CDS peptide fact with its concrete type.""" 

201 return cast("tuple[str, str] | None", get_fact(fact_context, "_full_cds_peptide_alleles")) 

202 

203 

204# -------------------------------------------------------------------------- 

205# SO term 

206# -------------------------------------------------------------------------- 

207 

208 

209def variant_class_so_term(fact_context: FactContext[FeatureOverlap]) -> ClassSOTerm | None: 

210 """Return the class SO term from the variation that owns the overlapping allele.""" 

211 variation = _variant(fact_context).variation 

212 return variation.class_so_term if variation is not None else None