Ejemplo n.º 1
0
def unwrap(molecular_system, selection='all', structure_indices='all',
        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

        coordinates= get(molecular_system, target='atom', selection=selection, coordinates=True)
        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)

        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

        length_units = puw.get_unit(coordinates)
        box = puw.convert(box, to_unit=length_units)

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

        libbox.unwrap(coordinates, box, orthogonal, n_atoms, n_structures)

        coordinates=np.ascontiguousarray(coordinates)*length_units

    else:

        raise NotImpementedEngineError()

    if in_place:

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

        pass

    else:

        tmp_molecular_system = extract(molecular_system, selection=selection, structure_indices=structure_indices, syntaxis=syntaxis)
        set(tmp_molecular_system, target='atom', selection='all', structure_indices='all', syntaxis='MolSysMT', coordinates=coordinates)

        return tmp_molecular_system
Ejemplo n.º 2
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
Ejemplo n.º 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
Ejemplo n.º 4
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
Ejemplo n.º 5
0
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()
Ejemplo n.º 6
0
def model_loop (item, target_sequence=None, finesse=0, engine='modeller', verbose=False):

    if engine=='modeller':

        from molsysmt.topology import get_sequence_alignment as get_sequence_alignment
        import tempfile as _tempfile
        from os import remove
        from os import curdir
        from os import listdir
        from glob import glob
        import modeller as modeller
        import modeller.automodel as automodel
        from molsysmt.basic import get_form, get, set

        form_in = get_form(item)

        tmp_box = get(item, box=True)

        seq_original = convert(item, to_form='string:aminoacids1')
        seq_aligned, _, _, _, _ = get_sequence_alignment('aminoacids1:'+seq_original,
                                                      'aminoacids1:'+target_sequence)[0]

        tmp_pdbfilename = tempfile.NamedTemporaryFile(suffix=".pdb").name
        tmp_name = tmp_pdbfilename.split('/')[-1].split('.')[0]
        tmp_seqfilename = '/tmp/'+tmp_name+'.seq'
        tmp_alifilename = '/tmp/'+tmp_name+'.ali'

        _ = convert(item, to_form=tmp_pdbfilename)

        if verbose:
            modeller.log.verbose()
        else:
            modeller.log.none()

        e = modeller.environ()
        e.io.atom_files_directory = ['.', '/tmp']
        m = modeller.model(e, file=tmp_pdbfilename)
        aln = modeller.alignment(e)
        aln.append_model(m, align_codes=tmp_name)
        aln.write(file=tmp_seqfilename)

        alifile = open(tmp_alifilename,'w')
        seqfile = open(tmp_seqfilename,'r')
        lines_seqfile = seqfile.readlines()
        alifile.write(lines_seqfile[1])
        alifile.write(lines_seqfile[2])
        alifile.write(seq_aligned+'*\n')
        alifile.write('>P1;'+tmp_name+'_fill\n')
        alifile.write('sequence:::::::::\n')
        alifile.write(target_sequence+'*\n')
        alifile.close()
        seqfile.close()
        del(lines_seqfile)

        a = automodel.loopmodel(e, alnfile = tmp_alifilename, knowns = tmp_name, sequence = tmp_name+'_fill')

        a.write_intermediates = False

        a.starting_model = 1
        a.ending_model = 1

        a.loop.starting_model = 1
        a.loop.ending_model = 12

        if finesse==0:
            a.loop.md_level = automodel.refine.very_fast
        elif finesse==1:
            a.loop.md_level = automodel.refine.fast
        elif finesse==2:
            a.loop.md_level = automodel.refine.slow
        elif finesse==3:
            a.loop.md_level = automodel.refine.very_slow
        elif finesse==4:
            a.loop.md_level = automodel.refine.slow_large

        a.make()

        remove(tmp_pdbfilename)
        remove(tmp_seqfilename)
        remove(tmp_alifilename)

        files_produced = [ ff for ff in _listdir(_curdir) if ff.startswith(tmp_name) ]

        candidate_value = float("inf")
        candidate_pdb_file = None

        for pdb_loopmodel in [ ff for ff in files_produced if ff.startswith(tmp_name+'_fill.BL')]:
            pdb_file=open(pdb_loopmodel,'r')
            _ = pdb_file.readline()
            objective_function_value = float(pdb_file.readline().split(' ')[-1])
            if objective_function_value < candidate_value:
                candidate_value = objective_function_value
                candidate_pdb_file = pdb_loopmodel
            pdb_file.close()

        print("Best loop model with modeller's objective function:",candidate_value)
        tmp_item = convert(candidate_pdb_file, to_form=form_in)

        for file_produced in files_produced:
            remove(file_produced)

        set(tmp_item, box=tmp_box)
        del(tmp_box)

        return tmp_item

    elif engine=='pyrosetta':
        pass

    elif engine=='ensembler':
        pass