图中的微笑

2024-10-03 15:31:15 发布

您现在位置:Python中文网/ 问答频道 /正文

有没有一种方法或包可以将一个图(或邻接矩阵)转换成SMILES字符串?在

例如,我知道原子是[6 6 7 6 6 6 6 8] ([C C N C C C C O]),邻接矩阵是

[[ 0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.],

 [ 1.,  0.,  2.,  0.,  0.,  0.,  0.,  1.],

 [ 0.,  2.,  0.,  1.,  0.,  0.,  0.,  0.],

 [ 0.,  0.,  1.,  0.,  1.,  0.,  0.,  0.],

 [ 0.,  0.,  0.,  1.,  0.,  1.,  0.,  0.],

 [ 0.,  0.,  0.,  0.,  1.,  0.,  1.,  1.],

 [ 0.,  0.,  0.,  0.,  0.,  1.,  0.,  0.],

 [ 0.,  1.,  0.,  0.,  0.,  1.,  0.,  0.]]

我需要一些函数来输出'CC1=NCCC(C)O1'。在

如果某个函数可以输出相应的"mol"对象,也可以这样做。RDkit软件具有'MolFromSmiles'功能。我想知道是否有类似'MolFromGraphs'的东西。在

提前谢谢你。在


Tags: 对象方法函数字符串功能软件smiles原子
1条回答
网友
1楼 · 发布于 2024-10-03 15:31:15

这是一个简单的解决方案,据我所知,RDKit中没有内置函数。在

def MolFromGraphs(node_list, adjacency_matrix):

    # create empty editable mol object
    mol = Chem.RWMol()

    # add atoms to mol and keep track of index
    node_to_idx = {}
    for i in range(len(node_list)):
        a = Chem.Atom(node_list[i])
        molIdx = mol.AddAtom(a)
        node_to_idx[i] = molIdx

    # add bonds between adjacent atoms
    for ix, row in enumerate(adjacency_matrix):
        for iy, bond in enumerate(row):

            # only traverse half the matrix
            if iy <= ix:
                continue

            # add relevant bond type (there are many more of these)
            if bond == 0:
                continue
            elif bond == 1:
                bond_type = Chem.rdchem.BondType.SINGLE
                mol.AddBond(node_to_idx[ix], node_to_idx[iy], bond_type)
            elif bond == 2:
                bond_type = Chem.rdchem.BondType.DOUBLE
                mol.AddBond(node_to_idx[ix], node_to_idx[iy], bond_type)

    # Convert RWMol to Mol object
    mol = mol.GetMol()            

    return mol
Chem.MolToSmiles(MolFromGraphs(nodes, a))

输出:
'CC1=NCCC(C)O1'

此解决方案是https://github.com/dakoner/keras-molecules/blob/dbbb790e74e406faa70b13e8be8104d9e938eba2/convert_rdkit_to_networkx.py的简化版本

还有许多其他的原子属性(如手性或质子化态)和键类型(三重键、与格键…)可能需要设置。如果可能的话,最好在图中明确地跟踪这些内容(如上面的链接所示),但是如果需要,也可以扩展此函数以合并这些内容。在

相关问题 更多 >