【问题标题】:Generate 2d images of molecules from PubChem FTP data从 PubChem FTP 数据生成分子的二维图像
【发布时间】:2015-09-17 14:45:45
【问题描述】:

与其爬 PubChem 的网站,我更愿意从 PubChem ftp 站点本地生成图像:

ftp://ftp.ncbi.nih.gov/pubchem/specifications/

唯一的问题是我仅限于 OSX 和 Linux,而且我似乎无法找到以编程方式生成他们网站上的 2d 图像的方法。看这个例子:

https://pubchem.ncbi.nlm.nih.gov/compound/6#section=Top

在“2D 结构”标题下,我们有这张图片:

https://pubchem.ncbi.nlm.nih.gov/image/imgsrv.fcgi?cid=6&t=l

这就是我想要生成的。

【问题讨论】:

标签: python scipy scientific-computing pubchem cheminformatics


【解决方案1】:

如果您想要开箱即用的东西,我建议您使用来自 ChemAxon 的 Marvin (https://www.chemaxon.com/products/marvin/) 的 molconvert,它对学者来说是免费的。它可以从命令行轻松使用,并且支持大量输入和输出格式。因此,对于您的示例,它将是:

molconvert "png" -s "C1=CC(=C(C=C1[N+](=O)[O-])[N+](=O)[O-])Cl" -o cdnb.png

结果如下图:

它还允许您设置宽度、高度、质量、背景颜色等参数。


但是,如果您是程序员,我肯定会推荐RDKit。遵循一个代码,该代码为一对以微笑给出的化合物生成图像。

from rdkit import Chem
from rdkit.Chem import Draw

ms_smis = [["C1=CC(=C(C=C1[N+](=O)[O-])[N+](=O)[O-])Cl", "cdnb"],
           ["C1=CC(=CC(=C1)N)C(=O)N", "3aminobenzamide"]]
ms = [[Chem.MolFromSmiles(x[0]), x[1]] for x in ms_smis]

for m in ms: Draw.MolToFile(m[0], m[1] + ".svg", size=(800, 800))

这将为您提供以下图像:

【讨论】:

  • 就 ChemAxon 的 Marvin 是一种商业产品而言,如果您能以某种方式与该供应商有关联的话,那就太好了。否则,请添加对RDKit 的引用,并提供一个简短的示例如何使用它来解决问题。
  • Jchem 拥有学术许可证,可以访问他们的所有工具。我在答案中添加了这些信息以及 RDKit 中的代码示例。
【解决方案2】:

所以我也给 PubChem 的人发了电子邮件,他们很快就回复了我:

我们对图像的唯一批量访问是通过下载 服务:https://pubchem.ncbi.nlm.nih.gov/pc_fetch/pc_fetch.cgi
您一次最多可以请求 50,000 张图片。

这比我预期的要好,但仍然不是惊人,因为它需要下载我理论上可以在本地生成的东西。所以我要让这个问题悬而未决,直到某个好心人写了一个开源库来做同样的事情。

编辑:

我想,如果他们和我做同样的事情,我还不如为他们节省一些时间。我创建了一个基于 Mechanize 的 Ruby Gem 来自动下载图像。请善待他们的服务器,只下载你需要的。

https://github.com/zachaysan/pubchem

gem install pubchem

【讨论】:

    【解决方案3】:

    一个开源选项是Indigo Toolkit,它还具有适用于 Linux、Windows 和 MacOS 的预编译包以及适用于 Python、Java、.NET 和 C 库的语言绑定。我选择了 1.4.0 测试版。

    我对将 SMILES 转换为 2D 结构有类似的兴趣,并调整了我的 Python 来解决您的问题并捕获时间信息。它使用 CID-SMILES.gz 的 PubChem FTP (Compound/Extras) 下载。下面的脚本是一个本地 SMILES 到 2D 结构转换器的实现,它从 PubChem CID-SMILES 文件的异构 SMILES(包含超过 1.02 亿条化合物记录)中读取一系列行,并将 SMILES 转换为二维结构。在具有 1000 个 SMILES 到结构转换的三个测试中,在我的 Windows 10 笔记本电脑(Intel i7-7500U CPU,2.70GHz ) 使用固态驱动器并运行 Python 3.7.4。这 3000 个文件的总大小为 100 MB。

    from indigo import *
    from indigo.renderer import *
    import subprocess
    import datetime
    
    def timerstart():
        # start timer and print time, return start time
        start = datetime.datetime.now()
        print("Start time =", start)
    
        return start
    
    def timerstop(start):
        # end timer and print time and elapsed time, return elapsed time
        endtime = datetime.datetime.now()
        elapsed = endtime - start
        print("End time =", endtime)
        print("Elapsed time =", elapsed)
    
        return elapsed
    
    numrecs = 1000
    recoffset = 0 # 10000000    # record offset
    starttime = timerstart()
    
    indigo = Indigo()
    renderer = IndigoRenderer(indigo)
    
    # set render options
    indigo.setOption("render-atom-color-property", "color")
    indigo.setOption("render-coloring", True)
    indigo.setOption("render-comment-position", "bottom")
    indigo.setOption("render-comment-offset", "20")
    indigo.setOption("render-background-color", 1.0, 1.0, 1.0)
    indigo.setOption("render-output-format", "png")
    
    # set data path (including data file) and output file path
    datapath = r'../Download/CID-SMILES'
    pngpath = r'./2D/'
    
    # read subset of rows from data file
    mycmd = "head -" + str(recoffset+numrecs) + " " + datapath + " | tail -" + str(numrecs) 
    print(mycmd)
    (out, err) = subprocess.Popen(mycmd, stdout=subprocess.PIPE, shell=True).communicate()
    
    lines = str(out.decode("utf-8")).split("\n")
    count = 0
    for line in lines: 
        try:
            cols = line.split("\t")   # split on tab
            key = cols[0]             # cid in cols[0]
            smiles = cols[1]          # smiles in cols[1]
            mol = indigo.loadMolecule(smiles)
            s = "CID=" + key
            indigo.setOption("render-comment", s)
            #indigo.setOption("render-image-size", 200, 250)
            #indigo.setOption("render-image-size", 400, 500)
            renderer.renderToFile(mol, pngpath + key + ".png")
            count += 1
        except:
            print("Error processing line after", str(count), ":", line)
            pass
    
    elapsedtime = timerstop(starttime)
    print("Converted", str(count), "SMILES to PNG")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-10-05
      • 1970-01-01
      • 1970-01-01
      • 2011-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多