Skip to content

Commit

Permalink
clean2d fixed for Queries.
Browse files Browse the repository at this point in the history
  • Loading branch information
stsouko committed Mar 3, 2021
1 parent ababd3b commit aee5d3e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
24 changes: 19 additions & 5 deletions CGRtools/algorithms/calculate2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,17 +119,31 @@ def _clean2d_prepare(self):
return ''.join(smiles), order


class Calculate2DCGR(Calculate2D):
class Calculate2DQuery(Calculate2D):
__slots__ = ()

def _clean2d_prepare(self):
charges = self._charges
radicals = self._radicals
self._radicals = {n: False for n in radicals}
try:
smiles, order = self._smiles(self._smiles_order, _return_order=True, stereo=False, hybridization=False,
neighbors=False, rings=False, hydrogens=False, heteroatoms=False)
except Exception:
raise
else:
return ''.join(smiles), order
finally:
self._radicals = radicals


class Calculate2DCGR(Calculate2D):
__slots__ = ()

def _clean2d_prepare(self):
mol = molecule.MoleculeContainer()
for n, atom in self._atoms.items():
atom = Element.from_atomic_number(atom.atomic_number)(atom.isotope)
mol.add_atom(atom, n, charge=charges[n], is_radical=radicals[n], xy=(0., 0.))
atom = Element.from_atomic_number(atom.atomic_number or 6)()
mol.add_atom(atom, n)
for n, m, bond in self.bonds():
if bond.order == bond.p_order:
mol.add_bond(n, m, bond.order)
Expand Down Expand Up @@ -179,4 +193,4 @@ def clean2d(self):
raise NotImplemented('py-mini-racer required for clean2d')


__all__ = ['Calculate2DMolecule', 'Calculate2DCGR']
__all__ = ['Calculate2DMolecule', 'Calculate2DCGR', 'Calculate2DQuery']
8 changes: 4 additions & 4 deletions CGRtools/containers/query.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
#
# Copyright 2018-2020 Ramil Nugmanov <nougmanoff@protonmail.com>
# Copyright 2018-2021 Ramil Nugmanov <nougmanoff@protonmail.com>
# This file is part of CGRtools.
#
# CGRtools is free software; you can redistribute it and/or modify
Expand All @@ -20,15 +20,15 @@
from . import molecule # cyclic imports resolve
from .bonds import Bond, QueryBond
from .common import Graph
from ..algorithms.calculate2d import Calculate2DMolecule
from ..algorithms.calculate2d import Calculate2DQuery
from ..algorithms.components import StructureComponents
from ..algorithms.depict import DepictQuery
from ..algorithms.smiles import QuerySmiles
from ..algorithms.stereo import QueryStereo
from ..periodictable import Element, QueryElement, AnyElement


class QueryContainer(QueryStereo, Graph, QuerySmiles, StructureComponents, DepictQuery, Calculate2DMolecule):
class QueryContainer(QueryStereo, Graph, QuerySmiles, StructureComponents, DepictQuery, Calculate2DQuery):
__slots__ = ('_neighbors', '_hybridizations', '_atoms_stereo', '_cis_trans_stereo', '_allenes_stereo',
'_hydrogens', '_rings_sizes', '_heteroatoms')

Expand Down Expand Up @@ -236,7 +236,7 @@ def substructure(self, atoms, **kwargs) -> 'QueryContainer':
sub._rings_sizes = {n: srs[n] for n in atoms}
sub._heteroatoms = {n: sha[n] for n in atoms}

lost = {n for n, a in sa.items() if a.atomic_number != 1} - set(atoms) # atoms not in substructure
lost = sa.keys() - set(atoms) # atoms not in substructure
not_skin = {n for n in atoms if lost.isdisjoint(sb[n])}
sub._atoms_stereo = {n: s for n, s in self._atoms_stereo.items() if n in not_skin}
sub._allenes_stereo = {n: s for n, s in self._allenes_stereo.items() if
Expand Down

0 comments on commit aee5d3e

Please sign in to comment.