Skip to content
Open
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
89 changes: 58 additions & 31 deletions MSMetaEnhancer/libs/converters/web/BridgeDb.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ class BridgeDb(WebConverter):
def __init__(self, session):
super().__init__(session)
# service URLs
self.endpoints = {"BridgeDb": "https://webservice.bridgedb.org/Human/xrefs/"}
self.endpoints = {
"BridgeDb": "https://webservice.bridgedb.org/Human/xrefs/",
"BridgeDbSearch": "https://webservice.bridgedb.org/Human/search/",
}

self.codes = {
"hmdbid": "Ch",
Expand All @@ -21,49 +24,42 @@ def __init__(self, session):
"wikidataid": "Wd",
"chebiid": "Ce",
"keggid": "Ck",
"casno": "Ca",
"inchikey": "Ik",
}
self.identifiers = {
"PubChem-compound": "pubchemid",
"Cpc": "pubchemid",
"Chemspider": "chemspiderid",
"Cs": "chemspiderid",
"ChEBI": "chebiid",
"Ce": "chebiid",
"HMDB": "hmdbid",
"Ch": "hmdbid",
"Wikidata": "wikidataid",
"Wd": "wikidataid",
"KEGG Compound": "keggid",
"Ck": "keggid",
"CAS": "casno",
"Ca": "casno",
"InChIKey": "inchikey",
"Ik": "inchikey",
}

# generate top level methods defining allowed conversions
conversion_sources = list(self.codes.keys())
conversions = [
("hmdbid", "pubchemid", "from_hmdbid"),
("hmdbid", "chemspiderid", "from_hmdbid"),
("hmdbid", "wikidataid", "from_hmdbid"),
("hmdbid", "chebiid", "from_hmdbid"),
("hmdbid", "keggid", "from_hmdbid"),
("pubchemid", "hmdbid", "from_pubchemid"),
("pubchemid", "chemspiderid", "from_pubchemid"),
("pubchemid", "wikidataid", "from_pubchemid"),
("pubchemid", "chebiid", "from_pubchemid"),
("pubchemid", "keggid", "from_pubchemid"),
("chemspiderid", "hmdbid", "from_chemspiderid"),
("chemspiderid", "pubchemid", "from_chemspiderid"),
("chemspiderid", "wikidataid", "from_chemspiderid"),
("chemspiderid", "chebiid", "from_chemspiderid"),
("chemspiderid", "keggid", "from_chemspiderid"),
("wikidataid", "hmdbid", "from_wikidataid"),
("wikidataid", "pubchemid", "from_wikidataid"),
("wikidataid", "chemspiderid", "from_wikidataid"),
("wikidataid", "chebiid", "from_wikidataid"),
("wikidataid", "keggid", "from_wikidataid"),
("chebiid", "hmdbid", "from_chebiid"),
("chebiid", "pubchemid", "from_chebiid"),
("chebiid", "chemspiderid", "from_chebiid"),
("chebiid", "wikidataid", "from_chebiid"),
("chebiid", "keggid", "from_chebiid"),
("keggid", "hmdbid", "from_keggid"),
("keggid", "pubchemid", "from_keggid"),
("keggid", "chemspiderid", "from_keggid"),
("keggid", "wikidataid", "from_keggid"),
("keggid", "chebiid", "from_keggid"),
(source, target, f"from_{source}")
for source in conversion_sources
for target in conversion_sources
if source != target
]
conversions.extend(
[
("compound_name", target, "from_name")
for target in conversion_sources
]
)
self.create_top_level_conversion_methods(conversions)

async def from_hmdbid(self, hmdbid):
Expand Down Expand Up @@ -126,6 +122,37 @@ async def from_keggid(self, keggid):
args = f"{self.codes['keggid']}/{keggid}"
return await self.call_service(args)

async def from_casno(self, casno):
"""
Convert CAS number to all possible IDs using BridgeDb web service

:param casno: given CAS number
:return: obtained IDs
"""
args = f"{self.codes['casno']}/{casno}"
return await self.call_service(args)

async def from_inchikey(self, inchikey):
"""
Convert InChIKey to all possible IDs using BridgeDb web service

:param inchikey: given InChIKey
:return: obtained IDs
"""
args = f"{self.codes['inchikey']}/{inchikey}"
return await self.call_service(args)

async def from_name(self, name):
"""
Search identifiers by compound name using BridgeDb web service

:param name: given compound name
:return: obtained IDs
"""
response = await self.query_the_service("BridgeDbSearch", name)
if response:
return self.parse_attributes(response)

async def call_service(self, args):
response = await self.query_the_service("BridgeDb", args)
if response:
Expand Down
34 changes: 34 additions & 0 deletions tests/test_BridgeDB.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import mock
import pytest

from MSMetaEnhancer.libs.converters.web import BridgeDb
Expand Down Expand Up @@ -29,3 +30,36 @@ def test_format():
def test_get_conversions():
jobs = BridgeDb(None).get_conversion_functions()
assert ("wikidataid", "pubchemid", "BridgeDb") in jobs
assert ("casno", "inchikey", "BridgeDb") in jobs
assert ("compound_name", "pubchemid", "BridgeDb") in jobs


def test_parse_attributes_supports_system_codes():
converter = BridgeDb(None)
response = (
"HMDB0000001\tCh\n"
"962\tCpc\n"
"CHEBI:15365\tCe\n"
"50-78-2\tCa\n"
"BSYNRYMUTXBXSQ-UHFFFAOYSA-N\tIk\n"
)

assert converter.parse_attributes(response) == {
"hmdbid": "HMDB0000001",
"pubchemid": "962",
"chebiid": "CHEBI:15365",
"casno": "50-78-2",
"inchikey": "BSYNRYMUTXBXSQ-UHFFFAOYSA-N",
}


def test_from_name_uses_search_endpoint():
converter = BridgeDb(None)
converter.query_the_service = mock.AsyncMock(
return_value="HMDB0000001\tCh\n962\tCpc\n"
)

result = asyncio.run(converter.from_name("aspirin"))

converter.query_the_service.assert_called_once_with("BridgeDbSearch", "aspirin")
assert result == {"hmdbid": "HMDB0000001", "pubchemid": "962"}
7 changes: 7 additions & 0 deletions tests/test_CIR.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,23 @@
import pytest

from MSMetaEnhancer.libs.converters.web import CIR
from MSMetaEnhancer.libs.utils.Errors import ServiceNotAvailable, UnknownResponse
from tests.utils import wrap_with_session


@pytest.mark.dependency()
@pytest.mark.xfail(raises=(UnknownResponse, ServiceNotAvailable))
def test_service_available():
asyncio.run(wrap_with_session(CIR, "casno_to_smiles", ["7783-89-3"]))


@pytest.mark.dependency(depends=["test_service_available"])
def test_format():
try:
asyncio.run(wrap_with_session(CIR, "casno_to_smiles", ["7783-89-3"]))
except Exception as exc:
pytest.skip(f"CIR service unavailable: {exc}")

casno = "7783-89-3"
args = "{}/smiles?resolver=cas_number".format(casno)
response = asyncio.run(wrap_with_session(CIR, "query_the_service", ["CIR", args]))
Expand Down
Loading