Skip to content

calculate_rmsd

esta.general.calculate_rmsd

ReorderCallable

Bases: Protocol

__call__(p_atoms: ndarray, q_atoms: ndarray, p_coord: ndarray, q_coord: ndarray, **kwargs: Any) -> ndarray

Protocol for a reorder callable function

Return: ndarray dtype=int # Array of indices

RmsdCallable

Bases: Protocol

__call__(P: ndarray, Q: ndarray, **kwargs: Any) -> float

Protocol for a rotation callable function

return: RMSD after rotation

str_atom(atom: int) -> str

Convert atom type from integer to string

Parameters:

  • atoms (string) –

Returns:

  • atoms ( integer ) –

int_atom(atom: str) -> int

Convert atom type from string to integer

Parameters:

  • atoms (string) –

Returns:

  • atoms ( integer ) –

rmsd(P: ndarray, Q: ndarray, **kwargs) -> float

Calculate Root-mean-square deviation from two sets of vectors V and W.

Parameters:

  • V (array) –

    (N,D) matrix, where N is points and D is dimension.

  • W (array) –

    (N,D) matrix, where N is points and D is dimension.

Returns:

  • rmsd ( float ) –

    Root-mean-square deviation between the two vectors

kabsch_rmsd(P: ndarray, Q: ndarray, W: Optional[ndarray] = None, translate: bool = False, **kwargs: Any) -> float

Rotate matrix P unto Q using Kabsch algorithm and calculate the RMSD. An optional vector of weights W may be provided.

Parameters:

  • P (array) –

    (N,D) matrix, where N is points and D is dimension.

  • Q (array) –

    (N,D) matrix, where N is points and D is dimension.

  • W (array or None, default: None ) –

    (N) vector, where N is points.

  • translate (bool, default: False ) –

    Use centroids to translate vector P and Q unto each other.

Returns:

  • rmsd ( float ) –

    root-mean squared deviation

kabsch_rotate(P: ndarray, Q: ndarray) -> ndarray

Rotate matrix P unto matrix Q using Kabsch algorithm.

Parameters:

  • P (array) –

    (N,D) matrix, where N is points and D is dimension.

  • Q (array) –

    (N,D) matrix, where N is points and D is dimension.

Returns:

  • P ( array ) –

    (N,D) matrix, where N is points and D is dimension, rotated

kabsch_fit(P: ndarray, Q: ndarray, W: Optional[ndarray] = None) -> ndarray

Rotate and translate matrix P unto matrix Q using Kabsch algorithm. An optional vector of weights W may be provided.

Parameters:

  • P (array) –

    (N,D) matrix, where N is points and D is dimension.

  • Q (array) –

    (N,D) matrix, where N is points and D is dimension.

  • W (array or None, default: None ) –

    (N) vector, where N is points.

Returns:

  • P ( array ) –

    (N,D) matrix, where N is points and D is dimension, rotated and translated.

kabsch(P: ndarray, Q: ndarray) -> ndarray

Using the Kabsch algorithm with two sets of paired point P and Q, centered around the centroid. Each vector set is represented as an NxD matrix, where D is the the dimension of the space. The algorithm works in three steps: - a centroid translation of P and Q (assumed done before this function call) - the computation of a covariance matrix C - computation of the optimal rotation matrix U For more info see http://en.wikipedia.org/wiki/Kabsch_algorithm

Parameters:

  • P (array) –

    (N,D) matrix, where N is points and D is dimension.

  • Q (array) –

    (N,D) matrix, where N is points and D is dimension.

Returns:

  • U ( matrix ) –

    Rotation matrix (D,D)

kabsch_weighted(P: ndarray, Q: ndarray, W: Optional[ndarray] = None) -> Tuple[ndarray, ndarray, float]

Using the Kabsch algorithm with two sets of paired point P and Q. Each vector set is represented as an NxD matrix, where D is the dimension of the space. An optional vector of weights W may be provided.

Note that this algorithm does not require that P and Q have already been overlayed by a centroid translation.

