mgbam commited on
Commit
192721e
·
verified ·
1 Parent(s): b1bf985

Update mcp/opentargets.py

Browse files
Files changed (1) hide show
  1. mcp/opentargets.py +19 -11
mcp/opentargets.py CHANGED
@@ -1,14 +1,22 @@
1
- ### `opentargets.py` tractability & constraint
2
- import os, httpx, textwrap, asyncio
3
  from functools import lru_cache
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"): HDR["Authorization"]="Bearer "+os.getenv("OT_KEY")
7
- _Q=textwrap.dedent("""query Q($g:String!,$n:Int){associations(geneSymbol:$g,size:$n){rows{score datatypeId disease{id name} target{id symbol}}}}""")
 
 
 
 
 
 
 
 
 
8
  @lru_cache(maxsize=512)
9
- async def fetch_ot(sym:str, n:int=30):
10
- p={"query":_Q,"variables":{"g":sym,"n":n}}
11
- async with httpx.AsyncClient(timeout=10,headers=_HDR) as c:
12
- r=await c.post(_URL,json=p);
 
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"]