Tonic commited on
Commit
63a236d
1 Parent(s): ab3b1db

fix pdb utils

Browse files
Files changed (1) hide show
  1. app.py +44 -18
app.py CHANGED
@@ -126,6 +126,39 @@ def add_noise_to_coordinates(protein: ESMProtein, noise_level: float) -> ESMProt
126
  noisy_coordinates = coordinates + noise
127
  return ESMProtein(sequence=protein.sequence, coordinates=noisy_coordinates)
128
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
129
  def prediction_visualization(pdb_file, num_runs: int, noise_level: float, num_frames: int):
130
  protein = get_protein(pdb_file)
131
  runs = []
@@ -136,30 +169,20 @@ def prediction_visualization(pdb_file, num_runs: int, noise_level: float, num_fr
136
  for i in range(num_runs):
137
  structure_prediction = run_structure_prediction(noisy_protein)
138
  aligned, crmsd = align_after_prediction(protein, structure_prediction)
139
- runs.append((crmsd, aligned))
 
 
 
 
140
 
141
  best_aligned = sorted(runs)[0]
142
  view = visualize_after_pred(protein, best_aligned[1])
143
  return view, f"Best cRMSD: {best_aligned[0]:.4f}"
144
 
145
- def run_structure_prediction(protein: ESMProtein) -> ESMProtein:
146
- structure_prediction_config = GenerationConfig(
147
- track="structure",
148
- num_steps=40,
149
- temperature=0.7,
150
- )
151
- structure_prediction = model.generate(protein, structure_prediction_config)
152
- return structure_prediction
153
-
154
- def align_after_prediction(protein: ESMProtein, structure_prediction: ESMProtein) -> tuple[ESMProtein, float]:
155
- structure_prediction_chain = structure_prediction.to_protein_chain()
156
- protein_chain = protein.to_protein_chain()
157
- structure_indices = np.arange(0, len(structure_prediction_chain.sequence))
158
- aligned_chain = structure_prediction_chain.align(protein_chain, mobile_inds=structure_indices, target_inds=structure_indices)
159
- crmsd = structure_prediction_chain.rmsd(protein_chain, mobile_inds=structure_indices, target_inds=structure_indices)
160
- return ESMProtein.from_protein_chain(aligned_chain), crmsd
161
-
162
  def visualize_after_pred(protein: ESMProtein, aligned: ESMProtein):
 
 
 
163
  view = py3Dmol.view(width=800, height=600)
164
  view.addModel(protein.to_pdb_string(), "pdb")
165
  view.setStyle({"cartoon": {"color": "lightgrey"}})
@@ -175,6 +198,9 @@ def run_prediction(pdb_file, num_runs, noise_level, num_frames):
175
  return "Please upload a PDB file.", "No file uploaded"
176
 
177
  view, crmsd_text = prediction_visualization(pdb_file, num_runs, noise_level, num_frames)
 
 
 
178
  html = view._make_html()
179
  return f"""
180
  <div style="height: 600px;">
 
126
  noisy_coordinates = coordinates + noise
127
  return ESMProtein(sequence=protein.sequence, coordinates=noisy_coordinates)
128
 
129
+ def run_structure_prediction(protein: ESMProtein) -> ESMProtein:
130
+ structure_prediction_config = GenerationConfig(
131
+ track="structure",
132
+ num_steps=40,
133
+ temperature=0.7,
134
+ )
135
+ try:
136
+ structure_prediction = model.generate(protein, structure_prediction_config)
137
+ if isinstance(structure_prediction, ESMProtein):
138
+ return structure_prediction
139
+ else:
140
+ raise ValueError(f"Unexpected result type: {type(structure_prediction)}")
141
+ except Exception as e:
142
+ print(f"Error during structure prediction: {str(e)}")
143
+ return None
144
+
145
+ def align_after_prediction(protein: ESMProtein, structure_prediction: ESMProtein) -> tuple[ESMProtein, float]:
146
+ if structure_prediction is None:
147
+ return None, float('inf')
148
+
149
+ try:
150
+ structure_prediction_chain = structure_prediction.to_protein_chain()
151
+ protein_chain = protein.to_protein_chain()
152
+ structure_indices = np.arange(0, len(structure_prediction_chain.sequence))
153
+ aligned_chain = structure_prediction_chain.align(protein_chain, mobile_inds=structure_indices, target_inds=structure_indices)
154
+ crmsd = structure_prediction_chain.rmsd(protein_chain, mobile_inds=structure_indices, target_inds=structure_indices)
155
+ return ESMProtein.from_protein_chain(aligned_chain), crmsd
156
+ except AttributeError as e:
157
+ print(f"Error during alignment: {str(e)}")
158
+ print(f"Structure prediction type: {type(structure_prediction)}")
159
+ print(f"Structure prediction attributes: {dir(structure_prediction)}")
160
+ return None, float('inf')
161
+
162
  def prediction_visualization(pdb_file, num_runs: int, noise_level: float, num_frames: int):
163
  protein = get_protein(pdb_file)
164
  runs = []
 
169
  for i in range(num_runs):
170
  structure_prediction = run_structure_prediction(noisy_protein)
171
  aligned, crmsd = align_after_prediction(protein, structure_prediction)
172
+ if aligned is not None:
173
+ runs.append((crmsd, aligned))
174
+
175
+ if not runs:
176
+ return None, "No successful predictions"
177
 
178
  best_aligned = sorted(runs)[0]
179
  view = visualize_after_pred(protein, best_aligned[1])
180
  return view, f"Best cRMSD: {best_aligned[0]:.4f}"
181
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
182
  def visualize_after_pred(protein: ESMProtein, aligned: ESMProtein):
183
+ if aligned is None:
184
+ return py3Dmol.view(width=800, height=600)
185
+
186
  view = py3Dmol.view(width=800, height=600)
187
  view.addModel(protein.to_pdb_string(), "pdb")
188
  view.setStyle({"cartoon": {"color": "lightgrey"}})
 
198
  return "Please upload a PDB file.", "No file uploaded"
199
 
200
  view, crmsd_text = prediction_visualization(pdb_file, num_runs, noise_level, num_frames)
201
+ if view is None:
202
+ return "No successful predictions were made. Try adjusting the parameters.", crmsd_text
203
+
204
  html = view._make_html()
205
  return f"""
206
  <div style="height: 600px;">