Skip to content

Commit

Permalink
feat: coerce string input
Browse files Browse the repository at this point in the history
  • Loading branch information
jsstevenson committed May 22, 2024
1 parent fe483de commit 296228f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
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 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 genome: {list(Genome)}"
raise ValueError(msg) from e

data_handler = CustomData(
f"chainfile_{from_db.value}_to_{to_db.value}",
"chain",
Expand Down

0 comments on commit 296228f

Please sign in to comment.