An HTS-specs compliant BED toolkit.
The package can be installed with pip
:
pip install bedspec
>>> from bedspec import Bed3
>>>
>>> bed = Bed3("chr1", start=2, end=8)
>>> from bedspec import BedWriter
>>> from tempfile import NamedTemporaryFile
>>>
>>> temp_file = NamedTemporaryFile(mode="w+t", suffix=".txt")
>>>
>>> with BedWriter.from_path(temp_file.name, Bed3) as writer:
... writer.write(bed)
>>> from bedspec import BedReader
>>>
>>> with BedReader.from_path(temp_file.name, Bed3) as reader:
... for bed in reader:
... print(bed)
Bed3(refname='chr1', start=2, end=8)
This package provides builtin classes for the following BED formats:
>>> from bedspec import Bed2
>>> from bedspec import Bed3
>>> from bedspec import Bed4
>>> from bedspec import Bed5
>>> from bedspec import Bed6
>>> from bedspec import Bed12
>>> from bedspec import BedGraph
>>> from bedspec import BedPE
Use a fast overlap detector for any collection of interval types, including third-party:
>>> from bedspec import Bed3, Bed4
>>> from bedspec.overlap import OverlapDetector
>>>
>>> bed1 = Bed3("chr1", start=1, end=4)
>>> bed2 = Bed3("chr1", start=5, end=9)
>>>
>>> detector = OverlapDetector[Bed3]([bed1, bed2])
>>>
>>> my_feature = Bed4("chr1", start=2, end=3, name="hi-mom")
>>> detector.overlaps(my_feature)
True
The overlap detector supports the following operations:
overlapping
: return all overlapping featuresoverlaps
: test if any overlapping features existenclosed_by
: return those enclosed by the input featureenclosing
: return those enclosing the input feature
To create a custom BED record, inherit from the relevant BED-type (PointBed
, SimpleBed
, PairBed
).
For example, to create a custom BED3+1 class:
>>> from dataclasses import dataclass
>>>
>>> from bedspec import SimpleBed
>>>
>>> @dataclass(eq=True)
... class Bed3Plus1(SimpleBed):
... refname: str
... start: int
... end: int
... my_custom_field: float | None
See the contributing guide for more information.