Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create export modules for cytoscape and neo4j, and add node types to the networkx graph #36

Merged
merged 3 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/pyBiodatafuse/graph/cytoscape.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# coding: utf-8

"""Python module to export a NetworkX graph to cytoscape-compliant format and set the styling for cytoscape."""

import json

import networkx as nx


def replace_graph_attrs_for_cytoscape(g: nx.MultiDiGraph):
"""Adapt the node and edge attributes keys to the cytoscape json structure.

:param g: input NetworkX graph object.
:returns: output NetworkX graph object.
"""
for node in list(g.nodes()):
for k, v in list(g.nodes[node].items()):
if k == "labels":
nx.set_node_attributes(g, {node: {"name": v}})
del g.nodes[node]["labels"]
elif k == "source":
nx.set_node_attributes(g, {node: {"datasource": v}})
del g.nodes[node]["source"]

for u, v, k in list(g.edges(keys=True)):
for x, y in list(g[u][v][k].items()):
if x == "label":
g[u][v][k]["interaction"] = y
del g[u][v][k]["label"]

return g


def convert_graph_to_cytoscape_json(g: nx.MultiDiGraph):
"""Convert a NetworkX graph to cytoscape json file.

:param g: the NetworkX graph object.
:returns: a cytoscape network json object.
"""
adj_g = replace_graph_attrs_for_cytoscape(g)

cytoscape_graph = nx.cytoscape_data(adj_g)

return cytoscape_graph


def write_cytoscape_graph_to_file(cytoscape_graph: dict, output_path: str):
"""Write cytoscape graph to json file.

:param cytoscape_graph: the cytoscape graph object.
:param output_path: the output path.
"""
with open(output_path, "w") as out:
json.dump(cytoscape_graph, out)
14 changes: 13 additions & 1 deletion src/pyBiodatafuse/graph/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def add_disgenet_disease_subgraph(g, gene_node_label, annot_list):
"source": "DisGeNET",
"labels": dg["disease_name"],
"id": dg["diseaseid"],
"node_type": "disease",
"disease_id": dg["diseaseid"],
"disease_class": dg["disease_class"],
"disease_class_name": dg["disease_class_name"],
Expand Down Expand Up @@ -75,6 +76,7 @@ def add_opentargets_location_subgraph(g, gene_node_label, annot_list):
"source": "OpenTargets",
"labels": loc["location"],
"id": loc["loc_identifier"],
"node_type": "location",
"subcellular_loc": loc["subcellular_loc"],
}

Expand All @@ -97,7 +99,12 @@ def add_opentargets_go_subgraph(g, gene_node_label, annot_list):
"""
for go in annot_list:
go_node_label = go["go_name"]
go_node_attrs = {"source": "OpenTargets", "labels": go["go_name"], "id": go["go_id"]}
go_node_attrs = {
"source": "OpenTargets",
"labels": go["go_name"],
"id": go["go_id"],
"node_type": "gene ontology",
}

g.add_node(go_node_label, attr_dict=go_node_attrs)

Expand All @@ -123,6 +130,7 @@ def add_opentargets_pathway_subgraph(g, gene_node_label, annot_list):
"source": "OpenTargets",
"labels": pathway["pathway_name"],
"id": pathway["pathway_id"],
"node_type": "reactome pathways",
}

g.add_node(pathway_node_label, attr_dict=pathway_node_attrs)
Expand All @@ -149,6 +157,7 @@ def add_opentargets_drug_subgraph(g, gene_node_label, annot_list):
"source": "OpenTargets",
"labels": drug["drug_name"],
"id": drug["chembl_id"],
"node_type": "drug interactions",
}

g.add_node(drug_node_label, attr_dict=drug_node_attrs)
Expand All @@ -175,6 +184,7 @@ def add_opentargets_disease_subgraph(g, gene_node_label, annot_list):
"source": "OpenTargets",
"labels": dg["disease_name"],
"id": dg["disease_id"],
"node_type": "disease",
"therapeutic_areas": dg["therapeutic_areas"],
}

Expand Down Expand Up @@ -202,6 +212,7 @@ def add_wikipathways_subgraph(g, gene_node_label, annot_list):
"source": "WikiPathways",
"labels": pathway["pathwayLabel"],
"id": pathway["pathwayId"],
"node_type": "wikipathways pathway",
"gene_count": pathway["pathwayGeneCount"],
}

Expand Down Expand Up @@ -244,6 +255,7 @@ def generate_networkx_graph(fuse_df: pd.DataFrame):
"source": "BridgeDB",
"labels": row["identifier"],
"id": row["target"],
"node_type": "gene",
row["target.source"]: row["target"],
}

Expand Down
14 changes: 14 additions & 0 deletions src/pyBiodatafuse/graph/neo4j.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# coding: utf-8

"""Python module to export a NetworkX graph to cytoscape-compliant format and set the styling for cytoscape."""

import networkx as nx


def write_graph_to_neo4j_graphml(g: nx.MultiDiGraph, output_path: str):
"""Convert a NetworkX graph to Neo4J graphml file.

:param g: the NetworkX graph object.
:param output_path: the output path of the graphml file
"""
nx.write_graphml(g, output_path, named_key_ids=True)
Loading