LinuxCommandLibrary

bedtools

bedtools

TLDR

Intersect two files regarding the sequences' strand and save the result to the specified file

$ bedtools intersect -a [path/to/file1] -b [path/to/file2] -s > [path/to/output_file]
copy


Intersect two files with a left outer join, i.e. report each feature from file1 and NULL if no overlap with file2
$ bedtools intersect -a [path/to/file1] -b [path/to/file2] -lof > [path/to/output_file]
copy


Using more efficient algorithm to intersect two pre-sorted files
$ bedtools intersect -a [path/to/file1] -b [path/to/file2] -sorted > [path/to/output_file]
copy


Group file path/to/file based on the first three and the fifth column and summarize the sixth column by summing it up
$ bedtools groupby -i [path/to/file] -c 1-3,5 -g 6 -o sum
copy


Convert bam-formatted file to a bed-formatted one
$ bedtools bamtobed -i [path/to/file.bam] > [path/to/file.bed]
copy


Find for all features in file1.bed the closest one in file2.bed and write their distance in an extra column (input files must be sorted)
$ bedtools closest -a [path/to/file1.bed] -b [path/to/file2.bed] -d
copy

Help

bedtools is a powerful toolset for genome arithmetic. 

Version:   v2.30.0 
About:     developed in the quinlanlab.org and by many contributors worldwide. 
Docs:      http://bedtools.readthedocs.io/ 
Code:      https://github.com/arq5x/bedtools2 
Mail:      https://groups.google.com/forum/#!forum/bedtools-discuss 

Usage:     bedtools  [options] 

The bedtools sub-commands include:

[ Genome arithmetic ] 
    intersect     Find overlapping intervals in various ways. 
    window        Find overlapping intervals within a window around an interval. 
    closest       Find the closest, potentially non-overlapping interval. 
    coverage      Compute the coverage over defined intervals. 
    map           Apply a function to a column for each overlapping interval. 
    genomecov     Compute the coverage over an entire genome. 
    merge         Combine overlapping/nearby intervals into a single interval. 
    cluster       Cluster (but don't merge) overlapping/nearby intervals. 
    complement    Extract intervals _not_ represented by an interval file. 
    shift         Adjust the position of intervals. 
    subtract      Remove intervals based on overlaps b/w two files. 
    slop          Adjust the size of intervals. 
    flank         Create new intervals from the flanks of existing intervals. 
    sort          Order the intervals in a file. 
    random        Generate random intervals in a genome. 
    shuffle       Randomly redistribute intervals in a genome. 
    sample        Sample random records from file using reservoir sampling. 
    spacing       Report the gap lengths between intervals in a file. 
    annotate      Annotate coverage of features from multiple files. 

[ Multi-way file comparisons ] 
    multiinter    Identifies common intervals among multiple interval files. 
    unionbedg     Combines coverage intervals from multiple BEDGRAPH files. 

[ Paired-end manipulation ] 
    pairtobed     Find pairs that overlap intervals in various ways. 
    pairtopair    Find pairs that overlap other pairs in various ways. 

[ Format conversion ] 
    bamtobed      Convert BAM alignments to BED (& other) formats. 
    bedtobam      Convert intervals to BAM records. 
    bamtofastq    Convert BAM records to FASTQ records. 
    bedpetobam    Convert BEDPE intervals to BAM records. 
    bed12tobed6   Breaks BED12 intervals into discrete BED6 intervals. 

[ Fasta manipulation ] 
    getfasta      Use intervals to extract sequences from a FASTA file. 
    maskfasta     Use intervals to mask sequences from a FASTA file. 
    nuc           Profile the nucleotide content of intervals in a FASTA file. 

[ BAM focused tools ] 
    multicov      Counts coverage from multiple BAMs at specific intervals. 
    tag           Tag BAM alignments based on overlaps with interval files. 

[ Statistical relationships ] 
    jaccard       Calculate the Jaccard statistic b/w two sets of intervals. 
    reldist       Calculate the distribution of relative distances b/w two files. 
    fisher        Calculate Fisher statistic b/w two feature files. 

[ Miscellaneous tools ] 
    overlap       Computes the amount of overlap from two intervals. 
    igv           Create an IGV snapshot batch script. 
    links         Create a HTML page of links to UCSC locations. 
    makewindows   Make interval "windows" across a genome. 
    groupby       Group by common cols. & summarize oth. cols. (~ SQL "groupBy") 
    expand        Replicate lines based on lists of values in columns. 
    split         Split a file into multiple files with equal records or base pairs. 
    summary       Statistical summary of intervals in a file. 

[ General Parameters ] 
     --cram-ref    Reference used by a CRAM input 

[ General help ] 
    --help        Print this help menu. 
    --version     What version of bedtools are you using?. 
    --contact     Feature requests, bugs, mailing lists, etc. 

Copied to clipboard