{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 7-OutputTo3DViewer\n", "This tutorial shows how to pass an mmtf structure record directly into the py3Dmol viewer.\n", "\n", "This functionality is required if structures in a custom mmtf files, e.g., Rosetta models, must be visualized." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "from pyspark.sql import SparkSession\n", "from mmtfPyspark.io import mmtfReader\n", "from mmtfPyspark.io import mmtfWriter\n", "import py3Dmol" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Configure Spark" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "spark = SparkSession.builder.appName(\"7-OutputTo3DViewer\").getOrCreate()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Importing MMTF records into py3Dmol" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "pdb = mmtfReader.download_full_mmtf_files([\"4HHB\"])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Get first mmtf structure record and convert to base64-encoded string\n", "We use mmtWriter to create a base-64 encoded string of an mmtf structure." ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "mmtf_structure = pdb.first()\n", "mmtf = mmtfWriter.to_mmtf_base64(mmtf_structure[1])" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mmtf_structure[1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Add base64-encoded mmtf record to py3Dmol\n", "Using addModel, we can pass in the mmtf record as a string to py3Dmol." ] }, { "cell_type": "code", "execution_count": 18, "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": [ "viewer = py3Dmol.view()\n", "viewer.addModel(mmtf,'mmtf')\n", "viewer.setStyle({'cartoon': {'color': 'spectrum'}})\n", "viewer.show()" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "spark.stop()" ] } ], "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.13" } }, "nbformat": 4, "nbformat_minor": 4 }