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!
Asked
Active
Viewed 5,371 times
3 Answers
5
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')
2. Along with the atoms
mol = Chem.MolFromSmiles('c1ccccc(C(N)=O)1')
show_atom_number(mol, 'molAtomMapNumber')
3. On top of the atoms
mol = Chem.MolFromSmiles('c1ccccc(C(N)=O)1')
show_atom_number(mol, 'atomNote')
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

betelgeuse
- 1,136
- 3
- 13
- 25
4
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()))

rapelpy
- 1,684
- 1
- 11
- 14
-
Anyone knows since when this drawing feature is included in rdkit? – KareemJ Apr 09 '20 at 23:36
-
1@KareemJeiroudi I do not know since when this feature is included, but it seems that in the actual version `2020.03.1` it is broken. – rapelpy Apr 10 '20 at 10:07
-
@repelpy one of my colleagues encountered the same problem with the 2020 release – KareemJ Apr 10 '20 at 19:35
-
@KareemJeiroudi Updated the answer – rapelpy Apr 20 '20 at 16:04
1
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())

Artem Pavlovskii
- 19
- 2