1

I am trying to highlight carbon positions in a test molecule while hiding the implicit hydrogens. This is unexpectedly complicated, as I have two compounding problems which each have a solution, but are incompatible.

from rdkit import Chem
from rdkit.Chem import Draw
from rdkit.Chem.Draw import rdMolDraw2D
from rdkit.Chem.Draw import IPythonConsole
from IPython.display import SVG
import rdkit

Molblock = 'molblock information here'
mx = Chem.MolFromMolBlock(Molblock,sanitize=False)# this molblock already provides an atom map, which I must remove to keep from displaying all assignments in the final image


def remove_atom_indices(mol):
    for a in mol.GetAtoms():
        a.SetAtomMapNum(0)

remove_atom_indices(mx) # remove atom indicies, to keep them from being displayed - can this be passed as an arg? 
highlight = [96,89,113] # locations of atoms I wish to highlight, fetched from indicies which are now removed
drawer = rdMolDraw2D.MolDraw2DSVG(500,500) # I want to actually see this with eyeballs
# mx=Chem.RemoveHs(mx) #this does not work - assuming it rewrtires the indicies and is now incompatable when they are removed
drawer.DrawMolecule(mx,highlightAtoms=highlight)
drawer.FinishDrawing()
svg = drawer.GetDrawingText().replace('svg:','')
SVG(svg)

I can get generate image files under the following conditions:

  1. I do not provide a highlight atoms list - which is a non-starter here, that is the main point.
  2. I do not hide the implicit hydrogens - which is "fine...I guess" except that in large structures, this creates a massive and unreadable scaffold.

A solution would be great if it allowed for one of the following:

  1. Simply not render the 1H in the structure, but retain their presence in the mol file (for indexing)
  2. Render the image without displaying the atom map numbers - cannot find how to do this without removing them.
Jmegan042
  • 251
  • 5
  • 15

1 Answers1

1

Sorry if I'm not understanding your question right, but if the hydrogens are implicit, you do not need to directly modify the mol object to prevent them from displaying. You can use this snippet of code from the RDKit Cookbook: https://rdkit.org/docs/Cookbook.html#without-implicit-hydrogens

for atom in m.GetAtoms():
    atom.SetProp("atomLabel", atom.GetSymbol())

As for hiding atom indices, you can just modify drawOptions like so: http://rdkit.blogspot.com/2020/04/new-drawing-options-in-202003-release.html#Atom-and-bond-indices

drawer.drawOptions().addAtomIndices = False

And here's a thread with more information on how RDKit deals with hydrogens: https://sourceforge.net/p/rdkit/mailman/message/36699970/

pooterpan
  • 11
  • 1