Coverage for src/python/ensembl/vep/models/feature.py: 92%
93 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"""Feature 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.sequence import Slice, CoordSystem
24from ensembl.vep.common import Strand, UnsafeSetterMixin, invalidates
26logger = logging.getLogger(__name__)
29@define(kw_only=True, auto_attribs=True)
30class Feature(UnsafeSetterMixin):
31 """
32 Represents a genomic feature in a specific locus.
34 Attributes:
35 start: Start position relative to slice (1-based, inclusive).
36 end: End position relative to slice (1-based, inclusive).
37 strand: Feature orientation relative to slice.
38 slice: Slice object describing the feature context.
39 seq: Associated biological sequence.
40 """
42 start: int = field(on_setattr=invalidates("_length", "_seq_region_start", "_seq"))
43 end: int = field(on_setattr=invalidates("_length", "_seq_region_end", "_seq"))
44 strand: Strand = field(on_setattr=invalidates("_seq_region_strand"))
45 slice: Slice | None = field(
46 default=None,
47 on_setattr=invalidates("_seq_region_start", "_seq_region_end", "_seq_region_strand", "_seq"),
48 )
49 _lock: RLock = field(factory=RLock, init=False, repr=False)
51 @property
52 def length(self) -> int:
53 """
54 Public accessor for the _length.
56 Returns:
57 The length of the feature.
58 """
59 return self._length
61 @cached_property
62 def _length(self) -> int:
63 """
64 Returns the length of the feature.
66 Returns:
67 The length of the feature.
68 """
69 with self._lock:
70 return self.end - self.start + 1
72 @property
73 def seq_region_start(self) -> int | None:
74 """
75 Public accessor for the _seq_region_start.
77 Returns:
78 The start position in the sequence region,
79 or None if the feature has no attached slice.
80 """
81 return self._seq_region_start
83 @cached_property
84 def _seq_region_start(self) -> int | None:
85 """
86 Returns the start position in the sequence region.
88 If the attached slice is on the reverse strand,
89 seq_region_start corresponds to the feature.end position
91 Returns:
92 The start position in the sequence region,
93 or None if the feature has no attached slice.
94 """
95 with self._lock:
96 if self.slice is None:
97 logger.warning("Cannot obtain seq_region_start for a feature without attached slice.")
98 return None
99 if self.slice.strand == Strand.REVERSE:
100 return self.slice.end - self.end + 1
101 return self.slice.start + self.start - 1
103 @property
104 def seq_region_end(self) -> int | None:
105 """
106 Public accessor for the _seq_region_end.
108 Returns:
109 The end position in the sequence region, or None if the feature has no attached slice.
110 """
111 return self._seq_region_end
113 @cached_property
114 def _seq_region_end(self) -> int | None:
115 """
116 Returns the end position in the sequence region.
118 If the attached slice is on the reverse strand,
119 seq_region_end corresponds to the feature.start position
121 Returns:
122 The end position in the sequence region, or None if the feature has no attached slice.
123 """
124 with self._lock:
125 if self.slice is None:
126 logger.warning("Cannot obtain seq_region_end for a feature without attached slice.")
127 return None
128 if self.slice.strand == Strand.REVERSE:
129 return self.slice.end - self.start + 1
130 return self.slice.start + self.end - 1
132 @property
133 def seq_region_strand(self) -> Strand | None:
134 """
135 Public accessor for the _seq_region_strand.
137 Returns:
138 The strand in the sequence region, or None if the feature has no attached slice.
139 """
140 return self._seq_region_strand
142 @cached_property
143 def _seq_region_strand(self) -> Strand | None:
144 """
145 Returns the strand in the sequence region.
147 Returns:
148 The strand in the sequence region, or None if the feature has no attached slice.
149 """
150 with self._lock:
151 if self.slice is None:
152 logger.warning("Cannot obtain seq_region_strand for a feature without attached slice.")
153 return None
154 return Strand(self.slice.strand.value * self.strand.value)
156 @property
157 def seq(self) -> str | None:
158 """
159 Public accessor for the _seq.
161 Returns:
162 The associated biological sequence, or None if unavailable.
163 """
164 return self._seq
166 @cached_property
167 def _seq(self) -> str | None:
168 """
169 Returns the associated biological sequence.
171 Here, ``start``/``end`` are always ascending order in the attached slice and ``strand`` here
172 is the feature's orientation relative to the slice. Therefore when feature's strand disagrees
173 with the slice's strand, the slice sequence must be reverse-complemented before being returned.
175 Returns:
176 The associated biological sequence, or None if unavailable.
177 """
178 with self._lock:
179 if self.slice is None:
180 logger.warning("Cannot obtain seq for a feature without attached slice.")
181 return None
183 return self.slice.subseq(self.start, self.end, self.strand)
185 def __eq__(self, other: object) -> bool:
186 """
187 Compares two features using various criteria.
189 Args:
190 other: Another object to compare against.
192 Returns:
193 True if equal, False otherwise.
194 """
195 if type(self) is not type(other):
196 return False
198 return (
199 self.start == other.start
200 and self.end == other.end
201 and self.strand == other.strand
202 and self.slice == other.slice
203 )
205 def transform(self, coord_system: CoordSystem) -> Feature | None:
206 """
207 Transforms the feature to a new coordinate system.
209 Args:
210 coord_system: The coordinate system to transform to.
212 Returns:
213 The transformed feature or None if transformation is not possible.
214 """
215 raise NotImplementedError("Feature transformation is not implemented yet.")
217 def transfer(self, _genomic_slice: Slice) -> Feature | None:
218 """
219 Transfers the feature to a new slice.
221 Args:
222 _genomic_slice: The slice to transfer to. If None, uses current slice.
224 Returns:
225 The transferred feature or None if transfer is not possible.
226 """
227 raise NotImplementedError("Feature transfer is not implemented yet.")
229 def overlaps(self, other: Feature) -> bool | None:
230 """
231 Checks if this feature overlaps with another feature.
233 Args:
234 other: Another feature to check overlap with.
236 Returns:
237 True if overlaps, False if not, None if slices are missing.
238 """
239 if self.slice is None or other.slice is None:
240 logger.warning("Cannot compare two features is any of them is without attached slice.")
241 return None
243 if self.slice.seq_region_name != other.slice.seq_region_name:
244 logger.warning(
245 "Cannot compare two features on different sequence regions."
246 f" {self.__class__.__name__} is on {self.slice.seq_region_name}"
247 f" and {other.__class__.__name__} is on {other.slice.seq_region_name}."
248 )
249 return False
251 if ( 251 ↛ 262line 251 didn't jump to line 262 because the condition on line 251 was always true
252 self.seq_region_start is not None
253 and other.seq_region_start is not None
254 and self.seq_region_end is not None
255 and other.seq_region_end is not None
256 ):
257 return (
258 self.seq_region_start <= other.seq_region_end
259 and self.seq_region_end >= other.seq_region_start
260 )
262 if self.slice != other.slice:
263 return self.start <= other.end and self.end >= other.start
265 return None
267 def feature_slice(self) -> Slice | None:
268 """
269 Returns a slice that covers the area of this feature.
271 The feature start will be at 1 on the returned slice, and it will have the length of this feature.
272 If the feature has no attached slice, a warning will be logged and None will be returned.
274 Returns:
275 A Slice object covering the feature area,
276 or None if the feature has no attached slice.
277 """
278 genomic_slice = self.slice
280 if genomic_slice is None:
281 logger.warning("Cannot obtain feature_slice for a feature without attached slice.")
282 return None
284 if self.seq_region_start is None or self.seq_region_end is None or self.seq_region_strand is None: 284 ↛ 285line 284 didn't jump to line 285 because the condition on line 284 was never true
285 logger.warning(
286 (
287 "Cannot obtain feature_slice for a feature with undefined"
288 " seq_region_start, seq_region_end, or seq_region_strand."
289 )
290 )
291 return None
293 return Slice(
294 seq_region_name=genomic_slice.seq_region_name,
295 coord_system=genomic_slice.coord_system,
296 start=self.seq_region_start,
297 end=self.seq_region_end,
298 strand=self.seq_region_strand,
299 sequence_source=genomic_slice.sequence_source,
300 )