pyrosetta.distributed.viewer
¶Warning: This notebook uses pyrosetta.distributed.viewer
code, which runs in jupyter notebook
and might not run if you're using jupyterlab
.
The pyrosetta.distributed
Viewer quickly renders .pdb
files, dynamically instantiating Pose
objects if required for certain visualization modules (matching the name viewer.set*
). So when adding
visualization modules to the Viewer or using presets, passing Pose
or PackedPose
objects to the Viewer is suggested for quicker rendering. If a Pose
object or list
, tuple
, or set
of Pose
objects are provided to the Viewer, the Viewer will dynamically update upon Pose
conformational changes by calling the view.show()
method or equivalently view()
. The Viewer applies visualization modules in the same order they are added (from left to right), so layering different styles (and ResidueSelectors
) on top of one another becomes possible. The user must have already initialized PyRosetta providing .params
files for any ligands and non-canonical residues in the input Pose
(s)/PackedPose
(s)/.pdb
file(s), otherwise pyrosetta.distributed
automatically initializes PyRosetta with default command line options.
Throughout this notebook, different syntaxes for constructing visualizations are demonstrated to show the flexibility of the pyrosetta.distributed.viewer
API, with hopes you find your favorite.
If you are using Google Colab: Currently, pyrosetta.distributed.viewer
does not require the local machine's PyMOL. You can always dump any Pose
object into a .pdb
file and open that in PyMOL on your own computer.
pose.dump_pdb("output_file.pdb")
Note: If you are having trouble using this notebook, please use the PyRosetta.notebooks
conda environment. Please see chapter 1.0 for instructions on how to setup and use this environment.
!pip install pyrosettacolabsetup
import pyrosettacolabsetup; pyrosettacolabsetup.install_pyrosetta()
import pyrosetta; pyrosetta.init()
import glob
import logging
logging.basicConfig(level=logging.INFO)
import numpy as np
import os
import pyrosetta
import pyrosetta.distributed
import pyrosetta.distributed.io as io
import pyrosetta.distributed.viewer as viewer
import sys
viewer
objects:¶viewer.__all__
For example: viewer.init()
viewer.presets
objects:¶viewer.presets.__all__
For example: viewer.presets.coreBoundarySurface()
viewer
objects contain docstrings as user documentation:¶help(viewer.init)
viewer.presets
objects contain docstrings as brief descriptions of each preset visualization¶help(viewer.presets.coreBoundarySurface)
help(viewer.presets.ligandsAndMetals)
From previous section:
Make sure you are in the right directory for accessing the .pdb
files:
cd google_drive/MyDrive/student-notebooks/
Note: import pyrosetta.distributed.viewer
expands the Jupyter notebook cell width to fit your internet browser, and can be called anytime using pyrosetta.distributed.viewer.expand_notebook()
flags = """
-auto_setup_metals 1
-detect_disulf 1
"""
pyrosetta.distributed.init(flags)
Show basic line representation of .pdb
file on disk:
view = viewer.init("path/to/my.pdb") view.show()
Change the window_size
option of viewer.init()
to fit your screen size, which should prevent the need to scroll to view your macromolecular objects.
Show basic line representation of Pose
or PackedPose
objects in memory:
pose = pyrosetta.io.pose_from_file("inputs/3EK4.pdb")
view = viewer.init(pose, window_size=(800, 600))
view()
To show basic cartoon representation, add viewer.setStyle()
:
view = viewer.init(pose, window_size=(800, 600)) + viewer.setStyle()
view()
To visualize ligands, non-conanical residues, metals, etc., viewer.setStyle()
(and several other viewer.set*
objects) optionally accept PyRosetta ResidueSelector
objects. In the case of inputs/3EK4.pdb
which is a $Ca^{2+}$-saturated GCaMP2 monomer, a ligand is a $Ca^{2+}$ ion and a non-canonical residue is the chromophore "CRO", both of which are aleady parameterized and available in the PyRosetta residue type set database. Therefore, we do not need to initialize PyRosetta with custom "CA" or "CRO" .params
files. However, if one wishes to visualize ligands and non-canonical residues for which .params
files are not available in the PyRosetta residue type set database, one must first initialize PyRosetta providing .params
files for any ligands and non-canonical residues in the input Pose
(s) or PackedPose
(s):
pyrosetta.init("-extra_res_fa /path/to/LIG.fa.params")
pyrosetta.distributed.viewer
passes the Pose
's PDB numbering of selected residues to py3dmol
for macromolecular rendering. Also, one may layer any visualization modules with any ResidueSelectors
in the order they are summed (from left to right). In this case:
metals_selector = pyrosetta.rosetta.core.select.residue_selector.ResiduePropertySelector(
pyrosetta.rosetta.core.chemical.ResidueProperty.METAL
)
ligands_selector = pyrosetta.rosetta.core.select.residue_selector.ResiduePropertySelector(
pyrosetta.rosetta.core.chemical.ResidueProperty.LIGAND
)
view = viewer.init(pose, window_size=(800, 600)) \
+ viewer.setStyle() \
+ viewer.setStyle(residue_selector=ligands_selector, style="stick", colorscheme="magentaCarbon", radius=0.5) \
+ viewer.setStyle(residue_selector=metals_selector, style="sphere", colorscheme="chainHetatm", radius=1.5)
viewer.setStyle()
layers the cartoon backbone and stick side-chain representation by defaultviewer.setStyle(residue_selector=ligands_selector, style="stick", colorscheme="magentaCarbon", radius=0.5)
layers any residue with the LIGAND
property a 0.5 radius stick with magenta carbon atoms.viewer.setStyle(residue_selector=metals_selector, style="sphere", colorscheme="chainHetatm", radius=1.5)
layers any residue with the METAL
property a 1.5 radius sphere with the py3dmol
color scheme "chainHetatm".view()
Show many Pose
s in memory using the interactive slider widget:
poses = [pyrosetta.io.pose_from_sequence("TESTVIEWER" * i) for i in range(1, 10)]
view = viewer.init(poses, continuous_update=True)
view.add(viewer.setStyle(colorscheme="lightgreyCarbon"))
view.add(viewer.setHydrogenBonds(radius=0.1, dashed=False))
view()
Below, layer a ResidueSelector
of residues with ResidueProperty.POLAR
, then hydrogen atoms, hydrogen bonds, and disulfide bonds:
polar_residue_selector = pyrosetta.rosetta.core.select.residue_selector.ResiduePropertySelector(
pyrosetta.rosetta.core.chemical.ResidueProperty.POLAR
)
# Optionally, initialize the visualizer with a PackedPose object:
# NOTE: PackedPose requires the serialization build of PyRosetta.
# pose = io.to_packed(pose) # Test the Viewer with a PackedPose object
view = viewer.init(pose)
view.add(viewer.setStyle(radius=0.1))
view.add(viewer.setStyle(residue_selector=polar_residue_selector, colorscheme="whiteCarbon", radius=0.25, label=False))
view.add(viewer.setHydrogens(color="white", polar_only=True, radius=0.1))
view.add(viewer.setHydrogenBonds(color="black"))
view.add(viewer.setDisulfides(radius=0.1))
view()
view = sum(
[
viewer.init(pose),
viewer.setStyle(cartoon=False, style="sphere", radius=1.5, colorscheme="darkgreyCarbon"),
viewer.setZoom(factor=0.95)
]
)
view()
pose = pyrosetta.io.pose_from_file("inputs/6MSR.pdb")
chA = pyrosetta.rosetta.core.select.residue_selector.ChainSelector("A")
chB = pyrosetta.rosetta.core.select.residue_selector.ChainSelector("B")
view = sum(
[
viewer.init(pose),
viewer.setStyle(cartoon_color="lightgrey", radius=0.25),
viewer.setSurface(residue_selector=chA, colorscheme="greenCarbon", opacity=0.65, surface_type="VDW"),
viewer.setSurface(residue_selector=chB, color="blue", opacity=0.75, surface_type="SAS"),
viewer.setDisulfides(radius=0.25),
viewer.setZoom(factor=1.5)
]
)
view()
view.reinit() # Subtract all visualization modules previously added to the Viewer
view()
viewer.setZoomTo()
can zoom into a PyRosetta ResidueSelector
:
pose = pyrosetta.io.pose_from_file("inputs/1QCQ.pdb")
helix_selector = pyrosetta.rosetta.core.select.residue_selector.SecondaryStructureSelector("H")
sheet_selector = pyrosetta.rosetta.core.select.residue_selector.SecondaryStructureSelector("E")
loop_selector = pyrosetta.rosetta.core.select.residue_selector.SecondaryStructureSelector("L")
modules = [
viewer.setBackgroundColor(color="black"),
viewer.setStyle(residue_selector=helix_selector, cartoon_color="cyan", label=False, radius=0),
viewer.setStyle(residue_selector=sheet_selector, cartoon_color="red", label=False, radius=0),
viewer.setStyle(residue_selector=loop_selector, cartoon_color="white", label=False, radius=0),
viewer.setZoomTo(residue_selector=sheet_selector)
]
viewer.init(pose, window_size=(1200, 600), modules=modules).show()
View live trajectories:
pose = pyrosetta.io.pose_from_file("inputs/2FD7.pdb")
view = viewer.init(pose, delay=0.15) \
+ viewer.setStyle(radius=0.1) \
+ viewer.setDisulfides(radius=0.1)
backrub = pyrosetta.rosetta.protocols.backrub.BackrubMover()
minimize = pyrosetta.rosetta.protocols.minimization_packing.MinMover()
for _ in range(100):
backrub.apply(pose)
minimize.apply(pose)
view.show()
pose = pyrosetta.Pose()
view = viewer.init(pose, delay=0.5) + viewer.setStyle(radius=0.1)
add_pdb_info_mover = pyrosetta.rosetta.protocols.simple_moves.AddPDBInfoMover()
mb = pyrosetta.rosetta.protocols.helical_bundle.MakeBundle()
bpc = pyrosetta.rosetta.protocols.helical_bundle.BPC_Parameters
n_helices = 4
for h in range(1, (n_helices + 1)):
mb.add_helix()
mb.helix(h).calculator_op().real_parameter(bpc.BPC_delta_omega0).set_value(h * (np.radians(360) / n_helices))
for i in range(2, 42, 3):
for h in range(1, (n_helices + 1)):
mb.helix(h).set_helix_length(i)
mb.helix(h).calculator_op().real_parameter(bpc.BPC_r0).set_value(n_helices * 2)
mb.helix(h).calculator_op().real_parameter(bpc.BPC_omega0).set_value(np.radians(5))
mb.apply(pose)
add_pdb_info_mover.apply(pose)
view()
pose = pyrosetta.Pose()
view = viewer.init(pose, delay=0.5) + viewer.setStyle(radius=0.1)
add_pdb_info_mover = pyrosetta.rosetta.protocols.simple_moves.AddPDBInfoMover()
mb = pyrosetta.rosetta.protocols.helical_bundle.MakeBundle()
bpc = pyrosetta.rosetta.protocols.helical_bundle.BPC_Parameters
n_helices = 6
for h in range(1, (n_helices + 1)):
mb.add_helix()
mb.helix(h).calculator_op().real_parameter(bpc.BPC_delta_omega0).set_value(h * (np.radians(360) / n_helices))
for i in np.arange(1, 15):
for h in range(1, (n_helices + 1)):
mb.helix(h).set_helix_length(20)
mb.helix(h).calculator_op().real_parameter(bpc.BPC_r0).set_value(i)
mb.helix(h).calculator_op().real_parameter(bpc.BPC_omega0).set_value(np.radians(4))
mb.apply(pose)
add_pdb_info_mover.apply(pose)
view()
Display preset custom viewers for routine visualizations:
pdbfiles = [os.path.join("inputs", f) for f in ["3EK4.pdb", "6MSR.pdb", "1QCQ.pdb", "2FD7.pdb"]]
poses = [pyrosetta.io.pose_from_file(p) for p in pdbfiles]
# Display preset custom viewers for routine visualizations:
viewer.presets.coreBoundarySurface(poses, window_size=(800, 600), continuous_update=True)
viewer.presets.ligandsAndMetals(poses, window_size=(800, 600), continuous_update=False)
For Developers: contribute your custom preset visualizations to PyRosetta:
~Rosetta/main/source/src/python/PyRosetta/src/pyrosetta/distributed/viewer/presets/\_\_init\_\_.py
templatePreset
function, renaming it to the name of your new preset Viewer (e.g. myCustomPreset
in step #5).__all__
list.RosettaCommons/main
import pyrosetta.distributed.viewer as viewer
viewer.presets.myCustomPreset(poses)
def myCustomPreset(packed_and_poses_and_pdbs=None, *args, **kwargs):
"""
Add a description of the preset Viewer here
"""
# Add custrom ResidueSelectors
metals_selector = pyrosetta.rosetta.core.select.residue_selector.ResiduePropertySelector(
pyrosetta.rosetta.core.chemical.ResidueProperty.METAL
)
ligands_selector = pyrosetta.rosetta.core.select.residue_selector.ResiduePropertySelector(
pyrosetta.rosetta.core.chemical.ResidueProperty.LIGAND
)
# Add custom Viewer commands
view = viewer.init(packed_and_poses_and_pdbs=packed_and_poses_and_pdbs, *args, **kwargs) \
+ viewer.setBackgroundColor("white") \
+ viewer.setStyle(style="stick", colorscheme="lightgreyCarbon", radius=0.15) \
+ viewer.setStyle(residue_selector=ligands_selector, style="stick", colorscheme="brownCarbon", radius=0.5, label=True) \
+ viewer.setStyle(residue_selector=metals_selector, style="sphere", colorscheme="chainHetatm", radius=1.5, label=True) \
+ viewer.setHydrogenBonds() \
+ viewer.setDisulfides(radius=0.15) \
+ viewer.setHydrogens(color="white", radius=0.033, polar_only=True) \
+ viewer.setSurface(residue_selector=ligands_selector, surface_type="VDW", opacity=0.5, color="magenta") \
+ viewer.setSurface(residue_selector=metals_selector, surface_type="VDW", opacity=0.5, color="magenta") \
+ viewer.setZoomTo(residue_selector=ligands_selector)
return view()
myCustomPreset(poses[0])
pyrosetta.distributed.viewer
visualization modules¶help(viewer.setBackgroundColor)
help(viewer.setDisulfides)
help(viewer.setHydrogenBonds)
help(viewer.setHydrogens)
help(viewer.setStyle)
help(viewer.setSurface)
help(viewer.setZoom)
help(viewer.setZoomTo)