You need to enable JavaScript to run this app.
文档中心
云数据库 PostgreSQL 版

云数据库 PostgreSQL 版

复制全文
下载 pdf
插件
化学分子计算检索(RDKit)
复制全文
下载 pdf
化学分子计算检索(RDKit)
RDKit 是一款化学信息学开源工具包,基于机器学习方法生成化合物指纹(fingerprint),用于化合物子结构查询、化合物结构相似性计算。​
使用限制​
仅支持在 PostgreSQL 13 及以上版本的实例中使用 RDKit 插件,各版本实例需要使用的插件版本请参见支持的插件列表。​
使用插件​
创建与删除插件​
  • 创建插件​
说明​
需使用高权限账号创建插件。​
  • ​
    CREATE EXTENSION rdkit;​
    ​
  • 删除插件​
  • ​
    DROP EXTENSION rdkit;​
    ​
数据类型​
  • 数据类型说明​
  • 插件支持的基本数据类型有 mol、qmol、bfp、sfp 和 reaction。各类型的含义如下:​
  • mol 是一种常用的化学分子格式,通常用于保存单个分子。​
  • qmol 是 RDKit 提供的一种包含查询特征的化学分子数据类型,可由 SMARTS 等格式数据直接转化为 qmol 类型数据,例如 ‘c1cccc[c,n]1’::qmol。​
  • bfp 是 RDKit 提供的一种位向量形式的化学分子指纹类型。​
  • sfp 是 RDKit 提供的一种稀疏向量形式的化学分子指纹类型。​
  • reaction 是 RDKit 提供的化学反应模版类型。​
  • 数据类型转换​
  • 将 SMILES 格式分子转化成 mol 类型并插入表格。​
  • ​
    insert into mols values​
    (1, 'C1=CC=C\\2C(=C1)C=CC(=O)/C2=N\\NC3=NC=CS3'::mol),​
    (2, 'CC1=C(N=CC=C1)NC2=NC(=CS2)C3=CC=CC=N3'::mol),​
    (3, 'CC(=NNC1=C(C=C(C=N1)Cl)Cl)C2=CC=CC=N2'::mol),​
    (4, 'CC(=NNC(=S)NCC=C)C1=CC=CC=N1'::mol),​
    (5, 'CC1=CC=C(C=C1)C(=O)SC2=CC=CC=CC2=O'::mol);​
    ​
  • 将 SMARTS 格式分子转化成 qmol 类型。​
  • ​
    select 'c1[o,s]ncn1'::qmol;​
    ​
  • 使用指纹计算函数将 mol 类型数据转换成 sfp 类型数据,并插入表格。​
  • ​
    select id, torsion_fp(m) as tfp, morgan_fp(m) as mfp, featmorgan_fp(m) as ffp into sfps from mols;​
    ​
  • 使用指纹计算函数将 mol 类型数据转换成 bfp 类型数据,并插入表格。​
  • ​
    select id, torsionbv_fp(m) as tfp, morganbv_fp(m) as mfp, featmorganbv_fp(m) as ffp into bfps from mols;​
    ​
  • 将 SMARTS 格式的化学反应模板转化成 reaction 对象(化学反应引擎对象)。​
  • ​
    SELECT reaction_from_smarts('c1ccc[n,c]c1>>c1nccnc1');​
    ​
