def make_box (self, selection, target_map) : from mmtbx import building box = building.box_build_refine_base( pdb_hierarchy=self.pdb_hierarchy, xray_structure=self.fmodel.xray_structure, processed_pdb_file=self.processed_pdb_file, target_map=target_map, selection=selection, d_min=self.fmodel.f_obs().d_min(), out=null_out()) return box
def exercise_box_rebuild(): # # UNSTABLE! 3x # pdb_in = get_1yjp_pdb() xrs = pdb_in.input.xray_structure_simple() fc = xrs.structure_factors(d_min=1.5).f_calc() fc_fft = fc.fft_map(resolution_factor=0.25) fc_map = fc_fft.apply_sigma_scaling().real_map_unpadded() sel_cache = pdb_in.hierarchy.atom_selection_cache() sel = sel_cache.selection("resseq 4:5") sites_orig = xrs.sites_cart().deep_copy() xrs.shake_sites_in_place(0.3, selection=sel) pdb_in.hierarchy.atoms().set_xyz(xrs.sites_cart()) sites_shaken = xrs.sites_cart().deep_copy() sites_new = building.run_real_space_annealing( xray_structure=xrs, pdb_hierarchy=pdb_in.hierarchy, selection=sel, target_map=fc_map, rsr_after_anneal=False, d_min=1.5, out=null_out()) pdb_in.hierarchy.atoms().set_xyz(sites_new) rmsd_start = sites_orig.rms_difference(sites_shaken) assert (rmsd_start > 0.1) #assert (sites_orig.rms_difference(sites_new) < rmsd_start), "%f < %f" % ( # sites_orig.rms_difference(sites_new), rmsd_start) assert (sites_orig.select(sel).rms_difference(sites_new.select(sel)) > 0) assert (sites_new.select(sel).rms_difference(sites_shaken.select(sel)) > 0) assert approx_equal( sites_orig.select(~sel).rms_difference(sites_new.select(~sel)), 0) box_builder = building.box_build_refine_base( pdb_hierarchy=pdb_in.hierarchy, xray_structure=xrs, processed_pdb_file=None, target_map=fc_map, selection=sel, d_min=1.5, out=null_out()) box_builder.restrain_atoms(box_builder.others_in_box, 0.02) box_builder.geometry_minimization() box_builder.real_space_refine() sites_new = box_builder.update_original_coordinates() assert (sites_orig.rms_difference(sites_new) < 0.3), sites_orig.rms_difference(sites_new) assert (box_builder.mean_density_at_sites() > 3), box_builder.mean_density_at_sites()
def exercise_box_rebuild () : # # UNSTABLE! 3x # pdb_in = get_1yjp_pdb() xrs = pdb_in.input.xray_structure_simple() fc = xrs.structure_factors(d_min=1.5).f_calc() fc_fft = fc.fft_map(resolution_factor=0.25) fc_map = fc_fft.apply_sigma_scaling().real_map_unpadded() sel_cache = pdb_in.hierarchy.atom_selection_cache() sel = sel_cache.selection("resseq 4:5") sites_orig = xrs.sites_cart().deep_copy() xrs.shake_sites_in_place(0.3, selection=sel) pdb_in.hierarchy.atoms().set_xyz(xrs.sites_cart()) sites_shaken = xrs.sites_cart().deep_copy() sites_new = building.run_real_space_annealing( xray_structure=xrs, pdb_hierarchy=pdb_in.hierarchy, selection=sel, target_map=fc_map, rsr_after_anneal=False, d_min=1.5, out=null_out()) pdb_in.hierarchy.atoms().set_xyz(sites_new) rmsd_start = sites_orig.rms_difference(sites_shaken) assert (rmsd_start > 0.1) #assert (sites_orig.rms_difference(sites_new) < rmsd_start), "%f < %f" % ( # sites_orig.rms_difference(sites_new), rmsd_start) assert (sites_orig.select(sel).rms_difference(sites_new.select(sel)) > 0) assert (sites_new.select(sel).rms_difference(sites_shaken.select(sel)) > 0) assert (sites_orig.select(~sel).rms_difference(sites_new.select(~sel)) == 0) box_builder = building.box_build_refine_base( pdb_hierarchy=pdb_in.hierarchy, xray_structure=xrs, processed_pdb_file=None, target_map=fc_map, selection=sel, d_min=1.5, out=null_out()) box_builder.restrain_atoms(box_builder.others_in_box, 0.02) box_builder.geometry_minimization() box_builder.real_space_refine() sites_new = box_builder.update_original_coordinates() assert (sites_orig.rms_difference(sites_new) < 0.3) assert (box_builder.mean_density_at_sites() > 3)
def __call__(self, atom_group, log, window_size=2, backbone_sample_angle=10, anneal=False, annealing_temperature=1000, use_chi1_sampling=False): import iotbx.pdb.hierarchy from scitbx.array_family import flex assert (atom_group is not None) pdb_hierarchy = self.pdb_hierarchy.deep_copy() xray_structure = self.xray_structure.deep_copy_scatterers() geometry_restraints_manager = self.geometry_restraints_manager # FIXME this doesn't work - can't recover the atom_group afterwards! #hd_sel = xray_structure.hd_selection() #n_hydrogen = hd_sel.count(True) #if (n_hydrogen > 0): # non_hd_sel = ~hd_sel # pdb_hierarchy = pdb_hierarchy.select(non_hd_sel) # xray_structure = xray_structure.select(non_hd_sel) # geometry_restraints_manager = geometry_restraints_manager.select( # non_hd_sel) pdb_atoms = pdb_hierarchy.atoms() pdb_atoms.reset_i_seq() isel = building.extract_iselection([atom_group]) atom_group = pdb_atoms[isel[0]].parent() atom_group_start = atom_group.detached_copy() needs_rebuild = not building.is_stub_residue(atom_group) residue_group = atom_group.parent() assert (len(residue_group.atom_groups()) == 1) sel_residues = building.get_window_around_residue( residue=atom_group, window_size=window_size) # get rid of sidechains for surrounding residues only adjacent_residues = [] for other_rg in sel_residues: if (other_rg != residue_group): adjacent_residues.append(other_rg) building.remove_sidechain_atoms(adjacent_residues) pdb_atoms = pdb_hierarchy.atoms() adjacent_trimmed_atom_names = pdb_atoms.extract_name() adjacent_trimmed_sel = pdb_atoms.extract_i_seq() xrs_adjacent_trimmed = xray_structure.select(adjacent_trimmed_sel) grm_adjacent_trimmed = geometry_restraints_manager.select( adjacent_trimmed_sel) pdb_atoms.reset_i_seq() # get rid of central sidechain and refine mainchain for entire window truncate = (not atom_group.resname in ["GLY", "ALA"]) # XXX PRO? if (truncate): building.remove_sidechain_atoms([atom_group]) pdb_atoms = pdb_hierarchy.atoms() all_mc_sel = pdb_atoms.extract_i_seq() xrs_mc = xrs_adjacent_trimmed.select(all_mc_sel) pdb_atoms.reset_i_seq() window_mc_sel = building.extract_iselection(sel_residues) selection = flex.bool(pdb_atoms.size(), False).set_selected(window_mc_sel, True) restraints_manager = grm_adjacent_trimmed.select(all_mc_sel) box = building.box_build_refine_base( xray_structure=xrs_mc, pdb_hierarchy=pdb_hierarchy, selection=selection, processed_pdb_file=None, target_map=self.target_map, geometry_restraints_manager=restraints_manager.geometry, d_min=self.d_min, out=null_out(), debug=True) box.restrain_atoms(selection=box.others_in_box, reference_sigma=0.1) box.real_space_refine(selection=box.selection_in_box) sites_new = box.update_original_coordinates() pdb_atoms.set_xyz(sites_new) # extend and replace existing residue. this is done in such a way that # the original atom ordering for the central residue is preserved, which # allows us to use the pre-existing geometry restraints instead of # re-calculating them every time this function is called. target_atom_group = self.ideal_dict[atom_group.resname.lower()].\ only_model().only_chain().only_residue_group().only_atom_group() new_atom_group_base = extend_sidechains.extend_residue( residue=atom_group, target_atom_group=target_atom_group, mon_lib_srv=self.mon_lib_srv) new_atom_group = iotbx.pdb.hierarchy.atom_group( resname=atom_group.resname) for atom in atom_group_start.atoms(): for new_atom in new_atom_group_base.atoms(): if (new_atom.name == atom.name): new_atom_group.append_atom(new_atom.detached_copy()) n_atoms_new = len(new_atom_group.atoms()) n_atoms_start = len(atom_group_start.atoms()) if (n_atoms_new != n_atoms_start): raise RuntimeError( ("Inconsistent atom counts for residue %s after " + "building (%d versus %d).") % (atom_group.id_str(), n_atoms_start, n_atoms_new)) rg = atom_group.parent() rg.remove_atom_group(atom_group) rg.append_atom_group(new_atom_group) pdb_atoms = pdb_hierarchy.atoms() pdb_atoms.reset_i_seq() new_names = pdb_atoms.extract_name() assert new_names.all_eq(adjacent_trimmed_atom_names) # get new box around this residue residue_sel = building.extract_iselection([new_atom_group]) selection = flex.bool(pdb_atoms.size(), False).set_selected(residue_sel, True) xrs_adjacent_trimmed.set_sites_cart(pdb_atoms.extract_xyz()) box = building.box_build_refine_base( xray_structure=xrs_adjacent_trimmed, pdb_hierarchy=pdb_hierarchy, selection=selection, processed_pdb_file=None, target_map=self.target_map, geometry_restraints_manager=grm_adjacent_trimmed.geometry, d_min=self.d_min, out=null_out(), debug=True) # place sidechain using mmtbx.refinement.real_space.fit_residue if ((atom_group.resname in rotatable_sidechain_atoms) and (use_chi1_sampling)): fit_chi1_simple(residue=box.only_residue(), unit_cell=box.unit_cell_box, target_map=box.target_map_box, rotamer_eval=self.rotamer_eval) box.update_sites_from_pdb_atoms() else: box.fit_residue_in_box(backbone_sample_angle=backbone_sample_angle) if (anneal): box.anneal(start_temperature=annealing_temperature) #box.real_space_refine() sites_new = box.update_original_coordinates() pdb_hierarchy.atoms().set_xyz(sites_new) return building.atom_group_as_hierarchy(new_atom_group)
def __call__ (self, atom_group, log, window_size=2, backbone_sample_angle=10, anneal=False, annealing_temperature=1000, use_chi1_sampling=False) : import iotbx.pdb.hierarchy from scitbx.array_family import flex assert (atom_group is not None) pdb_hierarchy = self.pdb_hierarchy.deep_copy() xray_structure = self.xray_structure.deep_copy_scatterers() geometry_restraints_manager = self.geometry_restraints_manager # FIXME this doesn't work - can't recover the atom_group afterwards! #hd_sel = xray_structure.hd_selection() #n_hydrogen = hd_sel.count(True) #if (n_hydrogen > 0) : # non_hd_sel = ~hd_sel # pdb_hierarchy = pdb_hierarchy.select(non_hd_sel) # xray_structure = xray_structure.select(non_hd_sel) # geometry_restraints_manager = geometry_restraints_manager.select( # non_hd_sel) pdb_atoms = pdb_hierarchy.atoms() pdb_atoms.reset_i_seq() isel = building.extract_iselection([atom_group]) atom_group = pdb_atoms[isel[0]].parent() atom_group_start = atom_group.detached_copy() needs_rebuild = not building.is_stub_residue(atom_group) residue_group = atom_group.parent() assert (len(residue_group.atom_groups()) == 1) sel_residues = building.get_window_around_residue( residue=atom_group, window_size=window_size) # get rid of sidechains for surrounding residues only adjacent_residues = [] for other_rg in sel_residues : if (other_rg != residue_group) : adjacent_residues.append(other_rg) building.remove_sidechain_atoms(adjacent_residues) pdb_atoms = pdb_hierarchy.atoms() adjacent_trimmed_atom_names = pdb_atoms.extract_name() adjacent_trimmed_sel = pdb_atoms.extract_i_seq() xrs_adjacent_trimmed = xray_structure.select(adjacent_trimmed_sel) grm_adjacent_trimmed = geometry_restraints_manager.select( adjacent_trimmed_sel) pdb_atoms.reset_i_seq() # get rid of central sidechain and refine mainchain for entire window truncate = (not atom_group.resname in ["GLY","ALA"]) # XXX PRO? if (truncate) : building.remove_sidechain_atoms([ atom_group ]) pdb_atoms = pdb_hierarchy.atoms() all_mc_sel = pdb_atoms.extract_i_seq() xrs_mc = xrs_adjacent_trimmed.select(all_mc_sel) pdb_atoms.reset_i_seq() window_mc_sel = building.extract_iselection(sel_residues) selection = flex.bool(pdb_atoms.size(), False).set_selected(window_mc_sel, True) restraints_manager = grm_adjacent_trimmed.select(all_mc_sel) box = building.box_build_refine_base( xray_structure=xrs_mc, pdb_hierarchy=pdb_hierarchy, selection=selection, processed_pdb_file=None, target_map=self.target_map, geometry_restraints_manager=restraints_manager.geometry, d_min=self.d_min, out=null_out(), debug=True) box.restrain_atoms( selection=box.others_in_box, reference_sigma=0.1) box.real_space_refine(selection=box.selection_in_box) sites_new = box.update_original_coordinates() pdb_atoms.set_xyz(sites_new) # extend and replace existing residue. this is done in such a way that # the original atom ordering for the central residue is preserved, which # allows us to use the pre-existing geometry restraints instead of # re-calculating them every time this function is called. new_atom_group_base = extend_sidechains.extend_residue( residue=atom_group, ideal_dict=self.ideal_dict, hydrogens=False, mon_lib_srv=self.mon_lib_srv, match_conformation=True) new_atom_group = iotbx.pdb.hierarchy.atom_group(resname=atom_group.resname) for atom in atom_group_start.atoms() : for new_atom in new_atom_group_base.atoms() : if (new_atom.name == atom.name) : new_atom_group.append_atom(new_atom.detached_copy()) n_atoms_new = len(new_atom_group.atoms()) n_atoms_start = len(atom_group_start.atoms()) if (n_atoms_new != n_atoms_start) : raise RuntimeError(("Inconsistent atom counts for residue %s after "+ "building (%d versus %d).") % (atom_group.id_str(), n_atoms_start, n_atoms_new)) rg = atom_group.parent() rg.remove_atom_group(atom_group) rg.append_atom_group(new_atom_group) pdb_atoms = pdb_hierarchy.atoms() pdb_atoms.reset_i_seq() new_names = pdb_atoms.extract_name() assert new_names.all_eq(adjacent_trimmed_atom_names) # get new box around this residue residue_sel = building.extract_iselection([ new_atom_group ]) selection = flex.bool(pdb_atoms.size(), False).set_selected(residue_sel, True) xrs_adjacent_trimmed.set_sites_cart(pdb_atoms.extract_xyz()) box = building.box_build_refine_base( xray_structure=xrs_adjacent_trimmed, pdb_hierarchy=pdb_hierarchy, selection=selection, processed_pdb_file=None, target_map=self.target_map, geometry_restraints_manager=grm_adjacent_trimmed.geometry, d_min=self.d_min, out=null_out(), debug=True) # place sidechain using mmtbx.refinement.real_space.fit_residue if ((atom_group.resname in rotatable_sidechain_atoms) and (use_chi1_sampling)) : fit_chi1_simple( residue=box.only_residue(), unit_cell=box.unit_cell_box, target_map=box.target_map_box, rotamer_eval=self.rotamer_eval) box.update_sites_from_pdb_atoms() else : box.fit_residue_in_box(backbone_sample_angle=backbone_sample_angle) if (anneal) : box.anneal(start_temperature=annealing_temperature) #box.real_space_refine() sites_new = box.update_original_coordinates() pdb_hierarchy.atoms().set_xyz(sites_new) return building.atom_group_as_hierarchy(new_atom_group)