Exercise: 1-D convolutional motif scanner
Goal
Implement a PWM motif scanner [scan a position weight matrix along DNA, producing a score at every position] as a 1-D convolution. Given:
one_hot: Tensor<B, 3>of shape[N, 4, L]— one-hot DNA from exercise 2pwm: Tensor<B, 3>of shape[M, 4, w]—Mmotifs, each a4 × wweight matrix
return a tensor of shape [N, M, L − w + 1] where entry (n, m, p) is the score of motif m at position p in sequence n.
By the time cargo test passes you will have used burn::tensor::module::conv1d and ConvOptions.
Background — PWM scanning is exactly 1-D conv
A PWM [position weight matrix — a [4, w] matrix where entry (b, p) is the log-odds score for seeing base b at relative position p] is the classical motif representation in bioinformatics (MEME, JASPAR).
Scanning a PWM along a DNA sequence means: at every position p along the genome, slide the PWM into place, look up the score for each (base, position) pair, sum them. That is exactly what a 1-D convolution computes:
\[ \mathrm{score}(n,\, m,\, p) \;=\; \sum_{b=0}^{3} \sum_{k=0}^{w-1} \mathrm{one\_hot}[n,\, b,\, p+k] \cdot \mathrm{PWM}[m,\, b,\, k] . \]
For each sample \(n\), each motif \(m\), and each valid starting position \(p\), we sum the products of the one-hot indicator and the PWM weight over the 4 bases × \(w\) positions of the window.
The PyTorch one-liner:
import torch.nn.functional as F
scores = F.conv1d(one_hot, pwm) # [N, M, L-w+1]The Burn version uses burn::tensor::module::conv1d — the functional-style conv that takes raw input and weight tensors (no nn::Conv1d module needed):
use burn::tensor::module::conv1d;
use burn::tensor::ops::ConvOptions;
let scores = conv1d(
one_hot, // [N, 4, L]
pwm, // [M, 4, w]
None, // no bias
ConvOptions::new([1], [0], [1], 1), // stride 1, no padding, no dilation, 1 group
);
// scores: [N, M, L - w + 1]This is the same operation that every modern sequence-ML model (DeepBind, DanQ, Basenji, Enformer) starts with. The historical PWM scan and the input layer of a 2020s genomics neural network are the same line of code.
The exercise
Open day11/ex-motif-scanner/. Implement:
fn scan_motifs<B: Backend>(
one_hot: Tensor<B, 3>,
pwm: Tensor<B, 3>,
) -> Tensor<B, 3>;so the tests pass. The starter includes a helper that builds a one-hot tensor from string sequences (reusing the code from exercise 2) and constructs a small handcrafted PWM for the motif "GATA".
You may not change the function signature or the tests.
How to run
cd day11/ex-motif-scanner
cargo test
cargo runWhen the body is right you will see:
test result: ok. 4 passed; 0 failed; 0 ignored; 0 measured
cargo run will print, for a small batch of sequences, the score of the GATA motif at every position — high scores at positions where GATA actually occurs.
Hints
ConvOptions::new(stride, padding, dilation, groups):
- stride — how far to slide the kernel between outputs.
[1]for PWM scanning (every position). - padding — how many zeros to prepend/append.
[0]for now (output is shorter than input). - dilation — gap between kernel elements.
[1]for contiguous motifs. - groups —
1(standard convolution; group convolutions are an optimisation we don’t need here).
For 1-D conv every argument is a length-1 array. The values above are the defaults you want.
For input [N, C_in, L] and weight [C_out, C_in, w]:
- output shape
[N, C_out, L − w + 1]with default stride/padding/dilation - here:
C_in = 4(the four bases),C_out = M(number of motifs)
So [N, 4, L] × [M, 4, w] → [N, M, L − w + 1]. Verify the output shape in your tests.
A perfect PWM for the motif GATA looks like:
position: 0 1 2 3
A [ 0 1 0 1 ]
C [ 0 0 0 0 ]
G [ 1 0 0 0 ]
T [ 0 0 1 0 ]
When this PWM is convolved with a one-hot sequence containing GATA somewhere, the score at the start position of the motif is 4 (one match per column), and at non-matching positions is ≤ 3. This is why the test below checks for argmax = 4 in a sequence with "GATA" starting at position 4.
Solution
fn scan_motifs<B: Backend>(
one_hot: Tensor<B, 3>,
pwm: Tensor<B, 3>,
) -> Tensor<B, 3> {
use burn::tensor::module::conv1d;
use burn::tensor::ops::ConvOptions;
conv1d(one_hot, pwm, None, ConvOptions::new([1], [0], [1], 1))
}The full reference is in day11/ex-motif-scanner-solution/src/main.rs.
Where this leads
- Real PWMs are log-odds (positive for over-represented bases, negative for under-represented). Loading PWMs from JASPAR and convolving them against a genome with
conv1dreproduces classical tools like FIMO in a few lines of Rust. - Trainable filters — replace the handcrafted PWM with a Burn
nn::Conv1dwhose weights are learned by gradient descent (exercise 7). This is the DeepBind idea: each conv filter ends up learning a motif from data. - Wider receptive fields — stacking conv layers (or using dilated convolutions like Basenji) lets a network see thousands of bases of context. Same
conv1dfunction, more layers.