Skip to content

Commit

Permalink
Issue 382 (#401)
Browse files Browse the repository at this point in the history
* issue #339: implemented instance-specific logging formatting

* NegativeLonePairsError exception;  JazzyError exception handler for API; migrated utils.py into exception.py
  • Loading branch information
ghiander authored Mar 19, 2024
1 parent 9a46b28 commit 127cf6b
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 21 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "jazzy"
version = "0.0.12"
version = "0.0.13"
description = "Jazzy"
authors = ["Gian Marco Ghiandoni <ghiandoni.g@gmail.com>", "Eike Caldeweyher <e.caldeweyher@gmail.com>"]
license = "Apache-2.0"
Expand Down
2 changes: 1 addition & 1 deletion src/jazzy/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from jazzy.api import atomic_strength_vis_from_smiles
from jazzy.api import molecular_vector_from_smiles
from jazzy.config import Config
from jazzy.utils import JazzyError
from jazzy.exception import JazzyError

pass_config = click.make_pass_decorator(Config, ensure=True)

Expand Down
8 changes: 7 additions & 1 deletion src/jazzy/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
from jazzy.core import get_covalent_atom_idxs
from jazzy.core import kallisto_molecule_from_rdkit_molecule
from jazzy.core import rdkit_molecule_from_smiles
from jazzy.exception import exception_handling
from jazzy.exception import JazzyError
from jazzy.helpers import condense_atomic_map
from jazzy.helpers import convert_map_to_tuples
from jazzy.helpers import sum_atomic_map
from jazzy.utils import JazzyError
from jazzy.visualisation import depict_strengths


Expand All @@ -37,6 +38,7 @@ def __smiles_to_molecule_objects(smiles, minimisation_method=MINIMISATION_METHOD
return rdkit_mol, kallisto_mol


@exception_handling
def molecular_vector_from_smiles(
smiles: str, minimisation_method=MINIMISATION_METHOD, only_strengths=False
):
Expand Down Expand Up @@ -92,6 +94,7 @@ def molecular_vector_from_smiles(
return mol_vector


@exception_handling
def deltag_from_smiles(smiles: str, minimisation_method=MINIMISATION_METHOD):
"""API route to calculate molecular free energy scalar.
Expand Down Expand Up @@ -134,6 +137,7 @@ def deltag_from_smiles(smiles: str, minimisation_method=MINIMISATION_METHOD):
return round(sum(dg.values()), ROUNDING_DIGITS)


@exception_handling
def atomic_tuples_from_smiles(smiles: str, minimisation_method=MINIMISATION_METHOD):
"""API route to generate a tuple representation on the atomic map.
Expand All @@ -160,6 +164,7 @@ def atomic_tuples_from_smiles(smiles: str, minimisation_method=MINIMISATION_METH
return convert_map_to_tuples(atomic_map)


@exception_handling
def atomic_map_from_smiles(smiles: str, minimisation_method=MINIMISATION_METHOD):
"""API route to generate a condensed representation on the atomic map.
Expand All @@ -186,6 +191,7 @@ def atomic_map_from_smiles(smiles: str, minimisation_method=MINIMISATION_METHOD)
return condense_atomic_map(atomic_map)


@exception_handling
def atomic_strength_vis_from_smiles(
smiles: str,
minimisation_method=MINIMISATION_METHOD,
Expand Down
10 changes: 8 additions & 2 deletions src/jazzy/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
from rdkit.Chem import rdMolDescriptors

from jazzy.config import ROUNDING_DIGITS
from jazzy.exception import KallistoError
from jazzy.exception import NegativeLonePairsError
from jazzy.logging import logger
from jazzy.utils import KallistoError


def rdkit_molecule_from_smiles(smiles: str, minimisation_method=None):
Expand Down Expand Up @@ -494,8 +495,13 @@ def calculate_delta_polar(
sdi = sdi / nh

# acceptor contribution
nlps = atom["num_lp"]
sak = atom["sa"]
nlps = atom["num_lp"]
if nlps < 0:
raise NegativeLonePairsError(
"The input compound contains atoms with "
f"negative lone pairs (got {nlps} for atom at idx {idx})"
)

don += sdi * (nh**expd)
acc += sak * (nlps**expa)
Expand Down
32 changes: 32 additions & 0 deletions src/jazzy/exception.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""Anything about exceptions and their handling."""
# src/jazzy/exception.py


class JazzyError(Exception):
"""Raised when Jazzy cannot calculate results."""

pass


class KallistoError(Exception):
"""Raised when something goes wrong inside kallisto."""

pass


class NegativeLonePairsError(Exception):
"""Raised when a molecule contains negative lone pairs."""

pass


def exception_handling(func):
"""Catches core exceptions and raises a JazzyError."""

def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except (NegativeLonePairsError, KallistoError) as e:
raise JazzyError(str(e))

return wrapper
14 changes: 0 additions & 14 deletions src/jazzy/utils.py

This file was deleted.

15 changes: 14 additions & 1 deletion tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from jazzy.api import molecular_vector_from_smiles
from jazzy.config import Config
from jazzy.config import ROUNDING_DIGITS
from jazzy.utils import JazzyError
from jazzy.exception import JazzyError

# global jazzy config (parameter)
config = Config()
Expand Down Expand Up @@ -68,6 +68,19 @@ def test_api_molecular_vector_from_smiles_fails_for_empty_smiles():
assert error.value.args[0] == "An empty SMILES string was passed."


def test_api_molecular_vector_from_smiles_complex_numbers():
"""Exception for structure containing negative lone pairs."""
smiles = "C12C3C4C5C1[Fe]23456789C%10C6C7C8C9%10"
minimisation_method = "MMFF94"
only_strengths = False
with pytest.raises(JazzyError) as error:
molecular_vector_from_smiles(smiles, minimisation_method, only_strengths)
assert (
"The input compound contains atoms with negative lone pairs"
in error.value.args[0]
)


def test_deltag_from_smiles():
"""Correcly calculates free energy scalar."""
smiles = "CC"
Expand Down
31 changes: 31 additions & 0 deletions tests/test_free_energy_contributions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Test cases for the free energy contributions."""
import numpy as np
import pytest

from jazzy.core import any_hydrogen_neighbors
from jazzy.core import calculate_delta_apolar
Expand All @@ -11,6 +12,7 @@
from jazzy.core import interaction_strength
from jazzy.core import kallisto_molecule_from_rdkit_molecule
from jazzy.core import rdkit_molecule_from_smiles
from jazzy.exception import NegativeLonePairsError


def test_calculate_delta_apolar():
Expand Down Expand Up @@ -87,6 +89,35 @@ def test_calculate_delta_polar():
assert np.isclose(got, want[idx], 3)


def test_negative_lone_pairs_delta_polar():
"""Exception calculating the polar free energy contribution."""
smiles = "C12C3C4C5C1[Fe]23456789C%10C6C7C8C9%10"
# mock parameters
parameter = [0, 0, 0, 0]
rdkit_molecule = rdkit_molecule_from_smiles(
smiles=smiles, minimisation_method="MMFF94"
)
kallisto_molecule = kallisto_molecule_from_rdkit_molecule(
rdkit_molecule=rdkit_molecule
)
eeq = get_charges_from_kallisto_molecule(kallisto_molecule, 0)
aan = get_covalent_atom_idxs(rdkit_molecule)
mol_map = calculate_polar_strength_map(rdkit_molecule, kallisto_molecule, aan, eeq)
with pytest.raises(NegativeLonePairsError) as error:
calculate_delta_polar(
mol_map=mol_map,
atoms_and_nbrs=aan,
gd=parameter[0],
ga=parameter[1],
expd=parameter[2],
expa=parameter[3],
)
assert (
"The input compound contains atoms with negative lone pairs"
in error.value.args[0]
)


def test_correct_interaction_strength():
"""Correctly calculates interaction strength."""
want = 4
Expand Down
2 changes: 1 addition & 1 deletion tests/test_kallisto.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from jazzy.core import get_charges_from_kallisto_molecule
from jazzy.core import kallisto_molecule_from_rdkit_molecule
from jazzy.core import rdkit_molecule_from_smiles
from jazzy.utils import KallistoError
from jazzy.exception import KallistoError


def test_kallisto_charges_are_correct_from_molecule():
Expand Down

0 comments on commit 127cf6b

Please sign in to comment.