Coverage for src/python/ensembl/vep/models/overlap/custom_variation_allele.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"""Custom-feature overlap model.""" 

15 

16from __future__ import annotations 

17 

18from typing import TYPE_CHECKING 

19 

20from attrs import define, field 

21 

22from ensembl.vep.models.custom_feature import CustomFeature 

23from ensembl.vep.models.overlap.feature_overlap import FeatureOverlap 

24 

25if TYPE_CHECKING: 

26 from ensembl.vep.models.variation.variation_allele import VariationAllele 

27 

28__all__ = ["CustomVariationAllele"] 

29 

30 

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

32class CustomVariationAllele(FeatureOverlap): 

33 """Represent an association between a custom feature and variation allele. 

34 

35 Attributes: 

36 feature (CustomFeature): The custom record represented as a feature. 

37 overlapping_feature (VariationAllele): The associated input allele. 

38 summary_statistics (dict[str, object]): Track- or annotator-derived summary values. 

39 """ 

40 

41 feature: CustomFeature 

42 overlapping_feature: VariationAllele 

43 summary_statistics: dict[str, object] = field(factory=dict) 

44 

45 @property 

46 def overlap_length(self) -> int: 

47 """Return the number of shared reference bases.""" 

48 start = max(self.feature.start, self.overlapping_feature.start) 

49 end = min(self.feature.end, self.overlapping_feature.end) 

50 return max(0, end - start + 1) 

51 

52 @property 

53 def feature_overlap_fraction(self) -> float: 

54 """Return the fraction of the custom feature sharing reference bases.""" 

55 feature_length = max(0, self.feature.end - self.feature.start + 1) 

56 if feature_length == 0: 56 ↛ 57line 56 didn't jump to line 57 because the condition on line 56 was never true

57 return 0.0 

58 return self.overlap_length / feature_length 

59 

60 @property 

61 def variation_overlap_fraction(self) -> float | None: 

62 """Return the fraction of the allele's reference span shared with the feature. 

63 

64 Insertions have no reference span, so their fraction is undefined even when the selected 

65 matcher associates the insertion point with this record. 

66 """ 

67 variation_length = max(0, self.overlapping_feature.end - self.overlapping_feature.start + 1) 

68 if variation_length == 0: 

69 return None 

70 return self.overlap_length / variation_length