I'm trying to generate nodes and edges for some chemicals and associated reactions in neo4j with python but am hitting a problem with node/relationship creation...
My code...
from neo4j.v1 import GraphDatabase, basic_auth
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "password"))
session = driver.session()
def addReactionNeo4j(reagents, products, reaction):
cypher = "CREATE (%s:Reaction {RXNid:\"%s\", name:\"%s\"})" % (reaction[0],reaction[1],reaction[2])
session.run(cypher)
print cypher
for reagent in reagents:
cypher = "CREATE (%s:Molecule {CHMid: \"%s\", smiles:\"%s\"})" % (reagent[0], reagent[1], reagent[2])
session.run(cypher)
print cypher
cypher = "CREATE (%s)-[:REAGENT]->(%s)" %(reagent[0], reaction[0])
print cypher
session.run(cypher)
for product in products:
cypher = "CREATE (%s:Molecule {CHMid: \"%s\", smiles:\"%s\"})" % (product[0], product[1], product[2])
session.run(cypher)
print cypher
cypher = "CREATE (%s)-[:PRODUCT]->(%s)" %(reaction[0], product[0])
session.run(cypher)
print cypher
reagents1 = []
reagents1.append(["Chem2","nbutylamine","CCCCN"])
reagents1.append(["Chem3","butanoicAcid","CCCCOO"])
products1 =[]
products1.append(["Chem1","Nbutylbutanamide","CCCCNC(O)CCCC"])
reaction1 = ["Reaction1", "reaction1", "AmideFormation"]
addReactionNeo4j(reagents1, products1, reaction1)
session.close()
This code should write cypher code and execute it in Neo4j.
It gives the following cypher code as output
CREATE (Reaction1:Reaction {RXNid:"reaction1", name:"AmideFormation"})
CREATE (Chem2:Molecule {CHMid: "nbutylamine", smiles:"CCCCN"})
CREATE (Chem2)-[:REAGENT]->(Reaction1)
CREATE (Chem3:Molecule {CHMid: "butanoicAcid", smiles:"CCCCOO"})
CREATE (Chem3)-[:REAGENT]->(Reaction1)
CREATE (Chem1:Molecule {CHMid: "Nbutylbutanamide ", smiles:"CCCCNC(O)CCCC"})
CREATE (Reaction1)-[:PRODUCT]->(Chem1)
This code works as expected when pasted into Neo4J
But if I look the graph created by the code (which in theory should be executing the same cypher code) I get the nodes created but it has problems linking the nodes together with edges - leaving the nodes unconnected.
Can anyone advise me on what I am doing wrong ?
Thanks
Chris
I have a solution.
It appears if in neo4j I run
CREATE (Reaction1:Reaction {RXNid:"reaction1", name:"AmideFormation"})
CREATE (Chem2:Molecule {CHMid: "nbutylamine", smiles:"CCCCN"})
CREATE (Chem3:Molecule {CHMid: "butanoicAcid", smiles:"CCCCOO"})
CREATE (Chem1:Molecule {CHMid: "Nbutylbutanamide", smiles:"CCCCNC(O)CCCC"})
and then run ...
CREATE (Chem2)-[:REAGENT]->(Reaction1)
CREATE (Chem3)-[:REAGENT]->(Reaction1)
CREATE (Reaction1)-[:PRODUCT]->(Chem1)
I can replicate the problems with connecting nodes. However if I run them at the same time everything is fine.
Changing my code therefore to use MATCH and MERGE instead of simply create gets round the problem....
from neo4j.v1 import GraphDatabase, basic_auth
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "arthuc01"))
session = driver.session()
def addReactionNeo4j(reagents, products, reaction):
reagents = reagents1
products = products1
reaction = reaction1
cypher = "MERGE (%s:Reaction {RXNid:\"%s\", name:\"%s\"})" % (reaction[0],reaction[1],reaction[2])
session.run(cypher)
print cypher
for reagent in reagents:
#USe MERGE to only add reagent/product or reaction in already if not:
cypher = "MERGE (%s:Molecule {CHMid: \"%s\", smiles:\"%s\"})" % (reagent[0], reagent[1], reagent[2])
session.run(cypher)
print cypher
cypher = "MATCH (m:Molecule {CHMid: \"%s\"} ), (r:Reaction {RXNid:\"%s\"}) \
MERGE (m)-[:REAGENT]->(r)" %(reagent[0], reaction[0])
print cypher
session.run(cypher)
for product in products:
#Check if product in already if not:
cypher = "MERGE (%s:Molecule {CHMid: \"%s\", smiles:\"%s\"})" % (product[0], product[1], product[2])
print cypher
session.run(cypher)
cypher = "MATCH (m:Molecule {CHMid: \"%s\"}), (r:Reaction {RXNid:\"%s\"}) \
MERGE (r)-[:PRODUCT]->(m)" %(product[0], reaction[0])
print cypher
session.run(cypher)
# temp =""
# for cypher in nodes:
# temp = temp + cypher
# session.run(temp)
#
reagents1 = []
reagents1.append(["Chem2","Chem2","CCCCN"])
reagents1.append(["Chem3","Chem3","CCCCOO"])
products1 =[]
products1.append(["Chem1","Chem1","CCCCNC(O)CCCC"])
reaction1 = ["reaction1", "reaction1", "AmideFormation"]
addReactionNeo4j(reagents1, products1, reaction1)
session.close()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.