Spaces:
Running
Running
# 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 | |