rdkit: How to show molecular's atoms number

6k views Asked by At

Hello1 I was trying to use rdkit pack to finish the work of displaying the molecular's atom numbers in Jupyter Notebook ,"import IPython.core.interactiveshell" and "import InteractiveShell" ,and "from rdkit.Chem.Draw import DrawingOptions" packs,then I was using "DrawingOptions.includeAtomNumbers=True" to work it ,but the result didn't display the atoms index at all . I don't konw what 's the reason lead to the atoms number didn't showed. So I want to please you to give an answer fittable. Thanks a lot!

3

There are 3 answers

4
rapelpy On

This works for me in a Jupyter Notebook:

from rdkit import Chem
from rdkit.Chem.Draw import IPythonConsole
from rdkit.Chem import Draw

smiles = 'O=C(C)Oc1ccccc1C(=O)O'
mol = Chem.MolFromSmiles(smiles)
Draw.MolToImage(mol, includeAtomNumbers=True)

Update

As of version 2020.03.1 this did not work anymore.

But you can annotate atoms directly.

from rdkit import Chem

smiles = 'O=C(C)Oc1ccccc1C(=O)O'
mol = Chem.MolFromSmiles(smiles)
for atom in mol.GetAtoms():
    atom.SetProp('atomLabel',str(atom.GetIdx()))
0
betelgeuse On

There are three ways to show atom numbers in the molecule.

from rdkit import Chem
from rdkit.Chem.Draw import IPythonConsole

def show_atom_number(mol, label):
    for atom in mol.GetAtoms():
        atom.SetProp(label, str(atom.GetIdx()+1))
    return mol

1. In place of the atoms

mol = Chem.MolFromSmiles('c1ccccc(C(N)=O)1')
show_atom_number(mol, 'atomLabel')

enter image description here

2. Along with the atoms

mol = Chem.MolFromSmiles('c1ccccc(C(N)=O)1')
show_atom_number(mol, 'molAtomMapNumber')

enter image description here

3. On top of the atoms

mol = Chem.MolFromSmiles('c1ccccc(C(N)=O)1')
show_atom_number(mol, 'atomNote')

enter image description here

If you want to change the numbers to display, change the part str(atom.GetIdx()+1) as per your requirements. Checkout my blog post on the same for more detailed explanation here

0
Artem Pavlovskii On

In version 2020.03.2.0 you could try

from rdkit.Chem.Draw import rdMolDraw2D
from rdkit import Chem

mol = Chem.MolFromSmiles('c1ccccc1O')
d = rdMolDraw2D.MolDraw2DCairo(250, 200)
d.drawOptions().addAtomIndices = True
d.DrawMolecule(mol)
d.FinishDrawing()
with open('atom_annotation_1.png', 'wb') as f:   
    f.write(d.GetDrawingText())