Spaces:
Sleeping
Sleeping
Update genesis/molecule_viewer.py
Browse files- genesis/molecule_viewer.py +42 -39
genesis/molecule_viewer.py
CHANGED
|
@@ -1,45 +1,48 @@
|
|
| 1 |
# genesis/molecule_viewer.py
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import requests
|
| 3 |
-
from typing import Optional
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
def
|
| 14 |
-
"""Fetch
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
def
|
| 22 |
-
"""
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
viewer.addModel(pdbData, "pdb");
|
| 30 |
-
viewer.setStyle({{}}, {{cartoon: {{color: 'spectrum'}}}});
|
| 31 |
-
viewer.zoomTo();
|
| 32 |
-
viewer.render();
|
| 33 |
-
</script>
|
| 34 |
-
"""
|
| 35 |
|
| 36 |
-
def
|
| 37 |
-
"""
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
return render_3dmol(pdb_data)
|
| 45 |
-
return f"<p style='color:red;'>No structure found for {pdb_or_gene}</p>"
|
|
|
|
| 1 |
# genesis/molecule_viewer.py
|
| 2 |
+
"""
|
| 3 |
+
Molecule Viewer for GENESIS-AI
|
| 4 |
+
Fetches 3D molecular structure data and metadata from public chemical databases.
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
import requests
|
|
|
|
| 8 |
|
| 9 |
+
PDBE_API = "https://www.ebi.ac.uk/pdbe/api/pdb/entry/summary/"
|
| 10 |
+
PUBCHEM_API = "https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/name"
|
| 11 |
+
CHEMBL_API = "https://www.ebi.ac.uk/chembl/api/data/molecule"
|
| 12 |
+
|
| 13 |
+
def get_pdbe_structure(pdb_id):
|
| 14 |
+
"""Fetch structure summary from PDBe."""
|
| 15 |
+
try:
|
| 16 |
+
r = requests.get(f"{PDBE_API}{pdb_id}")
|
| 17 |
+
r.raise_for_status()
|
| 18 |
+
return r.json()
|
| 19 |
+
except Exception as e:
|
| 20 |
+
return {"error": str(e)}
|
| 21 |
|
| 22 |
+
def get_pubchem_structure(compound_name):
|
| 23 |
+
"""Fetch compound data from PubChem."""
|
| 24 |
+
try:
|
| 25 |
+
url = f"{PUBCHEM_API}/{compound_name}/JSON"
|
| 26 |
+
r = requests.get(url)
|
| 27 |
+
r.raise_for_status()
|
| 28 |
+
return r.json()
|
| 29 |
+
except Exception as e:
|
| 30 |
+
return {"error": str(e)}
|
| 31 |
|
| 32 |
+
def get_chembl_structure(query):
|
| 33 |
+
"""Search ChEMBL for molecule info."""
|
| 34 |
+
try:
|
| 35 |
+
r = requests.get(f"{CHEMBL_API}?molecule_synonyms__icontains={query}")
|
| 36 |
+
r.raise_for_status()
|
| 37 |
+
return r.json()
|
| 38 |
+
except Exception as e:
|
| 39 |
+
return {"error": str(e)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
def fetch_structure(query):
|
| 42 |
+
"""Try PDBe (if PDB ID), else PubChem, else ChEMBL."""
|
| 43 |
+
if len(query) == 4 and query.isalnum():
|
| 44 |
+
return get_pdbe_structure(query)
|
| 45 |
+
pubchem_result = get_pubchem_structure(query)
|
| 46 |
+
if "error" not in pubchem_result:
|
| 47 |
+
return pubchem_result
|
| 48 |
+
return get_chembl_structure(query)
|
|
|
|
|
|