Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions pdbtools/pdb_tidy.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@

import os
import sys
import re
import textwrap

__author__ = "Joao Rodrigues"
__email__ = "j.p.g.l.m.rodrigues@gmail.com"
Expand Down Expand Up @@ -108,7 +110,7 @@ def check_input(args):

def run(fhandle, strict=False):
"""
Add TER/END statements and pads all lines to 80 characters.
Add TER/END statements and truncates/pads all lines to 80 characters.

This function is a generator.

Expand Down Expand Up @@ -161,8 +163,14 @@ def make_TER(prev_line):
if line.startswith(ignored): # to avoid matching END _and_ ENDMDL
continue

# Check line length
line = "{:<80}\n".format(line)
# Check line length, wrapping and padding as necessary
# preserve the line prefix for wrapping
prefix = re.match(r"\S+\s*", line).group(0)
content = line[len(prefix):].lstrip()

line = "".join(
f"{prefix}{part:<{80 - len(prefix)}}\n"
for part in textwrap.wrap(content, width=80 - len(prefix)))

yield line

Expand Down
20 changes: 20 additions & 0 deletions tests/test_pdb_tidy.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,26 @@ def test_tidy_removes_master(self):
m_master = sum(1 for i in self.stdout if i.startswith('MASTER'))
self.assertEqual(m_master, 0)

def test_tidy_rewraps_long_lines(self):
"""Test pdb_tidy re-wraps long lines in the header maintaining padding"""
sys.argv = ['']
self.exec_module(
'TITLE THIS IS A VERY LONG LINE WHICH SHOULD BE WRAPPED '
'CORRECTLY OVER MULTIPLE LINES WITH PADDING'
)

# Check no long lines in output
long_lines = [line for line in self.stdout if len(line) > 80]
self.assertEqual(len(long_lines), 0)

# Check correct wrapping and padding
self.assertEqual(self.stdout[0],
'TITLE THIS IS A VERY LONG LINE WHICH SHOULD BE '
'WRAPPED CORRECTLY OVER ')
self.assertEqual(self.stdout[1],
'TITLE MULTIPLE LINES WITH PADDING '
' ')

def test_default_stdin(self):
"""$ cat data/dummy.pdb | pdb_tidy"""

Expand Down