Skip to content

Commit

Permalink
AnyMetal query atom added. (#185)
Browse files Browse the repository at this point in the history
  • Loading branch information
stsouko committed Jul 1, 2021
1 parent 4bbb64d commit d4fc2c6
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 9 deletions.
4 changes: 2 additions & 2 deletions CGRtools/periodictable/__init__.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 Down Expand Up @@ -43,7 +43,7 @@
elements = {k: v for k, v in globals().items() if isinstance(v, ABCMeta) and k != 'Element' and issubclass(v, Element)}

__all__ = ['Element', 'DynamicElement', 'QueryElement', 'DynamicQueryElement', 'AnyElement', 'DynamicAnyElement',
'ListElement', 'AnyAtom']
'ListElement', 'AnyAtom', 'AnyMetal']
__all__.extend(k for k in globals() if k.startswith('Group'))
__all__.extend(k for k in globals() if k.startswith('Period'))
__all__.extend(elements)
Expand Down
7 changes: 4 additions & 3 deletions CGRtools/periodictable/element/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
#
# Copyright 2019, 2020 Ramil Nugmanov <nougmanoff@protonmail.com>
# Copyright 2019-2021 Ramil Nugmanov <nougmanoff@protonmail.com>
# Copyright 2019 Tagir Akhmetshin <tagirshin@gmail.com>
# Copyright 2019 Dayana Bashirova <dayana.bashirova@yandex.ru>
# This file is part of CGRtools.
Expand All @@ -26,8 +26,9 @@
from .dynamic_query import *


AnyAtom = Union[Element, DynamicElement, QueryElement, DynamicQueryElement, AnyElement, DynamicAnyElement, ListElement]
AnyAtom = Union[Element, DynamicElement, QueryElement, DynamicQueryElement, AnyElement, AnyMetal, DynamicAnyElement,
ListElement]


__all__ = ['Core', 'Element', 'DynamicElement', 'QueryElement', 'DynamicQueryElement',
'AnyElement', 'DynamicAnyElement', 'AnyAtom', 'ListElement']
'AnyElement', 'AnyMetal', 'DynamicAnyElement', 'AnyAtom', 'ListElement']
44 changes: 40 additions & 4 deletions CGRtools/periodictable/element/query.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
#
# Copyright 2020, 2021 Ramil Nugmanov <nougmanoff@protonmail.com>
# Copyright 2021 Dmitrij Zanadvornykh <>
# This file is part of CGRtools.
#
# CGRtools is free software; you can redistribute it and/or modify
Expand Down Expand Up @@ -314,6 +315,44 @@ def __hash__(self):
self.implicit_hydrogens, self.heteroatoms))


class AnyMetal(AnyElement):
"""
Charge and radical ignored any metal.
"""
@property
def atomic_symbol(self) -> str:
return 'M'

def __eq__(self, other):
if isinstance(other, Element):
if other.atomic_symbol not in {'He', 'Ne', 'Ar', 'Kr', 'Xe', 'F', 'Cl', 'Br', 'I', 'C', 'N', 'O', 'H', 'Si',
'P', 'S', 'Se', 'Ge', 'As', 'Sb', 'Te'}:
if self.neighbors and other.neighbors not in self.neighbors:
return False
if self.hybridization and other.hybridization not in self.hybridization:
return False
if self.ring_sizes:
if self.ring_sizes[0]:
if set(self.ring_sizes).isdisjoint(other.ring_sizes):
return False
elif other.ring_sizes: # not in ring expected
return False
if self.implicit_hydrogens and other.implicit_hydrogens not in self.implicit_hydrogens:
return False
if self.heteroatoms and other.heteroatoms not in self.heteroatoms:
return False
return True
elif isinstance(other, AnyMetal) and self.neighbors == other.neighbors \
and self.hybridization == other.hybridization and self.ring_sizes == other.ring_sizes \
and self.implicit_hydrogens == other.implicit_hydrogens and self.heteroatoms == other.heteroatoms:
return True
return False

def __hash__(self):
return tuple_hash((self.neighbors, self.hybridization, self.ring_sizes, self.implicit_hydrogens,
self.heteroatoms))


class ListElement(AnyElement):
__slots__ = ('_elements', '_numbers')

Expand Down Expand Up @@ -374,9 +413,6 @@ def copy(self) -> 'ListElement':
return copy

def __hash__(self):
"""
13bit = 4bit | 1bit | 4bit | 4bit
"""
return tuple_hash((self._numbers, self.charge, self.is_radical, self.neighbors, self.hybridization,
self.ring_sizes, self.implicit_hydrogens, self.heteroatoms))

Expand All @@ -395,4 +431,4 @@ def __repr__(self):
return f'{self.__class__.__name__}([{",".join(self._elements)}])'


__all__ = ['QueryElement', 'AnyElement', 'ListElement']
__all__ = ['QueryElement', 'AnyElement', 'AnyMetal', 'ListElement']

0 comments on commit d4fc2c6

Please sign in to comment.