Skip to content

Commit

Permalink
now aromatize works with n-oxydes withput standardization.
Browse files Browse the repository at this point in the history
new standardization rule added: enamine to imine.
  • Loading branch information
stsouko committed Dec 7, 2020
1 parent b6a19f8 commit d3edbac
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 26 deletions.
62 changes: 62 additions & 0 deletions CGRtools/algorithms/aromatics.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,33 @@ def check_thiele(self, fast=True) -> bool:
return False
return True

def __fix_oxides(self):
atoms = self._atoms
bonds = self._bonds
atoms_order = self.atoms_order
connected_components = [set(x) for x in self.connected_components]

seen = set()
for q, af, bf in self.__oxyde_rules:
components, closures = q._compiled_query
for candidate in connected_components:
for mapping in q._get_mapping(components[0], closures, atoms, bonds, candidate - seen, atoms_order):
match = set(mapping.values())
if not match.isdisjoint(seen): # skip intersected groups
continue
seen.update(match)

for n, fix in af.items():
n = mapping[n]
for key, value in fix.items():
getattr(self, key)[n] = value
for n, m, b in bf:
n = mapping[n]
m = mapping[m]
bonds[n][m]._Bond__order = b
if seen:
self.flush_cache()

def __prepare_rings(self):
atoms = self._atoms
charges = self._charges
Expand Down Expand Up @@ -391,6 +418,7 @@ def __kekule_patch(self, patch):
self._calc_implicit(n)

def __kekule_full(self):
self.__fix_oxides() # fix pyridine n-oxyde
rings, pyroles, double_bonded = self.__prepare_rings()
atoms = set(rings)
components = []
Expand Down Expand Up @@ -580,5 +608,39 @@ def __freaks(self):

return rules

@cached_property
def __oxyde_rules(self):
rules = []

# Aromatic N-Oxide
#
# : N : >> : [N+] :
# \\ \
# O [O-]
#
q = query.QueryContainer()
q.add_atom('N', neighbors=3, hybridization=4)
q.add_atom('O', neighbors=1)
q.add_bond(1, 2, 2)
atom_fix = {1: {'_charges': 1}, 2: {'_charges': -1, '_hybridizations': 1}}
bonds_fix = ((1, 2, 1),)
rules.append((q, atom_fix, bonds_fix))

# Aromatic N-Nitride?
#
# : N : >> : [N+] :
# \\ \
# N [N-]
#
q = query.QueryContainer()
q.add_atom('N', neighbors=3, hybridization=4)
q.add_atom('N', neighbors=(1, 2), hybridization=2)
q.add_bond(1, 2, 2)
atom_fix = {1: {'_charges': 1}, 2: {'_charges': -1, '_hybridizations': 1}}
bonds_fix = ((1, 2, 1),)
rules.append((q, atom_fix, bonds_fix))

return rules


__all__ = ['Aromatize']
42 changes: 16 additions & 26 deletions CGRtools/algorithms/standardize.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ def canonicalize(self) -> bool:
"""
Convert molecule to canonical forms of functional groups and aromatic rings without explicit hydrogens.
"""
s = self.standardize(fix_stereo=False)
k = self.kekule()
s = self.standardize(fix_stereo=False)
h = self.implicify_hydrogens(fix_stereo=False)
t = self.thiele()
return s or k or h or t
return k or s or h or t

def standardize(self, *, fix_stereo=True, logging=False) -> bool:
"""
Expand Down Expand Up @@ -558,6 +558,20 @@ def __standardize_rules():
bonds_fix = ()
rules.append((atoms, bonds, atom_fix, bonds_fix))

#
# RNH RN
# / //
# C = C >> C - C
# \ \
# A A
#
atoms = ({'atom': 'C', 'neighbors': 3}, {'atom': 'N', 'hybridization': 1, 'neighbors': (1, 2)},
{'atom': 'C', 'hybridization': 2}, {'atom': 'A'})
bonds = ((1, 2, 1), (1, 3, 2), (1, 4, 1))
atom_fix = {2: {'hybridization': 2}, 3: {'hybridization': 1}}
bonds_fix = ((1, 2, 2), (1, 3, 1))
rules.append((atoms, bonds, atom_fix, bonds_fix))

# Nitrone
#
# O [O-]
Expand Down Expand Up @@ -968,30 +982,6 @@ def __standardize_rules():
bonds_fix = ((1, 2, 2),)
rules.append((atoms, bonds, atom_fix, bonds_fix))

# Aromatic N-Oxide
#
# : N : >> : [N+] :
# \\ \
# O [O-]
#
atoms = ({'atom': 'N', 'neighbors': 3, 'hybridization': 4}, {'atom': 'O', 'neighbors': 1})
bonds = ((1, 2, 2),)
atom_fix = {1: {'charge': 1}, 2: {'charge': -1, 'hybridization': 1}}
bonds_fix = ((1, 2, 1),)
rules.append((atoms, bonds, atom_fix, bonds_fix))

# Aromatic N-Nitride?
#
# : N : >> : [N+] :
# \\ \
# N [N-]
#
atoms = ({'atom': 'N', 'neighbors': 3, 'hybridization': 4}, {'atom': 'N', 'hybridization': 2})
bonds = ((1, 2, 2),)
atom_fix = {1: {'charge': 1}, 2: {'charge': -1, 'hybridization': 1}}
bonds_fix = ((1, 2, 1),)
rules.append((atoms, bonds, atom_fix, bonds_fix))

# Nitroso
#
# - [N+] - [O-] >> - N = O
Expand Down

0 comments on commit d3edbac

Please sign in to comment.