wangjin2000 commited on
Commit
1ab89f3
·
verified ·
1 Parent(s): 521a83f

Upload plot_pdb.py

Browse files
Files changed (1) hide show
  1. plot_pdb.py +56 -0
plot_pdb.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import py3Dmol
2
+ import requests
3
+ from pathlib import Path
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.setBackgroundColor('black');
11
+ view.addModel(pdb, "pdb")
12
+ view.setStyle({'cartoon': {'color': 'spectrum'}})
13
+ view.zoomTo()
14
+ output = view._make_html().replace("'", '"')
15
+ x = f"""<!DOCTYPE html><html></center> {output} </center></html>""" # do not use ' in this input
16
+
17
+ return f"""<iframe height="500px" width="100%" name="result" allow="midi; geolocation; microphone; camera;
18
+ display-capture; encrypted-media;" sandbox="allow-modals allow-forms
19
+ allow-scripts allow-same-origin allow-popups
20
+ allow-top-navigation-by-user-activation allow-downloads" allowfullscreen=""
21
+ allowpaymentrequest="" frameborder="0" srcdoc='{x}'></iframe>"""
22
+
23
+ def get_pdb(sequence):
24
+ retries = 0
25
+ pdb_str = None
26
+ url = "https://api.esmatlas.com/foldSequence/v1/pdb/"
27
+ while retries < 3 and pdb_str is None:
28
+ response = requests.post(url, data=sequence, verify=False)
29
+ pdb_str = response.text
30
+ if pdb_str == "INTERNAL SERVER ERROR":
31
+ retries += 1
32
+ time.sleep(0.1)
33
+ pdb = str = None
34
+ return pdb_str
35
+
36
+ def plot_struc(sequence):
37
+ headers = {
38
+ 'Content-Type': 'application/x-www-form-urlencoded',
39
+ }
40
+
41
+ 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
42
+
43
+ pdb_string = get_pdb(sequence)
44
+ name = sequence[:3] + sequence[-3:]
45
+
46
+ outpath = (
47
+ Path.cwd() / f"PDB-{name}.pdb")
48
+
49
+ with open(outpath.name, "w") as f:
50
+ f.write(pdb_string)
51
+
52
+ outpath_str = str(outpath)
53
+
54
+ html_view = display_pdb_by_pdb(pdb_string)
55
+
56
+ return outpath_str, html_view