Coverage for src/python/ensembl/vep/overlaps/transcript.py: 91%
39 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"""Transcript-overlap finding."""
16from __future__ import annotations
18from attrs import define, field
20from ensembl.vep.caches.interval_tree import Interval
21from ensembl.vep.common import DEFAULT_DISTANCE, Readthrough, Strand
22from ensembl.vep.models.genic import Transcript
23from ensembl.vep.models.overlap import TranscriptStructuralVariationAllele, TranscriptVariationAllele
24from ensembl.vep.models.variation import StructuralVariationAllele, VariationAllele
25from ensembl.vep.overlaps.base import CandidateLookup, FeatureMatcher
26from ensembl.vep.overlaps.feature import FeatureOverlapFinder
28__all__ = ["TranscriptOverlapFinder", "transcript_overlap"]
31def transcript_overlap(
32 transcript: Transcript,
33 variation_allele: VariationAllele,
34 *,
35 readthrough: Readthrough = Readthrough.NONE,
36) -> TranscriptVariationAllele:
37 """Construct the appropriate transcript overlap model."""
38 overlap_class = (
39 TranscriptStructuralVariationAllele
40 if isinstance(variation_allele, StructuralVariationAllele)
41 else TranscriptVariationAllele
42 )
43 return overlap_class(
44 feature=transcript,
45 overlapping_feature=variation_allele,
46 readthrough=readthrough,
47 )
50@define(kw_only=True, auto_attribs=True)
51class TranscriptOverlapFinder(FeatureOverlapFinder[Transcript, TranscriptVariationAllele]):
52 """Transcript-overlap finder for a variation-allele buffer.
54 Attributes:
55 upstream_distance (int): Genomic bases considered before a transcript's 5' end.
56 downstream_distance (int): Genomic bases considered past a transcript's 3' end.
57 """
59 candidates: CandidateLookup[Transcript]
60 matcher: FeatureMatcher
61 readthrough: Readthrough = field(default=Readthrough.NONE)
62 upstream_distance: int = field(default=DEFAULT_DISTANCE)
63 downstream_distance: int = field(default=DEFAULT_DISTANCE)
65 def __attrs_post_init__(self) -> None:
66 for name in ("upstream_distance", "downstream_distance"):
67 distance = getattr(self, name)
68 if isinstance(distance, bool) or not isinstance(distance, int): 68 ↛ 69line 68 didn't jump to line 69 because the condition on line 68 was never true
69 raise TypeError(f"Transcript overlap {name} must be an integer.")
70 if distance < 0: 70 ↛ 71line 70 didn't jump to line 71 because the condition on line 70 was never true
71 raise ValueError(f"Transcript overlap {name} must not be negative.")
72 self.overlap_factory = self._overlap_factory
73 self.candidate_intervals = self._candidate_intervals
74 self.match_subject = self._match_subject
76 def _candidate_intervals(self, transcript: Transcript) -> tuple[Interval, ...]:
77 """Return the transcript span expanded for flanking consequences.
79 Which side each distance applies to follows the transcript: a reverse-strand transcript's
80 5' end is its highest coordinate, so the upstream distance extends past ``end`` there.
81 """
82 before, after = self.upstream_distance, self.downstream_distance
83 if transcript.strand == Strand.REVERSE:
84 before, after = after, before
85 return (
86 Interval(
87 start=max(1, transcript.start - before),
88 end=transcript.end + after,
89 ),
90 )
92 def _match_subject(self, transcript: Transcript) -> Interval:
93 """Return the transcript influence interval used for final association."""
94 return self._candidate_intervals(transcript)[0]
96 def _overlap_factory(
97 self,
98 transcript: Transcript,
99 variation_allele: VariationAllele,
100 ) -> TranscriptVariationAllele:
101 """Construct a transcript overlap using this finder's readthrough policy."""
102 return transcript_overlap(transcript, variation_allele, readthrough=self.readthrough)