Skip to content

Commit

Permalink
Merge pull request #19 from SynBioDex/14-return_terms_in_english
Browse files Browse the repository at this point in the history
14 return terms in english
  • Loading branch information
bbartley authored Apr 19, 2021
2 parents ef099e3 + 8ed60d2 commit 7bb61ac
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 10 deletions.
8 changes: 8 additions & 0 deletions test/test_ontology.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,13 @@ def test_dynamic_ontology_attributes(self):
def test_NCIT(self):
self.assertEqual(NCIT.Growth_Medium, 'https://identifiers.org/ncit:C85504')

def test_OM(self):
self.assertEqual(OM.molar, OM.molair)
self.assertEqual(OM.liter, OM.liter)
self.assertEqual(OM.meter, OM.metre)
self.assertEqual(OM.get_term_by_uri('http://www.ontology-of-units-of-measure.org/resource/om-2/hour'),
'hour')


if __name__ == '__main__':
unittest.main()
45 changes: 35 additions & 10 deletions tyto/owlet.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ def _convert_ontobee_response(response):
response = response.convert() # Convert http response to JSON
for var, binding in zip(response['head']['vars'],
response['results']['bindings']):
converted_response.append(binding[var]['value'])
if var in binding:
converted_response.append(binding[var]['value'])
return converted_response

def _convert_rdflib_response(response):
Expand All @@ -107,12 +108,20 @@ def get_term_by_uri(self, uri):
SELECT distinct ?label
WHERE
{{{{
<{uri}> rdfs:label ?label .
optional
{{{{
<{uri}> rdfs:label ?label .
filter langMatches(lang(?label), "en")
}}}}
optional
{{{{
<{uri}> rdfs:label ?label .
}}}}
}}}}
'''.format(uri=uri)
error_msg = '{} not found'.format(uri)
response = self._query(query, error_msg)
return response[0]
return self._reverse_sanitize_term(response[0])

def get_uri_by_term(self, term):
'''
Expand All @@ -127,19 +136,25 @@ def get_uri_by_term(self, term):
# than underscores. This creates a problem when looking up terms by
# an attribute, e.g., SBO.systems_biology_representation
sanitized_term=self._sanitize_term(term)

query = '''
SELECT distinct ?uri
{{from_clause}}
WHERE
{{{{
{{{{?uri rdfs:label "{sanitized_term}"}}}} UNION
{{{{?uri rdfs:label "{sanitized_term}"^^xsd:string}}}} UNION
{{{{?uri rdfs:label "{sanitized_term}"@en}}}} UNION
{{{{?uri rdfs:label "{sanitized_term}"@nl}}}}
optional
{{{{
?uri rdfs:label "{sanitized_term}"@en
}}}}
optional
{{{{
?uri rdfs:label "{sanitized_term}"
}}}}
optional
{{{{
?uri rdfs:label "{sanitized_term}"^^xsd:string
}}}}
}}}}
'''.format(sanitized_term=sanitized_term)

error_msg = '{} not a valid ontology term'.format(term)
response = self._query(query, error_msg)[0]
return self._to_user(response)
Expand All @@ -160,6 +175,12 @@ def _sanitize_term(self, term):
# or changing camel-case to snake-case
return term

def _reverse_sanitize_term(self, term):
# Some Ontology instances may override this method to perform string
# manipulation of an ontology terms, for example, replacing spaces
# or changing camel-case to snake-case
return term

def get_ontology(self):
query = '''
SELECT distinct ?ontology_uri
Expand Down Expand Up @@ -199,6 +220,9 @@ def multi_replace(target_uri, old_namespaces, new_namespace):
'http://identifiers.org/so/SO:'],
'http://purl.obolibrary.org/obo/SO_')

SBO = Ontology(path=installation_path('ontologies/SBO_OWL.owl'),
endpoint='http://sparql.hegroup.org/sparql/',
uri='http://purl.obolibrary.org/obo/sbo.owl')
SBO = Ontology(path=installation_path('ontologies/SBO_OWL.owl'),
endpoint='http://sparql.hegroup.org/sparql/',
uri='http://purl.obolibrary.org/obo/sbo.owl')
Expand All @@ -225,7 +249,8 @@ def multi_replace(target_uri, old_namespaces, new_namespace):

OM = Ontology(path=installation_path('ontologies/om-2.0.rdf'),
endpoint=None)

OM._sanitize_term = lambda term: term.replace('liter', 'litre').replace('meter', 'metre').replace('molar', 'molair')
OM._reverse_sanitize_term = lambda term: term.replace('litre', 'liter').replace('metre', 'meter').replace('molair', 'molar')
'''
'http://purl.obolibrary.org/obo/SO_0000167'
'http://biomodels.net/SBO/SBO_0000241'
Expand Down

0 comments on commit 7bb61ac

Please sign in to comment.