Spaces:
Sleeping
Sleeping
| """Pre-warm the gnomAD SQLite cache for a list of variants. | |
| The gnomAD GraphQL API is open but slow under sustained load. For batch | |
| classification runs (eval set, day-1 lab variant pull), pre-fetching every | |
| variant ID into the local SQLite cache (`data/gnomad_cache.db`) keeps the | |
| critical-path classification under 30s per variant. | |
| Input | |
| ----- | |
| A text file with one gnomAD variant ID per line (format: `chr-pos-ref-alt`, | |
| no `chr` prefix). Lines starting with `#` are skipped. | |
| Usage | |
| ----- | |
| python -m scripts.warm_gnomad_cache variant_ids.txt | |
| """ | |
| from __future__ import annotations | |
| import argparse | |
| import asyncio | |
| import logging | |
| import sys | |
| from pathlib import Path | |
| from backend.app.services.gnomad import GnomADClient | |
| logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s") | |
| logger = logging.getLogger("warm_gnomad") | |
| async def warm(ids: list[str], concurrency: int) -> None: | |
| client = GnomADClient() | |
| sem = asyncio.Semaphore(concurrency) | |
| async def one(vid: str) -> None: | |
| async with sem: | |
| try: | |
| freq = await client.lookup(vid) | |
| logger.info( | |
| "%s — AF=%s, hom=%s", | |
| vid, | |
| f"{freq.overall_af:.2e}" if freq.overall_af else "—", | |
| freq.homozygote_count or 0, | |
| ) | |
| except Exception as e: | |
| logger.warning("%s failed: %s", vid, e) | |
| await asyncio.gather(*(one(v) for v in ids)) | |
| def main() -> int: | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument("ids_file", type=Path, help="One variant ID per line (chr-pos-ref-alt)") | |
| parser.add_argument("--concurrency", type=int, default=4, help="Parallel requests (gnomAD rate-limits)") | |
| args = parser.parse_args() | |
| if not args.ids_file.exists(): | |
| logger.error("file not found: %s", args.ids_file) | |
| return 1 | |
| raw = args.ids_file.read_text().splitlines() | |
| ids = [ln.strip() for ln in raw if ln.strip() and not ln.startswith("#")] | |
| logger.info("warming %d variants with concurrency=%d", len(ids), args.concurrency) | |
| asyncio.run(warm(ids, args.concurrency)) | |
| return 0 | |
| if __name__ == "__main__": | |
| sys.exit(main()) | |