Skip to content
This repository

Python wrapper -- and more -- for Aaron Quinlan's BEDTools (bioinformatics tools)

Octocat-spinner-32 docs sphinx doctest fix
Octocat-spinner-32 pybedtools fix doctest for new jaccard output
Octocat-spinner-32 src BedFile::Open now returns int; allows improved error handling
Octocat-spinner-32 .gitignore docs, including note on bedtools 2.18
Octocat-spinner-32 install samtools for travis-ci
Octocat-spinner-32 edit tests and travis-ci config for bedtools 2.19.1
Octocat-spinner-32 install samtools for travis-ci
Octocat-spinner-32 set paths in travis-ci wrapper script
Octocat-spinner-32 prepend to path in travis-ci config; better for local testing
Octocat-spinner-32 "make clean" on docs before doctesting
Octocat-spinner-32 .travis.yml travis-ci with bedtools 2.18
Octocat-spinner-32 LICENSE.txt license to GPLv2 like bedtools
Octocat-spinner-32 removed and fake_pyrex dirs from manifest
Octocat-spinner-32 README.rst use url
Octocat-spinner-32 no longer compile pdf docs
Octocat-spinner-32 add and fake pyrex stuff to make setuptools work
Octocat-spinner-32 requirements.txt more readthedocs edits
Octocat-spinner-32 setup.cfg add some dev notes to docs
Octocat-spinner-32 auto-install ordereddict and argparse if on <2.7
Octocat-spinner-32 split out sphinx doc creation and doctests from
Octocat-spinner-32 more travis-ci changes


pybedtools is a Python wrapper for Aaron Quinlan's BEDtools programs (, which are widely used for genomic interval manipulation or "genome algebra". pybedtools extends BEDTools by offering feature-level manipulations from with Python. See full online documentation, including installation instructions, at

Why pybedtools?

Here is an example to get the names of genes that are <5 kb away from intergenic SNPs:

from pybedtools import BedTool

snps = BedTool('snps.bed.gz')
genes = BedTool('hg19.gff')

intergenic_snps = snps.subtract(genes)
nearby = genes.closest(intergenic_snps, d=True, stream=True)

for gene in nearby:
    if int(gene[-1]) < 5000:

Useful features shown here include:

  • support for all BEDTools-supported formats (here gzipped BED and GFF)
  • wrapping of all BEDTools programs and arguments (here, subtract and closest and passing the -d flag to closest);
  • streaming results (like Unix pipes, here specified by stream=True)
  • iterating over results while accessing feature data by index or by attribute access (here [-1] and .name).

In contrast, here is the same analysis using shell scripting. Note that this requires knowledge in Perl, bash, and awk. The run time is identical to the pybedtools version above:


snp_fields=`zcat $snps | awk '(NR == 2){print NF; exit;}'`
distance_field=$(($gene_fields + $snp_fields + 1))

intersectBed -a $snps -b $genes -v > $intergenic_snps

closestBed -a $genes -b $intergenic_snps -d \
| awk '($'$distance_field' < 5000){print $9;}' \
| perl -ne 'm/[ID|Name|gene_id]=(.*?);/; print "$1\n"'

rm $intergenic_snps

See the Shell script comparison in the docs for more details on this comparison, or keep reading the full documentation at

Something went wrong with that request. Please try again.