Export matrix table as vcf, group by chromosome

Hi Hail team,

I am trying to export a matrix table as vcf.bgz. I found hl.export_vcf(dataset, 'output/example.vcf.bgz') will allow me to do so. However, in my downstream analysis, it will make more sense to export multiple vcfs that is grouped by chromosome. Is there an easy way to do this?

for chrm in range(1,23):
    chrom_mt = hl.filter_intervals(mt,[hl.parse_locus_interval(f'chr{chrm}', reference_genome='GRCh38')])
    hl.export_vcf(chrom_mt, f'{bucket}/XXX.Chr{chrm}.vcf.bgz')

I think the above code will work, but is there a more efficient way?


The above should be fine. You can also do the easier:

chrom_mt = mt.filter_rows(mt.locus.contig == f'chr{chrm}')

The backend will optimize this into the interval filter!

1 Like