Dear Hao,

I don't think that's possible through the current rdMolDraw2D API.
However, you may obtain that effect fiddling a bit with the SVG XML text:

import re
import xml.etree.ElementTree as ET
from rdkit import Chem
from rdkit.Chem.Draw import rdMolDraw2D
from IPython.display import SVG

mol =
Chem.MolFromSmiles('CN1C(=O)C2=C(ON=C2c2ccccc2Cl)C(Cl)=C1c1cnc(N2CCC(C)(N)CC2)nn1')
mol
[image: image.png]
red_query = Chem.MolFromSmarts("NC(C)1CCN(CC1)[c](n)nn")
red_match = mol.GetSubstructMatch(red_query)
red_match
(28, 26, 27, 25, 24, 23, 30, 29, 22, 21, 31, 32)

green_query =
Chem.MolFromSmarts("[#6]~[#6]~[#6]1~[#7](~[#6])~[#6](~[#8])~[#6]2~[#6](c3ccccc(Cl)3)~[#7]~[#8]~[#6]~2~[#6]~1")
green_match = mol.GetSubstructMatch(green_query)
green_match
(20, 19, 18, 1, 0, 2, 3, 4, 8, 9, 10, 11, 12, 13, 14, 15, 7, 6, 5, 16)

drawer = rdMolDraw2D.MolDraw2DSVG(400, 300)
mol_draw = rdMolDraw2D.PrepareMolForDrawing(mol)
drawer.drawOptions().bondLineWidth = 3
drawer.drawOptions().prepareMolsBeforeDrawing = False
drawer.DrawMolecule(mol)
drawer.FinishDrawing()

def set_match_color(svg_text, match_color_list, no_match_color="#000000"):
    path_class_regex = re.compile(r"bond-(\d+) atom-(\d+) atom-(\d+)")
    path_style_regex = re.compile(r"^(.*stroke:)(#[0-9A-F]{6})(;.*)$")
    svg_tree = ET.fromstring(svg_text)
    for path in svg_tree.findall("{http://www.w3.org/2000/svg}path";):
        path_class = path.get("class")
        if not path_class:
            continue
        m = path_class_regex.match(path_class)
        if not m:
            continue
        ai1 = int(m.group(2))
        ai2 = int(m.group(3))
        new_color = no_match_color
        for match, color in match_color_list:
            if (ai1 in match and ai2 in match):
                new_color = color
                break
        path_style = path.get("style")
        if not path_style:
            continue
        path_style = path_style_regex.sub(f"\\g<1>{new_color}\\g<3>",
path_style)
        path.set("style", path_style)
    return ET.tostring(svg_tree)

svg_text = drawer.GetDrawingText()
svg_text = set_match_color(svg_text, ((red_match, "#ffacac"), (green_match,
"#50a850")))
SVG(svg_text)
[image: image.png]

Cheers,
p.

On Thu, Oct 28, 2021 at 11:01 AM hwang929 <hwang...@163.com> wrote:

> Hi,
> I would like to ask if I can show molecules like 1 in the figure below in
> addition to the conventional highlighting method like figure 2 when using
> rdkit to draw molecules?
>
> Thanks
> Kind regards,
> Hao Wang
>                                                   figure 1
>                                                 figure 2
>
>
>
> _______________________________________________
> Rdkit-discuss mailing list
> Rdkit-discuss@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/rdkit-discuss
>
_______________________________________________
Rdkit-discuss mailing list
Rdkit-discuss@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rdkit-discuss

Reply via email to