@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)
Maintainers
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 —
seekto a virtual offset,readorreadlinefrom there,tellwhere 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 blockBlocks 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)andBgzfReader(fileobj=handle)are bothnew BgzfReader(source, { mode, maxCache }), wheresourceis aUint8Arrayor aBytesIO.BgzfWriter(filename, mode)isnew BgzfWriter(bytes, { mode, compresslevel }): mode"a"adds to the bytes given,"w"starts afresh.BgzfWriter(fileobj=handle)isnew 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.BytesIOis Python'sio.BytesIOas far as BGZF uses one. Unlike Python's,getvalue()still answers afterclose(), since closing a writer closes its handle.__next__isnextLine(), which throwsStopIterationat the end;for (const line of reader)is the ordinary way.withhas no counterpart: callclose(), which is all__exit__does.fileno()always throwsUnsupportedOperation, as it does in Python for a file in memory.- Virtual offsets are numbers, exact to 253, so
makeVirtualOffsetthrowsOverflowErrorfor 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.errorisStructErrorandzlib.erroris core'sZlibError; both are namederror, as Python names them.io.UnsupportedOperation,StopIterationandUnicodeEncodeErrorare 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.
