-
Notifications
You must be signed in to change notification settings - Fork 7
/
utils_data.py
43 lines (33 loc) · 1.63 KB
/
utils_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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jul 8 16:05:54 2019
@author: Chonghua Xue (Kolachalama's Lab, BU)
"""
from torch.utils.data import Dataset
# true if gapped else false
vocab_o = { True: ['-'] + ['A', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'Y'],
False: ['A', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'Y']}
aa2id_o = { True: dict(zip(vocab_o[True], list(range(len(vocab_o[True]))))),
False: dict(zip(vocab_o[False], list(range(len(vocab_o[False])))))}
id2aa_o = { True: dict(zip(list(range(len(vocab_o[True]))), vocab_o[True])),
False: dict(zip(list(range(len(vocab_o[False]))), vocab_o[False]))}
vocab_i = { True: vocab_o[True] + ['<SOS>', '<EOS>'],
False: vocab_o[False] + ['<SOS>', '<EOS>']}
aa2id_i = { True: dict(zip(vocab_i[True], list(range(len(vocab_i[True]))))),
False: dict(zip(vocab_i[False], list(range(len(vocab_i[False])))))}
id2aa_i = { True: dict(zip(list(range(len(vocab_i[True]))), vocab_i[True])),
False: dict(zip(list(range(len(vocab_i[False]))), vocab_i[False]))}
class ProteinSeqDataset(Dataset):
def __init__(self, fn, gapped=True):
# load data
with open(fn, 'r') as f:
self.data = [l.strip('\n') for l in f]
# char to id
self.data = [[aa2id_i[gapped][c] for c in r] for r in self.data]
def __len__(self):
return len(self.data)
def __getitem__(self, idx):
return self.data[idx]
def collate_fn(batch):
return batch, [x for seq in batch for x in seq]