问题描述
我试图突出测试分子中的碳位置,同时隐藏隐含的氢。这出乎意料地复杂,因为我有两个复合问题,每个问题都有一个解决方案,但不兼容。
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 @R_396_4045@ion 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)
- 我没有提供高亮原子列表-这不是重点,这是重点。
- 我不会隐藏隐含的氢-这是“好...我想”,除了在大型结构中,这会产生巨大且难以读取的支架。
如果允许以下任一解决方案,那将是很好的选择:
解决方法
抱歉,如果我没有正确理解您的问题,但是如果氢是隐式的,您不需要直接修改 mol 对象来阻止它们显示。您可以使用 RDKit Cookbook 中的以下代码片段:https://rdkit.org/docs/Cookbook.html#without-implicit-hydrogens
for atom in m.GetAtoms():
atom.SetProp("atomLabel",atom.GetSymbol())
至于隐藏原子索引,您可以像这样修改drawOptions
:http://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/