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 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 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 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) xyz = item.getPositions(asNumpy=True) unit = _puw.get_unit(xyz) xyz = _np.expand_dims(xyz, axis=0) if structure_indices is not 'all': xyz = xyz[structure_indices, :, :] if indices is not 'all': xyz = xyz[:, indices, :] xyz = _puw.standardize(xyz * unit) return xyz
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 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 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 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_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 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 get_box_from_system(item, structure_indices='all', check=True): if check: _digest_item(item, _form) structure_indices = _digest_structure_indices(structure_indices) box = getBoxVectors(asNumpy=True) unit = _puw.get_unit(box) box = _np.expand_dims(box, axis=0) box = _puw.standardize(box * unit) return box
def get_box_from_system(item, structure_indices='all', check=True): if check: _digest_item(item, _form) structure_indices = _digest_structure_indices(structure_indices) box = item.getPeriodicBoxVectors() unit = _puw.get_unit(box) box = _np.expand_dims(puw.get_value(box), axis=0) box = _puw.standardize(box * unit) return box
def get_rank_3_XYZ(item): unit = puw.get_unit(item) value = puw.get_value(item) if type(value) == list: value = np.array(value) if len(value.shape) == 2: value = np.expand_dims(value, axis=0) elif len(value.shape) == 1: value = np.expand_dims(value, axis=0) value = np.expand_dims(value, axis=0) return value * unit
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 get_coordinates_from_atom(item, indices='all', structure_indices='all'): 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 get_coordinates_from_atom(item, indices='all', structure_indices='all'): coordinates = item.getState(getPositions=True).getPositions(asNumpy=True) unit = puw.get_unit(coordinates) coordinates = puw.get_value(coordinates) 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 get_box_from_system(item, structure_indices='all', check=True): if check: _digest_item(item, _form) structure_indices = _digest_structure_indices(structure_indices) box = item.getPeriodicBoxVectors() output = None if box is not None: unit = _puw.get_unit(box) box = _np.array(_puw.get_value(box)) box = box.reshape(1, box.shape[0], box.shape[1]) box = box * unit output = _puw.standardize(box) return output
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 set_coordinates_to_atom(item, indices='all', structure_indices='all', value=None): length_unit = puw.get_unit(item['coordinates']) value = puw.convert(value, to_unit=length_unit) if indices is 'all': if structure_indices is 'all': item['coordinates'] = value else: item['coordinates'][structure_indices, :, :] = value else: if structure_indices is 'all': item['coordinates'][:, indices, :] = value else: item['coordinates'][np.ix_(indices, structure_indices)] = value pass
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
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
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()
def get_rmsd(molecular_system, selection='backbone', structure_indices='all', reference_molecular_system=None, reference_selection=None, reference_frame_index=0, reference_coordinates=None, parallel=True, syntaxis='MolSysMT', engine='MolSysMT'): from molsysmt.basic import is_a_molecular_system if not is_a_molecular_system(molecular_system): raise SingleMolecularSystemNeededError() engine = digest_engine(engine) if engine == 'MolSysMT': from molsysmt.basic import select, get from molsysmt._private._digestion import digest_structure_indices n_atoms, n_structures = get(molecular_system, n_atoms=True, n_structures=True) atom_indices = select(molecular_system, selection=selection, syntaxis=syntaxis) 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_coordinates is None: 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) reference_coordinates = get( reference_molecular_system, target='atom', indices=reference_atom_indices, structure_indices=reference_frame_index, coordinates=True) coordinates = get(molecular_system, coordinates=True, structure_indices='all') 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" ) rmsd_val = librmsd.rmsd(coordinates, atom_indices, reference_coordinates, structure_indices, n_atoms, n_structures, n_atom_indices, n_structure_indices) rmsd_val = rmsd_val * units rmsd_val = puw.standardize(rmsd_val) del (coordinates, units) return rmsd_val elif engine == 'MDTraj': #from mdtraj import rmsd as mdtraj_rmsd #from molsysmt.basic import convert #tmp_molecular_system = convert(molecular_system, to_form='mdtraj.Trajectory') #rmsd_val = mdtraj_rmsd(tmp_molecular_system, ref_item, frame=ref_structure_indices, # ref_atom_indices=ref_atom_indices, atom_indices=atom_indices, # parallel=parallel, precentered=precentered) #rmsd_val = puw.standardize(rmsd_val) #return rmsd_val raise NotImplementedError else: raise NotImplementedError
def get_maximum_distances(molecular_system, selection="all", groups_of_atoms=None, group_behavior=None, as_entity=True, structure_indices="all", selection_2=None, groups_of_atoms_2=None, group_behavior_2=None, as_entity_2=True, structure_indices_2=None, atom_indices=False, pairs=False, pbc=False, parallel=False, engine='MolSysMT', syntaxis='MDTraj'): from molsysmt.structure.get_distances import get_distances if atom_indices: 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, pairs=pairs, pbc=pbc, parallel=parallel, output_form='tensor', output_atom_indices=True, engine=engine, syntaxis=syntaxis) else: 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, pairs=pairs, pbc=pbc, parallel=parallel, output_form='tensor', engine=engine, syntaxis=syntaxis) if pairs is False: nframes, nelements_1, nelements_2 = all_dists.shape length_units = puw.get_unit(all_dists) all_dists = puw.get_value(all_dists) if (as_entity is True) and (as_entity_2 is True): pairs=np.empty((nframes,2),dtype=int) dists=np.empty((nframes),dtype=float) for indice_frame in range(nframes): ii,jj = np.unravel_index(all_dists[indice_frame,:,:].argmax(), all_dists[indice_frame,:,:].shape) if atom_indices: pairs[indice_frame,0] = atom_indices_1[ii] pairs[indice_frame,1] = atom_indices_2[jj] else: pairs[indice_frame,0] = ii pairs[indice_frame,1] = jj dists[indice_frame] = all_dists[indice_frame,ii,jj] del(all_dists) dists=dists*length_units return pairs, dists elif (as_entity is False) and (as_entity_2 is True): pairs=np.empty((nframes, nelements_1), dtype=int) dists=np.empty((nframes, nelements_1), dtype=float) for indice_frame in range(nframes): for ii in range(nelements_1): jj = all_dists[indice_frame,ii,:].argmax() if atom_indices: pairs[indice_frame,ii]=atom_indices_2[jj] else: pairs[indice_frame,ii]=jj dists[indice_frame,ii]=all_dists[indice_frame,ii,jj] del(all_dists) dists=dists*length_units return pairs, dists elif (as_entity is True) and (as_entity_2 is False): pairs=np.empty((nframes, nelements_2), dtype=int) dists=np.empty((nframes, nelements_2), dtype=float) for indice_frame in range(nframes): for ii in range(nelements_2): jj = all_dists[indice_frame,:,ii].argmax() if atom_indices: pairs[indice_frame,ii]=atom_indices_1[jj] else: pairs[indice_frame,ii]=jj dists[indice_frame,ii]=all_dists[indice_frame,jj,ii] del(all_dists) dists=dists*length_units return pairs, dists else: raise ValueError("If both input arguments 'as_entity' and 'as_entity_2' are False, the\ method you are looking for is molsysmt.distance()") else: nframes, nelements = all_dists.shape length_units = puw.get_unit(all_dists) all_dists = puw.get_value(all_dists) if (as_entity is True) and (as_entity_2 is True): pairs=np.empty((nframes),dtype=int) dists=np.empty((nframes),dtype=float) for indice_frame in range(nframes): ii = all_dists[indice_frame,:].argmax() pairs[indice_frame] = ii dists[indice_frame] = all_dists[indice_frame,ii] del(all_dists) dists=dists*length_units return pairs, dists else: raise ValueError("If 'pairs=True' both input arguments 'as_entity' and 'as_entity_2' need to be True")
def get_radius_of_gyration(molecular_system, selection='all', structure_indices='all', weights=None, pbc=False, engine='MolSysMT', syntaxis='MolSysMT'): molecular_system = digest_molecular_system(molecular_system) engine = digest_engine(engine) structure_indices = digest_structure_indices(structure_indices) if engine == 'MolSysMT': coordinates == msm.get(molecular_system, target='atom', selection=selection, structure_indices=structure_indices, syntaxis=syntaxis, coordinates=True) length_units = puw.get_unit(coordinates_1) coordinates = np.asfortranarray(puw.get_value(coordinates), dtype='float64') n_structures = coordinates.shape[0] n_atoms = coordinates.shape[1] if pbc: box, box_shape = get(molecular_system, target='system', box=True, box_shape=True, structure_indices=structure_indices) 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 else: box = np.zeros([nframes, 3, 3]) * length_units orthogonal = 1 box = np.asfortranarray(puw.get_value(box, to_unit=length_units), dtype='float64') if weights is None: weights = np.ones(n_atoms) weights_units = 1 elif weights is 'masses': masses = msm.chemphys.get_masses(molecular_systems, selection=selection, syntaxis=syntaxis) weights_units = msm.puw.get_unit(masses) weights = msm.puw.get_value(masses) output = libgeometry.radius_of_gyration(coordinates, weights, box, orthogonal, int(pbc), n_structures, n_atoms) output = output * weights_units * length_units * length_units del (weights, coordinates, box, orthogonal) elif engine == 'mdtraj': raise NotImplementedError() else: raise NotImplementedError() return output
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_least_rmsd(molecular_system=None, selection='backbone', structure_indices='all', reference_molecular_system=None, reference_selection=None, reference_frame_index=0, reference_coordinates=None, parallel=True, syntaxis='MolSysMT', engine='MolSysMT', check=True): if check: from molsysmt.tools.molecular_system import is_molecular_system if not is_a_molecular_system(molecular_system): raise SingleMolecularSystemNeededError() 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_coordinates is None: 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" ) rmsd_val = librmsd.least_rmsd(coordinates, atom_indices, reference_coordinates, structure_indices, n_atoms, n_structures, n_atom_indices, n_structure_indices) rmsd_val = rmsd_val * units rmsd_val = puw.standardize(rmsd_val) del (coordinates, units) return rmsd_val elif engine == 'MDTraj': raise NotImplementedError else: raise NotImplementedError
def get_neighbors(molecular_system, selection="all", 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, threshold=None, num_neighbors=None, atom_indices=False, pbc=False, parallel=False, engine='MolSysMT', syntaxis='MolSysMT'): from molsysmt.structure.get_distances import get_distances if (threshold is None) and (num_neighbors is None): raise BadCallError(BadCallMessage) same_set = False same_selections = False same_groups = False same_frames = False if groups_of_atoms is not None: selection = None if (selection is not None) and (selection_2 is None) and (groups_of_atoms_2 is None): same_selections = True elif (groups_of_atoms is not None) and (selection_2 is None) and ( groups_of_atoms_2 is None): same_groups = True if structure_indices_2 is None: same_frames = True same_set = (same_selections or same_groups) and same_frames if atom_indices: 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, pbc=pbc, parallel=parallel, output_form='tensor', engine=engine, syntaxis=syntaxis) else: 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, pbc=pbc, parallel=parallel, output_form='tensor', engine=engine, syntaxis=syntaxis) nframes, nelements_1, nelements_2 = all_dists.shape length_units = puw.get_unit(all_dists) all_dists = puw.get_value(all_dists) if num_neighbors is not None and threshold is None: neighs = np.empty((nframes, nelements_1, num_neighbors), dtype=int) dists = np.empty((nframes, nelements_1, num_neighbors), dtype=float) offset = 0 if same_set: offset = 1 for indice_frame in range(nframes): for ii in range(nelements_1): neighs_aux = np.argpartition(all_dists[indice_frame, ii, :], num_neighbors - 1 + offset)[:num_neighbors + offset] dists_aux = all_dists[indice_frame, ii, neighs_aux] good_order = np.argsort(dists_aux) neighs_aux = neighs_aux[good_order] dists_aux = dists_aux[good_order] if atom_indices: neighs[indice_frame, ii, :] = atom_indices_2[neighs_aux[offset:]] else: neighs[indice_frame, ii, :] = neighs_aux[offset:] dists[indice_frame, ii, :] = dists_aux[offset:] if same_set: if dists_aux[0] > 0.01: raise ValueError( "Error in algorithm, sets are different.") del (all_dists) dists = dists * length_units return neighs, dists elif threshold is not None and num_neighbors is None: threshold = puw.get_value(threshold, to_unit=length_units) neighs = np.empty((nframes, nelements_1), dtype=object) dists = np.empty((nframes, nelements_1), dtype=object) offset = 0 if same_set: offset = 1 for indice_frame in range(nframes): for ii in range(nelements_1): neighs_aux = np.argwhere( all_dists[indice_frame, ii, :] <= threshold)[:, 0] dists_aux = all_dists[indice_frame, ii, neighs_aux] good_order = np.argsort(dists_aux) neighs_aux = neighs_aux[good_order] dists_aux = dists_aux[good_order] if atom_indices: neighs[indice_frame, ii] = atom_indices_2[np.array(neighs_aux, dtype=int)[offset:]] else: neighs[indice_frame, ii] = np.array(neighs_aux, dtype=int)[offset:] dists[indice_frame, ii] = np.array(dists_aux, dtype=float)[offset:] if same_set: if dists_aux[0] > 0.01: raise ValueError( "Error in algorithm, sets are different.") del (all_dists) dists = dists * length_units return neighs, dists else: raise ValueError( "Use either threshold or num_neighbors, but not both at the same time" )
def get_distances(molecular_system, selection="all", groups_of_atoms=None, group_behavior=None, structure_indices="all", molecular_system_2=None, selection_2=None, groups_of_atoms_2=None, group_behavior_2=None, structure_indices_2=None, pairs=False, crossed_frames=False, pbc=False, parallel=False, output_form='tensor', output_atom_indices=False, output_structure_indices=False, engine='MolSysMT', syntaxis='MolSysMT'): from molsysmt.structure.get_center_of_mass import get_center_of_mass from molsysmt.structure.get_geometric_center import get_geometric_center # group_behavior in # ['center_of_mass','geometric_center','minimum_distance','maximum_distance'] # output_form in ['tensor','dict'] # crossed_frames es para cuando queremos calcular lista de frames1 contra lista de frames 2 # (todos con todos), si crossed_frames=False entonces es sólo el primer frame de lista 1 contra # el primer frame de lista 2, el segundo contra el segundo, etc. # selection groups está por si quiero distancias entre centros de masas, necesita # hacer un lista de listas frente a otra lista de listas. molecular_system = digest_molecular_system(molecular_system) if molecular_system_2 is None: same_system = True molecular_system_2 = molecular_system else: same_system = False molecular_system_2 = digest_molecular_system(molecular_system_2) engine = digest_engine(engine) structure_indices = digest_structure_indices(structure_indices) structure_indices_2 = digest_structure_indices(structure_indices_2) if group_behavior == 'minimum_distance' or group_behavior_2 == 'minimum_distance': if group_behavior == 'minimum_distance' and group_behavior_2 == 'minimum_distance': raise NotImplementedError(NotImplementedMessage) #num_groups_1=len(groups_of_atoms) #num_groups_2=len(groups_of_atoms_2) #structure_indices = _digest_structure_indices(item, structure_indices) #num_frames=len(structure_indices) #dists = np.zeros((num_frames, num_groups_1, num_groups_2),dtype=float) #for ii in range(num_groups_1): # group1 = groups_of_atoms_2[ii] # for jj in range(num_groups_2): # group2 = groups_of_atoms_2[jj] # _, min_dist = min_distances(molecular_system=molecular_system, selection=group1, # structure_indices=structure_indices, # selection_2=group2, # pbc=pbc, parallel=parallel, engine=engine) # dists[:,ii,jj]=min_dist #del(num_groups1,num_groups2,structure_indices,num_frames,group1,group2) #return dists else: raise NotImplementedError(NotImplementedMessage) if engine == 'MolSysMT': same_selection = False same_groups = False same_frames = False if groups_of_atoms is not None: selection = None if (selection is not None) and (selection_2 is None): if (groups_of_atoms_2 is None): selection_2 = selection same_selection = True if groups_of_atoms is not None: if (selection_2 is None) and (groups_of_atoms_2 is None): groups_of_atoms_2 = groups_of_atoms same_groups = True if structure_indices_2 is None: structure_indices_2 = structure_indices same_frames = True if selection is not None: if group_behavior == 'center_of_mass': coordinates_1 = get_center_of_mass( molecular_system, selection=selection, structure_indices=structure_indices) atom_indices_1 = [0] elif group_behavior == 'geometric_center': coordinates_1 = get_geometric_center( molecular_system, selection=selection, structure_indices=structure_indices) atom_indices_1 = [0] else: atom_indices_1 = select(molecular_system, selection=selection, syntaxis=syntaxis) coordinates_1 = get(molecular_system, target='atom', indices=atom_indices_1, structure_indices=structure_indices, coordinates=True) else: if group_behavior == 'center_of_mass': coordinates_1 = get_center_of_mass( molecular_system, groups_of_atoms=groups_of_atoms, structure_indices=structure_indices) atom_indices_1 = np.range(coordinates_1.shape[1]) elif group_behavior == 'geometric_center': coordinates_1 = get_geometric_center( molecular_system, groups_of_atoms=groups_of_atoms, structure_indices=structure_indices) atom_indices_1 = np.arange(coordinates_1.shape[1]) else: raise ValueError( "Value of argument group_behavior not recognized.") if selection_2 is not None: if same_system and same_selection and same_frames: atom_indices_2 = atom_indices_1 coordinates_2 = coordinates_1 else: if group_behavior_2 == 'center_of_mass': coordinates_2 = get_center_of_mass( molecular_system_2, selection=selection_2, structure_indices=structure_indices_2) atom_indices_2 = [0] elif group_behavior_2 == 'geometric_center': coordinates_2 = get_geometric_center( molecular_system_2, selection=selection_2, structure_indices=structure_indices_2) atom_indices_2 = [0] else: atom_indices_2 = select(molecular_system_2, selection=selection_2, syntaxis=syntaxis) coordinates_2 = get(molecular_system_2, target='atom', indices=atom_indices_2, structure_indices=structure_indices_2, coordinates=True) else: if same_system and same_groups and same_frames: atom_indices_2 = atom_indices_1 coordinates_2 = coordinates_1 else: if group_behavior_2 == 'center_of_mass': coordinates_2 = get_center_of_mass( molecular_system_2, groups_of_atoms=groups_of_atoms_2, structure_indices=structure_indices_2) atom_indices_2 = np.arange(coordinates_2.shape[1]) elif group_behavior_2 == 'geometric_center': coordinates_2 = get_geometric_center( molecular_system_2, groups_of_atoms=groups_of_atoms_2, structure_indices=structure_indices_2) atom_indices_2 = np.arange(coordinates_2.shape[1]) else: raise ValueError( "Value of argument group_behavior_2 not recognized.") diff_set = not ((same_system and same_selection and same_frames) or (same_system and same_groups and same_frames)) length_units = puw.get_unit(coordinates_1) coordinates_1 = np.asfortranarray(puw.get_value(coordinates_1), dtype='float64') coordinates_2 = np.asfortranarray(puw.get_value(coordinates_2), dtype='float64') nframes_1 = coordinates_1.shape[0] nframes_2 = coordinates_2.shape[0] nelements1 = coordinates_1.shape[1] nelements2 = coordinates_2.shape[1] if pbc: box, box_shape = get(molecular_system, target='system', box=True, box_shape=True, structure_indices=structure_indices) 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 else: box = np.zeros([nframes_1, 3, 3]) * length_units orthogonal = 1 box = np.asfortranarray(puw.get_value(box), dtype='float64') if crossed_frames is False: if nframes_1 != nframes_2: raise ValueError( "Both structure_indices and structure_indices_2 need the same number of frames." ) else: if pairs is False: dists = libgeometry.distance(int(diff_set), coordinates_1, coordinates_2, box, orthogonal, int(pbc), nelements1, nelements2, nframes_1) else: if nframes_1 != nframes_2: raise ValueError( "Both selection and selection_2 need the same number of atoms." ) else: dists = libgeometry.distance_pairs( coordinates_1, coordinates_2, box, orthogonal, int(pbc), nelements1, nelements2, nframes_1) else: raise NotImplementedError(NotImplementedMessage) del (coordinates_1, coordinates_2, box) dists = dists * length_units if output_form == 'tensor': if output_structure_indices: if structure_indices is 'all': structure_indices = np.arange(nframes_1) if structure_indices_2 is 'all': structure_indices_2 = np.arange(nframes_2) if output_atom_indices: return atom_indices_1, structure_indices, atom_indices_2, structure_indices_2, dists else: return structure_indices, structure_indices_2, dists elif output_atom_indices: return atom_indices_1, atom_indices_2, dists else: return dists elif output_form == 'dict': if structure_indices is 'all': structure_indices = np.arange(nframes_1) if structure_indices_2 is 'all': structure_indices_2 = np.arange(nframes_2) if pairs is False: if crossed_frames is False: tmp_dict = {} for ii in range(len(atom_indices_1)): atom1 = atom_indices_1[ii] tmp_dict[atom1] = {} for jj in range(len(atom_indices_2)): atom2 = atom_indices_2[jj] tmp_dict[atom1][atom2] = {} for kk in range(len(structure_indices)): frame_index_1 = structure_indices[kk] tmp_dict[atom1][atom2][frame_index_1] = dists[ kk, ii, jj] return tmp_dict else: raise NotImplementedError(NotImplementedMessage) else: if crossed_frames is False: tmp_dict = {} for ii in range(len(atom_indices_1)): atom1 = atom_indices_1[ii] atom2 = atom_indices_2[ii] if atom1 not in tmp_dict: tmp_dict[atom1] = {} if atom2 not in tmp_dict[atom1]: tmp_dict[atom1][atom2] = {} for kk in range(len(structure_indices)): frame_index_1 = structure_indices[kk] tmp_dict[atom1][atom2][frame_index_1] = dists[kk, ii] return tmp_dict else: raise NotImplementedError(NotImplementedMessage) else: raise NotImplementedError(NotImplementedMessage) elif engine == 'MDTraj': #tmp_item1, atom_indices1, structure_indices1, tmp_item2, atom_indices2, structure_indices2,\ #single_item, single_selection = _digest_comparison_two_systems(item, selection, frame,\ # item2, selection2, frame2,\ # form='mdtraj.Trajectory') #if (group_behavior is None) and (group_behavior2 is None): # if item2 is None: # from mdtraj import compute_distances as _mdtraj_compute_distances # tensor1_to_grid, tensor2_to_grid = np.meshgrid(atom_indices1,atom_indices2) # pairs_list =np.vstack([tensor1_to_grid.ravel(), tensor2_to_grid.ravel()]).T # dists = _mdtraj_compute_distances(tmp_item1,pairs_list,pbc) # if output_form=='matrix': # nframes=dists.shape[0] # del(_mdtraj_compute_distances,pairs_list) # return dists.reshape(len(atom_indices2),len(atom_indices1),nframes).T # elif output_form=='dict': # tmp_dict={} # for kk in range(dists.shape[1]): # ii=pairs_list[kk,0] # jj=pairs_list[kk,1] # try: # tmp_dict[ii][jj]=dists[:,kk] # except: # tmp_dict[ii]={} # tmp_dict[ii][jj]=dists[:,kk] # del(_mdtraj_compute_distances,pairs_list) # return tmp_dict # else: # raise NotImplementedError(NotImplementedMessage) # else: # raise NotImplementedError(NotImplementedMessage) #else: # raise NotImplementedError(NotImplementedMessage) raise NotImplementedError(NotImplementedMessage) else: raise NotImplementedError(NotImplementedMessage)