Genomics and RNA
Reverse-complement sequences and align strand-aware outputs
Basenji2BorzoiAlphaGenomeSpliceAICaduceus
Free account with email, Google or GitHub. Your code runs in an isolated PyTorch sandbox.
Your task
Reverse-complement DNA channels and align strand-aware tracks to the reversed sequence. Return the transformed DNA, tracks, and transported validity mask.
Function signature
dna_reverse_complement(
dna: torch.Tensor,
tracks: torch.Tensor,
valid_mask: torch.Tensor,
track_permutation: torch.Tensor,
) -> dict[str, torch.Tensor]Worked example
Acgt and unknown
The DNA reads ACGT?, where ? is an all-zero row. Reversing gives ?TGCA; complementing gives ?ACGT. The last track pair [8,9] becomes the first pair [9,8] under permutation [1,0]. Its validity stays False, but its values are retained: the mask travels with the data instead of zeroing it.
import torch
dna = torch.tensor([[[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0], [0.0, 0.0, 0.0, 0.0]]], dtype=torch.float64, requires_grad=True)
tracks = torch.tensor([[[0.0, 1.0], [2.0, 3.0], [4.0, 5.0], [6.0, 7.0], [8.0, 9.0]]], dtype=torch.float64, requires_grad=True)
valid_mask = torch.tensor([[True, True, True, True, False]], dtype=torch.bool)
track_permutation = torch.tensor([1, 0], dtype=torch.int64)
result = dna_reverse_complement(dna=dna, tracks=tracks, valid_mask=valid_mask, track_permutation=track_permutation)Expected return
{
'dna_rc': torch.tensor([[[0.0, 0.0, 0.0, 0.0], [1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]]], dtype=torch.float64),
'tracks_rc': torch.tensor([[[9.0, 8.0], [7.0, 6.0], [5.0, 4.0], [3.0, 2.0], [1.0, 0.0]]], dtype=torch.float64),
'valid_mask_rc': torch.tensor([[False, True, True, True, True]], dtype=torch.bool),
}Parameters
| Parameter | Shape and meaning |
|---|---|
dnatorch.Tensor | [B, L, 4]Nucleotide channel values in A, C, G, T order; one-hot, fractional, and all-zero unknown rows are allowed. |
trackstorch.Tensor | [B, L, T]Strand-aware annotation values at each sequence position. |
valid_masktorch.Tensor | [B, L]True means valid; transported without masking payloads |
track_permutationtorch.Tensor | [T]Output-to-input track map; applying it twice restores the original track order. |
Return
dict[str, torch.Tensor]
Return exactly the named fields applicable to this operation.
Required behavior
- Implement the ordinary full-length reverse complement, including strand-aware target alignment.
dnahas shape[B,L,4], nucleotide order[A,C,G,T].trackshas shape[B,L,T].BandTare positive;Lmay be zero. All floating tensors share dtype/device. track_permutationis anint64vector[T]: itsj-\mathrm{th}entry is the INPUT track to place at output trackj. It is a permutation of0..T-1satisfyingp[p[j]]=j.valid_maskis bool[B,L], withTruemeaning a measured/valid position.- Return
\mathrm{dna\_rc}[b,t,c]=\mathrm{dna}[b,L-1-t,3-c],\mathrm{tracks\_rc}[b,t,j]=\mathrm{tracks}[b,L-1-t,p[j]], and\mathrm{valid\_mask\_rc}[b,t]=\mathrm{valid\_mask}[b,L-1-t]. Reverse the entire padded lengthL, not only each example's valid subsequence. - The mask is metadata to transport, not a multiplier: do not discard or zero any DNA/track values at invalid positions. Unknown bases encoded as all-zero rows remain zero; soft/fractional nucleotide vectors are permuted without renormalizing.
- For
L=0return identically shaped empty tensors. Applying this operation twice, with the same supplied permutation, must recover every payload and the mask. This ordinary track operation does not transform contact maps, integer splice-coordinate transforms, and AlphaGenome's special boundary-losing DNase shift.
Why this matters
DNA reversal changes the coordinate order; complementation changes base identity. Output tracks may have a different supplied strand permutation.
Starter code
You write the body of this function in the BioTorch workspace. Your code runs in an isolated PyTorch sandbox; Run checks the public examples and Submit grades the full test suite, including gradients.
import torch
def dna_reverse_complement(
dna: torch.Tensor, # (B, L, 4)
tracks: torch.Tensor, # (B, L, T)
valid_mask: torch.Tensor, # (B, L)
track_permutation: torch.Tensor, # (T,)
) -> dict[str, torch.Tensor]:
"""Return dict[str, torch.Tensor]:
dna_rc: (B, L, 4).
tracks_rc: (B, L, T).
valid_mask_rc: (B, L)."""
# Implement the operation. Inputs and test setup are supplied by BioTorch.
raise NotImplementedError("Implement dna_reverse_complement")
Ready to implement it?
3 progressive hints, a solution walkthrough, and grading that checks values, shapes and gradients are waiting in the workspace.
Free account with email, Google or GitHub. Your code runs in an isolated PyTorch sandbox.
Sources and further reading
A primitive exercise, not an end-to-end reproduction of the named models. No affiliation or endorsement is implied.