forked from haddocking/pdb-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pdb_wc.py
executable file
·172 lines (139 loc) · 4.99 KB
/
pdb_wc.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/env python
"""
Summarizes the contents of a PDB file.
usage: python pdb_wc.py -[acrmHgdiA] <pdb file>
example: python pdb_wc.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, division
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):
# No option, from pipe
if not sys.stdin.isatty():
pdbfh = sys.stdin
option = 'A'
else:
sys.stderr.write(USAGE)
sys.exit(1)
elif len(args) == 1:
# option & Pipe _or_ file
if re.match('\-[acrmHgdiA]', args[0]):
option = args[0][1]
if not sys.stdin.isatty():
pdbfh = sys.stdin
else:
sys.stderr.write(USAGE)
sys.exit(1)
else:
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')
option = 'A'
elif len(args) == 2:
# option & File
if not re.match('\-[acrmHgdiA]', args[0]):
sys.stderr.write('Invalid option: ' + args[0] + '\n')
sys.stderr.write(USAGE)
sys.exit(1)
if not os.path.isfile(args[1]):
sys.stderr.write('File not found: ' + args[1] + '\n')
sys.stderr.write(USAGE)
sys.exit(1)
option = args[0][1]
pdbfh = open(args[1], 'r')
else:
sys.stderr.write(USAGE)
sys.exit(1)
return (option, pdbfh)
def _summarize(fhandle, option):
"""Enclosing logic in a function"""
option = option
n_models = 0
has_hetero, has_gaps, has_double, has_insert = "No", "No", "No", "No"
at_list, res_list, chain_list = [], [], []
prev_resuid, prev_chainid = None, None
gap_check = True
for line in fhandle:
line = line.strip()
record = line[0:6]
if record == 'HETATM':
has_hetero = "Yes"
if record.startswith('ENDMDL'):
gap_check = False
if record.startswith('MODEL'):
n_models += 1
if record == 'ATOM ':
if not n_models:
n_models += 1
res_uid = (line[17:20], line[21], int(line[22:26]))
at_uid = (line[12:16], line[16], line[17:20], line[21], line[22:26])
chain_uid = line[21]
if res_uid != prev_resuid:
if prev_chainid == line[21] and prev_resuid and res_uid[2] - 1 != prev_resuid[2]:
if gap_check:
has_gaps = "Yes"
else:
gap_check = True
prev_resuid = res_uid
res_list.append(res_uid)
if line[21] != prev_chainid:
prev_chainid = line[21]
chain_list.append(chain_uid)
altloc = line[16]
if altloc != ' ':
has_double = "Yes"
resinsert = line[26]
if resinsert != ' ':
has_insert = "Yes"
at_list.append(at_uid)
n_atoms, n_u_atoms = len(at_list), len(set(at_list))
n_residues, n_u_residues = len(res_list), len(set(res_list))
n_chains, n_u_chains = len(chain_list), len(set(chain_list))
if option == 'a':
print(n_atoms, n_u_atoms)
elif option == 'r':
print(n_residues, n_u_residues)
elif option == 'c':
print(n_chains, n_u_chains)
elif option == 'm':
print(n_models)
elif option == 'H':
print(has_hetero)
elif option == 'g':
print(has_gaps)
elif option == 'd':
print(has_double)
elif option == 'i':
print(has_insert)
elif option == 'A':
print('No. atoms:\t{0}\t({1:4.1f} per model)'.format(n_atoms, n_u_atoms))
print('No. residues:\t{0}\t({1:4.1f} per model)'.format(n_residues, n_u_residues))
print('No. chains:\t{0}\t({1:4.1f} per model)'.format(n_chains, n_u_chains))
print('No. models:\t{0}'.format(n_models))
print('Hetero Atoms:\t{0}'.format(has_hetero))
print('Has seq. gaps:\t{0}'.format(has_gaps))
print('Double Occ.:\t{0}'.format(has_double))
print('Insertions:\t{0}'.format(has_insert))
if __name__ == '__main__':
# Check Input
option, pdbfh = check_input(sys.argv[1:])
# Do the job
_summarize(pdbfh, option)
# last line of the script
# We can close it even if it is sys.stdin
pdbfh.close()
sys.exit(0)