Skip to content

Commit

Permalink
fix(neo4jsbml): taking account "tag" value in Cypher query
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaume-gricourt committed Feb 21, 2023
1 parent b3fa43b commit 46e3b5f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/neo4jsbml/connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,9 @@ def create_nodes(self, nodes: List[snode.SNode]) -> None:
query = (
"MERGE (n:"
+ ":".join(node.labels)
+ ' {id:"'
+ node.id
+ '"}) ON CREATE SET n += '
+ " "
+ node.id_to_neo4j()
+ ") ON CREATE SET n += "
+ node.properties_to_neo4j()
+ " ON MATCH SET n += "
+ node.properties_to_neo4j()
Expand All @@ -161,13 +161,16 @@ def create_relationships(
query = (
"MATCH (a:"
+ rel.from_label
+ ' {id: $rel["from_id"]}) MATCH (b:'
+ " "
+ rel.id_to_neo4j(id='$rel["from_id"]')
+ ") MATCH (b:"
+ rel.to_label
+ ' {id: $rel["to_id"]}) MERGE (a)-[r:'
+ " "
+ rel.id_to_neo4j(id='$rel["to_id"]')
+ ") MERGE (a)-[r:"
+ rel.label
+ ']->(b) ON CREATE SET r += $rel["properties"] RETURN r'
)

res = session.run(
query,
rel=rel.to_dict(),
Expand Down
23 changes: 23 additions & 0 deletions src/neo4jsbml/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ class Entity(metaclass=ABCMeta):
clean_properties(self)
Remove empty values
id_to_neo4j(self) -> str
Format id to query Neo4j
properties_to_neo4j() -> str
Format properties to insert in query
"""
Expand Down Expand Up @@ -89,6 +92,26 @@ def add_property(self, label: str, value: str, overwrite: bool = True) -> None:
):
self.properties[label] = value

def id_to_neo4j(self, id: Optional[str] = None) -> str:
"""Format ids to insert in query
Parameters
----------
id: str (default: None
Return
------
str
"""
data = "{id: "
if id is None:
data += '"' + self.id + '"'
else:
data += id
if "tag" in self.properties.keys():
data += ', tag: "' + self.properties["tag"] + '"'
data += "}"
return data

def properties_to_neo4j(self) -> str:
"""Format properties to insert in query
Expand Down
3 changes: 3 additions & 0 deletions src/neo4jsbml/sbml.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,9 @@ def format_relationships(
"No method was found for entities: %s and %s, belongs to the relationships: %s"
% (from_label, to_label, arrow_rel.label)
)
if self.tag is not None:
for srel in res:
srel.add_property(label="tag", value=self.tag)
return res

def validate_id(self, value: Any) -> bool:
Expand Down

0 comments on commit 46e3b5f

Please sign in to comment.