This tutorial shows how to pass an mmtf structure record directly into the py3Dmol viewer.
This functionality is required if structures in a custom mmtf files, e.g., Rosetta models, must be visualized.
from pyspark.sql import SparkSession
from mmtfPyspark.io import mmtfReader
from mmtfPyspark.io import mmtfWriter
import py3Dmol
spark = SparkSession.builder.appName("7-OutputTo3DViewer").getOrCreate()
pdb = mmtfReader.download_full_mmtf_files(["4HHB"])
We use mmtWriter to create a base-64 encoded string of an mmtf structure.
mmtf_structure = pdb.first()
mmtf = mmtfWriter.to_mmtf_base64(mmtf_structure[1])
mmtf_structure[1]
<mmtfPyspark.utils.mmtfStructure.MmtfStructure at 0x119ed9e10>
Using addModel, we can pass in the mmtf record as a string to py3Dmol.
viewer = py3Dmol.view()
viewer.addModel(mmtf,'mmtf')
viewer.setStyle({'cartoon': {'color': 'spectrum'}})
viewer.show()
You appear to be running in JupyterLab (or JavaScript failed to load for some other reason). You need to install the 3dmol extension:
jupyter labextension install jupyterlab_3dmol
spark.stop()