Update mcp/cbio.py
Browse files- mcp/cbio.py +35 -33
mcp/cbio.py
CHANGED
@@ -1,43 +1,45 @@
|
|
1 |
"""
|
2 |
-
|
|
|
|
|
|
|
|
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
"""
|
8 |
|
9 |
-
|
10 |
-
|
|
|
|
|
|
|
|
|
11 |
from functools import lru_cache
|
12 |
from typing import List, Dict
|
13 |
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
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
|
25 |
-
|
26 |
-
|
|
|
|
|
27 |
) -> List[Dict]:
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
await asyncio.sleep(0.5) # one back-off
|
40 |
-
try:
|
41 |
-
return await _hit()
|
42 |
-
except Exception:
|
43 |
-
return [] # graceful fallback
|
|
|
1 |
"""
|
2 |
+
MedGenesis v2 – core modules synchronised with the new helper API clients.
|
3 |
+
Contains three top‑level files:
|
4 |
+
• mcp/cbio.py – cancer cohort variants via cBioPortal REST v3
|
5 |
+
• mcp/orchestrator.py – async pipeline coordinator
|
6 |
+
• app.py – Streamlit UI (CPU‑only)
|
7 |
|
8 |
+
All modules assume the new helper files (mygene.py, ensembl.py, opentargets.py,
|
9 |
+
drugcentral_ext.py, pubchem_ext.py, gene_hub.py, clinicaltrials.py, etc.) are
|
10 |
+
present in the mcp package as created earlier in the conversation.
|
11 |
"""
|
12 |
|
13 |
+
# ───────────────────────── mcp/cbio.py ────────────────────────────────
|
14 |
+
"""cBioPortal REST helper – returns detailed mutation calls for a gene.
|
15 |
+
The public instance does **not** require an API‑key; however, if you have a
|
16 |
+
personal or GENIE instance token set as CBIO_KEY, it will be used.
|
17 |
+
"""
|
18 |
+
import os, httpx
|
19 |
from functools import lru_cache
|
20 |
from typing import List, Dict
|
21 |
|
22 |
+
_CBIO_BASE = os.getenv("CBIO_BASE_URL", "https://www.cbioportal.org/api")
|
23 |
+
_CBIO_TOKEN = os.getenv("CBIO_KEY")
|
24 |
+
_HEADERS = {"accept": "application/json"}
|
25 |
+
if _CBIO_TOKEN:
|
26 |
+
_HEADERS["Authorization"] = f"Bearer {_CBIO_TOKEN}"
|
|
|
|
|
|
|
27 |
|
28 |
@lru_cache(maxsize=256)
|
29 |
+
async def fetch_cbio_variants(
|
30 |
+
gene_symbol: str,
|
31 |
+
study: str = "brca_tcga_pan_can_atlas_2018",
|
32 |
+
*,
|
33 |
+
timeout: float = 15.0,
|
34 |
) -> List[Dict]:
|
35 |
+
"""Return list of variant dicts (DETAILED projection)."""
|
36 |
+
profile = f"{study}_mutations"
|
37 |
+
url = (
|
38 |
+
f"{_CBIO_BASE}/molecular-profiles/{profile}/genes/{gene_symbol}/"
|
39 |
+
"mutations"
|
40 |
+
)
|
41 |
+
params = {"projection": "DETAILED"}
|
42 |
+
async with httpx.AsyncClient(timeout=timeout, headers=_HEADERS, follow_redirects=True) as client:
|
43 |
+
r = await client.get(url, params=params)
|
44 |
+
r.raise_for_status()
|
45 |
+
return r.json()
|
|
|
|
|
|
|
|
|
|