The function returns the rotation matrix U, translation vector V, and RMS deviation between Q and P', where P' is:

P' = P * U + V

For more info see http://en.wikipedia.org/wiki/Kabsch_algorithm

Parameters:

  • P (array) –

    (N,D) matrix, where N is points and D is dimension.

  • Q (array) –

    (N,D) matrix, where N is points and D is dimension.

  • W (array or None, default: None ) –

    (N) vector, where N is points.

Returns:

  • U ( matrix ) –

    Rotation matrix (D,D)

  • V ( vector ) –

    Translation vector (D)

  • RMSD ( float ) –

    Root mean squared deviation between P and Q

kabsch_weighted_fit(P: ndarray, Q: ndarray, W: Optional[ndarray] = None, return_rmsd: bool = False) -> Tuple[ndarray, Optional[float]]

Fit P to Q with optional weights W. Also returns the RMSD of the fit if return_rmsd=True.

Parameters:

  • P (ndarray) –

    (N,D) matrix, where N is points and D is dimension.

  • Q (ndarray) –

    (N,D) matrix, where N is points and D is dimension.

  • W (Optional[ndarray], default: None ) –

    (N) vector, where N is points

  • rmsd (Bool) –

    If True, rmsd is returned as well as the fitted coordinates.

Returns:

  • P' : array

    (N,D) matrix, where N is points and D is dimension.

  • RMSD ( float ) –

    if the function is called with rmsd=True

kabsch_weighted_rmsd(P: ndarray, Q: ndarray, W: Optional[ndarray] = None) -> float

Calculate the RMSD between P and Q with optional weights W

Parameters:

  • P (array) –

    (N,D) matrix, where N is points and D is dimension.

  • Q (array) –

    (N,D) matrix, where N is points and D is dimension.

  • W (vector, default: None ) –

    (N) vector, where N is points

Returns:

  • RMSD ( float ) –

quaternion_rmsd(P: ndarray, Q: ndarray, **kwargs: Any) -> float

Rotate matrix P unto Q and calculate the RMSD based on doi:10.1016/1049-9660(91)90036-O

Parameters:

  • P (array) –

    (N,D) matrix, where N is points and D is dimension.

  • Q (array) –

    (N,D) matrix, where N is points and D is dimension.

Returns:

  • rmsd ( float ) –

quaternion_transform(r: ndarray) -> ndarray

Get optimal rotation note: translation will be zero when the centroids of each molecule are the same

makeW(r1: float, r2: float, r3: float, r4: float = 0) -> ndarray

matrix involved in quaternion rotation

makeQ(r1: float, r2: float, r3: float, r4: float = 0) -> ndarray

matrix involved in quaternion rotation

quaternion_rotate(X: ndarray, Y: ndarray) -> ndarray

Calculate the rotation

Parameters:

  • X (array) –

    (N,D) matrix, where N is points and D is dimension.

  • Y (ndarray) –

    (N,D) matrix, where N is points and D is dimension.

Returns:

  • rot ( matrix ) –

    Rotation matrix (D,D)

centroid(X: ndarray) -> ndarray

Centroid is the mean position of all the points in all of the coordinate directions, from a vectorset X.

https://en.wikipedia.org/wiki/Centroid

C = sum(X)/len(X)

Parameters:

  • X (array) –

    (N,D) matrix, where N is points and D is dimension.

Returns:

  • C ( ndarray ) –

    centroid

hungarian_vectors(p_vecs: ndarray, q_vecs: ndarray, sigma: float = 1.0, use_kernel: bool = True) -> ndarray

Hungarian cost assignment of a similiarty molecule kernel.

Note: Assumes p and q are atoms of same type

Parameters:

  • p_vecs (array) –

    (N,L) matrix, where N is no. of atoms and L is representation length

  • q_vecs (array) –

    (N,L) matrix, where N is no. of atoms and L is representation length

Returns:

  • indices_b ( array ) –

    (N) view vector of reordered assignment

reorder_similarity(p_atoms: ndarray, q_atoms: ndarray, p_coord: ndarray, q_coord: ndarray, use_kernel: bool = True, **kwargs: Any) -> ndarray

