{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "py3Dmol\n", "=======\n", "\n", "A simple [IPython/Jupyter](http://jupyter.org/) widget to\n", "embed an interactive [3Dmol.js](http://3dmol.csb.pitt.edu) viewer in a notebook.\n", "\n", "The widget is completely static, which means the viewer doesn't need a running\n", "IPython kernel to be useful and web pages and presentations generated from\n", "the notebook will work as expected. However, this also means there is only\n", "one-way communication between the notebook and the viewer.\n", "\n", "Installation\n", "------------\n", "\n", "From PyPI:\n", "\n", " pip install py3Dmol\n", " \n", "API\n", "---\n", "\n", "The returned view object has the exact same API as [3Dmol.GLViewer](http://3dmol.csb.pitt.edu/doc/$3Dmol.GLViewer.html)\n", "with the exception that functions return None.\n", "\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import py3Dmol" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false, "scrolled": false }, "outputs": [ { "data": { "text/html": [ "
\n", "" ], "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p = py3Dmol.view(query='pdb:1ycr')\n", "p.setStyle({'cartoon': {'color':'spectrum'}})\n", "p.show()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [], "source": [ "xyz = '''4\n", "* (null), Energy -1000.0000000\n", "N 0.000005 0.019779 -0.000003 -0.157114 0.000052 -0.012746\n", "H 0.931955 -0.364989 0.000003 1.507100 -0.601158 -0.004108\n", "H -0.465975 -0.364992 0.807088 0.283368 0.257996 -0.583024\n", "H -0.465979 -0.364991 -0.807088 0.392764 0.342436 0.764260\n", "'''" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "" ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "xyzview = py3Dmol.view(width=400,height=400)\n", "xyzview.addModel(xyz,'xyz',{'vibrate': {'frames':10,'amplitude':1}})\n", "xyzview.setStyle({'stick':{}})\n", "xyzview.setBackgroundColor('0xeeeeee')\n", "xyzview.animate({'loop': 'backAndForth'})\n", "xyzview.zoomTo()\n", "xyzview.show()" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "" ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "view = py3Dmol.view(query='pdb:1dc9')\n", "view.setViewStyle({'style':'outline','color':'black','width':0.1})\n", "view.setStyle({'cartoon':{'arrows':True, 'tubes':True, 'style':'oval', 'color':'white'}})\n", "view" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "" ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "view = py3Dmol.view(query='pdb:1ycr')\n", "chA = {'chain':'A'}\n", "chB = {'chain':'B'}\n", "view.setStyle(chA,{'cartoon': {'color':'spectrum'}})\n", "view.addSurface(py3Dmol.VDW,{'opacity':0.7,'color':'white'}, chA)\n", "view.setStyle(chB,{'stick':{}})\n", "view.show()" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "" ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "view = py3Dmol.view(query='pdb:5ire',options={'doAssembly':True})\n", "view.setStyle({'cartoon':{'color':'spectrum'}})\n", "view.show()" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "Color by temperature factors" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "" ], "text/plain": [ "" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "view = py3Dmol.view(query='pdb:1ycr')\n", "view.setStyle({'cartoon': {'color':'white'}})\n", "view.addSurface(py3Dmol.VDW,{'opacity':0.7,'colorscheme':{'prop':'b','gradient':'sinebow','min':0,'max':70}})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Generate an inline image of what is currently in the viewer (all white if the structure hasn't loaded yet)." ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", " " ], "text/plain": [ "" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "png = view.png()\n", "png" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "Display local file." ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "" ], "text/plain": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "benz='''\n", " RDKit 3D\n", "\n", " 6 6 0 0 0 0 0 0 0 0999 V2000\n", " -0.9517 0.7811 -0.6622 C 0 0 0 0 0 0 0 0 0 0 0 0\n", " 0.2847 1.3329 -0.3121 C 0 0 0 0 0 0 0 0 0 0 0 0\n", " 1.2365 0.5518 0.3512 C 0 0 0 0 0 0 0 0 0 0 0 0\n", " 0.9517 -0.7811 0.6644 C 0 0 0 0 0 0 0 0 0 0 0 0\n", " -0.2847 -1.3329 0.3144 C 0 0 0 0 0 0 0 0 0 0 0 0\n", " -1.2365 -0.5518 -0.3489 C 0 0 0 0 0 0 0 0 0 0 0 0\n", " 1 2 2 0\n", " 2 3 1 0\n", " 3 4 2 0\n", " 4 5 1 0\n", " 5 6 2 0\n", " 6 1 1 0\n", "M END\n", "$$$$'''\n", "view = py3Dmol.view()\n", "view.addModel(benz,'sdf')\n", "view.setStyle({'stick':{}})\n", "view.zoomTo()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.13" }, "widgets": { "state": {}, "version": "1.1.1" } }, "nbformat": 4, "nbformat_minor": 0 }