-
-
Notifications
You must be signed in to change notification settings - Fork 195
/
pdb_parser.py
98 lines (76 loc) · 2.61 KB
/
pdb_parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
"""PDB parser
This module contains a class that can read PDB files and return a dictionary of structural data"""
import re
import parmed as pmd
class PdbParser:
"""
Parse the protein data bank (PDB) file to generate
input data for Molecule3dViewer, Molecule2dViewer and Speck components.
@param pdb_path
Path to PDB file (either HTTP or local path).
"""
def __init__(self, s: str):
if re.search(r'^[1-9][0-9A-z]{3}$', s):
# d is PDB id (https://proteopedia.org/wiki/index.php/PDB_code)
self.structure = pmd.download_PDB(s)
else:
# d is HTTP url or local file
self.structure = pmd.load_file(s)
self.atoms = self.structure.atoms
self.bonds = self.structure.bonds
def mol3d_data(self) -> dict:
"""
Generate input data for Molecule3dViewer component.
"""
data = {'atoms': [], 'bonds': []}
for a in self.atoms:
data["atoms"].append({
"serial": a.idx,
"name": a.name,
"elem": a.element_name,
"positions": [a.xx, a.xy, a.xz],
"mass_magnitude": a.mass,
"residue_index": a.residue.idx,
"residue_name": a.residue.name,
"chain": a.residue.chain,
})
for b in self.bonds:
data["bonds"].append({
"atom1_index": b.atom1.idx,
"atom2_index": b.atom2.idx,
"bond_order": b.order
})
return data
def mol2d_data(self) -> dict:
"""
Generate input data for Molecule2dViewer component.
"""
data = {'nodes': [], 'links': []}
for a in self.atoms:
data["nodes"].append({
"id": a.idx,
"atom": a.element_name
})
for i, b in enumerate(self.bonds):
data["links"].append({
"id": i,
"source": b.atom1.idx,
"target": b.atom2.idx,
"bond": b.order,
"distance": b.measure(),
"strength": b.energy() # TODO verify
})
return data
def speck_data(self) -> list:
"""
Generate input data for Speck component.
"""
data = []
for a in self.atoms:
data.append({
"symbol": a.element_name,
"x": a.xx,
"y": a.xy,
"z": a.xz
})
return data