Genomics and RNA
Align insertion/deletion predictions to reference coordinates
Free account with email, Google or GitHub. Your code runs in an isolated PyTorch sandbox.
Your task
Map alternate-allele prediction positions back to reference coordinates across an insertion or deletion. Return aligned values, validity, source indices, and deleted-position indicators.
Function signature
indel_prediction_alignment(
alternate: torch.Tensor,
alternate_valid: torch.Tensor,
reference_valid: torch.Tensor,
anchor: int,
ref_length: int,
alt_length: int,
) -> dict[str, torch.Tensor]Worked example
Insertions and deletions map differently
Insertion example: reference length 4, anchor=1, ref_length=1, alt_length=3; alternate values are [(1,10),(2,3),(5,1),(4,8),(6,2),(7,0)], with every source/reference position valid. Alternate indices 1,2,3 collapse to reference anchor 1: channel maxima [5,8] come from different source indices [2,3]. The aligned result is [(1,10),(5,8),(6,2),(7,0)], with source indices [(0,0),(2,3),(4,4),(5,5)]. All four outputs are valid and none are deleted. Separately, for reference length 5, anchor=1, ref_length=3, alt_length=1, and valid scalar alternate values [10,20,30], the aligned result is [10,20,0,0,30] with source indices [0,1,-1,-1,2]. Deleted reference slots 2 and 3 remain valid when reference_valid is true; their zeros represent the deletion.
Parameters
| Parameter | Shape and meaning |
|---|---|
alternatetorch.Tensor | [B, \mathrm{La}, C]Predictions on alternate-allele sequence coordinates. |
alternate_validtorch.Tensor | [B, \mathrm{La}]True permits an alternate source position. |
reference_validtorch.Tensor | [B, L]True permits comparison at a reference position, including deliberately zeroed deletions. |
anchorint | Zero-based start of the reference allele or alignment edit. |
ref_lengthint | Length of the replaced reference allele. |
alt_lengthint | Length of the alternate allele. |
Return
dict[str, torch.Tensor]
Return exactly the named fields applicable to this operation.
Required behavior
- Use zero-based
anchorP, positiveref_lengthRandalt_lengthA including the retained VCF-style anchor.alternate[B,\mathrm{La},C]has\mathrm{La}=L+A-Randreference_valid[B,L]defines the reference grid. RequireP+R\le Land eitherR=Aor\operatorname{min}(R,A)=1. - For equal lengths, coordinates map directly.
- For an insertion
R=1,A>1, map the entirealternatespan[P,P+A)to referencePby a per-channel maximum over valid positions, then shift downstream indices byA-1. - For a deletion
R>1,A=1, preservealternateP, put zero predictions at reference positions[P+1,P+R), and shift the downstreamalternateindex by1-R. Returnaligned[B,L,C], reference-coordinatevalid_mask, channel-wisesource_indices(first winningalternateindex, or-1), anddeleted_mask[L]. - Deleted slots are valid if
reference_validisTrue: they are deliberate biological-zero predictions, not missing data. Other positions require both reference andalternatesupport. Invalid values, including NaNs, are excluded before a maximum. An all-missing inserted span returns zero and invalid. - Complex unequal-length replacements with
R>1andA>1are rejected rather than guessed.
Why this matters
Downstream variant scores compare homologous reference positions, while an insertion may contribute a span of alternate predictions to one reference anchor.
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 indel_prediction_alignment(
alternate: torch.Tensor, # (B, La, C)
alternate_valid: torch.Tensor, # (B, La)
reference_valid: torch.Tensor, # (B, L)
anchor: int,
ref_length: int,
alt_length: int,
) -> dict[str, torch.Tensor]:
"""Return dict[str, torch.Tensor]:
aligned: (B, L, C).
valid_mask: (B, L).
source_indices: (B, L, C).
deleted_mask: (L,)."""
# Implement the operation. Inputs and test setup are supplied by BioTorch.
raise NotImplementedError("Implement indel_prediction_alignment")
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.