def _as_grid(self, feature_type=None, tolerance=2): """ returns _features as grid """ if feature_type == None: filtered_features = self._features else: filtered_features = [ feat for feat in self._features if feat.feature_type == feature_type ] x = [feat.feature_coordinates.x for feat in filtered_features] y = [feat.feature_coordinates.y for feat in filtered_features] z = [feat.feature_coordinates.z for feat in filtered_features] origin = [min(x) - tolerance, min(y) - tolerance, min(z) - tolerance] far_corner = [ max(x) + tolerance, max(y) + tolerance, max(z) + tolerance ] grd = Grid(origin=origin, far_corner=far_corner, spacing=0.5, default=0, _grid=None) for feat in filtered_features: grd.set_sphere(point=feat.feature_coordinates, radius=self.settings.radius, value=1, scaling='None') return grd
def get_residue_vdw(self, residue, res_num, padding=3.0): """ generates a mask for the residue where points within VdW distance of residue heavy atoms are :param residue: `ccdc.prottein.Residue` :param res_num: The value which to assign to voxels in the masked area :param padding: float, padding around minimal coordinates in Angstroms :return: `hotspots.grid_extension.Grid` """ coords = np.array([a.coordinates for a in residue.atoms]) min_coords = (np.min(coords[:, 0]), np.min(coords[:, 1]), np.min(coords[:, 2])) max_coords = (np.max(coords[:, 0]), np.max(coords[:, 1]), np.max(coords[:, 2])) # Put some padding around the minimal and maximum values: g_origin = tuple(x - padding for x in min_coords) g_far_corner = tuple(y + padding for y in max_coords) layer = Grid(origin=g_origin, far_corner=g_far_corner, spacing=self.g_spacing, default=0.0, _grid=None) for a in residue.atoms: layer.set_sphere(point=a.coordinates, radius=a.vdw_radius, value=1, scaling='None') layer = self.set_uniform_values(layer, res_num) print("Size of layer: {}".format(layer.count_grid())) return layer
def grid_from_protein(self): """ Puts the protein inside a CCDC Grid object :return: a :class: hotspots.grid_extension.Grid instance """ if not self.protein: self.get_protein() coords = np.array([ a.coordinates for res in self.protein.residues for a in res.atoms ]) min_coords = (np.min(coords[:, 0]), np.min(coords[:, 1]), np.min(coords[:, 2])) max_coords = (np.max(coords[:, 0]), np.max(coords[:, 1]), np.max(coords[:, 2])) # Put some padding around the minimal and maximum values: g_origin = tuple(x - 3.0 for x in min_coords) g_far_corner = tuple(y + 3.0 for y in max_coords) prot_grid = Grid(origin=g_origin, far_corner=g_far_corner, spacing=self.g_spacing, default=0.0, _grid=None) return prot_grid
def as_grid(origin_coords, far_corner_coords, array, spacing=0.5): """ Given an array, outputs a grid with the dimensions of the GridEnsemble :param array: 3D numpy array, usually containing processed ensemble data :return: a :class: 'ccdc.utilities.Grid' instance """ # Initialise the Grid grid = Grid(origin=origin_coords, far_corner=far_corner_coords, spacing=spacing, default=0.0, _grid=None) # Get the nonzero indices and values of the array nonz = array.nonzero() values = array[nonz] # Get indices per value as_triads = zip(*nonz) # Fill in the grid for (i, j, k), v in zip(as_triads, values): grid._grid.set_value(int(i), int(j), int(k), v) return grid
def make_grid(offset, vals, idxs, nsteps): grid_origin = offset grid_far_corner = ( offset[0] + (nsteps[0] - 1) * 0.5, offset[1] + (nsteps[1] - 1) * 0.5, offset[2] + (nsteps[2] - 1) * 0.5) out_grid = Grid(origin=grid_origin, far_corner=grid_far_corner, spacing=0.5, _grid=None, default=0) for (nx, ny, nz), v in zip(idxs, vals): # print(nx, ny, nz, v) # print(int(nx-offset[0]*2), int(ny-offset[1]*2), int(nz-offset[2]*2)) out_grid.set_value(int(nx - offset[0] * 2), int(ny - offset[1] * 2), int(nz - offset[2] * 2), v) return out_grid
def from_array(array, g_origin, g_far_corner): """ creates a grid from array :param fname: path to pickled numpy array :return: `hotspots.grid_extension.Grid` """ grid = Grid(origin=g_origin, far_corner=g_far_corner, spacing=0.5, default=0.0, _grid=None) indices = np.nonzero(array) values = array[indices] as_triads = zip(*indices) for (i, j, k), v in zip(as_triads, values): grid._grid.set_value(int(i), int(j), int(k), v) return grid
def get_grid_from_plip_coords(probe, coords, out_path, padding=4.0): """ :param probe: :param coords: :param padding: :return: """ Coordinates = collections.namedtuple('Coordinates', ['x', 'y', 'z']) dims = np.array(coords) min_coords = np.array( [np.min(dims[:, 0]), np.min(dims[:, 1]), np.min(dims[:, 2])]) max_coords = np.array( [np.max(dims[:, 0]), np.max(dims[:, 1]), np.max(dims[:, 2])]) origin = Coordinates(x=round(min_coords[0] - padding), y=round(min_coords[1] - padding), z=round(min_coords[2] - padding)) far_corner = Coordinates(x=round(max_coords[0] + padding), y=round(max_coords[1] + padding), z=round(max_coords[2] + padding)) plip_grid = Grid(origin=origin, far_corner=far_corner, spacing=0.5, default=0, _grid=None) for coo in coords: plip_grid.set_sphere(point=coo, radius=1, value=1, scaling='None') plip_grid.write(str(Path(out_path, f"plip_{probe}.ccp4").resolve())) return plip_grid
def _filter_map(self, g1, g2, tol): """ *Experimental feature* Takes 2 grids of the same size and coordinate frames. Points that are zero in one grid but sampled in the other are set to the mean of their nonzero neighbours. Grids are then subtracted and the result is returned. :param int tol: how many grid points away to consider scores from :param g1: a :class: "ccdc.utilities.Grid" instance :param g2: a :class: "ccdc.utilities.Grid" instance :return: a :class: "ccdc.utilities.Grid" instance """ def filter_point(x, y, z): loc_arr = np.array([ g[x + i][y + j][z + k] for i in range(-tol, tol + 1) for j in range(-tol, tol + 1) for k in range(-tol, tol + 1) ]) if loc_arr[loc_arr > 0].size != 0: # print(np.mean(loc_arr[loc_arr > 0])) new_grid[x][y][z] = np.mean(loc_arr[loc_arr > 0]) vfilter_point = np.vectorize(filter_point) com_bound_box = g1.bounding_box com_spacing = g1.spacing arr1 = g1.get_array() arr2 = g2.get_array() b_arr1 = np.copy(arr1) b_arr2 = np.copy(arr2) b_arr1[b_arr1 > 0] = 1.0 b_arr2[b_arr2 > 0] = -1.0 diff_arr = b_arr1 + b_arr2 unmatch1 = np.where(diff_arr == 1) unmatch2 = np.where(diff_arr == -1) g = arr1 new_grid = np.copy(arr1) vfilter_point(unmatch2[0], unmatch2[1], unmatch2[2]) f_arr1 = np.copy(new_grid) g = arr2 new_grid = np.copy(arr2) vfilter_point(unmatch1[0], unmatch1[1], unmatch1[2]) f_arr2 = np.copy(new_grid) sel_arr = f_arr1 - f_arr2 sel_arr[sel_arr < 0] = 0 sel_map = Grid(origin=com_bound_box[0], far_corner=com_bound_box[1], spacing=com_spacing, _grid=None) idxs = sel_arr.nonzero() vals = sel_arr[idxs] as_triads = zip(*idxs) for (i, j, k), v in zip(as_triads, vals): sel_map._grid.set_value(int(i), int(j), int(k), v) return sel_map