[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DrawRDKitEnvs Can't kekulize mol. #7971

Open
ivannnnnnnnnn opened this issue Oct 29, 2024 · 0 comments
Open

DrawRDKitEnvs Can't kekulize mol. #7971

ivannnnnnnnnn opened this issue Oct 29, 2024 · 0 comments
Labels

Comments

@ivannnnnnnnnn
Copy link

Hello! When I try to draw RDKitBits for a molecule, I get the error Can't kekulize mol. This is because some parts of the molecule have a non-Kekulized structure.

Example of code for reproduce

from rdkit import Chem
from rdkit.Chem import Draw


mol = Chem.MolFromSmiles('CCOC(C1NC2C(=C(C=CC=2)OC)C=1)=O')
bi = {}
fp = Chem.RDKFingerprint(mol, bitInfo=bi)
tpls = [(mol, x, bi) for x in fp.GetOnBits()]

bits = fp.GetOnBits()
Draw.DrawRDKitBits(tpls, molsPerRow=4,legends=[str(x) for x in bits])

Inside the Draw.DrawRDKitBits, the Draw.DrawRDKitEnvs method is called.

I fix it locally by converting submols to smarts and create new mol from that smarts but I think it should be fixed in other way inside of Draw module.

Example of fixed Draw.DrawRDKitEnvs


def DrawRDKitEnvsFixed(envs, molsPerRow=3, subImgSize=(150, 150), baseRad=0.3, useSVG=True,
                  aromaticColor=(0.9, 0.9, 0.2), extraColor=(0.9, 0.9, 0.9), nonAromaticColor=None,
                  legends=None, drawOptions=None, **kwargs):
  submols = []
  highlightAtoms = []
  atomColors = []
  highlightBonds = []
  bondColors = []
  highlightRadii = []
  for mol, bondpath in envs:
    menv = _getRDKitEnv(mol, bondpath, baseRad, aromaticColor, extraColor, nonAromaticColor,
                        **kwargs)


    # <--- Fix is here -->
    # submols.append(menv.submol)
    submol = Chem.MolFromSmarts(Chem.MolToSmarts(menv.submol))
    submol.UpdatePropertyCache()
    submols.append(submol)
    # <--- End --->


    highlightAtoms.append(menv.highlightAtoms)
    atomColors.append(menv.atomColors)
    highlightBonds.append(menv.highlightBonds)
    bondColors.append(menv.bondColors)
    highlightRadii.append(menv.highlightRadii)

  if legends is None:
    legends = [''] * len(envs)

  nRows = len(envs) // molsPerRow
  if len(envs) % molsPerRow:
    nRows += 1

  fullSize = (molsPerRow * subImgSize[0], nRows * subImgSize[1])
  # Drawing
  if useSVG:
    drawer = rdMolDraw2D.MolDraw2DSVG(fullSize[0], fullSize[1], subImgSize[0], subImgSize[1])
  else:
    drawer = rdMolDraw2D.MolDraw2DCairo(fullSize[0], fullSize[1], subImgSize[0], subImgSize[1])

  if drawOptions is None:
    drawOptions = drawer.drawOptions()
  drawOptions.continuousHighlight = False
  drawOptions.includeMetadata = False
  drawer.SetDrawOptions(drawOptions)
  drawer.DrawMolecules(submols, legends=legends, highlightAtoms=highlightAtoms,
                       highlightAtomColors=atomColors, highlightBonds=highlightBonds,
                       highlightBondColors=bondColors, highlightAtomRadii=highlightRadii, **kwargs)
  drawer.FinishDrawing()
  return drawer.GetDrawingText()


Not sure if that right solution but its fix my case.

That place in code: https://github.com/rdkit/rdkit/blob/master/rdkit/Chem/Draw/__init__.py#L886

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant