# Changes to filter\_variants\_intervals; filter intervals programmatically

**URL:** <https://discuss.hail.is/t/changes-to-filter-variants-intervals-filter-intervals-programmatically/147>\
**Category:** Updates\
**Created:** [March 3, 2017, 10:28pm UTC](https://discuss.hail.is/t/changes-to-filter-variants-intervals-filter-intervals-programmatically/147 "2017-03-03T22:28:06Z")\
**Posts on this page:** 2\
**Page:** 1

<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:** [March 3, 2017, 10:28pm UTC](https://discuss.hail.is/t/changes-to-filter-variants-intervals-filter-intervals-programmatically/147/1 "2017-03-03T22:28:06Z")

</div>

# Breaking change to `filter_variants_intervals`

We’ve pushed some updates to the [filter\_variants\_intervals](https://hail.is/hail/hail.VariantDataset.html#hail.VariantDataset.filter_variants_intervals) method. This involves building out functionality on the [Interval](https://hail.is/hail/representation/hail.representation.Interval.html), and adding an [IntervalTree](https://hail.is/hail/representation/hail.representation.IntervalTree.html) class. The [filter\_variants\_intervals](https://hail.is/hail/hail.VariantDataset.html#hail.VariantDataset.filter_variants_intervals) now takes one of these objects ([Interval](https://hail.is/hail/representation/hail.representation.Interval.html) or [IntervalTree](https://hail.is/hail/representation/hail.representation.IntervalTree.html)) instead of a file path. This is a breaking change, but the old functionality can still be accessed.

Filtering from interval file, old style:

```python
>>> vds = vds.filter_variants_intervals('my_interval_list.txt')

```

Filtering from interval file: as of today:

```python
>>> vds = vds.filter_variants_intervals(IntervalTree.read('my_interval_list.txt'))

```

#New functionality  
The new `filter_variants_intervals` method takes either an [Interval](https://hail.is/hail/representation/hail.representation.Interval.html) or [IntervalTree](https://hail.is/hail/representation/hail.representation.IntervalTree.html) object now, instead of a file path. We’ve also added parse functions on Interval [(parse)](https://hail.is/hail/representation/hail.representation.Interval.html#hail.representation.Interval.parse) and IntervalTree [(parse\_all)](https://hail.is/hail/representation/hail.representation.IntervalTree.html#hail.representation.IntervalTree.parse_all), which let you easily construct interval objects from strings like:

```python
>>> from hail.representation import Interval

# self-explanatory
>>> interval = Interval.parse('1:150-250')

# Chromosome-spanning interval: 1:150 to 2:350 
>>> interval = Interval.parse('1:150-2:350')

# All of chromosome X
>>> interval = Interval.parse('X')

# All of the autosomes (beginning of chr 1, to end chr 22)
>>> interval = Interval.parse('1-22')

# Special keywords: M = 10^6, K = 10^3
>>> interval = Interval.parse('16:29.5M-30.2M')
>>> interval = Interval.parse('15:200k-300k')

# Special keywords: start and end
>>> interval = Interval.parse('1:150M-END')
>>> interval = Interval.parse('19:start-30000000')

# Use an interval to filter a VDS
>>> vds = vds.filter_variants_intervals(interval)

# subset to autosomes
>>> vds = vds.filter_variants_intervals(Interval.parse('1-22'))

# subset to just chromosome 19
>>> vds = vds.filter_variants_intervals(Interval.parse('19'))

```

In case you’re still not sold on this awesome interface, take a look at [this blog post](http://discuss.hail.is/t/predicate-pushdown-why-youre-not-using-filter-variants-intervals-enough/148) to see how interval filtering can make your analysis much, much faster.

---

<div class="post-metadata">

**Author:** ![xiaoli](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.hail.is/xiaoli/32/71_2.png) [@xiaoli](https://discuss.hail.is/u/xiaoli)\
**Post date:** [November 1, 2017, 7:13pm UTC](https://discuss.hail.is/t/changes-to-filter-variants-intervals-filter-intervals-programmatically/147/2 "2017-11-01T19:13:32Z")

</div>

Just a note that the function currently is `filter_intervals`.
