|
| 1 | +"""Provide abstraction for a VLM-to-AnyVar connection.""" |
| 2 | + |
| 3 | +import logging |
| 4 | + |
| 5 | +import requests |
| 6 | +from anyvar.utils.types import VrsVariation |
| 7 | +from ga4gh.vrs import models |
| 8 | + |
| 9 | +from anyvlm.anyvar.base_client import ( |
| 10 | + AnyVarClientError, |
| 11 | + BaseAnyVarClient, |
| 12 | + UnidentifiedObjectError, |
| 13 | +) |
| 14 | + |
| 15 | +_logger = logging.getLogger(__name__) |
| 16 | + |
| 17 | + |
| 18 | +class HttpAnyVarClient(BaseAnyVarClient): |
| 19 | + """AnyVar HTTP-based client""" |
| 20 | + |
| 21 | + def __init__( |
| 22 | + self, hostname: str = "http://localhost:8000", request_timeout: int = 30 |
| 23 | + ) -> None: |
| 24 | + """Initialize client instance |
| 25 | +
|
| 26 | + :param hostname: service API root |
| 27 | + :param request_timeout: timeout value, in seconds, for HTTP requests |
| 28 | + """ |
| 29 | + self.hostname = hostname |
| 30 | + self.request_timeout = request_timeout |
| 31 | + |
| 32 | + def put_objects(self, objects: list[VrsVariation]) -> None: |
| 33 | + """Register objects with AnyVar |
| 34 | +
|
| 35 | + All input objects must have a populated ID field. A validation check for this is |
| 36 | + performed before any variants are registered. |
| 37 | +
|
| 38 | + :param objects: variation objects to register |
| 39 | + :return: completed VRS objects |
| 40 | + :raise AnyVarClientError: if connection is unsuccessful during registration request |
| 41 | + :raise UnidentifiedObjectError: if *any* provided object lacks a VRS ID |
| 42 | + """ |
| 43 | + objects_to_submit = [] |
| 44 | + for vrs_object in objects: |
| 45 | + if not vrs_object.id: |
| 46 | + _logger.error("Provided variant %s has no VRS ID", vrs_object) |
| 47 | + raise UnidentifiedObjectError |
| 48 | + objects_to_submit.append( |
| 49 | + vrs_object.model_dump(exclude_none=True, mode="json") |
| 50 | + ) |
| 51 | + for vrs_object in objects_to_submit: |
| 52 | + response = requests.put( |
| 53 | + f"{self.hostname}/vrs_variation", |
| 54 | + json=vrs_object, |
| 55 | + timeout=self.request_timeout, |
| 56 | + ) |
| 57 | + try: |
| 58 | + response.raise_for_status() |
| 59 | + except requests.HTTPError as e: |
| 60 | + raise AnyVarClientError from e |
| 61 | + |
| 62 | + def search_by_interval( |
| 63 | + self, accession: str, start: int, end: int |
| 64 | + ) -> list[VrsVariation]: |
| 65 | + """Get all variation IDs located within the specified range |
| 66 | +
|
| 67 | + :param accession: sequence accession |
| 68 | + :param start: start position for genomic region |
| 69 | + :param end: end position for genomic region |
| 70 | + :return: list of matching variant objects |
| 71 | + :raise AnyVarClientError: if connection is unsuccessful during search query |
| 72 | + """ |
| 73 | + response = requests.get( |
| 74 | + f"{self.hostname}/search?accession={accession}&start={start}&end={end}", |
| 75 | + timeout=self.request_timeout, |
| 76 | + ) |
| 77 | + try: |
| 78 | + response.raise_for_status() |
| 79 | + except requests.HTTPError as e: |
| 80 | + if response.json() == { |
| 81 | + "detail": "Unable to dereference provided accession ID" |
| 82 | + }: |
| 83 | + return [] |
| 84 | + raise AnyVarClientError from e |
| 85 | + return [models.Allele(**v) for v in response.json()["variations"]] |
| 86 | + |
| 87 | + def close(self) -> None: |
| 88 | + """Clean up AnyVar connection. |
| 89 | +
|
| 90 | + This is a no-op for this class. |
| 91 | + """ |
0 commit comments