# Filter samples from MatrixTable

**URL:** <https://discuss.hail.is/t/filter-samples-from-matrixtable/1645>\
**Category:** Hail Query & hailctl\
**Created:** [September 10, 2020, 8:53pm UTC](https://discuss.hail.is/t/filter-samples-from-matrixtable/1645 "2020-09-10T20:53:56Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![nonchev](https://avatars.discourse-cdn.com/v4/letter/n/54ee81/32.png) [@nonchev](https://discuss.hail.is/u/nonchev)\
**Post date:** [September 10, 2020, 8:53pm UTC](https://discuss.hail.is/t/filter-samples-from-matrixtable/1645/1 "2020-09-10T20:53:57Z")

</div>

Hello,

i want to filter from MatrixTable only individuals(samples) of interest  
I found in previous post:

> mt.filter\_rows(mt.s == ‘NA00001’)

which works for single sample, but how can I filter it if I have a list of samples such as:

> samples = [‘NA00001’, ‘NA00002’]

---

<div class="post-metadata">

**Author:** ![johnc1231](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.hail.is/johnc1231/32/286_2.png) [@johnc1231](https://discuss.hail.is/u/johnc1231)\
**Post date:** [September 10, 2020, 8:59pm UTC](https://discuss.hail.is/t/filter-samples-from-matrixtable/1645/2 "2020-09-10T20:59:23Z")

</div>

`mt.filter_cols(hl.array(samples).contains(mt.s))`

---

<div class="post-metadata">

**Author:** ![Shiri.Margalit](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@Shiri.Margalit](https://discuss.hail.is/u/Shiri.Margalit)\
**Post date:** [April 8, 2021, 12:33pm UTC](https://discuss.hail.is/t/filter-samples-from-matrixtable/1645/3 "2021-04-08T12:33:23Z")

</div>

Hi,  
I’m doing exactly this solution and I’m getting this error:  
TypeError: array: parameter ‘collection’: expected expression of type set or array or dict\<(‘any’, ‘any’)\>, found

This is my code:  
samples = table[‘target panel prefix’]  
mt = mt.filter\_cols(hl.array(samples).contains(mt.s))

What can be the reason?  
Thanks!  
Shiri

---

<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 8, 2021, 1:14pm UTC](https://discuss.hail.is/t/filter-samples-from-matrixtable/1645/4 "2021-04-08T13:14:53Z")

</div>

`samples` here isn’t a python list, it’s a table field. If you do:

```auto
samples = table['target panel prefix'].collect()

```

This should work, I think

---

<div class="post-metadata">

**Author:** ![Shiri.Margalit](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@Shiri.Margalit](https://discuss.hail.is/u/Shiri.Margalit)\
**Post date:** [April 8, 2021, 1:19pm UTC](https://discuss.hail.is/t/filter-samples-from-matrixtable/1645/5 "2021-04-08T13:19:29Z")

</div>

> [@tpoterba](#):
>
> table field

Thank you so much, it worked!

---

<div class="post-metadata">

**Author:** ![shuang](https://avatars.discourse-cdn.com/v4/letter/s/f08c70/32.png) [@shuang](https://discuss.hail.is/u/shuang)\
**Post date:** [October 21, 2021, 1:59pm UTC](https://discuss.hail.is/t/filter-samples-from-matrixtable/1645/6 "2021-10-21T13:59:58Z")

</div>

Hi,  
If I have a txt file which listed all samples ID without header. Looks like:

```auto
11111111
2222222
3333333
AAAAAA

```

Now I want to filter out these samples from my MT file. What should I do?  
with Hail v0.1 I do:

```auto
to_remove = hc.import_table('outliers.txt', no_header=True).key_by('f0')
vds_filtered = vds.filter_samples_table(to_remove, keep=False)

```

I am new to v0.2. According the above answer, I think it might should be:

```auto
to_remove = hl.import_table('outliers.txt', no_header=True).key_by('f0')
samples_to_remove = to_remove.collect()
filtered_mt = mt.filter_cols(hl.array(samples_to_remove).contains(mt.s), keep=False)

```

But I am not sure about it and I do not understand what ‘.contains(mt.s)’ do, any help?  
Thanks a lot!

---

<div class="post-metadata">

**Author:** ![shuang](https://avatars.discourse-cdn.com/v4/letter/s/f08c70/32.png) [@shuang](https://discuss.hail.is/u/shuang)\
**Post date:** [October 22, 2021, 3:19pm UTC](https://discuss.hail.is/t/filter-samples-from-matrixtable/1645/7 "2021-10-22T15:19:41Z")

</div>

I make it work by:

```auto
sample_table = hl.import_table('outlier.txt', no_header=True).key_by('f0')
filt_mt = mt.filter_cols(hl.is_defined(sample_table[mt.col_key]), keep=False)

```

---

<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:** [October 22, 2021, 3:21pm UTC](https://discuss.hail.is/t/filter-samples-from-matrixtable/1645/8 "2021-10-22T15:21:17Z")

</div>

that solution is exactly what we would have suggested!

There’s also a set of `anti_join` methods which translates to “remove keys appearing in this other table” that can make this a little more terse:

```python
sample_table = hl.import_table('outlier.txt', no_header=True).key_by('f0')
filt_mt = mt.anti_join_cols(sample_table)

```

---

<div class="post-metadata">

**Author:** ![shuang](https://avatars.discourse-cdn.com/v4/letter/s/f08c70/32.png) [@shuang](https://discuss.hail.is/u/shuang)\
**Post date:** [October 22, 2021, 3:31pm UTC](https://discuss.hail.is/t/filter-samples-from-matrixtable/1645/9 "2021-10-22T15:31:08Z")

</div>

Thanks Tim and good to know this “.anti\_join\_cols” method!
