Removing elements and structures#

This page documents view.remove(...).

view.remove(...) removes atoms and/or structures from the molecular system behind the view.

It is a live operation: it updates view.molsys and refreshes the viewer so the change is reflected in the browser.

For more details and additional examples, see the MolSysMT tutorial: Remove.

A practical guideline: whenever you can, prefer removing by selection rather than by hard-coded atom indices. Atom indices can change after removals, and selections are usually more robust.

Elements and structures#

remove supports two complementary operations, and you can use either one depending on what you are trying to do:

  • Remove atoms (and therefore any higher-level elements that become empty) using selection=....

  • Remove structures using structure_indices=....

If you need a reminder of the available element levels (atom, group, molecule, …), here is a quick reference:

Element

Meaning

atom

Atoms of the molecular system.

group

First supra-atomic chemical level: amino acids, waters, ions, lipids, etc.

component

A covalently connected set of atoms (a covalent component).

molecule

A single molecular instance (one water molecule, one benzene molecule, etc.).

chain

An author-defined grouping that can vary across sources; it may represent anything from a polymer chain to a higher-level supra-molecular partition.

entity

Molecular nature/type. For example, you can have two entities (water and benzene) and six molecules (four waters and two benzenes).

system

The whole molecular system.

bond

Covalent connectivity between atoms (may be absent or inferred depending on the input).

Example: remove water#

We start by checking how many atoms we have, and then we remove water molecules.

This is a destructive operation on the loaded system: if you want to undo it, reload the view (or re-create it from the original data).

import molsysviewer as viewer

view = viewer.demo["181L"]
view.show()
view.get(element="system", n_atoms=True, n_waters=True, output_type="dictionary")
{'n_atoms': 1441, 'n_waters': 136}
view.remove(selection='molecule_type=="water"')
view.get(element="system", n_atoms=True, n_waters=True, output_type="dictionary")
{'n_atoms': 1305, 'n_waters': 0}

You have to find something like this in your viewer:

Example: remove structures#

Some systems include multiple structures (for example, a trajectory or multiple coordinate sets). In that case, you can remove structures by index (0-based).

In the demo below, the molecular system includes many structures, and we remove a few of them.

view = viewer.demo["pentalanine"]
view.show()
view.get(element="system", n_structures=True)
200
view.remove(structure_indices=[0, 1, 2])
view.get(element="system", n_structures=True)
197

Check your first structure now and the total number of structures in the viewer: