# Genotype matrix in hail 0.2

**URL:** <https://discuss.hail.is/t/genotype-matrix-in-hail-0-2/834>\
**Category:** Hail Query & hailctl\
**Created:** [January 16, 2019, 1:57pm UTC](https://discuss.hail.is/t/genotype-matrix-in-hail-0-2/834 "2019-01-16T13:57:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wei\_Zhang](https://avatars.discourse-cdn.com/v4/letter/w/3d9bf3/32.png) [@Wei\_Zhang](https://discuss.hail.is/u/Wei_Zhang)\
**Post date:** [January 16, 2019, 1:57pm UTC](https://discuss.hail.is/t/genotype-matrix-in-hail-0-2/834/1 "2019-01-16T13:57:33Z")

</div>

Dear Hail team,

Given a genomic region, is it possible to output a genotype matrix (M individuals by N SNPs, in which the values are minor allele counts (0, 1, 2)) with hail 0.2?

Thank you very much!  
Best regards,  
Wei

---

<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:** [January 16, 2019, 5:45pm UTC](https://discuss.hail.is/t/genotype-matrix-in-hail-0-2/834/2 "2019-01-16T17:45:30Z")

</div>

Hi Wei,  
Try the following:

```python
mt = hl.import_vcf('src/test/resources/sample.vcf')

mt = hl.filter_intervals(mt, [hl.parse_locus_interval('20:10620000-10650000')])

mt = mt.select_entries(GT = mt.GT.n_alt_alleles())

mt.make_table(separator='').export('/tmp/foo.txt')

```

You can change the interval(s) and files as necessary.

---

<div class="post-metadata">

**Author:** ![Wei\_Zhang](https://avatars.discourse-cdn.com/v4/letter/w/3d9bf3/32.png) [@Wei\_Zhang](https://discuss.hail.is/u/Wei_Zhang)\
**Post date:** [January 16, 2019, 6:50pm UTC](https://discuss.hail.is/t/genotype-matrix-in-hail-0-2/834/3 "2019-01-16T18:50:46Z")

</div>

Hi Tim,

Thank you so much! That is exactly what I need.

Best regards,  
Wei

---

<div class="post-metadata">

**Author:** ![enriquea](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.hail.is/enriquea/32/237_2.png) [@enriquea](https://discuss.hail.is/u/enriquea)\
**Post date:** [April 15, 2019, 3:23pm UTC](https://discuss.hail.is/t/genotype-matrix-in-hail-0-2/834/4 "2019-04-15T15:23:24Z")

</div>

Hi @tpoterba,

I was trying your suggestion to export a table containing ‘samples’ and ‘counts’ and I’m getting ‘RuntimeException: Class file too large!’.

Here what I did,

```auto
# aggregate by AF bins and consequence type
mt_grouped = (mt
              .group_rows_by(mt.af_bins, mt.csq_group)
              .aggregate_entries(n_hets=agg.count_where(mt.GT.is_het()))
              .result()
              )

# export table
tb = (mt_grouped
      .make_table(separator='')
      .export('samples_counts.txt')
      )

```

The grouped matrix have 10k samples, six consequence groups and 10 AF bins…so I expect a file with 600,000 rows (that’s small).

any idea? do you have any other suggestion to get a table with four columns (e.g. sample\_id, af\_bin, csq\_group and counts (entries))?

Best,

E.

---

<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 15, 2019, 3:47pm UTC](https://discuss.hail.is/t/genotype-matrix-in-hail-0-2/834/5 "2019-04-15T15:47:47Z")

</div>

the code you posted above will export a table with 10,000 fields – and this is where Hail is having trouble.

If you instead run

```auto
tb = (mt_grouped
      .entries()
      .export('samples_counts.txt'))

```

I expect things should work, and give you the 4-column file you want.

---

<div class="post-metadata">

**Author:** ![enriquea](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.hail.is/enriquea/32/237_2.png) [@enriquea](https://discuss.hail.is/u/enriquea)\
**Post date:** [April 15, 2019, 3:57pm UTC](https://discuss.hail.is/t/genotype-matrix-in-hail-0-2/834/6 "2019-04-15T15:57:43Z")

</div>

Hi Tim,

It works perfectly! 😉

Thanks
