如何使用ChemDraw / Python从InChI创建.cdx文件? [英] How to create a .cdx file from InChI with ChemDraw/Python?
本文介绍了如何使用ChemDraw / Python从InChI创建.cdx文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用Python从InChI创建ChemDraw .cdx文件。
此答案为 cdx->提供了一种解决方案。 InChI
。
I would like to create a ChemDraw .cdx file from an InChI with Python.
This answer gives a solution for cdx --> InChI
.
cdx_to_inchi
下面的最小示例很好用,但
我不知道如何使 inchi_to_cdx
正常工作。
The minimal example below cdx_to_inchi
works fine, but
I could not figure out how I can get inchi_to_cdx
to work.
import comtypes.client as w32
def cdx_to_inchi(cdx):
ChemDraw = w32.CreateObject("ChemDraw.Application")
ChemDraw.Visible = False
Compound = ChemDraw.Documents.Open(cdx) # opens existing file
inchi = Compound.Objects.Data("chemical/x-inchi")
print(inchi)
ChemDraw.Quit()
def inchi_to_cdx(inchi):
ChemDraw = w32.CreateObject("ChemDraw.Application")
ChemDraw.Visible = False
Compound = ChemDraw.Documents.Open("Emtpy.cdx") # opens existing file
# ChemDraw.Documents.New("NewFile.cdx") # How to create a new file?
# Compound.Objects.SetData(inchi) # How to paste InChi data?
# ChemDraw.Documents.Save() # How to save?
# ChemDraw.Documents.SaveAs("MyMolecule.cdx") # How to save under different name?
ChemDraw.Quit()
cdx = r'C:\Data\Test.cdx'
inchi = '1S/C6H6/c1-2-4-6-5-3-1/h1-6H'
cdx_to_inchi(cdx)
inchi_to_cdx(inchi)
推荐答案
以下至少是某种可行的解决方案。它基本上是通过按键来模仿人类用户。改编自此处。缺点是:
The following is at least a somehow working solution. It basically "mimics" a human user with keypresses. It is adapted from here. The drawbacks are:
- 它似乎是Windows特定的
- ChemDraw必须是活动应用程序,即无法在后台运行
- 其他延迟会减慢整个过程。
- it seems to be Windows specific
- ChemDraw has to be the active application, i.e. will not work in the background
- additional delays slow down the whole procedure. a few 10'000 molecules would take about a day (well, better than by hand ;-)
这些问题仍然存在: p>
These questions remain:
- 如何使其独立于平台?
- 如何使其在后台运行?
- 如何避免额外的延迟,或者延迟多久才能使其仍然可靠地工作?
代码:
### create a ChemDraw .cdx file from an InChI string
import win32com.client as win32
import win32clipboard
import time
def sleep():
time.sleep(0.5) # wait for 0.5 seconds that script is not too fast for ChemDraw
def sleep_short():
time.sleep(0.25) # wait for 0.25 seconds between key presses
# initialize windows shell (so we can send keyboard commands)
shell = win32.Dispatch("WScript.Shell")
def hit_keys(keys): # function to wait, then send a keypress signal
sleep_short()
shell.SendKeys(keys,1)
# initialize ChemDraw
chemdraw = win32.gencache.EnsureDispatch('ChemDraw.Application') # connect to ChemDraw
chemdraw.Visible = True # window needs to be visible otherwise keypresses will not work
sleep()
doc = chemdraw.Documents.Add() # create a new document
doc.Activate()
sleep()
# ChemDraw must be the active application, so it can receive keyboard commands
shell.AppActivate("ChemDraw Prime") # name of the ChemDraw window bar
sleep()
inchi = 'InChI=1S/C6H6/c1-2-4-6-5-3-1/h1-6H'
# set clipboard data
win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardText(inchi)
win32clipboard.CloseClipboard()
sleep() # not sure whether a delay is needed here
hit_keys("%e") # edit
hit_keys("s") # paste special
hit_keys("i") # pase as inchi
sleep()
ffname = r'C:\Users\Test\Scripts\MyMolecule.cdx' # full filename
doc.SaveAs(ffname) # save the file
doc.Close()
chemdraw.Quit() # close ChemDraw
### end of code
这篇关于如何使用ChemDraw / Python从InChI创建.cdx文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文