Fragment Coloring
MolecularViewer can color atoms by fragment instead of by element. Use it to tell apart the
molecules in a cluster, a host and a guest, or the layers of a slab and an adsorbate.
Choose Color By → Fragment in the viewer sidebar. The option appears only when fragments are
defined.
Defining Fragments
Fragments are read from the first of these sources that is present:
- the
fragments=argument - ASE
tags(atoms.set_tags(...)). Tags are ignored when every tag is equal, such as the all-zero default. atoms.info['fragments']- residue names in
atoms.arrays['residuenames'], supplied by PDB files and MD topologies
The first three sources can be written in either of two forms:
| Form | Example | Meaning |
|---|---|---|
| Per-atom labels | ["w", "w", "w", "n", "n", "n", "n"] |
One label per atom. Each unique label is one fragment. |
| Atom-index groups | [[0, 1, 2], [3, 4, 5, 6]] |
Group k is fragment k. Atoms in no group keep their element color. |
Fragments are numbered in the order they first appear, and the numbering is shared across trajectory frames.
PDB residue names
PDB resname values become fragment labels automatically, including when the
PDB supplies topology for a DCD/XTC/TRR trajectory. Atoms with the same name
(for example, HOH) belong to one fragment even when their residue numbers or
chains differ. Blank names stay unassigned. The original residue names and
numbers remain in the per-atom arrays through viewer data conversion.
This also works with MolecularViewer(read("system.pdb")) and in the VS Code
PDB viewer. Choose Color By → Fragment to switch coloring interactively.
Per-Atom Labels
from ase import Atoms
from ase.build import molecule
from aseview import MolecularViewer
cluster = Atoms()
labels = []
for label, name, offset in [
("water", "H2O", (0.0, 0.0, 0.0)),
("ammonia", "NH3", (3.2, 0.0, 0.0)),
("methanol", "CH3OH", (0.0, 3.4, 0.0)),
("benzene", "C6H6", (4.2, 4.6, 0.0)),
]:
part = molecule(name)
part.translate(offset)
cluster += part
labels += [label] * len(part)
viewer = MolecularViewer(cluster, fragments=labels)
viewer.show()
Passing fragments= turns on color_by="fragment" automatically unless you set color_by yourself.
The same labels can be stored on the structure with cluster.info["fragments"] = labels.
Atom-Index Groups
List only the atoms you want to highlight. Atoms left out, such as methanol here, keep their element colors.
water = [0, 1, 2]
ammonia = [3, 4, 5, 6]
benzene = list(range(13, 25))
viewer = MolecularViewer(
cluster,
fragments=[water, ammonia, benzene],
fragment_colormap="viridis",
style="glossy",
)
viewer.show()
From ASE Tags
Builders such as fcc111 tag each slab layer, and add_adsorbate tags the adsorbate 0, so a
slab needs no extra input. Color overrides are keyed by label, which here is the tag value.
from ase.build import add_adsorbate, fcc111, molecule
slab = fcc111("Pt", size=(3, 3, 3), vacuum=8.0)
add_adsorbate(slab, molecule("CO"), height=1.9, position="ontop")
viewer = MolecularViewer(
slab,
color_by="fragment",
fragment_colors={0: "#a855f7"}, # tag 0 = adsorbate
fragment_opacities={3: 0.2, 2: 0.5}, # fade the lower layers
style="glossy",
)
viewer.show()
Colors
Colors are assigned deterministically from the fragment order, so the same input always gives the same colors:
fragment_colormap="default"cycles a fixed categorical palette (blue, red, green, amber, purple, …). After ten fragments it repeats with shifted lightness.viridis,plasma,coolwarm,jet,rainbow, andgrayscalesample fragment k of n atk / (n - 1).fragment_colorsoverrides individual fragments. Pass a dict keyed by label (for index groups, the label is the group index) or a list in fragment order, such as[None, "#ff0000"].
In the viewer, the Fragment palette menu switches the palette, and the fragment rows under Color change one fragment's color and opacity.
| Option | Description |
|---|---|
fragments= |
Per-atom labels or atom-index groups |
fragment_colors= |
Per-fragment overrides (dict keyed by label, or list) |
color_by="fragment" |
Start in fragment coloring mode |
fragment_colormap= |
"default" or a colormap name |
Opacity
The Opacity slider fades the whole structure. When fragments are
defined, a Fragment Opacity list gives each fragment its own slider. This list is available in
every Color By mode, not only Fragment. The two values multiply. A bond takes the opacity of its
more transparent end, and a fragment set to 0 is hidden together with its bonds.
MolecularViewer(atoms, opacity=0.6) # whole structure
MolecularViewer(cluster, fragments=labels,
fragment_opacities={"water": 1.0, "benzene": 0.25})
| Option | Description |
|---|---|
opacity= |
Whole-structure opacity, 0–1 |
fragment_opacities= |
Per-fragment opacity (dict keyed by label, or list) |
Command Line
aseview cluster.xyz --fragments 'w*3,n*4,m*6,b*12' # per-atom labels with repeats
aseview cluster.xyz --fragments '0-2;3-6;13-24' # ';'-separated index groups
aseview cluster.xyz --fragments '0-2;3-6' --cmap viridis --fragment-colors '1=#ff0000'
aseview cluster.xyz --fragments 'w*3,n*4,m*6,b*12' \
--fragment-opacities 'b=0.25' --opacity 0.9
aseview slab.traj # tags are picked up automatically
See the MolecularViewer API and the CLI reference for the full option list.