【问题标题】:Programatically creating edges in Neo4j with python使用 python 在 Neo4j 中以编程方式创建边缘
【发布时间】: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


    【解决方案1】:

    我有办法。

    如果我在 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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-08
      • 1970-01-01
      • 1970-01-01
      • 2023-03-07
      • 1970-01-01
      相关资源
      最近更新 更多