diff --git a/CHANGELOG.md b/CHANGELOG.md index bbc1d9f1..d456b783 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ The project uses [Semantic Versioning](https://semver.org/): `MAJOR.MINOR.PATCH` ### Added ### Changed ### Fixed +- Improve UniProt alias error handling by returning unresolved aliases as `None` entries and raising a `ValueError` when required alias conversions cannot be resolved. ## [2.1.0] - 2026-07-16 diff --git a/cancermuts/datasources.py b/cancermuts/datasources.py index 9f2c6b93..1554e7e4 100644 --- a/cancermuts/datasources.py +++ b/cancermuts/datasources.py @@ -247,6 +247,8 @@ def get_sequence(self, gene_id, upid=None, upac=None, isoform=None): elif upid is not None: self.log.info("UniProt AC will be mapped from UniProt ID") this_upac = self._get_aliases(upid, ['UniProtKB_primaryAccession'])['UniProtKB_primaryAccession'] + if this_upac is None: + raise ValueError(f"Could not resolve UniProt primary accession for {upid}") else: self.log.info("retrieving UniProt ID for human gene %s" % gene_id) try: @@ -264,9 +266,12 @@ def get_sequence(self, gene_id, upid=None, upac=None, isoform=None): self.log.info("will use Uniprot ID %s" % this_upid) this_upac = self._get_aliases(this_upid, ['UniProtKB_primaryAccession'])['UniProtKB_primaryAccession'] - + if this_upac is None: + raise ValueError(f"Could not resolve UniProt primary accession for {this_upid}") if upid is None: this_upid = self._get_aliases(this_upac, ['UniProtKB_uniProtkbId'])['UniProtKB_uniProtkbId'] + if this_upid is None: + raise ValueError(f"Could not resolve UniProt ID for accession {this_upac}") else: this_upid = upid @@ -429,13 +434,25 @@ def _get_aliases(self, gene_id, to, fr='UniProtKB_AC-ID'): t_keyword = None t_query = t self.log.info("fetching alias, fr=%s, to=%s, query=%s" % (fr, t_query, gene_id)) - responses = self._uniprot_service.mapping( fr = fr, - to = t_query, - query = gene_id )['results'] + + mapping_result = self._uniprot_service.mapping( + fr=fr, + to=t_query, + query=gene_id) + + if mapping_result is None: + self.log.warning( + f"UniProt mapping returned no response for {gene_id} " + f"when requesting {t}") + out[t] = None + continue + + responses = mapping_result.get('results', []) if len(responses) == 0: self.log.warning(f"No {t} found for {gene_id}") - return None + out[t] = None + continue if len(responses) > 1: self.log.warning(f"Multiple {t} found for {gene_id}. Found {t}: {', '.join(response['to'] for response in responses)}. No {t} will be assigned.")