Skip to content

Center

Module for centering molecular structures.

center_structure(u)

Centers an MDAnalysis Universe object by shifting its center of mass to the origin.

Calculates the center of mass of all atoms in the provided MDAnalysis Universe and then subtracts this center of mass from the coordinates of each atom, effectively placing the system's center of mass at the coordinates (0.0, 0.0, 0.0).

PARAMETER DESCRIPTION
u

The MDAnalysis Universe object representing the molecular system to be centered.

TYPE: Universe

RETURNS DESCRIPTION
Universe

The same MDAnalysis Universe object, but with the coordinates of all

Universe

atoms shifted so that the center of mass is at the origin.

run_center_structure(path_topo, path_coords=None, path_output=None, kwargs_universe=dict(), kwargs_writer=dict(), overwrite=False)

Loads a molecular structure, centers it by its center of mass, and optionally saves the centered structure to a new file.

This function orchestrates the process of reading a molecular structure using MDAnalysis, centering it using the [center_structure][simlify.structure.center.center_structure] function, and then writing the centered coordinates to a specified output file.

PARAMETER DESCRIPTION
path_topo

The path to the topology file of the molecular system. Common file formats include PDB, GRO, and TPR.

TYPE: str

path_coords

The path to the coordinate file(s) of the molecular system. This can be a single file path (e.g., a trajectory file like TRR or XTC), a list of file paths, or None if the topology file already contains coordinate information (as is the case for some PDB files).

TYPE: str | list[str] | None DEFAULT: None

path_output

The path to the output file where the centered structure will be saved. If this argument is None, the centered structure will not be written to a file. The file format is typically inferred from the file extension (e.g., .pdb, .gro).

TYPE: str | None DEFAULT: None

kwargs_universe

A dictionary of keyword arguments that will be passed to the [load_mda][simlify.structure.io.load_mda] function when creating the MDAnalysis Universe object. This allows for customization of how the structure is loaded (e.g., specifying atom names or residue numbers).

TYPE: dict[str, Any] DEFAULT: dict()

kwargs_writer

A dictionary of keyword arguments that will be passed to the [write_mda][simlify.structure.io.write_mda] function when saving the centered structure. This can be used to control the output format or other writing options.

TYPE: dict[str, Any] DEFAULT: dict()

overwrite

A boolean flag indicating whether to overwrite the output file if it already exists. If True, the existing file will be overwritten. If False and the file exists, an error might be raised depending on the write_mda function's behavior.

TYPE: bool DEFAULT: False

RETURNS DESCRIPTION
Universe

The MDAnalysis Universe object representing the molecular system after

Universe

it has been centered by its center of mass.

Examples:

Centering a PDB file and saving the result:

from simlify.structure.center import run_center_structure

centered_universe = run_center_structure(
    path_topo="input.pdb",
    path_output="centered.pdb",
    overwrite=True,
)
print(f"Centered universe has {centered_universe.atoms.n_atoms} atoms.")

Centering a topology and trajectory file without saving:

from simlify.structure.center import run_center_structure

centered_universe = run_center_structure(
    path_topo="topol.gro",
    path_coords="traj.xtc",
)
print(
    f"Center of mass of the centered universe: {centered_universe.atoms.center_of_mass()}"
)

Passing additional arguments to the MDAnalysis Universe constructor:

from simlify.structure.center import run_center_structure

centered_universe = run_center_structure(
    path_topo="protein.pdb",
    kwargs_universe={"atom_name_column": "name"},
    path_output="centered_protein.pdb",
)