{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercise 4.7: Making a stand-alone app for exploring parameters\n", "\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Recall that in our [lesson on dashboarding](l26_dashboards.ipynb), we built a dashboard investigating how fold change in gene expression varies with repressor copy number $R$ and inducer concentration $c$. The theoretical curve is\n", "\n", "\\begin{align}\n", "\\text{fold change} = \\left[1 + \\frac{\\frac{R}{K}\\left(1 + c/K_\\mathrm{d}^\\mathrm{A}\\right)^2}{\\left(1 + c/K_\\mathrm{d}^\\mathrm{A}\\right)^2 + K_\\mathrm{switch}\\left(1 + c/K_\\mathrm{d}^\\mathrm{I}\\right)^2}\\right]^{-1},\n", "\\end{align}\n", "\n", "with parameters given in the table below.\n", "\n", "|Parameter|Description|\n", "|:--:|:--:|\n", "|$K_\\mathrm{d}^\\mathrm{A}$|dissoc. const. for active repressor binding IPTG|\n", "|$K_\\mathrm{d}^\\mathrm{I}$|dissoc. const. for inactive repressor binding IPTG|\n", "|$K_\\mathrm{switch}$|equil. const. for switching active/inactive|\n", "|$K$|dissoc. const. for active repressor binding operator|\n", "|$R$|number of repressors in cell|\n", "\n", "Rebuild that dashboard, but use JavaScript callbacks so you can have full interactivity with a stand-alone HTML file." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Solution \n", "\n", "
" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " Loading BokehJS ...\n", "
" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "\n", "(function(root) {\n", " function now() {\n", " return new Date();\n", " }\n", "\n", " const force = true;\n", "\n", " if (typeof root._bokeh_onload_callbacks === \"undefined\" || force === true) {\n", " root._bokeh_onload_callbacks = [];\n", " root._bokeh_is_loading = undefined;\n", " }\n", "\n", " const JS_MIME_TYPE = 'application/javascript';\n", " const HTML_MIME_TYPE = 'text/html';\n", " const EXEC_MIME_TYPE = 'application/vnd.bokehjs_exec.v0+json';\n", " const CLASS_NAME = 'output_bokeh rendered_html';\n", "\n", " /**\n", " * Render data to the DOM node\n", " */\n", " function render(props, node) {\n", " const script = document.createElement(\"script\");\n", " node.appendChild(script);\n", " }\n", "\n", " /**\n", " * Handle when an output is cleared or removed\n", " */\n", " function handleClearOutput(event, handle) {\n", " const cell = handle.cell;\n", "\n", " const id = cell.output_area._bokeh_element_id;\n", " const server_id = cell.output_area._bokeh_server_id;\n", " // Clean up Bokeh references\n", " if (id != null && id in Bokeh.index) {\n", " Bokeh.index[id].model.document.clear();\n", " delete Bokeh.index[id];\n", " }\n", "\n", " if (server_id !== undefined) {\n", " // Clean up Bokeh references\n", " const cmd_clean = \"from bokeh.io.state import curstate; print(curstate().uuid_to_server['\" + server_id + \"'].get_sessions()[0].document.roots[0]._id)\";\n", " cell.notebook.kernel.execute(cmd_clean, {\n", " iopub: {\n", " output: function(msg) {\n", " const id = msg.content.text.trim();\n", " if (id in Bokeh.index) {\n", " Bokeh.index[id].model.document.clear();\n", " delete Bokeh.index[id];\n", " }\n", " }\n", " }\n", " });\n", " // Destroy server and session\n", " const cmd_destroy = \"import bokeh.io.notebook as ion; ion.destroy_server('\" + server_id + \"')\";\n", " cell.notebook.kernel.execute(cmd_destroy);\n", " }\n", " }\n", "\n", " /**\n", " * Handle when a new output is added\n", " */\n", " function handleAddOutput(event, handle) {\n", " const output_area = handle.output_area;\n", " const output = handle.output;\n", "\n", " // limit handleAddOutput to display_data with EXEC_MIME_TYPE content only\n", " if ((output.output_type != \"display_data\") || (!Object.prototype.hasOwnProperty.call(output.data, EXEC_MIME_TYPE))) {\n", " return\n", " }\n", "\n", " const toinsert = output_area.element.find(\".\" + CLASS_NAME.split(' ')[0]);\n", "\n", " if (output.metadata[EXEC_MIME_TYPE][\"id\"] !== undefined) {\n", " toinsert[toinsert.length - 1].firstChild.textContent = output.data[JS_MIME_TYPE];\n", " // store reference to embed id on output_area\n", " output_area._bokeh_element_id = output.metadata[EXEC_MIME_TYPE][\"id\"];\n", " }\n", " if (output.metadata[EXEC_MIME_TYPE][\"server_id\"] !== undefined) {\n", " const bk_div = document.createElement(\"div\");\n", " bk_div.innerHTML = output.data[HTML_MIME_TYPE];\n", " const script_attrs = bk_div.children[0].attributes;\n", " for (let i = 0; i < script_attrs.length; i++) {\n", " toinsert[toinsert.length - 1].firstChild.setAttribute(script_attrs[i].name, script_attrs[i].value);\n", " toinsert[toinsert.length - 1].firstChild.textContent = bk_div.children[0].textContent\n", " }\n", " // store reference to server id on output_area\n", " output_area._bokeh_server_id = output.metadata[EXEC_MIME_TYPE][\"server_id\"];\n", " }\n", " }\n", "\n", " function register_renderer(events, OutputArea) {\n", "\n", " function append_mime(data, metadata, element) {\n", " // create a DOM node to render to\n", " const toinsert = this.create_output_subarea(\n", " metadata,\n", " CLASS_NAME,\n", " EXEC_MIME_TYPE\n", " );\n", " this.keyboard_manager.register_events(toinsert);\n", " // Render to node\n", " const props = {data: data, metadata: metadata[EXEC_MIME_TYPE]};\n", " render(props, toinsert[toinsert.length - 1]);\n", " element.append(toinsert);\n", " return toinsert\n", " }\n", "\n", " /* Handle when an output is cleared or removed */\n", " events.on('clear_output.CodeCell', handleClearOutput);\n", " events.on('delete.Cell', handleClearOutput);\n", "\n", " /* Handle when a new output is added */\n", " events.on('output_added.OutputArea', handleAddOutput);\n", "\n", " /**\n", " * Register the mime type and append_mime function with output_area\n", " */\n", " OutputArea.prototype.register_mime_type(EXEC_MIME_TYPE, append_mime, {\n", " /* Is output safe? */\n", " safe: true,\n", " /* Index of renderer in `output_area.display_order` */\n", " index: 0\n", " });\n", " }\n", "\n", " // register the mime type if in Jupyter Notebook environment and previously unregistered\n", " if (root.Jupyter !== undefined) {\n", " const events = require('base/js/events');\n", " const OutputArea = require('notebook/js/outputarea').OutputArea;\n", "\n", " if (OutputArea.prototype.mime_types().indexOf(EXEC_MIME_TYPE) == -1) {\n", " register_renderer(events, OutputArea);\n", " }\n", " }\n", "\n", " \n", " if (typeof (root._bokeh_timeout) === \"undefined\" || force === true) {\n", " root._bokeh_timeout = Date.now() + 5000;\n", " root._bokeh_failed_load = false;\n", " }\n", "\n", " const NB_LOAD_WARNING = {'data': {'text/html':\n", " \"
\\n\"+\n", " \"

\\n\"+\n", " \"BokehJS does not appear to have successfully loaded. If loading BokehJS from CDN, this \\n\"+\n", " \"may be due to a slow or bad network connection. Possible fixes:\\n\"+\n", " \"

\\n\"+\n", " \"\\n\"+\n", " \"\\n\"+\n", " \"from bokeh.resources import INLINE\\n\"+\n", " \"output_notebook(resources=INLINE)\\n\"+\n", " \"\\n\"+\n", " \"
\"}};\n", "\n", " function display_loaded() {\n", " const el = document.getElementById(\"1002\");\n", " if (el != null) {\n", " el.textContent = \"BokehJS is loading...\";\n", " }\n", " if (root.Bokeh !== undefined) {\n", " if (el != null) {\n", " el.textContent = \"BokehJS \" + root.Bokeh.version + \" successfully loaded.\";\n", " }\n", " } else if (Date.now() < root._bokeh_timeout) {\n", " setTimeout(display_loaded, 100)\n", " }\n", " }\n", "\n", "\n", " function run_callbacks() {\n", " try {\n", " root._bokeh_onload_callbacks.forEach(function(callback) {\n", " if (callback != null)\n", " callback();\n", " });\n", " } finally {\n", " delete root._bokeh_onload_callbacks\n", " }\n", " console.debug(\"Bokeh: all callbacks have finished\");\n", " }\n", "\n", " function load_libs(css_urls, js_urls, callback) {\n", " if (css_urls == null) css_urls = [];\n", " if (js_urls == null) js_urls = [];\n", "\n", " root._bokeh_onload_callbacks.push(callback);\n", " if (root._bokeh_is_loading > 0) {\n", " console.debug(\"Bokeh: BokehJS is being loaded, scheduling callback at\", now());\n", " return null;\n", " }\n", " if (js_urls == null || js_urls.length === 0) {\n", " run_callbacks();\n", " return null;\n", " }\n", " console.debug(\"Bokeh: BokehJS not loaded, scheduling load and callback at\", now());\n", " root._bokeh_is_loading = css_urls.length + js_urls.length;\n", "\n", " function on_load() {\n", " root._bokeh_is_loading--;\n", " if (root._bokeh_is_loading === 0) {\n", " console.debug(\"Bokeh: all BokehJS libraries/stylesheets loaded\");\n", " run_callbacks()\n", " }\n", " }\n", "\n", " function on_error(url) {\n", " console.error(\"failed to load \" + url);\n", " }\n", "\n", " for (let i = 0; i < css_urls.length; i++) {\n", " const url = css_urls[i];\n", " const element = document.createElement(\"link\");\n", " element.onload = on_load;\n", " element.onerror = on_error.bind(null, url);\n", " element.rel = \"stylesheet\";\n", " element.type = \"text/css\";\n", " element.href = url;\n", " console.debug(\"Bokeh: injecting link tag for BokehJS stylesheet: \", url);\n", " document.body.appendChild(element);\n", " }\n", "\n", " for (let i = 0; i < js_urls.length; i++) {\n", " const url = js_urls[i];\n", " const element = document.createElement('script');\n", " element.onload = on_load;\n", " element.onerror = on_error.bind(null, url);\n", " element.async = false;\n", " element.src = url;\n", " console.debug(\"Bokeh: injecting script tag for BokehJS library: \", url);\n", " document.head.appendChild(element);\n", " }\n", " };\n", "\n", " function inject_raw_css(css) {\n", " const element = document.createElement(\"style\");\n", " element.appendChild(document.createTextNode(css));\n", " document.body.appendChild(element);\n", " }\n", "\n", " \n", " const js_urls = [\"https://cdn.bokeh.org/bokeh/release/bokeh-2.4.2.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-gl-2.4.2.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-widgets-2.4.2.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-tables-2.4.2.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-mathjax-2.4.2.min.js\"];\n", " const css_urls = [];\n", " \n", "\n", " const inline_js = [\n", " function(Bokeh) {\n", " Bokeh.set_log_level(\"info\");\n", " },\n", " function(Bokeh) {\n", " \n", " \n", " }\n", " ];\n", "\n", " function run_inline_js() {\n", " \n", " if (root.Bokeh !== undefined || force === true) {\n", " \n", " for (let i = 0; i < inline_js.length; i++) {\n", " inline_js[i].call(root, root.Bokeh);\n", " }\n", " if (force === true) {\n", " display_loaded();\n", " }} else if (Date.now() < root._bokeh_timeout) {\n", " setTimeout(run_inline_js, 100);\n", " } else if (!root._bokeh_failed_load) {\n", " console.log(\"Bokeh: BokehJS failed to load within specified timeout.\");\n", " root._bokeh_failed_load = true;\n", " } else if (force !== true) {\n", " const cell = $(document.getElementById(\"1002\")).parents('.cell').data().cell;\n", " cell.output_area.append_execute_result(NB_LOAD_WARNING)\n", " }\n", "\n", " }\n", "\n", " if (root._bokeh_is_loading === 0) {\n", " console.debug(\"Bokeh: BokehJS loaded, going straight to plotting\");\n", " run_inline_js();\n", " } else {\n", " load_libs(css_urls, js_urls, function() {\n", " console.debug(\"Bokeh: BokehJS plotting callback run at\", now());\n", " run_inline_js();\n", " });\n", " }\n", "}(window));" ], "application/vnd.bokehjs_load.v0+json": "\n(function(root) {\n function now() {\n return new Date();\n }\n\n const force = true;\n\n if (typeof root._bokeh_onload_callbacks === \"undefined\" || force === true) {\n root._bokeh_onload_callbacks = [];\n root._bokeh_is_loading = undefined;\n }\n\n \n\n \n if (typeof (root._bokeh_timeout) === \"undefined\" || force === true) {\n root._bokeh_timeout = Date.now() + 5000;\n root._bokeh_failed_load = false;\n }\n\n const NB_LOAD_WARNING = {'data': {'text/html':\n \"
\\n\"+\n \"

\\n\"+\n \"BokehJS does not appear to have successfully loaded. If loading BokehJS from CDN, this \\n\"+\n \"may be due to a slow or bad network connection. Possible fixes:\\n\"+\n \"

\\n\"+\n \"\\n\"+\n \"\\n\"+\n \"from bokeh.resources import INLINE\\n\"+\n \"output_notebook(resources=INLINE)\\n\"+\n \"\\n\"+\n \"
\"}};\n\n function display_loaded() {\n const el = document.getElementById(\"1002\");\n if (el != null) {\n el.textContent = \"BokehJS is loading...\";\n }\n if (root.Bokeh !== undefined) {\n if (el != null) {\n el.textContent = \"BokehJS \" + root.Bokeh.version + \" successfully loaded.\";\n }\n } else if (Date.now() < root._bokeh_timeout) {\n setTimeout(display_loaded, 100)\n }\n }\n\n\n function run_callbacks() {\n try {\n root._bokeh_onload_callbacks.forEach(function(callback) {\n if (callback != null)\n callback();\n });\n } finally {\n delete root._bokeh_onload_callbacks\n }\n console.debug(\"Bokeh: all callbacks have finished\");\n }\n\n function load_libs(css_urls, js_urls, callback) {\n if (css_urls == null) css_urls = [];\n if (js_urls == null) js_urls = [];\n\n root._bokeh_onload_callbacks.push(callback);\n if (root._bokeh_is_loading > 0) {\n console.debug(\"Bokeh: BokehJS is being loaded, scheduling callback at\", now());\n return null;\n }\n if (js_urls == null || js_urls.length === 0) {\n run_callbacks();\n return null;\n }\n console.debug(\"Bokeh: BokehJS not loaded, scheduling load and callback at\", now());\n root._bokeh_is_loading = css_urls.length + js_urls.length;\n\n function on_load() {\n root._bokeh_is_loading--;\n if (root._bokeh_is_loading === 0) {\n console.debug(\"Bokeh: all BokehJS libraries/stylesheets loaded\");\n run_callbacks()\n }\n }\n\n function on_error(url) {\n console.error(\"failed to load \" + url);\n }\n\n for (let i = 0; i < css_urls.length; i++) {\n const url = css_urls[i];\n const element = document.createElement(\"link\");\n element.onload = on_load;\n element.onerror = on_error.bind(null, url);\n element.rel = \"stylesheet\";\n element.type = \"text/css\";\n element.href = url;\n console.debug(\"Bokeh: injecting link tag for BokehJS stylesheet: \", url);\n document.body.appendChild(element);\n }\n\n for (let i = 0; i < js_urls.length; i++) {\n const url = js_urls[i];\n const element = document.createElement('script');\n element.onload = on_load;\n element.onerror = on_error.bind(null, url);\n element.async = false;\n element.src = url;\n console.debug(\"Bokeh: injecting script tag for BokehJS library: \", url);\n document.head.appendChild(element);\n }\n };\n\n function inject_raw_css(css) {\n const element = document.createElement(\"style\");\n element.appendChild(document.createTextNode(css));\n document.body.appendChild(element);\n }\n\n \n const js_urls = [\"https://cdn.bokeh.org/bokeh/release/bokeh-2.4.2.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-gl-2.4.2.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-widgets-2.4.2.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-tables-2.4.2.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-mathjax-2.4.2.min.js\"];\n const css_urls = [];\n \n\n const inline_js = [\n function(Bokeh) {\n Bokeh.set_log_level(\"info\");\n },\n function(Bokeh) {\n \n \n }\n ];\n\n function run_inline_js() {\n \n if (root.Bokeh !== undefined || force === true) {\n \n for (let i = 0; i < inline_js.length; i++) {\n inline_js[i].call(root, root.Bokeh);\n }\n if (force === true) {\n display_loaded();\n }} else if (Date.now() < root._bokeh_timeout) {\n setTimeout(run_inline_js, 100);\n } else if (!root._bokeh_failed_load) {\n console.log(\"Bokeh: BokehJS failed to load within specified timeout.\");\n root._bokeh_failed_load = true;\n } else if (force !== true) {\n const cell = $(document.getElementById(\"1002\")).parents('.cell').data().cell;\n cell.output_area.append_execute_result(NB_LOAD_WARNING)\n }\n\n }\n\n if (root._bokeh_is_loading === 0) {\n console.debug(\"Bokeh: BokehJS loaded, going straight to plotting\");\n run_inline_js();\n } else {\n load_libs(css_urls, js_urls, function() {\n console.debug(\"Bokeh: BokehJS plotting callback run at\", now());\n run_inline_js();\n });\n }\n}(window));" }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import numpy as np\n", "\n", "import bokeh.io\n", "import bokeh.layouts\n", "import bokeh.models\n", "import bokeh.plotting\n", "\n", "bokeh.io.output_notebook()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To build our app, we will first code up the functions to compute the fold change in Python so we can make the starting plot." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "def bohr_parameter(c, R, K, KdA, KdI, Kswitch):\n", " \"\"\"Compute Bohr parameter based on MWC model.\"\"\"\n", " # Big nasty argument of logarithm\n", " log_arg = (1 + c / KdA) ** 2 / ((1 + c / KdA) ** 2 + Kswitch * (1 + c / KdI) ** 2)\n", "\n", " return -np.log(R / K) - np.log(log_arg)\n", "\n", "\n", "def fold_change(c, R, K, KdA, KdI, Kswitch):\n", " \"\"\"Compute theoretical fold change for MWC model.\"\"\"\n", " return 1.0 / (1.0 + np.exp(-bohr_parameter(c, R, K, KdA, KdI, Kswitch)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next, we can make the sliders we want. We will again vary the parameters on a logarithmic scale, but will use a clever trick with the labels to we do not display the base ten logarithm of the parameter value, but rather the value itself. To do this, we use a `bokeh.models.FuncTickFormatter` to give a snippet of JavaScript code to display the value of the slider. We will raise ten to the value of the log slider and then display the resulting value with a precision of 3." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "slider_format = bokeh.models.FuncTickFormatter(\n", " code=\"return Math.pow(10, tick).toPrecision(3)\"\n", ")\n", "log_R_slider = bokeh.models.Slider(\n", " title=\"R (1/cell)\", start=0, end=3, step=0.1, value=2, format=slider_format,\n", ")\n", "log_K_slider = bokeh.models.Slider(\n", " title=\"K (1/cell)\", start=-6, end=3, step=0.1, value=0, format=slider_format,\n", ")\n", "log_KdA_slider = bokeh.models.Slider(\n", " title=\"KdA (1/mM)\", start=-6, end=3, step=0.1, value=-2, format=slider_format,\n", ")\n", "log_KdI_slider = bokeh.models.Slider(\n", " title=\"KdI (1/mM)\", start=-6, end=3, step=0.1, value=-2, format=slider_format,\n", ")\n", "log_Kswitch_slider = bokeh.models.Slider(\n", " title=\"Kswitch\", start=-3, end=6, step=0.1, value=1, format=slider_format,\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, let's build out plot, using a `ColumnDataSource` so we can manipulate the values." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "c = np.logspace(-6, 2, 200)\n", "fc = fold_change(\n", " c,\n", " 10 ** log_R_slider.value,\n", " 10 ** log_K_slider.value,\n", " 10 ** log_KdA_slider.value,\n", " 10 ** log_KdI_slider.value,\n", " 10 ** log_Kswitch_slider.value,\n", ")\n", "\n", "cds = bokeh.models.ColumnDataSource(dict(c=c, fc=fc))\n", "\n", "p = bokeh.plotting.figure(\n", " frame_height=250,\n", " frame_width=350,\n", " x_axis_label=\"[IPTG] (mM)\",\n", " y_axis_label=\"fold change\",\n", " x_range=[1e-6, 1e2],\n", " y_range=[-0.05, 1.05],\n", " x_axis_type=\"log\",\n", ")\n", "\n", "p.line(source=cds, x=\"c\", y=\"fc\", line_width=2);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next, we code up the JavaScript code to get triggered when the sliders change values. The JS code is pretty straightforward, directly following what we have seen in the lesson." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "jscode = \"\"\"\n", "function bohr_parameter(c, R, K, KdA, KdI, Kswitch){\n", " var log_arg = (1 + c / KdA) ** 2 / ((1 + c / KdA) ** 2 + Kswitch * (1 + c / KdI) ** 2);\n", "\n", " return -Math.log(R / K) - Math.log(log_arg);\n", "}\n", "\n", "function fold_change(c, R, K, KdA, KdI, Kswitch) {\n", " return 1 / (1 + Math.exp(-bohr_parameter(c, R, K, KdA, KdI, Kswitch)));\n", "}\n", "\n", "// For convenience, get views into the ColumnDataSource data\n", "var c = cds.data['c'];\n", "var fc = cds.data['fc'];\n", "\n", "// Pull the values off of the sliders\n", "var R = 10 ** log_R_slider.value;\n", "var K = 10 ** log_K_slider.value;\n", "var KdA = 10 ** log_KdA_slider.value;\n", "var KdI = 10 ** log_KdI_slider.value;\n", "var Kswitch = 10 ** log_Kswitch_slider.value;\n", "\n", "// Loop through and update\n", "var cLen = c.length;\n", "for (var i = 0; i < cLen; i++) {\n", " fc[i] = fold_change(c[i], R, K, KdA, KdI, Kswitch);\n", "}\n", "\n", "cds.change.emit();\n", "\"\"\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Finally, we link the code to the sliders as a `bokeh.models.CustomJS` instance, making sure to specify the arguments that the JS code needs." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "args = dict(\n", " cds=cds,\n", " log_R_slider=log_R_slider,\n", " log_K_slider=log_K_slider,\n", " log_KdA_slider=log_KdA_slider,\n", " log_KdI_slider=log_KdI_slider,\n", " log_Kswitch_slider=log_Kswitch_slider,\n", ")\n", "code = bokeh.models.CustomJS(code=jscode, args=args)\n", "log_R_slider.js_on_change(\"value\", code)\n", "log_K_slider.js_on_change(\"value\", code)\n", "log_KdA_slider.js_on_change(\"value\", code)\n", "log_KdI_slider.js_on_change(\"value\", code)\n", "log_Kswitch_slider.js_on_change(\"value\", code)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Great! Now, let's lay out our app and take a look!" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "\n", "
\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "(function(root) {\n", " function embed_document(root) {\n", " \n", " const docs_json = {\"04a28c7d-4811-4e39-aabe-f1c4c21dca39\":{\"defs\":[],\"roots\":{\"references\":[{\"attributes\":{\"children\":[{\"id\":\"1010\"},{\"id\":\"1048\"},{\"id\":\"1049\"}]},\"id\":\"1050\",\"type\":\"Row\"},{\"attributes\":{\"end\":6,\"format\":{\"id\":\"1003\"},\"js_property_callbacks\":{\"change:value\":[{\"id\":\"1047\"}]},\"start\":-3,\"step\":0.1,\"title\":\"Kswitch\",\"value\":1},\"id\":\"1008\",\"type\":\"Slider\"},{\"attributes\":{},\"id\":\"1058\",\"type\":\"AllLabels\"},{\"attributes\":{},\"id\":\"1030\",\"type\":\"SaveTool\"},{\"attributes\":{\"line_color\":\"#1f77b4\",\"line_width\":2,\"x\":{\"field\":\"c\"},\"y\":{\"field\":\"fc\"}},\"id\":\"1042\",\"type\":\"Line\"},{\"attributes\":{\"overlay\":{\"id\":\"1033\"}},\"id\":\"1029\",\"type\":\"BoxZoomTool\"},{\"attributes\":{\"end\":100.0,\"start\":1e-06},\"id\":\"1011\",\"type\":\"Range1d\"},{\"attributes\":{\"end\":3,\"format\":{\"id\":\"1003\"},\"js_property_callbacks\":{\"change:value\":[{\"id\":\"1047\"}]},\"start\":0,\"step\":0.1,\"title\":\"R (1/cell)\",\"value\":2},\"id\":\"1004\",\"type\":\"Slider\"},{\"attributes\":{},\"id\":\"1027\",\"type\":\"PanTool\"},{\"attributes\":{},\"id\":\"1032\",\"type\":\"HelpTool\"},{\"attributes\":{\"end\":3,\"format\":{\"id\":\"1003\"},\"js_property_callbacks\":{\"change:value\":[{\"id\":\"1047\"}]},\"start\":-6,\"step\":0.1,\"title\":\"KdA (1/mM)\",\"value\":-2},\"id\":\"1006\",\"type\":\"Slider\"},{\"attributes\":{},\"id\":\"1054\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{\"ticker\":null},\"id\":\"1057\",\"type\":\"LogTickFormatter\"},{\"attributes\":{\"code\":\"return Math.pow(10, tick).toPrecision(3)\"},\"id\":\"1003\",\"type\":\"FuncTickFormatter\"},{\"attributes\":{\"end\":3,\"format\":{\"id\":\"1003\"},\"js_property_callbacks\":{\"change:value\":[{\"id\":\"1047\"}]},\"start\":-6,\"step\":0.1,\"title\":\"K (1/cell)\",\"value\":0},\"id\":\"1005\",\"type\":\"Slider\"},{\"attributes\":{},\"id\":\"1031\",\"type\":\"ResetTool\"},{\"attributes\":{},\"id\":\"1055\",\"type\":\"AllLabels\"},{\"attributes\":{\"bottom_units\":\"screen\",\"coordinates\":null,\"fill_alpha\":0.5,\"fill_color\":\"lightgrey\",\"group\":null,\"left_units\":\"screen\",\"level\":\"overlay\",\"line_alpha\":1.0,\"line_color\":\"black\",\"line_dash\":[4,4],\"line_width\":2,\"right_units\":\"screen\",\"syncable\":false,\"top_units\":\"screen\"},\"id\":\"1033\",\"type\":\"BoxAnnotation\"},{\"attributes\":{\"line_alpha\":0.2,\"line_color\":\"#1f77b4\",\"line_width\":2,\"x\":{\"field\":\"c\"},\"y\":{\"field\":\"fc\"}},\"id\":\"1044\",\"type\":\"Line\"},{\"attributes\":{},\"id\":\"1024\",\"type\":\"BasicTicker\"},{\"attributes\":{\"end\":3,\"format\":{\"id\":\"1003\"},\"js_property_callbacks\":{\"change:value\":[{\"id\":\"1047\"}]},\"start\":-6,\"step\":0.1,\"title\":\"KdI (1/mM)\",\"value\":-2},\"id\":\"1007\",\"type\":\"Slider\"},{\"attributes\":{\"source\":{\"id\":\"1009\"}},\"id\":\"1046\",\"type\":\"CDSView\"},{\"attributes\":{},\"id\":\"1017\",\"type\":\"LinearScale\"},{\"attributes\":{\"args\":{\"cds\":{\"id\":\"1009\"},\"log_K_slider\":{\"id\":\"1005\"},\"log_KdA_slider\":{\"id\":\"1006\"},\"log_KdI_slider\":{\"id\":\"1007\"},\"log_Kswitch_slider\":{\"id\":\"1008\"},\"log_R_slider\":{\"id\":\"1004\"}},\"code\":\"\\nfunction bohr_parameter(c, R, K, KdA, KdI, Kswitch){\\n var log_arg = (1 + c / KdA) ** 2 / ((1 + c / KdA) ** 2 + Kswitch * (1 + c / KdI) ** 2);\\n\\n return -Math.log(R / K) - Math.log(log_arg);\\n}\\n\\nfunction fold_change(c, R, K, KdA, KdI, Kswitch) {\\n return 1 / (1 + Math.exp(-bohr_parameter(c, R, K, KdA, KdI, Kswitch)));\\n}\\n\\n// For convenience, get views into the ColumnDataSource data\\nvar c = cds.data['c'];\\nvar fc = cds.data['fc'];\\n\\n// Pull the values off of the sliders\\nvar R = 10 ** log_R_slider.value;\\nvar K = 10 ** log_K_slider.value;\\nvar KdA = 10 ** log_KdA_slider.value;\\nvar KdI = 10 ** log_KdI_slider.value;\\nvar Kswitch = 10 ** log_Kswitch_slider.value;\\n\\n// Loop through and update\\nvar cLen = c.length;\\nfor (var i = 0; i < cLen; i++) {\\n fc[i] = fold_change(c[i], R, K, KdA, KdI, Kswitch);\\n}\\n\\ncds.change.emit();\\n\"},\"id\":\"1047\",\"type\":\"CustomJS\"},{\"attributes\":{\"children\":[{\"id\":\"1004\"},{\"id\":\"1005\"},{\"id\":\"1006\"},{\"id\":\"1007\"},{\"id\":\"1008\"}],\"width\":200},\"id\":\"1049\",\"type\":\"Column\"},{\"attributes\":{},\"id\":\"1028\",\"type\":\"WheelZoomTool\"},{\"attributes\":{\"line_alpha\":0.1,\"line_color\":\"#1f77b4\",\"line_width\":2,\"x\":{\"field\":\"c\"},\"y\":{\"field\":\"fc\"}},\"id\":\"1043\",\"type\":\"Line\"},{\"attributes\":{\"coordinates\":null,\"group\":null},\"id\":\"1051\",\"type\":\"Title\"},{\"attributes\":{\"axis\":{\"id\":\"1023\"},\"coordinates\":null,\"dimension\":1,\"group\":null,\"ticker\":null},\"id\":\"1026\",\"type\":\"Grid\"},{\"attributes\":{\"axis_label\":\"[IPTG] (mM)\",\"coordinates\":null,\"formatter\":{\"id\":\"1057\"},\"group\":null,\"major_label_policy\":{\"id\":\"1058\"},\"ticker\":{\"id\":\"1020\"}},\"id\":\"1019\",\"type\":\"LogAxis\"},{\"attributes\":{\"axis_label\":\"fold change\",\"coordinates\":null,\"formatter\":{\"id\":\"1054\"},\"group\":null,\"major_label_policy\":{\"id\":\"1055\"},\"ticker\":{\"id\":\"1024\"}},\"id\":\"1023\",\"type\":\"LinearAxis\"},{\"attributes\":{\"tools\":[{\"id\":\"1027\"},{\"id\":\"1028\"},{\"id\":\"1029\"},{\"id\":\"1030\"},{\"id\":\"1031\"},{\"id\":\"1032\"}]},\"id\":\"1034\",\"type\":\"Toolbar\"},{\"attributes\":{\"num_minor_ticks\":10},\"id\":\"1020\",\"type\":\"LogTicker\"},{\"attributes\":{\"end\":1.05,\"start\":-0.05},\"id\":\"1013\",\"type\":\"Range1d\"},{\"attributes\":{\"coordinates\":null,\"data_source\":{\"id\":\"1009\"},\"glyph\":{\"id\":\"1042\"},\"group\":null,\"hover_glyph\":null,\"muted_glyph\":{\"id\":\"1044\"},\"nonselection_glyph\":{\"id\":\"1043\"},\"view\":{\"id\":\"1046\"}},\"id\":\"1045\",\"type\":\"GlyphRenderer\"},{\"attributes\":{\"axis\":{\"id\":\"1019\"},\"coordinates\":null,\"group\":null,\"ticker\":null},\"id\":\"1022\",\"type\":\"Grid\"},{\"attributes\":{\"width\":15},\"id\":\"1048\",\"type\":\"Spacer\"},{\"attributes\":{},\"id\":\"1059\",\"type\":\"UnionRenderers\"},{\"attributes\":{},\"id\":\"1060\",\"type\":\"Selection\"},{\"attributes\":{},\"id\":\"1015\",\"type\":\"LogScale\"},{\"attributes\":{\"below\":[{\"id\":\"1019\"}],\"center\":[{\"id\":\"1022\"},{\"id\":\"1026\"}],\"frame_height\":250,\"frame_width\":350,\"left\":[{\"id\":\"1023\"}],\"renderers\":[{\"id\":\"1045\"}],\"title\":{\"id\":\"1051\"},\"toolbar\":{\"id\":\"1034\"},\"x_range\":{\"id\":\"1011\"},\"x_scale\":{\"id\":\"1015\"},\"y_range\":{\"id\":\"1013\"},\"y_scale\":{\"id\":\"1017\"}},\"id\":\"1010\",\"subtype\":\"Figure\",\"type\":\"Plot\"},{\"attributes\":{\"data\":{\"c\":{\"__ndarray__\":\"je21oPfGsD7h5umjhGeyPmr9tO13MLQ+qEnhi7wltj75sWjUnku4PpQiz9TVpro+7wW+q408vT6ak8xxOQnAPqCffHNfl8E+AZwx1SJMwz64/5xUQivFPh+1aKvZOMc+e8Dck2p5yT6y1Gyt5vHLPp8O5Va6p84+RhwES2zQ0D4tt2AT5HHSPlHlSefYO9Q+kELdCTgy1j6/WDw9UFnYPrElODfbtdo+mOjBAAhN3T5BF3MrQxLgPh81JpNJoeE+LMZQHgNX4z67Lf6mMDflPjdMmjfwRec++U+sFMaH6T4OpvempgHsPqGAxlsBue4+0uCeSebZ8D5VbnVbSXzyPidAo0pAR/Q+bBTYkLo+9j4rnrpdCWf4PqBY6BDpxPo+Q/M+n4td/T7DkSD9URsAP0OeT0k5qwE/V7msiOlhAz/9vc2yJUMFP5+XLiQOUwc/FhoErSmWCT85c/OAbxEMP9/ufR1Syg4/OCiHn2XjED+LjHN/tIYSP91cXhuuUhQ/EczIJERLFj8rAT06ynQYP4k4pWb/0xo/pxpxjBhuHT/x97PpZSQgP6MXH5kutSE/hOO5F9ZsIz+Y5dV7IU8lPxHlTXUzYCc/eKVzYZWkKT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+eVsf2OXrk/55Wx/Y5euT/nlbH9jl65P+eVsf2OXrk/55Wx/Y5euT/nlbH9jl65P+WVsf2OXrk/55Wx/Y5euT/nlbH9jl65P+eVsf2OXrk/55Wx/Y5euT/nlbH9jl65P+eVsf2OXrk/55Wx/Y5euT/nlbH9jl65P+eVsf2OXrk/55Wx/Y5euT/nlbH9jl65P+eVsf2OXrk/55Wx/Y5euT/nlbH9jl65P+WVsf2OXrk/55Wx/Y5euT/nlbH9jl65P+eVsf2OXrk/5ZWx/Y5euT/nlbH9jl65P+eVsf2OXrk/55Wx/Y5euT/nlbH9jl65P+eVsf2OXrk/55Wx/Y5euT/nlbH9jl65P+eVsf2OXrk/55Wx/Y5euT/nlbH9jl65P+eVsf2OXrk/55Wx/Y5euT/nlbH9jl65P+eVsf2OXrk/55Wx/Y5euT/nlbH9jl65P+eVsf2OXrk/55Wx/Y5euT/llbH9jl65P+eVsf2OXrk/55Wx/Y5euT/nlbH9jl65P+eVsf2OXrk/55Wx/Y5euT/nlbH9jl65Pw==\",\"dtype\":\"float64\",\"order\":\"little\",\"shape\":[200]}},\"selected\":{\"id\":\"1060\"},\"selection_policy\":{\"id\":\"1059\"}},\"id\":\"1009\",\"type\":\"ColumnDataSource\"}],\"root_ids\":[\"1050\"]},\"title\":\"Bokeh Application\",\"version\":\"2.4.2\"}};\n", " const render_items = [{\"docid\":\"04a28c7d-4811-4e39-aabe-f1c4c21dca39\",\"root_ids\":[\"1050\"],\"roots\":{\"1050\":\"89532359-2841-483b-a579-0ab4e5d195f8\"}}];\n", " root.Bokeh.embed.embed_items_notebook(docs_json, render_items);\n", "\n", " }\n", " if (root.Bokeh !== undefined) {\n", " embed_document(root);\n", " } else {\n", " let attempts = 0;\n", " const timer = setInterval(function(root) {\n", " if (root.Bokeh !== undefined) {\n", " clearInterval(timer);\n", " embed_document(root);\n", " } else {\n", " attempts++;\n", " if (attempts > 100) {\n", " clearInterval(timer);\n", " console.log(\"Bokeh: ERROR: Unable to run BokehJS code because BokehJS library is missing\");\n", " }\n", " }\n", " }, 10, root)\n", " }\n", "})(window);" ], "application/vnd.bokehjs_exec.v0+json": "" }, "metadata": { "application/vnd.bokehjs_exec.v0+json": { "id": "1050" } }, "output_type": "display_data" } ], "source": [ "layout = bokeh.layouts.row(\n", " p,\n", " bokeh.layouts.Spacer(width=15),\n", " bokeh.layouts.column(\n", " log_R_slider,\n", " log_K_slider,\n", " log_KdA_slider,\n", " log_KdI_slider,\n", " log_Kswitch_slider,\n", " width=200,\n", " ),\n", ")\n", "\n", "bokeh.io.show(layout)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Computing environment" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "tags": [ "hide-input" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Python implementation: CPython\n", "Python version : 3.9.12\n", "IPython version : 8.3.0\n", "\n", "numpy : 1.21.5\n", "bokeh : 2.4.2\n", "jupyterlab: 3.3.2\n", "\n" ] } ], "source": [ "%load_ext watermark\n", "%watermark -v -p numpy,bokeh,jupyterlab" ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.9.12" } }, "nbformat": 4, "nbformat_minor": 4 }