# Efficient way to filter on large number of samples by an entry value

**URL:** <https://discuss.hail.is/t/efficient-way-to-filter-on-large-number-of-samples-by-an-entry-value/2039>\
**Category:** Hail Query & hailctl\
**Created:** [May 9, 2021, 5:44pm UTC](https://discuss.hail.is/t/efficient-way-to-filter-on-large-number-of-samples-by-an-entry-value/2039 "2021-05-09T17:44:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jimmy\_1](https://avatars.discourse-cdn.com/v4/letter/j/d6d6ee/32.png) [@jimmy\_1](https://discuss.hail.is/u/jimmy_1)\
**Post date:** [May 9, 2021, 5:44pm UTC](https://discuss.hail.is/t/efficient-way-to-filter-on-large-number-of-samples-by-an-entry-value/2039/1 "2021-05-09T17:44:15Z")

</div>

If I have a MT indexed by row keys [‘locus’, ‘alleles’] and columns key ‘s’, what is the best way to filter this MT by a certain entry value? For example if i wanted to identify all samples with a certain genotype, denoted by an the field ‘GT’, how could i do this efficiently for either one specific locus or possibly many different loci in the row index?

---

<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:** [May 10, 2021, 12:11pm UTC](https://discuss.hail.is/t/efficient-way-to-filter-on-large-number-of-samples-by-an-entry-value/2039/2 "2021-05-10T12:11:01Z")

</div>

Can you be a little more specific? Perhaps an example and what you’d want as output?

> [@jimmy\_1](#):
>
> identify all samples with a certain genotype

Does this mean identify all samples per row (variant), or overall?

---

<div class="post-metadata">

**Author:** ![jimmy\_1](https://avatars.discourse-cdn.com/v4/letter/j/d6d6ee/32.png) [@jimmy\_1](https://discuss.hail.is/u/jimmy_1)\
**Post date:** [May 10, 2021, 2:34pm UTC](https://discuss.hail.is/t/efficient-way-to-filter-on-large-number-of-samples-by-an-entry-value/2039/3 "2021-05-10T14:34:01Z")

</div>

Yep I mean per row.  
If the table has keys:

`Column key: ['s']`  
`Row key: ['locus', 'alleles']`

And a certain row is indexed :  
`|locus<GRCh38>|array<str>|`  
`|chr7:5991910|[G,A,C]|`

What would be the best way to identify the samples with genotype 0/1 for example in that row, when GT is an entry field.

---

<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:** [May 10, 2021, 2:38pm UTC](https://discuss.hail.is/t/efficient-way-to-filter-on-large-number-of-samples-by-an-entry-value/2039/4 "2021-05-10T14:38:04Z")

</div>

> [@jimmy\_1](#):
>
> What would be the best way to identify the samples with genotype 0/1 for example in that row, when GT is an entry field.

By “identify”, do you mean collect the list of sample IDs per variant that are heterozygous? This would work:

```python

mt = mt.annotate_rows(
    het_sample_ids = hl.agg.filter(mt.GT.is_het(), hl.agg.collect(mt.s))
)

```
