Implementation of Neo4j (Individual)

  • Install neo4j python driver
pip install neo4j
  • Import neo4j to current environment
from neo4j import GraphDatabase
  • Create a python driver
driver = GraphDatabase.driver(uri = "bolt://localhost:7687",\
                      auth = ("user","password"))
  • Clean the graph database, if using with pre-existing data
with driver.session() as session:
        session.run("MATCH (a) DETACH DELETE a")
  • Create nodes in the graph
for item in Data:
    with driver.session() as session:
        session.run("MERGE(a:Disease{ID: $ID}) "
                        "ON CREATE SET a.code = $code, \
                        a.title = $title, a.defn = $defn,\
                        a.syns = $syns, a.childs = $childs,\
                        a.parents = $parents",
            ID=item['id'], code=item['code'], title=item['title'],\
            defn=item['defn'], syns=item['syns'], childs=item['childs'],\
            parents=item['parents'])
  • Create edge with relationship "parents"
with driver.session() as session:
    session.run("MATCH (a:Disease),(b:Disease) "
            "WHERE a.ID in b.parents "
            "MERGE (a)<-[r:Parent]-(b)")
  • Create edge with relationship "children"
with driver.session() as session:
    session.run("MATCH (a:Disease),(b:Disease) "
            "WHERE a.ID in b.childs "
            "MERGE (a)<-[r:Child]-(b)")