Debian Codegraph @

docs/content/tools/intersect.rst

raw
1 .. _intersect:
2
3 #########################################
4 *intersect*
5 #########################################
6
7 |
8
9 .. image:: ../images/tool-glyphs/intersect-glyph.png
10
11 |
12
13 By far, the most common question asked of two sets of genomic features
14 is whether or not any of the features in the two sets "overlap"
15 with one another. This is known as feature intersection.
16 ``bedtools intersect`` allows one to screen for overlaps between
17 two sets of genomic features. Moreover, it allows one to have fine control
18 as to how the intersections are reported. ``bedtools intersect`` works
19 with both BED/GFF/VCF and BAM files as input.
20
21 .. note::
22
23 If you are trying to intersect very large files and are having trouble
24 with excessive memory usage, please presort your data by chromosome and
25 then by start position (e.g., ``sort -k1,1 -k2,2n in.bed > in.sorted.bed``
26 for BED files) and then use the ``-sorted`` option. This invokes a
27 memory-efficient algorithm designed for large files. This algorithm has
28 been *substantially* improved in recent (>=2.18.0) releases.
29
30 .. important::
31
32 As of version 2.21.0, the `intersect` tool can accept multiple files for
33 the `-b` option. This allows one to identify overlaps between a single
34 query (`-a`) file and multiple database files (`-b`) at once!
35
36
37 .. seealso::
38
39 :doc:`../tools/subtract`
40 :doc:`../tools/map`
41 :doc:`../tools/window`
42
43 ===============================
44 Usage and option summary
45 ===============================
46 **Usage**:
47 ::
48
49 bedtools intersect [OPTIONS] -a <FILE> \
50 -b <FILE1, FILE2, ..., FILEN>
51
52 **(or)**:
53 ::
54
55 intersectBed [OPTIONS] -a <FILE> \
56 -b <FILE1, FILE2, ..., FILEN>
57
58
59
60
61 =========================== =========================================================================================================================================================
62 Option Description
63 =========================== =========================================================================================================================================================
64 **-a** BAM/BED/GFF/VCF file "A". Each feature in A is compared to B in search of overlaps. Use "stdin" if passing A with a UNIX pipe.
65 **-b** One or more BAM/BED/GFF/VCF file(s) "B". Use "stdin" if passing B with a UNIX pipe.
66 **NEW!!!**: -b may be followed with multiple databases and/or wildcard (*) character(s).
67 **-abam** BAM file A. Each BAM alignment in A is compared to B in search of overlaps. Use "stdin" if passing A with a UNIX pipe: For example: samtools view -b <BAM> | bedtools intersect -abam stdin -b genes.bed. **Note**: no longer necessary after version 2.19.0
68 **-ubam** Write uncompressed BAM output. The default is write compressed BAM output.
69 **-bed** When using BAM input (-abam), write output as BED. The default is to write output in BAM when using -abam. For example: ``bedtools intersect -abam reads.bam -b genes.bed -bed``
70 **-wa** Write the original entry in A for each overlap.
71 **-wb** Write the original entry in B for each overlap. Useful for knowing what A overlaps. Restricted by -f and -r.
72 **-loj** Perform a "left outer join". That is, for each feature in A report each overlap with B. If no overlaps are found, report a NULL feature for B.
73 **-wo** Write the original A and B entries plus the number of base pairs of overlap between the two features. Only A features with overlap are reported. Restricted by -f and -r.
74 **-wao** Write the original A and B entries plus the number of base pairs of overlap between the two features. However, A features w/o overlap are also reported with a NULL B feature and overlap = 0. Restricted by -f and -r.
75 **-u** Write original A entry once if any overlaps found in B. In other words, just report the fact at least one overlap was found in B. Restricted by -f and -r.
76 **-c** For each entry in A, report the number of hits in B while restricting to -f. Reports 0 for A entries that have no overlap with B. Restricted -f, -F, -r, and -s.
77 **-C** For each entry in A, separately report the number of overlaps with each B file on a distinct line. Reports 0 for A entries that have no overlap with B. Overlaps restricted by -f, -F, -r, and -s.
78 **-v** Only report those entries in A that have no overlap in B. Restricted by -f and -r.
79 **-f** Minimum overlap required as a fraction of A. Default is 1E-9 (i.e. 1bp).
80 **-F** Minimum overlap required as a fraction of B. Default is 1E-9 (i.e., 1bp).
81 **-r** Require that the fraction of overlap be reciprocal for A and B. In other words, if -f is 0.90 and -r is used, this requires that B overlap at least 90% of A and that A also overlaps at least 90% of B.
82 **-e** Require that the minimum fraction be satisfied for A _OR_ B. In other words, if -e is used with -f 0.90 and -F 0.10 this requires that either 90% of A is covered OR 10% of B is covered. Without -e, both fractions would have to be satisfied.
83 **-s** Force "strandedness". That is, only report hits in B that overlap A on the same strand. By default, overlaps are reported without respect to strand.
84 **-S** Require different strandedness. That is, only report hits in B that overlap A on the _opposite_ strand. By default, overlaps are reported without respect to strand.
85 **-split** Treat "split" BAM (i.e., having an "N" CIGAR operation) or BED12 entries as distinct BED intervals.
86 **-sorted** For very large B files, invoke a "sweeping" algorithm that requires position-sorted (e.g., ``sort -k1,1 -k2,2n`` for BED files) input. When using -sorted, memory usage remains low even for very large files.
87 **-g** Specify a genome file the defines the expected chromosome order in the input files for use with the ``-sorted`` option.
88 **-header** Print the header from the A file prior to results.
89 **-names** When using *multiple databases* (`-b`), provide an alias for each that will appear instead of a fileId when also printing the DB record.
90 **-filenames** When using *multiple databases* (`-b`), show each complete filename instead of a fileId when also printing the DB record.
91 **-sortout** When using *multiple databases* (`-b`), sort the output DB hits for each record.
92 **-nobuf** Disable buffered output. Using this option will cause each line of output to be printed as it is generated, rather than saved in a buffer. This will make printing large output files noticeably slower, but can be useful in conjunction with other software tools and scripts that need to process one line of bedtools output at a time.
93 **-iobuf** Follow with desired integer size of read buffer. Optional suffixes `K/M/G` supported. **Note**: currently has no effect with compressed files.
94 =========================== =========================================================================================================================================================
95
96
97 ===============================
98 Default behavior
99 ===============================
100 By default, if an overlap is found, ``bedtools intersect`` reports the shared interval between the two
101 overlapping features.
102
103 .. code-block:: bash
104
105 $ cat A.bed
106 chr1 10 20
107 chr1 30 40
108
109 $ cat B.bed
110 chr1 15 20
111
112 $ bedtools intersect -a A.bed -b B.bed
113 chr1 15 20
114
115
116 ==========================================================================
117 Intersecting against MULTIPLE -b files.
118 ==========================================================================
119 As of version 2.21.0, the `intersect` tool can detect overlaps between
120 a single `-a` file and multiple `-b` files (instead of just one previously).
121 One simply provides multiple `-b` files on the command line.
122
123 For example, consider the following query (`-a`) file and three distinct (`-b`) files:
124
125 .. code-block:: bash
126
127 $ cat query.bed
128 chr1 1 20
129 chr1 40 45
130 chr1 70 90
131 chr1 105 120
132 chr2 1 20
133 chr2 40 45
134 chr2 70 90
135 chr2 105 120
136 chr3 1 20
137 chr3 40 45
138 chr3 70 90
139 chr3 105 120
140 chr3 150 200
141 chr4 10 20
142
143 $ cat d1.bed
144 chr1 5 25
145 chr1 65 75
146 chr1 95 100
147 chr2 5 25
148 chr2 65 75
149 chr2 95 100
150 chr3 5 25
151 chr3 65 75
152 chr3 95 100
153
154 $ cat d2.bed
155 chr1 40 50
156 chr1 110 125
157 chr2 40 50
158 chr2 110 125
159 chr3 40 50
160 chr3 110 125
161
162 $ cat d3.bed
163 chr1 85 115
164 chr2 85 115
165 chr3 85 115
166
167 We can now compare query.bed to all three database files at once.:
168
169 .. code-block:: bash
170
171 $ bedtools intersect -a query.bed \
172 -b d1.bed d2.bed d3.bed
173 chr1 5 20
174 chr1 40 45
175 chr1 70 75
176 chr1 85 90
177 chr1 110 120
178 chr1 105 115
179 chr2 5 20
180 chr2 40 45
181 chr2 70 75
182 chr2 85 90
183 chr2 110 120
184 chr2 105 115
185 chr3 5 20
186 chr3 40 45
187 chr3 70 75
188 chr3 85 90
189 chr3 110 120
190 chr3 105 115
191
192 Clearly this is not completely informative because we cannot tell from which file each intersection came. However, if we use `-wa` and `-wb`, this becomes abundantly clear. When these options are used, the first column after the complete `-a` record lists the file number from which the overlap came. The number corresponds to the order in which the files were given on the command line.
193
194 .. code-block:: bash
195
196 $ bedtools intersect -wa -wb \
197 -a query.bed \
198 -b d1.bed d2.bed d3.bed \
199 -sorted
200 chr1 1 20 1 chr1 5 25
201 chr1 40 45 2 chr1 40 50
202 chr1 70 90 1 chr1 65 75
203 chr1 70 90 3 chr1 85 115
204 chr1 105 120 2 chr1 110 125
205 chr1 105 120 3 chr1 85 115
206 chr2 1 20 1 chr2 5 25
207 chr2 40 45 2 chr2 40 50
208 chr2 70 90 1 chr2 65 75
209 chr2 70 90 3 chr2 85 115
210 chr2 105 120 2 chr2 110 125
211 chr2 105 120 3 chr2 85 115
212 chr3 1 20 1 chr3 5 25
213 chr3 40 45 2 chr3 40 50
214 chr3 70 90 1 chr3 65 75
215 chr3 70 90 3 chr3 85 115
216 chr3 105 120 2 chr3 110 125
217 chr3 105 120 3 chr3 85 115
218
219 In many cases, it may be more useful to report an informative "label" for each file instead of a file number. One can do this with the `-names` option.
220
221 .. code-block:: bash
222
223 $ bedtools intersect -wa -wb \
224 -a query.bed \
225 -b d1.bed d2.bed d3.bed \
226 -names d1 d2 d3 \
227 -sorted
228 chr1 1 20 d1 chr1 5 25
229 chr1 40 45 d2 chr1 40 50
230 chr1 70 90 d1 chr1 65 75
231 chr1 70 90 d3 chr1 85 115
232 chr1 105 120 d2 chr1 110 125
233 chr1 105 120 d3 chr1 85 115
234 chr2 1 20 d1 chr2 5 25
235 chr2 40 45 d2 chr2 40 50
236 chr2 70 90 d1 chr2 65 75
237 chr2 70 90 d3 chr2 85 115
238 chr2 105 120 d2 chr2 110 125
239 chr2 105 120 d3 chr2 85 115
240 chr3 1 20 d1 chr3 5 25
241 chr3 40 45 d2 chr3 40 50
242 chr3 70 90 d1 chr3 65 75
243 chr3 70 90 d3 chr3 85 115
244 chr3 105 120 d2 chr3 110 125
245 chr3 105 120 d3 chr3 85 115
246
247 Or perhaps it may be more useful to report the file name. One can do this with the `-filenames` option.
248
249 .. code-block:: bash
250
251 $ bedtools intersect -wa -wb \
252 -a query.bed \
253 -b d1.bed d2.bed d3.bed \
254 -sorted \
255 -filenames
256 chr1 1 20 d1.bed chr1 5 25
257 chr1 40 45 d2.bed chr1 40 50
258 chr1 70 90 d1.bed chr1 65 75
259 chr1 70 90 d3.bed chr1 85 115
260 chr1 105 120 d2.bed chr1 110 125
261 chr1 105 120 d3.bed chr1 85 115
262 chr2 1 20 d1.bed chr2 5 25
263 chr2 40 45 d2.bed chr2 40 50
264 chr2 70 90 d1.bed chr2 65 75
265 chr2 70 90 d3.bed chr2 85 115
266 chr2 105 120 d2.bed chr2 110 125
267 chr2 105 120 d3.bed chr2 85 115
268 chr3 1 20 d1.bed chr3 5 25
269 chr3 40 45 d2.bed chr3 40 50
270 chr3 70 90 d1.bed chr3 65 75
271 chr3 70 90 d3.bed chr3 85 115
272 chr3 105 120 d2.bed chr3 110 125
273 chr3 105 120 d3.bed chr3 85 115
274
275 Other options to `intersect` can be used as well. For example, let's use `-v` to report those intervals in query.bed that do not overlap any of the intervals in the three database files:
276
277 .. code-block:: bash
278
279 $ bedtools intersect -wa -wb \
280 -a query.bed \
281 -b d1.bed d2.bed d3.bed \
282 -sorted \
283 -v
284 chr3 150 200
285 chr4 10 20
286
287 Or, let's report only those intersections where 100% of the query record is overlapped by a database record:
288
289 .. code-block:: bash
290
291 $ bedtools intersect -wa -wb \
292 -a query.bed \
293 -b d1.bed d2.bed d3.bed \
294 -sorted \
295 -names d1 d2 d3
296 -f 1.0
297 chr1 40 45 d2 chr1 40 50
298 chr2 40 45 d2 chr2 40 50
299 chr3 40 45 d2 chr3 40 50
300
301
302 =============================================
303 ``-wa`` Reporting the original A feature
304 =============================================
305 Instead, one can force ``bedtools intersect`` to report the *original* **"A"** feature when an overlap is found. As
306 shown below, the entire "A" feature is reported, not just the portion that overlaps with the "B" feature.
307
308 For example:
309
310 .. code-block:: bash
311
312 $ cat A.bed
313 chr1 10 20
314 chr1 30 40
315
316 $ cat B.bed
317 chr1 15 20
318
319 $ bedtools intersect -a A.bed -b B.bed -wa
320 chr1 10 20
321
322
323 =============================================
324 ``-wb`` Reporting the original B feature
325 =============================================
326 Similarly, one can force ``bedtools intersect`` to report the *original* **"B"** feature when an overlap is found. If
327 just -wb is used, the overlapping portion of A will be reported followed by the *original* **"B"**. If both -wa
328 and -wb are used, the *originals* of both **"A"** and **"B"** will be reported.
329
330 For example (-wb alone):
331
332 .. code-block:: bash
333
334 $ cat A.bed
335 chr1 10 20
336 chr1 30 40
337
338 $ cat B.bed
339 chr1 15 20
340
341 $ bedtools intersect -a A.bed -b B.bed -wb
342 chr1 15 20 chr1 15 20
343
344
345 Now -wa and -wb:
346
347 .. code-block:: bash
348
349 $ cat A.bed
350 chr1 10 20
351 chr1 30 40
352
353 $ cat B.bed
354 chr1 15 20
355
356 $ bedtools intersect -a A.bed -b B.bed -wa -wb
357 chr1 10 20 chr1 15 20
358
359 ========================================================================
360 ``-loj`` Left outer join. Report features in A with and without overlaps
361 ========================================================================
362 By default, ``bedtools intersect`` will only report features in A that
363 have an overlap in B. The ``-loj`` option will report every A feature
364 no matter what. When there is an overlap (or more than 1), it will report
365 A with its overlaps. Yet when there are no overlaps, an A feature will be
366 reported with a NULL B feature to indicate that there were no overlaps
367
368 For example (*without* ``-loj``):
369
370 .. code-block:: bash
371
372 $ cat A.bed
373 chr1 10 20
374 chr1 30 40
375
376 $ cat B.bed
377 chr1 15 20
378
379 $ bedtools intersect -a A.bed -b B.bed
380 chr1 10 20 chr1 15 20
381
382 Now *with* ``-loj``:
383
384 .. code-block:: bash
385
386 $ cat A.bed
387 chr1 10 20
388 chr1 30 40
389
390 $ cat B.bed
391 chr1 15 20
392
393 $ bedtools intersect -a A.bed -b B.bed -loj
394 chr1 10 20 chr1 15 20
395 chr1 30 40 . -1 -1
396
397
398 =======================================================================
399 ``-wo`` Write the *amount* of overlap between intersecting features
400 =======================================================================
401 The ``-wo`` option reports a column after each combination of intersecting
402 "A" and "B" features indicating the *amount* of overlap in bases pairs that
403 is observed between the two features.
404
405 .. note::
406
407 When an interval in A does not intersect an interval in B, it will not be
408 reported. If you would like to report such intervals with an overlap equal
409 to 0, see the ``-wao`` option.
410
411 .. code-block:: bash
412
413 $ cat A.bed
414 chr1 10 20
415 chr1 30 40
416
417 $ cat B.bed
418 chr1 15 20
419 chr1 18 25
420
421 $ bedtools intersect -a A.bed -b B.bed -wo
422 chr1 10 20 chr1 15 20 5
423 chr1 10 20 chr1 18 25 2
424
425
426 =======================================================================
427 ``-wao`` Write *amounts* of overlap for all features.
428 =======================================================================
429 The ``-wao`` option extends upon the ``-wo`` option in that, unlike ``-wo``,
430 it reports an overlap of 0 for features in A that do not have an intersection
431 in B.
432
433 .. code-block:: bash
434
435 $ cat A.bed
436 chr1 10 20
437 chr1 30 40
438
439 $ cat B.bed
440 chr1 15 20
441 chr1 18 25
442
443 $ bedtools intersect -a A.bed -b B.bed -wao
444 chr1 10 20 chr1 15 20 5
445 chr1 10 20 chr1 18 25 2
446 chr1 30 40 . -1 -1 0
447
448 ==========================================================================
449 ``-u`` (unique) Reporting the mere presence of *any* overlapping features
450 ==========================================================================
451 Often you'd like to simply know a feature in "A" overlaps one or more
452 features in B without reporting each and every intersection. The ``-u``
453 option will do exactly this: if an one or more overlaps exists, the
454 A feature is reported. Otherwise, nothing is reported.
455
456 For example, without ``-u``:
457
458 .. code-block:: bash
459
460 $ cat A.bed
461 chr1 10 20
462
463 $ cat B.bed
464 chr1 15 20
465 chr1 17 22
466
467 $ bedtools intersect -a A.bed -b B.bed
468 chr1 15 20
469 chr1 17 20
470
471 Now with ``-u``:
472
473 .. code-block:: bash
474
475 $ cat A.bed
476 chr1 10 20
477
478 $ cat B.bed
479 chr1 15 20
480 chr1 17 22
481
482 $ bedtools intersect -a A.bed -b B.bed -u
483 chr1 10 20
484
485
486 =======================================================================
487 ``-c`` Reporting the number of overlapping features
488 =======================================================================
489 The -c option reports a column after each "A" feature indicating the *number* (0 or more) of overlapping
490 features found in "B". Therefore, *each feature in A is reported once*.
491
492 .. code-block:: bash
493
494 $ cat A.bed
495 chr1 10 20
496 chr1 30 40
497
498 $ cat B.bed
499 chr1 15 20
500 chr1 18 25
501
502 $ bedtools intersect -a A.bed -b B.bed -c
503 chr1 10 20 2
504 chr1 30 40 0
505
506 The `-c` option can also work with multiple -B files. In this case, the reported count will reflect the total number of intersections observed across _all_ `-B` files. It will not report a separate count for each database file. This can be achieved with the `-C` option.
507
508
509 .. code-block:: bash
510
511 $ cat A.bed
512 chr1 10 20
513 chr1 30 40
514
515 $ cat B.bed
516 chr1 15 20
517 chr1 18 25
518
519 $ cat C.bed
520 chr1 16 21
521 chr1 19 26
522
523 $ bedtools intersect -a A.bed -b B.bed -c
524 chr1 10 20 4
525 chr1 30 40 0
526
527 ==========================================================================
528 ``-C`` Reporting the number of overlapping features for each database file
529 ==========================================================================
530 Unlike the the `-c` option, in the case of multiple `-B` files, the `-C` option will report a separate count for each database file.
531
532 .. code-block:: bash
533
534 $ cat A.bed
535 chr1 10 20
536 chr1 30 40
537
538 $ cat B.bed
539 chr1 15 20
540 chr1 18 25
541
542 $ cat C.bed
543 chr1 16 21
544 chr1 19 26
545
546 $ bedtools intersect -a A.bed -b B.bed -C
547 chr1 10 20 1 2
548 chr1 10 20 2 2
549 chr1 30 40 1 0
550 chr1 30 40 1 0
551
552 If you would like to see more useful information than the file _number_ from which the counts came, one can use the `-filenames` or `-names` options.
553
554 .. code-block:: bash
555
556 $ bedtools intersect -a A.bed -b B.bed -C -filenames
557 chr1 10 20 A.bed 2
558 chr1 10 20 B.bed 2
559 chr1 30 40 A.bed 0
560 chr1 30 40 B.bed 0
561
562 $ bedtools intersect -a A.bed -b B.bed -C -names a b
563 chr1 10 20 a 2
564 chr1 10 20 b 2
565 chr1 30 40 a 0
566 chr1 30 40 b 0
567
568
569 =======================================================================
570 ``-v`` Reporting the absence of any overlapping features
571 =======================================================================
572 There will likely be cases where you'd like to know which "A" features
573 do not overlap with any of the "B" features. Perhaps you'd like to know
574 which SNPs don't overlap with any gene annotations. The ``-v``
575 (an homage to "grep -v") option will only report those "A" features
576 that have no overlaps in "B".
577
578 .. code-block:: bash
579
580 $ cat A.bed
581 chr1 10 20
582 chr1 30 40
583
584 $ cat B.bed
585 chr1 15 20
586
587 $ bedtools intersect -a A.bed -b B.bed -v
588 chr1 30 40
589
590
591
592 =======================================================================
593 ``-f`` Requiring a minimal overlap fraction
594 =======================================================================
595 By default, ``bedtools intersect`` will report an overlap between A and B so long as there is at least one base
596 pair is overlapping. Yet sometimes you may want to restrict reported overlaps between A and B to cases
597 where the feature in B overlaps at least X% (e.g. 50%) of the A feature. The -f option does exactly
598 this.
599
600 For example (note that the second B entry is not reported):
601
602 .. code-block:: bash
603
604 $ cat A.bed
605 chr1 100 200
606
607 $ cat B.bed
608 chr1 130 201
609 chr1 180 220
610
611 $ bedtools intersect -a A.bed -b B.bed -f 0.50 -wa -wb
612 chr1 100 200 chr1 130 201
613
614 ==========================================================================
615 ``-r, and -f`` Requiring reciprocal minimal overlap fraction
616 ==========================================================================
617 Similarly, you may want to require that a minimal fraction of both the A and the B features is
618 overlapped. For example, if feature A is 1kb and feature B is 1Mb, you might not want to report the
619 overlap as feature A can overlap at most 1% of feature B. If one set -f to say, 0.02, and one also
620 enable the -r (reciprocal overlap fraction required), this overlap would not be reported.
621
622 For example (note that the second B entry is not reported):
623
624 .. code-block:: bash
625
626 $ cat A.bed
627 chr1 100 200
628
629 $ cat B.bed
630 chr1 130 201
631 chr1 130 200000
632
633 $ bedtools intersect -a A.bed -b B.bed -f 0.50 -r -wa -wb
634 chr1 100 200 chr1 130 201
635
636 ==========================================================================
637 ``-s`` Enforcing *same* strandedness
638 ==========================================================================
639 By default, ``bedtools intersect`` will report overlaps between features
640 even if the features are on opposite strands. However, if strand information
641 is present in both BED files and the "-s" option is used, overlaps will only
642 be reported when features are on the same strand.
643
644 For example (note that the first B entry is not reported):
645
646 .. code-block:: bash
647
648 $ cat A.bed
649 chr1 100 200 a1 100 +
650
651 $ cat B.bed
652 chr1 130 201 b1 100 -
653 chr1 132 203 b2 100 +
654
655 $ bedtools intersect -a A.bed -b B.bed -wa -wb -s
656 chr1 100 200 a1 100 + chr1 132 203 b2 100 +
657
658
659 ==========================================================================
660 ``-S`` Enforcing *opposite* "strandedness"
661 ==========================================================================
662 The ``-s`` option enforces that overlaps be on the *same* strand. In some
663 cases, you may want to enforce that overlaps be found on *opposite* strands.
664 In this, case use the ``-S`` option.
665
666 For example:
667
668 .. code-block:: bash
669
670 $ cat A.bed
671 chr1 100 200 a1 100 +
672
673 $ cat B.bed
674 chr1 130 201 b1 100 -
675 chr1 132 203 b2 100 +
676
677 $ bedtools intersect -a A.bed -b B.bed -wa -wb -S
678 chr1 100 200 a1 100 + chr1 130 201 b1 100 -
679
680
681 ==========================================================================
682 ``-abam`` Default behavior when using BAM input (deprecated since 2.18.0)
683 ==========================================================================
684 When comparing alignments in BAM format (**-abam**) to features in BED format (**-b**), ``bedtools intersect``
685 will, **by default**, write the output in BAM format. That is, each alignment in the BAM file that meets
686 the user's criteria will be written (to standard output) in BAM format. This serves as a mechanism to
687 create subsets of BAM alignments are of biological interest, etc. Note that only the mate in the BAM
688 alignment is compared to the BED file. Thus, if only one end of a paired-end sequence overlaps with a
689 feature in B, then that end will be written to the BAM output. By contrast, the other mate for the
690 pair will not be written. One should use **pairToBed(Section 5.2)** if one wants each BAM alignment
691 for a pair to be written to BAM output.
692
693 .. code-block:: bash
694
695 $ bedtools intersect -abam reads.unsorted.bam -b simreps.bed | \
696 samtools view - | \
697 head -3
698
699 BERTHA_0001:3:1:15:1362#0 99 chr4 9236904 0 50M = 9242033 5 1 7 9
700 AGACGTTAACTTTACACACCTCTGCCAAGGTCCTCATCCTTGTATTGAAG W c T U ] b \ g c e g X g f c b f c c b d d g g V Y P W W _
701 \c`dcdabdfW^a^gggfgd XT:A:R NM:i:0 SM:i:0 AM:i:0 X0:i:19 X1:i:2 XM:i:0 XO:i:0 XG:i:0 MD:Z:50
702 BERTHA _0001:3:1:16:994#0 83 chr6 114221672 37 25S6M1I11M7S =
703 114216196 -5493 G A A A G G C C A G A G T A T A G A A T A A A C A C A A C A A T G T C C A A G G T A C A C T G T T A
704 gffeaaddddggggggedgcgeggdegggggffcgggggggegdfggfgf XT:A:M NM:i:3 SM:i:37 AM:i:37 XM:i:2 X O : i :
705 1 XG:i:1 MD:Z:6A6T3
706 BERTHA _0001:3:1:16:594#0 147 chr8 43835330 0 50M =
707 43830893 -4487 CTTTGGGAGGGCTTTGTAGCCTATCTGGAAAAAGGAAATATCTTCCCATG U
708 \e^bgeTdg_Kgcg`ggeggg_gggggggggddgdggVg\gWdfgfgff XT:A:R NM:i:2 SM:i:0 AM:i:0 X0:i:10 X1:i:7 X M : i :
709 2 XO:i:0 XG:i:0 MD:Z:1A2T45
710
711 .. note::
712
713 As of version 2.18.0, it is no longer necessary to specify a BAM input file via ``-abam``.
714 Bedtools now autodetects this when ``-a`` is used.
715
716
717
718 ==========================================================================
719 ``-ubam`` Default behavior when using BAM input
720 ==========================================================================
721 The ``-ubam`` option writes *uncompressed* BAM output to stdout. This is
722 useful for increasing the speed of pipelines that accept the output of
723 ``bedtools`` intersect as input, since the receiving tool does not need to
724 uncompress the data.
725
726 ==========================================================================
727 ``-bed`` Output BED format when using BAM input
728 ==========================================================================
729 When comparing alignments in BAM format (**-abam**) to features in BED format (**-b**), ``bedtools intersect``
730 will **optionally** write the output in BED format. That is, each alignment in the BAM file is converted
731 to a 6 column BED feature and if overlaps are found (or not) based on the user's criteria, the BAM
732 alignment will be reported in BED format. The BED "name" field is comprised of the RNAME field in
733 the BAM alignment. If mate information is available, the mate (e.g., "/1" or "/2") field will be
734 appended to the name. The "score" field is the mapping quality score from the BAM alignment.
735
736 .. code-block:: bash
737
738 $ bedtools intersect -abam reads.unsorted.bam -b simreps.bed -bed | head -20
739
740 chr4 9236903 9236953 BERTHA_0001:3:1:15:1362#0/1 0 +
741 chr6 114221671 114221721 BERTHA_0001:3:1:16:994#0/1 37 -
742 chr8 43835329 43835379 BERTHA_0001:3:1:16:594#0/2 0 -
743 chr4 49110668 49110718 BERTHA_0001:3:1:31:487#0/1 23 +
744 chr19 27732052 27732102 BERTHA_0001:3:1:32:890#0/2 46 +
745 chr19 27732012 27732062 BERTHA_0001:3:1:45:1135#0/1 37 +
746 chr10 117494252 117494302 BERTHA_0001:3:1:68:627#0/1 37 -
747 chr19 27731966 27732016 BERTHA_0001:3:1:83:931#0/2 9 +
748 chr8 48660075 48660125 BERTHA_0001:3:1:86:608#0/2 37 -
749 chr9 34986400 34986450 BERTHA_0001:3:1:113:183#0/2 37 -
750 chr10 42372771 42372821 BERTHA_0001:3:1:128:1932#0/1 3 -
751 chr19 27731954 27732004 BERTHA_0001:3:1:130:1402#0/2 0 +
752 chr10 42357337 42357387 BERTHA_0001:3:1:137:868#0/2 9 +
753 chr1 159720631 159720681 BERTHA_0001:3:1:147:380#0/2 37 -
754 chrX 58230155 58230205 BERTHA_0001:3:1:151:656#0/2 37 -
755 chr5 142612746 142612796 BERTHA_0001:3:1:152:1893#0/1 37 -
756 chr9 71795659 71795709 BERTHA_0001:3:1:177:387#0/1 37 +
757 chr1 106240854 106240904 BERTHA_0001:3:1:194:928#0/1 37 -
758 chr4 74128456 74128506 BERTHA_0001:3:1:221:724#0/1 37 -
759 chr8 42606164 42606214 BERTHA_0001:3:1:244:962#0/1 37 +
760
761 ==================================================================================
762 ``-split`` Reporting overlaps with spliced alignments or blocked BED features
763 ==================================================================================
764 As described in section 1.3.19, bedtools intersect will, by default, screen for overlaps against the entire span
765 of a spliced/split BAM alignment or blocked BED12 feature. When dealing with RNA-seq reads, for
766 example, one typically wants to only screen for overlaps for the portions of the reads that come from
767 exons (and ignore the interstitial intron sequence). The **-split** command allows for such overlaps to be
768 performed.
769
770 For example, the diagram below illustrates the *default* behavior. The blue dots represent the "split/
771 spliced" portion of the alignment (i.e., CIGAR "N" operation). In this case, the two exon annotations
772 are reported as overlapping with the "split" BAM alignment, but in addition, a third feature that
773 overlaps the "split" portion of the alignment is also reported.
774
775 ::
776
777 Chromosome ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
778
779 Exons --------------- ----------
780
781 BED/BAM A ************.......................................****
782
783 BED File B ^^^^^^^^^^^^^^^ ^^^^^^^^ ^^^^^^^^^^
784
785 Result =============== ======== ==========
786
787
788 In contrast, when using the **-split** option, only the exon overlaps are reported.
789
790 ::
791
792 Chromosome ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
793
794 Exons --------------- ----------
795
796 BED/BAM A ************.......................................****
797
798 BED File B ^^^^^^^^^^^^^^^ ^^^^^^^^ ^^^^^^^^^^
799
800 Result =============== ==========
801
802
803 ==========================================================================
804 ``-sorted`` Invoke a memory-efficient algorithm for very large files.
805 ==========================================================================
806 The default algorithm for detecting overlaps loads the B file into an R-tree
807 structure in memory. While fast, it can consume substantial memory for large
808 files. For these reason, we provide an alternative, memory efficient algorithm
809 that depends upon inout files that have been sorted by chromosome and then by
810 start position. When both input files are position-sorted, the algorithm can
811 "sweep" through the data and detect overlaps on the fly in a manner much
812 like the way database systems join two tables. This option is invoked with the
813 ``-sorted`` option.
814
815 .. note::
816
817 By default, the ``-sorted`` option requires that the records are **GROUPED**
818 by chromosome and that within each chromosome group, the records are sorted by
819 chromosome position. One way to achieve this (for BED files for example) is use
820 the UNIX sort utility to sort both files by chromosome and then by position.
821 That is, ``sort -k1,1 -k2,2n in.bed > in.sorted.bed``. However, since we merely
822 require that the chromsomes are grouped (that is, all records for a given chromosome
823 come in a single block in the file), sorting criteria other than the alphanumeric
824 criteria that is used by the ``sort`` utility are fine. For example, you could use
825 the "version sort" (``-V``) option in newer versions of GNU sort to make the chromosomes
826 come in this (chr1, chr2, chr3) order instead of this (chr1, chr10, chr11) order.
827
828
829 For example:
830
831 .. code-block:: bash
832
833 $ bedtools intersect -a big.sorted.bed -b huge.sorted.bed -sorted
834
835
836 ==========================================================================
837 ``-g`` Define an alternate chromosome sort order via a genome file.
838 ==========================================================================
839 As described above, the ``-sorted`` option expects that the input files are grouped
840 by chromosome. However, there arise cases where ones input
841 files are sorted by a different criteria and it is to computationally onerous
842 to resort the files alphanumerically. For example, the GATK expects that
843 BAM files are sorted in a very specific manner. The ``-g`` option allows
844 one to specify an exact ording that should be expected in the input (e.g.,
845 BAM, BED, etc.) files. All you need to do is re-order you genome file to
846 specify the order. Also, the use of a genome file to specify the expected
847 order allows the ``intersect`` tool to detect when two files are internally
848 grouped but each file actually follows a different order. This will cause
849 incorrect results and the ``-g`` file will alert you to such problems.
850
851 For example, an alphanumerically ordered genome file would look like the
852 following:
853
854 .. code-block:: bash
855
856 $ cat hg19.genome
857 chr1 249250621
858 chr10 135534747
859 chr11 135006516
860 chr12 133851895
861 chr13 115169878
862 chr14 107349540
863 chr15 102531392
864 chr16 90354753
865 chr17 81195210
866 chr18 78077248
867 chr19 59128983
868 chr2 243199373
869 chr20 63025520
870 chr21 48129895
871 chr22 51304566
872 chr3 198022430
873 chr4 191154276
874 chr5 180915260
875 chr6 171115067
876 chr7 159138663
877 chr8 146364022
878 chr9 141213431
879 chrM 16571
880 chrX 155270560
881 chrY 59373566
882
883 However, if your input BAM or BED files are ordered such as ``chr1, chr2, chr3``, etc.,
884 one need to simply reorder the genome file accordingly:
885
886 .. code-block:: bash
887
888 $ sort -k1,1V hg19.genome > hg19.versionsorted.genome
889 $ cat hg19.versionsorted.genome
890 chr1 249250621
891 chr2 243199373
892 chr3 198022430
893 chr4 191154276
894 chr5 180915260
895 chr6 171115067
896 chr7 159138663
897 chr8 146364022
898 chr9 141213431
899 chr10 135534747
900 chr11 135006516
901 chr12 133851895
902 chr13 115169878
903 chr14 107349540
904 chr15 102531392
905 chr16 90354753
906 chr17 81195210
907 chr18 78077248
908 chr19 59128983
909 chr20 63025520
910 chr21 48129895
911 chr22 51304566
912 chrM 16571
913 chrX 155270560
914 chrY 59373566
915
916 At this point, one can now use the ``-sorted`` option along with the genome file
917 in order to properly process the input files that abide by something other than an
918 alphanumeric sorting order.
919
920 .. code-block:: bash
921
922 $ bedtools intersect -a a.versionsorted.bam -b b.versionsorted.bed \
923 -sorted \
924 -g hg19.versionsorted.genome
925
926 Et voila.
927
928
929
930
931 ==========================================================================
932 ``-header`` Print the header for the A file before reporting results.
933 ==========================================================================
934 By default, if your A file has a header, it is ignored when reporting results.
935 This option will instead tell bedtools to first print the header for the
936 A file prior to reporting results.
937