File size: 13,009 Bytes
29134bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import numpy as np
import random
import math

from sklearn.metrics import *

import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import Dataset
import pickle

    
def word2idx(word, words):
    if word in words.keys():
        return int(words[word])
    
    return 0

def pad_seq(dataset, max_len):
    output = []
    for seq in dataset:
        pad = np.zeros(max_len)
        pad[:len(seq)] = seq
        output.append(pad)
        
    return np.array(output)

def rna2vec(seqset):
    letters = ['A', 'C', 'G', 'U', 'N']

    words = np.array([letters[i] + letters[j] + letters[k]
                     for i in range(len(letters))
                     for j in range(len(letters))
                     for k in range(len(letters))])

    words = {word: i + 1 for i, word in enumerate(words)}

    outputs = []

    for seq in seqset:
        output = []

        converted_seq = dna2rna(seq)  # Assuming dna2rna function converts DNA to RNA

        for i in range(0, len(converted_seq) - 2):  # -2 so we can index 3 letters
            output.append(word2idx(converted_seq[i:i + 3], words))

        if sum(output) != 0:
            # pad individual sequence
            padded_seq = np.pad(output, (0, 275 - len(output)), 'constant', constant_values=0)
            outputs.append(padded_seq)

    return np.array(outputs)

def rna2vec_pretraining(seqset):
    letters = ['A','C','G','U','N']
    
    words = np.array([letters[i]+letters[j]+letters[k]
                     for i in range(len(letters)) 
                     for j in range(len(letters)) 
                     for k in range(len(letters))])
    words = np.unique(words) 
    # print("words:", len(words))
    words = {word:i+1 for i, word in enumerate(words)}
    
    ss = ['S','H','M','I','B','X','E']
    words_ss = np.array([i + j + k for i in ss for j in ss for k in ss])
    words_ss = np.unique(words_ss)
    # print("words_ss:", len(words_ss))
    words_ss = {word:i+1 for i, word in enumerate(words_ss)}
    
    outputs = []
    outputs_ss = []
    for seq, ss in seqset:
        output = [] 
        output_ss = []
        conveted_seq = dna2rna(seq)

        for i in range(0, len(conveted_seq)-1):
            output.append(word2idx(conveted_seq[i:i+3], words))
            output_ss.append(word2idx(ss[i:i+3], words_ss))

            if len(output) == 275:
                outputs.append(np.array(output))
                outputs_ss.append(np.array(output_ss))
                
                output = []
                output_ss = []
                
        # We'll handle the padding of individual sequences before appending to the outputs list
        if len(output) > 0:
            padded_output = np.pad(output, (0, 275 - len(output)), 'constant', constant_values=0)
            outputs.append(padded_output)

            padded_output_ss = np.pad(output_ss, (0, 275 - len(output_ss)), 'constant', constant_values=0)
            outputs_ss.append(padded_output_ss)

    return np.array(outputs), np.array(outputs_ss)

def seq2vec(seqset, max_len, n_vocabs, n_target_vocabs, words, words_ss):
    word_max_len= 3
    
    outputs = []
    outputs_ss = []
    for seq, ss in seqset:
        output = []
        output_ss = [] 
        i = 0
        while i < len(seq):
            flag=False
            for j in range(word_max_len, 0, -1):
                if i+j <=len(seq):
                    if word2idx(seq[i:i+j], words)!= 0:
                        flag = True
                        output.append(word2idx(seq[i:i+j], words))
                        output_ss.append(word2idx(ss[i:i+j], words_ss))

                        if len(output)==max_len:
                            outputs.append(np.array(output))
                            outputs_ss.append(np.array(output_ss))
                            output = [] 
                            output_ss = []
                        i+=j
                        break 
            if flag==False: 
                i+=1
        if len(output) != 0: 
            outputs.append(np.array(output))
            outputs_ss.append(np.array(output_ss))
        
    return pad_seq(outputs, max_len), pad_seq(outputs_ss, max_len)

def tokenize_sequences(seqset, max_len, n_vocabs, words, word_max_len=3):
    outputs = []
    for seq in seqset:
        output = []
        i = 0
        while i < len(seq): 
            flag=False
            for j in range(word_max_len, 0, -1): 
                if i+j <=len(seq): 
                    if word2idx(seq[i:i+j], words)!= 0: 
                        flag = True 
                        output.append(word2idx(seq[i:i+j], words)) 
                        if len(output)==max_len: 
                            outputs.append(np.array(output))
                            output = [] 
                        i+=j 
                        break 
            if flag==False:
                i+=1
                
        if len(output) != 0: 
            outputs.append(np.array(output))
        
    return pad_seq(outputs, max_len)

