|
|
|
"""Async wrapper for MyGene.info API (https://mygene.info) |
|
|
|
Provides a single helper: |
|
• `fetch_gene_info(query)` → returns the first matching hit dict or `{}`. |
|
|
|
No API‑key needed; community tier allows 500 QPS. |
|
""" |
|
from __future__ import annotations |
|
|
|
import httpx |
|
from functools import lru_cache |
|
|
|
_BASE = "https://mygene.info/v3" |
|
|
|
|
|
@lru_cache(maxsize=128) |
|
async def fetch_gene_info(query: str) -> dict: |
|
"""Return first hit for *query* or an empty dict.""" |
|
params = { |
|
"q": query, |
|
"fields": "symbol,name,summary,alias,entrezgene,clinvar,location,go", |
|
"size": 1, |
|
} |
|
async with httpx.AsyncClient(timeout=10) as client: |
|
resp = await client.get(f"{_BASE}/query", params=params) |
|
resp.raise_for_status() |
|
hits = resp.json().get("hits", []) |
|
return hits[0] if hits else {} |
|
|