当前位置:网站首页>基于Pytorch和RDKit的QSAR模型建立脚本
基于Pytorch和RDKit的QSAR模型建立脚本
2022-07-03 03:42:00 【LRJ-jonas】
QSAR
定量构效方法(quantitative structure-activity relationship, QSAR)是应用最为广泛的药物设计方法。所谓定量构效方法就是通过一些数理统计方法建立其一系列化合 物的生理活性或某种性质与其物理化学性质之间的定量关系,通过这些定关系。可以预测化合物的生理活性或某些性质,指导我们设计出具有更高活性的化合物。
安装环境
pip install pprint
pip install argparse
#安装rdkit
conda install -c rdkit rdkit
安装pytorch 教程
基于pytorch和rdkit的QSAR模型建立
#!/usr/bin/python3
import pprint
import argparse
import torch
import torch.optim as optim
from torch import nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
from rdkit import Chem
from rdkit.Chem import AllChem
from rdkit.Chem import DataStructs
import numpy as np
#from sklearn import preprocessing
def base_parser():
parser = argparse.ArgumentParser("This is simple test of pytorch")
parser.add_argument("trainset", help="sdf for train")
parser.add_argument("testset", help="sdf for test")
parser.add_argument("--epochs", default=150)
return parser
parser = base_parser()
args = parser.parse_args()
traindata = [mol for mol in Chem.SDMolSupplier(args.trainset) if mol is not None]
testdata = [mol for mol in Chem.SDMolSupplier(args.testset) if mol is not None]
def molsfeaturizer(mols):
fps = []
for mol in mols:
arr = np.zeros((0,))
fp = AllChem.GetMorganFingerprintAsBitVect(mol, 2)
DataStructs.ConvertToNumpyArray(fp, arr)
fps.append(arr)
fps = np.array(fps, dtype = np.float)
return fps
classes = {"(A) low":0, "(B) medium":1, "(C) high":2}
#classes = {"(A) low":0, "(B) medium":1, "(C) high":1}
trainx = molsfeaturizer(traindata)
testx = molsfeaturizer(testdata)
# for pytorch, y must be long type!!
trainy = np.array([classes[mol.GetProp("SOL_classification")] for mol in traindata], dtype=np.int64)
testy = np.array([classes[mol.GetProp("SOL_classification")] for mol in testdata], dtype=np.int64)
#在pytorch中构建模型,定义每个层和整个结构
X_train = torch.from_numpy(trainx)
X_test = torch.from_numpy(testx)
Y_train = torch.from_numpy(trainy)
Y_test = torch.from_numpy(testy)
print(X_train.size(),Y_train.size())
print(X_test.size(), Y_train.size())
class QSAR_mlp(nn.Module):
def __init__(self):
super(QSAR_mlp, self).__init__()
self.fc1 = nn.Linear(2048, 524)
self.fc2 = nn.Linear(524, 10)
self.fc3 = nn.Linear(10, 10)
self.fc4 = nn.Linear(10,3)
def forward(self, x):
x = x.view(-1, 2048)
h1 = F.relu(self.fc1(x))
h2 = F.relu(self.fc2(h1))
h3 = F.relu(self.fc3(h2))
output = F.sigmoid(self.fc4(h3))
return output
#构建训练和预测模型
model = QSAR_mlp()
print(model)
losses = []
optimizer = optim.Adam( model.parameters(), lr=0.005)
for epoch in range(args.epochs):
data, target = Variable(X_train).float(), Variable(Y_train).long()
optimizer.zero_grad()
y_pred = model(data)
loss = F.cross_entropy(y_pred, target)
print("Loss: {}".format(loss.data[0]))
loss.backward()
optimizer.step()
pred_y = model(Variable(X_test).float())
predicted = torch.max(pred_y, 1)[1]
for i in range(len(predicted)):
print("pred:{}, target:{}".format(predicted.data[i], Y_test[i]))
print( "Accuracy: {}".format(sum(p==t for p,t in zip(predicted.data, Y_test))/len(Y_test)))
测试模型
python qsar_pytorch.py solubility.train.sdf solubility.test.sdf
边栏推荐
- How does the pytorch project run?
- pytorch开源吗?
- Hutool动态添加定时任务
- Tidal characteristics of the Bohai Sea and the Yellow Sea
- 递归使用和多维数组对象变一维数组对象
- 递归:深度优先搜索
- 2022 P cylinder filling examination content and P cylinder filling practice examination video
- MongoDB主配置文件
- In Net 6 project using startup cs
- Web session management security issues
猜你喜欢
FileZilla client download and installation
Download and install captura and configure ffmpeg in captura
Error in compiled file: error: unmapped character encoding GBK
Elsevier latex submitted the article pdftex def Error: File `thumbnails/cas-email. jpeg‘ not found: using draf
IPv6 transition technology-6to4 manual tunnel configuration experiment -- Kuige of Shangwen network
Mysql Mac版下载安装教程
简易版 微信小程序开发之页面跳转、数据绑定、获取用户信息、获取用户位置信息
IPv6过渡技术-6to4手工隧道配置实验--尚文网络奎哥
The latest analysis of the main principals of hazardous chemical business units in 2022 and the simulated examination questions of the main principals of hazardous chemical business units
Simple wechat applet development page Jump, data binding, obtaining user information, obtaining user location information
随机推荐
在 .NET 6 项目中使用 Startup.cs
ffmpeg之 一张/多张图片合成视频
Arlo's thinking about himself
How to download pytorch? Where can I download pytorch?
递归:深度优先搜索
Elsevier latex submitted the article pdftex def Error: File `thumbnails/cas-email. jpeg‘ not found: using draf
简易版 微信小程序开发之for指令、上传图片及展示效果优化
学会pytorch能干什么?
2022 Shandong Province safety officer C certificate examination questions and Shandong Province safety officer C certificate simulation examination question bank
Tidal characteristics of the Bohai Sea and the Yellow Sea
How does the pytorch project run?
What can learning pytorch do?
[mathematical logic] propositional logic (propositional logic reasoning | formal structure of reasoning | inference law | additional law | simplification law | hypothetical reasoning | refusal | disju
Is pytorch difficult to learn? How to learn pytorch well?
PHP generates PDF tcpdf
可分离债券与可转债
Wechat applet + Alibaba IOT platform + Hezhou air724ug build a serverless IOT system (III) -- wechat applet is directly connected to Alibaba IOT platform aliiot
[mathematical logic] propositions and connectives (propositions | propositional symbolization | truth connectives | no | conjunction | disjunction | non truth connectives | implication | equivalence)
Separable bonds and convertible bonds
Compare float with 0