Skip to content

Commit 127cf6b

Browse files
authored
Issue 382 (#401)
* issue #339: implemented instance-specific logging formatting * NegativeLonePairsError exception; JazzyError exception handler for API; migrated utils.py into exception.py
1 parent 9a46b28 commit 127cf6b

File tree

9 files changed

+95
-21
lines changed

9 files changed

+95
-21
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "jazzy"
3-
version = "0.0.12"
3+
version = "0.0.13"
44
description = "Jazzy"
55
authors = ["Gian Marco Ghiandoni <[email protected]>", "Eike Caldeweyher <[email protected]>"]
66
license = "Apache-2.0"

src/jazzy/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from jazzy.api import atomic_strength_vis_from_smiles
77
from jazzy.api import molecular_vector_from_smiles
88
from jazzy.config import Config
9-
from jazzy.utils import JazzyError
9+
from jazzy.exception import JazzyError
1010

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

src/jazzy/api.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@
1313
from jazzy.core import get_covalent_atom_idxs
1414
from jazzy.core import kallisto_molecule_from_rdkit_molecule
1515
from jazzy.core import rdkit_molecule_from_smiles
16+
from jazzy.exception import exception_handling
17+
from jazzy.exception import JazzyError
1618
from jazzy.helpers import condense_atomic_map
1719
from jazzy.helpers import convert_map_to_tuples
1820
from jazzy.helpers import sum_atomic_map
19-
from jazzy.utils import JazzyError
2021
from jazzy.visualisation import depict_strengths
2122

2223

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

3940

41+
@exception_handling
4042
def molecular_vector_from_smiles(
4143
smiles: str, minimisation_method=MINIMISATION_METHOD, only_strengths=False
4244
):
@@ -92,6 +94,7 @@ def molecular_vector_from_smiles(
9294
return mol_vector
9395

9496

97+
@exception_handling
9598
def deltag_from_smiles(smiles: str, minimisation_method=MINIMISATION_METHOD):
9699
"""API route to calculate molecular free energy scalar.
97100
@@ -134,6 +137,7 @@ def deltag_from_smiles(smiles: str, minimisation_method=MINIMISATION_METHOD):
134137
return round(sum(dg.values()), ROUNDING_DIGITS)
135138

136139

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

162166

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

188193

194+
@exception_handling
189195
def atomic_strength_vis_from_smiles(
190196
smiles: str,
191197
minimisation_method=MINIMISATION_METHOD,

src/jazzy/core.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
from rdkit.Chem import rdMolDescriptors
1515

1616
from jazzy.config import ROUNDING_DIGITS
17+
from jazzy.exception import KallistoError
18+
from jazzy.exception import NegativeLonePairsError
1719
from jazzy.logging import logger
18-
from jazzy.utils import KallistoError
1920

2021

2122
def rdkit_molecule_from_smiles(smiles: str, minimisation_method=None):
@@ -494,8 +495,13 @@ def calculate_delta_polar(
494495
sdi = sdi / nh
495496

496497
# acceptor contribution
497-
nlps = atom["num_lp"]
498498
sak = atom["sa"]
499+
nlps = atom["num_lp"]
500+
if nlps < 0:
501+
raise NegativeLonePairsError(
502+
"The input compound contains atoms with "
503+
f"negative lone pairs (got {nlps} for atom at idx {idx})"
504+
)
499505

500506
don += sdi * (nh**expd)
501507
acc += sak * (nlps**expa)

src/jazzy/exception.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""Anything about exceptions and their handling."""
2+
# src/jazzy/exception.py
3+
4+
5+
class JazzyError(Exception):
6+
"""Raised when Jazzy cannot calculate results."""
7+
8+
pass
9+
10+
11+
class KallistoError(Exception):
12+
"""Raised when something goes wrong inside kallisto."""
13+
14+
pass
15+
16+
17+
class NegativeLonePairsError(Exception):
18+
"""Raised when a molecule contains negative lone pairs."""
19+
20+
pass
21+
22+
23+
def exception_handling(func):
24+
"""Catches core exceptions and raises a JazzyError."""
25+
26+
def wrapper(*args, **kwargs):
27+
try:
28+
return func(*args, **kwargs)
29+
except (NegativeLonePairsError, KallistoError) as e:
30+
raise JazzyError(str(e))
31+
32+
return wrapper

src/jazzy/utils.py

Lines changed: 0 additions & 14 deletions
This file was deleted.

tests/test_api.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from jazzy.api import molecular_vector_from_smiles
1010
from jazzy.config import Config
1111
from jazzy.config import ROUNDING_DIGITS
12-
from jazzy.utils import JazzyError
12+
from jazzy.exception import JazzyError
1313

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

7070

71+
def test_api_molecular_vector_from_smiles_complex_numbers():
72+
"""Exception for structure containing negative lone pairs."""
73+
smiles = "C12C3C4C5C1[Fe]23456789C%10C6C7C8C9%10"
74+
minimisation_method = "MMFF94"
75+
only_strengths = False
76+
with pytest.raises(JazzyError) as error:
77+
molecular_vector_from_smiles(smiles, minimisation_method, only_strengths)
78+
assert (
79+
"The input compound contains atoms with negative lone pairs"
80+
in error.value.args[0]
81+
)
82+
83+
7184
def test_deltag_from_smiles():
7285
"""Correcly calculates free energy scalar."""
7386
smiles = "CC"

tests/test_free_energy_contributions.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Test cases for the free energy contributions."""
22
import numpy as np
3+
import pytest
34

45
from jazzy.core import any_hydrogen_neighbors
56
from jazzy.core import calculate_delta_apolar
@@ -11,6 +12,7 @@
1112
from jazzy.core import interaction_strength
1213
from jazzy.core import kallisto_molecule_from_rdkit_molecule
1314
from jazzy.core import rdkit_molecule_from_smiles
15+
from jazzy.exception import NegativeLonePairsError
1416

1517

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

8991

92+
def test_negative_lone_pairs_delta_polar():
93+
"""Exception calculating the polar free energy contribution."""
94+
smiles = "C12C3C4C5C1[Fe]23456789C%10C6C7C8C9%10"
95+
# mock parameters
96+
parameter = [0, 0, 0, 0]
97+
rdkit_molecule = rdkit_molecule_from_smiles(
98+
smiles=smiles, minimisation_method="MMFF94"
99+
)
100+
kallisto_molecule = kallisto_molecule_from_rdkit_molecule(
101+
rdkit_molecule=rdkit_molecule
102+
)
103+
eeq = get_charges_from_kallisto_molecule(kallisto_molecule, 0)
104+
aan = get_covalent_atom_idxs(rdkit_molecule)
105+
mol_map = calculate_polar_strength_map(rdkit_molecule, kallisto_molecule, aan, eeq)
106+
with pytest.raises(NegativeLonePairsError) as error:
107+
calculate_delta_polar(
108+
mol_map=mol_map,
109+
atoms_and_nbrs=aan,
110+
gd=parameter[0],
111+
ga=parameter[1],
112+
expd=parameter[2],
113+
expa=parameter[3],
114+
)
115+
assert (
116+
"The input compound contains atoms with negative lone pairs"
117+
in error.value.args[0]
118+
)
119+
120+
90121
def test_correct_interaction_strength():
91122
"""Correctly calculates interaction strength."""
92123
want = 4

tests/test_kallisto.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from jazzy.core import get_charges_from_kallisto_molecule
1010
from jazzy.core import kallisto_molecule_from_rdkit_molecule
1111
from jazzy.core import rdkit_molecule_from_smiles
12-
from jazzy.utils import KallistoError
12+
from jazzy.exception import KallistoError
1313

1414

1515
def test_kallisto_charges_are_correct_from_molecule():

0 commit comments

Comments
 (0)