Coverage for src/python/ensembl/vep/parsers/fasta.py: 100%
42 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"""FASTA parser interface backed by the Rust parser."""
16from __future__ import annotations
18from pathlib import Path
19from attr import define, field
21from ensembl.vep.common import format_region_string
22from ensembl.vep.parsers.parser import Parser
23from ensembl.vep.ensembl_vep_core import ( # noqa: F401,F403 pylint: disable=E0401,E0611
24 FastaParser as _RustFastaParser,
25 FastaRecord,
26)
28__all__ = ["FastaParser", "FastaRecord"]
31@define(kw_only=True, auto_attribs=True)
32class FastaParser(Parser[FastaRecord]):
33 """Parser facade for FASTA files.
35 Attributes:
36 format (str): The format of the file being parsed.
37 file_path (str | Path | None): The path to the file being parsed.
38 mode (str): The reader mode, either "auto", "query" or "sequential".
39 Default is "auto", which will attempt to use "query" mode
40 and fall back to "sequential" if the file is not indexed.
41 """
43 format: str = field(init=False, default="fasta")
44 file_path: str | Path | None = field(default=None)
45 mode: str = field(default="auto")
46 _parser: _RustFastaParser = field(init=False, repr=False)
48 def __attrs_post_init__(self) -> None:
49 self._parser = _RustFastaParser(
50 str(self.file_path) if self.file_path is not None else None,
51 self.mode,
52 )
54 @property
55 def metadata(self) -> dict:
56 """Return parsed file metadata.
58 Returns:
59 dict: The metadata dictionary.
60 """
62 return self._metadata
64 @property
65 def _metadata(self) -> dict:
66 """Metadata dictionary containing FASTA sequence lengths.
68 Returns:
69 dict: A dictionary with sequence length metadata.
70 """
72 return dict(self._parser.metadata)
74 @property
75 def reader_mode(self) -> str:
76 """Return the configured reader mode."""
78 return self._parser.reader_mode
80 def open(self, file_path: str | Path, mode: str = "auto") -> FastaParser:
81 """Open a FASTA file.
83 Args:
84 file_path (str | Path): The path to the FASTA file to be parsed.
85 mode (str): The mode in which to open the file.
87 Returns:
88 FastaParser: The parser instance.
89 """
91 self.file_path = file_path
92 self._parser.open(str(file_path), mode)
93 return self
95 def close(self) -> None:
96 """Close the parser."""
98 self._parser.close()
100 def seek(
101 self,
102 region: str | None = None,
103 *,
104 seq_region_name: str | None = None,
105 start: int | None = None,
106 end: int | None = None,
107 ) -> bool:
108 """Seek to a FASTA record or subsequence.
110 Args:
111 region (str | None): The region to seek to, in the format "chr:start-end".
112 seq_region_name (str | None): The name of the region to seek to.
113 start (int | None): The start position of the region to seek to.
114 end (int | None): The end position of the region to seek to.
116 Returns:
117 bool: True if the seek was successful, False otherwise.
118 """
120 return self._parser.seek(
121 format_region_string(region, seq_region_name=seq_region_name, start=start, end=end)
122 )
124 def next(self) -> FastaRecord | None:
125 """Return the next FASTA record, or None at EOF.
127 Returns:
128 FastaRecord | None: The next FASTA record, or None if there are no more records.
129 """
131 return self._parser.next()
133 def records(self, limit: int | None = 10000) -> list[FastaRecord]:
134 """Return records from the current stream position.
136 Args:
137 limit (int | None): The maximum number of records to return. Default is 10000.
139 Returns:
140 list[FastaRecord]: A list of FASTA records.
141 """
143 return self._parser.records(limit)
145 @staticmethod
146 def parse_text(data: str, limit: int | None = None) -> list[FastaRecord]:
147 """Parse FASTA records from text.
149 Args:
150 data (str): The FASTA text to parse.
151 limit (int | None): The maximum number of records to return. If None, return all records.
153 Returns:
154 list[FastaRecord]: A list of Fasta records.
155 """
157 return _RustFastaParser.parse_text(data, limit)
159 @staticmethod
160 def parse_file(file_path: str | Path, limit: int | None = None) -> list[FastaRecord]:
161 """Parse FASTA records from a file.
163 Args:
164 file_path (str | Path): The path to the FASTA file to parse.
165 limit (int | None): The maximum number of records to return. If None, return all records.
167 Returns:
168 list[FastaRecord]: A list of Fasta records.
169 """
171 return _RustFastaParser.parse_file(str(file_path), limit)