simonduerr commited on
Commit
0c3025c
1 Parent(s): dbecb33

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -8
app.py CHANGED
@@ -5,7 +5,12 @@ from shutil import copyfile
5
  import gradio as gr
6
  import os
7
 
8
- def get_pdb(pdb_code="", filepath=""):
 
 
 
 
 
9
  if pdb_code is None or pdb_code == "":
10
  try:
11
  #move file to home folder to have it accessible from the web
@@ -14,8 +19,8 @@ def get_pdb(pdb_code="", filepath=""):
14
  except AttributeError as e:
15
  return None
16
  else:
17
- os.system(f"wget -qnc https://files.rcsb.org/view/{pdb_code}.pdb")
18
- return f"{pdb_code}.pdb"
19
 
20
 
21
  def read_mol(molpath):
@@ -103,10 +108,17 @@ def molecule(input_pdb, public_link):
103
  allowpaymentrequest="" frameborder="0" srcdoc='{x}'></iframe>"""
104
 
105
 
106
- def update(inp, file, public_link):
107
- pdb_path = get_pdb(inp, file)
108
  return molecule(pdb_path, public_link)
109
 
 
 
 
 
 
 
 
110
 
111
  demo = gr.Blocks()
112
 
@@ -117,12 +129,18 @@ with demo:
117
  public_link = gr.Variable(value="https://simonduerr-molstar-gradio.hf.space")
118
  with gr.Row():
119
  with gr.Box():
 
120
  inp = gr.Textbox(
121
- placeholder="PDB Code or upload file below", label="Input structure"
122
  )
123
  file = gr.File(file_count="single")
124
- gr.Examples(["2CBA", "6VXX"], inp)
 
 
 
 
 
125
  btn = gr.Button("View structure")
126
  mol = gr.HTML()
127
- btn.click(fn=update, inputs=[inp, file, public_link], outputs=mol)
128
  _, _, pl = demo.launch() # use public link with share=True locally
 
5
  import gradio as gr
6
  import os
7
 
8
+ def get_pdb(upload_choice="PDB Code", pdb_code="", filepath=""):
9
+ urls = {
10
+ "PDB Code": {base:"https://files.rcsb.org/view/", suffix:".pdb"},
11
+ "AlphaFold DB": {base:"https://alphafold.ebi.ac.uk/files/", suffix: "-F1-model_v4.pdb"}
12
+ "ESM Atlas": {base: "https://api.esmatlas.com/fetchPredictedStructure/", suffix:".pdb"}
13
+ }
14
  if pdb_code is None or pdb_code == "":
15
  try:
16
  #move file to home folder to have it accessible from the web
 
19
  except AttributeError as e:
20
  return None
21
  else:
22
+ os.system(f"wget -qnc {urls[upload_choice]['base']}{pdb_code}{urls[upload_choice]['suffix']}")
23
+ return f"{pdb_code}{urls[upload_choice]['suffix']}"
24
 
25
 
26
  def read_mol(molpath):
 
108
  allowpaymentrequest="" frameborder="0" srcdoc='{x}'></iframe>"""
109
 
110
 
111
+ def update(upload_choice, inp, file, public_link):
112
+ pdb_path = get_pdb(upload_choice, inp, file)
113
  return molecule(pdb_path, public_link)
114
 
115
+ def toggle_upload_input(choice):
116
+ if choice not "local file":
117
+ return gr.update(visible=True, value=None, placeholder=choice), gr.update(visible=False, value=None)
118
+ elif choice == "local file":
119
+ return gr.update(visible=False, value=None), gr.update(visible=True, value=None)
120
+
121
+
122
 
123
  demo = gr.Blocks()
124
 
 
129
  public_link = gr.Variable(value="https://simonduerr-molstar-gradio.hf.space")
130
  with gr.Row():
131
  with gr.Box():
132
+ upload_choice = gr.Radio(["PDB Code", "AlphaFold DB", "ESM Atlas","local file"], label="File source", value='PDB Code')
133
  inp = gr.Textbox(
134
+ placeholder="PDB Code", label="Input structure"
135
  )
136
  file = gr.File(file_count="single")
137
+ upload_choice.change(fn=toggle_upload_input,
138
+ inputs=[upload_choice],
139
+ outputs=[inp, file],
140
+ queue=False)
141
+
142
+ gr.Examples([["PDB code", "2CBA"], ["ESM Atlas", "MGYP001531319262"]], [upload_choice,inp])
143
  btn = gr.Button("View structure")
144
  mol = gr.HTML()
145
+ btn.click(fn=update, inputs=[upload_choice, inp, file, public_link], outputs=mol)
146
  _, _, pl = demo.launch() # use public link with share=True locally