def finite_differences_scalar(parameter_name, target_ftor, structure, delta=0.00001): derivatives = flex.double() for i_scatterer in range(structure.scatterers().size()): target_values = [] for d_sign in (-1, 1): modified_structure = structure.deep_copy_scatterers() ms = modified_structure.scatterers()[i_scatterer] if (parameter_name == "u_iso" and ms.flags.use_u_iso()): ms.u_iso += d_sign * delta elif (parameter_name == "tan_u_iso" and ms.flags.use_u_iso()): assert ms.u_iso >= 0 x = math.tan(ms.u_iso * math.pi / adptbx.b_as_u(ms.flags.param) - math.pi / 2) + d_sign * delta ms.u_iso = adptbx.b_as_u( ms.flags.param) / math.pi * (math.atan(x) + math.pi / 2) elif (parameter_name == "occupancy"): ms.occupancy += d_sign * delta elif (parameter_name == "fp"): ms.fp = ms.fp + d_sign * delta elif (parameter_name == "fdp"): ms.fdp = ms.fdp + d_sign * delta else: raise RuntimeError f_calc = target_ftor.f_obs().structure_factors_from_scatterers( xray_structure=modified_structure, algorithm="direct").f_calc() target_result = target_ftor(f_calc, compute_derivatives=False) target_values.append(target_result.target()) derivative = (target_values[1] - target_values[0]) / (2 * delta) derivatives.append(derivative) return derivatives
def read_scatterer(flds, default_b_iso=3.0): scatterer = xray.scatterer(scattering_type="const") # Label [ScatFact] x y z [Occ [Biso]] try: scatterer.label = flds[0] try: float(flds[1]) except Exception: offs = 2 scatterer.scattering_type = eltbx.xray_scattering.get_standard_label(label=flds[1], exact=True) else: offs = 1 scatterer.scattering_type = eltbx.xray_scattering.get_standard_label(label=flds[0], exact=False) site = flds[offs : offs + 3] for i in xrange(3): site[i] = float(site[i]) scatterer.site = site scatterer.occupancy = 1.0 scatterer.set_use_u_iso_only() scatterer.u_iso = adptbx.b_as_u(default_b_iso) if len(flds) >= offs + 4: scatterer.occupancy = float(flds[offs + 3]) if len(flds) == offs + 5: scatterer.u_iso = adptbx.b_as_u(float(flds[offs + 4])) else: assert len(flds) < offs + 5 except Exception: raise cgi_utils.FormatError, flds return scatterer
def finite_differences_scalar(parameter_name, target_ftor, structure, delta=0.00001): derivatives = flex.double() for i_scatterer in xrange(structure.scatterers().size()): target_values = [] for d_sign in (-1, 1): modified_structure = structure.deep_copy_scatterers() ms = modified_structure.scatterers()[i_scatterer] if (parameter_name == "u_iso" and ms.flags.use_u_iso()): ms.u_iso += d_sign * delta elif (parameter_name == "tan_u_iso" and ms.flags.use_u_iso()): assert ms.u_iso >= 0 x = math.tan(ms.u_iso*math.pi/adptbx.b_as_u(ms.flags.param)-math.pi/2) + d_sign * delta ms.u_iso = adptbx.b_as_u(ms.flags.param)/math.pi*(math.atan(x)+math.pi/2) elif (parameter_name == "occupancy"): ms.occupancy += d_sign * delta elif (parameter_name == "fp"): ms.fp = ms.fp + d_sign * delta elif (parameter_name == "fdp"): ms.fdp = ms.fdp + d_sign * delta else: raise RuntimeError f_calc = target_ftor.f_obs().structure_factors_from_scatterers( xray_structure=modified_structure, algorithm="direct").f_calc() target_result = target_ftor(f_calc, compute_derivatives=False) target_values.append(target_result.target()) derivative = (target_values[1] - target_values[0]) / (2 * delta) derivatives.append(derivative) return derivatives
def read_scatterer(flds, default_b_iso=3.0): scatterer = xray.scatterer(scattering_type="const") # Label [ScatFact] x y z [Occ [Biso]] try: scatterer.label = flds[0] try: float(flds[1]) except Exception: offs = 2 scatterer.scattering_type = eltbx.xray_scattering.get_standard_label( label=flds[1], exact=True) else: offs = 1 scatterer.scattering_type = eltbx.xray_scattering.get_standard_label( label=flds[0], exact=False) site = flds[offs:offs + 3] for i in xrange(3): site[i] = float(site[i]) scatterer.site = site scatterer.occupancy = 1. scatterer.set_use_u_iso_only() scatterer.u_iso = adptbx.b_as_u(default_b_iso) if (len(flds) >= offs + 4): scatterer.occupancy = float(flds[offs + 3]) if (len(flds) == offs + 5): scatterer.u_iso = adptbx.b_as_u(float(flds[offs + 4])) else: assert (len(flds) < offs + 5) except Exception: raise cgi_utils.FormatError, flds return scatterer
def apply_aniso_correction(self, f_array=None): if self.b_cart is None or self.b_cart_aniso_removed is None: return f_array # nothing to do from mmtbx.scaling import absolute_scaling from cctbx import adptbx u_star = adptbx.u_cart_as_u_star(f_array.unit_cell(), adptbx.b_as_u(self.b_cart)) u_star_aniso_removed = adptbx.u_cart_as_u_star( f_array.unit_cell(), adptbx.b_as_u(self.b_cart_aniso_removed)) no_aniso_array = absolute_scaling.anisotropic_correction( f_array, 0.0, u_star, must_be_greater_than=-0.0001) no_aniso_array = absolute_scaling.anisotropic_correction( no_aniso_array, 0.0, u_star_aniso_removed, must_be_greater_than=-0.0001) no_aniso_array = no_aniso_array.set_observation_type(f_array) return no_aniso_array
def __init__(self, label="", site=(0, 0, 0), u=None, occupancy=1, scattering_info="", b=None): assert u is None or b is None if (b is not None): u = adptbx.b_as_u(b) elif (u is None): u = 0 if (type(scattering_info) == type("")): if (scattering_info == ""): scattering_info = neutron_news_1992_table(label, 0) else: scattering_info = neutron_news_1992_table(scattering_info, 1) self.label = label self.site = site try: self.u_iso = float(u) self.anisotropic_flag = False except Exception: assert len(u) == 6 self.anisotropic_flag = True self.u_star = tuple([float(uij) for uij in u]) self.occupancy = occupancy self.scattering_info = scattering_info self._multiplicity = 0 self._weight = 0
def iass_as_xray_structure(self, iass): ias_xray_structure = xray.structure( crystal_symmetry = self.xray_structure.crystal_symmetry()) for ias in iass: assert ias.status is not None if(ias.status): if(self.params.use_map): site_cart = ias.peak_position_cart b_iso = ias.b_iso q = ias.q else: site_cart = ias.site_cart_predicted b_iso = (ias.atom_1.b_iso + ias.atom_2.b_iso)*0.5 q = self.params.initial_ias_occupancy if(b_iso is None): b_iso = (ias.atom_1.b_iso + ias.atom_2.b_iso)*0.5 if(q is None): q = self.params.initial_ias_occupancy if(site_cart is None): site_cart = ias.site_cart_predicted assert [b_iso, q].count(None) == 0 if(site_cart is not None): ias_scatterer = xray.scatterer( label = ias.name, scattering_type = ias.name, site = self.xray_structure.unit_cell().fractionalize(site_cart), u = adptbx.b_as_u(b_iso), occupancy = q) ias_xray_structure.add_scatterer(ias_scatterer) ias_xray_structure.scattering_type_registry( custom_dict = ias_scattering_dict) return ias_xray_structure
def ml_normalisation(self, aniso=False): # estimate number of residues per unit cell mr = matthews.matthews_rupp(self.intensities.crystal_symmetry()) n_residues = mr.n_residues # estimate B-factor and scale factors for normalisation if aniso: normalisation = absolute_scaling.ml_aniso_absolute_scaling( self.intensities, n_residues=n_residues) u_star = normalisation.u_star else: normalisation = absolute_scaling.ml_iso_absolute_scaling( self.intensities, n_residues=n_residues) u_star = adptbx.b_as_u( adptbx.u_iso_as_u_star( self.intensities.unit_cell(), normalisation.b_wilson)) # apply scales self.intensities = self.intensities.customized_copy( data=scaling.ml_normalise_aniso( self.intensities.indices(), self.intensities.data(), normalisation.p_scale, self.intensities.unit_cell(), u_star), sigmas=scaling.ml_normalise_aniso( self.intensities.indices(), self.intensities.sigmas(), normalisation.p_scale, self.intensities.unit_cell(), u_star)).set_info(self.intensities.info()) # record output in log file s = StringIO() mr.show(out=s) normalisation.show(out=s) logger.info(s.getvalue())
def __init__(self, label="", site=(0,0,0), u=None, occupancy=1, scattering_info="", b=None): assert u is None or b is None if (b is not None): u = adptbx.b_as_u(b) elif (u is None): u = 0 if (type(scattering_info) == type("")): if (scattering_info == ""): scattering_info = neutron_news_1992_table(label, 0) else: scattering_info = neutron_news_1992_table(scattering_info, 1) self.label = label self.site = site try: self.u_iso = float(u) self.anisotropic_flag = False except Exception: assert len(u) == 6 self.anisotropic_flag = True self.u_star = tuple([float(uij) for uij in u]) self.occupancy = occupancy self.scattering_info = scattering_info self._multiplicity = 0 self._weight = 0
def _find_peaks(self, min_peak_peak_distance=1.5): cg = maptbx.crystal_gridding( space_group_info=self.model.crystal_symmetry().space_group_info(), symmetry_flags=maptbx.use_space_group_symmetry, unit_cell=self.unit_cell, pre_determined_n_real=self.map_data.accessor().all()) cgt = maptbx.crystal_gridding_tags(gridding=cg) peak_search_parameters = maptbx.peak_search_parameters( peak_search_level=3, max_peaks=0, peak_cutoff=self.cutoff, interpolate=True, min_distance_sym_equiv=0, general_positions_only=False, # ???????XXXXXXXXXXXXXX min_cross_distance=min_peak_peak_distance, min_cubicle_edge=5) psr = cgt.peak_search(parameters=peak_search_parameters, map=self.map_data).all(max_clusters=99999999) # Convert peaks into water xray.structure mean_b = flex.mean(self.model.get_hierarchy().atoms().extract_b()) sp = crystal.special_position_settings(self.model.crystal_symmetry()) scatterers = flex.xray_scatterer() for site_frac in psr.sites(): sc = xray.scatterer(label="O", site=site_frac, u=adptbx.b_as_u(mean_b), occupancy=1.0) scatterers.append(sc) self.xrs_water = xray.structure(sp, scatterers) # self.ma.add(" total peaks found: %d" % self.xrs_water.scatterers().size()) self.ma.add(" B factors set to : %8.3f" % mean_b) if (self.debug): self._write_pdb_file(file_name_prefix="hoh_all_peaks")
def debye_waller_factors(self, miller_index=None, miller_indices=None, u_iso=None, b_iso=None, u_cart=None, b_cart=None, u_cif=None, u_star=None, exp_arg_limit=50, truncate_exp_arg=False): assert [miller_index, miller_indices].count(None) == 1 assert [u_iso, b_iso, u_cart, b_cart, u_cif, u_star].count(None) == 5 from cctbx import adptbx h = miller_index if (h is None): h = miller_indices if (u_iso is not None): b_iso = adptbx.u_as_b(u_iso) if (b_iso is not None): return adptbx.debye_waller_factor_b_iso( self.stol_sq(h), b_iso, exp_arg_limit, truncate_exp_arg) if (b_cart is not None): u_cart = adptbx.b_as_u(b_cart) if (u_cart is not None): u_star = adptbx.u_cart_as_u_star(self, u_cart) if (u_cif is not None): u_star = adptbx.u_cif_as_u_star(self, u_cif) assert u_star is not None return adptbx.debye_waller_factor_u_star( h, u_star, exp_arg_limit, truncate_exp_arg)
def _append_to_model(self): mean_b = flex.mean(self.model.get_hierarchy().atoms().extract_b()) self.ma.add(" new water B factors will be set to mean B: %8.3f" % mean_b) sp = crystal.special_position_settings(self.model.crystal_symmetry()) scatterers = flex.xray_scatterer() for site_frac in self.sites_frac_water: sc = xray.scatterer(label="O", site=site_frac, u=adptbx.b_as_u(mean_b), occupancy=1.0) scatterers.append(sc) xrs_water = xray.structure(sp, scatterers) # chain_ids_taken = [] for chain in self.model.get_hierarchy().chains(): chain_ids_taken.append(chain.id) unique_taken = list(set(chain_ids_taken)) if (len(unique_taken) == 1): solvent_chain = unique_taken[0] else: for solvent_chain in all_chain_ids(): if (not solvent_chain in chain_ids_taken): break self.ma.add(" new water will have chain ID: '%s'" % solvent_chain) # self.model.add_solvent(solvent_xray_structure=xrs_water, atom_name="O", residue_name="HOH", chain_id=solvent_chain, refine_adp="isotropic")
def get_sf(k_sol, b_sol, b_cart, xrs, miller_set=None, d_min=None, twin_law=None, sfg_params=None): random.seed(0) flex.set_random_seed(0) if (miller_set is None): assert d_min is not None f_dummy = abs( xrs.structure_factors(d_min=d_min, anomalous_flag=False).f_calc()) else: f_dummy = miller_set assert d_min is None r_free_flags = f_dummy.generate_r_free_flags(fraction=0.1) fmodel = mmtbx.f_model.manager(r_free_flags=r_free_flags, f_obs=f_dummy, sf_and_grads_accuracy_params=sfg_params, xray_structure=xrs, twin_law=twin_law) ss = 1. / flex.pow2(r_free_flags.d_spacings().data()) / 4. k_mask = mmtbx.f_model.ext.k_mask(ss, k_sol, b_sol) u_star = adptbx.u_cart_as_u_star(xrs.unit_cell(), adptbx.b_as_u(b_cart)) k_anisotropic = mmtbx.f_model.ext.k_anisotropic(r_free_flags.indices(), u_star) fmodel.update_xray_structure(xray_structure=xrs, update_f_calc=True, update_f_mask=True) fmodel.update_core(k_mask=k_mask, k_anisotropic=k_anisotropic) f_obs = abs(fmodel.f_model()) return f_obs, r_free_flags
def print_atom_statisitics(self, selection_string='name CA', selection_bool_array=None, model=None): if model == None: model = self.ensemble_obj.model pdb_hierarchy = model.get_hierarchy() pdb_atoms = pdb_hierarchy.atoms() if selection_bool_array == None: selection_bool_array = pdb_hierarchy.atom_selection_cache( ).selection(selection_string) xrs = model.get_xray_structure().deep_copy_scatterers() xrs.convert_to_isotropic() b_iso_atoms = xrs.scatterers().extract_u_iso() / adptbx.b_as_u(1) q_atoms = xrs.scatterers().extract_occupancies() for i_seq, x in enumerate(selection_bool_array): if x: atom_info = pdb_atoms[i_seq].fetch_labels() if self.ensemble_obj.er_data.ke_protein_running == None: print(' {0:6} {1:6} {2:6} {3:6} {4:6d} {5:6.3f} {6:6.3f} '. format(atom_info.name, atom_info.resseq, atom_info.resname, atom_info.chain_id, i_seq, b_iso_atoms[i_seq], q_atoms[i_seq]), file=self.ensemble_obj.log) else: print( ' {0:6} {1:6} {2:6} {3:6} {4:6d} {5:6.3f} {6:6.3f} {7:6.3f}' .format( atom_info.name, atom_info.resseq, atom_info.resname, atom_info.chain_id, i_seq, b_iso_atoms[i_seq], q_atoms[i_seq], self. ensemble_obj.er_data.ke_protein_running[i_seq]), file=self.ensemble_obj.log)
def exercise_05_k_sol_b_sol_only(d_min=2.0): xray_structure = get_xray_structure_from_file() k_sol = 0.33 b_sol = 34.0 b_cart = [1, 2, 3, 0, 4, 0] f_obs, r_free_flags = get_f_obs_freer(d_min=d_min, k_sol=k_sol, b_sol=b_sol, b_cart=b_cart, xray_structure=xray_structure) fmodel = mmtbx.f_model.manager(r_free_flags=r_free_flags, f_obs=f_obs, xray_structure=xray_structure) params = bss.master_params.extract() params.anisotropic_scaling = False params.number_of_macro_cycles = 5 u_star = adptbx.u_cart_as_u_star(fmodel.f_obs().unit_cell(), adptbx.b_as_u(b_cart)) fmodel_kbu = mmtbx.f_model.manager_kbu(f_obs=fmodel.f_obs(), f_calc=fmodel.f_calc(), f_masks=fmodel.arrays.core.f_masks, f_part1=fmodel.arrays.core.f_part1, f_part2=fmodel.arrays.core.f_part2, ss=fmodel.ss, u_star=u_star) r_work_start = fmodel_kbu.r_factor() result = bss.bulk_solvent_and_scales(fmodel_kbu=fmodel_kbu, params=params) r_work = result.fmodel_kbu.r_factor() * 100. assert r_work_start > 0.05 # assert approx_equal(r_work, 0.0, eps=1.e-4) assert approx_equal(result.k_sols()[0], k_sol, eps=1.e-4) assert approx_equal(result.b_sols()[0], b_sol, eps=1.e-4) assert approx_equal(result.b_cart(), b_cart, eps=1.e-4)
def run_00(): time_aniso_u_scaler = 0 for symbol in sgtbx.bravais_types.acentric + sgtbx.bravais_types.centric: #print symbol, "-"*50 space_group_info = sgtbx.space_group_info(symbol=symbol) xrs = random_structure.xray_structure( space_group_info=space_group_info, elements=["N"] * 100, volume_per_atom=50.0, random_u_iso=True) # XXX ad a method to adptbx to do this point_group = sgtbx.space_group_info( symbol=symbol).group().build_derived_point_group() adp_constraints = sgtbx.tensor_rank_2_constraints( space_group=point_group, reciprocal_space=True) u_star = adptbx.u_cart_as_u_star( xrs.unit_cell(), adptbx.random_u_cart(u_scale=1, u_min=0.1)) u_indep = adp_constraints.independent_params(all_params=u_star) u_star = adp_constraints.all_params(independent_params=u_indep) b_cart_start = adptbx.u_as_b( adptbx.u_star_as_u_cart(xrs.unit_cell(), u_star)) # tr = (b_cart_start[0] + b_cart_start[1] + b_cart_start[2]) / 3 b_cart_start = [ b_cart_start[0] - tr, b_cart_start[1] - tr, b_cart_start[2] - tr, b_cart_start[3], b_cart_start[4], b_cart_start[5] ] tr = (b_cart_start[0] + b_cart_start[1] + b_cart_start[2]) / 3 # #print "Input b_cart :", " ".join(["%8.4f"%i for i in b_cart_start]), "tr:", tr F = xrs.structure_factors(d_min=2.0).f_calc() u_star = adptbx.u_cart_as_u_star(F.unit_cell(), adptbx.b_as_u(b_cart_start)) fbc = mmtbx.f_model.ext.k_anisotropic(F.indices(), u_star) fc = F.structure_factors_from_scatterers(xray_structure=xrs).f_calc() f_obs = F.customized_copy(data=flex.abs(fc.data() * fbc)) t0 = time.time() # obj = bulk_solvent.aniso_u_scaler( f_model_abs=flex.abs(fc.data()), f_obs=f_obs.data(), miller_indices=f_obs.indices(), adp_constraint_matrix=adp_constraints.gradient_sum_matrix()) time_aniso_u_scaler += (time.time() - t0) b_cart_final = adptbx.u_as_b( adptbx.u_star_as_u_cart( f_obs.unit_cell(), adp_constraints.all_params(tuple(obj.u_star_independent)))) # obj = bulk_solvent.aniso_u_scaler(f_model_abs=flex.abs(fc.data()), f_obs=f_obs.data(), miller_indices=f_obs.indices()) b_cart_final2 = adptbx.u_as_b( adptbx.u_star_as_u_cart(f_obs.unit_cell(), tuple(obj.u_star))) # assert approx_equal(b_cart_final, b_cart_final2) #print "Output b_cart:", " ".join(["%8.4f"%i for i in b_cart_final]) assert approx_equal(b_cart_start, b_cart_final, 1.e-4) print("Time (aniso_u_scaler only): %6.4f" % time_aniso_u_scaler)
def _ml_normalisation(intensities, aniso): # estimate number of residues per unit cell mr = matthews.matthews_rupp(intensities.crystal_symmetry()) n_residues = mr.n_residues # estimate B-factor and scale factors for normalisation if aniso: normalisation = absolute_scaling.ml_aniso_absolute_scaling( intensities, n_residues=n_residues ) u_star = normalisation.u_star else: normalisation = absolute_scaling.ml_iso_absolute_scaling( intensities, n_residues=n_residues ) u_star = adptbx.b_as_u( adptbx.u_iso_as_u_star(intensities.unit_cell(), normalisation.b_wilson) ) # record output in log file if aniso: b_cart = normalisation.b_cart logger.info("ML estimate of overall B_cart value:") logger.info( """\ %5.2f, %5.2f, %5.2f %12.2f, %5.2f %19.2f""" % (b_cart[0], b_cart[3], b_cart[4], b_cart[1], b_cart[5], b_cart[2]) ) else: logger.info("ML estimate of overall B value:") logger.info(" %5.2f A**2" % normalisation.b_wilson) logger.info("ML estimate of -log of scale factor:") logger.info(" %5.2f" % (normalisation.p_scale)) s = StringIO() mr.show(out=s) normalisation.show(out=s) logger.debug(s.getvalue()) # apply scales return intensities.customized_copy( data=scaling.ml_normalise_aniso( intensities.indices(), intensities.data(), normalisation.p_scale, intensities.unit_cell(), u_star, ), sigmas=scaling.ml_normalise_aniso( intensities.indices(), intensities.sigmas(), normalisation.p_scale, intensities.unit_cell(), u_star, ), )
def __init__(self, label="", site=(0, 0, 0), u=None, occupancy=1, scattering_type=None, fp=0, fdp=0, b=None): assert u is None or b is None if b is not None: u = adptbx.b_as_u(b) elif u is None: u = 0 if scattering_type is None: scattering_type = eltbx.xray_scattering.get_standard_label(label=label, exact=False) ext.scatterer.__init__(self, label, site, u, occupancy, scattering_type, fp, fdp)
def exercise(space_group_info, anomalous_flag, d_min=1.0, reflections_per_bin=200, n_bins=10, verbose=0): elements = ("N", "C", "C", "O") * 5 structure_factors = random_structure.xray_structure( space_group_info, elements=elements, volume_per_atom=50., min_distance=1.5, general_positions_only=True, use_u_aniso=False, u_iso=adptbx.b_as_u(10) ).structure_factors( anomalous_flag=anomalous_flag, d_min=d_min, algorithm="direct") if (0 or verbose): structure_factors.xray_structure().show_summary() asu_contents = dicts.with_default_value(0) for elem in elements: asu_contents[elem] += 1 f_calc = abs(structure_factors.f_calc()) f_calc.setup_binner( auto_binning=True, reflections_per_bin=reflections_per_bin, n_bins=n_bins) if (0 or verbose): f_calc.binner().show_summary() for k_given in [1,0.1,0.01,10,100]: f_obs = miller.array( miller_set=f_calc, data=f_calc.data()*k_given).set_observation_type_xray_amplitude() f_obs.use_binner_of(f_calc) wp = statistics.wilson_plot(f_obs, asu_contents, e_statistics=True) if (0 or verbose): print "wilson_k, wilson_b:", wp.wilson_k, wp.wilson_b print "space group:", space_group_info.group().type().hall_symbol() print "<E^2-1>:", wp.mean_e_sq_minus_1 assert 0.8 < wp.wilson_k/k_given < 1.2 assert 0.64 < wp.wilson_intensity_scale_factor/(k_given*k_given) < 1.44 assert 9 < wp.wilson_b < 11 assert wp.xy_plot_info().fit_correlation == wp.fit_correlation if space_group_info.group().is_centric(): assert 0.90 < wp.mean_e_sq_minus_1 < 1.16 assert 3.15 < wp.percent_e_sq_gt_2 < 6.5 else: assert 0.65 < wp.mean_e_sq_minus_1 < 0.90 assert 1.0 < wp.percent_e_sq_gt_2 < 3.15 assert wp.normalised_f_obs.size() == f_obs.size() f_obs = f_calc.array(data=flex.double(f_calc.indices().size(), 0)) f_obs.use_binner_of(f_calc) n_bins = f_obs.binner().n_bins_used() try: statistics.wilson_plot(f_obs, asu_contents) except RuntimeError, e: assert not show_diff(str(e), """\ wilson_plot error: %d empty bins: Number of bins: %d Number of f_obs > 0: 0 Number of f_obs <= 0: %d""" % (n_bins, n_bins, f_obs.indices().size()))
def run_00(): time_aniso_u_scaler = 0 for symbol in sgtbx.bravais_types.acentric + sgtbx.bravais_types.centric: #print symbol, "-"*50 space_group_info = sgtbx.space_group_info(symbol = symbol) xrs = random_structure.xray_structure( space_group_info = space_group_info, elements = ["N"]*100, volume_per_atom = 50.0, random_u_iso = True) # XXX ad a method to adptbx to do this point_group = sgtbx.space_group_info( symbol=symbol).group().build_derived_point_group() adp_constraints = sgtbx.tensor_rank_2_constraints( space_group=point_group, reciprocal_space=True) u_star = adptbx.u_cart_as_u_star(xrs.unit_cell(), adptbx.random_u_cart(u_scale=1,u_min=0.1)) u_indep = adp_constraints.independent_params(all_params=u_star) u_star = adp_constraints.all_params(independent_params=u_indep) b_cart_start=adptbx.u_as_b(adptbx.u_star_as_u_cart(xrs.unit_cell(), u_star)) # tr = (b_cart_start[0]+b_cart_start[1]+b_cart_start[2])/3 b_cart_start = [b_cart_start[0]-tr,b_cart_start[1]-tr,b_cart_start[2]-tr, b_cart_start[3],b_cart_start[4],b_cart_start[5]] tr = (b_cart_start[0]+b_cart_start[1]+b_cart_start[2])/3 # #print "Input b_cart :", " ".join(["%8.4f"%i for i in b_cart_start]), "tr:", tr F = xrs.structure_factors(d_min = 2.0).f_calc() u_star = adptbx.u_cart_as_u_star( F.unit_cell(), adptbx.b_as_u(b_cart_start)) fbc = mmtbx.f_model.ext.k_anisotropic(F.indices(), u_star) fc = F.structure_factors_from_scatterers(xray_structure=xrs).f_calc() f_obs = F.customized_copy(data = flex.abs(fc.data()*fbc)) t0 = time.time() # obj = bulk_solvent.aniso_u_scaler( f_model_abs = flex.abs(fc.data()), f_obs = f_obs.data(), miller_indices = f_obs.indices(), adp_constraint_matrix = adp_constraints.gradient_sum_matrix()) time_aniso_u_scaler += (time.time()-t0) b_cart_final = adptbx.u_as_b(adptbx.u_star_as_u_cart(f_obs.unit_cell(), adp_constraints.all_params(tuple(obj.u_star_independent)))) # obj = bulk_solvent.aniso_u_scaler( f_model_abs = flex.abs(fc.data()), f_obs = f_obs.data(), miller_indices = f_obs.indices()) b_cart_final2 = adptbx.u_as_b(adptbx.u_star_as_u_cart(f_obs.unit_cell(), tuple(obj.u_star))) # assert approx_equal(b_cart_final, b_cart_final2) #print "Output b_cart:", " ".join(["%8.4f"%i for i in b_cart_final]) assert approx_equal(b_cart_start, b_cart_final, 1.e-4) print "Time (aniso_u_scaler only): %6.4f"%time_aniso_u_scaler
def run_group(symbol): group = space_group_info(symbol); print "\n==" elements = ('C', 'N', 'O', 'H')*11 xrs = random_structure.xray_structure( space_group_info = group, volume_per_atom = 25., general_positions_only = False, elements = elements, min_distance = 1.0) fo = abs(xrs.structure_factors(d_min=2).f_calc()) fmodel = mmtbx.f_model.manager( f_obs = fo, xray_structure = xrs) # k_sol=flex.double([10.35,5.34]) b_sol=flex.double([30.0, 24.0]) b_cart = [10,20,30,40,50,60] u_star = flex.double(adptbx.b_as_u( adptbx.u_cart_as_u_star(xrs.unit_cell(), b_cart))) # TGO = cpp_tg(fmodel=fmodel) tg = TGO.get_tg(k_sol=k_sol, b_sol=b_sol, u_star=u_star) # k_sol gk_a=list(tg.grad_k_sols()) ck_a=list(tg.curv_k_sols()) gk_fd, ck_fd=fd(TGO=TGO, k_sol=k_sol, b_sol=b_sol, u_star=u_star, param="k_sol") # b_sol gb_a=list(tg.grad_b_sols()) cb_a=list(tg.curv_b_sols()) gb_fd, cb_fd=fd(TGO=TGO, k_sol=k_sol, b_sol=b_sol, u_star=u_star, param="b_sol") # u_star gu_a=list(tg.grad_u_star()) gu_fd, junk=fd(TGO=TGO, k_sol=k_sol, b_sol=b_sol, u_star=u_star, param="u_star") print "u_star:",gu_a print "u_star:",gu_fd TGO2 = cpp_tg_u_star_only(fmodel=fmodel) tg2 = TGO2.get_tg(k_sol=k_sol, b_sol=b_sol, u_star=u_star) gu_a2=list(tg2.grad_u_star()) gu_fd2, junk=fd(TGO=TGO2, k_sol=k_sol, b_sol=b_sol, u_star=u_star, param="u_star") print "u_star:",gu_a2 print "u_star:",gu_fd2 # print "k_sol:", gk_a, ck_a print "k_sol:", gk_fd, ck_fd print "b_sol:", gb_a, cb_a print "b_sol:", gb_fd, cb_fd # assert approx_equal(gk_a, gk_fd, eps=1.e-4) assert approx_equal(gb_a, gb_fd, eps=1.e-4) assert approx_equal(ck_a, ck_fd, eps=1.e-4) assert approx_equal(cb_a, cb_fd, eps=1.e-4) assert approx_equal(gu_a, gu_fd, eps=1.e-4) assert approx_equal(gu_a2, gu_fd2, eps=1.e-6)
def dummy_structure(space_group_info, volume, n_scatterers): structure = xray.structure( special_position_settings=crystal.special_position_settings( crystal_symmetry=crystal.symmetry( unit_cell=space_group_info.any_compatible_unit_cell(volume=volume), space_group_info=space_group_info))) b_iso = 20 u_iso = adptbx.b_as_u(b_iso) u_star = adptbx.u_iso_as_u_star(structure.unit_cell(), u_iso) scatterer = xray.scatterer(label="C", site=(0.123,0.234,0.345), u=u_star) for i in xrange(n_scatterers): structure.add_scatterer(scatterer) return structure
def add_new_solvent(self): if (self.params.b_iso is None): sol_sel = self.model.solvent_selection() xrs_mac_h = self.model.get_xray_structure().select(~sol_sel) hd_mac = self.model.get_hd_selection().select(~sol_sel) xrs_mac = xrs_mac_h.select(~hd_mac) b = xrs_mac.extract_u_iso_or_u_equiv() * math.pi**2 * 8 b_solv = flex.mean_default(b, None) if (b_solv is not None and b_solv < self.params.b_iso_min or b_solv > self.params.b_iso_max): b_solv = (self.params.b_iso_min + self.params.b_iso_max) / 2. else: b_solv = self.params.b_iso if (self.params.new_solvent == "isotropic"): new_scatterers = flex.xray_scatterer( self.sites.size(), xray.scatterer(occupancy=self.params.occupancy, b=b_solv, scattering_type=self.params.scattering_type)) elif (self.params.new_solvent == "anisotropic"): u_star = adptbx.u_iso_as_u_star( self.model.get_xray_structure().unit_cell(), adptbx.b_as_u(b_solv)) new_scatterers = flex.xray_scatterer( self.sites.size(), xray.scatterer(occupancy=self.params.occupancy, u=u_star, scattering_type=self.params.scattering_type)) else: raise RuntimeError new_scatterers.set_sites(self.sites) solvent_xray_structure = xray.structure( special_position_settings=self.model.get_xray_structure(), scatterers=new_scatterers) xrs_sol = self.model.get_xray_structure().select( self.model.solvent_selection()) xrs_mac = self.model.get_xray_structure().select( ~self.model.solvent_selection()) xrs_sol = xrs_sol.concatenate(other=solvent_xray_structure) sol_sel = flex.bool(xrs_mac.scatterers().size(), False) sol_sel.extend(flex.bool(xrs_sol.scatterers().size(), True)) self.model.add_solvent( solvent_xray_structure=solvent_xray_structure, residue_name=self.params.output_residue_name, atom_name=self.params.output_atom_name, chain_id=self.params.output_chain_id, refine_occupancies=self.params.refine_occupancies, refine_adp=self.params.new_solvent) self.fmodel.update_xray_structure( xray_structure=self.model.get_xray_structure(), update_f_calc=True)
def as_xray_scatterer(self, unit_cell=None): scattering_type = eltbx.xray_scattering.get_standard_label( label=self.type, exact=False, optional=True) if (scattering_type is None): scattering_type = eltbx.xray_scattering.get_standard_label( label=self.segid, exact=False, optional=True) if (scattering_type is None): scattering_type = "unknown" site = (self.x, self.y, self.z) if (unit_cell is not None): site = unit_cell.fractionalize(site) return xray.scatterer(label="_".join((self.segid, self.type)), site=site, u=adptbx.b_as_u(self.b), occupancy=self.q, scattering_type=scattering_type)
def customized_copy( self, label=None, site=None, u=None, b=None, occupancy=None, scattering_type=None, fp=None, fdp=None, flags=None, u_iso=None, u_star=None, ): assert u is None or b is None if b is not None: u = adptbx.b_as_u(b) del b if u is not None: assert u_iso is None and u_star is None if flags is None: assert self.flags.use_u_iso() ^ self.flags.use_u_aniso() else: assert flags.use_u_iso() ^ flags.use_u_aniso() result = ext.scatterer(other=self) if label is not None: result.label = label if site is not None: result.site = site if flags is not None: result.flags = flags if u is not None: if result.flags.use_u_iso(): result.u_iso = u else: result.u_star = u if u_iso is not None: result.u_iso = u_iso if u_star is not None: result.u_star = u_star if occupancy is not None: result.occupancy = occupancy if scattering_type is not None: result.scattering_type = scattering_type if fp is not None: result.fp = fp if fdp is not None: result.fdp = fdp return result
def as_xray_scatterer(self, unit_cell=None): scattering_type = eltbx.xray_scattering.get_standard_label( label=self.type, exact=False, optional=True) if (scattering_type is None): scattering_type = eltbx.xray_scattering.get_standard_label( label=self.segid, exact=False, optional=True) if (scattering_type is None): scattering_type = "unknown" site = (self.x, self.y, self.z) if (unit_cell is not None): site = unit_cell.fractionalize(site) return xray.scatterer( label="_".join((self.segid, self.type)), site=site, u=adptbx.b_as_u(self.b), occupancy=self.q, scattering_type=scattering_type)
def exercise_cumulative_intensity_distribution(space_group_info, anomalous_flag, d_min=1.0, reflections_per_bin=200, n_bins=10, verbose=0): elements = ("N", "C", "C", "O") * 5 structure_factors = random_structure.xray_structure( space_group_info, elements=elements, volume_per_atom=50., min_distance=1.5, general_positions_only=True, use_u_aniso=False, u_iso=adptbx.b_as_u(10)).structure_factors( anomalous_flag=anomalous_flag, d_min=d_min, algorithm="direct") if (0 or verbose): structure_factors.xray_structure().show_summary() for n_bins in [10, 20]: f_calc = abs(structure_factors.f_calc()) f_calc.setup_binner(auto_binning=True, reflections_per_bin=reflections_per_bin, n_bins=n_bins) f_obs = miller.array( miller_set=f_calc, data=f_calc.data()).set_observation_type_xray_amplitude() f_obs.use_binner_of(f_calc) f_obs_sq = f_obs.f_as_f_sq() f_obs_sq.use_binner_of(f_obs) for ma in (f_obs, f_obs_sq): if ma.is_xray_amplitude_array(): dist = statistics.cumulative_intensity_distribution(f_obs=ma) else: dist = statistics.cumulative_intensity_distribution( f_obs_sq=ma) n_bins_used = f_obs.binner().n_bins_used() x_index = {} x_index[0.1] = n_bins_used // 10 x_index[0.5] = n_bins_used // 2 x_index[0.9] = 9 * n_bins_used // 10 if space_group_info.group().is_centric(): assert 0.23 < dist.y[x_index[0.1]] < 0.32 assert 0.49 < dist.y[x_index[0.5]] < 0.57 assert 0.65 < dist.y[x_index[0.9]] < 0.70 else: assert 0.08 < dist.y[x_index[0.1]] < 0.21 assert 0.37 < dist.y[x_index[0.5]] < 0.49 assert 0.57 < dist.y[x_index[0.9]] < 0.65
def add_new_solvent(self): if(self.params.b_iso is None): sol_sel = self.model.solvent_selection() xrs_mac_h = self.model.xray_structure.select(~sol_sel) hd_mac = self.model.xray_structure.hd_selection().select(~sol_sel) xrs_mac = xrs_mac_h.select(~hd_mac) b = xrs_mac.extract_u_iso_or_u_equiv() * math.pi**2*8 b_solv = flex.mean_default(b, None) if(b_solv is not None and b_solv < self.params.b_iso_min or b_solv > self.params.b_iso_max): b_solv = (self.params.b_iso_min + self.params.b_iso_max) / 2. else: b_solv = self.params.b_iso if(self.params.new_solvent == "isotropic"): new_scatterers = flex.xray_scatterer( self.sites.size(), xray.scatterer(occupancy = self.params.occupancy, b = b_solv, scattering_type = self.params.scattering_type)) elif(self.params.new_solvent == "anisotropic"): u_star = adptbx.u_iso_as_u_star(self.model.xray_structure.unit_cell(), adptbx.b_as_u(b_solv)) new_scatterers = flex.xray_scatterer( self.sites.size(), xray.scatterer( occupancy = self.params.occupancy, u = u_star, scattering_type = self.params.scattering_type)) else: raise RuntimeError new_scatterers.set_sites(self.sites) solvent_xray_structure = xray.structure( special_position_settings = self.model.xray_structure, scatterers = new_scatterers) xrs_sol = self.model.xray_structure.select(self.model.solvent_selection()) xrs_mac = self.model.xray_structure.select(~self.model.solvent_selection()) xrs_sol = xrs_sol.concatenate(other = solvent_xray_structure) sol_sel = flex.bool(xrs_mac.scatterers().size(), False) sol_sel.extend( flex.bool(xrs_sol.scatterers().size(), True) ) self.model.add_solvent( solvent_xray_structure = solvent_xray_structure, residue_name = self.params.output_residue_name, atom_name = self.params.output_atom_name, chain_id = self.params.output_chain_id, refine_occupancies = self.params.refine_occupancies, refine_adp = self.params.new_solvent) self.fmodel.update_xray_structure( xray_structure = self.model.xray_structure, update_f_calc = True)
def __init__(self, label="", site=(0, 0, 0), u=None, occupancy=1, scattering_type=None, fp=0, fdp=0, b=None): assert u is None or b is None if (b is not None): u = adptbx.b_as_u(b) elif (u is None): u = 0 if (scattering_type is None): scattering_type = eltbx.xray_scattering.get_standard_label( label=label, exact=False) ext.scatterer.__init__(self, label, site, u, occupancy, scattering_type, fp, fdp)
def exercise_cumulative_intensity_distribution(space_group_info, anomalous_flag, d_min=1.0, reflections_per_bin=200, n_bins=10, verbose=0): elements = ("N", "C", "C", "O") * 5 structure_factors = random_structure.xray_structure( space_group_info, elements=elements, volume_per_atom=50., min_distance=1.5, general_positions_only=True, use_u_aniso=False, u_iso=adptbx.b_as_u(10) ).structure_factors( anomalous_flag=anomalous_flag, d_min=d_min, algorithm="direct") if (0 or verbose): structure_factors.xray_structure().show_summary() for n_bins in [10,20]: f_calc = abs(structure_factors.f_calc()) f_calc.setup_binner( auto_binning=True, reflections_per_bin=reflections_per_bin, n_bins=n_bins) f_obs = miller.array( miller_set=f_calc, data=f_calc.data()).set_observation_type_xray_amplitude() f_obs.use_binner_of(f_calc) f_obs_sq = f_obs.f_as_f_sq() f_obs_sq.use_binner_of(f_obs) for ma in (f_obs,f_obs_sq): if ma.is_xray_amplitude_array(): dist = statistics.cumulative_intensity_distribution(f_obs=ma) else: dist = statistics.cumulative_intensity_distribution(f_obs_sq=ma) n_bins_used = f_obs.binner().n_bins_used() x_index = {} x_index[0.1] = n_bins_used//10 x_index[0.5] = n_bins_used//2 x_index[0.9] = 9*n_bins_used//10 if space_group_info.group().is_centric(): assert 0.23 < dist.y[x_index[0.1]] < 0.32 assert 0.49 < dist.y[x_index[0.5]] < 0.57 assert 0.65 < dist.y[x_index[0.9]] < 0.70 else: assert 0.08 < dist.y[x_index[0.1]] < 0.21 assert 0.37 < dist.y[x_index[0.5]] < 0.49 assert 0.57 < dist.y[x_index[0.9]] < 0.65
def run(refine_xyz=True, refine_adp=False): # get xray_structure from PDB file inp = get_inputs(pdb_str=pdb_str) if (1): inp.pdb_hierarchy.adopt_xray_structure(inp.xray_structure) inp.pdb_hierarchy.write_pdb_file(file_name="start.pdb") # simulate poor starting model: shake coordinates and B-factors xrs_poor = inp.xray_structure.deep_copy_scatterers() if (refine_xyz): xrs_poor.shake_sites_in_place(mean_distance=0.3) if (refine_adp): for sc in xrs_poor.scatterers(): sc.u_iso = adptbx.b_as_u(random.choice([7, 8, 9, 11, 12, 13])) if (1): inp.pdb_hierarchy.adopt_xray_structure(xrs_poor) inp.pdb_hierarchy.write_pdb_file(file_name="poor.pdb") # simulate Fobs f_obs = abs( inp.xray_structure.structure_factors(d_min=1.0, algorithm="direct").f_calc()) r_free_flags = f_obs.generate_r_free_flags() # get fmodel params = mmtbx.f_model.sf_and_grads_accuracy_master_params.extract() params.algorithm = "direct" fmodel = mmtbx.f_model.manager(f_obs=f_obs, r_free_flags=r_free_flags, xray_structure=xrs_poor, sf_and_grads_accuracy_params=params, target_name="ls_wunit_kunit") # refinement loop print "start r_factor: %6.4f" % fmodel.r_work() for macro_cycle in xrange(100): # refine coordinates if (refine_xyz): minimized = minimizer(fmodel=fmodel, sites=True) print " macro_cycle %3d (sites) r_factor: %6.4f" % ( macro_cycle, fmodel.r_work()) # refine ADPs if (refine_adp): minimized = minimizer(fmodel=fmodel, u_iso=True) print " macro_cycle %3d (adp) r_factor: %6.4f" % ( macro_cycle, fmodel.r_work()) if (1): inp.pdb_hierarchy.adopt_xray_structure(fmodel.xray_structure) inp.pdb_hierarchy.write_pdb_file(file_name="refined.pdb")
def anisotropic_correction(self): if not self.params.anisotropic_correction: return self.aniso_scale_and_b = None n_copies_solc = self.params.asu_contents.n_copies_per_asu n_residues = self.params.asu_contents.n_residues n_bases = self.params.asu_contents.n_bases self.aniso_scale_and_b = absolute_scaling.ml_aniso_absolute_scaling( miller_array=self.f_obs, n_residues=n_residues*self.f_obs.space_group().order_z()*n_copies_solc, n_bases=n_bases*self.f_obs.space_group().order_z()*n_copies_solc) self.aniso_scale_and_b.show(out=self.log) b_cart = self.aniso_scale_and_b.b_cart trace = sum(b_cart[:3])/3 b_cart = [b_cart[0]-trace, b_cart[1]-trace, b_cart[2]-trace, b_cart[3], b_cart[4], b_cart[5]] u_star = adptbx.u_cart_as_u_star(self.f_obs.unit_cell(), adptbx.b_as_u(b_cart)) self.f_obs = absolute_scaling.anisotropic_correction( self.f_obs, 0.0, u_star).set_observation_type(self.f_obs)
def exercise_packed(structure_ideal, f_obs, anomalous_flag, verbose=0): sh, ls = shift_all(structure_ideal, f_obs, anomalous_flag) flag = (random.random() > 0.5) gradient_flags = randomize_gradient_flags( xray.structure_factors.gradient_flags(site=flag, u=not flag), f_obs.anomalous_flag(), thresholds=(1/2.,0)) u_iso_refinable_params = flex.double() for scatterer in sh.structure_shifted.scatterers(): scatterer.flags.set_grad_site(gradient_flags.site) scatterer.flags.set_grad_u_iso(gradient_flags.u_iso) scatterer.flags.set_grad_u_aniso(gradient_flags.u_aniso) scatterer.flags.set_grad_occupancy(gradient_flags.occupancy) scatterer.flags.set_grad_fp(gradient_flags.fp) scatterer.flags.set_grad_fdp(gradient_flags.fdp) scatterer.flags.set_tan_u_iso(True) param = random.randint(90,120) scatterer.flags.param= param value = math.tan(scatterer.u_iso*math.pi/adptbx.b_as_u(param)-math.pi/2) u_iso_refinable_params.append(value) n_parameters = xray.scatterer_grad_flags_counts( sh.structure_shifted.scatterers()).n_parameters() assert n_parameters == sh.structure_shifted.n_parameters() if (n_parameters > 0): sfd = xray.structure_factors.gradients_direct( xray_structure=sh.structure_shifted, u_iso_refinable_params=u_iso_refinable_params, miller_set=f_obs, d_target_d_f_calc=ls.derivatives(), n_parameters=n_parameters) assert sfd.packed().size() == n_parameters re = resampling(miller_set=f_obs) map0 = re( xray_structure=sh.structure_shifted, u_iso_refinable_params=u_iso_refinable_params, dp=miller.array(miller_set=f_obs, data=ls.derivatives()), n_parameters=n_parameters, verbose=verbose) assert map0.packed().size() == n_parameters correlation = flex.linear_correlation(sfd.packed(), map0.packed()) assert correlation.is_well_defined() assert correlation.coefficient() > 0.999
def run(): # get xray_structure from PDB file inp = get_inputs() if(1): inp.pdb_hierarchy.adopt_xray_structure(inp.xray_structure) inp.pdb_hierarchy.write_pdb_file(file_name="start.pdb") # simulate poor starting model xrs_poor = inp.xray_structure.deep_copy_scatterers() xrs_poor.shake_sites_in_place(mean_distance = 0.3) for sc in xrs_poor.scatterers(): # what is that for? why 7-13? # isn't adptbx.b_as_u(_) retruns 1.0? sc.u_iso = adptbx.b_as_u(random.choice([7,8,9,11,12,13])) if(1): inp.pdb_hierarchy.adopt_xray_structure(xrs_poor) inp.pdb_hierarchy.write_pdb_file(file_name="poor.pdb") # simulate Fobs f_obs = abs(inp.xray_structure.structure_factors( d_min = 1.0, algorithm="direct").f_calc()) r_free_flags = f_obs.generate_r_free_flags() # get fmodel params = mmtbx.f_model.sf_and_grads_accuracy_master_params.extract() params.algorithm = "direct" fmodel = mmtbx.f_model.manager( f_obs = f_obs, r_free_flags = r_free_flags, xray_structure = xrs_poor, sf_and_grads_accuracy_params = params, target_name = "ls_wunit_kunit") # refinement loop print "start r_factor: %6.4f" % fmodel.r_work() for macro_cycle in xrange(100): # refine coordinates minimized = minimizer(fmodel = fmodel, sites = True) print " macro_cycle %3d (sites) r_factor: %6.4f"%(macro_cycle, fmodel.r_work()) # refine ADPs (atomic displacement parameter) minimized = minimizer(fmodel = fmodel, u_iso = True) print " macro_cycle %3d (adp) r_factor: %6.4f"%(macro_cycle, fmodel.r_work()) if fmodel.r_work() < 0.03: break if(1): inp.pdb_hierarchy.adopt_xray_structure(fmodel.xray_structure) inp.pdb_hierarchy.write_pdb_file(file_name="refined.pdb")
def __init__(self, space_group_info, **kwds): libtbx.adopt_optional_init_args(self, kwds) self.space_group_info = space_group_info self.structure = random_structure.xray_structure( space_group_info, elements=self.elements, volume_per_atom=20., min_distance=1.5, general_positions_only=True, use_u_aniso=False, u_iso=adptbx.b_as_u(10), ) self.structure.set_inelastic_form_factors(1.54, "sasaki") self.scale_factor = 0.05 + 10 * flex.random_double() fc = self.structure.structure_factors(anomalous_flag=True, d_min=self.d_min, algorithm="direct").f_calc() fo = fc.as_amplitude_array() fo.set_observation_type_xray_amplitude() if self.use_students_t_errors: nu = random.uniform(1, 10) normal_g = variate(normal_distribution()) gamma_g = variate(gamma_distribution(0.5 * nu, 2)) errors = normal_g(fc.size()) / flex.sqrt(2 * gamma_g(fc.size())) else: # use gaussian errors g = variate(normal_distribution()) errors = g(fc.size()) fo2 = fo.as_intensity_array() self.fo2 = fo2.customized_copy( data=(fo2.data() + errors) * self.scale_factor, sigmas=flex.double(fc.size(), 1), ) self.fc = fc xs_i = self.structure.inverse_hand() self.fc_i = xs_i.structure_factors(anomalous_flag=True, d_min=self.d_min, algorithm="direct").f_calc() fo2_twin = self.fc.customized_copy( data=self.fc.data() + self.fc_i.data()).as_intensity_array() self.fo2_twin = fo2_twin.customized_copy( data=(errors + fo2_twin.data()) * self.scale_factor, sigmas=self.fo2.sigmas())
def run(d_min=2.0, radius_max=5, radius_step=0.01): for i, mxp in enumerate([0, 6]): o = maptbx.atom_curves(scattering_type="C", scattering_table="wk1995") b = o.bcr_approx( d_min=d_min, radius_max=radius_max, radius_step=radius_step, mxp=mxp, epsc=0.001, kpres=0 # BCR params ) r = rfactor(b.image_values, b.bcr_approx_values) if (i == 0): assert r > 10, r else: assert r < 0.4, r # # b_iso is not 0 # b_iso = 50 im = o.image(d_min=d_min, b_iso=b_iso, radius_max=radius_max, radius_step=radius_step) bcr_approx_values = flex.double() bcr_approx_values_cpp = flex.double() # c++ sc = xray.scatterer("c", site=(0, 0, 0), u=adptbx.b_as_u(b_iso)) bcr_cpp = ext.bcr_model(scatterer=sc, B=b.B, C=b.C, R=b.R) calc = ext.calculator(bcr_model=bcr_cpp) # for r in im.radii: first = 0 second = 0 for B, C, R in zip(b.B, b.C, b.R): if (abs(R) < 1.e-6): first += bcr.gauss(B=B, C=C, r=r, b_iso=b_iso) else: second += C * bcr.chi(B=B, R=R, r=r, b_iso=b_iso) bcr_approx_values.append(first + second) bcr_approx_values_cpp.append(calc.rho(r)) r = rfactor(im.image_values, bcr_approx_values) if (i == 0): assert r > 10, r else: assert r < 0.4, r assert approx_equal(bcr_approx_values, bcr_approx_values_cpp)
def __init__(self, space_group_info, **kwds): libtbx.adopt_optional_init_args(self, kwds) self.space_group_info = space_group_info self.structure = random_structure.xray_structure( space_group_info, elements=self.elements, volume_per_atom=20., min_distance=1.5, general_positions_only=True, use_u_aniso=False, u_iso=adptbx.b_as_u(10), ) self.structure.set_inelastic_form_factors(1.54, "sasaki") self.scale_factor = 0.05 + 10 * flex.random_double() fc = self.structure.structure_factors( anomalous_flag=True, d_min=self.d_min, algorithm="direct").f_calc() fo = fc.as_amplitude_array() fo.set_observation_type_xray_amplitude() if self.use_students_t_errors: nu = random.uniform(1, 10) normal_g = variate(normal_distribution()) gamma_g = variate(gamma_distribution(0.5*nu, 2)) errors = normal_g(fc.size())/flex.sqrt(2*gamma_g(fc.size())) else: # use gaussian errors g = variate(normal_distribution()) errors = g(fc.size()) fo2 = fo.as_intensity_array() self.fo2 = fo2.customized_copy( data=(fo2.data()+errors)*self.scale_factor, sigmas=flex.double(fc.size(), 1), ) self.fc = fc xs_i = self.structure.inverse_hand() self.fc_i = xs_i.structure_factors( anomalous_flag=True, d_min=self.d_min, algorithm="direct").f_calc() fo2_twin = self.fc.customized_copy( data=self.fc.data()+self.fc_i.data()).as_intensity_array() self.fo2_twin = fo2_twin.customized_copy( data=(errors + fo2_twin.data()) * self.scale_factor, sigmas=self.fo2.sigmas())
def adjust_stp(O, stp, csd, large_shift_site=0.1, large_shift_u_iso_factor=0.5, min_large_shift_u_iso=b_as_u(1)): print "adjust_stp", stp assert csd.size() == O.x.size() max_stp = 100 ix = 0 uc = O.xray_structure.unit_cell() sstab = O.xray_structure.site_symmetry_table() for i_sc, sc in enumerate(O.xray_structure.scatterers()): site_symmetry = sstab.get(i_sc) if (site_symmetry.is_point_group_1()): np = 3 for i, ucp in enumerate(uc.parameters()[:3]): lsx = large_shift_site / ucp \ * O.p_as_x[ix+i] if (abs(stp * csd[ix + i]) > lsx): max_stp = min(max_stp, abs(lsx / csd[ix])) else: constr = site_symmetry.site_constraints() np = constr.n_independent_params() raise RuntimeError( "SPECIAL POSITION LARGE SHIFT NOT IMPLEMENTED.") ix += np u_iso = O.x[ix] / O.p_as_x[ix] lux = max(min_large_shift_u_iso, abs(u_iso) * large_shift_u_iso_factor) \ * O.p_as_x[ix] if (abs(stp * csd[ix]) > lux): max_stp = min(max_stp, abs(lux / csd[ix])) ix += 1 assert ix == O.x.size() print "max_stp:", max_stp sys.stdout.flush() if (O.params.use_max_stp): return min(max_stp, stp) return stp
def apply_back_trace_of_overall_exp_scale_matrix(self, xray_structure=None): if (xray_structure is None): return None k_sol, b_sol, b_cart = self.k_sols(), self.b_sols(), self.b_cart() assert len(k_sol) == 1 # XXX Only one mask! k_sol = k_sol[0] b_sol = b_sol[0] # xrs = xray_structure if (xrs is None): return b_min = min(b_sol, xrs.min_u_cart_eigenvalue() * adptbx.u_as_b(1.)) if (b_min < 0): xrs.tidy_us() b_iso = (b_cart[0] + b_cart[1] + b_cart[2]) / 3.0 b_test = b_min + b_iso if (b_test < 0.0): b_adj = b_iso + abs(b_test) + 0.001 else: b_adj = b_iso b_cart_new = [ b_cart[0] - b_adj, b_cart[1] - b_adj, b_cart[2] - b_adj, b_cart[3], b_cart[4], b_cart[5] ] b_sol_new = b_sol + b_adj xrs.shift_us(b_shift=b_adj) b_min = min(b_sol_new, xrs.min_u_cart_eigenvalue() * adptbx.u_as_b(1.)) assert b_min >= 0.0 xrs.tidy_us() # assert self.fmodel_kbu k_masks = [ext.k_mask(self.fmodel_kbu.ss, k_sol, b_sol_new)] u_star = adptbx.u_cart_as_u_star(self.fmodel_kbu.f_obs.unit_cell(), adptbx.b_as_u(b_cart_new)) k_anisotropic = ext.k_anisotropic(self.fmodel_kbu.f_obs.indices(), u_star) self.fmodel_kbu = self.fmodel_kbu.update(b_sols=[b_sol_new], b_cart=b_cart_new) return group_args(xray_structure=xrs, b_adj=b_adj, b_sol=b_sol_new, b_cart=b_cart_new)
def customized_copy(self, label=None, site=None, u=None, b=None, occupancy=None, scattering_type=None, fp=None, fdp=None, flags=None, u_iso=None, u_star=None): assert u is None or b is None if (b is not None): u = adptbx.b_as_u(b) del b if (u is not None): assert u_iso is None and u_star is None if (flags is None): assert self.flags.use_u_iso() ^ self.flags.use_u_aniso() else: assert flags.use_u_iso() ^ flags.use_u_aniso() result = ext.scatterer(other=self) if (label is not None): result.label = label if (site is not None): result.site = site if (flags is not None): result.flags = flags if (u is not None): if (result.flags.use_u_iso()): result.u_iso = u else: result.u_star = u if (u_iso is not None): result.u_iso = u_iso if (u_star is not None): result.u_star = u_star if (occupancy is not None): result.occupancy = occupancy if (scattering_type is not None): result.scattering_type = scattering_type if (fp is not None): result.fp = fp if (fdp is not None): result.fdp = fdp return result
def split_u(xray_structure, tls_selections, offset): global time_split_u timer = user_plus_sys_time() uc = xray_structure.unit_cell() u_iso = xray_structure.scatterers().extract_u_iso() u_eq_1 = xray_structure.extract_u_iso_or_u_equiv() for tls_selection in tls_selections: u_iso_sel = u_iso.select(tls_selection) u_iso_min = flex.min(u_iso_sel) if (offset): offset_ = adptbx.b_as_u(5.0) else: offset_ = 0.0 if u_iso_min >= offset_: u_iso_min = u_iso_min - offset_ t = adptbx.u_iso_as_u_star(uc, u_iso_min) for i_seq in tls_selection: sc = xray_structure.scatterers()[i_seq] assert sc.u_iso == u_iso[i_seq] u_iso_new = sc.u_iso - u_iso_min assert u_iso_new >= 0.0 sc.u_iso = u_iso_new assert sc.flags.use_u_aniso() assert sc.flags.use_u_iso() if (sc.u_star == (-1.0, -1.0, -1.0, -1.0, -1.0, -1.0)): sc.u_star = t else: x = flex.double(sc.u_star) y = flex.double(t) z = list(x + y) sc.u_star = z u_iso = xray_structure.scatterers().extract_u_iso().select( xray_structure.use_u_iso()) assert (u_iso < 0.0).count(True) == 0 u_eq_2 = xray_structure.extract_u_iso_or_u_equiv() assert approx_equal(u_eq_1, u_eq_2) time_split_u += timer.elapsed()
def print_atom_statisitics(self, selection_string = 'name CA', selection_bool_array = None, model = None): if model == None: model = self.ensemble_obj.model pdb_hierarchy = model.pdb_hierarchy pdb_atoms = pdb_hierarchy.atoms() if selection_bool_array == None: selection_bool_array = pdb_hierarchy.atom_selection_cache().selection(selection_string) xrs = model.xray_structure.deep_copy_scatterers() xrs.convert_to_isotropic() b_iso_atoms = xrs.scatterers().extract_u_iso()/adptbx.b_as_u(1) q_atoms = xrs.scatterers().extract_occupancies() for i_seq, x in enumerate(selection_bool_array): if x: atom_info = pdb_atoms[i_seq].fetch_labels() if self.ensemble_obj.er_data.ke_protein_running == None: print >> self.ensemble_obj.log, ' {0:6} {1:6} {2:6} {3:6} {4:6d} {5:6.3f} {6:6.3f} '.format( atom_info.name, atom_info.resseq, atom_info.resname, atom_info.chain_id, i_seq, b_iso_atoms[i_seq], q_atoms[i_seq]) else: print >> self.ensemble_obj.log, ' {0:6} {1:6} {2:6} {3:6} {4:6d} {5:6.3f} {6:6.3f} {7:6.3f}'.format( atom_info.name, atom_info.resseq, atom_info.resname, atom_info.chain_id, i_seq, b_iso_atoms[i_seq], q_atoms[i_seq], self.ensemble_obj.er_data.ke_protein_running[i_seq])
def split_u(u_eq, xray_structure, tls_selections, offset): global time_split_u timer = user_plus_sys_time() uc = xray_structure.unit_cell() for tls_selection in tls_selections: if (offset): offset_ = adptbx.b_as_u(5.0) else: offset_ = 0.0 for i_seq in tls_selection: sc = xray_structure.scatterers()[i_seq] u_nm = sc.u_star u_nm_iso = adptbx.u_star_as_u_iso(uc, u_nm) u_iso_new = u_eq[i_seq] - u_nm_iso if u_iso_new < 0.0: u_iso_new = 0.0 assert u_iso_new >= 0.0 sc.u_iso = u_iso_new assert sc.flags.use_u_aniso() assert sc.flags.use_u_iso() u_iso = xray_structure.scatterers().extract_u_iso().select( xray_structure.use_u_iso()) assert (u_iso < 0.0).count(True) == 0 time_split_u += timer.elapsed()
def split_u(xray_structure, tls_selections, offset): global time_split_u timer = user_plus_sys_time() uc = xray_structure.unit_cell() u_iso = xray_structure.scatterers().extract_u_iso() u_eq_1 = xray_structure.extract_u_iso_or_u_equiv() for tls_selection in tls_selections: u_iso_sel = u_iso.select(tls_selection) u_iso_min = flex.min(u_iso_sel) if(offset): offset_ = adptbx.b_as_u(5.0) else: offset_ = 0.0 if u_iso_min >= offset_: u_iso_min = u_iso_min - offset_ t = adptbx.u_iso_as_u_star(uc, u_iso_min) for i_seq in tls_selection: sc = xray_structure.scatterers()[i_seq] assert sc.u_iso == u_iso[i_seq] u_iso_new = sc.u_iso - u_iso_min assert u_iso_new >= 0.0 sc.u_iso = u_iso_new assert sc.flags.use_u_aniso() assert sc.flags.use_u_iso() if(sc.u_star == (-1.0,-1.0,-1.0,-1.0,-1.0,-1.0)): sc.u_star = t else: x = flex.double(sc.u_star) y = flex.double(t) z = list(x + y) sc.u_star = z u_iso = xray_structure.scatterers().extract_u_iso().select( xray_structure.use_u_iso()) assert (u_iso < 0.0).count(True) == 0 u_eq_2 = xray_structure.extract_u_iso_or_u_equiv() assert approx_equal(u_eq_1, u_eq_2) time_split_u += timer.elapsed()
def adjust_stp(O, stp, csd, large_shift_site=0.1, large_shift_u_iso_factor=0.5, min_large_shift_u_iso=b_as_u(1)): print "adjust_stp", stp assert csd.size() == O.x.size() max_stp = 100 ix = 0 uc = O.xray_structure.unit_cell() sstab = O.xray_structure.site_symmetry_table() for i_sc,sc in enumerate(O.xray_structure.scatterers()): site_symmetry = sstab.get(i_sc) if (site_symmetry.is_point_group_1()): np = 3 for i,ucp in enumerate(uc.parameters()[:3]): lsx = large_shift_site / ucp \ * O.p_as_x[ix+i] if (abs(stp*csd[ix+i]) > lsx): max_stp = min(max_stp, abs(lsx/csd[ix])) else: constr = site_symmetry.site_constraints() np = constr.n_independent_params() raise RuntimeError("SPECIAL POSITION LARGE SHIFT NOT IMPLEMENTED.") ix += np u_iso = O.x[ix]/O.p_as_x[ix] lux = max(min_large_shift_u_iso, abs(u_iso) * large_shift_u_iso_factor) \ * O.p_as_x[ix] if (abs(stp*csd[ix]) > lux): max_stp = min(max_stp, abs(lux/csd[ix])) ix += 1 assert ix == O.x.size() print "max_stp:", max_stp sys.stdout.flush() if (O.params.use_max_stp): return min(max_stp, stp) return stp
def apply_back_trace_of_overall_exp_scale_matrix(self, xray_structure=None): if(xray_structure is None): return None k_sol, b_sol, b_cart = self.k_sols(), self.b_sols(), self.b_cart() assert len(k_sol)==1 # XXX Only one mask! k_sol = k_sol[0] b_sol = b_sol[0] # xrs = xray_structure if(xrs is None): return b_min = min(b_sol, xrs.min_u_cart_eigenvalue()*adptbx.u_as_b(1.)) if(b_min < 0): xrs.tidy_us() b_iso = (b_cart[0]+b_cart[1]+b_cart[2])/3.0 b_test = b_min+b_iso if(b_test < 0.0): b_adj = b_iso + abs(b_test) + 0.001 else: b_adj = b_iso b_cart_new = [b_cart[0]-b_adj,b_cart[1]-b_adj,b_cart[2]-b_adj, b_cart[3], b_cart[4], b_cart[5]] b_sol_new = b_sol + b_adj xrs.shift_us(b_shift = b_adj) b_min = min(b_sol_new, xrs.min_u_cart_eigenvalue()*adptbx.u_as_b(1.)) assert b_min >= 0.0 xrs.tidy_us() # assert self.fmodel_kbu k_masks = [ext.k_mask(self.fmodel_kbu.ss, k_sol, b_sol_new)] u_star=adptbx.u_cart_as_u_star( self.fmodel_kbu.f_obs.unit_cell(), adptbx.b_as_u(b_cart_new)) k_anisotropic = ext.k_anisotropic(self.fmodel_kbu.f_obs.indices(), u_star) self.fmodel_kbu = self.fmodel_kbu.update( b_sols = [b_sol_new], b_cart = b_cart_new) return group_args( xray_structure = xrs, b_adj = b_adj, b_sol = b_sol_new, b_cart = b_cart_new)
def exercise_05_k_sol_b_sol_only(d_min = 2.0): xray_structure = get_xray_structure_from_file() k_sol = 0.33 b_sol = 34.0 b_cart = [1,2,3,0,4,0] f_obs, r_free_flags = get_f_obs_freer( d_min = d_min, k_sol = k_sol, b_sol = b_sol, b_cart = b_cart, xray_structure = xray_structure) fmodel = mmtbx.f_model.manager( r_free_flags = r_free_flags, f_obs = f_obs, xray_structure = xray_structure) params = bss.master_params.extract() params.anisotropic_scaling = False u_star = adptbx.u_cart_as_u_star( fmodel.f_obs().unit_cell(),adptbx.b_as_u(b_cart)) fmodel_kbu = mmtbx.f_model.manager_kbu( f_obs = fmodel.f_obs(), f_calc = fmodel.f_calc(), f_masks = fmodel.arrays.core.f_masks, f_part1 = fmodel.arrays.core.f_part1, f_part2 = fmodel.arrays.core.f_part2, ss = fmodel.ss, u_star = u_star) r_work_start = fmodel_kbu.r_factor() result = bss.bulk_solvent_and_scales( fmodel_kbu = fmodel_kbu, params = params) r_work = result.fmodels.r_factor()*100. assert r_work_start > 0.05 assert approx_equal(r_work, 0.0, eps = 1.e-6) assert approx_equal(result.k_sol(0), k_sol, eps = 1.e-6) assert approx_equal(result.b_sol(), b_sol, eps = 1.e-6) assert approx_equal(result.b_cart(), b_cart, eps = 1.e-6)
def exercise(small=1.e-9): for symbol in ["P 1"]: space_group_info = sgtbx.space_group_info(symbol=symbol) random_xray_structure = random_structure.xray_structure( space_group_info=space_group_info, elements=["N"] * 10, volume_per_atom=50.0, random_u_iso=False, u_iso=adptbx.b_as_u(20.0)) sg = random_xray_structure.space_group() uc = random_xray_structure.unit_cell() print(symbol, uc) print() sys.stdout.flush() counter = 0 trials = 100000 i = 0 branch_0 = 0 branch_1 = 0 branch_1_1 = 0 branch_1_2 = 0 branch_1_2_1 = 0 branch_1_2_2 = 0 branch_1_2_3 = 0 branch_1_2_3_1 = 0 branch_1_2_3_2 = 0 while i < trials: i += 1 counter += 1 r = random.random() if (r < 0.333): m = adptbx.random_u_cart(u_scale=20. * random.random(), u_min=0) n = adptbx.random_u_cart(u_scale=20. * random.random(), u_min=0) while 1: for ind in range(6): r = random.random() m = flex.double(m) if (r > 0.5): m[ind] = n[ind] m = list(m) if (adptbx.is_positive_definite(m, 0) and adptbx.is_positive_definite(n, 0)): break elif (r >= 0.333 and r < 0.66): m, n = branch_3_mn() else: m, n = branch_2_mn(0) m_dc = copy.deepcopy(m) n_dc = copy.deepcopy(n) m = factor(list(m), i) n = factor(list(n), i) qq = tools.common(n, m, small) #assert approx_equal(m,m_dc) #assert approx_equal(n,n_dc) if (qq.branch_0()): branch_0 += 1 if (qq.branch_1()): branch_1 += 1 if (qq.branch_1_1()): branch_1_1 += 1 if (qq.branch_1_2()): branch_1_2 += 1 if (qq.branch_1_2_1()): branch_1_2_1 += 1 if (qq.branch_1_2_2()): branch_1_2_2 += 1 if (qq.branch_1_2_3()): branch_1_2_3 += 1 if (qq.branch_1_2_3_1()): branch_1_2_3_1 += 1 if (qq.branch_1_2_3_2()): branch_1_2_3_2 += 1 if (counter >= 10000): counter = 0 print("." * 30, symbol) print("i= ", i, "out of ", trials) print("branch_0 = ", branch_0) print("branch_1 = ", branch_1) print("branch_1_1 = ", branch_1_1) print("branch_1_2 = ", branch_1_2) print("branch_1_2_1 = ", branch_1_2_1) print("branch_1_2_2 = ", branch_1_2_2) print("branch_1_2_3 = ", branch_1_2_3) print("branch_1_2_3_1 = ", branch_1_2_3_1) print("branch_1_2_3_2 = ", branch_1_2_3_2) sys.stdout.flush()
def __init__(self, fmodels, restraints_manager = None, model = None, is_neutron_scat_table = None, target_weights = None, tan_b_iso_max = None, refine_xyz = False, refine_adp = False, lbfgs_termination_params = None, use_fortran = False, verbose = 0, correct_special_position_tolerance = 1.0, iso_restraints = None, h_params = None, qblib_params = None, macro_cycle = None, u_min = adptbx.b_as_u(-5.0), u_max = adptbx.b_as_u(999.99), collect_monitor = True, log = None): timer = user_plus_sys_time() adopt_init_args(self, locals()) self.f=None self.xray_structure = self.fmodels.fmodel_xray().xray_structure self.fmodels.create_target_functors() self.fmodels.prepare_target_functors_for_minimization() if(self.refine_adp and fmodels.fmodel_neutron() is None): self.xray_structure.tidy_us() self.fmodels.update_xray_structure( xray_structure = self.xray_structure, update_f_calc = True) self.weights = None # QBLIB INSERT self.qblib_params = qblib_params if(self.qblib_params is not None and self.qblib_params.qblib): self.macro = macro_cycle self.qblib_cycle_count = 0 self.tmp_XYZ = None self.XYZ_diff_curr=None # QBLIB END self.correct_special_position_tolerance = correct_special_position_tolerance if(refine_xyz and target_weights is not None): self.weights = target_weights.xyz_weights_result elif(refine_adp and target_weights is not None): self.weights = target_weights.adp_weights_result else: from phenix.refinement import weight_xray_chem self.weights = weight_xray_chem.weights(wx = 1, wx_scale = 1, angle_x = None, wn = 1, wn_scale = 1, angle_n = None, w = 0, wxn = 1) if(self.collect_monitor): self.monitor = monitor( weights = self.weights, fmodels = fmodels, model = model, iso_restraints = iso_restraints, refine_xyz = refine_xyz, refine_adp = refine_adp, refine_occ = False) if(self.collect_monitor): self.monitor.collect() self.neutron_refinement = (self.fmodels.fmodel_n is not None) self.x = flex.double(self.xray_structure.n_parameters(), 0) self._scatterers_start = self.xray_structure.scatterers() self.minimizer = scitbx.lbfgs.run( target_evaluator = self, termination_params = lbfgs_termination_params, use_fortran = use_fortran, exception_handling_params = scitbx.lbfgs.exception_handling_parameters( ignore_line_search_failed_step_at_lower_bound = True)) self.apply_shifts() del self._scatterers_start self.compute_target(compute_gradients = False,u_iso_refinable_params = None) if(self.refine_adp and self.fmodels.fmodel_neutron() is None): self.xray_structure.tidy_us() self.fmodels.update_xray_structure( xray_structure = self.xray_structure, update_f_calc = True) if(self.collect_monitor): self.monitor.collect(iter = self.minimizer.iter(), nfun = self.minimizer.nfun()) self.fmodels.create_target_functors() # QBLIB INSERT if(self.qblib_params is not None and self.qblib_params.qblib): print >>self.qblib_params.qblib_log,'{:-^80}'.format("") print >>self.qblib_params.qblib_log
def exercise_6_instantiate_consistency(symbol = "C 2"): random.seed(0) flex.set_random_seed(0) for scale in [1.e-4, 1.0, 1.e+4]: for k_sol in [0, 0.3]: for b_sol in [0, 50]: for set_h_occ_to_zero in [True, False]: for update_f_part1 in [True, False]: for apply_scale_to in ["f_obs", "f_model"]: # Simulate Fobs START x = random_structure.xray_structure( space_group_info = sgtbx.space_group_info(symbol=symbol), elements =(("O","N","C")*3+("H",)*10), volume_per_atom = 50, min_distance = 3, general_positions_only = True, random_u_iso = True, random_occupancy = False) x.scattering_type_registry(table="wk1995") x.set_occupancies(value=0.8, selection = x.hd_selection()) f_calc = x.structure_factors(d_min = 2.0).f_calc() mask_manager = mmtbx.masks.manager(miller_array = f_calc) f_mask = mask_manager.shell_f_masks(xray_structure = x)[0] assert flex.mean(abs(f_mask).data()) > 0 b_cart=adptbx.random_traceless_symmetry_constrained_b_cart( crystal_symmetry=x.crystal_symmetry()) u_star = adptbx.u_cart_as_u_star(x.unit_cell(), adptbx.b_as_u(b_cart)) k_anisotropic = mmtbx.f_model.ext.k_anisotropic(f_calc.indices(), u_star) ss = 1./flex.pow2(f_calc.d_spacings().data()) / 4. k_mask = mmtbx.f_model.ext.k_mask(ss, k_sol, b_sol) if(apply_scale_to=="f_model"): k_isotropic = flex.double(f_calc.data().size(), scale) else: k_isotropic = flex.double(f_calc.data().size(), 1) f_model_data = scale*k_anisotropic*(f_calc.data()+k_mask*f_mask.data()) f_model = f_calc.customized_copy(data = f_model_data) f_obs = abs(f_model) if(apply_scale_to=="f_obs"): f_obs = f_obs.customized_copy(data = f_obs.data()*scale) r_free_flags = f_obs.generate_r_free_flags() # Simulate Fobs END if(set_h_occ_to_zero): x.set_occupancies(value=0.0, selection = x.hd_selection()) x.shake_sites_in_place(mean_distance=5) sel = x.random_remove_sites_selection(fraction=0.3) x = x.select(sel) fmodel = mmtbx.f_model.manager( xray_structure = x, f_obs = f_obs, r_free_flags = r_free_flags) fmodel.update_all_scales(fast=True, show=False, update_f_part1=update_f_part1) f_part1_data = fmodel.f_calc().data()*flex.random_double( fmodel.f_calc().data().size()) f_part1 = fmodel.f_calc().customized_copy(data = f_part1_data) fmodel.update(f_part1 = f_part1) r1 = fmodel.r_work() # zero=fmodel.f_calc().customized_copy(data=fmodel.f_calc().data()*0) fmodel_dc = mmtbx.f_model.manager( f_obs = fmodel.f_obs(), r_free_flags = fmodel.r_free_flags(), k_isotropic = fmodel.k_isotropic(), k_anisotropic = fmodel.k_anisotropic(), f_calc = fmodel.f_model_no_scales(), f_part1 = fmodel.f_part1(), f_part2 = fmodel.f_part2(), f_mask = zero) r2 = fmodel_dc.r_work() if(0): print "r1=%8.6f r2=%8.6f fp1=%6.3f fp2=%6.3f fc=%6.3f"%(r1, r2, flex.mean(abs(fmodel.f_part1()).data()), \ flex.mean(abs(fmodel.f_part2()).data()), \ flex.mean(abs(fmodel.f_calc()).data())), \ "set_h_occ_to_zero=", set_h_occ_to_zero,\ "update_f_part1=", update_f_part1 assert approx_equal(r1, r2), [r1, r2]
def u_iso(structure_ideal, d_min, f_obs, tan_u_iso, verbose=0): sh = shifted_u_iso(f_obs, structure_ideal, 0, 0.05) if (0 or verbose): print "u_iso" sh.structure_shifted.show_summary().show_scatterers() print ls = xray.targets_least_squares_residual( f_obs.data(), sh.f_calc.data(), True, 1) gradient_flags = randomize_gradient_flags( xray.structure_factors.gradient_flags(u_iso=True), f_obs.anomalous_flag()) if(tan_u_iso): u_iso_refinable_params = flex.double() else: u_iso_refinable_params = None for scatterer in sh.structure_shifted.scatterers(): scatterer.flags.set_grad_site(gradient_flags.site) scatterer.flags.set_grad_u_iso(gradient_flags.u_iso) scatterer.flags.set_grad_u_aniso(gradient_flags.u_aniso) scatterer.flags.set_grad_occupancy(gradient_flags.occupancy) scatterer.flags.set_grad_fp(gradient_flags.fp) scatterer.flags.set_grad_fdp(gradient_flags.fdp) if(tan_u_iso): scatterer.flags.set_tan_u_iso(True) param = random.randint(90,120) scatterer.flags.param= param value=math.tan(scatterer.u_iso*math.pi/adptbx.b_as_u(param)-math.pi/2) u_iso_refinable_params.append(value) if (0): print "u_iso" print "gradient_flags.site ", gradient_flags.site print "gradient_flags.u_iso ", gradient_flags.u_iso print "gradient_flags.u_aniso ", gradient_flags.u_aniso print "gradient_flags.occupancy ", gradient_flags.occupancy print "gradient_flags.fp ", gradient_flags.fp print "gradient_flags.fdp ", gradient_flags.fdp cntr_use_u_iso = 0 cntr_use_u_aniso = 0 cntr_grad_u_iso = 0 cntr_grad_u_aniso = 0 for scatterer in sh.structure_shifted.scatterers(): if (scatterer.flags.use_u_iso()): cntr_use_u_iso += 1 if (scatterer.flags.use_u_aniso()): cntr_use_u_aniso += 1 if (scatterer.flags.grad_u_iso()): cntr_grad_u_iso += 1 if (scatterer.flags.grad_u_aniso()): cntr_grad_u_aniso += 1 print "use_u_iso ", cntr_use_u_iso,cntr_grad_u_iso print "use_u_aniso ", cntr_use_u_aniso,cntr_grad_u_aniso grad_flags_counts = \ xray.scatterer_grad_flags_counts(sh.structure_shifted.scatterers()) if(grad_flags_counts.n_parameters() > 0): sfd = xray.structure_factors.gradients_direct( xray_structure=sh.structure_shifted, u_iso_refinable_params=u_iso_refinable_params, miller_set=f_obs, d_target_d_f_calc=ls.derivatives(), n_parameters=0) re = resampling(miller_set=f_obs) map0 = re( xray_structure=sh.structure_shifted, u_iso_refinable_params=u_iso_refinable_params, dp=miller.array(miller_set=f_obs, data=ls.derivatives()), n_parameters=0, verbose=verbose) if(grad_flags_counts.u_aniso > 0): sfd_d_target_d_u_cart = sfd.d_target_d_u_cart() map0_d_target_d_u_cart = map0.d_target_d_u_cart() top_gradient = None gradients_1 = [] for i_scatterer,scatterer in enumerate(sh.structure_shifted.scatterers()): if(0): print "i_scatterer= ", i_scatterer,scatterer.flags.use_u_iso(),\ scatterer.flags.grad_u_iso(), scatterer.flags.use_u_aniso(),\ scatterer.flags.grad_u_aniso(), scatterer.u_iso, scatterer.u_star if(scatterer.flags.use_u_iso()): parameter_name = "u_iso" if(scatterer.flags.use_u_aniso()): parameter_name = "u_star" if(parameter_name == "u_iso" and scatterer.flags.grad_u_iso() and scatterer.flags.use_u_iso()): direct_summ = sfd.d_target_d_u_iso()[i_scatterer] if (top_gradient is None): top_gradient = direct_summ fast_gradie = map0.d_target_d_u_iso()[i_scatterer] sys.stdout.flush() gradients_1.append([direct_summ, fast_gradie]) match = judge(scatterer, parameter_name, direct_summ, fast_gradie, top_gradient) if (0 or verbose): print "direct summ[%d]: " % i_scatterer, direct_summ print "fast gradie[%d]: " % i_scatterer, fast_gradie, match print assert not match.is_bad if(parameter_name == "u_star" and scatterer.flags.grad_u_aniso() and scatterer.flags.use_u_aniso()): sfd_star = sfd.d_target_d_u_star()[i_scatterer] sfd_cart = adptbx.grad_u_star_as_u_cart( structure_ideal.unit_cell(), sfd_star) assert approx_equal( sfd_star, adptbx.grad_u_cart_as_u_star(structure_ideal.unit_cell(), sfd_cart)) for ij in xrange(6): direct_summ = sfd_d_target_d_u_cart[i_scatterer][ij] if (top_gradient is None): top_gradient = direct_summ fast_gradie = map0_d_target_d_u_cart[i_scatterer][ij] gradients_1.append([direct_summ, fast_gradie]) match =judge(scatterer,"u_star",direct_summ,fast_gradie,top_gradient) if (0 or verbose or match.is_bad): print "direct summ[%d][%d]: " % (i_scatterer, ij), direct_summ print "fast gradie[%d][%d]: " % (i_scatterer, ij),fast_gradie,match print assert not match.is_bad # Making sure that gradients_1 = gradients_2 for i_scatterer,scatterer in enumerate(sh.structure_shifted.scatterers()): if(not scatterer.flags.use_u_iso()): scatterer.u_iso = -12345.0 if(not scatterer.flags.use_u_aniso()): scatterer.u_star =(-999.,-999.,-999.,-999.,-999.,-999.) sfd = xray.structure_factors.gradients_direct( xray_structure=sh.structure_shifted, u_iso_refinable_params=u_iso_refinable_params, miller_set=f_obs, d_target_d_f_calc=ls.derivatives(), n_parameters=0) re = resampling(miller_set=f_obs) map0 = re( xray_structure=sh.structure_shifted, u_iso_refinable_params=u_iso_refinable_params, dp=miller.array(miller_set=f_obs, data=ls.derivatives()), n_parameters=0, verbose=verbose) grad_flags_counts = \ xray.scatterer_grad_flags_counts(sh.structure_shifted.scatterers()) if(grad_flags_counts.u_aniso): sfd_d_target_d_u_cart = sfd.d_target_d_u_cart() map0_d_target_d_u_cart = map0.d_target_d_u_cart() gradients_2 = [] for i_scatterer,scatterer in enumerate(sh.structure_shifted.scatterers()): if(scatterer.flags.use_u_iso()): parameter_name = "u_iso" if(scatterer.flags.use_u_aniso()): parameter_name = "u_star" if(parameter_name == "u_iso" and scatterer.flags.grad_u_iso() and scatterer.flags.use_u_iso()): direct_summ = sfd.d_target_d_u_iso()[i_scatterer] fast_gradie = map0.d_target_d_u_iso()[i_scatterer] gradients_2.append([direct_summ, fast_gradie]) if(parameter_name == "u_star" and scatterer.flags.grad_u_aniso() and scatterer.flags.use_u_aniso()): sfd_star = sfd.d_target_d_u_star()[i_scatterer] sfd_cart = adptbx.grad_u_star_as_u_cart(structure_ideal.unit_cell(), sfd_star) assert approx_equal( sfd_star, adptbx.grad_u_cart_as_u_star(structure_ideal.unit_cell(), sfd_cart)) for ij in xrange(6): direct_summ = sfd_d_target_d_u_cart[i_scatterer][ij] fast_gradie = map0_d_target_d_u_cart[i_scatterer][ij] gradients_2.append([direct_summ, fast_gradie]) for g1,g2 in zip(gradients_1, gradients_2): assert approx_equal(g1, g2) sys.stdout.flush()
def exercise(space_group_info, n_elements = 10, table = "wk1995", d_min = 2.0, k_sol = 0.35, b_sol = 45.0, b_cart = None, quick=False, verbose=0): xray_structure = random_structure.xray_structure( space_group_info = space_group_info, elements =(("O","N","C")*(n_elements//3+1))[:n_elements], volume_per_atom = 100, min_distance = 1.5, general_positions_only = True, random_u_iso = False, random_occupancy = False) xray_structure.scattering_type_registry(table = table) sg = xray_structure.space_group() uc = xray_structure.unit_cell() u_cart_1 = adptbx.random_u_cart(u_scale=5, u_min=5) u_star_1 = adptbx.u_cart_as_u_star(uc, u_cart_1) b_cart = adptbx.u_star_as_u_cart(uc, sg.average_u_star(u_star = u_star_1)) for anomalous_flag in [False, True]: scatterers = xray_structure.scatterers() if (anomalous_flag): assert scatterers.size() >= 7 for i in [1,7]: scatterers[i].fp = -0.2 scatterers[i].fdp = 5 have_non_zero_fdp = True else: for i in [1,7]: scatterers[i].fp = 0 scatterers[i].fdp = 0 have_non_zero_fdp = False f_obs = abs(xray_structure.structure_factors( d_min = d_min, anomalous_flag = anomalous_flag, cos_sin_table = sfg_params.cos_sin_table, algorithm = sfg_params.algorithm).f_calc()) f_obs_comp = f_obs.structure_factors_from_scatterers( xray_structure = xray_structure, algorithm = sfg_params.algorithm, cos_sin_table = sfg_params.cos_sin_table).f_calc() f_obs = abs(f_obs_comp) flags = f_obs.generate_r_free_flags(fraction = 0.1, max_free = 99999999) #flags = flags.array(data = flex.bool(f_obs.data().size(), False)) xrs = xray_structure.deep_copy_scatterers() xrs.shake_sites_in_place(rms_difference=0.3) for target in mmtbx.refinement.targets.target_names: if (quick): if (target not in ["ls_wunit_k1", "ml", "mlhl", "ml_sad"]): continue if (target == "mlhl"): if (have_non_zero_fdp): continue # XXX gradients not correct! experimental_phases = generate_random_hl(miller_set=f_obs) else: experimental_phases = None if (target == "ml_sad" and (not anomalous_flag or mmtbx.refinement.targets.phaser is None)): continue print " ",target xray.set_scatterer_grad_flags( scatterers = xrs.scatterers(), site = True) ss = 1./flex.pow2(f_obs.d_spacings().data()) / 4. u_star = adptbx.u_cart_as_u_star( f_obs.unit_cell(), adptbx.b_as_u(b_cart)) k_anisotropic = mmtbx.f_model.ext.k_anisotropic( f_obs.indices(), u_star) k_mask = mmtbx.f_model.ext.k_mask(ss, k_sol, b_sol) fmodel = mmtbx.f_model.manager( xray_structure = xrs, f_obs = f_obs, r_free_flags = flags, target_name = target, abcd = experimental_phases, sf_and_grads_accuracy_params = sfg_params, k_mask = k_mask, k_anisotropic = k_anisotropic, mask_params = masks.mask_master_params.extract()) fmodel.update_xray_structure( xray_structure=xrs, update_f_calc=True, update_f_mask=True) xray.set_scatterer_grad_flags( scatterers=fmodel.xray_structure.scatterers(), site=True) fmodel.update_xray_structure(update_f_calc=True) t_f = fmodel.target_functor() t_f.prepare_for_minimization() gs = t_f(compute_gradients=True).d_target_d_site_cart().as_double() gfd = finite_differences_site(target_functor=t_f) cc = flex.linear_correlation(gs, gfd).coefficient() if (0 or verbose): print "ana:", list(gs) print "fin:", list(gfd) print "rat:", [f/a for a,f in zip(gs,gfd)] print target, "corr:", cc, space_group_info print diff = gs - gfd diff /= max(1, flex.max(flex.abs(gfd))) tolerance = 1.2e-5 assert approx_equal(abs(flex.min(diff) ), 0.0, tolerance) assert approx_equal(abs(flex.mean(diff)), 0.0, tolerance) assert approx_equal(abs(flex.max(diff) ), 0.0, tolerance) assert approx_equal(cc, 1.0, tolerance) fmodel.model_error_ml()
def run_0(symbol = "C 2"): space_group_info = sgtbx.space_group_info(symbol = symbol) xrs = random_structure.xray_structure( space_group_info = space_group_info, elements = ["N"]*50, volume_per_atom = 100.0, random_u_iso = True) # b_cart = adptbx.random_traceless_symmetry_constrained_b_cart( crystal_symmetry=xrs.crystal_symmetry()) u_star = adptbx.u_cart_as_u_star(xrs.unit_cell(), adptbx.b_as_u(b_cart)) # F = xrs.structure_factors(d_min = 1.5).f_calc() k_anisotropic = mmtbx.f_model.ext.k_anisotropic(F.indices(), u_star) # bin_selections = [] F.setup_binner(reflections_per_bin=50) for i_bin in F.binner().range_used(): sel = F.binner().selection(i_bin) bin_selections.append(sel) # d_spacings = F.d_spacings().data() ss = 1./flex.pow2(d_spacings) / 4. k_mask_tmp = mmtbx.f_model.ext.k_mask(ss, 0.35, 80.) k_mask = flex.double(F.data().size(), 0) k_isotropic = flex.double(F.data().size(), 0) for s in bin_selections: d = d_spacings.select(s) k_mask.set_selected(s, flex.mean(k_mask_tmp.select(s))) k_isotropic.set_selected(s, random.randint(1,10)) # fmodel = mmtbx.f_model.manager( xray_structure = xrs, f_obs = abs(F), k_isotropic = k_isotropic, k_anisotropic = k_anisotropic, k_mask = k_mask) f_calc = fmodel.f_calc() f_masks = fmodel.f_masks() f_model = fmodel.f_model() f_obs = abs(f_model) r_free_flags = f_obs.generate_r_free_flags(use_lattice_symmetry=False) # assert approx_equal(bulk_solvent.r_factor(f_obs.data(), f_model.data()), 0) aso = scaler.run( f_obs = f_obs, f_calc = f_calc, f_mask = f_masks, r_free_flags = r_free_flags, bin_selections = bin_selections, number_of_cycles = 500, auto_convergence_tolerance = 1.e-9, ss = ss, try_poly = True, try_expanal = True, try_expmin = True, verbose = False) assert approx_equal(aso.r_final, 0.00037, 0.00001) assert approx_equal(aso.r_low, 0.00002, 0.00001) assert approx_equal(aso.r_high, 0.00006, 0.00001) assert approx_equal( bulk_solvent.r_factor(f_obs.data(), abs(aso.core.f_model).data(), 1), bulk_solvent.r_factor(f_obs.data(), abs(aso.core.f_model).data()))
def run_02(): time_aniso_u_scaler = 0 for symbol in sgtbx.bravais_types.acentric + sgtbx.bravais_types.centric: #print symbol, "-"*50 space_group_info = sgtbx.space_group_info(symbol = symbol) xrs = random_structure.xray_structure( space_group_info = space_group_info, elements = ["N"]*100, volume_per_atom = 50.0, random_u_iso = True) xrs.scattering_type_registry(table = "wk1995") # XXX ad a method to adptbx to do this point_group = sgtbx.space_group_info( symbol=symbol).group().build_derived_point_group() adp_constraints = sgtbx.tensor_rank_2_constraints( space_group=point_group, reciprocal_space=True) u_star = adptbx.u_cart_as_u_star(xrs.unit_cell(), adptbx.random_u_cart(u_scale=1,u_min=0.1)) u_indep = adp_constraints.independent_params(all_params=u_star) u_star = adp_constraints.all_params(independent_params=u_indep) b_cart_start=adptbx.u_as_b(adptbx.u_star_as_u_cart(xrs.unit_cell(), u_star)) # tr = (b_cart_start[0]+b_cart_start[1]+b_cart_start[2])/3 b_cart_start = [b_cart_start[0]-tr,b_cart_start[1]-tr,b_cart_start[2]-tr, b_cart_start[3],b_cart_start[4],b_cart_start[5]] tr = (b_cart_start[0]+b_cart_start[1]+b_cart_start[2])/3 # #print "Input b_cart :", " ".join(["%8.4f"%i for i in b_cart_start]), "tr:", tr reg = xrs.scattering_type_registry(table="wk1995", d_min=1/12) f_000 = reg.sum_of_scattering_factors_at_diffraction_angle_0() F = xrs.structure_factors(d_min = 2.0).f_calc() i = F.indices() i.append([0,0,0]) d = F.data() d.append(f_000) F = F.customized_copy(indices = i, data = d) u_star = adptbx.u_cart_as_u_star( F.unit_cell(), adptbx.b_as_u(b_cart_start)) fbc = mmtbx.f_model.ext.k_anisotropic(F.indices(), u_star) fc = F.structure_factors_from_scatterers(xray_structure=xrs).f_calc() f_obs = F.customized_copy(data = flex.abs(fc.data()*fbc)) #print bulk_solvent.r_factor(f_obs.data(), fmodel.f_model().data()) obj = bulk_solvent.aniso_u_scaler( f_model = fc.data(), f_obs = f_obs.data(), miller_indices = f_obs.indices(), unit_cell = f_obs.unit_cell()) a = obj.a #### #print "Input a :", " ".join(["%7.3f"%i for i in a]) overall_anisotropic_scale = mmtbx.f_model.ext.k_anisotropic( f_obs.indices(), a, f_obs.unit_cell()) #print bulk_solvent.r_factor(f_obs.data(), fmodel.f_model().data()*overall_anisotropic_scale) f_obs = abs(fc) f_obs = f_obs.customized_copy(data = f_obs.data() * overall_anisotropic_scale) #print bulk_solvent.r_factor(f_obs.data(), fmodel.f_model().data()) #print bulk_solvent.r_factor(f_obs.data(), fmodel.f_model().data()) t0 = time.time() obj = bulk_solvent.aniso_u_scaler( f_model = fc.data(), f_obs = f_obs.data(), miller_indices = f_obs.indices(), unit_cell = f_obs.unit_cell()) time_aniso_u_scaler += (time.time()-t0) overall_anisotropic_scale = mmtbx.f_model.ext.k_anisotropic( f_obs.indices(), obj.a, f_obs.unit_cell()) assert approx_equal(bulk_solvent.r_factor(f_obs.data(), fc.data()*overall_anisotropic_scale), 0.0, 1.e-2) # XXX seems to be low #print "Output a:", " ".join(["%7.3f"%i for i in obj.a]) assert approx_equal(a, obj.a, 1.e-4) # XXX can it be smaller? assert overall_anisotropic_scale[len(overall_anisotropic_scale)-1]==1 print "Time (aniso_u_scaler only): %6.4f"%time_aniso_u_scaler
def energies_adp_iso(self, xray_structure, parameters, use_u_local_only, use_hd, wilson_b=None, compute_gradients=False, tan_b_iso_max=None, u_iso_refinable_params=None, gradients=None): """ Compute target and gradients for isotropic ADPs/B-factors relative to restraints. :returns: scitbx.restraints.energies object """ result = scitbx.restraints.energies( compute_gradients=compute_gradients, gradients=gradients, gradients_size=xray_structure.scatterers().size(), gradients_factory=flex.double, normalization=self.normalization) if (self.geometry is None): result.geometry = None else: result.geometry = cctbx.adp_restraints.energies_iso( geometry_restraints_manager=self.geometry, xray_structure=xray_structure, parameters=parameters, wilson_b=wilson_b, use_hd = use_hd, use_u_local_only=use_u_local_only, compute_gradients=compute_gradients, gradients=result.gradients) result += result.geometry if (self.ncs_groups is not None and \ self.torsion_ncs_groups is not None): raise Sorry("Cannot have both Cartesian and torsion NCS restraints"+\ " at the same time.") if (self.ncs_groups is None): result.ncs_groups = None else: result.ncs_groups = self.ncs_groups.energies_adp_iso( u_isos=xray_structure.extract_u_iso_or_u_equiv(), average_power=parameters.average_power, compute_gradients=compute_gradients, gradients=result.gradients) result += result.ncs_groups if (self.torsion_ncs_groups is None): result.torsion_ncs_groups = None else: result.torsion_ncs_groups = self.torsion_ncs_groups.energies_adp_iso( u_isos=xray_structure.extract_u_iso_or_u_equiv(), average_power=parameters.average_power, compute_gradients=compute_gradients, gradients=result.gradients) result += result.torsion_ncs_groups result.finalize_target_and_gradients() if(compute_gradients): #XXX highly inefficient code: do something asap by adopting new scatters flags if(tan_b_iso_max is not None and tan_b_iso_max != 0): u_iso_max = adptbx.b_as_u(tan_b_iso_max) if(u_iso_refinable_params is not None): chain_rule_scale = u_iso_max / math.pi / (flex.pow2(u_iso_refinable_params)+1.0) else: u_iso_refinable_params = flex.tan(math.pi*(xray_structure.scatterers().extract_u_iso()/u_iso_max-1./2.)) chain_rule_scale = u_iso_max / math.pi / (flex.pow2(u_iso_refinable_params)+1.0) else: chain_rule_scale = 1.0 result.gradients = result.gradients * chain_rule_scale return result