def get_contacts(molecular_system, selection='atom_name=="CA"', groups_of_atoms=None, group_behavior=None, structure_indices="all", selection_2=None, groups_of_atoms_2=None, group_behavior_2=None, structure_indices_2=None, output_atom_indices=False, threshold='12 angstroms', pbc=False, parallel=False, engine='MolSysMT', syntaxis='MolSysMT'): from molsysmt.structure.get_distances import get_distances atom_indices_1, atom_indices_2, all_dists = get_distances(molecular_system=molecular_system, selection=selection, groups_of_atoms=groups_of_atoms, group_behavior=group_behavior, structure_indices=structure_indices, selection_2=selection_2, groups_of_atoms_2=groups_of_atoms_2, group_behavior_2=group_behavior_2, structure_indices_2=structure_indices_2, output_atom_indices=True, pbc=pbc, parallel=parallel, output_form='tensor', engine=engine, syntaxis=syntaxis) if threshold is None: raise BadCallError(BadCallMessage) length_units = puw.get_unit(all_dists) threshold = puw.get_value(threshold, to_unit=length_units) all_dists = puw.get_value(all_dists) num_frames=all_dists.shape[0] contact_map=np.empty(all_dists.shape, dtype=bool) for indice_frame in range(num_frames): contact_map[indice_frame,:,:]=(all_dists[indice_frame,:,:]<=threshold) del(all_dists, num_frames, indice_frame, length_units) if output_atom_indices: return atom_indices_1, atom_indices_2, contact_map else: return contact_map
def add(self, item, selection='all', structure_indices='all'): from molsysmt.basic import get step, time, box = get(item, target="system", structure_indices=structure_indices, step=True, time=True, box=True) coordinates = get(item, target="atom", selection=selection, structure_indices=structure_indices, coordinates=True) if self.n_structures == 0: self.append_structures(step, time, coordinates, box) else: if self.n_structures != coordinates.shape[0]: raise ValueError( 'Both items need to have the same n_structures') else: unit = puw.get_unit(self.coordinates) value_coordinates = puw.get_value(coordinates, to_unit=unit) value_self_coordinates = puw.get_value(self.coordinates) self.coordinates = np.hstack( [value_self_coordinates, value_coordinates]) * unit del (value_coordinates, value_self_coordinates) self.n_atoms = self.coordinates.shape[1] pass
def box_shape_from_box_angles(angles): shape = None if angles is not None: alpha = angles[:, 0].mean() beta = angles[:, 1].mean() gamma = angles[:, 2].mean() alpha = puw.get_value(alpha, to_unit='degrees') beta = puw.get_value(beta, to_unit='degrees') gamma = puw.get_value(gamma, to_unit='degrees') if np.allclose([alpha, beta, gamma], [90.0, 90.0, 90.0]): shape = 'cubic' elif np.allclose([alpha, beta, gamma], [70.52878, 109.471221, 70.52878]): shape = 'truncated octahedral' elif np.allclose([alpha, beta, gamma], [60.0, 60.0, 90.0]): shape = 'rhombic dodecahedral' else: shape = 'triclinic' return shape
def to_mdtraj_Trajectory(item, atom_indices='all', structure_indices='all', check=True): if check: digest_item(item, 'molsysmt.MolSys') atom_indices = digest_atom_indices(atom_indices) structure_indices = digest_structure_indices(structure_indices) from molsysmt import puw from . import to_mdtraj_Topology from . import get_box_lengths_from_system, get_box_angles_from_system, get_coordinates_from_atom, get_time_from_system try: from mdtraj.core.trajectory import Trajectory as mdtraj_Trajectory except: raise LibraryNotFound('mdtraj') tmp_item_topology = to_mdtraj_Topology(item, atom_indices=atom_indices, check=False) tmp_box_lengths = get_box_lengths_from_system( item, structure_indices=structure_indices, check=False) if tmp_box_lengths is not None: tmp_box_lengths = puw.get_value(tmp_box_lengths, to_unit='nm') tmp_box_angles = get_box_angles_from_system( item, structure_indices=structure_indices, check=False) if tmp_box_angles is not None: tmp_box_angles = puw.get_value(tmp_box_angles, to_unit='degrees') tmp_coordinates = get_coordinates_from_atom( item, indices=atom_indices, structure_indices=structure_indices, check=False) tmp_coordinates = puw.get_value(tmp_coordinates, to_unit='nm') tmp_time = get_time_from_system(item, structure_indices=structure_indices, check=False) if tmp_time is not None: tmp_time = puw.get_value(tmp_time, to_unit='ps') tmp_item = mdtraj_Trajectory(tmp_coordinates, tmp_item_topology, tmp_time, unitcell_lengths=tmp_box_lengths, unitcell_angles=tmp_box_angles) return tmp_item
def to_file_msmpk(item, atom_indices='all', structure_indices='all', output_filename=None, check=True): if check: digest_item(item, 'file:inpcrd') atom_indices = digest_atom_indices(atom_indices) structure_indices = digest_structure_indices(structure_indices) if output_filename is None: raise ValueError( 'A value different from None is required for the argument "output_filename"' ) from . import extract from molsysmt import puw import pickle tmp_item = extract(item, atom_indices=atom_indices, structure_indices=structure_indices, copy_if_all=True, check=False) # lengths with nm values and times in ps if tmp_item.structures.coordinates is not None: value = puw.get_value(tmp_item.structures.coordinates, to_unit='nm') tmp_item.structures.coordinates = value if tmp_item.structures.box is not None: value = puw.get_value(tmp_item.structures.box, to_unit='nm') tmp_item.structures.box = value if tmp_item.structures.time is not None: value = puw.get_value(tmp_item.structures.time, to_unit='ps') tmp_item.structures.time = value fff = open(output_filename, 'wb') pickle.dump(tmp_item, fff) fff.close() tmp_item = output_filename return tmp_item
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
def to_mdtraj_Trajectory(item, atom_indices='all', structure_indices='all', check=True): if check: digest_item(item, 'openmm.Modeller') atom_indices = digest_atom_indices(atom_indices) structure_indices = digest_structure_indices(structure_indices) try: from mdtraj.core.trajectory import Trajectory as mdtraj_Trajectory except: raise LibraryNotFound('MDTraj') from . import to_mdtraj_Topology from ..mdtraj_Trajectory import extract as extract_mdtraj_Trajectory from molsysmt import puw tmp_topology = to_mdtraj_Topology(item, check=False) positions = puw.get_value(item.positions, to_unit='nanometers') tmp_item = mdtraj_Trajectory(positions, tmp_topology) tmp_item = extract_mdtraj_Trajectory(tmp_item, atom_indices=atom_indices, structure_indices=structure_indices, copy_if_all=False, check=False) return tmp_item
def test_get_distances_from_molsysmt_MolSys_12(): from itertools import combinations molsys = msm.convert(msm.demo['pentalanine']['traj.h5'], to_form='molsysmt.MolSys') list_atom_groups = msm.get(molsys, target='group', selection='all', atom_index=True) list_atom_groups_1 = [] list_atom_groups_2 = [] aux_list_1 = [] aux_list_2 = [] for ii, jj in combinations(range(7), 2): aux_list_1.append(ii) aux_list_2.append(jj) list_atom_groups_1.append(list_atom_groups[ii]) list_atom_groups_2.append(list_atom_groups[jj]) distances = msm.structure.get_distances( molsys, groups_of_atoms=list_atom_groups_1, group_behavior='geometric_center', groups_of_atoms_2=list_atom_groups_2, group_behavior_2='geometric_center', pairs=True) check_shape = ((5000, 21) == distances.shape) check_distance = np.isclose( puw.get_value(distances[1000, 12], to_unit='nm'), 0.69240215) assert check_shape and check_distance
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) unit = _puw.get_unit(item.positions) coordinates = _np.array(_puw.get_value(item.positions)) coordinates = coordinates.reshape(1, coordinates.shape[0], coordinates.shape[1]) if structure_indices is not 'all': coordinates = coordinates[structure_indices, :, :] if indices is not 'all': coordinates = coordinates[:, indices, :] coordinates = coordinates * unit coordinates = _puw.standardize(coordinates) return coordinates
def test_get_distances_from_XYZ_10(): molsys = msm.convert(msm.demo['4 particles']['traj.xyznpy'], to_form='XYZ') distances = msm.structure.get_distances(molsys, selection=[0,0,1], selection_2=[1,2,2], structure_indices=[1,2], pairs=True) check_shape = ((2,3)==distances.shape) check_distance = np.isclose(puw.get_value(distances[0,1], to_unit='nm'), 3.741657) assert check_shape and check_distance
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 solvate(self, unit_name, solvent_model, clearance, box_geometry="cubic"): """ Solvate a unit in LEaP isometrically This adds to the script Parameters ---------- unit_name : str Name of the existing LEaP unit which will be solvated solvent_model : str LEaP recognized name of the solvent model to use, e.g. "TIP3PBOX" clearance : unit.Quantity Add solvent up to clearance distance away (units of length) from the unit_name (radial) box_geometry : "cubic" or "truncated octahedral" Shape of the box to be solvated (Default is "cubic"). """ if box_geometry=="cubic": solvate_command='solvateBox' elif box_geometry=="truncated octahedral": solvate_command='solvateOct' else: raise ValueError('The argument box_geometry must take one of the following values: \ "cubic" or "truncated octahedral".') clearance = puw.get_value(clearance, to_unit='angstroms') self.add_commands('{} {} {} {} iso'.format(solvate_command, unit_name, solvent_model, clearance))
def test_get_minimum_distances_from_XYZ_3(): molsys = msm.convert(msm.demo['4 particles']['traj.xyznpy'], to_form='XYZ') min_pairs, min_distances = msm.structure.get_minimum_distances(molsys, selection=[1,2], structure_indices=[0,1,2], selection_2=[0,1], as_entity=False, as_entity_2=True) check_shape_1 = ((3,2)==min_pairs.shape) check_shape_2 = ((3,2)==min_distances.shape) check_distance = np.isclose(puw.get_value(min_distances[1,0], to_unit='nm'), 0.0) assert check_shape_1 and check_shape_2 and check_distance
def test_get_minimum_distances_from_XYZ_1(): molsys = msm.convert(msm.demo['4 particles']['traj.xyznpy'], to_form='XYZ') min_pairs, min_distances = msm.structure.get_minimum_distances(molsys) check_shape_1 = ((3,2)==min_pairs.shape) check_shape_2 = ((3,)==min_distances.shape) check_pairs = np.all(min_pairs==np.array([[0, 0], [0, 0], [0, 0]])) check_distance = np.isclose(puw.get_value(min_distances[2], to_unit='nm'), 0.0) assert check_shape_1 and check_shape_2 and check_pairs and check_distance
def digest_box_angles(box_angles): output = None unit = puw.get_unit(box_angles) box_angles_value = puw.get_value(box_angles) box_angles_value = digest_box_angles_value(box_angles_value) output = box_angles_value * unit return output
def digest_box_lengths(box_lengths): output = None unit = puw.get_unit(box_lengths) box_lengths_value = puw.get_value(box_lengths) box_lengths_value = digest_box_lengths_value(box_lengths_value) output = box_lengths_value * unit return output
def box_angles_from_box_vectors(box): n_structures = box.shape[0] tmp_box = np.asfortranarray(puw.get_value(box), dtype='float64') angles = libbox.angles_box(tmp_box, n_structures) angles = np.ascontiguousarray(angles, dtype='float64') del (tmp_box) return puw.quantity(angles.round(6), 'degrees')
def get_time_from_system(item, indices='all', structure_indices='all'): output = item.getState().getTime() value = puw.get_value(output) unit = puw.get_unit(output) output = np.array([value]) * unit output = puw.standardize(output) return output
def test_get_distances_from_molsysmt_MolSys_1(): molsys = msm.convert(msm.demo['TcTIM']['1tcd.msmpk'], to_form='molsysmt.MolSys') distances = msm.structure.get_distances(molsys, selection="group_index==0", selection_2="group_index==1") check_shape = ((1, 9, 7) == distances.shape) check_distance = np.isclose( puw.get_value(distances[0, 5, 5], to_unit='nm'), 0.5271685) assert check_shape and check_distance
def box_lengths_from_box_vectors(box): unit = puw.get_unit(box) n_structures = box.shape[0] tmp_box = np.asfortranarray(puw.get_value(box), dtype='float64') lengths = libbox.length_edges_box(tmp_box, n_structures) lengths = np.ascontiguousarray(lengths, dtype='float64') del (tmp_box) return lengths.round(6) * unit
def box_volume_from_box_vectors(box, check=True): if box is not None: units = puw.get_unit(box) value = puw.get_value(box) volume = np.linalg.det(value) * units**3 else: volume = None return volume
def box_vectors_from_box_lengths_and_angles(lengths, angles): lengths = digest_box_lengths(lengths) angles = digest_box_angles(angles) units = puw.get_unit(lengths) lengths_value = puw.get_value(lengths) angles_value = puw.get_value(angles, to_unit='degrees') lengths_value = np.asfortranarray(lengths_value, dtype='float64') angles_value = np.asfortranarray(angles_value, dtype='float64') n_structures = lengths.shape[0] box = libbox.lengths_and_angles_to_box(lengths_value, angles_value, n_structures) box = np.ascontiguousarray(box, dtype='float64').round(6) * units del (lengths_value, angles_value) return box
def test_get_distances_from_molsysmt_MolSys_1(): molsys = msm.convert(msm.demo['TcTIM']['1tcd.msmpk'], to_form='molsysmt.MolSys') distances = msm.structure.get_distances(molsys, selection="group_index==0", group_behavior="geometric_center", selection_2="group_index==1") check_shape = ((1, 1, 7) == distances.shape) check_distance = np.isclose( puw.get_value(distances[0, 0, 4], to_unit='nm'), 0.5724921) assert check_shape and check_distance
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 test_get_neighbors_from_molsysmt_MolSys_1(): molsys = msm.convert(msm.demo['pentalanine']['traj.h5'], to_form='molsysmt.MolSys') CA_atoms_list = msm.select(molsys, selection='atom_name=="CA"') neighbors, distances = msm.structure.get_neighbors(molsys, selection=CA_atoms_list, num_neighbors=3) check_shape_1 = ((5000, 5, 3) == neighbors.shape) check_shape_2 = ((5000, 5, 3) == distances.shape) check_distance = np.isclose( puw.get_value(distances[2000, 0, 0], to_unit='nm'), 0.38743175) assert check_shape_1 and check_shape_2 and check_distance
def test_get_neighbors_from_molsysmt_MolSys_2(): molsys = msm.convert(msm.demo['pentalanine']['traj.h5'], to_form='molsysmt.MolSys') CA_atoms_list = msm.select(molsys, selection='atom_name=="CA"') neighbors, distances = msm.structure.get_neighbors(molsys, selection=CA_atoms_list, selection_2='all', num_neighbors=4) check_neighbors = (10 == neighbors[2000, 0, 3]) check_distance = np.isclose( puw.get_value(distances[2000, 0, 3], to_unit='nm'), 0.1532800) assert check_neighbors and check_distance
def get_center(molecular_system, selection='all', groups_of_atoms=None, weights=None, structure_indices='all', syntaxis='MolSysMT', engine='MolSysMT', parallel=False): molecular_system = digest_molecular_system(molecular_system) engine = digest_engine(engine) if engine == 'MolSysMT': if groups_of_atoms is None: atom_indices = select(molecular_system, selection=selection, syntaxis=syntaxis) groups_of_atoms = [atom_indices] groups_serialized = serialized_lists(groups_of_atoms, dtype='int64') if weights is None: weights = np.ones((groups_serialized.n_values)) elif weights is 'masses': raise NotImplementedError coordinates = get(molecular_system, target='system', structure_indices=structure_indices, coordinates=True) length_units = puw.get_unit(coordinates) coordinates = np.asfortranarray(puw.get_value(coordinates), dtype='float64') n_atoms = coordinates.shape[1] n_structures = coordinates.shape[0] com = libcom.center_of_mass(coordinates, groups_serialized.indices, groups_serialized.values, groups_serialized.starts, weights, n_structures, n_atoms, groups_serialized.n_indices, groups_serialized.n_values) del (coordinates, groups_serialized) return com * length_units else: raise NotImplementedError(NotImplementedMessage)
def test_get_distances_from_molsysmt_MolSys_6(): molsys = msm.convert(msm.demo['pentalanine']['traj.h5'], to_form='molsysmt.MolSys') distances = msm.structure.get_distances( molsys, selection="group_index==0", group_behavior="geometric_center", selection_2="group_index==6", group_behavior_2="geometric_center") check_shape = ((5000, 1, 1) == distances.shape) check_distance = np.isclose( puw.get_value(distances[1000, 0, 0], to_unit='nm'), 2.0584818) assert check_shape and check_distance
def test_get_neighbors_from_molsysmt_MolSys_6(): molsys = msm.convert(msm.demo['TcTIM']['1tcd.msmpk'], to_form='molsysmt.MolSys') CA_atoms = msm.select(molsys, selection='atom_name=="CA"') neighbors, distances = msm.structure.get_neighbors(molsys, selection=CA_atoms, threshold='8 angstroms') check_shape_1 = ((1, 497) == neighbors.shape) check_shape_2 = ((1, 497) == distances.shape) check_neighbors = (14 == len(neighbors[0, 9])) check_neighbors_2 = (21 == neighbors[0, 20][0]) check_distance = np.isclose( puw.get_value(distances[0, 20][0], to_unit='nm'), 0.3807746) assert check_shape_1 and check_shape_2 and check_neighbors and check_neighbors_2 and check_distance
def test_get_distances_from_molsysmt_MolSys_11(): molsys = msm.convert(msm.demo['pentalanine']['traj.h5'], to_form='molsysmt.MolSys') list_atom_groups = msm.get(molsys, target='group', selection='all', atom_index=True) distances = msm.structure.get_distances(molsys, groups_of_atoms=list_atom_groups, group_behavior='geometric_center') check_shape = ((5000, 7, 7) == distances.shape) check_distance = np.isclose( puw.get_value(distances[1000, 2, 3], to_unit='nm'), 0.4240467) assert check_shape and check_distance