mgbam commited on
Commit
320163c
·
verified ·
1 Parent(s): db31f82

Update genesis/structures.py

Browse files
Files changed (1) hide show
  1. genesis/structures.py +34 -27
genesis/structures.py CHANGED
@@ -1,33 +1,40 @@
1
  # genesis/structures.py
 
 
 
 
 
2
  import requests
3
- from typing import List, Dict
4
 
5
- def fetch_structures_for_terms(terms: List[str]) -> List[Dict[str, str]]:
6
- """Fetch PDB structures from PDBe or RCSB for given search terms."""
7
- structures = []
8
 
9
- for term in terms:
10
- try:
11
- # PDBe search
12
- pdbe_url = f"https://www.ebi.ac.uk/pdbe/search/pdb/select"
13
- params = {"q": term, "rows": 2, "wt": "json"}
14
- r = requests.get(pdbe_url, params=params, timeout=10)
15
- r.raise_for_status()
16
- data = r.json()
17
 
18
- docs = data.get("response", {}).get("docs", [])
19
- for doc in docs:
20
- pdb_id = doc.get("pdb_id")
21
- title = doc.get("title", term)
22
- if pdb_id:
23
- structures.append({
24
- "term": term,
25
- "pdb_id": pdb_id,
26
- "title": title,
27
- "pdbe_url": f"https://www.ebi.ac.uk/pdbe/entry/pdb/{pdb_id}",
28
- "rcsb_url": f"https://www.rcsb.org/structure/{pdb_id}"
29
- })
30
- except Exception as e:
31
- print(f"[Structures] Failed to fetch for {term}: {e}")
32
 
33
- return structures
 
 
 
 
 
 
 
 
 
 
 
1
  # genesis/structures.py
2
+ """
3
+ Structure Fetching for GENESIS-AI
4
+ Integrates PDBe (PDB) and ChEMBL molecule lookups.
5
+ """
6
+
7
  import requests
 
8
 
9
+ PDBe_API = "https://www.ebi.ac.uk/pdbe/api/pdb/entry/summary/"
10
+ CHEMBL_API = "https://www.ebi.ac.uk/chembl/api/data/molecule/"
 
11
 
12
+ def fetch_pdb_structure(pdb_id):
13
+ """Fetch PDB structure summary from PDBe."""
14
+ try:
15
+ r = requests.get(f"{PDBe_API}{pdb_id.lower()}")
16
+ r.raise_for_status()
17
+ return r.json()
18
+ except Exception as e:
19
+ return {"error": str(e)}
20
 
21
+ def fetch_chembl_molecule(chembl_id):
22
+ """Fetch ChEMBL molecule data."""
23
+ try:
24
+ r = requests.get(f"{CHEMBL_API}{chembl_id}.json")
25
+ r.raise_for_status()
26
+ return r.json()
27
+ except Exception as e:
28
+ return {"error": str(e)}
 
 
 
 
 
 
29
 
30
+ def fetch_structures_for_terms(terms):
31
+ """
32
+ Searches for possible PDB or ChEMBL IDs in terms and fetches details.
33
+ """
34
+ results = []
35
+ for term in terms:
36
+ if term.upper().startswith("CHEMBL"):
37
+ results.append({"term": term, "data": fetch_chembl_molecule(term)})
38
+ elif len(term) == 4 and term.isalnum():
39
+ results.append({"term": term, "data": fetch_pdb_structure(term)})
40
+ return results