File size: 1,220 Bytes
3b4e96a
320163c
 
 
 
 
3b4e96a
 
320163c
 
3b4e96a
320163c
 
 
 
 
 
 
 
3b4e96a
320163c
 
 
 
 
 
 
 
3b4e96a
320163c
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# genesis/structures.py
"""
Structure Fetching for GENESIS-AI
Integrates PDBe (PDB) and ChEMBL molecule lookups.
"""

import requests

PDBe_API = "https://www.ebi.ac.uk/pdbe/api/pdb/entry/summary/"
CHEMBL_API = "https://www.ebi.ac.uk/chembl/api/data/molecule/"

def fetch_pdb_structure(pdb_id):
    """Fetch PDB structure summary from PDBe."""
    try:
        r = requests.get(f"{PDBe_API}{pdb_id.lower()}")
        r.raise_for_status()
        return r.json()
    except Exception as e:
        return {"error": str(e)}

def fetch_chembl_molecule(chembl_id):
    """Fetch ChEMBL molecule data."""
    try:
        r = requests.get(f"{CHEMBL_API}{chembl_id}.json")
        r.raise_for_status()
        return r.json()
    except Exception as e:
        return {"error": str(e)}

def fetch_structures_for_terms(terms):
    """
    Searches for possible PDB or ChEMBL IDs in terms and fetches details.
    """
    results = []
    for term in terms:
        if term.upper().startswith("CHEMBL"):
            results.append({"term": term, "data": fetch_chembl_molecule(term)})
        elif len(term) == 4 and term.isalnum():
            results.append({"term": term, "data": fetch_pdb_structure(term)})
    return results