Update mcp/opentargets.py
Browse files- mcp/opentargets.py +19 -11
mcp/opentargets.py
CHANGED
@@ -1,14 +1,22 @@
|
|
1 |
-
|
2 |
-
import os, httpx, textwrap, asyncio
|
3 |
from functools import lru_cache
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
@lru_cache(maxsize=512)
|
9 |
-
async def fetch_ot(sym:str, n:int=30):
|
10 |
-
|
11 |
-
async with httpx.AsyncClient(timeout=10,headers=_HDR) as c:
|
12 |
-
r=await c.post(_URL,json=
|
|
|
13 |
return r.json()["data"]["associations"]["rows"]
|
14 |
-
``` :contentReference[oaicite:9]{index=9
|
|
|
1 |
+
import os, httpx, textwrap
|
|
|
2 |
from functools import lru_cache
|
3 |
+
|
4 |
+
_URL = "https://api.platform.opentargets.org/api/v4/graphql"
|
5 |
+
_HDR = {"Content-Type": "application/json", "Accept": "application/json"}
|
6 |
+
if os.getenv("OT_KEY"): # optional higher quota
|
7 |
+
_HDR["Authorization"] = f"Bearer {os.getenv('OT_KEY')}"
|
8 |
+
|
9 |
+
_QUERY = textwrap.dedent("""
|
10 |
+
query Assoc($g:String!, $n:Int!) {
|
11 |
+
associations(geneSymbol:$g, size:$n) {
|
12 |
+
rows { score datasourceId disease { id name } target { id symbol } }
|
13 |
+
}
|
14 |
+
}""")
|
15 |
+
|
16 |
@lru_cache(maxsize=512)
|
17 |
+
async def fetch_ot(sym: str, n: int = 30):
|
18 |
+
payload = {"query": _QUERY, "variables": {"g": sym, "n": n}}
|
19 |
+
async with httpx.AsyncClient(timeout=10, headers=_HDR) as c:
|
20 |
+
r = await c.post(_URL, json=payload)
|
21 |
+
r.raise_for_status()
|
22 |
return r.json()["data"]["associations"]["rows"]
|
|