-
Notifications
You must be signed in to change notification settings - Fork 114
/
pdb_linewidth.py
executable file
·89 lines (69 loc) · 2.14 KB
/
pdb_linewidth.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
#!/usr/bin/env python
"""
Sets the width of all lines in the PDB to 80 characters by
paddding or trimming whitespace.
usage: python pdb_linewidth.py <pdb file>
example: python pdb_linewidth.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.
"""
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:
# Read from 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 _fix_lines(fhandle):
"""Enclosing logic in a function to speed up a bit"""
fhandle = fhandle
for line in fhandle:
line = line.strip()
size_of_line = len(line)
# Pad short lines
if size_of_line < 80:
padding = 80 - size_of_line
line = line + ' '*padding
yield line[:80] + '\n'
if __name__ == '__main__':
# Check Input
pdbfh = check_input(sys.argv[1:])
# Do the job
new_pdb = _fix_lines(pdbfh)
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)