utils

grandfep.utils.find_reference_atom_indices(topology, ref_atoms_list)

Find atom indices in the topology that match the given reference atom definitions.

Parameters:
  • topology (Topology) – OpenMM topology object

  • ref_atoms_list (list) –

    A list of dictionaries specifying reference atoms. Each dictionary can contain any combination of the following keys:

    • chain_index: int

      Index of the chain in the topology.

    • res_name: str

      Residue name (e.g., “HOH”).

    • res_id: str

      In openmm topology, res_id is string.

    • res_index: int

      0-based index of the residue in the topology.

    • atom_name: str

      Atom name (e.g., “O”, “H1”).

Returns:

A list of integer atom indices matching the provided atom specifications.

Return type:

list

Examples

>>> top = "test/KcsA_5VKE_SF/step1_pdbreader_12WAT.psf"
>>> topology, _ = utils.load_top(top)
>>> ref_atoms_list = [{"res_id":"71", "res_name":"GLU", "atom_name":"O"}]
>>> utils.find_reference_atom_indices(topology, ref_atoms_list)
[21, 164, 307, 450]
grandfep.utils.load_sys(sys_file)

Load a serialized OpenMM system from a xml or xml.gz file.

Parameters:

sys_file (Union[str, Path]) – Path to the serialized OpenMM system file (.xml or .xml.gz).

Returns:

The deserialized OpenMM System object.

Return type:

openmm.System

Examples

1from grandfep import utils
2system = utils.load_sys("system.xml.gz")
grandfep.utils.load_top(top_file)

Load a topology file in PSF (CHARMM) or PRMTOP/PARM7 (AMBER) format.

After loading a top file, you should call .setPeriodicBoxVectors() on the topology object to define the box. Without setting the box, trajectory files may lack box information.

Parameters:

top_file (Union[str, Path]) – Path to the topology file (either .psf, .prmtop, or .parm7).

Return type:

tuple[Topology, Union[CharmmPsfFile, AmberPrmtopFile]]

Returns:

  • topology (openmm.app.Topology) – The OpenMM Topology object for use in simulation setup.

  • top_object (openmm.app.CharmmPsfFile or openmm.app.AmberPrmtopFile) – The loaded OpenMM file object used to construct the topology.

Raises:

ValueError – If the file format is not supported. Only the extensions .psf, .prmtop, and .parm7 are supported.

grandfep.utils.random_rotation_matrix()

Generate a random rotation matrix using Shoemake method.

Returns:

A 3x3 rotation matrix.

Return type:

np.ndarray

Examples

 1import numpy as np
 2import matplotlib.pyplot as plt
 3from grandfep import utils
 4def gen_random_vec():
 5    axis = np.random.normal(0, 1, 3)
 6    axis /= np.linalg.norm(axis)
 7    return axis
 8res_new = []
 9res_ref = []
10vec_init = gen_random_vec() # regardless of the initial vector, the rotated vector should have uniform distribution on x,y,z
11for i in range(100000):
12    rot_matrix = utils.random_rotation_matrix()
13    res_new.append(np.dot(rot_matrix, vec_init))
14    res_ref.append(gen_random_vec())
15res_new = np.array(res_new)
16res_ref = np.array(res_ref)
17fig, axes = plt.subplots(2, 3, dpi=300, figsize = (9,6))
18for res, ax_list in zip([res_new, res_ref], axes):
19    for i, ax in enumerate(ax_list):
20        ax.hist(res[:, i], orientation='horizontal', density=True)
grandfep.utils.random_rotation_matrix_protoms()

Copied from https://github.com/essex-lab/grand/blob/master/grand/utils.py. Possibly be wrong.

Returns:

A 3x3 rotation matrix.

Return type:

np.ndarray