-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdata.py
383 lines (319 loc) · 16.9 KB
/
data.py
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
import os
import pickle
import torch
from torch.utils.data import DataLoader
from torch.utils.data import Dataset
import pandas as pd
import random
class PytdcDatasetTriplet(Dataset):
def __init__(self, dataframe, configs, nearest_neighbors):
"""
Initializes the PytdcDatasetTriplet dataset object.
Args:
dataframe (pd.DataFrame): A DataFrame containing the data to be used in this dataset.
configs: Configuration parameters that include dataset and model settings.
This method processes the dataframe to create dictionaries that map TCR sequences to their
associated epitopes and vice versa, for both positive and negative pairs. It also generates a
list of unique epitopes for sampling purposes.
"""
self.configs = configs
self.nearest_neighbors = nearest_neighbors
# Using specific columns for features and labels
if configs.tcr_embedding_source == "BindingSite":
TCR = dataframe['tcr'].values
elif configs.tcr_embedding_source == "Full":
TCR = dataframe['tcr_full'].values
else:
raise ValueError("Invalid TCR embedding source specified in configs.")
epitope = dataframe['epitope_aa'].values
label = dataframe['label'].values
# Storing TCR and epitopes based on label, these are positive pairs
self.TCR = TCR[label == 1]
self.epitope = epitope[label == 1]
# These are negative pairs in the original dataset
self.TCR_neg = TCR[label != 1]
self.epitope_neg = epitope[label != 1]
# Generate dictionaries mapping TCR to all related epitopes and vice versa
self.TCR_epitope = {}
self.epitope_TCR = {}
for tcr, epi in zip(self.TCR, self.epitope):
if tcr not in self.TCR_epitope:
self.TCR_epitope[tcr] = []
self.TCR_epitope[tcr].append(epi)
if epi not in self.epitope_TCR:
self.epitope_TCR[epi] = []
self.epitope_TCR[epi].append(tcr)
# Negative sampling dictionaries
self.TCR_epitope_neg = {}
self.epitope_TCR_neg = {}
for tcr, epi_neg in zip(self.TCR, self.epitope_neg):
if tcr not in self.TCR_epitope_neg:
self.TCR_epitope_neg[tcr] = []
self.TCR_epitope_neg[tcr].append(epi_neg)
for epi, tcr_neg in zip(self.epitope, self.TCR_neg):
if epi not in self.epitope_TCR_neg:
self.epitope_TCR_neg[epi] = []
self.epitope_TCR_neg[epi].append(tcr_neg)
self.full_list = []
if configs.batch_mode == "ByEpitope":
for ep in self.epitope_TCR.keys():
self.full_list.append(ep)
elif configs.batch_mode == "Regular":
for tcr, epitope in zip(self.TCR, self.epitope):
self.full_list.append((tcr, epitope))
else:
raise ValueError("Invalid batch mode specified in configs.")
def __len__(self):
"""
"""
return len(self.full_list)
def __getitem__(self, idx):
"""
Retrieves a single data sample for the triplet-based contrastive learning task.
Args:
idx (int): Index for accessing the anchor epitope from the unique epitopes list.
Returns:
dict: A dictionary containing 'anchor_TCR', 'positive_TCR', and 'negative_TCR'.
These are the sequences involved in the triplet, with the anchor TCR corresponding
to the sampled anchor epitope.
"""
if self.configs.batch_mode == "ByEpitope":
anchor_epitope = self.full_list[idx]
anchor_TCR = random.choice(self.epitope_TCR[anchor_epitope])
elif self.configs.batch_mode == "Regular":
anchor_TCR, anchor_epitope = self.full_list[idx]
else:
raise ValueError("Invalid batch mode specified in configs.")
positive_TCR = random.choice([tcr for tcr in self.epitope_TCR[anchor_epitope] if tcr != anchor_TCR])
# Select a negative TCR based on configuration setting
if self.configs.negative_sampling_mode == 'RandomNeg':
# Option 1: Randomly select from negative pairs
negative_TCR_candidates = list(set(self.epitope_TCR_neg.get(anchor_epitope, [])) - {anchor_TCR})
if not negative_TCR_candidates:
# Fallback to 'ExcludePos' method if no negative candidates are found
all_options = set(self.TCR_epitope.keys())
positive_options = set(self.epitope_TCR[anchor_epitope])
negative_TCR_candidates = list(all_options - positive_options)
negative_TCR = random.choice(negative_TCR_candidates)
elif self.configs.negative_sampling_mode == 'ExcludePos':
# Option 2: Exclude all positive samples and randomly select
all_options = set(self.TCR_epitope.keys())
positive_options = set(self.epitope_TCR[anchor_epitope])
non_positive_options = list(all_options-positive_options)
negative_TCR = random.choice(non_positive_options)
elif self.configs.negative_sampling_mode == 'HardNeg':
# Option 3: Hard negative samples mining
all_options = set(self.TCR_epitope.keys())
positive_options = set(self.epitope_TCR[anchor_epitope])
neg_options = list(all_options - positive_options)
if self.nearest_neighbors is not None:
nearest_list = self.nearest_neighbors.get(anchor_epitope)
# print(anchor_epitope)
if nearest_list is not None:
neg_epitope_options = [i['epitope'] for i in nearest_list]
neg_options = set()
for i in neg_epitope_options:
neg_options |= set(self.epitope_TCR[i])
neg_options.discard(anchor_TCR)
neg_options = list(neg_options)
negative_TCR = random.choice(neg_options)
else:
raise ValueError("Invalid negative sampling strategy specified in configs.")
return {'anchor_epitope': anchor_epitope, 'anchor_TCR': anchor_TCR, 'positive_TCR': positive_TCR, 'negative_TCR': negative_TCR}
class PytdcDatasetMulti(Dataset):
def __init__(self, dataframe, configs, nearest_neighbors):
self.configs = configs
self.nearest_neighbors = nearest_neighbors
self.n_pos = configs.n_pos
self.n_neg = configs.n_neg
# Using specific columns for features and labels
if configs.tcr_embedding_source == "BindingSite":
TCR = dataframe['tcr'].values
elif configs.tcr_embedding_source == "Full":
TCR = dataframe['tcr_full'].values
else:
raise ValueError("Invalid TCR embedding source specified in configs.")
epitope = dataframe['epitope_aa'].values
label = dataframe['label'].values
# Storing TCR and epitopes based on label, these are positive pairs
self.TCR = TCR[label == 1]
self.epitope = epitope[label == 1]
# These are negative pairs in the original dataset
self.TCR_neg = TCR[label != 1]
self.epitope_neg = epitope[label != 1]
# Generate dictionaries mapping TCR to all related epitopes and vice versa
self.TCR_epitope = {}
self.epitope_TCR = {}
for tcr, epi in zip(self.TCR, self.epitope):
if tcr not in self.TCR_epitope:
self.TCR_epitope[tcr] = []
self.TCR_epitope[tcr].append(epi)
if epi not in self.epitope_TCR:
self.epitope_TCR[epi] = []
self.epitope_TCR[epi].append(tcr)
# Negative sampling dictionaries
self.TCR_epitope_neg = {}
self.epitope_TCR_neg = {}
for tcr, epi_neg in zip(self.TCR, self.epitope_neg):
if tcr not in self.TCR_epitope_neg:
self.TCR_epitope_neg[tcr] = []
self.TCR_epitope_neg[tcr].append(epi_neg)
for epi, tcr_neg in zip(self.epitope, self.TCR_neg):
if epi not in self.epitope_TCR_neg:
self.epitope_TCR_neg[epi] = []
self.epitope_TCR_neg[epi].append(tcr_neg)
self.full_list = []
if configs.batch_mode == "ByEpitope":
for ep in self.epitope_TCR.keys():
self.full_list.append(ep)
elif configs.batch_mode == "Regular":
for tcr, epitope in zip(self.TCR, self.epitope):
self.full_list.append((tcr, epitope))
else:
raise ValueError("Invalid batch mode specified in configs.")
def __len__(self):
"""
"""
return len(self.full_list)
def __getitem__(self, idx):
"""
Retrieves a single data sample for the triplet-based contrastive learning task.
Args:
idx (int): Index for accessing the anchor epitope from the unique epitopes list.
Returns:
dict: A dictionary containing 'anchor_TCR', 'positive_TCR', and 'negative_TCR'.
These are the sequences involved in the triplet, with the anchor TCR corresponding
to the sampled anchor epitope.
"""
if self.configs.batch_mode == "ByEpitope":
anchor_epitope = self.full_list[idx]
anchor_TCR = random.choice(self.epitope_TCR[anchor_epitope])
elif self.configs.batch_mode == "Regular":
anchor_TCR, anchor_epitope = self.full_list[idx]
else:
raise ValueError("Invalid batch mode specified in configs.")
positive_TCR_candidates = [tcr for tcr in self.epitope_TCR[anchor_epitope] if tcr != anchor_TCR]
if len(positive_TCR_candidates) < self.n_pos:
positive_TCR_candidates = positive_TCR_candidates * (self.n_pos // len(positive_TCR_candidates) + 1)
positive_TCR = random.sample(positive_TCR_candidates, self.n_pos)
# Select a negative TCR based on configuration setting
if self.configs.negative_sampling_mode == 'RandomNeg':
# Option 1: Randomly select from negative pairs
negative_TCR_candidates = list(set(self.epitope_TCR_neg.get(anchor_epitope, [])) - {anchor_TCR})
if not negative_TCR_candidates:
# Fallback to 'ExcludePos' method if no negative candidates are found
all_options = set(self.TCR_epitope.keys())
positive_options = set(self.epitope_TCR[anchor_epitope])
negative_TCR_candidates = list(all_options - positive_options)
if len(negative_TCR_candidates) < self.n_neg:
negative_TCR_candidates = negative_TCR_candidates * (self.n_neg // len(negative_TCR_candidates) + 1)
negative_TCR = random.sample(negative_TCR_candidates, self.n_neg)
elif self.configs.negative_sampling_mode == 'ExcludePos':
# Option 2: Exclude all positive samples and randomly select
all_options = set(self.TCR_epitope.keys())
positive_options = set(self.epitope_TCR[anchor_epitope])
non_positive_options = list(all_options - positive_options)
if len(non_positive_options) < self.n_neg:
non_positive_options = non_positive_options * (self.n_neg // len(non_positive_options) + 1)
negative_TCR = random.sample(non_positive_options, self.n_neg)
elif self.configs.negative_sampling_mode == 'HardNeg':
# Option 3: Hard negative samples mining
all_options = set(self.TCR_epitope.keys())
positive_options = set(self.epitope_TCR[anchor_epitope])
neg_options = list(all_options - positive_options)
if self.nearest_neighbors is not None:
nearest_list = self.nearest_neighbors.get(anchor_epitope)
# print(anchor_epitope)
if nearest_list is not None:
neg_epitope_options = [i['epitope'] for i in nearest_list]
neg_options = set()
for i in neg_epitope_options:
neg_options |= set(self.epitope_TCR[i])
neg_options.discard(anchor_TCR)
neg_options = list(neg_options)
if len(neg_options) < self.n_neg:
neg_options = neg_options * (self.n_neg // len(neg_options) + 1)
negative_TCR = random.sample(neg_options, self.n_neg)
else:
raise ValueError("Invalid negative sampling strategy specified in configs.")
# print("anc", anchor_TCR)
# print("pos", positive_TCR)
# print("neg", negative_TCR)
anchor_positive_negative_TCR = [anchor_TCR]
anchor_positive_negative_TCR.extend(positive_TCR)
anchor_positive_negative_TCR.extend(negative_TCR)
# print(anchor_positive_negative_TCR)
# print(len(anchor_positive_negative_TCR))
# exit(0)
return {'anchor_epitope': anchor_epitope, 'anchor_positive_negative_TCR': anchor_positive_negative_TCR}
def preserve_structure_collate_fn(batch):
return batch
def get_dataloader(configs, nearest_neighbors):
if configs.dataset == "PyTDC":
train_data = pd.read_csv(f'./dataset/pytdc_new/train1_PyTDC.csv')
# valid_data = pd.read_csv(f'./dataset/pytdc/valid_PyTDC.csv')
if configs.contrastive_mode == "Triplet":
train_dataset = PytdcDatasetTriplet(train_data, configs, nearest_neighbors)
# valid_dataset = PytdcDatasetTriplet(valid_data, configs, nearest_neighbors)
elif configs.contrastive_mode == "MultiPosNeg":
train_dataset = PytdcDatasetMulti(train_data, configs, nearest_neighbors)
# valid_dataset = PytdcDatasetMulti(valid_data, configs, nearest_neighbors)
else:
raise ValueError("Wrong contrastive mode specified.")
if configs.batch_mode == "ByEpitope":
batch_size = len(train_dataset.epitope_TCR.keys())
elif configs.batch_mode == "Regular":
batch_size = configs.batch_size
else:
raise ValueError("Invalid batch mode specified in configs.")
if configs.contrastive_mode == "Triplet":
train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True, drop_last=True)
# valid_loader = DataLoader(valid_dataset, batch_size=batch_size, shuffle=False)
elif configs.contrastive_mode == "MultiPosNeg":
train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True, collate_fn=preserve_structure_collate_fn, drop_last=True)
# valid_loader = DataLoader(valid_dataset, batch_size=batch_size, shuffle=False, collate_fn=preserve_structure_collate_fn)
return {'train1_loader': train_loader, 'train2_loader': None, # valid_loader,
'epitope_TCR': train_dataset.epitope_TCR, 'TCR_epitope': train_dataset.TCR_epitope,
'epitope_TCR_neg': train_dataset.epitope_TCR_neg, 'TCR_epitope_neg': train_dataset.TCR_epitope_neg}
else:
raise ValueError("Wrong dataset specified.")
class PytdcDatasetInfer(Dataset):
def __init__(self, dataframe, configs, is_train=False):
self.configs = configs
if configs.tcr_embedding_source == "BindingSite":
TCR = dataframe['tcr'].values
elif configs.tcr_embedding_source == "Full":
TCR = dataframe['tcr_full'].values
else:
raise ValueError("Invalid TCR embedding source specified in configs.")
epitope = dataframe['epitope_aa'].values
label = dataframe['label'].values
if is_train:
TCR = TCR[label == 1]
epitope = epitope[label == 1]
label = label[label == 1]
self.TCR = TCR
self.epitope = epitope
self.label = label
self.full_list = []
for tcr, epitope, label in zip(self.TCR, self.epitope, self.label):
self.full_list.append((tcr, epitope, label))
def __len__(self):
return len(self.full_list)
def __getitem__(self, idx):
TCR, epitope, label = self.full_list[idx]
return {'epitope': epitope, 'TCR': TCR, 'label': label}
def get_dataloader_extraction(configs):
if configs.dataset == "PyTDC":
train_data = pd.read_csv(f'./dataset/pytdc_new/train1_PyTDC.csv')
valid_data = pd.read_csv(f'./dataset/pytdc_new/train2_PyTDC.csv')
test_data = pd.read_csv(f'./dataset/pytdc_new/test_PyTDC.csv')
train_dataset = PytdcDatasetInfer(train_data, configs, is_train=True)
train_loader = DataLoader(train_dataset, batch_size=256, shuffle=False)
valid_dataset = PytdcDatasetInfer(valid_data, configs)
valid_loader = DataLoader(valid_dataset, batch_size=256, shuffle=False)
test_dataset = PytdcDatasetInfer(test_data, configs)
test_loader = DataLoader(test_dataset, batch_size=256, shuffle=False)
return {'train1_loader': train_loader, 'train2_loader': valid_loader, 'test_loader': test_loader}
else:
raise ValueError("Wrong dataset specified.")