def _calc_min_distance(self, walker): """Min-min distance for a walker. Parameters ---------- walker : object implementing the Walker interface Returns ------- min_distance : float """ cell_lengths, cell_angles = box_vectors_to_lengths_angles(walker.state['box_vectors']) t2 = time.time() # make a traj out of it so we can calculate distances through # the periodic boundary conditions walker_traj = mdj.Trajectory(walker.state['positions'], topology=self._mdj_top, unitcell_lengths=cell_lengths, unitcell_angles=cell_angles) t3 = time.time() # calculate the distances through periodic boundary conditions # and get hte minimum distance min_distance = np.min(mdj.compute_distances(walker_traj, it.product(self.ligand_idxs, self.receptor_idxs), periodic=self._periodic) ) t4 = time.time() logging.info("Make a traj: {0}; Calc dists: {1}".format(t3-t2,t4-t3)) return min_distance
def _unaligned_image(self, state): """ Parameters ---------- state : Returns ------- """ # get the box lengths from the vectors box_lengths, box_angles = box_vectors_to_lengths_angles( state['box_vectors']) # recenter the protein-ligand complex into the center of the # periodic boundary conditions # regroup the ligand and protein in together grouped_positions = group_pair(state['positions'], box_lengths, self._bs_idxs, self._lig_idxs) # then center them around the binding site centered_positions = center_around(grouped_positions, self._bs_idxs) # slice these positions to get the image state_image = centered_positions[self._image_idxs] return state_image
def __init__(self, *, init_state=None, json_topology=None, main_rep_idxs=None, **kwargs): super().__init__(**kwargs) assert json_topology is not None, "must give a JSON format topology" assert main_rep_idxs is not None, "must give the indices of the atoms the topology represents" assert init_state is not None, "must give an init state for the topology PDB" self.main_rep_idxs = main_rep_idxs # take a subset of the topology using the main rep atom idxs self.json_main_rep_top = json_top_subset(json_topology, self.main_rep_idxs) # get the main rep idxs only self.init_main_rep_positions = init_state['positions'][ self.main_rep_idxs] # convert the box vectors self.init_unitcell_lengths, self.init_unitcell_angles = box_vectors_to_lengths_angles( init_state['box_vectors'])
def binding_site_idxs(json_topology, coords, box_vectors, cutoff): # convert quantities to numbers in nanometers cutoff = cutoff.value_in_unit(unit.nanometer) coords = coords.value_in_unit(unit.nanometer) box_lengths, box_angles = box_vectors_to_lengths_angles( box_vectors.value_in_unit(unit.nanometer)) # selecting ligand and protein binding site atom indices for # resampler and boundary conditions lig_idxs = ligand_idxs(json_topology) prot_idxs = protein_idxs(json_topology) # make a trajectory to compute the neighbors from traj = mdj.Trajectory(np.array([coords]), unitcell_lengths=[box_lengths], unitcell_angles=[box_angles], topology=json_to_mdtraj_topology(json_topology)) # selects protein atoms which have less than 8 A from ligand # atoms in the crystal structure neighbors_idxs = mdj.compute_neighbors(traj, cutoff, lig_idxs) # selects protein atoms from neighbors list binding_selection_idxs = np.intersect1d(neighbors_idxs, prot_idxs) return binding_selection_idxs
def _calc_min_distance(self, walker): """Min-min distance for a walker. Parameters ---------- walker : object implementing the Walker interface Returns ------- min_distance : float """ cell_lengths, cell_angles = box_vectors_to_lengths_angles( walker.state['box_vectors']) # convert the json topology to an mdtraj one mdj_top = json_to_mdtraj_topology(self._topology) # make a traj out of it so we can calculate distances through # the periodic boundary conditions walker_traj = mdj.Trajectory(walker.state['positions'], topology=mdj_top, unitcell_lengths=cell_lengths, unitcell_angles=cell_angles) # calculate the distances through periodic boundary conditions # and get hte minimum distance min_distance = np.min( mdj.compute_distances( walker_traj, it.product(self.ligand_idxs, self.receptor_idxs))) return min_distance
def get_incr(self, k1, d0_1, state1, k2, d0_2): # k old, d0 old (inital, used values), current state, # (k new, d0 new <-- both not used in simulation yet) # old = a, new = b # get the uncentered positions # state1 = current state pos = np.array(state1['positions']) # get the distance for the current state box_vectors = state1['box_vectors'] unitcell_lengths, unitcell_angles = box_vectors_to_lengths_angles( box_vectors) unitcell_lengths = np.array(unitcell_lengths) traj = mdj.Trajectory(pos, self.topology, unitcell_lengths=unitcell_lengths, unitcell_angles=unitcell_angles) # new dist, b dist = mdj.compute_distances(traj, [(0, 1)], periodic=self.periodic_state) # integrate to calculate work (a = old, b = new) val_a = k1 / 2 * (dist - d0_1)**2 val_b = k2 / 2 * (dist - d0_2)**2 work = val_b - val_a return work
def binding_site_idxs(json_topology, ligand_idxs, receptor_idxs, coords, box_vectors, cutoff, periodic=True): """ Parameters ---------- json_topology : str ligand_idxs : arraylike (1,) receptor_idxs : arraylike (1,) coords : simtk.Quantity box_vectors : simtk.Quantity cutoff : float Returns ------- binding_site_idxs : arraylike (1,) """ # convert quantities to numbers in nanometers cutoff = cutoff.value_in_unit(unit.nanometer) coords = coords.value_in_unit(unit.nanometer) box_lengths, box_angles = box_vectors_to_lengths_angles( box_vectors.value_in_unit(unit.nanometer)) # make a trajectory to compute the neighbors from traj = mdj.Trajectory(np.array([coords]), unitcell_lengths=[box_lengths], unitcell_angles=[box_angles], topology=json_to_mdtraj_topology(json_topology)) neighbors_idxs = mdj.compute_neighbors(traj, cutoff, ligand_idxs, periodic=periodic)[0] # selects protein atoms from neighbors list binding_selection_idxs = np.intersect1d(neighbors_idxs, receptor_idxs) return binding_selection_idxs
def _progress(self, walker): """Calculate if the walker has bound and provide progress record. Parameters ---------- walker : object implementing the Walker interface Returns ------- is_bound : bool Whether the walker is unbound (warped) or not progress_data : dict of str : value Dictionary of the progress record group fields for this walker alone. """ # first recenter the ligand and the receptor in the walker box_lengths, box_angles = box_vectors_to_lengths_angles( walker.state['box_vectors']) grouped_walker_pos = group_pair(walker.state['positions'], box_lengths, self.binding_site_idxs, self.ligand_idxs) # center the positions around the center of the binding site centered_walker_pos = center_around(grouped_walker_pos, self.binding_site_idxs) # superimpose the walker state positions over the native state # matching the binding site indices only sup_walker_pos, _, _ = superimpose(self.native_state['positions'], centered_walker_pos, idxs=self.binding_site_idxs) # calculate the rmsd of the walker ligand (superimposed # according to the binding sites) to the native state ligand native_rmsd = calc_rmsd(self.native_state['positions'], sup_walker_pos, idxs=self.ligand_idxs) # test to see if the ligand is re-bound rebound = False if native_rmsd <= self.cutoff_rmsd: rebound = True progress_data = {'native_rmsd': native_rmsd} return rebound, progress_data
def _unaligned_image(self, state): # get the box lengths from the vectors box_lengths, box_angles = box_vectors_to_lengths_angles( state['box_vectors']) # recenter the protein-ligand complex into the center of the # periodic boundary conditions rece_positions = recenter_pair(state['positions'], box_lengths, self._bs_idxs, self._lig_idxs) # slice these positions to get the image state_image = rece_positions[self._image_idxs] return state_image
def __init__(self, *, init_state=None, json_topology=None, main_rep_idxs=None, **kwargs): """Constructor for the WalkerReporter. Parameters ---------- init_state : object implementing WalkerState An initial state, only used for writing the PDB topology. json_topology : str A molecular topology in the common JSON format, that matches the main_rep_idxs. main_rep_idxs : listlike of int The indices of the atoms to select from the full representation. """ super().__init__(**kwargs) assert json_topology is not None, "must give a JSON format topology" assert init_state is not None, "must give an init state for the topology PDB" # if the main rep indices were not given infer them as all of the atoms if main_rep_idxs is None: self.main_rep_idxs = list(range(init_state['positions'].shape[0])) else: self.main_rep_idxs = main_rep_idxs # take a subset of the topology using the main rep atom idxs self.json_main_rep_top = json_top_subset(json_topology, self.main_rep_idxs) # get the main rep idxs only self.init_main_rep_positions = init_state['positions'][ self.main_rep_idxs] # convert the box vectors self.init_unitcell_lengths, self.init_unitcell_angles = box_vectors_to_lengths_angles( init_state['box_vectors'])
def to_mdtraj(self, topology): """Returns an mdtraj.Trajectory object from this walker's state. Parameters ---------- topology : mdtraj.Topology object Topology for the state. Returns ------- state_traj : mdtraj.Trajectory object """ import mdtraj as mdj # resize the time to a 1D vector unitcell_lengths, unitcell_angles = box_vectors_to_lengths_angles(self.box_vectors) return mdj.Trajectory([self.positions], unitcell_lengths=[unitcell_lengths], unitcell_angles=[unitcell_angles], topology=topology)
def _unaligned_image(self, state): """The preprocessing method of states. First it groups the binding site and ligand into the same periodic box image and then centers the box around their mutual center of mass and returns only the positions of the binding site and ligand. Parameters ---------- state : object implementing WalkerState State with 'positions' (Nx3 dims) and 'box_vectors' (3x3 array) attributes. Returns ------- """ # get the box lengths from the vectors box_lengths, box_angles = box_vectors_to_lengths_angles( state['box_vectors']) # recenter the protein-ligand complex into the center of the # periodic boundary conditions # regroup the ligand and protein in together grouped_positions = group_pair(state['positions'], box_lengths, self._bs_idxs, self._lig_idxs) # then center them around the binding site centered_positions = center_around(grouped_positions, self._bs_idxs) # slice these positions to get the image state_image = centered_positions[self._image_idxs] return state_image
def binding_site_idxs(json_topology, ligand_idxs, receptor_idxs, coords, box_vectors, cutoff, periodic=True): """Parameters ---------- json_topology : str ligand_idxs : arraylike (1,) receptor_idxs : arraylike (1,) coords : N x 3 arraylike of float or simtk.Quantity If not a quantity will implicitly be treated as being in nanometers. box_vectors : simtk.Quantity If not a quantity will implicitly be treated as being in nanometers. cutoff : float or simtk.Quantity If not a quantity will implicitly be treated as being in nanometers. Returns ------- binding_site_idxs : arraylike (1,) """ # if they are simtk.units convert quantities to numbers in # nanometers if unit.is_quantity(cutoff): cutoff = cutoff.value_in_unit(unit.nanometer) if unit.is_quantity(coords): coords = coords.value_in_unit(unit.nanometer) if unit.is_quantity(box_vectors): box_vectors = box_vectors.value_in_unit(unit.nanometer) box_lengths, box_angles = box_vectors_to_lengths_angles(box_vectors) # make a trajectory to compute the neighbors from traj = mdj.Trajectory(np.array([coords]), unitcell_lengths=[box_lengths], unitcell_angles=[box_angles], topology=json_to_mdtraj_topology(json_topology)) neighbors_idxs = mdj.compute_neighbors(traj, cutoff, ligand_idxs, periodic=periodic)[0] # selects protein atoms from neighbors list binding_selection_idxs = np.intersect1d(neighbors_idxs, receptor_idxs) return binding_selection_idxs