def setUp(self):
     self.lfh = sys.stderr
     self.verbose = False
     self.pathPdbxDataFile = os.path.join(home(dataDir='molecule-readers'),
                                          "1kip.cif")
     self.pathOutputFile = tempname(suffix='.cif')
Exemple #2
0
# (c) 2015-2018 Acellera Ltd http://www.acellera.com
# All Rights Reserved
# Distributed under HTMD Software License Agreement
# No redistribution in whole or part
#
import numpy as np
import ctypes as ct
import os
from moleculekit.home import home
import platform
import tempfile
import logging

logger = logging.getLogger(__name__)

libdir = home(libDir=True)
if platform.system() == "Windows":
    tmalignlib = ct.cdll.LoadLibrary(os.path.join(libdir, "tmalign.dll"))
else:
    tmalignlib = ct.cdll.LoadLibrary(os.path.join(libdir, "tmalign.so"))


def tempname(suffix='', create=False):
    if create:
        file = tempfile.NamedTemporaryFile(delete=False, suffix=suffix)
    else:
        file = tempfile.NamedTemporaryFile(delete=True, suffix=suffix)
    file.close()
    return file.name

Exemple #3
0
def pp_calcMinDistances(mol,
                        sel1,
                        sel2,
                        periodic,
                        metric="distances",
                        threshold=8,
                        gap=1,
                        truncate=None):
    import os
    import ctypes
    from moleculekit.home import home

    # Converting non-grouped boolean atomselection to group-style atomselections
    if np.ndim(sel1) != 2:
        sel1idx = tuple(np.where(sel1)[0])
        sel1 = np.zeros((len(sel1idx), len(sel1)), dtype=bool)
        sel1[range(sel1.shape[0]), sel1idx] = True
    if np.ndim(sel2) != 2:
        sel2idx = tuple(np.where(sel2)[0])
        sel2 = np.zeros((len(sel2idx), len(sel2)), dtype=bool)
        sel2[range(sel2.shape[0]), sel2idx] = True

    coords = mol.coords
    box = mol.box
    if periodic is not None:
        if box is None or np.sum(box) == 0:
            raise RuntimeError(
                "No periodic box dimensions given in the molecule/trajectory. "
                "If you want to calculate distance without wrapping, set the `periodic` option to None"
            )
    else:
        box = np.zeros((3, coords.shape[2]), dtype=np.float32)

    if box.shape[1] != coords.shape[2]:
        raise RuntimeError(
            "Different number of frames in mol.coords and mol.box. "
            "Please ensure they both have the same number of frames")

    # Converting from 2D boolean atomselect array to 2D int array where each row starts with the indexes of the boolean
    groups1 = np.ones((sel1.shape[0], mol.numAtoms), dtype=np.int32) * -1
    groups2 = np.ones((sel2.shape[0], mol.numAtoms), dtype=np.int32) * -1
    for i in range(sel1.shape[0]):
        idx = np.where(sel1[i, :])[0]
        groups1[i, 0:len(idx)] = idx
    for i in range(sel2.shape[0]):
        idx = np.where(sel2[i, :])[0]
        groups2[i, 0:len(idx)] = idx

    selfdist = np.array_equal(sel1, sel2)

    # Digitize chains to not do PBC calculations of the same chain
    if periodic is None:  # Won't be used since box is 0
        digitized_chains = np.zeros(mol.numAtoms, dtype=np.int32)
    elif periodic == "chains":
        digitized_chains = np.unique(mol.chain,
                                     return_inverse=True)[1].astype(np.int32)
    elif periodic == "selections":
        digitized_chains = np.ones(mol.numAtoms, dtype=np.int32)
        for i in range(sel2.shape[0]):
            digitized_chains[sel2[i, :]] = 2

    # Running the actual calculations
    lib = ctypes.cdll.LoadLibrary(
        os.path.join(home(libDir=True), "mindist_ext.so"))
    mindist = np.zeros((mol.numFrames, len(groups1) * len(groups2)),
                       dtype=np.float32)  # Preparing the return array
    if selfdist:
        mindist = np.zeros(
            (mol.numFrames, int((len(groups1) * (len(groups2) - 1)) / 2)),
            dtype=np.float32,
        )

    # import time
    # t = time.time()
    lib.mindist_trajectory(
        coords.ctypes.data_as(ctypes.POINTER(ctypes.c_float)),
        box.ctypes.data_as(ctypes.POINTER(ctypes.c_float)),
        groups1.ctypes.data_as(ctypes.POINTER(ctypes.c_int)),
        groups2.ctypes.data_as(ctypes.POINTER(ctypes.c_int)),
        digitized_chains.ctypes.data_as(ctypes.POINTER(ctypes.c_int)),
        ctypes.c_int(len(groups1)),
        ctypes.c_int(len(groups2)),
        ctypes.c_int(mol.numAtoms),
        ctypes.c_int(mol.numFrames),
        ctypes.c_int(int(periodic is not None)),
        ctypes.c_int(int(selfdist)),
        mindist.ctypes.data_as(ctypes.POINTER(ctypes.c_float)),
    )
    # print(time.time() - t)

    if truncate is not None:
        mindist[mindist > truncate] = truncate

    if metric == "contacts":
        mindist = mindist <= threshold
    elif metric == "distances":
        pass
    else:
        raise RuntimeError(
            "The metric you asked for is not supported. Check spelling and documentation"
        )
    return mindist
