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

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

15 

16from __future__ import annotations 

17 

18from pathlib import Path 

19from attr import define, field 

20 

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) 

27 

28__all__ = ["FastaParser", "FastaRecord"] 

29 

30 

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

32class FastaParser(Parser[FastaRecord]): 

33 """Parser facade for FASTA files. 

34 

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

42 

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) 

47 

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 ) 

53 

54 @property 

55 def metadata(self) -> dict: 

56 """Return parsed file metadata. 

57 

58 Returns: 

59 dict: The metadata dictionary. 

60 """ 

61 

62 return self._metadata 

63 

64 @property 

65 def _metadata(self) -> dict: 

66 """Metadata dictionary containing FASTA sequence lengths. 

67 

68 Returns: 

69 dict: A dictionary with sequence length metadata. 

70 """ 

71 

72 return dict(self._parser.metadata) 

73 

74 @property 

75 def reader_mode(self) -> str: 

76 """Return the configured reader mode.""" 

77 

78 return self._parser.reader_mode 

79 

80 def open(self, file_path: str | Path, mode: str = "auto") -> FastaParser: 

81 """Open a FASTA file. 

82 

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. 

86 

87 Returns: 

88 FastaParser: The parser instance. 

89 """ 

90 

91 self.file_path = file_path 

92 self._parser.open(str(file_path), mode) 

93 return self 

94 

95 def close(self) -> None: 

96 """Close the parser.""" 

97 

98 self._parser.close() 

99 

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. 

109 

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. 

115 

116 Returns: 

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

118 """ 

119 

120 return self._parser.seek( 

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

122 ) 

123 

124 def next(self) -> FastaRecord | None: 

125 """Return the next FASTA record, or None at EOF. 

126 

127 Returns: 

128 FastaRecord | None: The next FASTA record, or None if there are no more records. 

129 """ 

130 

131 return self._parser.next() 

132 

133 def records(self, limit: int | None = 10000) -> list[FastaRecord]: 

134 """Return records from the current stream position. 

135 

136 Args: 

137 limit (int | None): The maximum number of records to return. Default is 10000. 

138 

139 Returns: 

140 list[FastaRecord]: A list of FASTA records. 

141 """ 

142 

143 return self._parser.records(limit) 

144 

145 @staticmethod 

146 def parse_text(data: str, limit: int | None = None) -> list[FastaRecord]: 

147 """Parse FASTA records from text. 

148 

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. 

152 

153 Returns: 

154 list[FastaRecord]: A list of Fasta records. 

155 """ 

156 

157 return _RustFastaParser.parse_text(data, limit) 

158 

159 @staticmethod 

160 def parse_file(file_path: str | Path, limit: int | None = None) -> list[FastaRecord]: 

161 """Parse FASTA records from a file. 

162 

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. 

166 

167 Returns: 

168 list[FastaRecord]: A list of Fasta records. 

169 """ 

170 

171 return _RustFastaParser.parse_file(str(file_path), limit)