Coverage for src/python/ensembl/vep/matcher/overlap.py: 100%
31 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"""General interval-overlap matching."""
16from __future__ import annotations
18from attrs import define
20from ensembl.vep.common.enums import Strand
21from ensembl.vep.matcher.base import InsertionMatchPolicy, IntervalLike
23__all__ = ["OverlapMatcher"]
26@define(kw_only=True, auto_attribs=True, frozen=True)
27class OverlapMatcher:
28 """Matcher for containing or partially intersecting intervals.
30 A normal interval uses inclusive base coordinates. An insertion is encoded as
31 ``start=end+1`` and remains a zero-width interbase location during matching.
33 Attributes:
34 insertion_policy (InsertionMatchPolicy): Rule for associating an insertion with a feature.
35 """
37 insertion_policy: InsertionMatchPolicy = InsertionMatchPolicy.CONTAINED
39 def matches(self, left: IntervalLike, right: IntervalLike) -> bool:
40 """Return whether two inclusive intervals meet; an insertion is start == end + 1."""
42 if left.start == left.end + 1 and right.start == right.end + 1:
43 return left.start == right.start
44 if left.start == left.end + 1:
45 return self._insertion_matches(left, right, right)
46 if right.start == right.end + 1:
47 return self._insertion_matches(right, left, left)
48 return left.start <= right.end and left.end >= right.start
50 def _insertion_matches(
51 self,
52 insertion: IntervalLike,
53 feature_interval: IntervalLike,
54 feature: IntervalLike,
55 ) -> bool:
56 """Return whether an insertion point associates with a feature."""
58 if self.insertion_policy is InsertionMatchPolicy.CONTAINED:
59 return feature_interval.start < insertion.start <= feature_interval.end
60 if self.insertion_policy is InsertionMatchPolicy.TOUCHING:
61 return feature_interval.start <= insertion.start <= feature_interval.end + 1
62 return self._three_prime_flank_matches(insertion, feature_interval, feature)
64 @staticmethod
65 def _three_prime_flank_matches(
66 insertion: IntervalLike,
67 feature_interval: IntervalLike,
68 feature: IntervalLike,
69 ) -> bool:
70 """Return whether an insertion's feature-relative 3-prime flank is covered."""
72 strand = getattr(feature, "strand", Strand.UNKNOWN)
73 if strand is Strand.FORWARD:
74 flank = insertion.start
75 elif strand is Strand.REVERSE:
76 flank = insertion.end
77 else:
78 return False
79 return feature_interval.start <= flank <= feature_interval.end