npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@biolibs/bgzf

v0.1.0

Published

BGZF, the blocked gzip of BAM and tabix files, verified against Biopython: read at any virtual offset, write byte for byte what Biopython writes, and list a file's blocks (Bio.bgzf)

Readme

@biolibs/bgzf

BGZF — the blocked gzip that BAM, tabix and bgzip files are written in — read and written the way Biopython's Bio.bgzf reads and writes it.

A BGZF file is a gzip file made of small blocks, each compressed on its own. Any gzip reader can unpack it, and a BGZF reader can start at any block, so a place in the file can be returned to without decompressing what comes before it. Such a place is a virtual offset: where its block starts in the file, and how far into that block's data it is, packed into one number.

  • Read at random — seek to a virtual offset, read or readline from there, tell where you are
  • Write byte for byte what Biopython writes — at every compression level, with blocks ended where you flush
  • List a file's blocks — where each is in the file and in the data
  • The same answers for a damaged file — cut short, a byte changed, not BGZF at all
  • One dependency, @biolibs/core, whose DEFLATE this is built on; nothing from Node or the browser

Biopython works on a file by name or by handle. Nothing here touches a file system: a file is its bytes (a Uint8Array), or a BytesIO over them when it matters where a handle stands.

What is verified

A generator has the real Biopython write files, read them in order and at random, list their blocks, and read files that have been cut short or had a byte changed or removed. Each case is a list of steps against one reader or writer; what every step returned or raised — exception type and message — and what tell() said after it are recorded, and the tests replay them.

| Upstream module | Items at parity | Checklist | |---|---|---| | Bio.bgzf | 27 / 27 | Bio.bgzf.md |

Against Biopython 1.88:

| Cases | What they record | |---|---| | 6,486 readers, 13,391 steps | whole files by line, by read and by iteration, in text and binary mode; every mode a reader takes or refuses; 1,440 random seeks into, to the end of, past the end of and between blocks, with caches of 1, 2 and 100 blocks; reading after close; and 6,000 damaged files | | 5,867 block listings | BgzfBlocks over good files and the same damaged ones | | 155 writers, 1,528 steps | every level from -1 to 9 and four that are refused; text, bytes and text that will not encode; appending; a handle that stands part way in; writes that fill a block exactly and by one byte either side; data that will not compress; and the file left at the end | | 267 | make_virtual_offset and split_virtual_offset | | 36 round trips | what the port writes is what Biopython writes, and reads back as what went in |

@biolibs/core gained a decompressor that returns what it has when the data stops early, as Python's zlib.decompressobj does, and refuses bad data for zlib's reasons in zlib's words: 12,349 more cases there.

Reading

import { BgzfReader, BgzfWriter } from "@biolibs/bgzf";

const writer = new BgzfWriter();
writer.write(">seq1\nACGT\n>seq2\nTTGA\n");
writer.close();
const file = writer.getvalue();                // a Uint8Array

const reader = new BgzfReader(file);           // text mode: Python's BgzfReader(fileobj=..., mode="r")
reader.readline();                             // ">seq1\n"
const here = reader.tell();                    // 6
reader.readline();                             // "ACGT\n"
[...reader];                                   // [">seq2\n", "TTGA\n"]
reader.seek(here);
reader.read(4);                                // "ACGT"

new BgzfReader(file, { mode: "rb" }).read(5);  // Uint8Array [ 62, 115, 101, 113, 49 ]

Text mode is Latin-1 with line ends left alone, as in Biopython, so that a character is always one byte. Read UTF-8 in binary mode and decode it yourself. read() needs a size: there is no reading "the rest".

Writing

const writer = new BgzfWriter(undefined, { compresslevel: 9 });
writer.write("first record\n");
writer.tell();                                 // 13: the block at byte 0, thirteen bytes in
writer.flush();                                // end the block here
writer.tell();                                 // 2686976: a block starting at byte 41, nothing in it yet
writer.write(Uint8Array.of(1, 2, 3));
writer.close();                                // writes the 28-byte end-of-file block

Blocks and virtual offsets

import { bgzfBlocks, makeVirtualOffset, splitVirtualOffset } from "@biolibs/bgzf";

[...bgzfBlocks(file)];            // [[0, 48, 0, 22], [48, 28, 22, 0]]
                                  //  start in file, length in file, start in data, length of data
makeVirtualOffset(48, 0);         // 3145728
splitVirtualOffset(3145728);      // [48, 0]

From Python

  • Bytes, not file names. BgzfReader(filename) and BgzfReader(fileobj=handle) are both new BgzfReader(source, { mode, maxCache }), where source is a Uint8Array or a BytesIO. BgzfWriter(filename, mode) is new BgzfWriter(bytes, { mode, compresslevel }): mode "a" adds to the bytes given, "w" starts afresh. BgzfWriter(fileobj=handle) is new BgzfWriter(bytesIO). open(bytes, mode) gives one or the other.
  • getvalue() on a writer returns the file. Biopython has no such method, having a file.
  • BytesIO is Python's io.BytesIO as far as BGZF uses one. Unlike Python's, getvalue() still answers after close(), since closing a writer closes its handle.
  • __next__ is nextLine(), which throws StopIteration at the end; for (const line of reader) is the ordinary way. with has no counterpart: call close(), which is all __exit__ does.
  • fileno() always throws UnsupportedOperation, as it does in Python for a file in memory.
  • Virtual offsets are numbers, exact to 253, so makeVirtualOffset throws OverflowError for a block that starts 237 bytes (128 GiB) or more into a file. Biopython allows 2**48.
  • A negative read of a handle is "everything", as BytesIO.read(-5) is; a real file in Python refuses one. This is only reachable through a block whose stated size is smaller than its own header.
  • Errors. struct.error is StructError and zlib.error is core's ZlibError; both are named error, as Python names them. io.UnsupportedOperation, StopIteration and UnicodeEncodeError are defined here.
  • One zlib message differs on a Mac. Apple's zlib reports a code that stands for nothing as "invalid literal/length/distance code"; stock zlib, and this port, say "invalid literal/length code" or "invalid distance code".
  • Level 0 writes stored blocks split as zlib splits them when Biopython calls it, which is not how zlib.compress(data, 0) splits them.