-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.py
More file actions
217 lines (176 loc) · 7.24 KB
/
Copy pathtrain.py
File metadata and controls
217 lines (176 loc) · 7.24 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import pandas as pd
import numpy as np
from tqdm import tqdm
from sklearn.model_selection import train_test_split
from collections import Counter
from constants import x_map, e_map
from model import Net
from Bio import SeqIO
from rdkit import Chem
import torch
from torch import nn
from torch_geometric.data import Data
from torch_geometric.loader import DataLoader
from torch_geometric.utils import one_hot
# Load the data
df = pd.read_csv('ALL/DATA/NR-DBIND.csv', delimiter=';')
df['ID'] = df['ID'].astype(int)
df.dropna(subset=['accession'], inplace=True)
df.reset_index(drop=True, inplace=True)
row_indexes = {}
for index, row in df.iterrows():
key = (row['accession'], row['canonical_smile'])
if key in row_indexes:
row_indexes[key].append(index)
else:
row_indexes[key] = [index]
# Create a dataset with the median p_binding_value for each unique protein-canonical_smiles pair
dataset = []
for (protein_id, smiles), indexes in row_indexes.items():
values = [df.loc[index]['p_binding_value'] for index in indexes if not np.isnan(df.loc[index]['p_binding_value'])]
if len(values) > 0:
p_binding_value = np.median(values).item()
if p_binding_value < 5.0 or p_binding_value >= 8:
continue
y_class = int(p_binding_value) - 5
y_one_hot = torch.zeros(3)
y_one_hot[y_class] = 1
dataset.append(Data(
protein_id=protein_id,
smiles=smiles,
y=p_binding_value,
# y_class=y_one_hot,
y_class=y_class)
)
# Load the protein sequences
input_fasta_file = 'seqs_raw.fasta'
output_fasta_file = 'seqs.fasta'
protein_ids = []
records = SeqIO.parse(input_fasta_file, 'fasta')
renamed_records = []
for record in records:
protein_id = record.id.split('|')[1]
protein_ids.append(protein_id)
record.id = protein_id
record.name = protein_id
record.description = protein_id
renamed_records.append(record)
SeqIO.write(renamed_records, output_fasta_file, 'fasta')
protein_embeds = {}
for protein_id in protein_ids:
protein_embeds[protein_id] = torch.load(f'./seq_embeds/{protein_id}.pt')['representations'][33]
for data in dataset:
protein_id = data.protein_id
data.protein_embed = protein_embeds[protein_id]
# Create the node and edge features
for data in dataset:
mol = Chem.MolFromSmiles(data.smiles)
ringinfo = mol.GetRingInfo()
x = []
atoms = [atom for atom in mol.GetAtoms()] # type: ignore
x.append(one_hot(
torch.tensor([x_map['atomic_num'].index(atom.GetAtomicNum()) for atom in atoms]),
num_classes=len(x_map['atomic_num']), dtype=torch.float)
)
x.append(one_hot(
torch.tensor([x_map['degree'].index(atom.GetTotalDegree()) for atom in atoms]),
num_classes=len(x_map['degree']), dtype=torch.float)
)
x.append(one_hot(
torch.tensor([x_map['num_hs'].index(atom.GetTotalNumHs()) for atom in atoms]),
num_classes=len(x_map['num_hs']), dtype=torch.float)
)
x.append(one_hot(
torch.tensor([x_map['hybridization'].index(str(atom.GetHybridization())) for atom in atoms]),
num_classes=len(x_map['hybridization']), dtype=torch.float)
)
x.append(
torch.tensor([[x_map['is_aromatic'].index(atom.GetIsAromatic())] for atom in atoms], dtype=torch.float)
)
x.append(
torch.tensor([[x_map['is_in_ring'].index(atom.IsInRing())] for atom in atoms], dtype=torch.float)
)
x.append(one_hot(
torch.tensor([x_map['formal_charge'].index(atom.GetFormalCharge()) for atom in atoms]),
num_classes=len(x_map['formal_charge']), dtype=torch.float)
)
x.append(one_hot(
torch.tensor([x_map['implicit_valence'].index(atom.GetImplicitValence()) for atom in atoms]),
num_classes=len(x_map['implicit_valence']), dtype=torch.float)
)
x.append(one_hot(
torch.tensor([x_map['num_radical_electrons'].index(atom.GetNumRadicalElectrons()) for atom in atoms]),
num_classes=len(x_map['num_radical_electrons']), dtype=torch.float)
)
x.append(one_hot(
torch.tensor([x_map['num_atom_rings'].index(ringinfo.NumAtomRings(atom.GetIdx())) for atom in atoms]),
num_classes=len(x_map['num_atom_rings']), dtype=torch.float)
)
x.append(
torch.tensor([[x_map['is_in_ring_3'].index(ringinfo.IsAtomInRingOfSize(atom.GetIdx(), 3))] for atom in atoms], dtype=torch.float)
)
x.append(
torch.tensor([[x_map['is_in_ring_4'].index(ringinfo.IsAtomInRingOfSize(atom.GetIdx(), 4))] for atom in atoms], dtype=torch.float)
)
x.append(
torch.tensor([[x_map['is_in_ring_5'].index(ringinfo.IsAtomInRingOfSize(atom.GetIdx(), 5))] for atom in atoms], dtype=torch.float)
)
x.append(
torch.tensor([[x_map['is_in_ring_6'].index(ringinfo.IsAtomInRingOfSize(atom.GetIdx(), 6))] for atom in atoms], dtype=torch.float)
)
x.append(
torch.tensor([[x_map['is_in_ring_7'].index(ringinfo.IsAtomInRingOfSize(atom.GetIdx(), 7))] for atom in atoms], dtype=torch.float)
)
x.append(
torch.tensor([[x_map['is_in_ring_8'].index(ringinfo.IsAtomInRingOfSize(atom.GetIdx(), 8))] for atom in atoms], dtype=torch.float)
)
x = torch.cat(x, dim=1)
data.x = x
edge_index = torch.tensor(Chem.GetAdjacencyMatrix(mol), dtype=torch.long).to_sparse().indices()
edge_attr = []
for i in range(edge_index.shape[1]):
bond = mol.GetBondBetweenAtoms(edge_index[0][i].item(), edge_index[1][i].item())
edge_attr.append(e_map['bond_type'].index(str(bond.GetBondType())))
edge_attr = torch.tensor(edge_attr, dtype=torch.long)
edge_attr = one_hot(edge_attr, num_classes=len(e_map['bond_type']), dtype=torch.float)
data.edge_index = edge_index
data.edge_attr = edge_attr
# Split the dataset
train_dataset, test_dataset = train_test_split(dataset, test_size=0.2)
train_loader = DataLoader(train_dataset, batch_size=16, shuffle=True)
test_loader = DataLoader(test_dataset, batch_size=16, shuffle=False)
# Train the model
model = Net()
num_epochs = 10
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
loss_fn = nn.CrossEntropyLoss()
for epoch in range(num_epochs):
model.train()
train_loss = 0.0
for batch in tqdm(train_loader, desc=f"Training Epoch {epoch+1}/{num_epochs}"):
optimizer.zero_grad()
logits = model(batch)
loss = loss_fn(logits, batch.y_class)
loss.backward()
optimizer.step()
train_loss += loss.item() * len(batch)
avg_train_loss = train_loss / len(train_loader.dataset)
model.eval()
test_loss = 0.0
num_correct = 0
num_samples = 0
with torch.no_grad():
for batch in tqdm(test_loader, desc="Testing"):
logits = model(batch)
loss = loss_fn(logits, batch.y_class)
test_loss += loss.item() * len(batch)
_, preds = logits.max(dim=1)
num_correct += (preds == batch.y_class).sum().item()
num_samples += batch.y_class.size(0)
avg_test_loss = test_loss / len(test_loader.dataset)
acc = num_correct / num_samples
print(f'Epoch: {epoch+1}/{num_epochs}, '
f'Train Loss: {avg_train_loss:.4f}, '
f'Test Loss: {avg_test_loss:.4f}, '
f'Accuracy: {acc:.4f}')
print()