Create a BedTool¶
First, follow the Installation instructions if you haven’t already
done so to install both BEDTools and pybedtools.
Then import the pybedtools module and make a new BedTool. A
BedTool object encapsulates all of the available BEDTools programs and
makes them easier to use within Python. Most of the time when working with
pybedtools you’ll be using BedTool objects. In general, a
single BedTool object points to an interval file (BED, GFF, GTF, VCF,
SAM, or BAM format).
>>> import pybedtools
>>> # use a BED file that ships with pybedtools...
>>> a = pybedtools.example_bedtool('a.bed')
>>> # ...or use your own by passing a filename
>>> a = pybedtools.BedTool('peaks.bed')
This documentation uses example files that ship with pybedtools. To
access these files from their installation location, we use the
example_bedtool() function. This is convenient because if you copy-paste
the examples, they will work. When using the example_bedtool() function,
the resulting BedTool object will point to the corresponding file in
the test/data directory of your pybedtools installation. If you would
rather learn using your own files, just pass the filename to a new
BedTool, like the above example.
You can use any file that BEDTools supports – this includes BED, VCF,
GFF, and gzipped versions of any of these. See Creating a BedTool
for more on the different ways of creating a BedTool, including
from iterators and directly from a string.
Now, let’s see how to do a common task performed on BED files: intersections.