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

Implement text labels and more examples

Browse files
Files changed (1) hide show
  1. app.py +26 -10
app.py CHANGED
@@ -5,10 +5,11 @@ from functools import partial
5
 
6
  import gradio as gr
7
  import torch
8
- import py3Dmol
9
  from biopandas.pdb import PandasPdb
10
  from colour import Color
11
 
 
12
  from mutils.pdb import download_pdb
13
  from mutils.mutations import Mutation
14
  from ppiref.extraction import PPIExtractor
@@ -121,16 +122,29 @@ def plot_3dmol(pdb_path, ppi_path, muts, attn):
121
  color.saturation = row['attn']
122
  color = color.hex_l
123
  if row['mutated']:
124
- styles.append([{'chain': row['chain_id'], 'resi': str(row['residue_number'])}, {'stick': {'color': 'red', 'radius': 0.2, 'opacity': 1.0}}])
 
 
 
125
  zoom_atoms.append(row['atom_number'])
126
  else:
127
- styles.append([{'chain': row['chain_id'], 'resi': str(row['residue_number'])}, {'stick': {'color': color, 'radius': row['attn'] / 5, 'opacity': row['attn']}}])
 
 
 
128
 
129
  # Convert style dicts to JS lines
130
- styles = '\n'.join(['viewer.addStyle(' + ', '.join([str(s).replace("'", '"') for s in dcts]) + ');' for dcts in styles])
131
-
132
- # Connert zoom atoms to 3DMol.js selection
133
- zoom = 'viewer.zoomTo({\"or\": [' + ', '.join(["{\"serial\": " + str(a) + "}" for a in zoom_atoms]) + ']}, 1000);'
 
 
 
 
 
 
 
134
 
135
  # Construct 3Dmol.js visualization script in HTML
136
  html = (
@@ -164,10 +178,9 @@ def plot_3dmol(pdb_path, ppi_path, muts, attn):
164
 
165
  $(document).ready(function () {
166
  let element = $("#container");
167
- let config = { backgroundColor: "white" };
168
  let viewer = $3Dmol.createViewer(element, config);
169
  viewer.addModel(pdb, "pdb");
170
- viewer.setBackgroundColor("black");
171
  viewer.setStyle({"model": 0}, {"ray_opaque_background": "off"}, {"stick": {"color": "lightgrey", "opacity": 0.5}});
172
  """
173
  + styles
@@ -227,7 +240,10 @@ with app:
227
  muts_path = gr.File(file_count="single", label="Or file with mutations")
228
 
229
  examples = gr.Examples(
230
- examples=[["1BUI", "A,B,C", "SC16A;FC47A;SC16A,FC47A"]],
 
 
 
231
  inputs=[pdb_code, partners, muts],
232
  label="Examples (press line to fill)"
233
  )
 
5
 
6
  import gradio as gr
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
14
  from mutils.mutations import Mutation
15
  from ppiref.extraction import PPIExtractor
 
122
  color.saturation = row['attn']
123
  color = color.hex_l
124
  if row['mutated']:
125
+ styles.append([
126
+ {'chain': row['chain_id'], 'resi': str(row['residue_number'])},
127
+ {'stick': {'color': 'red', 'radius': 0.2, 'opacity': 1.0}}
128
+ ])
129
  zoom_atoms.append(row['atom_number'])
130
  else:
131
+ styles.append([
132
+ {'chain': row['chain_id'], 'resi': str(row['residue_number'])},
133
+ {'stick': {'color': color, 'radius': row['attn'] / 5, 'opacity': row['attn']}}
134
+ ])
135
 
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});'
143
+ for atom in zoom_atoms:
144
+ sel = '{\"serial\": ' + str(atom) + '}'
145
+ row = ppi_df[ppi_df['atom_number'] == atom].iloc[0]
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 = (
 
178
 
179
  $(document).ready(function () {
180
  let element = $("#container");
181
+ let config = { backgroundColor: "black" };
182
  let viewer = $3Dmol.createViewer(element, config);
183
  viewer.addModel(pdb, "pdb");
 
184
  viewer.setStyle({"model": 0}, {"ray_opaque_background": "off"}, {"stick": {"color": "lightgrey", "opacity": 0.5}});
185
  """
186
  + styles
 
240
  muts_path = gr.File(file_count="single", label="Or file with mutations")
241
 
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
  )