forked from haddocking/pdb-tools
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pdb_format.py
executable file
·149 lines (119 loc) · 4.88 KB
/
pdb_format.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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env python
"""
Validates each ATOM/HETATM line against the 'official' PDB format specs.
usage: python pdb_format.py <pdb file>
example: python pdb_format.py 1CTF.pdb
Author: {0} ({1})
This program is part of the PDB tools distributed with HADDOCK
or with the HADDOCK tutorial. The utilities in this package
can be used to quickly manipulate PDB files, with the benefit
of 'piping' several different commands. This is a rewrite of old
FORTRAN77 code that was taking too much effort to compile. RIP.
"""
from __future__ import print_function
import os
import re
import sys
__author__ = "Joao Rodrigues"
__email__ = "[email protected]"
USAGE = __doc__.format(__author__, __email__)
def check_input(args):
"""
Checks whether to read from stdin/file and validates user input/options.
"""
if not len(args):
# Read from pipe
if not sys.stdin.isatty():
pdbfh = sys.stdin
else:
sys.stderr.write(USAGE)
sys.exit(1)
elif len(args) == 1:
# File
if not os.path.isfile(args[0]):
sys.stderr.write('File not found: ' + args[0] + '\n')
sys.stderr.write(USAGE)
sys.exit(1)
pdbfh = open(args[0], 'r')
else:
sys.stderr.write(USAGE)
sys.exit(1)
return pdbfh
def _check_pdb_format(fhandle):
"""
Compares each ATOM/HETATM line with the format defined on the official
PDB website.
http://deposit.rcsb.org/adit/docs/pdb_atom_format.html
"""
has_error = False
_fmt_check = (
('Atm. Num.', (slice(6, 11), re.compile('[\d\s]+'))),
('Alt. Loc.', (slice(11, 12), re.compile('\s'))),
('Atm. Nam.', (slice(12, 16), re.compile('\s*[A-Z0-9]+\s*'))),
('Spacer #1', (slice(16, 17), re.compile('[A-Z0-9 ]{1}'))),
('Res. Nam.', (slice(17, 20), re.compile('\s*[A-Z0-9]+\s*'))),
('Spacer #2', (slice(20, 21), re.compile('\s'))),
('Chain Id.', (slice(21, 22), re.compile('[A-Za-z0-9 ]{1}'))),
('Res. Num.', (slice(22, 26), re.compile('\s*[\d]+\s*'))),
('Ins. Code', (slice(26, 27), re.compile('[A-Z0-9 ]{1}'))),
('Spacer #3', (slice(27, 30), re.compile('\s+'))),
('Coordn. X', (slice(30, 38), re.compile('\s*[\d\.\-]+\s*'))),
('Coordn. Y', (slice(38, 46), re.compile('\s*[\d\.\-]+\s*'))),
('Coordn. Z', (slice(46, 54), re.compile('\s*[\d\.\-]+\s*'))),
('Occupancy', (slice(54, 60), re.compile('\s*[\d\.\-]+\s*'))),
('Tmp. Fac.', (slice(60, 66), re.compile('\s*[\d\.\-]+\s*'))),
('Spacer #4', (slice(66, 72), re.compile('\s+'))),
('Segm. Id.', (slice(72, 76), re.compile('[\sA-Z0-9\-\+]+'))),
('At. Elemt', (slice(76, 78), re.compile('[\sA-Z0-9\-\+]+'))),
('At. Charg', (slice(78, 80), re.compile('[\sA-Z0-9\-\+]+'))),
)
def _make_pointer(column):
col_bg, col_en = column.start, column.stop
pt = ['^' if c in range(col_bg+1, col_en) else ' ' for c in range(80)]
return ''.join(pt)
for iline, line in enumerate(fhandle, start=1):
line = line.rstrip('\n').rstrip('\r') # CR/LF
if not line:
continue
# Type check for ATOM/HETATM lines
if line[0:6] in ('ATOM ', 'HETATM'):
linelen = len(line)
if linelen < 80:
print('[!] Line {0} is short: {1} < 80'.format(iline, linelen))
has_error = True
elif linelen > 80:
print('[!] Line {0} is long: {1} > 80'.format(iline, linelen))
has_error = True
for fname, (fcol, fcheck) in _fmt_check:
field = line[fcol]
if not fcheck.match(field):
pointer = _make_pointer(fcol)
emsg = '[!] Offending field ({0}) at line {1}'
print(emsg.format(fname, iline))
print('{0!r}'.format(line))
print('{0}'.format(pointer))
has_error = True
break
else:
# Do basic line length check
linelen = len(line)
if linelen < 80:
print('[!] Line {0} is short: {1} < 80'.format(iline, linelen))
has_error = True
elif linelen > 80:
print('[!] Line {0} is long: {1} > 80'.format(iline, linelen))
has_error = True
if has_error:
print('\nTo understand your errors, read the format specification:')
print(' http://deposit.rcsb.org/adit/docs/pdb_atom_format.html')
else:
print('It *seems* everything is OK.')
if __name__ == '__main__':
# Check Input
pdbfh = check_input(sys.argv[1:])
# Do the job
_check_pdb_format(pdbfh)
# last line of the script
# We can close it even if it is sys.stdin
pdbfh.close()
sys.exit(0)