Skip to content

Commit

Permalink
Merge pull request #527 from avcopan/dev
Browse files Browse the repository at this point in the history
New: Guess spin from ChI string
  • Loading branch information
avcopan authored Jul 10, 2024
2 parents 2e61f5c + f87f7ef commit 9063331
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
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

0 comments on commit 9063331

Please sign in to comment.