[Python] How to change highlight color using MolsToGridImage in RDKit using python?

Discussão em 'Python' iniciado por Stack, Setembro 29, 2024.

  1. Stack

    Stack Membro Participativo

    The cheminformatics RDKit tool features the RDKit Cookbook with example code for a range of applications. One of them is to Highlight the Differences between 2 Molecules, here by using fMCS to map similarities, then marking those residues which are not shared. This example works well, the code is here

    I need the change the highlight color, but cannot get it to work. Based on the code link above, I've tried so far:

    from rdkit import Chem
    from rdkit.Chem import Draw
    from rdkit.Chem.Draw import rdMolDraw2D
    from rdkit.Chem.Draw import IPythonConsole
    from rdkit.Chem import rdFMCS
    from rdkit.Chem.Draw import rdDepictor
    rdDepictor.SetPreferCoordGen(True)
    IPythonConsole.drawOptions.minFontSize=20
    IPythonConsole.drawOptions.setHighlightColour=(0,1,0)
    from collections import defaultdict


    This had no effect at all, pic was drawn with default highlight.

    d2g = Draw.MolsToGridImage([mol1, mol2])
    opts = d2g.drawOptions()
    opts.setHighlightColour = (0,1,0)
    d2g([mol1, mol2],highlightAtomLists=[target_atm1, target_atm2])


    This throws the error


    AttributeError: 'Image' object has no attribute 'drawOptions'
    ​

    Then I got it to work with this one:

    from rdkit import Chem
    from rdkit.Chem import Draw
    from rdkit.Chem.Draw import rdMolDraw2D
    from rdkit.Chem.Draw import IPythonConsole
    from rdkit.Chem import rdFMCS
    from rdkit.Chem.Draw import rdDepictor
    rdDepictor.SetPreferCoordGen(True)
    IPythonConsole.drawOptions.minFontSize=20
    from collections import defaultdict

    mol1 = Chem.MolFromSmiles('FC1=CC=C2C(=C1)C=NN2')
    mol2 = Chem.MolFromSmiles('CCC1=C2NN=CC2=CC(Cl)=C1')

    from PIL import Image
    from io import BytesIO
    def show_mol(d2d,mol,legend='',highlightAtoms=[]):
    d2d.DrawMolecule(mol,legend=legend, highlightAtoms=highlightAtoms)
    d2d.FinishDrawing()
    bio = BytesIO(d2d.GetDrawingText())
    return Image.open(bio)
    def show_images(imgs,buffer=5):
    height = 0
    width = 0
    for img in imgs:
    height = max(height,img.height)
    width += img.width
    width += buffer*(len(imgs)-1)
    res = Image.new("RGBA",(width,height))
    x = 0
    for img in imgs:
    res.paste(img,(x,0))
    x += img.width + buffer
    return res

    mcs = rdFMCS.FindMCS([mol1,mol2])
    mcs_mol = Chem.MolFromSmarts(mcs.smartsString)
    match1 = mol1.GetSubstructMatch(mcs_mol)
    target_atm1 = []
    for atom in mol1.GetAtoms():
    if atom.GetIdx() not in match1:
    target_atm1.append(atom.GetIdx())
    match2 = mol2.GetSubstructMatch(mcs_mol)
    target_atm2 = []
    for atom in mol2.GetAtoms():
    if atom.GetIdx() not in match2:
    target_atm2.append(atom.GetIdx())

    img1 = []
    d2dx = Draw.MolDraw2DCairo(350,300)
    doptsx = d2dx.drawOptions()
    doptsx.setHighlightColour((1,0.84,0,.5))
    img1.append(show_mol(d2dx,mol1,highlightAtoms=target_atm1))
    d2dx = Draw.MolDraw2DCairo(350,300)
    doptsx = d2dx.drawOptions()
    doptsx.setHighlightColour((1,0.84,0,.5))
    img1.append(show_mol(d2dx,mol2, highlightAtoms=target_atm2))
    pic = show_images(img1)
    pic.save('hallo.png')
    pic


    Problems are, 1) I need an svg output and, 2) if the molecules have different size or alternate SMILE config is used, only MolsToGrid places them properly adjacent within same subpanel boundaries. So I need to use MolsToGrid for scientific publication-quality images.

    I am a complete python n00b using jupyter lab on a windows 11 PC.


    Python 3.8.18 (default, Sep 11 2023, 13:39:12) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
    ​

    Continue reading...

Compartilhe esta Página