Coverage for src/python/ensembl/vep/parsers/tabix.py: 100%

44 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"""Tabix parser interface backed by Rust.""" 

15 

16from __future__ import annotations 

17 

18import logging 

19from pathlib import Path 

20from functools import cached_property 

21from attrs import define, field 

22 

23from ensembl.vep.common import format_region_string 

24from ensembl.vep.parsers.parser import Parser 

25from ensembl.vep.ensembl_vep_core import ( # noqa: F401,F403 pylint: disable=E0401,E0611 

26 TabixParser as _RustTabixParser, 

27) 

28 

29__all__ = ["TabixParser"] 

30logger = logging.getLogger(__name__) 

31 

32 

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

34class TabixParser(Parser[str]): 

35 """Parser facade for tabix-indexed row streams. 

36 

37 Attributes: 

38 format (str): The format of the file being parsed. 

39 file_path (str | Path | None): The path to the file to be parsed. 

40 tabix_path (str | Path | None): The path to the tabix executable. 

41 _parser (_RustTabixParser): The underlying Rust tabix parser. 

42 """ 

43 

44 format: str = field(init=False, default="tabix") 

45 file_path: str | Path | None = field(default=None) 

46 tabix_path: str | Path | None = field(default="tabix") 

47 _parser: _RustTabixParser = field(init=False, repr=False) 

48 

49 def __attrs_post_init__(self) -> None: 

50 self._parser = _RustTabixParser( 

51 str(self.file_path) if self.file_path is not None else None, 

52 str(self.tabix_path) if self.tabix_path is not None else None, 

53 ) 

54 

55 @property 

56 def metadata(self) -> dict: 

57 """Return parsed file metadata. 

58 

59 Returns: 

60 dict: The metadata dictionary. 

61 """ 

62 

63 return self._metadata 

64 

65 @cached_property 

66 def _metadata(self) -> dict: 

67 """Metadata dictionary. 

68 

69 Returns: 

70 dict: The metadata dictionary. 

71 """ 

72 

73 return dict(self._parser.metadata) 

74 

75 def open(self, file_path: str | Path, mode: str | None = None) -> TabixParser: 

76 """Open a tabix-indexed BGZF file. 

77 

78 Args: 

79 file_path (str | Path): The path to the file to be parsed. 

80 mode (str | None): The mode in which to open the file. 

81 

82 Returns: 

83 TabixParser: The parser instance. 

84 """ 

85 

86 if mode is not None: 

87 logger.debug("The 'mode' argument is not used by TabixParser and will be ignored.") 

88 

89 self._parser.open(str(file_path)) 

90 self._clear_metadata_cache() 

91 return self 

92 

93 def close(self) -> None: 

94 """Close the parser.""" 

95 

96 self._parser.close() 

97 self._clear_metadata_cache() 

98 

99 def seek( 

100 self, 

101 region: str | None = None, 

102 *, 

103 seq_region_name: str | None = None, 

104 start: int | None = None, 

105 end: int | None = None, 

106 ) -> bool: 

107 """Seek to a region and make next() stream matching raw rows. 

108 

109 Args: 

110 region (str | None): The region to seek to, in the format "chr:start-end". 

111 seq_region_name (str | None): The name of the region to seek to. 

112 start (int | None): The start position of the region to seek to. 

113 end (int | None): The end position of the region to seek to. 

114 

115 Returns: 

116 bool: True if the seek was successful, False otherwise. 

117 """ 

118 

119 return self._parser.seek( 

120 format_region_string(region, seq_region_name=seq_region_name, start=start, end=end) 

121 ) 

122 

123 def next(self) -> str | None: 

124 """Return the next raw row from the active region query. 

125 

126 Returns: 

127 str | None: The next raw row from the active region query, or None if there are no more rows. 

128 """ 

129 

130 return self._parser.next() 

131 

132 def records(self, limit: int | None = None) -> list[str]: 

133 """Return raw rows from the active region query. 

134 

135 Args: 

136 limit (int | None): The maximum number of records to return. If None, return all records. 

137 

138 Returns: 

139 list[str]: A list of raw rows from the active region query. 

140 """ 

141 

142 return self._parser.records(limit) 

143 

144 def _clear_metadata_cache(self) -> None: 

145 try: 

146 del self._metadata 

147 except AttributeError: 

148 pass