bgen
C library and Python bindings for reading BGEN files (format specifications 1.2 and 1.3).
This repository is a maintained fork of the original limix/bgen and limix/cbgen projects, merged into a single codebase with a modern build system, nanobind-based Python bindings, and S3 support.
Features
- Fast C library for parsing BGEN genotype files
- Python bindings via nanobind
- Supports BGEN 1.2 and 1.3 format specifications
- zlib and zstd decompression support
- Metafile indexing for efficient random access
Quick start (pixi)
pixi install
pixi run test
Building the C library
cmake -B build -G Ninja -DCMAKE_BUILD_TYPE=Release
cmake --build build
ctest --test-dir build --output-on-failure
System dependencies: zlib, zstd (installed via your system package manager or conda).
Installing the Python package
pip install .
Or in development mode:
pip install -e ".[test]"
pytest python/tests
Python usage
from cbgen import BgenFile, BgenMetafile
# Open a BGEN file
with BgenFile("example.bgen") as bgen:
print(f"Variants: {bgen.nvariants}, Samples: {bgen.nsamples}")
if bgen.contain_samples:
samples = bgen.read_samples()
# Create a metafile index for efficient access
bgen.create_metafile("example.bgen.metafile")
# Read variant data via metafile
with BgenFile("example.bgen") as bgen:
with BgenMetafile("example.bgen.metafile") as mf:
partition = mf.read_partition(0)
for variant in partition.variants:
gt = bgen.read_genotype(variant.offset)
print(f"{variant.rsid}: {gt.probability.shape}")
bgen.layout reports the native header's layout version (1 or 2), also
available on the legacy bgen_file API. Sample and variant counts are unsigned
32-bit values exposed as Python integers. Unsupported header flags and truncated
headers are rejected by the native reader.
Layout 1 supports raw and zlib-compressed, biallelic, diploid, unphased records,
including metafile indexing. Its three independently rounded probabilities are
decoded as q / 32768 without renormalization; all-zero triplets are marked
missing and returned as NaNs. Layout 2 supports raw, zlib and zstd blocks with
1--32-bit probabilities, variable ploidy, and multiple alleles. Phased records
whose samples all have zero ploidy return shape (nsamples, 0), retaining
their ploidy and missingness arrays. Both layouts support precision=32 and
precision=64; returned Python arrays remain valid after subsequent reads and
after closing the file.
Performance checks
scripts/benchmark_decode.py compares two Release shared-library builds through
their exported native public APIs. It generates deterministic, compressed,
biallelic diploid fixtures and measures single reads, batch reads, combination
scans and indexing. Timings exclude fixture generation, initial output-buffer
allocation and warm-up; old/new process order alternates across repetitions.
For example, on macOS:
pixi run python scripts/benchmark_decode.py --generate build-benchmark/fixtures \
--zstd .pixi/envs/default/lib/libzstd.dylib
pixi run python scripts/benchmark_decode.py \
--manifest build-benchmark/fixtures/fixtures.json \
--old /path/to/before/libbgen.dylib --new /path/to/after/libbgen.dylib \
--repeats 11 --loops 10 --output build-benchmark/results.json
Use matching compilers, optimization flags and compression libraries, and run without competing builds/tests. Results are local, warm-cache measurements, not a guarantee for other hardware, remote storage or arbitrary datasets.
C API
Link against the bgen library:
find_package(bgen REQUIRED)
target_link_libraries(mytarget PRIVATE BGEN::bgen)
See include/bgen/bgen.h for the full C API.
Acknowledgments
- Marc Jan Bonder for bug-reporting and improvement suggestions.
- Yan Wong for bug-reporting and improvement suggestions.
- Carl Kadie for bug-reporting and improvement suggestions.
Authors
License
This project is licensed under the MIT License.
Metadata
Release files for bgen-limix 4.12.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| bgen_limix-4.12.0.tar.gz | 4.9 MB | Details |
Built distributions (wheels)
| File | Reset | |||
|---|---|---|---|---|
| bgen_limix-4.12.0-cp311-abi3-win_amd64.whl | CPython 3.11 | abi3 | Windows x86-64 | Details |
| bgen_limix-4.12.0-cp311-abi3-manylinux_2_28_x86_64.whl | CPython 3.11 | abi3 | Linux glibc 2.28+ x86-64 | Details |
| bgen_limix-4.12.0-cp311-abi3-manylinux_2_28_aarch64.whl | CPython 3.11 | abi3 | Linux glibc 2.28+ ARM64 | Details |
| bgen_limix-4.12.0-cp311-abi3-macosx_11_0_arm64.whl | CPython 3.11 | abi3 | macOS 11.0+ ARM64 | Details |
Total release size: 18.5 MB
Release files / bgen_limix-4.12.0.tar.gz
| Download URL | bgen_limix-4.12.0.tar.gz |
|---|---|
| Size | 4.9 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3dec8f1f38cd942df978355e9c3ab8e72b6b404edfffcb28e9689e7885796668
|
|
BLAKE2b-256 checksum How to use checksums |
c3fb869d46fca596b9fecf0a6bcd496a438aeea762e6876d6ee02cf37b612081
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.13
|
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 Oct 16, 2026.
Transparency logRelease files / bgen_limix-4.12.0-cp311-abi3-win_amd64.whl
| Download URL | bgen_limix-4.12.0-cp311-abi3-win_amd64.whl |
|---|---|
| Size | 3.3 MB |
| Tags | CPython 3.11 Windows x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
ed49727923f50c87eda4c399884d5b2974144ac90f401fcc48d45012c1e100a6
|
|
BLAKE2b-256 checksum How to use checksums |
3f94df0bc7ba33916de740885314a8629f75f367c6b36adbc2451928d1899e95
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.13
|
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 Oct 16, 2026.
Transparency logRelease files / bgen_limix-4.12.0-cp311-abi3-manylinux_2_28_x86_64.whl
| Download URL | bgen_limix-4.12.0-cp311-abi3-manylinux_2_28_x86_64.whl |
|---|---|
| Size | 4.2 MB |
| Tags | CPython 3.11 Linux glibc 2.28+ x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
b2b10da68c2cf0cbbe26c66b7682fd1fcda1f203ca56c38b761a6e2533eb9057
|
|
BLAKE2b-256 checksum How to use checksums |
48a3a252662dce399281a1441080e63467c79be50057fa178a1acfd558c3aefa
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.13
|
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 Oct 16, 2026.
Transparency logRelease files / bgen_limix-4.12.0-cp311-abi3-manylinux_2_28_aarch64.whl
| Download URL | bgen_limix-4.12.0-cp311-abi3-manylinux_2_28_aarch64.whl |
|---|---|
| Size | 4.0 MB |
| Tags | CPython 3.11 Linux glibc 2.28+ ARM64 abi3 |
|
SHA-256 checksum How to use checksums |
33966adecd4a9621fd0474d66f2ea9b4a4abbac0414daca95bd1f448307d6f7b
|
|
BLAKE2b-256 checksum How to use checksums |
7bedd921df2175e9a64fa4311d177ada5b515c3b723d7a9aafec5b5bb38b7235
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.13
|
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 Oct 16, 2026.
Transparency logRelease files / bgen_limix-4.12.0-cp311-abi3-macosx_11_0_arm64.whl
| Download URL | bgen_limix-4.12.0-cp311-abi3-macosx_11_0_arm64.whl |
|---|---|
| Size | 2.1 MB |
| Tags | CPython 3.11 abi3 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
3b358043406b52f963c86e16e10113c7b6119d0dd92aa118e7fe3af8662aae5a
|
|
BLAKE2b-256 checksum How to use checksums |
3027f79dc4236442548be5413360584773a28b40124f8bb3cf2386f283d9da6e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.13
|
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 Oct 16, 2026.
Transparency log