# Multiallelic in multisample MatrixTable

**URL:** <https://discuss.hail.is/t/multiallelic-in-multisample-matrixtable/2955>\
**Category:** Hail Query & hailctl\
**Created:** [November 28, 2022, 5:17am UTC](https://discuss.hail.is/t/multiallelic-in-multisample-matrixtable/2955 "2022-11-28T05:17:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![matin](https://avatars.discourse-cdn.com/v4/letter/m/db5fbb/32.png) [@matin](https://discuss.hail.is/u/matin)\
**Post date:** [November 28, 2022, 5:17am UTC](https://discuss.hail.is/t/multiallelic-in-multisample-matrixtable/2955/1 "2022-11-28T05:17:33Z")

</div>

Hello. What is the proper way to query multiallelic sites in a MatrixTable containing hundreds of samples? I know you can ask for ` hl.len(mt.alleles) > 3` , but what can you do for multiple samples?

---

<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:** [November 28, 2022, 1:07pm UTC](https://discuss.hail.is/t/multiallelic-in-multisample-matrixtable/2955/2 "2022-11-28T13:07:56Z")

</div>

Can you be specific about the query you want to write? You’re correct that:

```python
mt.filter_rows(hl.len(mt.alleles) >= 3)

```

will return a matrixtable with only multiallelic variants.

---

<div class="post-metadata">

**Author:** ![matin](https://avatars.discourse-cdn.com/v4/letter/m/db5fbb/32.png) [@matin](https://discuss.hail.is/u/matin)\
**Post date:** [November 28, 2022, 4:48pm UTC](https://discuss.hail.is/t/multiallelic-in-multisample-matrixtable/2955/3 "2022-11-28T16:48:33Z")

</div>

Let’s say we have a row, for which we have `hl.len(mt.alleleS) == 8`. We also have 3 samples at this position each with the following GT 1/2 0/1 2/3. How can we query what alleles each sample has and what they are?

---

<div class="post-metadata">

**Author:** ![danking](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.hail.is/danking/32/43_2.png) [@danking](https://discuss.hail.is/u/danking)\
**Post date:** [November 28, 2022, 9:08pm UTC](https://discuss.hail.is/t/multiallelic-in-multisample-matrixtable/2955/4 "2022-11-28T21:08:21Z")

</div>

@matin ,

I directly answer your question below, but I think we can give you a better answer if you tell us what you’re ultimately trying to accomplish. Are you trying to run a GWAS on these alleles? Are you doing a burden test? Something else?

Hail MatrixTables are not indexed by number of alleles, so this pipeline necessary scans across the entire dataset. For large datasets, this will take a long time. If you plan to do a lot of work on this subset of the variants, you might want to filter once and write to a smaller file and use the smaller file for your analysis.

* * *

```python
mt = hl.read_matrix_table(...)
mt = mt.filter_rows(hl.len(mt.alleles) >= 3)
mt.show()

```

Will print the top-left corner of the filtered matrix of genotypes to your Notebook or terminal.

If you want to export those genotypes to a sharded VCF you can replace `mt.show()` with:

```python
hl.export_vcf(mt, 'gs://path/to/file.vcf.bgz', parallel=True)

```

If you want to collect those genotypes as an array of arrays of genotypes, replace `mt.show()` with:

```python
mt = mt.annotate_rows(genotypes = hl.agg.collect(mt.GT))
array_of_arrays_of_gts = mt.genotypes.collect()

```

---

<div class="post-metadata">

**Author:** ![matin](https://avatars.discourse-cdn.com/v4/letter/m/db5fbb/32.png) [@matin](https://discuss.hail.is/u/matin)\
**Post date:** [November 29, 2022, 5:58am UTC](https://discuss.hail.is/t/multiallelic-in-multisample-matrixtable/2955/5 "2022-11-29T05:58:53Z")

</div>

To elaborate on my previous comment: say we have three samples in our MatrixTable. It also has a row at locus 5:5000 for which we have an `alleles` field that contains 8 values ( e.g, `["CTGTGTGTGTGTG","<*>", "C", "CTGTG", "CTGTGTG", "CTGTGTGTG", "CTGTGTGTGTGTGT...` ). We have called variants for each sample at this locus. Assume that the GT values are 1/2, 0/1, and 2/3, respectively for each sample. Now I want to query the ref/alt values for say, the first sample. my ultimate goal is to map alt/ref’s at this row to each sample.

---

<div class="post-metadata">

**Author:** ![danking](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.hail.is/danking/32/43_2.png) [@danking](https://discuss.hail.is/u/danking)\
**Post date:** [November 29, 2022, 3:30pm UTC](https://discuss.hail.is/t/multiallelic-in-multisample-matrixtable/2955/6 "2022-11-29T15:30:42Z")

</div>

Are you asking how to index into the alleles array? Meaning if you have these GTs with your shared alleles field:

```python
1/2 0/1 2/3

```

You want these values:

```python
("<*>", "C") ("CTGTGTGTGTGTG", "<*>") ("C", "CTGTG")

```

?

You can index both the alleles array and the call like normal Python arrays:

```python
mt = mt.annotate_entries(
    first_allele_sequence = mt.alleles[mt.GT[0]],
    second_allele_sequence = mt.alleles[mt.GT[1]]
)

```
