Choose representations and colours#
Goal: control how a molecule is drawn, by view() and by render(), using
mol.reps, a Representations object.
Example#
from moleculekit.molecule import Molecule
mol = Molecule("3PTB")
mol.reps.addDefaults() # the scene you get with no representations
mol.reps.remove(1) # drop the automatic waters/ion/ligand
mol.reps.add("protein and within 5 of resname BEN", "Surf", "Hydrophobicity",
opacity=0.3)
mol.reps.add("resname BEN", "ball-and-stick", "element-symbol", size=0.4)
mol.reps.add("resname BEN", "Labels", "black", size=1.6)
mol.render("pocket.png", center="resname BEN", zoom=0.3)
addDefaults() writes the automatic scene out as ordinary entries you can edit,
reorder or remove(). Without it, add() replaces the automatic scene entirely, so
colouring one ligand costs you the cartoon and everything else.
Styles#
Each style can be written in VMD’s vocabulary or Mol*’s. Spacing, case and
hyphens are ignored, so Secondary Structure, secondary-structure and
SecondaryStructure are one name.
VMD name |
Mol* name |
Draws |
|---|---|---|
|
|
Ribbon following the backbone |
|
|
Sticks with atoms as spheres |
|
|
Space-filling spheres |
|
|
Bond lines, cheapest to draw |
|
|
Solvent-excluded surface |
|
|
Coarser gaussian surface, cheaper |
|
|
One dot per atom |
|
|
Tube thickened by B factor |
|
|
Backbone trace, proteins and nucleic acids |
|
|
Residues as ellipsoids |
|
|
Each atom’s name beside it |
|
|
Labels and FormalCharges draw text on top of another representation, so
they need one that draws the atoms themselves. A style from neither column is
passed to the VMD backend as written, so VMD’s own styles still work there.
Colours#
VMD name |
Mol* name |
Colours by |
|---|---|---|
|
|
Chemical element |
|
|
Chain |
|
|
Residue name |
|
|
Position in the sequence |
|
|
Helix, sheet, coil |
|
|
B factor, or pLDDT for predicted structures |
|
|
Occupancy |
|
|
Residue hydrophobicity |
|
|
Protein, nucleic, water, ligand |
|
|
Atom index |
|
Position along the chain, a different gradient |
|
|
Entity: polymer, ligand, water |
|
|
Polymer |
|
|
Model |
|
|
Structure |
|
|
Chain, with non-carbon atoms picked out |
Any SVG colour name ("red", "steelblue") or #rrggbb string gives a
uniform colour, as does a VMD ColorID integer.
Size and transparency#
mol.reps.add("resname BEN", "Licorice", "Name", size=0.4) # thin sticks
mol.reps.add("resname BEN", "VDW", "Name", size=0.85, opacity=0.25) # ghost spheres
mol.reps.add("resname BEN", "Licorice", "Name", c_atom_color="#66ccff") # cyan carbons
Parameter |
Meaning |
|---|---|
|
Scales the drawn size: stick and sphere radius, surface probe, point size, label text. Each style keeps its own sensible size at |
|
|
|
Colours carbon only, leaving N, O and S their element colours. A colour, or one of |
|
How sizes are decided before |
|
What a |
|
Cosmetics for a |
|
|
Representations layer, so a transparent VDW over a Licorice of the same
selection gives sticks inside a ghost surface.
Labelling atoms#
mol.reps.add("resname BEN", "Licorice", "Name")
mol.reps.add("resname BEN", "Labels", "black",
label_fields=["resname", "resid", "name"], size=1.4)
mol.reps.add("resname BEN", "Labels", label_fields="name", size=1.6,
label_style={"bg_color": "#003366", "bg_opacity": 0.85, "offset_y": 1.5})
label_fields takes any per-atom fields the molecule has — name, element,
resname, resid, chain, segid, beta, occupancy, index and so on —
and writes them space-separated. Each label is drawn separately, so label a
selection worth naming rather than a whole structure; past a couple of hundred
atoms they are skipped with a warning.
Changing a representation you already added#
update() changes one entry in place and leaves the rest of it
alone, so recolouring does not cost you the size or the selection. It keeps the
entry where it is, which is what makes indices stable: removing and re-adding
would move it to the end and renumber everything after it.
mol.reps.update(0, color="Chain")
mol.reps.update(0, visibility=False) # keep it, stop drawing it
mol.reps.update(0, visibility=True)
Only what you pass is changed, so passing nothing changes nothing.
Gotchas#
Representations replace the automatic scene, they do not add to it. This matches the VMD backend. Use
addDefaults()to start from the automatic scene, or leavemol.repsempty to get it.Formal charge labels follow the same rule. With no representations they are drawn automatically on charged atoms; once you set any, add a
FormalChargesrepresentation to keep them.addDefaults()includes one when the molecule has a charged atom, so starting from the default scene keeps them without asking.update_sel_every_frameis for the interactive viewer. It re-evaluates a coordinate-dependent selection such aswithin 5 of resname BENon every trajectory frame. A rendered image is a single frame, so it is carried on the representation and changes nothing about the picture.An unknown style or colour raises. It used to draw ball-and-stick or a garbage uniform colour, which looked like a deliberate picture rather than a typo.
A representation whose selection matches no atoms is dropped with a warning, and if every one matches nothing, the scene raises rather than drawing an empty image.
Putty,LabelsandFormalChargesonly reach Mol*. The VMD and NGL backends skip them rather than being sent a style they have no representation for.framesonly applies to the interactive viewer. A render is a single still, soadd()’sframesargument has nothing to act on there.