vtkmodules.util.molecule#
Pythonic API for vtkMolecule.
Adds iteration, len, array properties, and interop with ASE and RDKit::
from vtkmodules.vtkCommonDataModel import vtkMolecule
# Construction from arrays:
mol = vtkMolecule(atomic_numbers=[1, 1, 8],
positions=[[0, 0, 0], [1, 0, 0], [0.5, 0.8, 0]])
mol = vtkMolecule(symbols=['H', 'H', 'O'],
positions=[[0, 0, 0], [1, 0, 0], [0.5, 0.8, 0]])
# Sequence protocol:
len(mol) # atom count
mol[0] # vtkAtom proxy
for atom in mol: ... # iterate atoms
# Array properties:
mol.positions # (N, 3) float32 numpy array
mol.atomic_numbers # (N,) uint16 numpy array
mol.bond_orders # (N_bonds,) uint16 numpy array
# Views:
mol.atoms[2] # indexable atom view
mol.bonds[0] # indexable bond view
# Data:
mol.atom_data # vtkDataSetAttributes
mol.bond_data # vtkDataSetAttributes
# Interop:
ase_atoms = mol.to_ase()
mol2 = vtkMolecule().from_ase(ase_atoms)
rdmol = mol.to_rdkit()
mol3 = vtkMolecule().from_rdkit(rdmol)
Module Contents#
Classes#
Indexable, iterable view of atoms in a vtkMolecule. |
|
Indexable, iterable view of bonds in a vtkMolecule. |
|
Functions#
Return a lazily-created singleton vtkPeriodicTable. |
Data#
API#
- vtkmodules.util.molecule._periodic_table#
None
- vtkmodules.util.molecule._get_periodic_table()#
Return a lazily-created singleton vtkPeriodicTable.
- vtkmodules.util.molecule._RDKIT_BOND_TYPE_TO_VTK_ORDER#
None
- vtkmodules.util.molecule._VTK_ORDER_TO_RDKIT_BOND_TYPE#
None
- class vtkmodules.util.molecule._AtomView(mol)#
Indexable, iterable view of atoms in a vtkMolecule.
Initialization
- __slots__#
(‘_mol’,)
- __len__()#
- __getitem__(key)#
- __iter__()#
- __repr__()#
- class vtkmodules.util.molecule._BondView(mol)#
Indexable, iterable view of bonds in a vtkMolecule.
Initialization
- __slots__#
(‘_mol’,)
- __len__()#
- __getitem__(key)#
- __iter__()#
- __repr__()#
- class vtkmodules.util.molecule._MoleculeMixin(*args, atomic_numbers=None, symbols=None, positions=None, **kwargs)#
Initialization
- __len__()#
- __getitem__(key)#
- __iter__()#
- property atoms#
Indexable, iterable view of atoms.
- property bonds#
Indexable, iterable view of bonds.
- property positions#
Atom positions as an (N, 3) float32 numpy array.
This is a live view into VTK memory; mutating it changes the molecule in place (call
Modified()afterwards to notify the pipeline). The view is invalidated if the atom array is reallocated (e.g. byAppendAtom), so do not hold onto it across mutations. Use thepositionssetter for a safe, length-checked bulk assignment.
- property atomic_numbers#
Atomic numbers as an (N,) uint16 numpy array.
This is a live view into VTK memory; mutating it changes the molecule in place (call
Modified()afterwards to notify the pipeline). The view is invalidated if the atom array is reallocated (e.g. byAppendAtom), so do not hold onto it across mutations. Use theatomic_numberssetter for a safe, length-checked bulk assignment.
- property bond_orders#
Bond orders as an (N_bonds,) uint16 numpy array.
This is a live view into VTK memory; mutating it changes the molecule in place (call
Modified()afterwards to notify the pipeline). The view is invalidated if the bond array is reallocated (e.g. byAppendBond), so do not hold onto it across mutations.
- property symbols#
Element symbols as a list of strings.
Requires vtkDomainsChemistry.
- property formula#
Molecular formula in Hill system order.
Requires vtkDomainsChemistry.
- property atom_data#
Atom data arrays (vtkDataSetAttributes).
- property bond_data#
Bond data arrays (vtkDataSetAttributes).
- append(atomic_number_or_symbol, position)#
Append an atom and return the vtkAtom proxy.
Parameters
atomic_number_or_symbol : int or str Atomic number or element symbol. position : sequence of 3 floats (x, y, z) position.
- add_bond(atom1, atom2, order=1)#
Add a bond between two atoms.
Parameters
atom1, atom2 : int Atom indices. order : int Bond order (1=single, 2=double, 3=triple).
- property lattice#
Unit cell lattice as a 3x3 numpy array, or None if not set.
VTK stores lattice vectors as columns of a 3x3 matrix.
- property lattice_origin#
Lattice origin as a (3,) numpy array, or None if no lattice is set.
The origin is only meaningful together with a lattice, so this mirrors
latticeand returns None whenHasLattice()is false.
- __repr__()#
- to_ase()#
Convert to an ASE Atoms object.
Positions and atomic numbers are copied. If a lattice is set, it is transferred as the ASE cell (transposed, since VTK stores vectors as columns and ASE as rows) and
pbc=True.Bonds are not transferred (ASE does not store bonds).
- from_ase(atoms)#
Populate this molecule from an ASE Atoms object.
Existing atoms and bonds are cleared first. Bonds are not set (use
vtkSimpleBondPerceiverto infer them). Returnsselfso it can be chained, e.g.vtkMolecule().from_ase(atoms).
- to_rdkit(sanitize=False)#
Convert to an RDKit Mol object.
Atoms, bonds, and 3D coordinates (as a conformer) are transferred. Bond order mapping: 1→SINGLE, 2→DOUBLE, 3→TRIPLE, 4→AROMATIC.
Parameters
sanitize : bool If True, run
Chem.SanitizeMolon the result. The returned mol is otherwise unsanitized: perception of rings, valence, and aromaticity has not been run, and some downstream RDKit operations require a sanitized mol. Sanitization is off by default because molecules derived from geometry or bond perceivers need not satisfy RDKit’s valence model, andSanitizeMolwould raise on them.
- from_rdkit(rdkit_mol, conformer_id=-1)#
Populate this molecule from an RDKit Mol object.
Existing atoms and bonds are cleared first. Returns
selfso it can be chained, e.g.vtkMolecule().from_rdkit(rdmol).Parameters
rdkit_mol : rdkit.Chem.Mol Source molecule. conformer_id : int Which conformer to use for positions (-1 = first/default). If no conformer exists, positions default to (0, 0, 0).
- class vtkmodules.util.molecule.Molecule(*args, atomic_numbers=None, symbols=None, positions=None, **kwargs)#
Bases:
vtkmodules.util.molecule._MoleculeMixin,vtkmodules.vtkCommonDataModel.vtkMoleculeInitialization