Skip to content

Commit

Permalink
Add check PDB function
Browse files Browse the repository at this point in the history
  • Loading branch information
laumalo committed Nov 3, 2020
1 parent 4259fe1 commit fc9e4af
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions peleffy/topology/molecule.py
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,36 @@ def _initialize(self):
from peleffy.forcefield.parameters import BaseParameterWrapper
self._parameters = BaseParameterWrapper()

def _PDB_checkup(self, path):
"""
Safety check for PDB files in order to properly handle exceptions
related with its format prior running the parameterization.
Parameters
----------
path : str
The path to a PDB with the molecule structure
"""
# Parse PDB file
atom_id, res_name, res_id = ([] for i in range(3))
for line in open(path):
if not len(line.strip()) == 0:
list = line.split()

This comment has been minimized.

Copy link
@martimunicoy

martimunicoy Nov 4, 2020

Owner

Be careful when using Python's built-in names such as list or id. It is not recommended.

This comment has been minimized.

Copy link
@martimunicoy

martimunicoy Nov 4, 2020

Owner

Besides, it is safer to access to the PDB data by index rather than splitting each line by empty space. If you look at the PDB format standards, you will see which index ranges the values to parse are expected to be

id = list[0]
if id == 'HETATM':

This comment has been minimized.

Copy link
@martimunicoy

martimunicoy Nov 4, 2020

Owner

I would use if line.startswith() instead, it is more comprehensive.

atom_id.append(list[2])
res_name.append(list[3])
res_id.append(list[4])

# Handle exceptions related with the PDB file format
assert res_id[:-1] == res_id[1:], \

This comment has been minimized.

Copy link
@martimunicoy

martimunicoy Nov 4, 2020

Owner

Remember raising a general Exception rather than an AssertionError with the messages added to issue #77.

'A single ligand with immutable residue ids is expected'
assert res_name[:-1] == res_name[1:], \
'A single ligand with immutable residue names is expected'
assert len(atom_id) == len(set(atom_id)), \
'Ligand in input PDB has no unique atom names'


def _initialize_from_pdb(self, path):
"""
It initializes a molecule with the molecule structure read from
Expand All @@ -585,6 +615,9 @@ def _initialize_from_pdb(self, path):
logger.info(' - Initializing molecule from PDB')
self._initialize()

#Validate PDB
self._PDB_checkup(path)

logger.info(' - Loading molecule from RDKit')
rdkit_toolkit = RDKitToolkitWrapper()
self._rdkit_molecule = rdkit_toolkit.from_pdb(path)
Expand Down

0 comments on commit fc9e4af

Please sign in to comment.