Create ncbi_turbo.py
Browse files- mcp/ncbi_turbo.py +21 -0
mcp/ncbi_turbo.py
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os, asyncio, httpx
|
2 |
+
from functools import lru_cache
|
3 |
+
|
4 |
+
_KEY = os.getenv("NCBI_EUTILS_KEY")
|
5 |
+
_BASE = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/"
|
6 |
+
|
7 |
+
async def _call(ep: str, params: dict, *, t=8):
|
8 |
+
if _KEY: params["api_key"] = _KEY
|
9 |
+
async with httpx.AsyncClient(timeout=t) as c:
|
10 |
+
r = await c.get(f"{_BASE}{ep}", params=params); r.raise_for_status()
|
11 |
+
return r.json() if r.headers["content-type"].startswith("application/json") else r.text
|
12 |
+
|
13 |
+
@lru_cache(maxsize=512)
|
14 |
+
async def pubmed_ids(term: str, n: int = 20):
|
15 |
+
data = await _call("esearch.fcgi", {"db":"pubmed","term":term,"retmode":"json","retmax":n})
|
16 |
+
return data["esearchresult"]["idlist"]
|
17 |
+
|
18 |
+
@lru_cache(maxsize=512)
|
19 |
+
async def gene_summary(gene_id: str):
|
20 |
+
data = await _call("esummary.fcgi", {"db":"gene","id":gene_id,"retmode":"json"})
|
21 |
+
return list(data["result"].values())[1]
|