def str2bool(seq):
    out = []
    for s in seq:
        if s == "positive": 
            out.append(1)
        elif s == "negative": 
            out.append(0)
            
    return np.array(out)

def dna2rna(seq):
    mapping = {'A':'A','C':'C','G':'G', 'U':'U', 'T':'U'}
    result = ""
    for s in seq:
        if s in mapping.keys():
            result += mapping[s]
        else:
            result += 'N'
            
    return result

class Custom_Dataset(Dataset):
    def __init__(self, x, y):
        super(Dataset, self).__init__()
        
        self.x = np.array(x, dtype=np.int64)
        self.y = np.array(y, dtype=np.int64)
        self.len = len(self.x)

    def __len__(self):
        return self.len

    def __getitem__(self, index):
        return torch.tensor(self.x[index], dtype=torch.int64), torch.tensor(self.y[index], dtype=torch.int64)
    
class Masked_Dataset(Dataset):
    def __init__(self, x, y, max_len, masked_rate, mask_idx, isrna=False):
        self.x = np.array(x)
        self.y = np.array(y)
        self.box = np.array([i for i in range(max_len)])
        self.masked_rate = masked_rate
        self.mask_idx = mask_idx
        self.isrna = isrna
        
        if len(self.x) != len(self.y):
            raise
        else:
            self.len = len(self.x)
        
    def __len__(self):
        return self.len

    def __getitem__(self, index):
        x = torch.tensor(self.x[index], dtype=torch.int64)
        y = torch.tensor(self.y[index], dtype=torch.int64)
       
        masks = []
        x_masked = x.clone().detach()
        y_masked = x.clone().detach()
        
        seq_len = torch.sum(x_masked > 0)
        mask = random.sample(self.box[x_masked > 0].tolist(), int(seq_len * self.masked_rate))
        masks.append(mask)
        no_mask = [b for b in self.box[x_masked > 0].tolist() if b not in mask]
        
        mask = random.sample(mask, int(len(mask) * 0.8))
        x_masked[mask] = self.mask_idx #msk
        
        if self.isrna==True:
            x_masked[[m+1 for m in mask if m < 274]] = self.mask_idx
            x_masked[[m-1 for m in mask if m >0]] = self.mask_idx
        
        y_masked[no_mask] = 0
        
        return x_masked, y_masked, x, y
    
class API_Dataset(Dataset):
    def __init__(self, apta, esm_prot, y):
        super(Dataset, self).__init__()
        
        self.apta = np.array(apta, dtype=np.int64)
        self.esm_prot = np.array(esm_prot, dtype=np.int64)
        self.y = np.array(y, dtype=np.int64)
        self.len = len(self.apta)

    def __len__(self):
        return self.len

    def __getitem__(self, index):
        return torch.tensor(self.apta[index], dtype=torch.int64), torch.tensor(self.esm_prot[index], dtype=torch.int64), torch.tensor(self.y[index], dtype=torch.int64)

def find_opt_threshold(target, pred):
    result = 0
    best = 0
    
    for i in range(0, 1000):
        pred_threshold = np.where(pred > i/1000, 1, 0)
        now = f1_score(target, pred_threshold)
        if now > best:
            result = i/1000
            best = now
            
    return result

def argument_seqset(seqset):
    arg_seqset = []
    for s, ss in seqset:
        arg_seqset.append([s, ss]) 

        arg_seqset.append([s[::-1], ss[::-1]])

    return arg_seqset

def augment_apis(apta, prot, ys):
    aug_apta = []
    aug_prot = []
    aug_y = []
    for a, p, y in zip(apta, prot, ys):
        aug_apta.append(a) 
        aug_prot.append(p)
        aug_y.append(y)

        aug_apta.append(a[::-1]) 
        aug_prot.append(p)
        aug_y.append(y)

    return np.array(aug_apta), np.array(aug_prot), np.array(aug_y)


def load_aptatrans_prot_index(filepath):
    with open(filepath, 'rb') as fr:
        words = pickle.load(fr)
        words = words[words["freq"]>words.freq.mean()].seq.values
        return {word:i+1 for i, word in enumerate(words)}


def load_data_source(filepath):
    with open(filepath,"rb") as fr:
        dataset = pickle.load(fr)
        dataset_train = np.array(dataset[dataset["dataset"]=="training dataset"])
        dataset_test = np.array(dataset[dataset["dataset"]=="test dataset"])
        dataset_bench = np.array(dataset[dataset['dataset']=='benchmark dataset'])

        return dataset_train, dataset_test, dataset_bench


