-
Notifications
You must be signed in to change notification settings - Fork 3
/
dataloader.py
589 lines (498 loc) · 19.5 KB
/
dataloader.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
# standard library
import pathlib
import pickle
from typing import List, Union, Type
# third party
import numpy as np
import pandas as pd
import torch
import torch.nn.functional as F
import torchvision.transforms as transforms
from pyfaidx import Fasta
from torch.utils.data import DataLoader, Dataset
from torch.utils.data.sampler import SubsetRandomSampler
from tqdm import tqdm
# project
from utils import data_directory
from config import emojis
class DnaSequenceMapper:
"""
DNA sequences translation to one-hot or label encoding.
"""
def __init__(self):
nucleobase_symbols = ["A", "C", "G", "T", "N"]
self.nucleobase_letters = sorted(nucleobase_symbols)
self.num_nucleobase_letters = len(self.nucleobase_letters)
self.nucleobase_letter_to_index = {
nucleobase_letter: index
for index, nucleobase_letter in enumerate(self.nucleobase_letters)
}
self.index_to_nucleobase_letter = {
index: nucleobase_letter
for index, nucleobase_letter in enumerate(self.nucleobase_letters)
}
def sequence_to_one_hot(self, sequence):
sequence_indexes = [
self.nucleobase_letter_to_index[nucleobase_letter]
for nucleobase_letter in sequence
]
one_hot_sequence = F.one_hot(
torch.tensor(sequence_indexes), num_classes=self.num_nucleobase_letters
)
one_hot_sequence = one_hot_sequence.type(torch.float32)
return one_hot_sequence
def sequence_to_label_encoding(self, sequence):
label_encoded_sequence = [
self.nucleobase_letter_to_index[nucleobase] for nucleobase in sequence
]
label_encoded_sequence = torch.tensor(label_encoded_sequence, dtype=torch.int32)
return label_encoded_sequence
def label_encoding_to_sequence(self, label_encoded_sequence):
sequence = [
self.index_to_nucleobase_letter[label] for label in label_encoded_sequence
]
return "".join(sequence)
def label_encoding_to_nucleobase_letter(self, label):
if label in self.index_to_nucleobase_letter.keys():
return self.index_to_nucleobase_letter[label]
else:
return label
class TranslateCoordinatesReverse:
"""Convert (center, span) relative coordinates to (start, end)."""
def __init__(self):
pass
def __call__(self, target):
# [n, 2]
span, center = target[0], target[1]
end = (span + 2 * center) / 2
start = (span - 2 * center) / 2
return (start, end)
class DeNormalizeCoordinates:
"""DeNormalize a sample's repeat annotation coordinates to a relative location
in the sequence, defined as start and end floats between 0 and 1."""
def __init__(self, segment_length):
self.segment_length = segment_length
def __call__(self, coordinates):
return (
int(coordinates[0].item() * self.segment_length),
int(coordinates[1].item() * self.segment_length),
)
class CategoryMapper:
"""
Categorical data mapping class, with methods to translate from the category
text labels to one-hot encoding and vice versa.
"""
def __init__(self, categories):
self.categories = sorted(categories)
self.num_categories = len(self.categories)
self.emojis = emojis[: self.num_categories + 2]
self.label_to_index_dict = {
label: index for index, label in enumerate(categories)
}
self.index_to_label_dict = {
index: label for index, label in enumerate(categories)
}
self.index_to_emoji_dict = {
index: emoji for index, emoji in enumerate(self.emojis)
}
self.label_to_emoji_dict = {
label: self.index_to_emoji_dict[index]
for index, label in enumerate(categories + ["sos", "eos"])
}
def label_to_index(self, label):
"""
Get the class index of label.
"""
return self.label_to_index_dict[label]
def index_to_label(self, index):
"""
Get the label string from its class index.
"""
return self.index_to_label_dict[index]
def label_to_emoji(self, index):
return self.index_to_emoji_dict[index]
def label_to_one_hot(self, label):
"""
Get the one-hot representation of label.
"""
one_hot_label = F.one_hot(
torch.tensor(self.label_to_index_dict[label]),
num_classes=self.num_categories,
)
one_hot_label = one_hot_label.type(torch.float32)
return one_hot_label
def one_hot_to_label(self, one_hot_label):
"""
Get the label string from its one-hot representation.
"""
index = torch.argmax(one_hot_label)
label = self.index_to_label_dict[index]
return label
def print_label_and_emoji(self, logger):
logger.info(self.label_to_emoji_dict)
class RepeatSequenceDataset(Dataset):
def __init__(
self,
fasta_path: Union[str, pathlib.Path],
annotations_path: Union[str, pathlib.Path],
chromosomes: List[str],
dna_sequence_mapper: Type[DnaSequenceMapper],
segment_length: int = 2000,
overlap: int = 500,
transform=None,
):
super().__init__()
self.chromosomes = chromosomes
self.dna_sequence_mapper = dna_sequence_mapper
self.path = [f"{fasta_path}/{chromosome}.fa" for chromosome in self.chromosomes]
self.annotation = [
f"{annotations_path}/hg38_{chromosome}.csv"
for chromosome in self.chromosomes
]
self.transform = transform
self.segment_length = segment_length
self.overlap = overlap
self.repeat_list = self.select_chr()
category = self.get_unique_category()
print(len(category), category)
self.category_mapper = CategoryMapper(category)
def get_unique_category(self):
df_list = [
pd.read_csv(annotation_path, sep="\t", names=["start", "end", "subtype"])
for annotation_path in self.annotation
]
return sorted(pd.concat(df_list)["subtype"].unique().tolist())
def select_chr(self):
repeat_list = []
for fasta_path, chromosome, annotation_path in zip(
self.path, self.chromosomes, self.annotation
):
annotation_path = pathlib.Path(annotation_path)
segments_repeats_pickle_path = (
data_directory / annotation_path.name.replace(".csv", ".pickle")
)
# load the segments_with_repeats list from disk if it has already been generated
if segments_repeats_pickle_path.is_file():
with open(segments_repeats_pickle_path, "rb") as pickle_file:
segments_with_repeats = pickle.load(pickle_file)
else:
genome = Fasta(fasta_path)[chromosome]
annotations = pd.read_csv(
annotation_path, sep="\t", names=["start", "end", "subtype"]
)
segments_with_repeats = self.get_segments_with_repeats(
genome, annotations
)
# save the segments_with_repeats list as a pickle file
with open(segments_repeats_pickle_path, "wb") as pickle_file:
pickle.dump(segments_with_repeats, pickle_file)
repeat_list.extend(segments_with_repeats)
return repeat_list
def get_the_corresponding_repeat(self, anno_df, start, end):
repeats_in_sequence = anno_df.loc[
(
(anno_df["start"] >= start)
& (anno_df["end"] <= end)
& (anno_df["start"] < anno_df["end"])
)
# ----------------------------
# ^seq_start ^seq_end
# -----------------------
# ^rep_start ^rep_end
| (
(anno_df["start"] < end)
& (end < anno_df["end"])
& (anno_df["start"] < anno_df["end"])
)
# ----------------------------
# ^seq_start ^seq_end
# -----------------------
# ^rep_start ^rep_end
| (
(anno_df["start"] < start)
& (start < anno_df["end"])
& (anno_df["start"] < anno_df["end"])
)
]
return repeats_in_sequence
def get_segments_with_repeats(self, genome, annotations):
repeat_list = []
for index in tqdm(range(len(genome) // (self.segment_length - self.overlap))):
genome_index = index * (self.segment_length - self.overlap)
anno_df = annotations
start = genome_index
end = genome_index + self.segment_length
repeats_in_sequence = self.get_the_corresponding_repeat(anno_df, start, end)
if not repeats_in_sequence.empty:
repeats_in_sequence = repeats_in_sequence.apply(
lambda x: [
max(start, x["start"]),
min(end, x["end"]),
x["subtype"],
],
axis=1,
result_type="broadcast",
)
repeat_list.append(
(genome[start:end].seq.upper(), start, repeats_in_sequence)
)
return repeat_list
def forward_strand(self, index):
sequence, start, repeats_in_sequence = self.repeat_list[index]
end = start + self.segment_length
sample = {"sequence": sequence, "start": start}
repeat_ids_series = repeats_in_sequence["subtype"].map(
self.category_mapper.label_to_index
)
repeat_ids_array = np.array(repeat_ids_series, np.int32)
repeat_ids_tensor = torch.tensor(repeat_ids_array, dtype=torch.long)
coordinates = repeats_in_sequence[["start", "end"]]
sample, coordinates = self.transform((sample, coordinates))
target = {
"seq_start": [start for _ in range(coordinates.shape[0])],
"classes": repeat_ids_tensor,
"coordinates": coordinates,
}
return (sample, target)
def seq2seq(self, index):
sequence, start, repeats_in_sequence = self.repeat_list[index]
end = start + self.segment_length
repeat_ids_series = repeats_in_sequence["subtype"].map(
self.category_mapper.label_to_index
)
repeat_ids_array = np.array(repeat_ids_series, np.int32)
repeat_ids_tensor = torch.tensor(repeat_ids_array, dtype=torch.long)
sample = {"sequence": sequence, "start": start}
coordinates = repeats_in_sequence[["start", "end"]]
sample, coordinates = self.transform((sample, coordinates))
sample = sample["sequence"]
target = sample.clone().detach()
for coord, c in zip(coordinates, repeat_ids_array):
start = int(coord[0].item())
end = int(coord[1].item())
repeat_cls = c.item() + self.dna_sequence_mapper.num_nucleobase_letters
target[start:end] = repeat_cls
# <sos> target <eos>
sos = (
self.category_mapper.num_categories
+ self.dna_sequence_mapper.num_nucleobase_letters
)
eos = sos + 1
target = torch.cat(
(
torch.tensor([sos], dtype=torch.long),
target,
torch.tensor([eos], dtype=torch.long),
)
)
return sample, target
def __getitem__(self, index):
return self.seq2seq(index)
def __len__(self):
return len(self.repeat_list)
def collate_fn(self, batch):
sequences = [data[0]["sequence"] for data in batch]
seq_starts = [data[0]["start"] for data in batch]
labels = [data[1] for data in batch]
return torch.stack(sequences), seq_starts, labels
def build_dataloader(configuration):
dna_sequence_mapper = DnaSequenceMapper()
dataset = RepeatSequenceDataset(
fasta_path="./data/genome_assemblies/datasets",
annotations_path="./data/annotations",
chromosomes=configuration.chromosomes,
segment_length=configuration.segment_length,
overlap=configuration.overlap,
dna_sequence_mapper=dna_sequence_mapper,
transform=transforms.Compose(
[
SampleMapEncode(dna_sequence_mapper),
CoordinatesToTensor(),
NormalizeCoordinates(),
TranslateCoordinates(),
]
),
)
configuration.num_classes = dataset.category_mapper.num_categories
configuration.dna_sequence_mapper = dataset.dna_sequence_mapper
configuration.num_nucleobase_letters = (
configuration.dna_sequence_mapper.num_nucleobase_letters
)
dataset_size = len(dataset)
if hasattr(configuration, "dataset_size"):
dataset_size = min(dataset_size, configuration.dataset_size)
indices = list(range(dataset_size))
validation_size = int(configuration.validation_ratio * dataset_size)
test_size = int(configuration.test_ratio * dataset_size)
np.random.seed(configuration.seed)
np.random.shuffle(indices)
val_indices, test_indices, train_indices = (
indices[:validation_size],
indices[validation_size : validation_size + test_size],
indices[validation_size + test_size : dataset_size],
)
train_sampler = SubsetRandomSampler(train_indices)
valid_sampler = SubsetRandomSampler(val_indices)
test_sampler = SubsetRandomSampler(test_indices)
train_loader = torch.utils.data.DataLoader(
dataset,
batch_size=configuration.batch_size,
sampler=train_sampler,
collate_fn=dataset.collate_fn,
)
validation_loader = torch.utils.data.DataLoader(
dataset,
batch_size=configuration.batch_size,
sampler=valid_sampler,
collate_fn=dataset.collate_fn,
)
test_loader = torch.utils.data.DataLoader(
dataset,
batch_size=configuration.batch_size,
sampler=test_sampler,
collate_fn=dataset.collate_fn,
)
return train_loader, validation_loader, test_loader
def build_seq2seq_dataset(configuration):
dna_sequence_mapper = DnaSequenceMapper()
dataset = RepeatSequenceDataset(
fasta_path="./data/genome_assemblies/datasets",
annotations_path="./data/annotations",
chromosomes=configuration.chromosomes,
segment_length=configuration.segment_length,
overlap=configuration.overlap,
dna_sequence_mapper=dna_sequence_mapper,
transform=transforms.Compose(
[
SampleMapEncode(dna_sequence_mapper),
CoordinatesToTensor(),
ZeroStartCoordinates(),
]
),
)
configuration.num_classes = dataset.category_mapper.num_categories
configuration.dna_sequence_mapper = dataset.dna_sequence_mapper
configuration.category_mapper = dataset.category_mapper
configuration.num_nucleobase_letters = (
configuration.dna_sequence_mapper.num_nucleobase_letters
)
dataset_size = len(dataset)
if hasattr(configuration, "dataset_size"):
dataset_size = min(dataset_size, configuration.dataset_size)
indices = list(range(dataset_size))
validation_size = int(configuration.validation_ratio * dataset_size)
test_size = int(configuration.test_ratio * dataset_size)
np.random.seed(configuration.seed)
np.random.shuffle(indices)
val_indices, test_indices, train_indices = (
indices[:validation_size],
indices[validation_size : validation_size + test_size],
indices[validation_size + test_size : dataset_size],
)
train_sampler = SubsetRandomSampler(train_indices)
valid_sampler = SubsetRandomSampler(val_indices)
test_sampler = SubsetRandomSampler(test_indices)
train_loader = torch.utils.data.DataLoader(
dataset,
batch_size=configuration.batch_size,
sampler=train_sampler,
)
validation_loader = torch.utils.data.DataLoader(
dataset,
batch_size=configuration.batch_size,
sampler=valid_sampler,
)
test_loader = torch.utils.data.DataLoader(
dataset,
batch_size=configuration.batch_size,
sampler=test_sampler,
)
return train_loader, validation_loader, test_loader
class SampleMapEncode:
def __init__(self, mapper):
self.sequence_mapper = mapper
def __call__(self, item):
# [n, 2]
sample, target_df = item
sample["sequence"] = self.sequence_mapper.sequence_to_label_encoding(
sample["sequence"]
)
return (sample, target_df)
class CoordinatesToTensor:
def __init__(self):
pass
def __call__(self, item):
# [n, 2]
sample, target_df = item
target_array = np.array(target_df, dtype=np.float32)
target_tensor = torch.tensor(target_array, dtype=torch.float32)
return (sample, target_tensor)
class NormalizeCoordinates:
"""Normalize a sample's repeat annotation coordinates to a relative location
in the sequence, defined as start and end floats between 0 and 1."""
def __init__(self):
pass
def __call__(self, item):
sample, coordinates = item
length = len(sample["sequence"])
start_coordinate = sample["start"]
coordinates[:, :] -= start_coordinate
coordinates[:, :] /= length
return (sample, coordinates)
class ZeroStartCoordinates:
"""Normalize a sample's repeat annotation coordinates to a relative location
in the sequence, defined as start and end floats between 0 and 1."""
def __init__(self):
pass
def __call__(self, item):
sample, coordinates = item
start_coordinate = sample["start"]
coordinates[:, :] -= start_coordinate
return (sample, coordinates)
class TranslateCoordinates:
"""Convert (start, end) relative coordinates to (center, span)."""
def __init__(self):
pass
def __call__(self, item):
# [n, 2]
sample, target = item
center = (target[:, 1] + target[:, 0]) / 2 # [n]
span = (target[:, 1]) - target[:, 0] # [n]
return (sample, torch.stack((center, span), axis=1))
if __name__ == "__main__":
dna_sequence_mapper = DnaSequenceMapper()
dataset = RepeatSequenceDataset(
fasta_path="./data/genome_assemblies/datasets",
annotations_path="./data/annotations",
chromosomes=["chrX"],
dna_sequence_mapper=dna_sequence_mapper,
transform=transforms.Compose(
[
SampleMapEncode(dna_sequence_mapper),
CoordinatesToTensor(),
NormalizeCoordinates(),
TranslateCoordinates(),
]
),
)
print(dataset[0])
# repeat_dict = dict()
# for repeat in dataset:
# key = repeat[1]["classes"].nelement()
# repeat_dict[key] = repeat_dict.get(key, 0) + 1
# print(repeat_dict)
# index = 10100
# index = 0
# index = 165_970
# import random
# print(dataset[0])
# while True:
# index = random.randint(1, 5000)
# item = dataset[index]
# print(f"{index=}, {item=}")
# annotation = item[1]
# if annotation["classes"].nelement() > 0:
# break
# # dataloader, _ = build_dataloader()
# for data in dataloader:
# print(data)