pybedtools Reference

The following tables summarize the methods and functions; click on a method or function name to see the complete documentation.

BedTool creation

The main BedTool documentation, with a list of all methods in alphabetical order at the bottom. For more details, please see Creating a BedTool.

pybedtools.bedtool.BedTool([fn, ...])

BEDTools wrappers

These methods wrap BEDTools programs for easy use with Python; you can then use the other pybedtools functionality for further manipulation and analysis.

The documentation of each of these methods starts with pybedtools-specific documentation, possibly followed by an example. Finally, the BEDTools help is copied verbatim from whatever version was installed when generating these docs.

In general the BEDTool wrapper methods adhere to the Design principles:

pybedtools.bedtool.BedTool.intersect(*args, ...)

Wraps bedtools intersect.

pybedtools.bedtool.BedTool.window(*args, ...)

Wraps bedtools window.

pybedtools.bedtool.BedTool.closest(*args, ...)

Wraps bedtools closest.

pybedtools.bedtool.BedTool.coverage(*args, ...)

Wraps bedtools coverage.*args, **kwargs)

Wraps bedtools map; See also BedTool.each().


Wraps bedtools genomecov.

pybedtools.bedtool.BedTool.merge(*args, **kwargs)

Wraps bedtools merge.

pybedtools.bedtool.BedTool.cluster(*args, ...)

Wraps bedtools cluster.

pybedtools.bedtool.BedTool.complement(*args, ...)

Wraps bedtools complement.

pybedtools.bedtool.BedTool.subtract(*args, ...)

Wraps bedtools subtract.

pybedtools.bedtool.BedTool.slop(*args, **kwargs)

Wraps bedtools slop.

pybedtools.bedtool.BedTool.flank(*args, **kwargs)

Wraps bedtools flank.

pybedtools.bedtool.BedTool.sort(*args, **kwargs)

Wraps bedtools sort.

pybedtools.bedtool.BedTool.random(*args, ...)

Wraps bedtools random.

pybedtools.bedtool.BedTool.shuffle(*args, ...)

Wraps bedtools shuffle.

pybedtools.bedtool.BedTool.annotate(*args, ...)

Wraps bedtools annotate.


Wraps bedtools multiintersect.


Wraps bedtools unionbedg.


Wraps bedtools pairtobed.


Wraps bedtools pairtopair.

pybedtools.bedtool.BedTool.bam_to_bed(*args, ...)

Wraps bedtools bamtobed.

pybedtools.bedtool.BedTool.to_bam(*args, ...)

Wraps bedtools bedtobam


Wraps bedtools bedpetobam.

pybedtools.bedtool.BedTool.bed6(*args, **kwargs)

Wraps bedtools bed12tobed6.


Wraps bedtools bamtofastq.

pybedtools.bedtool.BedTool.sequence(*args, ...)

Wraps bedtools getfasta.

pybedtools.bedtool.BedTool.mask_fasta(*args, ...)

Wraps bedtools maskfasta.


Wraps bedtools nuc.


Wraps bedtools multicov.

pybedtools.bedtool.BedTool.tag_bam(*args, ...)

Wraps bedtools tag.

pybedtools.bedtool.BedTool.jaccard(*args, ...)

Returns a dictionary with keys (intersection, union, jaccard).

pybedtools.bedtool.BedTool.reldist(*args, ...)

