| 1 |
.. _closest: |
| 2 |
|
| 3 |
############### |
| 4 |
*closest* |
| 5 |
############### |
| 6 |
|
| 7 |
Similar to :doc:`../tools/intersect`, `closest` searches for overlapping features in A and B. In the event that |
| 8 |
no feature in B overlaps the current feature in A, `closest` will report the nearest (that is, least |
| 9 |
genomic distance from the start or end of A) feature in B. For example, one might want to find which |
| 10 |
is the closest gene to a significant GWAS polymorphism. Note that `closest` will report an |
| 11 |
overlapping feature as the closest---that is, it does not restrict to closest *non-overlapping* feature. The following iconic "cheatsheet" summarizes the functionality available through the various options provided by the `closest` tool. |
| 12 |
|
| 13 |
| |
| 14 |
|
| 15 |
.. image:: ../images/tool-glyphs/closest-glyph.png |
| 16 |
|
| 17 |
| |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
.. note:: |
| 22 |
|
| 23 |
``bedtools closest`` requires that all input files are presorted data by chromosome and |
| 24 |
then by start position (e.g., ``sort -k1,1 -k2,2n in.bed > in.sorted.bed`` |
| 25 |
for BED files). |
| 26 |
|
| 27 |
.. note:: |
| 28 |
|
| 29 |
Reports "none" for chrom and "-1" for all other fields when a feature |
| 30 |
is not found in B on the same chromosome as the feature in A. |
| 31 |
E.g. `none -1 -1` |
| 32 |
|
| 33 |
.. important:: |
| 34 |
|
| 35 |
As of version 2.22.0, the `closest` tool can accept multiple files for |
| 36 |
the `-b` option. This allows one to identify the closest intervals between a single |
| 37 |
query (`-a`) file and multiple database files (`-b`) at once! This functionality |
| 38 |
now requires that all input files be sorted by chromosome and start coordinate |
| 39 |
in an identical manner (e.g., `sort -k1,1 -k2,2n`). |
| 40 |
|
| 41 |
|
| 42 |
=============================== |
| 43 |
Usage and option summary |
| 44 |
=============================== |
| 45 |
**Usage**: |
| 46 |
:: |
| 47 |
|
| 48 |
bedtools closest [OPTIONS] -a <FILE> \ |
| 49 |
-b <FILE1, FILE2, ..., FILEN> |
| 50 |
|
| 51 |
**(or)**: |
| 52 |
:: |
| 53 |
|
| 54 |
closestBed [OPTIONS] -a <FILE> \ |
| 55 |
-b <FILE1, FILE2, ..., FILEN> |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
=========================== =============================================================================================================================================================================================================== |
| 60 |
Option Description |
| 61 |
=========================== =============================================================================================================================================================================================================== |
| 62 |
**-s** Require same strandedness. That is, find the closest feature in B that overlaps A on the _same_ strand. By default, overlaps are reported without respect to strand. |
| 63 |
|
| 64 |
**-S** Require opposite strandedness. That is, find the closest feature in B that overlaps A on the _opposite_ strand. By default, overlaps are reported without respect to strand. |
| 65 |
|
| 66 |
**-d** In addition to the closest feature in B, report its distance to A as an extra column. The reported distance for overlapping features will be 0. |
| 67 |
|
| 68 |
**-D** | Like `-d`, report the closest feature in B, and its distance to A as an extra column. However unlike `-d`, use negative distances to report upstream features. |
| 69 |
| The options for defining which orientation is "upstream" are: |
| 70 |
| - `ref` Report distance with respect to the reference genome. |
| 71 |
| B features with a lower (start, stop) are upstream |
| 72 |
| - `a` Report distance with respect to A. |
| 73 |
| When A is on the - strand, "upstream" means B has a |
| 74 |
| higher (start,stop). |
| 75 |
| - `b` Report distance with respect to B. |
| 76 |
| When B is on the - strand, "upstream" means A has a |
| 77 |
| higher (start,stop). |
| 78 |
**-io** Ignore features in B that overlap A. That is, we want close, yet not touching features only. |
| 79 |
|
| 80 |
**-iu** Ignore features in B that are upstream of features in A. This option requires -D and follows its orientation rules for determining what is "upstream". |
| 81 |
|
| 82 |
**-id** Ignore features in B that are downstream of features in A. This option requires -D and follows its orientation rules for determining what is "downstream". |
| 83 |
|
| 84 |
**-fu** Choose first from features in B that are upstream of features in A. |
| 85 |
This option requires -D and follows its orientation rules for determining what is "upstream". |
| 86 |
|
| 87 |
**-fd** Choose first from features in B that are downstream of features in A. |
| 88 |
This option requires -D and follows its orientation rules for determining what is "downstream". |
| 89 |
|
| 90 |
**-t** | Specify how ties for closest feature should be handled. This occurs when two features in B have exactly the same "closeness" with A. By default, all such features in B are reported. |
| 91 |
| Here are all the options: |
| 92 |
| - `all` Report all ties (default). |
| 93 |
| - `first` Report the first tie that occurred in the B file. |
| 94 |
| - `last` Report the last tie that occurred in the B file. |
| 95 |
|
| 96 |
**-mdb** | Specify how multiple databases should be resolved. |
| 97 |
| - `each` Report closest records for each database (default). |
| 98 |
| - `all` Report closest records among all databases. |
| 99 |
|
| 100 |
**-k** Report the k closest hits. Default is 1. If tieMode = "all", all ties will still be reported. |
| 101 |
|
| 102 |
**-names** When using *multiple databases* (`-b`), provide an alias for each that will appear instead of a fileId when also printing the DB record. |
| 103 |
|
| 104 |
**-filenames** When using *multiple databases* (`-b`), show each complete filename instead of a fileId when also printing the DB record. |
| 105 |
|
| 106 |
**-N** Require that the query and the closest hit have different names. For BED, the 4th column is compared. |
| 107 |
|
| 108 |
**-header** Print the header from the A file prior to results. |
| 109 |
=========================== =============================================================================================================================================================================================================== |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
========================================================================== |
| 115 |
Default behavior |
| 116 |
========================================================================== |
| 117 |
The `closest` tool first searches for features in B that overlap a feature in A. If overlaps are found, each feature in B that overlaps A is reported. If no overlaps are found, `closestBed` looks for |
| 118 |
the feature in B that is *closest* (that is, least genomic distance to the start or end of A) to A. For example, |
| 119 |
|
| 120 |
For example, consider the case where one of the intervals in B overlaps the interval in B, yet another does not: |
| 121 |
|
| 122 |
.. code-block:: bash |
| 123 |
|
| 124 |
$ cat a.bed |
| 125 |
chr1 10 20 a1 1 - |
| 126 |
|
| 127 |
$ cat b.bed |
| 128 |
chr1 7 8 b1 1 - |
| 129 |
chr1 15 25 b2 2 + |
| 130 |
|
| 131 |
$ bedtools closest -a a.bed -b b.bed |
| 132 |
chr1 10 20 a1 1 - chr1 15 25 b2 2 + |
| 133 |
|
| 134 |
|
| 135 |
Now compare what happens when neither interval in B overlaps the record in A, yet one is closer than the other. |
| 136 |
|
| 137 |
.. code-block:: bash |
| 138 |
|
| 139 |
$ cat a.bed |
| 140 |
chr1 10 20 a1 1 - |
| 141 |
|
| 142 |
$ cat b.bed |
| 143 |
chr1 7 8 b1 1 - |
| 144 |
chr1 30 40 b2 2 + |
| 145 |
|
| 146 |
$ bedtools closest -a a.bed -b b.bed |
| 147 |
chr1 10 20 a1 1 - chr1 7 8 b1 1 |
| 148 |
|
| 149 |
But what if each interval in B is equally close to the interval in A? In this case, the default behavior is to report all intervals in B that are tied for proximity. Check out the `-t` option to adjust this behaviour. |
| 150 |
|
| 151 |
.. code-block:: bash |
| 152 |
|
| 153 |
$ cat a.bed |
| 154 |
chr1 10 20 a1 1 - |
| 155 |
|
| 156 |
$ cat b.bed |
| 157 |
chr1 7 8 b1 1 - |
| 158 |
chr1 22 23 b2 2 + |
| 159 |
|
| 160 |
$ bedtools closest -a a.bed -b b.bed |
| 161 |
chr1 10 20 a1 1 - chr1 7 8 b1 1 - |
| 162 |
chr1 10 20 a1 1 - chr1 22 23 b2 2 + |
| 163 |
|
| 164 |
========================================================================== |
| 165 |
Using multiple `-b` files. |
| 166 |
========================================================================== |
| 167 |
As of version, 2.22.0, the `closest` tool allows one to find the closest |
| 168 |
intervals in multiple `-b` files. Consider the following examples. |
| 169 |
|
| 170 |
.. note:: |
| 171 |
|
| 172 |
When using multiple `-b` files, an additional column describing the file number from which the closest B interval came will be added between the columns representing the full A interval and the columns representing the full A interval. This file number will refer to the order in which the files were provided on the command line. |
| 173 |
|
| 174 |
|
| 175 |
.. code-block:: bash |
| 176 |
|
| 177 |
$ cat a.bed |
| 178 |
chr1 10 20 a1 1 - |
| 179 |
|
| 180 |
$ cat b1.bed |
| 181 |
chr1 5 6 b1.1 1 - |
| 182 |
chr1 30 40 b1.2 2 + |
| 183 |
|
| 184 |
$ cat b2.bed |
| 185 |
chr1 0 1 b2.1 1 - |
| 186 |
chr1 21 22 b2.2 2 + |
| 187 |
|
| 188 |
# In this example, the 7th column reflects the file number from |
| 189 |
# which the closest interval came. |
| 190 |
|
| 191 |
$ bedtools closest -a a.bed -b b1.bed b2.bed |
| 192 |
chr1 10 20 a1 1 - 1 chr1 5 6 b1.1 1 - |
| 193 |
chr1 10 20 a1 1 - 2 chr1 21 22 b2.2 2 + |
| 194 |
|
| 195 |
Instead of using file numbers, you can also provide more informative labels via the `-names` option. |
| 196 |
|
| 197 |
.. code-block:: bash |
| 198 |
|
| 199 |
$ bedtools closest -a a.bed -b b1.bed b2.bed -names b1 b2 |
| 200 |
chr1 10 20 a1 1 - b1 chr1 5 6 b1.1 1 - |
| 201 |
chr1 10 20 a1 1 - b2 chr1 21 22 b2.2 2 + |
| 202 |
|
| 203 |
Or, you can use the full original filename via the `-filenames` option. |
| 204 |
|
| 205 |
.. code-block:: bash |
| 206 |
|
| 207 |
$ bedtools closest -a a.bed -b b1.bed b2.bed -filenames |
| 208 |
chr1 10 20 a1 1 - b1.bed chr1 5 6 b1.1 1 - |
| 209 |
chr1 10 20 a1 1 - b2.bed chr1 21 22 b2.2 2 + |
| 210 |
|
| 211 |
|
| 212 |
========================================================================================= |
| 213 |
``-mdb`` Find the closest interval in **each* or among **all** `-b` files. |
| 214 |
========================================================================================= |
| 215 |
By default, the closest interval from **each** file is reported when using multiple `-b` files. |
| 216 |
|
| 217 |
.. code-block:: bash |
| 218 |
|
| 219 |
$ cat a.bed |
| 220 |
chr1 10 20 a1 1 - |
| 221 |
|
| 222 |
$ cat b1.bed |
| 223 |
chr1 5 6 b1.1 1 - |
| 224 |
chr1 30 40 b1.2 2 + |
| 225 |
|
| 226 |
$ cat b2.bed |
| 227 |
chr1 0 1 b2.1 1 - |
| 228 |
chr1 21 22 b2.2 2 + |
| 229 |
|
| 230 |
$ bedtools closest -a a.bed -b b1.bed b2.bed -d |
| 231 |
chr1 10 20 a1 1 - 1 chr1 5 6 b1.1 1 - 5 |
| 232 |
chr1 10 20 a1 1 - 2 chr1 21 22 b2.2 2 + 2 |
| 233 |
|
| 234 |
$ bedtools closest -a a.bed -b b1.bed b2.bed -mdb each -d |
| 235 |
chr1 10 20 a1 1 - 1 chr1 5 6 b1.1 1 - 5 |
| 236 |
chr1 10 20 a1 1 - 2 chr1 21 22 b2.2 2 + 2 |
| 237 |
|
| 238 |
However, one can optionally choose to report only the closest interval(s) observed among **all** of the `-b` files. In this example, the second interval from b2.bed is only 2 base pairs away from the interval in A, whereas the first interval in b1.bed is 5 base pairs away. Therefore, when using `mdb all`, the the second interval from b2.bed wins. |
| 239 |
|
| 240 |
.. code-block:: bash |
| 241 |
|
| 242 |
$ bedtools closest -a a.bed -b b1.bed b2.bed -mdb all -d |
| 243 |
chr1 10 20 a1 1 - 2 chr1 21 22 b2.2 2 + 2 |
| 244 |
|
| 245 |
========================================================================== |
| 246 |
``-io`` Ignoring overlapping intervals |
| 247 |
========================================================================== |
| 248 |
This option prevents intervals in B that overlap the interval in A from being reported as "closest". |
| 249 |
|
| 250 |
Without `-ip` the second record in B will be reported as closest. |
| 251 |
|
| 252 |
.. code-block:: bash |
| 253 |
|
| 254 |
$ cat a.bed |
| 255 |
chr1 10 20 a1 1 - |
| 256 |
|
| 257 |
$ cat b.bed |
| 258 |
chr1 7 8 b1 1 - |
| 259 |
chr1 15 25 b2 2 + |
| 260 |
|
| 261 |
$ bedtools closest -a a.bed -b b.bed |
| 262 |
chr1 10 20 a1 1 - chr1 15 25 b2 2 + |
| 263 |
|
| 264 |
Yet with `-io`, the overlapping interval is ignored in favor of the closest, non-overlapping interval. |
| 265 |
|
| 266 |
.. code-block:: bash |
| 267 |
|
| 268 |
$ cat a.bed |
| 269 |
chr1 10 20 a1 1 - |
| 270 |
|
| 271 |
$ cat b.bed |
| 272 |
chr1 7 8 b1 1 - |
| 273 |
chr1 15 25 b2 2 + |
| 274 |
|
| 275 |
$ bedtools closest -a a.bed -b b.bed -io |
| 276 |
chr1 10 20 a1 1 - chr1 7 8 b1 1 - |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
========================================================================== |
| 281 |
``-s`` Requiring closest intervals to have the *same* strand |
| 282 |
========================================================================== |
| 283 |
The `-s` option finds the closest interval that is also on the same strand as the interval in A. |
| 284 |
|
| 285 |
.. code-block:: bash |
| 286 |
|
| 287 |
$ cat a.bed |
| 288 |
chr1 10 20 a1 1 - |
| 289 |
|
| 290 |
$ cat b.bed |
| 291 |
chr1 2 3 b1 1 - |
| 292 |
chr1 21 22 b2 2 + |
| 293 |
|
| 294 |
$ bedtools closest -a a.bed -b b.bed -s |
| 295 |
chr1 10 20 a1 1 - chr1 2 3 b1 1 - |
| 296 |
|
| 297 |
|
| 298 |
========================================================================== |
| 299 |
``-S`` Requiring closest intervals to have the *opposite* strand |
| 300 |
========================================================================== |
| 301 |
The `-s` option finds the closest interval that is also on the same strand as the interval in A. |
| 302 |
|
| 303 |
.. code-block:: bash |
| 304 |
|
| 305 |
$ cat a.bed |
| 306 |
chr1 10 20 a1 1 - |
| 307 |
|
| 308 |
$ cat b.bed |
| 309 |
chr1 15 16 b1 1 - |
| 310 |
chr1 21 22 b2 2 + |
| 311 |
|
| 312 |
$ bedtools closest -a a.bed -b b.bed -S |
| 313 |
chr1 10 20 a1 1 - chr1 21 22 b2 2 + |
| 314 |
|
| 315 |
|
| 316 |
========================================================================== |
| 317 |
``-t`` Controlling how ties for "closest" are broken |
| 318 |
========================================================================== |
| 319 |
When there are two or more features in B are tied for proximity to the interval in A, `closest` will, by default, report all such intervals in B. |
| 320 |
As shown in the examples below, this behavior can be changed via the `-t` option: |
| 321 |
|
| 322 |
.. code-block:: bash |
| 323 |
|
| 324 |
$ cat a.bed |
| 325 |
chr1 10 20 a1 1 - |
| 326 |
|
| 327 |
$ cat b.bed |
| 328 |
chr1 30 40 b1 1 - |
| 329 |
chr1 30 40 b2 2 + |
| 330 |
|
| 331 |
# default |
| 332 |
$ bedtools closest -a a.bed -b b.bed |
| 333 |
chr1 10 20 a1 1 - chr1 30 40 b1 1 - |
| 334 |
chr1 10 20 a1 1 - chr1 30 40 b2 2 + |
| 335 |
|
| 336 |
# -t all (default) |
| 337 |
$ bedtools closest -a a.bed -b b.bed -t all |
| 338 |
chr1 10 20 a1 1 - chr1 30 40 b1 1 - |
| 339 |
chr1 10 20 a1 1 - chr1 30 40 b2 2 + |
| 340 |
|
| 341 |
# -t first |
| 342 |
$ bedtools closest -a a.bed -b b.bed -t first |
| 343 |
chr1 10 20 a1 1 - chr1 30 40 b1 1 - |
| 344 |
|
| 345 |
# -t last |
| 346 |
$ bedtools closest -a a.bed -b b.bed -t last |
| 347 |
chr1 10 20 a1 1 - chr1 30 40 b2 1 + |
| 348 |
|
| 349 |
|
| 350 |
========================================================================== |
| 351 |
``-d`` Reporting the distance to the closest feature in base pairs |
| 352 |
========================================================================== |
| 353 |
One often wants to also know the distance in base pairs between the interval in A and the closest interval(s) in B. `closest` will optionally report the distance to the closest feature in the B file using the `-d` option. The distance (in base pairs) will be reported as the last column in the output. |
| 354 |
|
| 355 |
.. note:: |
| 356 |
|
| 357 |
When a feature in B overlaps a feature in A, a distance of 0 is reported. |
| 358 |
|
| 359 |
.. code-block:: bash |
| 360 |
|
| 361 |
$ cat a.bed |
| 362 |
chr1 10 20 a1 1 - |
| 363 |
|
| 364 |
$ cat b.bed |
| 365 |
chr1 7 8 b1 1 - |
| 366 |
chr1 22 23 b2 2 + |
| 367 |
|
| 368 |
$ bedtools closest -a a.bed -b b.bed |
| 369 |
chr1 10 20 a1 1 - chr1 7 8 b1 1 - 3 |
| 370 |
chr1 10 20 a1 1 - chr1 22 23 b2 2 + 3 |
| 371 |
|
| 372 |
========================================================================== |
| 373 |
``-D`` Reporting **signed** distances to the closest feature in base pairs |
| 374 |
========================================================================== |
| 375 |
Whereas the `-d` option always reports distances as positive integers, the |
| 376 |
`-D` option will use negative integers to report distances to "upstream" features. There are three options for dictating how "upstream" should be defined. |
| 377 |
|
| 378 |
1. `-D ref`: Report distance with respect to the reference genome. That is, B features with lower start/stop coordinates are considered to be upstream. |
| 379 |
|
| 380 |
2. `-D a`: Report distance with respect to the orientation of the interval in A. That is, when A is on the - strand, "upstream" means B has higher start/stop coordinates. When A is on the + strand, "upstream" means B has lower start/stop coordinates. |
| 381 |
|
| 382 |
3. `-D b`: Report distance with respect to the orientation of the interval in B. That is, when B is on the - strand, "upstream" means A has higher start/stop coordinates. When B is on the + strand, "upstream" means A has lower start/stop coordinates. |
| 383 |
|
| 384 |
This is best demonstrated through multiple examples. |
| 385 |
|
| 386 |
.. code-block:: bash |
| 387 |
|
| 388 |
$ cat a.bed |
| 389 |
chr1 10 20 a1 1 + |
| 390 |
|
| 391 |
$ cat b.bed |
| 392 |
chr1 7 8 b1 1 + |
| 393 |
chr1 22 23 b2 2 - |
| 394 |
|
| 395 |
$ bedtools closest -a a.bed -b b.bed -D ref |
| 396 |
chr1 10 20 a1 1 + chr1 7 8 b1 1 + -3 |
| 397 |
chr1 10 20 a1 1 + chr1 22 23 b2 2 - 3 |
| 398 |
|
| 399 |
Since the A record is on the "+" strand in this example, `-D ref` and `-D a` have the same effect. |
| 400 |
|
| 401 |
.. code-block:: bash |
| 402 |
|
| 403 |
$ bedtools closest -a a.bed -b b.bed -D a |
| 404 |
chr1 10 20 a1 1 + chr1 7 8 b1 1 + -3 |
| 405 |
chr1 10 20 a1 1 + chr1 22 23 b2 2 - 3 |
| 406 |
|
| 407 |
However, the signs of the distances change if the A interval is on the "-" strand. |
| 408 |
|
| 409 |
.. code-block:: bash |
| 410 |
|
| 411 |
$ cat a.bed |
| 412 |
chr1 10 20 a1 1 - |
| 413 |
|
| 414 |
$ bedtools closest -a a.bed -b b.bed -D a |
| 415 |
chr1 10 20 a1 1 - chr1 7 8 b1 1 + 3 |
| 416 |
chr1 10 20 a1 1 - chr1 22 23 b2 2 - -3 |
| 417 |
|
| 418 |
Let's switch the A interval back to the "+" strand and now report distances with respect to the orientation of the closest B records. |
| 419 |
|
| 420 |
.. code-block:: bash |
| 421 |
|
| 422 |
$ cat a.bed |
| 423 |
chr1 10 20 a1 1 + |
| 424 |
|
| 425 |
$ bedtools closest -a a.bed -b b.bed -D b |
| 426 |
chr1 10 20 a1 1 + chr1 7 8 b1 1 + 3 |
| 427 |
chr1 10 20 a1 1 + chr1 22 23 b2 2 - 3 |
| 428 |
|
| 429 |
Let's flip the stand of the two B records and compare. |
| 430 |
|
| 431 |
.. code-block:: bash |
| 432 |
|
| 433 |
$ cat b.bed |
| 434 |
chr1 7 8 b1 1 - |
| 435 |
chr1 22 23 b2 2 + |
| 436 |
|
| 437 |
$ bedtools closest -a a.bed -b b.bed -D b |
| 438 |
chr1 10 20 a1 1 + chr1 7 8 b1 1 - -3 |
| 439 |
chr1 10 20 a1 1 + chr1 22 23 b2 2 + -3 |
| 440 |
|
| 441 |
|
| 442 |
|