def compute_target(self, sites_cart, selection=None): if (selection is not None): return maptbx.real_space_target_simple(unit_cell=self.unit_cell, density_map=self.target_map, sites_cart=sites_cart, selection=selection) else: return maptbx.real_space_target_simple(unit_cell=self.unit_cell, density_map=self.target_map, sites_cart=sites_cart)
def get_target_value(self, sites_cart, selection=None, target_map=None): if (target_map is None): target_map = self.target_map if (selection is None): return maptbx.real_space_target_simple(unit_cell=self.unit_cell, density_map=target_map, sites_cart=sites_cart) else: return maptbx.real_space_target_simple(unit_cell=self.unit_cell, density_map=target_map, sites_cart=sites_cart, selection=selection)
def get_target_value(self, sites_cart, selection=None): if(selection is None): return maptbx.real_space_target_simple( unit_cell = self.unit_cell, density_map = self.target_map, sites_cart = sites_cart) else: return maptbx.real_space_target_simple( unit_cell = self.unit_cell, density_map = self.target_map, sites_cart = sites_cart, selection = selection)
def compute_functional_and_gradients(O): if (O.number_of_function_evaluations == 0): O.number_of_function_evaluations += 1 return O.f_start, O.g_start O.number_of_function_evaluations += 1 O.sites_cart_residue = flex.vec3_double(O.x) rs_f = maptbx.real_space_target_simple( unit_cell=O.unit_cell, density_map=O.density_map, sites_cart=O.sites_cart_residue, selection=flex.bool(O.sites_cart_residue.size(),True)) O.real_space_target = rs_f rs_g = maptbx.real_space_gradients_simple( unit_cell=O.unit_cell, density_map=O.density_map, sites_cart=O.sites_cart_residue, delta=O.real_space_gradients_delta, selection=flex.bool(O.sites_cart_residue.size(),True)) O.rs_f = rs_f rs_f *= -O.real_space_target_weight rs_g *= -O.real_space_target_weight if (O.geometry_restraints_manager is None): f = rs_f g = rs_g else: O.sites_cart_all.set_selected(O.residue_i_seqs, O.sites_cart_residue) gr_e = O.geometry_restraints_manager.energies_sites( sites_cart=O.sites_cart_all, compute_gradients=True) f = rs_f + gr_e.target g = rs_g + gr_e.gradients.select(indices=O.residue_i_seqs) return f, g.as_double()
def compute_functional_and_gradients(O): if (O.number_of_function_evaluations == 0): O.number_of_function_evaluations += 1 return O.f_start, O.g_start O.number_of_function_evaluations += 1 O.residue_tardy_model.unpack_q(q_packed=O.x) O.sites_cart_residue = O.residue_tardy_model.sites_moved() rs_f = maptbx.real_space_target_simple( unit_cell=O.unit_cell, density_map=O.density_map, sites_cart=O.sites_cart_residue, selection=flex.bool(O.sites_cart_residue.size(),True)) rs_g = real_space_rigid_body_gradients_simple( unit_cell=O.unit_cell, density_map=O.density_map, sites_cart_0=O.sites_cart_residue_0, center_of_mass=O.residue_center_of_mass, q=O.x) O.rs_f = rs_f rs_f *= -O.real_space_target_weight rs_g *= -O.real_space_target_weight if (O.geometry_restraints_manager is None): f = rs_f g = rs_g else: O.sites_cart_all.set_selected(O.residue_i_seqs, O.sites_cart_residue) gr_e = O.geometry_restraints_manager.energies_sites( sites_cart=O.sites_cart_all, compute_gradients=True) O.__d_e_pot_d_sites = gr_e.gradients.select(indices=O.residue_i_seqs) f = rs_f + gr_e.target g = rs_g + O.residue_tardy_model.d_e_pot_d_q_packed() return f, g.as_double()
def __init__(self, unit_cell, target_map, residue, vector = None, selection=None): adopt_init_args(self, locals()) self.target = None self.sites_cart = None self.i_seqs = [] self.weights = flex.double() for el in self.residue.atoms().extract_element(): std_lbl = eltbx.xray_scattering.get_standard_label( label=el, exact=True, optional=True) self.weights.append(tiny_pse.table(std_lbl).weight()) self.occ = self.residue.atoms().extract_occ() self.vector_flat = None if(vector is not None): self.vector_flat = flex.size_t(flatten(self.vector)) self.sites_cart = self.residue.atoms().extract_xyz() if(selection is None): selection = self.vector_flat self.target = maptbx.real_space_target_simple( unit_cell = self.unit_cell, density_map = self.target_map, sites_cart = self.sites_cart, selection = selection)
def __init__(self, pdb_hierarchy, unit_cell, map_data): adopt_init_args(self, locals()) self.sites_cart = self.pdb_hierarchy.atoms().extract_xyz() self.target = maptbx.real_space_target_simple( unit_cell = self.unit_cell, density_map = self.map_data, sites_cart = self.sites_cart)
def compute_functional_and_gradients(self): if (self.number_of_function_evaluations == 0): self.number_of_function_evaluations += 1 return self.f_start, self.g_start self.number_of_function_evaluations += 1 self.residue_tardy_model.unpack_q(q_packed=self.x) self.sites_cart_residue = self.residue_tardy_model.sites_moved() if(self.states_collector is not None): self.states_collector.add(sites_cart = self.sites_cart_residue) rs_f = maptbx.real_space_target_simple( unit_cell=self.unit_cell, density_map=self.density_map, sites_cart=self.sites_cart_residue, selection=flex.bool(self.sites_cart_residue.size(),True)) rs_g = real_space_rigid_body_gradients_simple( unit_cell=self.unit_cell, density_map=self.density_map, sites_cart_0=self.sites_cart_residue_0, center_of_mass=self.residue_center_of_mass, q=self.x) self.rs_f = rs_f rs_f *= -self.real_space_target_weight rs_g *= -self.real_space_target_weight if (self.geometry_restraints_manager is None): f = rs_f g = rs_g else: gr_e = self.geometry_restraints_manager.energies_sites( sites_cart=self.sites_cart_residue, flags = self.cctbx_geometry_restraints_flags, compute_gradients=True) self.__d_e_pot_d_sites = gr_e.gradients f = rs_f + gr_e.target g = rs_g + self.residue_tardy_model.d_e_pot_d_q_packed() return f, g.as_double()
def __init__(self, unit_cell, target_map, residue, vector=None, selection=None): adopt_init_args(self, locals()) self.target = None self.sites_cart = None self.i_seqs = [] self.weights = flex.double() for el in self.residue.atoms().extract_element(): std_lbl = eltbx.xray_scattering.get_standard_label(label=el, exact=True, optional=True) self.weights.append(tiny_pse.table(std_lbl).weight()) self.occ = self.residue.atoms().extract_occ() self.vector_flat = None if (vector is not None): self.vector_flat = flex.size_t(flatten(self.vector)) self.sites_cart = self.residue.atoms().extract_xyz() if (selection is None): selection = self.vector_flat self.target = maptbx.real_space_target_simple( unit_cell=self.unit_cell, density_map=self.target_map, sites_cart=self.sites_cart, selection=selection)
def update(self, sites_cart): target = maptbx.real_space_target_simple(unit_cell=self.unit_cell, density_map=self.map_data, sites_cart=sites_cart) if (target > self.target): self.target = target self.sites_cart = sites_cart
def compute_functional_and_gradients(O): if (O.number_of_function_evaluations == 0): O.number_of_function_evaluations += 1 return O.f_start, O.g_start O.number_of_function_evaluations += 1 O.sites_cart_variable = flex.vec3_double(O.x) if (O.real_space_target_weight == 0): rs_f = 0. rs_g = flex.vec3_double(O.sites_cart_variable.size(), (0,0,0)) else: if (O.local_standard_deviations_radius is None): rs_f = maptbx.real_space_target_simple( unit_cell = O.unit_cell, density_map = O.density_map, sites_cart = O.sites_cart_variable, selection = O.selection_variable_real_space) rs_g = maptbx.real_space_gradients_simple( unit_cell = O.unit_cell, density_map = O.density_map, sites_cart = O.sites_cart_variable, delta = O.real_space_gradients_delta, selection = O.selection_variable_real_space) else: rs_f = local_standard_deviations_target( unit_cell=O.unit_cell, density_map=O.density_map, weight_map=O.weight_map, weight_map_scale_factor=O.weight_map_scale_factor, sites_cart=O.sites_cart_variable, site_radii=O.site_radii) rs_g = local_standard_deviations_gradients( unit_cell=O.unit_cell, density_map=O.density_map, weight_map=O.weight_map, weight_map_scale_factor=O.weight_map_scale_factor, sites_cart=O.sites_cart_variable, site_radii=O.site_radii, delta=O.real_space_gradients_delta) rs_f *= -O.real_space_target_weight rs_g *= -O.real_space_target_weight if (O.geometry_restraints_manager is None): f = rs_f g = rs_g else: if (O.selection_variable is None): O.sites_cart = O.sites_cart_variable else: O.sites_cart.set_selected(O.selection_variable, O.sites_cart_variable) if(O.states_collector is not None): O.states_collector.add(sites_cart = O.sites_cart) gr_e = O.geometry_restraints_manager.energies_sites( sites_cart=O.sites_cart, compute_gradients=True) gr_e_gradients = gr_e.gradients if (O.selection_variable is not None): gr_e_gradients = gr_e.gradients.select(O.selection_variable) f = rs_f + gr_e.target g = rs_g + gr_e_gradients return f, g.as_double()
def update(self, sites_cart): target = maptbx.real_space_target_simple( unit_cell=self.unit_cell, density_map=self.map_data, sites_cart=sites_cart ) if target > self.target: self.target = target self.sites_cart = sites_cart print self.target, target # XXX for debugging
def show_target(self, prefix): if (self.show): print(prefix, maptbx.real_space_target_simple( unit_cell=self.xray_structure.unit_cell(), density_map=self.map_data, sites_cart=self.xray_structure.sites_cart()), file=self.log)
def update(self, sites_cart, selection=None): if (selection is None): selection = self.vector_flat target = maptbx.real_space_target_simple(unit_cell=self.unit_cell, density_map=self.target_map, sites_cart=sites_cart, selection=selection) if (target > self.target): self.sites_cart = sites_cart self.target = target
def e_pot(O, sites_moved): if ( O.last_sites_moved is None or O.last_sites_moved.id() is not sites_moved.id()): O.last_sites_moved = sites_moved sites_cart = sites_moved # if (O.reduced_geo_manager is None): flags = None if (O.orca_experiments): flags = cctbx.geometry_restraints.flags.flags( nonbonded=False, default=True) else: # computing nonbonded interactions only with geo_manager, # contributions from other restraints below with reduced_geo_manager flags = cctbx.geometry_restraints.flags.flags( nonbonded=True, default=False) geo_energies = O.geo_manager.energies_sites( sites_cart=sites_cart, flags=flags, custom_nonbonded_function=O.custom_nonbonded_function, compute_gradients=True) if (0): # XXX orca_experiments print "geo_energies:" geo_energies.show() if (0): # XXX orca_experiments O.geo_manager.show_sorted(site_labels=O.site_labels) O.f = geo_energies.target O.g = geo_energies.gradients if (O.reduced_geo_manager is not None): reduced_geo_energies = O.reduced_geo_manager.energies_sites( sites_cart=sites_cart, compute_gradients=True) O.f += reduced_geo_energies.target O.g += reduced_geo_energies.gradients O.last_grms = group_args(geo=flex.mean_sq(O.g.as_double())**0.5) # if (O.density_map is not None): rs_f = maptbx.real_space_target_simple( unit_cell=O.geo_manager.crystal_symmetry.unit_cell(), density_map=O.density_map, sites_cart=sites_cart, selection=flex.bool(sites_cart.size(),True)) rs_g = maptbx.real_space_gradients_simple( unit_cell=O.geo_manager.crystal_symmetry.unit_cell(), density_map=O.density_map, sites_cart=sites_cart, delta=O.real_space_gradients_delta, selection=flex.bool(sites_cart.size(),True)) rs_f *= -O.real_space_target_weight rs_g *= -O.real_space_target_weight O.f += rs_f O.g += rs_g O.last_grms.real = flex.mean_sq(rs_g.as_double())**0.5 O.last_grms.real_or_xray = "real" # O.last_grms.total = flex.mean_sq(O.g.as_double())**0.5 return O.f
def update(self, sites_cart, selection=None): if(selection is None): selection = self.vector_flat target = maptbx.real_space_target_simple( unit_cell = self.unit_cell, density_map = self.target_map, sites_cart = sites_cart, selection = selection) if(target > self.target): self.sites_cart = sites_cart self.target = target
def check(): map = flex.double(flex.grid(22,26,36).set_focus(22,26,34)) site_frac = [i/n for i,n in zip(grid_point, map.focus())] sites_cart = flex.vec3_double([uc.orthogonalize(site_frac)]) target = maptbx.real_space_target_simple( unit_cell=uc, density_map=map, sites_cart=sites_cart, selection=flex.bool(sites_cart.size(), True)) assert approx_equal(target, 0) terms = maptbx.real_space_target_simple_per_site( unit_cell=uc, density_map=map, sites_cart=sites_cart) assert approx_equal(terms, [0]) grads = maptbx.real_space_gradients_simple( unit_cell=uc, density_map=map, sites_cart=sites_cart, delta=0.1, selection=flex.bool(sites_cart.size(), True)) assert approx_equal(grads, [(0,0,0)]) grid_point_mod = [i%n for i,n in zip(grid_point, map.focus())] map[grid_point_mod] = 1 target = maptbx.real_space_target_simple( unit_cell=uc, density_map=map, sites_cart=sites_cart, selection=flex.bool(sites_cart.size(), True)) assert approx_equal(target, 1) terms = maptbx.real_space_target_simple_per_site( unit_cell=uc, density_map=map, sites_cart=sites_cart) assert approx_equal(terms, [1]) grads = maptbx.real_space_gradients_simple( unit_cell=uc, density_map=map, sites_cart=sites_cart, delta=0.1, selection=flex.bool(sites_cart.size(), True)) assert approx_equal(grads, [(0,0,0)]) i,j,k = grid_point_mod u,v,w = map.focus() map[((i+1)%u,j,k)] = 0.3 map[(i,(j+1)%v,k)] = 0.5 map[(i,j,(k+1)%w)] = 0.7 target = maptbx.real_space_target_simple( unit_cell=uc, density_map=map, sites_cart=sites_cart, selection=flex.bool(sites_cart.size(), True)) assert approx_equal(target, 1) for delta in [0.1, 0.2]: grads = maptbx.real_space_gradients_simple( unit_cell=uc, density_map=map, sites_cart=sites_cart, delta=delta, selection=flex.bool(sites_cart.size(), True)) assert approx_equal(grads, [(0.3,0.5,0.7)])
def __init__(self, unit_cell, target_map, residue, rotamer_eval): adopt_init_args(self, locals()) self.target = None self.sites_cart = self.residue.atoms().extract_xyz() self.target_start = maptbx.real_space_target_simple( unit_cell = self.unit_cell, density_map = self.target_map, sites_cart = self.sites_cart)
def one_residue_tune_up(self, residue): re = self.rotamer_manager.rotamer_evaluator if (re.evaluate_residue(residue) == "OUTLIER"): sites_cart_start = residue.atoms().extract_xyz() mv1 = maptbx.real_space_target_simple( unit_cell=self.crystal_symmetry.unit_cell(), density_map=self.target_map, sites_cart=sites_cart_start) mmtbx.refinement.real_space.fit_residue.tune_up( residue=residue, unit_cell=self.crystal_symmetry.unit_cell(), target_map=self.target_map, mon_lib_srv=self.mon_lib_srv, rotamer_manager=self.rotamer_manager.rotamer_evaluator) mv2 = maptbx.real_space_target_simple( unit_cell=self.crystal_symmetry.unit_cell(), density_map=self.target_map, sites_cart=residue.atoms().extract_xyz()) pshift = abs(abs(mv1) - abs(mv2)) / ( (abs(mv1) + abs(mv2)) / 2) * 100. if (pshift < 3): self.sites_cart = self.pdb_hierarchy.atoms().extract_xyz() else: residue.atoms().set_xyz(sites_cart_start)
def get(i, delta): fs = [] for signed_delta in [delta, -delta]: q_delta[i] = q[i] + signed_delta aja = matrix.rt(scitbx.rigid_body.joint_lib_six_dof_aja_simplified( center_of_mass=center_of_mass, q=q_delta)) sites_cart_delta = aja * sites_cart_0 rs_f = maptbx.real_space_target_simple( unit_cell=unit_cell, density_map=density_map, sites_cart=sites_cart_delta, selection=flex.bool(sites_cart_delta.size(),True)) fs.append(rs_f) result.append((fs[0]-fs[1])/(2*delta))
def compute_functional_lite(pdb_hierarchy, residue, density_map, geometry_restraints_manager, real_space_target_weight): unit_cell = geometry_restraints_manager.crystal_symmetry.unit_cell() sites_cart_all = pdb_hierarchy.atoms().extract_xyz() residue_i_seqs = residue.atoms().extract_i_seq() x = sites_cart_all.select(indices=residue_i_seqs).as_double() sites_cart_residue = flex.vec3_double(x) rs_f = maptbx.real_space_target_simple( unit_cell=unit_cell, density_map=density_map, sites_cart=sites_cart_residue, selection=flex.bool(sites_cart_residue.size(),True)) return rs_f
def get_mean_side_chain_density_value_residue(residue, map_data, unit_cell): results = flex.double() get_class = iotbx.pdb.common_residue_names_get_class main_chain = ["N", "CA", "CB", "C", "O"] if (get_class(residue.resname) != "common_amino_acid"): return None sites_cart = flex.vec3_double() atoms = residue.atoms() for a in atoms: if (a.name.strip() in main_chain): continue sites_cart.append(a.xyz) if (sites_cart.size() == 0): return None mv = maptbx.real_space_target_simple(unit_cell=unit_cell, density_map=map_data, sites_cart=sites_cart) results.append(mv) return flex.mean_default(results, 0)
def exercise_03(): """ Exercise maptbx.target_and_gradients_simple. """ def compute_map(xray_structure, d_min=1.5, resolution_factor=1. / 4): fc = xray_structure.structure_factors(d_min=d_min).f_calc() fft_map = fc.fft_map(resolution_factor=resolution_factor) fft_map.apply_sigma_scaling() result = fft_map.real_map_unpadded() return result, fc, fft_map xrs = random_structure.xray_structure( space_group_info=sgtbx.space_group_info("P212121"), elements=["N", "C", "O", "S", "P"] * 10, volume_per_atom=50) map_target, tmp, tmp = compute_map(xray_structure=xrs) xrs_sh = xrs.deep_copy_scatterers() xrs_sh.shake_sites_in_place(mean_distance=0.8) # t1 = maptbx.real_space_target_simple(unit_cell=xrs.unit_cell(), density_map=map_target, sites_cart=xrs_sh.sites_cart(), selection=flex.bool( xrs_sh.scatterers().size(), True)) g1 = maptbx.real_space_gradients_simple(unit_cell=xrs.unit_cell(), density_map=map_target, sites_cart=xrs_sh.sites_cart(), delta=0.25, selection=flex.bool( xrs_sh.scatterers().size(), True)) o = maptbx.target_and_gradients_simple(unit_cell=xrs.unit_cell(), map_target=map_target, sites_cart=xrs_sh.sites_cart(), delta=0.25, selection=flex.bool( xrs_sh.scatterers().size(), True)) assert approx_equal(t1, o.target()) for gi, gj in zip(g1, o.gradients()): assert approx_equal(gi, gj)
def get_mean_side_chain_density_value(hierarchy, map_data, unit_cell): results = flex.double() get_class = iotbx.pdb.common_residue_names_get_class main_chain = ["N", "CA", "CB", "C", "O"] for model in hierarchy.models(): for chain in model.chains(): for residue_group in chain.residue_groups(): conformers = residue_group.conformers() if (len(conformers) > 1): continue for conformer in residue_group.conformers(): residue = conformer.only_residue() if (get_class(residue.resname) != "common_amino_acid"): continue sites_cart = flex.vec3_double() atoms = residue.atoms() for a in atoms: if (a.name.strip() in main_chain): continue sites_cart.append(a.xyz) if (sites_cart.size() == 0): continue mv = maptbx.real_space_target_simple(unit_cell=unit_cell, density_map=map_data, sites_cart=sites_cart) results.append(mv) return flex.mean_default(results, 0)
def fix_rama_outlier(self, pdb_hierarchy, out_res_num_list, prefix="", minimize=True, ss_annotation=None, tried_rama_angles_for_chain={}, tried_final_rama_angles_for_chain={}): def comb_pair_in_bad_pairs(comb_pair, bad_pairs): if None in comb_pair: return False all_combs = [comb_pair] all_combs.append((comb_pair[0]-20, comb_pair[1])) all_combs.append((comb_pair[0]+20, comb_pair[1])) all_combs.append((comb_pair[0], comb_pair[1]-20)) all_combs.append((comb_pair[0], comb_pair[1]+20)) all_c_adj = [] for p in all_combs: new_p = p if p[0] > 180: new_p = (p[0]-180, p[1]) if p[0] < -180: new_p = (p[0]+180, p[1]) if p[1] > 180: new_p = (p[0], p[1]-180) if p[0] < -180: new_p = (p[0], p[1]+180) all_c_adj.append(new_p) for p in all_c_adj: if p in bad_pairs: return True return False original_pdb_h = pdb_hierarchy.deep_copy() original_pdb_h.reset_atom_i_seqs() original_pdb_h_asc = original_pdb_h.atom_selection_cache() chain_id = original_pdb_h.only_model().only_chain().id all_results = [] # only forward # variants_searches = [ # #ccd_radius, change_all, change_radius, direction_forward # ((1, False, 0, True ),1), # # ((1, False, 0, False),1), # ((2, False, 0, True ),1), # # ((2, False, 0, False),1), # ((3, False, 0, True ),2), # # ((3, False, 0, False),2), # ((2, True, 1, True ),1), # # ((2, True, 1, False),1), # ((3, True, 1, True ),2), # # ((3, True, 1, False),2), # ((3, True, 2, True ),3), # # ((3, True, 2, False),3), # ] # only backward # variants_searches = [ # #ccd_radius, change_all, change_radius, direction_forward # # ((1, False, 0, True ),1), # ((1, False, 0, False),1), # # ((2, False, 0, True ),1), # ((2, False, 0, False),1), # # ((3, False, 0, True ),2), # ((3, False, 0, False),2), # # ((2, True, 1, True ),1), # ((2, True, 1, False),1), # # ((3, True, 1, True ),2), # ((3, True, 1, False),2), # # ((3, True, 2, True ),3), # ((3, True, 2, False),3), # ] # both variants_searches = [ #ccd_radius, change_all, change_radius, direction_forward ((1, False, 0, True ),1), ((1, False, 0, False),1), ((2, False, 0, True ),1), ((2, False, 0, False),1), ((3, False, 0, True ),2), ((3, False, 0, False),2), ((2, True, 1, True ),1), ((2, True, 1, False),1), ((3, True, 1, True ),2), ((3, True, 1, False),2), ((3, True, 2, True ),3), ((3, True, 2, False),3), ] decided_variants = [] for variant, level in variants_searches: if level <= self.params.variant_search_level: decided_variants.append(variant) for ccd_radius, change_all, change_radius, direction_forward in decided_variants: # while ccd_radius <= 3: fixing_omega = False print >> self.log, " Starting optimization with radius=%d, " % ccd_radius, print >> self.log, "change_all=%s, change_radius=%d, " % (change_all, change_radius), print >> self.log, "direction=forward" if direction_forward else "direction=backwards" self.log.flush() # (moving_h, moving_ref_atoms_iseqs, fixed_ref_atoms, m_selection, contains_ss_element, anchor_present) = get_fixed_moving_parts( pdb_hierarchy=pdb_hierarchy, out_res_num_list=out_res_num_list, # n_following=1, # n_previous=ccd_radius+ccd_radius-1, n_following=ccd_radius, n_previous=ccd_radius, ss_annotation=ss_annotation, direction_forward=direction_forward, log=self.log) # print " moving_ref_atoms_iseqs", moving_ref_atoms_iseqs print " moving_h resseqs:", [x.resseq for x in moving_h.residue_groups()] moving_h_set = [] all_angles_combination_f = starting_conformations.get_all_starting_conformations( moving_h, change_radius, n_outliers=len(out_res_num_list), direction_forward=direction_forward, cutoff=self.params.variant_number_cutoff, change_all=change_all, # log=self.log, check_omega=self.params.make_all_trans, ) # # print "len(all_angles_combination_f)", len(all_angles_combination_f) if len(all_angles_combination_f) == 0: print "In starting conformations - outlier was fixed?" # return result else: # here we should filter first ones that in # tried_rama_angles_for_chain filter_out = [] # [[tried values],[tried values],...] for three in generate_protein_threes( hierarchy=moving_h, geometry=None): if three[1].resseq in tried_rama_angles_for_chain.keys(): filter_out.append(tried_rama_angles_for_chain[three[1].resseq]) else: filter_out.append((None, None)) ff_all_angles = [] print "filter_out", filter_out for comb in all_angles_combination_f: good = True for comb_pair, bad_pairs in zip(comb, filter_out): if bad_pairs == (None, None): continue # print "comb_pair, bad_pairs", comb_pair, bad_pairs # if comb_pair in bad_pairs: if comb_pair_in_bad_pairs(comb_pair, bad_pairs): good = False # print " Rejecting comb_pair", comb_pair break if good: ff_all_angles.append(comb) print "len(all_angles_combination_f)", len(all_angles_combination_f) print "len(ff_all_angles)", len(ff_all_angles) n_added = 0 n_all_combination = len(ff_all_angles) if n_all_combination == 0: print >> self.log, "Strange - got 0 combinations." i_max = min(self.params.variant_number_cutoff, n_all_combination) # assert i_max > 0 step = 0 if i_max > 1: step = float(n_all_combination-1)/float(i_max-1) if step < 1: step = 1 for i in range(i_max): comb = ff_all_angles[int(round(step*i))] setted_h, fixed_omega = starting_conformations.set_rama_angles( moving_h, list(comb), direction_forward=direction_forward, check_omega=self.params.make_all_trans) fixing_omega = fixing_omega or fixed_omega moving_h_set.append(setted_h) # print >> self.log, "Model %d, angles:" % i, comb if self.params.make_all_trans and utils.n_bad_omegas(moving_h_set[-1]) != 0: print "Model_%d_angles_%s.pdb" % (i, comb), print "got ", utils.n_bad_omegas(moving_h_set[-1]), "bad omegas" moving_h_set[-1].write_pdb_file("Model_%d_angles_%s.pdb" % (i, comb)) utils.list_omega(moving_h_set[-1], self.log) assert 0 if len(moving_h_set) == 0: # outlier was fixed before somehow... # or there's a bug in get_starting_conformations print >> self.log, "outlier was fixed before somehow" return original_pdb_h print "self.tried_rama_angles inside", self.tried_rama_angles print "tried_rama_angles_for_chain", tried_rama_angles_for_chain print "checking values", ccd_radius, change_all, change_radius, direction_forward for i, h in enumerate(moving_h_set): # if [x in tried_rama_angles_for_chain.keys() for x in out_res_num_list].count(True) > 0: # print >> self.log, "Warning!!! make something here (check angles or so)" # print >> self.log, "Skipping nonstable solution, tried previously:", (ccd_radius, change_all, change_radius, direction_forward, i) # continue resulting_rmsd = None n_iter = 0 if anchor_present: fixed_ref_atoms_coors = [x.xyz for x in fixed_ref_atoms] # print "params to constructor", fixed_ref_atoms, h, moving_ref_atoms_iseqs # easy_pickle.dump(file_name="crash.pkl", obj=[ # fixed_ref_atoms_coors, # h, # moving_ref_atoms_iseqs, # direction_forward, # self.params.save_states]) ccd_obj = ccd_cpp(fixed_ref_atoms_coors, h, moving_ref_atoms_iseqs) ccd_obj.run(direction_forward=direction_forward, save_states=self.params.save_states) resulting_rmsd = ccd_obj.resulting_rmsd n_iter = ccd_obj.n_iter if self.params.save_states: states = ccd_obj.states states.write(file_name="%s%s_%d_%s_%d_%i_states.pdb" % (chain_id, out_res_num_list[0], ccd_radius, change_all, change_radius, i)) map_target = 0 if self.reference_map is not None: map_target = maptbx.real_space_target_simple( unit_cell = self.xrs.crystal_symmetry().unit_cell(), density_map = self.reference_map, sites_cart = h.atoms().extract_xyz()) mc_rmsd = get_main_chain_rmsd_range(moving_h, h, all_atoms=True) if self.verbose: print >> self.log, "Resulting anchor and backbone RMSDs, mapcc, n_iter for model %d:" % i, print >> self.log, resulting_rmsd, ",", mc_rmsd, ",", map_target, ",", n_iter self.log.flush() # # setting new coordinates # moved_with_side_chains_h = pdb_hierarchy.deep_copy() # setting xyz # for i_source, i_dest in enumerate(m_selection): moved_with_side_chains_h.atoms()[i_dest].set_xyz(h.atoms()[i_source].xyz) # set_xyz_smart( # dest_h=moved_with_side_chains_h, # source_h=h) # # placing side-chains # # moved_with_side_chains_h.write_pdb_file( # file_name="%s_before_sc_placement_%d.pdb" % (prefix, i)) placing_range = get_res_nums_around(moved_with_side_chains_h, center_resnum_list=out_res_num_list, n_following=ccd_radius, n_previous=ccd_radius, include_intermediate=True, avoid_ss_annot=ss_annotation) place_side_chains(moved_with_side_chains_h, original_pdb_h, original_pdb_h_asc, self.rotamer_manager, placing_range, self.ideal_res_dict) # moved_with_side_chains_h.write_pdb_file( # file_name="%s_after_sc_placement_%d.pdb" % (prefix, i)) # # finalizing with geometry_minimization # # determining angles of interest # print "Recording picked angle for outliers" threes = generate_protein_threes( # hierarchy=moving_h, hierarchy=h, geometry=None) start_angles = [] final_angles = [] for angle_pair, three in zip(ff_all_angles[int(round(step*i))], threes): # print "three[1].resseq in out_res_num_list, angle_pair", three[1].resseq, out_res_num_list, angle_pair if three[1].resseq in out_res_num_list: # if three[1].resseq not in tried_rama_angles_for_chain.keys(): # tried_rama_angles_for_chain[three[1].resseq] = [] start_angles.append((three[1].resseq, angle_pair)) ps_angles = three.get_phi_psi_angles() final_angles.append((three[1].resseq, tuple(ps_angles))) # tried_rama_angles_for_chain[three[1].resseq].append(angle_pair) # print >> self.log, "Ended up with", three[1].resseq, "%.1f %.1f" % (ps_angles[0], ps_angles[1]) # print "Updated tried_rama_angles_for_chain:", tried_rama_angles_for_chain if (not self.ccd_solution_is_duplicated( final_angles=final_angles, tried_final_rama_angles_for_chain=tried_final_rama_angles_for_chain)): all_results.append((moved_with_side_chains_h.deep_copy(), mc_rmsd, resulting_rmsd, map_target, n_iter)) else: continue if self.ccd_solution_is_ok( anchor_rmsd=resulting_rmsd, mc_rmsd=mc_rmsd, n_outliers=len(out_res_num_list), ccd_radius=ccd_radius, change_all_angles=change_all, change_radius=change_radius, contains_ss_element=contains_ss_element, fixing_omega=fixing_omega): print "Choosen result (mc_rmsd, anchor_rmsd, map_target, n_iter):", mc_rmsd, resulting_rmsd, map_target, n_iter # Save to tried_ccds for rn, angles in start_angles: if rn not in tried_rama_angles_for_chain.keys(): tried_rama_angles_for_chain[rn] = [] tried_rama_angles_for_chain[rn].append(angles) # Save final angles for rn, angles in final_angles: if rn not in tried_final_rama_angles_for_chain.keys(): tried_final_rama_angles_for_chain[rn] = [] tried_final_rama_angles_for_chain[rn].append(angles) print >> self.log, "Ended up with", final_angles print >> self.log, "Updated tried_rama_angles_for_chain:", tried_rama_angles_for_chain print >> self.log, "Updated tried_final_rama_angles_for_chain:", tried_final_rama_angles_for_chain self.log.flush() if minimize: print >> self.log, "minimizing..." # moved_with_side_chains_h.write_pdb_file( # file_name="%s_result_before_min_%d.pdb" % (prefix, i)) if self.reference_map is None: minimize_wrapper_for_ramachandran( hierarchy=moved_with_side_chains_h, xrs=xrs, original_pdb_h=original_pdb_h, log=self.log, grm=self.grm, ss_annotation=self.secondary_structure_annotation) else: mwwm = minimize_wrapper_with_map( pdb_h=moved_with_side_chains_h, xrs=xrs, target_map=self.reference_map, grm=self.grm, ss_annotation=self.secondary_structure_annotation, log=self.log) # moved_with_side_chains_h.write_pdb_file( # file_name="%s_result_minimized_%d.pdb" % (prefix, i)) final_rmsd = get_main_chain_rmsd_range(moved_with_side_chains_h, original_pdb_h, placing_range) print >> self.log, "FINAL RMSD after minimization:", final_rmsd return moved_with_side_chains_h all_results.sort(key=lambda tup: tup[1]) if self.verbose: print >> self.log, "ALL RESULTS:" i = 0 for ar in all_results: print >> self.log, ar[1:], if ar[2] < 0.4: # fn = "variant_%d.pdb" % i # ar[0].write_pdb_file(file_name=fn) # print fn i += 1 else: print >> self.log, " no output" if self.params.force_rama_fixes: # find and apply the best varian from all_results. This would be the one # with the smallest rmsd given satisfactory closure print >> self.log, "Applying the best found variant:", i = 0 while i < len(all_results) and all_results[i][2] > 1.5: i += 1 # apply # === duplication!!!! if i < len(all_results): print >> self.log, all_results[i][1:] if minimize: print >> self.log, "minimizing..." # all_results[i][0].write_pdb_file( # file_name="%s_result_before_min_%d.pdb" % (prefix, i)) if self.reference_map is None: minimize_wrapper_for_ramachandran( hierarchy=all_results[i][0], xrs=xrs, original_pdb_h=original_pdb_h, log=self.log, grm=self.grm, ss_annotation=self.secondary_structure_annotation) else: mwwm = minimize_wrapper_with_map( pdb_h=all_results[i][0], xrs=xrs, target_map=self.reference_map, grm=self.grm, ss_annotation=self.secondary_structure_annotation, log=self.log) # all_results[i][0].write_pdb_file( # file_name="%s_result_minimized_%d.pdb" % (prefix, i)) final_rmsd = get_main_chain_rmsd_range(all_results[i][0], original_pdb_h, placing_range) print >> self.log, "FINAL RMSD after minimization:", final_rmsd return all_results[i][0] else: print >> self.log, " NOT FOUND!" for i in all_results: print >> self.log, i[1:] # === end of duplication!!!! else: print >> self.log, "Epic FAIL: failed to fix rama outlier:", out_res_num_list print >> self.log, " Options were: (mc_rmsd, resultign_rmsd, n_iter)" for i in all_results: print >> self.log, i[1:] return original_pdb_h
def target(self): return -1.*maptbx.real_space_target_simple( unit_cell = self.unit_cell, density_map = self.density_map, sites_cart = self.sites_cart, selection = self.selection)
def show_target(self, prefix): if(self.show): print >> self.log, prefix, maptbx.real_space_target_simple( unit_cell = self.xray_structure.unit_cell(), density_map = self.map_data, sites_cart = self.xray_structure.sites_cart())
def target(self): return -1. * maptbx.real_space_target_simple( unit_cell=self.unit_cell, density_map=self.density_map, sites_cart=self.sites_cart, selection=self.selection)
def fix_rama_outlier(self, pdb_hierarchy, out_res_num, prefix="", minimize=True, ss_annotation=None): original_pdb_h = pdb_hierarchy.deep_copy() original_pdb_h.reset_atom_i_seqs() chain_id = original_pdb_h.only_model().only_chain().id all_results = [] variants_searches = [ ((1, False, 0), 1), ((2, False, 0), 1), ((3, False, 0), 2), ((2, True, 1), 1), ((3, True, 1), 2), ((3, True, 2), 3), ] decided_variants = [] for variant, level in variants_searches: if level <= self.params.variant_search_level: decided_variants.append(variant) for ccd_radius, change_all, change_radius in decided_variants: # while ccd_radius <= 3: print >> self.log, " Starting optimization with radius, change_all, change_radius:", ccd_radius, change_all, change_radius self.log.flush() # moving_h, moving_ref_atoms_iseqs, fixed_ref_atoms, m_selection, contains_ss_element = get_fixed_moving_parts( pdb_hierarchy=pdb_hierarchy, out_res_num=out_res_num, n_following=ccd_radius, n_previous=ccd_radius, ss_annotation=ss_annotation) moving_h_set = None if change_all: moving_h_set = starting_conformations.get_all_starting_conformations( moving_h, change_radius, cutoff=self.params.variant_number_cutoff, # log=self.log, ) else: moving_h_set = starting_conformations.get_starting_conformations( moving_h, cutoff=self.params.variant_number_cutoff, # log=self.log, ) if len(moving_h_set) == 0: # outlier was fixed before somehow... # or there's a bug in get_starting_conformations return original_pdb_h for i, h in enumerate(moving_h_set): fixed_ref_atoms_coors = [x.xyz for x in fixed_ref_atoms] # print "params to constructor", fixed_ref_atoms, h, moving_ref_atoms_iseqs ccd_obj = ccd_cpp(fixed_ref_atoms_coors, h, moving_ref_atoms_iseqs) ccd_obj.run() resulting_rmsd = ccd_obj.resulting_rmsd n_iter = ccd_obj.n_iter # states = ccd_obj.states # if self.params.save_states: # states.write(file_name="%s%s_%d_%s_%d_%i_states.pdb" % (chain_id, out_res_num, ccd_radius, change_all, change_radius, i)) map_target = 0 if self.reference_map is not None: map_target = maptbx.real_space_target_simple( unit_cell=self.xrs.crystal_symmetry().unit_cell(), density_map=self.reference_map, sites_cart=h.atoms().extract_xyz()) mc_rmsd = get_main_chain_rmsd_range(moving_h, h, all_atoms=True) if self.verbose: print >> self.log, "Resulting anchor and backbone RMSDs, mapcc, n_iter for model %d:" % i, print >> self.log, resulting_rmsd, ",", mc_rmsd, ",", map_target, ",", n_iter self.log.flush() # # setting new coordinates # moved_with_side_chains_h = pdb_hierarchy.deep_copy() # setting xyz # for i_source, i_dest in enumerate(m_selection): moved_with_side_chains_h.atoms()[i_dest].set_xyz( h.atoms()[i_source].xyz) # set_xyz_smart( # dest_h=moved_with_side_chains_h, # source_h=h) # # placing side-chains # # moved_with_side_chains_h.write_pdb_file( # file_name="%s_before_sc_placement_%d.pdb" % (prefix, i)) placing_range = get_res_nums_around(moved_with_side_chains_h, center_resnum=out_res_num, n_following=ccd_radius, n_previous=ccd_radius, include_intermediate=True) place_side_chains(moved_with_side_chains_h, original_pdb_h, self.rotamer_manager, placing_range) # moved_with_side_chains_h.write_pdb_file( # file_name="%s_after_sc_placement_%d.pdb" % (prefix, i)) # # finalizing with geometry_minimization # all_results.append( (moved_with_side_chains_h.deep_copy(), mc_rmsd, resulting_rmsd, map_target, n_iter)) if self.ccd_solution_is_ok( anchor_rmsd=resulting_rmsd, mc_rmsd=mc_rmsd, ccd_radius=ccd_radius, change_all_angles=change_all, change_radius=change_radius, contains_ss_element=contains_ss_element): print "Choosen result (mc_rmsd, anchor_rmsd, map_target, n_iter):", mc_rmsd, resulting_rmsd, map_target, n_iter self.log.flush() if minimize: print >> self.log, "minimizing..." # moved_with_side_chains_h.write_pdb_file( # file_name="%s_result_before_min_%d.pdb" % (prefix, i)) if self.reference_map is None: minimize_wrapper_for_ramachandran( hierarchy=moved_with_side_chains_h, xrs=xrs, original_pdb_h=original_pdb_h, log=self.log, grm=self.grm, ss_annotation=self. secondary_structure_annotation) else: mwwm = minimize_wrapper_with_map( pdb_h=moved_with_side_chains_h, xrs=xrs, target_map=self.reference_map, grm=self.grm, ss_annotation=self. secondary_structure_annotation, log=self.log) # moved_with_side_chains_h.write_pdb_file( # file_name="%s_result_minimized_%d.pdb" % (prefix, i)) final_rmsd = get_main_chain_rmsd_range( moved_with_side_chains_h, original_pdb_h, placing_range) print >> self.log, "FINAL RMSD after minimization:", final_rmsd return moved_with_side_chains_h all_results.sort(key=lambda tup: tup[1]) if self.verbose: print >> self.log, "ALL RESULTS:" i = 0 for ar in all_results: print >> self.log, ar[1:], if ar[2] < 0.4: # fn = "variant_%d.pdb" % i # ar[0].write_pdb_file(file_name=fn) # print fn i += 1 else: print >> self.log, " no output" if self.params.force_rama_fixes: # find and apply the best varian from all_results. This would be the one # with the smallest rmsd given satisfactory closure print >> self.log, "Applying the best found variant:", i = 0 while i < len(all_results) and all_results[i][2] > 0.1: i += 1 # apply # === duplication!!!! if i < len(all_results): print >> self.log, all_results[i][1:] if minimize: print >> self.log, "minimizing..." # all_results[i][0].write_pdb_file( # file_name="%s_result_before_min_%d.pdb" % (prefix, i)) minimize_wrapper_for_ramachandran( hierarchy=all_results[i][0], xrs=xrs, original_pdb_h=original_pdb_h, log=self.log, ss_annotation=self.secondary_structure_annotation) # all_results[i][0].write_pdb_file( # file_name="%s_result_minimized_%d.pdb" % (prefix, i)) final_rmsd = get_main_chain_rmsd_range(all_results[i][0], original_pdb_h, placing_range) print >> self.log, "FINAL RMSD after minimization:", final_rmsd return moved_with_side_chains_h else: print >> self.log, " NOT FOUND!" for i in all_results: print >> self.log, i[1:] # === end of duplication!!!! else: print >> self.log, "Epic FAIL: failed to fix rama outlier" print >> self.log, " Options were: (mc_rmsd, resultign_rmsd, n_iter)" for i in all_results: print >> self.log, i[1:] # STOP() return original_pdb_h
def __init__(self, pdb_hierarchy, unit_cell, map_data): adopt_init_args(self, locals()) self.sites_cart = self.pdb_hierarchy.atoms().extract_xyz() self.target = maptbx.real_space_target_simple( unit_cell=self.unit_cell, density_map=self.map_data, sites_cart=self.sites_cart )
def exercise_real_space_gradients_simple(timing): uc = uctbx.unit_cell((11,13,17)) def check(): map = flex.double(flex.grid(22,26,36).set_focus(22,26,34)) site_frac = [i/n for i,n in zip(grid_point, map.focus())] sites_cart = flex.vec3_double([uc.orthogonalize(site_frac)]) target = maptbx.real_space_target_simple( unit_cell=uc, density_map=map, sites_cart=sites_cart, selection=flex.bool(sites_cart.size(), True)) assert approx_equal(target, 0) terms = maptbx.real_space_target_simple_per_site( unit_cell=uc, density_map=map, sites_cart=sites_cart) assert approx_equal(terms, [0]) grads = maptbx.real_space_gradients_simple( unit_cell=uc, density_map=map, sites_cart=sites_cart, delta=0.1, selection=flex.bool(sites_cart.size(), True)) assert approx_equal(grads, [(0,0,0)]) grid_point_mod = [i%n for i,n in zip(grid_point, map.focus())] map[grid_point_mod] = 1 target = maptbx.real_space_target_simple( unit_cell=uc, density_map=map, sites_cart=sites_cart, selection=flex.bool(sites_cart.size(), True)) assert approx_equal(target, 1) terms = maptbx.real_space_target_simple_per_site( unit_cell=uc, density_map=map, sites_cart=sites_cart) assert approx_equal(terms, [1]) grads = maptbx.real_space_gradients_simple( unit_cell=uc, density_map=map, sites_cart=sites_cart, delta=0.1, selection=flex.bool(sites_cart.size(), True)) assert approx_equal(grads, [(0,0,0)]) i,j,k = grid_point_mod u,v,w = map.focus() map[((i+1)%u,j,k)] = 0.3 map[(i,(j+1)%v,k)] = 0.5 map[(i,j,(k+1)%w)] = 0.7 target = maptbx.real_space_target_simple( unit_cell=uc, density_map=map, sites_cart=sites_cart, selection=flex.bool(sites_cart.size(), True)) assert approx_equal(target, 1) for delta in [0.1, 0.2]: grads = maptbx.real_space_gradients_simple( unit_cell=uc, density_map=map, sites_cart=sites_cart, delta=delta, selection=flex.bool(sites_cart.size(), True)) assert approx_equal(grads, [(0.3,0.5,0.7)]) for grid_point in [(0,0,0), (3,4,5), (-3,15,20)]: check() for i_trial in xrange(10): grid_point = [random.randrange(-100,100) for i in [0,1,2]] check() if (timing): n = 1000000 else: n = 10 sites_cart = flex.vec3_double(flex.random_double(size=n*3)*40-20) map = flex.double(flex.grid(22,26,36).set_focus(22,26,34), 1) target = maptbx.real_space_target_simple( unit_cell=uc, density_map=map, sites_cart=sites_cart, selection=flex.bool(sites_cart.size(), True)) assert approx_equal(target, n) t0 = time.time() maptbx.real_space_gradients_simple( unit_cell=uc, density_map=map, sites_cart=sites_cart, delta=0.1, selection=flex.bool(sites_cart.size(), True)) tm = time.time() - t0 msg = "real_space_gradients_simple: %.2f s / %d sites" % (tm, n) if (tm >= 0.01): msg += ", %.0f sites / s" % (n / tm) if (timing): print msg