Repository navigation
feat(dataproxy)!: replace get_metdata with get_length and get_aliases #692
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
base: v3
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| """provides an abstract class for all data access required for VRS, and a concrete implementation based on seqrepo. | ||
|
|
||
| See https://vr-spec.readthedocs.io/en/1.1/impl-guide/required_data.html | ||
| See https://vrs.ga4gh.org/en/latest/conventions/required_data.html | ||
| """ | ||
|
|
||
| import datetime | ||
|
|
@@ -56,11 +56,6 @@ def get_sequence( | |
| ) -> str: | ||
| """Return the specified sequence or subsequence | ||
|
|
||
| start and end are optional | ||
|
|
||
| If the given sequence does not exist, KeyError is raised. If start is after end, | ||
| DataProxyValidationError may be raised. | ||
|
|
||
| For example: | ||
|
|
||
| >>> dp.get_sequence("NM_000551.3", 0, 10) | ||
|
|
@@ -75,26 +70,20 @@ def get_sequence( | |
| """ | ||
|
|
||
| @abstractmethod | ||
| def get_metadata(self, identifier: str) -> dict: | ||
| """For a given identifier, return a structure (dict) containing sequence length, aliases, and other optional info | ||
|
|
||
| If the given sequence does not exist, KeyError is raised. | ||
| def get_length(self, identifier: str) -> int: | ||
| """Return the sequence length. | ||
|
|
||
| For example: | ||
| :param identifier: Sequence identifier to resolve. | ||
| :return: Sequence length. | ||
| :raises KeyError: If the sequence identifier is not found. | ||
| """ | ||
|
|
||
| >>> dp.get_metadata("NM_000551.3") | ||
| {'added': '2016-08-24T05:03:11Z', | ||
| 'aliases': ['MD5:215137b1973c1a5afcf86be7d999574a', | ||
| 'RefSeq:NM_000551.3', | ||
| 'SEGUID:T12L0p2X5E8DbnL0+SwI4Wc1S6g', | ||
| 'SHA1:4f5d8bd29d97e44f036e72f4f92c08e167354ba8', | ||
| 'ga4gh:SQ.v_QTc1p-MUYdgrRv4LMT6ByXIOsdw3C_', | ||
| 'gi:319655736'], | ||
| 'alphabet': 'ACGT', | ||
| 'length': 4560} | ||
| @abstractmethod | ||
| def get_aliases(self, identifier: str) -> list[str]: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know the |
||
| """Return the known sequence aliases. | ||
|
|
||
| :param identifier: Sequence identifier to resolve. | ||
| :return: Sequence metadata, including length and aliases. | ||
| :return: List of known sequence aliases. | ||
| :raises KeyError: If the sequence identifier is not found. | ||
| """ | ||
|
|
||
|
|
@@ -142,10 +131,11 @@ def translate_sequence_identifier( | |
| :raises KeyError: If the sequence identifier is not found. | ||
| """ | ||
| try: | ||
| md = self.get_metadata(identifier) | ||
| # Deduplicate while preserving the backend's alias order. | ||
| aliases = list(dict.fromkeys(self.get_aliases(identifier))) | ||
| except (ValueError, KeyError, IndexError) as e: | ||
| raise KeyError(identifier) from e | ||
| aliases = list(set(md["aliases"])) # ensure uniqueness | ||
|
|
||
| if namespace is not None: | ||
| nsd = namespace + ":" | ||
| aliases = [a for a in aliases if a.startswith(nsd)] | ||
|
|
@@ -242,8 +232,7 @@ def validate_location_bounds( | |
| """ | ||
| # same cache key as derive_refget_accession | ||
| sequence_id = coerce_namespace(sequence_id) | ||
| md = self.get_metadata(sequence_id) | ||
| seq_len = md["length"] | ||
| seq_len = self.get_length(sequence_id) | ||
| bad = [] | ||
| for name, pos in (("start", start_pos), ("end", end_pos)): | ||
| values = pos.root if isinstance(pos, Range) else [pos] | ||
|
|
@@ -256,7 +245,10 @@ def validate_location_bounds( | |
| for name, pos in bad | ||
| ) | ||
| # Name the sequence as given, plus the refget accession it resolved to | ||
| refget_ac = next((a for a in md["aliases"] if a.startswith("ga4gh:")), None) | ||
| refget_ac = next( | ||
| (a for a in self.get_aliases(sequence_id) if a.startswith("ga4gh:")), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Small thing: before, the aliases here came from the same metadata fetch that gave the length. Now this is a second backend call on the error path, so if |
||
| None, | ||
| ) | ||
| seq_name = sequence_id | ||
| if refget_ac and refget_ac != sequence_id: | ||
| seq_name += f" ({refget_ac})" | ||
|
|
@@ -279,18 +271,6 @@ class _SeqRepoDataProxyBase(_DataProxy): | |
| # wraps seqreqpo classes in order to provide translation to/from | ||
| # `ga4gh` identifiers. | ||
|
|
||
| @functools.lru_cache | ||
| def get_metadata(self, identifier: str) -> dict: | ||
| """Fetch sequence metadata. | ||
|
|
||
| :param identifier: Sequence identifier to resolve. | ||
| :return: Sequence metadata, including length and aliases. | ||
| :raises KeyError: If the sequence identifier is not found. | ||
| """ | ||
| md = self._get_metadata(identifier) | ||
| md["aliases"] = list(a for a in md["aliases"]) # noqa: C400 | ||
| return md | ||
|
|
||
| @functools.lru_cache | ||
| def get_sequence( | ||
| self, identifier: str, start: int | None = None, end: int | None = None | ||
|
|
@@ -308,19 +288,48 @@ def get_sequence( | |
| validate_start_le_end(identifier, start, end) | ||
| return self._get_sequence(identifier, start=start, end=end) | ||
|
|
||
| @functools.lru_cache | ||
| def get_length(self, identifier: str) -> int: | ||
| """Return the sequence length. | ||
|
|
||
| :param identifier: Sequence identifier to resolve. | ||
| :return: Sequence length. | ||
| :raises KeyError: If the sequence identifier is not found. | ||
| """ | ||
| return self._get_length(identifier) | ||
|
|
||
| @abstractmethod | ||
| def _get_metadata(self, identifier: str) -> dict: # pragma: no cover | ||
| """Fetch sequence metadata from the backend. | ||
| def _get_length(self, identifier: str) -> int: | ||
| """Return the sequence length. | ||
|
|
||
| :param identifier: Sequence identifier to resolve. | ||
| :return: Sequence metadata, including length and aliases. | ||
| :return: Sequence length. | ||
| :raises KeyError: If the sequence identifier is not found. | ||
| """ | ||
|
|
||
| @functools.lru_cache | ||
| def get_aliases(self, identifier: str) -> list[str]: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because this is |
||
| """Return the known sequence aliases. | ||
|
|
||
| :param identifier: Sequence identifier to resolve. | ||
| :return: List of known sequence aliases. | ||
| :raises KeyError: If the sequence identifier is not found. | ||
| """ | ||
| return self._get_aliases(identifier) | ||
|
|
||
| @abstractmethod | ||
| def _get_aliases(self, identifier: str) -> list[str]: | ||
| """Return the known sequence aliases. | ||
|
|
||
| :param identifier: Sequence identifier to resolve. | ||
| :return: List of known sequence aliases. | ||
| :raises KeyError: If the sequence identifier is not found. | ||
| """ | ||
|
|
||
| @abstractmethod | ||
| def _get_sequence( | ||
| self, identifier: str, start: int | None = None, end: int | None = None | ||
| ) -> str: # pragma: no cover | ||
| ) -> str: | ||
| """Fetch a sequence or subsequence from the backend. | ||
|
|
||
| :param identifier: Sequence identifier to resolve. | ||
|
|
@@ -356,26 +365,48 @@ def _get_sequence( | |
| """ | ||
| return self.sr.fetch_uri(coerce_namespace(identifier), start, end) | ||
|
|
||
| def _get_metadata(self, identifier: str) -> dict: | ||
| """Fetch sequence metadata from the backend. | ||
| @functools.lru_cache | ||
| def _resolve_sequence_id(self, identifier: str) -> str: | ||
| """Resolve an identifier to the internal SeqRepo sequence ID. | ||
|
|
||
| :param identifier: Sequence identifier to resolve. | ||
| :return: Sequence metadata, including length and aliases. | ||
| :raises KeyError: If the sequence identifier is not found. | ||
| :return: Internal SeqRepo sequence ID. | ||
| :raises KeyError: If the identifier is not found or resolves to multiple sequences. | ||
| """ | ||
| ns, a = coerce_namespace(identifier).split(":", 2) | ||
| r = list(self.sr.aliases.find_aliases(namespace=ns, alias=a)) | ||
| if len(r) == 0: | ||
| namespace, alias = coerce_namespace(identifier).split(":", 1) | ||
| seq_ids = { | ||
| record["seq_id"] | ||
| for record in self.sr.aliases.find_aliases(namespace=namespace, alias=alias) | ||
| } | ||
| if not seq_ids: | ||
| raise KeyError(identifier) | ||
| seq_id = r[0]["seq_id"] | ||
| seqinfo = self.sr.sequences.fetch_seqinfo(seq_id) | ||
|
|
||
| if len(seq_ids) > 1: | ||
| msg = f"Identifier resolves to multiple sequences: {identifier}" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a behavior change from the old
If the stricter behavior is intended, could the ambiguity message reach the caller (or the change be noted in the changelog)? |
||
| raise KeyError(msg) | ||
|
|
||
| return seq_ids.pop() | ||
|
|
||
| def _get_length(self, identifier: str) -> int: | ||
| """Return the sequence length. | ||
|
|
||
| :param identifier: Sequence identifier to resolve. | ||
| :return: Sequence length. | ||
| :raises KeyError: If the sequence identifier is not found. | ||
| """ | ||
| seq_id = self._resolve_sequence_id(identifier) | ||
| return self.sr.sequences.fetch_seqinfo(seq_id)["len"] | ||
|
|
||
| def _get_aliases(self, identifier: str) -> list[str]: | ||
| """Return the known sequence aliases. | ||
|
|
||
| :param identifier: Sequence identifier to resolve. | ||
| :return: List of known sequence aliases. | ||
| :raises KeyError: If the sequence identifier is not found. | ||
| """ | ||
| seq_id = self._resolve_sequence_id(identifier) | ||
| aliases = self.sr.aliases.find_aliases(seq_id=seq_id) | ||
| return { | ||
| "length": seqinfo["len"], | ||
| "alphabet": seqinfo["alpha"], | ||
| "added": _isoformat(seqinfo["added"]), | ||
| "aliases": [f"{a['namespace']}:{a['alias']}" for a in aliases], | ||
| } | ||
| return [f"{a['namespace']}:{a['alias']}" for a in aliases] | ||
|
|
||
|
|
||
| class SeqRepoRESTDataProxy(_SeqRepoDataProxyBase): | ||
|
|
@@ -418,8 +449,29 @@ def _get_sequence( | |
| resp.raise_for_status() | ||
| return resp.text | ||
|
|
||
| def _get_length(self, identifier: str) -> int: | ||
| """Return the sequence length. | ||
|
|
||
| :param identifier: Sequence identifier to resolve. | ||
| :return: Sequence length. | ||
| :raises KeyError: If the sequence identifier is not found. | ||
| :raises requests.RequestException: If the request fails with an error other than a missing sequence. | ||
| """ | ||
| return self._get_metadata(identifier)["length"] | ||
|
|
||
| def _get_aliases(self, identifier: str) -> list[str]: | ||
| """Return the known sequence aliases. | ||
|
|
||
| :param identifier: Sequence identifier to resolve. | ||
| :return: List of known sequence aliases. | ||
| :raises KeyError: If the sequence identifier is not found. | ||
| :raises requests.RequestException: If the request fails with an error other than a missing sequence. | ||
| """ | ||
| return list(self._get_metadata(identifier)["aliases"]) | ||
|
|
||
| @functools.lru_cache | ||
| def _get_metadata(self, identifier: str) -> dict: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Optional: caching is now layered. |
||
| """Fetch sequence metadata from the backend. | ||
| """Fetch and cache the REST metadata used by length and alias lookups. | ||
|
|
||
| :param identifier: Sequence identifier to resolve. | ||
| :return: Sequence metadata, including length and aliases. | ||
|
|
@@ -447,10 +499,11 @@ def __init__(self, dp: _DataProxy, alias: str) -> None: | |
|
|
||
| :param dp: Sequence data proxy. | ||
| :param alias: Sequence accession or alias. | ||
| :raises KeyError: If the sequence identifier is not found. | ||
| """ | ||
| self.dp = dp | ||
| self.alias = alias | ||
| self._md = self.dp.get_metadata(self.alias) | ||
| self._length = self.dp.get_length(self.alias) | ||
|
|
||
| def __str__(self) -> str: | ||
| """Fetch the complete sequence. | ||
|
|
@@ -464,7 +517,7 @@ def __len__(self) -> int: | |
|
|
||
| :return: Sequence length. | ||
| """ | ||
| return self._md["length"] | ||
| return self._length | ||
|
|
||
| def __reversed__(self) -> NoReturn: | ||
| """Reject reversed iteration. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optional cleanup: with
_get_metadatagone from the SeqRepo base,_isoformat(near the bottom of the module) and thisimport datetimelook unused. Their only caller was"added": _isoformat(seqinfo["added"]). Both could probably go, along with the doctest.