-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpytorch_tutorial.py
More file actions
793 lines (621 loc) · 25.5 KB
/
Copy pathpytorch_tutorial.py
File metadata and controls
793 lines (621 loc) · 25.5 KB
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
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import random
import torch
from torch import nn
from torch.utils.data import Dataset, DataLoader
from Bio import SeqIO
from torch import Tensor
from sklearn.feature_extraction.text import CountVectorizer
from itertools import chain, repeat, islice
def pad_infinite(iterable, padding=None):
return chain(iterable, repeat(padding))
def pad(iterable, size, padding=None):
return islice(pad_infinite(iterable, padding), size)
def one_hot_encode(seq):
"""
Given a DNA sequence, return its one-hot encoding
"""
# Make sure seq has only allowed bases
allowed = set("ACTGN")
if not set(seq).issubset(allowed):
invalid = set(seq) - allowed
raise ValueError(f"Sequence contains chars not in allowed DNA alphabet (ACGTN): {invalid}")
# Dictionary returning one-hot encoding for each nucleotide
nuc_d = {'A':[1.0,0.0,0.0,0.0],
'C':[0.0,1.0,0.0,0.0],
'G':[0.0,0.0,1.0,0.0],
'T':[0.0,0.0,0.0,1.0],
'N':[0.0,0.0,0.0,0.0]}
# Create array from nucleotide sequence
vec=np.array([nuc_d[x] for x in seq])
return vec
def quick_split(df, split_frac=0.8, verbose=False):
'''
Given a df of samples, randomly split indices between
train and test at the desired fraction
'''
cols = df.columns # original columns, use to clean up reindexed cols
df = df.reset_index()
# shuffle indices
idxs = list(range(df.shape[0]))
random.shuffle(idxs)
# split shuffled index list by split_frac
split = int(len(idxs)*split_frac)
train_idxs = idxs[:split]
test_idxs = idxs[split:]
# split dfs and return
train_df = df[df.index.isin(train_idxs)]
test_df = df[df.index.isin(test_idxs)]
return train_df[cols], test_df[cols]
def getKmers(sequence, size=8):
return [sequence[x:x+size].lower() for x in range(len(sequence) - size + 1)]
incorrect_filename = '/home/katie/Documents/misclassified_read_classifier/misclassified_ba_2k.fasta'
correct_filename = '/home/katie/Documents/misclassified_read_classifier/correct_BA_2k.fasta'
def load_data_make_kmers(correctly_assigned_reads_filename, incorrectly_assigned_reads_filename, k=6, vectorize=True):
records_correct = list(SeqIO.parse(correctly_assigned_reads_filename, "fasta"))
records_incorrect = list(SeqIO.parse(incorrectly_assigned_reads_filename, "fasta"))
i = 0
#sequences = np.array(['' for i in range(len(records))])
input_data = ['' for i in range(len(records_correct) + len(records_incorrect))]
for record in records_correct:
input_data[i] = str(record.seq)
i += 1
for record in records_incorrect:
input_data[i] = str(record.seq)
i += 1
labels = Tensor([1 for j in range(len(records_correct))]+ [0 for k in range(len(records_incorrect))])
input_df = pd.DataFrame({"Sequence": input_data, "Class": labels})
input_df['words']=input_df.apply(lambda x: getKmers(x['Sequence'],size=k), axis=1)
input_df["lengths"] = input_df["Sequence"].astype(str).map(len)
max_len = input_df["lengths"].max()
seqs = list(input_df['words'].values)
for i, seq in enumerate(seqs):
seq = list(pad(seq, max_len, 'N'*k))
seqs[i] = seq
input_df['words'] = seqs
return input_df
class CountVectorizerDataset(Dataset):
'''
Dataset for one-hot-encoded sequences
'''
def __init__(self,
df,
seq_col='Sequence',
target_col='Class'
):
# +--------------------+
# | Get the X examples |
# +--------------------+
# extract the DNA from the appropriate column in the df
self.seqs = list(df[seq_col].values)
self.seq_len = len(self.seqs[0])
# words is list of kmers
texts = list(df['words'])
for item in range(len(texts)):
texts[item] = ' '.join(texts[item])
cv = CountVectorizer(ngram_range=(4,4)) #The n-gram size of 4 is previously determined by testing
X = cv.fit_transform(texts)
# vectorize sequences, then stack in a torch tensor
# https://stackoverflow.com/questions/50665141/converting-a-scipy-coo-matrix-to-pytorch-sparse-tensor
cvs_seqs = torch.sparse_coo_tensor(X.nonzero(), X.data, X.shape)
self.cvs_seqs = cvs_seqs.to_dense()
# +------------------+
# | Get the Y labels |
# +------------------+
self.labels = torch.tensor(list(df[target_col].values)).unsqueeze(1)
def __len__(self): return len(self.seqs)
def __getitem__(self,idx):
# Given an index, return a tuple of an X with it's associated Y
# This is called inside DataLoader
seq = self.cvs_seqs[idx]
label = self.labels[idx]
return seq, label
# https://gene46100.hakyimlab.org/post/2025-03-25-unit00/updated-basic_dna_tutorial
class SeqDatasetOHE(Dataset):
'''
Dataset for one-hot-encoded sequences
'''
def __init__(self,
df,
seq_col='words',
target_col='Class'
):
# +--------------------+
# | Get the X examples |
# +--------------------+
# extract the DNA from the appropriate column in the df
self.seqs = df[seq_col]
# one-hot encode sequences, then stack in a torch tensor
self.ohe_seqs = torch.stack([torch.tensor(one_hot_encode(x)) for x in self.seqs])
# +------------------+
# | Get the Y labels |
# +------------------+
self.labels = torch.tensor(list(df[target_col].values)).unsqueeze(1)
def __len__(self): return len(self.seqs)
def __getitem__(self,idx):
# Given an index, return a tuple of an X with it's associated Y
# This is called inside DataLoader
seq = self.ohe_seqs[idx]
label = self.labels[idx]
return seq, label
def build_dataloaders(train_df,
test_df,
seq_col='Sequence',
target_col='Class',
batch_size=128,
shuffle=True,
dataset_type="CV"
):
'''
Given a train and test df with some batch construction
details, put them into custom SeqDatasetOHE() objects.
Give the Datasets to the DataLoaders and return.
'''
if dataset_type == "CV":
# create Datasets
train_ds = CountVectorizerDataset(train_df,seq_col=seq_col,target_col=target_col)
test_ds = CountVectorizerDataset(test_df,seq_col=seq_col,target_col=target_col)
# Put DataSets into DataLoaders
train_dl = DataLoader(train_ds, batch_size=batch_size, shuffle=shuffle)
test_dl = DataLoader(test_ds, batch_size=batch_size)
else:
# create Datasets
train_ds = SeqDatasetOHE(train_df,seq_col=seq_col,target_col=target_col)
test_ds = SeqDatasetOHE(test_df,seq_col=seq_col,target_col=target_col)
# Put DataSets into DataLoaders
train_dl = DataLoader(train_ds, batch_size=batch_size, shuffle=shuffle)
test_dl = DataLoader(test_ds, batch_size=batch_size)
return train_dl,test_dl
full_df = load_data_make_kmers(correct_filename, incorrect_filename)
train_df, val_df = quick_split(full_df)
train_dl_cv, val_dl_cv = build_dataloaders(train_df, val_df)
train_dl_ohs, val_dl_ohs = build_dataloaders(train_df, val_df, dataset_type="OHS")
# in tutorial- they have same seq len everywher,e i have padded so all same length
# get the sequence length from the first seq in the df
# seq_len = len(train_df['seq'].values[0])
# very simple linear model
class DNA_Linear(nn.Module):
def __init__(self, seq_len):
super().__init__()
self.seq_len = seq_len
# the 4 is for our one-hot encoded vector length 4!
self.lin = nn.Linear(4*seq_len, 1)
def forward(self, xb):
# reshape to flatten sequence dimension
xb = xb.view(xb.shape[0],self.seq_len*4)
# Linear wraps up the weights/bias dot product operations
out = self.lin(xb)
return out
# basic CNN model
class DNA_CNN(nn.Module):
def __init__(self,
seq_len,
num_filters=32,
kernel_size=3):
super().__init__()
self.seq_len = seq_len
self.conv_net = nn.Sequential(
# 4 is for the 4 nucleotides
nn.Conv1d(4, num_filters, kernel_size=kernel_size),
nn.ReLU(inplace=True),
nn.Flatten(),
nn.Linear(num_filters*(seq_len-kernel_size+1), 1)
)
def forward(self, xb):
# reshape view to batch_size x 4channel x seq_len
# permute to put channel in correct order
xb = xb.permute(0,2,1)
#print(xb.shape)
out = self.conv_net(xb)
return out
# +--------------------------------+
# | Training and fitting functions |
# +--------------------------------+
def loss_batch(model, loss_func, xb, yb, opt=None,verbose=False):
'''
Apply loss function to a batch of inputs. If no optimizer
is provided, skip the back prop step.
'''
if verbose:
print('loss batch ****')
print("xb shape:",xb.shape)
print("yb shape:",yb.shape)
print("yb shape:",yb.squeeze(1).shape)
#print("yb",yb)
# get the batch output from the model given your input batch
# ** This is the model's prediction for the y labels! **
xb_out = model(xb.float())
if verbose:
print("model out pre loss", xb_out.shape)
#print('xb_out', xb_out)
print("xb_out:",xb_out.shape)
print("yb:",yb.shape)
print("yb.long:",yb.long().shape)
loss = loss_func(xb_out, yb.float()) # for MSE/regression
# __FOOTNOTE 2__
if opt is not None: # if opt
loss.backward()
opt.step()
opt.zero_grad()
return loss.item(), len(xb)
def train_step(model, train_dl, loss_func, device, opt):
'''
Execute 1 set of batched training within an epoch
'''
# Set model to Training mode
model.train()
tl = [] # train losses
ns = [] # batch sizes, n
# loop through train DataLoader
for xb, yb in train_dl:
# put on GPU
xb, yb = xb.to(device),yb.to(device)
# provide opt so backprop happens
t, n = loss_batch(model, loss_func, xb, yb, opt=opt)
# collect train loss and batch sizes
tl.append(t)
ns.append(n)
# average the losses over all batches
train_loss = np.sum(np.multiply(tl, ns)) / np.sum(ns)
return train_loss
def val_step(model, val_dl, loss_func, device):
'''
Execute 1 set of batched validation within an epoch
'''
# Set model to Evaluation mode
model.eval()
with torch.no_grad():
vl = [] # val losses
ns = [] # batch sizes, n
# loop through validation DataLoader
for xb, yb in val_dl:
# put on GPU
xb, yb = xb.to(device),yb.to(device)
# Do NOT provide opt here, so backprop does not happen
v, n = loss_batch(model, loss_func, xb, yb)
# collect val loss and batch sizes
vl.append(v)
ns.append(n)
# average the losses over all batches
val_loss = np.sum(np.multiply(vl, ns)) / np.sum(ns)
return val_loss
def fit(epochs, model, loss_func, opt, train_dl, val_dl,device,patience=1000):
'''
Fit the model params to the training data, eval on unseen data.
Loop for a number of epochs and keep train of train and val losses
along the way
'''
# keep track of losses
train_losses = []
val_losses = []
# loop through epochs
for epoch in range(epochs):
# take a training step
# fails in t, n = loss_batch(model, loss_func, xb, yb, opt=opt, verbose="True")
# at line xb_out = model(xb.float())
# xb is from dataloader
# xb = xb.view(xb.shape[0],self.seq_len*4)
# for CV
# RuntimeError: shape '[128, 13844]' is invalid for input of size 32834176
train_loss = train_step(model,train_dl,loss_func,device,opt)
train_losses.append(train_loss)
# take a validation step
val_loss = val_step(model,val_dl,loss_func,device)
val_losses.append(val_loss)
print(f"E{epoch} | train loss: {train_loss:.3f} | val loss: {val_loss:.3f}")
return train_losses, val_losses
def run_model(train_dl,val_dl,model,device,
lr=0.01, epochs=50,
lossf=None,opt=None
):
'''
Given train and val DataLoaders and a NN model, fit the mode to the training
data. By default, use MSE loss and an SGD optimizer
'''
# define optimizer
if opt:
optimizer = opt
else: # if no opt provided, just use SGD
optimizer = torch.optim.SGD(model.parameters(), lr=lr)
# define loss function
if lossf:
loss_func = lossf
else: # if no loss function provided, just use MSE
loss_func = torch.nn.MSELoss()
# run the training loop
train_losses, val_losses = fit(
epochs,
model,
loss_func,
optimizer,
train_dl,
val_dl,
device)
return train_losses, val_losses
# get the sequence length from the first seq in the df
seq_len = len(train_df['words'].values[0])
device = (
"cuda"
if torch.cuda.is_available()
else "mps"
if torch.backends.mps.is_available()
else "cpu"
)
print(f"Using {device} device")
# create Linear model object
model_lin = DNA_Linear(seq_len)
model_lin.to(device) # put on GPU
#incorrect_filename = '/home/katie/Documents/misclassified_read_classifier/misclassified_ba_2k.fasta'
#correct_filename = '/home/katie/Documents/misclassified_read_classifier/correct_BA_2k.fasta'
full_df = load_data_make_kmers(correct_filename, incorrect_filename)
train_df, val_df = quick_split(full_df)
train_dl_cv, val_dl_cv = build_dataloaders(train_df, val_df)
train_dl_ohs, val_dl_ohs = build_dataloaders(train_df, val_df, dataset_type="OHS")
for xb, yb in train_dl_cv:
# put on GPU
pass
# run the model with default settings!
lin_train_losses_cv, lin_val_losses_cv = run_model(
train_dl_cv,
val_dl_cv,
model_lin,
device
)
# RuntimeError: shape '[128, 13824]' is invalid for input of size 32834176
# xb.shape = torch.Size([128, 256517])
#
# run the model with default settings!
lin_train_losses_ohs, lin_val_losses_ohs = run_model(
train_dl_ohs,
val_dl_ohs,
model_lin,
device
)
# RuntimeError: shape '[128, 13824]' is invalid for input of size 10005504
def quick_loss_plot(data_label_list,loss_type="MSE Loss",sparse_n=0):
'''
For each train/test loss trajectory, plot loss by epoch
'''
for i,(train_data,test_data,label) in enumerate(data_label_list):
plt.plot(train_data,linestyle='--',color=f"C{i}", label=f"{label} Train")
plt.plot(test_data,color=f"C{i}", label=f"{label} Val",linewidth=3.0)
plt.legend()
plt.ylabel(loss_type)
plt.xlabel("Epoch")
plt.legend(bbox_to_anchor=(1,1),loc='upper left')
plt.show()
lin_data_label = (lin_train_losses,lin_val_losses,"Lin")
quick_loss_plot([lin_data_label])
seq_len = len(train_df['seq'].values[0])
# create Linear model object
model_cnn = DNA_CNN(seq_len)
model_cnn.to(device) # put on GPU
# run the model with default settings!
cnn_train_losses, cnn_val_losses = run_model(
train_dl,
val_dl,
model_cnn,
device
)
cnn_data_label = (cnn_train_losses,cnn_val_losses,"CNN")
quick_loss_plot([lin_data_label,cnn_data_label])
# oracle dict of true score for each seq
oracle = dict(mer8[['seq','score']].values)
def quick_seq_pred(model, desc, seqs, oracle):
'''
Given a model and some sequences, get the model's predictions
for those sequences and compare to the oracle (true) output
'''
print(f"__{desc}__")
for dna in seqs:
s = torch.tensor(one_hot_encode(dna)).unsqueeze(0).to(device)
pred = model(s.float())
actual = oracle[dna]
diff = pred.item() - actual
print(f"{dna}: pred:{pred.item():.3f} actual:{actual:.3f} ({diff:.3f})")
def quick_8mer_pred(model, oracle):
seqs1 = ("poly-X seqs",['AAAAAAAA', 'CCCCCCCC','GGGGGGGG','TTTTTTTT'])
seqs2 = ("other seqs", ['AACCAACA','CCGGTGAG','GGGTAAGG', 'TTTCGTTT'])
seqsTAT = ("with TAT motif", ['TATAAAAA','CCTATCCC','GTATGGGG','TTTATTTT'])
seqsGCG = ("with GCG motif", ['AAGCGAAA','CGCGCCCC','GGGCGGGG','TTGCGTTT'])
TATGCG = ("both TAT and GCG",['ATATGCGA','TGCGTATT'])
for desc,seqs in [seqs1, seqs2, seqsTAT, seqsGCG, TATGCG]:
quick_seq_pred(model, desc, seqs, oracle)
print()
# Ask the trained Linear model to make
# predictions for some 8-mers
quick_8mer_pred(model_lin, oracle)
#import altair as alt
from sklearn.metrics import r2_score
#import datapane as dp
def parity_plot(model_name,df,r2):
'''
Given a dataframe of samples with their true and predicted values,
make a scatterplot.
'''
plt.scatter(df['truth'].values, df['pred'].values, alpha=0.2)
# y=x line
xpoints = ypoints = plt.xlim()
plt.plot(xpoints, ypoints, linestyle='--', color='k', lw=2, scalex=False, scaley=False)
plt.ylim(xpoints)
plt.ylabel("Predicted Score",fontsize=14)
plt.xlabel("Actual Score",fontsize=14)
plt.title(f"{model_name} (r2:{r2:.3f})",fontsize=20)
plt.show()
# can'[t install datapanes in here
# def alt_parity_plot(model,df, r2,datapane=False):
# '''
# Make an interactive parity plot with altair
# '''
# chart = alt.Chart(df).mark_circle(size=100,opacity=0.4).encode(
# alt.X('truth:Q'),
# alt.Y('pred:Q'),
# tooltip=['seq:N']
# ).properties(
# title=f'{model} (r2:{r2:.3f})',
# ).interactive()
# chart.save(f'alt_out/parity_plot_{model}.html')
# display(chart)
# if datapane:
# report = dp.Report(dp.Plot(chart) ) #Create a report
# report.upload(name=f'dna_pytorch_tutorial_altair_{model}', open=True, visibility='PUBLIC') #Publish the report
def parity_pred(models, seqs, oracle):
'''Given some sequences, get the model's predictions '''
dfs = {} # key: model name, value: parity_df
for model_name,model in models:
print(f"Running {model_name}")
data = []
for dna in seqs:
s = torch.tensor(one_hot_encode(dna)).unsqueeze(0).to(device)
actual = oracle[dna]
pred = model(s.float())
data.append([dna,actual,pred.item()])
df = pd.DataFrame(data, columns=['seq','truth','pred'])
r2 = r2_score(df['truth'],df['pred'])
dfs[model_name] = (r2,df)
parity_plot(model_name, df, r2)
seqs = test_df['seq'].values
models = [
("Linear", model_lin),
("CNN", model_cnn)
]
# a perfect model will have x =y, off diagonals are errors in classification
parity_pred(models, seqs, oracle)
# CNN much better
def get_conv_layers_from_model(model):
'''
Given a trained model, extract its convolutional layers
'''
model_children = list(model.children())
# counter to keep count of the conv layers
model_weights = [] # we will save the conv layer weights in this list
conv_layers = [] # we will save the actual conv layers in this list
bias_weights = []
counter = 0
# append all the conv layers and their respective weights to the list
for i in range(len(model_children)):
# get model type of Conv1d
if type(model_children[i]) == nn.Conv1d:
counter += 1
model_weights.append(model_children[i].weight)
conv_layers.append(model_children[i])
bias_weights.append(model_children[i].bias)
# also check sequential objects' children for conv1d
elif type(model_children[i]) == nn.Sequential:
for child in model_children[i]:
if type(child) == nn.Conv1d:
counter += 1
model_weights.append(child.weight)
conv_layers.append(child)
bias_weights.append(child.bias)
print(f"Total convolutional layers: {counter}")
return conv_layers, model_weights, bias_weights
def view_filters(model_weights, num_cols=8):
model_weights = model_weights[0]
num_filt = model_weights.shape[0]
filt_width = model_weights[0].shape[1]
num_rows = int(np.ceil(num_filt/num_cols))
# visualize the first conv layer filters
plt.figure(figsize=(20, 17))
for i, filter in enumerate(model_weights):
ax = plt.subplot(num_rows, num_cols, i+1)
ax.imshow(filter.cpu().detach(), cmap='gray')
ax.set_yticks(np.arange(4))
ax.set_yticklabels(['A', 'C', 'G','T'])
ax.set_xticks(np.arange(filt_width))
ax.set_title(f"Filter {i}")
plt.tight_layout()
plt.show()
conv_layers, model_weights, bias_weights = get_conv_layers_from_model(model_cnn)
view_filters(model_weights)
## more ways to get insgiht into model:
def get_conv_output_for_seq(seq, conv_layer):
'''
Given an input sequeunce and a convolutional layer,
get the output tensor containing the conv filter
activations along each position in the sequence
'''
# format seq for input to conv layer (OHE, reshape)
seq = torch.tensor(one_hot_encode(seq)).unsqueeze(0).permute(0,2,1).to(DEVICE)
# run seq through conv layer
with torch.no_grad(): # don't want as part of gradient graph
# apply learned filters to input seq
res = conv_layer(seq.float())
return res[0]
def get_filter_activations(seqs, conv_layer,act_thresh=0):
'''
Given a set of input sequences and a trained convolutional layer,
determine the subsequences for which each filter in the conv layer
activate most strongly.
1.) Run seq inputs through conv layer.
2.) Loop through filter activations of the resulting tensor, saving the
position where filter activations were > act_thresh.
3.) Compile a count matrix for each filter by accumulating subsequences which
activate the filter above the threshold act_thresh
'''
# initialize dict of pwms for each filter in the conv layer
# pwm shape: 4 nucleotides X filter width, initialize to 0.0s
num_filters = conv_layer.out_channels
filt_width = conv_layer.kernel_size[0]
filter_pwms = dict((i,torch.zeros(4,filt_width)) for i in range(num_filters))
print("Num filters", num_filters)
print("filt_width", filt_width)
# loop through a set of sequences and collect subseqs where each filter activated
for seq in seqs:
# get a tensor of each conv filter activation along the input seq
res = get_conv_output_for_seq(seq, conv_layer)
# for each filter and it's activation vector
for filt_id,act_vec in enumerate(res):
# collect the indices where the activation level
# was above the threshold
act_idxs = torch.where(act_vec>act_thresh)[0]
activated_positions = [x.item() for x in act_idxs]
# use activated indicies to extract the actual DNA
# subsequences that caused filter to activate
for pos in activated_positions:
subseq = seq[pos:pos+filt_width]
#print("subseq",pos, subseq)
# transpose OHE to match PWM orientation
subseq_tensor = torch.tensor(one_hot_encode(subseq)).T
# add this subseq to the pwm count for this filter
filter_pwms[filt_id] += subseq_tensor
return filter_pwms
import logomaker
def view_filters_and_logos(model_weights,filter_activations, num_cols=8):
'''
Given some convolutional model weights and filter activation PWMs,
visualize the heatmap and motif logo pairs in a simple grid
'''
model_weights = model_weights[0].squeeze(1)
print(model_weights.shape)
# make sure the model weights agree with the number of filters
assert(model_weights.shape[0] == len(filter_activations))
num_filts = len(filter_activations)
num_rows = int(np.ceil(num_filts/num_cols))*2+1
# ^ not sure why +1 is needed... complained otherwise
plt.figure(figsize=(20, 17))
j=0 # use to make sure a filter and it's logo end up vertically paired
for i, filter in enumerate(model_weights):
if (i)%num_cols == 0:
j += num_cols
# display raw filter
ax1 = plt.subplot(num_rows, num_cols, i+j+1)
ax1.imshow(filter.cpu().detach(), cmap='gray')
ax1.set_yticks(np.arange(4))
ax1.set_yticklabels(['A', 'C', 'G','T'])
ax1.set_xticks(np.arange(model_weights.shape[2]))
ax1.set_title(f"Filter {i}")
# display sequence logo
ax2 = plt.subplot(num_rows, num_cols, i+j+1+num_cols)
filt_df = pd.DataFrame(filter_activations[i].T.numpy(),columns=['A','C','G','T'])
filt_df_info = logomaker.transform_matrix(filt_df,from_type='counts',to_type='information')
logo = logomaker.Logo(filt_df_info,ax=ax2)
ax2.set_ylim(0,2)
ax2.set_title(f"Filter {i}")
plt.tight_layout()
# just use some seqs from test_df to activate filters
some_seqs = random.choices(seqs, k=3000)
filter_activations = get_filter_activations(some_seqs, conv_layers[0])
view_filters_and_logos(model_weights,filter_activations)