roni commited on
Commit
79b6488
·
1 Parent(s): dfecb5b

visualization enhancement

Browse files
Files changed (2) hide show
  1. app.py +42 -20
  2. protein_viz.py +5 -0
app.py CHANGED
@@ -8,6 +8,28 @@ model_repo = "ronig/protein_search_engine"
8
  engine = get_engine(index_repo, model_repo)
9
 
10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  def format_search_results(raw_search_results):
12
  formatted_search_results = {}
13
  for res in raw_search_results:
@@ -22,22 +44,10 @@ def format_search_results(raw_search_results):
22
  return formatted_search_results
23
 
24
 
25
- def limit_n_results(n):
26
- return max(min(n, 20), 1)
27
-
28
-
29
- def update_html(search_res):
30
- first_entry = search_res[0]
31
- pdb_id = first_entry['pdb_name']
32
- return render_html(pdb_id=pdb_id, chain=first_entry['chain_id'])
33
-
34
-
35
- def search_and_display(seq, n_res):
36
- n_res = int(limit_n_results(n_res))
37
- search_res = engine.search_by_sequence(seq, n=n_res)
38
- formatted_search_results = format_search_results(search_res)
39
- new_html = update_html(search_res)
40
- return formatted_search_results, new_html
41
 
42
 
43
  with gr.Blocks() as demo:
@@ -51,21 +61,33 @@ with gr.Blocks() as demo:
51
  with gr.Row():
52
  with gr.Column():
53
  seq_input = gr.Textbox(
54
- placeholder="KFLIYQMECSTMIFGL",
55
  label="Input Sequence"
56
  )
57
  n_results = gr.Number(5, label="N Results")
58
  search_button = gr.Button("Search", variant='primary')
59
  search_results = gr.Label(num_top_classes=20, label="Search Results")
 
 
 
 
 
60
  protein_viz = gr.HTML(
61
- value=render_html(pdb_id="2POR", chain='A'),
62
  label="Protein Visualization"
63
  )
64
- gr.Examples(["KFLIYQMECSTMIFGL"], inputs=[seq_input])
 
 
 
 
65
  search_button.click(
66
  search_and_display,
67
  inputs=[seq_input, n_results],
68
- outputs=[search_results, protein_viz]
 
 
 
69
  )
70
 
71
  if __name__ == "__main__":
 
8
  engine = get_engine(index_repo, model_repo)
9
 
10
 
11
+ def search_and_display(seq, n_res):
12
+ n_res = int(limit_n_results(n_res))
13
+ search_res = engine.search_by_sequence(seq, n=n_res)
14
+ results_options = update_dropdown_menu(search_res)
15
+ formatted_search_results = format_search_results(search_res)
16
+ return formatted_search_results, results_options
17
+
18
+
19
+ def limit_n_results(n):
20
+ return max(min(n, 20), 1)
21
+
22
+
23
+ def update_dropdown_menu(search_res):
24
+ choices = [
25
+ ','.join([row['pdb_name'], row['chain_id']])
26
+ for row in search_res
27
+ ]
28
+ return gr.Dropdown.update(
29
+ choices=choices, interactive=True, value=choices[0], visible=True
30
+ )
31
+
32
+
33
  def format_search_results(raw_search_results):
34
  formatted_search_results = {}
35
  for res in raw_search_results:
 
44
  return formatted_search_results
45
 
46
 
47
+ def switch_viz(new_choice):
48
+ pdb_id, chain = new_choice.split(',')
49
+ title_update = gr.Markdown.update(visible=True)
50
+ return render_html(pdb_id=pdb_id, chain=chain), title_update
 
 
 
 
 
 
 
 
 
 
 
 
51
 
52
 
53
  with gr.Blocks() as demo:
 
61
  with gr.Row():
62
  with gr.Column():
63
  seq_input = gr.Textbox(
64
+ value="KFLIYQMECSTMIFGL",
65
  label="Input Sequence"
66
  )
67
  n_results = gr.Number(5, label="N Results")
68
  search_button = gr.Button("Search", variant='primary')
69
  search_results = gr.Label(num_top_classes=20, label="Search Results")
70
+ viz_header = gr.Markdown("## Visualization", visible=False)
71
+ results_selector = gr.Dropdown(
72
+ choices=[], multiselect=False, visible=False,
73
+ label="Visualized Search Result"
74
+ )
75
  protein_viz = gr.HTML(
76
+ value=render_html(pdb_id=None, chain=None),
77
  label="Protein Visualization"
78
  )
79
+ gr.Examples([
80
+ "KFLIYQMECSTMIFGL",
81
+ "PHFAMPPIHEDHLE",
82
+ "AEERIISLD"
83
+ ], inputs=[seq_input])
84
  search_button.click(
85
  search_and_display,
86
  inputs=[seq_input, n_results],
87
+ outputs=[search_results, results_selector]
88
+ )
89
+ results_selector.change(
90
+ switch_viz, inputs=results_selector, outputs=[protein_viz, viz_header]
91
  )
92
 
93
  if __name__ == "__main__":
protein_viz.py CHANGED
@@ -1,4 +1,6 @@
1
  def render_html(pdb_id, chain):
 
 
2
  html = f"""
3
  "<html>
4
  <header>
@@ -8,7 +10,10 @@ def render_html(pdb_id, chain):
8
  <body>
9
  <div style="height: 400px; position: relative;" class="viewer_3Dmoljs"
10
  data-pdb="{pdb_id}"
 
 
11
  data-select1="chain:{chain}"
 
12
  data-style1="cartoon:color=spectrum"
13
  />
14
  </body>
 
1
  def render_html(pdb_id, chain):
2
+ if pdb_id is None or chain is None:
3
+ return ""
4
  html = f"""
5
  "<html>
6
  <header>
 
10
  <body>
11
  <div style="height: 400px; position: relative;" class="viewer_3Dmoljs"
12
  data-pdb="{pdb_id}"
13
+ data-backgroundalpha="0.0"
14
+ data-style="cartoon:color=white"
15
  data-select1="chain:{chain}"
16
+ data-zoomto="chain:{chain}"
17
  data-style1="cartoon:color=spectrum"
18
  />
19
  </body>