Exemple #4
0
    def setUpClass(self):
        from moleculekit.molecule import Molecule

        self.moldiala = Molecule(
            os.path.join(home(dataDir='pdb'), 'alanine.pdb'))
Exemple #5
0
def pp_calcDistances(mol,
                     sel1,
                     sel2,
                     periodic,
                     metric="distances",
                     threshold=8,
                     gap=1,
                     truncate=None):
    import os
    import ctypes
    from moleculekit.home import home

    selfdist = np.array_equal(sel1, sel2)

    sel1 = np.where(sel1)[0].astype(np.int32)
    sel2 = np.where(sel2)[0].astype(np.int32)

    coords = mol.coords
    box = mol.box
    if periodic is not None:
        if box is None or np.sum(box) == 0:
            raise RuntimeError(
                "No periodic box dimensions given in the molecule/trajectory. "
                "If you want to calculate distance without wrapping, set the periodic option to `None`"
            )
    else:
        box = np.zeros((3, coords.shape[2]), dtype=np.float32)

    if box.shape[1] != coords.shape[2]:
        raise RuntimeError(
            "Different number of frames in mol.coords and mol.box. "
            "Please ensure they both have the same number of frames")

    # Digitize chains to not do PBC calculations of the same chain
    if periodic is None:  # Won't be used since box is 0
        digitized_chains = np.zeros(mol.numAtoms, dtype=np.int32)
    elif periodic == "chains":
        digitized_chains = np.unique(mol.chain,
                                     return_inverse=True)[1].astype(np.int32)
    elif periodic == "selections":
        digitized_chains = np.ones(mol.numAtoms, dtype=np.int32)
        digitized_chains[sel2] = 2

    # Running the actual calculations
    lib = ctypes.cdll.LoadLibrary(
        os.path.join(home(libDir=True), "dist_ext.so"))
    shape = (mol.numFrames, len(sel1) * len(sel2))
    if selfdist:
        shape = (mol.numFrames, int((len(sel1) * (len(sel2) - 1)) / 2))
    results = np.zeros(shape, dtype=np.float32)

    lib.dist_trajectory(
        coords.ctypes.data_as(ctypes.POINTER(ctypes.c_float)),
        box.ctypes.data_as(ctypes.POINTER(ctypes.c_float)),
        sel1.ctypes.data_as(ctypes.POINTER(ctypes.c_int)),
        sel2.ctypes.data_as(ctypes.POINTER(ctypes.c_int)),
        digitized_chains.ctypes.data_as(ctypes.POINTER(ctypes.c_int)),
        ctypes.c_int(len(sel1)),
        ctypes.c_int(len(sel2)),
        ctypes.c_int(mol.numAtoms),
        ctypes.c_int(mol.numFrames),
        ctypes.c_int(int(periodic is not None)),
        ctypes.c_int(int(selfdist)),
        results.ctypes.data_as(ctypes.POINTER(ctypes.c_float)),
    )

    if truncate is not None:
        results[results > truncate] = truncate

    if metric == "contacts":
        results = results <= threshold
    elif metric == "distances":
        pass
    else:
        raise RuntimeError(
            "The metric you asked for is not supported. Check spelling and documentation"
        )
    return results
