# How to give different color to each type of heteroatom during substructure matching in RdKit

Substructure matching in RdKit generally highlights the matched component in red color over the original molecule. The code for achieving the same is as follows.

# MWE for a Jupyter notebook:
from rdkit import Chem
from rdkit.Chem.Draw import IPythonConsole
from rdkit.Chem.Draw import rdDepictor

rdDepictor.SetPreferCoordGen(True)
IPythonConsole.molSize = (250,200)
from rdkit.Chem import AllChem

from rdkit.Chem import Draw
IPythonConsole.ipython_useSVG=True

# ----

smi = 'CCC1NC(=O)C(C(O)C(C)CCCC(=O)OC)N(C)C(=O)C(C(C)C)N(C)C(=O)C(CC(C)C)N(C)C(=O)C(CC(C)C)N(C)C(=O)C(C)NC(=O)C(C)NC(=O)C(CC(C)C)N(C)C(=O)C(C(C)C)NC(=O)C(CC(C)C)N(C)C(=O)CN(C)C1=O'
core = Chem.MolFromSmiles('C1C(=O)NCC(=O)NCC(=O)NCC(=O)NCC(=O)NCC(=O)NCC(=O)NCC(=O)NCC(=O)NCC(=O)NCC(=O)N1')

newm = Chem.MolFromSmiles(smi)
rdDepictor.Compute2DCoords(newm)
newm.GetSubstructMatch(core)

newm


Source of the code

Here, if we want to highlight different heteroatoms (like N and O) with different colors, what would be the best way??

Edit:

For clarification purposes. The requirement is to change the color of the highlight and not the color of the font of the atoms.

• For future reference: Please enclose snippets of code by three grave characters. Not only will code be visible as such, without unwanted line breaks applied to normal text, there will be (language specific) syntax highlighting, too. And it would be better to present a self-sufficient minimal working example (MWE), like e.g. in posts on tex.se. Nov 30 '21 at 12:27
• chemistry.stackexchange.com/questions/160056/…
– user119527
Dec 4 '21 at 11:42

IPythonConsole.molSize = (400,400)

• In addition, you can also change the color of the highlight: IPythonConsole.drawOptions.setHighlightColour((0.8,0.8,0.8)) Nov 30 '21 at 17:22