# Finding the closest intervals

With the nearest-method, you can search for the feature in other that is nearest
the ones in self.

```{python tidy=FALSE}
import pyranges as pr
gr = pr.data.chipseq()
gr2 = pr.data.chipseq_background()
print(gr.nearest(gr2, suffix="_Input"))
```

The nearest method takes a strandedness option, which can either be
`"same"`, `"opposite"` or `False`/`None`

```{python tidy=FALSE}
print(gr.nearest(gr2, suffix="_Input", strandedness="opposite"))
```

The nearest method also takes two variables, namely how and overlap. How can
take the values `None`, `"upstream"`, `"downstream"`, `"next"` and `"previous"`.
`"upstream"` and `"downstream"` are always in reference to the PyRange the
method is called on. `"next"` (to the right) and `"previous"` (to the left) are
most useful to implement your own custom nearest methods. The default is `None`,
which means that PyRanges looks in both directions. The overlap argument is a
bool which indicates whether you want to include overlaps or not.



```{python tidy=FALSE}
f1 = pr.data.f1()
print(f1)
f2 = pr.data.f2()
print(f2)
print(f2.nearest(f1, strandedness="opposite", how="next"))
print(f2.nearest(f1, how="upstream"))
print(f2.nearest(f1, strandedness="opposite", how="next", overlap=False))
```