diff --git a/ippisite/ippidb/tests/tests.py b/ippisite/ippidb/tests/tests.py index 74aea8cb22520247df011c9d75423284cf683d64..bfb91f4a87c8314694069a59bc04cecc6de69612 100644 --- a/ippisite/ippidb/tests/tests.py +++ b/ippisite/ippidb/tests/tests.py @@ -822,3 +822,20 @@ class TestWS(TestCaseWithRequestsCache): ws.get_go_info('GO_0140999'), {'label': 'histone H3K4 trimethyltransferase activity'} ) + + def test_get_pubchem_id(self): + self.assertEqual( + ws.get_pubchem_id('ZRIKDHLPCRURPX-CSKARUKUSA-N'), + '24875307' + ) + + def test_get_ligand_id(self): + self.assertRaises( + EntryNotFoundError, + ws.get_ligand_id, + 'CC(C)C(=O)c1cc(C(=O)c2ccc(Oc3ccccc3)cc2)c(O)c(O)c1O', + ) + self.assertEqual( + ws.get_ligand_id('FC(F)(F)c1c(Sc2ccccc2OCc2cccnc2)ccc(C=CC(=O)N2CCOCC2)c1C(F)(F)F'), + 'BQN', + ) diff --git a/ippisite/ippidb/ws.py b/ippisite/ippidb/ws.py index 73b0fd5335e237e4dc0d8c21aa1556f7b0157222..7a7cd26cdd1cffde22955ef326bc30a9cc53928e 100644 --- a/ippisite/ippidb/ws.py +++ b/ippisite/ippidb/ws.py @@ -1,9 +1,9 @@ """ iPPI-DB web-service client utility functions """ +import json from typing import List -import xml.etree.ElementTree as ET from urllib import parse as urllib_parse import requests from bioservices import xmltools @@ -580,19 +580,33 @@ def get_pubchem_id(inchikey: str) -> str: def get_ligand_id(smiles: str) -> str: - endpoint = "http://www.rcsb.org/pdb/rest/smilesQuery?" - text = requests.get( - endpoint, params={"smiles": smiles, "search_type": "exact"} - ).text - doc = ET.fromstring(text) - ligandInfoEls = doc.findall("ligandInfo") - if len(ligandInfoEls) > 0: - for ligandEl in ligandInfoEls[0].findall("ligand"): - if "chemicalID" in ligandEl.attrib: - return ligandEl.attrib["chemicalID"] - raise EntryNotFoundError( - smiles, msg=str(f"ligand for smiles {smiles} not found in PDB Ligand") + endpoint = "https://search.rcsb.org/rcsbsearch/v2/query" + query = { + "query": { + "type": "terminal", + "service": "chemical", + "parameters": { + "value": smiles, + "type": "descriptor", + "descriptor_type": "SMILES", + "match_type": "graph-strict" + } + }, + "return_type": "mol_definition" + } + resp = requests.get( + endpoint, params={"json": json.dumps(query)} ) + if resp.text == '': + response = dict(total_count=0) + else: + response = resp.json() + if response['total_count'] == 0: + raise EntryNotFoundError( + smiles, msg=str(f"ligand for smiles {smiles} not found in PDB Ligand") + ) + results = sorted(response['result_set'], key=lambda x: -x['score']) + return results[0]['identifier'] def get_orcid_user_details(orcid: str) -> str: