1. Installing the CDPL Python bindings

To be able to follow this tutorial the CDPL Python bindings need to be installed on your computer. The most straightforward way to accomplish this task is to install the latest official release deposited on PyPI using the pip command as follows:

pip install cdpkit

Other possible options for installing the Python bindings can be found here.

2. Working with molecules

The primary data structure for the in-memory representation of molecules (class Chem.BasicMolecule) as well as all basic processing functionality is located in package CDPL.Chem. Importing the package as shown below will make all contained classes and functions accessible via the prefix Chem.*.

import CDPL.Chem as Chem

An initally empty molecule object can then be created as follows:

mol = Chem.BasicMolecule()

The number of (explicit) atoms can be queried either by acessing the property numAtoms or by calling the method getNumAtoms():

mol.numAtoms
# or
mol.getNumAtoms()
0

In the same manner, the number of bonds can be retrieved by:

mol.numBonds
# or
mol.getNumBonds()
0

Atoms are created by calling the method addAtom():

a = mol.addAtom()

The method returns a Chem.BasicAtom object which is owned by the creating Chem.BasicMolecule instance mol. The created atom does not yet possess any chemical properties like element, formal charge, and so on. The values of these properties need to be set explicitly by invoking dedicated property functions which take the atom and desired value of the property as arguments. For example

Chem.setType(a, Chem.AtomType.C)

will set the type property of the atom to the atomic number of carbon. The value of the type property can be retrieved by the associated function

Chem.getType(a)
6

In a similar fashion, bonds are created by calling the method addBond() which expects the indices (zero-based) of the two atoms to connect as arguments:

Chem.setType(mol.addAtom(), Chem.AtomType.C) # add second carbon atom

b = mol.addBond(0, 1)

The method returns a Chem.BasicBond object which is also owned by the creating Chem.BasicMolecule instance mol. As with atoms, the created bond does not yet have any properties set. To set the bond order to a value of 2 (= double bond) the property function Chem.setOrder() needs to be invoked:

Chem.setOrder(b, 2)

A previously set bond order property value can be accessed by the accompanying getter function Chem.getOrder():

Chem.getOrder(b)
2