import MDAnalysis as mda
import molecularnodes as mn
import numpy as np
from MDAnalysis import transformations as trans
from MDAnalysis.analysis import density
from MDAnalysis.tests.datafiles import TPR, XTC
= mn.Canvas() canvas
Solvent Density
An example for visualizing solvent density.
This example follows the Calculating the solvent density around a protein example from the MDAnalysis user guide.
Load and transform Universe
= mda.Universe(TPR, XTC)
u
= u.select_atoms("protein")
protein = u.select_atoms("not protein")
water
= [
workflow # unwrap all fragments
trans.unwrap(u.atoms),
trans.center_in_box(
protein,="geometry", # move atoms to center protein
center
),
trans.wrap(
water,="residues", # wrap each water back into box
compound
),
trans.fit_rot_trans(
protein,
protein,="mass", # align protein to first frame
weights
),
]
*workflow) u.trajectory.add_transformations(
Analyse and Export .dx
= u.select_atoms("name OW")
ow = density.DensityAnalysis(ow, delta=4.0, padding=2)
dens
dens.run()# convert density unit to TIP4P
"TIP4P")
dens.results.density.convert_density("water.dx") dens.results.density.export(
Add Universe to Blender
Import universe as a Trajectory
and add a ribbon style to represent the protein. We also add a style to the non-rotein atoms, sliced along the y
axis. After taking the snapshot for visual reference we remove the sphere style as we will be showing the water as a density.
= np.mean(protein.atoms.positions, axis=0)
protein_center
= mn.Trajectory(u).add_style(mn.StyleRibbon(quality=5, backbone_radius=2))
t
t.add_style("Instance", subdivisions=4),
mn.StyleSpheres(=f"not protein and prop y >= {protein_center[1]}",
selection
)
/ 2, 0, np.pi / 3))
canvas.frame_view(t, (np.pi
canvas.snapshot()-1].remove() t.styles[
Add density component
We can load the density that was written-out from the analysis performed previously as a Density
entity.
# load density file
= mn.entities.density.io.load(
d ="water.dx",
file_path="density_iso_surface",
style=True,
overwrite
)# add a density info annotation for the density entity
= d.annotations.add_density_info()
da # only show the filename and ISO value
= da.show_delta = da.show_shape = False da.show_origin
Visualization
Set density style values
# get the density style
= d.styles[0]
ds # set the positive color to blue with 50% opacity
= (0, 0, 1, 0.5) ds.positive_color
ISO Value 0.5
# set ISO value to 0.5
= 0.5 ds.iso_value
# frame the density component and render
="front")
canvas.frame_view(d, viewpoint canvas.snapshot()
ISO Value 0.5 with Contours
# set ISO value to 0.5
= 0.5
ds.iso_value # enable contours
= True
ds.show_contours # set contour thickness
= 0.25
ds.contour_thickness # set contour colors
= (1, 1, 1, 1) ds.contour_color
# frame the density component and render
="front")
canvas.frame_view(d.get_view(), viewpoint canvas.snapshot()
From Top with Grid Axes
# add grid axes annotation
= d.annotations.add_grid_axes() ga
# set viewpoint to top
="top")
canvas.frame_view(d.get_view(), viewpoint
canvas.snapshot()# hide grid axes
= False ga.visible
ISO Value 0.5 sliced from Left
# slice the grid from the left 50%
= 50 ds.slice_left
# set viewpoint to left
="left")
canvas.frame_view(d.get_view(), viewpoint canvas.snapshot()
Only Contours
# reset slicing
= 0
ds.slice_left # only show contours
= True ds.only_contours
# set viewpoint to front
="front")
canvas.frame_view(d.get_view(), viewpoint canvas.snapshot()