@@ -210,42 +210,14 @@ def virtual_screening(X_repurpose, target, model, drug_names = None, target_name
210210
211211 return y_pred
212212
213- ## x is a list, len(x)=batch_size, x[i] is tuple, len(x[0])=5
214- # def mpnn_feature_collate_func(x):
215- # ## first version
216- # return [torch.cat([x[j][i] for j in range(len(x))], 0) for i in range(len(x[0]))]
217-
218- # def mpnn_feature_collate_func(x):
219- # assert len(x[0]) == 5
220- # N_atoms_N_bonds = [i[-1] for i in x]
221- # N_atoms_scope = []
222- # f_a = torch.cat([x[j][0] for j in range(len(x))], 0)
223- # f_b = torch.cat([x[j][1] for j in range(len(x))], 0)
224- # agraph_lst, bgraph_lst = [], []
225- # Na, Nb = 0, 0
226- # for j in range(len(x)):
227- # agraph_lst.append(x[j][2] + Na)
228- # bgraph_lst.append(x[j][3] + Nb)
229- # N_atoms_scope.append([Na, x[j][2].shape[0]])
230- # Na += x[j][2].shape[0]
231- # Nb += x[j][3].shape[0]
232- # agraph = torch.cat(agraph_lst, 0)
233- # bgraph = torch.cat(bgraph_lst, 0)
234- # return [f_a, f_b, agraph, bgraph, N_atoms_scope]
235-
236-
237- # def mpnn_collate_func(x):
238- # #print("len(x) is ", len(x)) ## batch_size
239- # #print("len(x[0]) is ", len(x[0])) ## 3--- data_process_loader.__getitem__
240- # mpnn_feature = [i[0] for i in x]
241- # #print("len(mpnn_feature)", len(mpnn_feature), "len(mpnn_feature[0])", len(mpnn_feature[0]))
242- # mpnn_feature = mpnn_feature_collate_func(mpnn_feature)
243- # from torch.utils.data.dataloader import default_collate
244- # x_remain = [[i[1], i[2]] for i in x]
245- # x_remain_collated = default_collate(x_remain)
246- # return [mpnn_feature] + x_remain_collated
247- # ## used in dataloader
248-
213+ def dgl_collate_func (x ):
214+ d , p , y = zip (* x )
215+ print (d )
216+ print (p )
217+ print (y )
218+ import dgl
219+ d = dgl .batch (d )
220+ return d , torch .tensor (p ), torch .tensor (y )
249221
250222class DBTA :
251223 '''
@@ -267,6 +239,23 @@ def __init__(self, **config):
267239 self .model_drug = transformer ('drug' , ** config )
268240 elif drug_encoding == 'MPNN' :
269241 self .model_drug = MPNN (config ['hidden_dim_drug' ], config ['mpnn_depth' ])
242+ elif drug_encoding == 'DGL_GCN' :
243+ self .model_drug = DGL_GCN (in_feats = 74 ,
244+ hidden_feats = [config ['gnn_hid_dim_drug' ]] * config ['gnn_num_layers' ],
245+ activation = [config ['gnn_activation' ]] * config ['gnn_num_layers' ],
246+ predictor_dim = config ['hidden_dim_drug' ])
247+ elif drug_encoding == 'DGL_NeuralFP' :
248+ self .model_drug = DGL_NeuralFP (in_feats = 74 ,
249+ hidden_feats = [config ['gnn_hid_dim_drug' ]] * config ['gnn_num_layers' ],
250+ max_degree = config ['neuralfp_max_degree' ],
251+ activation = [config ['gnn_activation' ]] * config ['gnn_num_layers' ],
252+ predictor_hidden_size = config ['neuralfp_predictor_hid_dim' ],
253+ predictor_dim = config ['hidden_dim_drug' ],
254+ predictor_activation = config ['neuralfp_predictor_activation' ])
255+ elif drug_encoding == 'DGL_GIN_AttrMasking' :
256+ self .model_drug = DGL_GIN_AttrMasking (predictor_dim = config ['hidden_dim_drug' ])
257+ elif drug_encoding == 'DGL_GIN_ContextPred' :
258+ self .model_drug = DGL_GIN_ContextPred (predictor_dim = config ['hidden_dim_drug' ])
270259 else :
271260 raise AttributeError ('Please use one of the available encoding method.' )
272261
@@ -308,7 +297,7 @@ def test_(self, data_generator, model, repurposing_mode = False, test = False):
308297 y_label = []
309298 model .eval ()
310299 for i , (v_d , v_p , label ) in enumerate (data_generator ):
311- if self .drug_encoding == "MPNN" or self . drug_encoding == 'Transformer' :
300+ if self .drug_encoding in [ "MPNN" , 'Transformer' , 'DGL_GCN' , 'DGL_NeuralFP' , 'DGL_GIN_AttrMasking' , 'DGL_GIN_ContextPred' , 'AttentiveFP' ] :
312301 v_d = v_d
313302 else :
314303 v_d = v_d .float ().to (self .device )
@@ -387,6 +376,8 @@ def train(self, train, val = None, test = None, verbose = True):
387376 'drop_last' : False }
388377 if (self .drug_encoding == "MPNN" ):
389378 params ['collate_fn' ] = mpnn_collate_func
379+ elif self .drug_encoding in ['DGL_GCN' , 'DGL_NeuralFP' , 'DGL_GIN_AttrMasking' , 'DGL_GIN_ContextPred' , 'AttentiveFP' ]:
380+ params ['collate_fn' ] = dgl_collate_func
390381
391382 training_generator = data .DataLoader (data_process_loader (train .index .values , train .Label .values , train , ** self .config ), ** params )
392383 if val is not None :
@@ -402,6 +393,8 @@ def train(self, train, val = None, test = None, verbose = True):
402393
403394 if (self .drug_encoding == "MPNN" ):
404395 params_test ['collate_fn' ] = mpnn_collate_func
396+ elif self .drug_encoding in ['DGL_GCN' , 'DGL_NeuralFP' , 'DGL_GIN_AttrMasking' , 'DGL_GIN_ContextPred' , 'AttentiveFP' ]:
397+ params_test ['collate_fn' ] = dgl_collate_func
405398 testing_generator = data .DataLoader (data_process_loader (test .index .values , test .Label .values , test , ** self .config ), ** params_test )
406399
407400 # early stopping
@@ -430,7 +423,7 @@ def train(self, train, val = None, test = None, verbose = True):
430423 v_p = v_p
431424 else :
432425 v_p = v_p .float ().to (self .device )
433- if self .drug_encoding == "MPNN" or self . drug_encoding == 'Transformer' :
426+ if self .drug_encoding in [ "MPNN" , 'Transformer' , 'DGL_GCN' , 'DGL_NeuralFP' , 'DGL_GIN_AttrMasking' , 'DGL_GIN_ContextPred' , 'AttentiveFP' ] :
434427 v_d = v_d
435428 else :
436429 v_d = v_d .float ().to (self .device )
@@ -570,7 +563,8 @@ def predict(self, df_data):
570563
571564 if (self .drug_encoding == "MPNN" ):
572565 params ['collate_fn' ] = mpnn_collate_func
573-
566+ elif self .drug_encoding in ['DGL_GCN' , 'DGL_NeuralFP' , 'DGL_GIN_AttrMasking' , 'DGL_GIN_ContextPred' , 'AttentiveFP' ]:
567+ params ['collate_fn' ] = dgl_collate_func
574568
575569 generator = data .DataLoader (info , ** params )
576570
0 commit comments