Genomics and RNA
Edit and crop an alternate-allele DNA window
Free account with email, Google or GitHub. Your code runs in an isolated PyTorch sandbox.
Your task
Replace a reference DNA interval with an alternate allele and crop the requested fixed-width window. Return its sequence, validity, and source-position maps.
Function signature
allele_window_edit(
reference: torch.Tensor,
alternate: torch.Tensor,
reference_valid: torch.Tensor,
alternate_valid: torch.Tensor,
options: dict,
) -> dict[str, torch.Tensor]Worked example
Insertion edit
In the first row, reference ATCATT has its C at zero-based index 2 replaced by ATG. The full edited sequence is ATATGATT, of length 6 + 3 − 1 = 8. Output slots 2–4 come from alternate indices 0–2; surviving reference slots keep indices 0, 1, 3, 4, 5.
import torch
reference = torch.tensor([[[1.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 1.0], [0.0, 1.0, 0.0, 0.0], [1.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 1.0], [0.0, 0.0, 0.0, 1.0]], [[0.0, 0.0, 0.0, 1.0], [0.0, 0.0, 0.0, 1.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 0.0, 1.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0]]], dtype=torch.float64, requires_grad=True)
alternate = torch.tensor([[[1.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 1.0], [0.0, 0.0, 1.0, 0.0]], [[1.0, 0.0, 0.0, 0.0], [1.0, 0.0, 0.0, 0.0], [1.0, 0.0, 0.0, 0.0]]], dtype=torch.float64, requires_grad=True)
reference_valid = torch.full((2, 6), True, dtype=torch.bool)
alternate_valid = torch.tensor([[True, True, True], [True, True, True]], dtype=torch.bool)
options = {
'anchor': 2,
'ref_length': 1,
}
result = allele_window_edit(reference=reference, alternate=alternate, reference_valid=reference_valid, alternate_valid=alternate_valid, options=options)Expected return
{
'sequence': torch.tensor([[[1.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 1.0], [1.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 1.0], [0.0, 0.0, 1.0, 0.0], [1.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 1.0], [0.0, 0.0, 0.0, 1.0]], [[0.0, 0.0, 0.0, 1.0], [0.0, 0.0, 0.0, 1.0], [1.0, 0.0, 0.0, 0.0], [1.0, 0.0, 0.0, 0.0], [1.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 1.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0]]], dtype=torch.float64),
'valid_mask': torch.full((2, 8), True, dtype=torch.bool),
'reference_index': torch.tensor([0, 1, -1, -1, -1, 3, 4, 5], dtype=torch.int64),
'alternate_index': torch.tensor([-1, -1, 0, 1, 2, -1, -1, -1], dtype=torch.int64),
}Parameters
| Parameter | Shape and meaning |
|---|---|
referencetorch.Tensor | [B, L, 4]float [B,L,4] one-hot or soft DNA payloads in A,C,G,T order |
alternatetorch.Tensor | [B, A, 4]float [B,A,4] complete replacement allele, including retained anchor when applicable |
reference_validtorch.Tensor | [B, L]bool [B,L], source-window support |
alternate_validtorch.Tensor | [B, A]bool [B,A], allele-payload support |
optionsdict | Scalar settings. Omitted keys use the defaults below. |
Return
dict[str, torch.Tensor]
Return exactly the named fields applicable to this operation.
Required behavior
reference[B,L,4]andalternate[B,A,4]use channel order[A,C,G,T]. Config\mathrm{anchor}=Pis a zero-based index into the suppliedreferencewindow;\mathrm{ref\_length}=Ris the positive number of replaced bases, including a retained anchor when using normalized VCF-style alleles.- Require
A\ge 1,0\le P,P+R\le L. Form\mathrm{edited}=\mathrm{reference}[:P] + \mathrm{alternate} + \mathrm{reference}[P+R:], of length\mathrm{Le}=L+A-R, after sanitizing missing payloads viareference_valid/alternate_valid. - Config
window_start(default0) indexes this EDITEDsequence, andwindow_length(defaultLe) may crop or pad the requested window; negative start or coordinates beyond Le produce zero DNA andvalid_mask=False. A biologically unknown but valid base can be an all-zero vector withvalid_mask=True. - Return
sequence[B,W,4],valid_mask[B,W], and payload provenance arrays:reference_index[W]gives a retained original-input coordinate,alternate_index[W]gives a suppliedalternate-allele coordinate;\mathrm{use} -1in the other array and for padding. - The supplied alternate's retained anchor is still
alternatepayload, not falsely labeledreferenceprovenance.W=0is permitted. This primitive edits tensor payloads, not strings or areference-genome database. - All entries in the batch use the same anchor, ref_length, window_start and window_length; only sequence payloads and masks differ.
Why this matters
An insertion or deletion changes sequence length, so payloads and their provenance must move through the same edit and crop.
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 allele_window_edit(
reference: torch.Tensor, # (B, L, 4)
alternate: torch.Tensor, # (B, A, 4)
reference_valid: torch.Tensor, # (B, L)
alternate_valid: torch.Tensor, # (B, A)
options: dict,
) -> dict[str, torch.Tensor]:
"""Return dict[str, torch.Tensor]:
sequence: (B, W, 4).
valid_mask: (B, W).
reference_index: (W,).
alternate_index: (W,)."""
# Implement the operation. Inputs and test setup are supplied by BioTorch.
raise NotImplementedError("Implement allele_window_edit")
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.