Skip to content

Commit aa9fd95

Browse files
author
Oliver Scott
committed
Update cytoscape_vis.ipynb example (styling)
1 parent 6f83020 commit aa9fd95

1 file changed

Lines changed: 87 additions & 12 deletions

File tree

examples/cytoscape_vis.ipynb

Lines changed: 87 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
},
1616
{
1717
"cell_type": "code",
18-
"execution_count": 10,
18+
"execution_count": 1,
1919
"metadata": {},
2020
"outputs": [],
2121
"source": [
@@ -35,13 +35,13 @@
3535
},
3636
{
3737
"cell_type": "code",
38-
"execution_count": 27,
38+
"execution_count": 2,
3939
"metadata": {},
4040
"outputs": [
4141
{
4242
"data": {
4343
"application/vnd.jupyter.widget-view+json": {
44-
"model_id": "d19cd6f1a72444d0bdbb976f3722b5cf",
44+
"model_id": "1b3cafb2bea34d4f8c77ecd284a6be91",
4545
"version_major": 2,
4646
"version_minor": 0
4747
},
@@ -86,7 +86,7 @@
8686
},
8787
{
8888
"cell_type": "code",
89-
"execution_count": 6,
89+
"execution_count": 3,
9090
"metadata": {},
9191
"outputs": [],
9292
"source": [
@@ -108,13 +108,13 @@
108108
},
109109
{
110110
"cell_type": "code",
111-
"execution_count": 19,
111+
"execution_count": 4,
112112
"metadata": {},
113113
"outputs": [
114114
{
115115
"data": {
116116
"application/vnd.jupyter.widget-view+json": {
117-
"model_id": "c3de0d5f8fe04244901098bccc3e3138",
117+
"model_id": "ed1b4914099d4a00804fd6df811cab1c",
118118
"version_major": 2,
119119
"version_minor": 0
120120
},
@@ -148,13 +148,13 @@
148148
},
149149
{
150150
"cell_type": "code",
151-
"execution_count": 21,
151+
"execution_count": 5,
152152
"metadata": {},
153153
"outputs": [
154154
{
155155
"data": {
156156
"application/vnd.jupyter.widget-view+json": {
157-
"model_id": "c33f4f8843474fa08fe80ba27f3ce68a",
157+
"model_id": "ac18776655314fb5af4bb64c530e33a0",
158158
"version_major": 2,
159159
"version_minor": 0
160160
},
@@ -182,13 +182,13 @@
182182
},
183183
{
184184
"cell_type": "code",
185-
"execution_count": 22,
185+
"execution_count": 6,
186186
"metadata": {},
187187
"outputs": [
188188
{
189189
"data": {
190190
"application/vnd.jupyter.widget-view+json": {
191-
"model_id": "90a9b007ca4044019846c10545c78d9e",
191+
"model_id": "9b62090602234268a15aa01167f5ae2b",
192192
"version_major": 2,
193193
"version_minor": 0
194194
},
@@ -211,18 +211,20 @@
211211
"cell_type": "markdown",
212212
"metadata": {},
213213
"source": [
214+
"# Further interactivity using ipywidgets \n",
215+
"\n",
214216
"A user also has the ability to add further interactivity to the visualisations using the ipywidgets interact function, an example below shows an interactive visualisation where the user can select the molecule to display using a dropdown box and also update the layout."
215217
]
216218
},
217219
{
218220
"cell_type": "code",
219-
"execution_count": 26,
221+
"execution_count": 7,
220222
"metadata": {},
221223
"outputs": [
222224
{
223225
"data": {
224226
"application/vnd.jupyter.widget-view+json": {
225-
"model_id": "33ca354a1d0647dbbd12751259795121",
227+
"model_id": "bd68bd2e87a74c2a9bf0aa6824f3dad1",
226228
"version_major": 2,
227229
"version_minor": 0
228230
},
@@ -259,6 +261,79 @@
259261
" layout=layouts\n",
260262
");"
261263
]
264+
},
265+
{
266+
"cell_type": "markdown",
267+
"metadata": {},
268+
"source": [
269+
"# Style Customization\n",
270+
"\n",
271+
"The user also has the ability to customize various aspects of the widgets style. Fopr example the nodes can be coloured based on an attribute. The example shows how to colour scaffold nodes based on the 'hierarchy' attribute."
272+
]
273+
},
274+
{
275+
"cell_type": "code",
276+
"execution_count": 8,
277+
"metadata": {},
278+
"outputs": [
279+
{
280+
"data": {
281+
"application/vnd.jupyter.widget-view+json": {
282+
"model_id": "5ff71126406b4fad84a0eaeae7262566",
283+
"version_major": 2,
284+
"version_minor": 0
285+
},
286+
"text/plain": [
287+
"CytoscapeWidget(cytoscape_layout={'name': 'dagre', 'nodeSpacing': 50, 'edgeLengthVal': 50}, cytoscape_style=[{…"
288+
]
289+
},
290+
"metadata": {},
291+
"output_type": "display_data"
292+
}
293+
],
294+
"source": [
295+
"from scaffoldgraph.vis import color_scaffold_nodes_by_attribute\n",
296+
"from rdkit.Chem.Draw import rdMolDraw2D\n",
297+
"\n",
298+
"# Add a 'background-color' attribute to each scaffold node.\n",
299+
"# The cmap can either be a string or a matplotlib.cm.Colormap object.\n",
300+
"color_scaffold_nodes_by_attribute(tree, 'hierarchy', cmap='BuPu', label='color')\n",
301+
"\n",
302+
"# Create a style element for the visualizer.\n",
303+
"background_style = {\n",
304+
" \"selector\": \"node[color]\", # select nodes with an attribute 'color'\n",
305+
" \"style\": {\n",
306+
" \"background-color\": \"data(color)\", # set background-color from attribute.\n",
307+
" }\n",
308+
"}\n",
309+
"\n",
310+
"# We can also customize rdkit drawing options (need a transparent background).\n",
311+
"drawOpts = rdMolDraw2D.MolDrawOptions()\n",
312+
"drawOpts.clearBackground = False\n",
313+
"drawOpts.bondLineWidth = 4\n",
314+
"\n",
315+
"# Create a new visualizer with new options.\n",
316+
"vis = cytoscape.CytoscapeVisualizer(\n",
317+
" tree,\n",
318+
" refresh_images=True,\n",
319+
" rd_draw_options=drawOpts,\n",
320+
")\n",
321+
"\n",
322+
"# Append the style element to the visualizer.\n",
323+
"vis.style.append(background_style)\n",
324+
"\n",
325+
"# Create visualization.\n",
326+
"widget = vis.draw_for_scaffold('c1ccncc1')\n",
327+
"widget.layout.height = '600px'\n",
328+
"widget\n"
329+
]
330+
},
331+
{
332+
"cell_type": "code",
333+
"execution_count": null,
334+
"metadata": {},
335+
"outputs": [],
336+
"source": []
262337
}
263338
],
264339
"metadata": {

0 commit comments

Comments
 (0)