forked from haddocking/pdb-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdb_splitchain.py
87 lines (71 loc) · 2.51 KB
/
pdb_splitchain.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
#!/usr/bin/env python
"""
Extracts each chain of a PDB file to a separate file.
usage: python pdb_splitchain.py <pdb file>
example: python pdb_splitchain.py 1CTF.pdb
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 itertools import chain as ichain
import os
import re
import sys
__author__ = "Joao Rodrigues"
USAGE = "usage: " + sys.argv[0] + " <pdb file>\n"
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:
# Read from file
if not os.path.exists(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 _extract_chains(fhandle):
""""""
coord_re = re.compile('^(ATOM|HETATM)')
fname_root = fhandle.name[:-4] if fhandle.name != '<stdin>' else 'output'
prev_chain, chain_atoms = None, []
for line in fhandle:
if coord_re.match(line):
# ATOM/HETATM line
if prev_chain != line[21]:
if chain_atoms:
# Write chain to file
output_handle = open(fname_root + '_' + prev_chain + '.pdb', 'w')
output_handle.write(''.join(chain_atoms))
output_handle.write('END\n')
output_handle.close()
chain_atoms = []
chain_atoms.append(line)
prev_chain = line[21]
else:
chain_atoms.append(line)
# Output last chain to file
output_handle = open(fname_root + '_' + chain_atoms[-1][21] + '.pdb', 'w')
output_handle.write(''.join(chain_atoms))
output_handle.write('END\n')
output_handle.close()
if __name__ == '__main__':
# Check Input
pdbfh = check_input(sys.argv[1:])
# Do the job
_extract_chains(pdbfh)
# last line of the script
# We can close it even if it is sys.stdin
pdbfh.close()
sys.exit(0)