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
« 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."""
16from __future__ import annotations
18import logging
19from pathlib import Path
20from functools import cached_property
21from attrs import define, field
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)
29__all__ = ["TabixParser"]
30logger = logging.getLogger(__name__)
33@define(kw_only=True, auto_attribs=True)
34class TabixParser(Parser[str]):
35 """Parser facade for tabix-indexed row streams.
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 """
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)
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 )
55 @property
56 def metadata(self) -> dict:
57 """Return parsed file metadata.
59 Returns:
60 dict: The metadata dictionary.
61 """
63 return self._metadata
65 @cached_property
66 def _metadata(self) -> dict:
67 """Metadata dictionary.
69 Returns:
70 dict: The metadata dictionary.
71 """
73 return dict(self._parser.metadata)
75 def open(self, file_path: str | Path, mode: str | None = None) -> TabixParser:
76 """Open a tabix-indexed BGZF file.
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.
82 Returns:
83 TabixParser: The parser instance.
84 """
86 if mode is not None:
87 logger.debug("The 'mode' argument is not used by TabixParser and will be ignored.")
89 self._parser.open(str(file_path))
90 self._clear_metadata_cache()
91 return self
93 def close(self) -> None:
94 """Close the parser."""
96 self._parser.close()
97 self._clear_metadata_cache()
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.
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.
115 Returns:
116 bool: True if the seek was successful, False otherwise.
117 """
119 return self._parser.seek(
120 format_region_string(region, seq_region_name=seq_region_name, start=start, end=end)
121 )
123 def next(self) -> str | None:
124 """Return the next raw row from the active region query.
126 Returns:
127 str | None: The next raw row from the active region query, or None if there are no more rows.
128 """
130 return self._parser.next()
132 def records(self, limit: int | None = None) -> list[str]:
133 """Return raw rows from the active region query.
135 Args:
136 limit (int | None): The maximum number of records to return. If None, return all records.
138 Returns:
139 list[str]: A list of raw rows from the active region query.
140 """
142 return self._parser.records(limit)
144 def _clear_metadata_cache(self) -> None:
145 try:
146 del self._metadata
147 except AttributeError:
148 pass