wangjin2000 commited on
Commit
d21699a
1 Parent(s): 25807af

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +120 -0
app.py ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import py3Dmol
4
+
5
+ def display_pdb_by_pdb(pdb):
6
+ # function to display pdb in py3dmol
7
+ # ref: https://huggingface.co/spaces/AIGE/A_B
8
+
9
+ view = py3Dmol.view(width=500, height=500)
10
+ view.addModel(pdb, "pdb")
11
+ view.setStyle({'cartoon': {'color': 'spectrum'}})
12
+ view.zoomTo()
13
+ output = view._make_html().replace("'", '"')
14
+ x = f"""<!DOCTYPE html><html></center> {output} </center></html>""" # do not use ' in this input
15
+
16
+ return f"""<iframe height="500px" width="100%" name="result" allow="midi; geolocation; microphone; camera;
17
+ display-capture; encrypted-media;" sandbox="allow-modals allow-forms
18
+ allow-scripts allow-same-origin allow-popups
19
+ allow-top-navigation-by-user-activation allow-downloads" allowfullscreen=""
20
+ allowpaymentrequest="" frameborder="0" srcdoc='{x}'></iframe>"""
21
+
22
+ def get_pdb(sequence):
23
+ retries = 0
24
+ pdb_str = None
25
+ url = "https://api.esmatlas.com/foldSequence/v1/pdb/"
26
+ while retries < 3 and pdb_str is None:
27
+ response = requests.post(url, data=sequence, verify=False)
28
+ pdb_str = response.text
29
+ if pdb_str == "INTERNAL SERVER ERROR":
30
+ retries += 1
31
+ time.sleep(0.1)
32
+ pdb = str = None
33
+ return pdb_str
34
+
35
+ def update(sequence=DEFAULT_SEQ):
36
+ headers = {
37
+ 'Content-Type': 'application/x-www-form-urlencoded',
38
+ }
39
+
40
+ response = requests.post('https://api.esmatlas.com/foldSequence/v1/pdb/', headers=headers, data=sequence, verify=False) #verify=false jw 0425 work around for SSL certificate
41
+
42
+ pdb_string = get_pdb(sequence)
43
+ name = sequence[:3] + sequence[-3:]
44
+
45
+ outpath = (
46
+ Path.cwd() / f"PDB-{name}.pdb")
47
+
48
+ with open(outpath.name, "w") as f:
49
+ f.write(pdb_string)
50
+
51
+ outpath_str = str(outpath)
52
+
53
+ html_view = display_pdb_by_pdb(pdb_string)
54
+
55
+ return outpath_str, html_view
56
+
57
+ def suggest(option):
58
+ if option == "Plastic degradation protein":
59
+ suggestion = "MGSSHHHHHHSSGLVPRGSHMRGPNPTAASLEASAGPFTVRSFTVSRPSGYGAGTVYYPTNAGGTVGAIAIVPGYTARQSSIKWWGPRLASHGFVVITIDTNSTLDQPSSRSSQQMAALRQVASLNGTSSSPIYGKVDTARMGVMGWSMGGGGSLISAANNPSLKAAAPQAPWDSSTNFSSVTVPTLIFACENDSIAPVNSSALPIYDSMSRNAKQFLEINGGSHSCANSGNSNQALIGKKGVAWMKRFMDNDTRYSTFACENPNSTRVSDFRTANCSLEDPAANKARKEAELAAATAEQ"
60
+ elif option == "Antifreeze protein":
61
+ suggestion = "QCTGGADCTSCTGACTGCGNCPNAVTCTNSQHCVKANTCTGSTDCNTAQTCTNSKDCFEANTCTDSTNCYKATACTNSSGCPGH"
62
+ elif option == "AI Generated protein":
63
+ suggestion = "MSGMKKLYEYTVTTLDEFLEKLKEFILNTSKDKIYKLTITNPKLIKDIGKAIAKAAEIADVDPKEIEEMIKAVEENELTKLVITIEQTDDKYVIKVELENEDGLVHSFEIYFKNKEEMEKFLELLEKLISKLSGS"
64
+ elif option == "7-bladed propeller fold":
65
+ suggestion = "VKLAGNSSLCPINGWAVYSKDNSIRIGSKGDVFVIREPFISCSHLECRTFFLTQGALLNDKHSNGTVKDRSPHRTLMSCPVGEAPSPYNSRFESVAWSASACHDGTSWLTIGISGPDNGAVAVLKYNGIITDTIKSWRNNILRTQESECACVNGSCFTVMTDGPSNGQASYKIFKMEKGKVVKSVELDAPNYHYEECSCYPNAGEITCVCRDNWHGSNRPWVSFNQNLEYQIGYICSGVFGDNPRPNDGTGSCGPVSSNGAYGVKGFSFKYGNGVWIGRTKSTNSRSGFEMIWDPNGWTETDSSFSVKQDIVAITDWSGYSGSFVQHPELTGLDCIRPCFWVELIRGRPKESTIWTSGSSISFCGVNSDTVGWSWPDGAELPFTIDK"
66
+ else:
67
+ suggestion = ""
68
+ return suggestion
69
+
70
+ DEFAULT_SEQ = "MGSSHHHHHHSSGLVPRGSHMRGPNPTAASLEASAGPFTVRSFTVSRPSGYGAGTVYYPTNAGGTVGAIAIVPGYTARQSSIKWWGPRLASHGFVVITIDTNSTLDQPSSRSSQQMAALRQVASLNGTSSSPIYGKVDTARMGVMGWSMGGGGSLISAANNPSLKAAAPQAPWDSSTNFSSVTVPTLIFACENDSIAPVNSSALPIYDSMSRNAKQFLEINGGSHSCANSGNSNQALIGKKGVAWMKRFMDNDTRYSTFACENPNSTRVSDFRTANCSLEDPAANKARKEAELAAATAEQ"
71
+
72
+ demo = gr.Blocks()
73
+
74
+ with demo:
75
+ gr.HTML("""<div style="text-align: center; max-width: 700px; margin: 0 auto;">
76
+ <div
77
+ style="
78
+ display: inline-flex;
79
+ align-items: center;
80
+ gap: 0.8rem;
81
+ font-size: 1.75rem;
82
+ "
83
+ >
84
+ <h1 style="font-weight: 900; margin-bottom: 7px; margin-top: 5px;">
85
+ ESMFold Protein Folding Structure
86
+ </h1>
87
+ </div>
88
+ <p style="margin-bottom: 10px; font-size: 94%">
89
+ You can input a single protein sequence and you get the predicted protein structure
90
+ </p>
91
+ </div>""")
92
+ name = gr.Dropdown(label="Choose a Sample Protein", value="Plastic degradation protein", choices=["Antifreeze protein", "Plastic degradation protein", "AI Generated protein", "7-bladed propeller fold", "custom"])
93
+ with gr.Row():
94
+ inp = gr.Textbox(label="Protein sequence", lines=3, value=DEFAULT_SEQ, placeholder="Paste your protein sequence here...")
95
+ btn = gr.Button("Plot Predicted Structure ").style(full_width=False)
96
+
97
+ with gr.Row():
98
+ PDB_string = gr.Textbox(
99
+ lines=4,
100
+ max_lines=120,
101
+ label="PDB_string Output"
102
+ )
103
+ with gr.Row():
104
+ output_file = gr.File(
105
+ label="Download PDB Structure as Text File",
106
+ file_count="single",
107
+ type="filepath",
108
+ interactive=False,
109
+ )
110
+ output_viewer = gr.HTML()
111
+
112
+ btn.click(fn=update, inputs=inp, outputs=[output_file, output_viewer])
113
+
114
+ name.change(fn=suggest, inputs=name, outputs=inp)
115
+ inp.change(fn=update, inputs=inp, outputs=output_file)
116
+
117
+ gr.Markdown("A demo of [ESM](https://esmatlas.com/about) by Meta using the API. You can also use ESM in Hugging Face `transformers` as shown [here](https://github.com/huggingface/notebooks/blob/ab81a52182acf691e6743a50bc47bd1c1622086f/examples/protein_folding.ipynb), which is supported since [v4.24](https://github.com/huggingface/transformers/releases/tag/v4.24.0).")
118
+
119
+
120
+ demo.launch()