Skip to main content

Another Fastx Parser — tiny, dependency-free FASTA/FASTQ reader with transparent gzip/bzip2/zip/zstd decompression.

Project description

afp — Another Fastx Parser

CI Coverage PyPI Python versions License: MIT

A tiny, dependency-free Python reader/writer for FASTA and FASTQ files with transparent gzip / bzip2 / zip / zstandard decompression. Standard library only — zstandard is an optional extra used only for .zst inputs.

The whole module is a single file: afp.py.

Install

Three ways to use it:

Drop the file into your project:

curl -O https://raw.githubusercontent.com/conchoecia/afp/main/afp.py
# put afp.py somewhere on your python path

Or pip-install:

pip install run-afp               # core, no extras
pip install "run-afp[zstd]"       # also read .zst-compressed files
pip install "run-afp[dev]"        # pytest + zstandard for development

The PyPI distribution is run-afp (the bare afp name was already taken). The import name stays import afp.

Or vendor inside another repo: copy afp.py into your dependencies/ directory, add that directory to sys.path, then import afp.

Quick start

import afp

# Auto-detects FASTA vs FASTQ from the first byte, and gzip/bzip2/zip/zstd
# compression from the file's magic bytes (not its extension).
for rec in afp.parse("reads.fq.gz"):
    print(rec.id, len(rec.seq), rec.qual[:10])

for rec in afp.parse("genome.fa"):
    print(rec.id, rec.desc, rec.seq[:50])

Force a specific format if needed:

for rec in afp.parse("weirdly_named_file", format="fasta"):
    ...

# Or use the explicit parsers:
afp.parse_fasta("genome.fa")
afp.parse_fastq("reads.fq")

The Record object

class Record:
    id: str             # token after '>' or '@', up to first whitespace
    seq: str            # sequence, newlines stripped
    desc: str | None    # everything after id on the header line (or None)
    qual: str | None    # quality string (FASTQ only; None for FASTA)

Records are mutable. You can rewrite record.id in place.

rec.format()           # back to FASTA / FASTQ text
rec.format(wrap=80)    # FASTA only: wrap sequence at 80 columns
len(rec)               # length of seq
"ACGT" in rec          # membership on the sequence string
list(rec)              # iterate over letters

Writing

afp.write(records, "out.fa")          # plain
afp.write(records, "out.fa.gz")       # auto-gzip from .gz extension
afp.write(records, "out.fq.gz")       # FASTQ if the first record has `qual`
afp.write(records, "out.fa", wrap=80) # wrapped FASTA

Mixing FASTA and FASTQ records in a single output stream is rejected.

Compression helpers

afp.detect_compression("file")   # 'gzip' | 'bzip2' | 'zip' | 'zstd' | 'none'
afp.get_open_func("file.gz")     # returns gzip.open

detect_compression reads only the first 4 bytes — it's cheap to call.

Why

Built for projects that want to vendor a single Python file rather than pull in a multi-megabyte sequence toolkit, under a permissive license they can carry through to their own code. The whole module is one ~400-line file, no external runtime dependencies, no compiled extensions.

License

MIT — see LICENSE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

run_afp-0.1.3.tar.gz (14.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

run_afp-0.1.3-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file run_afp-0.1.3.tar.gz.

File metadata

  • Download URL: run_afp-0.1.3.tar.gz
  • Upload date:
  • Size: 14.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for run_afp-0.1.3.tar.gz
Algorithm Hash digest
SHA256 1b75643db7fb2d4152670f19d94ba24ab22b967b144091261f876e250c044d01
MD5 1ee941dd2ad0c05e12868f3e2d7ccc0f
BLAKE2b-256 6361d399b3a7d0a3188b4050828a700a5d357700b2dfb8698f29dc71584481ef

See more details on using hashes here.

Provenance

The following attestation bundles were made for run_afp-0.1.3.tar.gz:

Publisher: publish.yml on conchoecia/afp

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file run_afp-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: run_afp-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 7.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for run_afp-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 20ab5aa7bb95d7f4dbe8139c1b9aa73726f74014115a0c893f71e69e7e285919
MD5 0b6bf7f0f96b725d8572929aafb47598
BLAKE2b-256 ec9401088d59bf5f20814ba304ee4a3b5e5344aced61894c39b12c4a5d246802

See more details on using hashes here.

Provenance

The following attestation bundles were made for run_afp-0.1.3-py3-none-any.whl:

Publisher: publish.yml on conchoecia/afp

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page