-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy path_merge.py
More file actions
1523 lines (1253 loc) · 57.7 KB
/
_merge.py
File metadata and controls
1523 lines (1253 loc) · 57.7 KB
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
######################################################################
# BioSimSpace: Making biomolecular simulation a breeze!
#
# Copyright: 2017-2025
#
# Authors: Lester Hedges <lester.hedges@gmail.com>
#
# BioSimSpace is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# BioSimSpace is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with BioSimSpace. If not, see <http://www.gnu.org/licenses/>.
#####################################################################
"""Functionality for merging molecules."""
__author__ = "Lester Hedges"
__email__ = "lester.hedges@gmail.com"
__all__ = ["merge"]
def merge(
molecule0,
molecule1,
mapping,
allow_ring_breaking=False,
allow_ring_size_change=False,
force=False,
roi=None,
property_map0={},
property_map1={},
):
"""
Merge this molecule with 'other'.
Parameters
----------
molecule0 : BioSimSpace._SireWrappers.Molecule
The reference molecule.
molecule1 : BioSimSpace._SireWrappers.Molecule
The molecule to merge with.
mapping : dict
The mapping between matching atom indices in the two molecules.
allow_ring_breaking : bool
Whether to allow the opening/closing of rings during a merge.
allow_ring_size_change : bool
Whether to allow changes in ring size.
force : bool
Whether to try to force the merge, even when the molecular
connectivity changes not as the result of a ring transformation.
This will likely lead to an unstable perturbation. This option
takes precedence over 'allow_ring_breaking' and
'allow_ring_size_change'.
roi : list
The region of interest to merge. Consist of two lists of atom indices.
property_map0 : dict
A dictionary that maps "properties" in this molecule to their
user defined values. This allows the user to refer to properties
with their own naming scheme, e.g. { "charge" : "my-charge" }
property_map1 : dict
A dictionary that maps "properties" in there other molecule to
their user defined values.
Returns
-------
merged : Sire.Mol.Molecule
The merged molecule.
"""
from sire.legacy import IO as _SireIO
from sire.legacy import MM as _SireMM
from sire.legacy import Base as _SireBase
from sire.legacy import Mol as _SireMol
from sire.legacy import Units as _SireUnits
from .._Exceptions import IncompatibleError as _IncompatibleError
from .._SireWrappers import Molecule as _Molecule
# Validate input.
if not isinstance(molecule0, _Molecule):
raise TypeError(
"'molecule0' must be of type 'BioSimSpace._SireWrappers.Molecule'"
)
if not isinstance(molecule1, _Molecule):
raise TypeError(
"'molecule0' must be of type 'BioSimSpace._SireWrappers.Molecule'"
)
# Cannot merge a perturbable molecule.
if molecule0._is_perturbable:
raise _IncompatibleError("'molecule0' has already been merged!")
if molecule1._is_perturbable:
raise _IncompatibleError("'molecule1' has already been merged!")
if not isinstance(property_map0, dict):
raise TypeError("'property_map0' must be of type 'dict'")
if not isinstance(property_map1, dict):
raise TypeError("'property_map1' must be of type 'dict'")
if not isinstance(allow_ring_breaking, bool):
raise TypeError("'allow_ring_breaking' must be of type 'bool'")
if not isinstance(allow_ring_size_change, bool):
raise TypeError("'allow_ring_size_change' must be of type 'bool'")
if not isinstance(force, bool):
raise TypeError("'force' must be of type 'bool'")
if not isinstance(mapping, dict):
raise TypeError("'mapping' must be of type 'dict'.")
else:
# Make sure all key/value pairs are of type AtomIdx.
for idx0, idx1 in mapping.items():
if not isinstance(idx0, _SireMol.AtomIdx) or not isinstance(
idx1, _SireMol.AtomIdx
):
raise TypeError(
"key:value pairs in 'mapping' must be of type 'Sire.Mol.AtomIdx'"
)
# Set 'allow_ring_breaking' and 'allow_ring_size_change' to true if the
# user has requested to 'force' the merge, i.e. the 'force' argument
# takes precedence.
if force:
allow_ring_breaking = True
allow_ring_size_change = True
# Create a copy of this molecule.
mol = _Molecule(molecule0)
# Extract the two Sire molecule objects.
molecule0 = molecule0._sire_object
molecule1 = molecule1._sire_object
# Get the atom indices from the mapping.
idx0 = mapping.keys()
idx1 = mapping.values()
# Create the reverse mapping: molecule1 --> molecule0
inv_mapping = {v: k for k, v in mapping.items()}
# Generate the mappings from each molecule to the merged molecule
mol0_merged_mapping = {}
mol1_merged_mapping = {}
# Invert the user property mappings.
inv_property_map0 = {v: k for k, v in property_map0.items()}
inv_property_map1 = {v: k for k, v in property_map1.items()}
# Make sure that the molecules have a "forcefield" property and that
# the two force fields are compatible.
# Get the user name for the "forcefield" property.
ff0 = inv_property_map0.get("forcefield", "forcefield")
ff1 = inv_property_map1.get("forcefield", "forcefield")
# Force field information is missing.
if not molecule0.has_property(ff0):
raise _IncompatibleError("Cannot determine 'forcefield' of 'molecule0'!")
if not molecule1.has_property(ff1):
raise _IncompatibleError("Cannot determine 'forcefield' of 'molecule1'!")
# The force fields are incompatible.
if not molecule0.property(ff0).is_compatible_with(molecule1.property(ff1)):
raise _IncompatibleError(
"Cannot merge molecules with incompatible force fields!"
)
# Create lists to store the atoms that are unique to each molecule,
# along with their indices.
atoms0 = []
atoms1 = []
atoms0_idx = []
atoms1_idx = []
# Loop over each molecule to find the unique atom indices.
# molecule0
for atom in molecule0.atoms():
if atom.index() not in idx0:
atoms0.append(atom)
atoms0_idx.append(atom.index())
# molecule1
for atom in molecule1.atoms():
if atom.index() not in idx1:
atoms1.append(atom)
atoms1_idx.append(atom.index())
# Create a new molecule to hold the merged molecule.
molecule = _SireMol.Molecule("Merged_Molecule")
# Only part of the ligand is to be merged
if roi is not None:
if molecule0.num_residues() != molecule1.num_residues():
raise ValueError(
"The two molecules need to have the same number of residues"
)
num = 1
for idx, (mol0_res, mol1_res) in enumerate(
zip(molecule0.residues(), molecule1.residues())
):
res = molecule.edit().add(_SireMol.ResNum(idx + 1))
if mol0_res.name() == mol1_res.name():
res.rename(mol0_res.name())
else:
resname = "MUT" if len(molecule0.residues()) > 1 else "LIG"
res.rename(_SireMol.ResName(resname))
cg = res.molecule().add(_SireMol.CGName(f"{idx}"))
for atom in mol0_res.atoms():
mol0_merged_mapping[atom.index()] = _SireMol.AtomIdx(num - 1)
added = cg.add(atom.name())
added.renumber(_SireMol.AtomNum(num))
added.reparent(_SireMol.ResIdx(idx))
num += 1
for atom in mol1_res.atoms():
if atom.index() in atoms1_idx:
added = cg.add(atom.name())
added.renumber(_SireMol.AtomNum(num))
added.reparent(_SireMol.ResIdx(idx))
mol1_merged_mapping[atom.index()] = _SireMol.AtomIdx(num - 1)
num += 1
else:
mol1_merged_mapping[atom.index()] = mol0_merged_mapping[
inv_mapping[atom.index()]
]
molecule = cg.molecule().commit()
else:
# Add a single residue called LIG.
res = molecule.edit().add(_SireMol.ResNum(1))
res.rename(_SireMol.ResName("LIG"))
# Create a single cut-group.
cg = res.molecule().add(_SireMol.CGName("1"))
# Counter for the number of atoms.
num = 1
# First add all of the atoms from molecule0.
for atom in molecule0.atoms():
# Add the atom.
added = cg.add(atom.name())
added.renumber(_SireMol.AtomNum(num))
added.reparent(_SireMol.ResIdx(0))
mol0_merged_mapping[atom.index()] = _SireMol.AtomIdx(num - 1)
num += 1
# Now add all of the atoms from molecule1 that aren't mapped from molecule0.
for atom in molecule1.atoms():
if atom in atoms1:
added = cg.add(atom.name())
added.renumber(_SireMol.AtomNum(num))
added.reparent(_SireMol.ResIdx(0))
mol1_merged_mapping[atom.index()] = _SireMol.AtomIdx(num - 1)
num += 1
else:
mol1_merged_mapping[atom.index()] = mol0_merged_mapping[
inv_mapping[atom.index()]
]
# Commit the changes to the molecule.
molecule = cg.molecule().commit()
# Make the molecule editable.
edit_mol = molecule.edit()
# Create lists of the actual property names in the molecules.
props0 = []
props1 = []
# molecule0
for prop in molecule0.property_keys():
if prop in inv_property_map0:
prop = inv_property_map0[prop]
props0.append(prop)
# molecule1
for prop in molecule1.property_keys():
if prop in inv_property_map1:
prop = inv_property_map1[prop]
props1.append(prop)
# Determine the common properties between the two molecules.
# These are the properties that can be perturbed.
shared_props = list(set(props0).intersection(props1))
# Now add default properties in each end state. This is required since Sire
# removes units from atom properties that are zero valued. By pre-setting
# a default property for the entire molecule, we ensure that atom properties
# are added with the correct type.
# Properties to ignore. (These will be handled later.)
ignored_props = [
"bond",
"angle",
"dihedral",
"improper",
"cmap",
"connectivity",
"intrascale",
]
# lambda = 0
for prop in props0:
if prop not in ignored_props:
# This is a perturbable property.
if prop in shared_props:
name = f"{prop}0"
# This property is unique to the lambda = 0 state.
else:
name = prop
# Get the property from molecule0.
property = molecule0.property(prop)
# Try to set a default property at the lambda = 0 end state.
try:
default_prop = type(property)(molecule.info())
edit_mol = edit_mol.set_property(name, default_prop).molecule()
except:
pass
# lambda = 1
for prop in props1:
if prop not in ignored_props:
# This is a perturbable property.
if prop in shared_props:
name = f"{prop}1"
# This property is unique to the lambda = 1 state.
else:
name = prop
# Get the property from molecule0.
property = molecule1.property(prop)
# Try to set a default property at the lambda = 1 end state.
try:
default_prop = type(property)(molecule.info())
edit_mol = edit_mol.set_property(name, default_prop).molecule()
except:
pass
del props0
del props1
# We now add properties to the merged molecule. The properties are used
# to represent the molecule at two states along the alchemical pathway:
#
# lambda = 0:
# Here only molecule0 is active. The "charge" and "LJ" properties
# for atoms that are part of molecule1 are set to zero. Other force
# field properties, e.g. "bond", "angle", "dihedral", and "improper",
# are retained for the atoms in molecule1, although the indices of
# the atoms involved in the interactions must be re-mapped to their
# positions in the merged molecule. (Also, the interactions may now
# be between atoms of different type.) The properties are given the
# suffix "0", e.g. "charge0".
#
# lambda = 1:
# Here only molecule1 is active. We perform the same process as above,
# only we modify the properties of the atoms that are unique to
# molecule0. The properties are given the suffix "1", e.g. "charge1".
#
# Properties that aren't shared between the molecules (and thus can't
# be merged) are set using their original names.
##############################
# SET PROPERTIES AT LAMBDA = 0
##############################
# Add the atom properties from molecule0.
for atom in molecule0.atoms():
# Get the atom index in the merged molecule.
idx = mol0_merged_mapping[atom.index()]
# Add an "name0" property.
edit_mol = (
edit_mol.atom(idx).set_property("name0", atom.name().value()).molecule()
)
# Loop over all atom properties.
for prop in atom.property_keys():
# Get the actual property name.
name = inv_property_map0.get(prop, prop)
# This is a perturbable property. Rename to "property0", e.g. "charge0".
if name in shared_props:
name = name + "0"
# Add the property to the atom in the merged molecule.
edit_mol = (
edit_mol.atom(idx).set_property(name, atom.property(prop)).molecule()
)
# Add the atom properties from molecule1.
for atom in atoms1:
# Get the atom index in the merged molecule.
idx = mol1_merged_mapping[atom.index()]
# Add an "name0" property.
edit_mol = (
edit_mol.atom(idx).set_property("name0", atom.name().value()).molecule()
)
# Loop over all atom properties.
for prop in atom.property_keys():
# Get the actual property name.
name = inv_property_map1.get(prop, prop)
# Zero the "charge" and "LJ" property for atoms that are unique to molecule1.
if name == "charge":
edit_mol = (
edit_mol.atom(idx)
.set_property("charge0", 0 * _SireUnits.e_charge)
.molecule()
)
elif name == "LJ":
edit_mol = (
edit_mol.atom(idx)
.set_property("LJ0", _SireMM.LJParameter())
.molecule()
)
elif name == "ambertype":
edit_mol = (
edit_mol.atom(idx).set_property("ambertype0", "du").molecule()
)
elif name == "element":
edit_mol = (
edit_mol.atom(idx)
.set_property("element0", _SireMol.Element(0))
.molecule()
)
else:
# This is a perturbable property. Rename to "property0", e.g. "charge0".
if name in shared_props:
name = name + "0"
# Add the property to the atom in the merged molecule.
edit_mol = (
edit_mol.atom(idx)
.set_property(name, atom.property(prop))
.molecule()
)
# We now need to merge "bond", "angle", "dihedral", and "improper" parameters.
# To do so, we extract the properties from molecule0, then add the additional
# properties from molecule1, making sure to update the atom indices, and bond
# atoms from molecule1 to the atoms to which they map in molecule0.
# 1) bonds
if "bond" in shared_props:
# Get the user defined property names.
prop0 = inv_property_map0.get("bond", "bond")
prop1 = inv_property_map1.get("bond", "bond")
# Get the "bond" property from the two molecules.
bonds0 = molecule0.property(prop0)
bonds1 = molecule1.property(prop1)
# Get the molInfo object for each molecule.
info0 = molecule0.info()
info1 = molecule1.info()
# Create the new set of bonds.
bonds = _SireMM.TwoAtomFunctions(edit_mol.info())
# Add all of the bonds from molecule0.
for bond in bonds0.potentials():
atom0 = mol0_merged_mapping[info0.atom_idx(bond.atom0())]
atom1 = mol0_merged_mapping[info0.atom_idx(bond.atom1())]
bonds.set(atom0, atom1, bond.function())
# Loop over all bonds in molecule1.
for bond in bonds1.potentials():
# This bond contains an atom that is unique to molecule1.
if (
info1.atom_idx(bond.atom0()) in atoms1_idx
or info1.atom_idx(bond.atom1()) in atoms1_idx
):
# Extract the bond information.
atom0 = info1.atom_idx(bond.atom0())
atom1 = info1.atom_idx(bond.atom1())
exprn = bond.function()
# Map the atom indices to their position in the merged molecule.
atom0 = mol1_merged_mapping[atom0]
atom1 = mol1_merged_mapping[atom1]
# Set the new bond.
bonds.set(atom0, atom1, exprn)
# Add the bonds to the merged molecule.
edit_mol.set_property("bond0", bonds)
# 2) angles
if "angle" in shared_props:
# Get the user defined property names.
prop0 = inv_property_map0.get("angle", "angle")
prop1 = inv_property_map1.get("angle", "angle")
# Get the "angle" property from the two molecules.
angles0 = molecule0.property(prop0)
angles1 = molecule1.property(prop1)
# Get the molInfo object for each molecule.
info0 = molecule0.info()
info1 = molecule1.info()
# Create the new set of angles.
angles = _SireMM.ThreeAtomFunctions(edit_mol.info())
# Add all of the angles from molecule0.
for angle in angles0.potentials():
atom0 = mol0_merged_mapping[info0.atom_idx(angle.atom0())]
atom1 = mol0_merged_mapping[info0.atom_idx(angle.atom1())]
atom2 = mol0_merged_mapping[info0.atom_idx(angle.atom2())]
angles.set(atom0, atom1, atom2, angle.function())
# Loop over all angles in molecule1.
for angle in angles1.potentials():
# This angle contains an atom that is unique to molecule1.
if (
info1.atom_idx(angle.atom0()) in atoms1_idx
or info1.atom_idx(angle.atom1()) in atoms1_idx
or info1.atom_idx(angle.atom2()) in atoms1_idx
):
# Extract the angle information.
atom0 = info1.atom_idx(angle.atom0())
atom1 = info1.atom_idx(angle.atom1())
atom2 = info1.atom_idx(angle.atom2())
exprn = angle.function()
# Map the atom indices to their position in the merged molecule.
atom0 = mol1_merged_mapping[atom0]
atom1 = mol1_merged_mapping[atom1]
atom2 = mol1_merged_mapping[atom2]
# Set the new angle.
angles.set(atom0, atom1, atom2, exprn)
# Add the angles to the merged molecule.
edit_mol.set_property("angle0", angles)
# 3) dihedrals
if "dihedral" in shared_props:
# Get the user defined property names.
prop0 = inv_property_map0.get("dihedral", "dihedral")
prop1 = inv_property_map1.get("dihedral", "dihedral")
# Get the "dihedral" property from the two molecules.
dihedrals0 = molecule0.property(prop0)
dihedrals1 = molecule1.property(prop1)
# Get the molInfo object for each molecule.
info0 = molecule0.info()
info1 = molecule1.info()
# Create the new set of dihedrals.
dihedrals = _SireMM.FourAtomFunctions(edit_mol.info())
# Add all of the dihedrals from molecule0.
for dihedral in dihedrals0.potentials():
atom0 = mol0_merged_mapping[info0.atom_idx(dihedral.atom0())]
atom1 = mol0_merged_mapping[info0.atom_idx(dihedral.atom1())]
atom2 = mol0_merged_mapping[info0.atom_idx(dihedral.atom2())]
atom3 = mol0_merged_mapping[info0.atom_idx(dihedral.atom3())]
dihedrals.set(atom0, atom1, atom2, atom3, dihedral.function())
# Loop over all dihedrals in molecule1.
for dihedral in dihedrals1.potentials():
# This dihedral contains an atom that is unique to molecule1.
if (
info1.atom_idx(dihedral.atom0()) in atoms1_idx
or info1.atom_idx(dihedral.atom1()) in atoms1_idx
or info1.atom_idx(dihedral.atom2()) in atoms1_idx
or info1.atom_idx(dihedral.atom3()) in atoms1_idx
):
# Extract the dihedral information.
atom0 = info1.atom_idx(dihedral.atom0())
atom1 = info1.atom_idx(dihedral.atom1())
atom2 = info1.atom_idx(dihedral.atom2())
atom3 = info1.atom_idx(dihedral.atom3())
exprn = dihedral.function()
# Map the atom indices to their position in the merged molecule.
atom0 = mol1_merged_mapping[atom0]
atom1 = mol1_merged_mapping[atom1]
atom2 = mol1_merged_mapping[atom2]
atom3 = mol1_merged_mapping[atom3]
# Set the new dihedral.
dihedrals.set(atom0, atom1, atom2, atom3, exprn)
# Add the dihedrals to the merged molecule.
edit_mol.set_property("dihedral0", dihedrals)
# 4) impropers
if "improper" in shared_props:
# Get the user defined property names.
prop0 = inv_property_map0.get("improper", "improper")
prop1 = inv_property_map1.get("improper", "improper")
# Get the "improper" property from the two molecules.
impropers0 = molecule0.property(prop0)
impropers1 = molecule1.property(prop1)
# Get the molInfo object for each molecule.
info0 = molecule0.info()
info1 = molecule1.info()
# Create the new set of impropers.
impropers = _SireMM.FourAtomFunctions(edit_mol.info())
# Add all of the impropers from molecule0.
for improper in impropers0.potentials():
atom0 = mol0_merged_mapping[info0.atom_idx(improper.atom0())]
atom1 = mol0_merged_mapping[info0.atom_idx(improper.atom1())]
atom2 = mol0_merged_mapping[info0.atom_idx(improper.atom2())]
atom3 = mol0_merged_mapping[info0.atom_idx(improper.atom3())]
impropers.set(atom0, atom1, atom2, atom3, improper.function())
# Loop over all impropers in molecule1.
for improper in impropers1.potentials():
# This improper contains an atom that is unique to molecule1.
if (
info1.atom_idx(improper.atom0()) in atoms1_idx
or info1.atom_idx(improper.atom1()) in atoms1_idx
or info1.atom_idx(improper.atom2()) in atoms1_idx
or info1.atom_idx(improper.atom3()) in atoms1_idx
):
# Extract the improper information.
atom0 = info1.atom_idx(improper.atom0())
atom1 = info1.atom_idx(improper.atom1())
atom2 = info1.atom_idx(improper.atom2())
atom3 = info1.atom_idx(improper.atom3())
exprn = improper.function()
# Map the atom indices to their position in the merged molecule.
atom0 = mol1_merged_mapping[atom0]
atom1 = mol1_merged_mapping[atom1]
atom2 = mol1_merged_mapping[atom2]
atom3 = mol1_merged_mapping[atom3]
# Set the new improper.
impropers.set(atom0, atom1, atom2, atom3, exprn)
# Add the impropers to the merged molecule.
edit_mol.set_property("improper0", impropers)
# 5) cmap
if "cmap" in shared_props:
# Get the user defined property names.
prop0 = inv_property_map0.get("cmap", "cmap")
prop1 = inv_property_map1.get("cmap", "cmap")
# Get the "cmap" property from the two molecules.
cmaps0 = molecule0.property(prop0)
cmaps1 = molecule1.property(prop1)
# Get the molInfo object for each molecule.
info0 = molecule0.info()
info1 = molecule1.info()
# Create the new set of CMAP functions.
cmaps = _SireMM.CMAPFunctions(edit_mol.info())
# Add all of the CMAP functions from molecule0.
for func in cmaps0.parameters():
atom0 = mol0_merged_mapping[info0.atom_idx(func.atom0())]
atom1 = mol0_merged_mapping[info0.atom_idx(func.atom1())]
atom2 = mol0_merged_mapping[info0.atom_idx(func.atom2())]
atom3 = mol0_merged_mapping[info0.atom_idx(func.atom3())]
atom4 = mol0_merged_mapping[info0.atom_idx(func.atom4())]
cmaps.set(atom0, atom1, atom2, atom3, atom4, func.parameter())
# Loop over all CMAP functions in molecule1.
for func in cmaps1.parameters():
# This CMAP function contains an atom that is unique to molecule1.
if (
info1.atom_idx(func.atom0()) in atoms1_idx
or info1.atom_idx(func.atom1()) in atoms1_idx
or info1.atom_idx(func.atom2()) in atoms1_idx
or info1.atom_idx(func.atom3()) in atoms1_idx
or info1.atom_idx(func.atom4()) in atoms1_idx
):
atom0 = mol1_merged_mapping[info1.atom_idx(func.atom0())]
atom1 = mol1_merged_mapping[info1.atom_idx(func.atom1())]
atom2 = mol1_merged_mapping[info1.atom_idx(func.atom2())]
atom3 = mol1_merged_mapping[info1.atom_idx(func.atom3())]
atom4 = mol1_merged_mapping[info1.atom_idx(func.atom4())]
cmaps.set(atom0, atom1, atom2, atom3, atom4, func.parameter())
# Add the CMAP functions to the merged molecule.
edit_mol.set_property("cmap0", cmaps)
##############################
# SET PROPERTIES AT LAMBDA = 1
##############################
# Add the atom properties from molecule1.
for atom in molecule1.atoms():
# Get the atom index in the merged molecule.
idx = mol1_merged_mapping[atom.index()]
# Add an "name1" property.
edit_mol = (
edit_mol.atom(idx).set_property("name1", atom.name().value()).molecule()
)
# Loop over all atom properties.
for prop in atom.property_keys():
# Get the actual property name.
name = inv_property_map1.get(prop, prop)
# This is a perturbable property. Rename to "property1", e.g. "charge1".
if name in shared_props:
name = name + "1"
# Add the property to the atom in the merged molecule.
edit_mol = (
edit_mol.atom(idx).set_property(name, atom.property(prop)).molecule()
)
# Add the properties from atoms unique to molecule0.
for atom in atoms0:
# Get the atom index in the merged molecule.
idx = mol0_merged_mapping[atom.index()]
# Add an "name1" property.
edit_mol = (
edit_mol.atom(idx).set_property("name1", atom.name().value()).molecule()
)
# Loop over all atom properties.
for prop in atom.property_keys():
# Get the actual property name.
name = inv_property_map0.get(prop, prop)
# Zero the "charge" and "LJ" property for atoms that are unique to molecule0.
if name == "charge":
edit_mol = (
edit_mol.atom(idx)
.set_property("charge1", 0 * _SireUnits.e_charge)
.molecule()
)
elif name == "LJ":
edit_mol = (
edit_mol.atom(idx)
.set_property("LJ1", _SireMM.LJParameter())
.molecule()
)
elif name == "ambertype":
edit_mol = (
edit_mol.atom(idx).set_property("ambertype1", "du").molecule()
)
elif name == "element":
edit_mol = (
edit_mol.atom(idx)
.set_property("element1", _SireMol.Element(0))
.molecule()
)
else:
# This is a perturbable property. Rename to "property1", e.g. "charge1".
if name in shared_props:
name = name + "1"
# Add the property to the atom in the merged molecule.
edit_mol = (
edit_mol.atom(idx)
.set_property(name, atom.property(prop))
.molecule()
)
# Tolerance for zero sigma values.
null_lj_sigma = 1e-9
# Atoms with zero LJ sigma values need to have their sigma values set to the
# value from the other end state.
for atom in edit_mol.atoms():
# Get the end state LJ sigma values.
lj0 = atom.property("LJ0")
lj1 = atom.property("LJ1")
# Lambda = 0 state has a zero sigma value.
if abs(lj0.sigma().value()) <= null_lj_sigma:
# Use the sigma value from the lambda = 1 state.
edit_mol = (
edit_mol.atom(atom.index())
.set_property("LJ0", _SireMM.LJParameter(lj1.sigma(), lj0.epsilon()))
.molecule()
)
# Lambda = 1 state has a zero sigma value.
if abs(lj1.sigma().value()) <= null_lj_sigma:
# Use the sigma value from the lambda = 0 state.
edit_mol = (
edit_mol.atom(atom.index())
.set_property("LJ1", _SireMM.LJParameter(lj0.sigma(), lj1.epsilon()))
.molecule()
)
# We now need to merge "bond", "angle", "dihedral", and "improper" parameters.
# To do so, we extract the properties from molecule1, then add the additional
# properties from molecule0, making sure to update the atom indices, and bond
# atoms from molecule0 to the atoms to which they map in molecule1.
# 1) bonds
if "bond" in shared_props:
# Get the info objects for the two molecules.
info0 = molecule0.info()
info1 = molecule1.info()
# Get the user defined property names.
prop0 = inv_property_map0.get("bond", "bond")
prop1 = inv_property_map1.get("bond", "bond")
# Get the "bond" property from the two molecules.
bonds0 = molecule0.property(prop0)
bonds1 = molecule1.property(prop1)
# Create the new set of bonds.
bonds = _SireMM.TwoAtomFunctions(edit_mol.info())
# Add all of the bonds from molecule1.
for bond in bonds1.potentials():
# Extract the bond information.
atom0 = info1.atom_idx(bond.atom0())
atom1 = info1.atom_idx(bond.atom1())
exprn = bond.function()
# Map the atom indices to their position in the merged molecule.
atom0 = mol1_merged_mapping[atom0]
atom1 = mol1_merged_mapping[atom1]
# Set the new bond.
bonds.set(atom0, atom1, exprn)
# Loop over all bonds in molecule0
for bond in bonds0.potentials():
# This bond contains an atom that is unique to molecule0.
if (
info0.atom_idx(bond.atom0()) in atoms0_idx
or info0.atom_idx(bond.atom1()) in atoms0_idx
):
# Extract the bond information.
atom0 = mol0_merged_mapping[info0.atom_idx(bond.atom0())]
atom1 = mol0_merged_mapping[info0.atom_idx(bond.atom1())]
exprn = bond.function()
# Set the new bond.
bonds.set(atom0, atom1, exprn)
# Add the bonds to the merged molecule.
edit_mol.set_property("bond1", bonds)
# 2) angles
if "angle" in shared_props:
# Get the info objects for the two molecules.
info0 = molecule0.info()
info1 = molecule1.info()
# Get the user defined property names.
prop0 = inv_property_map0.get("angle", "angle")
prop1 = inv_property_map1.get("angle", "angle")
# Get the "angle" property from the two molecules.
angles0 = molecule0.property(prop0)
angles1 = molecule1.property(prop1)
# Create the new set of angles.
angles = _SireMM.ThreeAtomFunctions(edit_mol.info())
# Add all of the angles from molecule1.
for angle in angles1.potentials():
# Extract the angle information.
atom0 = info1.atom_idx(angle.atom0())
atom1 = info1.atom_idx(angle.atom1())
atom2 = info1.atom_idx(angle.atom2())
exprn = angle.function()
# Map the atom indices to their position in the merged molecule.
atom0 = mol1_merged_mapping[atom0]
atom1 = mol1_merged_mapping[atom1]
atom2 = mol1_merged_mapping[atom2]
# Set the new angle.
angles.set(atom0, atom1, atom2, exprn)
# Loop over all angles in molecule0.
for angle in angles0.potentials():
# This angle contains an atom that is unique to molecule0.
if (
info0.atom_idx(angle.atom0()) in atoms0_idx
or info0.atom_idx(angle.atom1()) in atoms0_idx
or info0.atom_idx(angle.atom2()) in atoms0_idx
):
# Extract the angle information.
atom0 = mol0_merged_mapping[info0.atom_idx(angle.atom0())]
atom1 = mol0_merged_mapping[info0.atom_idx(angle.atom1())]
atom2 = mol0_merged_mapping[info0.atom_idx(angle.atom2())]
exprn = angle.function()
# Set the new angle.
angles.set(atom0, atom1, atom2, exprn)
# Add the angles to the merged molecule.
edit_mol.set_property("angle1", angles)
# 3) dihedrals
if "dihedral" in shared_props:
# Get the info objects for the two molecules.
info0 = molecule0.info()
info1 = molecule1.info()
# Get the user defined property names.
prop0 = inv_property_map0.get("dihedral", "dihedral")
prop1 = inv_property_map1.get("dihedral", "dihedral")
# Get the "dihedral" property from the two molecules.
dihedrals0 = molecule0.property(prop0)
dihedrals1 = molecule1.property(prop1)
# Create the new set of dihedrals.
dihedrals = _SireMM.FourAtomFunctions(edit_mol.info())
# Add all of the dihedrals from molecule1.
for dihedral in dihedrals1.potentials():
# Extract the dihedral information.
atom0 = info1.atom_idx(dihedral.atom0())
atom1 = info1.atom_idx(dihedral.atom1())
atom2 = info1.atom_idx(dihedral.atom2())
atom3 = info1.atom_idx(dihedral.atom3())
exprn = dihedral.function()
# Map the atom indices to their position in the merged molecule.
atom0 = mol1_merged_mapping[atom0]
atom1 = mol1_merged_mapping[atom1]
atom2 = mol1_merged_mapping[atom2]
atom3 = mol1_merged_mapping[atom3]
# Set the new dihedral.
dihedrals.set(atom0, atom1, atom2, atom3, exprn)
# Loop over all dihedrals in molecule0.
for dihedral in dihedrals0.potentials():
# This dihedral contains an atom that is unique to molecule0.
if (
info0.atom_idx(dihedral.atom0()) in atoms0_idx
or info0.atom_idx(dihedral.atom1()) in atoms0_idx
or info0.atom_idx(dihedral.atom2()) in atoms0_idx
or info0.atom_idx(dihedral.atom3()) in atoms0_idx
):
# Extract the dihedral information.
atom0 = mol0_merged_mapping[info0.atom_idx(dihedral.atom0())]
atom1 = mol0_merged_mapping[info0.atom_idx(dihedral.atom1())]
atom2 = mol0_merged_mapping[info0.atom_idx(dihedral.atom2())]
atom3 = mol0_merged_mapping[info0.atom_idx(dihedral.atom3())]
exprn = dihedral.function()
# Set the new dihedral.
dihedrals.set(atom0, atom1, atom2, atom3, exprn)
# Add the dihedrals to the merged molecule.
edit_mol.set_property("dihedral1", dihedrals)
# 4) impropers
if "improper" in shared_props:
# Get the info objects for the two molecules.
info0 = molecule0.info()
info1 = molecule1.info()
# Get the user defined property names.
prop0 = inv_property_map0.get("improper", "improper")
prop1 = inv_property_map1.get("improper", "improper")
# Get the "improper" property from the two molecules.
impropers0 = molecule0.property(prop0)
impropers1 = molecule1.property(prop1)