RDKit化学信息学

RDKit|搜索子结构时对侧链进行条件限制

2020-04-24  本文已影响0人  最会设计的科研狗

目录

一、简单子结构搜索

1. 初始化

>>> from rdkit import Chem
>>> from rdkit.Chem import Draw
>>> m = Chem.MolFromSmiles('C2NCC2CC1C(CCCC)C(OCCCC)C1c2ccccc2')
>>> p = Chem.MolFromSmarts('C1CCC1*')

2. 获取所有匹配的结构

>>> matches = m.GetSubstructMatches(p)
>>> matches
((5, 6, 11, 17, 18), (5, 17, 11, 6, 7), (6, 5, 17, 11, 12), (6, 11, 17, 5, 4))

3. 查看匹配的结构

>>> Draw.MolToImage(m, highlightAtoms=matches[0])

二、带条件的子结构搜索

2020.03版本以后,rdkit提供了一个可选功能,用于检查并筛选出符合条件的子结构。使用的时候 把这段代码粘过去,按流程操作 就好了。条件可以在matchers中定义,符合matchers中指定条件的子结构才会被返回。

1. 定义条件

>>> class SidechainChecker(object):
>>>     # 在这里添加条件
>>>     matchers = {'all_carbon':lambda at: at.GetAtomicNum() == 6,
>>>                 'all_aromatic':lambda at: at.GetIsAromatic()}
>>>     
>>>     def __init__(self, query, pName='queryType'):
>>>         # atom: [(idx, prop), ()]
>>>         self._atsToExamin = [(x.GetIdx(), x.GetProp(pName)) for x in query.GetAtoms() if x.HasProp(pName)]
>>>         self._pName = pName
>>>         
>>>     def __call__(self, mol, vect):
>>>         seen = [0] * mol.GetNumAtoms()
>>>         for idx in vect:
>>>             seen[idx] = 1
>>>         for idx, qtyp in self._atsToExamin:
>>>             midx = vect[idx]
>>>             stack = [midx]
>>>             atom = mol.GetAtomWithIdx(midx)
>>>             stack = [atom]
>>>             while stack:
>>>                 atom = stack.pop(0)
>>>                 if not self.matchers[qtyp](atom):
>>>                     return False
>>>                 seen[atom.GetIdx()] = 1
>>>                 for nbr in atom.GetNeighbors():
>>>                     if not seen[nbr.GetIdx()]:
>>>                         stack.append(nbr)
>>>         return True

2. 对侧链设置条件

>>> atom = p.GetAtomWithIdx(4)
>>> atom.SetProp("queryType", "all_aromatic")
>>> print(list(atom.GetPropNames()))
>>> atom.GetProp('queryType')
['queryType']
'all_aromatic'

3. 使用条件进行筛选

>>> params = Chem.SubstructMatchParameters()
>>> checker = SidechainChecker(p)
>>> params.setExtraFinalCheck(checker)
>>> matches = m.GetSubstructMatches(p, params)
>>> matches
((5, 6, 11, 17, 18),)
>>> Draw.MolToImage(m, highlightAtoms=matches[0])
带条件的搜索

4. 其他条件举例

>>> atom = p.GetAtomWithIdx(4)
>>> atom.SetProp("queryType", "all_carbon")
>>> params = Chem.SubstructMatchParameters()
>>> checker = SidechainChecker(p)
>>> params.setExtraFinalCheck(checker)
>>> matches = m.GetSubstructMatches(p, params)
>>> matches
((5, 6, 11, 17, 18), (5, 17, 11, 6, 7))

本文参考自rdkit官方文档
代码及源文件在这里

上一篇下一篇

猜你喜欢

热点阅读