"""Shared helpers for the alphagenome skill scripts. Standard library only, so variant parsing, interval arithmetic, API-key discovery, Phred conversion, and table export all work (and are testable) without the ``alphagenome`` package installed. The scripts import the SDK lazily through :func:`require_alphagenome`. """ from __future__ import annotations import csv import io import json import math import os import re import sys from collections.abc import Iterable, Mapping, Sequence from dataclasses import dataclass, field from pathlib import Path # -------------------------------------------------------------------------- # constants # -------------------------------------------------------------------------- #: Environment variables checked, in order, for the AlphaGenome API key. #: The first spelling is what DeepMind's own agent skills use; the second is #: what ``alphagenome.colab_utils.get_api_key`` reads by default. API_KEY_ENV_VARS: tuple[str, ...] = ("ALPHAGENOME_API_KEY", "ALPHA_GENOME_API_KEY") GET_KEY_URL = "https://deepmind.google.com/science/alphagenome" ATLAS_BASE_URL = "https://deepmind.google.com/science/alphagenome/atlas" ATLAS_TRACK_PREDICTIONS_URL = f"{ATLAS_BASE_URL}/track-predictions" DOCS_URL = "https://www.alphagenomedocs.com/" #: GENCODE v46 annotation used by the Atlas and the model, as a Feather file #: (about 318 MB; needs roughly 4 GB of RAM to load with pandas). GTF_FEATHER_URL = ( "https://storage.googleapis.com/alphagenome/reference/gencode/hg38/" "gencode.v46.annotation.gtf.gz.feather" ) #: Atlas scorer names for the composite AlphaGenome Variant Impact score and #: its 18-way SHAP feature attribution. AVI_SCORER = "AVI_SCORE" AVI_FEATURES_SCORER = "AVI_SCORE_FEATURE_IMPORTANCE" AVI_SCORERS: tuple[str, str] = (AVI_SCORER, AVI_FEATURES_SCORER) #: The 18 AVI input features, keyed by the ``var['name']`` values returned by #: the ``AVI_SCORE_FEATURE_IMPORTANCE`` scorer. Value: (display name, category, #: Atlas track scorers that feed the feature; empty for annotation features). AVI_FEATURES: dict[str, tuple[str, str, tuple[str, ...]]] = { "MERGED_SPLICING": ( "Splicing", "Splicing", ("SPLICE_SITES", "SPLICE_SITE_USAGE", "SPLICE_JUNCTIONS"), ), "MAX_ABS_RNA_SEQ": ("RNA-seq", "Transcription", ("RNA_SEQ",)), "MAX_ABS_ATAC": ("ATAC-seq", "Chromatin accessibility", ("ATAC",)), "MAX_ABS_DNASE": ("DNase-seq", "Chromatin accessibility", ("DNASE",)), "MAX_ABS_CHIP_TF": ("ChIP-TF", "Transcription factor binding", ("CHIP_TF",)), "MAX_ABS_CHIP_HISTONE": ("ChIP-Histone", "Histone modification", ("CHIP_HISTONE",)), "MAX_ABS_CAGE": ("CAGE", "Transcription initiation", ("CAGE",)), "MAX_ABS_PROCAP": ("PRO-cap", "Transcription initiation", ("PROCAP",)), "MAX_ABS_POLYADENYLATION": ("Polyadenylation", "Transcription", ("POLYADENYLATION",)), "MAX_ABS_CONTACT_MAPS": ("3D genome contacts", "3D genome organization", ("CONTACT_MAPS",)), "ALPHAMISSENSE": ("AlphaMissense", "Protein impact", ()), "CACTUS_241_WAY": ("Zoonomia Cactus 241-way", "Evolutionary conservation", ()), "PHASTCONS_470_WAY": ("PhastCons 470-way", "Evolutionary conservation", ()), "PROTEIN_TERMINATION": ("Protein termination", "Coding consequence", ()), "START_LOST": ("Start lost", "Coding consequence", ()), "STOP_LOST": ("Stop lost", "Coding consequence", ()), "IS_INSERTION": ("Insertion", "Indel type", ()), "IS_DELETION": ("Deletion", "Indel type", ()), } #: Atlas track-level scorers (one AnnData each; tracks on the ``var`` axis). #: ``scorer_metadata()`` is the authority on what the server currently serves. ATLAS_TRACK_SCORERS: tuple[str, ...] = ( "ATAC", "DNASE", "CHIP_TF", "CHIP_HISTONE", "CAGE", "PROCAP", "RNA_SEQ", "POLYADENYLATION", "SPLICE_SITES", "SPLICE_SITE_USAGE", "SPLICE_JUNCTIONS", "CONTACT_MAPS", ) #: Per-position substitution count the Atlas stores (three alternates per base). SNVS_PER_BASE = 3 INSTALL_HINT = ( "alphagenome is not installed. Run: uv pip install alphagenome " "(or invoke with: uv run --with alphagenome