Anton Bushuiev commited on
Commit
dfeea55
·
1 Parent(s): f230748

Implement "Infinite colors"

Browse files
Files changed (1) hide show
  1. app.py +21 -10
app.py CHANGED
@@ -1,4 +1,5 @@
1
  import copy
 
2
  import tempfile
3
  from pathlib import Path
4
  from functools import partial
@@ -8,6 +9,7 @@ import torch
8
  from Bio.PDB.Polypeptide import protein_letters_3to1
9
  from biopandas.pdb import PandasPdb
10
  from colour import Color
 
11
 
12
  from mutils.proteins import AMINO_ACID_CODES_1
13
  from mutils.pdb import download_pdb
@@ -21,6 +23,9 @@ from ppiformer.utils.torch import fill_diagonal
21
  from ppiformer.definitions import PPIFORMER_WEIGHTS_DIR
22
 
23
 
 
 
 
24
  def process_inputs(inputs, temp_dir):
25
  pdb_code, pdb_path, partners, muts, muts_path = inputs
26
 
@@ -110,8 +115,13 @@ def plot_3dmol(pdb_path, ppi_path, muts, attn):
110
  zoom_atoms = []
111
 
112
  # Cartoon chains
113
- colors = [Color(c) for c in ['LimeGreen', 'HotPink', 'RoyalBlue']]
114
- chain_to_color = dict(zip(chains, colors))
 
 
 
 
 
115
  for chain in chains:
116
  styles.append([{"chain": chain}, {"cartoon": {"color": chain_to_color[chain].hex_l, "opacity": 0.6}}])
117
 
@@ -136,7 +146,7 @@ def plot_3dmol(pdb_path, ppi_path, muts, attn):
136
  # Convert style dicts to JS lines
137
  styles = ''.join(['viewer.addStyle(' + ', '.join([str(s).replace("'", '"') for s in dcts]) + ');\n' for dcts in styles])
138
 
139
- # Connert zoom atoms to 3DMol.js selection and add labels for mutated residues
140
  zoom_animation_duration = 500
141
  sel = '{\"or\": [' + ', '.join(["{\"serial\": " + str(a) + "}" for a in zoom_atoms]) + ']}'
142
  zoom = 'viewer.zoomTo(' + sel + ',' + f'{zoom_animation_duration});'
@@ -146,7 +156,7 @@ def plot_3dmol(pdb_path, ppi_path, muts, attn):
146
  label = protein_letters_3to1[row['residue_name']] + row['chain_id'] + str(row['residue_number']) + row['insertion']
147
  styles += 'viewer.addLabel(' + f"\"{label}\"," + "{fontSize:16, fontColor:\"red\", backgroundOpacity: 0.0}," + sel + ');\n'
148
 
