Rdkit图纸隐藏(不删除)氢

2024-05-14 11:05:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图突出测试分子中的碳位置,同时隐藏隐含的氢。这出乎意料地复杂,因为我有两个复合问题,每个问题都有一个解决方案,但不兼容

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)

我可以在以下条件下获取并生成图像文件:

  1. 我不提供一个突出显示的原子列表-这是一个非初学者在这里,这是主要的一点
  2. 我没有隐藏隐含的氢——这是“很好的……我猜”,除了在大型结构中,这创造了一个巨大且无法阅读的支架

如果能够满足以下条件之一,则解决方案将非常好:

  1. 只是不在结构中呈现1H,而是在mol文件中保留它们的存在(用于索引)
  2. 在不显示原子贴图编号的情况下渲染图像-无法找到如何在不删除原子贴图编号的情况下执行此操作

Tags: tofromsvgimportthisremovedraweratom
1条回答
网友
1楼 · 发布于 2024-05-14 11:05:11

抱歉,如果我没有正确理解你的问题,但是如果氢是隐式的,你不需要直接修改mol对象来阻止它们显示。您可以使用RDKit手册中的这段代码:https://rdkit.org/docs/Cookbook.html#without-implicit-hydrogens

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

至于隐藏原子索引,您可以像这样修改drawOptionshttp://rdkit.blogspot.com/2020/04/new-drawing-options-in-202003-release.html#Atom-and-bond-indices

drawer.drawOptions().addAtomIndices = False

这里有一个关于RDKit如何处理氢的更多信息的线程:https://sourceforge.net/p/rdkit/mailman/message/36699970/

相关问题 更多 >

    热门问题