File size: 9,380 Bytes
65bd8af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pdb

import numpy as np
import torch
import torch.nn as nn
from .layers import *
from .modules import *
import pdb
from transformers import EsmModel, EsmTokenizer

def to_var(x):
    if torch.cuda.is_available():
        x = x.cuda()
    return x


class RepeatedModule3(nn.Module):
    def __init__(self, n_layers, d_model, d_hidden,
                 n_head, d_k, d_v, d_inner, dropout=0.1):
        super().__init__()

        self.linear1 = nn.Linear(1280, d_model)
        self.linear2 = nn.Linear(1280, d_model)
        self.sequence_embedding = nn.Embedding(20, d_model)
        self.d_model = d_model

        self.reciprocal_layer_stack = nn.ModuleList([
            ReciprocalLayerwithCNN(d_model, d_inner, d_hidden, n_head, d_k, d_v)
            for _ in range(n_layers)])

        self.dropout = nn.Dropout(dropout)
        self.dropout_2 = nn.Dropout(dropout)

    def forward(self, peptide_sequence, protein_sequence):
        sequence_attention_list = []

        prot_attention_list = []

        prot_seq_attention_list = []

        seq_prot_attention_list = []

        sequence_enc = self.dropout(self.linear1(peptide_sequence))

        prot_enc = self.dropout_2(self.linear2(protein_sequence))

        for reciprocal_layer in self.reciprocal_layer_stack:
            prot_enc, sequence_enc, prot_attention, sequence_attention, prot_seq_attention, seq_prot_attention = \
                reciprocal_layer(sequence_enc, prot_enc)

            sequence_attention_list.append(sequence_attention)

            prot_attention_list.append(prot_attention)

            prot_seq_attention_list.append(prot_seq_attention)

            seq_prot_attention_list.append(seq_prot_attention)

        return prot_enc, sequence_enc, sequence_attention_list, prot_attention_list, \
            seq_prot_attention_list, seq_prot_attention_list


class RepeatedModule2(nn.Module):
    def __init__(self, n_layers, d_model,
                 n_head, d_k, d_v, d_inner, dropout=0.1):
        super().__init__()

        self.linear1 = nn.Linear(1280, d_model)
        self.linear2 = nn.Linear(1280, d_model)
        self.sequence_embedding = nn.Embedding(20, d_model)
        self.d_model = d_model

        self.reciprocal_layer_stack = nn.ModuleList([
            ReciprocalLayer(d_model, d_inner, n_head, d_k, d_v)
            for _ in range(n_layers)])

        self.dropout = nn.Dropout(dropout)
        self.dropout_2 = nn.Dropout(dropout)

    def forward(self, peptide_sequence, protein_sequence):
        sequence_attention_list = []

        prot_attention_list = []

        prot_seq_attention_list = []

        seq_prot_attention_list = []

        sequence_enc = self.dropout(self.linear1(peptide_sequence))

        prot_enc = self.dropout_2(self.linear2(protein_sequence))

        for reciprocal_layer in self.reciprocal_layer_stack:
            prot_enc, sequence_enc, prot_attention, sequence_attention, prot_seq_attention, seq_prot_attention = \
                reciprocal_layer(sequence_enc, prot_enc)

            sequence_attention_list.append(sequence_attention)

            prot_attention_list.append(prot_attention)

            prot_seq_attention_list.append(prot_seq_attention)

            seq_prot_attention_list.append(seq_prot_attention)

        return prot_enc, sequence_enc, sequence_attention_list, prot_attention_list, \
            seq_prot_attention_list, seq_prot_attention_list


class RepeatedModule(nn.Module):
    
    def __init__(self, n_layers, d_model,
                 n_head, d_k, d_v, d_inner, dropout=0.1):
        
        super().__init__()
        
        self.linear = nn.Linear(1024, d_model)
        self.sequence_embedding = nn.Embedding(20, d_model)
        self.d_model = d_model 
        
        self.reciprocal_layer_stack = nn.ModuleList([
                ReciprocalLayer(d_model,  d_inner,  n_head, d_k, d_v) 
                for _ in range(n_layers)])
    
        self.dropout = nn.Dropout(dropout)
        self.dropout_2 = nn.Dropout(dropout)

       
    
    def _positional_embedding(self, batches, number):
        
        result = torch.exp(torch.arange(0, self.d_model,2,dtype=torch.float32)*-1*(np.log(10000)/self.d_model))
        
        numbers = torch.arange(0, number, dtype=torch.float32)
        
        numbers = numbers.unsqueeze(0)
        
        numbers = numbers.unsqueeze(2)
       
        result = numbers*result
        
        result = torch.cat((torch.sin(result), torch.cos(result)),2)
       
        return result
    
    def forward(self, peptide_sequence, protein_sequence):
        
        
        sequence_attention_list = []
        
        prot_attention_list = []
        
        prot_seq_attention_list = []
        
        seq_prot_attention_list = []
        
        sequence_enc = self.sequence_embedding(peptide_sequence)
        
        sequence_enc += to_var(self._positional_embedding(peptide_sequence.shape[0],
                                                           peptide_sequence.shape[1]))
        sequence_enc = self.dropout(sequence_enc)





        prot_enc = self.dropout_2(self.linear(protein_sequence))
        
    
        

        for reciprocal_layer in self.reciprocal_layer_stack:
            
            prot_enc, sequence_enc, prot_attention, sequence_attention, prot_seq_attention, seq_prot_attention =\
                reciprocal_layer(sequence_enc, prot_enc)
            
            sequence_attention_list.append(sequence_attention)
            
            prot_attention_list.append(prot_attention)
            
            prot_seq_attention_list.append(prot_seq_attention)
            
            seq_prot_attention_list.append(seq_prot_attention)
            
        
        
        return prot_enc, sequence_enc, sequence_attention_list, prot_attention_list,\
            seq_prot_attention_list, seq_prot_attention_list


class FullModel(nn.Module):

    def __init__(self, n_layers, d_model, n_head,
                 d_k, d_v, d_inner, return_attention=False, dropout=0.2):
        super().__init__()

        self.esm_model = EsmModel.from_pretrained("facebook/esm2_t33_650M_UR50D")

        # freeze all the esm_model parameters
        for param in self.esm_model.parameters():
            param.requires_grad = False

        self.repeated_module = RepeatedModule2(n_layers, d_model,
                                               n_head, d_k, d_v, d_inner, dropout=dropout)

        self.final_attention_layer = MultiHeadAttentionSequence(n_head, d_model,
                                                                d_k, d_v, dropout=dropout)

        self.final_ffn = FFN(d_model, d_inner, dropout=dropout)

        self.output_projection_prot = nn.Linear(d_model, 1)
        self.sigmoid = nn.Sigmoid()

        self.return_attention = return_attention

    def forward(self, binder_tokens, target_tokens):

        with torch.no_grad():
            peptide_sequence = self.esm_model(**binder_tokens).last_hidden_state
            protein_sequence = self.esm_model(**target_tokens).last_hidden_state

        # pdb.set_trace()

        prot_enc, sequence_enc, sequence_attention_list, prot_attention_list, \
            seq_prot_attention_list, seq_prot_attention_list = self.repeated_module(peptide_sequence,
                                                                                    protein_sequence)

        prot_enc, final_prot_seq_attention = self.final_attention_layer(prot_enc, sequence_enc, sequence_enc)

        # pdb.set_trace()

        prot_enc = self.final_ffn(prot_enc)

        prot_enc = self.sigmoid(self.output_projection_prot(prot_enc))

        return prot_enc



class Original_FullModel(nn.Module):
    
    def __init__(self, n_layers, d_model, n_head,
                 d_k, d_v, d_inner, return_attention=False, dropout=0.2):
        
        super().__init__()
        self.repeated_module = RepeatedModule(n_layers, d_model,
                               n_head, d_k, d_v, d_inner, dropout=dropout)
        
        self.final_attention_layer = MultiHeadAttentionSequence(n_head, d_model,
                                                                d_k, d_v, dropout=dropout)
        
        self.final_ffn = FFN(d_model, d_inner, dropout=dropout) 
        self.output_projection_prot = nn.Linear(d_model, 2)
        
        
        
        self.softmax_prot =nn.LogSoftmax(dim=-1)
   
                
        self.return_attention = return_attention
        
    def forward(self, peptide_sequence, protein_sequence):

        prot_enc, sequence_enc, sequence_attention_list, prot_attention_list,\
            seq_prot_attention_list, seq_prot_attention_list = self.repeated_module(peptide_sequence,
                                                                                    protein_sequence)
            
        
        
        prot_enc, final_prot_seq_attention  = self.final_attention_layer(prot_enc, sequence_enc, sequence_enc)
        
        prot_enc = self.final_ffn(prot_enc)

        prot_enc = self.softmax_prot(self.output_projection_prot(prot_enc))
        
        
        
        
        
        if not self.return_attention:
            return prot_enc
        else:
            return prot_enc, sequence_attention_list, prot_attention_list,\
            seq_prot_attention_list, seq_prot_attention_list