def subsample_xmap(xmap=None, ligand_centroid_orth=np.array([0, 0, 0]), grid_size=10, grid_step=0.5): # Get grid dimensions grid_dimensions = [grid_size, grid_size, grid_size] # Generate a random grid rotation grid_rotation = np.random.rand(3) * 2 * np.pi # Convert grid rotation to euler angle, and that to a rotation matrix rot_mat_np = euler2mat(grid_rotation[0], grid_rotation[1], grid_rotation[2], "sxyz") # Cast to clipper matrix rot_mat = clipper.Mat33_double(rot_mat_np) # Generate a scale matrix to get the right grid size scale_mat = clipper.Mat33_double(grid_step, 0, 0, 0, grid_step, 0, 0, 0, grid_step) # Get grid origin from ligand centroid and rotation # algorithm: rotate grid_dimensions*grid_step vector with random rotation, step to ligand origin, # then step back halfway along it vec_orth = np.array(grid_dimensions) * grid_step grid_diagonal_reshaped_orth = vec_orth.reshape(3, 1) rotated_grid_diagonal_vector_orth = np.matmul( rot_mat_np, grid_diagonal_reshaped_orth).reshape(-1) ligand_centroid_orth_reshaped = ligand_centroid_orth.reshape(-1) origin_orth = ligand_centroid_orth_reshaped - ( rotated_grid_diagonal_vector_orth / 2) # Generate the Translation vector as a clipper vector trans = clipper.Vec3_double(origin_orth[0], origin_orth[1], origin_orth[2]) # Generate the clipper rotation-translation operator rtop = clipper.RTop_double(rot_mat * scale_mat, trans) # Generate the clipper grid grid = clipper.Grid(grid_dimensions[0], grid_dimensions[1], grid_dimensions[2]) # Define nxmap from the clipper grid and rotation-translation operator nxmap = clipper.NXmap_float(grid, rtop) # Interpolate the Xmap onto the clipper nxmap clipper.interpolate(nxmap, xmap) # Convert the nxmap to a numpy array nxmap_np = nxmap.export_numpy() return nxmap_np
def __call__(self): rot = clipper_python.Mat33_double(self.rotation_mobile_to_ref) trans = clipper_python.Vec3_double( self.translation_mobile_to_ref[0], self.translation_mobile_to_ref[1], self.translation_mobile_to_ref[2], ) rtop = clipper_python.RTop_orth( rot, trans, ) # Generate the clipper grid grid = clipper_python.Grid( self.grid_params[0], self.grid_params[1], self.grid_params[2], ) # Define nxmap from the clipper grid and rotation-translation operator nxmap = clipper_python.NXmap_float( grid, rtop, ) # Interpolate the Xmap onto the clipper nxmap clipper_python.interpolate(nxmap, self.xmap.xmap) return mdc3.types.real_space.MCDNXMap(nxmap)
def output_mean_nxmap(mean_map_np, cell, path, grid_params): print("Outputting mean map to: {}".format(path)) rot = clipper_python.Mat33_double(np.eye(3)) trans = clipper_python.Vec3_double(0.0, 0.0, 0.0) rtop = clipper_python.RTop_orth( rot, trans, ) # Generate the clipper grid grid = clipper_python.Grid( grid_params[0], grid_params[1], grid_params[2], ) # resolution = template_xmap.hkl_info.resolution mean_map_nxmap = clipper_python.NXmap_float( grid, rtop, ) mean_map_nxmap.import_numpy( clipper_python.Coord_grid(0, 0, 0), mean_map_np, ) mapout = clipper_python.CCP4MAPfile() # print(dir(mapout)) mapout.open_write(str(path)) mapout.set_cell(cell) mapout.export_nxmap_float(mean_map_nxmap) mapout.close_write()
def interpolate_uniform_grid( xmap, translation_mobile_to_ref=(0, 0, 0), rotation_mobile_to_ref=np.eye(3), ): rot = clipper_python.Mat33_double(rotation_mobile_to_ref) trans = clipper_python.Vec3_double( translation_mobile_to_ref[0], translation_mobile_to_ref[1], translation_mobile_to_ref[2], ) rtop = clipper_python.RTop_orth( rot, trans, ) # Generate the clipper grid grid = clipper_python.Grid( 100, 100, 100, ) # Define nxmap from the clipper grid and rotation-translation operator nxmap = clipper_python.NXmap_float( grid, rtop, ) # Interpolate the Xmap onto the clipper nxmap # print("interpolating!") clipper_python.interpolate(nxmap, xmap.xmap) xmap_np = nxmap.export_numpy() return xmap_np
def interpolate_uniform_grid( xmap, translation_mobile_to_ref=(0, 0, 0), rotation_mobile_to_ref=np.eye(3), grid_params=[50, 50, 50], ): # print("translation_mobile_to_ref: {}".format(translation_mobile_to_ref)) rot = clipper_python.Mat33_double(rotation_mobile_to_ref) trans = clipper_python.Vec3_double( translation_mobile_to_ref[0], translation_mobile_to_ref[1], translation_mobile_to_ref[2], ) rtop = clipper_python.RTop_orth( rot, trans, ) # Generate the clipper grid grid = clipper_python.Grid( grid_params[0], grid_params[1], grid_params[2], ) # Define nxmap from the clipper grid and rotation-translation operator nxmap = clipper_python.NXmap_float( grid, rtop, ) # Interpolate the Xmap onto the clipper nxmap clipper_python.interpolate(nxmap, xmap.xmap) return nxmap
def align_map_to_reference( dtag, reference_dtag, dataset_path, reference_pdb_path, output_path, min_res, structure_factors="FWT,PHWT", ): # Load structures f_ref = PDBFile(reference_pdb_path) reference_structure = structure_biopython_from_pdb(f_ref) f_moving = PDBFile(dataset_path["pdb_path"]) moving_structure = structure_biopython_from_pdb(f_moving) # Load xmap xmap = mdc3.types.real_space.xmap_from_path( dataset_path["mtz_path"], structure_factors, ) # Get box limits from reference structure box_limits = np.max( np.vstack([ atom.coord for atom in reference_structure.structure.get_atoms() ]), axis=0, ) print(box_limits) # Align and Get RTop to moving protein frame from alignment alignment_moving_to_ref = mdc3.functions.alignment.align( reference_structure.structure, moving_structure.structure, ) alignment_ref_to_moving = mdc3.functions.alignment.align( moving_structure.structure, reference_structure.structure, ) rotation = alignment_moving_to_ref.rotran[0] translation = alignment_moving_to_ref.rotran[1] alignment_moving_to_ref.apply(moving_structure.structure) print("translation: orthogonal to grid") print(translation) print("rotation") print(rotation) # Interpolate NX map in moving protein frame grid_params = [int(x) + 5 for x in box_limits] nxmap = mdc3.types.real_space.interpolate_uniform_grid( xmap, translation, np.transpose(rotation), grid_params=grid_params, ) nxmap_data = nxmap.export_numpy() origin_nxmap = clipper_python.NXmap_float( clipper_python.Grid( grid_params[0], grid_params[1], grid_params[2], ), clipper_python.RTop_orth( clipper_python.Mat33_double(np.eye(3)), clipper_python.Vec3_double(0, 0, 0), )) origin_nxmap.import_numpy( clipper_python.Coord_grid(0, 0, 0), nxmap_data, ) # Output to ccp4 # cell = xmap.xmap.cell cell = clipper_python.Cell( clipper_python.Cell_descr( grid_params[0], grid_params[1], grid_params[2], np.pi / 2, np.pi / 2, np.pi / 2, )) mdc3.types.real_space.output_nxmap( origin_nxmap, output_path / "{}_origin.ccp4".format(dtag), cell, ) mdc3.types.real_space.output_nxmap( nxmap, output_path / "{}.ccp4".format(dtag), cell, ) # Output aligned pdb moving_structure.output(output_path / "{}_aligned.pdb".format(dtag))
def align_structures(ref_structure, mobile_structure, mobile_xmap): # translation_mobile_to_ref = ref_structure.atoms.center_of_mass() - mobile_structure.atoms.center_of_mass() # # # mobile0 = mobile_structure.select_atoms('name CA').positions - mobile_structure.atoms.center_of_mass() # ref0 = ref_structure.select_atoms('name CA').positions - ref_structure.atoms.center_of_mass() # rotation_mobile_to_ref, rmsd = align.rotation_matrix(mobile0, ref0) ref_atoms = [] alt_atoms = [] for (ref_model, alt_model) in zip(ref_structure, mobile_structure): for (ref_chain, alt_chain) in zip(ref_model, alt_model): for ref_res, alt_res in zip(ref_chain, alt_chain): # CA = alpha carbon try: # print("\tModel: {}".format(ref_model)) # print("\tChain: {}".format(ref_chain)) # print("\tResidue: {}".format(ref_res)) # print(ref_res) # print(dir(ref_res)) # print([x for x in ref_res.get_atoms()]) ref_atoms.append(ref_res['CA']) alt_atoms.append(alt_res['CA']) except: pass super_imposer = PDB.Superimposer() super_imposer.set_atoms( ref_atoms, alt_atoms, ) translation_mobile_to_ref = super_imposer.rotran[1] rotation_mobile_to_ref = super_imposer.rotran[0] # print("\tTranslation is: {}".format(translation_mobile_to_ref)) # print("\tRotation is: {}".format(rotation_mobile_to_ref)) # xmap_np = interpolate_uniform_grid(mobile_xmap, # translation_mobile_to_ref, # np.transpose(rotation_mobile_to_ref), # ) rtop = clipper_python.RTop_orth( clipper_python.Mat33_double(np.transpose(rotation_mobile_to_ref)), clipper_python.Vec3_double(translation_mobile_to_ref), ) xmap_new = clipper_python.Xmap_float( mobile_xmap.xmap.spacegroup, mobile_xmap.xmap.cell, mobile_xmap.xmap.grid_sampling, ) clipper_python.rotate_translate( mobile_xmap.xmap, xmap_new, rtop, ) return xmap_new.export_numpy()