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

feat: add cytoscape export #75

Open
wants to merge 26 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
d512274
Added drug mode
rbasu101 Jun 28, 2024
7ad0244
Updated __add_node_attributes()
rbasu101 Jul 22, 2024
7c224e3
Quick ruff error fix
rbasu101 Jul 22, 2024
b66dc2a
(Another) ruff error fix
rbasu101 Jul 22, 2024
034b16f
Merge remote-tracking branch 'origin/main' into 46-graph_app-drug-sea…
rbasu101 Aug 2, 2024
390e905
Merge from main
rbasu101 Aug 2, 2024
64776a5
Fix merge from main
rbasu101 Aug 2, 2024
84e8f06
Implemented graph visualization with cytoscape
rbasu101 Aug 6, 2024
b828390
Full-feature integration with cytoscape
rbasu101 Aug 13, 2024
97d8d3b
rbasu101 Aug 15, 2024
69a5a18
refactor(ruff): make ruff fixes
rbasu101 Aug 29, 2024
cf55a1d
Merge branch 'main' into dash-cytoscape-integration
rbasu101 Aug 29, 2024
7ca0695
refactor(ruff): Make ruff fix
rbasu101 Aug 29, 2024
585c1fb
Merge branch 'main' into dash-cytoscape-integration
rbasu101 Aug 30, 2024
2e2d505
Revert "Merge branch 'main' into dash-cytoscape-integration"
rbasu101 Aug 30, 2024
dca1847
Part 1: Merge branch 'main' into dash-cytoscape-integration
rbasu101 Sep 20, 2024
374f836
Merge branch
rbasu101 Sep 20, 2024
8563b42
fix(merge): Fix interactions data in network_graph.py
rbasu101 Sep 20, 2024
5e9ee06
style(ruff): Make ruff fixes
rbasu101 Sep 20, 2024
72e317c
test: :bug: Fix merged test
rbasu101 Sep 20, 2024
8215c66
fix(merge): Fix improperly merged files
rbasu101 Sep 20, 2024
77f065c
feat: Add Image/JSON exporting to cytoscape
rbasu101 Oct 16, 2024
9867c75
feat: Add margin class to Export buttons
rbasu101 Oct 16, 2024
4085749
Merge branch 'main' into 72-image-exporting-from-cytoscape-graph
rbasu101 Oct 16, 2024
07b3108
Merge branch 'main' into 72-image-exporting-from-cytoscape-graph
rbasu101 Oct 21, 2024
c222385
feat: Add .svg cytoscape export
rbasu101 Oct 25, 2024
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
79 changes: 66 additions & 13 deletions src/dgipy/graph_app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Provides functionality to create a Dash web application for interacting with drug-gene data from DGIdb"""

import json

import dash_bootstrap_components as dbc
import dash_cytoscape as cyto
from dash import Input, Output, State, ctx, dash, dcc, html
Expand All @@ -8,6 +10,8 @@
from dgipy import network_graph as ng
from dgipy.data_utils import make_tabular

cyto.load_extra_layouts()


def generate_app() -> dash.Dash:
"""Initialize a Dash application object with a layout designed for visualizing: drug-gene interactions, options for user interactivity, and other visual elements.
Expand All @@ -32,6 +36,8 @@ def generate_app() -> dash.Dash:
_update_selected_element_text(app)
_update_neighbors_dropdown(app)
_update_edge_info(app)
_generate_image(app)
_generate_json(app)

return app

Expand Down Expand Up @@ -134,14 +140,43 @@ def _set_app_layout(app: dash.Dash) -> None:
style={"margin": "10px"},
),
dbc.Card(
dbc.CardBody(
[
html.H4("Selected Node/Edge:"),
html.P(selected_element_text),
html.H4("Selected Edge Info:"),
html.P(selected_edge_info),
]
),
[
dbc.CardHeader("Selection Info"),
dbc.CardBody(
[
html.H4("Selected Node/Edge:"),
html.P(selected_element_text),
html.H4("Selected Edge Info:"),
html.P(selected_edge_info),
]
),
],
style={"margin": "10px"},
),
dbc.Card(
[
dbc.CardHeader("Export Graph"),
dbc.CardBody(
[
dbc.Button(
"Export Graph as .png",
id="export-png-graph",
class_name="m-1",
),
dbc.Button(
"Export Graph as .svg",
id="export-svg-graph",
class_name="m-1",
),
dbc.Button(
"Export Graph as .json",
id="export-json-graph",
class_name="m-1",
),
dcc.Download(id="json-download"),
]
),
],
style={"margin": "10px"},
),
],
Expand Down Expand Up @@ -282,8 +317,26 @@ def update(selected_element: str | dict, selected_neighbor: str | None) -> str:
return "No Edge Selected"


def _get_node_data_from_id(nodes: list, node_id: str) -> dict | None:
for node in nodes:
if node["id"] == node_id:
return node
return None
def _generate_image(app: dash.Dash) -> None:
@app.callback(
Output("cytoscape-figure", "generateImage"),
[Input("export-png-graph", "n_clicks"), Input("export-svg-graph", "n_clicks")],
)
def update(export_png_graph: int, export_svg_graph: int) -> dict: # noqa: ARG001
if ctx.triggered_id == "export-png-graph":
return {"type": "png", "action": "download"}
if ctx.triggered_id == "export-svg-graph":
return {"type": "svg", "action": "download"}
return dash.no_update


def _generate_json(app: dash.Dash) -> None:
@app.callback(
Output("json-download", "data"),
Input("export-json-graph", "n_clicks"),
State("cytoscape-figure", "elements"),
)
def update(export_png_graph: int, cytoscape_figure: dict) -> dict: # noqa: ARG001
if ctx.triggered_id is None:
return dash.no_update
return dcc.send_string(json.dumps(cytoscape_figure, indent=4), "cyto.json")