diff --git a/test/test_ontology.py b/test/test_ontology.py index 6fd4640..e28045a 100644 --- a/test/test_ontology.py +++ b/test/test_ontology.py @@ -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() diff --git a/tyto/owlet.py b/tyto/owlet.py index e63b1ed..2f5d48b 100644 --- a/tyto/owlet.py +++ b/tyto/owlet.py @@ -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): @@ -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): ''' @@ -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) @@ -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 @@ -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') @@ -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'