149
- # Construct 3Dmol.js visualization script in HTML
150
  html = (
151
  """<!DOCTYPE html>
152
  <html>
@@ -207,7 +217,10 @@ def predict(models, temp_dir, *inputs):
207
  print(ppi_path, muts)
208
 
209
  # Predict
210
- ddg, attn = predict_ddg(models, ppi_path, muts, return_attn=True)
 
 
 
211
 
212
  # Create dataframe
213
  ddg = ddg.detach().numpy().tolist()
@@ -221,9 +234,6 @@ def predict(models, temp_dir, *inputs):
221
 
222
  app = gr.Blocks()
223
  with app:
224
- # print('app.theme.background_fill_primary', app.theme.background_fill_primary, type(app.theme.background_fill_primary))
225
- # print('app.theme.background_fill_primary', app.theme.background_fill_primary_dark, type(app.theme.background_fill_primary))
226
- # print(app.theme.to_dict())
227
 
228
  # Input GUI
229
  with gr.Row():
@@ -242,10 +252,11 @@ with app:
242
  examples = gr.Examples(
243
  examples=[
244
  ["1BUI", "A,B,C", "SC16A;FC47A;SC16A,FC47A"],
245
- ["1KNE", "A,P", ';'.join([f"TP6{a}" for a in AMINO_ACID_CODES_1])]
 
246
  ],
247
  inputs=[pdb_code, partners, muts],
248
- label="Examples (press line to fill)"
249
  )
250
 
251
  # Predict GUI
 
1
  import copy
2
+ import random
3
  import tempfile
4
  from pathlib import Path
5
  from functools import partial
 
9
  from Bio.PDB.Polypeptide import protein_letters_3to1
10
  from biopandas.pdb import PandasPdb
11
  from colour import Color
12
+ from colour import RGB_TO_COLOR_NAMES
13
 
14
  from mutils.proteins import AMINO_ACID_CODES_1
15
  from mutils.pdb import download_pdb
 
23
  from ppiformer.definitions import PPIFORMER_WEIGHTS_DIR
24
 
25
 
26
+ random.seed(0)
27
+
28
+
29
  def process_inputs(inputs, temp_dir):
30
  pdb_code, pdb_path, partners, muts, muts_path = inputs
31
 
 
115
  zoom_atoms = []
116
 
117
  # Cartoon chains
118
+ preferred_colors = ['LimeGreen', 'HotPink', 'RoyalBlue']
119
+ all_colors = [c[0] for c in RGB_TO_COLOR_NAMES.values()]
120
+ all_colors = [c for c in all_colors if c not in preferred_colors + ['Black', 'White']]
121
+ random.shuffle(all_colors)
122
+ all_colors = preferred_colors + all_colors
123
+ all_colors = [Color(c) for c in all_colors]
124
+ chain_to_color = dict(zip(chains, all_colors))
125
  for chain in chains:
126
  styles.append([{"chain": chain}, {"cartoon": {"color": chain_to_color[chain].hex_l, "opacity": 0.6}}])
127
 
 
146
  # Convert style dicts to JS lines
147
  styles = ''.join(['viewer.addStyle(' + ', '.join([str(s).replace("'", '"') for s in dcts]) + ');\n' for dcts in styles])
148
 
149
+ # Convert zoom atoms to 3DMol.js selection and add labels for mutated residues
150
  zoom_animation_duration = 500
151
  sel = '{\"or\": [' + ', '.join(["{\"serial\": " + str(a) + "}" for a in zoom_atoms]) + ']}'
152
  zoom = 'viewer.zoomTo(' + sel + ',' + f'{zoom_animation_duration});'
 
156
  label = protein_letters_3to1[row['residue_name']] + row['chain_id'] + str(row['residue_number']) + row['insertion']
157
  styles += 'viewer.addLabel(' + f"\"{label}\"," + "{fontSize:16, fontColor:\"red\", backgroundOpacity: 0.0}," + sel + ');\n'
158
 
159
+ # Construct 3Dmol.js visualization script embedded in HTML
160
  html = (
161
  """<!DOCTYPE html>
162
  <html>
 
217
  print(ppi_path, muts)
218
 
219
  # Predict
220
+ try:
221
+ ddg, attn = predict_ddg(models, ppi_path, muts, return_attn=True)
222
+ except:
223
+ raise gr.Error("Prediction failed. Please double check your inputs.")
224
 
225
  # Create dataframe
226
  ddg = ddg.detach().numpy().tolist()
 
234
 
235
  app = gr.Blocks()
236
  with app:
 
 
 
237
 
238
  # Input GUI
239
  with gr.Row():
 
252
  examples = gr.Examples(
253
  examples=[
254
  ["1BUI", "A,B,C", "SC16A;FC47A;SC16A,FC47A"],
255
+ ["1KNE", "A,P", ';'.join([f"TP6{a}" for a in AMINO_ACID_CODES_1])],
256
+ ["1C4Z", "A,B,C,D", "FA690A;KD100A"]
257
  ],
258
  inputs=[pdb_code, partners, muts],
259
+ label="Examples (press line to fill inputs)"
260
  )
261
 
262
  # Predict GUI