Exemple #6
0
 def setUp(self):
     self.dataDir = home("test-smallmol")
     self.benzamidine_mol2 = os.path.join(self.dataDir, "benzamidine.mol2")
     self.indole_mol2 = os.path.join(self.dataDir, "indole.mol2")
Exemple #7
0
            return rep

        rep = 'SmallMol with {} atoms and {} conformers'.format(
            self.numAtoms, self.numFrames)
        for p in sorted(self._atom_fields):
            if p.startswith('_'):
                continue
            rep += '\n'
            rep += 'Atom field - {}'.format(p)
        for j in sorted(self.__dict__.keys() - list(SmallMol._atom_fields)):
            if j[0] == '_':
                continue
            rep += '\n'
            rep += formatstr(j, self.__dict__[j])

        return rep


if __name__ == '__main__':

    import doctest
    import os
    from moleculekit.home import home
    from moleculekit.smallmol.smallmollib import SmallMolLib

    lib = SmallMolLib(
        os.path.join(home(dataDir='test-smallmol'), 'fda_drugs_light.sdf'))
    sm = SmallMol(
        os.path.join(home(dataDir='test-smallmol'), 'benzamidine.mol2'))
    doctest.testmod(extraglobs={'lib': lib.copy(), 'sm': sm.copy()})
Exemple #8
0
                rep = f"{name}: {len(self.reps.replist)}"
            else:
                rep = f"{name}: {field}"
            return rep

        rep = f"SmallMol with {self.numAtoms} atoms and {self.numFrames} conformers"
        for p in sorted(self._atom_fields):
            if p.startswith("_"):
                continue
            rep += "\n"
            rep += f"Atom field - {p}"
        for j in sorted(self.__dict__.keys() - list(SmallMol._atom_fields)):
            if j[0] == "_":
                continue
            rep += "\n"
            rep += formatstr(j, self.__dict__[j])

        return rep


if __name__ == "__main__":
    import doctest
    from moleculekit.home import home
    from moleculekit.smallmol.smallmollib import SmallMolLib

    lib = SmallMolLib(
        os.path.join(home(dataDir="test-smallmol"), "fda_drugs_light.sdf"))
    sm = SmallMol(
        os.path.join(home(dataDir="test-smallmol"), "benzamidine.mol2"))
    doctest.testmod(extraglobs={"lib": lib.copy(), "sm": sm.copy()})
Exemple #9
0
    _, _, equivalent_group_by_atom = detectEquivalentAtoms(molecule)
    dihedral_groups = [
        tuple([equivalent_group_by_atom[atom] for atom in dihedral])
        for dihedral in dihedrals
    ]

    # Group equivalent dihedral angles and reverse them that equivalent atoms are matched
    equivalent_dihedrals = OrderedDict()
    for dihedral, groups in zip(dihedrals, dihedral_groups):
        dihedral, groups = ((dihedral[::-1],
                             groups[::-1]) if groups[::-1] < groups else
                            (dihedral, groups))
        equivalent_dihedrals[groups] = sorted(
            equivalent_dihedrals.get(groups, []) + [dihedral])
    equivalent_dihedrals = sorted(equivalent_dihedrals.values())
    return equivalent_dihedrals


if __name__ == "__main__":

    import sys
    import doctest

    # Prevent HTMD importing inside doctest to fail if importing gives text output
    from moleculekit.home import home

    home()

    if doctest.testmod().failed:
        sys.exit(1)