Coverage for src/python/ensembl/vep/overlaps/base.py: 100%
35 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"""Contracts shared by overlap finders."""
16from __future__ import annotations
18from collections.abc import Callable, Iterable, Iterator
19from typing import Protocol, TypeVar, runtime_checkable
21from ensembl.vep.models import Feature
22from ensembl.vep.models.overlap import FeatureOverlap
23from ensembl.vep.models.variation import VariationAllele
24from ensembl.vep.caches.interval_tree import Interval
25from ensembl.vep.matcher.base import IntervalLike
27__all__ = [
28 "CandidateLookup",
29 "CandidateIntervals",
30 "FeatureMatcher",
31 "MatchSubject",
32 "OverlapFactory",
33 "OverlapFinder",
34 "VariationAlleleBuffer",
35]
37FeatureT = TypeVar("FeatureT", bound=Feature)
38"""The feature type returned by a candidate lookup."""
40CandidateFeatureT_co = TypeVar("CandidateFeatureT_co", bound=Feature, covariant=True)
41"""The covariant feature type returned by a candidate lookup."""
43OverlapT = TypeVar("OverlapT", bound=FeatureOverlap)
44"""The overlap model returned by a finder."""
46FoundOverlapT_co = TypeVar("FoundOverlapT_co", bound=FeatureOverlap, covariant=True)
47"""The covariant overlap model yielded by a finder."""
50@runtime_checkable
51class VariationAlleleBuffer(Protocol):
52 """Input buffer exposing variation alleles."""
54 @property
55 def objects(self) -> Iterable[VariationAllele]:
56 """Return the variation alleles held by the buffer."""
57 raise NotImplementedError
59 def get_objects_by_intervals(self, intervals: Iterable[Interval]) -> list[VariationAllele]:
60 """Return alleles that are candidates for any query interval."""
61 raise NotImplementedError
64class CandidateLookup(Protocol[CandidateFeatureT_co]):
65 """Source-candidate lookup for an input buffer."""
67 def __call__(self, input_buffer: VariationAlleleBuffer) -> Iterable[CandidateFeatureT_co]:
68 """Return candidate features for ``input_buffer``."""
69 raise NotImplementedError
72CandidateIntervals = Callable[[FeatureT], Iterable[Interval]]
73"""Build buffer-index query intervals for one source feature."""
76class FeatureMatcher(Protocol):
77 """Association rule for a feature and variation allele."""
79 def matches(self, feature: IntervalLike, variation_allele: IntervalLike) -> bool:
80 """Return whether ``feature`` matches ``variation_allele``."""
81 raise NotImplementedError
84MatchSubject = Callable[[FeatureT], IntervalLike]
85"""Build the interval used for final association of one source feature."""
88OverlapFactory = Callable[[FeatureT, VariationAllele], OverlapT]
89"""Construct a typed overlap from a matched feature and variation allele."""
92@runtime_checkable
93class OverlapFinder(Protocol[FoundOverlapT_co]):
94 """Typed-overlap finder for one input buffer."""
96 def find(self, input_buffer: VariationAlleleBuffer) -> Iterator[FoundOverlapT_co]:
97 """Yield overlaps for ``input_buffer``."""
98 raise NotImplementedError