def _compute_f_maps(self): self.f_map = miller.structure_factor_box_from_map( map=self.map_data, crystal_symmetry=self.crystal_symmetry) if (self.map_data_1 is not None): self.f_map_1 = miller.structure_factor_box_from_map( map=self.map_data_1, crystal_symmetry=self.crystal_symmetry) self.f_map_2 = miller.structure_factor_box_from_map( map=self.map_data_2, crystal_symmetry=self.crystal_symmetry)
def _compute_f_maps(self): self.f_map = miller.structure_factor_box_from_map( map=self.base.map_data(), crystal_symmetry=self.base.crystal_symmetry()) if (self.base.half_map_data_1() is not None): self.f_map_1 = miller.structure_factor_box_from_map( map=self.base.half_map_data_1(), crystal_symmetry=self.base.crystal_symmetry()) self.f_map_2 = miller.structure_factor_box_from_map( map=self.base.half_map_data_2(), crystal_symmetry=self.base.crystal_symmetry())
def run(xray_structure, f_map=None, map_data=None, d_fsc_model=None): assert [f_map, map_data].count(None) == 1 xrs = xray_structure.deep_copy_scatterers().set_b_iso(value=0) if (f_map is None): f_map = miller.structure_factor_box_from_map( map=map_data, crystal_symmetry=xray_structure.crystal_symmetry()) fc = f_map.structure_factors_from_scatterers(xray_structure=xrs).f_calc() d_model_b0 = run_at_b(b=0, f_map=f_map, f_calc=fc).d_min del xrs if (d_fsc_model is None): d_fsc_model = fc.d_min_from_fsc(other=f_map, fsc_cutoff=0).d_min fo = f_map.resolution_filter(d_min=d_fsc_model) fo, fc, = fo.common_sets(fc) cc = -999 b = None ss = 1. / flex.pow2(fc.d_spacings().data()) / 4. data = fc.data() for b_ in range(-500, 500, 5): sc = flex.exp(-b_ * ss) fc_ = fc.customized_copy(data=data * sc) cc_ = fo.map_correlation(other=fc_) if (cc_ > cc): cc = cc_ b = b_ o = run_at_b(b=b, f_map=fo, f_calc=fc) return group_args(d_min=o.d_min, b_iso=b, d_model_b0=d_model_b0, d_fsc_model=d_fsc_model)
def _compute_f_maps(self): assert self.map_data.origin() == (0, 0, 0) assert self.map_data.as_1d().count( 0) != self.map_data.size() # need data if self.map_data_1 is not None: assert self.map_data_1.origin() == (0, 0, 0) assert self.map_data.all() == self.map_data_1.all() if self.map_data_2 is not None: assert self.map_data_2.origin() == (0, 0, 0) assert self.map_data.all() == self.map_data_2.all() self.f_map = miller.structure_factor_box_from_map( map=self.map_data, crystal_symmetry=self.crystal_symmetry) if (self.map_data_1 is not None): self.f_map_1 = miller.structure_factor_box_from_map( map=self.map_data_1, crystal_symmetry=self.crystal_symmetry) self.f_map_2 = miller.structure_factor_box_from_map( map=self.map_data_2, crystal_symmetry=self.crystal_symmetry)
def _compute_radius(self): if (not self.params.mask_maps): return if (self.xray_structure is None): return if (self.xray_structure is not None and [self.radius_smooth, self.resolution].count(None) == 2): f_map = miller.structure_factor_box_from_map( map=self.map_data, crystal_symmetry=self.crystal_symmetry) self.radius_smooth = maptbx.d99(f_map=f_map).result.d99 self.radius_smooth = get_atom_radius( xray_structure=self.xray_structure, radius=self.radius_smooth, resolution=self.resolution)
def _get_map_calc(self): if(self.box is True): f_calc = miller.structure_factor_box_from_map( crystal_symmetry = self.xray_structure.crystal_symmetry(), n_real = self.map.focus()).structure_factors_from_scatterers( xray_structure = self.xray_structure).f_calc() d_spacings = f_calc.d_spacings().data() sel = d_spacings > d_min f_calc = f_calc.select(sel) else: f_calc = self.xray_structure.structure_factors(d_min=self.d_min).f_calc() fft_map = miller.fft_map( crystal_gridding = self.crystal_gridding, fourier_coefficients = f_calc) return fft_map.real_map_unpadded()
def _compute_model_map_fsc(self): if(not self.params.compute_fsc_curve_model): return if(self.pdb_hierarchy is not None): f_obs = miller.structure_factor_box_from_map( map = self.box.map_data, crystal_symmetry = self.box.xray_structure.crystal_symmetry()) f_calc = f_obs.structure_factors_from_scatterers( xray_structure = self.box.xray_structure).f_calc() self.fsc_curve_model = f_calc.d_min_from_fsc( other=f_obs, bin_width=100, fsc_cutoff=0.5) self.d_fsc_model = self.fsc_curve_model.d_min self.d_fsc_model_0 = f_calc.d_min_from_fsc( other=f_obs, bin_width=100, fsc_cutoff=0.).d_min self.d_fsc_model_0143 = f_calc.d_min_from_fsc( other=f_obs, bin_width=100, fsc_cutoff=0.143).d_min
def map_as_fourier_coefficients(self, high_resolution=None): ''' Convert a map to Fourier coefficients to a resolution of high_resolution, if high_resolution is provided, otherwise box full of map coefficients will be created. NOTE: Fourier coefficients are relative the working origin (not original origin). A map calculated from the Fourier coefficients will superimpose on the working (current map) without origin shifts. ''' assert self.map_data() assert self.map_data().origin()==(0,0,0) return miller.structure_factor_box_from_map( crystal_symmetry = self.crystal_symmetry(), include_000 = True, map = self.map_data(), d_min = high_resolution)
def run(xray_structure, f_map=None, map_data=None, d_fsc_model=None): assert [f_map, map_data].count(None) == 1 xrs = xray_structure.deep_copy_scatterers().set_b_iso(value=0) if (f_map is None): f_map = miller.structure_factor_box_from_map( map=map_data, crystal_symmetry=xray_structure.crystal_symmetry()) fc = f_map.structure_factors_from_scatterers(xray_structure=xrs).f_calc() d_model_b0 = run_at_b(b=0, f_map=f_map, f_calc=fc).d_min del xrs if (d_fsc_model is None): d_fsc_model = fc.d_min_from_fsc(other=f_map, fsc_cutoff=0).d_min fo = f_map.resolution_filter(d_min=d_fsc_model) fo, fc, = fo.common_sets(fc) b = find_b(fo=fo.deep_copy(), fc=fc.deep_copy()) o = run_at_b(b=b, f_map=fo, f_calc=fc) return group_args(d_min=o.d_min, b_iso=b, d_model_b0=d_model_b0, d_fsc_model=d_fsc_model)
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 """ # sf_accuracy_params = \ mmtbx.f_model.sf_and_grads_accuracy_master_params.extract() sf_accuracy_params.algorithm="direct" p = sf_accuracy_params 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() # f_obs_cmpl = miller.structure_factor_box_from_map( map = m, crystal_symmetry = xrs.crystal_symmetry(), include_000 = True) # fc = f_obs_cmpl.structure_factors_from_scatterers( xray_structure=xrs, algorithm = p.algorithm, cos_sin_table = p.cos_sin_table, grid_resolution_factor = p.grid_resolution_factor, quality_factor = p.quality_factor, u_base = p.u_base, b_base = p.b_base, wing_cutoff = p.wing_cutoff, exp_table_one_over_step_size = p.exp_table_one_over_step_size).f_calc() # f1 = abs(fc).data() f2 = abs(f_obs_cmpl).data() r = 200*flex.sum(flex.abs(f1-f2))/flex.sum(f1+f2) print r assert r<0.5 # 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 run(args, log=None, ccp4_map=None, return_as_miller_arrays=False, nohl=False, return_f_obs=False, space_group_number=None, out=sys.stdout): if log is None: log = out inputs = mmtbx.utils.process_command_line_args( args=args, master_params=master_params()) got_map = False if ccp4_map: got_map = True broadcast(m="Parameters:", log=log) inputs.params.show(prefix=" ", out=out) params = inputs.params.extract() if (ccp4_map is None and inputs.ccp4_map is not None): broadcast(m="Processing input CCP4 map file: %s" % inputs.ccp4_map_file_name, log=log) ccp4_map = inputs.ccp4_map ccp4_map.show_summary(prefix=" ", out=out) got_map = True if (not got_map): raise Sorry("Map file is needed.") # m = ccp4_map if (m.space_group_number > 1): raise Sorry("Input map space group: %d. Must be P1." % m.space_group_number) broadcast(m="Input map information:", log=log) print("m.all() :", m.data.all(), file=out) print("m.focus() :", m.data.focus(), file=out) print("m.origin():", m.data.origin(), file=out) print("m.nd() :", m.data.nd(), file=out) print("m.size() :", m.data.size(), file=out) print("m.focus_size_1d():", m.data.focus_size_1d(), file=out) print("m.is_0_based() :", m.data.is_0_based(), file=out) print("map: min/max/mean:", flex.min(m.data), flex.max(m.data), flex.mean(m.data), file=out) print("unit cell:", m.unit_cell_parameters, file=out) # if not space_group_number: space_group_number = 1 if space_group_number <= 1: symmetry_flags = None else: symmetry_flags = maptbx.use_space_group_symmetry, #cs = crystal.symmetry(m.unit_cell_parameters, space_group_number) # this will not work if m.unit_cell_grid != m.data.all() # Instead use ccp4 map crystal_symmetry and classify according to the case cs = m.crystal_symmetry() if m.unit_cell_grid == m.data.all(): print("\nOne unit cell of data is present in map", file=out) else: if params.keep_origin: print("\nNOTE: This map does not have exactly one unit cell of data, so \n"+\ "keep_origin is not available\n", file=out) print( "--> Setting keep_origin=False and creating a new cell and gridding.\n", file=out) params.keep_origin = False print("Moving origin of input map to (0,0,0)", file=out) print("New cell will be: (%.3f, %.3f, %.3f, %.1f, %.1f, %.1f) A " % (cs.unit_cell().parameters()), file=out) print("New unit cell grid will be: (%s, %s, %s) " % (m.data.all()), file=out) map_data = m.data # Get origin in grid units and new position of origin in grid units original_origin = map_data.origin() print("\nInput map has origin at grid point (%s,%s,%s)" % (tuple(original_origin)), file=out) if params.output_origin_grid_units is not None: params.keep_origin = False new_origin = tuple(params.output_origin_grid_units) print("User-specified origin at grid point (%s,%s,%s)" % (tuple(params.output_origin_grid_units)), file=out) if tuple(params.output_origin_grid_units) == tuple(original_origin): print("This is the same as the input origin. No origin shift.", file=out) elif params.keep_origin: new_origin = original_origin print("Keeping origin at grid point (%s,%s,%s)" % (tuple(original_origin)), file=out) else: new_origin = ( 0, 0, 0, ) print("New origin at grid point (%s,%s,%s)" % (tuple(( 0, 0, 0, ))), file=out) # shift_cart is shift away from (0,0,0) if new_origin != ( 0, 0, 0, ): shift_cart = get_shift_cart(map_data=map_data, crystal_symmetry=cs, origin=new_origin) else: shift_cart = ( 0, 0, 0, ) map_data = maptbx.shift_origin_if_needed(map_data=map_data).map_data # generate complete set of Miller indices up to given high resolution d_min n_real = map_data.focus() crystal_gridding = maptbx.crystal_gridding( unit_cell=cs.unit_cell(), space_group_info=cs.space_group_info(), symmetry_flags=symmetry_flags, pre_determined_n_real=n_real) # d_min = params.d_min if (d_min is None and not params.box): d_min = maptbx.d_min_from_map( map_data=map_data, unit_cell=cs.unit_cell(), resolution_factor=params.resolution_factor) print("\nResolution of map coefficients using "+\ "resolution_factor of %.2f: %.1f A\n" %(params.resolution_factor,d_min), file=out) if (d_min is None): # box of reflections in |h|<N1/2, |k|<N2/2, 0<=|l|<N3/2 f_obs_cmpl = miller.structure_factor_box_from_map( map=map_data.as_double(), crystal_symmetry=cs, include_000=True) else: complete_set = miller.build_set(crystal_symmetry=cs, anomalous_flag=False, d_min=d_min) try: f_obs_cmpl = complete_set.structure_factors_from_map( map=map_data.as_double(), use_scale=True, anomalous_flag=False, use_sg=False) except Exception as e: if (str(e) == "cctbx Error: Miller index not in structure factor map."): msg = "Too high resolution requested. Try running with larger d_min." raise Sorry(msg) else: raise Sorry(str(e)) if params.scale_max is not None: f_obs_cmpl = f_obs_cmpl.apply_scaling(target_max=params.scale_max) from scitbx.matrix import col if col(shift_cart) != col(( 0, 0, 0, )): print("Output origin is at: (%.3f, %.3f, %.3f) A " % (tuple(-col(shift_cart))), file=out) f_obs_cmpl = f_obs_cmpl.translational_shift( cs.unit_cell().fractionalize(-col(shift_cart)), deg=False) else: print("Output origin is at (0.000, 0.000, 0.000) A", file=out) if nohl and return_as_miller_arrays and not return_f_obs: return f_obs_cmpl mtz_dataset = f_obs_cmpl.as_mtz_dataset(column_root_label="F") f_obs = abs(f_obs_cmpl) f_obs.set_sigmas(sigmas=flex.double(f_obs_cmpl.data().size(), 1)) if nohl and return_as_miller_arrays and return_f_obs: return 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") if not nohl: # convert phases into HL coefficeints broadcast(m="Convert phases into HL coefficients:", log=log) hl = get_hl(f_obs_cmpl=f_obs_cmpl, k_blur=params.k_blur, b_blur=params.b_blur) cc = get_cc(f=f_obs_cmpl, hl=hl) print("cc:", cc, file=out) if (abs(1. - cc) > 1.e-3): print( "Supplied b_blur is not good. Attempting to find optimal b_blur.", file=out) cc_best = 999. b_blur_best = params.b_blur for b_blur in range(1, 100): hl = get_hl(f_obs_cmpl=f_obs_cmpl, k_blur=params.k_blur, b_blur=b_blur) cc = get_cc(f=f_obs_cmpl, hl=hl) if (cc < cc_best): cc_best = cc b_blur_best = b_blur if (abs(1. - cc) < 1.e-3): b_blur_best = b_blur break hl = get_hl(f_obs_cmpl=f_obs_cmpl, k_blur=params.k_blur, b_blur=b_blur_best) print("cc:", get_cc(f=f_obs_cmpl, hl=hl), file=out) print("b_blur_best:", b_blur_best, file=out) mtz_dataset.add_miller_array(miller_array=hl, column_root_label="HL") else: hl = None if return_as_miller_arrays: if return_f_obs: return f_obs, hl else: return f_obs_cmpl, hl else: # write output MTZ file with all the data broadcast(m="Writing output MTZ file:", log=log) print(" file name:", params.output_file_name, file=log) mtz_object = mtz_dataset.mtz_object() mtz_object.write(file_name=params.output_file_name)
def run(args, log=None, ccp4_map=None, return_as_miller_arrays=False, nohl=False, return_f_obs=False, space_group_number=None, out=sys.stdout): if log is None: log = out inputs = mmtbx.utils.process_command_line_args( args=args, master_params=master_params()) got_map = False if ccp4_map: got_map = True broadcast(m="Parameters:", log=log) inputs.params.show(prefix=" ", out=out) params = inputs.params.extract() if (ccp4_map is None and inputs.ccp4_map is not None): broadcast(m="Processing input CCP4 map file: %s" % inputs.ccp4_map_file_name, log=log) ccp4_map = inputs.ccp4_map ccp4_map.show_summary(prefix=" ", out=out) got_map = True if (not got_map): raise Sorry("Map file is needed.") # m = ccp4_map if (m.space_group_number > 1): raise Sorry("Input map space group: %d. Must be P1." % m.space_group_number) broadcast(m="Input map information:", log=log) print >> out, "m.all() :", m.data.all() print >> out, "m.focus() :", m.data.focus() print >> out, "m.origin():", m.data.origin() print >> out, "m.nd() :", m.data.nd() print >> out, "m.size() :", m.data.size() print >> out, "m.focus_size_1d():", m.data.focus_size_1d() print >> out, "m.is_0_based() :", m.data.is_0_based() print >> out, "map: min/max/mean:", flex.min(m.data), flex.max( m.data), flex.mean(m.data) print >> out, "unit cell:", m.unit_cell_parameters # if not space_group_number: space_group_number = 1 if space_group_number <= 1: symmetry_flags = None else: symmetry_flags = maptbx.use_space_group_symmetry, cs = crystal.symmetry(m.unit_cell_parameters, space_group_number) map_data = m.data # Get origin in grid units and new position of origin in grid units original_origin = map_data.origin() print >> out, "Input map has origin at grid point (%s,%s,%s)" % ( tuple(original_origin)) if params.output_origin_grid_units is not None: params.keep_origin = False new_origin = tuple(params.output_origin_grid_units) print >> out, "User-specified origin at grid point (%s,%s,%s)" % ( tuple(params.output_origin_grid_units)) if tuple(params.output_origin_grid_units) == tuple(original_origin): print >> out, "This is the same as the input origin. No origin shift." elif params.keep_origin: new_origin = original_origin print >> out, "Keeping origin at grid point (%s,%s,%s)" % ( tuple(original_origin)) else: new_origin = ( 0, 0, 0, ) print >> out, "New origin at grid point (%s,%s,%s)" % (tuple(( 0, 0, 0, ))) # shift_cart is shift away from (0,0,0) if new_origin != ( 0, 0, 0, ): shift_cart = get_shift_cart(map_data=map_data, crystal_symmetry=cs, origin=new_origin) else: shift_cart = ( 0, 0, 0, ) map_data = maptbx.shift_origin_if_needed(map_data=map_data).map_data # generate complete set of Miller indices up to given high resolution d_min n_real = map_data.focus() crystal_gridding = maptbx.crystal_gridding( unit_cell=cs.unit_cell(), space_group_info=cs.space_group_info(), symmetry_flags=symmetry_flags, pre_determined_n_real=n_real) # d_min = params.d_min if (d_min is None and not params.box): d_min = maptbx.d_min_from_map(map_data=map_data, unit_cell=cs.unit_cell()) if (d_min is None): # box of reflections in |h|<N1/2, |k|<N2/2, 0<=|l|<N3/2 f_obs_cmpl = miller.structure_factor_box_from_map( map=map_data.as_double(), crystal_symmetry=cs, include_000=True) else: complete_set = miller.build_set(crystal_symmetry=cs, anomalous_flag=False, d_min=d_min) try: f_obs_cmpl = complete_set.structure_factors_from_map( map=map_data.as_double(), use_scale=True, anomalous_flag=False, use_sg=False) except Exception, e: if (str(e) == "cctbx Error: Miller index not in structure factor map."): msg = "Too high resolution requested. Try running with larger d_min." raise Sorry(msg) else: raise Sorry(str(e))
def calculate_inverse_fft(map_data=None, crystal_symmetry=None, d_min=None, box=False, resolution_factor=0.333, out=sys.stdout): ''' Combines two possible methods of generating the complete set of indices. Box means supply all possible reflections that can be calculated using the gridding in map_data in the FFT. Otherwise, generate Miller indices inside a sphere of resolution. This may be faster if the box is large and most indices in the box are not going to be used. If box=True, supply full box of reflections (all possible) Otherwise, generate complete set of Miller indices up to given high resolution d_min. If d_min not specified, use minimum resolution allowed with gridding available and requested resolution_factor (1/2 is finest available, 1/3 is typical) ''' n_real = map_data.focus() # Choose d_min and make sure it is bigger than smallest allowed if (d_min is None and not box): d_min = maptbx.d_min_from_map(map_data=map_data, unit_cell=crystal_symmetry.unit_cell(), resolution_factor=resolution_factor) print("\nResolution of map coefficients using "+\ "resolution_factor of %.2f: %.1f A\n" %(resolution_factor,d_min), file=out) elif (not box): # make sure d_min is big enough d_min_allowed = maptbx.d_min_from_map( map_data=map_data, unit_cell=crystal_symmetry.unit_cell(), resolution_factor=0.5) if d_min < d_min_allowed: print( "\nResolution of map coefficients allowed by gridding is %.3f " % (d_min_allowed), file=out) d_min = d_min_allowed if (d_min is None): # box of reflections in |h|<N1/2, |k|<N2/2, 0<=|l|<N3/2 f_obs_cmpl = miller.structure_factor_box_from_map( map=map_data.as_double(), crystal_symmetry=crystal_symmetry, include_000=True) else: complete_set = miller.build_set(crystal_symmetry=crystal_symmetry, anomalous_flag=False, d_min=d_min) try: f_obs_cmpl = complete_set.structure_factors_from_map( map=map_data.as_double(), use_scale=True, anomalous_flag=False, use_sg=False) except Exception as e: if (str(e) == "cctbx Error: Miller index not in structure factor map."): msg = "Too high resolution requested. Try running with larger d_min." raise Sorry(msg) else: raise Sorry(str(e)) return f_obs_cmpl