Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New: Guess spin from ChI string #527

Merged
merged 1 commit into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions automol/amchi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
# # derived properties
from automol.amchi._conv import is_complete
from automol.amchi._conv import is_valid_multiplicity
from automol.amchi._conv import guess_spin
# # derived transformations
from automol.amchi._conv import add_stereo
from automol.amchi._conv import expand_stereo
Expand Down Expand Up @@ -200,6 +201,7 @@
# # derived properties
'is_complete',
'is_valid_multiplicity',
'guess_spin',
# # derived transformations
'add_stereo',
'expand_stereo',
Expand Down
31 changes: 30 additions & 1 deletion automol/amchi/_conv.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import itertools
import numbers

from automol import geom
from automol import form, geom
from automol import graph as graph_
from automol.amchi.base import (
atom_stereo_parities,
Expand All @@ -13,12 +13,15 @@
breaking_bond_keys,
equivalent,
forming_bond_keys,
formula,
formula_layer,
has_stereo,
hydrogen_valences,
is_inchi,
is_inverted_enantiomer,
is_reversed_ts,
isotope_layers,
main_layers,
split,
standard_form,
symbols,
Expand Down Expand Up @@ -286,6 +289,32 @@ def is_valid_multiplicity(chi, mul):
return mul in graph_.possible_spin_multiplicities(graph(chi, stereo=False))


def guess_spin(chi: str) -> int:
"""Guess the spin of a ChI string.

Apart from a special list of common molecules, simply guesses 0 or 1 depending on
whether the eletron count is even or odd, respectively.

Could be made smarter by converting to a graph, but that would be more expensive.

:param chi: A ChI string
:return: The ground-state spin guess
"""
# First, check if this is a case with a hardcoded value
lookup_dct = {
("O",): 2,
('O2', ('c', '1-2')): 2,
}
key = (formula_layer(chi), *sorted(main_layers(chi).items()))
val = lookup_dct.get(key, None)
if val is not None:
return val

# Otherwise, guess based on the formula
fml = formula(chi)
return form.electron_count(fml) % 2


# # derived transformations
def add_stereo(chi):
"""Add stereochemistry to a ChI string converting to/from geometry.
Expand Down