Ejemplo n.º 1
0
def get_covalent_chains(molecular_system,
                        chain=None,
                        selection='all',
                        syntaxis='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_bondgraph import get_bondgraph

    if selection is 'all':
        mask = None
    else:
        mask = select(molecular_system,
                      selection=selection,
                      syntaxis=syntaxis,
                      check=False)

    chain_atom_indices = []

    for sel_in_chain in chain:
        atom_indices = select(molecular_system,
                              selection=sel_in_chain,
                              mask=mask)
        chain_atom_indices.append(atom_indices)

    atom_indices = np.sort(np.unique(np.concatenate(chain_atom_indices)))

    graph = get_bondgraph(molecular_system,
                          selection=atom_indices,
                          nodes_name='atom_index',
                          check=False)

    n_slaves = len(chain_atom_indices)

    output = [[ii] for ii in chain_atom_indices[0]]
    for slave_index in range(n_slaves):
        chain_atom_indices[slave_index] = set(chain_atom_indices[slave_index])

    for slave_index in range(1, n_slaves):
        slave_preindex = slave_index - 1
        tmp_output = output.copy()
        output = []
        for chain in tmp_output:
            for ii in graph.neighbors(chain[slave_preindex]):
                if ii in chain_atom_indices[slave_index]:
                    new_chain = chain.copy()
                    new_chain.append(ii)
                    output.append(new_chain)
    del (graph)

    return np.array(output, dtype=int)
Ejemplo n.º 2
0
def get_bondgraph(molecular_system, nodes_name='atom_index', selection='all', syntaxis='MolSysMT',
              to_form='networkx.Graph', check=True):

    # tengo que incluir la forma NetworkX para convertir.
    # en el caso de convert, lo que obtengo es una red con el nombre de los nodos dado por la
    # con el indice de atomo empezando por cero (todavía no lo he decidido)

    # el caso de este método es que nos da un grafo con los nodos nombrados según
    # nodes_name en ['atom_index', 'short_string', 'long_string']

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

    tmp_molecular_system = None

    to_form = digest_to_form(to_form)

    if to_form == 'networkx.Graph':

        G = Graph()

        if nodes_name is 'atom_index':

            atom_indices, bonded_atoms = get(molecular_system, target='atom', selection=selection,
                                             syntaxis=syntaxis, atom_index=True,
                                             inner_bonded_atoms=True, check=False)

            G.add_nodes_from(atom_indices)
            G.add_edges_from(bonded_atoms)

        else:

            raise NotImplementedError

        tmp_molecular_system = G

    else:

        raise NotImplementedError

    return tmp_molecular_system
Ejemplo n.º 3
0
def get_dihedral_angles(molecular_system,
                        dihedral_angle=None,
                        selection='all',
                        quartets=None,
                        structure_indices='all',
                        syntaxis='MolSysMT',
                        pbc=False,
                        check=True):

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

    structure_indices = digest_structure_indices(structure_indices)

    if quartets is None:

        quartets = get_covalent_dihedral_quartets(
            molecular_system,
            dihedral_angle=dihedral_angle,
            selection=selection,
            syntaxis=syntaxis)

    elif 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

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

    n_angles = quartets.shape[0]
    n_structures = coordinates.shape[0]
    n_atoms = coordinates.shape[1]

    if pbc:

        box, box_shape = get(molecular_system,
                             target='system',
                             structure_indices=structure_indices,
                             box=True,
                             box_shape=True)
        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')

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

    angles = libgeometry.dihedral_angles(coordinates, box, orthogonal,
                                         int(pbc), quartets, n_angles, n_atoms,
                                         n_structures)
    angles = np.ascontiguousarray(angles) * puw.unit('degrees')

    return angles
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 get_covalent_dihedral_quartets(molecular_system,
                                   dihedral_angle=None,
                                   with_blocks=False,
                                   selection='all',
                                   syntaxis='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
    from molsysmt.topology.get_covalent_chains import get_covalent_chains

    if dihedral_angle is not None:
        if dihedral_angle == 'phi':
            chain = [
                'atom_name=="C"', 'atom_name=="N"', 'atom_name=="CA"',
                'atom_name=="C"'
            ]
        elif dihedral_angle == 'psi':
            chain = [
                'atom_name=="N"', 'atom_name=="CA"', 'atom_name=="C"',
                'atom_name=="N"'
            ]
        elif dihedral_angle == 'omega':
            chain = [
                'atom_name==["CA","CH3"]', 'atom_name=="C"', 'atom_name=="N"',
                'atom_name==["CA","CH3"]'
            ]
        elif dihedral_angle == 'chi1':
            chain = [
                'atom_name=="N"', 'atom_name=="CA"', 'atom_name=="CB"',
                'atom_name==["CG","CG1","OG","OG1","SG"]'
            ]  # flexible but PRO
        elif dihedral_angle == 'chi2':
            chain = [
                'atom_name=="CA"', 'atom_name=="CB"',
                'atom_name==["CG","CG1"]',
                'atom_name==["CD","CD1","SD","OD1","ND1"]'
            ]  # flexible but PRO
        elif dihedral_angle == 'chi3':
            chain = [
                'atom_name=="CB"', 'atom_name=="CG"', 'atom_name==["CD","SD"]',
                'atom_name==["NE","OE1","CE"]'
            ]
        elif dihedral_angle == 'chi4':
            chain = [
                'atom_name=="CG"', 'atom_name=="CD"', 'atom_name==["NE","CE"]',
                'atom_name==["CZ","NZ"]'
            ]
        elif dihedral_angle == 'chi5':
            chain = [
                'atom_name=="CD"', 'atom_name=="NE"', 'atom_name=="CZ"',
                'atom_name=="NH1"'
            ]
        elif dihedral_angle == 'phi-psi':
            tmp_phi = get_covalent_dihedral_quartets(molecular_system,
                                                     dihedral_angle='phi',
                                                     with_blocks=with_blocks,
                                                     selection=selection,
                                                     syntaxis=syntaxis,
                                                     check=False)
            tmp_psi = get_covalent_dihedral_quartets(molecular_system,
                                                     dihedral_angle='psi',
                                                     with_blocks=with_blocks,
                                                     selection=selection,
                                                     syntaxis=syntaxis,
                                                     check=False)
            if not with_blocks:
                tmp_angs = [ii for ii in [tmp_phi, tmp_psi] if ii.shape[0] > 0]
                tmp_angs = np.vstack(tmp_angs)
                return tmp_angs
            else:
                tmp_angs = [
                    ii for ii in [tmp_phi[0], tmp_psi[0]] if ii.shape[0] > 0
                ]
                tmp_angs = np.vstack(tmp_angs)
                tmp_blocks = [
                    ii for ii in [tmp_phi[1], tmp_psi[1]] if ii.shape[0] > 0
                ]
                tmp_blocks = np.vstack(tmp_blocks)
                return tmp_angs, tmp_blocks
        elif dihedral_angle == 'phi-psi-omega':
            tmp_phi = get_covalent_dihedral_quartets(molecular_system,
                                                     dihedral_angle='phi',
                                                     with_blocks=with_blocks,
                                                     selection=selection,
                                                     syntaxis=syntaxis,
                                                     check=False)
            tmp_psi = get_covalent_dihedral_quartets(molecular_system,
                                                     dihedral_angle='psi',
                                                     with_blocks=with_blocks,
                                                     selection=selection,
                                                     syntaxis=syntaxis,
                                                     check=False)
            tmp_omega = get_covalent_dihedral_quartets(molecular_system,
                                                       dihedral_angle='omega',
                                                       with_blocks=with_blocks,
                                                       selection=selection,
                                                       syntaxis=syntaxis,
                                                       check=False)
            if not with_blocks:
                tmp_angs = [
                    ii for ii in [tmp_phi, tmp_psi, tmp_omega]
                    if ii.shape[0] > 0
                ]
                tmp_angs = np.vstack(tmp_angs)
                return tmp_angs
            else:
                tmp_angs = [
                    ii for ii in [tmp_phi[0], tmp_psi[0], tmp_omega[0]]
                    if ii.shape[0] > 0
                ]
                tmp_angs = np.vstack(tmp_angs)
                tmp_blocks = [
                    ii for ii in [tmp_phi[1], tmp_psi[1], tmp_omega[1]]
                    if ii.shape[0] > 0
                ]
                tmp_blocks = np.vstack(tmp_blocks)
                return tmp_angs, tmp_blocks
        elif dihedral_angle == 'all':
            tmp_phi = get_covalent_dihedral_quartets(molecular_system,
                                                     dihedral_angle='phi',
                                                     with_blocks=with_blocks,
                                                     selection=selection,
                                                     syntaxis=syntaxis,
                                                     check=False)
            tmp_psi = get_covalent_dihedral_quartets(molecular_system,
                                                     dihedral_angle='psi',
                                                     with_blocks=with_blocks,
                                                     selection=selection,
                                                     syntaxis=syntaxis,
                                                     check=False)
            tmp_omega = get_covalent_dihedral_quartets(molecular_system,
                                                       dihedral_angle='omega',
                                                       with_blocks=with_blocks,
                                                       selection=selection,
                                                       syntaxis=syntaxis,
                                                       check=False)
            tmp_chi1 = get_covalent_dihedral_quartets(molecular_system,
                                                      dihedral_angle='chi1',
                                                      with_blocks=with_blocks,
                                                      selection=selection,
                                                      syntaxis=syntaxis,
                                                      check=False)
            tmp_chi2 = get_covalent_dihedral_quartets(molecular_system,
                                                      dihedral_angle='chi2',
                                                      with_blocks=with_blocks,
                                                      selection=selection,
                                                      syntaxis=syntaxis,
                                                      check=False)
            tmp_chi3 = get_covalent_dihedral_quartets(molecular_system,
                                                      dihedral_angle='chi3',
                                                      with_blocks=with_blocks,
                                                      selection=selection,
                                                      syntaxis=syntaxis,
                                                      check=False)
            tmp_chi4 = get_covalent_dihedral_quartets(molecular_system,
                                                      dihedral_angle='chi4',
                                                      with_blocks=with_blocks,
                                                      selection=selection,
                                                      syntaxis=syntaxis,
                                                      check=False)
            tmp_chi5 = get_covalent_dihedral_quartets(molecular_system,
                                                      dihedral_angle='chi5',
                                                      with_blocks=with_blocks,
                                                      selection=selection,
                                                      syntaxis=syntaxis,
                                                      check=False)
            if not with_blocks:
                tmp_angs = [
                    ii for ii in [
                        tmp_phi, tmp_psi, tmp_omega, tmp_chi1, tmp_chi2,
                        tmp_chi3, tmp_chi4, tmp_chi5
                    ] if ii.shape[0] > 0
                ]
                tmp_angs = np.vstack(tmp_angs)
                return tmp_angs
            else:
                tmp_angs = [
                    ii for ii in [
                        tmp_phi[0], tmp_psi[0], tmp_omega[0], tmp_chi1[0],
                        tmp_chi2[0], tmp_chi3[0], tmp_chi4[0], tmp_chi5[0]
                    ] if ii.shape[0] > 0
                ]
                tmp_angs = np.vstack(tmp_angs)
                tmp_blocks = [
                    ii for ii in [
                        tmp_phi[1], tmp_psi[1], tmp_omega[1], tmp_chi1[1],
                        tmp_chi2[1], tmp_chi3[1], tmp_chi4[1], tmp_chi5[1]
                    ] if ii.shape[0] > 0
                ]
                tmp_blocks = np.vstack(tmp_blocks)
                return tmp_angs, tmp_blocks
        else:
            raise ValueError

    quartets = get_covalent_chains(molecular_system,
                                   chain=chain,
                                   selection=selection,
                                   syntaxis=syntaxis,
                                   check=False)

    if with_blocks:

        n_quartets = quartets.shape[0]

        blocks = []

        for quartet_index in range(n_quartets):

            quartet = quartets[quartet_index]
            component_index = get(molecular_system,
                                  target='atom',
                                  indices=quartet[1],
                                  component_index=True,
                                  check=False)[0]
            component_atom_indices = get(molecular_system,
                                         target='component',
                                         indices=component_index,
                                         atom_index=True,
                                         check=False)[0]
            tmp_blocks = get_covalent_blocks(
                molecular_system,
                remove_bonds=[quartet[1], quartet[2]],
                output_form='sets',
                check=False)
            blocks_in_component = []
            for block in tmp_blocks:
                if block.issubset(component_atom_indices):
                    blocks_in_component.append(block)
            blocks.append(blocks_in_component)

        return quartets, np.array(blocks)

    else:

        return quartets