If detail=False, then return a dictionary with keys (reldist, count,

pybedtools.bedtool.BedTool.overlap(*args, ...)

Wraps bedtools overlap.

pybedtools.bedtool.BedTool.links(*args, **kwargs)

Wraps linksBed.

pybedtools.bedtool.BedTool.igv(*args, **kwargs)

Wraps bedtools igv.


Wraps bedtools makewindows.

pybedtools.bedtool.BedTool.groupby(*args, ...)

Wraps bedtools groupby.

pybedtools.bedtool.BedTool.expand(*args, ...)

Wraps bedtools expand

Other BedTool methods

These methods are some of the ways in which pybedtools extend the BEDTools suite.

Feature-by-feature operations

Methods that operate on a feature-by-feature basis to modify or filter features on the fly.

pybedtools.bedtool.BedTool.each(func, *args, ...)

Modify each feature with a user-defined function.

pybedtools.bedtool.BedTool.filter(func, ...)

Filter features by user-defined function.

pybedtools.bedtool.BedTool.split(func, ...)

Split each feature using a user-defined function.


Ensure all features fall within chromosome limits.


Remove invalid features that may break BEDTools programs.

The pybedtools.featurefuncs module contains some commonly-used functions that can be passed to BedTool.each():


Returns the 3'-most coordinate, plus upstream and downstream bp; adds the string add_to_name to the feature's name if provided (e.g., "_polyA_site")


Returns the 5'-most coordinate, plus upstream and downstream bp; adds the string add_to_name to the feature's name if provided (e.g., "_TSS")

pybedtools.featurefuncs.TSS(feature[, ...])

Alias for five_prime.

pybedtools.featurefuncs.extend_fields(feature, n)

Pads the fields of the feature with "." to a total length of n fields,[, width])

Return the width bp from the center of a feature.


Specialized version of center() that just returns the single-bp midpoint


Normalizes the value at feature[idx] to the feature's length, in kb.

pybedtools.featurefuncs.rename(feature, name)

Forces a rename of all features, e.g., for renaming everything in a file 'exon'


Return True if feature length > size

pybedtools.featurefuncs.less_than(feature[, ...])

Return True if feature length < size


Normalizes the value at feature[idx] to the feature's length, in kb.

pybedtools.featurefuncs.rename(feature, name)

Forces a rename of all features, e.g., for renaming everything in a file 'exon'


pybedtools.featurefuncs.add_color(feature, ...)


pybedtools.featurefuncs.gff2bed(feature[, ...])




Searching for features

These methods take a single interval as input and return the intervals of the BedTool that overlap.

This can be useful when searching across many BED files for a particular coordinate range – for example, they can be used identify all binding sites, stored in many different BED files, that fall within a gene’s coordinates.


Return all intervals that overlap interval.


Return whether or not any intervals overlap interval.


Return the number of intervals that overlap interval.


Retrieve all intervals within coordinates from a "tabixed" BedTool.

pybedtools.bedtool.BedTool.tabix([in_place, ...])

Prepare a BedTool for use with Tabix.

pybedtools.bedtool.BedTool.bgzip([in_place, ...])

Helper function for more control over "tabixed" BedTools.

BedTool introspection

These methods provide information on the BedTool object.

If using BedTool.head(), don’t forget that you can index into BedTool objects, too.

pybedtools.bedtool.BedTool.head([n, as_string])

Prints the first n lines or returns them if as_string is True


Count the number features in this BedTool.


Number of fields in each line of this BedTool (checks n lines)


Return the type of the current file.

Randomization helpers

Helper methods useful for assessing empirical instersection distributions between interval files.


Generalized method for applying a function in parallel.


Dictionary of results from many randomly shuffled intersections.


Perform iterations shufflings, each time intersecting with other.


Like randomintersection, but return the bp overlap instead of the number of intersecting intervals.


Return a BedTool containing a random subset.


Computes the naive Jaccard statistic (intersection divided by union).

pybedtools.bedtool.BedTool.random_op(*args, ...)

For backwards compatibility; see BedTool.parallel_apply instead.

Managing BedTool objects on disk

These methods are used to specify where to save results from BedTool operations.

pybedtools.bedtool.BedTool.saveas(*args, ...)

Make a copy of the BedTool.

pybedtools.bedtool.BedTool.moveto(*args, ...)

Move to a new filename (can be much quicker than BedTool.saveas())

Misc operations

Methods that can’t quite be categorized into the above sections.*args, **kwargs)

Concatenate interval files together.

Returns a new BedTool with only intervals at lines inds


Returns an iterator of the absolute distances between features in self and other.

pybedtools.bedtool.BedTool.cut(indexes[, stream])

Analagous to unix cut.


Return the total number of bases covered by this interval file.

pybedtools.bedtool.BedTool.with_attrs(*args, ...)

