首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用ChemDraw/Python从.cdx InChI创建一个文件?

如何用ChemDraw/Python从.cdx InChI创建一个文件?
EN

Stack Overflow用户
提问于 2019-10-01 11:29:57
回答 1查看 756关注 0票数 4

我想用ChemDraw从InChI创建一个InChI文件。这个回答cdx --> InChI提供了一个解决方案。

cdx_to_inchi下面的最小示例运行良好,但我不知道如何使inchi_to_cdx工作。

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-10 07:51:32

以下至少是一种可行的解决方案。它基本上是用按键“模仿”人类用户。它是从这里改编而来。缺点是:

  1. 它似乎是特定于Windows的
  2. ChemDraw必须是活动的应用程序,即不会在后台工作。
  3. 额外的延误减缓了整个过程。几个10,000个分子大约需要一天(好吧,比手工好;-)

这些问题仍然存在:

  1. 如何使它独立于平台?
  2. 如何让它在后台工作?
  3. 如何避免额外的延迟,或者如何使延迟能够使它仍然可靠地工作?

代码:

代码语言:javascript
复制
### 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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58183902

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档