Skip to content

Commit

Permalink
removed molecule 2 molecule fingerprint based optimization of isomorp…
Browse files Browse the repository at this point in the history
…hism.

is_substructure and is_equal now supports optimize skipping.
  • Loading branch information
stsouko committed Jul 15, 2021
1 parent 6dbeacd commit 9124310
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 19 deletions.
20 changes: 12 additions & 8 deletions CGRtools/algorithms/isomorphism.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#
from abc import abstractmethod
from CachedMethods import cached_property
from collections import defaultdict
from collections import defaultdict, deque
from itertools import permutations
from typing import Dict, Iterator, Any, Set
from .._functions import lazy_product
Expand Down Expand Up @@ -59,24 +59,28 @@ def __gt__(self, other):
def __ge__(self, other):
return other.is_substructure(self)

def is_substructure(self, other) -> bool:
def is_substructure(self, other, *, optimize: bool = True) -> bool:
"""
Test self is substructure of other
:param optimize: Morgan weights based automorphism preventing.
"""
try:
next(self.get_mapping(other))
next(self.get_mapping(other, optimize=optimize, automorphism_filter=False))
except StopIteration:
return False
return True

def is_equal(self, other) -> bool:
def is_equal(self, other, *, optimize: bool = True) -> bool:
"""
Test self is same structure as other
:param optimize: Morgan weights based automorphism preventing.
"""
if len(self) != len(other):
return False
try:
next(self.get_mapping(other))
next(self.get_mapping(other, optimize=optimize, automorphism_filter=False))
except StopIteration:
return False
return True
Expand Down Expand Up @@ -152,12 +156,12 @@ def _get_mapping(linear_query, query_closures, o_atoms, o_bonds, scope, groups):
size = len(linear_query) - 1
order_depth = {v[0]: k for k, v in enumerate(linear_query)}

stack = []
stack = deque()
path = []
mapping = {}
reversed_mapping = {}

s_n, s_atom = linear_query[0]
s_n, _, s_atom, _ = linear_query[0]
for n, o_atom in o_atoms.items():
if n in scope and s_atom == o_atom:
stack.append((n, 0))
Expand Down Expand Up @@ -214,7 +218,7 @@ def __compile_query(atoms, bonds, atoms_frequencies):
seen.add(start)
stack = [(n, start, atoms[n], bond) for n, bond in sorted(bonds[start].items(), reverse=True,
key=lambda x: atoms_frequencies[x[0]])]
order = [(start, atoms[start])]
order = [(start, None, atoms[start], None)]
components.append(order)

while stack:
Expand Down
10 changes: 0 additions & 10 deletions CGRtools/containers/molecule.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,16 +490,6 @@ def _component_fingerprint(self, component):
scope = set(self.connected_components[component])
return {k: v & scope for k, v in self.fingerprint.items() if not v.isdisjoint(scope)}

def _isomorphism_candidates(self, other, self_component, other_component):
if self.fingerprint:
self_fingerprint = self._component_fingerprint(self_component)
if self_fingerprint:
other_fingerprint = other._component_fingerprint(other_component)
if self_fingerprint.keys() <= other_fingerprint.keys():
return {x for k in self_fingerprint for x in other_fingerprint[k]}
return set()
return super()._isomorphism_candidates(other, self_component, other_component)

@cached_args_method
def _explicit_hydrogens(self, n: int) -> int:
"""
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def finalize_options(self):

setup(
name='CGRtools',
version='4.2.14',
version='4.2.15',
packages=['CGRtools', 'CGRtools.algorithms', 'CGRtools.algorithms.calculate2d', 'CGRtools.algorithms.components',
'CGRtools.algorithms.standardize', 'CGRtools.containers', 'CGRtools.files', 'CGRtools.files._mdl',
'CGRtools.periodictable', 'CGRtools.periodictable.element', 'CGRtools.reactor', 'CGRtools.utils',
Expand Down

0 comments on commit 9124310

Please sign in to comment.