Source code for ensembl.vep.models.overlap.feature_overlap
# See the NOTICE file distributed with this work for additional information
# regarding copyright ownership.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""FeatureOverlap class."""
from __future__ import annotations
from typing import TYPE_CHECKING
from functools import cached_property
from threading import RLock
from attr import define, field
from ensembl.vep.models import Feature
from ensembl.vep.common import invalidates
if TYPE_CHECKING:
from ensembl.vep.models.overlap import OverlapConsequence
[docs]
@define(kw_only=True, auto_attribs=True)
class FeatureOverlap:
"""
Represents an overlap between a feature and a overlapping feature,
along with the consequences of that overlap.
Attributes:
feature (Feature):
The feature that is getting overlapped.
overlapping_feature (Feature):
The feature that is overlapping.
overlap_consequences (list[OverlapConsequence]):
A list of consequences the overlapping feature has on the feature.
"""
feature: Feature
overlapping_feature: Feature
overlap_consequences: list[OverlapConsequence] = field(
factory=list, init=False, on_setattr=invalidates("_most_severe_consequence", "_consequence_terms")
)
_lock: RLock = field(factory=RLock, init=False, repr=False)
@property
def most_severe_consequence(self) -> OverlapConsequence | None:
"""
Public accessor for _most_severe_consequence.
Returns:
OverlapConsequence | None: The most severe consequence, or None if there are no consequences.
"""
return self._most_severe_consequence
@cached_property
def _most_severe_consequence(self) -> OverlapConsequence | None:
"""
Returns the most severe consequence, or None if there are no consequences.
Returns:
OverlapConsequence | None: The most severe consequence, or None if there are no consequences.
"""
# Rank 1 is the most severe (see OverlapConsequence.rank), so the lowest rank wins; the
# tie-break on term keeps the answer stable across insertion order, as on Variation.
with self._lock:
most_severe_consequence = None
for consequence in self.overlap_consequences:
if most_severe_consequence is None or consequence.rank < most_severe_consequence.rank:
most_severe_consequence = consequence
elif (
consequence.rank == most_severe_consequence.rank
and consequence.term < most_severe_consequence.term
):
most_severe_consequence = consequence
return most_severe_consequence
@property
def consequence_terms(self) -> set[str]:
"""
Public accessor for _consequence_terms.
Returns:
set[str]: A set of all consequence terms.
"""
return self._consequence_terms
@cached_property
def _consequence_terms(self) -> set[str]:
"""
Returns a set of all consequence terms.
Returns:
set[str]: A set of all consequence terms.
"""
with self._lock:
return set(consequence.term for consequence in self.overlap_consequences)