示例#1
0
def translate(molecular_system,
              translation=None,
              selection='all',
              structure_indices='all',
              syntaxis='MolSysMT',
              in_place=False,
              check=True):

    from molsysmt.basic import get, set, select, copy, is_molecular_system

    if check:
        if not is_molecular_system(molecular_system):
            raise MolecularSystemNeededError()

    atom_indices = select(molecular_system,
                          selection=selection,
                          syntaxis=syntaxis)
    coordinates = get(molecular_system,
                      target='atom',
                      indices=atom_indices,
                      structure_indices=structure_indices,
                      coordinates=True)

    length_units = puw.get_unit(coordinates)
    coordinates = puw.get_value(coordinates)
    translation = puw.get_value(translation, to_unit=length_units)
    n_atoms = coordinates.shape[1]

    if type(translation) in [list, tuple]:
        translation = np.array(translation)

    if type(translation) == np.ndarray:
        if len(translation.shape) == 1:
            if translation.shape[0] == 3:
                translation = np.tile(translation, (n_atoms, 1))
            else:
                raise ValueError('Wrong shape of translation vector')
        elif len(translation.shape) == 2:
            if translation.shape[1] != 3:
                raise ValueError('Wrong shape of translation vector')
        elif len(translation.shape) == 3:
            if translation.shape[2] != 3:
                raise ValueError('Wrong shape of translation vector')
            if translation.shape[1] == 1:
                translation = np.tile(translation, (n_atoms, 1))

    coordinates += translation
    coordinates = coordinates * length_units

    if in_place:
        return set(molecular_system,
                   target='atom',
                   indices=atom_indices,
                   structure_indices=structure_indices,
                   coordinates=coordinates)
    else:
        tmp_molecular_system = copy(molecular_system)
        set(tmp_molecular_system,
            target='atom',
            indices=atom_indices,
            structure_indices=structure_indices,
            coordinates=coordinates)
        return tmp_molecular_system
示例#2
0
def set_dihedral_angles(molecular_system,
                        quartets=None,
                        angles=None,
                        blocks=None,
                        structure_indices='all',
                        pbc=True,
                        in_place=False,
                        engine='MolSysMT',
                        check=True):

    if check:
        from molsysmt.tools.molecular_system import is_molecular_system
        if not is_molecular_system(molecular_system):
            raise MolecularSystemNeededError()

    from molsysmt.topology.get_covalent_blocks import get_covalent_blocks

    structure_indices = digest_structure_indices(structure_indices)

    if type(quartets) in [list, tuple]:
        quartets = np.array(quartets, dtype=int)
    elif type(quartets) is np.ndarray:
        pass
    else:
        raise ValueError

    shape = quartets.shape

    if len(shape) == 1:
        if shape[0] == 4:
            quartets = quartets.reshape([1, 4])
        else:
            raise ValueError
    elif len(shape) == 2:
        if shape[1] != 4:
            raise ValueError
    else:
        raise ValueError

    n_atoms = get(molecular_system, target='system', n_atoms=True, check=False)
    n_quartets = quartets.shape[0]
    n_structures = get(molecular_system,
                       target='system',
                       structure_indices=structure_indices,
                       n_structures=True,
                       check=False)

    angles_units = puw.get_unit(angles)
    angles_value = puw.get_value(angles)

    if type(angles_value) in [float]:
        if (n_quartets == 1 and n_structures == 1):
            angles_value = np.array([[angles_value]], dtype=float)
        else:
            raise ValueError(
                "angles do not match the number of frames and quartets")
    elif type(angles_value) in [list, tuple]:
        angles_value = np.array(angles_value, dtype=float)
    elif type(angles_value) is np.ndarray:
        pass
    else:
        raise ValueError

    shape = angles_value.shape

    if len(shape) == 1:
        angles_value = angles_value.reshape([n_structures, n_quartets])

    angles = angles_value * angles_units

    if engine == 'MolSysMT':

        if blocks is None:

            blocks = []

            for quartet in quartets:

                tmp_blocks = get_covalent_blocks(
                    molecular_system,
                    remove_bonds=[quartet[1], quartet[2]],
                    check=False)
                blocks.append(tmp_blocks)

        coordinates = get(molecular_system,
                          target='system',
                          structure_indices=structure_indices,
                          coordinates=True,
                          check=False)

        if pbc:

            box, box_shape = get(molecular_system,
                                 target='system',
                                 structure_indices=structure_indices,
                                 box=True,
                                 box_shape=True,
                                 check=False)
            if box_shape is None:
                raise ValueError(
                    "The system has no PBC box. The input argument 'pbc' can not be True."
                )
            orthogonal = 0

            if box_shape is None:
                orthogonal = 1
            elif box_shape == 'cubic':
                orthogonal = 1

        else:

            orthogonal = 1
            box = np.zeros([n_structures, 3, 3]) * puw.unit('nm')

        length_units = puw.unit(coordinates)
        box = np.asfortranarray(puw.get_value(box, to_unit=length_units),
                                dtype='float64')
        coordinates = np.asfortranarray(puw.get_value(coordinates),
                                        dtype='float64')
        angles = np.asfortranarray(puw.get_value(angles), dtype='float64')

        aux_blocks = []
        aux_atoms_per_block = []

        for block in blocks:

            aux_atoms_per_block.append(len(block[1]))
            aux_blocks.append(list(block[1]))

        aux_blocks = np.ravel(aux_blocks)
        aux_atoms_per_block = np.array(aux_atoms_per_block, dtype=int)

        libgeometry.set_dihedral_angles(coordinates, box, orthogonal, int(pbc),
                                        quartets, angles, aux_blocks,
                                        aux_atoms_per_block, n_quartets,
                                        n_atoms, n_structures,
                                        aux_blocks.shape[0])

        coordinates = np.ascontiguousarray(coordinates) * length_units

        if in_place:
            return set(molecular_system,
                       target='system',
                       coordinates=coordinates,
                       structure_indices=structure_indices,
                       check=False)
        else:
            tmp_molecular_system = copy(molecular_system, check=False)
            set(tmp_molecular_system,
                target='system',
                coordinates=coordinates,
                structure_indices=structure_indices,
                check=False)
            return tmp_molecular_system

    else:

        raise NotImplementedError
