紐約大學、紐約大學上海分校、AWS上海研究院以及AWS MXNet Science Team共同開源了一個面向圖神經網絡及圖機器學習的全新框架,命名為Deep Graph Library(DGL)。
DGL必須和目前的主流的深度學習框架(Pytorch, MXNet, Tensorflow等)無縫銜接。從而實現從傳統的tensor運算到圖運算的自由轉換。
DGL應該提供最少的API以降低用戶的學習門檻。
在保證以上兩點的基礎之上,DGL能高效並透明地並行圖上的計算,以及能很方便地擴展到巨圖上。
個人關注的是藥物模型,用於分子性質預測,生成和優化的各種模型,DGL 致力於將GNN(圖形神經網絡)應用於化學領域,並且作為分子生成模型,DGMG(圖形的深度生成模型)和JT-VAE(連接樹變分自動編碼器),並且發行說明中有一個使用DGMG的非常簡單的示例。
為了評估候選藥物分子,我們需要了解其性質和活性。實際上,這主要是通過溼實驗室實驗來實現的。我們可以將該問題轉換為回歸或分類問題。實際上,由於標記數據的缺乏,這可能非常困難。
指紋已經成為化學信息學中廣泛使用的概念。化學家開發了一種規則,將分子轉換為二進位字符串,其中每個位都表明存在或不存在特定的子結構。指紋的發展使分子的比較容易得多。以前的機器學習方法主要基於分子指紋來開發。
圖神經網絡使數據驅動的原子、鍵和分子圖拓撲結構之外的分子表示成為可能,這可以看作是學習指紋。
圖的深度生成模型(DGMG,Deep Generative Models of Graphs):通過逐步添加原子和鍵來進行圖分布學習的非常通用的框架。
分子圖生成(JTNN,Junction Tree Variational Autoencoder for Molecular Graph Generation)的連接樹變分自動編碼器 :JTNN能夠逐步擴展分子,同時保持每一步的化學價。它們可用於分子生成和優化。
導入庫
import os
import numpy as np
import pandas as pd
from rdkit import Chem
from rdkit.Chem import RDConfig
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from torch.utils.data import DataLoader
from torch.utils.data import Dataset
import dgl
import dgl.function as fn
from dgl import DGLGraph
定義元素列表
ELEM_LIST = ['C', 'N', 'O', 'S', 'F', 'Si', 'P', 'Cl', 'Br', 'Mg', 'Na', 'Ca', 'Fe', 'Al', 'I', 'B', 'K', 'Se',
'Zn', 'H', 'Cu', 'Mn', 'unknown']
ATOM_FDIM = len(ELEM_LIST) + 6 + 5 + 4 + 1 # 23 + degree, charge, is_aromatic = 39
代碼來自dgl的 junction tree,生成分子結構圖
def get_mol(smiles):
mol = Chem.MolFromSmiles(smiles)
if mol is None:
return None
Chem.Kekulize(mol)
return mol
def onek_encoding_unk(x, allowable_set):
if x not in allowable_set:
x = allowable_set[-1]
return [x == s for s in allowable_set]
def atom_features(atom):
return (torch.Tensor(onek_encoding_unk(atom.GetSymbol(), ELEM_LIST)
+ onek_encoding_unk(atom.GetDegree(), [0,1,2,3,4,5])
+ onek_encoding_unk(atom.GetFormalCharge(), [-1,-2,1,2,0])
+ onek_encoding_unk(int(atom.GetChiralTag()), [0,1,2,3])
+ [atom.GetIsAromatic()]))
def mol2dgl_single(mols):
"""
inputs
mols: a list of molecules
outputs
cand_graphs: a list of dgl graphs
"""
cand_graphs = []
for mol in mols:
n_atoms = mol.GetNumAtoms()
g = DGLGraph()
node_feats = []
for i, atom in enumerate(mol.GetAtoms()):
assert i == atom.GetIdx()
node_feats.append(atom_features(atom))
g.add_nodes(n_atoms)
bond_src = []
bond_dst = []
for i, bond in enumerate(mol.GetBonds()):
a1 = bond.GetBeginAtom()
a2 = bond.GetEndAtom()
begin_idx = a1.GetIdx()
end_idx = a2.GetIdx()
bond_src.append(begin_idx)
bond_dst.append(end_idx)
bond_src.append(end_idx)
bond_dst.append(begin_idx)
g.add_edges(bond_src, bond_dst)
g.ndata['h'] = torch.Tensor([a.tolist() for a in node_feats])
cand_graphs.append(g)
return cand_graphs
載入數據,轉換smiles到分子圖
smiles = ['OCCS(=O)(=O)c1no[n+]([O-])c1c2ccccc2', 'Cl.CCCC1(C)CC(=O)N(CCCCN2CCN(CC2)c3nsc4ccccc34)C(=O)C1']
mols = []
for sm in smiles:
mol = get_mol(sm)
mols.append(mol)
graphs = mol2dgl_single(mols)
查看第一個分子的鄰接矩陣
graphs[0].adjacency_matrix().to_dense()
輸出領接矩陣的每個節點
for a in graphs[1].adjacency_matrix().to_dense():
print(a)
主頁地址:http://dgl.ai
項目地址:https://github.com/dmlc/dgl
初學者教程:https://docs.dgl.ai/tutorials/basics/index.html
所有示例模型的詳細從零教程:https://docs.dgl.ai/tutorials/models/index.html
DrugAI
( 掃描下方二維碼訂閱獲取最新消息!)
本文為DrugAI原創,如需轉載,請在公眾號後臺留言。