Skip to content

Commit

Permalink
Handle uncaught exceptions when filtering (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonBoothroyd authored Dec 22, 2021
1 parent 45f3f48 commit 31bb38e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 28 deletions.
2 changes: 2 additions & 0 deletions .lgtm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ path_classifiers:
- devtools/*
generated:
- nagl/_version.py
queries:
- exclude: py/catch-base-exception
67 changes: 40 additions & 27 deletions nagl/cli/prepare/filter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import functools
import logging
from multiprocessing import Pool
from typing import TYPE_CHECKING, Tuple

Expand All @@ -16,38 +17,50 @@
if TYPE_CHECKING:
from openff.toolkit.topology import Molecule

_logger = logging.getLogger(__name__)


def apply_filter(molecule: "Molecule", retain_largest: bool) -> Tuple["Molecule", bool]:

with capture_toolkit_warnings():

from openff.toolkit.topology import Molecule
from simtk import unit as simtk_unit

split_smiles = molecule.to_smiles().split(".")
n_sub_molecules = len(split_smiles)

if retain_largest and n_sub_molecules > 1:

largest_smiles = max(split_smiles, key=len)
molecule = Molecule.from_smiles(largest_smiles, allow_undefined_stereo=True)

# Retain H, C, N, O, F, P, S, Cl, Br, I
allowed_elements = [1, 6, 7, 8, 9, 15, 16, 17, 35, 53]

mass = sum(
atom.mass.value_in_unit(simtk_unit.gram / simtk_unit.mole)
for atom in molecule.atoms
)

return (
molecule,
(
all(atom.atomic_number in allowed_elements for atom in molecule.atoms)
and (250.0 < mass < 350.0)
and (len(molecule.find_rotatable_bonds()) <= 7)
),
)
try:
from openff.toolkit.topology import Molecule
from simtk import unit as simtk_unit

split_smiles = molecule.to_smiles().split(".")
n_sub_molecules = len(split_smiles)

if retain_largest and n_sub_molecules > 1:

largest_smiles = max(split_smiles, key=len)
molecule = Molecule.from_smiles(
largest_smiles, allow_undefined_stereo=True
)

# Retain H, C, N, O, F, P, S, Cl, Br, I
allowed_elements = [1, 6, 7, 8, 9, 15, 16, 17, 35, 53]

mass = sum(
atom.mass.value_in_unit(simtk_unit.gram / simtk_unit.mole)
for atom in molecule.atoms
)

return (
molecule,
(
all(
atom.atomic_number in allowed_elements
for atom in molecule.atoms
)
and (250.0 < mass < 350.0)
and (len(molecule.find_rotatable_bonds()) <= 7)
),
)

except BaseException:
_logger.exception("failed to apply filter")
return molecule, False


@click.command(
Expand Down
2 changes: 1 addition & 1 deletion nagl/labelling.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def label_molecules(
n_conformers,
rms_cutoff,
)
except (BaseException, Exception) as e: # lgtm [py/catch-base-exception]
except (BaseException, Exception) as e:

formatted_traceback = traceback.format_exception(
etype=type(e), value=e, tb=e.__traceback__
Expand Down

0 comments on commit 31bb38e

Please sign in to comment.