GotThatData commited on
Commit
10a0a58
·
verified ·
1 Parent(s): 446e924

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +51 -21
app.py CHANGED
@@ -15,7 +15,6 @@ Website: https://bioprime.one
15
  """
16
 
17
  import gradio as gr
18
- import py3Dmol
19
  import requests
20
  import json
21
  import hashlib
@@ -184,33 +183,64 @@ def fetch_pdb_structure(pdb_id: str) -> Optional[str]:
184
 
185
 
186
  def create_3d_viewer(pdb_content: str, binding_site: dict = None, style: str = "cartoon") -> str:
187
- """Create 3D molecular viewer HTML."""
188
- view = py3Dmol.view(width=700, height=500)
189
- view.addModel(pdb_content, "pdb")
190
 
 
 
 
 
191
  if style == "cartoon":
192
- view.setStyle({'cartoon': {'color': 'spectrum'}})
193
  elif style == "surface":
194
- view.setStyle({'cartoon': {'color': 'spectrum'}})
195
- view.addSurface(py3Dmol.SAS, {'opacity': 0.7, 'color': 'white'})
 
 
196
  elif style == "stick":
197
- view.setStyle({'stick': {'colorscheme': 'Jmol'}})
198
  elif style == "sphere":
199
- view.setStyle({'sphere': {'colorscheme': 'Jmol', 'scale': 0.3}})
 
 
200
 
201
- # Highlight binding site if provided
 
202
  if binding_site:
203
- view.addSphere({
204
- 'center': {'x': binding_site['x'], 'y': binding_site['y'], 'z': binding_site['z']},
205
- 'radius': 8,
206
- 'color': 'red',
207
- 'opacity': 0.3
208
- })
209
-
210
- view.zoomTo()
211
- view.spin(False)
212
-
213
- return view._make_html()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
214
 
215
 
216
  def generate_docking_result(target_id: str, compound_ids: List[str]) -> Tuple[str, str, str]:
 
15
  """
16
 
17
  import gradio as gr
 
18
  import requests
19
  import json
20
  import hashlib
 
183
 
184
 
185
  def create_3d_viewer(pdb_content: str, binding_site: dict = None, style: str = "cartoon") -> str:
186
+ """Create 3D molecular viewer HTML with embedded 3Dmol.js library."""
187
+ import html
 
188
 
189
+ # Escape PDB content for JavaScript
190
+ pdb_escaped = html.escape(pdb_content).replace('\n', '\\n').replace('\r', '').replace("'", "\\'")
191
+
192
+ # Build style configuration
193
  if style == "cartoon":
194
+ style_js = "viewer.setStyle({}, {cartoon: {color: 'spectrum'}});"
195
  elif style == "surface":
196
+ style_js = """
197
+ viewer.setStyle({}, {cartoon: {color: 'spectrum'}});
198
+ viewer.addSurface($3Dmol.SAS, {opacity: 0.7, color: 'white'});
199
+ """
200
  elif style == "stick":
201
+ style_js = "viewer.setStyle({}, {stick: {colorscheme: 'Jmol'}});"
202
  elif style == "sphere":
203
+ style_js = "viewer.setStyle({}, {sphere: {colorscheme: 'Jmol', scale: 0.3}});"
204
+ else:
205
+ style_js = "viewer.setStyle({}, {cartoon: {color: 'spectrum'}});"
206
 
207
+ # Binding site sphere
208
+ binding_site_js = ""
209
  if binding_site:
210
+ binding_site_js = f"""
211
+ viewer.addSphere({{
212
+ center: {{x: {binding_site['x']}, y: {binding_site['y']}, z: {binding_site['z']}}},
213
+ radius: 8,
214
+ color: 'red',
215
+ opacity: 0.3
216
+ }});
217
+ """
218
+
219
+ # Generate unique ID for the viewer
220
+ import random
221
+ viewer_id = f"viewer_{random.randint(100000, 999999)}"
222
+
223
+ html_content = f'''
224
+ <div style="width: 100%; display: flex; justify-content: center;">
225
+ <div id="{viewer_id}" style="width: 700px; height: 500px; position: relative; border-radius: 12px; overflow: hidden; background: #1a1a2e;"></div>
226
+ </div>
227
+ <script src="https://3dmol.org/build/3Dmol-min.js"></script>
228
+ <script>
229
+ (function() {{
230
+ var pdbData = '{pdb_escaped}';
231
+ var element = document.getElementById('{viewer_id}');
232
+ var config = {{ backgroundColor: '0x1a1a2e' }};
233
+ var viewer = $3Dmol.createViewer(element, config);
234
+ viewer.addModel(pdbData, "pdb");
235
+ {style_js}
236
+ {binding_site_js}
237
+ viewer.zoomTo();
238
+ viewer.render();
239
+ }})();
240
+ </script>
241
+ '''
242
+
243
+ return html_content
244
 
245
 
246
  def generate_docking_result(target_id: str, compound_ids: List[str]) -> Tuple[str, str, str]: