Coverage for src/python/ensembl/vep/matcher/reciprocal.py: 100%

19 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"""Reciprocal interval-overlap association.""" 

15 

16from __future__ import annotations 

17 

18from attrs import define 

19 

20from ensembl.vep.matcher.base import IntervalLike 

21 

22__all__ = ["ReciprocalOverlapMatcher"] 

23 

24 

25@define(kw_only=True, auto_attribs=True, frozen=True) 

26class ReciprocalOverlapMatcher: 

27 """Matcher requiring both configured reference-span fractions. 

28 

29 Zero-width insertions never match reciprocally because their reference-span fraction is 

30 undefined. Use ``OverlapMatcher`` for point association or ``ExactCoordinateMatcher`` for 

31 identical insertion positions. 

32 

33 Attributes: 

34 left_minimum (float): Minimum fraction of the left reference span covered. 

35 right_minimum (float): Minimum fraction of the right reference span covered. 

36 """ 

37 

38 left_minimum: float = 0.5 

39 right_minimum: float = 0.5 

40 

41 def __attrs_post_init__(self) -> None: 

42 for fraction in (self.left_minimum, self.right_minimum): 

43 if not 0.0 <= fraction <= 1.0: 

44 raise ValueError("Reciprocal overlap fractions must be between zero and one.") 

45 

46 def matches(self, left: IntervalLike, right: IntervalLike) -> bool: 

47 """Return whether both intervals meet their configured overlap fraction.""" 

48 left_length = left.end - left.start + 1 

49 right_length = right.end - right.start + 1 

50 if left_length <= 0 or right_length <= 0: 

51 return False 

52 

53 overlap_length = max(0, min(left.end, right.end) - max(left.start, right.start) + 1) 

54 return ( 

55 overlap_length / left_length >= self.left_minimum 

56 and overlap_length / right_length >= self.right_minimum 

57 )