moleculekit.util module#

moleculekit.util.assertSameAsReferenceDir(compareDir, outdir='.')#

Check if files in refdir are present in the directory given as second argument AND their content matches.

Raise an exception if not.

moleculekit.util.boundingBox(mol, sel='all')#

Calculates the bounding box of a selection of atoms.

Parameters:
  • mol (Molecule) – The molecule containing the atoms

  • sel (str | ndarray) – Atom selection (string, boolean mask, or integer index array) of atoms. See more here

Returns:

bbox – The bounding box around the atoms selected in sel.

Return type:

ndarray

Example

>>> boundingBox(mol, sel='chain A')
array([[-17.3390007 , -10.43700027,  -1.43900001],
       [ 25.40600014,  27.03800011,  46.46300125]], dtype=float32)
moleculekit.util.calculateAnglesAndDihedrals(bonds, cyclicdih=False)#

Calculate all angles and dihedrals from a set of bonds.

The bond graph is traversed to enumerate every angle (three connected atoms) and dihedral (four connected atoms). Each angle and dihedral is canonicalized so that an atom triplet/quadruplet and its reverse are not reported twice.

Parameters:
  • bonds (ndarray) – A 2D array of shape (N, 2) with the atom-index pairs defining the bonds.

  • cyclicdih (bool) – If True, dihedrals that close a ring (cyclic dihedrals) are also included. Default is False.

Returns:

  • angles (numpy.ndarray) – A 2D array of shape (N, 3) with the atom-index triplets of all angles.

  • dihedrals (numpy.ndarray) – A 2D array of shape (M, 4) with the atom-index quadruplets of all dihedrals.

moleculekit.util.check_port(port, host='127.0.0.1', timeout=120)#
moleculekit.util.ensurelist(tocheck, tomod=None)#

Convert np.ndarray and scalars to lists.

Lists and tuples are left as is. If a second argument is given, the type check is performed on the first argument, and the second argument is converted.

Parameters:
  • tocheck (object) – The value whose type determines the conversion. Numpy arrays and ranges are converted to lists; lists and tuples are returned unchanged; any other scalar is wrapped in a single-element list.

  • tomod (object, optional) – If given, the value that is actually converted/returned, while tocheck is only used for the type check. Defaults to tocheck.

Returns:

resulttomod converted to a list, or returned as-is if it is already a list or tuple.

Return type:

list or tuple

Examples

>>> ensurelist(3)
[3]
>>> ensurelist([1, 2, 3])
[1, 2, 3]
moleculekit.util.file_diff(file, reference)#
moleculekit.util.find_executable(execname)#
moleculekit.util.folder_diff(folder, reference, ignore_ftypes=('.log', '.txt'))#
moleculekit.util.guessAnglesAndDihedrals(bonds, cyclicdih=False)#

Calculate all angles and dihedrals from a set of bonds.

moleculekit.util.maxDistance(mol, sel='all', origin=None)#

Calculates the max distance of a set of atoms from an origin

Parameters:
  • mol (Molecule) – The molecule containing the atoms

  • sel (str | ndarray) – Atom selection (string, boolean mask, or integer index array) for atoms for which to calculate distances. See more here

  • origin (list | None) – The origin x,y,z coordinates

Returns:

maxd – The maximum distance in Angstrom

Return type:

float

Example

>>> y = maxDistance(mol, sel='protein', origin=[0, 0, 0])
>>> print(round(y,2))
48.39
moleculekit.util.molRMSD(mol, refmol, rmsdsel1, rmsdsel2)#

Calculates the RMSD between two Molecules

The two selections must pick the same number of atoms, in corresponding order, so that atom k of rmsdsel1 is compared against atom k of rmsdsel2. No alignment is performed; the RMSD is computed on the coordinates as given.

Parameters:
  • mol (Molecule) – The Molecule whose coordinates are compared against refmol.

  • refmol (Molecule) – The reference Molecule.

  • rmsdsel1 (ndarray) – Atom indices (or a boolean mask) selecting the atoms of mol to include.

  • rmsdsel2 (ndarray) – Atom indices (or a boolean mask) selecting the corresponding atoms of refmol.

Returns:

rmsd – The RMSD between the two structures. A scalar when both molecules have a single frame, otherwise an array with one value per frame.

Return type:

ndarray

moleculekit.util.natsorted(items)#
moleculekit.util.opm(pdbid, keep=False, keepaltloc='A', validateElements=True)#
moleculekit.util.orientOnAxes(mol, sel='all')#

Rotate a molecule so that its main axes are oriented along XYZ.

The calculation is based on the axes of inertia of the given selection, but masses will be ignored. After the operation, the main axis will be parallel to the Z axis, followed by Y and X (the shortest axis). Only the first frame is oriented. The reoriented molecule is returned.

Parameters:
  • mol (Molecule) – The Molecule to be rotated

  • sel (str | ndarray) – Atom selection (string, boolean mask, or integer index array) on which the rotation is computed. See more here

Return type:

Molecule

Examples

>>> mol = Molecule("1kdx")
>>> mol = orientOnAxes(mol,"chain B")
moleculekit.util.readCube(fname)#

Read 3D numpy array from CUBE file

Parameters:

fname (str) – CUBE file path

Returns:

  • data (numpy.ndarray) – 3D numpy array with the volumetric data

  • meta (dict) – Dictionary with the metadata of the CUBE file

moleculekit.util.rotationMatrix(axis, theta)#

Produces a rotation matrix given an axis and radians

Return the rotation matrix associated with counterclockwise rotation about the given axis by theta radians.

Parameters:
  • axis (list | ndarray) – The axis around which to rotate

  • theta (float) – The rotation angle in radians

Returns:

M – The rotation matrix.

Return type:

ndarray

Examples

>>> M = rotationMatrix([0, 0, 1], 1.5708)
>>> M.round(4)
array([[-0., -1.,  0.],
       [ 1., -0.,  0.],
       [ 0.,  0.,  1.]])
>>> axis = [4.0, 4., 1.]
>>> theta = 1.2
>>> v = [3.0, 5., 0.]
>>> np.dot(rotationMatrix(axis, theta), v).round(2)
array([ 2.75,  4.77,  1.92])
moleculekit.util.rotation_matrix_from_vectors(vec1, vec2)#

Find the rotation matrix that aligns vec1 to vec2

Taken from: https://stackoverflow.com/a/67767180

Parameters:
  • vec1 (ndarray) – A 3d “source” vector

  • vec2 (ndarray) – A 3d “destination” vector

Returns:

mat – A transform matrix (3x3) which when applied to vec1, aligns it with vec2.

Return type:

ndarray

moleculekit.util.sequenceID(field, prepend=None, step=1, return_idx=False)#

Array of integers which increments at value change of another array

Parameters:
  • field (ndarray | tuple) – An array of values. Once a change in value happens, a new ID will be created in seq. If a tuple of ndarrays is passed, a change in any of them will cause an increase in seq.

  • prepend (str | None) – A string to prepend to the incremental sequence

  • step (int) – The step size for incremeting the ID

  • return_idx (bool) – If set to True, the method will return the indices of the unique values

Returns:

  • seq (numpy.ndarray) – An array of equal size to field containing integers which increment every time there is a change in field

  • idx (numpy.ndarray) – A list of arrays, each containing the indices corresponding to the unique values in seq

Examples

>>> # A change in resid, insertion, chain or segid will cause an increase in the sequence
>>> sequenceID((mol.resid, mol.insertion, mol.chain, mol.segid))
array([  1,   1,   1, ..., 285, 286, 287])
>>> # it is typically used to renumber resids as follows
>>> mol.set('resid', sequenceID((mol.resid, mol.insertion, mol.chain, mol.segid)))
moleculekit.util.string_to_tempfile(content, ext)#
moleculekit.util.tempname(suffix='', create=False)#
moleculekit.util.uniformRandomRotation()#

Return a uniformly distributed rotation 3 x 3 matrix

The initial description of the calculation can be found in the section 5 of “How to generate random matrices from the classical compact groups” of Mezzadri (PDF: https://arxiv.org/pdf/math-ph/0609050.pdf; arXiv:math-ph/0609050; and NOTICES of the AMS, Vol. 54 (2007), 592-604). Sample code is provided in that section as the haar_measure function.

Apparently this code can randomly provide flipped molecules (chirality-wise), so a fix found in tmadl/sklearn-random-rotation-ensembles was applied.

This function takes no parameters.

Returns:

M – A uniformly distributed rotation matrix of shape (3, 3) with determinant +1.

Return type:

ndarray

moleculekit.util.wait_for_port(port, host='127.0.0.1', timeout=240.0, _logger=False)#

Wait until a port starts accepting TCP connections.

Parameters:
  • port (int) – Port number.

  • host (str) – Host address on which the port should exist.

  • timeout (float) – In seconds. How long to wait before raising errors.

Raises:

TimeoutError – The port isn’t accepting connections after the time specified in timeout.

moleculekit.util.writeCube(arr, filename, vecMin, vecRes)#

Writes 3D array to cube file

Parameters:
  • arr (ndarray) – 3D array with volumetric data.

  • filename (str) – string with the filename of the cubefile

  • vecMin (ndarray) – 3D vector denoting the minimal corner of the grid

  • vecRes (ndarray) – 3D vector denoting the resolution of the grid in each dimension in Angstrom

moleculekit.util.writeVoxels(arr, filename, vecMin, vecRes)#

DEPRECACTED: Use writeCube instead