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

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.""" 

15 

16from __future__ import annotations 

17 

18from collections.abc import Callable, Iterable, Iterator 

19from typing import Protocol, TypeVar, runtime_checkable 

20 

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 

26 

27__all__ = [ 

28 "CandidateLookup", 

29 "CandidateIntervals", 

30 "FeatureMatcher", 

31 "MatchSubject", 

32 "OverlapFactory", 

33 "OverlapFinder", 

34 "VariationAlleleBuffer", 

35] 

36 

37FeatureT = TypeVar("FeatureT", bound=Feature) 

38"""The feature type returned by a candidate lookup.""" 

39 

40CandidateFeatureT_co = TypeVar("CandidateFeatureT_co", bound=Feature, covariant=True) 

41"""The covariant feature type returned by a candidate lookup.""" 

42 

43OverlapT = TypeVar("OverlapT", bound=FeatureOverlap) 

44"""The overlap model returned by a finder.""" 

45 

46FoundOverlapT_co = TypeVar("FoundOverlapT_co", bound=FeatureOverlap, covariant=True) 

47"""The covariant overlap model yielded by a finder.""" 

48 

49 

50@runtime_checkable 

51class VariationAlleleBuffer(Protocol): 

52 """Input buffer exposing variation alleles.""" 

53 

54 @property 

55 def objects(self) -> Iterable[VariationAllele]: 

56 """Return the variation alleles held by the buffer.""" 

57 raise NotImplementedError 

58 

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 

62 

63 

64class CandidateLookup(Protocol[CandidateFeatureT_co]): 

65 """Source-candidate lookup for an input buffer.""" 

66 

67 def __call__(self, input_buffer: VariationAlleleBuffer) -> Iterable[CandidateFeatureT_co]: 

68 """Return candidate features for ``input_buffer``.""" 

69 raise NotImplementedError 

70 

71 

72CandidateIntervals = Callable[[FeatureT], Iterable[Interval]] 

73"""Build buffer-index query intervals for one source feature.""" 

74 

75 

76class FeatureMatcher(Protocol): 

77 """Association rule for a feature and variation allele.""" 

78 

79 def matches(self, feature: IntervalLike, variation_allele: IntervalLike) -> bool: 

80 """Return whether ``feature`` matches ``variation_allele``.""" 

81 raise NotImplementedError 

82 

83 

84MatchSubject = Callable[[FeatureT], IntervalLike] 

85"""Build the interval used for final association of one source feature.""" 

86 

87 

88OverlapFactory = Callable[[FeatureT, VariationAllele], OverlapT] 

89"""Construct a typed overlap from a matched feature and variation allele.""" 

90 

91 

92@runtime_checkable 

93class OverlapFinder(Protocol[FoundOverlapT_co]): 

94 """Typed-overlap finder for one input buffer.""" 

95 

96 def find(self, input_buffer: VariationAlleleBuffer) -> Iterator[FoundOverlapT_co]: 

97 """Yield overlaps for ``input_buffer``.""" 

98 raise NotImplementedError