Skip to content

Commit

Permalink
Add SynonymReplacement (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
hrshdhgd authored Mar 29, 2024
1 parent c64c448 commit e3a139b
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 16 deletions.
40 changes: 25 additions & 15 deletions src/data/examples/examples.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,29 +59,39 @@ Test_6:
command_with_curie: remove synonym 'foo' for GO:0005634
command_with_uri: remove synonym 'foo' for http://purl.obolibrary.org/obo/GO_0005634
Test_7:
id: CHANGE:001
type: SynonymReplacement
old_value: '''cell nucleus'''
new_value: '''cell NUCLEUS'''
about_node: GO:0005634
about_node_representation: curie
command_with_curie: change synonym 'cell nucleus' for GO:0005634 to 'cell NUCLEUS'
command_with_uri: change synonym 'cell nucleus' for http://purl.obolibrary.org/obo/GO_0005634
to 'cell NUCLEUS'
Test_8:
id: CHANGE:001
type: AddNodeToSubset
about_node: GO:0005634
about_node_representation: curie
in_subset: foo
command_with_curie: add GO:0005634 to subset foo
command_with_uri: add http://purl.obolibrary.org/obo/GO_0005634 to subset foo
Test_8:
Test_9:
id: CHANGE:001
type: RemoveNodeFromSubset
about_node: GO:0005634
about_node_representation: curie
in_subset: foo
command_with_curie: remove GO:0005634 from subset foo
command_with_uri: remove http://purl.obolibrary.org/obo/GO_0005634 from subset foo
Test_9:
Test_10:
id: CHANGE:001
type: ClassCreation
about_node_representation: curie
node_id: GO:9999999
command_with_curie: create GO:9999999
command_with_uri: create http://purl.obolibrary.org/obo/GO_9999999
Test_10:
Test_11:
id: CHANGE:001
type: NodeCreation
about_node: GO:9999999
Expand All @@ -90,7 +100,7 @@ Test_10:
name: '''foo'''
command_with_curie: create node GO:9999999 'foo'
command_with_uri: TODO
Test_11:
Test_12:
id: CHANGE:001
type: EdgeCreation
object_type: curie
Expand All @@ -102,7 +112,7 @@ Test_11:
command_with_curie: create edge GO:0005634 BFO:0000050 GO:0009411
command_with_uri: create edge http://purl.obolibrary.org/obo/GO_0005634 http://purl.obolibrary.org/obo/BFO_0000050
http://purl.obolibrary.org/obo/GO_0009411
Test_12:
Test_13:
id: CHANGE:001
type: PlaceUnder
object_type: curie
Expand All @@ -114,7 +124,7 @@ Test_12:
command_with_curie: create edge GO:0005634 rdfs:subClassOf GO:0009411
command_with_uri: create edge http://purl.obolibrary.org/obo/GO_0005634 http://www.w3.org/2000/01/rdf-schema#subClassOf
http://purl.obolibrary.org/obo/GO_0009411
Test_13:
Test_14:
id: CHANGE:001
type: PredicateChange
old_value: BFO:0000050
Expand All @@ -130,7 +140,7 @@ Test_13:
object_representation: curie
command_with_curie: change relationship between GO:0005635 and GO:0005634 from BFO:0000050
to rdfs:subClassOf
Test_14:
Test_15:
id: CHANGE:001
type: PredicateChange
old_value: rdfs:subClassOf
Expand All @@ -149,7 +159,7 @@ Test_14:
command_with_uri: change relationship between http://purl.obolibrary.org/obo/GO_0005634
and http://purl.obolibrary.org/obo/GO_0043231 from http://www.w3.org/2000/01/rdf-schema#subClassOf
to http://purl.obolibrary.org/obo/BFO_0000050
Test_15:
Test_16:
id: CHANGE:001
type: RemoveUnder
object_type: curie
Expand All @@ -161,7 +171,7 @@ Test_15:
command_with_curie: delete edge GO:0005634 rdfs:subClassOf GO:0043231
command_with_uri: delete edge http://purl.obolibrary.org/obo/GO_0005634 http://www.w3.org/2000/01/rdf-schema#subClassOf
http://purl.obolibrary.org/obo/GO_0043231
Test_16:
Test_17:
id: CHANGE:001
type: EdgeDeletion
object_type: curie
Expand All @@ -172,7 +182,7 @@ Test_16:
predicate_type: curie
command_with_curie: delete edge GO:0005635 BFO:0000050 GO:0005634
command_with_uri: TODO
Test_17:
Test_18:
id: CHANGE:001
type: NodeDeepening
old_value: GO:0043231
Expand All @@ -186,15 +196,15 @@ Test_17:
new_object_type: curie
command_with_curie: deepen GO:0005739 from GO:0043231 to GO:0005634
command_with_uri: TODO
Test_18:
Test_19:
id: CHANGE:001
type: NewTextDefinition
new_value: '''this is dummy description'''
about_node: GO:0005635
about_node_representation: curie
command_with_curie: add definition 'this is dummy description' to GO:0005635
command_with_uri: add definition 'this is dummy description' to http://purl.obolibrary.org/obo/GO_0005635
Test_19:
Test_20:
id: CHANGE:001
type: NodeTextDefinitionChange
new_value: '''this is dummy description'''
Expand All @@ -203,21 +213,21 @@ Test_19:
command_with_curie: change definition of GO:0005635 to 'this is dummy description'
command_with_uri: change definition of http://purl.obolibrary.org/obo/GO_0005635
to 'this is dummy description'
Test_20:
Test_21:
id: CHANGE:001
type: RemoveTextDefinition
about_node: GO:0005634
about_node_representation: curie
command_with_curie: remove definition for GO:0005634
command_with_uri: remove definition for http://purl.obolibrary.org/obo/GO_0005634
Test_21:
Test_22:
id: CHANGE:001
type: NodeObsoletion
about_node: GO:0005634
about_node_representation: curie
command_with_curie: obsolete GO:0005634
command_with_uri: obsolete http://purl.obolibrary.org/obo/GO_0005634
Test_22:
Test_23:
id: CHANGE:001
type: NodeObsoletionWithDirectReplacement
about_node: GO:0005634
Expand Down
11 changes: 11 additions & 0 deletions src/docs/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ old_value: foo
about_node: GO:0005634
about_node_representation: curie
```
## Example: Replace synonym of a node.
Class: [`SynonymReplacement`](https://w3id.org/kgcl/SynonymReplacement) </br></br>Command: `change synonym 'cell nucleus' for GO:0005634 to 'cell NUCLEUS'`</br></br>YAML:
```
id: CHANGE:001
type: SynonymReplacement
old_value: cell nucleus
new_value: cell NUCLEUS
about_node: GO:0005634
about_node_representation: curie
```
## Example: Addition of a node to a subset.
Class: [`AddNodeToSubset`](https://w3id.org/kgcl/AddNodeToSubset) </br></br>Command: `add GO:0005634 to subset foo`</br></br>YAML:
Expand Down
6 changes: 5 additions & 1 deletion src/kgcl_schema/datamodel/kgcl.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Auto generated from kgcl.yaml by pythongen.py version: 0.0.1
# Generation date: 2024-03-08T08:41:57
# Generation date: 2024-03-29T11:44:23
# Schema: kgcl_schema
#
# id: https://w3id.org/kgcl
Expand Down Expand Up @@ -1573,6 +1573,7 @@ class SynonymReplacement(NodeSynonymChange):
id: Union[str, SynonymReplacementId] = None
old_value: Optional[str] = None
new_value: Optional[str] = None
qualifier: Optional[str] = None
has_textual_diff: Optional[Union[dict, "TextualDiff"]] = None

def __post_init__(self, *_: List[str], **kwargs: Dict[str, Any]):
Expand All @@ -1587,6 +1588,9 @@ def __post_init__(self, *_: List[str], **kwargs: Dict[str, Any]):
if self.new_value is not None and not isinstance(self.new_value, str):
self.new_value = str(self.new_value)

if self.qualifier is not None and not isinstance(self.qualifier, str):
self.qualifier = str(self.qualifier)

if self.has_textual_diff is not None and not isinstance(self.has_textual_diff, TextualDiff):
self.has_textual_diff = TextualDiff()

Expand Down
5 changes: 5 additions & 0 deletions src/kgcl_schema/grammar/kgcl.lark
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
| add_definition
| change_definition
| remove_definition
| change_synonym

rename : "rename" _WS [id _WS "from" _WS] old_label ["@" old_language] _WS ("to"|"as"|"->") _WS new_label ["@" new_language]

Expand All @@ -29,6 +30,8 @@ synonym_qualifier : /exact|narrow|broad|related/

remove_synonym: "remove" _WS "synonym" _WS synonym ["@" language] _WS "for" _WS entity

change_synonym: "change" _WS [synonym_qualifier _WS] "synonym" _WS synonym ["@" language] _WS "for" _WS entity _WS ("to"|"->") _WS new_synonym

delete : "delete" _WS entity

obsolete : "obsolete" _WS entity
Expand Down Expand Up @@ -62,6 +65,8 @@ id : ID

subset: ID
synonym : LABEL
old_synonym: LABEL
new_synonym: LABEL

subclass: _entity
superclass: _entity
Expand Down
17 changes: 17 additions & 0 deletions src/kgcl_schema/grammar/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
RemoveUnder,
Session,
RemoveTextDefinition,
SynonymReplacement,
)
from kgcl_schema.datamodel.ontology_model import Edge
from kgcl_schema.utils import to_json, to_rdf, to_yaml
Expand Down Expand Up @@ -138,6 +139,8 @@ def parse_statement(input: str) -> Change:
return parse_change_definition(tree, id)
elif command == "remove_definition":
return parse_remove_definition(tree, id)
elif command == "change_synonym":
return parse_change_synonym(tree, id)
else:
raise NotImplementedError("No implementation for KGCL command: " + command)

Expand Down Expand Up @@ -223,6 +226,20 @@ def parse_remove_synonym(tree, id):
language=language_token,
)

def parse_change_synonym(tree, id):
"""Change the synonym of a class."""
entity_token = extract(tree, "entity")
old_value = extract(tree, "synonym")
new_value = extract(tree, "new_synonym")
entity, representation = get_entity_representation(entity_token)
return SynonymReplacement(
id=id,
about_node=entity,
about_node_representation=representation,
new_value=new_value,
old_value=old_value,
)


def parse_create_class(tree, id):
"""Create new class."""
Expand Down
33 changes: 33 additions & 0 deletions src/kgcl_schema/grammar/render_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
RemoveTextDefinition,
RemoveUnder,
Change,
SynonymReplacement,
)
from lark.lexer import Token

Expand Down Expand Up @@ -231,6 +232,38 @@ def render(kgcl_instance: Change) -> str:
# else:
return "remove synonym " + synonym + " for " + subject

if type(kgcl_instance) is SynonymReplacement:
subject = render_entity(kgcl_instance.about_node, "uri")
old_synonym = render_entity(kgcl_instance.old_value, "label")
new_synonym = render_entity(kgcl_instance.new_value, "label")
qualifier = kgcl_instance.qualifier
language = kgcl_instance.language

if language is not None:
old_synonym = old_synonym + "@" + language
new_synonym = new_synonym + "@" + language

if qualifier is not None:
return (
"change "
+ qualifier
+ " synonym "
+ old_synonym
+ " for "
+ subject
+ " to "
+ new_synonym
)
else:
return (
"change synonym "
+ old_synonym
+ " for "
+ subject
+ " to "
+ new_synonym
)

if type(kgcl_instance) is PlaceUnder:
subclass = render_entity(kgcl_instance.subject, "uri")
superclass = render_entity(kgcl_instance.object, "uri")
Expand Down
1 change: 1 addition & 0 deletions src/kgcl_schema/schema/kgcl.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,7 @@ classes:
slots:
- old_value
- new_value
- qualifier
- has_textual_diff
SynonymPredicateChange:
aliases:
Expand Down
13 changes: 13 additions & 0 deletions tests/cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
NodeDeepening,
AddNodeToSubset,
RemoveNodeFromSubset,
SynonymReplacement,
)
from kgcl_schema.datamodel.ontology_model import Edge

Expand Down Expand Up @@ -148,6 +149,18 @@
),
None,
),
(
f"change synonym 'cell nucleus' for {NUCLEUS} to 'cell NUCLEUS'",
f"change synonym 'cell nucleus' for {NUCLEUS_URI} to 'cell NUCLEUS'",
SynonymReplacement(
id=UID,
about_node=NUCLEUS,
about_node_representation="curie",
old_value="'cell nucleus'",
new_value="'cell NUCLEUS'",
),
None,
),
(
f"add {NUCLEUS} to subset foo",
f"add {NUCLEUS_URI} to subset foo",
Expand Down
1 change: 1 addition & 0 deletions tests/test_case_dumpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"NewTextDefinition": "New definition of a node.",
"NodeTextDefinitionChange": "Update defintion of a node.",
"RemoveTextDefinition": "Remove definition of a node.",
"SynonymReplacement": "Replace synonym of a node.",
}


Expand Down

0 comments on commit e3a139b

Please sign in to comment.