How to Use bedtools intersect

Renesh Bedre    4 minute read

bedtools intersect is a command-line utility for finding the overlapping and non-overlapping intervals between two or multiple BED files.

The general syntax of bedtools intersect looks like this:

# default: report overlap between two BED files
bedtools intersect -a fileA.bed -b fileB.bed 

# default: report overlap between multiple BED files
bedtools intersect -a fileA.bed -b fileB.bed fileC.bed

# loj: report all intervals from -a file with and without overlap
bedtools intersect -a fileA.bed -b fileB.bed -loj

# -wo: report a number of bp of overlap
bedtools intersect -a fileA.bed -b fileB.bed -wo

# report non-overlapping intervals
bedtools intersect -a fileA.bed -b fileB.bed -v

By default, the bedtools intersect report overlapping intervals from BED files if there is at least 1 bp of overlap.

In addition to the above parameters, the bedtools intersect has several other parameters for reporting overlapping and non-overlapping intervals.

Learn how to install bedtools

The following examples demonstrate how to use bedtools intersect for reporting the overlap between the BED files

Example 1: find overlap between BED files

The following example shows how to use bedtools intersect to find the overlap between the two BED files (default behavior).

head fileA.bed
Chr1    3996    4276
Chr1    4486    4600
Chr1    5439    5630

head fileB.bed
Chr1    7942    7987
Chr1    4000    4200
Chr1    8417    8464

# find overlap
bedtools intersect -a fileA.bed -b fileB.bed

# output
Chr1    4000    4200

If you have multiple files to find the overlaps, you can use bedtools intersect as below,

head fileA.bed
Chr1    3996    4276
Chr1    4486    4600
Chr1    5439    5630

head fileB.bed
Chr1    7942    7987
Chr1    4000    4200
Chr1    8417    8464

head fileC.bed
Chr1    3996    4376
Chr1    9486    9600
Chr1    1439    1630

# find overlap
bedtools intersect -a fileA.bed -b fileB.bed fileC.bed 

# output
Chr1    4000    4200
Chr1    3996    4276

The default parameters do not report which interval from the -a file overlaps with the intervals from the -b files. This information can be obtained using -wa and -wb parameters.

# find overlap
bedtools intersect -a fileA.bed -b fileB.bed fileC.bed -wa -wb

# output
Chr1    3996    4276    1       Chr1    4000    4200
Chr1    3996    4276    2       Chr1    3996    4376

The numbers in the fourth column represent the file numbers provided in the -b parameter. For example, number 1 represents fileB.bed and number 2 represents the fileC.bed

Example 2: report all entries with and without overlap (-loj)

bedtools intersect with the -loj parameter can be used to report all intervals from the -a file with and without overlap from intervals from -b files. This is similar to the left join operation.

head fileA.bed
Chr1    3996    4276
Chr1    4486    4600
Chr1    5439    5630

head fileB.bed
Chr1    7942    7987
Chr1    4000    4200
Chr1    8417    8464

# find overlap
bedtools intersect -a fileA.bed -b fileB.bed -loj

# output
Chr1    3996    4276    Chr1    4000    4200
Chr1    4486    4600    .       -1      -1
Chr1    5439    5630    .       -1      -1

The corresponding overlap will be shown by intervals between the two files. If there is no overlap, the corresponding interval for -a entry will be NULL (-1 coordinates)

Example 3: report number of bp of overlap (-wo)

bedtools intersect with -wo parameter can be used to report overlapping intervals between -a and -b files with amount of overlap.

head fileA.bed
Chr1    3996    4276
Chr1    4486    4600
Chr1    5439    5630

head fileB.bed
Chr1    7942    7987
Chr1    4000    4200
Chr1    8417    8464

# find overlap
bedtools intersect -a fileA.bed -b fileB.bed -wo

# output
Chr1    3996    4276    Chr1    4000    4200    200

The last column in the output indicates the number of bp overlaps. In the above example, there is 200 bp of overlap between overlapping intervals from -a and -b BED files.

You can also report overlapping and non-overlapping intervals using -wao as below,

bedtools intersect -a fileA.bed -b fileB.bed -wao

# output
Chr1    3996    4276    Chr1    4000    4200    200
Chr1    4486    4600    .       -1      -1      0
Chr1    5439    5630    .       -1      -1      0

If there is no overlap, the corresponding interval for -a entry will be NULL (-1 coordinates) and the overlap is 0.

Example 4: report non-overlapping intervals (-v)

bedtools intersect with the -v parameter can be used to report non-overlapping intervals between -a and -b files.

head fileA.bed
Chr1    3996    4276
Chr1    4486    4600
Chr1    5439    5630

head fileB.bed
Chr1    7942    7987
Chr1    4000    4200
Chr1    8417    8464

# find overlap
bedtools intersect -a fileA.bed -b fileB.bed -v

# output
Chr1    4486    4600
Chr1    5439    5630

Enhance your skills with courses on genomics and bioinformatics


This work is licensed under a Creative Commons Attribution 4.0 International License

Some of the links on this page may be affiliate links, which means we may get an affiliate commission on a valid purchase. The retailer will pay the commission at no additional cost to you.