【发布时间】:2016-07-13 14:31:25
【问题描述】:
我正在尝试使用 python 在 neo4j 中为某些化学物质和相关反应生成节点和边缘,但在创建节点/关系时遇到了问题...
我的代码...
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()
此代码应编写密码代码并在 Neo4j 中执行。
它给出以下密码作为输出
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)
此代码在粘贴到 Neo4J 时按预期工作
但是,如果我查看代码创建的图(理论上应该执行相同的密码代码),我会创建节点,但将节点与边连接在一起时存在问题 - 节点未连接。
谁能告诉我我做错了什么?
谢谢
克里斯
【问题讨论】:
标签: python neo4j cypher graph-databases