mgbam commited on
Commit
27e909a
·
verified ·
1 Parent(s): 82ea511

Update genesis/molecule_viewer.py

Browse files
Files changed (1) hide show
  1. 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
- def fetch_pdb(pdb_id: str) -> Optional[str]:
6
- """Fetch PDB file from RCSB."""
7
- url = f"https://files.rcsb.org/download/{pdb_id.upper()}.pdb"
8
- r = requests.get(url)
9
- if r.status_code == 200:
10
- return r.text
11
- return None
 
 
 
 
 
12
 
13
- def fetch_alphafold(uniprot_id: str) -> Optional[str]:
14
- """Fetch PDB model from AlphaFold."""
15
- url = f"https://alphafold.ebi.ac.uk/files/AF-{uniprot_id}-F1-model_v4.pdb"
16
- r = requests.get(url)
17
- if r.status_code == 200:
18
- return r.text
19
- return None
 
 
20
 
21
- def render_3dmol(pdb_data: str) -> str:
22
- """Generate HTML for 3Dmol.js viewer with biotech neon styling."""
23
- return f"""
24
- <div id="viewer" style="width: 100%; height: 600px; background-color: black; border: 2px solid #00ff99; box-shadow: 0 0 20px #ff6600;"></div>
25
- <script src="https://3dmol.csb.pitt.edu/build/3Dmol-min.js"></script>
26
- <script>
27
- let viewer = $3Dmol.createViewer('viewer', {{ backgroundColor: 'black' }});
28
- let pdbData = `{pdb_data}`;
29
- viewer.addModel(pdbData, "pdb");
30
- viewer.setStyle({{}}, {{cartoon: {{color: 'spectrum'}}}});
31
- viewer.zoomTo();
32
- viewer.render();
33
- </script>
34
- """
35
 
36
- def get_molecule_view(pdb_or_gene: str) -> str:
37
- """Fetch molecule structure and return 3D viewer HTML."""
38
- # Try PDB ID first
39
- pdb_data = fetch_pdb(pdb_or_gene)
40
- if not pdb_data:
41
- # Try AlphaFold with UniProt ID or gene name
42
- pdb_data = fetch_alphafold(pdb_or_gene)
43
- if pdb_data:
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)