Coverage for src/python/ensembl/vep/models/overlap/feature_overlap.py: 87%

33 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"""FeatureOverlap class.""" 

15 

16from __future__ import annotations 

17 

18from typing import TYPE_CHECKING 

19from functools import cached_property 

20from threading import RLock 

21from attr import define, field 

22 

23from ensembl.vep.models import Feature 

24from ensembl.vep.common import invalidates 

25 

26if TYPE_CHECKING: 

27 from ensembl.vep.models.overlap import OverlapConsequence 

28 

29 

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

31class FeatureOverlap: 

32 """ 

33 Represents an overlap between a feature and a overlapping feature, 

34 along with the consequences of that overlap. 

35 

36 Attributes: 

37 feature (Feature): 

38 The feature that is getting overlapped. 

39 overlapping_feature (Feature): 

40 The feature that is overlapping. 

41 overlap_consequences (list[OverlapConsequence]): 

42 A list of consequences the overlapping feature has on the feature. 

43 """ 

44 

45 feature: Feature 

46 overlapping_feature: Feature 

47 overlap_consequences: list[OverlapConsequence] = field( 

48 factory=list, init=False, on_setattr=invalidates("_most_severe_consequence", "_consequence_terms") 

49 ) 

50 _lock: RLock = field(factory=RLock, init=False, repr=False) 

51 

52 @property 

53 def most_severe_consequence(self) -> OverlapConsequence | None: 

54 """ 

55 Public accessor for _most_severe_consequence. 

56 

57 Returns: 

58 OverlapConsequence | None: The most severe consequence, or None if there are no consequences. 

59 """ 

60 return self._most_severe_consequence 

61 

62 @cached_property 

63 def _most_severe_consequence(self) -> OverlapConsequence | None: 

64 """ 

65 Returns the most severe consequence, or None if there are no consequences. 

66 

67 Returns: 

68 OverlapConsequence | None: The most severe consequence, or None if there are no consequences. 

69 """ 

70 # Rank 1 is the most severe (see OverlapConsequence.rank), so the lowest rank wins; the 

71 # tie-break on term keeps the answer stable across insertion order, as on Variation. 

72 with self._lock: 

73 most_severe_consequence = None 

74 for consequence in self.overlap_consequences: 

75 if most_severe_consequence is None or consequence.rank < most_severe_consequence.rank: 75 ↛ 77line 75 didn't jump to line 77 because the condition on line 75 was always true

76 most_severe_consequence = consequence 

77 elif ( 

78 consequence.rank == most_severe_consequence.rank 

79 and consequence.term < most_severe_consequence.term 

80 ): 

81 most_severe_consequence = consequence 

82 return most_severe_consequence 

83 

84 @property 

85 def consequence_terms(self) -> set[str]: 

86 """ 

87 Public accessor for _consequence_terms. 

88 

89 Returns: 

90 set[str]: A set of all consequence terms. 

91 """ 

92 return self._consequence_terms 

93 

94 @cached_property 

95 def _consequence_terms(self) -> set[str]: 

96 """ 

97 Returns a set of all consequence terms. 

98 

99 Returns: 

100 set[str]: A set of all consequence terms. 

101 """ 

102 with self._lock: 

103 return set(consequence.term for consequence in self.overlap_consequences)