Skip to content

Commit

Permalink
MAINT: improve error with SMILES
Browse files Browse the repository at this point in the history
* The error message when processing conventional SMILES strings
can be confusing, as observed at:
marrink-lab/polyply_1.0#390 (comment)

* For cases like that where there are no polymeric fragments (i.e., no
use of braces), this patch improves the error message (and adds a test
for it) to nudge the user to BigSMILES or CGSmiles formats.
  • Loading branch information
tylerjereddy committed Nov 6, 2024
1 parent 26d402c commit d32174e
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cgsmiles/resolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,9 @@ def from_string(cls, cgsmiles_str, last_all_atom=True, legacy=False):
"""
# here we figure out how many resolutions we are dealing with
elements = re.findall(r"\{[^\}]+\}", cgsmiles_str)
if len(elements) == 0:
raise SyntaxError("No polymeric fragments detected; likely "
"not a valid CGSmiles/BigSMILES string.")
# the first one describes our lowest resolution
molecule = read_cgsmiles(elements[0])
# the rest are fragment lists
Expand Down
2 changes: 2 additions & 0 deletions cgsmiles/tests/test_molecule_resolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,8 @@ def _atomname_match(n1, n2):
(("{[#A][#B]}.{#A=CC[$]}", "Found node #B but no corresponding fragment."),
("{[#A][#B]1}.{#A=CC[$],#B=OC[$]}", "You have a dangling ring index."),
("{[#A]1[#B]1}{#A=CC[$],#B=OC[$]}", "You define two edges between the same node. Use bond order symbols instead."),
# see: https://github.com/marrink-lab/polyply_1.0/issues/390#issuecomment-2455472121
("CC(c1ccccc1)CC(c1ccccc1)CCCCCCCCCC(CC)CC(CC)CC(c1ccccc1)CC(c1ccccc1)", "No polymeric fragments detected; likely not a valid CGSmiles/BigSMILES string."),
)))
def test_syntax_errors(cgsmiles_str, error_message):
with pytest.raises(SyntaxError) as e_message:
Expand Down

0 comments on commit d32174e

Please sign in to comment.