Repository navigation
Expand file tree
/
Copy pathexample.py
More file actions
72 lines (62 loc) · 3.12 KB
/
Copy pathexample.py
File metadata and controls
72 lines (62 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from sklearn.model_selection import KFold
from torch.utils.data import DataLoader
from NeuralGraph.dataset import MolData
from NeuralGraph.model import QSAR
import pandas as pd
import numpy as np
def main(target_id=None, reg=False, is_extra=True):
pair = ['TARGET_CHEMBLID', 'CMPD_CHEMBLID', 'PCHEMBL_VALUE',
'CANONICAL_SMILES', 'ACTIVITY_COMMENT', 'STANDARD_TYPE', 'RELATION']
df = pd.read_csv('dataset/AR_ALL.csv')
if target_id is not None:
df = df[df['TARGET_CHEMBLID'] == target_id]
cmps = df.set_index(pair[1])[pair[3]].drop_duplicates()
df = df[pair].set_index(pair[0:2])
df['PCHEMBL_VALUE'] = df.groupby(pair[0:2]).mean()
numery = df[pair[2:4]].dropna().drop_duplicates()
comments = df[(df.ACTIVITY_COMMENT.str.contains('Not Active') == True)]
inhibits = df[(df.STANDARD_TYPE == 'Inhibition') & df.RELATION.isin(['<', '<='])]
relations = df[df.STANDARD_TYPE.isin(['EC50', 'IC50', 'Kd', 'Ki']) & df.RELATION.isin(['>', '>='])]
binary = pd.concat([comments, inhibits, relations], axis=0)
binary = binary[~binary.index.isin(numery.index)]
binary['PCHEMBL_VALUE'] = 3.99
binary = binary[pair[2:4]].dropna().drop_duplicates()
df = numery.append(binary)
df = df[pair[2]].unstack(pair[0])
df = df.sample(len(df))
if reg:
test = binary[pair[2]].sample(len(binary)).unstack(pair[0])
else:
df = (df > 6.5).astype(float)
test = df.sample(len(df)//8)
df = df.drop(test.index)
data = df if is_extra else numery.sample(len(numery))
indep_set = MolData(cmps.loc[test.index], test.values)
indep_loader = DataLoader(indep_set, batch_size=BATCH_SIZE)
folds = KFold(5).split(data)
cvs = np.zeros(data.shape)
inds = np.zeros(test.shape)
out = 'output/gcn%s' % ('_' + target_id if target_id else '')
for i, (trained, valided) in enumerate(folds):
net = QSAR(hid_dim=128, n_class=data.shape[1])
train_set, valid_set = data.iloc[trained], data.iloc[valided]
train_set = MolData(cmps.loc[train_set.index], train_set.values)
valid_set = MolData(cmps.loc[valid_set.index], valid_set.values)
train_loader = DataLoader(train_set, batch_size=BATCH_SIZE, shuffle=True, drop_last=True)
valid_loader = DataLoader(valid_set, batch_size=BATCH_SIZE)
net = net.fit(train_loader, valid_loader, epochs=N_EPOCH, path='%s_%d' % (out, i))
print('Evaluation of Loss in validation Set: %f' % net.evaluate(valid_loader))
print('Evaluation of Loss in independent Set: %f' % net.evaluate(indep_loader))
cvs[valided] = net.predict(valid_loader)
inds += net.predict(indep_loader)
data_score, test_score = pd.DataFrame(), pd.DataFrame()
data_score['LABEL'] = data.stack()
test_score['LABEL'] = test.stack()
data_score['SCORE'] = pd.DataFrame(cvs, index=data.index, columns=data.columns).stack()
test_score['SCORE'] = pd.DataFrame(inds / 5, index=test.index, columns=test.columns).stack()
data_score.to_csv(out + '.cv.txt')
test_score.to_csv(out + '.ind.txt')
if __name__ == '__main__':
BATCH_SIZE = 128
N_EPOCH = 1000
main('CHEMBL251')