-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathHelpers.py
1361 lines (1222 loc) · 63.6 KB
/
Helpers.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
##################################################################################
# Copyright 2021-2023 Richardson Lab at Duke University
#
# Licensed under the Apache License, Version 2.0 (the "License"],
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##################################################################################
# This module exports functions that are helpful to create data structures
# needed by Probe.
from __future__ import print_function, nested_scopes, generators, division
from __future__ import absolute_import
import sys
import math
import traceback
import iotbx.map_model_manager
import iotbx.data_manager
import cctbx.maptbx.box
import mmtbx
import scitbx.matrix
from scitbx.array_family import flex
from mmtbx.probe import AtomTypes
from iotbx import pdb
from libtbx.utils import null_out, Sorry
import boost_adaptbx.boost.python as bp
bp.import_ext("mmtbx_probe_ext")
import mmtbx_probe_ext as probeExt
##################################################################################
# Helper string and functions for using Probe PHIL parameters. This lets Python
# code that want to use Probe PHIL parameters pass these to the various object
# constructors in the C++ ext library.
# PHIL parameters to be added to the master PHIL string. They make a subobject
# 'probe' and put all the parameters inside it.
probe_phil_parameters = """
probe
.style = menu_item auto_align
{
probe_radius = 0.25
.type = float
.short_caption = Probe radius
.help = Probe radius (half distance between touched atoms) (-radius in probe)
density = 16.0
.type = float
.short_caption = Dot density
.help = Probe dots per square angstrom on atom surface (-density in probe)
worse_clash_cutoff = 0.5
.type = float
.short_caption = Worse clash cutoff
.help = Cutoff for worse clashes, a positive (-divworse in probe)
clash_cutoff = 0.4
.type = float
.short_caption = Clash cutoff
.help = Cutoff for the clashes, a positive number (-divlow in probe)
contact_cutoff = 0.25
.type = float
.short_caption = Contact cutoff
.help = Cutoff for the contact (-divhigh in probe)
uncharged_hydrogen_cutoff = 0.6
.type = float
.short_caption = Uncharged hydrogen cutoff
.help = Cutoff for uncharged hydrogen overlap (-hbregular in probe)
charged_hydrogen_cutoff = 0.8
.type = float
.short_caption = Charged hydroen cutoff
.help = Cutoff for charged hydrogen overlap (-hbcharged in probe)
bump_weight = 10.0
.type = float
.short_caption = Bump weight
.help = Weight applied to bump score (-bumpweight in probe)
hydrogen_bond_weight = 4.0
.type = float
.short_caption = Hydrogen bond weight
.help = Weight applied to hydrogen bond score (-hbweight in probe)
gap_weight = 0.25
.type = float
.short_caption = Gap weight
.help = Weight applied to gap score (-gapweight in probe). Should be no smaller than probe_radius.
allow_weak_hydrogen_bonds = False
.type = bool
.short_caption = Count weak hydrogen bonds
.help = Separately account for weak hydrogen bonds (-LweakHbonds in probe)
ignore_ion_interactions = False
.type = bool
.short_caption = Ignore ion interactions
.help = Ignore interactions with ions
set_polar_hydrogen_radius = True
.type = bool
.short_caption = Override polar hydrogen radius
.help = Override the radius of polar Hydrogens with to ensure it has the expected value. (-usepolarh in probe)
}
"""
def createSpatialQuery(atoms, probePhil):
"""
Helper function to create a SpatialQuery object, passing it all required Phil parameters
in addition to the other constructor parameters.
:param atoms: Flex array of atoms to insert in the structure. This will determine the
bounds of the structure.
:param probePhi: Subobject of PHIL parameters for probe. Can be obtained using
self.params.probe from a Program Template program that includes the probe_phil_parameters
from above in its master PHIL parameters string.
:returns A mmtbx_probe_ext.SpatialQuery object.
"""
return probeExt.SpatialQuery(atoms)
def createDotSphereCache(probePhil):
"""
Helper function to create a DotSphereCache object, passing it all required Phil parameters
in addition to the other constructor parameters.
:param probePhi: Subobject of PHIL parameters for probe. Can be obtained using
self.params.probe from a Program Template program that includes the probe_phil_parameters
from above in its master PHIL parameters string.
:returns A mmtbx_probe_ext.DotSphereCache object.
"""
return probeExt.DotSphereCache(probePhil.density)
def createDotScorer(extraAtomInfo, probePhil):
"""
Helper function to create a DotScorer object, passing it all required Phil parameters
in addition to the other constructor parameters. Also enforces the constraint that the
gap_weight is at least as large as the probe_radius.
:param extraAtomInfo: Can be obtained from getExtraAtomInfo(). Holds extra atom information
needed for scoring atoms.
:param probePhil: Subobject of PHIL parameters for probe. Can be obtained using
self.params.probe from a Program Template program that includes the probe_phil_parameters
from above in its master PHIL parameters string.
:returns A mmtbx_probe_ext.DotScorer object.
"""
gap_weight = max(probePhil.gap_weight, probePhil.probe_radius)
return probeExt.DotScorer(extraAtomInfo, gap_weight,
probePhil.bump_weight, probePhil.hydrogen_bond_weight,
probePhil.uncharged_hydrogen_cutoff, probePhil.charged_hydrogen_cutoff,
probePhil.clash_cutoff, probePhil.worse_clash_cutoff,
probePhil.contact_cutoff, probePhil.allow_weak_hydrogen_bonds,
probePhil.ignore_ion_interactions)
##################################################################################
# Other helper functions.
def getBondedNeighborLists(atoms, bondProxies):
"""
Helper function to produce a dictionary of lists that contain all bonded
neighbors for each atom in a set of atoms.
:param atoms: Flex array of atoms (could be obtained using model.get_atoms() if there
are no chains with multiple conformations, must be a subset of the atoms including
all in the base conformation and in a particular conformation otherwise).
:param bondProxies: Flex array of bond proxies for the atoms. This could be obtained
using model.get_restraints_manager().geometry.get_all_bond_proxies(sites_cart =
model.get_sites_cart())[0] if the model has only a single conformation. Otherwise,
it should be a flex array of atom positions for the atoms that are in the first parameter.
It can include atoms that are not in the first parameter, but they will not be added
to the lists.
:returns a dictionary with one entry for each atom, indexed by i_seq, that contains a
list of all of the atoms (within the atoms list) that are bonded to it.
"""
atomDict = {}
for a in atoms:
atomDict[a.i_seq] = a
bondedNeighbors = {}
for a in atoms:
bondedNeighbors[a] = []
for bp in bondProxies:
try:
# These lookups will fail if the atoms are not in the list of atoms passed in.
first = atomDict[bp.i_seqs[0]]
second = atomDict[bp.i_seqs[1]]
bondedNeighbors[first].append(second)
bondedNeighbors[second].append(first)
except Exception:
# When an atom is bonded to an atom is not in our atom list (in a different conformer or not
# in our selection) we just ignore it.
pass
return bondedNeighbors
def addIonicBonds(bondedNeighborLists, atoms, spatialQuery, extraAtomInfo):
"""
Helper function to add ionic bonds to the list of bonded neighbors.
@todo: Be sure never to make an ionic bond to a water (fix in old and new probe).
:param bondedNeighborLists: Object to have the ionic bonds added to in place.
:param atoms: Flex array of atoms (could be obtained using model.get_atoms() if there
are no chains with multiple conformations, must be a subset of the atoms including
:param spatialQuery: mmtbx_probe_ext.SpatialQuery structure to rapidly determine which atoms
are within a specified distance of a location.
:param extraAtomInfo: mmtbx_probe_ext.ExtraAtomInfo mapper that provides radius and other
information about atoms beyond what is in the pdb.hierarchy. Used here to determine
which atoms may be acceptors.
"""
for a in atoms:
if a.element_is_positive_ion():
myRad = extraAtomInfo.getMappingFor(a).vdwRadius
minDist = myRad
maxDist = 0.25 + myRad + 3 # overestimate so we don't miss any
neighbors = spatialQuery.neighbors(a.xyz, minDist, maxDist)
for n in neighbors:
# Never add bonds with Phantom Hydrogens.
if extraAtomInfo.getMappingFor(n).isDummyHydrogen:
continue
# See if we're within range for an ionic bond between the two atoms.
dist = (rvec3(a.xyz) - rvec3(n.xyz)).length()
expected = myRad + extraAtomInfo.getMappingFor(n).vdwRadius
if dist >= (expected - 0.6) and dist <= (expected + 0.2):
# We're in range; bond each of us to the other.
try:
bondedNeighborLists[a].append(n)
except Exception:
bondedNeighborLists[a] = [n]
try:
bondedNeighborLists[n].append(a)
except Exception:
bondedNeighborLists[n] = [a]
def compatibleConformations(a1, a2):
'''
Returns True if the two atoms are in compatible conformations, False if not.
:param a1: First atom.
:param a2: Second atom.
:return: True if either atom is in the empty conformation or if both are in the
same conformation.
'''
alt1 = a1.parent().altloc
alt2 = a2.parent().altloc
if alt1 in ['', ' ']:
return True
if alt2 in ['', ' ']:
return True
return alt1 == alt2
def getAtomsWithinNBonds(atom, bondedNeighborLists, extraAtomInfo, probeRad, N, nonHydrogenN = 1e10):
"""
Helper function to produce a list of all of the atoms that are bonded to the
specified atom, or to one of the atoms bonded to the specified atom, recursively
to a depth of N. The atom itself will not be included in the list, so an atom that
has no bonded neighbors will have an empty result. This can be used to
produce a list of excluded atoms for dot scoring. It checks to ensure that all of
the bonded atoms are from compatible conformations (if the original atom
is in the empty configuration then this will return atoms from all conformations that
are in the bonded set).
For Phantom Hydrogens only ever check to a depth of one (their parent Oxygen atom)
to avoid spurious bonds found through ions.
:param atom: The atom to be tested.
:param bondedNeighborLists: Dictionary of lists that contain all bonded neighbors for
each atom in a set of atoms. Should be obtained using getBondedNeighborLists() and
perhaps augmented by calling addIonicBonds().
:param extraAtomInfo: mmtbx_probe_ext.ExtraAtomInfo mapper that provides radius and other
information about atoms beyond what is in the pdb.hierarchy. Used here to determine
radii of the potential bonded neighbors. No atom that is further than twice the probe
radius from the source atom is part of the chain of neighbors.
:param probeRad: Probe radius. No atom that is further than twice the probe
radius from the source atom is part of the chain of neighbors.
:param N: Depth of recursion. N=1 will return the atoms bonded to atom. N=2 will
also return those bonded to these neighbors (but not the atom itself).
:param nonHydrogenN: When neither the original atom nor the bonded atom is a Hydrogen,
limit the depth to this value (if this value is less than N).
:returns a list of all atoms that are bonded to atom within a depth of N. The original
atom is never on the list.
"""
aLoc = atom.xyz
aRad = extraAtomInfo.getMappingFor(atom).vdwRadius
atomIsHydrogen = atom.element_is_hydrogen()
if extraAtomInfo.getMappingFor(atom).isDummyHydrogen:
# Only ever allow a Phantom Hydrogen to be bonded to its parent Oxygen
N = 1
# Find all atoms to the specified depth
atoms = {atom} # Initialize the set with the atom itself
for i in range(N): # Repeat the recursion this many times
current = list(atoms) # Make a copy so we're not modifying the list we are traversing
for a in current: # Add all neighbors of all atoms in the current level
for n in bondedNeighborLists[a]:
# If we find a hydrogen, we no longer use the non-Hydrogen N limit.
if i < nonHydrogenN or atomIsHydrogen or n.element_is_hydrogen():
# Ensure that the new atom is in a compatible conformation with the original atom.
if compatibleConformations(atom, n):
# Ensure that the atom is not too far away from the original atom.
nLoc = n.xyz
nRad = extraAtomInfo.getMappingFor(n).vdwRadius
d = (rvec3(nLoc) - rvec3(aLoc)).length()
if d <= aRad + nRad + 2 * probeRad:
atoms.add(n)
# Remove the original atom from the result and turn the result into a list.
atoms.discard(atom)
return list(atoms)
def isPolarHydrogen(atom, bondedNeighborLists):
'''
Returns True if the atom is a polar hydrogen, False if not.
:param atom: The atom to be tested.
:param bondedNeighborLists: Dictionary of lists that contain all bonded neighbors for
each atom in a set of atoms. Should be obtained using
mmtbx.probe.Helpers.getBondedNeighborLists() and perhaps augmented by calling addIonicBonds().
:return: True if the atom is a polar hydrogen.
'''
if atom.element_is_hydrogen():
neighbors = bondedNeighborLists[atom]
if len(neighbors) == 1 and neighbors[0].element in ['N', 'O', 'S']:
return True
return False
def getMaxISeq(model):
"""Return the maximum i_seq value for all atoms in a model.
"""
maxISeq = 0
for a in model.get_atoms():
maxISeq = max(maxISeq, a.i_seq)
return maxISeq
class getExtraAtomInfoReturn(object):
"""
Return type from getExtraAtomInfo() call.
extraAtomInfo: ExtraAtomInfoMap with an entry for every atom in the model suitable for
passing to the scoring functions.
warnings: a string that if not empty lists warnings that the person running the program
might want to know about. Suitable for printing or logging.
"""
def __init__(self, extraAtomInfo, warnings):
self.extraAtomInfo = extraAtomInfo
self.warnings = warnings
def getExtraAtomInfo(model, bondedNeighborLists, useNeutronDistances = False, probePhil = None):
"""
Helper function to provide a mapper for ExtraAtomInfo needed by Probe when scoring
models.
:param model: Map Model Manager's Model containing all of the atoms to be described.
PDB interpretation must have been done on the model, perhaps by calling
model.process(make_restraints=True), with useNeutronDistances matching
the parameter to this function.
:param bondedNeighborLists: Lists of atoms that are bonded to each other.
Can be obtained by calling getBondedNeighborLists() and
perhaps augmented by calling addIonicBonds().
:param useNeutronDistances: Default is to use x-ray distances, but setting this to
True uses neutron distances instead. This must be set consistently with the
PDB interpretation parameter used on the model.
:param probePhil: None or subobject of PHIL parameters for probe. Can be obtained using
self.params.probe from a Program Template program that includes the probe_phil_parameters
from above in its master PHIL parameters string. If None, local defaults will be used.
The following are used:
set_polar_hydrogen_radius(bool): Default is to override the radius for polar
Hydrogen atoms with 1.05. Setting this to false uses the CCTBX value.
:returns a ExtraAtomInfoMap with an entry for every atom in the model suitable for
passing to the scoring functions.
"""
warnings = ""
# Traverse the hierarchy and look up the extra data to be filled in.
extras = probeExt.ExtraAtomInfoMap([],[])
mon_lib_srv = model.get_mon_lib_srv()
ener_lib = mmtbx.monomer_library.server.ener_lib()
ph = model.get_hierarchy()
for m in ph.models():
for chain in m.chains():
for rg in chain.residue_groups():
for ag in rg.atom_groups():
for a in ag.atoms():
extra = probeExt.ExtraAtomInfo()
try:
hb_type = model.get_specific_h_bond_type(a.i_seq)
if hb_type in ['A', 'B', 'D', 'N', 'H']: #isinstance(hb_type, str):
if hb_type == "A" or hb_type == "B":
extra.isAcceptor = True
if hb_type == "D" or hb_type == "B":
extra.isDonor = True
# Get the first non-blank character of the alternate for this atom.
# If there is none, set the value to the empty string. If there is, set
# it to a string with just that character.
alt = a.parent().altloc.strip()
extra.altLoc = alt
extra.charge = probeExt.atom_charge(a)
# For ions, the Richardsons determined in discussion with
# Michael Prisant that we want to use the ionic radius rather than the
# larger radius for all purposes.
# @todo Once the CCTBX radius determination discussion and upgrade is
# complete (ongoing as of March 2022), this check might be removed
# and we'll just use the CCTBX radius.
extra.isIon = a.element_is_ion()
if extra.isIon:
extra.vdwRadius = model.get_specific_ion_radius(a.i_seq)
warnings += ("Using ionic radius for "+a.name.strip()+": "+str(extra.vdwRadius)+
" (rather than "+
str(model.get_specific_vdw_radius(a.i_seq, False))+
")\n")
else:
extra.vdwRadius = model.get_specific_vdw_radius(a.i_seq, False)
# Mark aromatic ring N and C atoms as acceptors as a hack to enable the
# ring itself to behave as an acceptor.
# @todo Remove this once we have a better way to model the ring itself
# as an acceptor, perhaps making it a cylinder or a sphere in the center
# of the ring.
if a.element in ['C','N']:
if AtomTypes.IsAromaticAcceptor(ag.resname, a.name):
extra.isAcceptor = True
warnings += "Marking "+a.name.strip()+" as an aromatic-ring acceptor\n"
# Mark Nitrogens that do not have an attached Hydrogen as acceptors.
# We only do this in HET atoms because CCTBX routines seem to be working
# properly in standard residues.
# We determine whether it is in a het atom by seeing if the residue name
# is in the amino-acid mapping structure.
if not a.parent().resname in iotbx.pdb.amino_acid_codes.one_letter_given_three_letter:
if a.element == 'N':
# See if the atom has no Hydrogens covalently bonded to it.
found = False
for n in bondedNeighborLists[a]:
if n.element_is_hydrogen():
found = True
break
if not found and not extra.isAcceptor:
extra.isAcceptor = True
warnings += "Marking "+a.parent().resname.strip()+" "+a.name.strip()+" as a non-Hydrogen HET acceptor\n"
# Mark all Carbonyl's with the Probe radius while the Richardsons and
# the CCTBX decide how to handle this.
# @todo After 2021, see if the CCTBX has the same values (1.65 and 1.80)
# for Carbonyls and remove this if so. It needs to stay with these values
# to avoid spurious collisions per experiments run by the Richardsons in
# September 2021.
if (a.name.strip().upper() == 'C'
or AtomTypes.IsSpecialAminoAcidCarbonyl(a.parent().resname.strip().upper(),
a.name.upper()) ):
expected = 1.65
if extra.vdwRadius != expected:
warnings += ("Overriding radius for "+a.name.strip()+": "+str(expected)+
" (was "+str(extra.vdwRadius)+")\n")
extra.vdwRadius = expected
# If we've been asked to ensure polar hydrogen radius, do so here.
if probePhil.set_polar_hydrogen_radius and isPolarHydrogen(a, bondedNeighborLists):
if extra.vdwRadius != 1.05:
warnings += ("Overriding radius for "+a.name.strip()+": 1.05 (was "+
str(extra.vdwRadius)+")\n")
extra.vdwRadius = 1.05
extras.setMappingFor(a, extra)
# Did not find hydrogen-bond information for this atom.
else:
fullName = (chain.id + ' ' + a.parent().resname.strip() + ' ' +
str(a.parent().parent().resseq_as_int()) + ' ' + a.name.strip())
raise Sorry("Could not find atom info for "+fullName+
"(perhaps interpretation was not run on the model?)\n")
except Exception as e:
fullName = (chain.id + ' ' + a.parent().resname.strip() + ' ' +
str(a.parent().parent().resseq_as_int()) + ' ' + a.name.strip())
raise Sorry("Could not find atom info for "+fullName+
" (perhaps interpretation was not run on the model?)\n")
return getExtraAtomInfoReturn(extras, warnings)
def writeAtomInfoToString(atoms, extraAtomInfo):
"""
Write information about atoms, including their radius and donor/acceptor status,
into a string that matches the form used by the new options added to the original
Probe and Reduce code to enable comparisons between versions.
:param atoms: The atoms to be described.
:param extraAtomInfo: mmtbx_probe_ext.ExtraAtomInfo mapper that provides radius and other
information about atoms beyond what is in the pdb.hierarchy. Used here to determine
which atoms may be acceptors.
:return: String suitable for writing to a file to enable comparison with the dump
files from the original Probe or Reduce programs.
"""
#################################################################################
# Dump information about all of the atoms in the model into a string.
ret = ""
acceptorChoices = ["noAcceptor","isAcceptor"]
donorChoices = ["noDonor","isDonor"]
metallicChoices = ["noMetallic","isMetallic"]
for a in atoms:
chainID = a.parent().parent().parent().id
resName = a.parent().resname.upper()
resID = str(a.parent().parent().resseq_as_int())
alt = a.parent().altloc
if alt == " " or alt == "":
alt = "-"
ret += (
" "+str(chainID)+" "+resName+" {:3d} ".format(int(resID))+a.name+" "+alt+
" {:7.3f}".format(a.xyz[0])+" {:7.3f}".format(a.xyz[1])+" {:7.3f}".format(a.xyz[2])+
" {:5.2f}".format(extraAtomInfo.getMappingFor(a).vdwRadius)+
" "+acceptorChoices[extraAtomInfo.getMappingFor(a).isAcceptor]+
" "+donorChoices[extraAtomInfo.getMappingFor(a).isDonor]+
" "+metallicChoices[a.element_is_positive_ion()]+
"\n"
)
return ret
def compareAtomInfoFiles(fileName1, fileName2, distanceThreshold):
"""
Write information about atom differences in two files into a string that describes
their differences. This is used to perform a difference on files that contain the
output of writeAtomInfoToString() run on different versions of Probe or Reduce.
The lines in the files are sorted so that reordering of the atoms between files will
not cause this function to report differences.
:param fileName1: The first file.
:param fileName2: The second file.
:param distanceThreshold: If the same atom in each file is further apart than this
between files, report it as a difference; otherwise, not.
:return: String suitable for writing to a file to describe the differences.
"""
###########################
# Helper utility functions usable only inside this function
def atomID(s):
# Return the ID of the atom, which includes its chain, residue name,
# residue number, atom name, and alternate separated by spaces
w = s.split()
return w[0]+" "+w[1]+" "+w[2]+" "+w[3]+" "+w[4]
def position(s):
# Return a tuple that has the location of the atom.
w = s.split()
return ( float(w[5]), float(w[6]), float(w[7]) )
def radius(s):
# Return the radius for the atom.
w = s.split()
return ( w[8] )
def flag(s,f):
# Return a flag for the atom, indexed by 0-2.
w = s.split()
return ( w[9+f] )
def distance(a, b):
# Return the distance between 3-space tuples a and b
return math.sqrt( (a[0]-b[0])**2 + (a[1]-b[1])**2 + (a[2]-b[2])**2 )
###########################
# Initialize our return value to empty.
ret = ""
# Read the the data from each file.
# Sort the lines from the file so that we'll get chain, residue, atom name sorting.
with open(fileName1) as f:
m1 = f.readlines()
m1.sort(key=lambda x:atomID(x))
with open(fileName2) as f:
m2 = f.readlines()
m2.sort(key=lambda x:atomID(x))
# Compare the two and report on any differences.
# Both lists are sorted, so we'll know which has a missing element compared
# to the other. We walk from the beginning to the end of each list.
m1ID = 0
m2ID = 0
while m1ID < len(m1) and m2ID < len(m2):
# Atom names are unique within residues, so we can use that to
# match atoms in one file from those in the other and print out
# missing atoms and differences in positions.
if atomID(m1[m1ID]) < atomID(m2[m2ID]):
ret += 'Only in first: '+atomID(m1[m1ID])+'\n'
m1ID += 1
continue
if atomID(m1[m1ID]) > atomID(m2[m2ID]):
ret += 'Only in second: '+atomID(m2[m2ID])+'\n'
m2ID += 1
continue
# Check the radius to see if they differ
oldRadius = radius(m1[m1ID])
newRadius = radius(m2[m2ID])
if oldRadius != newRadius:
ret += atomID(m1[m1ID])+' radii differ: old is {}, new is {}'.format(oldRadius,newRadius)+'\n'
# Check the flag fields to see if any of them differ
for f in range(3):
oldFlag = flag(m1[m1ID], f)
newFlag = flag(m2[m2ID], f)
if oldFlag != newFlag:
ret += atomID(m1[m1ID])+' flags differ: old is {}, new is {}'.format(oldFlag,newFlag)+'\n'
# Check the distance between the atoms.
dist = distance(position(m1[m1ID]),position(m2[m2ID]))
if dist > distanceThreshold:
ret += atomID(m1[m1ID])+' Distance between runs: {:.2f}'.format(dist)+'\n'
# Go on to next line
m1ID += 1
m2ID += 1
# Finish up either list that is not done
while m1ID < len(m1):
ret += 'Only in first: '+atomID(m1[m1ID])+'\n'
m1ID += 1
while m2ID < len(m2):
ret += 'Only in second: '+atomID(m2[m2ID])+'\n'
m2ID += 1
return ret
def getPhantomHydrogensFor(largestISeq, atom, spatialQuery, extraAtomInfo, minOccupancy,
acceptorOnly = False, placedHydrogenRadius = 1.05, placedHydrogenDistance = 0.84):
"""
Get a list of phantom Hydrogens for the atom specified, which is asserted to be an Oxygen
atom for a water.
:param largestISeq: The largest i_seq number in the model so far. Each new Phantom will be
given a different number, starting with one larger than this. This should be incremented
by the length of the returned list if this function is called multiple times.
:param atom: The Oxygen that is to have phantoms added to it.
:param spatialQuery: mmtbx_probe_ext.SpatialQuery structure to rapidly determine which atoms
are within a specified distance of a location.
:param extraAtomInfo: mmtbx_probe_ext.ExtraAtomInfo mapper that provides radius and other
information about atoms beyond what is in the pdb.hierarchy. Used here to determine
which atoms may be acceptors.
:param minOccupancy: Minimum occupancy for a nearby atom to be considered.
:param acceptorOnly: Only allow bonds with atoms that are acceptors when this is True.
This is false by default because Reduce needs to check whether the bonded atom is either
an acceptor or a possible flipped position of an acceptor, and that is not something that
can be determined at the time we're placing phantom hydrogens. In that case, we want to
include all possible interactions and weed them out during optimization.
:param placedHydrogenRadius: Radius of the Phantom Hydrogen to be placed. Default is
for electron-cloud distances.
:param placedHydrogenDistance: Maximum distance from the placed Phantom Hydrogen to the
Water Oxygen. The Phantom Hydrogens are placed at the optimal overlap distance so may be
closer than this. Default is for electron-cloud distances.
:return: List of new atoms that make up the phantom Hydrogens, with only their name and
element type and xyz positions filled in. They will have i_seq as specified and they
should not be inserted into a structure.
"""
ret = []
# Get the list of nearby atoms. The center of the search is the water atom
# and the search radius is 4 (these values are pulled from the Reduce C++ code).
# Sort these by i_seq so that we get repeatable results from run to run.
maxDist = 4.0
nearby = sorted(spatialQuery.neighbors(atom.xyz, 0.001, maxDist), key=lambda x:x.i_seq)
# Candidates for nearby atoms. We use this list to keep track of one ones we
# have already found so that we can compare against them to only get one for each
# aromatic ring.
class Candidate(object):
def __init__(self, atom, overlap):
self._atom = atom
self._overlap = overlap
candidates = []
atomModel = atom.parent().parent().parent().parent().id
for a in nearby:
aModel = a.parent().parent().parent().parent().id
# Only check atoms in compatible conformations.
if not compatibleConformations(atom, a):
continue
# Skip atoms that are in different models.
if atomModel != aModel:
continue
# Check to ensure the occupancy of the neighbor is above threshold and that it is
# close enough to potentially bond to the atom. We want a minimum overlap of 0.1
# before we consider possible Hs.
overlap = ( (rvec3(atom.xyz) - rvec3(a.xyz)).length() -
(placedHydrogenRadius + extraAtomInfo.getMappingFor(a).vdwRadius + placedHydrogenDistance) )
if overlap <= -0.1 and a.occ >= minOccupancy and a.element != "H":
if not acceptorOnly or extraAtomInfo.getMappingFor(a).isAcceptor:
# If we have multiple atoms in the same Aromatic ring (part of the same residue)
# we only point at the closest one. To ensure this, we check all current candidates
# and if we find one that is on the same aromatic ring then we either ignore this new
# atom (if it is further) or replace the existing one (if it is closer).
skip = False
if AtomTypes.IsAromaticAcceptor(a.parent().resname.strip().upper(), a.name.strip().upper()):
for c in candidates:
# See if we belong to the same atom group and are both ring acceptors. If so, we need to replace
# or else squash this atom.
if (AtomTypes.IsAromaticAcceptor(c._atom.parent().resname.strip().upper(), c._atom.name.strip().upper()) and
a.parent() == c._atom.parent()):
if overlap < c._overlap:
# Replace the further atom with this atom.
c._atom = a
c._overlap = overlap
skip = True
break
else:
# This is further away, so we don't insert it.
skip = True
break
# Add the Candidate
if not skip:
candidates.append(Candidate(a, overlap))
# Generate phantoms pointing toward all of the remaining candidates.
# Make most of their characteristics copied from the source Oxygen.
# The element, name, and location are modified.
for c in candidates:
h = pdb.hierarchy.atom(atom.parent(), atom)
h.element = "H"
h.name = " H?"
# Place the Phantom Hydrogen pointing from the Oxygen towards the candidate at a distance
# of the standard H bond length plus an offset that is clamped to the range -H bond length..0 that
# is the sum of the overlap and the best hydrogen-bonding overlap. This is an approximation
# to the situation where the Phantom Hydrogen would rotate around the Oxygen to maintain a proper
# distance from the acceptor that does not involve trying to select a rotation direction.
# Because Phantom Hydrogens do not block the Oxygen from collisions with their neighbors, and
# because Phantom Hydrogens cannot clash with any atom, this will not interfere with clashes.
# Note that the Phantom Hydrogen also will not block any collision between the Oxygen atom
# in the Water and a nearby acceptor, so those collisions will still show up.
# The bond length will never be lengthened, but might be shortened if there is more than
# the ideal amount of overlap, and it will never be shorted to less than 0 (the location
# of the Oxygen).
BEST_HBOND_OVERLAP=0.6
distance = placedHydrogenDistance + max(
-placedHydrogenDistance, min(0.0, c._overlap + BEST_HBOND_OVERLAP) )
try:
normOffset = (rvec3(c._atom.xyz) - rvec3(atom.xyz)).normalize()
h.xyz = rvec3(atom.xyz) + distance * normOffset
largestISeq += 1
probeExt.set_atom_i_seq(h, largestISeq)
ret.append(h)
except Exception:
# If we have overlapping atoms (normalize() fails), don't add.
pass
return ret
def fixupExplicitDonors(atoms, bondedNeighborLists, extraAtomInfo):
"""
Fix up the donor status for models that have explicit hydrogens. All Nitrogens, Oxygens
and Sulphur atoms are stripped of their donor status because they will have explicit Hydrogens
added or else had some other form of covalent bond added. All hydrogens that are bonded to
Nitrogens, Oxygens, or Sulphur atoms are marked as donors. This does not handle any Phantom
Hydrogens unless those Hydrogens are marked as bonded to their Water Oxygen.
:param atoms: The list of atoms to adjust.
:param bondedNeighborLists: Dictionary of lists that contain all bonded neighbors for
each atom in a set of atoms. Should be obtained using
mmtbx.probe.Helpers.getBondedNeighborLists() and perhaps augmented by calling addIonicBonds().
:param extraAtomInfo: mmtbx_probe_ext.ExtraAtomInfo mapper that provides radius and other
information about atoms beyond what is in the pdb.hierarchy. Used here to determine
which atoms may be acceptors. This information is modified in place to adjust the donor
status of atoms.
:return: None. As a side effect, the extraAtomInfo is adjusted.
"""
for a in atoms:
# If we are a hydrogen that is bonded to a nitrogen, oxygen, or sulfur then we're a donor
# and our bonded neighbor is not.
if a.element_is_hydrogen():
for n in bondedNeighborLists[a]:
if n.element in ['N','O','S']:
# Copy the value, set the new values, then copy the new one back in.
# We are a donor and may have our radius adjusted
ei = extraAtomInfo.getMappingFor(a)
ei.isDonor = True
extraAtomInfo.setMappingFor(a, ei)
# Set our neigbor to not be a donor, since we are the donor
ei = extraAtomInfo.getMappingFor(n)
ei.isDonor = False
extraAtomInfo.setMappingFor(n, ei)
# Otherwise, if we're an N, O, or S then remove our donor status because
# the hydrogens will be the donors. Because we're not doing implicit
# hydrogens (and thus are doing explicit hydrogens), if we have a leftover
# atom that did not have a hydrogen attached we assume that this is because
# there is some other bonding and we still need to remove the donor status.
elif a.element in ['N','O','S']:
ei = extraAtomInfo.getMappingFor(a)
ei.isDonor = False
extraAtomInfo.setMappingFor(a, ei)
def dihedralChoicesForRotatableHydrogens(hydrogens, hParams, potentials):
"""
Determine which of the hydrogens passed in is the one that should be used to
define the conventional dihedral for a rotatable hydrogen bond, and which of
the potential third-link neighbors should be used as the other atom for the
calculation.
:param hydrogens: List of atoms in a set of rotatable hydrogens.
:param hParams: List indexed by sequence ID that stores the riding
coefficients for hydrogens that have associated dihedral angles. This can be
obtained by calling model.setup_riding_h_manager() and then model.get_riding_h_manager().
:param potentials: List of atoms; potential third-bonded neighbor atoms to the hydrogens.
It can be obtained by walking the covalent bonds in the structure from any of
the hydrogens.
:returns: A tuple whose first entry is the hydrogen to use, whose second entry
is the atom to use to compute the dihedral.
"""
conventionalH = None
conventionalFriend = None
for h in hydrogens:
# Find the Hydrogen whose "n" entry is 0 and fill in it and the associated
# potential atom.
try:
item = hParams[h.i_seq]
if item.n == 0:
whichPotential = item.a2
for p in potentials:
if p.i_seq == whichPotential:
conventionalH = h
conventionalFriend = p
except Exception as e:
pass
# Throw a Sorry if we were unable to find an answer.
if conventionalH is None or conventionalFriend is None:
raise Sorry("mmtbx.probe.Helpers.dihedralChoicesForRotatableHydrogens(): Could not determine atoms to use")
return conventionalH, conventionalFriend
##################################################################################
# Helper functions to make things that are compatible with vec3_double so
# that we can do math on them. We need a left-hand and right-hand one so that
# we can make both versions for multiplication.
def rvec3(xyz) :
return scitbx.matrix.rec(xyz, (3,1))
def lvec3(xyz) :
return scitbx.matrix.rec(xyz, (1,3))
def Test(inFileName = None):
"""
Run tests on all of our functions. Throw an assertion failure if one fails.
"""
#========================================================================
# Model file snippets used in the tests.
pdb_1xso_his_61 = (
"""
ATOM 442 N HIS A 61 26.965 32.911 7.593 1.00 7.19 N
ATOM 443 CA HIS A 61 27.557 32.385 6.403 1.00 7.24 C
ATOM 444 C HIS A 61 28.929 31.763 6.641 1.00 7.38 C
ATOM 445 O HIS A 61 29.744 32.217 7.397 1.00 9.97 O
ATOM 446 CB HIS A 61 27.707 33.547 5.385 1.00 9.38 C
ATOM 447 CG HIS A 61 26.382 33.956 4.808 1.00 8.78 C
ATOM 448 ND1AHIS A 61 26.168 34.981 3.980 1.00 9.06 N
ATOM 449 CD2 HIS A 61 25.174 33.397 5.004 1.00 11.08 C
ATOM 450 CE1 HIS A 61 24.867 35.060 3.688 1.00 12.84 C
ATOM 451 NE2 HIS A 61 24.251 34.003 4.297 1.00 11.66 N
HETATM 4333 CU CU A 1 22.291 33.388 3.996 1.00 13.22 Cu
END
"""
)
pdb_4fenH_C_26 = (
"""
ATOM 234 P C B 26 25.712 13.817 1.373 1.00 10.97 P
ATOM 235 OP1 C B 26 26.979 13.127 1.023 1.00 12.57 O
ATOM 236 OP2 C B 26 25.641 14.651 2.599 1.00 12.59 O
ATOM 237 O5' C B 26 25.264 14.720 0.142 1.00 10.58 O
ATOM 238 C5' C B 26 25.128 14.166 -1.162 1.00 9.37 C
ATOM 239 C4' C B 26 24.514 15.183 -2.092 1.00 9.05 C
ATOM 240 O4' C B 26 23.142 15.447 -1.681 1.00 10.46 O
ATOM 241 C3' C B 26 25.159 16.555 -2.048 1.00 8.49 C
ATOM 242 O3' C B 26 26.338 16.599 -2.839 1.00 7.94 O
ATOM 243 C2' C B 26 24.043 17.427 -2.601 1.00 8.44 C
ATOM 244 O2' C B 26 23.885 17.287 -3.999 1.00 9.33 O
ATOM 245 C1' C B 26 22.835 16.819 -1.888 1.00 10.06 C
ATOM 246 N1 C B 26 22.577 17.448 -0.580 1.00 9.44 N
ATOM 247 C2 C B 26 21.940 18.695 -0.552 1.00 9.61 C
ATOM 248 O2 C B 26 21.613 19.223 -1.623 1.00 10.13 O
ATOM 249 N3 C B 26 21.704 19.292 0.638 1.00 9.82 N
ATOM 250 C4 C B 26 22.082 18.695 1.771 1.00 10.10 C
ATOM 251 N4 C B 26 21.841 19.328 2.923 1.00 10.94 N
ATOM 252 C5 C B 26 22.728 17.423 1.773 1.00 9.51 C
ATOM 253 C6 C B 26 22.952 16.840 0.586 1.00 10.02 C
ATOM 0 H5' C B 26 24.573 13.371 -1.128 1.00 9.37 H new
ATOM 0 H5'' C B 26 25.996 13.892 -1.498 1.00 9.37 H new
ATOM 0 H4' C B 26 24.618 14.792 -2.973 1.00 9.05 H new
ATOM 0 H3' C B 26 25.463 16.833 -1.170 1.00 8.49 H new
ATOM 0 H2' C B 26 24.192 18.375 -2.460 1.00 8.44 H new
ATOM 0 HO2' C B 26 23.422 16.605 -4.162 1.00 9.33 H new
ATOM 0 H1' C B 26 22.041 16.954 -2.429 1.00 10.06 H new
ATOM 0 H41 C B 26 22.073 18.968 3.668 1.00 10.94 H new
ATOM 0 H42 C B 26 21.454 20.096 2.919 1.00 10.94 H new
ATOM 0 H5 C B 26 22.984 17.013 2.568 1.00 9.51 H new
ATOM 0 H6 C B 26 23.369 16.009 0.556 1.00 10.02 H new
"""
)
class philLike:
def __init__(self):
self.set_polar_hydrogen_radius = True
#========================================================================
# Run unit test on getExtraAtomInfo(). We use a specific PDB snippet
# for which we know the answer and then we verify that the results are what
# we expect.
# Spot check the values on the atoms for standard, neutron distances,
# and original Probe results.
standardChecks = [
# Name, vdwRadius, isAcceptor, isDonor, isDummyHydrogen, isIon, charge, altLoc
["CU", 0.72, False, False, False, True, 0, ''],
["N", 1.55, False, True, False, False, 0, ''],
["ND1", 1.55, False, True, False, False, 0, 'A'],
["C", 1.65, False, False, False, False, 0, ''],
["CB", 1.7, False, False, False, False, 0, ''],
["O", 1.4, True, False, False, False, 0, ''],
["CD2", 1.75, False, False, False, False, 0, '']
]
neutronChecks = [
# Name, vdwRadius, isAcceptor, isDummyHydrogen, isDonor, isIon, charge, altLoc
["CU", 0.72, False, False, False, True, 0, ''],
["N", 1.55, False, True, False, False, 0, ''],
["ND1", 1.55, False, True, False, False, 0, 'A'],
["C", 1.65, False, False, False, False, 0, ''],
["CB", 1.7, False, False, False, False, 0, ''],
["O", 1.4, True, False, False, False, 0, ''],
["CD2", 1.75, False, False, False, False, 0, '']
]
# Situations to run the test in and expected results:
cases = [
# Use neutron distances, expected results
[False, standardChecks],
[True, neutronChecks]
]
for cs in cases:
useNeutronDistances = cs[0]
checks = cs[1]
runType = "; neutron = "+str(useNeutronDistances)
dm = iotbx.data_manager.DataManager(['model'])
dm.process_model_str("1xso_snip.pdb",pdb_1xso_his_61)
model = dm.get_model()
p = mmtbx.model.manager.get_default_pdb_interpretation_params()
p.pdb_interpretation.use_neutron_distances = useNeutronDistances
model.process(make_restraints=True, pdb_interpretation_params = p)
# Get the atoms for the first model in the hierarchy.
atoms = model.get_hierarchy().models()[0].atoms()
# Get the Cartesian positions of all of the atoms we're considering for this alternate
# conformation.
carts = flex.vec3_double()
for a in atoms:
carts.append(a.xyz)
# Get the bond proxies for the atoms in the model and conformation we're using and
# use them to determine the bonded neighbor lists.
bondProxies = model.get_restraints_manager().geometry.get_all_bond_proxies(sites_cart = carts)[0]
bondedNeighborLists = getBondedNeighborLists(atoms, bondProxies)
# Get the extra atom information for the model using default parameters.
# Make a PHIL-like structure to hold the parameters.
extras = getExtraAtomInfo(model,bondedNeighborLists,
useNeutronDistances=useNeutronDistances,probePhil=philLike()).extraAtomInfo
# Get the atoms for the first model in the hierarchy.
atoms = model.get_hierarchy().models()[0].atoms()
for a in atoms:
e = extras.getMappingFor(a)
for c in checks:
if a.name.strip() == c[0]:
if hasattr(math, 'isclose'):
assert math.isclose(e.vdwRadius, c[1]), ("Helpers.Test(): Bad radius for "+a.name+runType+": "
+str(e.vdwRadius)+" (expected "+str(c[1])+")")
assert e.isAcceptor == c[2], "Helpers.Test(): Bad Acceptor status for "+a.name+": "+str(e.isAcceptor)+runType
assert e.isDonor == c[3], "Helpers.Test(): Bad Donor status for "+a.name+": "+str(e.isDonor)+runType
# Check the ability to set and check Dummy/Phantom Hydrogen status
assert e.isDummyHydrogen == False, "Helpers.Test(): Bad Dummy Hydrogen status for "+a.name+runType
e.isDummyHydrogen = True
assert e.isDummyHydrogen == True, "Helpers.Test(): Can't set DummyHydrogen status for "+a.name+runType
assert e.isIon == c[5], "Helpers.Test(): Bad Ion status for "+a.name+": "+str(e.isIon)+runType
assert e.charge == c[6], "Helpers.Test(): Bad charge for "+a.name+": "+str(e.charge)+runType
assert e.altLoc == c[7], "Helpers.Test(): Bad altLoc for "+a.name+": "+str(e.altLoc)+", wanted "+str(c[7])+runType
#========================================================================
# Run unit test on getPhantomHydrogensFor().
# Generate a Water Oxygen atom at the origin with a set of atoms around it.
# The surrounding ones will include atoms that are acceptors, atoms that are not
# and atoms that are on an aromatic ring (all on the same ring). The distance to
# each atom will be such that it is within range for a Phantom Hydrogen. The
# directions will be towards integer lattice points and we'll round-robin the type.
# NOTE: The atom names and types and radii and such are not correct, just made up