Coverage for src/python/ensembl/vep/overlaps/feature.py: 94%

28 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"""Generic feature-overlap finding.""" 

15 

16from __future__ import annotations 

17 

18from collections.abc import Iterator 

19from typing import Generic 

20 

21from attrs import define 

22 

23from ensembl.vep.caches.interval_tree import Interval 

24from ensembl.vep.models import Feature 

25from ensembl.vep.models.overlap import FeatureOverlap 

26from ensembl.vep.models.variation import VariationAllele 

27from ensembl.vep.overlaps.base import ( 

28 CandidateLookup, 

29 CandidateIntervals, 

30 FeatureMatcher, 

31 FeatureT, 

32 MatchSubject, 

33 OverlapFactory, 

34 OverlapT, 

35 VariationAlleleBuffer, 

36) 

37 

38__all__ = ["FeatureOverlapFinder"] 

39 

40 

41def _feature_overlap(feature: Feature, variation_allele: VariationAllele) -> FeatureOverlap: 

42 """Construct a generic feature overlap.""" 

43 return FeatureOverlap(feature=feature, overlapping_feature=variation_allele) 

44 

45 

46def _feature_intervals(feature: Feature) -> tuple[Interval, ...]: 

47 """Return the feature's own interval as one buffer query.""" 

48 return (Interval(start=feature.start, end=feature.end),) 

49 

50 

51def _match_feature(feature: Feature) -> Feature: 

52 """Return the feature itself as the final matcher subject.""" 

53 return feature 

54 

55 

56@define(kw_only=True, auto_attribs=True) 

57class FeatureOverlapFinder(Generic[FeatureT, OverlapT]): 

58 """Overlap finder for source features and variation alleles. 

59 

60 Candidate lookup owns source, cache, and index selection. This keeps the finder independent of 

61 the feature source and cache implementations used by a workflow track. 

62 

63 Attributes: 

64 candidates (CandidateLookup[FeatureT]): Fetch candidates for each input buffer. 

65 candidate_intervals (CandidateIntervals[FeatureT]): Build buffer-index queries for a feature. 

66 match_subject (MatchSubject[FeatureT]): Build the final matcher subject for a feature. 

67 matcher (FeatureMatcher): Decide whether each candidate pair is associated. 

68 overlap_factory (OverlapFactory[FeatureT, OverlapT]): Construct matched overlap models. 

69 """ 

70 

71 candidates: CandidateLookup[FeatureT] 

72 matcher: FeatureMatcher 

73 candidate_intervals: CandidateIntervals[FeatureT] = _feature_intervals 

74 match_subject: MatchSubject[FeatureT] = _match_feature 

75 overlap_factory: OverlapFactory[FeatureT, OverlapT] = _feature_overlap # type: ignore[assignment] 

76 

77 def find(self, input_buffer: VariationAlleleBuffer) -> Iterator[OverlapT]: 

78 """Yield overlaps for every matching candidate pair.""" 

79 for feature in self.candidates(input_buffer): 

80 for variation_allele in input_buffer.get_objects_by_intervals(self.candidate_intervals(feature)): 

81 if self.matcher.matches(self.match_subject(feature), variation_allele): 81 ↛ 80line 81 didn't jump to line 80 because the condition on line 81 was always true

82 yield self.overlap_factory(feature, variation_allele)