luna.wrappers.base module

class AtomWrapper(atm_obj, mol_obj=None)[source]

Bases: object

This class provides util functions to access atomic properties and other information from RDKit and Open Babel objects.

Parameters
Raises

AtomObjectTypeError – If the atom object is not an instance of MolWrapper, rdkit.Chem.rdchem.Mol, or openbabel.pybel.Molecule.

property atm_obj

The wrapped atom object.

Type

rdkit.Chem.rdchem.Atom or openbabel.OBAtom

get_atomic_invariants()[source]

Get the atomic invariants of this atom.

Atomic invariants are derived from ECFP 1 and E3FP 2 and consists of seven fields:

  • Number of heavy atoms;

  • Valence - Number of hydrogens;

  • Atomic number;

  • Isotope number;

  • Formal charge;

  • Number of hydrogens;

  • If the atom belongs to a ring or not.

Return type

list

get_atomic_mass()[source]

Get this atom’s atomic mass given by standard IUPAC average molar mass.

Return type

float

get_atomic_num()[source]

Get this atom’s atomic number.

Return type

int

get_bonds(wrapped=True)[source]

Get this atom’s bonds.

Parameters

wrapped (bool) – If True, wrap each bond with BondWrapper.

Return type

iterable of BondWrapper, rdkit.Chem.rdchem.Bond, or openbabel.OBBond

get_charge()[source]

Get this atom’s formal charge.

Return type

int

get_degree()[source]

Get this atom’s total degree.

Return type

int

get_h_count()[source]

Get the total number of hydrogens (implicit and explicit) bound to this atom.

Return type

int

get_id()[source]

Get this atom’s unique id.

When using RDKit, get_id() and get_idx() returns the same value.

Return type

int

get_idx()[source]

Get this atom’s internal index within a molecule.

Return type

int

get_isotope()[source]

Get this atom’s isotope number.

Return type

int

get_mass()[source]

Get this atom’s exact atomic mass, which can vary given the isotope.

Return type

float

get_neighbors(wrapped=True)[source]

Get all atoms that are bound to this atom.

Parameters

wrapped (bool) – If True, wrap each atom with AtomWrapper.

Return type

iterable of AtomWrapper, rdkit.Chem.rdchem.Atom, or openbabel.OBAtom

get_neighbors_number(only_heavy_atoms=False)[source]

Get the number of atoms bound to this atom.

Parameters

only_heavy_atoms (bool) – If True, count only heavy atoms.

Return type

int

get_parent(wrapped=True)[source]

Get the molecule that contains this atom.

Parameters

wrapped (bool) – If True, wrap the molecule with MolWrapper.

Return type

MolWrapper, rdkit.Chem.rdchem.Mol, or openbabel.pybel.Molecule

get_symbol()[source]

Get the element symbol of this atom.

Return type

str

get_valence()[source]

Get this atom’s total valence (implicit and explicit).

has_bond_type(bond_type)[source]

Check if this atom has a bond of type bond_type.

Parameters

bond_type (BondType)

Raises

IllegalArgumentError – If the informed bond type is not an instance of BondType.

has_only_bond_type(bond_type)[source]

Check if this atom has only bonds of type bond_type.

Parameters

bond_type (BondType)

Raises

IllegalArgumentError – If the informed bond type is not an instance of BondType.

is_aromatic()[source]

Check if this atom is aromatic.

is_in_ring()[source]

Check if this atom is in a ring.

is_openbabel_obj()[source]

Check if this atom is an Open Babel object.

is_rdkit_obj()[source]

Check if this atom is an RDKit object.

matches_smarts(smarts)[source]

Check if this atom matches the substructure through a SMARTS substructure search.

Note: currently, this function only works with molecules read with Open Babel.

Parameters

smarts (str) – A substructure defined as SMARTS.

Returns

Whether matches occurred. Return None if the molecule was read with RDKit.

Return type

bool or None

Examples

First, let’s read a molecule (glutamine) using Open Babel.

>>> from luna.wrappers.base import MolWrapper
>>> mol_obj = MolWrapper.from_smiles("N[C@@H](CCC(N)=O)C(O)=O", mol_obj_type="openbabel")

