https://github.com/clintval/bedspec
An HTS-specs compliant BED toolkit
https://github.com/clintval/bedspec
bed bioinformatics hts interval ngs
Last synced: about 1 month ago
JSON representation
An HTS-specs compliant BED toolkit
- Host: GitHub
- URL: https://github.com/clintval/bedspec
- Owner: clintval
- License: mit
- Created: 2024-02-04T17:24:24.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2026-04-14T00:35:22.000Z (3 months ago)
- Last Synced: 2026-04-28T22:35:16.959Z (2 months ago)
- Topics: bed, bioinformatics, hts, interval, ngs
- Language: Python
- Homepage: https://pypi.org/project/bedspec/
- Size: 143 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# bedspec
[](https://badge.fury.io/py/bedspec)
[](https://github.com/clintval/bedspec/actions/workflows/tests.yml?query=branch%3Amain)
[](https://github.com/clintval/typeline)
[](https://docs.basedpyright.com/latest/)
[](https://mypy-lang.org/)
[](https://python-poetry.org/)
[](https://docs.astral.sh/ruff/)
An HTS-specs compliant BED toolkit.
## Installation
The package can be installed with `pip`:
```console
pip install bedspec
```
## Quickstart
### Building a BED Feature
```pycon
>>> from bedspec import Bed3
>>>
>>> bed = Bed3("chr1", start=2, end=8)
```
### Writing
```pycon
>>> 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)
```
### Reading
```pycon
>>> 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)
```
### BED Types
This package provides builtin classes for the following BED formats:
```pycon
>>> 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
```
### Overlap Detection
Use a fast overlap detector for any collection of interval types, including third-party:
```pycon
>>> 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 features
- `overlaps`: test if any overlapping features exist
- `enclosed_by`: return those enclosed by the input feature
- `enclosing`: return those enclosing the input feature
### Custom BED Types
To create a custom BED record, inherit from the relevant BED-type (`PointBed`, `SimpleBed`, `PairBed`).
For example, to create a custom BED3+1 class:
```pycon
>>> from dataclasses import dataclass
>>>
>>> from bedspec import SimpleBed
>>>
>>> @dataclass
... class Bed3Plus1(SimpleBed):
... refname: str
... start: int
... end: int
... my_custom_field: float | None
```
You can also inherit and extend a pre-existing BED class:
```pycon
>>> from dataclasses import dataclass
>>>
>>> from bedspec import Bed3
>>>
>>> @dataclass
... class Bed3Plus1(Bed3):
... my_custom_field: float | None
>>>
>>> Bed3Plus1(refname="chr1", start=2, end=3, my_custom_field=0.1)
Bed3Plus1(refname='chr1', start=2, end=3, my_custom_field=0.1)
```
## Development and Testing
See the [contributing guide](./CONTRIBUTING.md) for more information.