def get_box_from_system(item, structure_indices='all', check=True): if check: _digest_item(item, _form) structure_indices = _digest_structure_indices(structure_indices) from molsysmt._private.math import serie_to_chunks from molsysmt.pbc import box_vectors_from_box_lengths_and_angles if structure_indices is 'all': structure_indices = _np.arange(get_n_structures_from_system(item)) starts_serie_frames, size_serie_frames = serie_to_chunks(structure_indices) box_list = [] for start, size in zip(starts_serie_frames, size_serie_frames): item.seek(start) frame_hdf5 = item.read(n_frames=size) cell_lengths = frame_hdf5.cell_lengths cell_angles = frame_hdf5.cell_angles box = box_vectors_from_box_lengths_and_angles( cell_lengths * _puw.unit('nm'), cell_angles * _puw.unit('degrees')) box_list.append(_puw.get_value(box)) box = _np.concatenate(box_list) del (box_list) box = box.astype('float64') box = box * _puw.unit('nm') box = _puw.standardize(box) return box
def get_time_from_system(item, structure_indices='all', check=True): if check: _digest_item(item, _form) structure_indices = _digest_structure_indices(structure_indices) from molsysmt._private.math import serie_to_chunks if structure_indices is 'all': n_structures = get_n_structures_from_system(item) structure_indices = np.arange(n_structures) starts_serie_frames, size_serie_frames = serie_to_chunks(structure_indices) time_list = [] for start, size in zip(starts_serie_frames, size_serie_frames): item.seek(start) _, time, _, _ = item.read(n_frames=size) time_list.append(time) time = _np.concatenate(time_list) del (time_list) time = time.astype('float64') time = time * _puw.unit('ps') time = _puw.standardize(time) return time
def get_box_from_system(item, structure_indices='all', check=True): if check: _digest_item(item, _form) structure_indices = _digest_structure_indices(structure_indices) from molsysmt._private.math import serie_to_chunks if structure_indices is 'all': n_structures = get_n_structures_from_system(item) structure_indices = np.arange(n_structures) starts_serie_frames, size_serie_frames = serie_to_chunks(structure_indices) box_list = [] for start, size in zip(starts_serie_frames, size_serie_frames): item.seek(start) _, _, _, box = item.read(n_frames=size) box_list.append(box) box = np.concatenate(box_list) del (box_list) box = box.astype('float64') box = box * _puw.unit(item.distance_unit) box = _puw.standardize(box) return box
def test_get_contacts_from_molsysmt_MolSys_2(): molsys = msm.convert(msm.demo['TcTIM']['1tcd.msmpk'], to_form='molsysmt.MolSys') CA_atoms_chain_0 = msm.get(molsys, selection="atom_name=='CA' and chain_index==0", atom_index=True) CA_atoms_chain_1 = msm.get(molsys, selection="atom_name=='CA' and chain_index==1", atom_index=True) contact_map = msm.structure.get_contacts(molsys, selection=CA_atoms_chain_0, selection_2=CA_atoms_chain_1, threshold=1.2*puw.unit('nm')) check_shape_1 = ((1, 248, 249)==contact_map.shape) check_value_1 = np.all(np.array([False, False, False, False, True, False, False, True, False, False])==contact_map[0,48,40:50]) assert check_shape_1 and check_value_1
def get_box_from_system(item, indices='all', structure_indices='all'): from molsysmt.pbc import box_vectors_from_box_lengths_and_angles _, _, cell_lengths, cell_angles = item.read() if cell_lengths is not None: cell_lengths = cell_lengths*puw.unit(item.distance_unit) cell_angles = cell_angles*puw.unit('degrees') box = box_vectors_from_box_lengths_and_angles(cell_lengths, cell_angles) if structure_indices is not 'all': box = box[structure_indices,:,:] box = puw.standardize(box) else: box = None return box
def box_shape_from_box_vectors(box): from molsysmt.pbc.box_shape_from_box_angles import box_shape_from_box_angles if box is None: return None else: tmp_box = np.asfortranarray(puw.get_value(box), dtype='float64') n_structures = tmp_box.shape[0] angles = libbox.angles_box(tmp_box, n_structures) * puw.unit('degrees') return box_shape_from_box_angles(angles)
def get_time_from_system(item, indices='all', structure_indices='all'): _, time, _, _ = item.read() time = time*puw.unit('ps') if structure_indices is not 'all': time = time[structure_indices]*picoseconds time = puw.standardize(time) return time
def get_coordinates_from_atom(item, indices='all', structure_indices='all'): xyz = item.positions if structure_indices is not 'all': xyz = xyz[structure_indices, :, :] if indices is not 'all': xyz = xyz[:, indices, :] xyz = xyz * puw.unit(item.distance_unit) xyz = puw.standardize(xyz) return xyz
def test_get_contacts_from_molsysmt_MolSys_3(): molsys = msm.convert(msm.demo['TcTIM']['1tcd.msmpk'], to_form='molsysmt.MolSys') atoms_in_residues_chain_0 = msm.get(molsys, target='group', selection="molecule_type=='protein' and chain_index==0", atom_index=True) atoms_in_residues_chain_1 = msm.get(molsys, target='group', selection="molecule_type=='protein' and chain_index==1", atom_index=True) contact_map = msm.structure.get_contacts(molsys, groups_of_atoms=atoms_in_residues_chain_0, group_behavior='geometric_center', groups_of_atoms_2=atoms_in_residues_chain_1, group_behavior_2='geometric_center', threshold=1.2*puw.unit('nm')) check_shape_1 = ((1, 248, 249)==contact_map.shape) check_value_1 = np.all(np.array([False, False, False, False, True, False, False, True, False, False])==contact_map[0,48,40:50]) assert check_shape_1 and check_value_1
def get_box_lengths_from_system(item, indices='all', structure_indices='all'): cell_lengths = item.unitcell_lengths if cell_lengths is not None: cell_lengths = cell_lengths * puw.unit(item.distance_unit) if structure_indices is not 'all': cell_lengths = cell_lengths[structure_indices] cell_lengths = puw.standardize(cell_lengths) else: cell_lengths = None return cell_lengths
def get_box_angles_from_system(item, indices='all', structure_indices='all'): cell_angles = item.unitcell_angles if cell_lengths is not None: cell_angles = cell_angles * puw.unit('degrees') if structure_indices is not 'all': cell_angles = cell_angles[structure_indices] cell_angles = puw.standardize(cell_angles) else: cell_angles = None return cell_angles
def is_XYZ(item): output = False if puw.is_quantity(item): if puw.compatibility(item, puw.unit('nm')): shape = np.shape(item) if len(shape) == 3 and shape[-1] == 3: output = True elif len(shape) == 2 and shape[-1] == 3: output = True elif len(shape) == 1 and shape[-1] == 3: output = True return output
def get_coordinates_from_atom(item, indices='all', structure_indices='all', check=True): if check: _digest_item(item, _form) indices = _digest_indices(indices) structure_indices = _digest_structure_indices(structure_indices) from molsysmt._private.math import serie_to_chunks if structure_indices is 'all': n_structures = get_n_structures_from_system(item) structure_indices = _np.arange(n_structures) starts_serie_frames, size_serie_frames = serie_to_chunks(structure_indices) xyz_list = [] for start, size in zip(starts_serie_frames, size_serie_frames): item.seek(start) if indices is 'all': xyz, _, _, _ = item.read(n_structures=size) else: xyz, _, _, _ = item.read(n_structures=size, atom_indices=indices) xyz_list.append(xyz) xyz = _np.concatenate(xyz_list) del (xyz_list) xyz = xyz.astype('float64') xyz = xyz * _puw.unit(item.distance_unit) xyz = _puw.standardize(xyz) return xyz
def test_get_neighbors_from_molsysmt_MolSys_7(): molsys = msm.convert(msm.demo['TcTIM']['1tcd.msmpk'], to_form='molsysmt.MolSys') atoms_in_residues_chain_0 = msm.get( molsys, target='group', selection="molecule_type=='protein' and chain_index==0", atom_index=True) atoms_in_residues_chain_1 = msm.get( molsys, target='group', selection="molecule_type=='protein' and chain_index==1", atom_index=True) neighbors, distances = msm.structure.get_neighbors( molsys, groups_of_atoms=atoms_in_residues_chain_0, group_behavior='geometric_center', groups_of_atoms_2=atoms_in_residues_chain_1, group_behavior_2='geometric_center', threshold=1.2 * puw.unit('nanometers')) check_n_contacts = (18 == len(neighbors[0, 11])) assert check_n_contacts
def mass(molecular_system, target='atom', selection='all'): from molsysmt.basic import get from molsysmt.physico_chemical_properties.atoms.mass import physical, units from molsysmt._private._digestion import digest_target target = digest_target(target) values = physical output = [] if target == 'atom': atom_types = get(molecular_system, target=target, selection=selection, atom_type=True) for ii in atom_types: output.append(values[ii.capitalize()]) elif target in ['group', 'component', 'molecule', 'chain', 'entity']: atom_types_in_target = get(molecular_system, target=target, selection=selection, atom_type=True) for aux in atom_types_in_target: output.append(np.sum([values[ii.capitalize()] for ii in aux])) elif target == 'system': atom_types_in_target = get(molecular_system, target='atom', selection='all', atom_type=True) output.append( np.sum([values[ii.capitalize()] for ii in atom_types_in_target])) if target == 'system': output = output[0] * puw.unit(units) else: output = puw.quantity(np.array(output), units) return output
import molsysmt as msm from molsysmt import puw import numpy as np from pathlib import Path import os # Removing all msmpk files for xyznpy_file in Path('.').glob('*.xyznpy'): os.remove(xyznpy) # New xyznpy files molecular_system = np.zeros([3, 4, 3], dtype='float64') * puw.unit('nm') molecular_system[0, 0, :] = [0, 2, -1] * puw.unit('nm') molecular_system[1, 0, :] = [1, 2, -1] * puw.unit('nm') molecular_system[2, 0, :] = [0, 2, -1] * puw.unit('nm') molecular_system[0, 1, :] = [-1, 1, 1] * puw.unit('nm') molecular_system[1, 1, :] = [-1, 0, 1] * puw.unit('nm') molecular_system[2, 1, :] = [0, 0, 1] * puw.unit('nm') molecular_system[0, 2, :] = [-2, 0, 1] * puw.unit('nm') molecular_system[1, 2, :] = [-2, 0, 0] * puw.unit('nm') molecular_system[2, 2, :] = [-1, 1, 0] * puw.unit('nm') molecular_system[0, 3, :] = [-2, -2, -2] * puw.unit('nm') molecular_system[1, 3, :] = [0, 0, 0] * puw.unit('nm') molecular_system[2, 3, :] = [2, 2, 2] * puw.unit('nm') msm.convert(molecular_system, to_form='traj.xyznpy')
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
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
def charge(molecular_system, target='group', selection='all', type=None, engine='OpenMM'): from molsysmt._private._digestion import digest_engine, digest_target engine = digest_engine(engine) target = digest_target(target) if type in ['physical_pH7', 'collantes']: from molsysmt.basic import get from molsysmt.physico_chemical_properties.groups.charge import units if type == 'physical_pH7': from molsysmt.physico_chemical_properties.groups.charge import physical_pH7 as values, units elif type == 'collantes': from molsysmt.physico_chemical_properties.groups.charge import collantes as values, units else: raise NotImplementedError() output = [] if target == 'atom': raise ValueError( 'Only targets bigger than, or equal to, groups are allowed when type is "physical_pH7" or "collantes"' ) elif target == 'group': group_names = get(molecular_system, target=target, selection=selection, group_name=True) for ii in group_names: output.append(values[ii.upper()]) elif target in ['component', 'molecule', 'chain', 'entity']: group_names = get(molecular_system, target=target, selection=selection, group_name=True) for aux in group_names: output.append(np.sum([values[ii.upper()] for ii in aux])) elif target == 'system': group_names = get(molecular_system, target='group', selection='all', group_names=True) output.append(np.sum([values[ii.upper()] for ii in group_names])) if target == 'system': output = output[0] * puw.unit(units) else: output = puw.quantity(np.array(output), units) else: from molsysmt._private._digestion import digest_molecular_system from molsysmt.basic import get, convert, get_form molecular_system = digest_molecular_system(molecular_system) if engine == 'OpenMM': from openmm import NonbondedForce openmm_system = convert(molecular_system, to_form='openmm.System') output = [] if target == 'atom': atom_indices = get(molecular_system, target=target, selection=selection, atom_index=True) for force_index in range(openmm_system.getNumForces()): force = openmm_system.getForce(force_index) if isinstance(force, NonbondedForce): for index in atom_indices: output.append( force.getParticleParameters( int(index))[0]._value) output = np.array(output, dtype=float).round(4) * puw.unit('e') elif target in [ 'group', 'component', 'chain', 'molecule', 'entity' ]: atom_indices = get(molecular_system, target=target, selection=selection, atom_index=True) for force_index in range(openmm_system.getNumForces()): force = openmm_system.getForce(force_index) if isinstance(force, NonbondedForce): for atom_list in atom_indices: var_aux = 0.0 for index in atom_list: var_aux += force.getParticleParameters( int(index))[0]._value output.append(var_aux) output = np.array(output, dtype=float).round(4) * puw.unit('e') elif target == 'system': atom_indices = get(molecular_system, target='atom', selection='all', index=True) var_aux = 0.0 for force_index in range(openmm_system.getNumForces()): force = openmm_system.getForce(force_index) if isinstance(force, NonbondedForce): for index in atom_indices: var_aux += force.getParticleParameters( int(index))[0]._value output = np.round(var_aux, 4) * puw.unit('e') else: raise NotImplementedError return output