{ "cells": [ { "cell_type": "markdown", "id": "728ce224-8da5-43dc-b310-7a957adca697", "metadata": {}, "source": [ "# 🧪 Panel Chemistry - NGL Viewer\n", "\n", "The Panel Chemistry `NGLViewer` allows you to use the powerful [JS NGL Viewer](http://nglviewer.org/ngl/api/) using Python 🐍 and [HoloViz Panel](https://panel.holoviz.org/) ❤️. \n", "\n", "![JSME Editor App](../assets/NGLViewer.gif)\n", "\n", "When using NGL please cite\n", "\n", "- AS Rose, AR Bradley, Y Valasatava, JM Duarte, A Prlić and PW Rose. *Web-based molecular graphics for large complexes*. ACM Proceedings of the 21st International Conference on Web3D Technology (Web3D '16): 185-186, 2016. [doi:10.1145/2945292.2945324](http://dx.doi.org/10.1145/2945292.2945324).\n", "- AS Rose and PW Hildebrand. *NGL Viewer: a web application for molecular visualization*. Nucl Acids Res (1 July 2015) 43 (W1): W576-W579 first published online April 29, 2015. [doi:10.1093/nar/gkv402](https://doi.org/10.1093/nar/gkv402).\n", "\n", "## Parameters:\n", "\n", "* **``object``** (str): The object to display. For example\n", "\n", " - An url like 'rcsb://3dqb.pdb' or 'rcsb://1NKT'.\n", " - A non-url like '1NKT'. This will automatically be loaded as 'rcsb://1NKT'.\n", " - A blob. This requires the `extension` parameter to be set to the relevant extension.\n", "\n", "* **``extension``** (str): One of '', 'pdb', 'cif', 'csv', 'ent', 'gro', 'json', 'mcif', 'mmcif', 'mmtf', 'mol2', 'msgpack', 'netcdf', 'parm7', 'pqr', 'prmtop', 'psf', 'sd', 'sdf', 'top', 'txt', 'xml'. Default is '', i.e. the blob object is loaded as a url or non-url.\n", "* **``representation``** (str): A display representation. One of 'axes', 'backbone', 'ball+stick', 'cartoon', 'helixorient', 'hyperball', 'label', 'licorice', 'line', 'point', 'ribbon', 'rocket', 'rope', 'spacefill', 'surface', 'trace', 'unitcell'. Default is 'ball+stick'.\n", "* **``color_scheme``** (str): A predefined or 'custom' color scheme. One of 'atomindex', 'bfactor', 'chainid', 'chainindex', 'chainname', 'custom', 'densityfit', 'electrostatic', 'element', 'entityindex', 'entitytype', 'geoquality', 'hydrophobicity', 'modelindex', 'moleculetype', 'occupancy', 'random', 'residueindex', 'resname', 'sstruc', 'uniform', 'value', 'volume'. If 'custom' is specified you need to set the 'custom_color_scheme' parameter. Default color_scheme is 'element'.\n", "* **``custom_color_scheme``** A custom color scheme. See http://nglviewer.org/ngl/api/manual/coloring.html#custom-coloring.\n", "* **``effect``** (str): One of '', 'spin' or 'rock'. Default is '', i.e. no effect\n", "\n", "\n", "For layout and styling related parameters see the [Panel Customization Guide](https://panel.holoviz.org/user_guide/Customization.html)." ] }, { "cell_type": "markdown", "id": "061ae600-7d0a-4707-9ed2-d7ba2c559f0f", "metadata": {}, "source": [ "## Example" ] }, { "cell_type": "code", "execution_count": null, "id": "6b53c66f-a059-4727-8cc1-f502c7b800d7", "metadata": {}, "outputs": [], "source": [ "import panel as pn \n", "from panel_chemistry.pane import NGLViewer # panel_chemistry needs to be imported before you run pn.extension()\n", "from panel_chemistry.pane.ngl_viewer import EXTENSIONS\n", "pn.extension(\"ngl_viewer\", sizing_mode=\"stretch_width\")" ] }, { "cell_type": "code", "execution_count": null, "id": "13bd929d-d3a5-4e2c-8c0b-492dc3e3d3d9", "metadata": {}, "outputs": [], "source": [ "viewer = NGLViewer(object=\"1CRN\", background=\"#F7F7F7\", min_height=700, sizing_mode=\"stretch_both\")" ] }, { "cell_type": "code", "execution_count": null, "id": "25e6eba4-31f5-488c-9c96-7aac092f56bb", "metadata": {}, "outputs": [], "source": [ "settings = pn.Param(\n", " viewer,\n", " parameters=[\"object\",\"extension\",\"representation\",\"color_scheme\",\"custom_color_scheme\",\"effect\",],\n", " name=\"⚙️ Settings\"\n", ")" ] }, { "cell_type": "code", "execution_count": null, "id": "6e35c77b", "metadata": { "jupyter": { "outputs_hidden": false }, "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "file_input = pn.widgets.FileInput(accept=','.join('.' + s for s in EXTENSIONS[1:]))\n", "\n", "def filename_callback(target, event):\n", " target.extension = event.new.split('.')[1]\n", "\n", "def value_callback(target, event):\n", " target.object = event.new.decode('utf-8')\n", "\n", "file_input.link(viewer, callbacks={'value': value_callback, 'filename': filename_callback})\n", "\n", "header = pn.widgets.StaticText(value='{0}'.format(\"💾 File Input\"))\n", "file_column = pn.layout.Column(header, file_input)" ] }, { "cell_type": "code", "execution_count": null, "id": "12cdb234-6533-4d9b-99a7-f78222abd717", "metadata": {}, "outputs": [], "source": [ "layout = pn.Param(\n", " viewer,\n", " parameters=[\"sizing_mode\", \"width\", \"height\", \"background\"],\n", " name=\"📐 Layout\"\n", ")" ] }, { "cell_type": "code", "execution_count": null, "id": "bcc77c95-8609-4854-bf41-c6b403f68a19", "metadata": {}, "outputs": [], "source": [ "pn.Row(\n", " viewer,\n", " pn.WidgetBox(settings, layout, width=300, sizing_mode=\"fixed\",),\n", ")" ] }, { "cell_type": "markdown", "id": "c3a610ac-8b26-413d-997d-7a75b8192aab", "metadata": {}, "source": [ "## App" ] }, { "cell_type": "code", "execution_count": null, "id": "e8dc2e01-5829-4ae4-ab55-038b4f90d9b8", "metadata": {}, "outputs": [], "source": [ "accent=\"#D2386C\"" ] }, { "cell_type": "code", "execution_count": null, "id": "8aabd1e2-4ab4-41fd-a0f6-62c9098ec0f7", "metadata": {}, "outputs": [], "source": [ "pn.template.FastListTemplate(\n", " site=\"Panel Chemistry\", site_url=\"./\",\n", " title=\"NGLViewer\", \n", " sidebar=[file_column, settings, layout],\n", " main=[viewer],\n", " accent_base_color=accent, header_background=accent,\n", ").servable();" ] }, { "cell_type": "markdown", "id": "0e69d85b", "metadata": {}, "source": [ "Serve the app via `panel serve NGLViewer.ipynb` and check it out at http://localhost:5006/NGLViewer.\n", "\n", "If you add the flag `--autoreload` you will get automatic reloading when ever you save the file.\n", "\n", "You can also use the [Panel Jupyter Preview](https://blog.holoviz.org/panel_0.12.0.html#JupyterLab-previews) to serve the app in a seperate window in Jupyter Lab." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.8.4" } }, "nbformat": 4, "nbformat_minor": 5 }