Spaces:
Running
Running
# genesis/molecule_viewer.py | |
""" | |
Molecule Viewer for GENESIS-AI | |
Fetches 3D molecular structure data and metadata from public chemical databases. | |
""" | |
import requests | |
PDBE_API = "https://www.ebi.ac.uk/pdbe/api/pdb/entry/summary/" | |
PUBCHEM_API = "https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/name" | |
CHEMBL_API = "https://www.ebi.ac.uk/chembl/api/data/molecule" | |
def get_pdbe_structure(pdb_id): | |
"""Fetch structure summary from PDBe.""" | |
try: | |
r = requests.get(f"{PDBE_API}{pdb_id}") | |
r.raise_for_status() | |
return r.json() | |
except Exception as e: | |
return {"error": str(e)} | |
def get_pubchem_structure(compound_name): | |
"""Fetch compound data from PubChem.""" | |
try: | |
url = f"{PUBCHEM_API}/{compound_name}/JSON" | |
r = requests.get(url) | |
r.raise_for_status() | |
return r.json() | |
except Exception as e: | |
return {"error": str(e)} | |
def get_chembl_structure(query): | |
"""Search ChEMBL for molecule info.""" | |
try: | |
r = requests.get(f"{CHEMBL_API}?molecule_synonyms__icontains={query}") | |
r.raise_for_status() | |
return r.json() | |
except Exception as e: | |
return {"error": str(e)} | |
def fetch_structure(query): | |
"""Try PDBe (if PDB ID), else PubChem, else ChEMBL.""" | |
if len(query) == 4 and query.isalnum(): | |
return get_pdbe_structure(query) | |
pubchem_result = get_pubchem_structure(query) | |
if "error" not in pubchem_result: | |
return pubchem_result | |
return get_chembl_structure(query) | |