Update mcp/mygene.py
Browse files- mcp/mygene.py +24 -9
mcp/mygene.py
CHANGED
|
@@ -1,13 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import httpx, asyncio
|
|
|
|
| 2 |
|
| 3 |
_BASE = "https://mygene.info/v3"
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Async wrapper for MyGene.info API (https://mygene.info)
|
| 2 |
+
Exposes `fetch_gene_info` which returns the top hit for a free‑text query.
|
| 3 |
+
No API‑key required; 500 QPS community quota.
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
import httpx, asyncio
|
| 7 |
+
from functools import lru_cache
|
| 8 |
|
| 9 |
_BASE = "https://mygene.info/v3"
|
| 10 |
|
| 11 |
+
@lru_cache(maxsize=128)
|
| 12 |
+
async def fetch_gene_info(query: str) -> dict: # noqa: C901 – simple IO fn
|
| 13 |
+
"""Return first hit dict or {} if nothing found.
|
| 14 |
+
|
| 15 |
+
Parameters
|
| 16 |
+
----------
|
| 17 |
+
query : str – gene symbol / keyword / Entrez id / HGNC etc.
|
| 18 |
+
"""
|
| 19 |
+
params = {
|
| 20 |
+
"q": query,
|
| 21 |
+
"fields": "symbol,name,summary,alias,entrezgene,clinvar,location,go",
|
| 22 |
+
"size": 1,
|
| 23 |
+
}
|
| 24 |
+
async with httpx.AsyncClient(timeout=10) as client:
|
| 25 |
+
resp = await client.get(f"{_BASE}/query", params=params)
|
| 26 |
+
resp.raise_for_status()
|
| 27 |
+
hits = resp.json().get("hits", [])
|
| 28 |
+
return hits[0] if hits else {}
|