def get_dataset(filepath, prot_max_len, n_prot_vocabs, prot_words):
    dataset_train, dataset_test, dataset_bench = load_data_source(filepath)

    
    arg_apta, arg_prot, arg_y = augment_apis(dataset_train[:, 0], dataset_train[:, 1], dataset_train[:, 2])
    datasets_train = [rna2vec(arg_apta), tokenize_sequences(arg_prot, prot_max_len, n_prot_vocabs, prot_words), str2bool(arg_y)]
    datasets_test = [rna2vec(dataset_test[:, 0]), tokenize_sequences(dataset_test[:, 1], prot_max_len, n_prot_vocabs, prot_words), str2bool(dataset_test[:, 2])]
    datasets_bench = [rna2vec(dataset_bench[:, 0]), tokenize_sequences(dataset_bench[:, 1], prot_max_len, n_prot_vocabs, prot_words), str2bool(dataset_bench[:, 2])]

    return datasets_train, datasets_test, datasets_bench


def get_esm_dataset(filepath, batch_converter):
    dataset_train, dataset_test, dataset_bench = load_data_source(filepath)

    arg_apta, arg_prot, arg_y = augment_apis(dataset_train[:, 0], dataset_train[:, 1], dataset_train[:, 2])
    # arg_prot is a np.array of strings (4640,) -> convert this to np.array of size (2x4640) where first row is a label

    train_inputs = [(i, j) for i, j in zip(arg_y, arg_prot)]
    _, _, prot_tokens = batch_converter(train_inputs)
    datasets_train = [rna2vec(arg_apta), prot_tokens, str2bool(arg_y)]

    test_inputs = [(i, j) for i, j in enumerate(dataset_test[:, 1])]
    _, _, test_prot_tokens = batch_converter(test_inputs)
    datasets_test = [rna2vec(dataset_test[:, 0]), test_prot_tokens, str2bool(dataset_test[:, 2])]

    bench_inputs = [(i, j) for i, j in enumerate(dataset_bench[:, 1])]
    _, _, bench_prot_tokens = batch_converter(bench_inputs)
    datasets_bench = [rna2vec(dataset_bench[:, 0]), bench_prot_tokens, str2bool(dataset_bench[:, 2])]

    return datasets_train, datasets_test, datasets_bench


def get_apta_esm_dataset(filepath, prot_max_len, n_prot_vocabs, prot_words, batch_converter):
    """Deprecated."""
    dataset_train, dataset_test = load_data_source(filepath)

    arg_apta, arg_prot, arg_y_train = augment_apis(dataset_train[:, 1], dataset_train[:, 2], dataset_train[:, 0])
    # arg_prot is a np.array of strings (4640,) -> convert this to np.array of size (2x4640) where first row is a label
    train_inputs = [(i, j) for i, j in zip(arg_y_train, arg_prot)]
    _, _, prot_tokens = batch_converter(train_inputs)
    datasets_train = [rna2vec(arg_apta), tokenize_sequences(arg_prot, prot_max_len, n_prot_vocabs, prot_words), prot_tokens, str2bool(arg_y_train)]

    # arg_apta, arg_prot, arg_y_test = augment_apis(dataset_test[:, 1], dataset_test[:, 2], dataset_test[:, 0])
    test_inputs = [(i, j) for i, j in zip(dataset_test[:, 0], dataset_test[:, 2])]
    _, _, test_prot_tokens = batch_converter(test_inputs)
    datasets_test = [rna2vec(dataset_test[:, 1]), tokenize_sequences(dataset_test[:, 2], prot_max_len, n_prot_vocabs, prot_words), test_prot_tokens, str2bool(dataset_test[:, 0])]
    return datasets_train, datasets_test


def get_scores(target, pred):
    threshold = find_opt_threshold(target, pred)
    pred_threshold = np.where(pred > threshold, 1, 0)
    acc = accuracy_score(target, pred_threshold)
    roc_auc = roc_auc_score(target, pred)
    mcc = matthews_corrcoef(target, pred_threshold)
    f1 = f1_score(target, pred_threshold)
    pr_auc = average_precision_score(target, pred)
    cls_report = classification_report(target, pred_threshold)
    scores = {
        'threshold': threshold,
        'acc': acc,
        'roc_auc': roc_auc,
        'mcc': mcc, 
        'f1': f1, 
        'pr_auc': pr_auc,
        'cls_report': cls_report
    }
    return scores