Skip to content
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

fix!: use correct representation of Coding object in mappings #457

Merged
merged 6 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ dependencies = [
"click",
"boto3",
"ga4gh.vrs==2.0.0a13",
"disease-normalizer~=0.7.0",
"disease-normalizer~=0.8.0",
]
dynamic = ["version"]

[project.optional-dependencies]
etl = [
"disease-normalizer[etl]~=0.7.0",
#"disease-normalizer[etl]~=0.7.0",
"owlready2",
"rdflib",
"wikibaseintegrator>=0.12.0",
Expand Down
51 changes: 23 additions & 28 deletions src/therapy/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
from typing import Any, TypeVar

from botocore.exceptions import ClientError
from disease.schemas import NAMESPACE_TO_SYSTEM_URI as DISEASE_NAMESPACE_TO_SYSTEM_URI
from disease.schemas import NamespacePrefix as DiseaseNamespacePrefix
from disease.query import get_concept_mapping as get_disease_concept_mapping
from ga4gh.core.models import (
Coding,
ConceptMapping,
Expand All @@ -23,7 +22,6 @@
from therapy.database import AbstractDatabase
from therapy.schemas import (
NAMESPACE_TO_SYSTEM_URI,
SYSTEM_URI_TO_NAMESPACE,
BaseNormalizationService,
HasIndication,
MatchesNormalized,
Expand Down Expand Up @@ -365,7 +363,7 @@ def _add_merged_meta(self, response: NormalizationService) -> NormalizationServi

sources = []
for m in therapy.mappings or []:
ns = SYSTEM_URI_TO_NAMESPACE.get(m.coding.system)
ns = m.coding.id.split(":")[0]
if ns in PREFIX_LOOKUP:
sources.append(PREFIX_LOOKUP[ns])

Expand Down Expand Up @@ -399,39 +397,42 @@ def _add_therapy(
:return: completed response object ready to return to user
"""

def _create_concept_mapping(
def _get_concept_mapping(
concept_id: str,
relation: Relation,
ns_to_system_uri: dict[str, str],
ns_prefix: NamespacePrefix | DiseaseNamespacePrefix,
) -> ConceptMapping:
"""Create concept mapping for therapy or disease identifier
"""Create concept mapping for identifier

``system`` will use OBO Foundry persistent URL (PURL), source homepage, or
namespace prefix, in that order of preference, if available.
``system`` will use system prefix URL, OBO Foundry persistent URL (PURL), or
source homepage, in that order of preference.

:param concept_id: Concept identifier represented as a curie
:param relation: SKOS mapping relationship, default is relatedMatch
:param ns_to_system_uri: Dictionary containing mapping from namespace to
system URI
:param ns_prefix: Namespace prefix enum
:return: Concept mapping for therapy or disease identifier
:raises ValueError: If source of concept ID is not a valid
``NamespacePrefix``
:return: Concept mapping for identifier
"""
source = concept_id.split(":")[0]
source, source_code = concept_id.split(":")

try:
source = ns_prefix(source)
source = NamespacePrefix(source)
except ValueError:
try:
source = ns_prefix(source.upper())
source = NamespacePrefix(source.upper())
except ValueError as e:
err_msg = f"Namespace prefix not supported: {source}"
raise ValueError(err_msg) from e

system = ns_to_system_uri.get(source, source)
if source == NamespacePrefix.CHEBI:
source_code = concept_id

return ConceptMapping(
coding=Coding(code=code(concept_id), system=system), relation=relation
coding=Coding(
id=concept_id,
code=code(source_code),
system=NAMESPACE_TO_SYSTEM_URI[source],
),
relation=relation,
)

therapy_obj = MappableConcept(
Expand All @@ -443,20 +444,16 @@ def _create_concept_mapping(

# mappings
mappings = [
_create_concept_mapping(
_get_concept_mapping(
concept_id=record["concept_id"],
relation=Relation.EXACT_MATCH,
ns_to_system_uri=NAMESPACE_TO_SYSTEM_URI,
ns_prefix=NamespacePrefix,
)
]
source_ids = record.get("xrefs", []) + record.get("associated_with", [])
mappings.extend(
_create_concept_mapping(
_get_concept_mapping(
concept_id=source_id,
relation=Relation.RELATED_MATCH,
ns_to_system_uri=NAMESPACE_TO_SYSTEM_URI,
ns_prefix=NamespacePrefix,
)
for source_id in source_ids
)
Expand Down Expand Up @@ -490,11 +487,9 @@ def _create_concept_mapping(

if indication.normalized_disease_id:
mappings = [
_create_concept_mapping(
get_disease_concept_mapping(
concept_id=indication.normalized_disease_id,
relation=Relation.RELATED_MATCH,
ns_to_system_uri=DISEASE_NAMESPACE_TO_SYSTEM_URI,
ns_prefix=DiseaseNamespacePrefix,
)
]
else:
Expand Down
73 changes: 38 additions & 35 deletions src/therapy/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from datetime import datetime
from enum import Enum, IntEnum
from types import MappingProxyType
from typing import Any, Literal

from ga4gh.core.models import MappableConcept
Expand Down Expand Up @@ -258,37 +259,39 @@ class NamespacePrefix(Enum):
WIKIDATA = "wikidata"


# Source to URI. Will use OBO Foundry persistent URL (PURL) or source homepage
NAMESPACE_TO_SYSTEM_URI: dict[NamespacePrefix, str] = {
NamespacePrefix.ATC: "https://www.who.int/tools/atc-ddd-toolkit/atc-classification/",
NamespacePrefix.CHEBI: "http://purl.obolibrary.org/obo/chebi.owl",
NamespacePrefix.CHEMBL: "https://www.ebi.ac.uk/chembl/",
NamespacePrefix.CHEMIDPLUS: "https://pubchem.ncbi.nlm.nih.gov/source/ChemIDplus",
NamespacePrefix.CASREGISTRY: "https://pubchem.ncbi.nlm.nih.gov/source/ChemIDplus",
NamespacePrefix.CVX: "https://www2a.cdc.gov/vaccines/iis/iisstandards/vaccines.asp?rpt=cvx",
NamespacePrefix.DRUGBANK: "https://go.drugbank.com",
NamespacePrefix.DRUGCENTRAL: "https://drugcentral.org",
NamespacePrefix.DRUGSATFDA_ANDA: "https://www.fda.gov/drugs/types-applications/abbreviated-new-drug-application-anda",
NamespacePrefix.DRUGSATFDA_NDA: "https://www.fda.gov/drugs/types-applications/new-drug-application-nda",
NamespacePrefix.HEMONC: "https://hemonc.org",
NamespacePrefix.INCHIKEY: "https://www.chemspider.com",
NamespacePrefix.IUPHAR_LIGAND: "https://www.guidetopharmacology.org/GRAC/LigandListForward",
NamespacePrefix.GUIDETOPHARMACOLOGY: "https://www.guidetopharmacology.org/GRAC/LigandListForward",
NamespacePrefix.MMSL: "https://www.nlm.nih.gov/research/umls/rxnorm/sourcereleasedocs/mmsl.html",
NamespacePrefix.MSH: "https://id.nlm.nih.gov/mesh/",
NamespacePrefix.NCIT: "http://purl.obolibrary.org/obo/ncit.owl",
NamespacePrefix.NDC: "https://dps.fda.gov/ndc",
NamespacePrefix.PUBCHEMCOMPOUND: "https://pubchem.ncbi.nlm.nih.gov/docs/compounds",
NamespacePrefix.PUBCHEMSUBSTANCE: "https://pubchem.ncbi.nlm.nih.gov/docs/substances",
NamespacePrefix.RXNORM: "https://www.nlm.nih.gov/research/umls/rxnorm/index.html",
NamespacePrefix.SPL: "https://www.fda.gov/industry/fda-data-standards-advisory-board/structured-product-labeling-resources",
NamespacePrefix.UMLS: "https://www.nlm.nih.gov/research/umls/index.html",
NamespacePrefix.UNII: "https://precision.fda.gov/uniisearch",
NamespacePrefix.UNIPROT: "https://www.uniprot.org",
NamespacePrefix.USP: "https://www.usp.org/health-quality-safety/compendial-nomenclature",
NamespacePrefix.VANDF: "https://www.nlm.nih.gov/research/umls/sourcereleasedocs/current/VANDF",
NamespacePrefix.WIKIDATA: "https://www.wikidata.org",
}
# Source to URI. Will use system prefix URL, OBO Foundry persistent URL (PURL) or source homepage
NAMESPACE_TO_SYSTEM_URI: MappingProxyType[NamespacePrefix, str] = MappingProxyType(
korikuzma marked this conversation as resolved.
Show resolved Hide resolved
{
NamespacePrefix.ATC: "https://atcddd.fhi.no/atc_ddd_index/?code=",
NamespacePrefix.CHEBI: "https://www.ebi.ac.uk/chebi/searchId.do?chebiId=",
NamespacePrefix.CHEMBL: "https://www.ebi.ac.uk/chembl/explore/compound/",
NamespacePrefix.CHEMIDPLUS: "https://commonchemistry.cas.org/detail?cas_rn=",
NamespacePrefix.CASREGISTRY: "https://commonchemistry.cas.org/detail?cas_rn=",
NamespacePrefix.CVX: "https://www2a.cdc.gov/vaccines/iis/iisstandards/vaccines.asp?rpt=cvx",
NamespacePrefix.DRUGBANK: "https://go.drugbank.com/drugs/",
NamespacePrefix.DRUGCENTRAL: "https://drugcentral.org/drugcard/",
NamespacePrefix.DRUGSATFDA_ANDA: "https://www.accessdata.fda.gov/scripts/cder/daf/index.cfm?event=overview.process&ApplNo=",
NamespacePrefix.DRUGSATFDA_NDA: "https://www.accessdata.fda.gov/scripts/cder/daf/index.cfm?event=overview.process&ApplNo=",
NamespacePrefix.HEMONC: "https://hemonc.org",
NamespacePrefix.INCHIKEY: "https://www.chemspider.com",
NamespacePrefix.IUPHAR_LIGAND: "https://www.guidetopharmacology.org/GRAC/LigandDisplayForward?ligandId=",
NamespacePrefix.GUIDETOPHARMACOLOGY: "https://www.guidetopharmacology.org/GRAC/LigandDisplayForward?ligandId=",
NamespacePrefix.MMSL: "https://www.nlm.nih.gov/research/umls/rxnorm/sourcereleasedocs/mmsl.html",
NamespacePrefix.MSH: "https://id.nlm.nih.gov/mesh/",
NamespacePrefix.NCIT: "https://ncit.nci.nih.gov/ncitbrowser/ConceptReport.jsp?dictionary=NCI_Thesaurus&code=",
NamespacePrefix.NDC: "https://dps.fda.gov/ndc/searchresult?selection=finished_product&content=PRODUCTNDC&type=",
NamespacePrefix.PUBCHEMCOMPOUND: "https://pubchem.ncbi.nlm.nih.gov/compound/",
NamespacePrefix.PUBCHEMSUBSTANCE: "https://pubchem.ncbi.nlm.nih.gov/substance/",
NamespacePrefix.RXNORM: "https://mor.nlm.nih.gov/RxNav/search?searchBy=RXCUI&searchTerm=",
NamespacePrefix.SPL: "https://www.fda.gov/industry/fda-data-standards-advisory-board/structured-product-labeling-resources",
NamespacePrefix.UMLS: "https://uts.nlm.nih.gov/uts/umls/concept/",
NamespacePrefix.UNII: "https://precision.fda.gov/uniisearch/srs/unii/",
NamespacePrefix.UNIPROT: "http://purl.uniprot.org/uniprot/",
NamespacePrefix.USP: "https://www.usp.org/health-quality-safety/compendial-nomenclature",
NamespacePrefix.VANDF: "https://www.nlm.nih.gov/research/umls/sourcereleasedocs/current/VANDF",
NamespacePrefix.WIKIDATA: "https://www.wikidata.org/wiki/",
}
)

# URI to source
SYSTEM_URI_TO_NAMESPACE = {
Expand Down Expand Up @@ -540,21 +543,21 @@ class NormalizationService(BaseNormalizationService):
{
"coding": {
"code": "2555",
"system": "https://www.nlm.nih.gov/research/umls/rxnorm/index.html",
"system": "https://mor.nlm.nih.gov/RxNav/search?searchBy=RXCUI&searchTerm=",
},
"relation": "exactMatch",
},
{
"coding": {
"code": "C376",
"system": "http://purl.obolibrary.org/obo/ncit.owl",
"system": "https://ncit.nci.nih.gov/ncitbrowser/ConceptReport.jsp?dictionary=NCI_Thesaurus&code=",
},
"relation": "relatedMatch",
},
{
"coding": {
"code": "15663-27-1",
"system": "https://pubchem.ncbi.nlm.nih.gov/source/ChemIDplus",
"system": "https://commonchemistry.cas.org/detail?cas_rn=",
},
"relation": "relatedMatch",
},
Expand Down Expand Up @@ -843,7 +846,7 @@ class SearchService(BaseModel):
"data_license": "CC0 1.0",
"data_license_url": "https://creativecommons.org/publicdomain/zero/1.0/",
"version": "5.1.10",
"data_url": "https://go.drugbank.com/releases/latest#open-data",
"data_url": "https://go.drugbank.com/drugs//releases/latest#open-data",
"rdp_url": "http://reusabledata.org/drugbank.html",
"data_license_attributes": {
"non_commercial": False,
Expand Down
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def pytest_collection_modifyitems(items):
When creating new test modules, be sure to add them here.
"""
MODULE_ORDER = [ # noqa: N806
"test_schemas",
"test_chembl",
"test_chemidplus",
"test_drugbank",
Expand Down
Loading