stk.Molecule
- class stk.Molecule(atoms, bonds, position_matrix)[source]
Bases:
object
An abstract base class for molecules.
Notes
You might notice that some of the methods of this abstract base class are implemented. This is purely for convenience when implementing subclasses. The implemented public methods are simply default implementations, which can be safely ignored or overridden, when implementing subclasses. Any private methods are implementation details of these default implementations.
Examples
Aligning a Molecule with a Vector
You want to rotate a molecule, such that it is aligned along with a specific direction.
import stk import numpy as np molecule1 = stk.BuildingBlock('CCCCC') # Align molecule1 along x-axis. molecule1 = molecule1.with_rotation_between_vectors( start=molecule1.get_direction(), target=np.array([1., 0., 0.]), origin=molecule1.get_centroid(), ) molecule2 = stk.ConstructedMolecule( topology_graph=stk.polymer.Linear( building_blocks=( stk.BuildingBlock( smiles='BrCCCBr', functional_groups=(stk.BromoFactory(), ), ), ), repeating_unit='A', num_repeating_units=15, ), ) # Align molecule2 along the [1, 4, -3] vector. molecule2 = molecule2.with_rotation_between_vectors( start=molecule2.get_direction(), target=np.array([1., 4., -3.]), origin=molecule2.get_centroid(), )
Aligning a Molecule along a Plane
You want to place the benzene flat along the xy plane.
import stk import numpy as np benzene = stk.BuildingBlock('c1ccccc1') benzene = benzene.with_rotation_between_vectors( start=benzene.get_plane_normal(), target=np.array([0., 0., 1.]), origin=benzene.get_centroid(), )
Initialize a
Molecule
.- Parameters:
Methods
Return a clone.
Yield the positions of atoms.
Yield the atoms in the molecule, ordered by id.
Yield the bond in the molecule.
Map the id of each atom to its id under canonical ordering.
Return the centroid.
Return a vector of best fit through the atoms.
Return the maximum diameter.
Return the number of atoms in the molecule.
Return the number of bonds in the molecule.
Return the normal to the plane of best fit.
Return a matrix holding the atomic positions.
Return an
rdkit
representation.Return a clone, with canonically ordered atoms.
Return a clone with its centroid at position.
Return a displaced clone.
Return a clone with atomic positions set by position_matrix.
Return a rotated clone.
Return a rotated clone.
Return a rotated clone.
Return a clone, with its structure taken from a file.
Write the structure to a file.
- get_centroid(atom_ids=None)[source]
Return the centroid.
- Parameters:
atom_ids (int | list[int] | None) – The ids of atoms which are used to calculate the centroid. If
None
, all atoms are used.- Returns:
The centroid of atoms specified by atom_ids.
- Raises:
ValueError – If atom_ids has a length of
0
.- Return type:
- get_direction(atom_ids=None)[source]
Return a vector of best fit through the atoms.
- Parameters:
atom_ids (int | list[int] | None) – The ids of atoms which should be used to calculate the vector. If
None
, all atoms are used.- Returns:
The vector of best fit.
- Raises:
ValueError – If atom_ids has a length of
0
.- Return type:
- get_maximum_diameter(atom_ids=None)[source]
Return the maximum diameter.
This method does not account for the van der Waals radius of atoms.
- Parameters:
atom_ids (int | list[int] | None) – The ids of atoms which are considered when looking for the maximum diameter. If
None
, all atoms are used.- Returns:
The maximum diameter in the molecule.
- Raises:
ValueError – If atom_ids has a length of
0
.- Return type:
- get_num_atoms()[source]
Return the number of atoms in the molecule.
- Returns:
The number of atoms in the molecule.
- Return type:
- get_num_bonds()[source]
Return the number of bonds in the molecule.
- Returns:
The number of bonds in the molecule.
- Return type:
- get_plane_normal(atom_ids=None)[source]
Return the normal to the plane of best fit.
- Parameters:
atom_ids (int | list[int] | None) – The ids of atoms which should be used to calculate the plane. If
None
, all atoms are used.- Returns:
Vector orthonormal to the plane of the molecule.
- Raises:
ValueError – If atom_ids has a length of
0
.- Return type:
- get_position_matrix()[source]
Return a matrix holding the atomic positions.
- Returns:
The array has the shape
(n, 3)
. Each row holds the x, y and z coordinates of an atom.- Return type:
- to_rdkit_mol()[source]
Return an
rdkit
representation.- Returns:
The molecule in
rdkit
format.- Return type:
Mol
- with_canonical_atom_ordering()[source]
Return a clone, with canonically ordered atoms.
- Returns:
The clone.
- Return type:
- with_centroid(position, atom_ids=None)[source]
Return a clone with its centroid at position.
- Parameters:
- Returns:
A clone with its centroid at position.
- Return type:
- with_position_matrix(position_matrix)[source]
Return a clone with atomic positions set by position_matrix.
- with_rotation_about_axis(angle, axis, origin)[source]
Return a rotated clone.
The clone is rotated by angle about axis on the origin.
- with_rotation_between_vectors(start, target, origin)[source]
Return a rotated clone.
The rotation is equal to a rotation from start to target.
Given two direction vectors, start and target, this method applies the rotation required transform start to target onto the clone. The rotation occurs about the origin.
For example, if the start and target vectors are 45 degrees apart, a 45 degree rotation will be applied to the clone. The rotation will be along the appropriate direction.
The great thing about this method is that you as long as you can associate a geometric feature of the molecule with a vector, then the clone can be rotated so that this vector is aligned with target. The defined vector can be virtually anything. This means that any geometric feature of the molecule can be easily aligned with any arbitrary direction.
- with_rotation_to_minimize_angle(start, target, axis, origin)[source]
Return a rotated clone.
The clone is rotated by the rotation required to minimize the angle between start and target.
Note that this function will not necessarily overlay the start and target vectors. This is because the possible rotation is restricted to the axis.
- Parameters:
- Returns:
A rotated clone.
- Return type:
- Raises:
ValueError – If target has a magnitude of 0. In this case it is not possible to calculate an angle between start and target.
- with_structure_from_file(path, extension=None)[source]
Return a clone, with its structure taken from a file.
Multiple file types are supported, namely:
.mol
,.sdf
- MDL V2000 and V3000 files.xyz
- XYZ files.mae
- Schrodinger Maestro files.coord
- Turbomole files.pdb
- PDB files
- Parameters:
- Returns:
A clone with atomic positions found in path.
- Return type: