File size: 8,882 Bytes
3ac1d94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch_scatter import scatter

from .prop_egnn import EnEquiEncoder
from ..common import compose_context_prop, ShiftedSoftplus


def get_encoder(config):
    if config.name == 'egnn' or config.name == 'egnn_enc':
        net = EnEquiEncoder(
            num_layers=config.num_layers,
            edge_feat_dim=config.edge_dim,
            hidden_dim=config.hidden_dim,
            num_r_gaussian=config.num_r_gaussian,
            act_fn=config.act_fn,
            norm=config.norm,
            update_x=False,
            k=config.knn,
            cutoff=config.cutoff,
        )
    else:
        raise ValueError(config.name)
    return net


class PropPredNet(nn.Module):
    def __init__(self, config, protein_atom_feature_dim, ligand_atom_feature_dim, output_dim=3):
        super(PropPredNet, self).__init__()
        self.config = config
        self.hidden_dim = config.hidden_channels
        self.output_dim = output_dim
        self.protein_atom_emb = nn.Linear(protein_atom_feature_dim, self.hidden_dim)
        self.ligand_atom_emb = nn.Linear(ligand_atom_feature_dim, self.hidden_dim)

        # self.mean = target_mean
        # self.std = target_std
        # self.register_buffer('target_mean', target_mean)
        # self.register_buffer('target_std', target_std)
        self.encoder = get_encoder(config.encoder)
        self.out_block = nn.Sequential(
            nn.Linear(self.hidden_dim, self.hidden_dim),
            ShiftedSoftplus(),
            nn.Linear(self.hidden_dim, output_dim),
        )

    def forward(self, protein_pos, protein_atom_feature, ligand_pos, ligand_atom_feature, batch_protein, batch_ligand,
                output_kind):
        h_protein = self.protein_atom_emb(protein_atom_feature)
        h_ligand = self.ligand_atom_emb(ligand_atom_feature)

        h_ctx, pos_ctx, batch_ctx = compose_context_prop(
            h_protein=h_protein,
            h_ligand=h_ligand,
            pos_protein=protein_pos,
            pos_ligand=ligand_pos,
            batch_protein=batch_protein,
            batch_ligand=batch_ligand,
        )

        h_ctx = self.encoder(
            node_attr=h_ctx,
            pos=pos_ctx,
            batch=batch_ctx,
        )  # (N_p+N_l, H)

        # Aggregate messages
        pre_out = scatter(h_ctx, index=batch_ctx, dim=0, reduce='sum')  # (N, F)
        output = self.out_block(pre_out)  # (N, C)
        if output_kind is not None:
            output_mask = F.one_hot(output_kind - 1, self.output_dim)
            output = torch.sum(output * output_mask, dim=-1, keepdim=True)
        return output

    def get_loss(self, batch, pos_noise_std, return_pred=False):
        protein_noise = torch.randn_like(batch.protein_pos) * pos_noise_std
        ligand_noise = torch.randn_like(batch.ligand_pos) * pos_noise_std
        pred = self(
            protein_pos=batch.protein_pos + protein_noise,
            protein_atom_feature=batch.protein_atom_feature.float(),
            ligand_pos=batch.ligand_pos + ligand_noise,
            ligand_atom_feature=batch.ligand_atom_feature_full.float(),
            batch_protein=batch.protein_element_batch,
            batch_ligand=batch.ligand_element_batch,
            output_kind=batch.kind,
            # output_kind=None
        )
        # pred = pred * y_std + y_mean
        loss_func = nn.MSELoss()
        loss = loss_func(pred.view(-1), batch.y)
        if return_pred:
            return loss, pred
        else:
            return loss


class PropPredNetEnc(nn.Module):
    def __init__(self, config, protein_atom_feature_dim, ligand_atom_feature_dim,
                 enc_ligand_dim, enc_node_dim, enc_graph_dim, enc_feature_type=None, output_dim=1):
        super(PropPredNetEnc, self).__init__()
        self.config = config
        self.hidden_dim = config.hidden_channels
        self.output_dim = output_dim
        self.enc_ligand_dim = enc_ligand_dim
        self.enc_node_dim = enc_node_dim
        self.enc_graph_dim = enc_graph_dim
        self.enc_feature_type = enc_feature_type

        self.protein_atom_emb = nn.Linear(protein_atom_feature_dim, self.hidden_dim)
        self.ligand_atom_emb = nn.Linear(ligand_atom_feature_dim + enc_ligand_dim, self.hidden_dim)
        # self.mean = target_mean
        # self.std = target_std
        # self.register_buffer('target_mean', target_mean)
        # self.register_buffer('target_std', target_std)
        self.encoder = get_encoder(config.encoder)
        if self.enc_node_dim > 0:
            self.enc_node_layer = nn.Sequential(
                nn.Linear(self.hidden_dim + self.enc_node_dim, self.hidden_dim),
                nn.ReLU(),
                nn.Linear(self.hidden_dim, self.hidden_dim),
            )

        self.out_block = nn.Sequential(
            nn.Linear(self.hidden_dim + self.enc_graph_dim, self.hidden_dim),
            ShiftedSoftplus(),
            nn.Linear(self.hidden_dim, output_dim),
        )

    def forward(self, protein_pos, protein_atom_feature, ligand_pos, ligand_atom_feature, batch_protein, batch_ligand,
                output_kind, enc_ligand_feature, enc_node_feature, enc_graph_feature):
        h_protein = self.protein_atom_emb(protein_atom_feature)
        if enc_ligand_feature is not None:
            ligand_atom_feature = torch.cat([ligand_atom_feature, enc_ligand_feature], dim=-1)
        h_ligand = self.ligand_atom_emb(ligand_atom_feature)

        h_ctx, pos_ctx, batch_ctx = compose_context_prop(
            h_protein=h_protein,
            h_ligand=h_ligand,
            pos_protein=protein_pos,
            pos_ligand=ligand_pos,
            batch_protein=batch_protein,
            batch_ligand=batch_ligand,
        )

        h_ctx = self.encoder(
            node_attr=h_ctx,
            pos=pos_ctx,
            batch=batch_ctx,
        )  # (N_p+N_l, H)

        if enc_node_feature is not None:
            h_ctx = torch.cat([h_ctx, enc_node_feature], dim=-1)
            h_ctx = self.enc_node_layer(h_ctx)

        # Aggregate messages
        pre_out = scatter(h_ctx, index=batch_ctx, dim=0, reduce='sum')  # (N, F)
        if enc_graph_feature is not None:
            pre_out = torch.cat([pre_out, enc_graph_feature], dim=-1)

        output = self.out_block(pre_out)  # (N, C)
        if output_kind is not None:
            output_mask = F.one_hot(output_kind - 1, self.output_dim)
            output = torch.sum(output * output_mask, dim=-1, keepdim=True)
        return output

    def get_loss(self, batch, pos_noise_std, return_pred=False):
        protein_noise = torch.randn_like(batch.protein_pos) * pos_noise_std
        ligand_noise = torch.randn_like(batch.ligand_pos) * pos_noise_std

        # add features
        enc_ligand_feature, enc_node_feature, enc_graph_feature = None, None, None
        if self.enc_feature_type == 'nll_all':
            enc_graph_feature = batch.nll_all  # [num_graphs, 22]
        elif self.enc_feature_type == 'nll':
            enc_graph_feature = batch.nll  # [num_graphs, 20]
        elif self.enc_feature_type == 'final_h':
            enc_node_feature = batch.final_h  # [num_pl_atoms, 128]
        elif self.enc_feature_type == 'pred_ligand_v':
            enc_ligand_feature = batch.pred_ligand_v  # [num_l_atoms, 13]
        elif self.enc_feature_type == 'pred_v_entropy_pre':
            enc_ligand_feature = batch.pred_v_entropy   # [num_l_atoms, 1]
        elif self.enc_feature_type == 'pred_v_entropy_post':
            enc_graph_feature = scatter(batch.pred_v_entropy, index=batch.ligand_element_batch, dim=0, reduce='sum')   # [num_graphs, 1]
        elif self.enc_feature_type == 'full':
            enc_graph_feature = torch.cat(
                [batch.nll_all, scatter(batch.pred_v_entropy, index=batch.ligand_element_batch, dim=0, reduce='sum')], dim=-1)
            enc_node_feature = batch.final_h
            enc_ligand_feature = torch.cat([batch.pred_ligand_v, batch.pred_v_entropy], -1)
        else:
            raise NotImplementedError

        pred = self(
            protein_pos=batch.protein_pos + protein_noise,
            protein_atom_feature=batch.protein_atom_feature.float(),
            ligand_pos=batch.ligand_pos + ligand_noise,
            ligand_atom_feature=batch.ligand_atom_feature_full.float(),
            batch_protein=batch.protein_element_batch,
            batch_ligand=batch.ligand_element_batch,
            output_kind=batch.kind,
            # output_kind=None,
            enc_ligand_feature=enc_ligand_feature,
            enc_node_feature=enc_node_feature,
            enc_graph_feature=enc_graph_feature
        )
        # pred = pred * y_std + y_mean
        loss_func = nn.MSELoss()
        loss = loss_func(pred.view(-1), batch.y)
        if return_pred:
            return loss, pred
        else:
            return loss