Now, we’ll loop over the list of atoms in the glutamine and check which atom is the amide’s carbon. To do so, we can call the function MolWrapper.get_atoms(), which, by default, returns AtomWrapper objects and then call matches_smarts().

>>> for atm in mol_obj.get_atoms():
>>>     print("%d       %s      %s" % (atm.get_idx(), atm.get_symbol(), atm.matches_smarts("C(N)(C)=O")))
1   N   False
2   C   False
3   C   False
4   C   False
5   C   True
6   N   False
7   O   False
8   C   False
9   O   False
10  O   False
property parent

The molecule that contains this atom.

Type

MolWrapper, rdkit.Chem.rdchem.Mol, or openbabel.pybel.Molecule

set_as_aromatic(is_aromatic)[source]

Set whether this atom is aromatic or not.

Parameters

is_aromatic (bool)

set_charge(charge)[source]

Set the formal charge of this atom.

Parameters

charge (int)

set_in_ring(in_ring)[source]

Set whether this atom belongs to a ring or not.

Parameters

in_ring (bool)

unwrap()[source]

Return the original atomic object.

Return type

rdkit.Chem.rdchem.Atom or openbabel.OBAtom

class BondType(value)[source]

Bases: enum.Enum

An enumeration of bond types available at RDKit.

AROMATIC = 12
DATIVE = 17
DATIVEL = 18
DATIVEONE = 16
DATIVER = 19
DOUBLE = 2
FIVEANDAHALF = 11
FOURANDAHALF = 10
HEXTUPLE = 6
HYDROGEN = 14
IONIC = 13
ONEANDAHALF = 7
OTHER = 20
QUADRUPLE = 4
QUINTUPLE = 5
SINGLE = 1
THREEANDAHALF = 9
THREECENTER = 15
TRIPLE = 3
TWOANDAHALF = 8
UNSPECIFIED = 0
ZERO = 21
class BondWrapper(bond_obj)[source]

Bases: object

This class provides util functions to access bond properties and other information from RDKit and Open Babel objects.

Parameters

bond_obj (BondWrapper, rdkit.Chem.rdchem.Bond, or openbabel.OBBond) – A bond to wrap.

Raises

BondObjectTypeError – If the bond object is not an instance of BondWrapper, rdkit.Chem.rdchem.Bond, or openbabel.pybel.OBBond.

property bond_obj

The wrapped bond object.

Type

rdkit.Chem.rdchem.Bond or openbabel.OBBond

get_begin_atom(wrapped=True)[source]

Return the bond’s first atom.

Parameters

wrapped (bool) – If True, wrap the atom with AtomWrapper.

Return type

AtomWrapper, rdkit.Chem.rdchem.Atom, or openbabel.OBAtom

get_bond_type()[source]

Get the bond type (e.g., single bond).

Return type

BondType

get_end_atom(wrapped=True)[source]

Return the bond’s second atom.

Parameters

wrapped (bool) – If True, wrap the atom with AtomWrapper.

Return type

AtomWrapper, rdkit.Chem.rdchem.Atom, or openbabel.OBAtom

get_partner_atom(atm, wrapped=True)[source]

Get the partner atom that forms this bond with atm.

Parameters
Return type

AtomWrapper, rdkit.Chem.rdchem.Atom, or openbabel.OBAtom

is_aromatic()[source]

Check if this bond is aromatic or not.

is_openbabel_obj()[source]

Check if this bond is an Open Babel object.

is_rdkit_obj()[source]

Check if this bond is an RDKit object.

set_as_aromatic(is_aromatic)[source]

Set if this bond is aromatic or not.

Parameters

is_aromatic (bool)

set_bond_type(bond_type)[source]

Set the type of the bond as a bond_type.

Parameters

bond_type (BondType)

unwrap()[source]

Return the original bond object.

Return type

rdkit.Chem.rdchem.Bond or openbabel.OBBond

class MolWrapper(mol_obj)[source]

Bases: object

This class provides util functions to access molecule properties and other information from RDKit and Open Babel objects.

Parameters

mol_obj (MolWrapper, rdkit.Chem.rdchem.Mol, or openbabel.pybel.Molecule) – A molecule to wrap.

