Skip to content

Commit dffe999

Browse files
committed
more error handling fixes, etc
1 parent 4d82f5e commit dffe999

File tree

7 files changed

+48
-20
lines changed

7 files changed

+48
-20
lines changed

src/anyvlm/anyvar/base_client.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import abc
44
from collections.abc import Iterable
55

6+
from anyvar.utils.liftover_utils import ReferenceAssembly
67
from anyvar.utils.types import VrsVariation
78

89

@@ -22,7 +23,9 @@ class BaseAnyVarClient(abc.ABC):
2223

2324
@abc.abstractmethod
2425
def put_allele_expressions(
25-
self, expressions: Iterable[str], assembly: str = "GRCh38"
26+
self,
27+
expressions: Iterable[str],
28+
assembly: ReferenceAssembly = ReferenceAssembly.GRCH38,
2629
) -> list[str | None]:
2730
"""Submit allele expressions to an AnyVar instance and retrieve corresponding VRS IDs
2831

src/anyvlm/anyvar/http_client.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from collections.abc import Iterable
55

66
import requests
7+
from anyvar.utils.liftover_utils import ReferenceAssembly
78
from anyvar.utils.types import VrsVariation
89
from ga4gh.vrs import models
910

@@ -32,7 +33,9 @@ def __init__(
3233
self.request_timeout = request_timeout
3334

3435
def put_allele_expressions(
35-
self, expressions: Iterable[str], assembly: str = "GRCh38"
36+
self,
37+
expressions: Iterable[str],
38+
assembly: ReferenceAssembly = ReferenceAssembly.GRCH38,
3639
) -> list[str | None]:
3740
"""Submit allele expressions to an AnyVar instance and retrieve corresponding VRS IDs
3841
@@ -47,7 +50,7 @@ def put_allele_expressions(
4750
url = f"{self.hostname}/variation"
4851
payload = {
4952
"definition": expression,
50-
"assembly_name": assembly,
53+
"assembly_name": assembly.value,
5154
"input_type": "Allele",
5255
}
5356
try:

src/anyvlm/anyvar/python_client.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
from anyvar import AnyVar
77
from anyvar.storage.base_storage import Storage
88
from anyvar.translate.translate import TranslationError, Translator
9+
from anyvar.utils.liftover_utils import ReferenceAssembly
910
from anyvar.utils.types import VrsVariation
11+
from ga4gh.vrs.dataproxy import DataProxyValidationError
1012

1113
from anyvlm.anyvar.base_client import BaseAnyVarClient
1214

@@ -25,7 +27,9 @@ def __init__(self, translator: Translator, storage: Storage) -> None:
2527
self.av = AnyVar(translator, storage)
2628

2729
def put_allele_expressions(
28-
self, expressions: Iterable[str], assembly: str = "GRCh38"
30+
self,
31+
expressions: Iterable[str],
32+
assembly: ReferenceAssembly = ReferenceAssembly.GRCH38,
2933
) -> list[str | None]:
3034
"""Submit allele expressions to an AnyVar instance and retrieve corresponding VRS IDs
3135
@@ -39,12 +43,17 @@ def put_allele_expressions(
3943
translated_variation = None
4044
try:
4145
translated_variation = self.av.translator.translate_variation(
42-
expression, assembly=assembly
46+
expression, assembly=assembly.value
4347
)
48+
except DataProxyValidationError:
49+
_logger.exception("Found invalid base in expression %s", expression)
4450
except TranslationError:
4551
_logger.exception("Failed to translate expression: %s", expression)
46-
self.av.put_objects([translated_variation]) # type: ignore
47-
results.append(translated_variation.id) # type: ignore
52+
if translated_variation:
53+
self.av.put_objects([translated_variation]) # type: ignore
54+
results.append(translated_variation.id) # type: ignore
55+
else:
56+
results.append(None)
4857
return results
4958

5059
def search_by_interval(

src/anyvlm/functions/ingest_vcf.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from pathlib import Path
77

88
import pysam
9+
from anyvar.utils.liftover_utils import ReferenceAssembly
910
from ga4gh.va_spec.base import CohortAlleleFrequencyStudyResult
1011

1112
from anyvlm.anyvar.base_client import BaseAnyVarClient
@@ -50,7 +51,11 @@ def _yield_expression_af_batches(
5051
yield batch
5152

5253

53-
def ingest_vcf(vcf_path: Path, av: BaseAnyVarClient, assembly: str = "GRCh38") -> None:
54+
def ingest_vcf(
55+
vcf_path: Path,
56+
av: BaseAnyVarClient,
57+
assembly: ReferenceAssembly = ReferenceAssembly.GRCH38,
58+
) -> None:
5459
"""Extract variant and frequency information from a single VCF
5560
5661
Current assumptions (subject to change):
@@ -74,4 +79,4 @@ def ingest_vcf(vcf_path: Path, av: BaseAnyVarClient, assembly: str = "GRCh38") -
7479
for variant_id, af in zip(variant_ids, afs, strict=True): # noqa: B007
7580
if variant_id is None:
7681
continue
77-
# put af object here
82+
# make call to object store method for putting CAF here

tests/unit/anyvar/test_http_client.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,6 @@ def test_put_allele_expressions_catch_httperror():
5353
client.put_allele_expressions(["1-1000000-A-T"])
5454

5555

56-
@pytest.mark.vcr
57-
def test_put_allele_expressions_invalid_assembly(
58-
client: HttpAnyVarClient, alleles: dict
59-
):
60-
with pytest.raises(AnyVarClientError):
61-
client.put_allele_expressions(
62-
alleles["ga4gh:VA.yi7A2l0uIUMaInQaJnHU_B2Cf_OuZRJg"]["vcf_expression"],
63-
assembly="GRCh30000",
64-
)
65-
66-
6756
@pytest.mark.vcr
6857
def test_search_by_interval(client: HttpAnyVarClient, alleles: dict):
6958
"""Test `search_by_interval` for a couple of basic cases"""

tests/unit/anyvar/test_python_client.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,20 @@ def test_put_allele_expressions(client: PythonAnyVarClient, alleles: dict):
4242
client.put_allele_expressions([allele_fixture["vcf_expression"]])
4343

4444

45+
@pytest.mark.vcf
46+
def test_put_allele_expressions_handle_invalid(
47+
client: PythonAnyVarClient, alleles: dict
48+
):
49+
results = client.put_allele_expressions(["Y-2781761-A-C"]) # wrong REF
50+
assert results == [None]
51+
52+
allele_fixture = alleles["ga4gh:VA.yi7A2l0uIUMaInQaJnHU_B2Cf_OuZRJg"]
53+
results = client.put_allele_expressions(
54+
["Y-2781761-A-C", allele_fixture["vcf_expression"]]
55+
)
56+
assert results == [None, allele_fixture["variation"]["id"]]
57+
58+
4559
@pytest.mark.vcr
4660
def test_search_by_interval(populated_client: PythonAnyVarClient, alleles: dict):
4761
"""Test `search_by_interval` for a couple of basic cases"""

tests/unit/functions/test_ingest_vcf.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,8 @@ def input_vcf_path(test_data_dir: Path) -> Path:
1414
@pytest.mark.vcr
1515
def test_ingest_vcf(input_vcf_path: Path, anyvar_client: HttpAnyVarClient):
1616
ingest_vcf(input_vcf_path, anyvar_client)
17+
18+
19+
# GRCh37 VCF
20+
# nonexistent path
21+
# once storage exists, think about how to validate that AFs are stored

0 commit comments

Comments
 (0)