How to filter and remove atoms#

Goal#

Drop atoms from a Molecule in place — either keep a subset (filter()) or discard a subset (remove()).

Minimal example#

from moleculekit.molecule import Molecule

mol = Molecule("3PTB")

# Keep only protein atoms (discards water, ligand, etc.)
mol.filter("protein")

# Alternatively, explicitly remove water
mol.remove("water")

Parameters that matter#

Parameter

Type

Default

What it does

selection

str, boolean np.ndarray, or integer np.ndarray

required

Atoms to keep (filter) or atoms to remove (remove)

Common variations#

# Use a precomputed boolean mask with filter
protein_mask = mol.atomselect("protein")
mol.filter(protein_mask)
# Remove a residue range by resid
mol.remove("resid 200 to 300")

Gotchas#

  • filter() keeps the selection; remove() drops it — the semantics are opposite, which is a common source of confusion.

  • Both methods mutate the molecule in place and update mol.bonds to reflect the new atom indices.

  • After either call, any precomputed boolean masks or index arrays are stale and must be recomputed.

  • Both methods accept the full string / boolean-mask / integer-index trio (each input is normalized through atomselect()).

See also#