Coverage for src/python/ensembl/vep/models/genic/exon.py: 92%
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"""Exon class."""
16from __future__ import annotations
18import logging
19from functools import cached_property
20from threading import RLock
21from attrs import define, field
23from ensembl.vep.models import Feature
24from ensembl.vep.common import ExonPhase, invalidates
26logger = logging.getLogger(__name__)
29@define(kw_only=True, auto_attribs=True)
30class Exon(Feature):
31 """
32 Represents an exon.
34 Attributes:
35 stable_id (str):
36 Ensembl stable exon identifier (e.g. "ENSE00003666667").
37 version (int | None):
38 Version of the stable identifier.
39 phase (ExonPhase | None):
40 The phase of the exon, which is the number of bases of the first codon
41 that are on the previous exon/intron. The value can be 0, 1, or 2.
42 end_phase (ExonPhase | None):
43 Number of bases of the last codon if it is incomplete. The value can be 0, 1, or 2.
44 """
46 stable_id: str
47 version: int | None = field(default=None, init=False)
48 phase: ExonPhase | None = field(default=None, init=False, on_setattr=invalidates("_frame"))
49 end_phase: ExonPhase | None = field(default=None, init=False)
50 _lock: RLock = field(factory=RLock, init=False, repr=False)
52 @property
53 def frame(self) -> int | None:
54 """Public accessor for _frame.
56 Returns:
57 int | None: The frame of the exon, or None if not applicable.
58 """
59 return self._frame
61 @cached_property
62 def _frame(self) -> int | None:
63 """
64 Returns the frame of the exon.
66 Frame tells where the exon starts in relation to the reading frame as given by the phase in GFF.
68 Returns:
69 int | None: The frame of the exon.
70 """
71 if self.phase is None or self.phase == ExonPhase.UNKNOWN:
72 logger.warning(
73 f"Cannot calculate frame for exon {self.stable_id} because phase is None or UNKNOWN"
74 )
75 return None
77 # TBD: why we use module of start position? instead would it be simple convert - 0->0, 1->2, 2->1?
78 if self.phase == ExonPhase.ZERO:
79 return self.start % 3
80 if self.phase == ExonPhase.ONE:
81 return (self.start + 2) % 3
82 if self.phase == ExonPhase.TWO: 82 ↛ 85line 82 didn't jump to line 85 because the condition on line 82 was always true
83 return (self.start + 1) % 3
85 logger.warning(
86 f"Cannot calculate frame for exon {self.stable_id} because phase is invalid: {self.phase}"
87 )
88 return None