Coverage for src/python/ensembl/vep/buffer/input_buffer/base.py: 100%
40 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"""Input buffers: a batch of input objects, indexed for overlap discovery."""
16from __future__ import annotations
18from collections.abc import Iterable, Iterator, Sequence
19from typing import Generic, TypeVar
21from attrs import define, field
23from ensembl.vep.caches.interval_tree import Interval, IntervalTree
24from ensembl.vep.matcher.base import IntervalLike
26__all__ = ["InputBuffer"]
28ObjectT = TypeVar("ObjectT", bound=IntervalLike)
29"""What object type an input buffer holds; anything with inclusive coordinates."""
32@define(kw_only=True, auto_attribs=True)
33class InputBuffer(Generic[ObjectT]):
34 """Indexed batch of input objects for overlap discovery.
36 The index is built on the first query and discarded whenever the batch changes, so a buffer
37 that is only ever filled costs nothing to index.
39 Attributes:
40 objects (tuple[ObjectT, ...]): The objects in the current batch, in input order.
41 """
43 objects: tuple[ObjectT, ...] = field(default=())
44 _interval_tree: IntervalTree[ObjectT] | None = field(default=None, init=False, repr=False)
45 _exhausted: bool = field(default=False, init=False)
47 @property
48 def is_empty(self) -> bool:
49 """Return whether the input this batch was drawn from is spent."""
51 return self._exhausted
53 def next(self, items: Iterator[ObjectT]) -> None:
54 """Reset the batch and refill it from ``items``.
56 Args:
57 items (Iterator[ObjectT]): The objects left to buffer. The iterator stays with the
58 caller, so a buffer holds nothing that cannot cross a process boundary.
60 Raises:
61 NotImplementedError: Always; a refill policy defines this.
62 """
64 raise NotImplementedError
66 def get_objects_by_intervals(self, intervals: Iterable[Interval]) -> list[ObjectT]:
67 """Return the objects meeting any candidate interval, in input order.
69 Args:
70 intervals (Iterable[Interval]): The candidate intervals to query.
72 Returns:
73 list[ObjectT]: Every object meeting at least one interval, reported once.
74 """
76 if not self.objects:
77 return []
79 tree = self._tree()
80 positions: set[int] = set()
81 for interval in intervals:
82 positions.update(tree.get_overlapping_positions(interval.lookup_start, interval.lookup_end))
83 return [self.objects[position] for position in sorted(positions)]
85 def get_overlapping_objects(self, start: int, end: int) -> list[ObjectT]:
86 """Return the objects overlapping one inclusive interval, in input order.
88 Args:
89 start (int): The 1-based inclusive start coordinate.
90 end (int): The inclusive end coordinate.
92 Returns:
93 list[ObjectT]: The overlapping objects, empty when the batch is.
94 """
96 if not self.objects:
97 return []
98 return self._tree().get_overlapping_features(start, end)
100 def clear(self) -> None:
101 """Drop the index built for the batch, leaving the batch itself alone."""
103 self._interval_tree = None
105 def _fill(self, objects: Sequence[ObjectT], *, exhausted: bool = False) -> None:
106 """Replace the batch, discarding the index built for the previous one.
108 Args:
109 objects (Sequence[ObjectT]): The new batch, in input order.
110 exhausted (bool): Whether the input the batch was drawn from is now spent.
111 """
113 self.objects = tuple(objects)
114 self._interval_tree = None
115 self._exhausted = exhausted
117 def _tree(self) -> IntervalTree[ObjectT]:
118 """Return the index for the current batch, building it on first query."""
120 if self._interval_tree is None:
121 self._interval_tree = IntervalTree(features=list(self.objects))
122 return self._interval_tree