sample
#
Functionality to obtain samples to fit the force field to.
Classes:
-
SampleFnArgs–Arguments for sampling functions.
-
SampleFn–A protocol for sampling functions.
Functions:
-
recalculate_energies_and_forces–Recalculate energies and forces for a dataset using a given OpenMM simulation.
-
sample_mmmd–Generate datasets of samples from MD with the given MM force field for multiple molecules.
-
sample_mlmd–Generate datasets of samples from MD with an ML potential for multiple molecules.
-
sample_mmmd_metadynamics–Generate datasets using metadynamics for multiple molecules.
-
generate_torsion_minimised_dataset–Generate a dataset of torsion-restrained minimised structures.
-
sample_mmmd_metadynamics_with_torsion_minimisation–Generate datasets using metadynamics with additional torsion-minimised structures.
-
load_precomputed_dataset–Load pre-computed dataset(s) from disk.
_SAMPLING_FNS_REGISTRY
module-attribute
#
_SAMPLING_FNS_REGISTRY: dict[
type[SamplingSettings], SampleFn
] = {}
Registry of sampling functions for different sampling settings types.
SampleFnArgs
#
Bases: TypedDict
Arguments for sampling functions.
SampleFn
#
Bases: Protocol
A protocol for sampling functions.
_copy_mol_and_add_conformers
#
Copy a molecule and add conformers to it.
Source code in presto/sample.py
_run_md
#
_run_md(
mol: Molecule,
simulation: Simulation,
step_fn: Callable[[int], None],
equilibration_n_steps_per_conformer: int,
production_n_snapshots_per_conformer: int,
production_n_steps_per_snapshot_per_conformer: int,
pdb_reporter_path: str | None = None,
) -> Dataset
Run MD on a molecule and return a dataset of the coordinates, energies, and forces of the snapshots.
Parameters:
-
mol(Molecule) –The molecule to run MD on. Should have conformers already generated.
-
simulation(Simulation) –The OpenMM simulation to use for MD.
-
step_fn(Callable[[int], None]) –A function that takes the number of steps to run and runs them in the simulation. This is to allow for different types of MD (e.g. with or without metadynamics).
-
equilibration_n_steps_per_conformer(int) –The number of equilibration steps to run per conformer.
-
production_n_snapshots_per_conformer(int) –The number of production snapshots to take per conformer.
-
production_n_steps_per_snapshot_per_conformer(int) –The number of production steps to run between each snapshot per conformer.
-
pdb_reporter_path(str | None, default:None) –The path to write a PDB trajectory of the MD simulation to. The frames saved correspond to the production snapshots. If None, no trajectory is saved.
Returns:
-
Dataset–The dataset of snapshots with coordinates, energies, and forces.
Source code in presto/sample.py
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 | |
_get_ml_omm_system
#
_get_ml_omm_system(
mol: Molecule, mlp_name: AvailableModels
) -> System
Get an OpenMM system for a molecule using a machine learning potential.
Parameters:
-
mol(Molecule) –The molecule for which to create the system.
-
mlp_name(AvailableModels) –The name of the ML potential to use.
Returns:
-
System–The OpenMM system for the molecule.
Raises:
-
InvalidSettingsError–If the molecule is charged and the ML potential does not support charges.
Source code in presto/sample.py
recalculate_energies_and_forces
#
Recalculate energies and forces for a dataset using a given OpenMM simulation.
Source code in presto/sample.py
sample_mmmd
#
sample_mmmd(
mols: list[Molecule],
off_ff: ForceField,
device: device,
settings: MMMDSamplingSettings,
output_paths: dict[OutputType, Path],
) -> list[Dataset]
Generate datasets of samples from MD with the given MM force field for multiple molecules.
Parameters:
-
mols(list[Molecule]) –The molecules to sample.
-
off_ff(ForceField) –The MM force field to use for sampling.
-
device(device) –The device to use for any MD or ML calculations.
-
settings(_SamplingSettings) –The sampling settings to use.
-
output_paths(dict[OutputType, Path]) –A mapping of output types to filesystem paths.
Returns:
-
list[Dataset]–The generated datasets of samples with energies and forces, one per molecule.
Source code in presto/sample.py
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 | |
sample_mlmd
#
sample_mlmd(
mols: list[Molecule],
off_ff: ForceField,
device: device,
settings: MLMDSamplingSettings,
output_paths: dict[OutputType, Path],
) -> list[Dataset]
Generate datasets of samples from MD with an ML potential for multiple molecules.
Parameters:
-
mols(list[Molecule]) –The molecules to sample.
-
off_ff(ForceField) –The MM force field (kept for consistency).
-
device(device) –The device to use for any MD or ML calculations.
-
settings(_SamplingSettings) –The sampling settings to use.
-
output_paths(dict[OutputType, Path]) –A mapping of output types to filesystem paths.
Returns:
-
list[Dataset]–The generated datasets of samples with energies and forces, one per molecule.
Source code in presto/sample.py
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 | |
_get_torsion_bias_forces
#
_get_torsion_bias_forces(
mol: Molecule,
torsions_to_include: list[
str
] = DEFAULT_TORSIONS_TO_INCLUDE_SMARTS,
torsions_to_exclude: list[
str
] = DEFAULT_TORSIONS_TO_EXCLUDE_SMARTS,
bias_width: float = pi / 10,
) -> list[BiasVariable]
Find important torsions in a molecule and return a list of BiasVariable objects - one for each torsion.
Args: mol: OpenFF Molecule. torsions_to_include: List of SMARTS patterns to include. torsions_to_exclude: List of SMARTS patterns to exclude. bias_width: Width of the bias to apply to each torsion.
Returns: List of BiasVariable objects for each torsion.
Source code in presto/sample.py
sample_mmmd_metadynamics
#
sample_mmmd_metadynamics(
mols: list[Molecule],
off_ff: ForceField,
device: device,
settings: MMMDMetadynamicsSamplingSettings,
output_paths: dict[OutputType, Path],
) -> list[Dataset]
Generate datasets using metadynamics for multiple molecules.
Parameters:
-
mols(list[Molecule]) –The molecules to sample.
-
off_ff(ForceField) –The MM force field to use.
-
device(device) –The device to use for any MD or ML calculations.
-
settings(MMMDMetadynamicsSamplingSettings) –The sampling settings to use.
-
output_paths(dict[OutputType, Path]) –A mapping of output types to filesystem paths.
Returns:
-
list[Dataset]–The generated datasets of samples with energies and forces, one per molecule.
Source code in presto/sample.py
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 | |
_get_molecule_from_dataset
#
Extract molecule from dataset using SMILES.
Parameters:
-
dataset(Dataset) –Dataset containing SMILES string
Returns:
-
Molecule–Reconstructed molecule from SMILES
Source code in presto/sample.py
_find_available_force_group
#
Find an unused force group in the simulation system.
Parameters:
-
simulation(Simulation) –OpenMM simulation object
Returns:
-
int–An available force group number (0-31)
Raises:
-
RuntimeError–If all force groups (0-31) are in use
Source code in presto/sample.py
_add_torsion_restraint_forces
#
_add_torsion_restraint_forces(
simulation: Simulation,
torsion_atoms_list: list[tuple[int, int, int, int]],
force_constant: float,
initial_angles: list[float] | None = None,
) -> tuple[list[int], int]
Add torsion restraint forces to the simulation system.
This adds CustomTorsionForce objects that can be updated later without reinitializing the context. All restraints are added to a dedicated force group.
Parameters:
-
simulation(Simulation) –OpenMM simulation object
-
torsion_atoms_list(list[tuple[int, int, int, int]]) –List of torsion atom indices to freeze
-
force_constant(float) –Force constant for torsion restraints (in kJ/mol/rad^2)
-
initial_angles(list[float] | None, default:None) –Initial target angles for each torsion (in radians). If None, defaults to 0.0 for all torsions.
Returns:
-
tuple[list[int], int]–Tuple of (list of force indices that were added, force group number)
Source code in presto/sample.py
_update_torsion_restraints
#
_update_torsion_restraints(
simulation: Simulation,
force_indices: list[int],
target_angles: list[float],
force_constant: float,
) -> None
Update the target angles for torsion restraints without reinitializing.
Parameters:
-
simulation(Simulation) –OpenMM simulation object
-
force_indices(list[int]) –List of force indices for the torsion restraints
-
target_angles(list[float]) –New target angles (in radians) for each torsion
-
force_constant(float) –Force constant for torsion restraints (in kJ/mol/rad^2)
Source code in presto/sample.py
_remove_torsion_restraint_forces
#
Remove torsion restraint forces from the simulation.
Parameters:
-
simulation(Simulation) –OpenMM simulation object
-
force_indices(list[int]) –List of force indices to remove
Source code in presto/sample.py
_minimize_with_frozen_torsions
#
_minimize_with_frozen_torsions(
simulation: Simulation,
coords: ndarray,
torsion_atoms_list: list[tuple[int, int, int, int]],
force_indices: list[int],
torsion_force_constant: float,
restraint_force_group: int,
max_iterations: int = 0,
) -> tuple[ndarray, float, ndarray]
Minimize a conformation with all torsions frozen.
Assumes torsion restraint forces have already been added to the system. Only updates the target angles without reinitializing.
Parameters:
-
simulation(Simulation) –OpenMM simulation object (with torsion forces already added)
-
coords(ndarray) –Starting coordinates
-
torsion_atoms_list(list[tuple[int, int, int, int]]) –List of torsion atom indices to freeze
-
force_indices(list[int]) –Indices of the torsion restraint forces in the system
-
torsion_force_constant(float) –Force constant for torsion restraints (in kJ/mol/rad^2)
-
restraint_force_group(int) –Force group number for the torsion restraints
-
max_iterations(int, default:0) –Maximum minimization iterations (0 = until convergence)
Returns:
-
tuple[ndarray, float, ndarray]–Minimized coordinates, energy, and forces (excluding restraint forces)
Source code in presto/sample.py
generate_torsion_minimised_dataset
#
generate_torsion_minimised_dataset(
mm_dataset: Dataset,
ml_simulation: Simulation,
mm_simulation: Simulation,
torsion_restraint_force_constant: float = 1000.0,
torsions_to_include_smarts: list[
str
] = DEFAULT_TORSIONS_TO_INCLUDE_SMARTS,
torsions_to_exclude_smarts: list[
str
] = DEFAULT_TORSIONS_TO_EXCLUDE_SMARTS,
ml_minimisation_steps: int = 10,
mm_minimisation_steps: int = 10,
ml_pdb_path: Path | str | None = None,
mm_pdb_path: Path | str | None = None,
map_ml_coords_energy_to_mm_coords_energy: bool = True,
mm_min_energy_weight: float = 1000.0,
mm_min_forces_weight: float = 0.1,
ml_min_energy_weight: float = 1000.0,
ml_min_forces_weight: float = 0.1,
) -> tuple[Dataset, Dataset]
Generate a dataset of torsion-restrained minimised structures.
For each conformation in the input dataset: 1. Restrain all rotatable torsions to their current values 2. Perform a short MLP minimisation and save the energies 3. From those coordinates, perform a short MM minimisation and save the coordinates 4. Set forces to 0
Parameters:
-
mm_dataset(Dataset) –Input dataset with coordinates from MM MD sampling.
-
ml_simulation(Simulation) –OpenMM simulation with ML potential.
-
mm_simulation(Simulation) –OpenMM simulation with MM force field.
-
torsion_restraint_force_constant(float, default:1000.0) –Force constant for torsion restraints in kJ/mol/rad^2.
-
torsions_to_include_smarts(list[str] | None, default:DEFAULT_TORSIONS_TO_INCLUDE_SMARTS) –List of SMARTS patterns to include for rotatable torsions. If None, include all rotatable torsions.
-
torsions_to_exclude_smarts(list[str] | None, default:DEFAULT_TORSIONS_TO_EXCLUDE_SMARTS) –List of SMARTS patterns to exclude for rotatable torsions. If None, exclude no rotatable torsions.
-
ml_minimisation_steps(int, default:10) –Number of MLP minimisation steps (default: 10).
-
mm_minimisation_steps(int, default:10) –Number of MM minimisation steps (default: 10).
-
ml_pdb_path(Path | str | None, default:None) –Path to save ML-minimised structures as a multi-model PDB file.
-
mm_pdb_path(Path | str | None, default:None) –Path to save MM-minimised structures as a multi-model PDB file.
-
map_ml_coords_energy_to_mm_coords_energy(bool, default:True) –Whether to substitute the MLP energy for the MM-minimised coordinates with the MLP energy for the corresponding MLP-minimised coordinates.
-
mm_min_energy_weight(float, default:1000.0) –Energy weight for MM-minimised dataset.
-
mm_min_forces_weight(float, default:0.1) –Forces weight for MM-minimised dataset.
-
ml_min_energy_weight(float, default:1000.0) –Energy weight for ML-minimised dataset.
-
ml_min_forces_weight(float, default:0.1) –Forces weight for ML-minimised dataset.
Returns:
-
tuple[Dataset, Dataset]–Tuple of (MM-minimised dataset, ML-minimised dataset).
Source code in presto/sample.py
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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 | |
sample_mmmd_metadynamics_with_torsion_minimisation
#
sample_mmmd_metadynamics_with_torsion_minimisation(
mols: list[Molecule],
off_ff: ForceField,
device: device,
settings: MMMDMetadynamicsTorsionMinimisationSamplingSettings,
output_paths: dict[OutputType, Path],
) -> list[Dataset]
Generate datasets using metadynamics with additional torsion-minimised structures.
This function extends sample_mmmd_metadynamics by: 1. Running metadynamics to generate samples (same as sample_mmmd_metadynamics) 2. For each sample, generating additional torsion-restrained minimised structures using both the ML and MM potentials. 3. Returning all datasets with requested weights.
Parameters:
-
mols(list[Molecule]) –The molecules to sample.
-
off_ff(ForceField) –The MM force field to use.
-
device(device) –The device to use for any MD or ML calculations.
-
settings(MMMDMetadynamicsTorsionMinimisationSamplingSettings) –The sampling settings to use.
-
output_paths(dict[OutputType, Path]) –A mapping of output types to filesystem paths.
Returns:
-
list[Dataset]–The generated datasets with combined metadynamics and torsion-minimised samples.
Source code in presto/sample.py
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 | |
load_precomputed_dataset
#
load_precomputed_dataset(
mols: list[Molecule],
off_ff: ForceField,
device: device,
settings: PreComputedDatasetSettings,
output_paths: dict[OutputType, Path],
) -> list[Dataset]
Load pre-computed dataset(s) from disk.
For single-molecule fits, loads one dataset. For multi-molecule fits,
loads one dataset per molecule in the order they appear in mols.
Parameters:
-
mols(list[Molecule]) –The molecules. The number of datasets loaded must match the number of molecules.
-
off_ff(ForceField) –The force field (not used, kept for API consistency).
-
device(device) –The device to set the dataset format to.
-
settings(PreComputedDatasetSettings) –Settings containing the path(s) to the pre-computed dataset(s).
-
output_paths(dict[OutputType, Path]) –Output paths (should be empty for this protocol).
Returns:
-
list[Dataset]–The loaded datasets, one per molecule.
Raises:
-
ValueError–If the number of dataset paths doesn't match the number of molecules.
-
FileNotFoundError–If any dataset path doesn't exist.
Source code in presto/sample.py
1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 | |