11import os
22import logging
33import requests
4- from lxml import etree
54from functools import lru_cache , cmp_to_key
65from indra .util import read_unicode_csv
76from indra .databases .obo_client import OboClient
109
1110logger = logging .getLogger (__name__ )
1211
13- # Namespaces used in the XML
14- chebi_xml_ns = {'n' : 'http://schemas.xmlsoap.org/soap/envelope/' ,
15- 'c' : 'https://www.ebi.ac.uk/webservices/chebi' }
16-
1712
1813def _add_prefix (chid ):
1914 if chid and not chid .startswith ('CHEBI:' ):
@@ -162,29 +157,17 @@ def get_chebi_entry_from_web(chebi_id):
162157
163158 Returns
164159 -------
165- xml.etree.ElementTree.Element
166- An ElementTree element representing the ChEBI entry.
160+ dict
161+ A dictionary containing the ChEBI entry data. If the lookup
162+ fails, None is returned.
167163 """
168- url_base = 'http://www.ebi.ac.uk/webservices/chebi/2.0/test/'
169- url_fmt = url_base + 'getCompleteEntity?chebiId=%s'
164+ url_fmt = 'https://www.ebi.ac.uk/chebi/backend/api/public/compound/%s/'
170165 resp = requests .get (url_fmt % chebi_id )
171166 if resp .status_code != 200 :
172167 logger .warning ("Got bad code form CHEBI client: %s" % resp .status_code )
173168 return None
174- tree = etree .fromstring (resp .content )
175- path = 'n:Body/c:getCompleteEntityResponse/c:return'
176- elem = tree .find (path , namespaces = chebi_xml_ns )
177- return elem
178-
179-
180- def _get_chebi_value_from_entry (entry , key ):
181- if entry is None :
182- return None
183- path = 'c:%s' % key
184- elem = entry .find (path , namespaces = chebi_xml_ns )
185- if elem is not None :
186- return elem .text
187- return None
169+ data = resp .json ()
170+ return data
188171
189172
190173def get_chebi_name_from_id_web (chebi_id ):
@@ -201,8 +184,8 @@ def get_chebi_name_from_id_web(chebi_id):
201184 The name corresponding to the given ChEBI ID. If the lookup
202185 fails, None is returned.
203186 """
204- entry = get_chebi_entry_from_web (chebi_id )
205- return _get_chebi_value_from_entry ( entry , 'chebiAsciiName' )
187+ json_data = get_chebi_entry_from_web (chebi_id )
188+ return json_data . get ( 'name' ) if json_data else None
206189
207190
208191def get_inchi_key (chebi_id ):
@@ -219,8 +202,11 @@ def get_inchi_key(chebi_id):
219202 The InChIKey corresponding to the given ChEBI ID. If the lookup
220203 fails, None is returned.
221204 """
222- entry = get_chebi_entry_from_web (chebi_id )
223- return _get_chebi_value_from_entry (entry , 'inchiKey' )
205+ json_data = get_chebi_entry_from_web (chebi_id )
206+ if not json_data :
207+ return None
208+ structure_data = json_data .get ('default_structure' )
209+ return structure_data .get ('standard_inchi_key' ) if structure_data else None
224210
225211
226212def get_primary_id (chebi_id ):
0 commit comments