示例#3
0
def wrap_to_pbc(molecular_system,
                selection='all',
                structure_indices='all',
                center='[0,0,0] nanometers',
                center_of_selection=None,
                weights_for_center=None,
                recenter=True,
                keep_covalent_bonds=False,
                syntaxis='MolSysMT',
                engine='MolSysMT',
                in_place=False):

    engine = digest_engine(engine)
    structure_indices = digest_structure_indices(structure_indices)

    if engine == 'MolSysMT':

        from molsysmt.basic import select, get, set, extract, copy

        atom_indices = select(molecular_system,
                              selection=selection,
                              syntaxis=syntaxis)

        coordinates = get(molecular_system,
                          target='atom',
                          indices=atom_indices,
                          coordinates=True)
        length_units = puw.get_unit(coordinates)
        n_structures = coordinates.shape[0]
        n_atoms = coordinates.shape[1]
        box, box_shape = get(molecular_system,
                             target='system',
                             structure_indices=structure_indices,
                             box=True,
                             box_shape=True)
        box = puw.convert(box, to_unit=length_units)

        orthogonal = 0
        if box_shape is None:
            raise ValueError(
                "The system has no PBC box. The input argument 'pbc' can not be True."
            )
        elif box_shape == 'cubic':
            orthogonal = 1

        if center_of_selection is not None:

            from molsysmt.structure import get_center
            center = get_center(molecular_system,
                                selection=center_of_selection,
                                weights=weights_for_center,
                                structure_indices=structure_indices,
                                syntaxis=syntaxis,
                                engine='MolSysMT')

            center = puw.convert(center, to_unit=length_units)
            center = puw.get_value(center)

        else:

            center = puw.quantity(center)
            center = puw.convert(center, to_unit=length_units)
            center = puw.get_value(center)

            center_shape = np.shape(center)
            if len(center_shape) == 1 and center_shape[-1] == 3:
                center = np.tile(center, [n_structures, 1, 1])
            elif len(center_shape) == 2 and center_shape[
                    -1] == 3 and center_shape[0] == n_structures:
                center = np.expand_dims(center, axis=1)
            elif len(center_shape
                     ) == 2 and center_shape[-1] == 3 and center_shape[0] == 1:
                center = np.tile(center[0], [n_structures, 1, 1])
            elif len(center_shape
                     ) == 3 and center_shape[-1] == 3 and center_shape[
                         0] == n_structures and center_shape[1] == 1:
                center = np.array(center)
            else:
                raise ValueError('center needs the right shape')

        box = np.asfortranarray(puw.get_value(box), dtype='float64')
        coordinates = np.asfortranarray(puw.get_value(coordinates),
                                        dtype='float64')
        center = np.asfortranarray(center, dtype='float64')

        libbox.wrap_pbc(coordinates, center, box, orthogonal, n_atoms,
                        n_structures)

        if recenter:
            translation = np.tile(-center, (n_atoms, 1))
            coordinates += translation

        coordinates = np.ascontiguousarray(coordinates) * length_units

    else:

        raise NotImpementedEngineError()

    if in_place:

        set(molecular_system,
            target='atom',
            indices=atom_indices,
            structure_indices=structure_indices,
            syntaxis=syntaxis,
            coordinates=coordinates)

        pass

    else:

        tmp_molecular_system = copy(molecular_system)
        set(tmp_molecular_system,
            target='atom',
            indices=atom_indices,
            structure_indices=structure_indices,
            syntaxis=syntaxis,
            coordinates=coordinates)

        return tmp_molecular_system
