当前位置:网站首页>基于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
边栏推荐
- redis在服务器linux下的启动的相关命令(安装和配置)
- MongoDB基本操作【增、删、改、查】
- Wechat applet + Alibaba IOT platform + Hezhou air724ug build a serverless IOT system (III) -- wechat applet is directly connected to Alibaba IOT platform aliiot
- Write it down once Net travel management background CPU Explosion Analysis
- FileZilla Client下载安装
- CEPH Shangwen network xUP Nange that releases the power of data
- pytorch项目怎么跑?
- Numpy warning visibledeprecationwarning: creating an ndarray from ragged needed sequences
- [embedded module] OLED display module
- Read a paper_ ChineseBert
猜你喜欢
TCP/IP模型中的重磅嘉宾TCP--尚文网络奎哥
ffmpeg之 一张/多张图片合成视频
pytorch开源吗?
2022 tea master (intermediate) examination questions and analysis and tea master (intermediate) practical examination video
编译文件时报错:错误: 编码GBK的不可映射字符
Use three JS make a simple 3D scene
Ffmpeg one / more pictures synthetic video
NPM: the 'NPM' item cannot be recognized as the name of a cmdlet, function, script file, or runnable program. Please check the spelling of the name. If the path is included, make sure the path is corr
Ffmpeg download and installation tutorial and introduction
递归:一维链表和数组
随机推荐
MySQL MAC download and installation tutorial
【学习笔记】seckill-秒杀项目--(11)项目总结
Introduction to mongodb
Ansible简介【暂未完成(半成品)】
Ansible introduction [unfinished (semi-finished products)]
MongoDB安装 & 部署
Bid farewell to artificial mental retardation: Mengzi open source project team received RMB 100 million financing to help NLP develop
Pytoch lightweight visualization tool wandb (local)
Web session management security issues
Message queue addition failure
SAP UI5 应用开发教程之一百零五 - SAP UI5 Master-Detail 布局模式的联动效果实现明细介绍
pytorch开源吗?
Summary of electromagnetic spectrum
105. Detailed introduction of linkage effect realization of SAP ui5 master detail layout mode
阿洛对自己的思考
PHP generates PDF tcpdf
Null and undefined
Convert binary stream to byte array
Elsevier latex submitted the article pdftex def Error: File `thumbnails/cas-email. jpeg‘ not found: using draf
机械臂速成小指南(八):运动学建模(标准DH法)