Reading uncompress file size from a gzip file is easy. The value is stored in the last 4 bytes of the gzip file. But it doesn't with bgzip file ! Any idea how can I get this information without uncompress the file ?
Reading uncompress file size from a gzip file is easy. The value is stored in the last 4 bytes of the gzip file. But it doesn't with bgzip file ! Any idea how can I get this information without uncompress the file ?
Each block of a BGZF file is itself a valid GZIP file. So you can calculate the overall uncompressed size as the total of all those size entries recorded in the last four bytes of each GZIP member file.
It remains to find the end of each BGZF block, but fortunately each block's header contains a BC extra subfield that records the compressed length of the block. Hence you can calculate the total uncompressed size without decompressing anything and only reading about 22 bytes out of each ~64K as follows:
You can find an implementation of this in C in my bgzfsize.c utility.
If you happen to have the *.gzi index file around (bgzip -i
option when compressing), you could read the uncompressed offset of the last block from the index file. This way you'll only need to decompress the last block to find its uncompressed size.
In Bash you could do it for example like this.
last_offset=$( tail -c 8 file.gz.gzi | od --endian=little -An -t u8 )
last_size=$( bgzip -b $offset file.gz | wc -c )
echo $(( $last_offset + $last_size ))
Explained: tail
to read the last 8 bytes from the index file which contains the uncompressed offset of the last block and od
to print is as an little-endian unsigned 64-bit integer (the format of the index file is described in the bgzip(1) man page). bgzip
to uncompress from that offset onwards and wc
to count the bytes in the uncompressed output.
If you don't happen to have the *.gzi index file, well, you could recreate it with bgzip -r
for an existing bgzipped file, but that of course requires processing the whole (compressed) file...
Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Is gzi same as tbi index ?
No, a gzi index file is in a different format than a tbi tabix index file. The gzi file is produced when running
bgzip
with the-i
switch when compressing or with the-r
switch when running on an existing bgzipped file.Ok ! I am asking because tbi is more common than gzi. So, I wonder if I can get this information from this index too.
Unfortunately I am not that familiar with the tabix tbi file format. The format is documented here, and by a quick glance I would assume that in a tbi index for a bgzipped file, all the offsets would be referring to the compressed file, and not the uncompressed content, so the answer would probably then be no.
You don't need to decompress the last chunk of data. You can access the information of the size of the last chunk in the same way you acessed the last offset:
Explained: BGZIP files contain an EOF marker, which is just an empty block compressed with GZIP. I happens to be of size 28. Also, the GZIP specification (RFC 1952) states that the last 4 bytes of a block contain the uncompressed size of that block. So get the last 28+4 bytes of the compressed file and decode the first 4 bytes to get the size of the last block.
Or, using only
od
for reading the file: