Coverage for src/python/ensembl/vep/annotators/consequence/facts/position.py: 99%
144 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"""Facts describing a variant's position relative to its feature."""
16from __future__ import annotations
18from typing import cast
20from ensembl.vep.annotators.consequence.facts.base import get_fact, register_fact
21from ensembl.vep.annotators.consequence.facts.helper import (
22 downstream_distance,
23 upstream_distance,
24 genomic_overlap,
25 intron_offset_hit,
26 variant_genomic_span,
27)
28from ensembl.vep.annotators.consequence.context.fact import FactContext
29from ensembl.vep.common import CostTier, Strand
30from ensembl.vep.models.genic import Intron
31from ensembl.vep.models.overlap import FeatureOverlap, TranscriptVariationAllele
33__all__ = [
34 "contain_feature",
35 "is_downstream",
36 "is_upstream",
37 "overlap_acceptor_site",
38 "overlap_cds",
39 "overlap_donor_5th_base",
40 "overlap_donor_region",
41 "overlap_donor_site",
42 "overlap_exon",
43 "overlap_feature",
44 "overlap_five_prime_utr",
45 "overlap_intron",
46 "overlap_intron_boundary",
47 "overlap_intron_interior",
48 "overlap_polypyrimidine_tract",
49 "overlap_splice_region",
50 "overlap_start_codon",
51 "overlap_stop_codon",
52 "overlap_three_prime_utr",
53 "partial_last_codon",
54 "within_any_exon",
55 "within_feature",
56]
58# --------------------------------------------------------------------------
59# Placement relative to the whole feature.
60# --------------------------------------------------------------------------
63@register_fact(CostTier.BOUNDS)
64def overlap_feature(fact_context: FactContext[FeatureOverlap]) -> bool:
65 """Return whether the variant overlaps the feature's genomic span.
67 The variant can
68 - just intersect the feature's span
69 - be fully contained in the feature's span
70 - or, fully contain the feature's span
71 """
72 variant_min, variant_max = variant_genomic_span(fact_context)
73 transcript = fact_context.overlap.feature
74 return genomic_overlap(variant_min, variant_max, transcript.start, transcript.end)
77@register_fact(CostTier.BOUNDS)
78def contain_feature(fact_context: FactContext[FeatureOverlap]) -> bool:
79 """Return whether the variant contains the feature's genomic span.
81 Not zone-mappable: a zone-map union answers "does the span touch this interval", not
82 "is the span fully inside one specific interval".
83 """
84 variant_min, variant_max = variant_genomic_span(fact_context)
85 transcript = fact_context.overlap.feature
86 return variant_min <= transcript.start and variant_max >= transcript.end
89@register_fact(CostTier.BOUNDS)
90def within_feature(fact_context: FactContext[FeatureOverlap]) -> bool:
91 """Return whether the variant lies within the feature's genomic span.
93 Not zone-mappable.
94 """
95 variant_min, variant_max = variant_genomic_span(fact_context)
96 transcript = fact_context.overlap.feature
97 return transcript.start <= variant_min and variant_max <= transcript.end
100@register_fact(CostTier.BOUNDS)
101def is_upstream(fact_context: FactContext[FeatureOverlap]) -> bool:
102 """Return whether the variant lies within the upstream pairing distance."""
103 variant_min, variant_max = variant_genomic_span(fact_context)
104 transcript = fact_context.overlap.feature
105 distance = upstream_distance(fact_context)
106 if transcript.strand == Strand.FORWARD:
107 return 0 < transcript.start - variant_max <= distance
108 return 0 < variant_min - transcript.end <= distance
111@register_fact(CostTier.BOUNDS)
112def is_downstream(fact_context: FactContext[FeatureOverlap]) -> bool:
113 """Return whether the variant lies within the downstream pairing distance."""
114 variant_min, variant_max = variant_genomic_span(fact_context)
115 transcript = fact_context.overlap.feature
116 distance = downstream_distance(fact_context)
117 if transcript.strand == Strand.FORWARD:
118 return 0 < variant_min - transcript.end <= distance
119 return 0 < transcript.start - variant_max <= distance
122# --------------------------------------------------------------------------
123# Placement within a transcript
124# --------------------------------------------------------------------------
127@register_fact(CostTier.INTERVAL, requires=("is_transcript", "overlap_feature"))
128def overlap_exon(fact_context: FactContext[TranscriptVariationAllele]) -> bool:
129 """Return whether the variant overlaps an exon."""
130 variant_min, variant_max = variant_genomic_span(fact_context)
131 return any(
132 genomic_overlap(variant_min, variant_max, exon.start, exon.end)
133 for exon in fact_context.overlap.feature.spliced_exons
134 )
137@register_fact(CostTier.INTERVAL, requires=("is_transcript", "overlap_feature"))
138def within_any_exon(fact_context: FactContext[TranscriptVariationAllele]) -> bool:
139 """Return whether the variant lies within one exon.
141 Not zone-mappable.
142 """
143 variant_min, variant_max = variant_genomic_span(fact_context)
144 return any(
145 exon.start <= variant_min and variant_max <= exon.end
146 for exon in fact_context.overlap.feature.spliced_exons
147 )
150@register_fact(CostTier.INTERVAL, requires=("is_transcript", "overlap_feature"))
151def overlap_intron(fact_context: FactContext[TranscriptVariationAllele]) -> bool:
152 """Return whether the variant overlaps an intron."""
153 variant_min, variant_max = variant_genomic_span(fact_context)
154 return any(
155 genomic_overlap(variant_min, variant_max, intron.start, intron.end)
156 for intron in fact_context.overlap.feature.introns
157 )
160@register_fact(CostTier.INTERVAL, requires=("is_transcript", "overlap_feature"))
161def overlap_intron_interior(fact_context: FactContext[TranscriptVariationAllele]) -> bool:
162 """Return whether the variant overlaps an intron away from its splice sites.
164 VEP's ``intronic`` test (``BaseTranscriptVariationAllele::_intron_effects``): overlap with
165 ``intron.start + 2 .. intron.end - 2``. An insertion's span is its two flanking bases, so one
166 at either end of that window counts, as VEP's own insertion special case has it.
167 """
168 variant_min, variant_max = variant_genomic_span(fact_context)
169 return any(
170 genomic_overlap(variant_min, variant_max, intron.start + 2, intron.end - 2)
171 for intron in fact_context.overlap.feature.introns
172 )
175@register_fact(CostTier.INTERVAL, requires=("is_transcript", "overlap_feature"))
176def overlap_intron_boundary(fact_context: FactContext[TranscriptVariationAllele]) -> bool:
177 """Return whether the variant overlaps an intron boundary window."""
178 variant_min, variant_max = variant_genomic_span(fact_context)
179 return any(
180 genomic_overlap(variant_min, variant_max, intron.start - 3, intron.start + 7)
181 or genomic_overlap(variant_min, variant_max, intron.end - 7, intron.end + 3)
182 for intron in fact_context.overlap.feature.introns
183 )
186@register_fact(CostTier.INTERVAL)
187def _nearby_introns(fact_context: FactContext[TranscriptVariationAllele]) -> tuple[Intron, ...]:
188 """Return every intron within 20 bases of the variant."""
189 variant_min, variant_max = variant_genomic_span(fact_context)
190 transcript = fact_context.overlap.feature
191 return tuple(
192 intron
193 for intron in transcript.introns
194 if genomic_overlap(variant_min - 20, variant_max + 20, intron.start, intron.end)
195 )
198@register_fact(
199 CostTier.INTERVAL,
200 deps=("_nearby_introns",),
201 requires=("is_transcript", "overlap_feature", "overlap_intron_boundary"),
202)
203def overlap_donor_site(fact_context: FactContext[TranscriptVariationAllele]) -> bool:
204 """Return whether the variant overlaps donor-site bases +1 or +2."""
205 return intron_offset_hit(fact_context, 1, 2, from_donor=True)
208@register_fact(
209 CostTier.INTERVAL,
210 deps=("_nearby_introns",),
211 requires=("is_transcript", "overlap_feature", "overlap_intron_boundary"),
212)
213def overlap_acceptor_site(fact_context: FactContext[TranscriptVariationAllele]) -> bool:
214 """Return whether the variant overlaps acceptor-site bases -1 or -2."""
215 return intron_offset_hit(fact_context, 1, 2, from_donor=False)
218@register_fact(
219 CostTier.INTERVAL,
220 deps=("_nearby_introns",),
221 requires=("is_transcript", "overlap_feature", "overlap_intron_boundary"),
222)
223def overlap_donor_5th_base(fact_context: FactContext[TranscriptVariationAllele]) -> bool:
224 """Return whether the variant overlaps donor base +5."""
225 return intron_offset_hit(fact_context, 5, 5, from_donor=True)
228@register_fact(
229 CostTier.INTERVAL,
230 deps=("_nearby_introns",),
231 requires=("is_transcript", "overlap_feature", "overlap_intron_boundary"),
232)
233def overlap_donor_region(fact_context: FactContext[TranscriptVariationAllele]) -> bool:
234 """Return whether the variant overlaps donor-region bases +3 to +6."""
235 return intron_offset_hit(fact_context, 3, 6, from_donor=True)
238@register_fact(
239 CostTier.INTERVAL,
240 deps=("_nearby_introns",),
241 requires=("is_transcript", "overlap_feature", "overlap_intron"),
242)
243def overlap_polypyrimidine_tract(fact_context: FactContext[TranscriptVariationAllele]) -> bool:
244 """Return whether the variant overlaps polypyrimidine-tract bases -3 to -17."""
245 return intron_offset_hit(fact_context, 3, 17, from_donor=False)
248@register_fact(
249 CostTier.INTERVAL,
250 deps=("_nearby_introns",),
251 requires=("is_transcript", "overlap_feature", "overlap_intron_boundary"),
252)
253def overlap_splice_region(fact_context: FactContext[TranscriptVariationAllele]) -> bool:
254 """Return whether the variant overlaps an exonic or intronic splice region."""
255 if intron_offset_hit(fact_context, 3, 8, from_donor=True):
256 return True
257 if intron_offset_hit(fact_context, 3, 8, from_donor=False):
258 return True
259 variant_min, variant_max = variant_genomic_span(fact_context)
260 for intron in fact_context.overlap.feature.introns:
261 if genomic_overlap(variant_min, variant_max, intron.start - 3, intron.start - 1):
262 return True
263 if genomic_overlap(variant_min, variant_max, intron.end + 1, intron.end + 3):
264 return True
265 return False
268# --------------------------------------------------------------------------
269# Placement within the coding sequence (CDS) - requires mapping genomic-to-CDS coordinates.
270# --------------------------------------------------------------------------
273@register_fact(CostTier.MAPPING)
274def _cds_footprint(fact_context: FactContext[TranscriptVariationAllele]) -> tuple[int, int] | None:
275 """Return the zero-based CDS offset span touched by the variant.
277 Delegates to Mapper.genomic_span_to_cds_offsets, which intersects the variant span against the
278 CDS's genomic spans directly (O(exon count)) rather than mapping every individual genomic
279 position in the variant's span (O(span length)) which can be very large for a large structural variant.
281 No accelerator needed here: the block-overlap approach is already O(exon count), which the O(1)
282 per-position MapperIndex cannot improve on further.
284 NOTE: It is important to check overlap with every coding span, not just the two endpoints of the variant's
285 genomic span. A variant can have both endpoints outside of coding exons but still overlap the CDS.
286 """
287 transcript = fact_context.overlap.feature
288 variant_min, variant_max = variant_genomic_span(fact_context)
289 mapper = transcript.cds_mapper
290 if mapper is None:
291 return None
292 return mapper.genomic_span_to_offsets(variant_min, variant_max)
295@register_fact(
296 CostTier.MAPPING,
297 deps=("_cds_footprint",),
298 requires=(
299 "is_transcript",
300 "overlap_feature",
301 "overlap_exon",
302 "has_translation",
303 ),
304)
305def overlap_cds(fact_context: FactContext[TranscriptVariationAllele]) -> bool:
306 """Return whether the variant overlaps the coding sequence."""
307 if fact_context.overlap.feature.translation is None:
308 return False
309 return get_fact(fact_context, "_cds_footprint") is not None
312@register_fact(
313 CostTier.INTERVAL,
314 requires=(
315 "is_transcript",
316 "overlap_feature",
317 "overlap_exon",
318 ),
319)
320def overlap_five_prime_utr(fact_context: FactContext[TranscriptVariationAllele]) -> bool:
321 """Return whether the variant overlaps the transcript's 5' UTR.
323 Tested against genomic spans of the five_prime_utr instead of just the span(start, end) as a UTR can
324 contain introns (non-UTR region) and therefore, can give inaccurate results.
325 """
326 spans = fact_context.overlap.feature.five_prime_utr_genomic_spans
327 if not spans:
328 return False
329 variant_min, variant_max = variant_genomic_span(fact_context)
330 return any(genomic_overlap(variant_min, variant_max, start, end) for start, end in spans)
333@register_fact(
334 CostTier.INTERVAL,
335 requires=(
336 "is_transcript",
337 "overlap_feature",
338 "overlap_exon",
339 ),
340)
341def overlap_three_prime_utr(fact_context: FactContext[TranscriptVariationAllele]) -> bool:
342 """Return whether the variant overlaps the transcript's 3' UTR."""
343 spans = fact_context.overlap.feature.three_prime_utr_genomic_spans
344 if not spans:
345 return False
346 variant_min, variant_max = variant_genomic_span(fact_context)
347 return any(genomic_overlap(variant_min, variant_max, start, end) for start, end in spans)
350@register_fact(
351 CostTier.MAPPING,
352 requires=(
353 "is_transcript",
354 "overlap_feature",
355 "overlap_exon",
356 "has_translation",
357 ),
358)
359def overlap_start_codon(fact_context: FactContext[TranscriptVariationAllele]) -> bool:
360 """Return whether the variant overlaps the start codon.
362 Similar to UTRs a start codon can split across an exon-exon junction and therefore should be tested
363 against all its genomic spans.
364 """
365 codon_spans = fact_context.overlap.feature.start_codon_genomic_spans()
366 if codon_spans is None:
367 return False
368 variant_min, variant_max = variant_genomic_span(fact_context)
369 return any(
370 genomic_overlap(variant_min, variant_max, span_start, span_end)
371 for span_start, span_end in codon_spans
372 )
375@register_fact(
376 CostTier.MAPPING,
377 requires=(
378 "is_transcript",
379 "overlap_feature",
380 "overlap_exon",
381 "overlap_cds",
382 ),
383)
384def overlap_stop_codon(fact_context: FactContext[TranscriptVariationAllele]) -> bool:
385 """Return whether the variant overlaps the stop codon."""
386 codon_spans = fact_context.overlap.feature.stop_codon_genomic_spans()
387 if codon_spans is None:
388 return False
389 variant_min, variant_max = variant_genomic_span(fact_context)
390 return any(
391 genomic_overlap(variant_min, variant_max, span_start, span_end)
392 for span_start, span_end in codon_spans
393 )
396@register_fact(
397 CostTier.MAPPING,
398 deps=("_cds_footprint",),
399 requires=(
400 "is_transcript",
401 "overlap_feature",
402 "overlap_exon",
403 ),
404)
405def partial_last_codon(fact_context: FactContext[TranscriptVariationAllele]) -> bool:
406 """Return whether the variant falls in an incomplete terminal codon."""
407 footprint = cast("tuple[int, int] | None", get_fact(fact_context, "_cds_footprint"))
408 if footprint is None:
409 return False
410 _, high_offset = footprint
412 cds_length = fact_context.overlap.feature.cds_length
413 if cds_length is None: 413 ↛ 414line 413 didn't jump to line 414 because the condition on line 413 was never true
414 return False
416 # The last codon that variant overlaps (high_offset) extends beyond the end of the CDS, meaning that
417 # the last codon is partial.
418 return (high_offset // 3) * 3 + 3 > cds_length