Bedtools Cheatsheet

September 17, 2019 · View on GitHub

General:

ToolsDescription
flankCreate new intervals from the flanks of existing intervals.
slopAdjust the size of intervals.
shiftAdjust the position of intervals.
subtractRemove intervals based on overlaps b/w two files.
complementExtract intervals not represented by an interval file.
closestFind the closest, potentially non-overlapping interval.
intersectFind overlapping intervals in various ways.
windowFind overlapping intervals within a window around an interval.
clusterCluster (but don't merge) overlapping/nearby intervals.
mergeCombine overlapping/nearby intervals into a single interval.
mapApply a function to a column for each overlapping interval.
groupbyGroup by common cols. & summarize oth. cols. (~ SQL "groupBy")

Formatting:

Notes: BED file format, GFF vs BED indexing

ToolsDescription
getfastaUse intervals to extract sequences from a FASTA file.
maskfastaUse intervals to mask sequences from a FASTA file.
sortOrder the intervals in a file.
bed12tobed6Breaks BED12 intervals into discrete BED6 intervals.
bamtofastqConvert BAM records to FASTQ records.
bamtobedConvert BAM alignments to BED (& other) formats.
bedpetobamConvert BEDPE intervals to BAM records.
bedtobamConvert intervals to BAM records.

Statistics:

ToolsDescription
jaccardCalculate the Jaccard statistic b/w two sets of intervals.
randomGenerate random intervals in a genome.
reldistCalculate the distribution of relative distances b/w two files.
shuffleRandomly redistribute intervals in a genome.
makewindowsMakes adjacent or sliding windows across a genome or BED file.
nucProfile the nucleotide content of intervals in a FASTA file.

Coverage:

ToolsDescription
annotateAnnotate coverage of features from multiple files.
coverageCompute the coverage over defined intervals.
genomecovCompute the coverage over an entire genome.
multicovCounts coverage from multiple BAMs at specific intervals.
unionbedgCombines coverage intervals from multiple BEDGRAPH files.

common flags:

  • -s, -S : Require same strandedness or opposite strandedness, respectively.
  • -f, -F : Minimum overlap required as a fraction of A or a fraction of B respectively.
  • -r, -e : Require that the minimum overlap be satisfied for A AND B, or A OR B respectively.
  • -split : Treat "split" BAM or BED12 entries as distinct BED intervals.
  • -abam : A is a BAM file.

General

flank, slop

Create new intervals from the flanks of existing intervals. (flank Docs)

Adjust the size of intervals. (slop Docs)

IN           ▓▓▓▓▓       ▓▓▓
Flank      ██     ██   ██   ██
Slop       █████████   ███████

$ bedtools flank [OPTIONS] -i <BED/GFF/VCF> -g <GENOME> [-b or (-l and -r)]

$ bedtools slop [OPTIONS] -i <BED/GFF/VCF> -g <GENOME> [-b or (-l and -r)]

OPTIONS.
-b, -l, -rFlank/extend regions by x bp on both sides, on the left, or on the right respectively.
-sDefine -l and -r based on strand.
-pctDefine -l and -r as a fraction of the feature's length.

shift

Adjust the position of intervals, while respecting chromosome edges. (Docs).

IN      ██   ██      ████
OUT        ██   ██      ████

$ bedtools shift [OPTIONS] -i <BED/GFF/VCF> -g <GENOME> [-s or (-m and -p)]

OPTIONS.
-sNumber of BPs to shift the features.
-m, -pNumber of BPs to shift the features on the - strand or + strand, respectively.
-pctDefine -s, -m and -p as a fraction of the feature's length.

subtract

Remove intervals based on overlaps b/w two files. (Docs)

A        ▓▓▓▓▓▓▓▓▓▓   ▓▓▓     ▓▓▓▓▓▓
B          ▓▓▓▓           ▓▓▓▓▓▓▓  
A sub B  ██    ████   ███        ███

$ bedtools subtract [OPTIONS] -a <BED/GFF/VCF> -b <BED/GFF/VCF>

OPTIONS.
-ARemove entire feature if any overlap.
commonstrandedness: -s, -S; overlap: -f, -F; overlap mode: -r, -e

complement

Extract intervals not represented by an interval file. (Docs)

IN           ▓▓▓▓▓     ▓▓▓     ▓▓▓▓▓▓
          ▓▓▓▓            ▓▓▓  
OUT  █████        █████      ██

$ bedtools complement -i <BED/GFF/VCF> -g <GENOME>

closest

Find the closest, potentially non-overlapping interval. (Docs)

A            █­███­█   ✓
B   ██­██            ██­█   

$ bedtools closest [OPTIONS] -a <FILE> -b <FILE1, FILE2, ..., FILEN>

OPTIONS.
-dAlso report distance from A to the closest feature.
-kReport the k closest hits. Default: 1.
-ioIgnore features in B that overlap A.
-iu, -idIgnore features in B that are upstream or downstream, respectively, of features in A.
commonstrandedness: -s, -S

intersect

Find overlapping intervals in various ways. (Docs)

A           ██████████
B         ▓▓▓▓    ▓▓        ▓▓▓  
A int B     ▓▓    ▓▓

$ bedtools intersect [OPTIONS] -a <BAM/BED/GFF/VCF> -b <FILE1, FILE2, ..., FILEN>

OPTIONS.
-wa, -wbWrite the original entry in A/original entry in B, respectively, for each overlap.
-lojFor each feature in A report each overlap with B. Report a NULL feature for B if no overlap.
-waoReport A and B features and no. of bp overlap between them.
-uOnly report each overlapping A feature once.
-cFor each entry in A, report count of overlapping B features.
-vOnly report features in A not overlapping B.
commonstrandedness: -s, -S; overlap: -f, -F; overlap mode: -r, -e; bam/bed12: -abam, -split

window

Find overlapping intervals within a window around an interval. (Docs)

A           ┌────█████────┐
B         ▓▓▓▓    ▓▓▓        ▓▓▓  
A win B   ▓▓▓▓    ▓▓▓

$ bedtools window [OPTIONS] [-a|-abam] -b <BED/GFF/VCF>

OPTIONS.
-w, -l, -rFlank length of overlap window in each direction, upstream or downstream, respectively.
-swDefine -l and -r based on strand.
-uOnly report each overlapping A feature once.
-cFor each entry in A, report count of overlapping B features.
-vOnly report features in A not overlapping B.
commonstrandedness: -sm, -Sm; bam: -abam

cluster

Cluster (but don't merge) overlapping/nearby intervals. (Docs)

BED        ██­██     █­███­█  ██­█  
clustID   └─#1─┘   └────#2────┘

$ bedtools cluster [OPTIONS] -i <BED/GFF/VCF>

OPTIONS.
-dMax distance between features in cluster.
commonstrandedness: -s, -S

Aggregation Tools

For merge, groupby, and map the following* aggregation functions (specified by -o) can be applied to a column/columns specified by -c: sum, count, count_distinct, min, max, mean, median, mode, antimode, stdev, sstdev, collapse, distinct, first, last

*Other functions are available.

merge

Combine overlapping/nearby intervals into a single interval. (Docs)

IN       ▓▓▓      ▓        ▓▓··d··▓▓▓
      ▓▓▓▓         ▓▓        
OUT   ██████      ███      ██████████

$ bedtools merge [OPTIONS] -i <BED/GFF/VCF/BAM>

OPTIONS.
-sRequire same strandedness.
-SForce merge for one specific strand only. Options: <+/->.
-dMaximum distance between features to be merged.
commonaggregation: -o, -c;

map

Apply a function to a column for each overlapping interval.(Docs)

        score = 3  1     5                 4      6
B              ▓▓▓ ▓   ▓▓▓▓▓             ▓▓▓▓▓▓ ▓▓▓▓
A               ██████████                 ███████
B map(mean) A   ██████████ mean(3,1,5)=5   ███████ mean(4,6)=5

$ bedtools map [OPTIONS] -a <BED/GFF/VCF> -b <BED/GFF/VCF>

OPTIONS. .
commonaggregation: -o, -c; strandedness: -s, -S; overlap: -f, -F; overlap mode: -r, -e; bed12: -split

groupby

Group by common cols & summarize other cols (~ SQL "groupBy"). (Docs)

$ bedtools groupby [OPTIONS] -i <BED> -g <groupby columns> -c <op. column> -o <operation>

OPTIONS.
commonaggregation: -o, -c

Formatting

BED file format

Columne.g.Defi­nit­ion
chromSc112.1<ST­R> name of chromo­som­e/s­caffold
start2134<IN­T> start position of feature
end2565<IN­T> end position of feature
namegene123<ST­R> name of feature
score544<NU­M> score for the feature e.g. bit score
strand+<+/­-/.> strand on which feature is located
thic­kSt­art2235
thic­kEnd2489
item­Rgb255,0,0
bloc­kCo­unt2<IN­T> number of blocks (exons) in the feature
bloc­kSi­zes150,80<IN­T>,<IN­T>,... list of block sizes
bloc­kSt­arts0,2333<IN­T>,<IN­T>,... list of block start positions relative to start position of feature

GFF vs BED indexing

GFF    ┌─1   2   3─┐ 4   ...
         G---A---T   C   ...
BED    └─0   1   2 └─3   ...
.gff -> bedbed -> gff
new_start =gff_start - 1bed_start + 1
new_end =gff_endbed_end

getfasta

Use intervals to extract sequences from a FASTA file. (Docs)

FASTA  ­ ACT­GAT­CAT­GAT­ACA­TGA­TAC­CAT­TAG­GAT­ACAATA
BED         ██­██       █­███­█      ██­██
OUTFA­ ­ ­     AT­CA       TGA­TA      G­GAT­      

$ bedtools getfasta [OPTIONS] -fi <input FASTA> -bed <BE­D/G­FF/­VCF­>

OPTIONS.
-nameUse “name” column in BED file for FASTA headers in the output.
-sReverse comple­ment features on "-" strand. Default: strand inform­ation ignored.
-splitGiven BED12 input, concat­enate the sequences from BED blocks (e.g., exons).

maskfasta

Use intervals to mask sequences from a FASTA file. (Docs)

FASTA­ ­ ­ ­ACT­GAT­CAT­GAT­ACA­TGA­TAC­CAT­TAG­GAT­ACAATA
BED           ██­██       █­███­█      ██­██
FASTA­'  AC­TGA­TNN­NNA­TAC­ATG­NNN­NNA­TTA­GGN­NNN­AATA

$ bedtools maskfasta [OPTIONS] -fi <input FASTA> -bed <BE­D/G­FF/­VCF> -fo <output FASTA>

OPTIONS.
-softSoft-mask (convert to lower-case bases) instead of masking with "N".
-mcSpecify masking character.

sort

Order the intervals in a file. (Docs)

$ bedtools sort [OPTIONS] -i <BED/GFF/VCF>

OPTIONS.
-sizeASort by feature size (asc).
-sizeDSort by feature size (desc).
-chrThenSizeASort by chromosome (asc), then by feature size (asc).
-chrThenSizeDSort by chromosome (asc), then by feature size (desc).
-chrThenScoreASort by chromosome (asc), then by score (asc).
-chrThenScoreDSort by chromosome (asc), then by score (desc).

Statistics

jaccard

Calculate the Jaccard statistic b/w two sets of intervals. (Docs)

A                 ███████████  15bp
B               ▓▓▓▓ 10bp ▓▓ 4bp       ▓▓▓ 8bp
A int B           ▓▓ 6bp  ▓▓ 4bp
Jaccard(A,B)     (6+4)/((15+10+4+8)-(6+4)) =  0.37     

$ bedtools jaccard [OPTIONS] -a <BED/GFF/VCF> -b <BED/GFF/VCF>

OPTIONS.
commonstrandedness: -s, -S; overlap: -f, -F; overlap mode: -r, -e; bed12: -split

random

Generate random intervals in a genome. (Docs)

$ bedtools random [OPTIONS] -g <GENOME>

OPTIONS.
-lThe length of the intervals to generate. Default: 100
-nThe number of intervals to generate. Default: 1,000,000
-seedSupply an integer seed for the shuffling.

reldist

Calculate the distribution of relative distances b/w two files. (Docs)

                ───────r──────
A            ▓▓▓▓▓▓         ▓▓▓▓
B                      ███
                ───d1─── ──d2──
reldist = min(d1,d2)/r

$ bedtools reldist [OPTIONS] -a <BED/GFF/VCF> -b <BED/GFF/VCF>

OPTIONS.
-detailInstead of a summary, report relative distance for each region in A.

shuffle

Randomly redistribute intervals in a genome. (Docs)

$ bedtools shuffle [OPTIONS] -i <BED/GFF/VCF> -g <GENOME>

OPTIONS.
-exclBED file with regions into which features won't be shuffled.
-inclBED file with regions into which features will be shuffled.
-chromKeep features on the same chromosome.
-chromFirstDistribute features ~uniformly across chroms, not across total sequence.
-noOverlappingDon't allow shuffled intervals to overlap.

makewindows

Makes adjacent or sliding windows across a genome or BED file.

$bedtools makewindows [OPTIONS] [-g <GENOME>|-b <BED>] [-w <window size> | -n <n windows>]

OPTIONS.
-sNumber of bases to step before creating a new window. Default: equal to -w

Coverage

annotate

Annotate coverage of features from multiple files. (Docs)

$ bedtools annotate -i variants.bed -files genes.bed conserve.bed known_var.bed
chr1  100 200 nasty 1 - 0.500000  1.000000  0.300000
chr2  500 1000  ugly  2 + 0.000000  0.600000  1.000000

$ bedtools annotate [OPTIONS] -i <BED/GFF/VCF> -files FILE1 FILE2 FILE3 ... FILEn

OPTIONS.
-countsReport count of features that overlap -i in each file. Default: report fraction of -i covered by each file.
-bothReport counts & fractions for each file.
commonstrandedness: -s, -S.

coverage

Compute the coverage over defined intervals. (Docs)

BED FILE A  ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓     ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓     ▓▓▓▓▓▓  
BED File B  ████ ████              ██             █████████
              ████████                                      
Result      [  N=3, 10/15 ]     [  N=1, 2/15  ]    [N=1,6/6]

$ bedtools coverage [OPTIONS] -a <BAM/BED/GFF/VCF> -b <FILE1, FILE2, ..., FILEN>

OPTIONS.
-dReport the depth at each position in each A feature.
commonstrandedness: -s, -S; overlap: -f, -F; overlap mode: -r, -e; bam/bed12: -split,-abam