Helper method for adding attributes in the middle of a pipeline.


Returns an IntervalFile of this BedTool for low-level interface.

pybedtools.bedtool.BedTool.introns([gene, exon])

Create intron features (requires specific input format).


Prepare BedTool for operations that require chromosome coords.


Print the sequence that was retrieved by BedTool.sequence.


Save sequences, after calling BedTool.sequence.

pybedtools.bedtool.BedTool.seq(loc, fasta)

Return just the sequence from a region string or a single location >>> fn = pybedtools.example_filename('test.fa') >>> BedTool.seq('chr1:2-10', fn) 'GATGAGTCT' >>> BedTool.seq(('chr1', 1, 10), fn) 'GATGAGTCT'


Returns a new BedTool of the liftedOver features, saving the unmapped ones as unmapped.


Returns a normalization instance for use by featurefuncs.add_color().


Returns an iterator of relative distances between features in self and other.

Module-level functions

Working with example files

pybedtools comes with many example files. Here are some useful functions for accessing them.


Return a bedtool using a bed file from the pybedtools examples directory.


Returns a list of files in the examples dir.


Return a bed file from the pybedtools examples directory.

Creating Interval objects from scratch

Interval objects are the core object in pybedtools to represent a genomic interval, written in Cython for speed.


Class to represent a genomic interval.


Create an Interval object from a list of strings.

pybedtools setup and config

Use these functions right after importing in order to use custom paths or to clean up the temp directory.


Explicitly set path to BEDTools installation dir.


Gets the current tempdir for the module.


Set the directory for temp files.

pybedtools.helpers.cleanup([verbose, remove_all])

Deletes all temp files from the current session (or optionally all sessions)


Enable debug mode.

Working with “chromsizes” or assembly coordinate files

Many BEDTools programs need “genome files” or “chromsizes” files so as to remain within the coordinates of the assembly you’re working on. These functions help manage these files.


Download chrom size info for genome from UCSC and returns the dictionary.


Looks for a genome already included in the genome registry; if not found it first tries to look it up via genomepy.

pybedtools.helpers.chromsizes_to_file(...[, fn])

Converts a chromsizes dictionary to a file.

Performing operations in parallel (multiprocessing)

pybedtools.parallel.parallel_apply(...[, ...])

Call an arbitrary BedTool method many times in parallel.


The pybedtools.contrib module contains higher-level code that leverages BedTool objects for common analyses.


Plotting results from BEDTools/pybedtools operations is very useful for exploring and understanding the tools as well as for teaching purposes.




Plots a "binary heatmap", showing the results of a multi-intersection.


Convenience function useful printing the results from binary_heatmap().



Working with bigWig files

At this time, pybedtools does not support reading bigWig files, only creating them via UCSC utilities.

pybedtools.contrib.bigwig.bam_to_bigwig(bam, ...)

Given a BAM file bam and assembly genome, create a bigWig file scaled such that the values represent scaled reads -- that is, reads per million mapped reads.


pybedtools.contrib.bigwig.wig_to_bigwig(wig, ...)

Working with bigBed files

pybedtools.contrib.bigbed.bigbed(x, genome, ...)

Converts a BedTool object to a bigBed format and returns the new filename.



The IntersectionMatrix class makes it easy to intersect a large number of interval files with each other.

pybedtools.contrib.IntersectionMatrix(beds, ...)

Class to handle many pairwise comparisons of interval files


The venn_maker module helps you make Venn diagrams using the R package VennDiagram.

Note that Venn diagrams are not good for when you have nested intersections. See the docs for pybedtools.contrib.venn_maker.cleaned_intersect() and its source for more details.


Interface between pybedtools and the R package VennDiagram.


Given a list of interval files, write an R script to create a Venn diagram of overlaps (and optionally run it).


Perform interval intersections such that the end products have identical features for overlapping intervals.



Tag each end of a BEDPE with a set of (possibly many) query BED files.


Converts the output from tag_bedpe into a pandas DataFrame containing information about regions that contact each other in cis (same fragment) or trans (different fragments).