protfasta - a robust parser for protein-based FASTA files.
For all documentation see https://protfasta.readthedocs.io/en/latest/.
For code see https://github.com/holehouse-lab/protfasta.
protfasta has been tested on Linux and macOS. It should also work on Windows but we haven't tested it there yet.
protfasta can be downloaded and installed directly from PyPI using pip:
pip install protfasta
If this has worked, the pfasta command-line tool should be available from the command-line
pfasta --help
And you're done. This also means you can now import and use protfasta in your Python workflow.
import protfasta
# sequences is now a dictionary where keys are FASTA headers and values are sequences.
sequences = protfasta.read_fasta('inputfile.fasta')
For files that are too large to fit in memory, read_fasta_stream() applies the same sanitization but yields one record at a time:
import protfasta
for header, sequence in protfasta.read_fasta_stream('huge.fasta'):
...
To run tests on your environment, clone the source code and from protfasta root
cd protfasta/tests
pytest --verboseTo run the test suite over all supported Python versions we use tox; from the protfasta source root directory simply run
tox And the tests should run across Python envs 3.9 to 3.15 (experimental).
For bug reports or errors please raise an issue on this github repository (see the Issues tab at the top).
-
0.1.23 (July 2026) - Bug fixes and more robust error handling.
- Fixed a crash when a FASTA file contained non-ASCII characters in a sequence. Duplicate detection hashes every sequence before invalid-residue handling runs, and the hashing step used an ASCII encoder, so any non-ASCII byte raised an unhandled
UnicodeEncodeErrorinstead of being reported (or removed/converted) as an invalid residue. This affectedread_fasta(...)with its default options. - All file-open failures now raise a
ProtfastaException. Previously only a missing file was handled, so passing a directory or an unreadable file raised a rawOSError. write_fasta(...)now raises aProtfastaExceptionwhen handed something that is neither a dictionary nor a list (it previously raised anUnboundLocalError) and whenlinelengthcannot be interpreted as an integer (it previously raised aTypeError). A numerical string such aslinelength='60'is now accepted.pathlib.Pathobjects are now accepted anywhere a filename is expected, including theoutput_filenamekeyword, which previously required a string. Passing a non-path (such as an integer, whichopen()would have silently treated as a file descriptor) now raises aProtfastaException.- Three input-validation error messages named the wrong keyword (
invalid_sequence) when reporting a badduplicate_record_action,duplicate_sequence_action, orinvalid_sequence_action. pfastano longer ignores--shortest-seq 0,--longest-seq 0, and--random-subsample 0, which were silently dropped because zero is falsy. Also tidied up the--helptext for the length filters and fixed some typos in the--print-statisticsoutput.- Added the
py.typedmarker file, which was declared inpyproject.tomlbut missing from the package, so the type annotations are now visible to type checkers. - Raised the minimum supported Python version to 3.9. The previous floor of 3.7 had not been tested for some time - both are long past end-of-life.
- Extended the test matrix to cover Python 3.9 through 3.14, plus the Python 3.15 pre-release (which is allowed to fail without blocking the build). The full test suite passes on all of them.
- Fixed a crash when a FASTA file contained non-ASCII characters in a sequence. Duplicate detection hashes every sequence before invalid-residue handling runs, and the hashing step used an ASCII encoder, so any non-ASCII byte raised an unhandled
-
0.1.22 (July 2026) - Updated the Read the Docs build configuration.
-
0.1.20 and 0.1.21 (July 2026) - Change defaults for
protfasta.read_fasta_stream(...)- Previously the default options for
protfasta.read_fasta_stream(...)led to a small O(N) memory growth due to duplicate record sanity checking. We have now changed the default behavior to not check for duplicates, ensuringprotfasta.read_fasta_stream(...)is truly memory flat. We also include a warning if options are passed toprotfasta.read_fasta_stream(...)that will not yeild a flat memory implementation. Note that even if this is the case, the memory footprint here remains much smaller than for theread_fasta(...)implementation. - Update to readme (for 0.1.21)
- Previously the default options for
-
0.1.19 (July 2026) - Streaming reads with full sanitization.
- New
protfasta.read_fasta_stream(...)- a streaming counterpart toread_fastawith an identical signature. It returns a generator that yields(header, sequence)tuples (or[header, sequence]lists withreturn_list=True) one record at a time, applying the same sanitization pipeline asread_fasta(duplicate handling, invalid-residue handling, alignment support, custom header parsing). Peak memory stays bounded to roughly one record, so files larger than RAM can be processed in a single pass. Sanitized output can be teed to disk as it streams viaoutput_filename. - Breaking change: removed the public
protfasta.iter_fastagenerator that was introduced in 0.1.18.read_fasta_streamsupersedes it - it provides the same streaming access plus the full sanitization pipeline. For the closest drop-in equivalent (streaming with no checks), useread_fasta_stream(f, expect_unique_header=False, duplicate_record_action='ignore', invalid_sequence_action='ignore'); or justread_fasta_stream(f)to additionally gain header-uniqueness, duplicate, and invalid-residue validation. - Added a dedicated
read_fasta_streamdocumentation page and worked examples, including guidance on when to useread_fasta_streamvsread_fasta. - Test suite expanded with 21 new tests covering the streaming parser.
- New
-
0.1.18 (April 2026) - Performance overhaul for large FASTA files (hundreds of millions of sequences).
read_fastanow streams the input file instead of reading it entirely into memory withreadlines()— peak memory is now O(single record) rather than O(file size).- New
protfasta.iter_fasta(filename, header_parser=None)generator for memory-bounded streaming access to(header, sequence)pairs from files that don't fit in RAM. - Core parser rewritten to use list-of-parts +
''.join()instead of quadratic string concatenation, and to skip header-uniqueness tracking entirely whenexpect_unique_header=False. convert_to_valid/ invalid-residue handling now uses a pre-builtstr.translatetable (single C-level pass) instead of a chainedstr.replaceloop — typically 5–20× faster.check_sequence_is_validnow uses frozenset membership instead of list-basedinscans.- Duplicate-detection utilities (
fail_on_duplicates,remove_duplicates,fail_on_duplicate_sequences,remove_duplicate_sequences) now store 16-byte blake2b digests instead of full sequences in their lookup structures, dramatically reducing peak memory for files with long sequences. write_fastareplaced its per-residuefh.write()loop with chunked slice writes and opens the output with a 1 MiB buffer — roughly two orders of magnitude faster on large files.- All existing behavior and the full test suite (239 tests) are preserved.
-
0.1.16 - 0.1.17 - skipped...
-
0.1.14 and 0.1.15 (October 2024) - Re-wrote build chain and versioning to use
pyproject.tomland versioningit. protfasta should now support Python beyond 3.12. About bloody time.- Added
--versionflag to pfasta - Messed around a bit with tags to ensure we had a tagged version compatible with them.
- Added
-
0.1.13 (January 2023) - Added upper limit of Python 3.11 to accomodate clash between versioneer and Python 3.12. Ultimately we'll move to versioningit for release versioning (as we have done internally) but need to make sure we have a robust protocol for this switch and then do this for ALL tools....
-
0.1.12 (March 2023) - integrated in check_header_parser flag via pull request from the amazing Friedlab !
-
Added in
append_to_fastaflag so you can append to an existing FASTA file (thanks Ryan!) -
0.1.11 (Sept 17th 2022) - re-wrote code for checking duplicate sequence to make it O(1) instead of O(n) for number of sequences (:-/) and added convert-remove option for invalid_sequences
-
0.1.9 (Sept 12th 2021) - added in robustness for whitespace in sequence files, which, bizarrely, was not present (i.e. added as an invalid residue type but can now be converted).
Copyright (c) 2020-2026, Alex Holehouse - Holehouse lab. protfasta is released under the MIT license. The codebase is well structured and relatively simple, lending it to feature addition. We welcome pull-requests assuming contributed code maintains an appropriate level of clarity and robustness.
Many of the software-engineering tools and approaches used in the development of protfasta come from resources developed by the Molecular Sciences Software Institute.