Tonic commited on
Commit
86c05eb
·
unverified ·
1 Parent(s): f2448d2

fix visualiser

Browse files
Files changed (1) hide show
  1. app.py +39 -20
app.py CHANGED
@@ -14,6 +14,8 @@ from esm.utils import residue_constants as RC
14
  import requests
15
  from dotenv import load_dotenv
16
  import torch
 
 
17
 
18
  load_dotenv()
19
 
@@ -187,15 +189,16 @@ def align_after_prediction(protein: ESMProtein, structure_prediction: ESMProtein
187
 
188
  def visualize_after_pred(protein: ESMProtein, aligned: ESMProtein):
189
  if aligned is None:
190
- return py3Dmol.view(width=800, height=600)
 
 
 
 
 
 
 
191
 
192
- view = py3Dmol.view(width=800, height=600)
193
- view.addModel(protein_to_pdb(protein), "pdb")
194
- view.setStyle({"cartoon": {"color": "lightgrey"}})
195
- view.addModel(protein_to_pdb(aligned), "pdb")
196
- view.setStyle({"model": 1}, {"cartoon": {"color": "lightgreen"}})
197
- view.zoomTo()
198
- return view
199
 
200
  def protein_to_pdb(protein: ESMProtein):
201
  pdb_str = ""
@@ -206,42 +209,58 @@ def protein_to_pdb(protein: ESMProtein):
206
  pdb_str += f"ATOM {i*37+j+1:5d} {atom:3s} {aa:3s} A{i+1:4d} {x:8.3f}{y:8.3f}{z:8.3f}\n"
207
  return pdb_str
208
 
209
- def prediction_visualization(pdb_file, num_runs: int, noise_level: float, num_frames: int):
210
  protein = get_protein(pdb_file)
211
  runs = []
212
 
213
- for frame in range(num_frames):
 
 
 
214
  noisy_protein = add_noise_to_coordinates(protein, noise_level)
215
 
216
  for i in range(num_runs):
 
217
  structure_prediction = run_structure_prediction(noisy_protein)
218
  if structure_prediction is not None:
219
  aligned, crmsd = align_after_prediction(protein, structure_prediction)
220
  if aligned is not None:
221
  runs.append((crmsd, aligned))
 
222
 
223
  if not runs:
224
  return None, "No successful predictions"
225
 
226
  best_aligned = sorted(runs, key=lambda x: x[0])[0]
227
- view = visualize_after_pred(protein, best_aligned[1])
228
- return view, f"Best cRMSD: {best_aligned[0]:.4f}"
229
 
230
- def run_prediction(pdb_file, num_runs, noise_level, num_frames):
231
  try:
232
  if pdb_file is None:
233
  return "Please upload a PDB file.", "No file uploaded"
234
 
235
- view, crmsd_text = prediction_visualization(pdb_file, num_runs, noise_level, num_frames)
236
- if view is None:
 
237
  return "No successful predictions were made. Try adjusting the parameters or check the PDB file.", crmsd_text
238
 
239
- html = view._make_html()
240
- return f"""
241
- <div style="height: 600px;">
242
- {html}
 
 
 
 
 
 
 
 
243
  </div>
244
- """, crmsd_text
 
 
245
  except Exception as e:
246
  error_message = str(e)
247
  stack_trace = traceback.format_exc()
 
14
  import requests
15
  from dotenv import load_dotenv
16
  import torch
17
+ import json
18
+ import time
19
 
20
  load_dotenv()
21
 
 
189
 
190
  def visualize_after_pred(protein: ESMProtein, aligned: ESMProtein):
191
  if aligned is None:
192
+ return None
193
+
194
+ viewer = py3Dmol.view(width=800, height=600)
195
+ viewer.addModel(protein_to_pdb(protein), "pdb")
196
+ viewer.setStyle({"cartoon": {"color": "lightgrey"}})
197
+ viewer.addModel(protein_to_pdb(aligned), "pdb")
198
+ viewer.setStyle({"model": -1}, {"cartoon": {"color": "lightgreen"}})
199
+ viewer.zoomTo()
200
 
201
+ return viewer.render()
 
 
 
 
 
 
202
 
203
  def protein_to_pdb(protein: ESMProtein):
204
  pdb_str = ""
 
209
  pdb_str += f"ATOM {i*37+j+1:5d} {atom:3s} {aa:3s} A{i+1:4d} {x:8.3f}{y:8.3f}{z:8.3f}\n"
210
  return pdb_str
211
 
212
+ def prediction_visualization(pdb_file, num_runs: int, noise_level: float, num_frames: int, progress=gr.Progress()):
213
  protein = get_protein(pdb_file)
214
  runs = []
215
 
216
+ total_iterations = num_frames * num_runs
217
+ progress(0, desc="Starting predictions")
218
+
219
+ for frame in progress.tqdm(range(num_frames), desc="Processing frames"):
220
  noisy_protein = add_noise_to_coordinates(protein, noise_level)
221
 
222
  for i in range(num_runs):
223
+ progress((frame * num_runs + i + 1) / total_iterations, desc=f"Frame {frame+1}, Run {i+1}")
224
  structure_prediction = run_structure_prediction(noisy_protein)
225
  if structure_prediction is not None:
226
  aligned, crmsd = align_after_prediction(protein, structure_prediction)
227
  if aligned is not None:
228
  runs.append((crmsd, aligned))
229
+ time.sleep(0.1) # Small delay to allow for UI updates
230
 
231
  if not runs:
232
  return None, "No successful predictions"
233
 
234
  best_aligned = sorted(runs, key=lambda x: x[0])[0]
235
+ view_data = visualize_after_pred(protein, best_aligned[1])
236
+ return view_data, f"Best cRMSD: {best_aligned[0]:.4f}"
237
 
238
+ def run_prediction(pdb_file, num_runs, noise_level, num_frames, progress=gr.Progress()):
239
  try:
240
  if pdb_file is None:
241
  return "Please upload a PDB file.", "No file uploaded"
242
 
243
+ progress(0, desc="Starting prediction")
244
+ view_data, crmsd_text = prediction_visualization(pdb_file, num_runs, noise_level, num_frames, progress)
245
+ if view_data is None:
246
  return "No successful predictions were made. Try adjusting the parameters or check the PDB file.", crmsd_text
247
 
248
+ progress(0.9, desc="Rendering visualization")
249
+ html_content = f"""
250
+ <div style="height: 600px; width: 100%;">
251
+ <script src="https://3dmol.csb.pitt.edu/build/3Dmol-min.js"></script>
252
+ <div id="container-{id(view_data)}" style="height: 100%; width: 100%; position: relative;"></div>
253
+ <script>
254
+ var viewer = $3Dmol.createViewer(document.getElementById("container-{id(view_data)}"), {{defaultcolors: $3Dmol.rasmolElementColors}});
255
+ viewer.addModel({json.dumps(view_data['pdb'])}, "pdb");
256
+ viewer.setStyle({{}}, {json.dumps(view_data['style'])});
257
+ viewer.zoomTo();
258
+ viewer.render();
259
+ </script>
260
  </div>
261
+ """
262
+ progress(1.0, desc="Completed")
263
+ return html_content, crmsd_text
264
  except Exception as e:
265
  error_message = str(e)
266
  stack_trace = traceback.format_exc()