def test_contact_matrix(): d = np.arange(5) radius = np.ones(5) * 2.5 out = contacts.contact_matrix(d, radius) assert_array_equal(out, [True, True, True, False, False]) # check in-place update out = np.empty(out.shape) contacts.contact_matrix(d, radius, out=out) assert_array_equal(out, [True, True, True, False, False])
def test_contact_matrix(): d = np.arange(5) radius = np.ones(5) * 2.5 out = contacts.contact_matrix(d, radius) assert_array_equal(out, [True, True, True, False, False]) # check in-place update out = np.empty(out.shape) contacts.contact_matrix(d, radius, out=out) assert_array_equal(out, [True, True, True, False, False])
def __init__(self, mobiles, refs, method="hard_cut", radius=4.5, kwargs=None): """ Parameters ---------- mobiles : tuple(AtomGroup, AtomGroup) two contacting groups that change over time refs : tuple(AtomGroup, AtomGroup) two contacting atomgroups in their reference conformation. This can also be a list of tuples containing different atom groups radius : float, optional (4.5 Angstroms) radius within which contacts exist in refgroup method : string | callable (optional) Can either be one of ``['hard_cut' , 'soft_cut']`` or a callable with call signature ``func(r, r0, **kwargs)`` (the "Contacts API"). kwargs : dict, optional dictionary of additional kwargs passed to `method`. Check respective functions for reasonable values. """ universe = mobiles[0].universe super(Contacts, self).__init__(universe, mobiles) if method == 'hard_cut': self.fraction_contacts = hard_cut_q elif method == 'soft_cut': self.fraction_contacts = soft_cut_q else: if not callable(method): raise ValueError("method has to be callable") self.fraction_contacts = method # contacts formed in reference self.r0 = [] self.initial_contacts = [] if isinstance(refs[0], mda.core.groups.AtomGroup): refA, refB = refs self.r0.append(distance_array(refA.positions, refB.positions)) self.initial_contacts.append(contact_matrix(self.r0[-1], radius)) else: for refA, refB in refs: self.r0.append(distance_array(refA.positions, refB.positions)) self.initial_contacts.append( contact_matrix(self.r0[-1], radius)) self.fraction_kwargs = kwargs if kwargs is not None else {}
def contact_maps_from_traj(pdb_file, traj_file, contact_cutoff=8.0, savefile=None): """ Get contact map from trajectory. """ mda_traj = mda.Universe(pdb_file, traj_file) traj_length = len(mda_traj.trajectory) ca = mda_traj.select_atoms('name CA') if savefile: savefile = os.path.abspath(savefile) outfile = tables.open_file(savefile, 'w') atom = tables.Float64Atom() cm_table = outfile.create_earray(outfile.root, 'contact_maps', atom, shape=(traj_length, 0)) contact_matrices = [] for frame in mda_traj.trajectory: cm_matrix = contacts.contact_matrix(distances.self_distance_array( ca.positions), radius=contact_cutoff) * 1.0 contact_matrices.append(cm_matrix) if savefile: cm_table.append(contact_matrices) outfile.close() return contact_matrices
def test_radius_cut_method(self, universe): acidic = universe.select_atoms(self.sel_acidic) basic = universe.select_atoms(self.sel_basic) r = contacts.distance_array(acidic.positions, basic.positions) initial_contacts = contacts.contact_matrix(r, 6.0) expected = [] for ts in universe.trajectory: r = contacts.distance_array(acidic.positions, basic.positions) expected.append(contacts.radius_cut_q(r[initial_contacts], None, radius=6.0)) ca = self._run_Contacts(universe, method='radius_cut') assert_array_equal(ca.timeseries[:, 1], expected)
def report(self, simulation, state): ca_indices = [] for atom in simulation.topology.atoms(): if atom.name == 'CA': ca_indices.append(atom.index) positions = np.array(state.getPositions().value_in_unit(u.angstrom)) time = int(np.round(state.getTime().value_in_unit(u.picosecond))) positions_ca = positions[ca_indices].astype(np.float32) distance_matrix = distances.self_distance_array(positions_ca) contact_map = contacts.contact_matrix(distance_matrix, radius=8.0) * 1.0 self._out.create_dataset(str(time), data=contact_map)