GetConformerRMSMatrix#

nvmolkit.conformerRmsd.GetConformerRMSMatrix(
mol: Mol,
prealigned: bool = False,
stream: Stream | None = None,
*,
output_format: str = 'condensed',
alignment_mode: str = 'pairwise',
) → AsyncGpuResult | Tensor#

Compute the pairwise RMSD matrix between all conformers of a molecule on GPU.

For N conformers with M atoms, computes N*(N-1)/2 pairwise RMSD values using one GPU thread-block per pair. When prealigned is False (default), the alignment behavior is selected by alignment_mode.

The default alignment_mode="pairwise" optimally superimposes every pair independently via the Kabsch algorithm. Its alignment semantics are similar to using rdkit.Chem.rdMolAlign.GetAllConformerBestRMS() with an identity atom map and symmetrizeConjugatedTerminalGroups=False:

identity_map = [[(i, i) for i in range(mol.GetNumAtoms())]]
rdMolAlign.GetAllConformerBestRMS(
    mol,
    map=identity_map,
    symmetrizeConjugatedTerminalGroups=False,
)

alignment_mode="first-conformer" follows rdkit.Chem.AllChem.GetConformerRMSMatrix(): each conformer is aligned to conformer 0, then RMSDs are computed between those aligned coordinates. The input molecule is not modified in either mode.

Differences from RDKit:

  • Zero-atom molecules always raise ValueError regardless of conformer count. RDKit returns [nan] for exactly 2 zero-atom conformers and raises ZeroDivisionError for 3 or more.

  • Results are returned as an AsyncGpuResult (device tensor) rather than a Python list, to keep the conformer-selection pipeline on the GPU.

By default, the result matches RDKit’s condensed lower-triangle shape. Use output_format="square" when a downstream API expects an N x N distance matrix, such as nvmolkit.clustering.butina().

Parameters:
  • mol – RDKit molecule with two or more conformers. Strip hydrogens first (Chem.RemoveHs) if you want heavy-atom RMSD, as this function operates on all atoms present in the molecule.

  • prealigned – If True, skip Kabsch alignment and compute RMSD on raw coordinates. If False (default), use alignment_mode.

  • stream – CUDA stream to use. If None, uses the current stream.

  • output_format – "condensed" returns an AsyncGpuResult wrapping a 1-D RDKit-style lower-triangle vector. "square" returns a symmetric torch.Tensor of shape (N, N) on the GPU with zeros on the diagonal.

  • alignment_mode – "pairwise" (default) optimally aligns each conformer pair independently. "first-conformer" aligns every conformer to conformer 0 before computing pairwise RMSDs. Ignored when prealigned=True.

Returns:

With output_format="condensed", an AsyncGpuResult wrapping a 1-D tensor of shape (N*(N-1)/2,) containing RMSD values in lower-triangle condensed order. The RMSD for conformer pair (i, j) with i > j is at index i*(i-1)//2 + j.

With output_format="square", a symmetric CUDA torch.Tensor of shape (N, N).

Raises:
  • ValueError – If mol is None, has conformers but no atoms, or an unsupported alignment_mode is requested.

  • TypeError – If stream is not a torch.cuda.Stream or None.

Example

>>> from rdkit import Chem
>>> from rdkit.Chem import AllChem, rdDistGeom
>>> from nvmolkit.conformerRmsd import GetConformerRMSMatrix
>>> from nvmolkit.clustering import butina
>>>
>>> mol = Chem.AddHs(Chem.MolFromSmiles('CCCCCC'))
>>> rdDistGeom.EmbedMultipleConfs(mol, numConfs=50)
>>> no_h = Chem.RemoveHs(mol)
>>>
>>> # Independently align each conformer pair
>>> rmsd_matrix = GetConformerRMSMatrix(no_h)
>>> # Match AllChem.GetConformerRMSMatrix alignment semantics
>>> rdkit_style = GetConformerRMSMatrix(no_h, alignment_mode="first-conformer")
>>>
>>> # Request square output for GPU Butina clustering
>>> square = GetConformerRMSMatrix(no_h, output_format="square")
>>> clusters = butina(square, cutoff=0.5)