Coverage for src/python/ensembl/vep/models/overlap/transcript_variation_allele.py: 95%

274 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"""TranscriptVariationAllele class.""" 

15 

16from __future__ import annotations 

17 

18from typing import TYPE_CHECKING 

19from functools import cached_property 

20from threading import RLock 

21from attr import define, field 

22 

23from ensembl.vep.common import Readthrough, SequenceEdit, Strand, invalidates, reverse_complement 

24from ensembl.vep.common.constants import EMPTY_ALLELE 

25from ensembl.vep.common.codons import translate_dna 

26from ensembl.vep.models.overlap.feature_overlap import FeatureOverlap 

27 

28if TYPE_CHECKING: 

29 from ensembl.vep.models.genic import Transcript 

30 from ensembl.vep.models.sequence import Mapper 

31 from ensembl.vep.models.variation import VariationAllele 

32 from ensembl.vep.common import Locus 

33 

34MAX_READTHROUGH_BASES = 1000 

35"""Cap on how far ``Readthrough.BEYOND_TRANSCRIPT`` reads past a transcript's own boundary.""" 

36 

37Position = tuple[int | None, int | None] 

38"""1-based positions. Primarily used for a variant's 5' and 3' ends on a transcript sequence, None for an 

39end that lies off it""" 

40 

41 

42def _nucleotide_sequence_up_to_stop(nucleotide_sequence: str) -> tuple[str, str]: 

43 """Truncate ``nucleotide_sequence`` to end at (and include) its first stop codon, if any. 

44 

45 Args: 

46 nucleotide_sequence (str): Nucleotide sequence, transcript-sense. 

47 

48 Returns: 

49 tuple[str, str]: The input truncated at (and including) its first stop codon, or 

50 unchanged if no stop codon is found within it, paired with its translation 

51 (truncated the same way). ``translate_dna`` translates strictly in fixed 3-base 

52 chunks from position 0 with no state carried between codons, so slicing its result 

53 at the stop residue is exactly ``translate_dna`` of the truncated nucleotides -- 

54 re-translating the truncated sequence would repeat work for the same answer. 

55 """ 

56 peptide = translate_dna(nucleotide_sequence) 

57 stop_index = peptide.find("*") 

58 if stop_index == -1: 

59 return nucleotide_sequence, peptide 

60 return nucleotide_sequence[: (stop_index + 1) * 3], peptide[: stop_index + 1] 

61 

62 

63def _edited_boundary(original_boundary: int, edit_start: int, edit_end: int, length_change: int) -> int: 

64 """Returns the offset of a boundary after an edit. 

65 

66 The new offset can be used to clip the edited sequence to only have the coding sequence. If the edit 

67 1. starts after the boundary, the boundary is unaffected. 

68 2. ends before the boundary, the boundary is shifted by the length change of the edit. 

69 3. overlaps the boundary, the boundary is anchored to the start of the edit. 

70 

71 Args: 

72 original_boundary (int): The boundary's own offset before the edit (0-based, into spliced_seq). 

73 edit_start (int): Start of the edited half-open range (0-based, into spliced_seq). 

74 edit_end (int): End of the edited half-open range (exclusive). 

75 length_change (int): Net length change of the edit (negative for deletion). 

76 

77 Returns: 

78 int: The boundary's offset in the edited sequence. 

79 """ 

80 if original_boundary < edit_start: 

81 return original_boundary 

82 if original_boundary >= edit_end: 

83 return original_boundary + length_change 

84 return edit_start 

85 

86 

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

88class TranscriptVariationAllele(FeatureOverlap): 

