Skip to content

Commit 41e2b1f

Browse files
committed
fixes. removed overoptimizations.
1 parent 3ab3629 commit 41e2b1f

18 files changed

Lines changed: 45 additions & 47 deletions

File tree

‎chython/algorithms/depict.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ def __render_atoms(self: 'MoleculeContainer', uid):
351351
define = []
352352
mask = []
353353

354-
for n, atom in self._atoms.items():
354+
for n, atom in self.atoms():
355355
x, y = atom.x, -atom.y
356356
symbol = atom.atomic_symbol
357357
if (symbol != 'C' or atom.charge or atom.is_radical or atom.isotope or carbon

‎chython/algorithms/fingerprints/__init__.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class Fingerprints(LinearFingerprint, MorganFingerprint):
3232
@property
3333
def _atom_identifiers(self: 'MoleculeContainer'):
3434
return {idx: hash((atom.isotope or 0, atom.atomic_number, atom.charge, atom.is_radical))
35-
for idx, atom in self._atoms.items()}
35+
for idx, atom in self.atoms()}
3636

3737

3838
class FingerprintsCGR(LinearFingerprint, MorganFingerprint):

‎chython/algorithms/isomorphism.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ def _cython_compiled_structure(self: 'MoleculeContainer'):
190190
bits2 = []
191191
bits3 = []
192192
bits4 = []
193-
for i, (n, a) in enumerate(self._atoms.items()):
193+
for i, (n, a) in enumerate(self.atoms()):
194194
mapping[n] = i
195195
numbers.append(n)
196196
v2 = 1 << (a.hybridization - 1)
@@ -318,7 +318,7 @@ def get_mapping(query, scope):
318318

319319
for mapping in self._get_mapping(other, automorphism_filter=automorphism_filter,
320320
searching_scope=searching_scope):
321-
for n, a in self._atoms.items():
321+
for n, a in self.atoms():
322322
if a.stereo is None:
323323
continue
324324
m = mapping[n]

