Update mcp/cbio.py
Browse files- mcp/cbio.py +29 -27
mcp/cbio.py
CHANGED
@@ -1,41 +1,43 @@
|
|
1 |
"""
|
2 |
-
cBioPortal
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
"""
|
8 |
|
9 |
-
from
|
10 |
import os, asyncio, httpx
|
|
|
|
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
_KEY = os.getenv("CBIO_KEY") # optional
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
) -> list[dict]:
|
22 |
-
hdr = {"Accept": "application/json"}
|
23 |
-
if _KEY:
|
24 |
-
hdr["Authorization"] = f"Bearer {_KEY}"
|
25 |
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
28 |
r = await c.get(_URL.format(profile=profile, gene=gene))
|
29 |
-
if r.status_code == 404:
|
30 |
-
return []
|
31 |
r.raise_for_status()
|
32 |
return r.json()
|
33 |
|
34 |
try:
|
35 |
-
return await
|
36 |
-
except httpx.HTTPStatusError:
|
37 |
-
await asyncio.sleep(0.
|
38 |
try:
|
39 |
-
return await
|
40 |
-
except Exception:
|
41 |
-
return []
|
|
|
1 |
"""
|
2 |
+
cBioPortal helper · REST v4 · zero-crash
|
3 |
|
4 |
+
* Public instance needs **no** API-key :contentReference[oaicite:0]{index=0}
|
5 |
+
* Optional Bearer token (`CBIO_KEY`) for private portals (e.g. GENIE) :contentReference[oaicite:1]{index=1}
|
6 |
+
* One retry on 5xx / ReadTimeout, else returns []
|
7 |
"""
|
8 |
|
9 |
+
from __future__ import annotations
|
10 |
import os, asyncio, httpx
|
11 |
+
from functools import lru_cache
|
12 |
+
from typing import List, Dict
|
13 |
|
14 |
+
_TOKEN = os.getenv("CBIO_KEY") # optional
|
15 |
+
_HDR = {"Accept": "application/json"}
|
16 |
+
if _TOKEN:
|
17 |
+
_HDR["Authorization"] = f"Bearer {_TOKEN}"
|
|
|
18 |
|
19 |
+
_URL = ("https://www.cbioportal.org/api"
|
20 |
+
"/molecular-profiles/{profile}/genes/{gene}/mutations?projection=SUMMARY"
|
21 |
+
) # swagger ref :contentReference[oaicite:2]{index=2}
|
|
|
|
|
|
|
|
|
22 |
|
23 |
+
@lru_cache(maxsize=256)
|
24 |
+
async def fetch_cbio(
|
25 |
+
gene : str,
|
26 |
+
profile : str = "brca_tcga_pan_can_atlas_2018_mutations"
|
27 |
+
) -> List[Dict]:
|
28 |
+
async def _hit() -> List[Dict]:
|
29 |
+
async with httpx.AsyncClient(timeout=10, headers=_HDR) as c:
|
30 |
r = await c.get(_URL.format(profile=profile, gene=gene))
|
31 |
+
if r.status_code == 404:
|
32 |
+
return [] # gene absent
|
33 |
r.raise_for_status()
|
34 |
return r.json()
|
35 |
|
36 |
try:
|
37 |
+
return await _hit()
|
38 |
+
except (httpx.HTTPStatusError, httpx.ReadTimeout):
|
39 |
+
await asyncio.sleep(0.5) # one back-off
|
40 |
try:
|
41 |
+
return await _hit()
|
42 |
+
except Exception:
|
43 |
+
return [] # graceful fallback
|