89 """ 

90 Represents the overlap between a transcript and a variation allele. 

91 

92 Attributes: 

93 feature (Transcript): 

94 The transcript that is getting overlapped. 

95 overlapping_feature (VariationAllele): 

96 The variation allele that is overlapping. 

97 is_reference (bool): 

98 Whether the overlapping variation allele is the reference allele. 

99 cdna_position (Position | None): 

100 The variant's position on the spliced transcript, or None if it touches no exon. 

101 cds_position (Position | None): 

102 The variant's position on the CDS, counted from a whole first codon, or None if 

103 it does not touch the CDS. 

104 protein_position (Position | None): 

105 The residues the variant's two ends fall in, or None if it does not touch the CDS. 

106 distance_to_transcript (int | None): 

107 The distance from the variation allele to the transcript, 

108 or None if the variation allele overlaps the transcript. 

109 exon_number (int | None): 

110 The exon number of the transcript that is overlapped by the variation allele, 

111 or None if the variation allele does not overlap any exon. 

112 intron_number (int | None): 

113 The intron number of the transcript that is overlapped by the variation allele, 

114 or None if the variation allele does not overlap any intron. 

115 reference_codon (str | None): 

116 The reference allele's whole-codon window in the CDS (transcript-sense), covering 

117 every codon touched by the variant, or None if the variant does not overlap the CDS. 

118 Cased for display as VEP writes it: lower case but for the allele's own bases. 

119 alternate_codon (str | None): 

120 The alternate allele's whole-codon window in the CDS (transcript-sense), i.e. 

121 reference_codon with the variant allele spliced in, or None if not applicable. Cased 

122 the same way, so a deletion's is all lower case. 

123 reference_peptide (str | None): 

124 The amino acid translation of reference_codon, or None if not applicable. 

125 alternate_peptide (str | None): 

126 The amino acid translation of alternate_codon, or None if not applicable. 

127 reference_cds (str | None): 

128 The whole reference CDS (transcript-sense, includes the start and stop codon), or 

129 None if the transcript has no CDS at all. 

130 alternate_cds (str | None): 

131 The whole reference CDS (transcript-sense) with the variant allele spliced in, or None 

132 if not applicable. 

133 reference_cds_peptide (str | None): 

134 The amino acid translation of reference_cds, or None if reference_cds is None. 

135 alternate_cds_peptide (str | None): 

136 The amino acid translation of alternate_cds, or None if alternate_cds is None. 

137 readthrough (Readthrough): 

138 How far translation of alternate_cds may run when the variant destroys the stop codon. 

139 """ 

140 

141 feature: Transcript 

142 overlapping_feature: VariationAllele 

143 is_reference: bool = field(default=False) 

144 readthrough: Readthrough = field( 

145 default=Readthrough.NONE, on_setattr=invalidates("_full_cds_alleles", "_full_peptide_alleles") 

146 ) 

147 distance_to_transcript: int | None = field(default=None, init=False) 

148 exon_number: int | None = field(default=None, init=False) 

149 intron_number: int | None = field(default=None, init=False) 

150 _lock: RLock = field(factory=RLock, init=False, repr=False) 

151 

152 def _position_on(self, mapper: Mapper | None) -> Position | None: 

153 """Place the variant's 5' and 3' ends on ``mapper``'s sequence, 1-based. 

154 

155 An insertion's ends are its two flanking bases. As in VEP, where the Ensembl mapper answers 

156 a gap for an end off the sequence, that end is None; a variant covering none of ``mapper``'s 

157 spans has no position at all. 

158 

159 Args: 

160 mapper (Mapper | None): The mapper whose sequence to place the variant on. 

161 

162 Returns: 

163 Position | None: The two ends, or None if the variant misses every span. 

164 """ 

165 if mapper is None: 

166 return None 

167 variant = self.overlapping_feature 

168 variant_min, variant_max = min(variant.start, variant.end), max(variant.start, variant.end) 

169 if mapper.genomic_span_to_offsets(variant_min, variant_max) is None: 

170 return None 

171 

172 if self.feature.strand == Strand.REVERSE: 

173 five_prime_end, three_prime_end = variant_max, variant_min 

174 else: 

175 five_prime_end, three_prime_end = variant_min, variant_max 

176 five_prime_offset = mapper.genomic_to_offset(five_prime_end) 

177 three_prime_offset = mapper.genomic_to_offset(three_prime_end) 

178 return ( 

179 None if five_prime_offset is None else five_prime_offset + 1, 

180 None if three_prime_offset is None else three_prime_offset + 1, 

181 ) 

182 

183 @property 

184 def cdna_position(self) -> Position | None: 

185 """Public accessor for _cdna_position. 

186 

187 Returns: 

188 Position | None: The variant's position on the spliced transcript, or None if 

189 it touches no exon. 

190 """ 

191 return self._cdna_position 

192 

193 @cached_property 

