Skip to content

OpenBabel

Utility functions for OpenBabel.

Rings

Rings()

A class for storing information about aromatic rings in a molecule.

This class stores the center and normal vectors for each aromatic ring in a molecule. It also stores the indices of the atoms in each ring and the index of the first atom in each ring.

Source code in lahuta/utils/ob.py
35
36
37
38
39
def __init__(self) -> None:
    self._centers: list[NDArray[np.float32]] = []
    self._normals: list[NDArray[np.float32]] = []
    self._atoms: list[list[int]] = []
    self._first_atom_idx: list[int] = []

centers property

centers

Returns the centers of the rings.

Returns:

Type Description
NDArray[float32]

NDArray[np.float32]: The centers of the rings.

normals property

normals

Returns the normal vectors of the rings.

Returns:

Type Description
NDArray[float32]

NDArray[np.float32]: The normal vectors of the rings.

atoms property

atoms

Returns the atoms in the rings.

Returns:

Type Description
NDArray[int32]

NDArray[np.int32]: The atoms in the rings.

first_atom_idx property

first_atom_idx

Returns the index of the first atom in each ring.

Returns:

Type Description
NDArray[int32]

NDArray[np.int32]: The index of the first atom in each ring.

add_ring

add_ring(ob_ring)

Add a ring to the Rings object.

Parameters:

Name Type Description Default
ob_ring ObRingType

The ring to be added.

required
Source code in lahuta/utils/ob.py
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
def add_ring(self, ob_ring: ObRingType) -> None:
    """Add a ring to the Rings object.

    Args:
        ob_ring (ObRingType): The ring to be added.
    """
    ob_vector3_wrapper = ObVector3Wrapper(ob.vector3(), ob.vector3())
    center = ob_vector3_wrapper.center
    normal = ob_vector3_wrapper.normal
    ob_ring.findCenterAndNormal(center, normal, ob.vector3())
    center_coords: NDArray[np.float32] = np.array([center.GetX(), center.GetY(), center.GetZ()])
    normal_coords: NDArray[np.float32] = np.array([normal.GetX(), normal.GetY(), normal.GetZ()])
    atoms = sorted(ob_ring._path)  # noqa: SLF001

    self._centers.append(center_coords)
    self._normals.append(normal_coords)
    self._atoms.append(atoms)
    self._first_atom_idx.append(atoms[0])

get_bonded_atoms

get_bonded_atoms(mol)

Return an array of bonded atoms in a molecule.

Parameters:

Name Type Description Default
mol MolType

The molecule for which to find bonded atoms.

required

Returns:

Type Description
NDArray[int32]

NDArray[np.int32]: A numpy array containing the indices of bonded atoms.

Source code in lahuta/utils/ob.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
def get_bonded_atoms(mol: MolType) -> NDArray[np.int32]:
    """Return an array of bonded atoms in a molecule.

    Args:
        mol (MolType): The molecule for which to find bonded atoms.

    Returns:
        NDArray[np.int32]: A numpy array containing the indices of bonded atoms.
    """
    bonds: NDArray[np.int32] = np.zeros((mol.NumBonds(), 2), dtype=int)
    for ix, bond in enumerate(ob.OBMolBondIter(mol)):
        atom_idx1, atom_idx2 = bond.GetBeginAtomIdx(), bond.GetEndAtomIdx()
        bonds[ix, :] = (atom_idx1, atom_idx2) if atom_idx1 < atom_idx2 else (atom_idx2, atom_idx1)

    return bonds

enumerate_rings

enumerate_rings(mol)

Enumerate the aromatic rings in a molecule.

This function takes a molecule as input and returns a Rings object containing information about the aromatic rings in the molecule. It is used by atom-plane and plane-plane contact calculations.

Parameters:

Name Type Description Default
mol MolType

The molecule for which to enumerate the rings.

required

Returns:

Name Type Description
Rings Rings

A Rings object containing information about the rings in the molecule.

Source code in lahuta/utils/ob.py
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
def enumerate_rings(mol: MolType) -> Rings:
    """Enumerate the aromatic rings in a molecule.

    This function takes a molecule as input and returns a Rings object containing information
    about the aromatic rings in the molecule. It is used by `atom-plane` and `plane-plane`
    contact calculations.

    Args:
        mol (MolType): The molecule for which to enumerate the rings.

    Returns:
        Rings: A Rings object containing information about the rings in the molecule.
    """
    rings = Rings()
    for ob_ring in mol.GetSSSR():
        if ob_ring.IsAromatic():
            rings.add_ring(ob_ring)
    return rings