Skip to content

Commit

Permalink
reagents removing protocol (#162)
Browse files Browse the repository at this point in the history
* reagents removing protocol

* reagents removing protocol, more general function

* reagents removing protocol, move to algo

* reagents removing protocol, move to algo, (self) corrections

* fix

* ff

Co-authored-by: Ramil Nugmanov <nougmanoff@protonmail.com>
  • Loading branch information
TimurGimadiev and stsouko committed Jan 15, 2021
1 parent af9bc72 commit eec679b
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions CGRtools/algorithms/components/reaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from itertools import chain, product
from typing import Tuple, Iterator, TYPE_CHECKING
from ...containers import molecule # cyclic imports resolve
from ...exceptions import MappingError


if TYPE_CHECKING:
Expand Down Expand Up @@ -207,5 +208,39 @@ def enumerate_centers(self) -> Iterator['ReactionContainer']:
cp.meta.clear()
yield cp

def remove_reagents(self, keep_reagents=False):
"""
Preprocess reaction according to mapping, using the following idea: molecules(each separated graph) will be
placed to reagents if it is not changed in the reaction (no bonds, charges reorders)
"""
cgr = ~self
if cgr.center_atoms:
active = set(cgr.center_atoms)
reactants = []
products = []
reagents = set(self.reagents)
for i in self.reactants:
if not active.isdisjoint(i):
reactants.append(i)
else:
reagents.add(i)
for i in self.products:
if not active.isdisjoint(i):
products.append(i)
else:
reagents.add(i)
if keep_reagents:
tmp = []
for m in self.reagents:
if m in reagents:
tmp.append(m)
reagents.discard(m)
tmp.extend(reagents)
reagents = tmp
else:
reagents = ()
return self.__class__(reactants, products, reagents, self.meta)
raise MappingError("Reaction center is absent according to mapping")


__all__ = ['ReactionComponents']

0 comments on commit eec679b

Please sign in to comment.