-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.py
1014 lines (744 loc) · 39.2 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
###
'''
April 2019
Code by: Arnaud Fickinger
'''
###
from loss import *
import numpy as np
from model import *
from collections import *
from options import Options
opt = Options().parse()
def sample_diag_gaussian(mu, logvar): #reparametrization trick
std = torch.exp(0.5 * logvar)
eps = torch.randn_like(std)
return mu + eps * std
def sample_diag_gaussian_original(mu, logsigma, k_iws = 1): #reparametrization trick
std = torch.exp(logsigma)
# print("stdshape: {}".format(std.shape))
# if opt.IWS:
# print("stdshape")
# print(std.shape)
# std = std.unsqueeze(1)
# std = std.repeat(1, k_iws, 1)
# mu = mu.unsqueeze(1)
# mu = mu.repeat(1, k_iws, 1)
eps = torch.randn_like(std)
return mu + eps * std
def _anneal(update_num):
""" Anneal the KL if using annealing"""
# If true, return first, else return second
KL_scale = torch.where(update_num < opt.warm_up, update_num/opt.warm_up, 1.0)
return KL_scale
def log_gaussian_logvar(x, mu, logvar):
return float(-0.5 * np.log(2 * np.pi)) - 0.5*logvar - (x - mu).pow(2) / logvar.exp()
def log_gaussian_logsigma(x, mu, logsigma):
return float(-0.5 * np.log(2 * np.pi)) - logsigma - (x - mu).pow(2) / torch.exp(logsigma)
def log_sum_exp(tensor, dim=-1, sum_op=torch.sum):
max, _ = torch.max(tensor, dim=dim, keepdim=True)
return torch.log(sum_op(torch.exp(tensor - max), dim=dim, keepdim=True) + 1e-8) + max
class DataHelper:
def __init__(self,
dataset,
theta,
custom_dataset = False,
alignment_file="",
focus_seq_name="",
calc_weights=True,
working_dir=".",
load_all_sequences=True,
alphabet_type="protein"):
"""
Class to load and organize alignment data.
This function also helps makes predictions about mutations.
Parameters
--------------
dataset: preloaded dataset names
We have found it easiest to organize datasets in this
way and use the self.configure_datasets() func
alignment_file: Name of the alignment file located in the "datasets"
folder. Not needed if dataset pre-entered
focus_seq_name: Name of the sequence in the alignment
Defaults to the first sequence in the alignment
calc_weights: (bool) Calculate sequence weights
Default True, but not necessary if just loading weights
and doing mutation effect prediction
working_dir: location of "params", "logs", "embeddings", and "datasets"
folders
theta: Sequence weighting hyperparameter
Generally: Prokaryotic and eukaryotic families = 0.2
Viruses = 0.01
load_all_sequences:
alphabet_type: Alphabet type of associated dataset.
Options are DNA, RNA, protein, allelic
Returns
------------
None
"""
# np.random.seed(42)
self.dataset = dataset
self.alignment_file = alignment_file
self.focus_seq_name = focus_seq_name
self.working_dir = working_dir
self.calc_weights = calc_weights
self.alphabet_type = alphabet_type
if theta == 0:
self.calc_weigths = False
# Initalize the elbo of the wt to None
# will be useful if eventually doing mutation effect prediction
self.wt_elbo = None
# Alignment processing parameters
self.theta = theta
# If I am running tests with the model, I don't need all the
# sequences loaded
self.load_all_sequences = load_all_sequences
# Load necessary information for preloaded datasets
if custom_dataset:
self.alignment_file = dataset
elif self.dataset != "":
self.configure_datasets()
# Load up the alphabet type to use, whether that be DNA, RNA, or protein
if self.alphabet_type == "protein":
self.alphabet = "ACDEFGHIKLMNPQRSTVWY"
self.reorder_alphabet = "DEKRHNQSTPGAVILMCFYW"
elif self.alphabet_type == "RNA":
self.alphabet = "ACGU"
self.reorder_alphabet = "ACGU"
elif self.alphabet_type == "DNA":
self.alphabet = "ACGT"
self.reorder_alphabet = "ACGT"
elif self.alphabet_type == "allelic":
self.alphabet = "012"
self.reorder_alphabet = "012"
#then generate the experimental data
self.gen_basic_alignment()
if self.load_all_sequences:
self.gen_full_alignment()
def configure_datasets(self):
if opt.test_algo:
self.alignment_file = self.working_dir + "/datasets/DLG4.a2m"
elif self.dataset == "BLAT_ECOLX":
self.alignment_file = self.working_dir+"/datasets/BLAT_ECOLX_hmmerbit_plmc_n5_m30_f50_t0.2_r24-286_id100_b105.a2m"
# self.theta = 0.2
elif self.dataset == "PABP_YEAST":
self.alignment_file = self.working_dir+"/datasets/PABP_YEAST_hmmerbit_plmc_n5_m30_f50_t0.2_r115-210_id100_b48.a2m"
# self.theta = 0.2
elif self.dataset == "DLG4_RAT":
self.alignment_file = self.working_dir+"/datasets/DLG4_RAT_hmmerbit_plmc_n5_m30_f50_t0.2_r300-400_id100_b50.a2m"
# self.theta = 0.2
elif self.dataset == "BG505":
self.alignment_file = self.working_dir+"/datasets/BG505_env_1_b0.5.a2m"
# self.theta = 0.2
elif self.dataset == "BF520":
self.alignment_file = self.working_dir+"/datasets/BF520_env_1_b0.5.a2m"
# self.theta = 0.01
elif self.dataset == "trna":
self.alignment_file = self.working_dir+"/datasets/RF00005_CCU.fasta"
self.alphabet_type = "RNA"
# self.theta = 0.2
def one_hot_3D(self, s):
""" Transform sequence string into one-hot aa vector"""
# One-hot encode as row vector
x = np.zeros((len(s), len(self.alphabet)))
for i, letter in enumerate(s):
if letter in self.aa_dict:
x[i , self.aa_dict[letter]] = 1
return x
def gen_basic_alignment(self):
""" Read training alignment and store basics in class instance """
# Make a dictionary that goes from aa to a number for one-hot
self.aa_dict = {}
for i,aa in enumerate(self.alphabet):
self.aa_dict[aa] = i
# Do the inverse as well
self.num_to_aa = {i:aa for aa,i in self.aa_dict.items()}
ix = np.array([self.alphabet.find(s) for s in self.reorder_alphabet])
# Read alignment
self.seq_name_to_sequence = defaultdict(str)
self.seq_names = []
name = ""
INPUT = open(self.alignment_file, "r")
for i, line in enumerate(INPUT):
line = line.rstrip()
if line.startswith(">"):
name = line
self.seq_names.append(name)
else:
self.seq_name_to_sequence[name] += line
INPUT.close()
# If we don"t have a focus sequence, pick the one that
# we used to generate the alignment
if self.focus_seq_name == "":
self.focus_seq_name = self.seq_names[0]
# Select focus columns
# These columns are the uppercase residues of the .a2m file
self.focus_seq = self.seq_name_to_sequence[self.focus_seq_name]
self.focus_cols = [ix for ix, s in enumerate(self.focus_seq) if s == s.upper()]
self.focus_seq_trimmed = [self.focus_seq[ix] for ix in self.focus_cols]
self.seq_len = len(self.focus_cols)
self.alphabet_size = len(self.alphabet)
# We also expect the focus sequence to be formatted as:
# >[NAME]/[start]-[end]
focus_loc = self.focus_seq_name.split("/")[-1]
start,stop = focus_loc.split("-")
self.focus_start_loc = int(start)
self.focus_stop_loc = int(stop)
self.uniprot_focus_cols_list \
= [idx_col+int(start) for idx_col in self.focus_cols]
self.uniprot_focus_col_to_wt_aa_dict \
= {idx_col+int(start):self.focus_seq[idx_col] for idx_col in self.focus_cols}
self.uniprot_focus_col_to_focus_idx \
= {idx_col+int(start):idx_col for idx_col in self.focus_cols}
def gen_full_alignment(self):
# Get only the focus columns
for seq_name,sequence in self.seq_name_to_sequence.items():
# Replace periods with dashes (the uppercase equivalent)
sequence = sequence.replace(".","-")
#then get only the focus columns
self.seq_name_to_sequence[seq_name] = [sequence[ix].upper() for ix in self.focus_cols]
# Remove sequences that have bad characters
alphabet_set = set(list(self.alphabet))
seq_names_to_remove = []
for seq_name,sequence in self.seq_name_to_sequence.items():
for letter in sequence:
if letter not in alphabet_set and letter != "-":
seq_names_to_remove.append(seq_name)
seq_names_to_remove = list(set(seq_names_to_remove))
for seq_name in seq_names_to_remove:
del self.seq_name_to_sequence[seq_name]
# Encode the sequences
print ("Encoding sequences")
self.x_train = np.zeros((len(self.seq_name_to_sequence.keys()),len(self.focus_cols),len(self.alphabet)))
self.x_train_name_list = []
for i,seq_name in enumerate(self.seq_name_to_sequence.keys()):
sequence = self.seq_name_to_sequence[seq_name]
self.x_train_name_list.append(seq_name)
for j,letter in enumerate(sequence):
if letter in self.aa_dict:
k = self.aa_dict[letter]
self.x_train[i,j,k] = 1.0
#Very fast weight computation
self.seqlen = self.x_train.shape[1]
self.datasize = self.x_train.shape[0]
if self.calc_weights and self.theta>0:
print("effective weigths")
weights = []
seq_batch = 1000
x_train_flat = self.x_train.reshape(self.x_train.shape[0], -1)
nb_seq = x_train_flat.shape[0]
nb_iter = int(nb_seq/seq_batch)
rest = nb_seq%seq_batch
xtfs_t = torch.Tensor(x_train_flat).float().to(device)
for i in range(nb_iter):
weights.append(1.0 / (((torch.div(torch.mm(xtfs_t[i*seq_batch:(i+1)*seq_batch], xtfs_t.transpose(0,1)), xtfs_t[i*seq_batch:(i+1)*seq_batch].sum(1).unsqueeze(1))) > (1 - self.theta)).sum(1).float()))
weights.append(1.0 / (((torch.div(torch.mm(xtfs_t[-rest:], xtfs_t.transpose(0,1)), xtfs_t[-rest:].sum(1).unsqueeze(1))) > (1 - self.theta)).sum(1).float()))
weights_tensor = torch.cat(weights)
self.weights = weights_tensor
# self.weights = weights_tensor.cpu().numpy()
self.Neff = weights_tensor.sum()
# print(self.Neff)
else:
# # If not using weights, use an isotropic weight matrix
self.weights = np.ones(self.x_train.shape[0])
self.Neff = self.x_train.shape[0]
print ("Neff =",str(self.Neff))
print ("Data Shape =",self.x_train.shape)
# # Fast sequence weights with Theano
# if self.calc_weights:
# print ("Computing sequence weights")
# # Numpy version
# import scipy
# from scipy.spatial.distance import pdist, squareform
# x_train_flat = self.x_train.reshape(self.x_train.shape[0], -1)
# print(x_train_flat.shape)
# # self.weights = 1.0 / np.sum(squareform(pdist(x_train_flat[:10000], metric="hamming")) < self.theta, axis=0)
# self.weights = 1.0 / np.sum(squareform(pdist(x_train_flat, metric="hamming")) < self.theta, axis=0)
# #
# # Theano weights
# # X = T.tensor3("x")
# # cutoff = T.scalar("theta")
# # X_flat = X.reshape((X.shape[0], X.shape[1]*X.shape[2]))
# # N_list, updates = theano.map(lambda x: 1.0 / T.sum(T.dot(X_flat, x) / T.dot(x, x) > 1 - cutoff), X_flat)
# # weightfun = theano.function(inputs=[X, cutoff], outputs=[N_list],allow_input_downcast=True)
# #
# # self.weights = weightfun(self.x_train, self.theta)[0]
# else:
# # If not using weights, use an isotropic weight matrix
# self.weights = np.ones(self.x_train.shape[0])
# self.Neff = np.sum(self.weights)
# print ("Neff =",str(self.Neff))
# print ("Data Shape =",self.x_train.shape)
def delta_elbo(self, model, mutant_tuple_list, N_pred_iterations=10):
for pos,wt_aa,mut_aa in mutant_tuple_list:
if pos not in self.uniprot_focus_col_to_wt_aa_dict \
or self.uniprot_focus_col_to_wt_aa_dict[pos] != wt_aa:
print ("Not a valid mutant!",pos,wt_aa,mut_aa)
return None
mut_seq = self.focus_seq_trimmed[:]
for pos,wt_aa,mut_aa in mutant_tuple_list:
mut_seq[self.uniprot_focus_col_to_focus_idx[pos]] = mut_aa
if self.wt_elbo == None:
mutant_sequences = [self.focus_seq_trimmed, mut_seq]
else:
mutant_sequences = [mut_seq]
# Then make the one hot sequence
mutant_sequences_one_hot = np.zeros(\
(len(mutant_sequences),len(self.focus_cols),len(self.alphabet)))
for i,sequence in enumerate(mutant_sequences):
for j,letter in enumerate(sequence):
k = self.aa_dict[letter]
mutant_sequences_one_hot[i,j,k] = 1.0
prediction_matrix = np.zeros((mutant_sequences_one_hot.shape[0],N_pred_iterations))
idx_batch = np.arange(mutant_sequences_one_hot.shape[0])
for i in range(N_pred_iterations):
batch_preds, _, _ = model.all_likelihood_components(mutant_sequences_one_hot)
prediction_matrix[:,i] = batch_preds
# Then take the mean of all my elbo samples
mean_elbos = np.mean(prediction_matrix, axis=1).flatten().tolist()
if self.wt_elbo == None:
self.wt_elbo = mean_elbos.pop(0)
return mean_elbos[0] - self.wt_elbo
def single_mutant_matrix(self, model, N_pred_iterations=10, \
minibatch_size=2000, filename_prefix=""):
""" Predict the delta elbo for all single mutants """
# Get the start and end index from the sequence name
start_idx, end_idx = self.focus_seq_name.split("/")[-1].split("-")
start_idx = int(start_idx)
wt_pos_focus_idx_tuple_list = []
focus_seq_index = 0
focus_seq_list = []
for i,letter in enumerate(self.focus_seq):
if letter == letter.upper():
wt_pos_focus_idx_tuple_list.append((letter,start_idx+i,focus_seq_index))
focus_seq_index += 1
self.mutant_sequences = ["".join(self.focus_seq_trimmed)]
self.mutant_sequences_descriptor = ["wt"]
for wt,pos,idx_focus in wt_pos_focus_idx_tuple_list:
for mut in self.alphabet:
if wt != mut:
# Make a descriptor
descriptor = wt+str(pos)+mut
# Hard copy the sequence
focus_seq_copy = list(self.focus_seq_trimmed)[:]
# Mutate
focus_seq_copy[idx_focus] = mut
# Add to the list
self.mutant_sequences.append("".join(focus_seq_copy))
self.mutant_sequences_descriptor.append(descriptor)
# Then make the one hot sequence
self.mutant_sequences_one_hot = np.zeros(\
(len(self.mutant_sequences),len(self.focus_cols),len(self.alphabet)))
for i,sequence in enumerate(self.mutant_sequences):
for j,letter in enumerate(sequence):
k = self.aa_dict[letter]
self.mutant_sequences_one_hot[i,j,k] = 1.0
self.prediction_matrix = np.zeros((self.mutant_sequences_one_hot.shape[0],N_pred_iterations))
batch_order = np.arange(self.mutant_sequences_one_hot.shape[0])
for i in range(N_pred_iterations):
np.random.shuffle(batch_order)
for j in range(0,self.mutant_sequences_one_hot.shape[0],minibatch_size):
batch_index = batch_order[j:j+minibatch_size]
batch_preds, _, _ = model.all_likelihood_components(self.mutant_sequences_one_hot[batch_index])
for k,idx_batch in enumerate(batch_index.tolist()):
self.prediction_matrix[idx_batch][i]= batch_preds[k]
# Then take the mean of all my elbo samples
self.mean_elbos = np.mean(self.prediction_matrix, axis=1).flatten().tolist()
self.wt_elbo = self.mean_elbos.pop(0)
self.mutant_sequences_descriptor.pop(0)
self.delta_elbos = np.asarray(self.mean_elbos) - self.wt_elbo
if filename_prefix == "":
return self.mutant_sequences_descriptor, self.delta_elbos
else:
OUTPUT = open(filename_prefix+"_samples-"+str(N_pred_iterations)\
+"_elbo_predictions.csv", "w")
for i,descriptor in enumerate(self.mutant_sequences_descriptor):
OUTPUT.write(descriptor+";"+str(self.mean_elbos[i])+"\n")
OUTPUT.close()
def custom_mutant_matrix(self, input_filename, model, N_pred_iterations=10, \
minibatch_size=2000, filename_prefix="", offset=0):
""" Predict the delta elbo for a custom mutation filename
"""
# Get the start and end index from the sequence name
start_idx, end_idx = self.focus_seq_name.split("/")[-1].split("-")
start_idx = int(start_idx)
wt_pos_focus_idx_tuple_list = []
focus_seq_index = 0
focus_seq_list = []
mutant_to_letter_pos_idx_focus_list = {}
# find all possible valid mutations that can be run with this alignment
for i,letter in enumerate(self.focus_seq):
if letter == letter.upper():
for mut in self.alphabet:
pos = start_idx+i
if letter != mut:
mutant = letter+str(pos)+mut
mutant_to_letter_pos_idx_focus_list[mutant] = [letter,start_idx+i,focus_seq_index]
focus_seq_index += 1
self.mutant_sequences = ["".join(self.focus_seq_trimmed)]
self.mutant_sequences_descriptor = ["wt"]
# run through the input file
INPUT = open(self.working_dir+"/"+input_filename, "r")
for i,line in enumerate(INPUT):
line = line.rstrip()
if i >= 1:
line_list = line.split(",")
# generate the list of mutants
mutant_list = line_list[0].split(":")
valid_mutant = True
# if any of the mutants in this list aren"t in the focus sequence,
# I cannot make a prediction
for mutant in mutant_list:
if mutant not in mutant_to_letter_pos_idx_focus_list:
valid_mutant = False
# If it is a valid mutant, add it to my list to make preditions
if valid_mutant:
focus_seq_copy = list(self.focus_seq_trimmed)[:]
for mutant in mutant_list:
wt_aa,pos,idx_focus = mutant_to_letter_pos_idx_focus_list[mutant]
mut_aa = mutant[-1]
focus_seq_copy[idx_focus] = mut_aa
self.mutant_sequences.append("".join(focus_seq_copy))
self.mutant_sequences_descriptor.append(":".join(mutant_list))
INPUT.close()
# Then make the one hot sequence
self.mutant_sequences_one_hot = np.zeros(\
(len(self.mutant_sequences),len(self.focus_cols),len(self.alphabet)))
for i,sequence in enumerate(self.mutant_sequences):
for j,letter in enumerate(sequence):
k = self.aa_dict[letter]
self.mutant_sequences_one_hot[i,j,k] = 1.0
self.prediction_matrix = np.zeros((self.mutant_sequences_one_hot.shape[0],N_pred_iterations))
batch_order = np.arange(self.mutant_sequences_one_hot.shape[0])
for i in range(N_pred_iterations):
np.random.shuffle(batch_order)
for j in range(0,self.mutant_sequences_one_hot.shape[0],minibatch_size):
batch_index = batch_order[j:j+minibatch_size]
batch_preds, _, _ = model.all_likelihood_components(self.mutant_sequences_one_hot[batch_index])
for k,idx_batch in enumerate(batch_index.tolist()):
self.prediction_matrix[idx_batch][i]= batch_preds[k]
# Then take the mean of all my elbo samples
self.mean_elbos = np.mean(self.prediction_matrix, axis=1).flatten().tolist()
self.wt_elbo = self.mean_elbos.pop(0)
self.mutant_sequences_descriptor.pop(0)
self.delta_elbos = np.asarray(self.mean_elbos) - self.wt_elbo
if filename_prefix == "":
return self.mutant_sequences_descriptor, self.delta_elbos
else:
OUTPUT = open(filename_prefix+"_samples-"+str(N_pred_iterations)\
+"_elbo_predictions.csv", "w")
for i,descriptor in enumerate(self.mutant_sequences_descriptor):
OUTPUT.write(descriptor+";"+str(self.delta_elbos[i])+"\n")
OUTPUT.close()
def mutation_file_to_onehot(self, input_filename):
""" Predict the delta elbo for a custom mutation filename
"""
# Get the start and end index from the sequence name
start_idx, end_idx = self.focus_seq_name.split("/")[-1].split("-")
start_idx = int(start_idx)
wt_pos_focus_idx_tuple_list = []
focus_seq_index = 0
focus_seq_list = []
mutant_to_letter_pos_idx_focus_list = {}
# find all possible valid mutations that can be run with this alignment
for i, letter in enumerate(self.focus_seq):
if letter == letter.upper():
for mut in self.alphabet:
pos = start_idx + i
if letter != mut:
mutant = letter + str(pos) + mut
mutant_to_letter_pos_idx_focus_list[mutant] = [letter, start_idx + i, focus_seq_index]
focus_seq_index += 1
self.mutant_sequences = ["".join(self.focus_seq_trimmed)]
self.mutant_sequences_descriptor = ["wt"]
# run through the input file
INPUT = open(input_filename, "r")
for i, line in enumerate(INPUT):
line = line.rstrip()
if i >= 1:
line_list = line.split(",")
# generate the list of mutants
mutant_list = line_list[0].split(":")
valid_mutant = True
# if any of the mutants in this list aren"t in the focus sequence,
# I cannot make a prediction
for mutant in mutant_list:
if mutant not in mutant_to_letter_pos_idx_focus_list:
valid_mutant = False
# If it is a valid mutant, add it to my list to make preditions
if valid_mutant:
focus_seq_copy = list(self.focus_seq_trimmed)[:]
for mutant in mutant_list:
wt_aa, pos, idx_focus = mutant_to_letter_pos_idx_focus_list[mutant]
mut_aa = mutant[-1]
focus_seq_copy[idx_focus] = mut_aa
self.mutant_sequences.append("".join(focus_seq_copy))
self.mutant_sequences_descriptor.append(":".join(mutant_list))
INPUT.close()
# Then make the one hot sequence
self.mutant_sequences_one_hot = np.zeros( \
(len(self.mutant_sequences), len(self.focus_cols), len(self.alphabet)))
for i, sequence in enumerate(self.mutant_sequences):
for j, letter in enumerate(sequence):
k = self.aa_dict[letter]
self.mutant_sequences_one_hot[i, j, k] = 1.0
def pred_from_onehot(self, model, N_pred_iterations=10, minibatch_size=2000, filename_prefix="", offset=0):
self.prediction_matrix = np.zeros((self.mutant_sequences_one_hot.shape[0], N_pred_iterations))
tmpr_mutant_sequences_descriptor = self.mutant_sequences_descriptor[:]
batch_order = np.arange(self.mutant_sequences_one_hot.shape[0])
for i in range(N_pred_iterations):
# print("i" + str(i))
np.random.shuffle(batch_order)
for j in range(0, self.mutant_sequences_one_hot.shape[0], minibatch_size):
# print(j)
batch_index = batch_order[j:j + minibatch_size]
# print(self.mutant_sequences_one_hot[batch_index].shape)
batch = self.mutant_sequences_one_hot[batch_index]
batch = batch.reshape(-1, self.alphabet_size * self.seq_len)
# print(batch.shape)
batch = torch.Tensor(batch).to(device)
mu, logsigma, _, logpx_z, z = model(batch)[0:5]
batch_preds = ELBO_no_mean(logpx_z, mu, logsigma, z, 1.0)
# print(batch_preds)
batch_preds_numpy = batch_preds.cpu().numpy()
# print(batch_index.shape)
for k, idx_batch in enumerate(batch_index.tolist()):
self.prediction_matrix[idx_batch][i] = batch_preds_numpy[k]
# Then take the mean of all my elbo samples
self.mean_elbos = np.mean(self.prediction_matrix, axis=1).flatten().tolist()
self.wt_elbo = self.mean_elbos.pop(0)
tmpr_mutant_sequences_descriptor.pop(0)
self.delta_elbos = np.asarray(self.mean_elbos) - self.wt_elbo
if filename_prefix == "":
return tmpr_mutant_sequences_descriptor, self.delta_elbos
else:
OUTPUT = open(filename_prefix + "_samples-" + str(N_pred_iterations) \
+ "_elbo_predictions.csv", "w")
for i, descriptor in enumerate(tmpr_mutant_sequences_descriptor):
OUTPUT.write(descriptor + ";" + str(self.delta_elbos[i]) + "\n")
OUTPUT.close()
return tmpr_mutant_sequences_descriptor, self.delta_elbos
def pred_from_onehot_ensemble(self, model, saved_models, N_pred_iterations=10, minibatch_size=2000, filename_prefix="", offset=0):
self.prediction_matrix = np.zeros((self.mutant_sequences_one_hot.shape[0], N_pred_iterations))
tmpr_mutant_sequences_descriptor = self.mutant_sequences_descriptor[:]
batch_order = np.arange(self.mutant_sequences_one_hot.shape[0])
for i in range(N_pred_iterations):
# print("i" + str(i))
np.random.shuffle(batch_order)
for j in range(0, self.mutant_sequences_one_hot.shape[0], minibatch_size):
# print(j)
batch_index = batch_order[j:j + minibatch_size]
# print(self.mutant_sequences_one_hot[batch_index].shape)
batch = self.mutant_sequences_one_hot[batch_index]
batch = batch.reshape(-1, self.alphabet_size * self.seq_len)
# print(batch.shape)
batch = torch.Tensor(batch).to(device)
preds = []
for best_str in saved_models:
model.load_state_dict(torch.load(opt.saving_path + best_str)['model'])
mu, logsigma, _, logpx_z, z, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = model(batch)
batch_preds = ELBO_no_mean(logpx_z, mu, logsigma, z, 1.0)
# print(batch_preds)
preds.append(batch_preds.cpu().numpy())
# print(batch_index.shape)
batch_preds_numpy = np.mean(preds, 0)
for k, idx_batch in enumerate(batch_index.tolist()):
self.prediction_matrix[idx_batch][i] = batch_preds_numpy[k]
# Then take the mean of all my elbo samples
self.mean_elbos = np.mean(self.prediction_matrix, axis=1).flatten().tolist()
self.wt_elbo = self.mean_elbos.pop(0)
tmpr_mutant_sequences_descriptor.pop(0)
self.delta_elbos = np.asarray(self.mean_elbos) - self.wt_elbo
if filename_prefix == "":
return tmpr_mutant_sequences_descriptor, self.delta_elbos
else:
OUTPUT = open(filename_prefix + "_samples-" + str(N_pred_iterations) \
+ "_elbo_predictions.csv", "w")
for i, descriptor in enumerate(tmpr_mutant_sequences_descriptor):
OUTPUT.write(descriptor + ";" + str(self.delta_elbos[i]) + "\n")
OUTPUT.close()
return tmpr_mutant_sequences_descriptor, self.delta_elbos
def custom_mutant_matrix_pytorch(self, input_filename, model, N_pred_iterations=10, \
minibatch_size=2000, filename_prefix="", offset=0):
""" Predict the delta elbo for a custom mutation filename
"""
# Get the start and end index from the sequence name
start_idx, end_idx = self.focus_seq_name.split("/")[-1].split("-")
start_idx = int(start_idx)
wt_pos_focus_idx_tuple_list = []
focus_seq_index = 0
focus_seq_list = []
mutant_to_letter_pos_idx_focus_list = {}
# find all possible valid mutations that can be run with this alignment
for i,letter in enumerate(self.focus_seq):
if letter == letter.upper():
for mut in self.alphabet:
pos = start_idx+i
if letter != mut:
mutant = letter+str(pos)+mut
mutant_to_letter_pos_idx_focus_list[mutant] = [letter,start_idx+i,focus_seq_index]
focus_seq_index += 1
self.mutant_sequences = ["".join(self.focus_seq_trimmed)]
self.mutant_sequences_descriptor = ["wt"]
# run through the input file
INPUT = open(input_filename, "r")
for i,line in enumerate(INPUT):
line = line.rstrip()
if i >= 1:
line_list = line.split(",")
# generate the list of mutants
mutant_list = line_list[0].split(":")
valid_mutant = True
# if any of the mutants in this list aren"t in the focus sequence,
# I cannot make a prediction
for mutant in mutant_list:
if mutant not in mutant_to_letter_pos_idx_focus_list:
valid_mutant = False
# If it is a valid mutant, add it to my list to make preditions
if valid_mutant:
focus_seq_copy = list(self.focus_seq_trimmed)[:]
for mutant in mutant_list:
wt_aa,pos,idx_focus = mutant_to_letter_pos_idx_focus_list[mutant]
mut_aa = mutant[-1]
focus_seq_copy[idx_focus] = mut_aa
self.mutant_sequences.append("".join(focus_seq_copy))
self.mutant_sequences_descriptor.append(":".join(mutant_list))
INPUT.close()
# Then make the one hot sequence
self.mutant_sequences_one_hot = np.zeros(\
(len(self.mutant_sequences),len(self.focus_cols),len(self.alphabet)))
for i,sequence in enumerate(self.mutant_sequences):
for j,letter in enumerate(sequence):
k = self.aa_dict[letter]
self.mutant_sequences_one_hot[i,j,k] = 1.0
self.prediction_matrix = np.zeros((self.mutant_sequences_one_hot.shape[0],N_pred_iterations))
batch_order = np.arange(self.mutant_sequences_one_hot.shape[0])
for i in range(N_pred_iterations):
# print("i" + str(i))
np.random.shuffle(batch_order)
for j in range(0,self.mutant_sequences_one_hot.shape[0],minibatch_size):
# print(j)
batch_index = batch_order[j:j+minibatch_size]
# print(self.mutant_sequences_one_hot[batch_index].shape)
batch = self.mutant_sequences_one_hot[batch_index]
batch = batch.reshape(-1, self.alphabet_size*self.seq_len)
# print(batch.shape)
batch = torch.Tensor(batch).to(device)
mu, logsigma, _, logpx_z, z, _, _, _, _, _, _, _, _, _, _ ,_,_,_,_,_,_,_,_= model(batch)
batch_preds = ELBO_no_mean(logpx_z, mu, logsigma, z, 1.0)
# print(batch_preds)
batch_preds_numpy = batch_preds.cpu().numpy()
# print(batch_index.shape)
for k,idx_batch in enumerate(batch_index.tolist()):
self.prediction_matrix[idx_batch][i]= batch_preds_numpy[k]
# Then take the mean of all my elbo samples
self.mean_elbos = np.mean(self.prediction_matrix, axis=1).flatten().tolist()
self.wt_elbo = self.mean_elbos.pop(0)
self.mutant_sequences_descriptor.pop(0)
self.delta_elbos = np.asarray(self.mean_elbos) - self.wt_elbo
if filename_prefix == "":
return self.mutant_sequences_descriptor, self.delta_elbos
else:
OUTPUT = open(filename_prefix+"_samples-"+str(N_pred_iterations)\
+"_elbo_predictions.csv", "w")
for i,descriptor in enumerate(self.mutant_sequences_descriptor):
OUTPUT.write(descriptor+";"+str(self.delta_elbos[i])+"\n")
OUTPUT.close()
return self.mutant_sequences_descriptor, self.delta_elbos
def get_pattern_activations(self, model, update_num, filename_prefix="",
verbose=False, minibatch_size=2000):
activations_filename = self.working_dir+"/embeddings/"+filename_prefix+"_pattern_activations.csv"
OUTPUT = open(activations_filename, "w")
batch_order = np.arange(len(self.x_train_name_list))
for i in range(0,len(self.x_train_name_list),minibatch_size):
batch_index = batch_order[i:i+minibatch_size]
one_hot_seqs = self.x_train[batch_index]
batch_activation = model.get_pattern_activations(one_hot_seqs)
for j,idx in enumerate(batch_index.tolist()):
sample_activation = [str(val) for val in batch_activation[j].tolist()]
sample_name = self.x_train_name_list[idx]
out_line = [str(update_num),sample_name]+sample_activation
if verbose:
print ("\t".join(out_line))
OUTPUT.write(",".join(out_line)+"\n")
OUTPUT.close()
def get_embeddings(self, model, update_num, filename_prefix="",
verbose=False, minibatch_size=2000):
""" Save the latent variables from all the sequences in the alignment """
embedding_filename = self.working_dir+"/embeddings/"+filename_prefix+"_seq_embeddings.csv"
# Append embeddings to file if it has already been created
# This is useful if you want to see the embeddings evolve over time
if os.path.isfile(embedding_filename):
OUTPUT = open(embedding_filename, "a")
else:
OUTPUT = open(embedding_filename, "w")
mu_header_list = ["mu_"+str(i+1) for i in range(model.n_latent)]
log_sigma_header_list = ["log_sigma_"+str(i+1) for i in range(model.n_latent)]
header_list = mu_header_list + log_sigma_header_list
OUTPUT.write("update_num,name,"+",".join(header_list)+"\n")
batch_order = np.arange(len(self.x_train_name_list))
for i in range(0,len(self.x_train_name_list),minibatch_size):
batch_index = batch_order[i:i+minibatch_size]
one_hot_seqs = self.x_train[batch_index]
batch_mu, batch_log_sigma = model.recognize(one_hot_seqs)
for j,idx in enumerate(batch_index.tolist()):
sample_mu = [str(val) for val in batch_mu[j].tolist()]
sample_log_sigma = [str(val) for val in batch_log_sigma[j].tolist()]
sample_name = self.x_train_name_list[idx]
out_line = [str(update_num),sample_name]+sample_mu+sample_log_sigma
if verbose:
print ("\t".join(out_line))
OUTPUT.write(",".join(out_line)+"\n")
OUTPUT.close()
def get_elbo_samples(self, model, N_pred_iterations=100, minibatch_size=2000):
self.prediction_matrix = np.zeros((self.one_hot_mut_array_with_wt.shape[0],N_pred_iterations))
batch_order = np.arange(self.one_hot_mut_array_with_wt.shape[0])
for i in range(N_pred_iterations):
np.random.shuffle(batch_order)
for j in range(0,self.one_hot_mut_array_with_wt.shape[0],minibatch_size):
batch_index = batch_order[j:j+minibatch_size]
batch_preds, _, _ = model.all_likelihood_components(self.one_hot_mut_array_with_wt[batch_index])
for k,idx_batch in enumerate(batch_index.tolist()):
self.prediction_matrix[idx_batch][i]= batch_preds[k]
def gen_job_string(data_params, model_params):
"""
Generates a unique job string given data and model parameters.
This is used later as an identifier for the
saved model weights and figures
Parameters
------------
data_params: dictionary of parameters for the data class
model_params: dictionary of parameters for the model class
Returns
------------
job string denoting parameters of run
"""
written_out_vals = ["n_latent"]
layer_num_list = ["zero","one","two","three","four"]
encoder_architecture = []
decoder_architecture = []
for layer_num in layer_num_list:
if "encode_dim_"+layer_num in model_params:
encoder_architecture.append(model_params["encode_dim_"+layer_num])
if "decode_dim_"+layer_num in model_params:
decoder_architecture.append(model_params["decode_dim_"+layer_num])
written_out_vals += ["encode_dim_"+layer_num, "decode_dim_"+layer_num]
n_latent = model_params["n_latent"]
encoder_architecture_str = "-".join([str(size) for size in encoder_architecture])
decoder_architecture_str = "-".join([str(size) for size in decoder_architecture])
job_str = "vae_output_encoder-"+encoder_architecture_str+"_Nlatent-"+str(n_latent)\
+"_decoder-"+decoder_architecture_str
job_id_list = []
for data_id,data_val in sorted(data_params.items()):
if data_id not in written_out_vals:
if str(type(data_val)) == "<type 'list'>":