Genomics and RNA

Align insertion/deletion predictions to reference coordinates

MediumRaw PyTorchAbout 20 min1 public example plus private tests

SpliceAI

Write and run your solution Sign in

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

ParameterShape 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.
anchorintZero-based start of the reference allele or alignment edit.
ref_lengthintLength of the replaced reference allele.
alt_lengthintLength of the alternate allele.

Return

dict[str, torch.Tensor]

Return exactly the named fields applicable to this operation.

Required behavior

  • Use zero-based anchor P, positive ref_length R and alt_length A including the retained VCF-style anchor. alternate [B,\mathrm{La},C] has \mathrm{La}=L+A-R and reference_valid [B,L] defines the reference grid. Require P+R\le L and either R=A or \operatorname{min}(R,A)=1.
  • For equal lengths, coordinates map directly.
  • For an insertion R=1,A>1, map the entire alternate span [P,P+A) to reference P by a per-channel maximum over valid positions, then shift downstream indices by A-1.
  • For a deletion R>1,A=1, preserve alternate P, put zero predictions at reference positions [P+1,P+R), and shift the downstream alternate index by 1-R. Return aligned [B,L,C], reference-coordinate valid_mask, channel-wise source_indices (first winning alternate index, or -1), and deleted_mask [L].
  • Deleted slots are valid if reference_valid is True: they are deliberate biological-zero predictions, not missing data. Other positions require both reference and alternate support. Invalid values, including NaNs, are excluded before a maximum. An all-missing inserted span returns zero and invalid.
  • Complex unequal-length replacements with R>1 and A>1 are 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.

Write and run your solution Sign in

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.