pybigtools

Python bindings to the Bigtools Rust library for high-performance reading and writing of BigWig and BigBed files.

Installation

pip install pybigtools

Quickstart

Open a file for reading. open() auto-detects whether the file is a BigWig or BigBed and returns a BBIReader:

import pybigtools

b = pybigtools.open("path/to/file.bigWig")  # also accepts an http(s) URL
print(b.chroms())          # {'chr1': 248956422, ...}
print(b.info())            # version, summary stats, zoom levels, ...

Rasterize values over a region as a NumPy array:

values = b.values("chr1", 0, 1000)        # one value per base
binned = b.values("chr1", 0, 1_000_000, bins=1000, summary="mean")

Iterate over raw records (intervals for BigWig, BED entries for BigBed):

for start, end, value in b.records("chr1"):
    ...

Files can be used as context managers, and file-like objects are accepted in place of a path:

with pybigtools.open(open("path/to/file.bigBed", "rb")) as b:
    schema = b.sql(parse=True)

Writing

import pybigtools

w = pybigtools.open("out.bigWig", "w")
w.write(
    {"chr1": 248956422},
    [("chr1", 0, 100, 1.5), ("chr1", 100, 200, 2.0)],
)
# the file is closed automatically once write() completes

See the API reference for the full set of methods and options.