forked from mrjoness/Flow-Back
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
1051 lines (811 loc) · 34 KB
/
utils.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
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import matplotlib.pyplot as plt # removing this thows scipy.optimize gcc error
import numpy as np
import torch
#import torchdyn
#from torchdyn.core import NeuralODE
from Bio.PDB import PDBParser, PDBIO, Select
#from torchcfm.conditional_flow_matching import *
import mdtraj as md
from tqdm import tqdm
from torch.utils.data import Dataset, DataLoader
from torch import nn, einsum, broadcast_tensors
import torch.nn.functional as F
from einops import rearrange, repeat
from einops.layers.torch import Rearrange
from itertools import islice, count
import math
#try:
from egnn_pytorch_se3.egnn_pytorch import EGNN_SE3, EGNN
se3_avail = True
# except:
# from egnn_pytorch import EGNN
# se3_avail = False
# print('using egnn_pytorch standard')
def exists(val):
return val is not None
class Attention(nn.Module):
def __init__(self, dim, heads = 8, dim_head = 64):
super().__init__()
inner_dim = heads * dim_head
self.heads = heads
self.scale = dim_head ** -0.5
self.to_q = nn.Linear(dim, inner_dim, bias = False)
self.to_kv = nn.Linear(dim, inner_dim * 2, bias = False)
self.to_out = nn.Linear(inner_dim, dim)
def forward(self, x, context, mask = None):
h = self.heads
q = self.to_q(x)
kv = self.to_kv(context).chunk(2, dim = -1)
q, k, v = map(lambda t: rearrange(t, 'b n (h d) -> b h n d', h = h), (q, *kv))
dots = einsum('b h i d, b h j d -> b h i j', q, k) * self.scale
if exists(mask):
mask_value = -torch.finfo(dots.dtype).max
mask = rearrange(mask, 'b n -> b () () n')
dots.masked_fill_(~mask, mask_value)
attn = dots.softmax(dim = -1)
out = einsum('b h i j, b h j d -> b h i d', attn, v)
out = rearrange(out, 'b h n d -> b n (h d)', h = h)
return self.to_out(out)
class GlobalLinearAttention(nn.Module):
def __init__(
self,
*,
dim,
heads = 8,
dim_head = 64
):
super().__init__()
self.norm_seq = nn.LayerNorm(dim)
self.norm_queries = nn.LayerNorm(dim)
self.attn1 = Attention(dim, heads, dim_head)
self.attn2 = Attention(dim, heads, dim_head)
self.ff = nn.Sequential(
nn.LayerNorm(dim),
nn.Linear(dim, dim * 4),
nn.GELU(),
nn.Linear(dim * 4, dim)
)
def forward(self, x, queries, mask = None):
res_x, res_queries = x, queries
x, queries = self.norm_seq(x), self.norm_queries(queries)
induced = self.attn1(queries, x, mask = mask)
out = self.attn2(x, induced)
x = out + res_x
queries = induced + res_queries
x = self.ff(x) + x
return x, queries
def generate_cos_pos_encoding(n, dim, device, scale=10000.0):
'''MJ -- replace pos_emb with sin/cos'''
assert dim % 2 == 0, "dim must be even for alternating sin/cos encoding"
pos_enc = torch.zeros(n, dim, device=device)
position = torch.arange(0, n, dtype=torch.float, device=device).unsqueeze(1)
div_term = torch.exp(torch.arange(0, dim, 2, device=device).float() * (-math.log(scale) / dim))
pos_enc[:, 0::2] = torch.sin(position * div_term)
pos_enc[:, 1::2] = torch.cos(position * div_term)
return pos_enc
# adapted from https://github.com/lucidrains/egnn-pytorch with added time conditioning
class EGNN_Network_time(nn.Module):
def __init__(
self,
*,
depth,
dim,
num_tokens = None,
num_edge_tokens = None,
num_positions = None,
emb_cos_scale = None,
edge_dim = 0,
num_adj_degrees = None,
adj_dim = 0,
global_linear_attn_every = 0,
global_linear_attn_heads = 8,
global_linear_attn_dim_head = 64,
num_global_tokens = 4,
time_dim=0,
res_dim=20, # change to 21
atom_dim=3, # change to 5
sym='e3',
**kwargs # MJ -- include seq_features, and seq_decay in kwargs
):
super().__init__()
assert not (exists(num_adj_degrees) and num_adj_degrees < 1), 'make sure adjacent degrees is greater than 1'
self.num_positions = num_positions
self.emb_cos_scale = emb_cos_scale
self.res_emb = nn.Embedding(res_dim, dim)
self.atom_emb = nn.Embedding(atom_dim, dim)
#self.time_emb = nn.Embedding(1, dim)
self.token_emb = nn.Embedding(num_tokens, dim) if exists(num_tokens) else None
self.pos_emb = nn.Embedding(num_positions, dim) if exists(num_positions) else None
self.edge_emb = nn.Embedding(num_edge_tokens, edge_dim) if exists(num_edge_tokens) else None
self.has_edges = edge_dim > 0
self.num_adj_degrees = num_adj_degrees
self.adj_emb = nn.Embedding(num_adj_degrees + 1, adj_dim) if exists(num_adj_degrees) and adj_dim > 0 else None
edge_dim = edge_dim if self.has_edges else 0
adj_dim = adj_dim if exists(num_adj_degrees) else 0
has_global_attn = global_linear_attn_every > 0
self.global_tokens = None
if has_global_attn:
self.global_tokens = nn.Parameter(torch.randn(num_global_tokens, dim))
self.layers = nn.ModuleList([])
for ind in range(depth):
is_global_layer = has_global_attn and (ind % global_linear_attn_every) == 0
if sym=='e3':
self.layers.append(nn.ModuleList([
GlobalLinearAttention(dim = dim, heads = global_linear_attn_heads, dim_head = global_linear_attn_dim_head) if is_global_layer else None,
EGNN(dim = dim, edge_dim = (edge_dim + adj_dim), norm_feats = True, **kwargs),
]))
elif sym=='se3' and se3_avail:
self.layers.append(nn.ModuleList([
GlobalLinearAttention(dim = dim, heads = global_linear_attn_heads, dim_head = global_linear_attn_dim_head) if is_global_layer else None,
EGNN_SE3(dim = dim, edge_dim = (edge_dim + adj_dim), norm_feats = True, **kwargs),
]))
# MJ -- add an MLP to encode time
self.time_dim = time_dim
if self.time_dim > 0:
self.time_net = torch.nn.Sequential(
torch.nn.Linear(1, self.time_dim),
torch.nn.SELU(),
torch.nn.Linear(self.time_dim, self.time_dim),
torch.nn.SELU(),
torch.nn.Linear(self.time_dim, dim),
)
#self.generate_cos_pos_encoding = generate_cos_pos_encoding
def forward(self, x):
return self.net(x)
def forward(
self,
feats,
coors,
time,
atom_feats=None,
adj_mat = None,
edges = None,
mask = None,
return_coor_changes = False
):
b, a, device = feats.shape[0], feats.shape[1], feats.device
if exists(self.token_emb):
feats = self.token_emb(feats)
if atom_feats != None:
feats += self.atom_emb(atom_feats)
if exists(self.pos_emb):
n = feats.shape[1]
assert n <= self.num_positions, f'given sequence length {n} must be less than the number of positions {self.num_positions} set at init'
pos_emb = self.pos_emb(torch.arange(n, device = device))
feats += rearrange(pos_emb, 'n d -> () n d')
# don't use both the linear and cos embeddings
elif exists(self.emb_cos_scale):
n, dim = feats.shape[1], feats.shape[2]
pos_emb_cos = generate_cos_pos_encoding(n, dim, device=device, scale=self.emb_cos_scale)
feats += rearrange(pos_emb_cos, 'n d -> () n d')
else:
pass
#print('No absolute pos encoding')
# if time passed as single float or dim 0 tensor
if isinstance(time, float) or time.dim()==0:
time = time*torch.ones((b, a, 1)).to(device)
# if time is passed as a tensor of size b
elif time.dim() == 1:
time = time[:, None].repeat(1, a)[:, :, None]
#else:
# feats += time[:, None, None]
# use a time embedding MLP
if self.time_dim > 0:
time = self.time_net(time)
feats += time
if exists(edges) and exists(self.edge_emb):
edges = self.edge_emb(edges)
# create N-degrees adjacent matrix from 1st degree connections
if exists(self.num_adj_degrees):
assert exists(adj_mat), 'adjacency matrix must be passed in (keyword argument adj_mat)'
if len(adj_mat.shape) == 2:
adj_mat = repeat(adj_mat.clone(), 'i j -> b i j', b = b)
adj_indices = adj_mat.clone().long()
for ind in range(self.num_adj_degrees - 1):
degree = ind + 2
next_degree_adj_mat = (adj_mat.float() @ adj_mat.float()) > 0
next_degree_mask = (next_degree_adj_mat.float() - adj_mat.float()).bool()
adj_indices.masked_fill_(next_degree_mask, degree)
adj_mat = next_degree_adj_mat.clone()
if exists(self.adj_emb):
adj_emb = self.adj_emb(adj_indices)
edges = torch.cat((edges, adj_emb), dim = -1) if exists(edges) else adj_emb
# setup global attention
global_tokens = None
if exists(self.global_tokens):
global_tokens = repeat(self.global_tokens, 'n d -> b n d', b = b)
# go through layers
coor_changes = [coors]
for global_attn, egnn in self.layers:
if exists(global_attn):
feats, global_tokens = global_attn(feats, global_tokens, mask = mask)
feats, coors = egnn(feats, coors, adj_mat = adj_mat, edges = edges, mask = mask)
coor_changes.append(coors)
if return_coor_changes:
return feats, coors, coor_changes
return feats, coors
def get_adj_mat(top):
num_atoms = top.n_atoms
bonded_pairs = []
for bond in top.bonds:
bonded_pairs.append((bond[0].index, bond[1].index))
adj_mat = np.zeros((num_atoms, num_atoms), dtype=int)
# Fill the adjacency matrix based on bonded pairs
for pair in bonded_pairs:
adj_mat[pair[0], pair[1]] = 1
adj_mat[pair[1], pair[0]] = 1
adj_mat = torch.tensor(adj_mat, dtype=torch.bool)
return adj_mat
def get_adj_CG(xyz, mask_idxs, cut=1.0):
'''Directly connect all CG atoms only'''
num_atoms = xyz.shape[1]
adj_mat = np.zeros((num_atoms, num_atoms), dtype=int)
for i in range(num_atoms):
if i in mask_idxs:
dists = np.sqrt(((xyz[i] - xyz[mask_idxs])**2).sum(axis=-1))
include_idxs = np.where(dists < cut)[0]
adj_mat[i, mask_idxs[include_idxs]] = 1
adj_mat = torch.tensor(adj_mat, dtype=torch.bool)
return adj_mat
def get_aa_to_cg(top, msk):
'''Mapping between AA and CG
Assign to Ca positions for now with mask, but will need to generalize this'''
aa_to_cg = []
for atom_idx, atom in enumerate(top.atoms):
res_idx = atom.residue.index
aa_to_cg.append(msk[res_idx])
return np.array(aa_to_cg)
def get_prior(xyz, aa_to_cg, mask_idxs=None, scale=1.0, frames=None):
'''Normally distribute around respective Ca center of mass'''
# set center of distribution to each CA and use uniform scale
xyz_ca = xyz[:, aa_to_cg]
scale = scale * np.ones(xyz_ca.shape)
xyz_prior = np.random.normal(loc=xyz_ca, scale=scale, size=xyz.shape)
# don't add noise to masked values
if mask_idxs is not None:
xyz_prior[:, mask_idxs] = xyz[:, mask_idxs]
return xyz_prior
def get_prior_mix(xyz, aa_to_cg, scale=1.0):
'''Normally distribute around respective Ca center of mass'''
# set center of distribution to each CA and use uniform scale
xyz_prior = []
for xyz_ref, map_ref in zip(xyz, aa_to_cg):
xyz_ca = xyz_ref[map_ref]
xyz_prior.append(np.random.normal(loc=xyz_ca, scale=scale * np.ones(xyz_ca.shape), size=xyz_ca.shape))
return xyz_prior #np.array(xyz_prior, dtype=object) # fix ragged nest warning
def get_prior_mask(xyz, aa_to_cg, masks=None, scale=1.0):
'''Normally distribute around respective masked coordinates
Optionally mask out CG values so they are identical in CG and AA traces'''
# set center of distribution to each CA and use uniform scale
xyz_prior = []
for i, (xyz_ref, map_ref) in enumerate(zip(xyz, aa_to_cg)):
xyz_ca = xyz_ref[map_ref]
xyz_ca = np.random.normal(loc=xyz_ca, scale=scale * np.ones(xyz_ca.shape), size=xyz_ca.shape)
# ensure masked values are not noised at all
if masks is not None:
mask = ~masks[i].astype(bool)
xyz_ca[mask] = xyz_ref[mask]
xyz_prior.append(xyz_ca)
return xyz_prior
def str_to_ohe(string_list):
unique_strings = list(set(string_list))
string_to_index = {string: index for index, string in enumerate(unique_strings)}
indices = [string_to_index[string] for string in string_list]
return np.array(indices)
def get_node_ohes(top):
'''get one-hot encodings of residue and atom element identities'''
res_list, atom_list = [], []
for a in top.atoms:
if a.element.name != 'hydrogen':
res_list.append(a.residue.name)
atom_list.append(a.element.name)
res_ohe = str_to_ohe(res_list)
atom_ohe = str_to_ohe(atom_list)
return res_ohe, atom_ohe
# load data set
class CustomDataset(Dataset):
def __init__(self, data_list1, data_list2):
self.data_list1 = data_list1
self.data_list2 = data_list2
def __len__(self):
return len(self.data_list1) # Assuming both lists have the same length
def __getitem__(self, index):
sample1 = torch.tensor(self.data_list1[index], dtype=torch.float32)
sample2 = torch.tensor(self.data_list2[index], dtype=torch.float32)
return sample1, sample2
# load data set
class FeatureDataset(Dataset):
def __init__(self, data_list1, data_list2, data_list3, data_list4, data_list5, data_list6):
self.data_list1 = data_list1
self.data_list2 = data_list2
self.data_list3 = data_list3
self.data_list4 = data_list4
self.data_list5 = data_list5
self.data_list6 = data_list6
def __len__(self):
return len(self.data_list1) # Assuming both lists have the same length
def __getitem__(self, index):
sample1 = torch.tensor(self.data_list1[index], dtype=torch.float32)
sample2 = torch.tensor(self.data_list2[index], dtype=torch.float32)
sample3 = torch.tensor(self.data_list3[index], dtype=torch.int)
sample4 = torch.tensor(self.data_list4[index], dtype=torch.int)
sample5 = torch.tensor(self.data_list5[index], dtype=torch.bool)
sample6 = torch.tensor(self.data_list6[index], dtype=torch.bool)
return sample1, sample2, sample3, sample4, sample5, sample6
def pro_res_to_ohe(string_list):
amino_acids = {
"ALA": 1, # Alanine
"ARG": 2, # Arginine
"ASN": 3, # Asparagine
"ASP": 4, # Aspartic acid
"CYS": 5, # Cysteine
"GLU": 6, # Glutamic acid
"GLN": 7, # Glutamine
"GLY": 8, # Glycine
"HIS": 9, # Histidine
"ILE": 10, # Isoleucine
"LEU": 11, # Leucine
"LYS": 12, # Lysine
"MET": 13, # Methionine
"PHE": 14, # Phenylalanine
"PRO": 15, # Proline
"SER": 16, # Serine
"THR": 17, # Threonine
"TRP": 18, # Tryptophan
"TYR": 19, # Tyrosine
"VAL": 20 # Valine
}
indices = [amino_acids[string] for string in string_list]
return np.array(indices)
def pro_atom_to_ohe(string_list):
atom_types = {
"carbon": 1,
"oxygen": 2,
"nitrogen": 3,
"sulfur": 4,
}
indices = [atom_types[string] for string in string_list]
return np.array(indices)
def pro_allatom_to_ohe(string_list):
'''List all possible atom names'''
atom_types = {
'C':1,
'CA':2,
'CB':3,
'CD':4,
'CD1':5,
'CD2':6,
'CE':7,
'CE1':8,
'CE2':9,
'CE3':10,
'CG':11,
'CG1':12,
'CG2':13,
'CH2':14,
'CZ':15,
'CZ2':16,
'CZ3':17,
'N':18,
'ND1':19,
'ND2':20,
'NE':21,
'NE1':22,
'NE2':23,
'NH1':24,
'NH2':25,
'NZ':26,
'O':27,
'OD1':28,
'OD2':29,
'OE1':30,
'OE2':31,
'OG':32,
'OG1':33,
'OH':34,
'SD':35,
'SG':36
}
indices = [atom_types[string] for string in string_list]
return np.array(indices)
def get_pro_ohes(top):
'''get one-hot encodings of residue and atom element identities'''
res_list, atom_list, allatom_list = [], [], []
for a in top.atoms:
if a.element.name != 'hydrogen':
res_list.append(a.residue.name)
atom_list.append(a.element.name)
allatom_list.append(a.name)
res_ohe = pro_res_to_ohe(res_list)
atom_ohe = pro_atom_to_ohe(atom_list)
allatom_ohe = pro_allatom_to_ohe(allatom_list)
return res_ohe, atom_ohe, allatom_ohe
def dna_res_to_ohe(string_list):
bases = {
"DA": 1,
"DC": 2,
"DG": 3,
"DT": 4,
}
indices = [bases[string] for string in string_list]
return np.array(indices)
# should change this to be consistent with proteins atom types
def dna_atom_to_ohe(string_list):
atom_types = {
"carbon": 1,
"oxygen": 2,
"nitrogen": 3,
"phosphorus": 5, # change to 5 and scale COMs by 1
"BCOM":6,
"SCOM":7,
"PCOM":8,
}
indices = [atom_types[string] for string in string_list]
return np.array(indices)
def dna_allatom_to_ohe(string_list):
'''List all possible atom names'''
atom_types = {
"O4": 1,
"O2": 2,
"C1'": 3,
"N4": 4,
"O6": 5,
"N7": 6,
"C3'": 7,
"C4'": 8,
"N1": 9,
"C7": 10,
"C2'": 11,
"C2": 12,
"C8": 13,
"C5'": 14,
"N3": 15,
"N9": 16,
"N6": 17,
"P": 18,
"OP1": 19,
"C6": 20,
"O4'": 21,
"O5'": 22,
"C4": 23,
"OP2": 24,
"N2": 25,
"O3'": 26,
"C5": 27,
"BCOM":28,
"SCOM":29,
"PCOM":30,
}
indices = [atom_types[string] for string in string_list]
return np.array(indices)
def get_dna_ohes(top):
'''get one-hot encodings of residue and atom element identities'''
res_list, atom_list, allatom_list = [], [], []
for a in top.atoms:
if a.element.name != 'hydrogen':
res_list.append(a.residue.name)
atom_list.append(a.element.name)
allatom_list.append(a.name)
res_ohe = dna_res_to_ohe(res_list)
atom_ohe = dna_atom_to_ohe(atom_list)
allatom_ohe = dna_atom_to_ohe(atom_list)
return res_ohe, atom_ohe, allatom_ohe
# backmap to all heavy atoms given only the Ca positions
def parse_dna_3spn(dna_trj, with_pro=False):
'''Extract GNN parameters compatible with 3sn2 CG representation of DNA
Ensure that dna_trj only includes dna residues
If proteins also included in then need to add constant to ohes'''
print('init trj shape', dna_trj.xyz.shape)
# seperate AA and CG components if both are included
try:
cg_idxs = dna_trj.top.select(f"name DS or name DP or name DB")
all_idxs = range(dna_trj.n_atoms)
aa_idxs = [idx for idx in all_idxs if idx not in cg_idxs]
cg_trj = dna_trj.atom_slice(cg_idxs)
dna_trj = dna_trj.atom_slice(aa_idxs)
except:
cg_trj = None
# get all 5' and 3' residues idxs
ter_res_list = []
for chain in dna_trj.topology.chains:
residues = list(chain.residues)
# Determine the site type for each residue in the chain
for index, residue in enumerate(residues):
if index == 0:
ter_res_list.append(5)
elif index == len(residues) - 1:
ter_res_list.append(3)
else:
ter_res_list.append(0)
dna_top = dna_trj.top
n_resid = dna_top.n_residues
xyz = dna_trj.xyz
n_frames = len(xyz)
n_atoms = xyz.shape[1]
xyz_com = [xyz]
aa_to_cg = np.zeros(n_atoms)
mask_idxs = []
cg_atom_list = []
cg_res_list = []
for n in range(0, n_resid):
# make sure to collect O3 from the previous residue
res_idxs = dna_top.select(f'resid {n} and not name "O3\'"')
chain_id = dna_top.atom(res_idxs[0]).residue.chain.index
# if not a 5' end then include the O3'
if ter_res_list[n] != 5:
O3_prev = dna_top.select(f'resid {n-1} and name "O3\'"')
res_idxs = np.concatenate([res_idxs, O3_prev])
# if a 3' end then incldue terminal 03' in mapping but not in com
if ter_res_list[n] == 3:
O3_curr = dna_top.select(f'resid {n} and name "O3\'"')[0]
else:
O3_curr = None
# get names of all atoms in resid
atom_list = [next(islice(dna_top.atoms, idx, None)).name for idx in res_idxs]
# get res name
res_name = next(islice(dna_top.atoms, res_idxs[0], None)).residue.name
# break if hit a CG coord:
if 'DS' in atom_list:
continue
# passing each res to each chain type
b_idxs, s_idxs, p_idxs = [], [], []
b_names, s_names, p_names = [], [], []
for idx, name in zip(res_idxs, atom_list):
# need to get exact lists here and verify against 3spn code -- eg 3
if name in ['P', 'OP2', 'OP1', 'O5\'', 'O3\'']:
p_idxs.append(idx)
p_names.append(name)
elif "'" in name:
s_idxs.append(idx)
s_names.append(name)
else:
b_idxs.append(idx)
b_names.append(name)
# compute center of mass for each
b_coms = md.compute_center_of_mass(dna_trj.atom_slice(b_idxs)).reshape((n_frames, 1, 3))
s_coms = md.compute_center_of_mass(dna_trj.atom_slice(s_idxs)).reshape((n_frames, 1, 3))
# append terminal 03' after calculating coms (part of mask but not COM)
if O3_curr is not None:
s_idxs.append(O3_curr)
# check if any phosphates in the residue (don't want to group based on 05' alone)
if len(p_idxs) > 1:
p_coms = md.compute_center_of_geometry(dna_trj.atom_slice(p_idxs)).reshape((n_frames, 1, 3))
xyz_com.append(p_coms)
xyz_com.append(s_coms)
xyz_com.append(b_coms)
# check why not getting any b_idxs -- residue has phosphat but no b or s?
#print('b s p', len(b_idxs), len(s_idxs), len(p_idxs))
# map to b, s, or p coms
aa_to_cg[np.array(p_idxs)] = n_atoms + len(xyz_com) - 4
aa_to_cg[np.array(s_idxs)] = n_atoms + len(xyz_com) - 3
aa_to_cg[np.array(b_idxs)] = n_atoms + len(xyz_com) - 2
cg_atom_list += ['PCOM', 'SCOM', 'BCOM'] #['BCOM', 'SCOM', 'PCOM']
cg_res_list += [res_name]*3
else:
# map any missing atoms to the sugar
xyz_com.append(s_coms)
xyz_com.append(b_coms)
s_idxs += p_idxs
aa_to_cg[np.array(s_idxs)] = n_atoms + len(xyz_com) - 3
aa_to_cg[np.array(b_idxs)] = n_atoms + len(xyz_com) - 2
cg_atom_list += ['SCOM', 'BCOM'] #['BCOM', 'SCOM']
cg_res_list += [res_name]*2
# a lot easier to append everything at the end the xyz
xyz_com = np.concatenate(xyz_com, axis=1)
n_atoms_com = xyz_com.shape[1]
# if cg coords exist, replace the xyz_com values with these
# need to change order from B-S-P to P-S-B
if cg_trj is not None:
print('xyz_com', xyz_com.shape, cg_trj.xyz.shape)
xyz_com[:, -cg_trj.xyz.shape[1]:] = cg_trj.xyz
# set mask values to COMs only
mask_idxs = np.arange(n_atoms, n_atoms_com)
# set CG mask values to themselves
aa_to_cg = np.concatenate([aa_to_cg, np.arange(n_atoms, n_atoms_com)])
aa_to_cg = np.array(aa_to_cg, dtype=int)
# get res and atom feats for standard atoms
res_ohe, atom_ohe, all_atom_ohe = get_dna_ohes(dna_top)
# manually add com encodings for now -- based on pos encoding this might work better dispersed in sequence
res_ohe = np.concatenate([res_ohe, dna_res_to_ohe(cg_res_list)])
atom_ohe = np.concatenate([atom_ohe, dna_atom_to_ohe(cg_atom_list)])
all_atom_ohe = np.concatenate([all_atom_ohe, dna_allatom_to_ohe(cg_atom_list)])
# ensure no overlap with pro encoding
if with_pro:
res_ohe = res_ohe + 20
atom_ohe = atom_ohe + 5
all_atom_ohe = all_atom_ohe + 36
return xyz_com, mask_idxs, aa_to_cg, res_ohe, all_atom_ohe
# set up alagous protein parse function
def parse_pro_CA(pro_trj):
'''Extract GNN parameters compatible with 3sn2 CG representation of DNA
Ensure that pro_trj only includes dna residues'''
res_ohe, atom_ohe, all_atom_ohe = get_pro_ohes(pro_trj.top)
mask_idxs = pro_trj.top.select('name CA')
aa_to_cg = get_aa_to_cg(pro_trj.top, mask_idxs)
xyz = pro_trj.xyz
return xyz, mask_idxs, aa_to_cg, res_ohe, all_atom_ohe
class ModelWrapper(EGNN_Network_time):
def __init__(
self,
model,
feats,
mask,
atom_feats=None,
adj_mat=None
):
super(EGNN_Network_time, self).__init__() # Call the nn.Module constructor
self.model = model
self.feats = feats
self.mask = mask
self.atom_feats = atom_feats
self.adj_mat = adj_mat
def forward(self, t, x, y=None, *args, **kwargs):
feats_out, coors_out = self.model(self.feats, x, mask=self.mask, time=t,
atom_feats=self.atom_feats, adj_mat=self.adj_mat)
return coors_out - x
# add custom integrators
def euler_integrator(model, x, nsteps=100, mask=None, noise=0.0):
# try adding small amounts of noise during integration
ode_list = []
dt = 1./(nsteps-1)
for t in np.linspace(0, 1, nsteps):
# Evaluate dx/dt using the model for the current state and time
with torch.no_grad():
dx_dt = model(t, x.detach())
# Compute the next state using Euler's formula
x = (x + dx_dt * dt).detach()
# add some noise to model sde (except for masked values)
if mask != None:
masked_noise = mask*noise*torch.randn(mask.shape).to(device)
masked_noise = masked_noise.unsqueeze(-1).expand(-1, -1, 3)
x += masked_noise
# track each update to show diffusion path
ode_list.append(x.cpu().numpy())
return np.array(ode_list)
def runge_kutta_integrator(model, x, nsteps=50):
'''Runge Kutta 4th order solver (takes ~4x longer per-step compated to Euler)'''
ode_list = []
dt = 1./(nsteps-1)
for t in np.linspace(0, 1, nsteps):
k1 = model(t, x).detach()
k2 = model(t + 0.5*dt, x + 0.5*dt*k1).detach()
k3 = model(t + 0.5*dt, x + 0.5*dt*k2).detach()
k4 = model(t + dt, x + dt*k3).detach()
# Compute the next state using the RK4 formula
x = x + (dt/6.0) * (k1 + 2*k2 + 2*k3 + k4)
# track each update to show diffusion path
ode_list.append(x.cpu().numpy())
return np.array(ode_list)
def bond_fraction(trj_ref, trj_gen, fraction=0.1):
'''Fraction of bonds within X percent of the reference'''
bond_pairs = [[b[0].index, b[1].index] for b in trj_ref.top.bonds]
ref_dist = md.compute_distances(trj_ref, bond_pairs)
gen_dist = md.compute_distances(trj_gen, bond_pairs)
bond_frac = np.sum((gen_dist < (1+fraction)*ref_dist) &
(gen_dist > (1-fraction)*ref_dist))
bond_frac = bond_frac / np.size(ref_dist)
return bond_frac
def get_res_idxs_cut(trj, thresh=0.12, Ca_cut=2.0):
Ca_idxs = []
for i, atom in enumerate(trj.top.atoms):
if 'CA' in atom.name:
Ca_idxs.append(i)
Ca_idxs = np.array(Ca_idxs)
Ca_xyzs = trj.xyz[0, Ca_idxs]
n_res = trj.n_residues
pairs = []
for i in range(n_res):
for j in range(i-1):
if np.linalg.norm(Ca_xyzs[i]-Ca_xyzs[j]) < Ca_cut:
pairs.append((i, j))
dist, pairs = md.compute_contacts(trj, contacts=pairs, scheme='closest')
# look at sidechain-heavy only
neighbor_pairs = [(i, i+1) for i in range(trj.n_residues-1) if (
trj.top.residue(i).name != 'GLY' and trj.top.residue(i+1).name != 'GLY')]
neighbor_dist, neighbor_pairs = md.compute_contacts(trj, contacts=neighbor_pairs, scheme='sidechain-heavy')
dist = np.concatenate([dist, neighbor_dist], axis=-1)
pairs = np.concatenate([pairs, neighbor_pairs], axis=0)
res_closes = list()
for n_res in range(trj.top.n_residues):
pair_mask = np.array([n_res in i for i in pairs])
res_close = np.any(dist[0, pair_mask] < thresh)
res_closes.append(res_close)
res_closes = np.array(res_closes)
return res_closes
def clash_res_percent(viz_gen, thresh=0.12, Ca_cut=2.0):
all_res_closes = list()
#for n in tqdm(range(len(viz_gen))):
for n in range(len(viz_gen)):
res_closes = get_res_idxs_cut(viz_gen[n], thresh=thresh, Ca_cut=Ca_cut)
all_res_closes.append(res_closes)
return 100 * sum([sum(i) for i in all_res_closes]) / sum([i.shape[0] for i in all_res_closes])
# for calculating generative diversity
def ref_rmsd(trj_ref, trj_sample_list):
rmsd_list = []
for i, trj_i in enumerate(trj_sample_list):
print(trj_i.xyz.shape, trj_ref.xyz.shape)
for k, (trj_if, trj_rf) in enumerate(zip(trj_i, trj_ref)):
rmsd = md.rmsd(trj_if, trj_rf)*10
rmsd_list.append(rmsd)
#frame_rmsds.append(rmsd)
#rmsd_list.append(np.mean(frame_rmsds))
return np.mean(rmsd_list), np.std(rmsd_list)
def sample_rmsd(trj_sample_list):
rmsd_list = []
for i, trj_i in enumerate(trj_sample_list):
for j, trj_j in enumerate(trj_sample_list[:i]):
# need to compare per frames rmsds or else will be relative to first frame
#frame_rmsds = []
for k, (trj_if, trj_jf) in enumerate(zip(trj_i, trj_j)):
rmsd = md.rmsd(trj_if, trj_jf)*10
rmsd_list.append(rmsd)
#frame_rmsds.append(rmsd)
#rmsd_list.append(np.mean(frame_rmsds))
return np.mean(rmsd_list), np.std(rmsd_list)
def sample_rmsd_percent(trj_ref, trj_sample_list):
R_ref, S_ref = ref_rmsd(trj_ref, trj_sample_list)
R_sam, S_sam = sample_rmsd(trj_sample_list)
R_per = (R_ref-R_sam) / R_ref
S_per = np.sqrt( (S_sam/R_ref)**2 + ((R_sam*S_ref)/(R_ref)**2)**2 )
return R_per, S_per
# get uncertainties on diversity scores
def jackknife_div(trj_ref, trj_sample_list):
gen_ref = get_ref_gen_rmsds(trj_ref, trj_gens)
gen_gen = get_sample_rmsds(trj_gens)
assert len(gen_ref) == len(gen_gen)
div_mat = np.zeros((trj_ref.n_frames, len(trj_sample_list)))
for frame_idx, (gen_gen_i, gen_ref_i) in enumerate(zip(gen_gen, gen_ref)):
for targ in range(len(trj_sample_list)):
gen_gen_mean = np.mean([v for i,v in gen_gen_i.items() if targ not in i])
gen_ref_mean = np.mean(np.delete(gen_ref_i, targ))
div_mat[frame_idx][targ] = 1 - (gen_gen_mean / gen_ref_mean)
return div_mat.mean(0)
# Define a custom selector for filtering
class ProteinDNASelect(Select):
def accept_residue(self, residue):
# Accept only protein and DNA residues
if "CA" in residue or residue.id[0] == " " and residue.resname.strip() in ["DA", "DC", "DG", "DT"]:
return 1 # Return 1 to indicate acceptance
else:
return 0 # Return 0 to exclude everything else