-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathClass_line.py
More file actions
131 lines (113 loc) · 4.23 KB
/
Class_line.py
File metadata and controls
131 lines (113 loc) · 4.23 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
from helper import line_feed
from AmberMaps import Resi_Ele_map
class PDB_line(object):
'''
Class for decoding the line in the PDB file. Functions used internally.
Generate from:
line: PDB_line(line) --> PDB_line
lines: PDB_line.fromlines(lines) --> [PDB_line, ...]
'''
def __init__(self,line):
'''
initilize with a specific line in the PDB file.
Get self.line_type
'''
try:
self.line=line
self.line_type = self.line[0:6].strip()
# atom
self.atom_id = int(self.line[6:11])
self.atom_name = self.line[12:16].strip()
# residue
self.resi_name = self.line[17:20].strip()
self.resi_id = int(self.line[22:26])
# chain
self.chain_id = self.line[21:22]
# coord
self.atom_x = float(self.line[30:38])
self.atom_y = float(self.line[38:46])
self.atom_z = float(self.line[46:54])
except ValueError:
pass
@classmethod
def fromlines(cls, lines):
'''
capable with multiple lines
return a list of PDB_line object
'''
line_feed='\n' # potential update
line_list = lines.strip().split(line_feed)
pdb_l_list = []
for line in line_list:
pdb_l_list.append(cls(line))
return pdb_l_list
def build(self, ff='AMBER'):
'''
build a pdb line str
'''
self.get_alternate_location_indicator()
self.get_charge()
self.get_element()
self.get_insert_code()
self.get_occupancy()
self.get_seg_id()
self.get_temp_factor()
if ff == 'AMBER':
#build an amber style line here
l_type = '{:<6}'.format(self.line_type)
a_index = '{:>5d}'.format(self.atom_id)
if len(self.atom_name) > 3:
a_name = '{:<4}'.format(self.atom_name)
else:
a_name = '{:<3}'.format(self.atom_name)
a_name = ' '+a_name
r_name = '{:>3}'.format(self.resi_name)
c_index = self.chain_id
r_index = '{:>4d}'.format(self.resi_id)
x = '{:>8.3f}'.format(self.atom_x)
y = '{:>8.3f}'.format(self.atom_y)
z = '{:>8.3f}'.format(self.atom_z)
AL_id = '{:1}'.format(self.AL_id)
insert_code = '{:1}'.format(self.insert_code)
occupancy = '{:>6.2f}'.format(self.occupancy)
temp_factor = '{:>6.2f}'.format(self.temp_factor)
seg_id = '{:<4}'.format(self.seg_id)
element = '{:1}'.format(self.element)
charge = '{:2}'.format(self.charge)
#example: ATOM 5350 HB2 PRO 347 32.611 15.301 24.034 1.00 0.00
line = l_type + a_index +' '+a_name + AL_id + r_name+' '+ c_index + r_index + insert_code + ' ' + x + y + z + occupancy + temp_factor + ' ' + seg_id + element + charge + line_feed
return line
#misc
def get_alternate_location_indicator(self):
self.AL_id = self.line[16:17].strip()
return self.AL_id
def get_insert_code(self):
self.insert_code = self.line[26:27].strip()
return self.insert_code
def get_occupancy(self):
self.occupancy = self.line[54:60].strip()
if self.occupancy == '':
self.occupancy = 1.0
else:
self.occupancy = float(self.occupancy)
return self.occupancy
def get_temp_factor(self):
self.temp_factor = self.line[60:66].strip()
if self.temp_factor == '':
self.temp_factor = 0.0
else:
self.temp_factor = float(self.temp_factor)
return self.temp_factor
def get_seg_id(self):
self.seg_id = self.line[72:76].strip()
return self.seg_id
def get_element(self):
self.element = self.line[76:78].strip()
# try to get if not exist
# if self.element == '':
# if self.atom_name in Resi_Ele_map['Amber']:
# self.element = Resi_Ele_map['Amber'][self.atom_name]
return self.element
def get_charge(self):
self.charge = self.line[78:80].strip()
return self.charge