# Export matrix table as vcf, group by chromosome

**URL:** <https://discuss.hail.is/t/export-matrix-table-as-vcf-group-by-chromosome/2639>\
**Category:** Hail Query & hailctl\
**Created:** [April 27, 2022, 6:20pm UTC](https://discuss.hail.is/t/export-matrix-table-as-vcf-group-by-chromosome/2639 "2022-04-27T18:20:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![TaotaoTan](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.hail.is/taotaotan/32/751_2.png) [@TaotaoTan](https://discuss.hail.is/u/TaotaoTan)\
**Post date:** [April 27, 2022, 6:20pm UTC](https://discuss.hail.is/t/export-matrix-table-as-vcf-group-by-chromosome/2639/1 "2022-04-27T18:20:48Z")

</div>

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?

```auto
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?

Best,  
Taotao

---

<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:** [April 27, 2022, 6:23pm UTC](https://discuss.hail.is/t/export-matrix-table-as-vcf-group-by-chromosome/2639/2 "2022-04-27T18:23:26Z")

</div>

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

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

```

The backend will optimize this into the interval filter!
