Skip to content

Commit

Permalink
Add function to check that a file is a PDB
Browse files Browse the repository at this point in the history
  • Loading branch information
benmwebb committed Jun 30, 2021
1 parent 4a61c8d commit 1b656e4
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
2 changes: 2 additions & 0 deletions doc/modules/frontend.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ The :mod:`saliweb.frontend` Python module

.. autofunction:: check_modeller_key

.. autofunction:: check_pdb

.. autofunction:: pdb_code_exists

.. autofunction:: get_pdb_code
Expand Down
22 changes: 22 additions & 0 deletions python/saliweb/frontend/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,28 @@ def check_email(email, required=False):
"Please provide a valid return email address")


def check_pdb(filename, show_filename=None):
"""Check that a PDB file really looks like a PDB file.
If it does not, raise an :exc:`InputValidationError` exception.
:param str filename: The PDB file to check.
:param str show_filename: If provided, include this filename in any
error message to identify the PDB file (useful for services
that allow upload of multiple PDB files).
"""
if show_filename:
pdb_file = "PDB file %s" % show_filename
else:
pdb_file = "PDB file"
# Use latin1 to avoid decode errors with 8-bit characters
with open(filename, encoding='latin1') as fh:
for line in fh:
if line.startswith('ATOM ') or line.startswith('HETATM'):
return
raise InputValidationError("%s contains no ATOM or HETATM records!"
% pdb_file)


def check_modeller_key(modkey):
"""Check a provided MODELLER key.
If the key is empty or invalid, raise an
Expand Down
21 changes: 21 additions & 0 deletions test/pyfrontend/test_frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,27 @@ def test_check_email_optional(self):
tf, 'garbage')
tf("test@test.com")

def test_check_pdb(self):
"""Test check_pdb"""
with util.temporary_directory() as tmpdir:
good_pdb = os.path.join(tmpdir, 'good.pdb')
with open(good_pdb, 'w') as fh:
fh.write("REMARK 6 TEST REMARK\n")
fh.write(
"ATOM 1 N ALA C 1 27.932 14.488 4.257 "
"1.00 23.91 N\n")
bad_pdb = os.path.join(tmpdir, 'bad.pdb')
with open(bad_pdb, 'w') as fh:
fh.write("not a pdb\n")

saliweb.frontend.check_pdb(good_pdb)
saliweb.frontend.check_pdb(good_pdb, show_filename='good.pdb')
self.assertRaises(saliweb.frontend.InputValidationError,
saliweb.frontend.check_pdb, bad_pdb)
self.assertRaises(saliweb.frontend.InputValidationError,
saliweb.frontend.check_pdb, bad_pdb,
show_filename='bad.pdb')

def test_check_modeller_key(self):
"""Test check_modeller_key function"""
class MockApp(object):
Expand Down

0 comments on commit 1b656e4

Please sign in to comment.