def create_helix_hydrogen_bond_proxies(params, pdb_hierarchy, selection_cache, weight, hbond_counts, distance_ideal, distance_cut, remove_outliers, restrain_hbond_angles, log=sys.stdout): assert (not None in [distance_ideal, distance_cut]) generated_proxies = geometry_restraints.shared_bond_simple_proxy() hb_angle_proxies = [] helix_class = params.helix_type if helix_class == "alpha": helix_step = 4 elif helix_class == "pi": helix_step = 5 elif helix_class == "3_10": helix_step = 3 else: print >> log, " Don't know bonding for helix class %s." % helix_class return generated_proxies, hb_angle_proxies try: helix_selection = selection_cache.selection(params.selection) except Exception, e: print >> log, str(e) return generated_proxies, hb_angle_proxies
def create_helix_hydrogen_bond_proxies ( params, pdb_hierarchy, selection_cache, weight, hbond_counts, distance_ideal, distance_cut, remove_outliers, log=sys.stdout) : assert (not None in [distance_ideal, distance_cut]) generated_proxies = geometry_restraints.shared_bond_simple_proxy() helix_class = params.helix_type if helix_class == "alpha" : helix_step = 4 elif helix_class == "pi" : helix_step = 5 elif helix_class == "3_10" : helix_step = 3 else : print >> log, " Don't know bonding for helix class %s." % helix_class return generated_proxies try : helix_selection = selection_cache.selection(params.selection) except Exception, e : print >> log, str(e) return generated_proxies
def __init__(self, *args, **kwds): super(restrained_crystal_structure_builder, self).__init__(*args, **kwds) geom = geometry_restraints adp = adp_restraints self._proxies = {} self._proxies = { 'bond': geometry_restraints.shared_bond_simple_proxy(), 'angle': geometry_restraints.shared_angle_proxy(), 'dihedral': geometry_restraints.shared_dihedral_proxy(), 'planarity': geometry_restraints.shared_planarity_proxy(), 'chirality': geometry_restraints.shared_chirality_proxy(), 'bond_similarity': geometry_restraints.shared_bond_similarity_proxy(), 'adp_similarity': adp_restraints.shared_adp_similarity_proxy(), 'rigid_bond': adp_restraints.shared_rigid_bond_proxy(), 'isotropic_adp': adp_restraints.shared_isotropic_adp_proxy(), 'fixed_u_eq_adp': adp_restraints.shared_fixed_u_eq_adp_proxy(), 'adp_u_eq_similarity': adp_restraints.shared_adp_u_eq_similarity_proxy(), 'adp_volume_similarity': adp_restraints.shared_adp_volume_similarity_proxy(), }
def __init__(O, sites_cart, geometry_restraints_manager, geometry_restraints_flags, lbfgs_termination_params, lbfgs_exception_handling_params): O.grm = geometry_restraints_manager cs = O.grm.crystal_symmetry sst = O.grm.site_symmetry_table sites_frac = cs.unit_cell().fractionalize(sites_cart) O.x = sst.pack_coordinates(sites_frac=sites_frac) O.proxies = geometry_restraints.shared_bond_simple_proxy() for i_seq,pair_sym_dict in enumerate(O.grm.shell_sym_tables[0]): for j_seq,sym_ops in pair_sym_dict.items(): assert i_seq <= j_seq bond_params = O.grm.bond_params_table[i_seq][j_seq] for rt_mx_ji in sym_ops: O.proxies.append(geometry_restraints.bond_simple_proxy( i_seqs=[i_seq, j_seq], rt_mx_ji=rt_mx_ji, params=bond_params)) def get_target_result(): f, _ = O.compute_functional_and_gradients() return dev_target_result( target=f, n_bond_proxies=O.proxies.size(), bond_residual_sum=f) O.first_target_result = get_target_result() import scitbx.lbfgs O.minimizer = scitbx.lbfgs.run( target_evaluator=O, termination_params=lbfgs_termination_params, exception_handling_params=lbfgs_exception_handling_params) f, _ = O.compute_functional_and_gradients() O.final_target_result = get_target_result()
def get_basepair_hbond_proxies( pdb_hierarchy, bp_phil_params, hbond_distance_cutoff=3.4): assert pdb_hierarchy is not None bond_proxies_result = [] angle_proxies_result = [] if len(bp_phil_params) > 0: # return bond_proxies_result, angle_proxies_result selection_cache = pdb_hierarchy.atom_selection_cache() pdb_atoms = pdb_hierarchy.atoms() # dashes = open('dashes.pml', 'w') pdb_atoms = pdb_hierarchy.atoms() for base_pair in bp_phil_params: if (base_pair.base1 is not None and base_pair.base2 is not None and base_pair.enabled): selected_atoms_1 = selection_cache.iselection(base_pair.base1) selected_atoms_2 = selection_cache.iselection(base_pair.base2) if len(selected_atoms_1) == 0: raise Sorry("Selection %s in base_pair retusulted in 0 atoms." % ( base_pair.base1)) if len(selected_atoms_2) == 0: raise Sorry("Selection %s in base_pair retusulted in 0 atoms." % ( base_pair.base2)) a1 = pdb_atoms[selected_atoms_1[0]] a2 = pdb_atoms[selected_atoms_2[0]] if base_pair.saenger_class == 0: hbonds, saenger_class = get_h_bonds_for_basepair( a1, a2, distance_cutoff=hbond_distance_cutoff, log=sys.stdout, verbose=-1) base_pair.saenger_class = saenger_class hbonds = get_h_bonds_for_particular_basepair((a1, a2), base_pair.saenger_class) for hb in hbonds: dist = hb[0].distance(hb[1]) if dist < hbond_distance_cutoff: if base_pair.restrain_hbonds: hb_target, hb_sigma = get_hb_lenght_targets(hb) p = geometry_restraints.bond_simple_proxy( i_seqs=[hb[0].i_seq, hb[1].i_seq], distance_ideal=hb_target, weight=1.0/hb_sigma**2, slack=0, top_out=False, limit=1, origin_id=1) bond_proxies_result.append(p) # print "bond:", hb[0].id_str(), hb[1].id_str(), "(%4.2f, %4.2f)" % (hb_target, hb_sigma) # s1 = pdb_atoms[hb[0].i_seq].id_str() # s2 = pdb_atoms[hb[1].i_seq].id_str() # ps = "dist chain \"%s\" and resi %s and name %s, chain \"%s\" and resi %s and name %s\n" % ( # s1[14:15], s1[15:19], s1[5:8], s2[14:15], s2[15:19], s2[5:8]) # dashes.write(ps) if base_pair.restrain_hb_angles: angle_proxies_result += get_angle_proxies_for_bond(hb) else: print "NA hbond rejected:",hb[0].id_str(), hb[1].id_str(), "distance=%.2f" % dist # dashes.close() return geometry_restraints.shared_bond_simple_proxy(bond_proxies_result), \ angle_proxies_result
class bond_restraint_test_case(geometry_restraints_test_case): manager = restraints.manager( bond_proxies=geometry_restraints.shared_bond_simple_proxy([ geom.bond_simple_proxy((0, 30), 1.42, 1), geom.bond_simple_proxy((1, 21), 1.42, 1) ])) proxies = manager.bond_proxies restraint_t = geom.bond
def create_protein_hbond_proxies (self, annotation=None, log=sys.stdout): # assert as_regular_bond_proxies=True if annotation is None: annotation = self.actual_sec_str remove_outliers = self.params.secondary_structure.protein.remove_outliers from scitbx.array_family import flex atoms = self.pdb_hierarchy.atoms() hbond_counts = flex.int(atoms.size(), 0) selection_cache = self.pdb_hierarchy.atom_selection_cache() distance_ideal = self.params.secondary_structure.protein.distance_ideal_n_o distance_cut = self.params.secondary_structure.protein.distance_cut_n_o if (distance_cut is None) : distance_cut = -1 generated_proxies = geometry_restraints.shared_bond_simple_proxy() if self.params.secondary_structure.protein.enabled: for helix in self.params.secondary_structure.protein.helix : if helix.selection is not None: print >> log, " Processing helix ", helix.selection proxies = proteins.create_helix_hydrogen_bond_proxies( params=helix, pdb_hierarchy=self.pdb_hierarchy, selection_cache=selection_cache, weight=1.0, hbond_counts=hbond_counts, distance_ideal=distance_ideal, distance_cut=distance_cut, remove_outliers=remove_outliers, log=log) if (proxies.size() == 0) : print >> log, " No H-bonds generated for '%s'" % helix.selection continue else: generated_proxies.extend(proxies) for k, sheet in enumerate(self.params.secondary_structure.protein.sheet) : print >> log, " Processing sheet with id=%s, first strand: %s" % ( sheet.sheet_id, sheet.first_strand) if sheet.first_strand is not None: proxies = proteins.create_sheet_hydrogen_bond_proxies( sheet_params=sheet, pdb_hierarchy=self.pdb_hierarchy, weight=1.0, hbond_counts=hbond_counts, distance_ideal=distance_ideal, distance_cut=distance_cut, remove_outliers=remove_outliers, log=log) if (proxies.size() == 0) : print >> log, \ " No H-bonds generated for sheet with id=%s" % sheet.sheet_id continue else: generated_proxies.extend(proxies) n_proxies = generated_proxies.size() print >> log, "" if (n_proxies == 0) : print >> log, " No hydrogen bonds defined for protein." else : print >> log, " %d hydrogen bonds defined for protein." % n_proxies # reg_proxies = [] # for hb_p in build_proxies.proxies: # reg_proxy = geometry_restraints.bond_simple_proxy( # i_seqs=hb_p.i_seqs, # distance_ideal=hb_p.distance_ideal, # weight=hb_p.weight, # slack=hb_p.slack, # top_out=hb_p.top_out, # limit=hb_p.limit, # origin_id=1) # reg_proxies.append(reg_proxy) # return geometry_restraints.shared_bond_simple_proxy(reg_proxies) return generated_proxies
def create_sheet_hydrogen_bond_proxies(sheet_params, pdb_hierarchy, selection_cache, weight, hbond_counts, distance_ideal, distance_cut, remove_outliers, restrain_hbond_angles, log=sys.stdout): assert (not None in [distance_ideal, distance_cut]) angle_restraint_type = 0 if restrain_hbond_angles: angle_restraint_type = 3 prev_strand = sheet_params.first_strand prev_selection = selection_cache.selection(prev_strand) hb_angle_proxies = [] prev_rgs = _get_residue_groups_from_selection( pdb_hierarchy=pdb_hierarchy, bool_selection=prev_selection) n_proxies = 0 k = 0 generated_proxies = geometry_restraints.shared_bond_simple_proxy() while k < len(sheet_params.strand): curr_strand = sheet_params.strand[k] curr_selection = selection_cache.selection(curr_strand.selection) curr_start = None prev_start = None if curr_strand.bond_start_current is not None: curr_start = selection_cache.selection( curr_strand.bond_start_current) if curr_strand.bond_start_previous is not None: prev_start = selection_cache.selection( curr_strand.bond_start_previous) curr_rgs = _get_residue_groups_from_selection( pdb_hierarchy=pdb_hierarchy, bool_selection=curr_selection) i = j = 0 len_prev_residues = len(prev_rgs) len_curr_residues = len(curr_rgs) if curr_start is not None and prev_start is not None: if curr_start.count(True) < 1 or prev_start.count(True) < 1: error_msg = """\ Wrong registration in SHEET record. One of these selections "%s" or "%s" yielded zero or several atoms. Possible reason for it is the presence of insertion codes or alternative conformations for one of these residues or the .pdb file was edited without updating SHEET records.""" \ % (curr_strand.bond_start_current, curr_strand.bond_start_previous) raise Sorry(error_msg) current_start_res_is_donor = pdb_hierarchy.atoms().select( curr_start)[0].name.strip() == 'N' if (len_curr_residues > 0) and (len_prev_residues > 0): i = _find_start_residue(residues=prev_rgs, start_selection=prev_start) j = _find_start_residue(residues=curr_rgs, start_selection=curr_start) if (i >= 0) and (j >= 0): # move i,j pointers from registration residues to the beginning of # beta-strands while (1 < i and ((1 < j and curr_strand.sense == "parallel") or (j < len_curr_residues - 2 and curr_strand.sense == "antiparallel"))): if curr_strand.sense == "parallel": i -= 2 j -= 2 elif curr_strand.sense == "antiparallel": i -= 2 j += 2 if (curr_strand.sense == "parallel"): # some tweaking for ensure correct donor assignment if i >= 2 and not current_start_res_is_donor: i -= 2 current_start_res_is_donor = not current_start_res_is_donor if j >= 2 and current_start_res_is_donor: j -= 2 current_start_res_is_donor = not current_start_res_is_donor while (i < len_prev_residues) and (j < len_curr_residues): prev_atoms = None if current_start_res_is_donor: donor_residue = curr_rgs[j] if j > 0: prev_atoms = curr_rgs[j - 1].atoms() acceptor_residue = prev_rgs[i] i += 2 else: donor_residue = prev_rgs[i] if i > 0: prev_atoms = prev_rgs[i - 1].atoms() acceptor_residue = curr_rgs[j] j += 2 current_start_res_is_donor = not current_start_res_is_donor if donor_residue.atom_groups()[0].resname.strip( ) != "PRO": proxies, angle_proxies = _create_hbond_proxy( acceptor_atoms=acceptor_residue.atoms(), donor_atoms=donor_residue.atoms(), hbond_counts=hbond_counts, distance_ideal=distance_ideal, distance_cut=distance_cut, remove_outliers=remove_outliers, prev_atoms=prev_atoms, angle_restraint_type=angle_restraint_type, weight=weight, sigma=sheet_params.sigma, slack=sheet_params.slack, top_out=sheet_params.top_out, angle_sigma_scale=sheet_params. angle_sigma_scale, angle_sigma_set=sheet_params. angle_sigma_set, log=log) for proxy in proxies: generated_proxies.append(proxy) hb_angle_proxies += angle_proxies elif (curr_strand.sense == "antiparallel"): while (i < len_prev_residues and j >= 0): prev_atoms = None if (prev_rgs[i].atom_groups()[0].resname.strip() != "PRO"): if i > 0: prev_atoms = prev_rgs[i - 1].atoms() proxies, angle_proxies = _create_hbond_proxy( acceptor_atoms=curr_rgs[j].atoms(), donor_atoms=prev_rgs[i].atoms(), hbond_counts=hbond_counts, distance_ideal=distance_ideal, distance_cut=distance_cut, remove_outliers=remove_outliers, prev_atoms=prev_atoms, angle_restraint_type=angle_restraint_type, weight=weight, sigma=sheet_params.sigma, slack=sheet_params.slack, top_out=sheet_params.top_out, angle_sigma_scale=sheet_params. angle_sigma_scale, angle_sigma_set=sheet_params. angle_sigma_set, log=log) for proxy in proxies: generated_proxies.append(proxy) hb_angle_proxies += angle_proxies prev_atoms = None if (curr_rgs[j].atom_groups()[0].resname.strip() != "PRO"): if j > 0: prev_atoms = curr_rgs[j - 1].atoms() proxies, angle_proxies = _create_hbond_proxy( acceptor_atoms=prev_rgs[i].atoms(), donor_atoms=curr_rgs[j].atoms(), hbond_counts=hbond_counts, distance_ideal=distance_ideal, distance_cut=distance_cut, remove_outliers=remove_outliers, prev_atoms=prev_atoms, angle_restraint_type=angle_restraint_type, weight=weight, sigma=sheet_params.sigma, slack=sheet_params.slack, top_out=sheet_params.top_out, angle_sigma_scale=sheet_params. angle_sigma_scale, angle_sigma_set=sheet_params. angle_sigma_set, log=log) for proxy in proxies: generated_proxies.append(proxy) hb_angle_proxies += angle_proxies i += 2 j -= 2 else: print(" WARNING: strand direction not defined!", file=log) print(" previous: %s" % prev_strand, file=log) print(" current: %s" % curr_strand.selection, file=log) else: print( " WARNING: can't find start of bonding for strands!", file=log) print(" previous: %s" % prev_strand, file=log) print(" current: %s" % curr_strand.selection, file=log) else: print(" WARNING: can't find one or more strands!", file=log) print(" previous: %s" % prev_strand, file=log) print(" current: %s" % curr_strand.selection, file=log) k += 1 prev_strand = curr_strand.selection prev_selection = curr_selection prev_rgs = curr_rgs return generated_proxies, hb_angle_proxies
def create_helix_hydrogen_bond_proxies(params, pdb_hierarchy, selection_cache, weight, hbond_counts, distance_ideal, distance_cut, remove_outliers, restrain_hbond_angles, log=sys.stdout): assert (not None in [distance_ideal, distance_cut]) generated_proxies = geometry_restraints.shared_bond_simple_proxy() hb_angle_proxies = [] helix_class = params.helix_type if helix_class == "alpha": helix_step = 4 elif helix_class == "pi": helix_step = 5 elif helix_class == "3_10": helix_step = 3 else: print(" Don't know bonding for helix class %s." % helix_class, file=log) return generated_proxies, hb_angle_proxies try: helix_selection = selection_cache.selection(params.selection) except Exception as e: print(str(e), file=log) return generated_proxies, hb_angle_proxies assert (helix_step in [3, 4, 5]) helix_rgs = _get_residue_groups_from_selection(pdb_hierarchy, helix_selection) i = 0 just_after_pro = False while i < len(helix_rgs) - helix_step: if helix_rgs[i + helix_step].atom_groups()[0].resname.strip() == "PRO": print(" Proline residue: %s - end of helix" % \ (helix_rgs[i+helix_step].id_str()), file=log) i += 3 just_after_pro = True continue # XXX is this safe? angle_restraint_type = 0 if restrain_hbond_angles and helix_class == "alpha": if i == 0 or i == len( helix_rgs) - helix_step - 1 or just_after_pro: angle_restraint_type = 1 just_after_pro = False else: angle_restraint_type = 2 proxies, angle_proxies = _create_hbond_proxy( acceptor_atoms=helix_rgs[i].atoms(), donor_atoms=helix_rgs[i + helix_step].atoms(), hbond_counts=hbond_counts, distance_ideal=distance_ideal, distance_cut=distance_cut, remove_outliers=remove_outliers, prev_atoms=helix_rgs[i + helix_step - 1].atoms(), angle_restraint_type=angle_restraint_type, weight=weight, sigma=params.sigma, slack=params.slack, angle_sigma_scale=params.angle_sigma_scale, angle_sigma_set=params.angle_sigma_set, log=log) for proxy in proxies: generated_proxies.append(proxy) hb_angle_proxies += angle_proxies i += 1 return generated_proxies, hb_angle_proxies
def exercise_restrained_refinement(options): import random random.seed(1) flex.set_random_seed(1) xs0 = smtbx.development.random_xray_structure( sgtbx.space_group_info('P1'), n_scatterers=options.n_scatterers, elements="random") for sc in xs0.scatterers(): sc.flags.set_grad_site(True) sc0 = xs0.scatterers() uc = xs0.unit_cell() mi = xs0.build_miller_set(anomalous_flag=False, d_min=options.resolution) fo_sq = mi.structure_factors_from_scatterers( xs0, algorithm="direct").f_calc().norm() fo_sq = fo_sq.customized_copy(sigmas=flex.double(fo_sq.size(), 1)) i, j, k, l = random.sample(xrange(options.n_scatterers), 4) bond_proxies = geometry_restraints.shared_bond_simple_proxy() w = 1e9 d_ij = uc.distance(sc0[i].site, sc0[j].site) * 0.8 bond_proxies.append( geom.bond_simple_proxy(i_seqs=(i, j), distance_ideal=d_ij, weight=w)) d_jk = uc.distance(sc0[j].site, sc0[k].site) * 0.85 bond_proxies.append( geom.bond_simple_proxy(i_seqs=(j, k), distance_ideal=d_jk, weight=w)) d_ki = min( uc.distance(sc0[k].site, sc0[i].site) * 0.9, (d_ij + d_jk) * 0.8) bond_proxies.append( geom.bond_simple_proxy(i_seqs=(k, i), distance_ideal=d_ki, weight=w)) d_jl = uc.distance(sc0[j].site, sc0[l].site) * 0.9 bond_proxies.append( geom.bond_simple_proxy(i_seqs=(j, l), distance_ideal=d_jl, weight=w)) d_lk = min( uc.distance(sc0[l].site, sc0[k].site) * 0.8, 0.75 * (d_jk + d_jl)) bond_proxies.append( geom.bond_simple_proxy(i_seqs=(l, k), distance_ideal=d_jl, weight=w)) restraints_manager = restraints.manager(bond_proxies=bond_proxies) xs1 = xs0.deep_copy_scatterers() xs1.shake_sites_in_place(rms_difference=0.1) def ls_problem(): xs = xs1.deep_copy_scatterers() reparametrisation = constraints.reparametrisation( structure=xs, constraints=[], connectivity_table=smtbx.utils.connectivity_table(xs), temperature=20) return least_squares.crystallographic_ls( fo_sq.as_xray_observations(), reparametrisation=reparametrisation, restraints_manager=restraints_manager) gradient_threshold, step_threshold = 1e-6, 1e-6 eps = 5e-3 ls = ls_problem() t = wall_clock_time() cycles = normal_eqns_solving.naive_iterations( ls, gradient_threshold=gradient_threshold, step_threshold=step_threshold, track_all=True) if options.verbose: print "%i %s steps in %.6f s" % (cycles.n_iterations, cycles, t.elapsed()) sc = ls.xray_structure.scatterers() for p in bond_proxies: d = uc.distance(*[sc[i_pair].site for i_pair in p.i_seqs]) assert approx_equal(d, p.distance_ideal, eps) ls = ls_problem() t = wall_clock_time() cycles = normal_eqns_solving.levenberg_marquardt_iterations( ls, gradient_threshold=gradient_threshold, step_threshold=step_threshold, tau=1e-3, track_all=True) if options.verbose: print "%i %s steps in %.6f s" % (cycles.n_iterations, cycles, t.elapsed()) sc = ls.xray_structure.scatterers() sc = ls.xray_structure.scatterers() for p in bond_proxies: d = uc.distance(*[sc[i].site for i in p.i_seqs]) assert approx_equal(d, p.distance_ideal, eps)
def exercise_geometry_restraints_as_cif(): quartz = xray.structure(crystal_symmetry=crystal.symmetry( (5.01, 5.01, 5.47, 90, 90, 120), "P6222"), scatterers=flex.xray_scatterer([ xray.scatterer("Si", (1 / 2., 1 / 2., 1 / 3.)), xray.scatterer("O", (0.197, -0.197, 0.83333)) ])) bond_proxies = geometry_restraints.shared_bond_simple_proxy(( geometry_restraints.bond_simple_proxy( i_seqs=[0, 1], rt_mx_ji=sgtbx.rt_mx("x-y,x,z-2/3"), distance_ideal=1.6, weight=3.2), geometry_restraints.bond_simple_proxy(i_seqs=[0, 1], distance_ideal=1.7, weight=1.8), )) dihedral_proxies = geometry_restraints.shared_dihedral_proxy(( geometry_restraints.dihedral_proxy( i_seqs=[1, 0, 1, 0], sym_ops=(sgtbx.rt_mx("1+y,1-x+y, z-1/3"), sgtbx.rt_mx(), sgtbx.rt_mx("x-y,x,z-2/3"), sgtbx.rt_mx("1-x,y-x,1/3-z")), angle_ideal=-30, weight=2), geometry_restraints.dihedral_proxy( i_seqs=[1, 0, 1, 0], sym_ops=(sgtbx.rt_mx("1+y,1-x+y, z-1/3"), sgtbx.rt_mx(), sgtbx.rt_mx("-y,x-y,z-1/3"), sgtbx.rt_mx("x-y,x,1/3+z")), angle_ideal=90, weight=3), )) angle_proxies = geometry_restraints.shared_angle_proxy(( geometry_restraints.angle_proxy(i_seqs=[1, 0, 1], sym_ops=(sgtbx.rt_mx("x-y,x,z-2/3"), sgtbx.rt_mx(), sgtbx.rt_mx("-y,x-y,z-1/3")), angle_ideal=103, weight=2), geometry_restraints.angle_proxy( i_seqs=[1, 0, 1], sym_ops=(sgtbx.rt_mx("y+1,-x+y+1,z-1/3"), sgtbx.rt_mx(), sgtbx.rt_mx("-y,x-y,z-1/3")), angle_ideal=110, weight=5), geometry_restraints.angle_proxy(i_seqs=[0, 1, 0], sym_ops=(sgtbx.rt_mx("y,-x+y,z+2/3"), sgtbx.rt_mx(), sgtbx.rt_mx("-x+y,-x,z+1/3")), angle_ideal=150, weight=5), )) bond_similarity_proxies = geometry_restraints.shared_bond_similarity_proxy( (geometry_restraints.bond_similarity_proxy( i_seqs=[(0, 1), (0, 1), (0, 1)], sym_ops=(sgtbx.rt_mx("x-y,x,z-2/3"), sgtbx.rt_mx("-y,x-y,z-1/3"), sgtbx.rt_mx("y+1,-x+y+1,z-1/3")), weights=(1, 1, 1)), )) cif_block = iotbx.cif.model.block() iotbx.cif.restraints.add_to_cif_block( cif_block, quartz, bond_proxies=bond_proxies, angle_proxies=angle_proxies, dihedral_proxies=dihedral_proxies, bond_similarity_proxies=bond_similarity_proxies) s = StringIO() cif_block.show(out=s) assert not show_diff( s.getvalue(), """\ loop_ _restr_distance_atom_site_label_1 _restr_distance_atom_site_label_2 _restr_distance_site_symmetry_2 _restr_distance_target _restr_distance_target_weight_param _restr_distance_diff Si O 2_554 1.6000 0.5590 -0.0160 Si O 1 1.7000 0.7454 -2.3838 loop_ _restr_angle_atom_site_label_1 _restr_angle_atom_site_label_2 _restr_angle_atom_site_label_3 _restr_angle_site_symmetry_1 _restr_angle_site_symmetry_2 _restr_angle_site_symmetry_3 _restr_angle_target _restr_angle_target_weight_param _restr_angle_diff O Si O 2_554 1 4_554 103.0000 0.7071 1.6926 O Si O 3_664 1 4_554 110.0000 0.4472 -1.3127 Si O Si 3 1 5 150.0000 0.4472 3.0700 loop_ _restr_torsion_atom_site_label_1 _restr_torsion_atom_site_label_2 _restr_torsion_atom_site_label_3 _restr_torsion_atom_site_label_4 _restr_torsion_site_symmetry_1 _restr_torsion_site_symmetry_2 _restr_torsion_site_symmetry_3 _restr_torsion_site_symmetry_4 _restr_torsion_angle_target _restr_torsion_weight_param _restr_torsion_diff O Si O Si 3_664 1 2_554 7_655 -30.0000 0.7071 6.9078 O Si O Si 3_664 1 4_554 2 90.0000 0.5774 11.7036 loop_ _restr_equal_distance_class_class_id _restr_equal_distance_class_target_weight_param _restr_equal_distance_class_average _restr_equal_distance_class_esd _restr_equal_distance_class_diff_max 1 1.0000 1.6160 0.0000 0.0000 loop_ _restr_equal_distance_atom_site_label_1 _restr_equal_distance_atom_site_label_2 _restr_equal_distance_site_symmetry_2 _restr_equal_distance_class_id Si O 2_554 1 Si O 4_554 1 Si O 3_664 1 """)
def exercise_geometry_restraints_as_cif(): quartz = xray.structure( crystal_symmetry=crystal.symmetry( (5.01,5.01,5.47,90,90,120), "P6222"), scatterers=flex.xray_scatterer([ xray.scatterer("Si", (1/2.,1/2.,1/3.)), xray.scatterer("O", (0.197,-0.197,0.83333))])) bond_proxies = geometry_restraints.shared_bond_simple_proxy(( geometry_restraints.bond_simple_proxy( i_seqs=[0,1], rt_mx_ji=sgtbx.rt_mx("x-y,x,z-2/3"), distance_ideal=1.6, weight=3.2), geometry_restraints.bond_simple_proxy( i_seqs=[0,1], distance_ideal=1.7, weight=1.8), )) dihedral_proxies = geometry_restraints.shared_dihedral_proxy(( geometry_restraints.dihedral_proxy( i_seqs = [1,0,1,0], sym_ops = (sgtbx.rt_mx("1+y,1-x+y, z-1/3"), sgtbx.rt_mx(), sgtbx.rt_mx("x-y,x,z-2/3"), sgtbx.rt_mx("1-x,y-x,1/3-z")), angle_ideal=-30, weight=2), geometry_restraints.dihedral_proxy( i_seqs = [1,0,1,0], sym_ops = (sgtbx.rt_mx("1+y,1-x+y, z-1/3"), sgtbx.rt_mx(), sgtbx.rt_mx("-y,x-y,z-1/3"), sgtbx.rt_mx("x-y,x,1/3+z")), angle_ideal=90, weight=3), )) angle_proxies = geometry_restraints.shared_angle_proxy(( geometry_restraints.angle_proxy( i_seqs = [1,0,1], sym_ops = (sgtbx.rt_mx("x-y,x,z-2/3"), sgtbx.rt_mx(), sgtbx.rt_mx("-y,x-y,z-1/3")), angle_ideal=103, weight=2), geometry_restraints.angle_proxy( i_seqs = [1,0,1], sym_ops = (sgtbx.rt_mx("y+1,-x+y+1,z-1/3"), sgtbx.rt_mx(), sgtbx.rt_mx("-y,x-y,z-1/3")), angle_ideal=110, weight=5), geometry_restraints.angle_proxy( i_seqs = [0,1,0], sym_ops = (sgtbx.rt_mx("y,-x+y,z+2/3"), sgtbx.rt_mx(), sgtbx.rt_mx("-x+y,-x,z+1/3")), angle_ideal=150, weight=5), )) bond_similarity_proxies = geometry_restraints.shared_bond_similarity_proxy(( geometry_restraints.bond_similarity_proxy( i_seqs=[(0,1),(0,1),(0,1)], sym_ops=(sgtbx.rt_mx("x-y,x,z-2/3"), sgtbx.rt_mx("-y,x-y,z-1/3"), sgtbx.rt_mx("y+1,-x+y+1,z-1/3")), weights=(1,1,1)), )) cif_block = iotbx.cif.model.block() iotbx.cif.restraints.add_to_cif_block( cif_block, quartz, bond_proxies=bond_proxies, angle_proxies=angle_proxies, dihedral_proxies=dihedral_proxies, bond_similarity_proxies=bond_similarity_proxies) s = StringIO() cif_block.show(out=s) assert not show_diff(s.getvalue(), """\ loop_ _restr_distance_atom_site_label_1 _restr_distance_atom_site_label_2 _restr_distance_site_symmetry_2 _restr_distance_target _restr_distance_target_weight_param _restr_distance_diff Si O 2_554 1.6000 0.5590 -0.0160 Si O 1 1.7000 0.7454 -2.3838 loop_ _restr_angle_atom_site_label_1 _restr_angle_atom_site_label_2 _restr_angle_atom_site_label_3 _restr_angle_site_symmetry_1 _restr_angle_site_symmetry_2 _restr_angle_site_symmetry_3 _restr_angle_target _restr_angle_target_weight_param _restr_angle_diff O Si O 2_554 1 4_554 103.0000 0.7071 1.6926 O Si O 3_664 1 4_554 110.0000 0.4472 -1.3127 Si O Si 3 1 5 150.0000 0.4472 3.0700 loop_ _restr_torsion_atom_site_label_1 _restr_torsion_atom_site_label_2 _restr_torsion_atom_site_label_3 _restr_torsion_atom_site_label_4 _restr_torsion_site_symmetry_1 _restr_torsion_site_symmetry_2 _restr_torsion_site_symmetry_3 _restr_torsion_site_symmetry_4 _restr_torsion_angle_target _restr_torsion_weight_param _restr_torsion_diff O Si O Si 3_664 1 2_554 7_655 -30.0000 0.7071 6.9078 O Si O Si 3_664 1 4_554 2 90.0000 0.5774 11.7036 loop_ _restr_equal_distance_class_class_id _restr_equal_distance_class_target_weight_param _restr_equal_distance_class_average _restr_equal_distance_class_esd _restr_equal_distance_class_diff_max 1 1.0000 1.6160 0.0000 0.0000 loop_ _restr_equal_distance_atom_site_label_1 _restr_equal_distance_atom_site_label_2 _restr_equal_distance_site_symmetry_2 _restr_equal_distance_class_id Si O 2_554 1 Si O 4_554 1 Si O 3_664 1 """)
def create_sheet_hydrogen_bond_proxies ( sheet_params, pdb_hierarchy, weight, hbond_counts, distance_ideal, distance_cut, remove_outliers, log=sys.stdout) : assert (not None in [distance_ideal, distance_cut]) cache = pdb_hierarchy.atom_selection_cache() prev_strand = sheet_params.first_strand prev_selection = cache.selection(prev_strand) prev_rgs = _get_residue_groups_from_selection( pdb_hierarchy=pdb_hierarchy, bool_selection=prev_selection) n_proxies = 0 k = 0 generated_proxies = geometry_restraints.shared_bond_simple_proxy() while k < len(sheet_params.strand) : curr_strand = sheet_params.strand[k] curr_selection = cache.selection(curr_strand.selection) curr_start = None prev_start = None if curr_strand.bond_start_current is not None: curr_start = cache.selection(curr_strand.bond_start_current) if curr_strand.bond_start_previous is not None: prev_start = cache.selection(curr_strand.bond_start_previous) curr_rgs = _get_residue_groups_from_selection( pdb_hierarchy=pdb_hierarchy, bool_selection=curr_selection) i = j = 0 len_prev_residues = len(prev_rgs) len_curr_residues = len(curr_rgs) if curr_start is not None and prev_start is not None: if curr_start.count(True) < 1 or prev_start.count(True) < 1: error_msg = """\ Wrong registration in SHEET record. One of these selections "%s" or "%s" yielded zero or several atoms. Possible reason for it is the presence of insertion codes or alternative conformations for one of these residues or the .pdb file was edited without updating SHEET records.""" \ % (curr_strand.bond_start_current, curr_strand.bond_start_previous) raise Sorry(error_msg) current_start_res_is_donor = pdb_hierarchy.atoms().select(curr_start)[0].name.strip() == 'N' if (len_curr_residues > 0) and (len_prev_residues > 0) : i = _find_start_residue( residues=prev_rgs, start_selection=prev_start) j = _find_start_residue( residues=curr_rgs, start_selection=curr_start) if (i >= 0) and (j >= 0) : # move i,j pointers from registration residues to the beginning of # beta-strands while (1 < i and ((1 < j and curr_strand.sense == "parallel") or (j < len_curr_residues-2 and curr_strand.sense == "antiparallel"))): if curr_strand.sense == "parallel": i -= 2 j -= 2 elif curr_strand.sense == "antiparallel": i -= 2 j += 2 if (curr_strand.sense == "parallel") : # some tweaking for ensure correct donor assignment if i >= 2 and not current_start_res_is_donor: i -= 2 current_start_res_is_donor = not current_start_res_is_donor if j >= 2 and current_start_res_is_donor: j -= 2 current_start_res_is_donor = not current_start_res_is_donor while (i < len_prev_residues) and (j < len_curr_residues): if current_start_res_is_donor: donor_residue = curr_rgs[j] acceptor_residue = prev_rgs[i] i += 2 else: donor_residue = prev_rgs[i] acceptor_residue = curr_rgs[j] j += 2 current_start_res_is_donor = not current_start_res_is_donor if donor_residue.atom_groups()[0].resname.strip() != "PRO": proxies = _create_hbond_proxy( acceptor_atoms=acceptor_residue.atoms(), donor_atoms=donor_residue.atoms(), hbond_counts=hbond_counts, distance_ideal=distance_ideal, distance_cut=distance_cut, remove_outliers=remove_outliers, weight=weight, sigma=sheet_params.sigma, slack=sheet_params.slack, top_out=sheet_params.top_out, log=log) if proxies is not None: for proxy in proxies: generated_proxies.append(proxy) elif (curr_strand.sense == "antiparallel") : while(i < len_prev_residues and j >= 0): if (prev_rgs[i].atom_groups()[0].resname.strip() != "PRO") : proxies = _create_hbond_proxy( acceptor_atoms=curr_rgs[j].atoms(), donor_atoms=prev_rgs[i].atoms(), hbond_counts=hbond_counts, distance_ideal=distance_ideal, distance_cut=distance_cut, remove_outliers=remove_outliers, weight=weight, sigma=sheet_params.sigma, slack=sheet_params.slack, top_out=sheet_params.top_out, log=log) if proxies is not None: for proxy in proxies: generated_proxies.append(proxy) if (curr_rgs[j].atom_groups()[0].resname.strip() != "PRO") : proxies = _create_hbond_proxy( acceptor_atoms=prev_rgs[i].atoms(), donor_atoms=curr_rgs[j].atoms(), hbond_counts=hbond_counts, distance_ideal=distance_ideal, distance_cut=distance_cut, remove_outliers=remove_outliers, weight=weight, sigma=sheet_params.sigma, slack=sheet_params.slack, top_out=sheet_params.top_out, log=log) if proxies is not None: for proxy in proxies: generated_proxies.append(proxy) i += 2; j -= 2; else : print >> log, " WARNING: strand direction not defined!" print >> log, " previous: %s" % prev_strand print >> log, " current: %s" % curr_strand.selection else : print >> log, " WARNING: can't find start of bonding for strands!" print >> log, " previous: %s" % prev_strand print >> log, " current: %s" % curr_strand.selection else : print >> log, " WARNING: can't find one or more strands!" print >> log, " previous: %s" % prev_strand print >> log, " current: %s" % curr_strand.selection k += 1 prev_strand = curr_strand.selection prev_selection = curr_selection prev_rgs = curr_rgs return generated_proxies
def create_protein_hbond_proxies(self, annotation=None, log=sys.stdout): # assert as_regular_bond_proxies=True if annotation is None: annotation = self.actual_sec_str remove_outliers = self.params.secondary_structure.protein.remove_outliers from scitbx.array_family import flex atoms = self.pdb_hierarchy.atoms() hbond_counts = flex.int(atoms.size(), 0) distance_ideal = self.params.secondary_structure.protein.distance_ideal_n_o distance_cut = self.params.secondary_structure.protein.distance_cut_n_o if (distance_cut is None): distance_cut = -1 generated_proxies = geometry_restraints.shared_bond_simple_proxy() hb_angle_proxies = [] if self.params.secondary_structure.protein.enabled: for helix in self.params.secondary_structure.protein.helix: if helix.selection is not None: print >> log, " Processing helix ", helix.selection proxies, angle_proxies = proteins.create_helix_hydrogen_bond_proxies( params=helix, pdb_hierarchy=self.pdb_hierarchy, selection_cache=self.selection_cache, weight=1.0, hbond_counts=hbond_counts, distance_ideal=distance_ideal, distance_cut=distance_cut, remove_outliers=remove_outliers, restrain_hbond_angles=self.params.secondary_structure. protein.restrain_hbond_angles, log=log) if (proxies.size() == 0): print >> log, " No H-bonds generated for '%s'" % helix.selection continue else: generated_proxies.extend(proxies) hb_angle_proxies += angle_proxies for k, sheet in enumerate( self.params.secondary_structure.protein.sheet): print >> log, " Processing sheet with id=%s, first strand: %s" % ( sheet.sheet_id, sheet.first_strand) if sheet.first_strand is not None: proxies, angle_proxies = proteins.create_sheet_hydrogen_bond_proxies( sheet_params=sheet, pdb_hierarchy=self.pdb_hierarchy, selection_cache=self.selection_cache, weight=1.0, hbond_counts=hbond_counts, distance_ideal=distance_ideal, distance_cut=distance_cut, remove_outliers=remove_outliers, restrain_hbond_angles=self.params.secondary_structure. protein.restrain_hbond_angles, log=log) if (proxies.size() == 0): print >> log, \ " No H-bonds generated for sheet with id=%s" % sheet.sheet_id continue else: generated_proxies.extend(proxies) hb_angle_proxies += angle_proxies n_proxies = generated_proxies.size() print >> log, "" if (n_proxies == 0): print >> log, " No hydrogen bonds defined for protein." else: print >> log, " %d hydrogen bonds defined for protein." % n_proxies print >> log, " %d hydrogen bond angles defined for protein." % len( hb_angle_proxies) return generated_proxies, geometry_restraints.shared_angle_proxy( hb_angle_proxies)
def exercise_restrained_refinement(options): import random random.seed(1) flex.set_random_seed(1) xs0 = smtbx.development.random_xray_structure( sgtbx.space_group_info('P1'), n_scatterers=options.n_scatterers, elements="random") for sc in xs0.scatterers(): sc.flags.set_grad_site(True) sc0 = xs0.scatterers() uc = xs0.unit_cell() mi = xs0.build_miller_set(anomalous_flag=False, d_min=options.resolution) fo_sq = mi.structure_factors_from_scatterers( xs0, algorithm="direct").f_calc().norm() fo_sq = fo_sq.customized_copy(sigmas=flex.double(fo_sq.size(), 1)) i, j, k, l = random.sample(xrange(options.n_scatterers), 4) bond_proxies = geometry_restraints.shared_bond_simple_proxy() w = 1e9 d_ij = uc.distance(sc0[i].site, sc0[j].site)*0.8 bond_proxies.append(geom.bond_simple_proxy( i_seqs=(i, j), distance_ideal=d_ij, weight=w)) d_jk = uc.distance(sc0[j].site, sc0[k].site)*0.85 bond_proxies.append(geom.bond_simple_proxy( i_seqs=(j, k), distance_ideal=d_jk, weight=w)) d_ki = min(uc.distance(sc0[k].site, sc0[i].site)*0.9, (d_ij + d_jk)*0.8) bond_proxies.append(geom.bond_simple_proxy( i_seqs=(k, i), distance_ideal=d_ki, weight=w)) d_jl = uc.distance(sc0[j].site, sc0[l].site)*0.9 bond_proxies.append(geom.bond_simple_proxy( i_seqs=(j, l), distance_ideal=d_jl, weight=w)) d_lk = min(uc.distance(sc0[l].site, sc0[k].site)*0.8, 0.75*(d_jk + d_jl)) bond_proxies.append(geom.bond_simple_proxy( i_seqs=(l, k), distance_ideal=d_jl, weight=w)) restraints_manager = restraints.manager(bond_proxies=bond_proxies) xs1 = xs0.deep_copy_scatterers() xs1.shake_sites_in_place(rms_difference=0.1) def ls_problem(): xs = xs1.deep_copy_scatterers() reparametrisation = constraints.reparametrisation( structure=xs, constraints=[], connectivity_table=smtbx.utils.connectivity_table(xs), temperature=20) return least_squares.crystallographic_ls( fo_sq.as_xray_observations(), reparametrisation=reparametrisation, restraints_manager=restraints_manager) gradient_threshold, step_threshold = 1e-6, 1e-6 eps = 5e-3 ls = ls_problem() t = wall_clock_time() cycles = normal_eqns_solving.naive_iterations( ls, gradient_threshold=gradient_threshold, step_threshold=step_threshold, track_all=True) if options.verbose: print "%i %s steps in %.6f s" % (cycles.n_iterations, cycles, t.elapsed()) sc = ls.xray_structure.scatterers() for p in bond_proxies: d = uc.distance(*[ sc[i_pair].site for i_pair in p.i_seqs ]) assert approx_equal(d, p.distance_ideal, eps) ls = ls_problem() t = wall_clock_time() cycles = normal_eqns_solving.levenberg_marquardt_iterations( ls, gradient_threshold=gradient_threshold, step_threshold=step_threshold, tau=1e-3, track_all=True) if options.verbose: print "%i %s steps in %.6f s" % (cycles.n_iterations, cycles, t.elapsed()) sc = ls.xray_structure.scatterers() sc = ls.xray_structure.scatterers() for p in bond_proxies: d = uc.distance(*[ sc[i].site for i in p.i_seqs ]) assert approx_equal(d, p.distance_ideal, eps)
def get_basepair_hbond_proxies(pdb_hierarchy, bp_phil_params, hbond_distance_cutoff=3.4): assert pdb_hierarchy is not None bond_proxies_result = [] angle_proxies_result = [] if len(bp_phil_params) > 0: # return bond_proxies_result, angle_proxies_result selection_cache = pdb_hierarchy.atom_selection_cache() pdb_atoms = pdb_hierarchy.atoms() # dashes = open('dashes.pml', 'w') pdb_atoms = pdb_hierarchy.atoms() for base_pair in bp_phil_params: if (base_pair.base1 is not None and base_pair.base2 is not None and base_pair.enabled): selected_atoms_1 = selection_cache.iselection(base_pair.base1) selected_atoms_2 = selection_cache.iselection(base_pair.base2) if len(selected_atoms_1) == 0: raise Sorry( "Selection %s in base_pair retusulted in 0 atoms." % (base_pair.base1)) if len(selected_atoms_2) == 0: raise Sorry( "Selection %s in base_pair retusulted in 0 atoms." % (base_pair.base2)) a1 = pdb_atoms[selected_atoms_1[0]] a2 = pdb_atoms[selected_atoms_2[0]] if base_pair.saenger_class == 0: hbonds, saenger_class = get_h_bonds_for_basepair( a1, a2, distance_cutoff=hbond_distance_cutoff, log=sys.stdout, verbose=-1) base_pair.saenger_class = saenger_class hbonds = get_h_bonds_for_particular_basepair( (a1, a2), base_pair.saenger_class) for hb in hbonds: if hb[0] is None or hb[1] is None: print "NA hbond rejected because one of the atoms is absent" continue dist = hb[0].distance(hb[1]) if dist < hbond_distance_cutoff: if base_pair.restrain_hbonds: hb_target, hb_sigma = get_hb_lenght_targets(hb) p = geometry_restraints.bond_simple_proxy( i_seqs=[hb[0].i_seq, hb[1].i_seq], distance_ideal=hb_target, weight=1.0 / hb_sigma**2, slack=0, top_out=False, limit=1, origin_id=1) bond_proxies_result.append(p) # print "bond:", hb[0].id_str(), hb[1].id_str(), "(%4.2f, %4.2f)" % (hb_target, hb_sigma) # s1 = pdb_atoms[hb[0].i_seq].id_str() # s2 = pdb_atoms[hb[1].i_seq].id_str() # ps = "dist chain \"%s\" and resi %s and name %s, chain \"%s\" and resi %s and name %s\n" % ( # s1[14:15], s1[15:19], s1[5:8], s2[14:15], s2[15:19], s2[5:8]) # dashes.write(ps) if base_pair.restrain_hb_angles: angle_proxies_result += get_angle_proxies_for_bond( hb) else: print "NA hbond rejected:", hb[0].id_str( ), hb[1].id_str(), "distance=%.2f" % dist # dashes.close() return geometry_restraints.shared_bond_simple_proxy(bond_proxies_result), \ angle_proxies_result