Beispiel #1
0
def set_position(mol: rdkit.Mol, position, conformer=-1):
    """
    Sets the centroid of the molecule to `position`.
    Parameters
    ----------
    position : :class:`numpy.array`
        This array holds the position on which the centroid of the
        molecule should be placed.
    conformer : :class:`int`, optional
        The id of the conformer to be used.
    Returns
    -------
    :class:`rdkit.Chem.rdchem.Mol`
        The ``rdkit`` molecule with the centroid placed at
        `position`. This is the same instance as that in
        :attr:`Molecule.mol`.
    """

    conf_id = mol.GetConformer(conformer).GetId()

    # Get the original centroid.
    centroid = get_centroid(mol, conf_id)
    # Find out how much it needs to shift to reach `position`.
    shift = position - centroid
    # Apply the shift and get the resulting rdkit conformer object.
    new_conf = apply_shift(mol, shift, conf_id).GetConformer()
    new_conf.SetId(conf_id)

    # Replace the old rkdit conformer with one where the centroid
    # is at `position`.
    mol.RemoveConformer(conf_id)
    mol.AddConformer(new_conf)

    return mol
Beispiel #2
0
def prune_last_conformer(
        mol: Chem.Mol, tfd_thresh: float,
        energies: List[float]) -> Tuple[Chem.Mol, List[float]]:
    """Prunes the last conformer of the molecule.

    If no conformers in `mol` have a TFD (Torsional Fingerprint Deviation) with the last conformer of less than `tfd_thresh`,
    the last conformer is kept. Otherwise, the lowest energy conformer with TFD less than `tfd_thresh` is kept and all other conformers
    are discarded.

    Parameters
    ----------
    mol : RDKit Mol
        The molecule to be pruned. The conformers in the molecule should be ordered by ascending energy.
    tfd_thresh : float
        The minimum threshold for TFD between conformers.
    energies : list of float
        A list of all the energies of the conformers in `mol`.

    Returns
    -------
    mol : RDKit Mol
        The updated molecule after pruning, with conformers sorted by ascending energy.
    energies : list of float
        A list of all the energies of the conformers in `mol` after pruning and sorting by ascending energy.
    """
    if tfd_thresh < 0 or mol.GetNumConformers() <= 1:
        return mol, energies

    idx = bisect.bisect(energies[:-1], energies[-1])
    tfd = TorsionFingerprints.GetTFDBetweenConformers(
        mol,
        range(0,
              mol.GetNumConformers() - 1), [mol.GetNumConformers() - 1],
        useWeights=False)
    tfd = np.array(tfd)

    # if lower energy conformer is within threshold, drop new conf
    if not np.all(tfd[:idx] >= tfd_thresh):
        energies = energies[:-1]
        mol.RemoveConformer(mol.GetNumConformers() - 1)
        return mol, energies
    else:
        keep = list(range(0, idx))
        keep.append(mol.GetNumConformers() - 1)
        keep += [
            x for x in range(idx,
                             mol.GetNumConformers() - 1)
            if tfd[x] >= tfd_thresh
        ]

        new = Chem.Mol(mol)
        new.RemoveAllConformers()
        for i in keep:
            conf = mol.GetConformer(i)
            new.AddConformer(conf, assignId=True)

        return new, [energies[i] for i in keep]