Skip to main content

🔗🐍🧬 rammappy Stars

PyO3 bindings and zero-copy Python interface to rammap, the Rust implementation of minimap2.

Release PyPI ruff ty

⚠️ Warning

This library is still in active development, with API refinements possible between minor versions.


🗺️ Overview

rammappy provides a zero-dependency, immutable, thread-safe Python OOP interface over rammap-core (Python >=3.12, abi3-py312 + free-threaded Python 3.14t):

  • Zero third-party dependencies: Pure Rust extension + Python standard library (collections.abc.Buffer & memoryview).
  • PEP 3118 zero-copy FFI in both directions:
    • Inputs: Accept bytes, bytearray, memoryview slices, mmap.mmap, or Record instances directly without copying (strictly rejecting str).
    • Outputs: MappingBatch, PairedMappingBatch, DpAlignmentBatch, and Sketch expose contiguous Struct-of-Arrays memoryview columns (offsets, scores, target_ids, cigar_data, cigar_offsets, etc.) that work natively in Python stdlib and wrap in $O(1)$ zero-copy via numpy.asarray(...) or pyarrow.
  • Immutable & thread-safe (#[pyclass(frozen)]): Index and Aligner share Arc<Index> in $O(1)$ without serialization. Single-thread (threads=1, the default) runs sequentially on the GIL-detached calling thread without spawning OS threads, threads=0 shares Rayon's global pool, and threads>1 lazily allocates an isolated worker pool with per-worker DP/chaining scratch reuse.

🔧 Installing

uv add rammappy

For local development:

just install

💡 Examples

🧬 Parsing FASTA/FASTQ Files & Zero-Copy mmap

All file readers, memory parsers, and chunk streamers yield immutable Record objects:

import mmap
import rammappy

# Optional context manager deterministically closes the OS file descriptor
with rammappy.FastxReader("sequences.fq.gz") as reader:
    for record in reader:
        mv = memoryview(record)  # zero-copy PEP 3118 byte view of record.sequence
        assert len(mv) == len(record)

# Parse directly from an mmap or memoryview slice without copying
with open("reference.fa", "rb") as fh, mmap.mmap(fh.fileno(), 0, access=mmap.ACCESS_READ) as mm:
    records = rammappy.parse_fasta_bytes(mm)

# Chunk-fed streaming (subclasses SequenceStreamer)
streamer = rammappy.FastaStreamer(rna_to_dna=False)
streamer.push(b">seq1\nACGT\n>seq2\nGGGG\n")
streamer.finalize()
for record in streamer:
    assert isinstance(record, rammappy.Record)

🧠 Building an In-Memory or Streaming Index & Aligner

Index.build, IndexBuilder, and Index.load_bytes / Index.to_bytes support in-memory, low-RSS streaming, and serialized buffer workflows, while Aligner.from_reference auto-detects .mmi / RMMI index files vs FASTA/FASTQ references via magic-byte sniffing:

import rammappy

# Stream contigs one-by-one without holding all raw FASTA bytes in memory at once
builder = rammappy.IndexBuilder(k=15, w=10, threads=4)
builder.add(b"chr1", b"ACGT" * 1000)
builder.add(b"chr2", b"TGCA" * 1000)
index = builder.finish()
assert b"chr1" in index

# In-memory index round-trip (also supports minimap2 .mmi files via Index.load / load_bytes)
raw_rmmi = index.to_bytes()
reloaded = rammappy.Index.load_bytes(memoryview(raw_rmmi))

# Share Arc<Index> in O(1) across one or more immutable Aligners
aligner = rammappy.Aligner(reloaded, preset=rammappy.Preset.Sr, threads=4)

⚙️ Immutable Configuration (MapOptions & AlignFlags)

Configuration objects are immutable (frozen) value objects supporting .replace(...) (and Python 3.13+ copy.replace), making accidental lost-mutation bugs impossible:

from rammappy import AlignFlags, MapOptions, Preset, ScoringParams

opts = MapOptions.from_preset(Preset.Sr).replace(
    scoring=ScoringParams(match_score=3, mismatch_penalty=6),
    flags=AlignFlags.SHORT_READ | AlignFlags.ALL_CHAINS | AlignFlags.OUT_CIGAR,
)

custom_aligner = aligner.with_options(opts)

🔬 Parallel Batch Alignment (MappingBatch)

Aligner.map_batch returns a CSR MappingBatch supporting both lazy Python Sequence iteration (batch[query_idx] -> QueryMappings -> Mapping) and zero-copy PEP 3118 memoryview columns:

queries = [
    (b"q1", b"ACGT" * 20),
    (b"q2", b"TGCA" * 20),
]

batch = aligner.map_batch(queries)

# 1. Idiomatic OOP Sequence access (preserves CIGAR, cs, md, target_name)
for q_idx, hits in enumerate(batch):
    if hits:
        top = hits[0]
        assert top.cigar is not None

# 2. Zero-copy stdlib memoryview column slicing (CSR offsets in record indices)
offsets = batch.offsets
scores = batch.scores
cigar_offsets = batch.cigar_offsets
cigar_data = batch.cigar_data  # BAM uint32 words: (op_len << 4) | op_code

q0_scores = scores[offsets[0] : offsets[1]]

👯 Paired-End Mapping (map_pair & PairedMappingBatch)

Aligner.map_pair and Aligner.map_pairs_batch run native structured paired-end rescue and proper-pair scoring (is_proper_pair), preserving full CIGAR (full_cigar), chimeric sa_tag, and mate-separated zero-copy MappingBatch views:

pairs = [
    (b"frag1", r1_seq, r2_seq),
]

pe_batch = aligner.map_pairs_batch(pairs)
r1_hits, r2_hits = pe_batch[0]
if r1_hits and r2_hits:
    assert r1_hits[0].is_proper_pair
    full = r1_hits[0].full_cigar  # FullCigar with leading/trailing S/H clips & seq_range

⚡ SIMD Pairwise Dynamic Programming (rammappy.dp)

PairwiseAligner exposes rammap's SIMD Needleman-Wunsch (Global), Smith-Waterman (Local), banded single/dual-affine SemiGlobal, and Z-drop seed Extension kernels directly without building an index:

from rammappy.dp import DpMode, DpScoring, PairwiseAligner

dp_aligner = PairwiseAligner(
    DpMode.Global,
    scoring=DpScoring(match_score=2, mismatch_penalty=4, gap_open=4, gap_extend=2),
)
aln = dp_aligner.align(b"ACGTACGT", b"ACGTTCGT")
assert aln.cigar == b"8M"

dp_batch = dp_aligner.align_batch([(b"ACGTACGT", b"ACGTTCGT"), (b"AAAA", b"AAAT")])
assert len(dp_batch.scores) == 2

📊 Polymorphic Sequence Sketchers (Sketcher -> Sketch)

MinimizerSketcher, SyncmerSketcher, and RandstrobeSketcher inherit from Sketcher and return a Sketch container with both Minimizer indexing and zero-copy memoryview columns:

sketcher: rammappy.Sketcher = rammappy.MinimizerSketcher(k=15, w=10)
sketch = sketcher.sketch(b"ACTG" * 50)

first = sketch[0]
assert first.hash == sketch.hashes[0]
assert first.position == sketch.positions[0]

🔄 Upgrading from 0.1.x (Breaking Changes)

The 0.2.0 architecture redesign introduces an immutable OOP model, strict zero-copy PEP 3118 byte buffers, and zero runtime dependencies. See CHANGELOG.md for full release notes.

Area 0.1.x 0.2.0
Python version >=3.10 >=3.12 (abi3-py312, PEP 3118 Buffer)
Sequence / name FFI Accepted str or bytes Contiguous byte buffers (bytes, bytearray, memoryview, mmap) or Record (str raises TypeError)
Record signature Record(name, desc, seq, qual) Record(name, seq, *, description=None, quality=None)
FASTA/FASTQ readers Returned (name, seq) tuples All readers & streamers yield Record objects
Options & mutability In-place mutation (opts.scoring.match_score = 3, index.strip_sequences(), aligner.load_junctions_bed(...)) Immutable frozen types (opts.replace(...), index.without_sequences(), aligner.with_options(...), aligner.with_junctions_bed(...))
Batch alignment list[MappingIterator] / NumPy map_batch_numpy Zero-dependency CSR MappingBatch (Sequence[QueryMappings] + zero-copy memoryview columns, numpy.asarray-ready)
Sketchers MinimizerSketcher(w, k) -> list[Minimizer] MinimizerSketcher(k, w, *, is_hpc=False) (subclass of Sketcher) -> Sketch (Sequence[Minimizer] + memoryview columns)
Exceptions & Strand ValueError on missing contig; FastxReader swallowed I/O errors Dual-inheritance SequenceNotFoundError (KeyError), FormatError (ValueError), IndexIOError (OSError); Strand IntEnum (+1 / -1)

👏 Acknowledgments

We thank the original authors of rammap, Jeremy R. Wang and Heng Li, for their high-performance Rust implementation of minimap21.

📚 References

  1. Jeremy R. Wang and Heng Li. Memory-safe high-performance sequence mapping with rammap (2026). bioRxiv. 10.64898/2026.05.26.726289. ↩

Metadata

Release files for rammappy 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for rammappy 0.3.0
File Size Uploaded Provenance Actions
rammappy-0.3.0.tar.gz 116.8 kB With provenance Details for rammappy-0.3.0.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for rammappy 0.3.0
File Interpreter ABI Platform Actions
rammappy-0.3.0-cp312-abi3-win_amd64.whl CPython 3.12 abi3 Windows x86-64 Details for rammappy-0.3.0-cp312-abi3-win_amd64.whl
rammappy-0.3.0-cp312-abi3-manylinux_2_34_x86_64.whl CPython 3.12 abi3 Linux glibc 2.34+ x86-64 Details for rammappy-0.3.0-cp312-abi3-manylinux_2_34_x86_64.whl
rammappy-0.3.0-cp312-abi3-macosx_11_0_arm64.whl CPython 3.12 abi3 macOS 11.0+ ARM64 Details for rammappy-0.3.0-cp312-abi3-macosx_11_0_arm64.whl

Total release size: 3.0 MB

Release files / rammappy-0.3.0.tar.gz

Download URL rammappy-0.3.0.tar.gz
Size 116.8 kB
Tags Source
SHA-256 checksum
How to use checksums
230701e90092f0d0cb328515cfa92004233d7ea51a96a0ecd7ca111da6c5d55c
BLAKE2b-256 checksum
How to use checksums
f72fdd7cf8d2eb9be3d6fd58403309e010a54b332a8adfb6e6b9adf56947ce34
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 30, 2026.

Transparency log

Release files / rammappy-0.3.0-cp312-abi3-win_amd64.whl

Download URL rammappy-0.3.0-cp312-abi3-win_amd64.whl
Size 869.7 kB
Tags CPython 3.12 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
b11c37a04cc963beae704fcfb6811d0fea220ed5af5650629266284879e4055c
BLAKE2b-256 checksum
How to use checksums
564a3a5141ee9e9c07e2f4b168f086a6ac097b36e241d95add79e1d3dea71b14
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 30, 2026.

Transparency log

Release files / rammappy-0.3.0-cp312-abi3-manylinux_2_34_x86_64.whl

Download URL rammappy-0.3.0-cp312-abi3-manylinux_2_34_x86_64.whl
Size 1.1 MB
Tags CPython 3.12 Linux glibc 2.34+ x86-64 abi3
SHA-256 checksum
How to use checksums
31b40fbb83cc154c073d6faba8db841d96e4065c28673fe7e735f4b9bff35c0b
BLAKE2b-256 checksum
How to use checksums
fb96349224b3a569566cfbf64273de5168e6c931d136fd4fa58d5f043f2aeca1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 30, 2026.

Transparency log

Release files / rammappy-0.3.0-cp312-abi3-macosx_11_0_arm64.whl

Download URL rammappy-0.3.0-cp312-abi3-macosx_11_0_arm64.whl
Size 951.0 kB
Tags CPython 3.12 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
ba24a7d1ae7a06e2a80a976415b5fabd63d7b7b9d61c2bc23e8d6051f66ca903
BLAKE2b-256 checksum
How to use checksums
8696cc02bb84977225d2e968e21c18927007465336449bea6ab62bebb2cb2b72
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 30, 2026.

Transparency log

Security for rammappy 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 4 files in this release have been uploaded with provenance metadata.

Select from 4 files

Attestation for rammappy-0.3.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 30, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page