‎chython/algorithms/mcs.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,10 @@ def __get_product(self: 'molecule.MoleculeContainer', other: 'molecule.MoleculeC
9797
o_bonds = other._bonds
9898

9999
s_equal = defaultdict(list) # equal self atoms
100-
for n, atom in self._atoms.items():
100+
for n, atom in self.atoms():
101101
s_equal[atom].append(n)
102102
p_equal = defaultdict(list) # equal other atoms
103-
for n, atom in other._atoms.items():
103+
for n, atom in other.atoms():
104104
p_equal[atom].append(n)
105105

106106
full_product = {}

‎chython/algorithms/morgan.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ def atoms_order(self: 'MoleculeContainer') -> Dict[int, int]:
4040
4141
:return: dict of atom-order pairs
4242
"""
43-
if not self._atoms: # for empty containers
43+
if not self: # for empty containers
4444
return {}
45-
elif len(self._atoms) == 1: # optimize single atom containers
46-
return dict.fromkeys(self._atoms, 1)
45+
elif len(self) == 1: # optimize single atom containers
46+
return dict.fromkeys(self, 1)
4747
ring = self.ring_atoms
48-
return _morgan({n: hash((hash(a), n in ring)) for n, a in self._atoms.items()}, self.int_adjacency)
48+
return _morgan({n: hash((hash(a), n in ring)) for n, a in self.atoms()}, self.int_adjacency)
4949

5050
@cached_property
5151
def int_adjacency(self: 'MoleculeContainer') -> Dict[int, Dict[int, int]]:

‎chython/algorithms/standardize/molecule.py‎

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def canonicalize(self: 'MoleculeContainer', *, fix_tautomers=True, keep_kekule=F
5555
h, changed = self.implicify_hydrogens(_fix_stereo=False, logging=True)
5656

5757
if fix_tautomers and (logging or keep_kekule): # thiele can change tautomeric form
58-
hgs = {n: a.implicit_hydrogens for n, a in self._atoms.items()}
58+
hgs = {n: a.implicit_hydrogens for n, a in self.atoms()}
5959
if keep_kekule: # save bond orders
6060
bonds = [(b, b.order) for _, _, b in self.bonds()]
6161

@@ -66,7 +66,7 @@ def canonicalize(self: 'MoleculeContainer', *, fix_tautomers=True, keep_kekule=F
6666

6767
if keep_kekule and t: # restore
6868
# check ring charge/hydrogen moving
69-
if c or fix_tautomers and hgs != self._hydrogens: # noqa
69+
if c or fix_tautomers and any(hgs[n] != a.implicit_hydrogens for n, a in self.atoms()):
7070
self.kekule() # we need to do full kekule again
7171
else:
7272
for b, o in bonds: # noqa
@@ -81,12 +81,12 @@ def canonicalize(self: 'MoleculeContainer', *, fix_tautomers=True, keep_kekule=F
8181
s.append((tuple(changed), -1, 'implicified'))
8282
if t:
8383
s.append(((), -1, 'aromatized'))
84-
if fix_tautomers and (x := tuple(n for n, a in self._atoms.items() if hgs[n] != a.implicit_hydrogens)):
84+
if fix_tautomers and (x := tuple(n for n, a in self.atoms() if hgs[n] != a.implicit_hydrogens)):
8585
s.append((x, -1, 'aromatic tautomer found'))
8686
if c:
8787
s.append((tuple(c), -1, 'recharged'))
8888
if keep_kekule and t:
89-
if c or fix_tautomers and any(hgs[n] != a.implicit_hydrogens for n, a in self._atoms.items()):
89+
if c or fix_tautomers and any(hgs[n] != a.implicit_hydrogens for n, a in self.atoms()):
9090
s.append(((), -1, 'kekulized again'))
9191
else:
9292
s.append(((), -1, 'kekule form restored'))
@@ -123,7 +123,7 @@ def standardize(self: Union['MoleculeContainer', 'Standardize'], *, logging=Fals
123123
log.extend(l)
124124
fixed.update(f)
125125

126-
if b := fixed.intersection(n for n, a in self._atoms.items() if a.implicit_hydrogens is None):
126+
if b := fixed.intersection(n for n, a in self.atoms() if a.implicit_hydrogens is None):
127127
if ignore:
128128
log.append((tuple(b), -1, 'standardization failed'))
129129
else:
@@ -271,17 +271,15 @@ def remove_coordinate_bonds(self: 'MoleculeContainer', *, keep_to_terminal=True,
271271
:param keep_to_terminal: Keep any bonds to terminal hydrogens
272272
:return: removed bonds count
273273
"""
274-
bonds = self._bonds
275-
276274
ab = [(n, m) for n, m, b in self.bonds() if b == 8]
277275

278276
if keep_to_terminal:
279277
skeleton = self.not_special_connectivity
280-
hs = {n for n, a in self._atoms.items() if a == H and not skeleton[n]}
278+
hs = {n for n, a in self.atoms() if a == H and not skeleton[n]}
281279
ab = [(n, m) for n, m in ab if n not in hs and m not in hs]
282280

283281
for n, m in ab:
284-
del bonds[n][m], bonds[m][n]
282+
self.delete_bond(n, m, _skip_calculation=True)
285283

286284
if ab:
287285
self.flush_cache(keep_sssr=True)
@@ -404,7 +402,7 @@ def check_valence(self: 'MoleculeContainer') -> List[int]:
404402
:return: list of invalid atoms
405403
"""
406404
# only invalid atoms have None hydrogens.
407-
return [n for n, a in self._atoms.items() if a.implicit_hydrogens is None]
405+
return [n for n, a in self.atoms() if a.implicit_hydrogens is None]
408406

409407
def clean_isotopes(self: 'MoleculeContainer') -> bool:
410408
"""

‎chython/algorithms/standardize/resonance.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ def __find_delocalize_path(self: 'MoleculeContainer', start, finish, constrains,
135135
def __entries(self: 'MoleculeContainer'):
136136
atoms = self._atoms
137137
bonds = self._bonds
138-
errors = {n for n, a in atoms.items() if a.implicit_hydrogens is None}
138+
errors = {n for n, a in self.atoms() if a.implicit_hydrogens is None}
139139

140140
transfer = set()
141141
entries = set()
@@ -144,7 +144,7 @@ def __entries(self: 'MoleculeContainer'):
144144
nitrogen_cat = set()
145145
nitrogen_ani = set()
146146
sulfur_cat = set()
147-
for n, a in atoms.items():
147+
for n, a in self.atoms():
148148
if a not in (B, C, N, O, Si, P, S, As, Se, Te):
149149
# filter non-organic set, halogens and aromatics
150150
continue
@@ -182,7 +182,7 @@ def __entries(self: 'MoleculeContainer'):
182182
transfer.add(n)
183183

184184
if exits or entries: # try to move cation to nitrogen. saturation fixup.
185-
for n, a in self._atoms.items():
185+
for n, a in self.atoms():
186186
if a == N and not a.charge:
187187
if a.hybridization == 1 and a.neighbors <= 3: # any amine - potential e-donor
188188
entries.add(n)

‎chython/algorithms/standardize/saturation.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@ def saturate(self: 'MoleculeContainer', neighbors_distances: Optional[Dict[int,
7676
expected_charge = int(self)
7777

7878
if reset_electrons:
79-
charges = {x: None for x in self._atoms}
80-
radicals = {x: None for x in self._atoms}
79+
charges = {x: None for x in self}
80+
radicals = {x: None for x in self}
8181
else:
82-
charges = {n: a.charge for n, a in self._atoms.items()}
83-
radicals = {n: a.is_radical for n, a in self._atoms.items()}
82+
charges = {n: a.charge for n, a in self.atoms()}
83+
radicals = {n: a.is_radical for n, a in self.atoms()}
8484
sat, adjacency = _find_possible_valences(atoms, neighbors_distances or self._bonds,
8585
charges, radicals, neighbors_distances is not None)
8686
charges = {} # new charge states

‎chython/algorithms/stereo.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ def tetrahedrons(self: 'MoleculeContainer') -> Tuple[int, ...]:
168168
Carbon sp3 atom numbers.
169169
"""
170170
tetra = []
171-
for n, atom in self._atoms.items():
171+
for n, atom in self.atoms():
172172
if atom == C and not atom.charge and not atom.is_radical:
173173
env = self._bonds[n]
174174
if all(b == 1 for b in env.values()):
@@ -577,7 +577,7 @@ def fix_stereo(self: 'MoleculeContainer'):
577577
atoms_stereo = []
578578
allenes_stereo = []
579579
cis_trans_stereo = []
580-
for n, a in self._atoms.items():
580+
for n, a in self.atoms():
581581
if a.stereo is None:
582582
continue
583583
elif n in stereo_tetrahedrons:
@@ -960,7 +960,7 @@ def __wedge_sign(self: 'MoleculeContainer', order):
960960

961961
@cached_property
962962
def _chiral_morgan(self: Union['MoleculeContainer', 'MoleculeStereo']) -> Dict[int, int]:
963-
stereo_atoms = {n for n, a in self._atoms.items() if a.stereo is not None}
963+
stereo_atoms = {n for n, a in self.atoms() if a.stereo is not None}
964964
stereo_bonds = {n for n, mb in self._bonds.items() if any(b.stereo is not None for m, b in mb.items())}
965965
if not stereo_atoms and not stereo_bonds:
966966
return self.atoms_order
@@ -1103,7 +1103,7 @@ def __chiral_centers(self: Union['MoleculeStereo', 'MoleculeContainer']):
11031103
chiral_c.add(n)
11041104

11051105
# skip already marked.
1106-
stereo_atoms = {n for n, a in self._atoms.items() if a.stereo is not None}
1106+
stereo_atoms = {n for n, a in self.atoms() if a.stereo is not None}
11071107
chiral_t.difference_update(stereo_atoms)
11081108
chiral_a.difference_update(stereo_atoms)
11091109
diff = set()

‎chython/algorithms/x3dom.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ def __render_atoms(self: 'MoleculeContainer', xyz):
181181

182182
atoms = []
183183
if carbon:
184-
for n, a in self._atoms.items():
184+
for n, a in self.atoms():
185185
r = radius or a.atomic_radius * multiplier
186186
fr = r * 0.71
187187
atoms.append(f" <transform translation='{' '.join(format(x, '.2f') for x in xyz[n])}'>\n"
@@ -197,7 +197,7 @@ def __render_atoms(self: 'MoleculeContainer', xyz):
197197
f" </appearance>\n <sphere radius='{r:.2f}'/>\n"
198198
" </shape>\n </group>\n </billboard>\n </transform>\n")
199199
else:
200-
for n, a in self._atoms.items():
200+
for n, a in self.atoms():
201201
r = radius or a.atomic_radius * multiplier
202202
atoms.append(f" <transform translation='{' '.join(format(x, '.2f') for x in xyz[n])}'>\n"
203203
" <shape>\n <appearance>\n"

0 commit comments

Comments
 (0)