mgbam commited on
Commit
70ede12
·
verified ·
1 Parent(s): b15fc81

Update mcp/cbio.py

Browse files
Files changed (1) hide show
  1. mcp/cbio.py +31 -17
mcp/cbio.py CHANGED
@@ -1,22 +1,36 @@
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")
 
 
 
 
 
 
 
 
 
 
1
+ import logging
2
 
3
+ # Try to import the official pybioportal client; if it isn't installed, we'll disable cBio queries.
4
+ try:
5
+ from pybioportal import fetch_muts_in_multiple_mol_profs, fetch_molecular_profiles
6
+ _CBIO_ENABLED = True
7
+ except ImportError:
8
+ logging.warning("pybioportal package not available; disabling cBioPortal integration")
9
+ _CBIO_ENABLED = False
10
 
11
+ async def fetch_cbio(gene: str) -> list:
12
  """
13
+ Fetch cancer variant data for the given gene from cBioPortal.
14
+ Returns an empty list if pybioportal isn't installed or on any error.
15
  """
16
+ if not _CBIO_ENABLED:
17
+ return []
18
 
19
+ try:
20
+ # 1) Grab all molecular profiles (you can restrict to particular studies if you like)
21
+ profiles = fetch_molecular_profiles() # returns list of dicts with 'molecular_profile_id', etc.
22
+
23
+ # 2) For each profile, fetch the mutations for our gene
24
+ variants = []
25
+ for prof in profiles:
26
+ mpid = prof.get("molecular_profile_id")
27
+ if mpid:
28
+ # fetch_muts_in_multiple_mol_profs returns a list of mutation dicts
29
+ muts = fetch_muts_in_multiple_mol_profs([mpid], gene=gene)
30
+ variants.extend(muts or [])
31
+
32
+ return variants
33
+
34
+ except Exception as e:
35
+ logging.warning("cBioPortal variant fetch failed: %s", e)
36
+ return []