python - 如何使用 ChemDraw/Python 从 InChI 创建 .cdx 文件?

标签 python python-3.x com comtypes cheminformatics

我想使用 Python 从 InChI 创建 ChemDraw .cdx 文件。 这个answer给出了 cdx --> InChI 的解决方案。

下面的最小示例cdx_to_inchi工作正常,但是 我不知道如何让 inchi_to_cdx 工作。

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)

最佳答案

以下至少是一个可行的解决方案。它基本上是“模仿”人类用户的按键操作。改编自here 。缺点是:

  1. 这似乎是 Windows 特定的
  2. ChemDraw 必须是事件应用程序,即无法在后台运行
  3. 额外的延迟会减慢整个过程。几个 10,000 个分子大约需要一天的时间(好吧,比手工更好;-)

这些问题仍然存在:

  1. 如何使其平台独立?
  2. 如何让它在后台运行?
  3. 如何避免额外的延迟,或者延迟多短才能保证其仍然可靠地工作?

代码:

### 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

关于python - 如何使用 ChemDraw/Python 从 InChI 创建 .cdx 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58183902/

相关文章:

python - 我应该等待 Django 开始支持 Python 3 吗?

c++ - 如何在接收器对象上实现传出接口(interface) (C++)

c# - ActiveX 组件无法创建对象? .NET 通讯

python - Beautiful Soup 找到具有隐藏样式的元素

python - 使用 ctypes 和 wchar_t 时如何将字符串从 C++ 获取到 python?

python - 在 python 中捕获 pytrends.exceptions.ResponseError 时出错

c# - 绑定(bind)到使用 CreateObject 创建的已打开表单

python - Node & python 不返回相同的 hash256

python - 在布局中移动小部件 pyqt

python - 删除验证码文本中不需要的行 - opencv - python