Commit 915bd48c authored by Hervé  MENAGER's avatar Hervé MENAGER
Browse files

add web service clients and code to set ChEMBL and PubChem links

parent f37b06cc
Pipeline #31090 passed with stages
in 15 minutes and 3 seconds
......@@ -30,6 +30,9 @@ from .ws import (
get_go_info,
get_pfam_info,
get_doi_info,
get_chembl_id,
get_pubchem_id,
EntryNotFoundError,
)
......@@ -999,12 +1002,36 @@ class Compound(AutoFillableModel):
self.save()
def set_drugbank_link(self):
"""
Set Drugbank ID by looking up InChi Key
in the database
"""
drugbank_qs = DrugBankCompound.objects.filter(inchikey=self.inchikey)
if drugbank_qs.count() > 0:
self.drugbank_id = drugbank_qs[0].id
else:
self.drugbank_id = None
def set_chembl_link(self):
"""
Set ChEMBL ID by looking up InChi Key
with the ChEMBL web service
"""
try:
self.chembl_id = get_chembl_id(self.inchikey)
except EntryNotFoundError:
pass
def set_pubchem_link(self):
"""
Set PubChem ID by looking up InChi Key
with the PubChem web service
"""
try:
self.pubchem_id = get_pubchem_id(self.inchikey)
except EntryNotFoundError:
pass
def compute_fsp3(self):
"""
Compute FSP3 from CSP3 and number of carbons
......
......@@ -10,6 +10,7 @@ from bioservices.eutils import EUtils
from bioservices.uniprot import UniProt
from bs4 import BeautifulSoup
from requests.exceptions import ContentDecodingError
from chembl_webresource_client.new_client import new_client
class EntryNotFoundError(Exception):
......@@ -475,3 +476,47 @@ def convert_smiles_to_iupac(smiles: str) -> str:
return requests.get(
f"https://cactus.nci.nih.gov/chemical/structure/{smiles}/iupac_name"
).text
def get_chembl_id(inchikey: str) -> str:
"""
Retrieve the ChEMBL compound ID for a given
compound InChi Key
:param inchikey: InChi Key for the compound
:type inchikey: str
:return: the ChEMBL ID
:rtype: str
"""
molecule = new_client.molecule
try:
m = molecule.get(inchikey)
return m["molecule_chembl_id"]
except Exception as e:
raise EntryNotFoundError(inchikey, msg=str(e))
def get_pubchem_id(inchikey: str) -> str:
"""
Retrieve the PubChem compound ID for a given
compound InChi Key
Documentation for the corresponding web-service:
https://pubchemdocs.ncbi.nlm.nih.gov/pug-rest
:param inchikey: InChi Key for the compound
:type inchikey: str
:return: the PubChem ID
:rtype: str
"""
url = f"https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/inchikey/{inchikey}/json"
resp = requests.get(url)
json_data = resp.json()
if "Fault" in json_data:
raise EntryNotFoundError(
inchikey, msg=str(f"compound for inchikey {inchikey} not found in PubChem")
)
else:
try:
return str(json_data["PC_Compounds"][0]["id"]["id"]["cid"])
except KeyError as ke:
print(ke, json_data)
......@@ -15,6 +15,7 @@ xlwt
bioservices
bioblend
bs4
chembl_webresource_client
# libraries to compute PCA
scikit-learn==0.20.3 #later versions do not support Python<3.5
scipy==1.2.1 #Scipy >=1.3 does not support Python<3.5
......@@ -26,4 +27,4 @@ git+https://gitlab.pasteur.fr/hmenager/django-diu.git#egg=django_diu
# openbabel is not installed from pip for now
#openbabel
django-crispy-forms
celery
\ No newline at end of file
celery
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment