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-bedtools.sh install samtools for travis-ci
Octocat-spinner-32 .install-bedtools2.sh edit tests and travis-ci config for bedtools 2.19.1
Octocat-spinner-32 .install-samtools.sh install samtools for travis-ci
Octocat-spinner-32 .install-tabix.sh set paths in travis-ci wrapper script
Octocat-spinner-32 .travis-test-2.18.sh prepend to path in travis-ci config; better for local testing
Octocat-spinner-32 .travis-test.sh "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 MANIFEST.in removed test.sh and fake_pyrex dirs from manifest
Octocat-spinner-32 README.rst use pythonhosted.org url
Octocat-spinner-32 compile-docs.sh no longer compile pdf docs
Octocat-spinner-32 ez_setup.py add ez_setup.py 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 setup.py auto-install ordereddict and argparse if on <2.7
Octocat-spinner-32 sphinx-test.sh split out sphinx doc creation and doctests from test.sh
Octocat-spinner-32 test.sh more travis-ci changes
README.rst

Overview

https://travis-ci.org/daler/pybedtools.png?branch=master

pybedtools is a Python wrapper for Aaron Quinlan's BEDtools programs (https://github.com/arq5x/bedtools), 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 http://pythonhosted.org/pybedtools/.

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:
        print gene.name

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:

snps=snps.bed.gz
genes=hg19.gff
intergenic_snps=/tmp/intergenic_snps

snp_fields=`zcat $snps | awk '(NR == 2){print NF; exit;}'`
gene_fields=9
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 http://pythonhosted.org/pybedtools/index.html.

Something went wrong with that request. Please try again.