Hi Joey,

not sure if by "color" you mean text labelling or actually mapping a
property to a color.
Anyway, here's some code for either use case.
The text labelling is easy, the individual bond coloring can be done by
fiddling with the SVG text.

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

mol =
Chem.MolFromSmiles('CN1C(=O)C2=C(ON=C2c2ccccc2Cl)C(Cl)=C1c1cnc(N2CCC(C)(N)CC2)nn1')
rdDepictor.Compute2DCoords(mol)
rdDepictor.NormalizeDepiction(mol)
rdDepictor.StraightenDepiction(mol)

# this is to assign a text label to each bond
for b in mol.GetBonds():
    bond_prop = int(b.GetBondType())
    b.SetIntProp("bondNote", bond_prop)

drawer = rdMolDraw2D.MolDraw2DSVG(600, 300)
drawer.DrawMolecule(mol)
drawer.FinishDrawing()
svg = drawer.GetDrawingText()

SVG(svg)
[image: image.png]
# this is to assign a color to each bond
for b in mol.GetBonds():
    b.ClearProp("bondNote")

int_bond_types = sorted(map(int, Chem.BondType.values))
min_bond_type = int_bond_types[0]
max_bond_type = int_bond_types[-1]
bond_type_range = max_bond_type - min_bond_type

def bond_type_to_rgb(bt):
    rgb = colorsys.hsv_to_rgb((int(bt) - min_bond_type) / bond_type_range,
.8, .8)
    return "#" + "".join("{0:02x}".format(round(c * 255.0)) for c in rgb)

bond_colors = [bond_type_to_rgb(b.GetBondType()) for b in mol.GetBonds()]
drawer = rdMolDraw2D.MolDraw2DSVG(600, 300)
drawer.drawOptions().bondLineWidth = 3
drawer.DrawMolecule(mol)
drawer.FinishDrawing()

def set_bond_colors(svg_text, bond_colors):
    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
        bi = int(m.group(1))
        if bi >= len(bond_colors):
            continue
        path_style = path.get("style")
        if not path_style:
            continue
        path_style = path_style_regex.sub(f"\\g<1>{bond_colors[bi]}\\g<3>",
path_style)
        path.set("style", path_style)
    return ET.tostring(svg_tree)

svg_text = drawer.GetDrawingText()
svg_text = set_bond_colors(svg_text, bond_colors)
SVG(svg_text)
[image: 1fa303ba-bc21-489c-af57-5324e07d7cb5.png]

Hope this helps, cheers
p.

On Tue, Jul 5, 2022 at 5:12 PM Storer, Joey (J) via Rdkit-discuss <
rdkit-discuss@lists.sourceforge.net> wrote:

> Hi all,
>
>
>
> I would like to color all bonds with a value.  Does anyone have a snippet
> for this?
>
>
>
> Many thanks!
>
> Joey Storer
>
> Dow Inc.
>
> Core R&D
>
> General Business
> _______________________________________________
> 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