Raises

MoleculeObjectTypeError – If the molecular object is not an instance of MolWrapper, rdkit.Chem.rdchem.Mol, or openbabel.pybel.Molecule.

as_openbabel()[source]

If the molecule is an RDKit object, convert it to an Open Babel object.

as_rdkit()[source]

If the molecule is an Open Babel object, convert it to an RDKit object.

classmethod from_mol_block(block, mol_format, mol_obj_type='rdkit')[source]

Initialize a molecule from a string block.

Parameters
  • block (str) – The molecular string block.

  • mol_format (str) – Define the format in which the molecule is represented (e.g., ‘mol2’ or ‘mol’).

  • mol_obj_type ({‘rdkit’, ‘openbabel’}) – Define which library (RDKit or Open Babel) to use to parse the molecular block. The default value is RDKit.

Return type

MolWrapper

classmethod from_smiles(smiles, mol_obj_type='rdkit', name=None)[source]

Initialize a molecule from a SMILES string.

Parameters
  • smiles (str) – The SMILES string. Define the format in which the molecule is represented (e.g., ‘mol2’ or ‘mol’).

  • mol_obj_type ({‘rdkit’, ‘openbabel’}) – Define which library (RDKit or Open Babel) to use to parse the molecular block. The default value is RDKit.

  • name (str, optional) – A name to identify the molecule.

Return type

MolWrapper

Raises

MoleculeObjectError – If it could not create a molecule from the provided SMILES.

Examples

>>> from luna.wrappers.base import MolWrapper
>>> mol_obj = MolWrapper.from_smiles("N[C@@H](CCC(N)=O)C(O)=O", mol_obj_type="openbabel", name="Glutamine")
get_atom_coord_by_id(atm_id)[source]

Get the coordinates of an atom given by its id.

Returns

Atomic coordinates (x, y, z).

Return type

array_like of float (size 3)

get_atoms(wrapped=True)[source]

Get all molecule’s atoms.

Parameters

wrapped (bool) – If True, wrap all atoms with AtomWrapper.

Return type

iterable of AtomWrapper, rdkit.Chem.rdchem.Atom, or openbabel.OBAtom

get_bonds(wrapped=True)[source]

Get all molecule’s bonds.

Parameters

wrapped (bool) – If True, wrap all bonds with BondWrapper.

Return type

iterable of BondWrapper, rdkit.Chem.rdchem.Bond, or openbabel.OBBond

get_name()[source]

Get the molecule name.

Return type

str

get_num_heavy_atoms()[source]

Get the number of heavy atoms in this molecule.

get_obj_type()[source]

Get the object type (“rdkit” or “openbabel”).

has_name()[source]

Check if this molecule has a name.

is_openbabel_obj()[source]

Check if this molecule is an Open Babel object.

is_pybel_obj()[source]

Check if this molecule is a Pybel object.

is_rdkit_obj()[source]

Check if this molecule is an RDKit object.

property mol_obj

The wrapped molecular object.

Type

rdkit.Chem.rdchem.Mol or openbabel.pybel.Molecule

set_name(name)[source]

Set a name to this molecule.

Parameters

name (str)

to_mol_block()[source]

Return the MOL string block for this molecule.

to_pdb_block()[source]

Return the PDB string block for this molecule.

to_smiles()[source]

Return the canonical SMILES string for this molecule.

unwrap()[source]

Return the original molecular object.

Return type

rdkit.Chem.rdchem.Mol or openbabel.pybel.Molecule.

class OBBondType(value)[source]

Bases: enum.Enum

An enumeration of bond types available at Open Babel.

AROMATIC = 5
DOUBLE = 2
SINGLE = 1
TRIPLE = 3

References

1

Rogers D & Hahn M. Extended-connectivity fingerprints. J. Chem. Inf. Model. 50: 742-54 (2010). Access the paper

2

Axen SD, Huang XP, Caceres EL, Gendelev L, Roth BL, Keiser MJ. A Simple Representation Of Three-Dimensional Molecular Structure. J. Med. Chem. 60 (17): 7393–7409 (2017). Access the paper Access the preprint on bioRxiv Access the recommendation on F1000Prime