def _calc_candidate_hkls(self): # First a list of indices that fill 1 ASU hkl_list = miller.build_set( self._target_symmetry_primitive, anomalous_flag=False, d_min=self._params.candidate_spots.d_min, ) rt = flex.reflection_table() rt["miller_index"] = hkl_list.indices() rt["d_star"] = 1.0 / hkl_list.d_spacings().data() rt["rlp_datum"] = self.Bmat.elems * rt["miller_index"].as_vec3_double() self.candidate_hkls = rt # Now P1 indices with separate Friedel pairs hkl_list = miller.build_set( self._target_symmetry_primitive, anomalous_flag=True, d_min=self._params.candidate_spots.d_min, ) hkl_list_p1 = hkl_list.expand_to_p1() rt = flex.reflection_table() rt["miller_index"] = hkl_list_p1.indices() rt["d_star"] = 1.0 / hkl_list_p1.d_spacings().data() rt["rlp_datum"] = self.Bmat.elems * rt["miller_index"].as_vec3_double() self.candidate_hkls_p1 = rt return
def run(): target_structure = random_structure.xray_structure( space_group_info=sgtbx.space_group_info("I432"), elements=['C']*6+['O'], use_u_iso=False, use_u_aniso=True, ) shift = tuple(flex.random_double(3)) print "shift to be found: (%.3f, %.3f, %.3f)" % shift target_structure_in_p1 = target_structure.expand_to_p1().apply_shift(shift) miller_indices = miller.build_set( crystal_symmetry=target_structure, anomalous_flag=True, d_min=0.8) f_obs = miller_indices.structure_factors_from_scatterers( xray_structure=target_structure, algorithm="direct").f_calc().amplitudes() miller_indices_in_p1 = miller.build_set( crystal_symmetry=target_structure_in_p1, anomalous_flag=True, d_min=0.8) f_calc = miller_indices_in_p1.structure_factors_from_scatterers( xray_structure=target_structure_in_p1, algorithm="direct").f_calc() crystal_gridding = f_calc.crystal_gridding( symmetry_flags=translation_search.symmetry_flags( is_isotropic_search_model=False, have_f_part=False), resolution_factor=1/2 ) omptbx.env.num_threads = 1 t_fast_tf = show_times() fast_tf_map = translation_search.fast_nv1995( gridding=crystal_gridding.n_real(), space_group=f_obs.space_group(), anomalous_flag=f_obs.anomalous_flag(), miller_indices_f_obs=f_obs.indices(), f_obs=f_obs.data(), f_part=flex.complex_double(), ## no sub-structure is already fixed miller_indices_p1_f_calc=f_calc.indices(), p1_f_calc=f_calc.data()).target_map() print print "Fast translation function" t_fast_tf() t_cross_corr = show_times() for op in target_structure.space_group(): f, op_times_f = f_calc.original_and_transformed(op) cross_corr_map = miller.fft_map(crystal_gridding, f * op_times_f.conjugate().data()) print print "Traditional cross-correlation" t_cross_corr()
def run(): target_structure = random_structure.xray_structure( space_group_info=sgtbx.space_group_info("I432"), elements=['C'] * 6 + ['O'], use_u_iso=False, use_u_aniso=True, ) shift = tuple(flex.random_double(3)) print "shift to be found: (%.3f, %.3f, %.3f)" % shift target_structure_in_p1 = target_structure.expand_to_p1().apply_shift(shift) miller_indices = miller.build_set(crystal_symmetry=target_structure, anomalous_flag=True, d_min=0.8) f_obs = miller_indices.structure_factors_from_scatterers( xray_structure=target_structure, algorithm="direct").f_calc().amplitudes() miller_indices_in_p1 = miller.build_set( crystal_symmetry=target_structure_in_p1, anomalous_flag=True, d_min=0.8) f_calc = miller_indices_in_p1.structure_factors_from_scatterers( xray_structure=target_structure_in_p1, algorithm="direct").f_calc() crystal_gridding = f_calc.crystal_gridding( symmetry_flags=translation_search.symmetry_flags( is_isotropic_search_model=False, have_f_part=False), resolution_factor=1 / 2) omptbx.env.num_threads = 1 t_fast_tf = show_times() fast_tf_map = translation_search.fast_nv1995( gridding=crystal_gridding.n_real(), space_group=f_obs.space_group(), anomalous_flag=f_obs.anomalous_flag(), miller_indices_f_obs=f_obs.indices(), f_obs=f_obs.data(), f_part=flex.complex_double(), ## no sub-structure is already fixed miller_indices_p1_f_calc=f_calc.indices(), p1_f_calc=f_calc.data()).target_map() print print "Fast translation function" t_fast_tf() t_cross_corr = show_times() for op in target_structure.space_group(): f, op_times_f = f_calc.original_and_transformed(op) cross_corr_map = miller.fft_map(crystal_gridding, f * op_times_f.conjugate().data()) print print "Traditional cross-correlation" t_cross_corr()
def calculate_completeness(self, resolution_bin = None): '''Calculate the completeness of observations in a named resolution bin.''' if resolution_bin is None: resolution_range = self._mf.get_resolution_range() hkl_list = list(self._merged_reflections) else: resolution_range = self._resolution_ranges[resolution_bin] hkl_list = self._hkl_ranges[resolution_bin] uc = self._mf.get_unit_cell() sg = self._mf.get_space_group() dmin = min(resolution_range) dmax = max(resolution_range) cs = crystal_symmetry(unit_cell = uc, space_group = sg) hkl_calc = [hkl for hkl in build_set( cs, False, d_min = dmin, d_max = dmax).indices()] # remove systematically absent reflections hkl_list = [hkl for hkl in itertools.ifilterfalse(sg.is_sys_absent, hkl_list)] return float(len(hkl_list)) / float(len(hkl_calc))
def timings(structure, wing_cutoff=1.e-3): print "wing_cutoff for following fft calculations: %3.1e"%wing_cutoff for calc_type,exp_table_one_over_step_size in (("exp function:",0), ("exp table:",-100)): print calc_type for d_min in [4,3,2,1]: structure_ng = structure.deep_copy_scatterers() structure_ng.scattering_type_registry(d_min=d_min, table="n_gaussian") structure_4g = structure.deep_copy_scatterers() structure_4g.scattering_type_registry(table="it1992") miller_set = miller.build_set( crystal_symmetry=structure, d_min=d_min, anomalous_flag=False) miller_set.show_summary() times = [] for structure in (structure_ng, structure_4g): structure.scattering_type_registry().show_summary() f_calc_object = xray.structure_factors.from_scatterers( miller_set=miller_set, wing_cutoff=wing_cutoff, exp_table_one_over_step_size=exp_table_one_over_step_size)( xray_structure=structure, miller_set=miller_set, algorithm="fft") times.append(f_calc_object.manager().estimate_time_fft.time_sampling) print " %.2f seconds," % times[-1] print "d_min=%d: %.2f s / %.2f s" % (d_min, times[0], times[1]), if (times[1] != 0): print "= %.2f" % (times[0] / times[1]), print sys.stdout.flush() print
def exercise_trigonometric_ff(): from math import cos, sin, pi sgi = sgtbx.space_group_info("P1") cs = sgi.any_compatible_crystal_symmetry(volume=1000) miller_set = miller.build_set(cs, anomalous_flag=False, d_min=1) miller_set = miller_set.select(flex.random_double(miller_set.size()) < 0.2) for i in xrange(5): sites = flex.random_double(9) x1, x2, x3 = (matrix.col(sites[:3]), matrix.col(sites[3:6]), matrix.col(sites[6:])) xs = xray.structure(crystal.special_position_settings(cs)) for x in (x1, x2, x3): sc = xray.scatterer(site=x, scattering_type="const") sc.flags.set_grad_site(True) xs.add_scatterer(sc) f_sq = structure_factors.f_calc_modulus_squared(xs) for h in miller_set.indices(): h = matrix.col(h) phi1, phi2, phi3 = 2 * pi * h.dot(x1), 2 * pi * h.dot(x2), 2 * pi * h.dot(x3) fc_mod_sq = 3 + 2 * (cos(phi1 - phi2) + cos(phi2 - phi3) + cos(phi3 - phi1)) g = [] g.extend(-2 * (sin(phi1 - phi2) - sin(phi3 - phi1)) * 2 * pi * h) g.extend(-2 * (sin(phi2 - phi3) - sin(phi1 - phi2)) * 2 * pi * h) g.extend(-2 * (sin(phi3 - phi1) - sin(phi2 - phi3)) * 2 * pi * h) grad_fc_mod_sq = g f_sq.linearise(h) assert approx_equal(f_sq.observable, fc_mod_sq) assert approx_equal(f_sq.grad_observable, grad_fc_mod_sq)
def generate_mtz_files(space_group_info, anomalous_flag): crystal_symmetry = crystal.symmetry( unit_cell=space_group_info.any_compatible_unit_cell(volume=1000), space_group_info=space_group_info) miller_set = miller.build_set(crystal_symmetry=crystal_symmetry, anomalous_flag=anomalous_flag, d_min=1) miller_array = miller.array( miller_set=miller_set, data=flex.random_double(size=miller_set.indices().size())) miller_array_p1 = miller_array.expand_to_p1() miller_arrays = [] file_names = [] subgrs = subgroups.subgroups(space_group_info).groups_parent_setting() for i_subgroup, subgroup in enumerate(subgrs): subgroup_miller_array = miller_array_p1.customized_copy( space_group_info=sgtbx.space_group_info(group=subgroup)) \ .merge_equivalents() \ .array() \ .as_reference_setting() \ .set_observation_type_xray_intensity() file_name = "tmp_refl_stats%d.mtz" % i_subgroup mtz_object = subgroup_miller_array.as_mtz_dataset( column_root_label="FOBS").mtz_object().write(file_name=file_name) miller_arrays.append( subgroup_miller_array.f_sq_as_f().expand_to_p1().map_to_asu()) file_names.append(file_name) return miller_arrays, file_names
def exercise_5(grid_step, radius, shell, a, b, d_min, site_cart, buffer_layer): xray_structure = xray_structure_of_one_atom(site_cart = site_cart, buffer_layer = buffer_layer, a = a, b = b) miller_set = miller.build_set( crystal_symmetry = xray_structure.crystal_symmetry(), anomalous_flag = False, d_min = d_min, d_max = None) f_calc = miller_set.structure_factors_from_scatterers( xray_structure = xray_structure, algorithm = "direct", cos_sin_table = False, exp_table_one_over_step_size = False).f_calc() fft_map = f_calc.fft_map(grid_step = grid_step, symmetry_flags = None) fft_map = fft_map.apply_scaling(scale=100.0/fft_map.real_map_unpadded()[0]) assert approx_equal(fft_map.real_map_unpadded()[0],100.0)
def predict_positions(A, crystal, specs, savepath): """ Predict the locations of Bragg peaks out to the specified resolution, and remove any predicted to fall inside the missing wedge region. Output positions and their associated Miller indices as separate keys in a dictionary. """ # generate complete list of Miller indices to given resolution ms = miller.build_set(crystal_symmetry = crystal.get_crystal_symmetry(), anomalous_flag=True, d_min = specs['res']).expand_to_p1() hkl = np.array(ms.indices()) # predict the xyz positions of each peak in the FFT of the tomogram qvecs = np.inner(A, np.squeeze(hkl)).T px_coords = qvecs * 1.0 / specs['mag'] * specs['px_size'] + np.array(specs['shape']) / 2.0 print "Predicted %i reflections to %.1f resolution" %(len(hkl), specs['res']) # remove any Millers located inside the missing wedge mwedge = missing_wedge_mask(specs['angle'], specs['shape']) sel = np.fliplr(px_coords.copy()) sel = np.around(sel).astype(int) valid_idx = mwedge.flatten()[np.ravel_multi_index(sel.T, specs['shape'])] hkl_valid, px_valid = hkl[valid_idx==1], px_coords[valid_idx==1] print "Retained %i reflection outside missing wedge" %(len(hkl_valid)) # store in dictionary, save as pickle, and return predicted = dict() predicted['hkl'], predicted['xyz'] = hkl_valid, px_valid with open(os.path.join(savepath, "predicted_info.pickle"), "wb") as handle: pickle.dump(predicted, handle) return predicted
def __init__( self, map_data, pdb_hierarchy, # XXX redundant inputs xray_structure, # XXX redundant inputs d_min, use_mask=False, masking_atom_radius=5, max_iterations=50, macro_cycles=1, prefix="", log=None): adopt_init_args(self, locals()) self.cc_best = None self.sites_cart_best = None if(self.log is None): self.log = sys.stdout self.sites_cart_start = self.xray_structure.sites_cart() assert approx_equal(self.pdb_hierarchy.atoms().extract_xyz(), self.sites_cart_start, 1.e-3) self.crystal_gridding = maptbx.crystal_gridding( unit_cell = self.xray_structure.unit_cell(), space_group_info = self.xray_structure.space_group_info(), pre_determined_n_real = self.map_data.all()) self.complete_set = miller.build_set( crystal_symmetry = self.xray_structure.crystal_symmetry(), anomalous_flag = False, d_min = self.d_min) self._show_and_track() self.d_mins = self._get_mz_resolution_limits() for macro_cycle in xrange(self.macro_cycles): self._refine() self.xray_structure.set_sites_cart(self.sites_cart_best) self.pdb_hierarchy.adopt_xray_structure(self.xray_structure)
def timings(structure, wing_cutoff=1.e-3): print("wing_cutoff for following fft calculations: %3.1e" % wing_cutoff) for calc_type, exp_table_one_over_step_size in (("exp function:", 0), ("exp table:", -100)): print(calc_type) for d_min in [4, 3, 2, 1]: structure_ng = structure.deep_copy_scatterers() structure_ng.scattering_type_registry(d_min=d_min, table="n_gaussian") structure_4g = structure.deep_copy_scatterers() structure_4g.scattering_type_registry(table="it1992") miller_set = miller.build_set(crystal_symmetry=structure, d_min=d_min, anomalous_flag=False) miller_set.show_summary() times = [] for structure in (structure_ng, structure_4g): structure.scattering_type_registry().show_summary() f_calc_object = xray.structure_factors.from_scatterers( miller_set=miller_set, wing_cutoff=wing_cutoff, exp_table_one_over_step_size=exp_table_one_over_step_size)( xray_structure=structure, miller_set=miller_set, algorithm="fft") times.append( f_calc_object.manager().estimate_time_fft.time_sampling) print(" %.2f seconds," % times[-1]) print("d_min=%d: %.2f s / %.2f s" % (d_min, times[0], times[1]), end=' ') if (times[1] != 0): print("= %.2f" % (times[0] / times[1]), end=' ') print() sys.stdout.flush() print()
def exercise_centrics(space_group_info, n_sites=10): structure = random_structure.xray_structure( space_group_info=space_group_info, elements=(("O", "N", "C") * (n_sites // 3 + 1))[:n_sites], volume_per_atom=30, min_distance=1) for anomalous_flag in [False, True]: miller_set = miller.build_set(crystal_symmetry=structure, d_min=1, anomalous_flag=anomalous_flag) for shrink_truncation_radius in [0, .5 * 6**.5]: for solvent_radius in [0, .5 * 5**.5]: bulk_solvent_mask = mmtbx.masks.bulk_solvent( xray_structure=structure, grid_step=0.5, ignore_zero_occupancy_atoms=False, solvent_radius=solvent_radius, shrink_truncation_radius=shrink_truncation_radius) f_mask = bulk_solvent_mask.structure_factors( miller_set=miller_set) centrics = f_mask.select_centric() if (centrics.indices().size() > 0): ideal = centrics.phase_transfer(centrics) assert flex.max( flex.abs(ideal.data() - centrics.data())) < 1.e-6
def exercise_02(): for file_name, input_model in [("m_good.pdb", model_good), ("m_bad.pdb", model_bad)]: tmp_f = open(file_name, "w") tmp_f.write(input_model) tmp_f.close() xrs_exact = iotbx.pdb.pdb_input(file_name="m_good.pdb").xray_structure_simple() xrs_part = iotbx.pdb.pdb_input(file_name="m_bad.pdb").xray_structure_simple() miller_set = miller.build_set(crystal_symmetry=xrs_exact.crystal_symmetry(), anomalous_flag=False, d_min=0.6) f_obs = abs( miller_set.structure_factors_from_scatterers( xray_structure=xrs_exact, algorithm="direct", cos_sin_table=False ).f_calc() ) sf_par = mmtbx.f_model.sf_and_grads_accuracy_master_params.extract() sf_par.algorithm = "direct" sf_par.cos_sin_table = False fmodel = mmtbx.f_model.manager( xray_structure=xrs_part, sf_and_grads_accuracy_params=sf_par, target_name="ls_wunit_k1", f_obs=f_obs ) # mon_lib_srv = monomer_library.server.server() ener_lib = monomer_library.server.ener_lib() processed_pdb_file = monomer_library.pdb_interpretation.process( mon_lib_srv=mon_lib_srv, ener_lib=ener_lib, file_name="m_bad.pdb" ) model = mmtbx.model.manager( xray_structure=xrs_part, pdb_hierarchy=processed_pdb_file.all_chain_proxies.pdb_hierarchy, log=None ) # out = StringIO() params = find_hydrogens.all_master_params().extract() params.map_cutoff = 6.5 find_hydrogens.run(fmodel=fmodel, model=model, log=out, params=params)
def exercise_derivatives(space_group_info, out): crystal_symmetry = space_group_info.any_compatible_crystal_symmetry( volume=1000) space_group = space_group_info.group() adp_constraints = space_group.adp_constraints() m = adp_constraints.row_echelon_form() print >> out, matrix.rec(m, (m.size()//6, 6)).mathematica_form( one_row_per_line=True) print >> out, list(adp_constraints.independent_indices) u_cart_p1 = adptbx.random_u_cart() u_star_p1 = adptbx.u_cart_as_u_star(crystal_symmetry.unit_cell(), u_cart_p1) u_star = space_group.average_u_star(u_star_p1) miller_set = miller.build_set( crystal_symmetry=crystal_symmetry, d_min=3, anomalous_flag=False) for h in miller_set.indices(): grads_fin = d_dw_d_u_star_finite(h=h, u_star=u_star) print >> out, "grads_fin:", list(grads_fin) grads_ana = d_dw_d_u_star_analytical(h=h, u_star=u_star) print >> out, "grads_ana:", list(grads_ana) compare_derivatives(grads_ana, grads_fin) curvs_fin = d2_dw_d_u_star_d_u_star_finite(h=h, u_star=u_star) print >> out, "curvs_fin:", list(curvs_fin) curvs_ana = d2_dw_d_u_star_d_u_star_analytical(h=h, u_star=u_star) print >> out, "curvs_ana:", list(curvs_ana) compare_derivatives(curvs_ana, curvs_fin) # u_indep = adp_constraints.independent_params(u_star) grads_indep_fin = d_dw_d_u_indep_finite( adp_constraints=adp_constraints, h=h, u_indep=u_indep) print >> out, "grads_indep_fin:", list(grads_indep_fin) grads_indep_ana = flex.double(adp_constraints.independent_gradients( all_gradients=list(grads_ana))) print >> out, "grads_indep_ana:", list(grads_indep_ana) compare_derivatives(grads_indep_ana, grads_indep_fin) curvs_indep_fin = d2_dw_d_u_indep_d_u_indep_finite( adp_constraints=adp_constraints, h=h, u_indep=u_indep) print >> out, "curvs_indep_fin:", list(curvs_indep_fin) curvs_indep_ana = adp_constraints.independent_curvatures( all_curvatures=curvs_ana) print >> out, "curvs_indep_ana:", list(curvs_indep_ana) compare_derivatives(curvs_indep_ana, curvs_indep_fin) # curvs_indep_mm = None if (str(space_group_info) == "P 1 2 1"): assert list(adp_constraints.independent_indices) == [0,1,2,4] curvs_indep_mm = p2_curv(h, u_star) elif (str(space_group_info) == "P 4"): assert list(adp_constraints.independent_indices) == [1,2] curvs_indep_mm = p4_curv(h, u_star) elif (str(space_group_info) in ["P 3", "P 6"]): assert list(adp_constraints.independent_indices) == [2,3] curvs_indep_mm = p3_curv(h, u_star) elif (str(space_group_info) == "P 2 3"): assert list(adp_constraints.independent_indices) == [2] curvs_indep_mm = p23_curv(h, u_star) if (curvs_indep_mm is not None): curvs_indep_mm = flex.double( curvs_indep_mm).matrix_symmetric_as_packed_u() print >> out, "curvs_indep_mm:", list(curvs_indep_mm) compare_derivatives(curvs_indep_ana, curvs_indep_mm)
def test_AnomalousPlotter(): "Make a larger array to allow all plots to be made" cs = crystal.symmetry(space_group_symbol="P1", unit_cell=(6, 6, 6, 90, 90, 90)) ms = miller.build_set(cs, anomalous_flag=True, d_min=1.0) indices = ms.indices() new_indices = flex.miller_index(list(indices) * 10) new_ms = miller.set(crystal_symmetry=cs, indices=new_indices, anomalous_flag=True) data = flex.double(float(random.randrange(1, 100)) for _ in range(new_ms.size())) iobs = miller.array(new_ms, data, sigmas=data) iobs.change_symmetry(space_group_symbol="P222", merge_non_unique=False) iobs.set_info(miller.array_info(source="DIALS", source_type="reflection_tables")) iobs.set_observation_type_xray_intensity() plotter = AnomalousPlotter(iobs) d = plotter.make_plots() expected = ["normal_distribution_plot_highres", "anom_correl_plot"] keys = d.keys() for k in expected: assert k in keys assert d[k]["data"][0]["x"] # check some data there plotter = AnomalousPlotter(iobs, strong_cutoff=3.0) d = plotter.make_plots() expected = [ "anom_correl_plot", "anom_scatter_plot_lowres", "normal_distribution_plot_lowres", ] keys = list(d.keys()) for k in expected: assert k in keys assert d[k]["data"][0]["x"] # check some data there
def generate_mtz_files(space_group_info, anomalous_flag): crystal_symmetry = crystal.symmetry( unit_cell=space_group_info.any_compatible_unit_cell(volume=1000), space_group_info=space_group_info) miller_set = miller.build_set( crystal_symmetry=crystal_symmetry, anomalous_flag=anomalous_flag, d_min=1) miller_array = miller.array( miller_set=miller_set, data=flex.random_double(size=miller_set.indices().size())) miller_array_p1 = miller_array.expand_to_p1() miller_arrays = [] file_names = [] subgrs = subgroups.subgroups(space_group_info).groups_parent_setting() for i_subgroup, subgroup in enumerate(subgrs): subgroup_miller_array = miller_array_p1.customized_copy( space_group_info=sgtbx.space_group_info(group=subgroup)) \ .merge_equivalents() \ .array() \ .as_reference_setting() \ .set_observation_type_xray_intensity() file_name = "tmp_refl_stats%d.mtz" % i_subgroup mtz_object = subgroup_miller_array.as_mtz_dataset( column_root_label="FOBS").mtz_object().write(file_name=file_name) miller_arrays.append( subgroup_miller_array.f_sq_as_f().expand_to_p1().map_to_asu()) file_names.append(file_name) return miller_arrays, file_names
def exercise_02(): for file_name, input_model in [("m_good.pdb", model_good), ("m_bad.pdb", model_bad)]: tmp_f = open(file_name, "w") tmp_f.write(input_model) tmp_f.close() xrs_exact = iotbx.pdb.pdb_input( file_name="m_good.pdb").xray_structure_simple() model = mmtbx.model.manager(model_input=iotbx.pdb.input( file_name="m_bad.pdb")) xrs_part = model.get_xray_structure() miller_set = miller.build_set( crystal_symmetry=xrs_exact.crystal_symmetry(), anomalous_flag=False, d_min=0.6) f_obs = abs( miller_set.structure_factors_from_scatterers( xray_structure=xrs_exact, algorithm="direct", cos_sin_table=False).f_calc()) sf_par = mmtbx.f_model.sf_and_grads_accuracy_master_params.extract() sf_par.algorithm = "direct" sf_par.cos_sin_table = False fmodel = mmtbx.f_model.manager(xray_structure=xrs_part, sf_and_grads_accuracy_params=sf_par, target_name="ls_wunit_k1", f_obs=f_obs) # out = StringIO() params = find_hydrogens.all_master_params().extract() params.map_cutoff = 6.5 find_hydrogens.run(fmodel=fmodel, model=model, log=out, params=params)
def exercise(xray_structure, anomalous_flag, max_n_indices, out): xray_structure.show_summary(f=out).show_scatterers(f=out) miller_set = miller.build_set( crystal_symmetry=xray_structure, anomalous_flag=anomalous_flag, d_min=max(1, min(xray_structure.unit_cell().parameters()[:3]) / 2.5)) n_indices = miller_set.indices().size() if (n_indices > max_n_indices): miller_set = miller_set.select( flex.random_size_t(size=max_n_indices) % n_indices) sf = structure_factors(xray_structure=xray_structure, miller_set=miller_set) f_calc = miller_set.structure_factors_from_scatterers( xray_structure=xray_structure, algorithm="direct", cos_sin_table=False).f_calc() f_calc.show_summary(f=out) assert approx_equal(sf.fs(), f_calc.data()) f_obs = miller_set.array(data=flex.abs(sf.fs())) noise_fin = compare_analytical_and_finite(f_obs=f_obs, xray_structure=xray_structure, gradients_should_be_zero=True, eps=1.e-5, out=out) compare_analytical_and_finite(f_obs=f_obs.customized_copy( data=f_obs.data() * (flex.random_double(size=f_obs.size()) + 0.5)), xray_structure=xray_structure, gradients_should_be_zero=False, eps=max(1.e-5, noise_fin), out=out)
def exercise_trigonometric_ff(): from math import cos, sin, pi sgi = sgtbx.space_group_info("P1") cs = sgi.any_compatible_crystal_symmetry(volume=1000) miller_set = miller.build_set(cs, anomalous_flag=False, d_min=1) miller_set = miller_set.select(flex.random_double(miller_set.size()) < 0.2) for i in range(5): sites = flex.random_double(9) x1, x2, x3 = (matrix.col(sites[:3]), matrix.col(sites[3:6]), matrix.col(sites[6:])) xs = xray.structure(crystal.special_position_settings(cs)) for x in (x1, x2, x3): sc = xray.scatterer(site=x, scattering_type="const") sc.flags.set_grad_site(True) xs.add_scatterer(sc) f_sq = structure_factors.f_calc_modulus_squared(xs) for h in miller_set.indices(): h = matrix.col(h) phi1, phi2, phi3 = 2 * pi * h.dot(x1), 2 * pi * h.dot( x2), 2 * pi * h.dot(x3) fc_mod_sq = 3 + 2 * (cos(phi1 - phi2) + cos(phi2 - phi3) + cos(phi3 - phi1)) g = [] g.extend(-2 * (sin(phi1 - phi2) - sin(phi3 - phi1)) * 2 * pi * h) g.extend(-2 * (sin(phi2 - phi3) - sin(phi1 - phi2)) * 2 * pi * h) g.extend(-2 * (sin(phi3 - phi1) - sin(phi2 - phi3)) * 2 * pi * h) grad_fc_mod_sq = g f_sq.linearise(h) assert approx_equal(f_sq.observable, fc_mod_sq) assert approx_equal(f_sq.grad_observable, grad_fc_mod_sq)
def exercise(flags, space_group_info, n_sampled): symbol = space_group_info.type().hall_symbol() print symbol, if flags.fix_seed: random.seed(0) if not flags.include_high_symmetry: if space_group_info.group().order_p() > 8: if len(symbol) > 15: print print " [ Omitted, rerun with --include_high_symmetry to override ]" return print n = int(flags.repeats) if n == 0: n = 1 progress = progress_displayed_as_fraction(n) for i in xrange(n): xs = random_structure.xray_structure( space_group_info=space_group_info, elements=['C']*5 + ['O']*2 + ['N'], use_u_iso=True, random_u_iso=True, random_u_iso_scale=0.04, use_u_aniso=False) f_c = miller.build_set( xs, anomalous_flag=False, d_min=0.8 ).structure_factors_from_scatterers( xs, algorithm='direct').f_calc() f_o = f_c.as_amplitude_array() f_c_in_p1 = f_c.expand_to_p1() exercise_value(f_c_in_p1, f_o, flags, n_sampled) exercise_gradient(f_c_in_p1, f_o, flags, n_sampled) progress.advance() progress.done()
def run(): structure = random_structure.xray_structure( sgtbx.space_group_info("P21/c"), elements=["Si"] * 10, volume_per_atom=18.6, min_distance=1.2, general_positions_only=False) miller_set_f_obs = miller.build_set(crystal_symmetry=structure, anomalous_flag=True, d_min=0.8) f_obs = miller_set_f_obs.structure_factors_from_scatterers( xray_structure=structure, algorithm="direct").f_calc() fft_map = f_obs.fft_map(symmetry_flags=maptbx.use_space_group_symmetry) padded = fft_map.real_map() unpadded = fft_map.real_map_unpadded() # copy unpadded_1d = unpadded.as_1d() # 1D view => in-place mmm = flex.min_max_mean_double(unpadded_1d) for delta in ((mmm.min + mmm.mean) / 2, mmm.mean, (mmm.mean + mmm.max) / 2): # in-place charge flipping ab_initio.ext.flip_charges_in_place(padded, delta) # same but on an unpadded copy using the flex tools flipped_selection = unpadded_1d < delta flipped = unpadded_1d.select(flipped_selection) flipped *= -1 unpadded_1d.set_selected(flipped_selection, flipped) assert approx_equal(padded, unpadded, 1e-15) print(format_cpu_times())
def exercise_get_experimental_phases(): crystal_symmetry = crystal.symmetry(unit_cell=(30, 31, 32, 85, 95, 100), space_group_symbol="P 1") miller_set = miller.build_set(crystal_symmetry=crystal_symmetry, anomalous_flag=False, d_min=3) input_array = miller_set.array( data=flex.hendrickson_lattman(miller_set.indices().size(), (0, 0, 0, 0))) mtz_dataset = input_array.as_mtz_dataset(column_root_label="P") mtz_dataset.mtz_object().write("tmp.mtz") reflection_files = [ reflection_file_reader.any_reflection_file(file_name="tmp.mtz") ] err = StringIO() reflection_file_srv = reflection_file_server( crystal_symmetry=crystal_symmetry, force_symmetry=True, reflection_files=reflection_files, err=err) experimental_phases = reflection_file_srv.get_experimental_phases( file_name=None, labels=None, ignore_all_zeros=False, parameter_scope="experimental_phases") assert str(experimental_phases.info()) == "tmp.mtz:PA,PB,PC,PD" try: reflection_file_srv.get_experimental_phases( file_name=None, labels=None, ignore_all_zeros=True, parameter_scope="experimental_phases") except Sorry, e: assert str(e) == "No array of experimental phases found." assert err.getvalue() == """\
def exercise_get_experimental_phases(): crystal_symmetry = crystal.symmetry( unit_cell=(30,31,32,85,95,100), space_group_symbol="P 1") miller_set = miller.build_set( crystal_symmetry=crystal_symmetry, anomalous_flag=False, d_min=3) input_array = miller_set.array( data=flex.hendrickson_lattman(miller_set.indices().size(), (0,0,0,0))) mtz_dataset = input_array.as_mtz_dataset(column_root_label="P") mtz_dataset.mtz_object().write("tmp.mtz") reflection_files = [reflection_file_reader.any_reflection_file( file_name="tmp.mtz")] err = StringIO() reflection_file_srv = reflection_file_server( crystal_symmetry=crystal_symmetry, force_symmetry=True, reflection_files=reflection_files, err=err) experimental_phases = reflection_file_srv.get_experimental_phases( file_name=None, labels=None, ignore_all_zeros=False, parameter_scope="experimental_phases") assert str(experimental_phases.info()) == "tmp.mtz:PA,PB,PC,PD" try: reflection_file_srv.get_experimental_phases( file_name=None, labels=None, ignore_all_zeros=True, parameter_scope="experimental_phases") except Sorry, e: assert str(e) == "No array of experimental phases found." assert err.getvalue() == """\
def predict_angles(pdb_path, resolution, A): """ Predict the tilt angle at which each reflection will be observed. Here a positive tilt angle corresponds to images with a +y coordinate. Reflections that lie in the missing wedge are excluded. Inputs: ------- pdb_path: path to reference PDB file resolution: high-resolution limit of structure factors A: crystal setting matrix Outputs: -------- hkl_t: dict with keys as Millers and values as tilt angles """ # predict coordinates of all reflections in reciprocal pixels sg_symbol, sg_no, cell, cs = cctbx_utils.unit_cell_info(pdb_path) hkl = np.array( miller.build_set(crystal_symmetry=cs, anomalous_flag=True, d_min=resolution).expand_to_p1().indices()) qvecs = np.inner(A, np.squeeze(hkl)).T # predict tilt angle from associated coordinates t = np.rad2deg(np.arctan2(qvecs[:, 1], qvecs[:, 0])) t[(t > 90) & (t <= 180)] = utils.wraptopi(t[(t > 90) & (t < 180)] + 180.0) # shift q2 to q4 t[(t >= -180) & (t <= -90)] += 180.0 # shift q3 to q1 # generate a dict with keys as Millers and values as tilt angles hkl_t = OrderedDict((tuple(key), val) for key, val in zip(hkl, t)) return hkl_t
def exercise(flags, space_group_info, n_sampled): symbol = space_group_info.type().hall_symbol() print symbol, if flags.fix_seed: random.seed(0) if not flags.include_high_symmetry: if space_group_info.group().order_p() > 8: if len(symbol) > 15: print print " [ Omitted, rerun with --include_high_symmetry to override ]" return print n = int(flags.repeats) if n == 0: n = 1 progress = progress_displayed_as_fraction(n) for i in xrange(n): xs = random_structure.xray_structure(space_group_info=space_group_info, elements=['C'] * 5 + ['O'] * 2 + ['N'], use_u_iso=True, random_u_iso=True, random_u_iso_scale=0.04, use_u_aniso=False) f_c = miller.build_set(xs, anomalous_flag=False, d_min=0.8).structure_factors_from_scatterers( xs, algorithm='direct').f_calc() f_o = f_c.as_amplitude_array() f_c_in_p1 = f_c.expand_to_p1() exercise_value(f_c_in_p1, f_o, flags, n_sampled) exercise_gradient(f_c_in_p1, f_o, flags, n_sampled) progress.advance() progress.done()
def run(): structure = random_structure.xray_structure( sgtbx.space_group_info("P21/c"), elements=["Si"]*10, volume_per_atom=18.6, min_distance=1.2, general_positions_only=False) miller_set_f_obs = miller.build_set( crystal_symmetry=structure, anomalous_flag=True, d_min=0.8) f_obs = miller_set_f_obs.structure_factors_from_scatterers( xray_structure=structure, algorithm="direct").f_calc() fft_map = f_obs.fft_map(symmetry_flags=maptbx.use_space_group_symmetry) padded = fft_map.real_map() unpadded = fft_map.real_map_unpadded() # copy unpadded_1d = unpadded.as_1d() # 1D view => in-place mmm = flex.min_max_mean_double(unpadded_1d) for delta in ((mmm.min + mmm.mean)/2, mmm.mean, (mmm.mean + mmm.max)/2): # in-place charge flipping ab_initio.ext.flip_charges_in_place(padded, delta) # same but on an unpadded copy using the flex tools flipped_selection = unpadded_1d < delta flipped = unpadded_1d.select(flipped_selection) flipped *= -1 unpadded_1d.set_selected(flipped_selection, flipped) assert approx_equal(padded, unpadded, 1e-15) print format_cpu_times()
def tst_twin_completion(): uc = uctbx.unit_cell("40,40,70,90,90,90") xs = crystal.symmetry(unit_cell=uc, space_group="P1") miller_set = miller.build_set(crystal_symmetry=xs, anomalous_flag=False, d_min=3.0).map_to_asu() select = flex.bool(miller_set.indices().size(), True) select[300] = False miller_set_mod = miller_set.select(select) # make sure we threw away a reflection assert not miller_set_mod.indices().all_eq(miller_set.indices()) checker = xray.twin_completion(miller_set_mod.indices(), xs.space_group(), False, [0, 1, 0, 1, 0, 0, 0, 0, -1]) new_hkl = checker.twin_complete() miller_set_mod = miller_set_mod.customized_copy(indices=new_hkl) miller_set_mod = miller_set_mod.map_to_asu() a, b = miller_set_mod.common_sets(miller_set) assert a.indices().size() == miller_set_mod.indices().size() assert a.indices().size() == miller_set.indices().size() assert miller_set_mod.is_unique_set_under_symmetry() checker = xray.twin_completion(miller_set.indices(), xs.space_group(), False, [0, 1, 0, 1, 0, 0, 0, 0, -1]) basic_flags = miller_set.generate_r_free_flags_basic() lattice_flags = miller_set.generate_r_free_flags_on_lattice_symmetry() assert not checker.check_free_flags( basic_flags.data()) # this should give False assert checker.check_free_flags( lattice_flags.data()) # this should give True selection_array = checker.get_free_model_selection(lattice_flags.indices(), lattice_flags.data()) assert selection_array.all_eq(lattice_flags.data())
def calculate_completeness(self, resolution_bin=None): """Calculate the completeness of observations in a named resolution bin.""" if resolution_bin is None: resolution_range = self._mf.get_resolution_range() hkl_list = list(self._merged_reflections) else: resolution_range = self._resolution_ranges[resolution_bin] hkl_list = self._hkl_ranges[resolution_bin] uc = self._mf.get_unit_cell() sg = self._mf.get_space_group() dmin = min(resolution_range) dmax = max(resolution_range) cs = crystal_symmetry(unit_cell=uc, space_group=sg) hkl_calc = [ hkl for hkl in build_set(cs, False, d_min=dmin, d_max=dmax).indices() ] # remove systematically absent reflections hkl_list = [hkl for hkl in itertools.ifilterfalse(sg.is_sys_absent, hkl_list)] return float(len(hkl_list)) / float(len(hkl_calc))
def data_for_error_model_test(background_variance=1, multiplicity=100, b=0.05, a=1.0): """Model a set of poisson-distributed observations on a constant-variance background.""" ## First create a miller array of observations (in asu) from cctbx import miller from cctbx import crystal ms = miller.build_set( crystal_symmetry=crystal.symmetry(space_group_symbol="P212121", unit_cell=(12, 12, 25, 90, 90, 90)), anomalous_flag=False, d_min=1.0, ) assert ms.size() == 2150 mean_intensities = 5.0 * (ms.d_spacings().data()**4) # ^ get a good range of intensities, with high intensity at low # miller index, mean = 285.2, median = 13.4 # when applying b, use fact that I' - Imean = alpha(I - Imean), will # give the same distribution as sigma' = alpha sigma, # where alpha = (1 + (b^2 I)) ^ 0.5. i.e. this is the way to increase the # deviations of I-Imean and keep the same 'poisson' sigmas, such that the # sigmas need to be inflated by the error model with the given a, b. import scitbx from scitbx.random import variate, poisson_distribution # Note, if a and b both set, probably not quite right, but okay for small # a and b for the purpose of a test scitbx.random.set_random_seed(0) intensities = flex.int() variances = flex.double() miller_index = flex.miller_index() for i, idx in zip(mean_intensities, ms.indices()): g = variate(poisson_distribution(mean=i)) for _ in range(multiplicity): intensity = next(g) if b > 0.0: alpha = (1.0 + (b**2 * intensity))**0.5 intensities.append( int((alpha * intensity) + ((1.0 - alpha) * i))) else: intensities.append(intensity) variances.append((intensity + background_variance) / (a**2)) miller_index.append(idx) reflections = flex.reflection_table() reflections["intensity"] = intensities.as_double() reflections["variance"] = variances.as_double() reflections["miller_index"] = miller_index reflections["inverse_scale_factor"] = flex.double(intensities.size(), 1.0) reflections["id"] = flex.int(intensities.size(), 1) return reflections
def exercise_2(): symmetry = crystal.symmetry( unit_cell=(5.67, 10.37, 10.37, 90, 135.49, 90), space_group_symbol="C2") structure = xray.structure(crystal_symmetry=symmetry) atmrad = flex.double() xyzf = flex.vec3_double() for k in xrange(100): scatterer = xray.scatterer( site = ((1.+k*abs(math.sin(k)))/1000.0, (1.+k*abs(math.cos(k)))/1000.0, (1.+ k)/1000.0), scattering_type = "C") structure.add_scatterer(scatterer) atmrad.append(van_der_waals_radii.vdw.table[scatterer.element_symbol()]) xyzf.append(scatterer.site) miller_set = miller.build_set( crystal_symmetry=structure, d_min=1.0, anomalous_flag=False) step = 0.5 crystal_gridding = maptbx.crystal_gridding( unit_cell=structure.unit_cell(), step=step) nxyz = crystal_gridding.n_real() shrink_truncation_radius = 1.0 solvent_radius = 1.0 m1 = around_atoms( structure.unit_cell(), structure.space_group().order_z(), structure.sites_frac(), atmrad, nxyz, solvent_radius, shrink_truncation_radius) assert m1.solvent_radius == 1 assert m1.shrink_truncation_radius == 1 assert flex.max(m1.data) == 1 assert flex.min(m1.data) == 0 assert m1.data.size() == m1.data.count(1) + m1.data.count(0) m2 = mmtbx.masks.bulk_solvent( xray_structure=structure, gridding_n_real=nxyz, ignore_zero_occupancy_atoms = False, solvent_radius=solvent_radius, shrink_truncation_radius=shrink_truncation_radius) assert m2.data.all_eq(m1.data) m3 = mmtbx.masks.bulk_solvent( xray_structure=structure, grid_step=step, ignore_zero_occupancy_atoms = False, solvent_radius=solvent_radius, shrink_truncation_radius=shrink_truncation_radius) assert m3.data.all_eq(m1.data) f_mask2 = m2.structure_factors(miller_set=miller_set) f_mask3 = m3.structure_factors(miller_set=miller_set) assert approx_equal(f_mask2.data(), f_mask3.data()) assert approx_equal(flex.sum(flex.abs(f_mask3.data())), 1095.17999134)
def exercise_sampled_model_density_1(): import iotbx.pdb pdb_str1 = """ CRYST1 10.000 10.000 10.000 90.00 90.00 90.00 P 1 ATOM 1 CB PHE A 1 5.000 5.000 5.000 1.00 15.00 C ANISOU 1 CB PHE A 1 900 2900 100 0 0 0 C TER END """ pdb_str2 = """ CRYST1 10.000 10.000 10.000 90.00 90.00 90.00 P 1 ATOM 1 CB PHE A 1 5.000 5.000 5.000 1.00 15.00 C TER END """ # for pdb_str in [pdb_str1, pdb_str2]: print pdb_inp = iotbx.pdb.input(source_info=None, lines=pdb_str) xrs = pdb_inp.xray_structure_simple() # crystal_gridding = maptbx.crystal_gridding( unit_cell=xrs.unit_cell(), space_group_info=xrs.space_group_info(), symmetry_flags=maptbx.use_space_group_symmetry, step=0.1) m = mmtbx.real_space.sampled_model_density( xray_structure=xrs, n_real=crystal_gridding.n_real()).data() # max_index = [(i - 1) // 2 for i in crystal_gridding.n_real()] complete_set = miller.build_set( crystal_symmetry=xrs.crystal_symmetry(), anomalous_flag=False, max_index=max_index) indices = complete_set.indices() indices.append((0, 0, 0)) # complete_set = complete_set.customized_copy(indices=indices) f_obs_cmpl = complete_set.structure_factors_from_map( map=m, use_scale=True, anomalous_flag=False, use_sg=False) fc = complete_set.structure_factors_from_scatterers( xray_structure=xrs).f_calc() # f1 = abs(fc).data() f2 = abs(f_obs_cmpl).data() r = 200 * flex.sum(flex.abs(f1 - f2)) / flex.sum(f1 + f2) assert r < 0.5 print r # fft_map = miller.fft_map(crystal_gridding=crystal_gridding, fourier_coefficients=f_obs_cmpl) fft_map.apply_volume_scaling() m_ = fft_map.real_map_unpadded() print m.as_1d().min_max_mean().as_tuple() print m_.as_1d().min_max_mean().as_tuple() assert approx_equal(m.as_1d().min_max_mean().as_tuple(), m_.as_1d().min_max_mean().as_tuple(), 1.e-3) # Must be smaller!?
def exercise_real_space_refinement(verbose): if (verbose): out = sys.stdout else: out = StringIO() out_of_bounds_clamp = maptbx.out_of_bounds_clamp(0) out_of_bounds_raise = maptbx.out_of_bounds_raise() crystal_symmetry = crystal.symmetry(unit_cell=(10, 10, 10, 90, 90, 90), space_group_symbol="P 1") xray_structure = xray.structure(crystal_symmetry=crystal_symmetry, scatterers=flex.xray_scatterer([ xray.scatterer(label="C", site=(0, 0, 0)) ])) miller_set = miller.build_set(crystal_symmetry=crystal_symmetry, anomalous_flag=False, d_min=1) f_calc = miller_set.structure_factors_from_scatterers( xray_structure=xray_structure).f_calc() fft_map = f_calc.fft_map() fft_map.apply_sigma_scaling() real_map = fft_map.real_map_unpadded() #### unit_cell test delta_h = .005 basic_map = maptbx.basic_map( maptbx.basic_map_unit_cell_flag(), real_map, real_map.focus(), crystal_symmetry.unit_cell().orthogonalization_matrix(), out_of_bounds_clamp.as_handle(), crystal_symmetry.unit_cell()) testing_function_for_rsfit(basic_map, delta_h, xray_structure, out) ### non_symmetric test # minfrac = crystal_symmetry.unit_cell().fractionalize((-5, -5, -5)) maxfrac = crystal_symmetry.unit_cell().fractionalize((5, 5, 5)) gridding_first = [ifloor(n * b) for n, b in zip(fft_map.n_real(), minfrac)] gridding_last = [iceil(n * b) for n, b in zip(fft_map.n_real(), maxfrac)] data = maptbx.copy(real_map, gridding_first, gridding_last) # basic_map = maptbx.basic_map( maptbx.basic_map_non_symmetric_flag(), data, fft_map.n_real(), crystal_symmetry.unit_cell().orthogonalization_matrix(), out_of_bounds_clamp.as_handle(), crystal_symmetry.unit_cell()) testing_function_for_rsfit(basic_map, delta_h, xray_structure, out) ### asu test # minfrac = crystal_symmetry.unit_cell().fractionalize((0, 0, 0)) maxfrac = crystal_symmetry.unit_cell().fractionalize((10, 10, 10)) gridding_first = [ifloor(n * b) for n, b in zip(fft_map.n_real(), minfrac)] gridding_last = [iceil(n * b) for n, b in zip(fft_map.n_real(), maxfrac)] data = maptbx.copy(real_map, gridding_first, gridding_last) # basic_map = maptbx.basic_map( maptbx.basic_map_asu_flag(), data, crystal_symmetry.space_group(), crystal_symmetry.direct_space_asu().as_float_asu(), real_map.focus(), crystal_symmetry.unit_cell().orthogonalization_matrix(), out_of_bounds_clamp.as_handle(), crystal_symmetry.unit_cell(), 0.5, True) testing_function_for_rsfit(basic_map, delta_h, xray_structure, out)
def predict_miller_indices(self): crystal_symmetry = crystal.symmetry( unit_cell=self.xparamdict['unit_cell'], space_group_symbol=self.xparamdict['spacegroup']) d_min = self.inData.get('resolution', float('3')) d_max = None miller_set = miller.build_set(crystal_symmetry, anomalous_flag=True, d_min=d_min, d_max=d_max) return miller_set.indices()
def exercise_basic (verbose=False) : symm = crystal.symmetry( space_group_info=sgtbx.space_group_info("P212121"), unit_cell=uctbx.unit_cell((6,7,8,90,90,90))) set1 = miller.build_set( crystal_symmetry=symm, anomalous_flag=True, d_min=1.0) assert (set1.indices().size() == 341) data0 = flex.double(set1.indices().size(), 100.) sigmas1 = flex.double(set1.indices().size(), 4.) for i in range(10) : data0[2+i*30] = -1 for i in range(10) : data0[5+i*30] = 7.5 array0 = set1.array(data=data0, sigmas=sigmas1) array0.set_observation_type_xray_intensity() flags = array0.generate_r_free_flags( use_lattice_symmetry=True).average_bijvoet_mates() mtz0 = array0.as_mtz_dataset(column_root_label="I-obs") mtz0.add_miller_array(flags, column_root_label="R-free-flags") mtz0.mtz_object().write("tst_data.mtz") # convert intensities to amplitudes new_phil = libtbx.phil.parse(""" mtz_file { output_file = tst1.mtz crystal_symmetry.space_group = P212121 crystal_symmetry.unit_cell = 6,7,8,90,90,90 miller_array { file_name = tst_data.mtz labels = I-obs(+),SIGI-obs(+),I-obs(-),SIGI-obs(-) output_labels = I-obs(+) SIGI-obs(+) I-obs(-) SIGI-obs(-) } miller_array { file_name = tst_data.mtz labels = R-free-flags output_labels = R-free-flags } }""") params = master_phil.fetch(source=new_phil).extract() log = sys.stdout if (not verbose) : log = null_out() def run_and_reload (params, file_name) : p = reflection_file_editor.process_arrays(params, log=log) p.finish() mtz_in = file_reader.any_file(file_name) miller_arrays = mtz_in.file_object.as_miller_arrays() return miller_arrays params.mtz_file.miller_array[0].output_as = "amplitudes" try : miller_arrays = run_and_reload(params, "tst1.mtz") except Sorry, e : assert ("inconsistent with" in str(e)), str(e)
def exercise_basic(verbose=False): symm = crystal.symmetry(space_group_info=sgtbx.space_group_info("P212121"), unit_cell=uctbx.unit_cell((6, 7, 8, 90, 90, 90))) set1 = miller.build_set(crystal_symmetry=symm, anomalous_flag=True, d_min=1.0) assert (set1.indices().size() == 341) data0 = flex.double(set1.indices().size(), 100.) sigmas1 = flex.double(set1.indices().size(), 4.) for i in range(10): data0[2 + i * 30] = -1 for i in range(10): data0[5 + i * 30] = 7.5 array0 = set1.array(data=data0, sigmas=sigmas1) array0.set_observation_type_xray_intensity() flags = array0.generate_r_free_flags( use_lattice_symmetry=True).average_bijvoet_mates() mtz0 = array0.as_mtz_dataset(column_root_label="I-obs") mtz0.add_miller_array(flags, column_root_label="R-free-flags") mtz0.mtz_object().write("tst_data.mtz") # convert intensities to amplitudes new_phil = libtbx.phil.parse(""" mtz_file { output_file = tst1.mtz crystal_symmetry.space_group = P212121 crystal_symmetry.unit_cell = 6,7,8,90,90,90 miller_array { file_name = tst_data.mtz labels = I-obs(+),SIGI-obs(+),I-obs(-),SIGI-obs(-) output_labels = I-obs(+) SIGI-obs(+) I-obs(-) SIGI-obs(-) } miller_array { file_name = tst_data.mtz labels = R-free-flags output_labels = R-free-flags } }""") params = master_phil.fetch(source=new_phil).extract() log = sys.stdout if (not verbose): log = null_out() def run_and_reload(params, file_name): p = reflection_file_editor.process_arrays(params, log=log) p.finish() mtz_in = file_reader.any_file(file_name) miller_arrays = mtz_in.file_object.as_miller_arrays() return miller_arrays params.mtz_file.miller_array[0].output_as = "amplitudes" try: miller_arrays = run_and_reload(params, "tst1.mtz") except Sorry, e: assert ("inconsistent with" in str(e)), str(e)
def exercise_4(grid_step, radius, shell, a, b, d_min, volume_per_atom, use_weights, optimize_cutoff_radius, scatterer_chemical_type = "C"): xray_structure = random_structure.xray_structure( space_group_info = sgtbx.space_group_info("P 1"), elements = ((scatterer_chemical_type)*1), volume_per_atom = volume_per_atom, min_distance = 1.5, general_positions_only = True) custom_dict = \ {scatterer_chemical_type: eltbx.xray_scattering.gaussian([a],[b])} xray_structure.scattering_type_registry(custom_dict = custom_dict) miller_set = miller.build_set( crystal_symmetry = xray_structure.crystal_symmetry(), anomalous_flag = False, d_min = d_min, d_max = None) f_calc = miller_set.structure_factors_from_scatterers( xray_structure = xray_structure, algorithm = "direct", cos_sin_table = False, exp_table_one_over_step_size = False).f_calc() fft_map = f_calc.fft_map(grid_step = grid_step, symmetry_flags = None) fft_map = fft_map.apply_volume_scaling() site_frac = xray_structure.sites_frac() m = fft_map.real_map_unpadded() amm = abs(flex.max(m)) r = abs(amm-abs(m.value_at_closest_grid_point(site_frac[0]))) / amm assert r < 0.15, r around_atom_obj_ = mmtbx.real_space.around_atom( unit_cell = xray_structure.unit_cell(), map_data = fft_map.real_map_unpadded(), radius = radius, shell = shell, site_frac = list(xray_structure.sites_frac())[0]) data = around_atom_obj_.data() dist = around_atom_obj_.distances() approx_obj_ = maptbx.one_gaussian_peak_approximation( data_at_grid_points = data, distances = dist, use_weights = use_weights, optimize_cutoff_radius = optimize_cutoff_radius) assert approx_equal(approx_obj_.a_reciprocal_space(), 6.0, 0.1) assert approx_equal(approx_obj_.b_reciprocal_space(), 3.0, 0.1) assert approx_obj_.gof() < 1.0 assert approx_obj_.cutoff_radius() < radius
def exercise_analyze_resolution_limits(): for x in range(1, 231): sg = sgtbx.space_group_info(number=x) #sg = sgtbx.space_group_info("P222") uc = sg.any_compatible_unit_cell(80000) ms = miller.build_set(crystal_symmetry=crystal.symmetry( space_group_info=sg, unit_cell=uc), anomalous_flag=True, d_min=1.5) arl = ds.analyze_resolution_limits(ms) if (x > 2): assert (arl.max_d_min_delta() < 0.1)
def exercise( space_group_info, use_u_aniso, anomalous_flag, max_n_indices=5, verbose=0): if (not verbose): out = StringIO() else: out = sys.stdout for n_scatterers in xrange(3,3+1): for i_trial in xrange(1): xray_structure = random_structure.xray_structure( space_group_info=space_group_info, elements=["const"]*n_scatterers, volume_per_atom=100, general_positions_only=True, random_f_prime_d_min=1, random_f_double_prime=anomalous_flag, use_u_aniso = use_u_aniso, use_u_iso = (not use_u_aniso), random_u_iso=True, random_u_iso_scale=0.3, random_occupancy=True) xray_structure.show_summary(f=out).show_scatterers(f=out) miller_set = miller.build_set( crystal_symmetry=xray_structure, anomalous_flag=anomalous_flag, d_min=max(1, min(xray_structure.unit_cell().parameters()[:3])/2.5)) n_indices = miller_set.indices().size() if (n_indices > max_n_indices): miller_set = miller_set.select( flex.random_size_t(size=max_n_indices) % n_indices) sf = structure_factors( xray_structure=xray_structure, miller_set=miller_set) f_calc = miller_set.structure_factors_from_scatterers( xray_structure=xray_structure, algorithm="direct", cos_sin_table=False).f_calc() f_calc.show_summary(f=out) assert approx_equal(sf.fs(), f_calc.data()) f_obs = miller_set.array(data=flex.abs(sf.fs())) compare_analytical_and_finite( f_obs=f_obs, xray_structure=xray_structure, out=out) compare_analytical_and_finite( f_obs=f_obs.customized_copy( data=f_obs.data()*(flex.random_double(size=f_obs.size())+0.5)), xray_structure=xray_structure, out=out)
def run(refine, target, residues_per_window=1, d_min=2): """ Makes sure this actually work: 'real-space' ADP refinement by converting the map into 'Fobs' and then actually doing usual reciprocal-space refinement. Surprisingly this converges to R=0 exactly regardless the resolution. ML does not work perhaps becase starting point is too far. """ pdb_inp_poor = iotbx.pdb.input(source_info=None, lines=pdb_poor_adp_str) pdb_inp_poor.write_pdb_file(file_name="poor.pdb") ph = pdb_inp_poor.construct_hierarchy() ph.atoms().reset_i_seq() xrs_poor = pdb_inp_poor.xray_structure_simple() xrs_answer = iotbx.pdb.input(source_info=None, lines=pdb_answer_str).xray_structure_simple() #### f_obs = xrs_answer.structure_factors(d_min=d_min, algorithm="direct").f_calc() mtz_dataset = f_obs.as_mtz_dataset(column_root_label="F-calc") fc = abs(f_obs) fft_map = f_obs.fft_map(resolution_factor=0.25) fft_map.apply_volume_scaling() map_data = fft_map.real_map_unpadded() complete_set = miller.build_set(crystal_symmetry=xrs_answer.crystal_symmetry(), anomalous_flag=False, d_min=d_min) f_obs = complete_set.structure_factors_from_map(map=map_data, use_scale=True, anomalous_flag=False, use_sg=True) f_obs = abs(f_obs) ### mtz_dataset.add_miller_array(miller_array=f_obs, column_root_label="F-obs") mtz_dataset.add_miller_array(miller_array=f_obs.generate_r_free_flags(), column_root_label="R-free-flags") mtz_object = mtz_dataset.mtz_object() mtz_object.write(file_name="data.mtz") ### sfp = mmtbx.f_model.sf_and_grads_accuracy_master_params.extract() sfp.algorithm = "direct" fmodel = mmtbx.f_model.manager(sf_and_grads_accuracy_params=sfp, xray_structure=xrs_poor, f_obs=f_obs) fmodel.show() fmodel.update(target_name=target) print "Initial r_work=%6.4f r_free=%6.4f" % (fmodel.r_work(), fmodel.r_free()) selections = ph.chunk_selections(residues_per_chunk=residues_per_window) rr = mmtbx.refinement.group.manager( fmodel=fmodel, selections=selections, max_number_of_iterations=50, number_of_macro_cycles=25, convergence_test=True, run_finite_differences_test=False, refine_adp=True, use_restraints=True, ) print "After refinement r_work=%6.4f r_free=%6.4f" % (fmodel.r_work(), fmodel.r_free()) ph.adopt_xray_structure(fmodel.xray_structure) ph.write_pdb_file(file_name="reciprocal_space_refined_group_adp.pdb", crystal_symmetry=f_obs.crystal_symmetry()) r_work = fmodel.r_work() * 100 assert r_work < 0.1, [r_work, target]
def exercise_least_squares_residual(): crystal_symmetry = crystal.symmetry(unit_cell=(6, 3, 8, 90, 90, 90), space_group_symbol="P222") miller_set = miller.build_set(crystal_symmetry=crystal_symmetry, anomalous_flag=False, d_min=0.7) f_obs = miller_set.array(data=flex.random_double(miller_set.size()), sigmas=flex.random_double(miller_set.size()) * 0.05) ls = xray.least_squares_residual( f_obs, use_sigmas_as_weights=True, )
def test_dano_over_sigdano(): """Create anomalous difference data and check the calculated value.""" ms = miller.build_set( crystal_symmetry=crystal.symmetry(space_group_symbol="P222", unit_cell=(6, 6, 6, 90, 90, 90)), anomalous_flag=True, d_min=5.0, ).expand_to_p1() ma = miller.array(ms, data=flex.double([1, 2, 1, 3, 1, 4]), sigmas=flex.double(6, 1)) # differences are (1, 2, 3) i.e. mean 2, sigmas (sqrt2, sqrt2, sqrt2) assert dano_over_sigdano(ma) == pytest.approx(2**0.5)
def exercise_util(): miller_set = miller.build_set( crystal.symmetry(unit_cell=(10, 10, 10, 90, 90, 90), space_group_symbol="P1"), d_min=1.5, anomalous_flag=True ) f_obs = miller_set.array(data=flex.double(miller_set.size(), 1.0), sigmas=flex.double(miller_set.size(), 0.1)) flags = f_obs.generate_r_free_flags() mtz_dataset = f_obs.as_mtz_dataset(column_root_label="F") mtz_dataset.add_miller_array(flags, column_root_label="FreeR_flag") mtz_dataset.mtz_object().write("tst_mtz_cutoff.mtz") mtz.cutoff_data("tst_mtz_cutoff.mtz", 2.5) mtz_in = mtz.object(file_name="tst_mtz_cutoff.mtz") ma = mtz_in.as_miller_arrays() assert approx_equal(ma[0].d_min(), 2.5)
def exercise_2(): symmetry = crystal.symmetry(unit_cell=(5.67, 10.37, 10.37, 90, 135.49, 90), space_group_symbol="C2") structure = xray.structure(crystal_symmetry=symmetry) atmrad = flex.double() xyzf = flex.vec3_double() for k in xrange(100): scatterer = xray.scatterer(site=((1. + k * abs(math.sin(k))) / 1000.0, (1. + k * abs(math.cos(k))) / 1000.0, (1. + k) / 1000.0), scattering_type="C") structure.add_scatterer(scatterer) atmrad.append( van_der_waals_radii.vdw.table[scatterer.element_symbol()]) xyzf.append(scatterer.site) miller_set = miller.build_set(crystal_symmetry=structure, d_min=1.0, anomalous_flag=False) step = 0.5 crystal_gridding = maptbx.crystal_gridding(unit_cell=structure.unit_cell(), step=step) nxyz = crystal_gridding.n_real() shrink_truncation_radius = 1.0 solvent_radius = 1.0 m1 = around_atoms(structure.unit_cell(), structure.space_group().order_z(), structure.sites_frac(), atmrad, nxyz, solvent_radius, shrink_truncation_radius) assert m1.solvent_radius == 1 assert m1.shrink_truncation_radius == 1 assert flex.max(m1.data) == 1 assert flex.min(m1.data) == 0 assert m1.data.size() == m1.data.count(1) + m1.data.count(0) m2 = mmtbx.masks.bulk_solvent( xray_structure=structure, gridding_n_real=nxyz, ignore_zero_occupancy_atoms=False, solvent_radius=solvent_radius, shrink_truncation_radius=shrink_truncation_radius) assert m2.data.all_eq(m1.data) m3 = mmtbx.masks.bulk_solvent( xray_structure=structure, grid_step=step, ignore_zero_occupancy_atoms=False, solvent_radius=solvent_radius, shrink_truncation_radius=shrink_truncation_radius) assert m3.data.all_eq(m1.data) f_mask2 = m2.structure_factors(miller_set=miller_set) f_mask3 = m3.structure_factors(miller_set=miller_set) assert approx_equal(f_mask2.data(), f_mask3.data()) assert approx_equal(flex.sum(flex.abs(f_mask3.data())), 1095.17999134)
def exercise(space_group_info, use_u_aniso, anomalous_flag, max_n_indices=5, verbose=0): if (not verbose): out = StringIO() else: out = sys.stdout for n_scatterers in xrange(3, 3 + 1): for i_trial in xrange(1): xray_structure = random_structure.xray_structure( space_group_info=space_group_info, elements=["const"] * n_scatterers, volume_per_atom=100, general_positions_only=True, random_f_prime_d_min=1, random_f_double_prime=anomalous_flag, use_u_aniso=use_u_aniso, use_u_iso=(not use_u_aniso), random_u_iso=True, random_u_iso_scale=0.3, random_occupancy=True) xray_structure.show_summary(f=out).show_scatterers(f=out) miller_set = miller.build_set( crystal_symmetry=xray_structure, anomalous_flag=anomalous_flag, d_min=max( 1, min(xray_structure.unit_cell().parameters()[:3]) / 2.5)) n_indices = miller_set.indices().size() if (n_indices > max_n_indices): miller_set = miller_set.select( flex.random_size_t(size=max_n_indices) % n_indices) sf = structure_factors(xray_structure=xray_structure, miller_set=miller_set) f_calc = miller_set.structure_factors_from_scatterers( xray_structure=xray_structure, algorithm="direct", cos_sin_table=False).f_calc() f_calc.show_summary(f=out) assert approx_equal(sf.fs(), f_calc.data()) f_obs = miller_set.array(data=flex.abs(sf.fs())) compare_analytical_and_finite(f_obs=f_obs, xray_structure=xray_structure, out=out) compare_analytical_and_finite(f_obs=f_obs.customized_copy( data=f_obs.data() * (flex.random_double(size=f_obs.size()) + 0.5)), xray_structure=xray_structure, out=out)
def compute_unique_reflections(unit_cell, space_group, anomalous, high_resolution_limit, low_resolution_limit = None): '''Compute the list of unique reflections from the unit cell and space group.''' cs = crystal_symmetry(unit_cell = unit_cell, space_group = space_group) return [hkl for hkl in build_set(cs, anomalous, d_min = high_resolution_limit, d_max = low_resolution_limit).indices()]
def exercise_analyze_resolution_limits () : for x in range(1, 231) : sg = sgtbx.space_group_info(number=x) #sg = sgtbx.space_group_info("P222") uc = sg.any_compatible_unit_cell(80000) ms = miller.build_set( crystal_symmetry=crystal.symmetry( space_group_info=sg, unit_cell=uc), anomalous_flag=True, d_min=1.5) arl = ds.analyze_resolution_limits(ms) if (x > 2) : assert (arl.max_d_min_delta() < 0.1)
def exercise_least_squares_residual(): crystal_symmetry = crystal.symmetry( unit_cell=(6,3,8,90,90,90), space_group_symbol="P222") miller_set = miller.build_set( crystal_symmetry=crystal_symmetry, anomalous_flag=False, d_min=0.7) f_obs = miller_set.array( data=flex.random_double(miller_set.size()), sigmas=flex.random_double(miller_set.size())*0.05) ls = xray.least_squares_residual( f_obs, use_sigmas_as_weights=True, )
def exercise_split_unmerged () : import random random.seed(42) flex.set_random_seed(42) from cctbx import crystal base_set = miller.build_set( crystal_symmetry=crystal.symmetry( unit_cell=(10,10,10,90,90,90), space_group_symbol="P1"), d_min=1.6, anomalous_flag=False) indices = base_set.indices() assert (len(indices) == 510) unmerged_hkl = flex.miller_index() unmerged_data = flex.double() unmerged_sigmas = flex.double() redundancies = flex.size_t() # XXX grossly overengineered, but I wanted to get a realistic CC to make sure # the reflections are being split properly for i, hkl in enumerate(indices) : n_obs = min(8, 1 + i % 12) redundancies.append(n_obs) intensity_merged = (510 - i) + (510 % 27) for j in range(n_obs) : unmerged_hkl.append(hkl) intensity = intensity_merged + 20 * (510 % (7 * (j+1))) sigma = max(0.5, i % 10) unmerged_data.append(intensity) unmerged_sigmas.append(sigma) assert (unmerged_hkl.size() == 2877) unmerged_array = miller.set( crystal_symmetry=base_set, indices=unmerged_hkl, anomalous_flag=False).array(data=unmerged_data, sigmas=unmerged_sigmas) split = miller.split_unmerged( unmerged_indices=unmerged_hkl, unmerged_data=unmerged_data, unmerged_sigmas=unmerged_sigmas) assert (split.data_1.size() == split.data_2.size() == 467) cc = miller.compute_cc_one_half(unmerged_array) assert approx_equal(cc, 0.861, eps=0.001) unmerged_array.setup_binner(n_bins=10) unmerged_array.set_observation_type_xray_intensity() result = unmerged_array.cc_one_half(use_binning=True) assert approx_equal( result.data[1:-1], [0.549, 0.789, 0.843, 0.835, 0.863, 0.860, 0.893, 0.847, 0.875, 0.859], eps=0.05)
def __init__(self, map_data, xray_structure, d_min, atom_radius): self.d_min = d_min self.map_data = map_data self.atom_radius = atom_radius self.f_map_diff = None # XXX rudimentary left-over, remove later self.crystal_gridding = maptbx.crystal_gridding( #XXX Likewise, remove later unit_cell = xray_structure.unit_cell(), pre_determined_n_real = map_data.all(), space_group_info = xray_structure.space_group_info()) self.complete_set = miller.build_set( crystal_symmetry = xray_structure.crystal_symmetry(), anomalous_flag = False, d_min = d_min) self.miller_array = self.map_to_sf(map_data = self.map_data) self.miller_array_masked = self.update_miller_array_masked( xray_structure = xray_structure)
def exercise_automation_wrappers () : from iotbx.reflection_file_utils import process_raw_data, \ change_space_group, load_f_obs_and_r_free from cctbx import sgtbx from libtbx.test_utils import approx_equal mtz_file = "tmp_iotbx_reflection_file_utils.mtz" crystal_symmetry = crystal.symmetry( unit_cell=(10,11,12,90,95,90), space_group_symbol="P 2") miller_set = miller.build_set( crystal_symmetry=crystal_symmetry, anomalous_flag=True, d_min=1.5) n_obs = miller_set.indices().size() i_obs = miller_set.array( data=flex.random_double(size=n_obs)).set_observation_type_xray_intensity() i_obs = i_obs.customized_copy(sigmas=flex.sqrt(i_obs.data())) r_free_flags = miller_set.generate_r_free_flags() r_free_flags_partial = r_free_flags.select(flex.random_bool(n_obs, 0.9)) out = StringIO() processed = process_raw_data( obs=i_obs, r_free_flags=None, test_flag_value=None, log=out) assert ("""WARNING: R-free flags not supplied.""" in out.getvalue()) assert (processed.data_labels() == "F(+),SIGF(+),F(-),SIGF(-)") assert (processed.phase_labels() is None) assert (processed.flags_are_new()) out2 = StringIO() processed2 = process_raw_data( obs=i_obs, r_free_flags=r_free_flags_partial, test_flag_value=True, log=out2) assert ("""WARNING: R-free flags are incomplete""" in out2.getvalue()) assert (not processed2.flags_are_new()) assert (processed.n_obs() == processed2.n_obs()) processed.write_mtz_file(mtz_file, title="tst_iotbx", wavelength=0.9792) f_obs, r_free = load_f_obs_and_r_free(mtz_file) change_space_group(mtz_file, sgtbx.space_group_info("P21")) f_obs_new, r_free_new = load_f_obs_and_r_free(mtz_file) assert (f_obs_new.size() == f_obs.size() - 4) f_obs_new, r_free_new = load_f_obs_and_r_free(mtz_file, anomalous_flag=True) assert (str(f_obs_new.space_group_info()) == "P 1 21 1") assert (approx_equal(f_obs_new.info().wavelength, 0.9792))
def random_data(B_add=35, n_residues=585.0, d_min=3.5): unit_cell = uctbx.unit_cell( (81.0, 81.0, 61.0, 90.0, 90.0, 120.0) ) xtal = crystal.symmetry(unit_cell, " P 3 ") ## In P3 I do not have to worry about centrics or reflections with different ## epsilons. miller_set = miller.build_set( crystal_symmetry = xtal, anomalous_flag = False, d_min = d_min) ## Now make an array with d_star_sq values d_star_sq = miller_set.d_spacings().data() d_star_sq = 1.0/(d_star_sq*d_star_sq) asu = {"H":8.0*n_residues*1.0, "C":5.0*n_residues*1.0, "N":1.5*n_residues*1.0, "O":1.2*n_residues*1.0} scat_info = absolute_scaling.scattering_information( asu_contents = asu, fraction_protein=1.0, fraction_nucleic=0.0) scat_info.scat_data(d_star_sq) gamma_prot = scat_info.gamma_tot sigma_prot = scat_info.sigma_tot_sq ## The number of residues is multriplied by the Z of the spacegroup protein_total = sigma_prot * (1.0+gamma_prot) ## add a B-value of 35 please protein_total = protein_total*flex.exp(-B_add*d_star_sq/2.0) ## Now that has been done, ## We can make random structure factors normalised_random_intensities = \ random_transform.wilson_intensity_variate(protein_total.size()) random_intensities = normalised_random_intensities*protein_total*math.exp(6) std_dev = random_intensities*5.0/100.0 noise = random_transform.normal_variate(N=protein_total.size()) noise = noise*std_dev random_intensities=noise+random_intensities ## STuff the arrays in the miller array miller_array = miller.array(miller_set, data=random_intensities, sigmas=std_dev) miller_array=miller_array.set_observation_type( xray.observation_types.intensity()) miller_array = miller_array.f_sq_as_f() return (miller_array)
def exercise_3(grid_step, radius, shell, a, b, d_min, site_cart, buffer_layer): xray_structure = xray_structure_of_one_atom(site_cart = site_cart, buffer_layer = buffer_layer, a = a, b = b) miller_set = miller.build_set( crystal_symmetry = xray_structure.crystal_symmetry(), anomalous_flag = False, d_min = d_min, d_max = None) f_calc = miller_set.structure_factors_from_scatterers( xray_structure = xray_structure, algorithm = "direct", cos_sin_table = False, exp_table_one_over_step_size = False).f_calc() fft_map = f_calc.fft_map(grid_step = grid_step, symmetry_flags = None) fft_map = fft_map.apply_volume_scaling() site_frac = xray_structure.sites_frac() r = abs(abs(flex.max(fft_map.real_map_unpadded()))-\ abs(fft_map.real_map_unpadded().value_at_closest_grid_point(site_frac[0])))/\ abs(flex.max(fft_map.real_map_unpadded())) * 100.0 assert approx_equal(r, 0.0) around_atom_obj_ = mmtbx.real_space.around_atom( unit_cell = xray_structure.unit_cell(), map_data = fft_map.real_map_unpadded(), radius = radius, shell = shell, site_frac = list(xray_structure.sites_frac())[0]) data = around_atom_obj_.data() dist = around_atom_obj_.distances() approx_obj_ = maptbx.one_gaussian_peak_approximation( data_at_grid_points = data, distances = dist, use_weights = False, optimize_cutoff_radius = True) assert approx_equal(approx_obj_.a_reciprocal_space(), 6.0, 0.1) assert approx_equal(approx_obj_.b_reciprocal_space(), 3.0, 0.01) assert approx_obj_.gof() < 0.6 assert approx_obj_.cutoff_radius() < radius
def test_shift(space_group_info, d_min=0.8, grid_resolution_factor=0.48, max_prime=5, verbose=0): n = 12 // len(space_group_info.group()) or 1 target_structure = random_structure.xray_structure( space_group_info=space_group_info, elements=['C']*n, use_u_iso=False, use_u_aniso=False, ) f_target = miller.build_set( crystal_symmetry=target_structure, anomalous_flag=False, d_min=d_min ).structure_factors_from_scatterers( xray_structure=target_structure, algorithm="direct").f_calc() f_obs = abs(f_target) indices_in_p1 = miller.set.expand_to_p1(f_target) target_structure_in_p1 = target_structure.expand_to_p1() reference_translation = matrix.col((0.1, 0.2, 0.7)) structure_in_p1 = target_structure_in_p1.apply_shift(reference_translation) f_structure_in_p1 = indices_in_p1.structure_factors_from_scatterers( xray_structure=structure_in_p1, algorithm="direct").f_calc() symmetry_flags = translation_search.symmetry_flags( is_isotropic_search_model=False, have_f_part=False) gridding = f_target.crystal_gridding( symmetry_flags=symmetry_flags, resolution_factor=grid_resolution_factor, max_prime=max_prime).n_real() grid_tags = maptbx.grid_tags(gridding) for f_calc_in_p1 in (f_structure_in_p1,): peak_list = run_fast_nv1995( f_obs=f_obs, f_calc_fixed=None, f_calc_p1=f_calc_in_p1, symmetry_flags=symmetry_flags, gridding=gridding, grid_tags=grid_tags, verbose=verbose) assert peak_list.heights()[0] > 0.9 shift = matrix.col(peak_list.sites()[0]) assert f_target.space_group_info().is_allowed_origin_shift( shift + reference_translation, tolerance=0.04)
def complete_miller_set_with_lattice_symmetry(self, anomalous_flag, d_min, lattice_symmetry_max_delta=3): cb_op = self.change_of_basis_op_to_niggli_cell() niggli_cell = self.change_basis(cb_op) lattice_group = niggli_cell.lattice_symmetry_group( max_delta=lattice_symmetry_max_delta) from cctbx import crystal niggli_lattice_symmetry = crystal.symmetry( unit_cell=niggli_cell, space_group_info=lattice_group.info()) from cctbx import miller niggli_lattice_set = miller.build_set( crystal_symmetry=niggli_lattice_symmetry, anomalous_flag=anomalous_flag, d_min=d_min) return niggli_lattice_set.change_basis(cb_op.inverse())
def exercise_hkl () : #--- HKL crystal_symmetry = crystal.symmetry( unit_cell=(10,11,12,85,95,100), space_group_symbol="P 1") miller_set = miller.build_set( crystal_symmetry=crystal_symmetry, anomalous_flag=False, d_min=3) input_arrays = [miller_set.array( data=flex.random_double(size=miller_set.indices().size())) .set_observation_type_xray_amplitude() for i in [0,1]] mtz_dataset = input_arrays[0].as_mtz_dataset(column_root_label="F0") mtz_dataset.mtz_object().write("tmp1.mtz") hkl = any_file("tmp1.mtz") assert hkl.file_type == "hkl" #assert hkl.file_server assert hkl.file_server.miller_arrays[0].info().labels == ["F0"] os.remove("tmp1.mtz")
def tst_twin_completion(): uc = uctbx.unit_cell( "40,40,70,90,90,90" ) xs = crystal.symmetry( unit_cell=uc, space_group="P1" ) miller_set = miller.build_set( crystal_symmetry=xs, anomalous_flag=False, d_min=3.0 ).map_to_asu() select = flex.bool( miller_set.indices().size(), True ) select[300]=False miller_set_mod = miller_set.select( select ) # make sure we threw away a reflection assert not miller_set_mod.indices().all_eq( miller_set.indices() ) checker = xray.twin_completion( miller_set_mod.indices(), xs.space_group(), False, [0,1,0,1,0,0,0,0,-1] ) new_hkl = checker.twin_complete() miller_set_mod = miller_set_mod.customized_copy( indices=new_hkl) miller_set_mod = miller_set_mod.map_to_asu() a,b = miller_set_mod.common_sets( miller_set ) assert a.indices().size() == miller_set_mod.indices().size() assert a.indices().size() == miller_set.indices().size() assert miller_set_mod.is_unique_set_under_symmetry() checker = xray.twin_completion( miller_set.indices(), xs.space_group(), False, [0,1,0,1,0,0,0,0,-1] ) basic_flags = miller_set.generate_r_free_flags_basic() lattice_flags = miller_set.generate_r_free_flags_on_lattice_symmetry() assert not checker.check_free_flags( basic_flags.data() ) # this should give False assert checker.check_free_flags( lattice_flags.data() ) # this should give True selection_array = checker.get_free_model_selection( lattice_flags.indices(), lattice_flags.data() ) assert selection_array.all_eq( lattice_flags.data() )