forked from haddocking/pdb-tools
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpdb_schave.py
More file actions
executable file
·126 lines (104 loc) · 3.69 KB
/
pdb_schave.py
File metadata and controls
executable file
·126 lines (104 loc) · 3.69 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
#!/usr/bin/env python
"""
Deletes side-chain atoms of specific residue selections.
usage: python pdb_schave.py <i>[,:<j>] <pdb file>
examples:
python pdb_rslice.py 1:10 1CTF.pdb # Shaves residues 1 to 10
python pdb_rslice.py 1: 1CTF.pdb # Shaves residues 1 to END
python pdb_rslice.py 1:10,20:25 1CTF.pdb # Shaves residues 1-10 and 20-25
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.
"""
import os
import re
import sys
__author__ = "Joao Rodrigues"
__email__ = "j.p.g.l.m.rodrigues@gmail.com"
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):
sys.stderr.write(USAGE)
sys.exit(1)
elif len(args) == 1:
# Resi & Pipe _or_ file & no rslice
if re.match('[\-0-9,:]+', args[0]):
rslice = args[0]
if not sys.stdin.isatty():
pdbfh = sys.stdin
else:
sys.stderr.write(USAGE)
sys.exit(1)
else:
sys.stderr.write(USAGE)
sys.exit(1)
elif len(args) == 2:
# Option & File
if not re.match('[\-0-9,:]+', args[0]):
sys.stderr.write('Invalid slice: ' + 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)
rslice = args[0]
pdbfh = open(args[1], 'r')
else:
sys.stderr.write(USAGE)
sys.exit(1)
# Parse selection
# Return set of integers to be mapped to residue numbers
resi_set = set()
groups = [g for g in rslice.split(',') if g.strip()]
for g in groups:
bits = [b for b in g.split(':') if b.strip()]
if len(bits) == 2:
st_slice, en_slice = map(int, bits)
resi_set = resi_set.union(range(st_slice, en_slice + 1))
elif len(bits) == 1 and rslice[0] == ':':
st_slice = -99
en_slice = int(bits[0])
resi_set = resi_set.union(range(st_slice, en_slice + 1))
elif len(bits) == 1 and rslice[-1] == ':':
st_slice = int(bits[0])
en_slice = 99999
resi_set = resi_set.union(range(st_slice, en_slice + 1))
elif len(bits) == 1 and bits[0].find(':') == -1:
resi_set = resi_set.union(map(int, bits))
else:
sys.stderr.write(USAGE)
sys.exit(1)
return (resi_set, pdbfh)
def _shave_pdb(fhandle, set_of_residues):
"""Enclosing logic in a function to speed up a bit"""
resset = set_of_residues
backbone = set(('CA', 'N', 'O', 'C'))
for line in fhandle:
if not (line.startswith(('ATOM', 'HETATM')) and
int(line[22:26]) in resset and
line[12:16].strip() not in backbone):
yield line
if __name__ == '__main__':
# Check Input
rslice, pdbfh = check_input(sys.argv[1:])
# Do the job
new_pdb = _shave_pdb(pdbfh, rslice)
try:
sys.stdout.write(''.join(new_pdb))
sys.stdout.flush()
except IOError:
# This is here to catch Broken Pipes
# for example to use 'head' or 'tail' without
# the error message showing up
pass
# last line of the script
# We can close it even if it is sys.stdin
pdbfh.close()
sys.exit(0)