Coverage for src/python/ensembl/vep/models/genic/cds.py: 95%

34 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"""CDS class.""" 

15 

16from __future__ import annotations 

17 

18from typing import TYPE_CHECKING 

19from functools import cached_property 

20from threading import RLock 

21from attrs import define, field 

22 

23from ensembl.vep.models import Feature 

24from ensembl.vep.common import invalidates 

25 

26if TYPE_CHECKING: 

27 from ensembl.vep.models.genic import Transcript, Translation 

28 

29 

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

31class CDS(Feature): 

32 """ 

33 Represents a CDS. 

34 

35 Attributes: 

36 seq (str | None): The nucleotide sequence of the CDS. 

37 transcript (Transcript | None): The transcript to which this CDS belongs. 

38 translation (Translation | None): The translation associated with this CDS. 

39 """ 

40 

41 seq: str | None = field(default=None, on_setattr=invalidates("_nucleotide_length", "_protein_length")) 

42 transcript: Transcript | None = field(default=None, init=False) 

43 translation: Translation | None = field(default=None, init=False) 

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

45 

46 @property 

47 def nucleotide_length(self) -> int | None: 

48 """Public accessor for _nucleotide_length. 

49 

50 Returns: 

51 int | None: Length in nucleotides, or None if not applicable. 

52 """ 

53 return self._nucleotide_length 

54 

55 @cached_property 

56 def _nucleotide_length(self) -> int | None: 

57 """Returns the length of the CDS in nucleotides. 

58 

59 Prefers the attached transcript's Mapper (position arithmetic) available even when this 

60 CDS has no seq of its own. Falls back to len(seq) when there is no transcript/mapper. 

61 

62 Returns: 

63 int | None: Length in nucleotides, or None if not applicable. 

64 """ 

65 with self._lock: 

66 if self.transcript is not None and self.transcript.cds_length is not None: 

67 return self.transcript.cds_length 

68 return len(self.seq) if self.seq is not None else None 

69 

70 @property 

71 def protein_length(self) -> int | None: 

72 """Public accessor for _protein_length. 

73 

74 Returns: 

75 int | None: Length in amino acids, or None if not applicable. 

76 """ 

77 return self._protein_length 

78 

79 @cached_property 

80 def _protein_length(self) -> int | None: 

81 """Returns the length of the CDS in amino acids. 

82 

83 Returns: 

84 int | None: Length in amino acids, or None if not applicable. 

85 """ 

86 with self._lock: 

87 nucleotide_length = self._nucleotide_length 

88 return (nucleotide_length // 3) if nucleotide_length is not None else None 

89 

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

91 """Compare two CDS using various criteria. 

92 

93 Args: 

94 other (object): Another object to compare against. 

95 

96 Returns: 

97 bool: True if equal, False otherwise. 

98 """ 

99 if not isinstance(other, CDS): 99 ↛ 100line 99 didn't jump to line 100 because the condition on line 99 was never true

100 return NotImplemented 

101 

102 return ( 

103 self.start == other.start 

104 and self.end == other.end 

105 and self.strand == other.strand 

106 and self.seq == other.seq 

107 and self.transcript == other.transcript 

108 and self.translation == other.translation 

109 )