mgbam commited on
Commit
6fe33f5
·
verified ·
1 Parent(s): f352c67

Update mcp/cbio.py

Browse files
Files changed (1) hide show
  1. mcp/cbio.py +20 -22
mcp/cbio.py CHANGED
@@ -1,24 +1,22 @@
1
- """
2
- cbio.py – cBioPortal REST helper (no API-key required for public portal).
3
- Returns variant summary rows for a gene / profile.
4
- """
5
- from functools import lru_cache
6
- import httpx, asyncio
7
 
8
- _BASE = "https://www.cbioportal.org/api/molecular-profiles/{profile}/genes/{gene}/mutations"
9
- _HEADERS = {"Accept": "application/json"}
10
 
11
- @lru_cache(maxsize=256)
12
- async def fetch_cbio(
13
- gene: str,
14
- profile: str = "brca_tcga_pan_can_atlas_2018_mutations",
15
- ) -> list[dict]:
16
- if not gene:
17
- return []
18
- url = _BASE.format(profile=profile, gene=gene)
19
- params = {"projection": "SUMMARY"}
20
- async with httpx.AsyncClient(timeout=10, headers=_HEADERS) as c:
21
- r = await c.get(url, params=params)
22
- if r.status_code >= 400:
23
- return [] # swallow 4xx/5xx → empty list
24
- return r.json()
 
 
 
 
1
+ # mcp/cbio.py
 
 
 
 
 
2
 
3
+ from pybioportal import PyBioPortal
4
+ import asyncio
5
 
6
+ async def fetch_cbio_variants(gene_symbol: str) -> list[dict]:
7
+ """
8
+ Fetches mutation/variant data for a specific gene symbol
9
+ from the default cBioPortal study (e.g. 'brca_mskcc_2020').
10
+ """
11
+ base_url = "https://www.cbioportal.org"
12
+ client = PyBioPortal(base_url)
13
+
14
+ # Build the query path for mutations in the gene
15
+ query_path = f"/mutation?gene={gene_symbol}&studyId=brca_mskcc_2020"
16
+ loop = asyncio.get_event_loop()
17
+ df = await loop.run_in_executor(
18
+ None,
19
+ lambda: client.get_data_frame(query_path)
20
+ )
21
+ # Convert DataFrame rows to dicts for JSON serialization
22
+ return df.to_dict(orient="records")