<p>bgzip files are backward compatible with gzip, but I have issues when using bgzip compressed vcf files with snpeff (java) or perl scripts that uses IO::Uncompress::Gunzip (that I believe it uses zlib under the hood). In both cases the data is decompressed but truncated after a few hundred lines aprox. I could be totally wrong but I was wondering if zlib (or whatever gzip compatible library they are using) is getting confused with the bgzip bloks and only processing one or a few of them leaving the output incomplete.</p>

<p>perl code that does not work:</p>

<pre><code>#!/usr/bin/env perl
use strict;
use IO::Uncompress::Gunzip qw(gunzip $GunzipError) ;

my $infile = shift;
my $infh = IO::Uncompress::Gunzip-&gt;new( $infile ) or die "IO::Uncompress::Gunzip failed: $GunzipError\n";
my $line_count = 0;
while (my $line=&lt;$infh&gt;){

    $line_count++
}
print "total lines read = $line_count\n";
</code></pre>

<p>This gives 419 lines</p>

<pre><code>$ perl /home/pmg/tmp/test_zlib-bgzip.pl 460112_TTAGGC_L005_L006_C3HVJACXX.sorted.rmdup.varsit.vcf.gz
total lines read = 419
</code></pre>

<p>but using open with gzip pipe works:</p>

<pre><code>#!/usr/bin/env perl
use strict;

my $infile = shift;
open(my $infh , 'gzip -dc '.$infile.' |');  # I can use bgzip intead gzip
my $line_count = 0;
while (my $line=&lt;$infh&gt;){

    $line_count++
}
print "total lines read = $line_count\n";
</code></pre>

<p>Gives the expected number of lines</p>

<pre><code>$ perl /home/pmg/tmp/test_gzip-bgzip.pl 460112_TTAGGC_L005_L006_C3HVJACXX.sorted.rmdup.varsit.vcf.gz
total lines read = 652829
</code></pre>

<p>I googled about and I was unable to find quickly any relevant entry, but this is something that I am sure other people would have already faced. Do someone have a clue about why is this happening? I am using ubuntu 12.04.4 with perl 5.16</p>

<p>Hi. I have just encountered the same problem and it seems there is a pure-Perl solution surrounding this as shown below</p>

<pre>
my $status = gunzip(&quot;in.gz&quot; =&gt; &quot;out.gz&quot;, MultiStream =&gt; 1)
  or die &quot;gunzip failed: $GunzipError\n&quot;;</pre>

<p>It also seems Apache Commons Compress can provide similar support for Java. Hope this helps</p>
