Coverage for src/python/ensembl/vep/models/sequence/circular_slice.py: 87%
49 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"""CircularSlice class."""
16from __future__ import annotations
18from functools import cached_property
19import logging
21from attrs import define
23from ensembl.vep.common import Strand, reverse_complement
24from ensembl.vep.models.sequence.slice import Slice
26logger = logging.getLogger(__name__)
29@define(kw_only=True, auto_attribs=True)
30class CircularSlice(Slice):
31 """Represents a circular genomic region."""
33 @cached_property
34 def _seq(self) -> str | None:
35 """Resolve the sequence for a circular slice.
37 Returns:
38 str | None: The resolved sequence, or None if unavailable.
39 """
41 with self._lock:
43 if self.sequence_source is None:
44 return None
46 seq_region_length = self.seq_region_length
47 if seq_region_length is None: 47 ↛ 48line 47 didn't jump to line 48 because the condition on line 47 was never true
48 logger.warning(
49 "Cannot resolve circular slice on %s without seq_region_length.",
50 self.seq_region_name,
51 )
52 return None
54 spans = self._sequence_spans(seq_region_length)
55 if not spans: 55 ↛ 56line 55 didn't jump to line 56 because the condition on line 55 was never true
56 return None
58 sequence = ""
59 for start, end in spans:
60 piece = self.sequence_source.get_sequence_by_start_end_strand(
61 self.seq_region_name,
62 start,
63 end,
64 strand=Strand.FORWARD,
65 )
66 if piece is None: 66 ↛ 67line 66 didn't jump to line 67 because the condition on line 66 was never true
67 piece = "N" * (end - start + 1)
68 sequence += piece
70 requested_length = self.end - self.start + 1
71 if len(sequence) < requested_length:
72 sequence += "N" * (requested_length - len(sequence))
74 if self.strand == Strand.REVERSE:
75 return reverse_complement(sequence)
77 return sequence
79 def _sequence_spans(self, seq_region_length: int) -> list[tuple[int, int]]:
80 """Return linear spans that cover the circular slice.
82 Args:
83 seq_region_length (int): The length of the circular sequence region.
85 Returns:
86 list[tuple[int, int]]: Linear spans used to assemble the slice sequence.
87 """
89 requested_length = self.end - self.start + 1
90 if requested_length <= 0 or seq_region_length <= 0: 90 ↛ 91line 90 didn't jump to line 91 because the condition on line 90 was never true
91 return []
93 normalized_start = ((self.start - 1) % seq_region_length) + 1
94 available_length = min(requested_length, seq_region_length)
95 first_span_length = min(available_length, seq_region_length - normalized_start + 1)
96 spans = [(normalized_start, normalized_start + first_span_length - 1)]
98 remaining_length = available_length - first_span_length
99 if remaining_length:
100 spans.append((1, remaining_length))
102 return spans
104 def is_circular(self) -> bool:
105 """Indicates whether the slice is circular.
107 Returns:
108 bool: True, indicating the slice is circular.
109 """
110 return True
112 # NOTE: we needed to override __eq__ of attrs regeneration which compares internal field like _lock;
113 # is there a generic way to do this?
114 def __eq__(self, other: object) -> bool:
115 """Compare two CircularSlice objects and define if they are identical."""
117 return Slice.__eq__(self, other)