Re-orders the input atom list and xyz coordinates using QML similarity the Hungarian method for assignment.

Parameters:

  • p_atoms (array) –

    (N,1) matrix, where N is points holding the atoms' names

  • p_atoms (array) –

    (N,1) matrix, where N is points holding the atoms' names

  • p_coord (array) –

    (N,D) matrix, where N is points and D is dimension

  • q_coord (array) –

    (N,D) matrix, where N is points and D is dimension

Returns:

  • view_reorder ( array ) –

    (N,1) matrix, reordered indexes of atom alignment based on the coordinates of the atoms

reorder_distance(p_atoms: ndarray, q_atoms: ndarray, p_coord: ndarray, q_coord: ndarray, **kwargs: Any) -> ndarray

Re-orders the input atom list and xyz coordinates by atom type and then by distance of each atom from the centroid.

Parameters:

  • atoms (array) –

    (N,1) matrix, where N is points holding the atoms' names

  • coord (array) –

    (N,D) matrix, where N is points and D is dimension

Returns:

  • atoms_reordered ( array ) –

    (N,1) matrix, where N is points holding the ordered atoms' names

  • coords_reordered ( array ) –

    (N,D) matrix, where N is points and D is dimension (rows re-ordered)

hungarian(A: ndarray, B: ndarray) -> ndarray

Hungarian reordering.

Assume A and B are coordinates for atoms of SAME type only

reorder_hungarian(p_atoms: ndarray, q_atoms: ndarray, p_coord: ndarray, q_coord: ndarray, **kwargs: Any) -> ndarray

Re-orders the input atom list and xyz coordinates using the Hungarian method (using optimized column results)

Parameters:

  • p_atoms (array) –

    (N,1) matrix, where N is points holding the atoms' names

  • p_atoms (array) –

    (N,1) matrix, where N is points holding the atoms' names

  • p_coord (array) –

    (N,D) matrix, where N is points and D is dimension

  • q_coord (array) –

    (N,D) matrix, where N is points and D is dimension

Returns:

  • view_reorder ( array ) –

    (N,1) matrix, reordered indexes of atom alignment based on the coordinates of the atoms

reorder_inertia_hungarian(p_atoms: ndarray, q_atoms: ndarray, p_coord: ndarray, q_coord: ndarray, **kwargs: Any) -> ndarray

Align the principal intertia axis and then re-orders the input atom list and xyz coordinates using the Hungarian method (using optimized column results)

Parameters:

  • p_atoms (array) –

    (N,1) matrix, where N is points holding the atoms' names

  • p_atoms (array) –

    (N,1) matrix, where N is points holding the atoms' names

  • p_coord (array) –

    (N,D) matrix, where N is points and D is dimension

  • q_coord (array) –

    (N,D) matrix, where N is points and D is dimension

Returns:

  • view_reorder ( array ) –

    (N,1) matrix, reordered indexes of atom alignment based on the coordinates of the atoms

generate_permutations(elements: List[int], n: int) -> Iterator[List[int]]

Heap's algorithm for generating all n! permutations in a list https://en.wikipedia.org/wiki/Heap%27s_algorithm

brute_permutation(A: ndarray, B: ndarray) -> ndarray

Re-orders the input atom list and xyz coordinates using the brute force method of permuting all rows of the input coordinates

Parameters:

  • A (array) –

    (N,D) matrix, where N is points and D is dimension

  • B (array) –

    (N,D) matrix, where N is points and D is dimension

Returns:

  • view ( array ) –

    (N,1) matrix, reordered view of B projected to A

reorder_brute(p_atoms: ndarray, q_atoms: ndarray, p_coord: ndarray, q_coord: ndarray, **kwargs: Any) -> ndarray

Re-orders the input atom list and xyz coordinates using all permutation of rows (using optimized column results)

Parameters:

  • p_atoms (array) –

    (N,1) matrix, where N is points holding the atoms' names

  • q_atoms (array) –

    (N,1) matrix, where N is points holding the atoms' names

  • p_coord (array) –

    (N,D) matrix, where N is points and D is dimension

  • q_coord (array) –

    (N,D) matrix, where N is points and D is dimension

