wangjin2000 commited on
Commit
228c475
·
verified ·
1 Parent(s): 722365c

Upload plot_pdb.py

Browse files
Files changed (1) hide show
  1. plot_pdb.py +55 -0
plot_pdb.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.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 plot_struc(sequence):
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