194 def _cdna_position(self) -> Position | None: 

195 """Place the variant on the spliced transcript. 

196 

197 Returns: 

198 Position | None: The position, or None if the variant touches no exon. 

199 """ 

200 return self._position_on(self.feature.cdna_mapper) 

201 

202 @property 

203 def cds_position(self) -> Position | None: 

204 """Public accessor for _cds_position. 

205 

206 Returns: 

207 Position | None: The variant's position on the CDS, or None if it does not 

208 touch the CDS. 

209 """ 

210 return self._cds_position 

211 

212 @cached_property 

213 def _cds_position(self) -> Position | None: 

214 """Place the variant on the CDS. 

215 

216 CDS offsets already start after any ``cds_start_phase`` padding, so a CDS with an 

217 incomplete 5' end is counted from a whole first codon, as VEP adds the start exon's phase. 

218 

219 Returns: 

220 Position | None: The position, or None if the variant does not touch the CDS. 

221 """ 

222 return self._position_on(self.feature.cds_mapper) 

223 

224 @property 

225 def protein_position(self) -> Position | None: 

226 """Public accessor for _protein_position. 

227 

228 Returns: 

229 Position | None: The residues the variant's two ends fall in, or None if it 

230 does not touch the CDS. 

231 """ 

232 return self._protein_position 

233 

234 @cached_property 

235 def _protein_position(self) -> Position | None: 

236 """Derive the residue positions from the CDS positions, three bases a residue. 

237 

238 Returns: 

239 Position | None: The residue positions, or None if the variant does not touch 

240 the CDS. 

241 """ 

242 cds_position = self.cds_position 

243 if cds_position is None: 

244 return None 

245 residues: list[int | None] = [] 

246 for position in cds_position: 

