Skip to content

Commit 51ef9b3

Browse files
committed
Fix synonym type extraction in RDF/XML parser
1 parent 7566b5c commit 51ef9b3

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed

pronto/parsers/rdfxml.py

+15-2
Original file line numberDiff line numberDiff line change
@@ -802,14 +802,27 @@ def _process_axiom(self, elem: etree.Element, curies: Dict[str, str]):
802802
)
803803
return
804804

805-
type_ = elem.find(_NS["oboInOwl"]["hasSynonymType"])
805+
# extract synonym type name or IRI
806+
elem_type = elem.find(_NS["oboInOwl"]["hasSynonymType"])
807+
if elem_type is None:
808+
type_ = None
809+
elif _NS["rdf"]["resource"] in elem_type.attrib:
810+
type_iri = elem_type.attrib[_NS["rdf"]["resource"]]
811+
type_ = curies.get(type_iri) or self._compact_id(type_iri)
812+
else:
813+
type_ = elem_type.text
814+
806815
try:
816+
# recover existing synonym on the entity if there's one already
807817
synonym = next(
808818
s._data()
809819
for s in entity.synonyms
810820
if s.description == elem_target.text
811821
and s.scope == _SYNONYMS[property]
812822
)
823+
# update synonym type of existing synonym if we got one
824+
if type_ is not None:
825+
synonym.type = type_
813826
except StopIteration:
814827
description = elem_target.get(_NS["rdf"]["resource"], elem_target.text)
815828
if description is None:
@@ -822,7 +835,7 @@ def _process_axiom(self, elem: etree.Element, curies: Dict[str, str]):
822835
synonym = SynonymData(
823836
description,
824837
scope=_SYNONYMS[property],
825-
type=type_.text if type_ is not None else None,
838+
type=type_,
826839
)
827840

828841
entity._data().synonyms.add(typing.cast(SynonymData, synonym))

tests/test_parser/test_rdfxml.py

+55
Original file line numberDiff line numberDiff line change
@@ -449,3 +449,58 @@ def test_relationship_symmetric(self):
449449
)
450450
self.assertIn("TST:001", ont.relationships())
451451
self.assertTrue(ont.get_relationship("TST:001").symmetric)
452+
453+
def test_existing_synonym_type_extraction(self):
454+
ont = self.get_ontology(
455+
"""
456+
<owl:Ontology rdf:about="http://purl.obolibrary.org/obo/chebi.owl"/>
457+
458+
<owl:AnnotationProperty rdf:about="http://purl.obolibrary.org/obo/chebi#BRAND_NAME">
459+
<rdfs:label rdf:datatype="http://www.w3.org/2001/XMLSchema#string">BRAND NAME</rdfs:label>
460+
<rdfs:subPropertyOf rdf:resource="http://www.geneontology.org/formats/oboInOwl#SynonymTypeProperty"/>
461+
</owl:AnnotationProperty>
462+
463+
<owl:Class rdf:about="http://purl.obolibrary.org/obo/CHEBI_4508">
464+
<oboInOwl:hasRelatedSynonym rdf:datatype="http://www.w3.org/2001/XMLSchema#string">Cataflam</oboInOwl:hasRelatedSynonym>
465+
<oboInOwl:id rdf:datatype="http://www.w3.org/2001/XMLSchema#string">CHEBI:4508</oboInOwl:id>
466+
</owl:Class>
467+
<owl:Axiom>
468+
<owl:annotatedSource rdf:resource="http://purl.obolibrary.org/obo/CHEBI_4508"/>
469+
<owl:annotatedProperty rdf:resource="http://www.geneontology.org/formats/oboInOwl#hasRelatedSynonym"/>
470+
<owl:annotatedTarget rdf:datatype="http://www.w3.org/2001/XMLSchema#string">Cataflam</owl:annotatedTarget>
471+
<oboInOwl:hasDbXref rdf:datatype="http://www.w3.org/2001/XMLSchema#string">DrugBank</oboInOwl:hasDbXref>
472+
<oboInOwl:hasSynonymType rdf:resource="http://purl.obolibrary.org/obo/chebi#BRAND_NAME"/>
473+
</owl:Axiom>
474+
"""
475+
)
476+
syntype, = ont.metadata.synonymtypedefs
477+
synonym, = ont["CHEBI:4508"].synonyms
478+
self.assertEqual(synonym.scope, "RELATED")
479+
self.assertEqual(synonym.type, syntype)
480+
481+
def test_missing_synonym_type_extraction(self):
482+
ont = self.get_ontology(
483+
"""
484+
<owl:Ontology rdf:about="http://purl.obolibrary.org/obo/chebi.owl"/>
485+
486+
<owl:AnnotationProperty rdf:about="http://purl.obolibrary.org/obo/chebi#BRAND_NAME">
487+
<rdfs:label rdf:datatype="http://www.w3.org/2001/XMLSchema#string">BRAND NAME</rdfs:label>
488+
<rdfs:subPropertyOf rdf:resource="http://www.geneontology.org/formats/oboInOwl#SynonymTypeProperty"/>
489+
</owl:AnnotationProperty>
490+
491+
<owl:Class rdf:about="http://purl.obolibrary.org/obo/CHEBI_4508">
492+
<oboInOwl:id rdf:datatype="http://www.w3.org/2001/XMLSchema#string">CHEBI:4508</oboInOwl:id>
493+
</owl:Class>
494+
<owl:Axiom>
495+
<owl:annotatedSource rdf:resource="http://purl.obolibrary.org/obo/CHEBI_4508"/>
496+
<owl:annotatedProperty rdf:resource="http://www.geneontology.org/formats/oboInOwl#hasRelatedSynonym"/>
497+
<owl:annotatedTarget rdf:datatype="http://www.w3.org/2001/XMLSchema#string">Cataflam</owl:annotatedTarget>
498+
<oboInOwl:hasDbXref rdf:datatype="http://www.w3.org/2001/XMLSchema#string">DrugBank</oboInOwl:hasDbXref>
499+
<oboInOwl:hasSynonymType rdf:resource="http://purl.obolibrary.org/obo/chebi#BRAND_NAME"/>
500+
</owl:Axiom>
501+
"""
502+
)
503+
syntype, = ont.metadata.synonymtypedefs
504+
synonym, = ont["CHEBI:4508"].synonyms
505+
self.assertEqual(synonym.scope, "RELATED")
506+
self.assertEqual(synonym.type, syntype)

0 commit comments

Comments
 (0)