# Table filter expression

**URL:** <https://discuss.hail.is/t/table-filter-expression/1243>\
**Category:** Hail Query & hailctl\
**Created:** [January 16, 2020, 5:52am UTC](https://discuss.hail.is/t/table-filter-expression/1243 "2020-01-16T05:52:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mhebrard](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.hail.is/mhebrard/32/322_2.png) [@mhebrard](https://discuss.hail.is/u/mhebrard)\
**Post date:** [January 16, 2020, 5:52am UTC](https://discuss.hail.is/t/table-filter-expression/1243/1 "2020-01-16T05:52:20Z")

</div>

After running `sample_qc()`, I extract the samples in a Hail Table then apply some filters. For the sake of example here some hard-coded filters:

```auto
mt = hl.sample_qc(mt)
ht_qc = mt.cols()
ht_fail = ht_qc.filter(
    (ht_qc.sample_qc.n_snp < 5200000)
    | (ht_qc.sample_qc.n_singleton < 130000)
    | (ht_qc.sample_qc.n_insertion < 550000)
    | (ht_qc.sample_qc.n_deletion < 550000)
)
nfail = ht_fail.count()
ht_fail.s.collect()

```

That expression allows me to display the list of samples id that failed my filters.  
I also wish to create a new table with only the samples that passed my filters…

I sould do

```auto
 ht_pass = ht_qc.filter(
    (ht_qc.sample_qc.n_snp < 5200000)
    | (ht_qc.sample_qc.n_singleton < 130000)
    | (ht_qc.sample_qc.n_insertion < 550000)
    | (ht_qc.sample_qc.n_deletion < 550000)
, keep=false
)

```

but that require to write 2 times the same set of filters, that is error prone if I update ht\_fail but do not synchronize ht\_pass.

Q\> Is there a smart way to declare one set of filters and gather both sets ?

soething like

```auto
myFilters = // complex boolean expression on table fields
ht_fail = ht_qc.filter(myFilters, keep=true)
ht_pass = ht_qc.filter(myFilters, 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:** [January 16, 2020, 1:53pm UTC](https://discuss.hail.is/t/table-filter-expression/1243/2 "2020-01-16T13:53:06Z")

</div>

The [semi\_join](https://hail.is/docs/0.2/hail.Table.html#hail.Table.semi_join) and [anti\_join](https://hail.is/docs/0.2/hail.Table.html#hail.Table.semi_join) methods will be helpful here:

```python
ht_fail = ht_qc.filter(myFilters, keep=true)
ht_pass = ht_qc.anti_join(ht_fail)

```

---

<div class="post-metadata">

**Author:** ![mhebrard](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.hail.is/mhebrard/32/322_2.png) [@mhebrard](https://discuss.hail.is/u/mhebrard)\
**Post date:** [January 17, 2020, 12:20am UTC](https://discuss.hail.is/t/table-filter-expression/1243/3 "2020-01-17T00:20:29Z")

</div>

Oh, good to know, thanks.

I actually change the way I store the filters result to do similar as how VCF filters field is encoded in Hail.  
I generate one field that is a set of string that contains the names of the filters that failed. Then I can test the length of the set …

```auto
ht_pass = ht_qc.filter(hl.len(ht_qc.my_filters) == 0)
ht_fail = ht_qc.filter(hl.len(ht_qc.my_filters) > 0)

```
