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

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.""" 

15 

16from __future__ import annotations 

17 

18import logging 

19from functools import cached_property 

20from threading import RLock 

21from attrs import define, field 

22 

23from ensembl.vep.models.sequence import Slice, CoordSystem 

24from ensembl.vep.common import Strand, UnsafeSetterMixin, invalidates 

25 

26logger = logging.getLogger(__name__) 

27 

28 

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

30class Feature(UnsafeSetterMixin): 

31 """ 

32 Represents a genomic feature in a specific locus. 

33 

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 """ 

41 

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) 

50 

51 @property 

52 def length(self) -> int: 

53 """ 

54 Public accessor for the _length. 

55 

56 Returns: 

57 The length of the feature. 

58 """ 

59 return self._length 

60 

61 @cached_property 

62 def _length(self) -> int: 

63 """ 

64 Returns the length of the feature. 

65 

66 Returns: 

67 The length of the feature. 

68 """ 

69 with self._lock: 

70 return self.end - self.start + 1 

71 

72 @property 

73 def seq_region_start(self) -> int | None: 

74 """ 

75 Public accessor for the _seq_region_start. 

76 

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 

82 

83 @cached_property 

84 def _seq_region_start(self) -> int | None: 

85 """ 

86 Returns the start position in the sequence region. 

87 

88 If the attached slice is on the reverse strand, 

89 seq_region_start corresponds to the feature.end position 

90 

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 

102 

103 @property 

104 def seq_region_end(self) -> int | None: 

105 """ 

106 Public accessor for the _seq_region_end. 

107 

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 

112 

113 @cached_property 

114 def _seq_region_end(self) -> int | None: 

115 """ 

116 Returns the end position in the sequence region. 

117 

118 If the attached slice is on the reverse strand, 

119 seq_region_end corresponds to the feature.start position 

120 

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 

131 

132 @property 

133 def seq_region_strand(self) -> Strand | None: 

134 """ 

135 Public accessor for the _seq_region_strand. 

136 

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 

141 

142 @cached_property 

143 def _seq_region_strand(self) -> Strand | None: 

144 """ 

145 Returns the strand in the sequence region. 

146 

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) 

155 

156 @property 

157 def seq(self) -> str | None: 

158 """ 

159 Public accessor for the _seq. 

160 

161 Returns: 

162 The associated biological sequence, or None if unavailable. 

163 """ 

164 return self._seq 

165 

166 @cached_property 

167 def _seq(self) -> str | None: 

168 """ 

169 Returns the associated biological sequence. 

170 

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. 

174 

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 

182 

183 return self.slice.subseq(self.start, self.end, self.strand) 

184 

185 def __eq__(self, other: object) -> bool: 

186 """ 

187 Compares two features using various criteria. 

188 

189 Args: 

190 other: Another object to compare against. 

191 

192 Returns: 

193 True if equal, False otherwise. 

194 """ 

195 if type(self) is not type(other): 

196 return False 

197 

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 ) 

204 

205 def transform(self, coord_system: CoordSystem) -> Feature | None: 

206 """ 

207 Transforms the feature to a new coordinate system. 

208 

209 Args: 

210 coord_system: The coordinate system to transform to. 

211 

212 Returns: 

213 The transformed feature or None if transformation is not possible. 

214 """ 

215 raise NotImplementedError("Feature transformation is not implemented yet.") 

216 

217 def transfer(self, _genomic_slice: Slice) -> Feature | None: 

218 """ 

219 Transfers the feature to a new slice. 

220 

221 Args: 

222 _genomic_slice: The slice to transfer to. If None, uses current slice. 

223 

224 Returns: 

225 The transferred feature or None if transfer is not possible. 

226 """ 

227 raise NotImplementedError("Feature transfer is not implemented yet.") 

228 

229 def overlaps(self, other: Feature) -> bool | None: 

230 """ 

231 Checks if this feature overlaps with another feature. 

232 

233 Args: 

234 other: Another feature to check overlap with. 

235 

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 

242 

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 

250 

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 ) 

261 

262 if self.slice != other.slice: 

263 return self.start <= other.end and self.end >= other.start 

264 

265 return None 

266 

267 def feature_slice(self) -> Slice | None: 

268 """ 

269 Returns a slice that covers the area of this feature. 

270 

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. 

273 

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 

279 

280 if genomic_slice is None: 

281 logger.warning("Cannot obtain feature_slice for a feature without attached slice.") 

282 return None 

283 

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 

292 

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 )