def _process_frame(self, trj, energy, hbonds, entropy): """ Frame wise calculation of GIST quantities. Parameters ---------- trj : Molecular dynamic trajectory representing current frame. energy : If True, solute-water and water-water energies are calculated for each water in each voxel in current frame. hbonds : bool If True, solute-water and water-water hydrogen bonds are calculated for each water in each voxel in current frame. entropy : bool If True, water coordinates and quaternions are stored for each water in each voxel in current frame. """ nbr_cutoff_sq = 3.5 ** 2 trj.xyz *= 10.0 coords = trj.xyz uc = trj.unitcell_vectors[0]*10. waters = [] calc.assign_voxels(trj.xyz, self.dims, self.gridmax, self.origin, waters, self.wat_oxygen_atom_ids) for wat in waters: self.voxeldata[wat[0], 4] += 1 if energy or hbonds: e_lj_array, e_elec_array = np.copy(self.acoeff), np.copy(self.chg_product) distance_matrix = np.zeros((self.water_sites, self.all_atom_ids.shape[0])) calc.get_pairwise_distances(wat, self.all_atom_ids, coords, uc, distance_matrix) wat_nbrs = self.wat_oxygen_atom_ids[np.where( (distance_matrix[0, :][self.wat_oxygen_atom_ids] <= nbr_cutoff_sq) & ( distance_matrix[0, :][self.wat_oxygen_atom_ids] > 0.0))] self.voxeldata[wat[0], 19] += wat_nbrs.shape[0] calc.calculate_energy(wat[1], distance_matrix, e_elec_array, e_lj_array, self.bcoeff) if self.prot_atom_ids.shape[0] != 0: #self.voxeldata[wat[0], 13] += np.sum(e_lj_array[:, self.prot_atom_ids]) #self.voxeldata[wat[0], 13] += np.sum(e_elec_array[:, self.prot_atom_ids]) self.voxeldata[wat[0], 13] += np.sum(e_lj_array[:, self.non_water_atom_ids]) self.voxeldata[wat[0], 13] += np.sum(e_elec_array[:, self.non_water_atom_ids]) self.voxeldata[wat[0], 15] += np.sum( e_lj_array[:, self.wat_oxygen_atom_ids[0]:wat[1]]) + np.sum(e_lj_array[:, wat[1] + self.water_sites:]) self.voxeldata[wat[0], 15] += np.sum( e_elec_array[:, self.wat_oxygen_atom_ids[0]:wat[1]]) + np.sum( e_elec_array[:, wat[1] + self.water_sites:]) e_nbr_list = [np.sum(e_lj_array[:, wat_nbrs + i] + e_elec_array[:, wat_nbrs + i]) for i in range(self.water_sites)] self.voxeldata[wat[0], 17] += np.sum(e_nbr_list) # H-bond calculations if hbonds: prot_nbrs_all = self.prot_atom_ids[ np.where(distance_matrix[0, :][self.prot_atom_ids] <= nbr_cutoff_sq)] prot_nbrs_hb = prot_nbrs_all[np.where(self.prot_hb_types[prot_nbrs_all] != 0)] if wat_nbrs.shape[0] > 0: hb_ww = self.calculate_hydrogen_bonds(trj, wat[1], wat_nbrs) acc_ww = hb_ww[:, 0][np.where(hb_ww[:, 0] == wat[1])].shape[0] don_ww = hb_ww.shape[0] - acc_ww self.voxeldata[wat[0], 25] += hb_ww.shape[0] self.voxeldata[wat[0], 31] += don_ww self.voxeldata[wat[0], 33] += acc_ww if wat_nbrs.shape[0] != 0 and hb_ww.shape[0] != 0: self.voxeldata[wat[0], 21] += wat_nbrs.shape[0] / hb_ww.shape[0] if prot_nbrs_hb.shape[0] > 0: hb_sw = self.calculate_hydrogen_bonds(trj, wat[1], prot_nbrs_hb, water_water=False) acc_sw = hb_sw[:, 0][np.where(hb_sw[:, 0] == wat[1])].shape[0] don_sw = hb_sw.shape[0] - acc_sw self.voxeldata[wat[0], 23] += hb_sw.shape[0] self.voxeldata[wat[0], 27] += don_sw self.voxeldata[wat[0], 29] += acc_sw if entropy: self.calculate_euler_angles(wat, coords[0, :, :])
def process_chunk(self, begin_chunk, chunk_size, topology, energy, hbonds, entropy): nbr_cutoff_sq = 3.5**2 with md.open(self.trajectory) as f: f.seek(begin_chunk) trj = f.read_as_traj(topology, n_frames=chunk_size, stride=1) trj.xyz *= 10.0 pbc = md.utils.in_units_of(trj.unitcell_lengths, "nanometers", "angstroms") frame_data = [[] for i in range(trj.n_frames)] calc.assign_voxels(trj.xyz, self.dims, self.gridmax, self.origin, frame_data, self.wat_oxygen_atom_ids) for frame in range(trj.n_frames): coords = trj.xyz[frame, :, :].reshape(1, trj.xyz.shape[1], trj.xyz.shape[2]) periodic_box = pbc[frame].reshape(1, pbc.shape[1]) waters = frame_data[frame] for wat in waters: self.voxeldata[wat[0], 4] += 1 if energy or hbonds: e_lj_array, e_elec_array = np.copy( self.acoeff), np.copy(self.chg_product) distance_matrix = np.zeros( (self.water_sites, self.all_atom_ids.shape[0])) calc.get_pairwise_distances(wat, self.all_atom_ids, coords, pbc, distance_matrix) wat_nbrs = self.wat_oxygen_atom_ids[np.where( (distance_matrix[0, :][self.wat_oxygen_atom_ids] <= nbr_cutoff_sq) & (distance_matrix[0, :][ self.wat_oxygen_atom_ids] > 0.0))] self.voxeldata[wat[0], 17] += wat_nbrs.shape[0] calc.calculate_energy(wat[1], distance_matrix, e_elec_array, e_lj_array, self.bcoeff) self.voxeldata[wat[0], 11] += np.sum( e_lj_array[:, :self.wat_oxygen_atom_ids[0]]) self.voxeldata[wat[0], 11] += np.sum( e_elec_array[:, :self.wat_oxygen_atom_ids[0]]) self.voxeldata[wat[0], 13] += np.sum( e_lj_array[:, self.wat_oxygen_atom_ids[0]:wat[1]] ) + np.sum(e_lj_array[:, wat[1] + self.water_sites:]) self.voxeldata[wat[0], 13] += np.sum( e_elec_array[:, self.wat_oxygen_atom_ids[0]:wat[1]] ) + np.sum(e_elec_array[:, wat[1] + self.water_sites:]) e_nbr_list = [ np.sum(e_lj_array[:, wat_nbrs + i] + e_elec_array[:, wat_nbrs + i]) for i in xrange(self.water_sites) ] self.voxeldata[wat[0], 15] += np.sum(e_nbr_list) """ ###DEBUG START### elj_sw = np.sum(e_lj_array[:, :self.wat_oxygen_atom_ids[0]]) eelec_sw = np.sum(e_elec_array[:, :self.wat_oxygen_atom_ids[0]]) elj_ww = np.sum(e_lj_array[:, self.wat_oxygen_atom_ids[0]:wat[1]]) + np.sum(e_lj_array[:, wat[1] + 1:]) eelec_ww = np.sum(e_elec_array[:, self.wat_oxygen_atom_ids[0]:wat[1]]) + np.sum(e_elec_array[:, wat[1] + self.water_sites:]) e_nbr_list = [np.sum(e_lj_array[:, wat_nbrs + i] + e_elec_array[:, wat_nbrs + i]) for i in xrange(self.water_sites)] enbr = np.sum(e_nbr_list) print "Calc: ", elj_sw, eelec_sw, elj_ww, eelec_ww, enbr distance_matrix = np.sqrt(distance_matrix) energy_lj, energy_elec = self.calculate_energy(distance_matrix) test_1 = np.sum(energy_lj[:self.wat_oxygen_atom_ids[0]:]) test_2 = np.sum(energy_elec[:, self.non_water_atom_ids]) test_3 = np.nansum(energy_lj[self.wat_oxygen_atom_ids[0]:]) test_4 = np.sum(energy_elec[:, self.wat_atom_ids[0]:wat[1]]) + np.sum(energy_elec[:, wat[1] + self.water_sites:]) test_5 = 0.0 test_5 += np.sum(energy_lj[self.wat_oxygen_atom_ids[0]:][(wat_nbrs - self.wat_oxygen_atom_ids[0]) / self.water_sites]) for i in range(self.water_sites): test_5 += np.sum(energy_elec[:, wat_nbrs + i]) print "Ref: ", test_1, test_2, test_3, test_4, test_5 ###DEBUG END### """ # H-bond calculations if hbonds: prot_nbrs_all = self.non_water_atom_ids[np.where( distance_matrix[0, :][ self.non_water_atom_ids] <= nbr_cutoff_sq)] prot_nbrs_hb = prot_nbrs_all[np.where( self.prot_hb_types[prot_nbrs_all] != 0)] if wat_nbrs.shape[0] != 0 and prot_nbrs_hb.shape[ 0] != 0: # hb_ww, hb_sw = self.calculate_hydrogen_bonds2(coords, wat[1], wat_nbrs, prot_nbrs_hb) hb_ww, hb_sw = self.calculate_hydrogen_bonds( trj, wat[1], wat_nbrs, prot_nbrs_hb) acc_ww = hb_ww[:, 0][np.where( hb_ww[:, 0] == wat[1])].shape[0] don_ww = hb_ww.shape[0] - acc_ww acc_sw = hb_sw[:, 0][np.where( hb_sw[:, 0] == wat[1])].shape[0] don_sw = hb_sw.shape[0] - acc_sw self.voxeldata[wat[0], 23] += hb_sw.shape[0] self.voxeldata[wat[0], 25] += hb_ww.shape[0] self.voxeldata[wat[0], 27] += don_sw self.voxeldata[wat[0], 29] += acc_sw self.voxeldata[wat[0], 31] += don_ww self.voxeldata[wat[0], 33] += acc_ww if wat_nbrs.shape[0] != 0 and hb_ww.shape[ 0] != 0: self.voxeldata[wat[0], 19] += wat_nbrs.shape[ 0] / hb_ww.shape[0] # f_enc = 1.0 - (wat_nbrs.shape[0] / 5.25) # if f_enc < 0.0: # f_enc = 0.0 # self.voxeldata[wat[0], 21] += f_enc if entropy: self.calculate_euler_angles(wat, coords[0, :, :])
def _process_frame(self, trj, energy, hbonds, entropy): """ Frame wise calculation of GIST quantities. Parameters ---------- trj : Molecular dynamic trajectory representing current frame. energy : If True, solute-water and water-water energies are calculated for each water in each voxel in current frame. hbonds : bool If True, solute-water and water-water hydrogen bonds are calculated for each water in each voxel in current frame. entropy : bool If True, water coordinates and quaternions are stored for each water in each voxel in current frame. """ nbr_cutoff_sq = 3.5**2 trj.xyz *= 10.0 coords = trj.xyz uc = trj.unitcell_vectors[0] * 10. waters = [] calc.assign_voxels(trj.xyz, self.dims, self.gridmax, self.origin, waters, self.wat_oxygen_atom_ids) distance_matrix = np.zeros( (self.water_sites, self.all_atom_ids.shape[0])) for wat in waters: self.voxeldata[wat[0], 4] += 1 ### wat[0]: Voxel index ### wat[1]: water molecule oxygen atom index if energy or hbonds: e_lj_array, e_elec_array = np.copy(self.acoeff), np.copy( self.chg_product) ### Here it is assumed that oxygen atom is always the first atom. Is that always the case? ### We should probably check that somewhere during init? ### ### The self.neighbor_ids array contains atom indices of all atoms that should ### be considered as potential neighbors. Valid_neighbors has same shape as ### self.neighbor_ids and is False at the position where index wat occures in ### self.neighbor_ids, otherwise it is True. neighbor_ids stores the indices of ### the actual neighbor candidates that will be commited to get_pairwise_distances ### routine and has length of self.neighbor_ids-1. wat_nbrs_shell is of length neighbor_ids ### and holds the shell_index of each neighbor candidate atom (0:first shell, 1: beyond first ### shell) valid_neighbors = np.ones(self.neighbor_ids.shape[0], dtype=bool) valid_neighbors[np.where(self.neighbor_ids == wat)] = False neighbor_ids = self.neighbor_ids[valid_neighbors] wat_nbrs_shell = self.wat_nbrs_shell[valid_neighbors] calc.get_pairwise_distances(wat, self.all_atom_ids, np.array([nbr_cutoff_sq]), neighbor_ids, wat_nbrs_shell, coords, uc, distance_matrix, 0) wat_nbrs = self.all_atom_ids[np.where(wat_nbrs_shell == 0)] self.voxeldata[wat[0], 19] += wat_nbrs.shape[0] calc.calculate_energy(wat[1], distance_matrix, e_elec_array, e_lj_array, self.bcoeff) if self.prot_atom_ids.shape[0] != 0: #self.voxeldata[wat[0], 13] += np.sum(e_lj_array[:, self.prot_atom_ids]) #self.voxeldata[wat[0], 13] += np.sum(e_elec_array[:, self.prot_atom_ids]) self.voxeldata[wat[0], 13] += np.sum( e_lj_array[:, self.non_water_atom_ids]) self.voxeldata[wat[0], 13] += np.sum( e_elec_array[:, self.non_water_atom_ids]) self.voxeldata[wat[0], 15] += np.sum( e_lj_array[:, self.wat_oxygen_atom_ids[0]:wat[1]]) + np.sum( e_lj_array[:, wat[1] + self.water_sites:]) self.voxeldata[wat[0], 15] += np.sum( e_elec_array[:, self.wat_oxygen_atom_ids[0]:wat[1]] ) + np.sum(e_elec_array[:, wat[1] + self.water_sites:]) e_nbr_list = [ np.sum(e_lj_array[:, wat_nbrs + i] + e_elec_array[:, wat_nbrs + i]) for i in range(self.water_sites) ] self.voxeldata[wat[0], 17] += np.sum(e_nbr_list) ### Might be usefull for API to have the neighbors and shell ### indices available. self.wat_nbrs_shell[valid_neighbors] = wat_nbrs_shell self.neighbor_ids[valid_neighbors] = neighbor_ids """ ###DEBUG START### elj_sw = np.sum(e_lj_array[:, :self.wat_oxygen_atom_ids[0]]) eelec_sw = np.sum(e_elec_array[:, :self.wat_oxygen_atom_ids[0]]) elj_ww = np.sum(e_lj_array[:, self.wat_oxygen_atom_ids[0]:wat[1]]) + np.sum(e_lj_array[:, wat[1] + 1:]) eelec_ww = np.sum(e_elec_array[:, self.wat_oxygen_atom_ids[0]:wat[1]]) + np.sum(e_elec_array[:, wat[1] + self.water_sites:]) e_nbr_list = [np.sum(e_lj_array[:, wat_nbrs + i] + e_elec_array[:, wat_nbrs + i]) for i in xrange(self.water_sites)] enbr = np.sum(e_nbr_list) print "Calc: ", elj_sw, eelec_sw, elj_ww, eelec_ww, enbr distance_matrix = np.sqrt(distance_matrix) energy_lj, energy_elec = self.calculate_energy(distance_matrix) test_1 = np.sum(energy_lj[:self.wat_oxygen_atom_ids[0]:]) test_2 = np.sum(energy_elec[:, self.non_water_atom_ids]) test_3 = np.nansum(energy_lj[self.wat_oxygen_atom_ids[0]:]) test_4 = np.sum(energy_elec[:, self.wat_atom_ids[0]:wat[1]]) + np.sum(energy_elec[:, wat[1] + self.water_sites:]) test_5 = 0.0 test_5 += np.sum(energy_lj[self.wat_oxygen_atom_ids[0]:][(wat_nbrs - self.wat_oxygen_atom_ids[0]) / self.water_sites]) for i in range(self.water_sites): test_5 += np.sum(energy_elec[:, wat_nbrs + i]) print "Ref: ", test_1, test_2, test_3, test_4, test_5 ###DEBUG END### """ # H-bond calculations if hbonds: prot_nbrs_all = self.prot_atom_ids[np.where( distance_matrix[0, :][ self.prot_atom_ids] <= nbr_cutoff_sq)] prot_nbrs_hb = prot_nbrs_all[np.where( self.prot_hb_types[prot_nbrs_all] != 0)] if wat_nbrs.shape[0] > 0: hb_ww = self.calculate_hydrogen_bonds( trj, wat[1], wat_nbrs) acc_ww = hb_ww[:, 0][np.where( hb_ww[:, 0] == wat[1])].shape[0] don_ww = hb_ww.shape[0] - acc_ww self.voxeldata[wat[0], 25] += hb_ww.shape[0] self.voxeldata[wat[0], 31] += don_ww self.voxeldata[wat[0], 33] += acc_ww if wat_nbrs.shape[0] != 0 and hb_ww.shape[0] != 0: self.voxeldata[ wat[0], 21] += wat_nbrs.shape[0] / hb_ww.shape[0] if prot_nbrs_hb.shape[0] > 0: hb_sw = self.calculate_hydrogen_bonds( trj, wat[1], prot_nbrs_hb, water_water=False) acc_sw = hb_sw[:, 0][np.where( hb_sw[:, 0] == wat[1])].shape[0] don_sw = hb_sw.shape[0] - acc_sw self.voxeldata[wat[0], 23] += hb_sw.shape[0] self.voxeldata[wat[0], 27] += don_sw self.voxeldata[wat[0], 29] += acc_sw if entropy: self.calculate_euler_angles(wat, coords[0, :, :])
def _process_frame(self, trj, frame_i, energy, hbonds, entropy): nbr_cutoff_sq = 3.5**2 trj.xyz *= 10.0 coords = trj.xyz periodic_box = md.utils.in_units_of(trj.unitcell_lengths, "nanometers", "angstroms") waters = [] calc.assign_voxels(trj.xyz, self.dims, self.gridmax, self.origin, waters, self.wat_oxygen_atom_ids) for wat in waters: self.voxeldata[wat[0], 4] += 1 if energy or hbonds: e_lj_array, e_elec_array = np.copy(self.acoeff), np.copy( self.chg_product) distance_matrix = np.zeros( (self.water_sites, self.all_atom_ids.shape[0])) calc.get_pairwise_distances(wat, self.all_atom_ids, coords, periodic_box, distance_matrix) wat_nbrs = self.wat_oxygen_atom_ids[np.where( (distance_matrix[0, :][self.wat_oxygen_atom_ids] <= nbr_cutoff_sq) & (distance_matrix[0, :][self.wat_oxygen_atom_ids] > 0.0))] self.voxeldata[wat[0], 19] += wat_nbrs.shape[0] calc.calculate_energy(wat[1], distance_matrix, e_elec_array, e_lj_array, self.bcoeff) if self.prot_atom_ids.shape[0] != 0: self.voxeldata[wat[0], 13] += np.sum( e_lj_array[:, self.prot_atom_ids]) self.voxeldata[wat[0], 13] += np.sum( e_elec_array[:, self.prot_atom_ids]) self.voxeldata[wat[0], 15] += np.sum( e_lj_array[:, self.wat_oxygen_atom_ids[0]:wat[1]]) + np.sum( e_lj_array[:, wat[1] + self.water_sites:]) self.voxeldata[wat[0], 15] += np.sum( e_elec_array[:, self.wat_oxygen_atom_ids[0]:wat[1]] ) + np.sum(e_elec_array[:, wat[1] + self.water_sites:]) e_nbr_list = [ np.sum(e_lj_array[:, wat_nbrs + i] + e_elec_array[:, wat_nbrs + i]) for i in xrange(self.water_sites) ] self.voxeldata[wat[0], 17] += np.sum(e_nbr_list) """ ###DEBUG START### elj_sw = np.sum(e_lj_array[:, :self.wat_oxygen_atom_ids[0]]) eelec_sw = np.sum(e_elec_array[:, :self.wat_oxygen_atom_ids[0]]) elj_ww = np.sum(e_lj_array[:, self.wat_oxygen_atom_ids[0]:wat[1]]) + np.sum(e_lj_array[:, wat[1] + 1:]) eelec_ww = np.sum(e_elec_array[:, self.wat_oxygen_atom_ids[0]:wat[1]]) + np.sum(e_elec_array[:, wat[1] + self.water_sites:]) e_nbr_list = [np.sum(e_lj_array[:, wat_nbrs + i] + e_elec_array[:, wat_nbrs + i]) for i in xrange(self.water_sites)] enbr = np.sum(e_nbr_list) print "Calc: ", elj_sw, eelec_sw, elj_ww, eelec_ww, enbr distance_matrix = np.sqrt(distance_matrix) energy_lj, energy_elec = self.calculate_energy(distance_matrix) test_1 = np.sum(energy_lj[:self.wat_oxygen_atom_ids[0]:]) test_2 = np.sum(energy_elec[:, self.non_water_atom_ids]) test_3 = np.nansum(energy_lj[self.wat_oxygen_atom_ids[0]:]) test_4 = np.sum(energy_elec[:, self.wat_atom_ids[0]:wat[1]]) + np.sum(energy_elec[:, wat[1] + self.water_sites:]) test_5 = 0.0 test_5 += np.sum(energy_lj[self.wat_oxygen_atom_ids[0]:][(wat_nbrs - self.wat_oxygen_atom_ids[0]) / self.water_sites]) for i in range(self.water_sites): test_5 += np.sum(energy_elec[:, wat_nbrs + i]) print "Ref: ", test_1, test_2, test_3, test_4, test_5 ###DEBUG END### """ # H-bond calculations if hbonds: prot_nbrs_all = self.non_water_atom_ids[np.where( distance_matrix[0, :][ self.non_water_atom_ids] <= nbr_cutoff_sq)] prot_nbrs_hb = prot_nbrs_all[np.where( self.prot_hb_types[prot_nbrs_all] != 0)] if wat_nbrs.shape[0] > 0: hb_ww = self.calculate_hydrogen_bonds( trj, wat[1], wat_nbrs) acc_ww = hb_ww[:, 0][np.where( hb_ww[:, 0] == wat[1])].shape[0] don_ww = hb_ww.shape[0] - acc_ww self.voxeldata[wat[0], 25] += hb_ww.shape[0] self.voxeldata[wat[0], 31] += don_ww self.voxeldata[wat[0], 33] += acc_ww if wat_nbrs.shape[0] != 0 and hb_ww.shape[0] != 0: self.voxeldata[ wat[0], 21] += wat_nbrs.shape[0] / hb_ww.shape[0] if prot_nbrs_hb.shape[0] > 0: hb_sw = self.calculate_hydrogen_bonds( trj, wat[1], prot_nbrs_hb, water_water=False) acc_sw = hb_sw[:, 0][np.where( hb_sw[:, 0] == wat[1])].shape[0] don_sw = hb_sw.shape[0] - acc_sw self.voxeldata[wat[0], 23] += hb_sw.shape[0] self.voxeldata[wat[0], 27] += don_sw self.voxeldata[wat[0], 29] += acc_sw if entropy: self.calculate_euler_angles(wat, coords[0, :, :])