Skip to content

Commit

Permalink
updating fix_mapping (#166)
Browse files Browse the repository at this point in the history
* updated return in fix_mapping
* updated fix_mapping
* add bracket
* fix log

Co-authored-by: zarinaIbr <ZAIbragimova@stud.kpfu.ru>
Co-authored-by: Ramil Nugmanov <nougmanoff@protonmail.com>
  • Loading branch information
3 people committed Mar 23, 2021
1 parent 3837340 commit 8a47f3f
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions CGRtools/algorithms/standardize.py
Original file line number Diff line number Diff line change
Expand Up @@ -1468,10 +1468,12 @@ def neutralize(self: 'ReactionContainer', *, logging=False) -> Union[bool, Tuple
self.flush_cache()
return total

def fix_mapping(self: Union['ReactionContainer', 'StandardizeReaction']) -> bool:
def fix_mapping(self: Union['ReactionContainer', 'StandardizeReaction'], *, logging: bool = False) -> bool:
"""
Fix atom-to-atom mapping of some functional groups. Return True if found AAM errors.
"""
if logging:
log = []
seen = set()
if not (self.reactants and self.products):
return False
Expand Down Expand Up @@ -1509,18 +1511,19 @@ def fix_mapping(self: Union['ReactionContainer', 'StandardizeReaction']) -> bool
else:
flag = False

for bad_query, good_query, fix, valid in self.__remapping_compiled_rules:
for rule_num, (bad_query, good_query, fix, valid) in enumerate(self.__remapping_compiled_rules):
cgr = ~self
first_free = max(cgr) + 1
free_number = count(first_free)
cgr_c = set(cgr.center_atoms)
del self.__dict__['__cached_method_compose']

flag_m = False
for mapping in bad_query.get_mapping(cgr, automorphism_filter=False):
if not seen.isdisjoint(mapping.values()): # prevent matching same RC
continue
mapping = {mapping[n]: next(free_number) if m is None else mapping[m] for n, m in fix.items()}

flag_m = True
reverse = {m: n for n, m in mapping.items()}
for m in self.products:
m.remap(mapping)
Expand All @@ -1532,6 +1535,9 @@ def fix_mapping(self: Union['ReactionContainer', 'StandardizeReaction']) -> bool
for m in good_query.get_mapping(check, automorphism_filter=False):
if valid.issubset(m) and delta.issubset(m.values()):
seen.update(mapping)
if logging:
log.append((rule_num, str(bad_query), str(good_query), tuple(mapping.values())))
flag = True
break
else:
# restore old mapping
Expand All @@ -1541,10 +1547,13 @@ def fix_mapping(self: Union['ReactionContainer', 'StandardizeReaction']) -> bool
free_number = count(first_free)
continue
break

else:
if logging and flag_m:
log.append((rule_num, str(bad_query), str(good_query), ()))
if seen:
self.flush_cache()
return True
if logging:
return log
return flag

@classmethod
Expand Down

0 comments on commit 8a47f3f

Please sign in to comment.