-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfinders.py
1015 lines (842 loc) · 39 KB
/
finders.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""finders are used by Geometry.find to locate atoms in a more general way"""
import sys
import inspect
from collections import deque
import numpy as np
from AaronTools import addlogger
def get_class(name):
"""returns the finder class with the given name"""
for obj_name, obj in inspect.getmembers(sys.modules[__name__]):
if obj_name == name and inspect.isclass(obj):
return obj
raise ValueError("no finder named %s in AaronTools.finders" % name)
class Finder:
def get_matching_atoms(self, atoms, geometry=None):
"""
overwrite with function that returns list(Atom) of the atoms that
match your Finder's criteria
geometry is an optional argument that could be used to e.g. find
atoms a certain number of bonds
"""
pass
class BondsFrom(Finder):
"""
exact number of bonds from specified atom
avoid: bonding path cannot pass through these atoms
"""
def __init__(self, central_atom, number_of_bonds, avoid=None):
super().__init__()
self.central_atom = central_atom
self.number_of_bonds = number_of_bonds
self.avoid = avoid
def __repr__(self):
return "atoms %i bonds from %s" % (self.number_of_bonds, self.central_atom)
def get_matching_atoms(self, atoms, geometry):
"""returns List(Atom) that are a certain number of bonds away from the given atom"""
matching_atoms = []
stack = deque([self.central_atom])
next_stack = deque([])
frag = [self.central_atom]
n_bonds = 1
while stack and self.number_of_bonds:
next_connected = stack.popleft()
connected = next_connected.connected - set(frag)
frag += connected
next_stack.extend(connected)
if not stack:
if n_bonds == self.number_of_bonds:
return list(next_stack)
n_bonds += 1
stack = next_stack
next_stack = deque([])
return matching_atoms
class WithinBondsOf(BondsFrom):
"""within a specified number of bonds from the atom"""
def __init__(self, central_atom, number_of_bonds, **kwargs):
super().__init__(central_atom, number_of_bonds)
def __repr__(self):
return "atoms within %i bonds of %s" % (self.number_of_bonds, self.central_atom)
def get_matching_atoms(self, atoms, geometry):
"""returns List(Atom) that are a certain number of bonds away from the given atom"""
matching_atoms = []
stack = deque([self.central_atom])
next_stack = deque([])
frag = [self.central_atom]
n_bonds = 1
while stack and self.number_of_bonds:
next_connected = stack.popleft()
connected = next_connected.connected - set(frag)
if n_bonds < self.number_of_bonds:
next_stack.extend(connected)
matching_atoms.extend(connected)
frag += connected
if not stack:
n_bonds += 1
stack = next_stack
next_stack = deque([])
return matching_atoms
class BondedTo(Finder):
"""returns all atoms that are bonded to the specified atom"""
def __init__(self, atom):
super().__init__()
self.atom = atom
def __repr__(self):
return "atoms bonded to %s" % self.atom
def get_matching_atoms(self, atoms, geometry=None):
"""returns list(Atom) that are within a radius of a point"""
try:
return [atom for atom in atoms if atom in self.atom.connected]
except AttributeError:
pass
class WithinRadiusFromPoint(Finder):
"""within a specified radius of a point"""
def __init__(self, point, radius):
super().__init__()
self.point = np.array(point)
self.radius = radius
def __repr__(self):
return "atoms within %.2f angstroms of (%.2f, %.2f, %.2f)" % (self.radius, *self.point)
def get_matching_atoms(self, atoms, geometry=None):
"""returns list(Atom) that are within a radius of a point"""
keep = np.arange(0, len(atoms), dtype=int)
coords = np.array([atom.coords for atom in atoms])
coords -= self.point
mask = np.where(coords[:, 0] < self.radius)
coords = coords[mask]
keep = keep[mask]
mask = np.where(coords[:, 1] < self.radius)
coords = coords[mask]
keep = keep[mask]
mask = np.where(coords[:, 2] < self.radius)
coords = coords[mask]
keep = keep[mask]
dist = np.linalg.norm(coords, axis=1)
mask = np.where(dist < self.radius)
keep = keep[mask]
matching_atoms = [atoms[k] for k in keep]
return matching_atoms
class WithinRadiusFromAtom(Finder):
"""within a specified radius of a point"""
def __init__(self, atom, radius):
super().__init__()
self.atom = atom
self.radius = radius
def __repr__(self):
return "atoms within %.2f angstroms of %s" % (self.radius, self.atom)
def get_matching_atoms(self, atoms, geometry=None):
"""returns list(Atom) that are within a radius of a point"""
keep = np.arange(0, len(atoms), dtype=int)
coords = np.array([atom.coords for atom in atoms])
coords -= self.atom.coords
mask = np.where(coords[:, 0] < self.radius)
coords = coords[mask]
keep = keep[mask]
mask = np.where(coords[:, 1] < self.radius)
coords = coords[mask]
keep = keep[mask]
mask = np.where(coords[:, 2] < self.radius)
coords = coords[mask]
keep = keep[mask]
dist = np.linalg.norm(coords, axis=1)
mask = np.where(dist < self.radius)
keep = keep[mask]
matching_atoms = [atoms[k] for k in keep]
return matching_atoms
class NotAny(Finder):
"""atoms not matching specifiers/Finders"""
def __init__(self, *critera, **kwargs):
"""critera can be any number of Finders and/or other atom specifiers (tags, elements, etc.)"""
super().__init__()
if not critera and "critera" in kwargs:
critera = kwargs["critera"]
if len(critera) == 1:
if isinstance(critera[0], tuple):
critera = critera[0]
self.critera = critera
def __repr__(self):
return "not any of: %s" % ", ".join([str(x) for x in self.critera])
def get_matching_atoms(self, atoms, geometry):
"""returns List(Atom) that do not match any of the critera"""
unmatching_atoms = []
for criterion in self.critera:
try:
unmatch = geometry.find(criterion)
unmatching_atoms.extend(unmatch)
except LookupError:
pass
return [atom for atom in atoms if atom not in set(unmatching_atoms)]
class AnyTransitionMetal(Finder):
"""any atoms that are transition metals"""
def __init__(self):
super().__init__()
def __repr__(self):
return "any transition metal"
def get_matching_atoms(self, atoms, geometry=None):
"""returns List(Atom) of atoms that are metals"""
from AaronTools.const import TMETAL
return [atom for atom in atoms if atom.element in TMETAL]
class AnyNonTransitionMetal(NotAny):
"""any atoms that are not transition metals"""
def __init__(self, *a, **kw):
super().__init__(AnyTransitionMetal())
def __repr__(self):
return "any non-transition metal"
class HasAttribute(Finder):
"""all atoms with the specified attribute"""
def __init__(self, attribute_name):
super().__init__()
self.attribute_name = attribute_name
def __repr__(self):
return "atoms with the '%s' attribute" % self.attribute_name
def get_matching_atoms(self, atoms, geometry=None):
"""returns List(Atom) of atoms that have the attribute"""
return [atom for atom in atoms if hasattr(atom, self.attribute_name)]
@addlogger
class VSEPR(Finder):
"""
atoms with the specified VSEPR geometry
see Atom.get_shape for a list of valid vsepr strings
"""
LOG = None
def __init__(self, vsepr, cutoff=0.5):
super().__init__()
self.vsepr = vsepr
if any(vsepr == x for x in ["triangular cupola", "heptagonal bipyramidal"]):
self.LOG.warning(
"triangular cupola and heptagonal bipyramidal cannot be distinguished"
)
self.cutoff = cutoff
def __repr__(self):
return "atoms with %s shape" % self.vsepr
def get_matching_atoms(self, atoms, geometry=None):
matching_atoms = []
for atom in atoms:
out = atom.get_vsepr()
if out is not None:
shape, score = atom.get_vsepr()
if shape == self.vsepr and score < self.cutoff:
matching_atoms.append(atom)
return matching_atoms
class BondedElements(Finder):
"""
atoms bonded to the specified neighboring elements
if match_exact=True (default), elements must match exactly
e.g. BondedElements('C') will find
atoms bonded to only one carbon and nothing else
"""
def __init__(self, *args, match_exact=True, **kwargs):
super().__init__()
if not args and "elements" in kwargs:
args = kwargs["elements"]
self.elements = list(args)
self.match_exact = match_exact
def __repr__(self):
if len(self.elements) == 0:
return "atoms bonded to nothing"
elif len(self.elements) == 1:
return "atoms bonded to %s" % self.elements[0]
else:
return "atoms bonded to %s and %s" % (", ".join(self.elements[:-1]), self.elements[-1])
def get_matching_atoms(self, atoms, geometry=None):
matching_atoms = []
if self.match_exact:
ref = "".join(sorted(self.elements))
else:
ref = self.elements
for atom in atoms:
if self.match_exact:
ele_list = [a.element for a in [ele for ele in atom.connected]]
test = "".join(sorted(ele_list))
if ref == test:
matching_atoms.append(atom)
else:
bonded_eles = [bonded_atom.element for bonded_atom in atom.connected]
if all([ele in bonded_eles for ele in self.elements]):
matching_atoms.append(atom)
return matching_atoms
class NumberOfBonds(Finder):
"""atoms with the specified number of bonds"""
def __init__(self, num_bonds):
super().__init__()
self.num_bonds = num_bonds
def __repr__(self):
return "atoms with %i bonds" % self.num_bonds
def get_matching_atoms(self, atoms, geometry=None):
return [atom for atom in atoms if len(atom.connected) == self.num_bonds]
class ChiralCentres(Finder):
"""
chiral centers
atoms with a non-planar VSEPR geometry with all bonded groups
being distinct
for rings, looks for a set of unique canonical ranks for atoms that
are all the same number of bonds away from one atom
"""
#IUPAC spelling
def __init__(self, RS_only=False):
"""RS_only: bool - if True, do not identify chiral centers that are chiral because they
are connected to multiple chiral fragments with the same chirality
this corresponds to R/S centers
False will include r/s centers as well
"""
super().__init__()
self.RS_only = RS_only
def __repr__(self):
return "chiral centers"
def get_matching_atoms(self, atoms, geometry):
from AaronTools.geometry import Geometry
from AaronTools.symmetry import PointGroup
# from time import perf_counter
#
# start = perf_counter()
matching_atoms = []
# b/c they are connected to chiral fragments
geometry.refresh_ranks()
chiral_atoms_changed = True
ranks = geometry.canonical_rank(break_ties=False, update=False, invariant=True)
frags = []
properly_shaped_atoms = []
for atom in geometry.atoms:
if len(atom.connected) < 3:
continue
vsepr, _ = atom.get_vsepr()
if vsepr in ['trigonal planar', 't shaped', 'sqaure planar']:
continue
properly_shaped_atoms.append(atom)
frags.append([])
single_atoms = dict()
for bonded_atom in atom.connected:
frag = geometry.get_fragment(bonded_atom, atom, as_object=False)
frags[-1].append(frag)
# keep track of single atom fragments to more quickly
# eliminate atoms that aren't chiral
if len(frag) == 1:
if frag[0].element in single_atoms:
single_atoms[frag[0].element] += 1
if single_atoms[frag[0].element] >= len(atom.connected) / 2:
frags.pop(-1)
properly_shaped_atoms.pop(-1)
break
else:
single_atoms[frag[0].element] = 1
# print(properly_shaped_atoms)
# need to do multiple passes b/c sometimes atoms are chiral
# because of other chiral centers
k = 0
while chiral_atoms_changed:
chiral_atoms_changed = False
k += 1
#skip atoms we've already found
for ndx, atom in enumerate(properly_shaped_atoms):
if atom in matching_atoms:
continue
neighbor_ranks = [
ranks[geometry.atoms.index(bonded_atom)]
for bonded_atom in atom.connected
]
# first iteration should only look for centers that are chiral
# because the fragments are different
if k == 1 and len(atom.connected) <= 4 and all(
neighbor_ranks.count(rank) == 1 for rank in neighbor_ranks
):
matching_atoms.append(atom)
chiral_atoms_changed = True
elif k == 1 and len(atom.connected) > 4:
test_geom = Geometry(
[atom, *atom.connected], refresh_ranks=False, refresh_connected=False
)
groups = [ranks[geometry.atoms.index(a)] for a in test_geom.atoms]
pg = PointGroup(test_geom, groups=groups, center=atom.coords)
print(pg.name)
if pg.name == "C1":
matching_atoms.append(atom)
chiral_atoms_changed = True
# more iterations should only look for centers that are
# chiral because of the presence of other chiral centers
elif k > 1 and all(
neighbor_ranks.count(rank) <= len(atom.connected) / 2 for rank in neighbor_ranks
):
chiral = True
for i, frag1 in enumerate(frags[ndx]):
#get the ranks of the atoms in this fragment
ranks_1 = [ranks[geometry.atoms.index(atom)] for atom in frag1]
for frag2 in frags[ndx][:i]:
same = True
ranks_2 = [ranks[geometry.atoms.index(atom)] for atom in frag2]
if len(frag1) != len(frag2):
same = False
continue
for a, b in zip(sorted(ranks_1), sorted(ranks_2)):
# want correct elements
if a != b:
same = False
break
for a, b in zip(sorted(frag1), sorted(frag2)):
# and other chiral atoms
# correct connected elements
for o, l in zip(
sorted([aa.element for aa in a.connected]),
sorted([bb.element for bb in b.connected]),
):
if o != l:
same = False
break
if a is b:
break
if not self.RS_only and a in matching_atoms and b in matching_atoms:
#use RMSD to see if they have the same handedness
a_connected = sorted(a.connected)
b_connected = sorted(b.connected)
a_targets = [a] + list(a_connected)
b_targets = [b] + list(b_connected)
if geometry.RMSD(
geometry,
targets=a_targets,
ref_targets=b_targets,
sort=False,
align=False,
) < 0.1:
same = False
break
# I'm not sure why this code was here...
# ring_atoms = [
# bonded_atom for bonded_atom in atom.connected
# if bonded_atom in frag1 and bonded_atom in frag2
# ]
# if len(ring_atoms) > 0:
# #this is a ring
# #look at the rank of all atoms that are n bonds away from this atom
# #if the ranks are ever all different, this is a chiral center
# n_bonds = 1
# acceptable_nbonds = True
# while acceptable_nbonds:
# try:
# atoms_within_nbonds = geometry.find(BondsFrom(atom, n_bonds))
# nbonds_ranks = [
# ranks[geometry.atoms.index(a)] for a in atoms_within_nbonds
# ]
# if all(nbonds_ranks.count(r) == 1 for r in nbonds_ranks):
# same = False
# acceptable_nbonds = False
# elif not self.RS_only:
# # need to find things in the ring that are chiral
# # b/c of other chiral centers
# for n, atom1 in enumerate(atoms_within_nbonds):
# for m, atom2 in enumerate(atoms_within_nbonds[n+1:]):
# p = m + n + 1
# if nbonds_ranks[n] == nbonds_ranks[p]:
# a_connected = sorted(atom1.connected)
# b_connected = sorted(atom2.connected)
# a_targets = [atom1] + list(a_connected)
# b_targets = [atom2] + list(b_connected)
# if geometry.RMSD(
# geometry,
# targets=a_targets,
# ref_targets=b_targets,
# sort=False,
# align=False,
# ) < 0.1:
# same = False
# break
# if not same:
# break
#
# n_bonds += 1
# except LookupError:
# acceptable_nbonds = False
#
# if not same:
# break
if same:
chiral = False
break
if chiral:
chiral_atoms_changed = True
matching_atoms.append(atom)
if self.RS_only:
break
# stop = perf_counter()
# print("took %.3fs" % (stop - start))
return matching_atoms
#alternative spelling
ChiralCenters = ChiralCentres
class FlaggedAtoms(Finder):
"""
atoms with a non-zero flag
"""
# useful for finding constrained atoms
def __repr__(self):
return "flagged atoms"
def get_matching_atoms(self, atoms, geometry):
return [atom for atom in atoms if atom.flag]
class CloserTo(Finder):
"""
atoms closer to atom1 than atom2 (based on bonds, not actual distance)
"""
def __init__(self, atom1, atom2, include_ties=False):
super().__init__()
self.atom1 = atom1
self.atom2 = atom2
self.include_ties = include_ties
def __repr__(self):
return "atoms closer to %s than %s" % (self.atom1, self.atom2)
def get_matching_atoms(self, atoms, geometry):
matching_atoms = []
for atom in atoms:
if atom is self.atom1 and atom is not self.atom2:
matching_atoms.append(atom)
continue
try:
d1 = len(geometry.shortest_path(self.atom1, atom))
except LookupError:
d1 = False
try:
d2 = len(geometry.shortest_path(self.atom2, atom))
except LookupError:
d2 = False
if d1 is not False and d2 is not False and d1 <= d2:
if self.include_ties:
matching_atoms.append(atom)
elif d1 < d2:
matching_atoms.append(atom)
elif d1 is not False and d2 is False:
matching_atoms.append(atom)
return matching_atoms
class IsElement(Finder):
"""all atoms of the specified element"""
def __init__(self, element):
super().__init__()
self.element = element
def __repr__(self):
return "atoms of the element '%s'" % self.element
def get_matching_atoms(self, atoms, geometry=None):
"""returns List(Atom) of atoms of that element"""
return [atom for atom in atoms if atom.element == self.element]
class OfType(Finder):
"""
all atoms of the specified GAFF atom type
if ignore_metals = True (default), bonding with metals will not count towards VSEPR shapes
"""
def __init__(self, atomtype, ignore_metals=True):
super().__init__()
self.atomtype = atomtype.capitalize()
if self.atomtype in {'Br', 'Cl'}:
self.element = self.atomtype
else:
self.split_type = list(self.atomtype)
self.element = self.split_type[0]
self.ignore_metals = ignore_metals
def __repr__(self):
return "atoms of the gaff atomtype '%s'" % self.atomtype
def get_matching_atoms(self, atoms, geometry):
"""returns List(Atom) that are of the given atom type"""
# geom = geometry.copy()
if self.ignore_metals == True:
metals = []
from AaronTools.const import TMETAL
for i, atom in enumerate(geometry.atoms):
atom.index = i
if atom.element in TMETAL:
metals.append(atom)
geometry - metals
atoms = geometry.atoms
atoms = [atom for atom in atoms if atom.element == self.element]
class CustomError(Exception):
pass
shapes = {'C1': ['linear 1', 'linear 2'],
'C2': ['trigonal planar', 'bent 2 planar'],
'C3': ['trigonal pyramidal', 'tetrahedral'],
'C': ['trigonal planar'],
'Ha': ['linear 1'],
'Hc': ['linear 1'],
'N1': ['linear 1', 'linear 2'],
'N2': ['bent 2 planar', 'bent 2 tetrahedral'],
'N3': ['trigonal pyramidal', 'bent 3 tetrahedral'],
'N4': ['tetrahedral'],
'Na': ['trigonal planar'],
'S4': ['trigonal planar'],
'S6': ['tetrahedral'],
'P3': ['trigonal pyramidal'],
'P4': ['trigonal planar'],
'P5': ['tetrahedral'],
'Ca': ['trigonal planar', 'bent 2 planar'],
'N': ['trigonal planar'],
'Nh': ['trigonal planar'],
'Os': ['bent 2 tetrahedral', 'bent 2 planar']}
"""helper functions"""
def is_carbonyl(atom):
"""returns True if atom is carbonyl carbon"""
for connected in atom.connected:
if connected.element == 'O' and connected in BondedElements(atom.element).get_matching_atoms(atoms):
carbonyl = True
break
else:
carbonyl = False
return carbonyl
def is_carboxyl(atom):
"""returns True if atom is carboxyl carbon"""
o_counter = 0
if is_carbonyl(atom):
for connected in atom.connected:
if connected.element == 'O':
o_counter += 1
if o_counter == 2:
return True
else:
return False
def is_water(atom):
if atom.element == 'O':
h_counter = 0
for connected in atom.connected:
if connected.element == 'H': h_counter +=1
if h_counter == 2: return True
else: return False
else: return False
def is_amide(atom):
if atom.element == 'N':
for connected in atom.connected:
if is_carbonyl(connected):
return True
break
else: return False
else: return False
matching_atoms = []
if self.atomtype in {'F', 'Cl', 'Br', 'I'}:
for atom in IsElement(self.atomtype).get_matching_atoms(atoms): matching_atoms.append(atom)
elif self.split_type[0] == 'H' and self.atomtype not in {'Ha', 'Hc'}:
if self.split_type[1] in {'o','w'}:
for atom in BondedElements('O').get_matching_atoms(atoms):
for connected in atom.connected:
if self.atomtype == 'Hw' and is_water(connected): matching_atoms.append(atom)
elif self.atomtype == 'Ho' and not is_water(connected): matching_atoms.append(atom)
else:
for atom in BondedElements(self.split_type[1].capitalize()).get_matching_atoms(atoms): matching_atoms.append(atom)
elif self.atomtype in {'O', 'S2', 'P2'}:
for atom in BondedElements('C').get_matching_atoms(atoms): matching_atoms.append(atom)
elif self.atomtype in {'C', 'C2', 'Ca', 'Na', 'Nh', 'Ha', 'Hc','N'}:
aromatics, charge, fused = geometry.get_aromatic_atoms(atoms, return_rings=False)
for shape in shapes.get(self.atomtype):
for atom in VSEPR(shape).get_matching_atoms(atoms):
if self.atomtype == 'Ca' and atom in aromatics and not is_carbonyl(atom): matching_atoms.append(atom)
elif self.atomtype == 'C2' and atom not in aromatics and not is_carbonyl(atom): matching_atoms.append(atom)
elif self.atomtype == 'C' and is_carbonyl(atom): matching_atoms.append(atom)
elif self.atomtype == 'N' and is_amide(atom) and atom not in aromatics: matching_atoms.append(atom)
elif self.atomtype == 'Na' and charge == 1 and atom.element == 'N' and not is_carboxyl(atom) and len(matching_atoms) == 0 and atom in aromatics: matching_atoms.append(atom)
elif self.atomtype == 'Na' and atom not in aromatics and not is_carboxyl(atom) and not is_amide(atom): matching_atoms.append(atom)
elif self.atomtype in {'Na','Nh'} and charge == 1 and atom.element == 'N' and len(matching_atoms) > 0 and atom in aromatics:
raise CustomError("Indistinguishable nitrogens in aromatic ring")
elif self.atomtype == 'Nh' and charge == 0 and atom.element == 'N' and atom in aromatics: matching_atoms.append(atom)
elif self.atomtype in {'Hc', 'Ha'}:
for connected in atom.connected:
if self.atomtype == 'Ha' and connected.element == 'C' and connected in aromatics: matching_atoms.append(atom)
elif self.atomtype == 'Hc' and connected.element == 'C' and connected not in aromatics: matching_atoms.append(atom)
elif self.atomtype in {'Oh', 'Os', 'Sh', 'Ss','Ow'}:
for shape in shapes.get('Os'):
for atom in VSEPR(shape,cutoff=0.7).get_matching_atoms(atoms):
counter = 0
for connected in atom.connected:
if self.split_type[1] == 'h' and connected.element == 'H' and not is_water(atom): matching_atoms.append(atom)
elif self.split_type[1] == 'w' and is_water(atom) and atom not in matching_atoms: matching_atoms.append(atom)
elif self.split_type[1] == 's' and connected.element != 'H': counter +=1
if counter == 2: matching_atoms.append(atom)
elif self.atomtype == 'No':
for atom in IsElement('N').get_matching_atoms(atoms):
if is_carboxyl(atom): matching_atoms.append(atom)
else:
for shape in shapes.get(self.atomtype):
for atom in VSEPR(shape).get_matching_atoms(atoms): matching_atoms.append(atom)
matching_atoms = [match for match in matching_atoms if match.element == self.element]
#add metals back into geometry and reorder
geometry = geometry + metals
new_atoms = [0]* len(geometry.atoms)
for atom in geometry.atoms:
new_atoms[atom.index] = atom
geometry.atoms = new_atoms
return matching_atoms
class Aromatics(Finder):
"""all atoms in aromatic rings"""
def __init__(self):
super().__init__()
def __repr__(self):
return "atoms that are in aromatic rings"
def get_matching_atoms(self, atoms, geometry):
aromatics, charge, fused = geometry.get_aromatic_atoms(atoms,return_rings=False)
return aromatics
class ONIOMLayer(Finder):
"""all atoms in a given ONIOM layer or list of ONIOM layers"""
def __init__(self, layers=""):
super().__init__()
self.layers = layers
if isinstance(layers, list):
for layer in self.layers:
if layer.capitalize() not in ['H', 'M', 'L']:
raise ValueError("layer must be H, M, or L")
def __repr__(self):
return "atoms in the ONIOM layer '%s'" % self.layers
def get_matching_atoms(self, atoms, geometry=None):
matching_atoms = []
for atom in atoms:
if isinstance(self.layers, list):
try:
for layer in self.layers:
if atom.layer.capitalize() == layer.capitalize(): matching_atoms.append(atom)
except AttributeError:
pass
#print("ONIOMlayer only accepts OniomAtom type atoms")
else:
try:
if atom.layer.capitalize() == self.layer.capitalize(): matching_atoms.append(atom)
except AttributeError:
pass #print("ONIOMlayer only accepts OniomAtom type atoms")
return matching_atoms
class AmideCarbon(Finder):
"""
amide carbons
trigonal planar carbons bonded to a linear oxygen and a
nitrogen with 3 bonds
"""
def __repr__(self):
return "amide carbons"
def get_matching_atoms(self, atoms, geometry):
matching_atoms = []
carbons = geometry.find("C", VSEPR("trigonal planar"))
oxygens = geometry.find("O", VSEPR("linear 1"))
nitrogens = geometry.find("N", NumberOfBonds(3))
for carbon in carbons:
if (
any(atom in oxygens for atom in carbon.connected)
and any(atom in nitrogens for atom in carbon.connected)
):
matching_atoms.append(carbon)
return matching_atoms
class Bridgehead(Finder):
"""
bridgehead atoms
can specify ring sizes that the atoms bridge
"""
def __init__(self, ring_sizes=None, match_exact=False):
"""
:param None|list(int) ring_sizes" list of int, size of rings (e.g. [6, 6] for atoms that bridge
two 6-membered rings)
not specifying yields bridgehead atoms for any ring size
:param bool match_exact: bool, if True, return atoms only bridging the specified rings
if False, the ring_sizes is taken as a minimum (e.g.
ring_size=[6, 6], match_exact=False would also yield atoms
bridging three 6-membered rings or two six-membered rings and
a five-membered ring)
"""
self.ring_sizes = ring_sizes
self.match_exact = match_exact
def __repr__(self):
if self.ring_sizes:
return "bridgeheads of %s-member rings" % " or ".join([str(x) for x in self.ring_sizes])
return "bridgehead atoms"
def get_matching_atoms(self, atoms, geometry):
matching_atoms = []
for atom1 in atoms:
matching = True
if self.ring_sizes:
unfound_rings = list(self.ring_sizes)
n_rings = 0
for i, atom2 in enumerate(atom1.connected):
for atom3 in list(atom1.connected)[:i]:
try:
path = geometry.shortest_path(atom2, atom3, avoid=atom1)
n_rings += 1
if self.ring_sizes:
ring_size = len(path) + 1
if ring_size in unfound_rings:
unfound_rings.remove(ring_size)
elif self.match_exact:
matching = False
break
except LookupError:
pass
if not matching:
break
if self.ring_sizes and not unfound_rings and matching:
matching_atoms.append(atom1)
elif n_rings > 1 and not self.ring_sizes:
matching_atoms.append(atom1)
return matching_atoms
class SpiroCenters(Finder):
"""
atom in two different rings with no other common atoms
"""
def __init__(self, ring_sizes=None, match_exact=False):
"""
:param None|list(int) ring_sizes: list of int, size of rings (e.g. [6, 6] for atoms that bridge
two 6-membered rings)
not specifying yields bridgehead atoms for any ring size
:param bool match_exact: if True, return atoms only bridging the specified rings
if False, the ring_sizes is taken as a minimum (e.g.
ring_size=[6, 6], match_exact=False would also yield atoms
bridging three 6-membered rings or two six-membered rings and
a five-membered ring)
"""
self.ring_sizes = ring_sizes
self.match_exact = match_exact
def __repr__(self):
if self.ring_sizes:
return "atoms in different %s-member rings" % " or ".join(
[str(x) for x in self.ring_sizes]
)
return "spiro atoms"
def get_matching_atoms(self, atoms, geometry):
matching_atoms = []
for atom1 in atoms:
matching = True
if self.ring_sizes:
unfound_rings = list(self.ring_sizes)
n_rings = 0
rings = []
for i, atom2 in enumerate(atom1.connected):
for atom3 in list(atom1.connected)[:i]:
try:
path = geometry.shortest_path(atom2, atom3, avoid=atom1)
for ring in rings:
if any(atom in path for atom in ring):
continue
rings.append(path)
except LookupError:
pass
for i, ring in enumerate(rings):
bad_ring = False
for ring2 in rings[:i]:
if any(atom in ring for atom in ring2):
bad_ring = True
break
if bad_ring:
continue
n_rings += 1
if self.ring_sizes:
ring_size = len(path) + 1
if ring_size in unfound_rings:
unfound_rings.remove(ring_size)
elif self.match_exact:
matching = False
break
if not matching:
break
if self.ring_sizes and not unfound_rings and matching:
matching_atoms.append(atom1)
elif n_rings > 1 and not self.ring_sizes:
matching_atoms.append(atom1)
return matching_atoms
class Residue(Finder):
"""all atoms in a given residue"""
def __init__(self, residue):
super().__init__()