-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfastapy.py
290 lines (239 loc) · 8.17 KB
/
fastapy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
"""A lightweight Python module to read and write FASTA sequence records"""
import bz2
import gzip
import io
import pathlib
import typing
import zipfile
__version__ = '1.0.5'
class Record:
"""Object representing a FASTA (aka Pearson) record.
Attributes:
id (str) : Sequence identifier
seq (str) : Sequence
description (str) : Description line (defline)
"""
def __init__(self, id: str, seq: str, desc: typing.Optional[str] = None):
"""Creates a Record.
Example:
>>> record = Record(id='NP_055309.2',
... seq='MRELEAKAT',
... desc='TNRC6A')
>>> print(record)
>NP_055309.2 TNRC6A
MRELEAKAT
"""
self.id = id
self.seq = seq
self.desc = desc
@property
def description(self) -> str:
"""Returns a description line (defline) of FASTA record.
Example:
>>> record = Record(id='NP_055309.2', seq='MRELEAKAT', desc='TNRC6A')
>>> print(record.description)
>NP_055309.2 TNRC6A
>>> record = Record(id='seqid', seq='ATCGA')
>>> print(record.description)
>seqid
"""
lst = [f'>{self.id}']
if self.desc:
lst.append(f'{self.desc}')
return " ".join(lst)
def __iter__(self):
"""Iterates over the characters in the sequence.
Example:
>>> record = Record(id='NP_055309.2', seq='MRELEAKAT', desc='TNRC6A')
>>> for amino_acid in record:
... print(amino_acid)
M
R
E
L
E
This is equivalent to iterating over the sequence directly:
>>> for amino_acid in record.seq:
... print(amino_acid)
M
R
E
L
E
"""
return iter(self.seq)
def __contains__(self, char):
"""Implements the 'in' keyword to search the sequence.
Example:
>>> record = Record(id='NP_055309.2', seq='MRELEAKAT', desc='TNRC6A')
>>> print('M' in record)
True
"""
return char in self.seq
def __str__(self):
"""Returns the record as a string in the FASTA format.
Example:
>>> record = Record(id='NP_055309.2', seq='MRELEAKAT', desc='TNRC6A')
>>> print(record)
>NP_055309.2 TNRC6A
MRELEAKAT
"""
return self.format().rstrip()
def __len__(self):
"""Returns the length of the sequence.
Example:
>>> record = Record(id='NP_055309.2', seq='MRELEAKAT')
>>> len(record)
9
"""
return len(self.seq)
def format(self, wrap:int = 70) -> str:
"""Returns a formatted FASTA record.
Args:
wrap (int): line length to wrap sequence lines.
Default: 70 characters, use zero (or None) for no wrapping.
Example:
>>> record = Record(id='NP_055309.2', seq='MRELEAKAT', desc='TNRC6A')
>>> print(record.format())
>NP_055309.2 TNRC6A
MRELEAKAT
>>> print(record.format(wrap=3))
>NP_055309.2 TNRC6A
MRE
LEA
KAT
"""
lst = [self.description, '\n']
if wrap:
for i in range(0, len(self.seq), wrap):
lst.append(f'{self.seq[i:i + wrap]}\n')
else:
lst.append(self.seq)
lst.append('\n')
return "".join(lst)
def parse_handle(handle) -> Record:
"""Iterates over `Record` objects from a FASTA file handle.
Args:
handle: a handle (file-like objects) that contains FASTA sequences
Returns:
A generator that yields `Record` objects.
Raises:
This function does not raise any exception.
Example:
with open("test/test.fasta") as handle:
for record in parse_handle(handle):
print(record.id, record.seq, record.description)
"""
seqid = None
desc = None
seq = []
for line in handle:
if line.startswith('>'):
if seq:
yield Record(seqid, "".join(seq), desc)
seq.clear()
seqid = line.split()[0][1:]
desc = line[len(seqid) + 1:].strip()
else:
seq.append(line.rstrip())
if seq:
yield Record(seqid, "".join(seq), desc)
def parse(filename: typing.Union[str, pathlib.Path]) -> Record:
"""Determines the compression type of a file and yields FASTA records.
Args:
filename: a name or pathlib.Path of a file containing FASTA sequences
Returns:
A generator that yields `Record` objects.
Raises:
FileNotFoundError: If the input file cannot be found.
OSError: If an operating system error occurs while opening the file.
ValueError: If the compression type of the file is unknown.
"""
# Try to open the file as a plain text.
try:
with open(filename, 'rt') as fh:
yield from parse_handle(fh)
return # Exit early if the file is successfully parsed
except (FileNotFoundError, OSError):
raise # Re-raise known exceptions
except Exception:
# Continue to check for compression types if an unknown exception occurs
pass
# Check and handle compressed formats
compression_type = get_compression_type(filename)
if compression_type == 'bz2':
with bz2.open(filename, 'rt') as fh:
yield from parse_handle(fh)
elif compression_type == 'gz':
with gzip.open(filename, 'rt') as fh:
yield from parse_handle(fh)
elif compression_type == 'zip':
with zipfile.ZipFile(filename) as z:
# Assuming the first file in the archive is the one we want
inner_filename = z.namelist()[0]
with io.TextIOWrapper(z.open(inner_filename)) as fh:
yield from parse_handle(fh)
return
else:
raise ValueError(f"Unknown compression type in file: '{filename}'")
def read(filename: typing.Union[str, pathlib.Path]) -> Record:
"""Reads a single `Record` object from a FASTA file.
Args:
filename: a name or pathlib.Path of a file containing FASTA sequences
Returns:
A single `Record` object.
Raises:
ValueError: If no FASTA records are found in the file.
"""
iterator = parse(filename)
try:
record = next(iterator)
except StopIteration:
raise ValueError(f"No records found in file: '{filename}'") from None
return record
def to_dict(records) -> dict:
"""Turns a generator or list of `Record` objects into a dictionary.
Args:
records: a generator that yields `Record` objects, or a list of
`Record` objects
Returns:
A dict mapping sequence id (key) to `Record` object (value).
Raises:
ValuError: If duplicate record ids are found.
Example:
>>> import fasta
>>> record_dict = fasta.to_dict(fasta.parse('test.fasta'))
>>> print(sorted(record_dict.keys()))
['ENO94161.1', 'NP_002433.1', 'sequence']
>>> print(record_dict['ENO94161.1'].description)
RRM domain-containing RNA-binding protein
>>> len(pdict)
3
"""
d = {}
for record in records:
if record.id in d:
raise ValueError(f"Duplicated key: '{record.id}'")
d[record.id] = record
return d
def get_compression_type(
filename: typing.Union[str, pathlib.Path]
) -> typing.Union[str, None]:
"""Returns the compression type of a file based on its first few bytes.
Args:
filename: a name or pathlib.Path of a file containing FASTA sequences
Returns:
A string representing the compression type of the file, or None
if the compression type could not be determined.
Reference:
http://stackoverflow.com/questions/13044562
"""
MAGIC_DICT = {b'\x1f\x8b\x08': 'gz',
b'\x42\x5a\x68': 'bz2',
b'\x50\x4b\x03\x04': 'zip'}
with open(filename, 'rb') as fh:
file_start = fh.read(max(len(few_bytes) for few_bytes in MAGIC_DICT))
for first_bytes, compression_type in MAGIC_DICT.items():
if file_start.startswith(first_bytes):
return compression_type
return None