操作符​
本文以 mol 类型和 bfp 类型为例对操作符的含义和使用进行说明,各操作符在其他数据类型下的含义和使用与 mol 类型和 bfp 类型基本一致,本文不再一一列举。如需了解更详细信息,请参见 RDKit 使用指南。​
mol 类型​
mol 类型支持子结构查询操作符(<@、@>、@=)和比较操作符(>、>=、<、<=、<>、=)。各操作符在操作 mol 类型对象时的含义及使用如下表所示:​
​
操作符​
说明​
使用示例​
<@​
mol1 <@ mol2:​
mol1 是 mol2 的子结构则返回 true,否则返回 false。​
​
-- 查询 mols 表中包含 'c1cccnc1' 结构的分子​
select * from mols where 'c1cccnc1'::mol <@ m;​
-- 查询 mols 表中是 'c1cccnc1' 子结构的分子​
select * from mols where m <@ 'c1cccnc1'::mol;​
​
@>​
mol1 @> mol2:​
mol2 是 mol1 的子结构则返回 true,否则返回 false。​
​
-- 查询 mols 表中是 'c1cccnc1' 子结构的分子​
select * from mols where 'c1cccnc1'::mol @> m;​
-- 查询 mols 表中包含 'c1cccnc1' 结构的分子​
select * from mols where m @> 'c1cccnc1'::mol;​
​
@=​
mol1 @= mol2:mol1 和 mol2的结构相同则返回 true,否则返回 false。​
​
-- 查询 mols 表中结构同 'C=CCNC(=S)NN=C(C)c1ccccn1' 的分子​
select * from mols where m @= 'C=CCNC(=S)NN=C(C)c1ccccn1'::mol;​
​
>​
mol1 > mol2:mol1 大于 mol2 则返回 true,否则返回 false。​
​
-- 查询 mols 表中大于 'CC(=NNc1ncc(Cl)cc1Cl)c1ccccn1' 的分子​
select * from mols where m > 'CC(=NNc1ncc(Cl)cc1Cl)c1ccccn1'::mol;​
​
>=​
mol1 >= mol2:mol1 大于或等于 mol2 则返回 true,否则返回 false。​
​
-- 查询 mols 表中大于等于 'CC(=NNc1ncc(Cl)cc1Cl)c1ccccn1' 的分子​
select * from mols where m >= 'CC(=NNc1ncc(Cl)cc1Cl)c1ccccn1'::mol;​
​
<​
mol1 < mol2:mol1 小于 mol2 则返回 true,否则返回 false。​
​
-- 查询 mols 表中小于 'CC(=NNc1ncc(Cl)cc1Cl)c1ccccn1' 的分子​
select * from mols where m < 'CC(=NNc1ncc(Cl)cc1Cl)c1ccccn1'::mol;​
​
<=​
mol1 <= mol2:mol1 小于或等于 mol2 则返回 true,否则返回 false。​
​
-- 查询 mols 表中小于等于 'CC(=NNc1ncc(Cl)cc1Cl)c1ccccn1' 的分子​
select * from mols where m <= 'CC(=NNc1ncc(Cl)cc1Cl)c1ccccn1'::mol;​
​
<>​
mol1 <> mol2:mol1 不等于 mol2 则返回 true,否则返回 false。​
​
-- 查询 mols 表中不等于 'CC(=NNc1ncc(Cl)cc1Cl)c1ccccn1' 的分子​
select * from mols where m <> 'CC(=NNc1ncc(Cl)cc1Cl)c1ccccn1'::mol;​
​
=​
mol1 = mol2:mol1 等于 mol2 则返回 true,否则返回 false。​
​
-- 查询 mols 表中等于 'CC(=NNc1ncc(Cl)cc1Cl)c1ccccn1' 的分子​
select * from mols where m = 'CC(=NNc1ncc(Cl)cc1Cl)c1ccccn1'::mol;​
​
​
​
bfp 类型​
bfp 类型支持相似度查询操作符(%、<%>、#、<#>)和比较操作符(>、>=、<、<=、<>、=)。各操作符在操作 bfp 类型对象时的含义及使用如下表所示:​
​
操作符​
说明​
使用示例​
%​
bfp1 % bfp2:​
bfp1 和 bfp2 的 Tanimoto 相似度超过阈值则返回 true,否则返回 false。​
​
-- 查询 bfps 表中分子与 'c1cccnc1' 的 Tanimoto 相似度​
select id, tanimoto_sml(morganbv_fp('c1cccnc1'::mol) , mfp) from bfps;​
-- 查询 bfps 表中分子与 'c1cccnc1' 的 Tanimoto 相似度是否超过阈值​
select id, morganbv_fp('c1cccnc1'::mol) % mfp from bfps;​
​
<%>​
bfp1 <%> bfp2:​
返回 bfp1 和 bfp2 的 Tanimoto 相似度距离,通常用于排序。​
​
-- 查询 bfps 表中分子与 'c1cccnc1' 的 Tanimoto 相似度距离​
select id, morganbv_fp('c1cccnc1'::mol) <%> mfp from bfps order by morganbv_fp('c1cccnc1'::mol) <%> mfp;​
​
#​
bfp1 # bfp2:​
bfp1 和 bfp2 的 Dice 相似度超过阈值则返回 true,否则返回 false。​
​
-- 查询 bfps 表中分子与 'c1cccnc1' 的 Dice 相似度​
select id, dice_sml(morganbv_fp('c1cccnc1'::mol), mfp) from bfps;​
-- 查询 bfps 表中分子与 'c1cccnc1' 的 Dice 相似度是否超过阈值​
select id, morganbv_fp('c1cccnc1'::mol) # mfp from bfps;​
​
<#>​
bfp1 <#> bfp2:​
返回 bfp1 和 bfp2 的 Dice 相似度距离,通常用于排序。​
​
-- 查询 bfps 表中分子与 'c1cccnc1' 的 Dice 相似度距离​
select id, morganbv_fp('c1cccnc1'::mol) <#> mfp from bfps order by morganbv_fp('c1cccnc1'::mol) <#> mfp;​
​
>​
bfp1 > bfp2:bfp1 大于 bfp2 则返回 true,否则返回 false。​
​
-- 查询 bfps 表中大于 'CC1=CC=C(C=C1)C(=O)SC2=CC=CC=CC2=O' 的分子​
select id, mfp from bfps where mfp > morganbv_fp('CC1=CC=C(C=C1)C(=O)SC2=CC=CC=CC2=O'::mol);​
​
>=​
bfp1 >= bfp2:bfp1 大于或等于 bfp2 则返回 true,否则返回 false。​
​
-- 查询 bfps 表中大于等于 'CC1=CC=C(C=C1)C(=O)SC2=CC=CC=CC2=O' 的分子​
select id, mfp from bfps where mfp >= morganbv_fp('CC1=CC=C(C=C1)C(=O)SC2=CC=CC=CC2=O'::mol);​
​
<​
bfp1 < bfp2:bfp1 小于 bfp2 则返回 true,否则返回 false。​
​
-- 查询 mols 表中小于 'CC1=CC=C(C=C1)C(=O)SC2=CC=CC=CC2=O' 的分子​
select id, mfp from bfps where mfp < morganbv_fp('CC1=CC=C(C=C1)C(=O)SC2=CC=CC=CC2=O'::mol);​
​
<=​
bfp1 <= bfp2:bfp1 小于或等于 bfp2 则返回 true,否则返回 false。​
​
-- 查询 mols 表中小于等于 'CC1=CC=C(C=C1)C(=O)SC2=CC=CC=CC2=O' 的分子​
select id, mfp from bfps where mfp <= morganbv_fp('CC1=CC=C(C=C1)C(=O)SC2=CC=CC=CC2=O'::mol);​
​
<>​
bfp1 <> bfp2:bfp1 不等于 bfp2 则返回 true,否则返回 false。​
​
-- 查询 mols 表中不等于 'CC1=CC=C(C=C1)C(=O)SC2=CC=CC=CC2=O' 的分子​
select id, mfp from bfps where mfp <> morganbv_fp('CC1=CC=C(C=C1)C(=O)SC2=CC=CC=CC2=O'::mol);​
​
=​
bfp1 = bfp2:bfp1 等于 bfp2 则返回 true,否则返回 false。​
​
-- 查询 mols 表中等于 'CC1=CC=C(C=C1)C(=O)SC2=CC=CC=CC2=O' 的分子​
select id, mfp from bfps where mfp = morganbv_fp('CC1=CC=C(C=C1)C(=O)SC2=CC=CC=CC2=O'::mol);​
​
​
​
函数​
RDKit 支持的函数较多,本节只做简单的调用展示。更多详细信息,请参见 RDKit 使用指南。​
  • tanimoto_sml(bfp, bfp):计算 tanimoto 相似度。​
  • ​
    postgres=# select tanimoto_sml(morganbv_fp('c1cccnc1'::mol), morganbv_fp('c1cccnc1'::mol));​
    tanimoto_sml​
    --------------​
    1​
    (1 row)​
    ​
    postgres=# select tanimoto_sml(morganbv_fp('C1C(OC2=CC(=CC(=C2C1=O)))'::mol), morganbv_fp('c1cccnc1'::mol));​
    tanimoto_sml​
    ---------------------​
    0.06666666666666667​
    (1 row)​
    ​
  • dice_sml(bfp, bfp):计算 dice 相似度。​
  • ​
    postgres=# select dice_sml(morganbv_fp('c1cccnc1'::mol), morganbv_fp('c1cccnc1'::mol));​
    dice_sml​
    ----------​
    1​
    (1 row)​
    ​
    postgres=# select dice_sml(morganbv_fp('C1C(OC2=CC(=CC(=C2C1=O)))'::mol), morganbv_fp('c1cccnc1'::mol));​
    dice_sml​
    ----------​
    0.125​
    (1 row)​
    ​
索引​
RDKit 支持 btree 索引、gist 索引和 gin 索引。下面以 bfp 类型为例描述各类索引的使用。​
  • 创建 btree 索引​
  • ​
    -- 创建btree索引​
    drop index if exists bfpidx;​
    create index bfpidx ON bfps(tfp);​
    ​
  • 创建 gist 索引​
  • ​
    -- 创建gist索引​
    drop index if exists bfpidx;​
    create index bfpidx on bfps using gist(tfp);​
    ​
  • 创建 gin 索引​
  • ​
    -- 创建gin索引​
    drop index if exists bfpidx;​
    create index bfpidx on bfps using gin(tfp);​
    ​
参考文档​
​
最近更新时间:2026.07.28 14:41:54
这个页面对您有帮助吗?
有用
有用
无用
无用