{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Interoperability With Visualization Software\n", "PyBigDFT can be used in combination with various kinds of visualization software. Here we present some basic examples of this functionality." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## py3Dmol\n", "[py3Dmol](https://pypi.org/project/py3Dmol/) is a convenient way to visualize molecules inside a notebook. First, let's define the system to look at." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from io import StringIO\n", "input_file = StringIO(\"\"\"ATOM 1 N GLU A 1 -1.154 2.418 1.060 1.00 0.00 N \n", "ATOM 2 CA GLU A 1 -0.291 1.391 1.511 1.00 0.00 C \n", "ATOM 3 C GLU A 1 0.988 1.375 0.674 1.00 0.00 C \n", "ATOM 4 O GLU A 1 2.084 1.489 1.199 1.00 0.00 O \n", "ATOM 5 H1 GLU A 1 -0.808 3.356 0.900 1.00 0.00 H \n", "ATOM 6 HB1 ALA A 2 2.228 0.884 -3.807 1.00 0.00 H \n", "ATOM 7 H2 GLU A 1 -2.131 2.117 0.916 1.00 0.00 H \n", "ATOM 8 HA GLU A 1 -0.012 1.610 2.555 1.00 0.00 H \n", "ATOM 9 CB GLU A 1 -1.012 0.017 1.443 1.00 0.00 C \n", "ATOM 10 CG GLU A 1 -1.458 -0.331 -0.002 1.00 0.00 C \n", "ATOM 11 HB1 GLU A 1 -1.897 0.046 2.100 1.00 0.00 H \n", "ATOM 12 HB2 GLU A 1 -0.338 -0.772 1.815 1.00 0.00 H \n", "ATOM 13 HG1 GLU A 1 -0.583 -0.365 -0.671 1.00 0.00 H \n", "ATOM 14 HG2 GLU A 1 -2.152 0.435 -0.382 1.00 0.00 H \n", "ATOM 15 CD GLU A 1 -2.156 -1.666 -0.029 1.00 0.00 C \n", "ATOM 16 OE1 GLU A 1 -1.704 -2.633 -0.619 1.00 0.00 O \n", "ATOM 17 OE2 GLU A 1 -3.307 -1.738 0.635 1.00 0.00 O \n", "ATOM 18 HE2 GLU A 1 -3.662 -2.616 0.558 1.00 0.00 H \n", "ATOM 19 N ALA A 2 0.783 1.228 -0.622 1.00 0.00 N \n", "ATOM 20 CA ALA A 2 1.867 1.202 -1.533 1.00 0.00 C \n", "ATOM 21 C ALA A 2 2.816 2.367 -1.249 1.00 0.00 C \n", "ATOM 22 O ALA A 2 3.998 2.171 -1.016 1.00 0.00 O \n", "ATOM 23 HB2 ALA A 2 0.925 1.765 -3.471 1.00 0.00 H \n", "ATOM 24 H ALA A 2 2.340 3.354 -1.287 1.00 0.00 H \n", "ATOM 25 H ALA A 2 -0.144 1.131 -1.066 1.00 0.00 H \n", "ATOM 26 HA ALA A 2 2.442 0.301 -1.259 1.00 0.00 H \n", "ATOM 27 CB ALA A 2 1.459 0.968 -3.161 1.00 0.00 C \n", "ATOM 28 HB3 ALA A 2 0.877 0.168 -3.356 1.00 0.00 H\"\"\")" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "from BigDFT.IO import read_pdb\n", "sys = read_pdb(input_file)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To use py3D mol first define an a visualization object, and then call display_system." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "application/3dmoljs_load.v0": "
\n

You appear to be running in JupyterLab (or JavaScript failed to load for some other reason). You need to install the 3dmol extension:
\n jupyter labextension install jupyterlab_3dmol

\n
\n", "text/html": [ "
\n", "

You appear to be running in JupyterLab (or JavaScript failed to load for some other reason). You need to install the 3dmol extension:
\n", " jupyter labextension install jupyterlab_3dmol

\n", "
\n", "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from BigDFT.Visualization import InlineVisualizer\n", "viz = InlineVisualizer(300,200)\n", "viz.display_system(sys)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can update the system with some new fragments." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "from BigDFT.Systems import System\n", "from BigDFT.Fragments import Fragment\n", "from BigDFT.Atoms import Atom\n", "ion = System()\n", "ion[\"ION:0\"] = Fragment([Atom({\"sym\":\"H\", \"r\": [0, 0, 0], \"units\": \"bohr\"})])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It will be easier to see if we explicitly decide the colors." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "colordict = {\"GLU:1\": \"blue\", \"ALA:2\": \"red\", \"ION:0\": \"yellow\"}" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "application/3dmoljs_load.v0": "
\n

You appear to be running in JupyterLab (or JavaScript failed to load for some other reason). You need to install the 3dmol extension:
\n jupyter labextension install jupyterlab_3dmol

\n
\n", "text/html": [ "
\n", "

You appear to be running in JupyterLab (or JavaScript failed to load for some other reason). You need to install the 3dmol extension:
\n", " jupyter labextension install jupyterlab_3dmol

\n", "
\n", "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "viz = InlineVisualizer(300,200)\n", "viz.display_system(sys, colordict=colordict, show=False)\n", "viz.display_system(ion, colordict=colordict)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you want to display a movie, just pass a list of systems instead." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "from copy import deepcopy\n", "syslist = [sys]\n", "for i in range(10):\n", " syslist.append(deepcopy(syslist[-1]))\n", " syslist[-1][\"GLU:1\"].translate([-1, 0, 0])" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "application/3dmoljs_load.v0": "
\n

You appear to be running in JupyterLab (or JavaScript failed to load for some other reason). You need to install the 3dmol extension:
\n jupyter labextension install jupyterlab_3dmol

\n
\n", "text/html": [ "
\n", "

You appear to be running in JupyterLab (or JavaScript failed to load for some other reason). You need to install the 3dmol extension:
\n", " jupyter labextension install jupyterlab_3dmol

\n", "
\n", "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "viz = InlineVisualizer(300,200)\n", "viz.display_system(*syslist, colordict=colordict)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## VMD\n", "We offer a simple routine for produce a VMD script allowing you to color fragments by name." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "from BigDFT.Visualization import VMDGenerator\n", "viz = VMDGenerator()\n", "colordict = {\"GLU:1\": \"1\", \"ALA:2\": \"2\"}\n", "viz.visualize_fragments(sys, \"script.pml\", \"script.xyz\", fragcolors=colordict)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "mol default style CPK\n", "mol new script.xyz\n", "mol modcolor 0 0 ColorID 16\n", "mol addrep 0\n", "mol modselect 1 0 index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16\n", "mol modcolor 1 0 ColorID 1\n", "mol addrep 0\n", "mol modselect 2 0 index 17 18 19 20 21 22 23 24 25 26 27\n", "mol modcolor 2 0 ColorID 2\n" ] } ], "source": [ "with open(\"script.pml\") as ifile:\n", " for line in ifile:\n", " print(line,end=\"\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.6.10" } }, "nbformat": 4, "nbformat_minor": 4 }