使用python以编程方式在Neo4j中创建边缘

时间:2016-07-13 14:31:25

标签: python neo4j cypher graph-databases

我尝试使用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

时按预期工作

enter image description here

但是,如果我查看代码创建的图形(理论上应该执行相同的密码),我会创建节点,但是将节点与边缘连接起来有问题 - 让节点保持不连接状态。

enter image description here

任何人都可以告诉我我做错了什么吗?

由于

克里斯

1 个答案:

答案 0 :(得分:0)

我有一个解决方案。

如果我在neo4j中运行

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"})

然后运行......

CREATE (Chem2)-[:REAGENT]->(Reaction1)
CREATE (Chem3)-[:REAGENT]->(Reaction1)
CREATE (Reaction1)-[:PRODUCT]->(Chem1)

我可以复制连接节点的问题。但是,如果我同时运行它们一切都很好。

因此改变我的代码以使用MATCH和MERGE而不是简单的创建来解决问题......

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()