Skip to content

Commit

Permalink
Fix synonym type extraction in RDF/XML parser
Browse files Browse the repository at this point in the history
  • Loading branch information
althonos committed Feb 21, 2024
1 parent 7566b5c commit 51ef9b3
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
17 changes: 15 additions & 2 deletions pronto/parsers/rdfxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -802,14 +802,27 @@ def _process_axiom(self, elem: etree.Element, curies: Dict[str, str]):
)
return

type_ = elem.find(_NS["oboInOwl"]["hasSynonymType"])
# extract synonym type name or IRI
elem_type = elem.find(_NS["oboInOwl"]["hasSynonymType"])
if elem_type is None:
type_ = None
elif _NS["rdf"]["resource"] in elem_type.attrib:
type_iri = elem_type.attrib[_NS["rdf"]["resource"]]
type_ = curies.get(type_iri) or self._compact_id(type_iri)
else:
type_ = elem_type.text

try:
# recover existing synonym on the entity if there's one already
synonym = next(
s._data()
for s in entity.synonyms
if s.description == elem_target.text
and s.scope == _SYNONYMS[property]
)
# update synonym type of existing synonym if we got one
if type_ is not None:
synonym.type = type_
except StopIteration:
description = elem_target.get(_NS["rdf"]["resource"], elem_target.text)
if description is None:
Expand All @@ -822,7 +835,7 @@ def _process_axiom(self, elem: etree.Element, curies: Dict[str, str]):
synonym = SynonymData(
description,
scope=_SYNONYMS[property],
type=type_.text if type_ is not None else None,
type=type_,
)

entity._data().synonyms.add(typing.cast(SynonymData, synonym))
Expand Down
55 changes: 55 additions & 0 deletions tests/test_parser/test_rdfxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,3 +449,58 @@ def test_relationship_symmetric(self):
)
self.assertIn("TST:001", ont.relationships())
self.assertTrue(ont.get_relationship("TST:001").symmetric)

def test_existing_synonym_type_extraction(self):
ont = self.get_ontology(
"""
<owl:Ontology rdf:about="http://purl.obolibrary.org/obo/chebi.owl"/>
<owl:AnnotationProperty rdf:about="http://purl.obolibrary.org/obo/chebi#BRAND_NAME">
<rdfs:label rdf:datatype="http://www.w3.org/2001/XMLSchema#string">BRAND NAME</rdfs:label>
<rdfs:subPropertyOf rdf:resource="http://www.geneontology.org/formats/oboInOwl#SynonymTypeProperty"/>
</owl:AnnotationProperty>
<owl:Class rdf:about="http://purl.obolibrary.org/obo/CHEBI_4508">
<oboInOwl:hasRelatedSynonym rdf:datatype="http://www.w3.org/2001/XMLSchema#string">Cataflam</oboInOwl:hasRelatedSynonym>
<oboInOwl:id rdf:datatype="http://www.w3.org/2001/XMLSchema#string">CHEBI:4508</oboInOwl:id>
</owl:Class>
<owl:Axiom>
<owl:annotatedSource rdf:resource="http://purl.obolibrary.org/obo/CHEBI_4508"/>
<owl:annotatedProperty rdf:resource="http://www.geneontology.org/formats/oboInOwl#hasRelatedSynonym"/>
<owl:annotatedTarget rdf:datatype="http://www.w3.org/2001/XMLSchema#string">Cataflam</owl:annotatedTarget>
<oboInOwl:hasDbXref rdf:datatype="http://www.w3.org/2001/XMLSchema#string">DrugBank</oboInOwl:hasDbXref>
<oboInOwl:hasSynonymType rdf:resource="http://purl.obolibrary.org/obo/chebi#BRAND_NAME"/>
</owl:Axiom>
"""
)
syntype, = ont.metadata.synonymtypedefs
synonym, = ont["CHEBI:4508"].synonyms
self.assertEqual(synonym.scope, "RELATED")
self.assertEqual(synonym.type, syntype)

def test_missing_synonym_type_extraction(self):
ont = self.get_ontology(
"""
<owl:Ontology rdf:about="http://purl.obolibrary.org/obo/chebi.owl"/>
<owl:AnnotationProperty rdf:about="http://purl.obolibrary.org/obo/chebi#BRAND_NAME">
<rdfs:label rdf:datatype="http://www.w3.org/2001/XMLSchema#string">BRAND NAME</rdfs:label>
<rdfs:subPropertyOf rdf:resource="http://www.geneontology.org/formats/oboInOwl#SynonymTypeProperty"/>
</owl:AnnotationProperty>
<owl:Class rdf:about="http://purl.obolibrary.org/obo/CHEBI_4508">
<oboInOwl:id rdf:datatype="http://www.w3.org/2001/XMLSchema#string">CHEBI:4508</oboInOwl:id>
</owl:Class>
<owl:Axiom>
<owl:annotatedSource rdf:resource="http://purl.obolibrary.org/obo/CHEBI_4508"/>
<owl:annotatedProperty rdf:resource="http://www.geneontology.org/formats/oboInOwl#hasRelatedSynonym"/>
<owl:annotatedTarget rdf:datatype="http://www.w3.org/2001/XMLSchema#string">Cataflam</owl:annotatedTarget>
<oboInOwl:hasDbXref rdf:datatype="http://www.w3.org/2001/XMLSchema#string">DrugBank</oboInOwl:hasDbXref>
<oboInOwl:hasSynonymType rdf:resource="http://purl.obolibrary.org/obo/chebi#BRAND_NAME"/>
</owl:Axiom>
"""
)
syntype, = ont.metadata.synonymtypedefs
synonym, = ont["CHEBI:4508"].synonyms
self.assertEqual(synonym.scope, "RELATED")
self.assertEqual(synonym.type, syntype)

0 comments on commit 51ef9b3

Please sign in to comment.