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

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

15 

16from __future__ import annotations 

17 

18from collections.abc import Iterable, Iterator, Sequence 

19from typing import Generic, TypeVar 

20 

21from attrs import define, field 

22 

23from ensembl.vep.caches.interval_tree import Interval, IntervalTree 

24from ensembl.vep.matcher.base import IntervalLike 

25 

26__all__ = ["InputBuffer"] 

27 

28ObjectT = TypeVar("ObjectT", bound=IntervalLike) 

29"""What object type an input buffer holds; anything with inclusive coordinates.""" 

30 

31 

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

33class InputBuffer(Generic[ObjectT]): 

34 """Indexed batch of input objects for overlap discovery. 

35 

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. 

38 

39 Attributes: 

40 objects (tuple[ObjectT, ...]): The objects in the current batch, in input order. 

41 """ 

42 

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) 

46 

47 @property 

48 def is_empty(self) -> bool: 

49 """Return whether the input this batch was drawn from is spent.""" 

50 

51 return self._exhausted 

52 

53 def next(self, items: Iterator[ObjectT]) -> None: 

54 """Reset the batch and refill it from ``items``. 

55 

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. 

59 

60 Raises: 

61 NotImplementedError: Always; a refill policy defines this. 

62 """ 

63 

64 raise NotImplementedError 

65 

66 def get_objects_by_intervals(self, intervals: Iterable[Interval]) -> list[ObjectT]: 

67 """Return the objects meeting any candidate interval, in input order. 

68 

69 Args: 

70 intervals (Iterable[Interval]): The candidate intervals to query. 

71 

72 Returns: 

73 list[ObjectT]: Every object meeting at least one interval, reported once. 

74 """ 

75 

76 if not self.objects: 

77 return [] 

78 

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)] 

84 

85 def get_overlapping_objects(self, start: int, end: int) -> list[ObjectT]: 

86 """Return the objects overlapping one inclusive interval, in input order. 

87 

88 Args: 

89 start (int): The 1-based inclusive start coordinate. 

90 end (int): The inclusive end coordinate. 

91 

92 Returns: 

93 list[ObjectT]: The overlapping objects, empty when the batch is. 

94 """ 

95 

96 if not self.objects: 

97 return [] 

98 return self._tree().get_overlapping_features(start, end) 

99 

100 def clear(self) -> None: 

101 """Drop the index built for the batch, leaving the batch itself alone.""" 

102 

103 self._interval_tree = None 

104 

105 def _fill(self, objects: Sequence[ObjectT], *, exhausted: bool = False) -> None: 

106 """Replace the batch, discarding the index built for the previous one. 

107 

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

112 

113 self.objects = tuple(objects) 

114 self._interval_tree = None 

115 self._exhausted = exhausted 

116 

117 def _tree(self) -> IntervalTree[ObjectT]: 

118 """Return the index for the current batch, building it on first query.""" 

119 

120 if self._interval_tree is None: 

121 self._interval_tree = IntervalTree(features=list(self.objects)) 

122 return self._interval_tree