protfasta 0.1.19__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- protfasta/__init__.py +517 -0
- protfasta/_configs.py +65 -0
- protfasta/_version.py +1 -0
- protfasta/data/README.md +21 -0
- protfasta/data/look_and_say.dat +15 -0
- protfasta/data/test_data/aligned_seq_all_valid.fasta +6 -0
- protfasta/data/test_data/aligned_seq_all_valid_convertable.fasta +6 -0
- protfasta/data/test_data/aligned_seq_all_valid_unconvertable.fasta +6 -0
- protfasta/data/test_data/fixable_invalid.fasta +10 -0
- protfasta/data/test_data/test_data_with_bad_chars.fa +44 -0
- protfasta/data/test_data/test_data_with_nonstandard_chars.fa +44 -0
- protfasta/data/test_data/testset_1.fasta +96 -0
- protfasta/data/test_data/testset_duplicate.fasta +26 -0
- protfasta/data/test_data/testset_duplicate_seqs.fasta +26 -0
- protfasta/data/test_data/unfixable_invalid.fasta +10 -0
- protfasta/io.py +690 -0
- protfasta/protfasta.py +232 -0
- protfasta/protfasta_exceptions.py +24 -0
- protfasta/scripts/__init__.py +0 -0
- protfasta/scripts/pfasta.py +400 -0
- protfasta/tests/__init__.py +3 -0
- protfasta/tests/conftest.py +7 -0
- protfasta/tests/outdata/readme.md +1 -0
- protfasta/tests/outdata/test_out_1.fasta +105 -0
- protfasta/tests/outdata/test_out_2.fasta +108 -0
- protfasta/tests/pfasta_test/100_seqs_with_allvalid.fasta +1109 -0
- protfasta/tests/pfasta_test/100_seqs_with_invalid.fasta +1091 -0
- protfasta/tests/pfasta_test/input_test.fasta +244 -0
- protfasta/tests/pfasta_test/run_coverage_tests.sh +110 -0
- protfasta/tests/test_pfasta_cli.py +437 -0
- protfasta/tests/test_protfasta.py +1491 -0
- protfasta/utilities.py +461 -0
- protfasta-0.1.19.dist-info/METADATA +97 -0
- protfasta-0.1.19.dist-info/RECORD +38 -0
- protfasta-0.1.19.dist-info/WHEEL +5 -0
- protfasta-0.1.19.dist-info/entry_points.txt +2 -0
- protfasta-0.1.19.dist-info/licenses/LICENSE +22 -0
- protfasta-0.1.19.dist-info/top_level.txt +1 -0
protfasta/__init__.py
ADDED
|
@@ -0,0 +1,517 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import os
|
|
4
|
+
from typing import Callable, Iterator, Optional, Union
|
|
5
|
+
|
|
6
|
+
from protfasta import utilities as _utilities
|
|
7
|
+
from protfasta import io as _io
|
|
8
|
+
from protfasta._configs import STANDARD_AAS, STANDARD_CONVERSION
|
|
9
|
+
from protfasta import protfasta as _protfasta
|
|
10
|
+
from protfasta.protfasta_exceptions import ProtfastaException
|
|
11
|
+
|
|
12
|
+
## ------------------------------------------------------------
|
|
13
|
+
# READTHEDOCS versioning hack
|
|
14
|
+
#
|
|
15
|
+
# Generate _version.py if missing and in the Read the Docs environment
|
|
16
|
+
if os.getenv("READTHEDOCS") == "True" and not os.path.isfile('../protfasta/_version.py'):
|
|
17
|
+
import versioningit
|
|
18
|
+
__version__ = versioningit.get_version('../')
|
|
19
|
+
else:
|
|
20
|
+
from ._version import __version__
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
## ------------------------------------------------------------
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
_ROOT = os.path.abspath(os.path.dirname(__file__))
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def _get_data(path: str) -> str:
|
|
32
|
+
"""Return the absolute path to a bundled data directory.
|
|
33
|
+
|
|
34
|
+
Used internally to locate test data shipped with the package.
|
|
35
|
+
|
|
36
|
+
Parameters
|
|
37
|
+
----------
|
|
38
|
+
path : str
|
|
39
|
+
Relative path (under the package ``data/`` directory) to resolve.
|
|
40
|
+
|
|
41
|
+
Returns
|
|
42
|
+
-------
|
|
43
|
+
str
|
|
44
|
+
Absolute filesystem path.
|
|
45
|
+
"""
|
|
46
|
+
return os.path.join(_ROOT, 'data', path)
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def read_fasta(
|
|
50
|
+
filename: str,
|
|
51
|
+
expect_unique_header: bool = True,
|
|
52
|
+
header_parser: Optional[Callable[[str], str]] = None,
|
|
53
|
+
check_header_parser: bool = True,
|
|
54
|
+
duplicate_sequence_action: str = 'ignore',
|
|
55
|
+
duplicate_record_action: str = 'fail',
|
|
56
|
+
invalid_sequence_action: str = 'fail',
|
|
57
|
+
alignment: bool = False,
|
|
58
|
+
return_list: bool = False,
|
|
59
|
+
output_filename: Optional[str] = None,
|
|
60
|
+
correction_dictionary: Optional[dict[str, str]] = None,
|
|
61
|
+
verbose: bool = False,
|
|
62
|
+
) -> Union[dict[str, str], list[list[str]]]:
|
|
63
|
+
"""Read a FASTA file, sanitize sequences, and return a dict or list.
|
|
64
|
+
|
|
65
|
+
This is the primary entry point for **protfasta**. At its simplest::
|
|
66
|
+
|
|
67
|
+
sequences = read_fasta('proteins.fasta')
|
|
68
|
+
|
|
69
|
+
returns a dictionary whose keys are FASTA headers and whose values
|
|
70
|
+
are amino-acid sequences. Many optional parameters allow automatic
|
|
71
|
+
handling of duplicates, invalid residues, alignment gap characters,
|
|
72
|
+
and more.
|
|
73
|
+
|
|
74
|
+
Sanitization is applied in the following order:
|
|
75
|
+
|
|
76
|
+
1. File is read, custom headers are parsed, and header uniqueness is
|
|
77
|
+
checked (when *expect_unique_header* is ``True``).
|
|
78
|
+
2. Duplicate records are processed (*duplicate_record_action*).
|
|
79
|
+
3. Duplicate sequences are processed (*duplicate_sequence_action*).
|
|
80
|
+
4. Invalid residues are processed (*invalid_sequence_action*).
|
|
81
|
+
5. Final sequences are optionally written to *output_filename*.
|
|
82
|
+
6. A dictionary or list is returned to the caller.
|
|
83
|
+
|
|
84
|
+
Incompatible option combinations are caught before the file is read.
|
|
85
|
+
|
|
86
|
+
Parameters
|
|
87
|
+
----------
|
|
88
|
+
filename : str
|
|
89
|
+
Path to the FASTA file to read.
|
|
90
|
+
|
|
91
|
+
expect_unique_header : bool, optional
|
|
92
|
+
If ``True`` (default), an exception is raised when a duplicate
|
|
93
|
+
header is encountered during parsing. Set to ``False`` when
|
|
94
|
+
the file is known to contain duplicate headers -- in that case
|
|
95
|
+
*return_list* should typically be ``True`` as well so that no
|
|
96
|
+
entries are silently lost via dictionary-key overwriting.
|
|
97
|
+
|
|
98
|
+
header_parser : callable or None, optional
|
|
99
|
+
A function ``(str) -> str`` applied to every raw header before
|
|
100
|
+
any uniqueness checks. Useful for extracting accession IDs
|
|
101
|
+
from structured headers. When *check_header_parser* is
|
|
102
|
+
``True`` (the default) the function is smoke-tested with the
|
|
103
|
+
string ``'this test string should work'`` before parsing
|
|
104
|
+
begins.
|
|
105
|
+
|
|
106
|
+
check_header_parser : bool, optional
|
|
107
|
+
If ``True`` (default), *header_parser* is tested with a dummy
|
|
108
|
+
string before the file is read to catch obvious problems early.
|
|
109
|
+
Set to ``False`` to skip this pre-check.
|
|
110
|
+
|
|
111
|
+
duplicate_record_action : str, optional
|
|
112
|
+
How to handle records that are identical in both header **and**
|
|
113
|
+
sequence. Default ``'fail'``.
|
|
114
|
+
|
|
115
|
+
* ``'ignore'`` -- keep all occurrences (requires
|
|
116
|
+
*expect_unique_header* = ``False``).
|
|
117
|
+
* ``'fail'`` -- raise an exception.
|
|
118
|
+
* ``'remove'`` -- keep only the first occurrence.
|
|
119
|
+
|
|
120
|
+
duplicate_sequence_action : str, optional
|
|
121
|
+
How to handle entries that share the same sequence regardless of
|
|
122
|
+
header. Default ``'ignore'``.
|
|
123
|
+
|
|
124
|
+
* ``'ignore'`` -- keep all occurrences.
|
|
125
|
+
* ``'fail'`` -- raise an exception.
|
|
126
|
+
* ``'remove'`` -- keep only the first occurrence.
|
|
127
|
+
|
|
128
|
+
invalid_sequence_action : str, optional
|
|
129
|
+
How to handle sequences containing non-standard amino-acid
|
|
130
|
+
characters. Default ``'fail'``.
|
|
131
|
+
|
|
132
|
+
* ``'ignore'`` -- silently accept invalid residues.
|
|
133
|
+
* ``'fail'`` -- raise an exception.
|
|
134
|
+
* ``'remove'`` -- discard the entire sequence.
|
|
135
|
+
* ``'convert'`` -- convert non-standard residues using
|
|
136
|
+
*correction_dictionary* (or built-in defaults); fail if any
|
|
137
|
+
unconvertible residues remain.
|
|
138
|
+
* ``'convert-ignore'`` -- convert what can be converted, then
|
|
139
|
+
ignore any remaining invalid residues.
|
|
140
|
+
* ``'convert-remove'`` -- convert what can be converted, then
|
|
141
|
+
discard sequences that still contain invalid residues.
|
|
142
|
+
|
|
143
|
+
alignment : bool, optional
|
|
144
|
+
If ``True``, dash (``'-'``) characters are treated as valid gap
|
|
145
|
+
characters and are neither flagged as invalid nor converted.
|
|
146
|
+
Default ``False``.
|
|
147
|
+
|
|
148
|
+
return_list : bool, optional
|
|
149
|
+
If ``True``, return a list of ``[header, sequence]`` pairs
|
|
150
|
+
instead of a dictionary. Required when duplicate headers are
|
|
151
|
+
present and you want to keep all of them. Default ``False``.
|
|
152
|
+
|
|
153
|
+
output_filename : str or None, optional
|
|
154
|
+
If provided, the final (sanitized) set of sequences is written
|
|
155
|
+
to a new FASTA file at this path before the function returns.
|
|
156
|
+
|
|
157
|
+
correction_dictionary : dict or None, optional
|
|
158
|
+
A mapping of non-standard characters to replacement strings used
|
|
159
|
+
when *invalid_sequence_action* involves conversion. When
|
|
160
|
+
``None``, the built-in table is used:
|
|
161
|
+
|
|
162
|
+
* ``B`` -> ``N``, ``U`` -> ``C``, ``X`` -> ``G``, ``Z`` -> ``Q``
|
|
163
|
+
* ``*`` -> ``''``, ``-`` -> ``''``, ``' '`` -> ``''``
|
|
164
|
+
|
|
165
|
+
A custom dictionary **replaces** the built-in table entirely.
|
|
166
|
+
|
|
167
|
+
verbose : bool, optional
|
|
168
|
+
If ``True``, informational messages are printed to stdout
|
|
169
|
+
during each processing step. Default ``False``.
|
|
170
|
+
|
|
171
|
+
Returns
|
|
172
|
+
-------
|
|
173
|
+
dict[str, str] or list[list[str]]
|
|
174
|
+
When *return_list* is ``False`` (default), a dictionary mapping
|
|
175
|
+
headers to sequences. When ``True``, a list of two-element
|
|
176
|
+
lists ``[header, sequence]``. Ordering always matches the
|
|
177
|
+
original file.
|
|
178
|
+
|
|
179
|
+
Raises
|
|
180
|
+
------
|
|
181
|
+
ProtfastaException
|
|
182
|
+
If any validation check fails or incompatible options are
|
|
183
|
+
provided.
|
|
184
|
+
"""
|
|
185
|
+
|
|
186
|
+
# first we sanity check all of the inputs provided. NOTE. If additional functionality is added, new
|
|
187
|
+
# keywords MUST be sanity checked in this function
|
|
188
|
+
_io.check_inputs(expect_unique_header,
|
|
189
|
+
header_parser,
|
|
190
|
+
check_header_parser,
|
|
191
|
+
duplicate_record_action,
|
|
192
|
+
duplicate_sequence_action,
|
|
193
|
+
invalid_sequence_action,
|
|
194
|
+
alignment,
|
|
195
|
+
return_list,
|
|
196
|
+
output_filename,
|
|
197
|
+
verbose,
|
|
198
|
+
correction_dictionary)
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
# the actual file i/o happens here
|
|
202
|
+
raw = _io.internal_parse_fasta_file(filename, expect_unique_header=expect_unique_header, header_parser=header_parser, verbose=verbose)
|
|
203
|
+
|
|
204
|
+
# first deal with duplicate records
|
|
205
|
+
updated = _protfasta._deal_with_duplicate_records(raw, duplicate_record_action, verbose)
|
|
206
|
+
|
|
207
|
+
# deal with duplicate sequences
|
|
208
|
+
updated = _protfasta._deal_with_duplicate_sequences(updated, duplicate_sequence_action, verbose)
|
|
209
|
+
|
|
210
|
+
# next decide how we deal with invalid amino acid sequences
|
|
211
|
+
|
|
212
|
+
##
|
|
213
|
+
## If we're using the convert-remove action...
|
|
214
|
+
if invalid_sequence_action == 'convert-remove':
|
|
215
|
+
|
|
216
|
+
# first run a convert ignore
|
|
217
|
+
updated = _protfasta._deal_with_invalid_sequences(updated,
|
|
218
|
+
'convert-ignore',
|
|
219
|
+
alignment=alignment,
|
|
220
|
+
verbose=verbose,
|
|
221
|
+
correction_dictionary=correction_dictionary)
|
|
222
|
+
|
|
223
|
+
# then a remove on those that are left
|
|
224
|
+
updated = _protfasta._deal_with_invalid_sequences(updated,
|
|
225
|
+
'remove',
|
|
226
|
+
alignment=alignment,
|
|
227
|
+
verbose=verbose,
|
|
228
|
+
correction_dictionary=False)
|
|
229
|
+
|
|
230
|
+
else:
|
|
231
|
+
updated = _protfasta._deal_with_invalid_sequences(updated,
|
|
232
|
+
invalid_sequence_action,
|
|
233
|
+
alignment=alignment,
|
|
234
|
+
verbose=verbose,
|
|
235
|
+
correction_dictionary=correction_dictionary)
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
# If we wanted to write the final set of sequences we're going to use...:
|
|
239
|
+
if output_filename:
|
|
240
|
+
write_fasta(updated, output_filename)
|
|
241
|
+
|
|
242
|
+
# if we asked for a list...
|
|
243
|
+
if return_list is True:
|
|
244
|
+
pass
|
|
245
|
+
else:
|
|
246
|
+
updated = _utilities.convert_list_to_dictionary(updated, verbose)
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
return updated
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
# ------------------------------------------------------------------
|
|
254
|
+
#
|
|
255
|
+
def read_fasta_stream(
|
|
256
|
+
filename: str,
|
|
257
|
+
expect_unique_header: bool = True,
|
|
258
|
+
header_parser: Optional[Callable[[str], str]] = None,
|
|
259
|
+
check_header_parser: bool = True,
|
|
260
|
+
duplicate_sequence_action: str = 'ignore',
|
|
261
|
+
duplicate_record_action: str = 'fail',
|
|
262
|
+
invalid_sequence_action: str = 'fail',
|
|
263
|
+
alignment: bool = False,
|
|
264
|
+
return_list: bool = False,
|
|
265
|
+
output_filename: Optional[str] = None,
|
|
266
|
+
correction_dictionary: Optional[dict[str, str]] = None,
|
|
267
|
+
verbose: bool = False,
|
|
268
|
+
) -> Iterator[Union[tuple[str, str], list[str]]]:
|
|
269
|
+
"""Stream a FASTA file record-by-record, sanitizing as it goes.
|
|
270
|
+
|
|
271
|
+
This is the streaming counterpart to :func:`read_fasta`. It takes an
|
|
272
|
+
identical set of arguments but, instead of loading the whole file and
|
|
273
|
+
returning a dictionary or list, it returns a **generator** that yields
|
|
274
|
+
one sanitized record at a time. This keeps peak memory bounded and
|
|
275
|
+
makes it possible to process files far larger than RAM::
|
|
276
|
+
|
|
277
|
+
for header, seq in read_fasta_stream('huge.fasta'):
|
|
278
|
+
...
|
|
279
|
+
|
|
280
|
+
Because a lazily-produced result cannot be a dictionary, the
|
|
281
|
+
dict-versus-list distinction of :func:`read_fasta` collapses: this
|
|
282
|
+
function yields ``(header, sequence)`` tuples by default, or
|
|
283
|
+
``[header, sequence]`` lists when *return_list* is ``True``.
|
|
284
|
+
|
|
285
|
+
All argument validation is performed eagerly, at call time, so bad
|
|
286
|
+
keyword combinations raise immediately (before iteration begins).
|
|
287
|
+
Data-dependent errors -- a duplicate header, a duplicate record or
|
|
288
|
+
sequence under a ``'fail'`` action, or an invalid residue -- are
|
|
289
|
+
inherent to streaming and are therefore raised **mid-iteration**, at
|
|
290
|
+
the offending record, rather than up front.
|
|
291
|
+
|
|
292
|
+
The same processing steps as :func:`read_fasta` are applied, in the
|
|
293
|
+
same order (header uniqueness, duplicate records, duplicate sequences,
|
|
294
|
+
invalid residues). See :func:`read_fasta` for a full description of
|
|
295
|
+
each argument; the notes below cover only where streaming differs.
|
|
296
|
+
|
|
297
|
+
Parameters
|
|
298
|
+
----------
|
|
299
|
+
filename : str
|
|
300
|
+
Path to the FASTA file to read.
|
|
301
|
+
|
|
302
|
+
expect_unique_header : bool, optional
|
|
303
|
+
As in :func:`read_fasta`. Enforced with a running set of seen
|
|
304
|
+
headers. Default ``True``.
|
|
305
|
+
|
|
306
|
+
header_parser : callable or None, optional
|
|
307
|
+
As in :func:`read_fasta`. Default ``None``.
|
|
308
|
+
|
|
309
|
+
check_header_parser : bool, optional
|
|
310
|
+
As in :func:`read_fasta`. Default ``True``.
|
|
311
|
+
|
|
312
|
+
duplicate_sequence_action : str, optional
|
|
313
|
+
As in :func:`read_fasta`. The ``'fail'`` and ``'remove'``
|
|
314
|
+
variants keep a running set of 16-byte sequence digests (never
|
|
315
|
+
whole sequences). Default ``'ignore'``.
|
|
316
|
+
|
|
317
|
+
duplicate_record_action : str, optional
|
|
318
|
+
As in :func:`read_fasta`. The ``'fail'`` and ``'remove'``
|
|
319
|
+
variants keep a running header-to-digest map. Default
|
|
320
|
+
``'fail'``.
|
|
321
|
+
|
|
322
|
+
invalid_sequence_action : str, optional
|
|
323
|
+
As in :func:`read_fasta`. Every mode is a per-record decision
|
|
324
|
+
and streams without extra state. Default ``'fail'``.
|
|
325
|
+
|
|
326
|
+
alignment : bool, optional
|
|
327
|
+
As in :func:`read_fasta`. Default ``False``.
|
|
328
|
+
|
|
329
|
+
return_list : bool, optional
|
|
330
|
+
If ``True``, yield ``[header, sequence]`` lists (matching the
|
|
331
|
+
internal format of :func:`read_fasta` with ``return_list=True``);
|
|
332
|
+
otherwise yield ``(header, sequence)`` tuples. Default
|
|
333
|
+
``False``.
|
|
334
|
+
|
|
335
|
+
output_filename : str or None, optional
|
|
336
|
+
If provided, each sanitized record is written to this path as it
|
|
337
|
+
is yielded (60 residues per line, as in :func:`write_fasta`).
|
|
338
|
+
The file is only complete once the generator has been fully
|
|
339
|
+
consumed. Must differ from *filename*.
|
|
340
|
+
|
|
341
|
+
correction_dictionary : dict or None, optional
|
|
342
|
+
As in :func:`read_fasta`. Default ``None``.
|
|
343
|
+
|
|
344
|
+
verbose : bool, optional
|
|
345
|
+
If ``True``, print an opening message and, once the generator is
|
|
346
|
+
exhausted, a summary of removed/converted counts. Per-total
|
|
347
|
+
summaries are only emitted at exhaustion because a stream never
|
|
348
|
+
sees the file total up front. Default ``False``.
|
|
349
|
+
|
|
350
|
+
Returns
|
|
351
|
+
-------
|
|
352
|
+
Iterator[tuple[str, str]] or Iterator[list[str]]
|
|
353
|
+
A generator yielding ``(header, sequence)`` tuples (or
|
|
354
|
+
``[header, sequence]`` lists when *return_list* is ``True``) in
|
|
355
|
+
file order. Sequences are upper-cased and sanitized.
|
|
356
|
+
|
|
357
|
+
Raises
|
|
358
|
+
------
|
|
359
|
+
ProtfastaException
|
|
360
|
+
Eagerly, for invalid argument combinations; lazily (during
|
|
361
|
+
iteration) for data-dependent failures.
|
|
362
|
+
|
|
363
|
+
See Also
|
|
364
|
+
--------
|
|
365
|
+
read_fasta : Load and return the whole file as a dict or list.
|
|
366
|
+
write_fasta : Write sequences out to a FASTA file.
|
|
367
|
+
"""
|
|
368
|
+
|
|
369
|
+
# Validate arguments eagerly (this function is not itself a generator,
|
|
370
|
+
# so its body runs at call time -- bad keywords fail fast, before any
|
|
371
|
+
# iteration begins, matching read_fasta).
|
|
372
|
+
_io.check_inputs(expect_unique_header,
|
|
373
|
+
header_parser,
|
|
374
|
+
check_header_parser,
|
|
375
|
+
duplicate_record_action,
|
|
376
|
+
duplicate_sequence_action,
|
|
377
|
+
invalid_sequence_action,
|
|
378
|
+
alignment,
|
|
379
|
+
return_list,
|
|
380
|
+
output_filename,
|
|
381
|
+
verbose,
|
|
382
|
+
correction_dictionary)
|
|
383
|
+
|
|
384
|
+
# Streaming-specific guard: reading and simultaneously overwriting the
|
|
385
|
+
# same file would corrupt the input mid-stream. read_fasta is immune
|
|
386
|
+
# because it reads the whole file before writing, but streaming is not.
|
|
387
|
+
if output_filename is not None:
|
|
388
|
+
if os.path.abspath(output_filename) == os.path.abspath(filename):
|
|
389
|
+
raise ProtfastaException("keyword 'output_filename' must differ from 'filename' when streaming")
|
|
390
|
+
|
|
391
|
+
return _io._stream_fasta(filename,
|
|
392
|
+
expect_unique_header=expect_unique_header,
|
|
393
|
+
header_parser=header_parser,
|
|
394
|
+
duplicate_sequence_action=duplicate_sequence_action,
|
|
395
|
+
duplicate_record_action=duplicate_record_action,
|
|
396
|
+
invalid_sequence_action=invalid_sequence_action,
|
|
397
|
+
alignment=alignment,
|
|
398
|
+
return_list=return_list,
|
|
399
|
+
output_filename=output_filename,
|
|
400
|
+
correction_dictionary=correction_dictionary,
|
|
401
|
+
verbose=verbose)
|
|
402
|
+
|
|
403
|
+
|
|
404
|
+
|
|
405
|
+
# ------------------------------------------------------------------
|
|
406
|
+
#
|
|
407
|
+
def write_fasta(
|
|
408
|
+
fasta_data: Union[dict[str, str], list[list[str]]],
|
|
409
|
+
filename: str,
|
|
410
|
+
linelength: Union[int, bool, None] = 60,
|
|
411
|
+
append_to_fasta: bool = False,
|
|
412
|
+
) -> None:
|
|
413
|
+
"""Write sequences to a FASTA file.
|
|
414
|
+
|
|
415
|
+
Accepts sequence data as either a dictionary (header -> sequence) or
|
|
416
|
+
a list of ``[header, sequence]`` pairs and writes a standards-
|
|
417
|
+
compliant FASTA file to *filename*.
|
|
418
|
+
|
|
419
|
+
Parameters
|
|
420
|
+
----------
|
|
421
|
+
fasta_data : dict[str, str] or list[list[str]]
|
|
422
|
+
Sequence data. If a dictionary, keys are headers and values are
|
|
423
|
+
amino-acid sequences. If a list, each element must be a
|
|
424
|
+
two-element list ``[header, sequence]``.
|
|
425
|
+
|
|
426
|
+
filename : str
|
|
427
|
+
Destination file path. Should conventionally end with
|
|
428
|
+
``.fasta`` or ``.fa``, but this is not enforced.
|
|
429
|
+
|
|
430
|
+
linelength : int, bool, or None, optional
|
|
431
|
+
Maximum number of residues per line in the output. Default is
|
|
432
|
+
``60`` (the UniProt convention). Values below ``5`` are clamped
|
|
433
|
+
to ``5``. Set to ``0``, ``None``, or ``False`` to write each
|
|
434
|
+
sequence on a single line.
|
|
435
|
+
|
|
436
|
+
append_to_fasta : bool, optional
|
|
437
|
+
If ``True``, new entries are appended to *filename* if it
|
|
438
|
+
already exists; otherwise the file is created. If ``False``
|
|
439
|
+
(default), any existing file is overwritten.
|
|
440
|
+
|
|
441
|
+
Returns
|
|
442
|
+
-------
|
|
443
|
+
None
|
|
444
|
+
|
|
445
|
+
Raises
|
|
446
|
+
------
|
|
447
|
+
ProtfastaException
|
|
448
|
+
If a sequence is empty or a list element does not contain
|
|
449
|
+
exactly two items.
|
|
450
|
+
"""
|
|
451
|
+
|
|
452
|
+
# This part of code means we can pass either a dictionary or a list of lists
|
|
453
|
+
# in for write_fasta to deal with
|
|
454
|
+
|
|
455
|
+
if type(fasta_data) == list:
|
|
456
|
+
def get_sequence():
|
|
457
|
+
return (entry[0], entry[1])
|
|
458
|
+
|
|
459
|
+
# quick validate
|
|
460
|
+
for i in fasta_data:
|
|
461
|
+
if len(i) != 2:
|
|
462
|
+
raise ProtfastaException('While processing a list for write_fasta_file at least one of the elements was not a 2-position sublist:\n%s'%(str(i)))
|
|
463
|
+
|
|
464
|
+
if type(fasta_data) == dict:
|
|
465
|
+
def get_sequence():
|
|
466
|
+
return (entry, fasta_data[entry])
|
|
467
|
+
|
|
468
|
+
# override line length for sane input. N
|
|
469
|
+
if linelength == False or linelength == None or linelength < 1:
|
|
470
|
+
linelength = False
|
|
471
|
+
|
|
472
|
+
else:
|
|
473
|
+
# cast linelength to an integer here as a soft type checking, and
|
|
474
|
+
# if it's shorter than 5 then reset to 5
|
|
475
|
+
linelength = int(linelength)
|
|
476
|
+
if linelength < 5:
|
|
477
|
+
linelength = 5
|
|
478
|
+
|
|
479
|
+
# set the 'mode' for open. If append_to_file==False, use 'w' and overwrite
|
|
480
|
+
# existing .fasta file. Otherwise use 'a' and add to existing file if it exists.
|
|
481
|
+
if append_to_fasta==False:
|
|
482
|
+
open_mode='w'
|
|
483
|
+
else:
|
|
484
|
+
open_mode='a'
|
|
485
|
+
|
|
486
|
+
# Use a large write buffer (1 MiB) to minimise syscall overhead when
|
|
487
|
+
# writing very large files.
|
|
488
|
+
with open(filename, open_mode, buffering=1024 * 1024) as fh:
|
|
489
|
+
|
|
490
|
+
# for each entry
|
|
491
|
+
for entry in fasta_data:
|
|
492
|
+
|
|
493
|
+
(header, seq) = get_sequence()
|
|
494
|
+
if len(seq) < 1:
|
|
495
|
+
raise ProtfastaException('Seqence associated with [%s] is empty'%(header))
|
|
496
|
+
|
|
497
|
+
# write the header line
|
|
498
|
+
fh.write('>')
|
|
499
|
+
fh.write(header)
|
|
500
|
+
fh.write('\n')
|
|
501
|
+
|
|
502
|
+
if linelength:
|
|
503
|
+
# Slice the sequence into line-length chunks and write
|
|
504
|
+
# each chunk followed by a newline. This is O(n) with
|
|
505
|
+
# a handful of Python-level write calls, vs one write
|
|
506
|
+
# per residue in the naive implementation.
|
|
507
|
+
seq_len = len(seq)
|
|
508
|
+
for start in range(0, seq_len, linelength):
|
|
509
|
+
fh.write(seq[start:start + linelength])
|
|
510
|
+
fh.write('\n')
|
|
511
|
+
# Blank separator line between records.
|
|
512
|
+
fh.write('\n')
|
|
513
|
+
else:
|
|
514
|
+
# Single-line sequence output.
|
|
515
|
+
fh.write(seq)
|
|
516
|
+
fh.write('\n\n')
|
|
517
|
+
|
protfasta/_configs.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Internal configuration constants for protfasta.
|
|
3
|
+
|
|
4
|
+
Defines the standard amino-acid alphabet, the extended alphabet that
|
|
5
|
+
includes gap characters, and the default character-conversion tables
|
|
6
|
+
used when cleaning non-standard residues from protein sequences.
|
|
7
|
+
|
|
8
|
+
.............................................................................
|
|
9
|
+
protfasta was developed by the Holehouse lab
|
|
10
|
+
Original release March 2020
|
|
11
|
+
|
|
12
|
+
Question/comments/concerns? Raise an issue on github:
|
|
13
|
+
https://github.com/holehouse-lab/protfasta
|
|
14
|
+
|
|
15
|
+
Licensed under the MIT license.
|
|
16
|
+
|
|
17
|
+
Be kind to each other.
|
|
18
|
+
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
from __future__ import annotations
|
|
22
|
+
|
|
23
|
+
STANDARD_CONVERSION: dict[str, str] = {
|
|
24
|
+
'B': 'N',
|
|
25
|
+
'U': 'C',
|
|
26
|
+
'X': 'G',
|
|
27
|
+
'Z': 'Q',
|
|
28
|
+
'*': '',
|
|
29
|
+
'-': '',
|
|
30
|
+
' ': '',
|
|
31
|
+
}
|
|
32
|
+
"""Default mapping of non-standard characters to standard replacements."""
|
|
33
|
+
|
|
34
|
+
STANDARD_CONVERSION_WITH_GAP: dict[str, str] = {
|
|
35
|
+
'B': 'N',
|
|
36
|
+
'U': 'C',
|
|
37
|
+
'X': 'G',
|
|
38
|
+
'Z': 'Q',
|
|
39
|
+
' ': '',
|
|
40
|
+
'*': '',
|
|
41
|
+
}
|
|
42
|
+
"""Like :data:`STANDARD_CONVERSION` but preserves dashes as gap characters."""
|
|
43
|
+
|
|
44
|
+
STANDARD_AAS: list[str] = [
|
|
45
|
+
'A', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'K', 'L',
|
|
46
|
+
'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'Y',
|
|
47
|
+
]
|
|
48
|
+
"""The 20 standard amino-acid one-letter codes."""
|
|
49
|
+
|
|
50
|
+
STANDARD_AAS_WITH_GAP: list[str] = [
|
|
51
|
+
'A', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'K', 'L',
|
|
52
|
+
'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'Y', '-',
|
|
53
|
+
]
|
|
54
|
+
"""The 20 standard amino acids plus the dash gap character."""
|
|
55
|
+
|
|
56
|
+
# Internal frozensets used for O(1) membership tests on hot paths
|
|
57
|
+
# (public list versions are kept above for backwards compatibility).
|
|
58
|
+
_STANDARD_AAS_SET: frozenset[str] = frozenset(STANDARD_AAS)
|
|
59
|
+
_STANDARD_AAS_WITH_GAP_SET: frozenset[str] = frozenset(STANDARD_AAS_WITH_GAP)
|
|
60
|
+
|
|
61
|
+
# Pre-built translation tables for fast single-pass sequence cleaning via
|
|
62
|
+
# ``str.translate``. Built once at import time to avoid per-call rebuild.
|
|
63
|
+
_TRANSLATE_STANDARD = str.maketrans(STANDARD_CONVERSION)
|
|
64
|
+
_TRANSLATE_WITH_GAP = str.maketrans(STANDARD_CONVERSION_WITH_GAP)
|
|
65
|
+
|
protfasta/_version.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.1.19"
|
protfasta/data/README.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# Sample Package Data
|
|
2
|
+
|
|
3
|
+
This directory contains sample additional data you may want to include with your package.
|
|
4
|
+
This is a place where non-code related additional information (such as data files, molecular structures, etc.) can
|
|
5
|
+
go that you want to ship alongside your code.
|
|
6
|
+
|
|
7
|
+
Please note that it is not recommended to place large files in your git directory. If your project requires files larger
|
|
8
|
+
than a few megabytes in size it is recommended to host these files elsewhere. This is especially true for binary files
|
|
9
|
+
as the `git` structure is unable to correctly take updates to these files and will store a complete copy of every version
|
|
10
|
+
in your `git` history which can quickly add up. As a note most `git` hosting services like GitHub have a 1 GB per repository
|
|
11
|
+
cap.
|
|
12
|
+
|
|
13
|
+
## Including package data
|
|
14
|
+
|
|
15
|
+
Modify your package's `setup.py` file and the `setup()` command. Include the
|
|
16
|
+
[`package_data`](http://setuptools.readthedocs.io/en/latest/setuptools.html#basic-use) keyword and point it at the
|
|
17
|
+
correct files.
|
|
18
|
+
|
|
19
|
+
## Manifest
|
|
20
|
+
|
|
21
|
+
* `look_and_say.dat`: first entries of the "Look and Say" integer series, sequence [A005150](https://oeis.org/A005150)
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
1
|
|
2
|
+
11
|
|
3
|
+
21
|
|
4
|
+
1211
|
|
5
|
+
111221
|
|
6
|
+
312211
|
|
7
|
+
13112221
|
|
8
|
+
1113213211
|
|
9
|
+
31131211131221
|
|
10
|
+
13211311123113112211
|
|
11
|
+
11131221133112132113212221
|
|
12
|
+
3113112221232112111312211312113211
|
|
13
|
+
1321132132111213122112311311222113111221131221
|
|
14
|
+
11131221131211131231121113112221121321132132211331222113112211
|
|
15
|
+
311311222113111231131112132112311321322112111312211312111322212311322113212221
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
>sp|O00401|WASL_HUMAN Neural Wiskott-Aldrich syndrome protein OS=Homo sapiens OX=9606 GN=WASL PE=1 SV=2
|
|
2
|
+
MSSVQQQPPPPRRVTNVGSLLLTPQENESLFTFLGKKCVTMSSAVVQLYAADRNCMWSKK
|
|
3
|
+
UCSGVACLVKDNPQRSYFLRIFDIKDGKLLWEQELYNNFVYNSPRGYFHTFAGDTCQVALN
|
|
4
|
+
FANEXEEAKKFRKAVTDLLGRRQRKSEKRRDPPNGPNLPMATVDIKNPEITTNRFYGPQVN
|
|
5
|
+
NISHTKEKKKGKAKKKRLTKADIGTPSNFQHIGHVGWDPNTGFDLNNLDPELKNLFDMCG
|
|
6
|
+
ISEAQLKDRETSKVIYDFIEKTGGVEAVKNELRRQAPPPPPPSRGGPPPPPPPPHNSGPP
|
|
7
|
+
PPPARBGRGA***PPP---PPSRAPTAAPPPPPPSRPSVAVPPPPPNRMYPPPPPALPSSAPSGPPP
|
|
8
|
+
PPPSVLGVGPVAPPPPPPPPPPPGPPPPPGLPSDGDHQVPTTAGNKAALLDQIREGAQLK
|
|
9
|
+
KVEQNSRPVSCSGRDALLDQIRQGIQLKSVADGQESTPPTPAPTSGIVGALMEVMQKRSK
|
|
10
|
+
AIHSSDEDEDEDDEEDFEDDDEWED
|