Skip to content

Commit

Permalink
feat: use typeline for IO (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
clintval authored Nov 8, 2024
1 parent 0db2106 commit 523b9d4
Show file tree
Hide file tree
Showing 18 changed files with 711 additions and 872 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @clintval
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: publish

on:
push:
tags: '\d+.\d+.\d+'
tags: '[0-9]+.[0-9]+.[0-9]+'

env:
POETRY_VERSION: 1.6
Expand All @@ -16,27 +16,28 @@ jobs:
- uses: actions/checkout@v4
with:
fetch-depth: 0
submodules: true

- uses: rickstaa/action-contains-tag@v1
id: contains_tag
with:
reference: "main"
tag: "${{ github.ref_name }}"

tests:
name: tests
unit-tests:
name: unit-tests
needs: on-main-branch-check
if: ${{ needs.on-main-branch-check.outputs.on_main == 'true' }}
uses: "./.github/workflows/tests.yml"

build-wheels:
name: build wheels
needs: tests
needs: unit-tests
uses: "./.github/workflows/wheels.yml"

build-sdist:
name: build source distribution
needs: tests
needs: unit-tests
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand Down Expand Up @@ -101,6 +102,7 @@ jobs:
with:
fetch-depth: 0
ref: ${{ github.ref_name }}
submodules: true

- name: Generate a Changelog
uses: orhun/git-cliff-action@v3
Expand All @@ -127,6 +129,6 @@ jobs:
with:
name: ${{ github.ref_name }}
body: |
${{ needs.draft-changelog.outputs.release_body }}
${{ needs.make-changelog.outputs.release_body }}
draft: false
prerelease: false
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Code checks
name: unit tests

on:
push:
Expand All @@ -16,7 +16,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
PYTHON_VERSION: ["3.11", "3.12"]
PYTHON_VERSION: ["3.10", "3.11", "3.12"]
steps:
- uses: actions/checkout@v4
with:
Expand Down
102 changes: 55 additions & 47 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

[![PyPi Release](https://badge.fury.io/py/bedspec.svg)](https://badge.fury.io/py/bedspec)
[![CI](https://github.com/clintval/bedspec/actions/workflows/tests.yml/badge.svg?branch=main)](https://github.com/clintval/bedspec/actions/workflows/tests.yml?query=branch%3Amain)
[![Python Versions](https://img.shields.io/badge/python-3.11_|_3.12-blue)](https://github.com/clintval/bedspec)
[![MyPy Checked](http://www.mypy-lang.org/static/mypy_badge.svg)](http://mypy-lang.org/)
[![Python Versions](https://img.shields.io/badge/python-3.10_|_3.11_|_3.12-blue)](https://github.com/clintval/typeline)
[![basedpyright](https://img.shields.io/badge/basedpyright-checked-42b983)](https://docs.basedpyright.com/latest/)
[![mypy](https://www.mypy-lang.org/static/mypy_badge.svg)](https://mypy-lang.org/)
[![Poetry](https://img.shields.io/endpoint?url=https://python-poetry.org/badge/v0.json)](https://python-poetry.org/)
[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://docs.astral.sh/ruff/)

Expand All @@ -21,65 +22,71 @@ pip install bedspec

### Building a BED Feature

```python
from bedspec import Bed3
```pycon
>>> from bedspec import Bed3
>>>
>>> bed = Bed3("chr1", start=2, end=8)

bed = Bed3("chr1", start=2, end=8)
```

### Writing

```python
from bedspec import BedWriter
```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)

with BedWriter.from_path("test.bed") as writer:
writer.write(bed)
```

### Reading

```python
from bedspec import BedReader
```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)

with BedReader.from_path("test.bed", Bed3) as reader:
for bed in reader:
print(bed)
```
```console
Bed3(refname="chr1", start=2, end=8)
```

### BED Types

This package provides builtin classes for the following BED formats:

```python
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
```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:

```python
from bedspec import Bed3, Bed4
from bedspec.overlap import OverlapDetector

bed1 = Bed3("chr1", start=1, end=4)
bed2 = Bed3("chr1", start=5, end=9)
```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

detector = OverlapDetector[Bed3]([bed1, bed2])

my_feature = Bed4("chr1", start=2, end=3, name="hi-mom")

assert detector.overlaps(my_feature) is True
```

The overlap detector supports the following operations:
Expand All @@ -95,17 +102,18 @@ To create a custom BED record, inherit from the relevant BED-type (`PointBed`, `

For example, to create a custom BED3+1 class:

```python
from dataclasses import dataclass

from bedspec import SimpleBed
```pycon
>>> 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

@dataclass(eq=True)
class Bed3Plus1(SimpleBed):
refname: str
start: int
end: int
my_custom_field: float | None
```

## Development and Testing
Expand Down
31 changes: 27 additions & 4 deletions bedspec/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# ruff: noqa: F401
from ._bedspec import Bed2
from ._bedspec import Bed3
from ._bedspec import Bed4
Expand All @@ -10,11 +9,35 @@
from ._bedspec import BedLike
from ._bedspec import BedPE
from ._bedspec import BedStrand
from ._bedspec import GenomicSpan
from ._bedspec import BedType
from ._bedspec import Named
from ._bedspec import PairBed
from ._bedspec import PointBed
from ._bedspec import ReferenceSpan
from ._bedspec import SimpleBed
from ._bedspec import Stranded
from ._io import BedReader
from ._io import BedWriter
from ._reader import BedReader
from ._writer import BedWriter

__all__ = [
"Bed2",
"Bed3",
"Bed4",
"Bed5",
"Bed6",
"Bed12",
"BedColor",
"BedGraph",
"BedLike",
"BedPE",
"BedStrand",
"BedType",
"Named",
"PairBed",
"PointBed",
"ReferenceSpan",
"SimpleBed",
"Stranded",
"BedReader",
"BedWriter",
]
Loading

0 comments on commit 523b9d4

Please sign in to comment.