mgbam commited on
Commit
2a448c1
·
verified ·
1 Parent(s): 4764268

Update mcp/cbio.py

Browse files
Files changed (1) hide show
  1. mcp/cbio.py +35 -33
mcp/cbio.py CHANGED
@@ -1,43 +1,45 @@
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
 
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()