Skip to content

Commit

Permalink
tests and account for bond order
Browse files Browse the repository at this point in the history
  • Loading branch information
fgrunewald committed Nov 21, 2024
1 parent 715e1bc commit b36f0fc
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
16 changes: 9 additions & 7 deletions cgsmiles/pysmiles_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ def rebuild_h_atoms(mol_graph, keep_bonding=False):
pysmiles.smiles_helper.correct_aromatic_rings(mol_graph, strict=True)
except SyntaxError as pysmiles_err:
print(pysmiles_err)
msg = ("Likely you are writing an aromatic molecule that does not "
"show delocalization-induced molecular equivalency and thus "
"is not considered aromatic. For example, 4-methyl imidazole "
"is often written as [nH]1cc(nc1)C, but should be written as "
"[NH]1C=C(N=C1)C. A corresponding CGSmiles string would be "
"{[#A]1[#B][#C]1}.{#A=[>][<]N,#B=[$]N=C[>],#C=[$]C(C)=C[<]}")
msg = (r"Likely you are writing an aromatic molecule that does not "
r"show delocalization-induced molecular equivalency and thus "
r"is not considered aromatic. For example, 4-methyl imidazole "
r"is often written as [nH]1cc(nc1)C, but should be written as "
r"[NH]1C=C(N=C1)C. A corresponding CGSmiles string would be "
r"{[#A]1[#B][#C]1}.{#A=[>][<]N,#B=[$]N=C[>],#C=[$]C(C)=C[<]}")
raise SyntaxError(msg)
nx.set_node_attributes(mol_graph, 0, 'hcount')

Expand All @@ -84,7 +84,9 @@ def rebuild_h_atoms(mol_graph, keep_bonding=False):
if keep_bonding:
bonding_nodes = nx.get_node_attributes(mol_graph, 'bonding')
for node, bond_ops in bonding_nodes.items():
mol_graph.nodes[node]['hcount'] -= len(bond_ops)
print(bond_ops)
print(sum([int(bond[-1]) for bond in bond_ops]))
mol_graph.nodes[node]['hcount'] -= sum([int(bond[-1]) for bond in bond_ops])

# now we add the hydrogen atoms
pysmiles.smiles_helper.add_explicit_hydrogens(mol_graph)
Expand Down
32 changes: 32 additions & 0 deletions cgsmiles/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import re
import pytest
import cgsmiles

err_msg_rebuild_h = ("Likely you are writing an aromatic molecule that does not "
"show delocalization-induced molecular equivalency and thus "
"is not considered aromatic. For example, 4-methyl imidazole "
"is often written as [nH]1cc(nc1)C, but should be written as "
"[NH]1C=C(N=C1)C. A corresponding CGSmiles string would be "
"{[#A]1[#B][#C]1}.{#A=[>][<]N,#B=[$]N=C[>],#C=[$]C(C)=C[<]}")

@pytest.mark.parametrize('frag_str, hatoms_ref, error_type, err_msg', (
('{#A=[$]CCC[$]}', 6, None, None),
('{#A=CCC}', 8, None, None),
('{#A=C[!]CC}', 7, None, None),
('{#A=[$]=CCC=[$]}', 4, None, None),
('{#A=[$]cccc}',5, None, None),
('{#A=[$]ccc}', 0, SyntaxError, err_msg_rebuild_h),
))
def test_rebuild_hatoms(frag_str, hatoms_ref, error_type, err_msg):
frag_dict = cgsmiles.read_fragments(frag_str)
frag_graph = frag_dict['A']
if error_type:
with pytest.raises(error_type, match=re.escape(err_msg)):
cgsmiles.pysmiles_utils.rebuild_h_atoms(frag_graph, keep_bonding=True)
else:
cgsmiles.pysmiles_utils.rebuild_h_atoms(frag_graph, keep_bonding=True)
hatoms = 0
for node, ele in frag_graph.nodes(data='element'):
if ele == 'H':
hatoms += 1
assert hatoms == hatoms_ref

0 comments on commit b36f0fc

Please sign in to comment.