Returns:

  • view_reorder ( array ) –

    (N,1) matrix, reordered indexes of atom alignment based on the coordinates of the atoms

check_reflections(p_atoms: ndarray, q_atoms: ndarray, p_coord: ndarray, q_coord: ndarray, reorder_method: Optional[ReorderCallable] = None, rmsd_method: RmsdCallable = kabsch_rmsd, keep_stereo: bool = False) -> Tuple[float, ndarray, ndarray, ndarray]

Minimize RMSD using reflection planes for molecule P and Q

Warning: This will affect stereo-chemistry

Parameters:

  • p_atoms (array) –

    (N,1) matrix, where N is points holding the atoms' names

  • q_atoms (array) –

    (N,1) matrix, where N is points holding the atoms' names

  • p_coord (array) –

    (N,D) matrix, where N is points and D is dimension

  • q_coord (array) –

    (N,D) matrix, where N is points and D is dimension

Returns:

  • min_rmsd
  • min_swap
  • min_reflection
  • min_review

rotation_matrix_vectors(v1: ndarray, v2: ndarray) -> ndarray

Returns the rotation matrix that rotates v1 onto v2 using Rodrigues' rotation formula.

(see https://math.stackexchange.com/a/476311)

v1 : array Dim 3 float array v2 : array Dim 3 float array

Return

output : 3x3 matrix Rotation matrix

get_cm(atoms: ndarray, V: ndarray) -> ndarray

Get the center of mass of V.

atoms : list List of atomic types V : array (N,3) matrix of atomic coordinates

Return

output : (3) array The CM vector

get_inertia_tensor(atoms: ndarray, V: ndarray) -> ndarray

Get the tensor of intertia of V.

atoms : list List of atomic types V : array (N,3) matrix of atomic coordinates

Return

output : 3x3 float matrix The tensor of inertia

get_principal_axis(atoms: ndarray, V: ndarray) -> ndarray

Get the molecule's principal axis.

atoms : list List of atomic types V : array (N,3) matrix of atomic coordinates

Return

output : array Array of dim 3 containing the principal axis

set_coordinates(atoms: ndarray, V: ndarray, title: str = '', decimals: int = 8) -> str

Print coordinates V with corresponding atoms to stdout in XYZ format.

Parameters:

  • atoms (list) –

    List of atomic types

  • V (array) –

    (N,3) matrix of atomic coordinates

  • title (string(optional), default: '' ) –

    Title of molecule

  • decimals (int(optional), default: 8 ) –

    number of decimals for the coordinates

Return

output : str Molecule in XYZ format

get_coordinates(filename: Path, fmt: str, is_gzip: bool = False, return_atoms_as_int: bool = False) -> Tuple[ndarray, ndarray]

Get coordinates from filename in format fmt. Supports XYZ and PDB.

Parameters:

  • filename (string) –

    Filename to read

  • fmt (string) –

    Format of filename. Either xyz or pdb.

Returns:

  • atoms ( list ) –

    List of atomic types

  • V ( array ) –

    (N,3) where N is number of atoms

get_coordinates_pdb(filename: Path, is_gzip: bool = False, return_atoms_as_int: bool = False) -> Tuple[ndarray, ndarray]

Get coordinates from the first chain in a pdb file and return a vectorset with all the coordinates.

Parameters:

  • filename (string) –

    Filename to read

Returns:

  • atoms ( list ) –

    List of atomic types

  • V ( array ) –

    (N,3) where N is number of atoms

get_coordinates_xyz(filename: Path, is_gzip: bool = False, return_atoms_as_int: bool = False) -> Tuple[ndarray, ndarray]

Get coordinates from filename and return a vectorset with all the coordinates, in XYZ format.

Parameters:

  • filename (string) –

    Filename to read

Returns:

  • atoms ( list ) –

    List of atomic types

  • V ( array ) –

    (N,3) where N is number of atoms