Skip to content

Commit

Permalink
add fipronil notebook
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesamcl committed Sep 27, 2024
1 parent 25b3c19 commit 6d41110
Showing 1 changed file with 115 additions and 0 deletions.
115 changes: 115 additions & 0 deletions notebooks/fipronil.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 65,
"metadata": {},
"outputs": [],
"source": [
"import sys, os, io, json\n",
"from neo4j import GraphDatabase\n",
"from py2neo import Graph\n",
"from pathlib import Path\n",
"from pandas import DataFrame\n",
"import networkx as nx\n",
"\n",
"graph = Graph(\"bolt://localhost:7687\")\n",
"driver = GraphDatabase.driver('bolt://localhost:7687', auth=None)\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Get the neighbourhood of Fipronil as GraphML"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [],
"source": [
"df = DataFrame(graph.run(\"\"\"\n",
"WITH $query AS query\n",
"CALL apoc.export.graphml.query(query, null, {stream:true, useTypes:true, storeNodeIds:true, readLabels:true})\n",
"YIELD file, source, format, nodes, relationships, properties, time, rows, batchSize, batches, done, data\n",
"RETURN data;\n",
"\"\"\", {'query': \"\"\"\n",
"MATCH (other)-[r]-(fipronil:GraphNode)-[:id]->(id:Id { id: \\\"chebi:5063\\\" })\n",
"RETURN *\n",
"\"\"\"}).data())\n",
"\n",
"with open('fipronil.graphml', 'w') as f:\n",
" f.write(df['data'][0])\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Loading into networkx and writing Cytoscape JSON"
]
},
{
"cell_type": "code",
"execution_count": 79,
"metadata": {},
"outputs": [],
"source": [
"\n",
"## Helper function to run a Cypher query over a GrEBI graph and return a NetworkX graph from the results\n",
"##\n",
"def query_to_nx(query):\n",
" results = driver.session().run(query)\n",
" G = nx.MultiDiGraph()\n",
" for node in list(results.graph()._nodes.values()):\n",
" if not 'Id' in node._labels:\n",
" G.add_nodes_from([(node._properties['grebi:nodeId'], dict({\"grebi:type\": list(node._labels)}, **node._properties))])\n",
" for rel in list(results.graph()._relationships.values()):\n",
" if not 'Id' in rel.start_node._labels and not 'Id' in rel.end_node._labels:\n",
" G.add_edges_from([(rel.start_node._properties['grebi:nodeId'], rel.end_node._properties['grebi:nodeId'], rel._properties['edge_id'], dict({\"grebi:type\": rel.type}, **rel._properties))])\n",
" return G\n",
"\n",
"G = query_to_nx(\"\"\"\n",
"MATCH (other)-[r]-(fipronil:GraphNode)-[:id]->(id:Id { id: \\\"chebi:5063\\\" })\n",
"RETURN *\n",
"\"\"\")\n",
"\n",
"with open('fipronil.cyjs.json', 'w') as f:\n",
" cydata = nx.cytoscape_data(G)\n",
" for node in cydata['elements']['nodes']:\n",
" node['data']['ids'] = node['data']['id']\n",
" node['data']['id'] = node['data']['grebi:nodeId']\n",
" del node['data']['grebi:nodeId']\n",
" json.dump(cydata, f, indent=2)\n",
"\n",
"\n",
"\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit 6d41110

Please sign in to comment.