247 residues.append(None if position is None else (position - 1) // 3 + 1) 

248 return residues[0], residues[1] 

249 

250 def _edit_on(self, mapper: Mapper | None) -> SequenceEdit | None: 

251 """Express the edit caused by this variant in ``mapper``'s offset space. 

252 

253 The geometry shared by every downstream reconstruction, parameterised by which mapper does 

254 the genomic->offset conversion. Uses pure coordinate and allele-string arithmetic with no 

255 sequence involved (which makes ``cds_length_change`` cheap). 

256 

257 Args: 

258 mapper (Mapper | None): The mapper whose offset space the edit is expressed in. 

259 

260 Returns: 

261 SequenceEdit | None: The edit, or None when the variant covers none of ``mapper``'s 

262 spans (so there is nothing to replace). 

263 """ 

264 with self._lock: 

265 if mapper is None: 

266 return None 

267 

268 variant = self.overlapping_feature 

269 variant_min, variant_max = min(variant.start, variant.end), max(variant.start, variant.end) 

270 footprint = mapper.genomic_span_to_offsets(variant_min, variant_max) 

271 if footprint is None: 

272 return None 

273 low_offset, high_offset = footprint 

274 

275 alternate_allele = (variant.alternate_allele or "").replace(EMPTY_ALLELE, "") 

276 reference_allele_length = len((variant.reference_allele or "").replace(EMPTY_ALLELE, "")) 

277 transcript_sense_alternate = ( 

278 alternate_allele 

279 if self.feature.strand == Strand.FORWARD 

280 else reverse_complement(alternate_allele) 

281 ) 

282 

283 if reference_allele_length == 0: 

284 # Pure insertion: splice in immediately after the anchor offset. 

285 edit_start = edit_end = low_offset + 1 

286 else: 

287 edit_start, edit_end = low_offset, high_offset + 1 

288 

289 return SequenceEdit( 

290 footprint=footprint, 

291 start=edit_start, 

292 end=edit_end, 

293 replacement=transcript_sense_alternate, 

294 ) 

295 

296 @cached_property 

297 def _cds_edit(self) -> SequenceEdit | None: 

298 """This variant as an edit in CDS offset space (clipped to the coding sequence). 

299 

300 Returns: 

301 SequenceEdit | None: The edit, or None if the variant does not overlap the CDS. 

302 """ 

303 return self._edit_on(self.feature.cds_mapper) 

304 

305 @cached_property 

306 def _cdna_edit(self) -> SequenceEdit | None: 

307 """This variant as an edit in cDNA offset space (clipped to the exons). 

308 

309 Returns: 

310 SequenceEdit | None: The edit, or None if the variant touches no exon at all. 

311 """ 

312 return self._edit_on(self.feature.cdna_mapper) 

313 

314 @property 

315 def reference_codon(self) -> str | None: 

316 """Returns the codons variant touches in the reference sequence. 

317 

318 Returns: 

319 str | None: The reference allele's whole-codon window (transcript-sense), or None if 

320 the variant does not overlap the CDS. 

321 """ 

322 codons = self._codon_window_alleles 

323 return None if codons is None else codons[0] 

324 

325 @property 

326 def alternate_codon(self) -> str | None: 

327 """Returns the codons variant touches with the variant allele spliced in. 

328 

329 Returns: 

330 str | None: reference_codon with the variant allele spliced in, or None if not 

331 applicable. 

332 """ 

333 codons = self._codon_window_alleles 

334 return None if codons is None else codons[1] 

335 

336 @cached_property 

337 def _codon_window_alleles(self) -> tuple[str, str] | None: 

338 """Returns (reference_codon, alternate_codon), the whole-codon window around the variant, 

339 in transcript-sense, with the variant allele spliced in for the alternate half. 

340 

341 An insertion that falls between two codons has an empty reference window and the inserted 

342 bases as its alternate one, as VEP's does. 

343 

344 Each is lower case but for its own allele's bases, which are upper case: VEP's 

345 ``display_codon``. An empty allele (the reference side of an insertion, the alternate side 

346 of a deletion) leaves its codon all lower case. Anything computing on a codon upper-cases it 

347 first. 

348 Returns: 

349 tuple[str, str] | None: (reference_codon, alternate_codon), or None if the variant 

350 does not overlap the CDS. 

351 """ 

352 with self._lock: 

353 transcript = self.feature 

354 mapper = transcript.cds_mapper 

355 edit = self._cds_edit 

356 if mapper is None or edit is None: 

357 return None 

358 

359 window = mapper.codon_window(edit.footprint) 

360 if window is None: 360 ↛ 361line 360 didn't jump to line 361 because the condition on line 360 was never true

361 return None 

362 

363 coding_sequence = transcript.coding_sequence 

364 if coding_sequence is None: 364 ↛ 365line 364 didn't jump to line 365 because the condition on line 364 was never true

365 return None 

366 

367 if edit.start == edit.end and edit.start % 3 == 0: 

368 # An insertion between two codons: VEP's window runs from the residue after the 

369 # insertion point back to the one before it, which is empty, leaving the inserted 

370 # bases alone on the alternate side (``TranscriptVariationAllele::codon``). 

371 return "", edit.replacement.upper() 

372 

373 codon_start, codon_end = window 

374 leading_bases = coding_sequence[codon_start : edit.start].lower() 

375 trailing_bases = coding_sequence[edit.end : codon_end].lower() 

376 reference_bases = coding_sequence[edit.start : edit.end].upper() 

377 reference_codon = leading_bases + reference_bases + trailing_bases 

378 alternate_codon = leading_bases + edit.replacement.upper() + trailing_bases 

379 return reference_codon, alternate_codon 

380 

381 @property 

382 def reference_peptide(self) -> str | None: 

383 """Returns the amino acid translation of the reference_codon. 

384 

385 Returns: 

386 str | None: The amino acid translation of reference_codon, or None if not applicable. 

387 """ 

388 peptides = self._peptide_window_alleles 

389 return None if peptides is None else peptides[0] 

390 

391 @property 

392 def alternate_peptide(self) -> str | None: 

393 """Returns the amino acid translation of the alternate_codon. 

394 

395 Returns: 

396 str | None: The amino acid translation of alternate_codon, or None if not applicable. 

397 """ 

398 peptides = self._peptide_window_alleles 

399 return None if peptides is None else peptides[1] 

400 

401 @cached_property 

402 def _peptide_window_alleles(self) -> tuple[str, str] | None: 

403 """Returns (reference_peptide, alternate_peptide), translated from _codon_window_alleles. 

404 

405 Returns: 

406 tuple[str, str] | None: (reference_peptide, alternate_peptide), or None if the 

407 variant does not overlap the CDS. 

408 """ 

409 codons = self._codon_window_alleles 

410 if codons is None: 

411 return None 

412 reference_codon, alternate_codon = codons 

413 return translate_dna(reference_codon.upper()), translate_dna(alternate_codon.upper()) 

414 

415 @property 

416 def cds_length_change(self) -> int | None: 

417 """Net change in **coding** length the variant makes, in bases. 

418 

419 Positive for an insertion, negative for a deletion, zero for a substitution. Derived from the CDS 

420 edit geometry rather than the cDNA. Any intronic portion of the variant's genomic span is already 

421 discounted, since the offsets come from the mapper's block-overlap clipping. 

422 

423 Returns: 

424 int | None: The net coding length change, or None when the variant does not overlap 

425 the CDS and so changes no coding length at all. 

426 """ 

427 edit = self._cds_edit 

428 return None if edit is None else edit.length_change 

429 

430 @property 

431 def reference_cds(self) -> str | None: 

432 """Returns the whole reference CDS. 

433 

434 Returns: 

435 str | None: The whole reference CDS (transcript-sense), or None if the transcript has 

436 no CDS at all. 

437 """ 

438 alleles = self._full_cds_alleles 

439 return None if alleles is None else alleles[0] 

440 

441 @property 

442 def alternate_cds(self) -> str | None: 

443 """Returns the whole CDS with the variant applied. 

444 

445 Returns: 

446 str | None: The whole CDS with the variant applied, or None if the transcript has no CDS 

447 at all. 

448 """ 

449 alleles = self._full_cds_alleles 

450 return None if alleles is None else alleles[1] 

451 

452 @cached_property 

453 def _full_cds_alleles(self) -> tuple[str, str, str | None] | None: 

454 """Returns (reference_cds, alternate_cds, alternate_peptide), the whole CDS, in 

455 transcript-sense, with the variant applied for the alternate half. 

456 

457 For alternate CDS edit, replaces the half-open transcript-wide cDNA offset that the variant overlapped 

458 with the strand-corrected alternate allele in the transcript.spliced_seq (5' UTR + CDS + 3' UTR). By 

459 default, we assume there is no intron retention, exon skipping or readthrough. Only the 

460 coding sequence 

461 is considered. 

462 

463 Rather than just the CDS it uses cDNA sequence to optionally allow readthrough into the UTR. If not 

464 opted-for, the CDS is truncated to include only the coding sequence. 

465 

466 ``reference_cds`` reuses ``transcript.coding_sequence`` in place of re-slicing ``spliced_seq`` 

467 when its length matches -- both are assembled from the same coding exons, so the content 

468 agrees (proved corpus-wide by tests, not re-checked per overlap). Both sides carry the 

469 transcript's ``cds_start_phase`` ``N`` padding, as its CDS offsets do. 

470 ``alternate_peptide`` is the translation ``_nucleotide_sequence_up_to_stop`` already produced 

471 while truncating ``alternate_cds`` at its first stop codon, so ``_full_peptide_alleles`` can 

472 reuse it instead of re-translating. It is None only when the variant touches no exon at all, 

473 the one path that leaves ``alternate_cds`` untranslated here. 

474 

475 Returns: 

476 tuple[str, str, str | None] | None: (reference_cds, alternate_cds, alternate_peptide), 

477 or None if the transcript has no CDS at all (non-coding, or missing sequence 

478 data). A variant that changes nothing about the CDS (entirely within a UTR, 

479 entirely intronic, or entirely outside the transcript) yields two identical 

480 sequences and no alternate peptide. 

481 """ 

482 with self._lock: 

483 transcript = self.feature 

484 cdna_mapper = transcript.cdna_mapper 

485 spliced_seq = transcript.spliced_seq 

486 cds_length = transcript.cds_length 

487 cdna_coding_start = transcript.cdna_coding_start 

488 if cdna_mapper is None or spliced_seq is None or not cds_length or cdna_coding_start is None: 

489 return None 

490 

491 # 0-based start/end position for slicing. cds_length counts only real bases, so the 

492 # padding is prefixed separately; the spliced sequence does not carry it either. 

493 padding = "N" * transcript.cds_start_phase 

494 cdna_coding_start0 = cdna_coding_start - 1 

495 cdna_coding_end0 = cdna_coding_start0 + cds_length 

496 coding_sequence = transcript.coding_sequence 

497 if coding_sequence is not None and len(coding_sequence) == cds_length + len(padding): 

498 reference_cds = coding_sequence 

499 else: 

500 reference_cds = padding + spliced_seq[cdna_coding_start0:cdna_coding_end0] 

501 

502 edit = self._cdna_edit 

503 if edit is None: 

504 # The variant touches no exon at all (purely intronic, or entirely outside the 

505 # transcript). Splicing is assumed to proceed normally, so the CDS is unchanged. 

506 return reference_cds, reference_cds, None 

507 edit_start, edit_end, transcript_sense_alternate = edit.start, edit.end, edit.replacement 

508 

509 alternate_spliced_seq = ( 

510 spliced_seq[:edit_start] + transcript_sense_alternate + spliced_seq[edit_end:] 

511 ) 

512 

513 length_change = edit.length_change 

514 

515 # Clip the edited sequence boundary to contain only the coding sequence. 

516 anchor = _edited_boundary(cdna_coding_start0, edit_start, edit_end, length_change) 

517 if self.readthrough >= Readthrough.UTR: 

518 # Translate on through whatever follows the coding sequence in the edited cDNA. 

519 end_bound = len(alternate_spliced_seq) 

520 else: 

521 end_bound = _edited_boundary(cdna_coding_end0, edit_start, edit_end, length_change) 

522 

523 alternate_tail = padding + alternate_spliced_seq[anchor:end_bound] 

524 alternate_cds, alternate_peptide = _nucleotide_sequence_up_to_stop(alternate_tail) 

525 

526 if self.readthrough >= Readthrough.BEYOND_TRANSCRIPT and "*" not in alternate_peptide: 

527 # No in-frame stop anywhere in the edited cDNA, so carry on into raw genomic 

528 # sequence past the transcript's own boundary. 

529 flank = self._downstream_flanking_sequence(MAX_READTHROUGH_BASES) 

530 if flank: 

531 alternate_cds, alternate_peptide = _nucleotide_sequence_up_to_stop(alternate_tail + flank) 

532 

533 return reference_cds, alternate_cds, alternate_peptide 

534 

535 def _downstream_flanking_sequence(self, length: int) -> str: 

536 """Up to ``length`` bases of raw genomic sequence past this transcript's own 3' boundary 

537 (transcript sense), read directly from the attached Slice. 

538 

539 Only reached at ``Readthrough.BEYOND_TRANSCRIPT``. It trusts the attached slice already covers 

540 what is needed rather than issuing a fresh sequence-source fetch. 

541 

542 Args: 

543 length (int): How many bases to try to read. 

544 

545 Returns: 

546 str: Up to ``length`` bases of transcript-sense sequence past the transcript's own 

547 boundary, or ``""`` if none is available. 

548 """ 

549 transcript = self.feature 

550 slice_ = transcript.slice 

551 if length <= 0 or slice_ is None or slice_.seq is None: 

552 return "" 

553 if transcript.strand == Strand.FORWARD: 

554 search_end = min(slice_.end, transcript.end + length) 

555 if search_end <= transcript.end: 

556 return "" 

557 return slice_.seq[transcript.end : search_end] 

558 search_start = max(slice_.start, transcript.start - length) 

559 if search_start >= transcript.start: 

560 return "" 

561 return reverse_complement(slice_.seq[search_start - 1 : transcript.start - 1]) 

562 

563 @property 

564 def reference_cds_peptide(self) -> str | None: 

565 """Returns the amino acid translation of the reference CDS. 

566 

567 Returns: 

568 str | None: The amino acid translation of reference_cds, or None if not applicable. 

569 """ 

570 peptides = self._full_peptide_alleles 

571 return None if peptides is None else peptides[0] 

572 

573 @property 

574 def alternate_cds_peptide(self) -> str | None: 

575 """Returns the amino acid translation of the alternate CDS. 

576 

577 Returns: 

578 str | None: The amino acid translation of alternate_cds, or None if not applicable. 

579 """ 

580 peptides = self._full_peptide_alleles 

581 return None if peptides is None else peptides[1] 

582 

583 @cached_property 

584 def _full_peptide_alleles(self) -> tuple[str, str] | None: 

585 """Returns (reference_cds_peptide, alternate_cds_peptide), translated from 

586 _full_cds_alleles. 

587 

588 ``alternate_cds_peptide`` reuses the peptide ``_full_cds_alleles`` already produced while 

589 truncating ``alternate_cds`` at its first stop codon, translating it here only for the one 

590 path that leaves it untranslated (the variant touches no exon at all). See 

591 ``_reference_cds_peptide`` for how the reference half is sourced. 

592 

593 Returns: 

594 tuple[str, str] | None: (reference_cds_peptide, alternate_cds_peptide), or None if 

595 the variant does not overlap the CDS. 

596 """ 

597 alleles = self._full_cds_alleles 

598 if alleles is None: 

599 return None 

600 reference_cds, alternate_cds, alternate_peptide = alleles 

601 if alternate_peptide is None: 

602 alternate_peptide = translate_dna(alternate_cds) 

603 return self._reference_cds_peptide(reference_cds), alternate_peptide 

604 

605 def _reference_cds_peptide(self, reference_cds: str) -> str: 

606 """Returns the translation of the reference CDS, reusing ``transcript.translated_seq`` 

607 when it is provably ``translate_dna(reference_cds)``. 

608 

609 ``translated_seq`` returns ``translation.seq`` when set. For every source in this codebase 

610 today that is itself ``translate_dna(cds.seq)`` (GFF/GTF-built translations seed 

611 ``seq=None`` and fall back the same way), and ``Transcript._translation`` marks exactly 

612 that case via ``Translation.derived_from_coding_sequence``. A hypothetical future source 

613 could instead seed a curated peptide (for example, one edited for selenocysteine or without 

614 a trailing stop), which the stop facts compare positionally against this CDS -- so reuse is 

615 guarded to the cases where that cannot have happened: the transcript's coding sequence 

616 matches ``reference_cds`` in length, and its translation is either unset, has no seq of its 

617 own, or is marked as derived from this same coding sequence. 

618 

619 Checking the marker rather than *when* ``_translation`` resolved (the previous guard) means 

620 reuse no longer depends on evaluation order: a transcript with several overlapping variants 

621 benefits from the very first one resolving ``_translation`` the plain way, instead of only 

622 the first overlap getting to reuse it and every later one paying to retranslate. 

623 

624 Args: 

625 reference_cds (str): The reference CDS this TVA computed (possibly itself already 

626 reused from ``transcript.cds.seq``). 

627 

628 Returns: 

629 str: The reference CDS peptide. 

630 """ 

631 transcript = self.feature 

632 coding_sequence = transcript.coding_sequence 

633 if coding_sequence is None or len(coding_sequence) != len(reference_cds): 

634 return translate_dna(reference_cds) 

635 

636 translation = transcript.translation 

637 if translation is None or translation.seq is None or translation.derived_from_coding_sequence: 

638 translated_seq = transcript.translated_seq 

639 if translated_seq is not None: 639 ↛ 642line 639 didn't jump to line 642 because the condition on line 639 was always true

640 return translated_seq 

641 

642 return translate_dna(reference_cds) 

643 

644 @property 

645 def shifted(self) -> bool: 

646 """ 

647 Public accessor for _shifted. 

648 

649 Returns: 

650 bool: True if the allele has been shifted, False otherwise. 

651 """ 

652 return self._shifted 

653 

654 @cached_property 

655 def _shifted(self) -> bool: 

656 """ 

657 Returns whether the allele has been shifted from its original position. 

658 

659 Returns: 

660 bool: True if the allele has been shifted, False otherwise. 

661 """ 

662 return False 

663 

664 @property 

665 def shifted_cdna_locus(self) -> Locus | None: 

666 """ 

667 Public accessor for _shifted_cdna_locus. 

668 

669 Returns: 

670 Locus | None: The cDNA locus of the variation allele after shifting, 

671 or None if not applicable. 

672 """ 

673 return self._shifted_cdna_locus 

674 

675 @cached_property 

676 def _shifted_cdna_locus(self) -> Locus | None: 

677 """ 

678 Returns the cDNA locus of the variation allele after shifting, or None if not applicable. 

679 

680 Returns: 

681 Locus | None: The cDNA locus of the variation allele after shifting, 

682 or None if not applicable. 

683 """ 

684 return None 

685 

686 @property 

687 def shifted_cds_locus(self) -> Locus | None: 

688 """ 

689 Public accessor for _shifted_cds_locus. 

690 

691 Returns: 

692 Locus | None: The CDS locus of the variation allele after shifting, 

693 or None if not applicable. 

694 """ 

695 return self._shifted_cds_locus 

696 

697 @cached_property 

698 def _shifted_cds_locus(self) -> Locus | None: 

699 """ 

700 Returns the CDS locus of the variation allele after shifting, or None if not applicable. 

701 

702 Returns: 

703 Locus | None: The CDS locus of the variation allele after shifting, 

704 or None if not applicable. 

705 """ 

706 return None 

707 

708 @property 

709 def shifted_protein_locus(self) -> Locus | None: 

710 """ 

711 Public accessor for _shifted_protein_locus. 

712 

713 Returns: 

714 Locus | None: The protein locus of the variation allele after shifting, 

715 or None if not applicable. 

716 """ 

717 return self._shifted_protein_locus 

718 

719 @cached_property 

720 def _shifted_protein_locus(self) -> Locus | None: 

721 """ 

722 Returns the protein locus of the variation allele after shifting, or None if not applicable. 

723 

724 Returns: 

725 Locus | None: The protein locus of the variation allele after shifting, 

726 or None if not applicable. 

727 """ 

728 return None 

729 

730 @property 

731 def original_cdna_locus(self) -> Locus | None: 

732 """Public accessor for _original_cdna_locus. 

733 

734 Returns: 

735 Locus | None: The original cDNA locus of the variation allele before shifting, 

736 or None if not applicable. 

737 """ 

738 return self._original_cdna_locus 

739 

740 @cached_property 

741 def _original_cdna_locus(self) -> Locus | None: 

742 """Returns the original cDNA locus of the variation allele before shifting, or None if not applicable. 

743 

744 Returns: 

745 Locus | None: The original cDNA locus of the variation allele before shifting, 

746 or None if not applicable. 

747 """ 

748 return None 

749 

750 @property 

751 def original_cds_locus(self) -> Locus | None: 

752 """Public accessor for _original_cds_locus. 

753 

754 Returns: 

755 Locus | None: The original CDS locus of the variation allele before shifting, 

756 or None if not applicable. 

757 """ 

758 return self._original_cds_locus 

759 

760 @cached_property 

761 def _original_cds_locus(self) -> Locus | None: 

762 """Returns the original CDS locus of the variation allele before shifting, or None if not applicable. 

763 

764 Returns: 

765 Locus | None: The original CDS locus of the variation allele before shifting, 

766 or None if not applicable. 

767 """ 

768 return None 

769 

770 @property 

771 def original_protein_locus(self) -> Locus | None: 

772 """Public accessor for _original_protein_locus. 

773 

774 Returns: 

775 Locus | None: The original protein locus of the variation allele before shifting, 

776 or None if not applicable. 

777 """ 

778 return self._original_protein_locus 

779 

780 @cached_property 

781 def _original_protein_locus(self) -> Locus | None: 

782 """The original protein locus of the variation allele before shifting, 

783 or None if not applicable. 

784 

785 Returns: 

786 Locus | None: The original protein locus of the variation allele before shifting, 

787 or None if not applicable. 

788 """ 

789 raise NotImplementedError("Feature is not implemented yet.") 

790 

791 def shift(self) -> None: 

792 """Shifts the variation allele to its most 3' position on the transcript according to HGVS rules.""" 

793 raise NotImplementedError("Feature is not implemented yet.") 

794 

795 def hgvs_genomic(self) -> str | None: 

796 """Returns the HGVS genomic notation for this variation allele.""" 

797 raise NotImplementedError("Feature is not implemented yet.") 

798 

799 def hgvs_transcript(self) -> str | None: 

800 """Returns the HGVS transcript notation for this variation allele.""" 

801 raise NotImplementedError("Feature is not implemented yet.") 

802 

803 def hgvs_protein(self) -> str | None: 

804 """Returns the HGVS protein notation for this variation allele.""" 

805 raise NotImplementedError("Feature is not implemented yet.")