File size: 2,222 Bytes
3e219fa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""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())