Genomics and RNA

Splice Variant Effects

MediumRaw PyTorchAbout 20 min2 public examples 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

Compare reference and alternate splice probabilities, then report the strongest acceptor/donor gains and losses and their offsets from the variant.

Function signature

splice_variant_effects(
    activations: dict,
    masks: dict,
    structure: dict,
    annotation_mask: bool = False,
) -> dict[str, torch.Tensor]

Worked example

Worked example

Acceptor changes are [0.2, -0.1, 0.1], giving gain 0.2 at offset -1 and loss 0.1 at offset 0. Donor changes are [0, 0, -0.2], giving gain 0 at the first tied position (offset -1) and loss 0.2 at offset 1. Return scores [0.2, 0.1, 0, 0.2] and offsets [-1, 0, -1, 1].

import torch

activations = {
    'reference': torch.tensor([[[0.8, 0.1, 0.1], [0.6, 0.2, 0.2], [0.4, 0.3, 0.3]]], dtype=torch.float64, requires_grad=True),
    'alternate': torch.tensor([[[0.6, 0.3, 0.1], [0.7, 0.1, 0.2], [0.5, 0.4, 0.1]]], dtype=torch.float64, requires_grad=True),
}
masks = {
    'valid_mask': torch.tensor([[True, True, True]], dtype=torch.bool),
}
structure = {
    'anchor': torch.tensor([1], dtype=torch.int64),
    'annotation_offset': torch.tensor([0], dtype=torch.int64),
}
annotation_mask = False

result = splice_variant_effects(activations=activations, masks=masks, structure=structure, annotation_mask=annotation_mask)

Expected return

{
    'scores': torch.tensor([[0.19999999999999998, 0.1, 0.0, 0.19999999999999998]], dtype=torch.float64),
    'offsets': torch.tensor([[-1, 0, -1, 1]], dtype=torch.int64),
    'has_support': torch.tensor([True], dtype=torch.bool),
    'nearest_annotation_offset': torch.tensor([0], dtype=torch.int64),
    'annotation_present': torch.tensor([True], dtype=torch.bool),
}

Parameters

ParameterShape and meaning
activationsdictSupplied intermediate tensors.
masksdictSupplied masks. Each field below defines what True includes or permits.
structuredictFields supplied together in this dictionary.
annotation_maskboolDefault FalseSplice mode only: apply the stated annotation-aware gain/loss suppression when True.

Return

dict[str, torch.Tensor]

Return exactly the named fields applicable to this operation.

Required behavior

  • reference and alternate are [B,L,3]: other, acceptor, donor probabilities. valid_mask [B,L] marks positions included in the comparison. Compute alternate − reference only at valid positions.
  • Return four entries in order: acceptor gain, acceptor loss, donor gain, donor loss. Maximize the signed change over valid positions; losses maximize reference − alternate. Do not clamp negative maxima to zero. Break ties by the first sequence index.
  • Each offset is winning_index − anchor. If no valid position exists, scores and offsets are zero and has_support=False.
  • If annotation_candidates and annotation_valid are supplied, choose the available offset with smallest absolute value, breaking equal-distance ties toward the numerically smaller signed offset. No available candidate yields offset 0 and annotation_present=False. Otherwise use annotation_offset and mark annotation_present=True.
  • annotation_mask defaults to False. When enabled and an annotation exists, suppress a gain at the nearest annotation or a loss away from it. Suppression zeroes only its score, preserving the already-selected offset.
  • Use CPU float32/float64 tensors with one shared floating dtype/device. Preserve strided layouts and do not mutate supplied tensors.
  • Use raw tensor operations, not a ready-made implementation of the requested primitive. Supplied parameters and random variates must not be reinitialized.

Why this matters

Compare predicted splice probabilities at aligned reference positions. The four scores locate the strongest acceptor and donor gains and losses around the variant.

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 splice_variant_effects(
    activations: dict,
    masks: dict,
    structure: dict,
    annotation_mask: bool = False,
) -> dict[str, torch.Tensor]:
    """Return dict[str, torch.Tensor]:
    
    scores: (B,4).
    offsets: (B, 4).
    has_support: (B,).
    nearest_annotation_offset: (B,).
    annotation_present: (B,)."""
    # Implement the operation. Inputs and test setup are supplied by BioTorch.
    raise NotImplementedError("Implement splice_variant_effects")

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.

Related exercises

Sources and further reading

A primitive exercise, not an end-to-end reproduction of the named models. No affiliation or endorsement is implied.