输入为包含分子的SMILES表示符,输出为.png或.svg格式的图片,其中凯库勒式分子结构的样式,包含键长、键宽、图大小、原子和键的高亮表示等功能,具体参考了RDkit的手册------The RDKit Documentation — The RDKit 2023.03.1 documentation
''' SMILES is used to create tailored 2D molecular pictures to assist reaxFF
reaction network analysis, the mol image format supports png and svg ,
Written by Rui Zhou, UCAS '''
import os
from rdkit import Chem
from rdkit.Chem import Draw
from rdkit.Chem import AllChem
from rdkit.Chem import rdDepictor
from rdkit.Chem.Draw import rdMolDraw2D
def createPng(sms_content):
for i in range(len(sms_content)):
mol = Chem.MolFromSmiles(sms_content[i])
drawer = rdMolDraw2D.MolDraw2DCairo(150, 350)
do = rdMolDraw2D.MolDrawOptions()
do.bondLineWidth = 5 # 键宽
do.fixedBondLength = 50 # 键长
do.rotate = 0 # 顺时针旋转0度
#do.atomLabels[9] = 'ester group' # 修改原子标签,[atom ID]
drawer.SetDrawOptions(do)
rdMolDraw2D.PrepareAndDrawMolecule(d, mol) # 准备并绘制分子
drawer.FinishDrawing() # 结束绘制
drawer.WriteDrawingText('./' + str(i + 1) + 'reax.png') # 图片命名从1开始
def createSvg(sms_content):
for i in range(len(sms_content)):
mol = Chem.MolFromSmiles(sms_content[i])
substructure = Chem.MolFromSmarts('C(=O)O')
#rdDepictor.Compute2DCoords(mol) # for generating conformer ID
drawer = rdMolDraw2D.MolDraw2DSVG(400, 200)
do = rdMolDraw2D.MolDrawOptions()
#do.addAtomIndices = True # adds atom indices to drawings
#do.addBondIndices = True # adds bond indices to drawings
do.bondLineWidth = 5 # 键宽
do.fixedBondLength = 40 # 键长
do.baseFontSize = 1.2 # relative size of font
#do.annotationFontScale = 10 # Scale of font for atom and bond annotation relative to atomlabel font. Default=0.75.
# do.explicitMethyl = True # Draw terminal methyls explictly. Default is false.
do.setHighlightColour((0,1,0)) # HighlightColour (0.95,0.7,0.95)-紫红色 (0,1,0)-草绿色
#do.variableBondWidthMultiplier = 10 # what to multiply standard bond width by for variable attachment points.
do.rotate = 0 # 顺时针旋转0度
#do.atomLabels[9] = 'ester group' # 修改原子标签,[atom ID]
drawer.SetDrawOptions(do)
rdMolDraw2D.PrepareAndDrawMolecule(drawer, mol, highlightAtoms=[], highlightBonds=[]) # 准备并绘制分子, 代码跨行
drawer.FinishDrawing() # 结束绘制
svg = drawer.GetDrawingText()
with open('./' + str(i + 24) + 'reax.svg', "w") as f:
f.write(svg)
def reactionSvg(sms_content):
for i in range(len(sms_content)):
mol = AllChem.ReactionFromSmarts(sms_content[i])
drawer = rdMolDraw2D.MolDraw2DSVG(800, 300)
do = rdMolDraw2D.MolDrawOptions()
do.bondLineWidth = 2 # 键宽
do.fixedBondLength = 30 # 键长
do.rotate = 0 # 顺时针旋转0度
#do.atomLabels[9] = 'ester group' # 修改原子标签,[atom ID]
drawer.SetDrawOptions(do)
drawer.DrawReaction(mol, highlightByReactant=Ture) # 准备并绘制分子
drawer.FinishDrawing() # 结束绘制
svg = drawer.GetDrawingText()
with open('./' + str(i + 1) + 'reaction.svg', "w") as f:
f.write(svg)
#sms_file_path = './reaction.sms'
sms_file_path = './species.sms'
sms = open(sms_file_path, 'r')
sms_content = sms.readlines()
#createPng(sms_content)
createSvg(sms_content)
#reactionSvg(sms_content)
sms.close()
print('********* finished **********')
species.sms文件内容示例如下:
[H]C([H])C([H])([H])C([H])([H])C([H])([H])C([H])([H])C([H])([H])C([H])([H])[H]
[H]C([H])C([H])([H])C(C([H])([H])[H])(C([H])([H])[H])C([H])([H])C([H])[H]
[H]C([H])C([H])([H])C([H])([H])C([H])([H])[H]
[H]C([H])=C([H])[H]
reaction.sms 文件内容示例如下:
[cH:5]1[cH:6][c:7]2[cH:8][n:9][cH:10][cH:11][c:12]2[c:3]([cH:4]1)[C:2](=[O:1])O.[N-:13]=[N+:14]=[N-:15]>C(Cl)Cl.C(=O)(C(=O)Cl)Cl>[cH:5]1[cH:6][c:7]2[cH:8][n:9][cH:10][cH:11][c:12]2[c:3]([cH:4]1)[C:2](=[O:1])[N:13]=[N+:14]=[N-:15]