Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow string input #36

Merged
merged 3 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ python3 -m pip install agct
Initialize a class instance:

```python3
from agct import Converter
c = Converter("hg38", "hg19")
from agct import Converter, Genome
c = Converter(Genome.HG38, Genome.HG19)
```

> If a chainfile is unavailable locally, it's downloaded from UCSC and saved using the `wags-tails` package -- see the [configuration instructions](https://github.com/GenomicMedLab/wags-tails?tab=readme-ov-file#configuration) for information on how to designate a non-default storage location.
> If a chainfile is unavailable locally, it's downloaded from UCSC and saved using the `wags-tails` package -- see the [wags-tails configuration instructions](https://github.com/GenomicMedLab/wags-tails?tab=readme-ov-file#configuration) for information on how to designate a non-default storage location.

Call ``convert_coordinate()``:

```python3
c.convert_coordinate("chr7", 140453136, "+")
# [['chr7', 140152936, '+']]
# [['chr7', 140152936, <Strand.POSITIVE: '+'>]]
```

## Development
Expand Down
23 changes: 18 additions & 5 deletions src/agct/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ class Converter:

def __init__(
self,
from_db: Genome | None = None,
to_db: Genome | None = None,
from_db: Genome | str | None = None,
to_db: Genome | str | None = None,
chainfile: str | None = None,
) -> None:
"""Initialize liftover instance.

:param from_db: database name, e.g. ``"19"``. Must be different than ``to_db``
:param from_db: database name, e.g. ``"hg19"``. Must be different than ``to_db``
If ``chainfile`` is provided, will ignore this argument
:param to_db: database name, e.g. ``"38"``. Must be different than ``from_db``
:param to_db: database name, e.g. ``"hg38"``. Must be different than ``from_db``
If ``chainfile`` is provided, will ignore this argument
:param chainfile: Path to chainfile
If not provided, must provide both ``from_db`` and ``to_db`` so that
Expand All @@ -56,14 +56,27 @@ def __init__(
:raise _core.ChainfileError: if unable to read chainfile (i.e. it's invalid)
"""
if not chainfile:
if from_db is None and to_db is None:
if from_db is None or to_db is None:
msg = "Must provide both `from_db` and `to_db`"
raise ValueError(msg)

if from_db == to_db:
msg = "Liftover must be to/from different sources."
raise ValueError(msg)

if isinstance(from_db, str):
try:
from_db = Genome(from_db)
except ValueError as e:
msg = f"Unable to coerce from_db value '{from_db}' to a known reference genome: {list(Genome)}"
raise ValueError(msg) from e
if isinstance(to_db, str):
try:
to_db = Genome(to_db)
except ValueError as e:
msg = f"Unable to coerce to_db value '{to_db}' to a known reference genome: {list(Genome)}"
raise ValueError(msg) from e

data_handler = CustomData(
f"chainfile_{from_db.value}_to_{to_db.value}",
"chain",
Expand Down
10 changes: 10 additions & 0 deletions tests/test_converter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""Module for testing Converter initialization"""
import re

import pytest
from tests.conftest import DATA_DIR

Expand All @@ -19,3 +21,11 @@ def test_invalid():

with pytest.raises(ValueError, match="Liftover must be to/from different sources."):
Converter(Genome.HG19, Genome.HG19)

with pytest.raises(
ValueError,
match=re.escape(
"Unable to coerce to_db value 'hg18' to a known reference genome: [<Genome.HG38: 'hg38'>, <Genome.HG19: 'hg19'>]"
),
):
Converter(Genome.HG19, "hg18")
Loading