from rdkit import Chem
from rdkit.Chem import AllChem
from rdkit.Chem.Draw import IPythonConsole
from rdkit.Chem.Draw import MolDrawing, DrawingOptions
DrawingOptions.bondLineWidth=1.8
DrawingOptions.atomLabelFontSize=14
DrawingOptions.includeAtomNumbers=False
Create new molecule from a SMILES string:
start_mol = Chem.MolFromSmiles('c1cc(CCCO)ccc1')
start_mol
Let's try to delete some atoms by matching a simple pattern:
truncate = Chem.DeleteSubstructs(start_mol,Chem.MolFromSmiles('O'))
truncate
Now we will build a fragment that we want to connect:
mod = Chem.MolFromSmiles('OC=O')
mod
Combine them into one molecule object:
combo = Chem.CombineMols(truncate,mod)
combo
Here the molecules are merged into a single molecule object, but are still separate fragments. Let's see what this looks like in a SMILES string:
Chem.MolToSmiles(combo)
The period (.
) indicates that the molecule contains two unbonded fragments. No such obvious distinction appears in the connection table:
print Chem.MolToMolBlock(combo)
One way to stitch these together is to make an editable copy of the molecule object, add a bond between atoms by giving the two indices of atoms to be bonded, and then turning this back into a "normal" molecule object. Note that indices are zero indexed even though the are 1-indexed in the mol block above
edcombo = Chem.EditableMol(combo)
DrawingOptions.includeAtomNumbers=True
combo
edcombo.AddBond(5,10,order=Chem.rdchem.BondType.SINGLE)
Before we can do much of anything to our newly joined molecule, we need to copy the information back into a "normal" molecule object.
back = edcombo.GetMol()
back
Replacing one substructure with another¶
If our reason to stitch two molecules together, as in the example above, it is easier to use RDKit to replace one functional group in the molecule with another.
DrawingOptions.includeAtomNumbers=False
start_mol
mod_mol = Chem.ReplaceSubstructs(start_mol,
Chem.MolFromSmiles('CO'),
Chem.MolFromSmiles('C(=O)O'),
replaceAll=True)
mod_mol[0]
We can start to see how RDKit will be useful for handling chemical reactions.