示例#4
0
文件: fit.py 项目: uibcdf/MolSysMT
def fit(molecular_system=None,
        selection='backbone',
        structure_indices='all',
        reference_molecular_system=None,
        reference_selection=None,
        reference_frame_index=0,
        to_form=None,
        parallel=True,
        syntaxis='MolSysMT',
        method='least rmsd',
        engine='MolSysMT',
        check=True):

    from molsysmt.basic import select, get, set, convert, copy, is_molecular_system

    if check:
        if not is_molecular_system(molecular_system):
            raise MolecularSystemNeededError()

    engine = digest_engine(engine)

    if engine == 'MolSysMT':

        n_atoms, n_structures = get(molecular_system,
                                    n_atoms=True,
                                    n_structures=True,
                                    check=False)
        atom_indices = select(molecular_system,
                              selection=selection,
                              syntaxis=syntaxis,
                              check=False)
        n_atom_indices = atom_indices.shape[0]
        structure_indices = digest_structure_indices(structure_indices)
        if structure_indices is 'all':
            structure_indices = np.arange(n_structures)
        n_structure_indices = structure_indices.shape[0]

        if reference_molecular_system is None:
            reference_molecular_system = molecular_system

        if reference_selection is None:
            reference_selection = selection

        reference_atom_indices = select(reference_molecular_system,
                                        selection=reference_selection,
                                        syntaxis=syntaxis,
                                        check=False)

        reference_coordinates = get(reference_molecular_system,
                                    target='atom',
                                    indices=reference_atom_indices,
                                    structure_indices=reference_frame_index,
                                    coordinates=True,
                                    check=False)

        coordinates = get(molecular_system,
                          coordinates=True,
                          structure_indices='all',
                          check=False)
        units = puw.get_unit(coordinates)
        coordinates = np.asfortranarray(puw.get_value(coordinates),
                                        dtype='float64')
        reference_coordinates = np.asfortranarray(puw.get_value(
            reference_coordinates, to_unit=units),
                                                  dtype='float64')

        if reference_coordinates.shape[1] != n_atom_indices:
            raise ValueError(
                "reference selection and selection needs to have the same number of atoms"
            )

        librmsd.least_rmsd_fit(coordinates, atom_indices,
                               reference_coordinates, structure_indices,
                               n_atoms, n_structures, n_atom_indices,
                               n_structure_indices)

        coordinates = np.ascontiguousarray(coordinates) * units
        coordinates = puw.standardize(coordinates)

        if to_form is None:
            tmp_molecular_system = copy(molecular_system, check=False)
        else:
            tmp_molecular_system = convert(molecular_system,
                                           to_form=to_form,
                                           check=False)

        set(tmp_molecular_system,
            target='system',
            coordinates=coordinates,
            check=False)
        del (coordinates, units)
        return tmp_molecular_system

    elif engine == 'MDTraj':

        #tmp_item.superpose(tmp_ref_item,frame=ref_structure_indices,atom_indices=atom_indices,ref_atom_indices=ref_atom_indices,parallel=parallel)

        #if in_form==x_form:
        #    item=tmp_item
        #elif in_form=='molsysmt.Trajectory':
        #    item._import_mdtraj_data(tmp_item)
        #elif in_form=='molsysmt.MolSys':
        #    item.trajectory._import_mdtraj_data(tmp_item)
        #else:
        #    item=_convert(tmp_item, to_form=in_form)

        raise NotImplementedMethodError()

    else:

        raise NotImplementedMethodError()