Skip to content

Commit 59b9880

Browse files
authored
Merge pull request #62 from SynBioDex/feature-support_pubchem
Feature support pubchem
2 parents 70e0259 + 114ed2b commit 59b9880

File tree

5 files changed

+48
-4
lines changed

5 files changed

+48
-4
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from setuptools import setup, find_packages
22

33
setup(name='tyto',
4-
version='1.0',
4+
version='1.1',
55
description='Automatically generates Python symbols for ontology terms',
66
python_requires='>=3.6',
77
url='https://github.com/SynBioDex/tyto',

test/test_ontology.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,15 @@ def test_sbol3(self):
172172
self.assertEqual(SBOL3.reverseComplement, 'http://sbols.org/v3#reverseComplement')
173173
self.assertEqual(SBOL3.Component, 'http://sbols.org/v3#Component')
174174

175+
class TestPubChem(unittest.TestCase):
176+
177+
def test_pubchem(self):
178+
self.assertEqual(PubChem['LUDOX(R) CL-X colloidal silica, 45 wt. % suspension in H2O'], 'https://identifiers.org/pubchem.substance:24866361')
179+
self.assertEqual(PubChem.get_term_by_uri('https://identifiers.org/pubchem.substance:24866361'), 'LUDOX(R) CL-X colloidal silica, 45 wt. % suspension in H2O')
180+
with self.assertRaises(LookupError):
181+
# Ambiguous term matches many SIDs
182+
PubChem['water']
183+
175184

176185
if __name__ == '__main__':
177186
unittest.main()

tyto/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from .tyto import Ontology, URI, configure_cache_size
2-
from .endpoint import Ontobee, EBIOntologyLookupService
2+
from .endpoint import Ontobee, EBIOntologyLookupService, PubChemAPI
33
from .sbo import SBO
44
from .so import SO
55
from .ncit import NCIT
@@ -8,3 +8,4 @@
88
from .sbol2 import SBOL2
99
from .sbol3 import SBOL3
1010
from .edam import EDAM
11+
from .pubchem import PubChem

tyto/endpoint/endpoint.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,6 @@ def get_ontologies(self):
162162
n_ontologies = int(len(response) / 2)
163163
for uri, ontology_name in zip(response[:n_ontologies], response[n_ontologies:]):
164164
ontologies[uri] = ontology_name
165-
print(ontology_name, uri)
166-
print(len(response[:n_ontologies]), len(response[n_ontologies+1:]))
167165
return ontologies
168166

169167

@@ -418,6 +416,36 @@ def convert(self, response):
418416
def query(self, query):
419417
pass
420418

419+
class PUG_REST(RESTEndpoint):
420+
421+
def __init__(self):
422+
super().__init__('https://pubchem.ncbi.nlm.nih.gov/rest/pug/substance')
423+
424+
def get_term_by_uri(self, ontology: "Ontology", uri: str):
425+
if 'https://identifiers.org/pubchem.substance:' in uri:
426+
uri = uri.replace('https://identifiers.org/pubchem.substance:',
427+
'https://pubchem.ncbi.nlm.nih.gov/rest/pug/substance/sid/')
428+
get_query = f'{uri}/synonyms/JSON'
429+
response = requests.get(get_query)
430+
if response.status_code == 200:
431+
return response.json()['InformationList']['Information'][0]['Synonym'][0]
432+
if response.status_code == 404:
433+
return None
434+
raise urllib.error.HTTPError(get_query, response.status_code, response.reason, response.headers, None)
435+
436+
def get_uri_by_term(self, ontology: "Ontology", term: str):
437+
term = urllib.parse.quote(term)
438+
get_query = f'https://pubchem.ncbi.nlm.nih.gov/rest/pug/substance/name/{term}/sids/JSON'
439+
response = requests.get(get_query)
440+
if response.status_code == 200:
441+
response = response.json()
442+
if not response:
443+
return None
444+
if len(response['IdentifierList']['SID']) > 1:
445+
raise LookupError('Ambiguous term--more than one matching ID found')
446+
return f"https://identifiers.org/pubchem.substance:{response['IdentifierList']['SID'][0]}"
447+
raise urllib.error.HTTPError(get_query, response.status_code, response.reason, response.headers, None)
448+
421449

422450
Ontobee = OntobeeEndpoint()
423451
"""Endpoint instance representing Ontobee. Ontobee is the default linked data server for most OBO Foundry library ontologies, but is also been used for many non-OBO ontologies.
@@ -426,3 +454,5 @@ def query(self, query):
426454
EBIOntologyLookupService = EBIOntologyLookupServiceAPI()
427455
"""The Ontology Lookup Service (OLS) is a repository for biomedical ontologies that aims to provide a single point of access to the latest ontology versions. Hosted by the European Bioinformatics Institute
428456
"""
457+
458+
PubChemAPI = PUG_REST()

tyto/pubchem.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from . import Ontology, PubChemAPI
2+
3+
PubChem = Ontology(endpoints=[PubChemAPI])
4+

0 commit comments

Comments
 (0)