# BlockMatrix specification?

**URL:** <https://discuss.hail.is/t/blockmatrix-specification/3118>\
**Category:** Hail Query & hailctl\
**Created:** [February 10, 2023, 2:03am UTC](https://discuss.hail.is/t/blockmatrix-specification/3118 "2023-02-10T02:03:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![biona001](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.hail.is/biona001/32/900_2.png) [@biona001](https://discuss.hail.is/u/biona001)\
**Post date:** [February 10, 2023, 2:03am UTC](https://discuss.hail.is/t/blockmatrix-specification/3118/1 "2023-02-10T02:03:32Z")

</div>

Hi community,

I wonder if there is a BlockMatrix file format specification? Something like the [PGEN spec](https://github.com/chrchang/plink-ng/blob/master/pgen_spec/pgen_spec.pdf)?

I downloaded a bunch of BlockMatrices from the gnomAD LD panel, which seems to be binary-compressed, and I want to write a parser in Julia that can read the (i,j)th entry like `bm[i,j]`. I read the [source code](https://github.com/hail-is/hail/blob/main/hail/python/hail/linalg/blockmatrix.py) but I can’t seem to find the info I need.

Any help/suggestion is appreciated. Thank you.

---

<div class="post-metadata">

**Author:** ![biona001](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.hail.is/biona001/32/900_2.png) [@biona001](https://discuss.hail.is/u/biona001)\
**Post date:** [February 10, 2023, 3:18am UTC](https://discuss.hail.is/t/blockmatrix-specification/3118/2 "2023-02-10T03:18:48Z")

</div>

After some experimentation, in python, accessing a single `BlockMatrix` entry (e.g. via `bm[0,0]`) takes ~0.3 seconds. Thus, building a 10 by 10 matrix takes almost 7 seconds. I don’t know if this performance is expected?

Here is a [naive Julia parser](https://github.com/biona001/gnomAD.jl/blob/main/src/hailBM.jl) that does essentially what I want. But it internally calls python code, so the speed is the same. I’d like to be able to do something like `bm[1:10000, 1:10000]` reasonably fast (within a few seconds?), if this is possible.

---

<div class="post-metadata">

**Author:** ![tpoterba](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.hail.is/tpoterba/32/61_2.png) [@tpoterba](https://discuss.hail.is/u/tpoterba)\
**Post date:** [February 10, 2023, 3:19am UTC](https://discuss.hail.is/t/blockmatrix-specification/3118/3 "2023-02-10T03:19:39Z")

</div>

It would be possible to write a reader in Python, but you won’t be able to randomly access elements – these files are compressed with LZ4.

We haven’t documented our file formats because they are subject to change and we don’t want to external code depending on them, though in practice we haven’t changed our formats in a while (though it may happen soon!)

These files are compressed in blocks. The easiest thing is to decompress each block first.

Each block is:

- four bytes comprising 32-bit int BLOCK\_LEN, followed by four bytes comprising 32-bit int DECOMP\_LEN, followed by (BLOCK\_LEN-4) compressed bytes. These bytes are compressed using LZ4.

Once you’ve decompressed and concatenated (virtually or physically) these blocks, the matrix block is specified by:

- four bytes comprising 32-bit int N\_ROWS
- four bytes comprising 32-bit int N\_COLS
- a byte denoting IS\_TRANSPOSE (1 if transposed, 0 if not)
- N\_ROWS \* N\_COLS \* 8-byte float64s, row-major if IS\_TRANSPOSE, column-major if not IS\_TRANSPOSE.
