Coverage for src/python/ensembl/vep/models/variation/variation.py: 97%
57 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"""Variation class."""
16from __future__ import annotations
18from functools import cached_property
19from threading import RLock
20from typing import Sequence
21from attrs import define, field
23from ensembl.vep.common import ClassSOTerm, Locus, invalidate, invalidates, infer_variant_so_term
24from ensembl.vep.models import Feature
25from ensembl.vep.models.overlap import OverlapConsequence
26from ensembl.vep.models.variation.variation_allele import VariationAllele
29@define(kw_only=True, auto_attribs=True)
30class Variation(Feature):
31 """Represents a variation to genomic sequences in a specific locus.
33 Attributes:
34 stable_id (str | None):
35 Stable identifier for the variation.
36 allele_string (str | None):
37 String representation of the alleles.
38 uploaded_locus (Locus | None):
39 Original region name, position and allele string supplied by the variation source.
40 Its ``seq_region_name`` is what output reports and what a run summary counts by; the
41 slice names the same region as the annotation sources spell it.
42 variation_alleles (list[VariationAllele] | None):
43 List of VariationAllele objects describing the alleles of the variation.
44 is_somatic (bool | None):
45 Indicates whether the variation is somatic.
46 class_so_term (str | None):
47 Sequence Ontology term describing the class of the variation.
48 most_severe_consequence (OverlapConsequence | None):
49 Most severe consequence type for the variation.
50 """
52 stable_id: str | None = field(default=None)
53 allele_string: str | None = field(
54 default=None,
55 on_setattr=invalidates("_variation_alleles", "_class_so_term", "_most_severe_consequence"),
56 )
57 uploaded_locus: Locus | None = field(default=None)
58 is_somatic: bool = field(default=False)
59 _lock: RLock = field(factory=RLock, init=False, repr=False)
61 @property
62 def variation_alleles(self) -> Sequence[VariationAllele]:
63 """Public accessor for the _variation_alleles.
65 Returns:
66 Sequence[VariationAllele]: The list of VariationAllele objects for the variation.
67 """
68 return self._variation_alleles
70 @cached_property
71 def _variation_alleles(self) -> Sequence[VariationAllele]:
72 """Returns the list of VariationAllele objects for the variation.
74 Returns:
75 Sequence[VariationAllele]: The list of VariationAllele objects for the variation.
76 """
77 invalidate(self, "_most_severe_consequence")
79 with self._lock:
80 variation_alleles = []
81 alleles = (self.allele_string or "").split("/")
83 ref = alleles[0]
84 alts = alleles[1:]
85 for alt in alts:
86 # ref is still same for all alleles, so we can use the start and end for all alleles
87 variation_allele = VariationAllele(
88 start=self.start,
89 end=self.end,
90 strand=self.strand,
91 slice=self.slice,
92 reference_allele=ref,
93 alternate_allele=alt,
94 variation=self,
95 )
97 variation_alleles.append(variation_allele)
99 return variation_alleles
101 @property
102 def most_severe_consequence(self) -> OverlapConsequence | None:
103 """Public accessor for the _most_severe_consequence.
105 Returns:
106 OverlapConsequence | None: The most severe consequence type for the variation
107 or None if not deducible.
108 """
109 return self._most_severe_consequence
111 @cached_property
112 def _most_severe_consequence(self) -> OverlapConsequence | None:
113 """Returns the most severe consequence type for the variation.
115 Returns:
116 OverlapConsequence | None: The most severe consequence type or None if not deducible.
117 """
118 highest = None
119 for variation_allele in self.variation_alleles or []:
120 for overlap_consequence in variation_allele.overlap_consequences or []:
121 highest = highest or overlap_consequence
122 if overlap_consequence.rank < highest.rank:
123 highest = overlap_consequence
124 elif overlap_consequence.rank == highest.rank and overlap_consequence.term < highest.term: 124 ↛ 125line 124 didn't jump to line 125 because the condition on line 124 was never true
125 highest = overlap_consequence
127 return highest
129 @property
130 def class_so_term(self) -> ClassSOTerm | None:
131 """Public accessor for the _class_so_term.
133 Returns:
134 ClassSOTerm | None: The SO term for the class of the variation.
135 """
136 return self._class_so_term
138 @cached_property
139 def _class_so_term(self) -> ClassSOTerm | None:
140 """
141 Return the SO term for the class of the variation.
143 Returns:
144 ClassSOTerm | None: The SO term string for the variation.
145 """
146 return infer_variant_so_term(self.allele_string or "", ref_correct=True)
148 def minimise_alleles(self) -> None:
149 """Minimise all alleles."""
150 for variation_allele in self.variation_alleles or []:
151 variation_allele.minimise()
153 def shift_alleles(self) -> None:
154 """Shift all alleles."""
155 for variation_allele in self.variation_alleles or []:
156 variation_allele.shift()