Spaces:
Sleeping
Sleeping
File size: 1,511 Bytes
eaf86b1 27e909a eaf86b1 27e909a eaf86b1 27e909a eaf86b1 27e909a eaf86b1 27e909a |
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 42 43 44 45 46 47 48 49 |
# 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)
|