def __init__( self, map_data_obs, xrs, d_min, vector_map, box_dimension=30): adopt_init_args(self, locals()) self.crystal_gridding = maptbx.crystal_gridding( unit_cell = self.xrs.unit_cell(), space_group_info = self.xrs.space_group_info(), pre_determined_n_real = self.map_data_obs.all()) self.n_real = self.crystal_gridding.n_real() crystal_gridding = maptbx.crystal_gridding( unit_cell = self.xrs.unit_cell(), space_group_info = self.xrs.space_group_info(), pre_determined_n_real = self.map_data_obs.all()) mc = xrs.structure_factors(d_min=d_min).f_calc() fft_map = miller.fft_map( crystal_gridding = crystal_gridding, fourier_coefficients = mc) fft_map.apply_sigma_scaling() self.map_data_calc = fft_map.real_map_unpadded() scale = scale_k1(x=self.map_data_obs, y=self.map_data_calc) self.map_data_calc = self.map_data_calc * scale # # result map self.map_result = flex.double(flex.grid(self.map_data_obs.all())) # iterate over boxes self.box_iterator()
def exercise_flood_fill(): uc = uctbx.unit_cell('10 10 10 90 90 90') for uc in (uctbx.unit_cell('10 10 10 90 90 90'), uctbx.unit_cell('9 10 11 87 91 95')): gridding = maptbx.crystal_gridding( unit_cell=uc, pre_determined_n_real=(5,5,5)) corner_cube = (0,4,20,24,100,104,120,124) # cube across all 8 corners channel = (12,37,38,39,42,43,62,63,67,68,87,112) data = flex.int(flex.grid(gridding.n_real())) for i in (corner_cube + channel): data[i] = 1 flood_fill = masks.flood_fill(data, uc) assert data.count(0) == 105 for i in corner_cube: assert data[i] == 2 for i in channel: assert data[i] == 3 assert approx_equal(flood_fill.centres_of_mass(), ((-0.5, -0.5, -0.5), (-2.5, 7/3, 2.5))) assert approx_equal(flood_fill.centres_of_mass_frac(), ((-0.1, -0.1, -0.1), (-0.5, 7/15, 0.5))) assert approx_equal(flood_fill.centres_of_mass_cart(), uc.orthogonalize(flood_fill.centres_of_mass_frac())) assert flood_fill.n_voids() == 2 assert approx_equal(flood_fill.grid_points_per_void(), (8, 12)) if 0: from crys3d import wx_map_viewer wx_map_viewer.display(raw_map=data.as_double(), unit_cell=uc, wires=False) # gridding = maptbx.crystal_gridding( unit_cell=uc, pre_determined_n_real=(10,10,10)) data = flex.int(flex.grid(gridding.n_real())) # parallelogram points = [(2,4,5),(3,4,5),(4,4,5),(5,4,5),(6,4,5), (3,5,5),(4,5,5),(5,5,5),(6,5,5),(7,5,5), (4,6,5),(5,6,5),(6,6,5),(7,6,5),(8,6,5)] points_frac = flex.vec3_double() for p in points: data[p] = 1 points_frac.append([p[i]/gridding.n_real()[i] for i in range(3)]) points_cart = uc.orthogonalize(points_frac) flood_fill = masks.flood_fill(data, uc) assert data.count(2) == 15 assert approx_equal(flood_fill.centres_of_mass_frac(), ((0.5,0.5,0.5),)) pai_cart = math.principal_axes_of_inertia( points=points_cart, weights=flex.double(points_cart.size(),1.0)) F = matrix.sqr(uc.fractionalization_matrix()) O = matrix.sqr(uc.orthogonalization_matrix()) assert approx_equal( pai_cart.center_of_mass(), flood_fill.centres_of_mass_cart()[0]) assert approx_equal( flood_fill.covariance_matrices_cart()[0], (F.transpose() * matrix.sym( sym_mat3=flood_fill.covariance_matrices_frac()[0]) * F).as_sym_mat3()) assert approx_equal( pai_cart.inertia_tensor(), flood_fill.inertia_tensors_cart()[0]) assert approx_equal(pai_cart.eigensystem().vectors(), flood_fill.eigensystems_cart()[0].vectors()) assert approx_equal(pai_cart.eigensystem().values(), flood_fill.eigensystems_cart()[0].values()) return
def run(args, log=sys.stdout): print >> log, "-" * 79 print >> log, legend print >> log, "-" * 79 inputs = mmtbx.utils.process_command_line_args( args=args, master_params=master_params()) file_names = inputs.pdb_file_names if (len(file_names) != 1): raise Sorry("A PDB file is expected.") xrs = iotbx.pdb.input(file_name=file_names[0]).xray_structure_simple() params = inputs.params.extract() # crystal_gridding = maptbx.crystal_gridding( unit_cell=xrs.unit_cell(), d_min=params.resolution, resolution_factor=params.resolution_factor, symmetry_flags=maptbx.use_space_group_symmetry, space_group_info=xrs.space_group().info()) mmtbx_masks_asu_mask_obj = mmtbx.masks.asu_mask( xray_structure=xrs.expand_to_p1(sites_mod_positive=True), n_real=crystal_gridding.n_real()) bulk_solvent_mask = mmtbx_masks_asu_mask_obj.mask_data_whole_uc() # fem.ccp4_map(cg=crystal_gridding, file_name="mask.ccp4", map_data=bulk_solvent_mask)
def prepare_reference_map_3(self, xrs, pdb_h): """ with ramachandran outliers """ print >> self.log, "Preparing reference map, method 3" outlier_selection_txt = mmtbx.building.loop_closure.utils. \ rama_outliers_selection(pdb_h, self.rama_manager, 1) asc = pdb_h.atom_selection_cache() print >> self.log, "rama outlier selection:", outlier_selection_txt rama_out_sel = asc.selection(outlier_selection_txt) xrs=xrs.set_b_iso(value=50) # side_chain_no_cb_selection = ~ xrs.main_chain_selection() side_chain_no_cb_selection = ~ xrs.backbone_selection() xrs = xrs.set_b_iso(value=200, selection=side_chain_no_cb_selection) xrs = xrs.set_b_iso(value=150, selection=rama_out_sel) # xrs = xrs.set_occupancies(value=0.3, selection=rama_out_sel) crystal_gridding = maptbx.crystal_gridding( unit_cell = xrs.unit_cell(), space_group_info = xrs.space_group_info(), symmetry_flags = maptbx.use_space_group_symmetry, d_min = self.params.reference_map_resolution) fc = xrs.structure_factors(d_min = self.params.reference_map_resolution, algorithm = "direct").f_calc() fft_map = miller.fft_map( crystal_gridding=crystal_gridding, fourier_coefficients=fc) fft_map.apply_sigma_scaling() self.reference_map = fft_map.real_map_unpadded(in_place=False) fft_map.as_xplor_map(file_name="%s_3.map" % self.params.output_prefix)
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 exercise_translational_phase_shift(n_sites=100, d_min=1.5, resolution_factor=0.3): sgi = space_group_info("P1") xrs = random_structure.xray_structure( space_group_info=sgi, elements=(("O", "N", "C") * (n_sites // 3 + 1))[:n_sites], volume_per_atom=50, min_distance=1.5) f_calc = xrs.structure_factors(d_min=d_min).f_calc() print f_calc.unit_cell() from scitbx.matrix import col shift_frac = col((.23984120, .902341127, .51219021)) # Shift phases directly phase_shifted = f_calc.translational_shift(shift_frac=shift_frac) # Check that map from phase_shifted FC matches map calculated from # translated xrs # Map from phase-shifted FC shifted_fft_map = phase_shifted.fft_map( resolution_factor=resolution_factor) shifted_fft_map.apply_sigma_scaling() shifted_map_data = shifted_fft_map.real_map_unpadded() cs = xrs.crystal_symmetry() from cctbx.maptbx import crystal_gridding cg = crystal_gridding(unit_cell=cs.unit_cell(), space_group_info=cs.space_group_info(), pre_determined_n_real=shifted_map_data.all()) # Map from translated xrs sites_shifted = xrs.sites_frac() + shift_frac xrs.set_sites_frac(sites_shifted) f_calc_from_shifted_xrs = xrs.structure_factors(d_min=d_min).f_calc() fft_map_from_shifted_xrs = f_calc_from_shifted_xrs.fft_map( resolution_factor=resolution_factor, crystal_gridding=cg) map_data_from_shifted_xrs = fft_map_from_shifted_xrs.real_map_unpadded() # shifted_map_data (map from phase shifted f_calc), # map_data_from_shifted_xrs (recalculated with shifted xrs) assert shifted_map_data.all() == map_data_from_shifted_xrs.all() from cctbx import maptbx sel = maptbx.grid_indices_around_sites(unit_cell=xrs.unit_cell(), fft_n_real=shifted_map_data.focus(), fft_m_real=shifted_map_data.all(), sites_cart=xrs.sites_cart(), site_radii=flex.double( xrs.scatterers().size(), 1.5)) shifted_map_data = shifted_map_data.select(sel) map_data_from_shifted_xrs = map_data_from_shifted_xrs.select(sel) cc_map_data_from_shifted_xrs_shifted_map_data = flex.linear_correlation( x=map_data_from_shifted_xrs.as_1d(), y=shifted_map_data.as_1d()).coefficient() print "cc_map_data_from_shifted_xrs_shifted_map_data",\ cc_map_data_from_shifted_xrs_shifted_map_data assert cc_map_data_from_shifted_xrs_shifted_map_data > 0.99 print "*" * 25
def check(tuple_calc, selection, prefix): miller_arrays = reflection_file_reader.any_reflection_file( file_name=prefix + "polder_map_coeffs.mtz").as_miller_arrays() mc_polder = None for ma in miller_arrays: lbl = ma.info().label_string() if (lbl == "mFo-DFc_polder,PHImFo-DFc_polder"): mc_polder = ma.deep_copy() assert (mc_polder is not None) cg = maptbx.crystal_gridding(unit_cell=mc_polder.unit_cell(), d_min=mc_polder.d_min(), resolution_factor=0.25, space_group_info=mc_polder.space_group_info()) map_polder = get_map(cg=cg, mc=mc_polder) pdb_hierarchy = iotbx.pdb.input(source_info=None, lines=pdb_str).construct_hierarchy() sel = pdb_hierarchy.atom_selection_cache().selection(string=selection) sites_cart_lig = pdb_hierarchy.atoms().extract_xyz().select(sel) sites_frac_lig = mc_polder.unit_cell().fractionalize(sites_cart_lig) mp = get_map_stats(map=map_polder, sites_frac=sites_frac_lig) # mmm_mp = mp.min_max_mean().as_tuple() print("Polder map : %7.3f %7.3f %7.3f" % mmm_mp) assert approx_equal(mmm_mp, tuple_calc, eps=1.0), "\ calculated is %s and expected is %s" % (mmm_mp, tuple_calc)
def run(args): assert len(args) == 1 timer = time_log("pdb.input").start() pdb_inp = iotbx.pdb.input(file_name=args[0]) print "number of pdb atoms:", pdb_inp.atoms().size() print timer.log() crystal_symmetry = pdb_inp.crystal_symmetry() assert crystal_symmetry is not None crystal_symmetry.show_summary() assert crystal_symmetry.unit_cell() is not None assert crystal_symmetry.space_group_info() is not None sites_cart = pdb_inp.atoms().extract_xyz() site_radii = flex.double(sites_cart.size(), 2.5) crystal_gridding = maptbx.crystal_gridding( unit_cell=crystal_symmetry.unit_cell(), d_min=2, resolution_factor=1/3) fft = fftpack.real_to_complex_3d(crystal_gridding.n_real()) print "n_real:", fft.n_real() print "m_real:", fft.m_real() timer = time_log("grid_indices_around_sites").start() grid_indices = maptbx.grid_indices_around_sites( unit_cell=crystal_symmetry.unit_cell(), fft_n_real=fft.n_real(), fft_m_real=fft.m_real(), sites_cart=sites_cart, site_radii=site_radii) print "grid_indices.size():", grid_indices.size() print timer.log() print "grid fraction:", \ grid_indices.size() / matrix.col(fft.n_real()).product()
def run(args): assert len(args) == 1 timer = time_log("pdb.input").start() pdb_inp = iotbx.pdb.input(file_name=args[0]) print("number of pdb atoms:", pdb_inp.atoms().size()) print(timer.log()) crystal_symmetry = pdb_inp.crystal_symmetry() assert crystal_symmetry is not None crystal_symmetry.show_summary() assert crystal_symmetry.unit_cell() is not None assert crystal_symmetry.space_group_info() is not None sites_cart = pdb_inp.atoms().extract_xyz() site_radii = flex.double(sites_cart.size(), 2.5) crystal_gridding = maptbx.crystal_gridding( unit_cell=crystal_symmetry.unit_cell(), d_min=2, resolution_factor=1 / 3) fft = fftpack.real_to_complex_3d(crystal_gridding.n_real()) print("n_real:", fft.n_real()) print("m_real:", fft.m_real()) timer = time_log("grid_indices_around_sites").start() grid_indices = maptbx.grid_indices_around_sites( unit_cell=crystal_symmetry.unit_cell(), fft_n_real=fft.n_real(), fft_m_real=fft.m_real(), sites_cart=sites_cart, site_radii=site_radii) print("grid_indices.size():", grid_indices.size()) print(timer.log()) print("grid fraction:", \ grid_indices.size() / matrix.col(fft.n_real()).product())
def get_f_masks(xrs, miller_array): crystal_gridding = maptbx.crystal_gridding( unit_cell=xrs.unit_cell(), d_min=miller_array.d_min(), resolution_factor=1. / 4, symmetry_flags=maptbx.use_space_group_symmetry, space_group_info=xrs.space_group_info()) mp = mmtbx.masks.mask_master_params.extract() mask_data = mmtbx.masks.mask_from_xray_structure( xray_structure=xrs, p1=True, solvent_radius=mp.solvent_radius, shrink_truncation_radius=mp.shrink_truncation_radius, for_structure_factors=True, n_real=crystal_gridding.n_real()).mask_data n = mask_data.all() mask_data1 = flex.double(flex.grid(n), 0) mask_data2 = flex.double(flex.grid(n), 0) I, J, K = range(n[0]), range(n[1]), range(n[2]) for i in I: for j in J: for k in K: if (i < n[0] // 2 and j < n[1] // 2 and k < n[2] // 2): mask_data1[i, j, k] = mask_data[i, j, k] else: mask_data2[i, j, k] = mask_data[i, j, k] f_mask1 = miller_array.structure_factors_from_map(map=mask_data1, use_scale=True, anomalous_flag=False, use_sg=False) f_mask2 = miller_array.structure_factors_from_map(map=mask_data2, use_scale=True, anomalous_flag=False, use_sg=False) return [f_mask1.data(), f_mask2.data()]
def get_map_from_map_coeffs(map_coeffs=None, crystal_symmetry=None, n_real=None, resolution_factor=None, apply_sigma_scaling=True): if resolution_factor is None: resolution_factor = 0.25 from cctbx import maptbx from cctbx.maptbx import crystal_gridding if not crystal_symmetry: crystal_symmetry = map_coeffs.crystal_symmetry() if map_coeffs.crystal_symmetry().space_group_info()!= \ crystal_symmetry.space_group_info(): assert str(map_coeffs.crystal_symmetry().space_group_info()).replace( " ", "").lower() == 'p1' # use map_coeffs.crystal_symmetry crystal_symmetry = map_coeffs.crystal_symmetry() if n_real: cg = crystal_gridding( unit_cell=crystal_symmetry.unit_cell(), space_group_info=crystal_symmetry.space_group_info(), pre_determined_n_real=n_real) else: cg = None fft_map = map_coeffs.fft_map( resolution_factor=resolution_factor, crystal_gridding=cg, symmetry_flags=maptbx.use_space_group_symmetry) if apply_sigma_scaling: fft_map.apply_sigma_scaling() else: fft_map.apply_volume_scaling() map_data = fft_map.real_map_unpadded() return map_data
def get_f_masks(xrs, miller_array): crystal_gridding = maptbx.crystal_gridding( unit_cell = xrs.unit_cell(), d_min = miller_array.d_min(), resolution_factor = 1./4, symmetry_flags = maptbx.use_space_group_symmetry, space_group_info = xrs.space_group_info()) mp = mmtbx.masks.mask_master_params.extract() mask_data = mmtbx.masks.mask_from_xray_structure( xray_structure = xrs, p1 = True, solvent_radius = mp.solvent_radius, shrink_truncation_radius = mp.shrink_truncation_radius, for_structure_factors = True, n_real = crystal_gridding.n_real()).mask_data n = mask_data.all() mask_data1 = flex.double(flex.grid(n), 0) mask_data2 = flex.double(flex.grid(n), 0) I,J,K = xrange(n[0]), xrange(n[1]), xrange(n[2]) for i in I: for j in J: for k in K: if(i < n[0]//2 and j < n[1]//2 and k < n[2]//2): mask_data1[i,j,k]=mask_data[i,j,k] else: mask_data2[i,j,k]=mask_data[i,j,k] f_mask1 = miller_array.structure_factors_from_map(map=mask_data1, use_scale = True, anomalous_flag = False, use_sg = False) f_mask2 = miller_array.structure_factors_from_map(map=mask_data2, use_scale = True, anomalous_flag = False, use_sg = False) return [f_mask1.data(), f_mask2.data()]
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 __init__(self, xray_structure=None, fmodel=None, map_data=None, d_min=None, resolution_factor=0.25, map_type="2mFo-DFc"): """ Utility to calculate correlation between two maps: CC(xray_structure, map_data), xray_structure are map_data inputs or CC(2mFo-DFc, Fc), 2mFo-DFc and Fc are from input fmodel . """ assert [fmodel, map_data].count(None) == 1 assert [xray_structure, map_data].count(None) in [0, 2] assert [fmodel, xray_structure].count(None) == 1 assert [d_min, fmodel].count(None) == 1 adopt_init_args(self, locals()) if (fmodel is not None): self.xray_structure = fmodel.xray_structure # get map_data defined if (self.fmodel is not None): e_map_obj = fmodel.electron_density_map() isotropize = True if (fmodel.is_twin_fmodel_manager()): isotropize = False mc = e_map_obj.map_coefficients(map_type=map_type, fill_missing=False, isotropize=isotropize) crystal_gridding = self.fmodel.f_obs().crystal_gridding( d_min=self.fmodel.f_obs().d_min(), resolution_factor=resolution_factor) fft_map = miller.fft_map(crystal_gridding=crystal_gridding, fourier_coefficients=mc) self.map_data = fft_map.real_map_unpadded() # get model map if (self.fmodel is not None): if (fmodel.is_twin_fmodel_manager()): f_model = self.fmodel.f_model() else: f_model = self.fmodel.f_model_scaled_with_k1() fft_map = miller.fft_map(crystal_gridding=crystal_gridding, fourier_coefficients=f_model) fft_map.apply_sigma_scaling() self.map_model = fft_map.real_map_unpadded() else: 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.accessor().all()) f_model = self.xray_structure.structure_factors( d_min=self.d_min).f_calc() fft_map = miller.fft_map(crystal_gridding=crystal_gridding, fourier_coefficients=f_model) fft_map.apply_sigma_scaling() self.map_model = fft_map.real_map_unpadded() if (self.fmodel is not None): self.sites_cart = self.fmodel.xray_structure.sites_cart() self.sites_frac = self.fmodel.xray_structure.sites_frac() else: self.sites_cart = self.xray_structure.sites_cart() self.sites_frac = self.xray_structure.sites_frac()
def exercise_01(d_min=1.0): """ Exercise maptbx.target_and_gradients_diffmap in action: minimization. """ xrs = get_xrs() map_target, f_calc = get_map(xrs=xrs) assert approx_equal(xrs.sites_cart(), [[0,0,0]]) for sx in [-1,0,1]: for sy in [-1,0,1]: for sz in [-1,0,1]: xrs_cp = xrs.deep_copy_scatterers() xrs_cp = xrs_cp.translate(x=0.3*sx, y=0.5*sy, z=0.7*sz) assert approx_equal(xrs_cp.sites_cart(), [[0.3*sx,0.5*sy,0.7*sz]],1.e-6) crystal_gridding = maptbx.crystal_gridding( unit_cell = xrs_cp.unit_cell(), space_group_info = xrs_cp.space_group_info(), pre_determined_n_real = map_target.accessor().all()) o = minimization.run( xray_structure = xrs_cp, miller_array = f_calc, crystal_gridding = crystal_gridding, map_target = map_target, step = d_min/4, target_type = "diffmap") assert approx_equal(xrs.sites_cart(), [[0,0,0]])
def check(tuple_calc, selection, prefix): miller_arrays = reflection_file_reader.any_reflection_file(file_name = prefix+"polder_map_coeffs.mtz").as_miller_arrays() mc_polder = None for ma in miller_arrays: lbl = ma.info().label_string() if(lbl == "mFo-DFc_polder,PHImFo-DFc_polder"): mc_polder = ma.deep_copy() assert (mc_polder is not None) cg = maptbx.crystal_gridding( unit_cell = mc_polder.unit_cell(), d_min = mc_polder.d_min(), resolution_factor = 0.25, space_group_info = mc_polder.space_group_info()) map_polder = get_map(cg=cg, mc=mc_polder) pdb_hierarchy = iotbx.pdb.input( source_info=None, lines=pdb_str).construct_hierarchy() sel = pdb_hierarchy.atom_selection_cache().selection(string = selection) sites_cart_lig = pdb_hierarchy.atoms().extract_xyz().select(sel) sites_frac_lig = mc_polder.unit_cell().fractionalize(sites_cart_lig) mp = get_map_stats( map = map_polder, sites_frac = sites_frac_lig) # mmm_mp = mp.min_max_mean().as_tuple() print "Polder map : %7.3f %7.3f %7.3f" % mmm_mp assert approx_equal(mmm_mp, tuple_calc, eps=1.0), "\ calculated is %s and expected is %s" % (mmm_mp, tuple_calc)
def run(args, out=sys.stdout): cmdline = iotbx.phil.process_command_line_with_files( args=args, master_phil_string=master_phil_str, pdb_file_def="model", map_file_def="map", usage_string="""\ em_rscc.py model.pdb map.ccp4 %s""" % __doc__) params = cmdline.work.extract() assert (not None in [params.model, params.map]) pdb_in = cmdline.get_file(params.model).file_object m = cmdline.get_file(params.map).file_object print >> out, "Input electron density map:" 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 symm = crystal.symmetry(space_group_symbol="P1", unit_cell=m.unit_cell_parameters) xrs = pdb_in.input.xray_structure_simple(crystal_symmetry=symm) print >> out, "Setting up electron scattering table (d_min=%g)" % params.d_min xrs.scattering_type_registry(d_min=params.d_min, table="electron") fc = xrs.structure_factors(d_min=params.d_min).f_calc() cg = maptbx.crystal_gridding(unit_cell=symm.unit_cell(), space_group_info=symm.space_group_info(), pre_determined_n_real=m.data.all()) fc_map = fc.fft_map( crystal_gridding=cg).apply_sigma_scaling().real_map_unpadded() assert (fc_map.all() == fc_map.focus() == m.data.all()) em_data = m.data.as_double() unit_cell_for_interpolation = m.grid_unit_cell() frac_matrix = unit_cell_for_interpolation.fractionalization_matrix() sites_cart = xrs.sites_cart() sites_frac = xrs.sites_frac() print >> out, "PER-RESIDUE CORRELATION:" for chain in pdb_in.hierarchy.only_model().chains(): for residue_group in chain.residue_groups(): i_seqs = residue_group.atoms().extract_i_seq() values_em = flex.double() values_fc = flex.double() for i_seq in i_seqs: rho_em = maptbx.non_crystallographic_eight_point_interpolation( map=em_data, gridding_matrix=frac_matrix, site_cart=sites_cart[i_seq]) rho_fc = fc_map.eight_point_interpolation(sites_frac[i_seq]) values_em.append(rho_em) values_fc.append(rho_fc) cc = flex.linear_correlation(x=values_em, y=values_fc).coefficient() print >> out, residue_group.id_str(), cc
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 __init__(self, xray_structure, solvent_radius, shrink_truncation_radius, ignore_hydrogen_atoms=False, crystal_gridding=None, grid_step=None, d_min=None, resolution_factor=1 / 4, atom_radii_table=None, use_space_group_symmetry=False): self.xray_structure = xray_structure if crystal_gridding is None: self.crystal_gridding = maptbx.crystal_gridding( unit_cell=xray_structure.unit_cell(), space_group_info=xray_structure.space_group_info(), step=grid_step, d_min=d_min, resolution_factor=resolution_factor, symmetry_flags=sgtbx.search_symmetry_flags( use_space_group_symmetry=use_space_group_symmetry)) else: self.crystal_gridding = crystal_gridding if use_space_group_symmetry: atom_radii = cctbx.masks.vdw_radii( xray_structure, table=atom_radii_table).atom_radii asu_mappings = xray_structure.asu_mappings( buffer_thickness=flex.max(atom_radii) + solvent_radius) scatterers_asu_plus_buffer = flex.xray_scatterer() frac = xray_structure.unit_cell().fractionalize for sc, mappings in zip(xray_structure.scatterers(), asu_mappings.mappings()): for mapping in mappings: scatterers_asu_plus_buffer.append( sc.customized_copy(site=frac(mapping.mapped_site()))) xs = xray.structure(crystal_symmetry=xray_structure, scatterers=scatterers_asu_plus_buffer) else: xs = xray_structure.expand_to_p1() self.vdw_radii = cctbx.masks.vdw_radii(xs, table=atom_radii_table) self.mask = cctbx.masks.around_atoms( unit_cell=xs.unit_cell(), space_group_order_z=xs.space_group().order_z(), sites_frac=xs.sites_frac(), atom_radii=self.vdw_radii.atom_radii, gridding_n_real=self.crystal_gridding.n_real(), solvent_radius=solvent_radius, shrink_truncation_radius=shrink_truncation_radius) if use_space_group_symmetry: tags = self.crystal_gridding.tags() tags.tags().apply_symmetry_to_mask(self.mask.data) self.flood_fill = cctbx.masks.flood_fill(self.mask.data, xray_structure.unit_cell()) self.exclude_void_flags = [False] * self.flood_fill.n_voids() self.solvent_accessible_volume = self.n_solvent_grid_points() \ / self.mask.data.size() * xray_structure.unit_cell().volume()
def get_map_data(xrs, d_min): cg = maptbx.crystal_gridding(unit_cell=xrs.unit_cell(), space_group_info=xrs.space_group_info(), step=0.1) fc = xrs.structure_factors(d_min=d_min, algorithm="direct").f_calc() fft_map = miller.fft_map(crystal_gridding=cg, fourier_coefficients=fc, f_000=xrs.f_000()) fft_map.apply_volume_scaling() return fft_map.real_map_unpadded()
def __init__(self, map, xray_structure, d_min, box=None, compute_cc_box=False, compute_cc_image=False, compute_cc_mask=True, compute_cc_volume=True, compute_cc_peaks=True): adopt_init_args(self, locals()) if (box is None and xray_structure.crystal_symmetry().space_group(). type().number() == 1): box = True else: box = False # self.cc_box = None self.cc_image = None self.cc_mask = None self.cc_volume = None self.cc_peaks = None # self.crystal_gridding = maptbx.crystal_gridding( unit_cell=xray_structure.unit_cell(), space_group_info=xray_structure.space_group_info(), pre_determined_n_real=map.accessor().all(), symmetry_flags=maptbx.use_space_group_symmetry) self.atom_radius = None bs_mask = masks.mask_from_xray_structure( xray_structure=self.xray_structure, p1=True, for_structure_factors=False, n_real=self.map.accessor().all()).mask_data maptbx.unpad_in_place(map=bs_mask) self.sel_inside = (bs_mask == 0.).iselection() self.n_nodes_inside = self.sel_inside.size() del bs_mask # map_calc = self.get_map_calc() # if (compute_cc_mask): self.cc_mask = from_map_map_selection(map_1=self.map, map_2=map_calc, selection=self.sel_inside) del self.sel_inside if (compute_cc_box): self.cc_box = from_map_map(map_1=self.map, map_2=map_calc) if (compute_cc_image): self.atom_radius = self._atom_radius() self.cc_image = self._cc_image(map_calc=map_calc) if (compute_cc_volume): self.cc_volume = self._cc_volume(map_calc=map_calc) if (compute_cc_peaks): self.cc_peaks = self._cc_peaks(map_calc=map_calc)
def __init__( self, xray_structure, solvent_radius, shrink_truncation_radius, ignore_hydrogen_atoms=False, crystal_gridding=None, grid_step=None, d_min=None, resolution_factor=1 / 4, atom_radii_table=None, use_space_group_symmetry=False, ): self.xray_structure = xray_structure if crystal_gridding is None: self.crystal_gridding = maptbx.crystal_gridding( unit_cell=xray_structure.unit_cell(), space_group_info=xray_structure.space_group_info(), step=grid_step, d_min=d_min, resolution_factor=resolution_factor, symmetry_flags=sgtbx.search_symmetry_flags(use_space_group_symmetry=use_space_group_symmetry), ) else: self.crystal_gridding = crystal_gridding if use_space_group_symmetry: atom_radii = cctbx.masks.vdw_radii(xray_structure, table=atom_radii_table).atom_radii asu_mappings = xray_structure.asu_mappings(buffer_thickness=flex.max(atom_radii) + solvent_radius) scatterers_asu_plus_buffer = flex.xray_scatterer() frac = xray_structure.unit_cell().fractionalize for sc, mappings in zip(xray_structure.scatterers(), asu_mappings.mappings()): for mapping in mappings: scatterers_asu_plus_buffer.append(sc.customized_copy(site=frac(mapping.mapped_site()))) xs = xray.structure(crystal_symmetry=xray_structure, scatterers=scatterers_asu_plus_buffer) else: xs = xray_structure.expand_to_p1() self.vdw_radii = cctbx.masks.vdw_radii(xs, table=atom_radii_table) self.mask = cctbx.masks.around_atoms( unit_cell=xs.unit_cell(), space_group_order_z=xs.space_group().order_z(), sites_frac=xs.sites_frac(), atom_radii=self.vdw_radii.atom_radii, gridding_n_real=self.crystal_gridding.n_real(), solvent_radius=solvent_radius, shrink_truncation_radius=shrink_truncation_radius, ) if use_space_group_symmetry: tags = self.crystal_gridding.tags() tags.tags().apply_symmetry_to_mask(self.mask.data) self.flood_fill = cctbx.masks.flood_fill(self.mask.data, xray_structure.unit_cell()) self.exclude_void_flags = [False] * self.flood_fill.n_voids() self.solvent_accessible_volume = ( self.n_solvent_grid_points() / self.mask.data.size() * xray_structure.unit_cell().volume() )
def crystal_gridding(self, assert_shannon_sampling=True): if (self._crystal_gridding is None): self._crystal_gridding = maptbx.crystal_gridding( unit_cell=self.unit_cell(), d_min=self.d_min(), resolution_factor=self.grid_resolution_factor(), symmetry_flags=self.symmetry_flags(), space_group_info=self.space_group_info(), mandatory_factors=self.mandatory_grid_factors(), max_prime=self.max_prime(), assert_shannon_sampling=assert_shannon_sampling) return self._crystal_gridding
def prepare_reference_map_3(self): """ with ramachandran outliers """ xrs = self.model.get_xray_structure().deep_copy_scatterers() pdb_h = self.model.get_hierarchy() print("Preparing reference map, method 3", file=self.log) outlier_selection_txt = mmtbx.building.loop_closure.utils. \ rama_score_selection(pdb_h, self.model.get_ramachandran_manager(), "outlier",1) asc = self.model.get_atom_selection_cache() # print >> self.log, "rama outlier selection:", outlier_selection_txt rama_out_sel = asc.selection(outlier_selection_txt) xrs=xrs.set_b_iso(value=50) # side_chain_no_cb_selection = ~ xrs.main_chain_selection() side_chain_no_cb_selection = ~ xrs.backbone_selection() xrs = xrs.set_b_iso(value=200, selection=side_chain_no_cb_selection) xrs = xrs.set_b_iso(value=150, selection=rama_out_sel) # xrs = xrs.set_occupancies(value=0.3, selection=rama_out_sel) crystal_gridding = maptbx.crystal_gridding( unit_cell = xrs.unit_cell(), space_group_info = xrs.space_group_info(), symmetry_flags = maptbx.use_space_group_symmetry, d_min = self.params.reference_map_resolution) fc = xrs.structure_factors( d_min = self.params.reference_map_resolution, algorithm = "direct").f_calc() fft_map = miller.fft_map( crystal_gridding=crystal_gridding, fourier_coefficients=fc) fft_map.apply_sigma_scaling() self.reference_map = fft_map.real_map_unpadded(in_place=False) if self.params.debug: fft_map.as_xplor_map(file_name="%s_3.map" % self.params.output_prefix) self.master_map = self.reference_map.deep_copy() if self.model.ncs_constraints_present(): # here we are negating non-master part of the model # self.master_sel=master_sel # self.master_map = self.reference_map.deep_copy() mask = maptbx.mask( xray_structure=xrs.select(self.model.get_master_selection()), n_real=self.master_map.focus(), mask_value_inside_molecule=1, mask_value_outside_molecule=-1, solvent_radius=0, atom_radius=1.) self.master_map = self.reference_map * mask if self.params.debug: iotbx.mrcfile.write_ccp4_map( file_name="%s_3_master.map" % self.params.output_prefix, unit_cell=xrs.unit_cell(), space_group=xrs.space_group(), map_data=self.master_map, labels=flex.std_string([""]))
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 start(self, f_obs, phases, f_000=0): self.f_obs = f_obs self.crystal_gridding = maptbx.crystal_gridding( unit_cell=self.f_obs.unit_cell(), space_group_info=sgtbx.space_group_info('P1'), d_min=self.f_obs.d_min(), resolution_factor=1 / 2, symmetry_flags=maptbx.use_space_group_symmetry) self.fft_scale = (self.f_obs.crystal_symmetry().unit_cell().volume() / self.crystal_gridding.n_grid_points()) self.f_calc = self.f_obs.phase_transfer(phases) self.f_000 = f_000 self.compute_electron_density_map()
def start(self, f_obs, phases, f_000=0): self.f_obs = f_obs self.crystal_gridding = maptbx.crystal_gridding( unit_cell=self.f_obs.unit_cell(), space_group_info=sgtbx.space_group_info('P1'), d_min=self.f_obs.d_min(), resolution_factor=1/2, symmetry_flags=maptbx.use_space_group_symmetry) self.fft_scale = (self.f_obs.crystal_symmetry().unit_cell().volume() / self.crystal_gridding.n_grid_points()) self.f_calc = self.f_obs.phase_transfer(phases) self.f_000 = f_000 self.compute_electron_density_map()
def _filter_by_map_model_cc(self): self.ma.add(" start: %d" % self.xrs_water.scatterers().size()) # Compute model Fourier map m_all = self.model.deep_copy() m_all.add_solvent(solvent_xray_structure=self.xrs_water, atom_name="O", residue_name="HOH", chain_id="S", refine_adp="isotropic") m_all.setup_scattering_dictionaries( scattering_table=self.scattering_table) xrs_all = m_all.get_xray_structure() f_calc = xrs_all.structure_factors(d_min=self.resolution).f_calc() crystal_gridding = maptbx.crystal_gridding( unit_cell=xrs_all.unit_cell(), space_group_info=xrs_all.space_group_info(), pre_determined_n_real=self.map_data.accessor().all()) fft_map = miller.fft_map(crystal_gridding=crystal_gridding, fourier_coefficients=f_calc) map_calc = fft_map.real_map_unpadded() sites_cart = xrs_all.sites_cart() # Remove water by CC wsel = m_all.selection(string="water") for i, s in enumerate(wsel): if not s: continue # XXX cc = mmtbx.maps.correlation.from_map_map_atom( map_1=self.map_data_resampled, map_2=map_calc, site_cart=sites_cart[i], unit_cell=xrs_all.unit_cell(), radius=self.atom_radius) if cc < self.cc_mask_filter_threshold: wsel[i] = False self.xrs_water = m_all.select(wsel).get_xray_structure() # Exclude poor macro-molecule atoms from interaction analysis sites_cart = self.model.get_sites_cart() for i, s in enumerate(self.interaction_selection): if not s: continue # XXX cc = mmtbx.maps.correlation.from_map_map_atom( map_1=self.map_data_resampled, map_2=map_calc, site_cart=sites_cart[i], unit_cell=xrs_all.unit_cell(), radius=self.atom_radius) if cc < self.cc_mask_threshold_interacting_atoms: self.interaction_selection[i] = False self.sites_frac_interaction = self.model.get_sites_frac().select( self.interaction_selection) # self.ma.add(" final: %d" % self.xrs_water.scatterers().size())
def exercise_mask_data_3(space_group_info, n_sites=100, d_min=2.0, resolution_factor=1. / 4): from cctbx import maptbx xrs = random_structure.xray_structure( space_group_info=space_group_info, elements=(("O", "N", "C") * (n_sites // 3 + 1))[:n_sites], volume_per_atom=50, min_distance=1.5) crystal_gridding = maptbx.crystal_gridding( unit_cell=xrs.unit_cell(), space_group_info=xrs.space_group_info(), symmetry_flags=maptbx.use_space_group_symmetry, resolution_factor=resolution_factor, d_min=d_min) n_real = crystal_gridding.n_real() dummy_set = xrs.structure_factors(d_min=d_min).f_calc() xrs.shake_sites_in_place(mean_distance=10) xrs_p1 = xrs.expand_to_p1(sites_mod_positive=True) mo1 = mmtbx.masks.mask_from_xray_structure(xray_structure=xrs, p1=False, solvent_radius=1, shrink_truncation_radius=1, for_structure_factors=True, n_real=n_real) asu_mask, mask_data1 = mo1.asu_mask, mo1.mask_data assert mask_data1.focus() == n_real # get Fmask option 1 f_mask_1 = dummy_set.set().array( data=asu_mask.structure_factors(dummy_set.indices())) # get Fmask option 2 f_mask_2 = dummy_set.structure_factors_from_map(map=mask_data1, use_scale=True, anomalous_flag=False, use_sg=True) # get Fmask option 3 mo3 = mmtbx.masks.mask_from_xray_structure(xray_structure=xrs, p1=True, solvent_radius=1, shrink_truncation_radius=1, for_structure_factors=True, n_real=n_real) f_mask_3 = dummy_set.structure_factors_from_map( map=mo3.mask_data, use_scale=True, anomalous_flag=False, use_sg=False) # Note use_sg = False ! # assert approx_equal(f_mask_1.data(), f_mask_2.data()) assert approx_equal(f_mask_1.data(), f_mask_3.data())
def get_mask_data(xrs, d_min): crystal_gridding = maptbx.crystal_gridding( unit_cell=xrs.unit_cell(), d_min=d_min, resolution_factor=1. / 4, symmetry_flags=maptbx.use_space_group_symmetry, space_group_info=xrs.space_group_info()) mp = mmtbx.masks.mask_master_params.extract() return mmtbx.masks.mask_from_xray_structure( xray_structure=xrs, p1=True, solvent_radius=mp.solvent_radius, shrink_truncation_radius=mp.shrink_truncation_radius, for_structure_factors=True, n_real=crystal_gridding.n_real()).mask_data
def exercise_mask_data_1(space_group_info, n_sites=100): from cctbx import maptbx from cctbx.masks import vdw_radii_from_xray_structure for d_min in [1, 1.5, 2.1]: for resolution_factor in [1. / 2, 1. / 3, 1. / 4, 1. / 5]: xrs = 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) atom_radii = vdw_radii_from_xray_structure(xray_structure=xrs) asu_mask = masks.atom_mask(unit_cell=xrs.unit_cell(), group=xrs.space_group(), resolution=d_min, grid_step_factor=resolution_factor, solvent_radius=1.0, shrink_truncation_radius=1.0) asu_mask.compute(xrs.sites_frac(), atom_radii) mask_data = asu_mask.mask_data_whole_uc() assert flex.min(mask_data) == 0.0 # It's not just 0 and 1 ... assert flex.max(mask_data) == xrs.space_group().order_z() # In fact, it is a mixture ... if 0: # XXX this will rightfully crash mask_data_ = mask_data / xrs.space_group().order_z() s0 = mask_data_ < 0.5 s1 = mask_data_ > 0.5 if (mask_data_.size() != s0.count(True) + s1.count(True)): for d in mask_data_: if (d != 0 and d != 1): print(d, xrs.space_group().order_z()) assert mask_data_.size( ) == s0.count(True) + s1.count(True), [ mask_data_.size() - (s0.count(True) + s1.count(True)) ] if ( 0 ): # XXX This would crash with the message: "... The grid is not ..." cr_gr = maptbx.crystal_gridding( unit_cell=xrs.unit_cell(), d_min=d_min, resolution_factor=resolution_factor) asu_mask = masks.atom_mask(unit_cell=xrs.unit_cell(), space_group=xrs.space_group(), gridding_n_real=cr_gr.n_real(), solvent_radius=1.0, shrink_truncation_radius=1.0) asu_mask.compute(xrs.sites_frac(), atom_radii)
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 get_fcalc_map(model, symmetry, d_min, emmap, scatterer="electron"): """Fcalc map generation borrowed from EMRinger""" xrs = model.input.xray_structure_simple(crystal_symmetry=symmetry) if scatterer == "electron": # until ions are added to the electron scattering tables neutralize_scatterers(xrs) xrs.scattering_type_registry(d_min=d_min, table=scatterer) fc = xrs.structure_factors(d_min=d_min).f_calc() cg = maptbx.crystal_gridding(unit_cell=symmetry.unit_cell(), space_group_info=symmetry.space_group_info(), pre_determined_n_real=emmap.data.all()) fc_map = fc.fft_map( crystal_gridding=cg).apply_sigma_scaling().real_map_unpadded() scale = len(fc_map) / flex.sum(flex.abs(fc_map)) fc_map_scaled = fc_map * scale return fc_map_scaled
def exercise_crystal_gridding(): crystal_symmetry = crystal.symmetry( unit_cell=(95.2939, 95.2939, 98.4232, 94.3158, 115.226, 118.822), space_group_symbol="Hall: C 2y (x+y,-x+y+z,z)") for mandatory_factors,n_real in ((None,(90,90,90)), ((20,20,20),(100,100,100))): crystal_gridding = maptbx.crystal_gridding( unit_cell=crystal_symmetry.unit_cell(), d_min=3.5, resolution_factor=1/3., symmetry_flags=maptbx.use_space_group_symmetry, space_group_info=crystal_symmetry.space_group_info(), mandatory_factors=mandatory_factors, max_prime=5, assert_shannon_sampling=True) assert crystal_gridding.n_real() == n_real
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_crystal_gridding(): crystal_symmetry = crystal.symmetry( unit_cell=(95.2939, 95.2939, 98.4232, 94.3158, 115.226, 118.822), space_group_symbol="Hall: C 2y (x+y,-x+y+z,z)") for mandatory_factors, n_real in ((None, (90, 90, 90)), ((20, 20, 20), (100, 100, 100))): crystal_gridding = maptbx.crystal_gridding( unit_cell=crystal_symmetry.unit_cell(), d_min=3.5, resolution_factor=1 / 3., symmetry_flags=maptbx.use_space_group_symmetry, space_group_info=crystal_symmetry.space_group_info(), mandatory_factors=mandatory_factors, max_prime=5, assert_shannon_sampling=True) assert crystal_gridding.n_real() == n_real
def scale_two_real_maps_in_fourier_space(m1, m2, cs, d_min, vector_map): f1 = maptbx.map_to_map_coefficients(m=m1, cs=cs, d_min=d_min) f2 = maptbx.map_to_map_coefficients(m=m2, cs=cs, d_min=d_min) if (vector_map): f2 = f2.phase_transfer(phase_source=f1) ss = 1. / flex.pow2(f1.d_spacings().data()) / 4. bs = flex.double([i for i in xrange(0, 100)]) mc = mmtbx.bulk_solvent.complex_f_minus_f_kb_scaled( f1.data(), f2.data(), bs, ss) crystal_gridding = maptbx.crystal_gridding( unit_cell=cs.unit_cell(), space_group_info=cs.space_group_info(), pre_determined_n_real=m1.all()) fft_map = miller.fft_map(crystal_gridding=crystal_gridding, fourier_coefficients=f1.array(data=mc)) return fft_map.real_map_unpadded()
def exercise_d99(): pdb_inp = iotbx.pdb.input(source_info=None, lines=pdb_str) ph = pdb_inp.construct_hierarchy() xrs = ph.extract_xray_structure( crystal_symmetry=pdb_inp.crystal_symmetry()) crystal_gridding = maptbx.crystal_gridding( unit_cell=xrs.unit_cell(), space_group_info=xrs.space_group_info(), resolution_factor=0.25, d_min=2., symmetry_flags=maptbx.use_space_group_symmetry) fc = xrs.structure_factors(d_min=2.).f_calc() fft_map = fc.fft_map(crystal_gridding=crystal_gridding) map = fft_map.real_map_unpadded() # o = maptbx.d99(map=map, crystal_symmetry=xrs.crystal_symmetry()) assert approx_equal(o.result.d999, 2.0, 0.03)
def run(args, log=sys.stdout): print >> log, "-" * 79 print >> log, legend print >> log, "-" * 79 inputs = mmtbx.utils.process_command_line_args( args=args, master_params=master_params()) inputs.params.show(prefix=" ", out=log) print >> log file_names = inputs.pdb_file_names if (len(file_names) != 1): raise Sorry("A PDB file is expected.") pdb_inp = iotbx.pdb.input(file_name=file_names[0]) awl = list(pdb_inp.atoms_with_labels()) xrs = pdb_inp.xray_structure_simple().expand_to_p1(sites_mod_positive=True) # Check for B=0 bs = xrs.extract_u_iso_or_u_equiv() sel_zero = bs < 1.e-3 n_zeros = sel_zero.count(True) if (n_zeros > 0): print "Atoms with B=0:" for i_seq in sel_zero.iselection(): print awl[i_seq].format_atom_record() raise Sorry("Input model contains %d atoms with B=0" % n_zeros) # params = inputs.params.extract() mmtbx.utils.setup_scattering_dictionaries( scattering_table=params.scattering_table, xray_structure=xrs, d_min=0.5) # 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=params.grid_step) m = mmtbx.real_space.sampled_model_density( xray_structure=xrs, n_real=crystal_gridding.n_real()) map_data = m.data() # prefix = "model_map" if (params.output_file_name_prefix is not None): prefix = params.output_file_name_prefix # m.write_as_xplor_map(file_name="%s.xplor" % prefix) fem.ccp4_map(cg=crystal_gridding, file_name="%s.ccp4" % prefix, map_data=map_data)
def exercise_mask_data_3(space_group_info, n_sites=100, d_min=2.0, resolution_factor=1./4): from cctbx import maptbx xrs = random_structure.xray_structure( space_group_info=space_group_info, elements=(("O","N","C")*(n_sites//3+1))[:n_sites], volume_per_atom=50, min_distance=1.5) crystal_gridding = maptbx.crystal_gridding( unit_cell = xrs.unit_cell(), space_group_info = xrs.space_group_info(), symmetry_flags = maptbx.use_space_group_symmetry, resolution_factor = resolution_factor, d_min = d_min) n_real = crystal_gridding.n_real() dummy_set = xrs.structure_factors(d_min = d_min).f_calc() xrs.shake_sites_in_place(mean_distance=10) xrs_p1 = xrs.expand_to_p1(sites_mod_positive=True) mo1 = mmtbx.masks.mask_from_xray_structure( xray_structure=xrs, p1=False, solvent_radius=1, shrink_truncation_radius=1, for_structure_factors=True, n_real=n_real) asu_mask, mask_data1 = mo1.asu_mask, mo1.mask_data assert mask_data1.focus()==n_real # get Fmask option 1 f_mask_1 = dummy_set.set().array( data = asu_mask.structure_factors(dummy_set.indices())) # get Fmask option 2 f_mask_2 = dummy_set.structure_factors_from_map(map=mask_data1, use_scale = True, anomalous_flag = False, use_sg = True) # get Fmask option 3 mo3 = mmtbx.masks.mask_from_xray_structure( xray_structure=xrs, p1=True, solvent_radius=1, shrink_truncation_radius=1, for_structure_factors=True, n_real=n_real) f_mask_3 = dummy_set.structure_factors_from_map(map=mo3.mask_data, use_scale = True, anomalous_flag = False, use_sg = False) # Note use_sg = False ! # assert approx_equal(f_mask_1.data(), f_mask_2.data()) assert approx_equal(f_mask_1.data(), f_mask_3.data())
def run(args, log=sys.stdout): print >> log, "-"*79 print >> log, legend print >> log, "-"*79 inputs = mmtbx.utils.process_command_line_args(args = args, master_params = master_params()) inputs.params.show(prefix=" ", out=log) print >> log file_names = inputs.pdb_file_names if(len(file_names) != 1): raise Sorry("A PDB file is expected.") pdb_inp = iotbx.pdb.input(file_name = file_names[0]) awl = list(pdb_inp.atoms_with_labels()) xrs = pdb_inp.xray_structure_simple().expand_to_p1(sites_mod_positive=True) # Check for B=0 bs = xrs.extract_u_iso_or_u_equiv() sel_zero = bs<1.e-3 n_zeros = sel_zero.count(True) if(n_zeros>0): print "Atoms with B=0:" for i_seq in sel_zero.iselection(): print awl[i_seq].format_atom_record() raise Sorry("Input model contains %d atoms with B=0"%n_zeros) # params = inputs.params.extract() mmtbx.utils.setup_scattering_dictionaries( scattering_table = params.scattering_table, xray_structure = xrs, d_min = 0.5) # 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 = params.grid_step) m = mmtbx.real_space.sampled_model_density( xray_structure = xrs, n_real = crystal_gridding.n_real()) map_data = m.data() # prefix = "model_map" if(params.output_file_name_prefix is not None): prefix = params.output_file_name_prefix # m.write_as_xplor_map(file_name = "%s.xplor"%prefix) fem.ccp4_map(cg=crystal_gridding, file_name="%s.ccp4"%prefix, map_data=map_data)
def exercise_mask_data_1(space_group_info, n_sites=100): from cctbx import maptbx from cctbx.masks import vdw_radii_from_xray_structure for d_min in [1, 1.5, 2.1]: for resolution_factor in [1./2, 1./3, 1./4, 1./5]: xrs = 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) atom_radii = vdw_radii_from_xray_structure(xray_structure = xrs) asu_mask = masks.atom_mask( unit_cell = xrs.unit_cell(), group = xrs.space_group(), resolution = d_min, grid_step_factor = resolution_factor, solvent_radius = 1.0, shrink_truncation_radius = 1.0) asu_mask.compute(xrs.sites_frac(), atom_radii) mask_data = asu_mask.mask_data_whole_uc() assert flex.min(mask_data) == 0.0 # It's not just 0 and 1 ... assert flex.max(mask_data) == xrs.space_group().order_z() # In fact, it is a mixture ... if 0: # XXX this will rightfully crash mask_data_ = mask_data / xrs.space_group().order_z() s0 = mask_data_ < 0.5 s1 = mask_data_ > 0.5 if(mask_data_.size() != s0.count(True)+s1.count(True)): for d in mask_data_: if(d != 0 and d != 1): print d, xrs.space_group().order_z() assert mask_data_.size() == s0.count(True)+s1.count(True), [ mask_data_.size()-(s0.count(True)+s1.count(True))] if(0): # XXX This would crash with the message: "... The grid is not ..." cr_gr = maptbx.crystal_gridding( unit_cell = xrs.unit_cell(), d_min = d_min, resolution_factor = resolution_factor) asu_mask = masks.atom_mask( unit_cell = xrs.unit_cell(), space_group = xrs.space_group(), gridding_n_real = cr_gr.n_real(), solvent_radius = 1.0, shrink_truncation_radius = 1.0) asu_mask.compute(xrs.sites_frac(), atom_radii)
def prepare_reference_map(self, xrs, pdb_h): print >> self.log, "Preparing reference map" # new_h = pdb_h.deep_copy() # truncate_to_poly_gly(new_h) # xrs = new_h.extract_xray_structure(crystal_symmetry=xrs.crystal_symmetry()) xrs=xrs.set_b_iso(value=50) crystal_gridding = maptbx.crystal_gridding( unit_cell = xrs.unit_cell(), space_group_info = xrs.space_group_info(), symmetry_flags = maptbx.use_space_group_symmetry, d_min = self.params.reference_map_resolution) fc = xrs.structure_factors(d_min = self.params.reference_map_resolution, algorithm = "fft").f_calc() fft_map = miller.fft_map( crystal_gridding=crystal_gridding, fourier_coefficients=fc) fft_map.apply_sigma_scaling() self.reference_map = fft_map.real_map_unpadded(in_place=False) fft_map.as_xplor_map(file_name="%s.map" % self.params.output_prefix)
def get_mask_1(fmodel, grid_step_factor): grid_step = fmodel.f_obs().d_min()*(1./grid_step_factor) if(grid_step < 0.15): grid_step = 0.15 grid_step = min(0.8, grid_step) crystal_gridding = maptbx.crystal_gridding( unit_cell = fmodel.xray_structure.unit_cell(), space_group_info = fmodel.xray_structure.space_group_info(), symmetry_flags = maptbx.use_space_group_symmetry, step = grid_step) n_real = crystal_gridding.n_real() # Compute mask in P1 mask_data_p1 = mmtbx.masks.mask_from_xray_structure( xray_structure = fmodel.xray_structure, p1 = True, for_structure_factors = True, n_real = n_real, in_asu = False).mask_data maptbx.unpad_in_place(map=mask_data_p1) return mask_data_p1, n_real, crystal_gridding
def __init__(self, xray_structure, ignore_zero_occupancy_atoms, solvent_radius, shrink_truncation_radius, ignore_hydrogen_atoms=True, gridding_n_real=None, grid_step=None, atom_radii=None): global number_of_mask_calculations number_of_mask_calculations += 1 assert [gridding_n_real, grid_step].count(None) == 1 self.xray_structure = xray_structure if (gridding_n_real is None): gridding_n_real = maptbx.crystal_gridding( unit_cell=xray_structure.unit_cell(), step=grid_step).n_real() if(atom_radii is None): atom_radii = vdw_radii_from_xray_structure(xray_structure = self.xray_structure) sites_frac = xray_structure.sites_frac() self.n_atoms_excluded = 0 selection = flex.bool(xray_structure.scatterers().size(), True) if(ignore_zero_occupancy_atoms): selection &= xray_structure.scatterers().extract_occupancies() > 0 if(ignore_hydrogen_atoms): selection &= (~xray_structure.hd_selection()) sites_frac = sites_frac.select(selection) atom_radii = atom_radii.select(selection) self.n_atoms_excluded = selection.count(False) around_atoms.__init__(self, unit_cell = xray_structure.unit_cell(), space_group_order_z = xray_structure.space_group().order_z(), sites_frac = sites_frac, atom_radii = atom_radii, gridding_n_real = gridding_n_real, solvent_radius = solvent_radius, shrink_truncation_radius = shrink_truncation_radius) introspection.virtual_memory_info().update_max()
def get_mask_2(fmodel, grid_step_factor): sgt = fmodel.xray_structure.space_group().type() mask_params = mmtbx.masks.mask_master_params.extract() mask_params.grid_step_factor = grid_step_factor asu_mask_obj = mmtbx.masks.asu_mask( xray_structure = fmodel.xray_structure, d_min = fmodel.f_obs().d_min(), mask_params = mask_params).asu_mask mask_data_p1 = asu_mask_obj.mask_data_whole_uc() maptbx.unpad_in_place(map=mask_data_p1) n_real = mask_data_p1.all() crystal_gridding = maptbx.crystal_gridding( unit_cell = fmodel.xray_structure.unit_cell(), space_group_info = fmodel.xray_structure.space_group_info(), symmetry_flags = maptbx.use_space_group_symmetry, pre_determined_n_real = n_real) #n_real = mask_data_p1.all() #mask_data_p1 = asu_map_ext.asymmetric_map(sgt, mask_data_p1, n_real).symmetry_expanded_map() #maptbx.unpad_in_place(map=mask_data_p1) return mask_data_p1, n_real, crystal_gridding
def exercise1(): pdb_str=""" CRYST1 10.000 10.000 10.000 90.00 90.00 90.00 P 1 HETATM 1 C C 1 2.000 2.000 2.000 1.00 20.00 C END """ pdb_inp = iotbx.pdb.input(source_info=None, lines=pdb_str) xrs = pdb_inp.xray_structure_simple() cg = maptbx.crystal_gridding(unit_cell=xrs.unit_cell(), pre_determined_n_real=(100,100,100), space_group_info=xrs.space_group_info()) fc = xrs.structure_factors(d_min = 1., algorithm = "direct").f_calc() fft_map = miller.fft_map(crystal_gridding=cg, fourier_coefficients=fc) map_data = fft_map.real_map_unpadded() # pass map and threshold value co = maptbx.connectivity(map_data=map_data, threshold=100.) # get 'map' of the same size with integers: 0 where below threshold, # 1,2,3... - for connected regions map_result = co.result() # to find out the number of connected region for particular point: assert map_result[0,0,0] == 0 # means under threshold assert map_result[20,20,20] == 1 # blob 1 # get 1d array of integer volumes and transform it to list. volumes = list(co.regions()) # find max volume (except volume of 0-region which will be probably max) max_volume = max(volumes[1:]) # find number of the region with max volume max_index = volumes.index(max_volume) v=[0,0,0] for i in range(3): # !!! Do not do this because it's extremely slow! Used for test purposes. v[i] = (map_result==i).count(True) assert v[2] == 0 assert v[1] < 15000 assert v[0]+v[1]+v[2] == 1000000 assert volumes == v[:2]
def run(args, log=sys.stdout): print >> log, "-"*79 print >> log, legend print >> log, "-"*79 inputs = mmtbx.utils.process_command_line_args(args = args, master_params = master_params()) file_names = inputs.pdb_file_names if(len(file_names) != 1): raise Sorry("A PDB file is expected.") xrs = iotbx.pdb.input(file_name = file_names[0]).xray_structure_simple() params = inputs.params.extract() # crystal_gridding = maptbx.crystal_gridding( unit_cell = xrs.unit_cell(), d_min = params.resolution, resolution_factor = params.resolution_factor, symmetry_flags = maptbx.use_space_group_symmetry, space_group_info = xrs.space_group().info()) mmtbx_masks_asu_mask_obj = mmtbx.masks.asu_mask( xray_structure = xrs.expand_to_p1(sites_mod_positive=True), n_real = crystal_gridding.n_real()) bulk_solvent_mask = mmtbx_masks_asu_mask_obj.mask_data_whole_uc() # fem.ccp4_map(cg=crystal_gridding, file_name="mask.ccp4", map_data=bulk_solvent_mask)
def exercise3(): pdb_str=""" CRYST1 10.000 10.000 10.000 90.00 90.00 90.00 P 1 HETATM 1 C C 1 2.000 2.000 2.000 1.00 2.00 C HETATM 1 C C 1 3.500 2.000 2.000 1.00 2.00 C END """ pdb_inp = iotbx.pdb.input(source_info=None, lines=pdb_str) xrs = pdb_inp.xray_structure_simple() cg = maptbx.crystal_gridding(unit_cell=xrs.unit_cell(), pre_determined_n_real=(100,100,100), space_group_info=xrs.space_group_info()) fc = xrs.structure_factors(d_min = 1., algorithm = "direct").f_calc() fft_map = miller.fft_map(crystal_gridding=cg, fourier_coefficients=fc) fft_map.apply_sigma_scaling() map_data = fft_map.real_map_unpadded() #all filled v, volumes = getvs(map_data, -100) v2, volumes2 = getvs(map_data, -100, False) assert v == v2 == [0, 1000000, 0] assert v[:2] == v2[:2] == volumes == volumes2 # can see one blob v, volumes = getvs(map_data, 5) assert v[0]+v[1]+v[2] == 1000000 assert v[2] == 0 assert v[:2] == volumes # can see separate, approx equal volume bloobs v, volumes = getvs(map_data, 10) assert v[0]+v[1]+v[2] == 1000000 assert abs(v[1] - v[2]) < 5 assert v == volumes # nothing to see v, volumes = getvs(map_data, 1000) assert v == [1000000, 0, 0] assert v[:1] == volumes
def run(args, log = sys.stdout, as_gui_program=False): if(len(args)==0): parsed = defaults(log=log) parsed.show(prefix=" ", out=log) return command_line = (option_parser() .enable_symmetry_comprehensive() .option("-q", "--quiet", action="store_true", default=False, help="suppress output") .option("--output_plots", action="store_true", default=False) ).process(args=args) parsed = defaults(log=log) processed_args = mmtbx.utils.process_command_line_args( args=command_line.args, cmd_cs=command_line.symmetry, master_params=parsed, log=log, suppress_symmetry_related_errors=True) processed_args.params.show(out=log) params = processed_args.params.extract().density_modification output_plots = command_line.options.output_plots crystal_symmetry = crystal.symmetry( unit_cell=params.input.unit_cell, space_group_info=params.input.space_group) reflection_files = {} for rfn in (params.input.reflection_data.file_name, params.input.experimental_phases.file_name, params.input.map_coefficients.file_name): if os.path.isfile(str(rfn)) and rfn not in reflection_files: reflection_files.setdefault( rfn, iotbx.reflection_file_reader.any_reflection_file( file_name=rfn, ensure_read_access=False)) server = iotbx.reflection_file_utils.reflection_file_server( crystal_symmetry=crystal_symmetry, reflection_files=reflection_files.values()) fo = mmtbx.utils.determine_data_and_flags( server, parameters=params.input.reflection_data, extract_r_free_flags=False,log=log).f_obs hl_coeffs = mmtbx.utils.determine_experimental_phases( server, params.input.experimental_phases, log=log, parameter_scope="", working_point_group=None, symmetry_safety_check=True, ignore_all_zeros=True) if params.input.map_coefficients.file_name is not None: map_coeffs = server.get_phases_deg( file_name=params.input.map_coefficients.file_name, labels=params.input.map_coefficients.labels, convert_to_phases_if_necessary=False, original_phase_units=None, parameter_scope="", parameter_name="labels").map_to_asu() else: map_coeffs = None ncs_object = None if params.input.ncs_file_name is not None: ncs_object = ncs.ncs() ncs_object.read_ncs(params.input.ncs_file_name) ncs_object.display_all(log=log) fo = fo.map_to_asu() hl_coeffs = hl_coeffs.map_to_asu() fo = fo.eliminate_sys_absent().average_bijvoet_mates() hl_coeffs = hl_coeffs.eliminate_sys_absent().average_bijvoet_mates() model_map = None model_map_coeffs = None if len(processed_args.pdb_file_names): pdb_file = mmtbx.utils.pdb_file( pdb_file_names=processed_args.pdb_file_names) xs = pdb_file.pdb_inp.xray_structure_simple() fo_, hl_ = fo, hl_coeffs if params.change_basis_to_niggli_cell: change_of_basis_op = xs.change_of_basis_op_to_niggli_cell() xs = xs.change_basis(change_of_basis_op) fo_ = fo_.change_basis(change_of_basis_op).map_to_asu() hl_ = hl_.change_basis(change_of_basis_op).map_to_asu() #fo_, hl_ = fo_.common_sets(hl_) fmodel_refined = mmtbx.utils.fmodel_simple( f_obs=fo_, scattering_table="wk1995",#XXX pva: 1) neutrons? 2) move up as a parameter. xray_structures=[xs], bulk_solvent_correction=True, anisotropic_scaling=True, r_free_flags=fo_.array(data=flex.bool(fo_.size(), False))) fmodel_refined.update(abcd=hl_) master_phil = mmtbx.maps.map_and_map_coeff_master_params() map_params = master_phil.fetch(iotbx.phil.parse("""\ map_coefficients { map_type = 2mFo-DFc isotropize = True } """)).extract().map_coefficients[0] model_map_coeffs = mmtbx.maps.map_coefficients_from_fmodel( fmodel=fmodel_refined, params=map_params) model_map = model_map_coeffs.fft_map( resolution_factor=params.grid_resolution_factor).real_map_unpadded() import time t0 = time.time() dm = density_modify( params, fo, hl_coeffs, ncs_object=ncs_object, map_coeffs=map_coeffs, model_map_coeffs=model_map_coeffs, log=log, as_gui_program=as_gui_program) time_dm = time.time()-t0 print >> log, "Time taken for density modification: %.2fs" %time_dm # run cns if 0: from cctbx.development import cns_density_modification cns_result = cns_density_modification.run(params, fo, hl_coeffs) print cns_result.modified_map.all() print dm.map.all() dm_map_coeffs = dm.map_coeffs_in_original_setting from cctbx import maptbx, miller crystal_gridding = maptbx.crystal_gridding( dm_map_coeffs.unit_cell(), space_group_info=dm_map_coeffs.space_group().info(), pre_determined_n_real=cns_result.modified_map.all()) dm_map = miller.fft_map(crystal_gridding, dm_map_coeffs).apply_sigma_scaling() corr = flex.linear_correlation(cns_result.modified_map.as_1d(), dm_map.real_map_unpadded().as_1d()) print "CNS dm/mmtbx dm correlation:" corr.show_summary() if dm.model_map_coeffs is not None: model_map = miller.fft_map( crystal_gridding, dm.miller_array_in_original_setting(dm.model_map_coeffs)).apply_sigma_scaling() corr = flex.linear_correlation(cns_result.modified_map.as_1d(), model_map.real_map_unpadded().as_1d()) print "CNS dm/model correlation:" corr.show_summary() if output_plots: plots_to_make = ( "fom", "skewness", "r1_factor", "r1_factor_fom", "mean_solvent_density", "mean_protein_density", "f000_over_v", "k_flip", "rms_solvent_density", "rms_protein_density", "standard_deviation_local_rms", "mean_delta_phi", "mean_delta_phi_initial", ) from matplotlib.backends.backend_pdf import PdfPages from libtbx import pyplot stats = dm.get_stats() pdf = PdfPages("density_modification.pdf") if len(dm.correlation_coeffs) > 1: if 0: start_coeffs, model_coeffs = dm.map_coeffs_start.common_sets(model_map_coeffs) model_phases = model_coeffs.phases(deg=True).data() exptl_phases = nearest_phase( model_phases, start_coeffs.phases(deg=True).data(), deg=True) corr = flex.linear_correlation(exptl_phases, model_phases) corr.show_summary() fig = pyplot.figure() ax = fig.add_subplot(1,1,1) ax.set_title("phases start") ax.set_xlabel("Experimental phases") ax.set_ylabel("Phases from refined model") ax.scatter(exptl_phases, model_phases, marker="x", s=10) pdf.savefig(fig) # dm_coeffs, model_coeffs = dm.map_coeffs.common_sets(model_map_coeffs) model_phases = model_coeffs.phases(deg=True).data() dm_phases = nearest_phase( model_phases, dm_coeffs.phases(deg=True).data(), deg=True) corr = flex.linear_correlation(dm_phases, model_phases) corr.show_summary() fig = pyplot.figure() ax = fig.add_subplot(1,1,1) ax.set_title("phases dm") ax.set_xlabel("Phases from density modification") ax.set_ylabel("Phases from refined model") ax.scatter(dm_phases, model_phases, marker="x", s=10) pdf.savefig(fig) # data = dm.correlation_coeffs fig = pyplot.figure() ax = fig.add_subplot(1,1,1) ax.set_title("correlation coefficient") ax.plot(range(1, dm.i_cycle+2), data) pdf.savefig(fig) # data = dm.mean_phase_errors fig = pyplot.figure() ax = fig.add_subplot(1,1,1) ax.set_title("Mean effective phase errors") ax.plot(range(1, dm.i_cycle+2), data) pdf.savefig(fig) for plot in plots_to_make: data = [getattr(stats.get_cycle_stats(i), plot) for i in range(1, dm.i_cycle+2)] fig = pyplot.figure() ax = fig.add_subplot(1,1,1) ax.set_title(plot.replace("_", " ")) ax.plot(range(1, dm.i_cycle+2), data) pdf.savefig(fig) data = [stats.get_cycle_stats(i).rms_solvent_density/ stats.get_cycle_stats(i).rms_protein_density for i in range(1, dm.i_cycle+2)] fig = pyplot.figure() ax = fig.add_subplot(1,1,1) ax.set_title("RMS solvent/protein density ratio") ax.plot(range(1, dm.i_cycle+2), data) pdf.savefig(fig) pdf.close() dm_map_coeffs = dm.map_coeffs_in_original_setting dm_hl_coeffs = dm.hl_coeffs_in_original_setting # output map if requested map_params = params.output.map if map_params.file_name is not None: fft_map = dm_map_coeffs.fft_map(resolution_factor=params.grid_resolution_factor) if map_params.scale == "sigma": fft_map.apply_sigma_scaling() else: fft_map.apply_volume_scaling() gridding_first = gridding_last = None title_lines = [] if map_params.format == "xplor": fft_map.as_xplor_map( file_name = map_params.file_name, title_lines = title_lines, gridding_first = gridding_first, gridding_last = gridding_last) else : fft_map.as_ccp4_map( file_name = map_params.file_name, gridding_first = gridding_first, gridding_last = gridding_last, labels=title_lines) # output map coefficients if requested mtz_params = params.output.mtz # Decide if we are going to actually write the mtz if mtz_params.file_name is not None: orig_fom,final_fom=dm.start_and_end_fom() if mtz_params.skip_output_if_worse and final_fom < orig_fom: ok_to_write_mtz=False print "Not writing out mtz. Final FOM (%7.3f) worse than start (%7.3f)" %( final_fom,orig_fom) else: # usual ok_to_write_mtz=True else: ok_to_write_mtz=True if mtz_params.file_name is not None and ok_to_write_mtz: label_decorator=iotbx.mtz.ccp4_label_decorator() fo = dm.miller_array_in_original_setting(dm.f_obs_complete).common_set(dm_map_coeffs) mtz_dataset = fo.as_mtz_dataset( column_root_label="F", label_decorator=label_decorator) mtz_dataset.add_miller_array( dm_map_coeffs, column_root_label="FWT", label_decorator=label_decorator) phase_source = dm.miller_array_in_original_setting(dm.phase_source).common_set(dm_map_coeffs) mtz_dataset.add_miller_array( phase_source.array(data=flex.abs(phase_source.data())), column_root_label="FOM", column_types='W', label_decorator=label_decorator) mtz_dataset.add_miller_array( phase_source.array(data=phase_source.phases(deg=True).data()), column_root_label="PHIB", column_types='P', label_decorator=None) if mtz_params.output_hendrickson_lattman_coefficients: mtz_dataset.add_miller_array( dm_hl_coeffs, column_root_label="HL", label_decorator=label_decorator) mtz_dataset.mtz_object().write(mtz_params.file_name) return result( map_file=map_params.file_name, mtz_file=mtz_params.file_name, stats=dm.get_stats())
def run (args, out=sys.stdout) : cmdline = iotbx.phil.process_command_line_with_files( args=args, master_phil_string=master_phil_str, pdb_file_def="model", map_file_def="map", usage_string="""\ em_rscc.py model.pdb map.ccp4 %s""" % __doc__) params = cmdline.work.extract() assert (not None in [params.model, params.map]) pdb_in = cmdline.get_file(params.model).file_object m = cmdline.get_file(params.map).file_object print >> out, "Input electron density map:" 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 symm = crystal.symmetry( space_group_symbol="P1", unit_cell=m.unit_cell_parameters) xrs = pdb_in.input.xray_structure_simple(crystal_symmetry=symm) print >> out, "Setting up electron scattering table (d_min=%g)" % params.d_min xrs.scattering_type_registry( d_min=params.d_min, table="electron") fc = xrs.structure_factors(d_min=params.d_min).f_calc() cg = maptbx.crystal_gridding( unit_cell=symm.unit_cell(), space_group_info=symm.space_group_info(), pre_determined_n_real=m.data.all()) fc_map = fc.fft_map( crystal_gridding=cg).apply_sigma_scaling().real_map_unpadded() assert (fc_map.all() == fc_map.focus() == m.data.all()) em_data = m.data.as_double() unit_cell_for_interpolation = m.grid_unit_cell() frac_matrix = unit_cell_for_interpolation.fractionalization_matrix() sites_cart = xrs.sites_cart() sites_frac = xrs.sites_frac() print >> out, "PER-RESIDUE CORRELATION:" for chain in pdb_in.hierarchy.only_model().chains() : for residue_group in chain.residue_groups() : i_seqs = residue_group.atoms().extract_i_seq() values_em = flex.double() values_fc = flex.double() for i_seq in i_seqs : rho_em = maptbx.non_crystallographic_eight_point_interpolation( map=em_data, gridding_matrix=frac_matrix, site_cart=sites_cart[i_seq]) rho_fc = fc_map.eight_point_interpolation(sites_frac[i_seq]) values_em.append(rho_em) values_fc.append(rho_fc) cc = flex.linear_correlation(x=values_em, y=values_fc).coefficient() print >> out, residue_group.id_str(), cc
def __init__ (self, pdb_hierarchy, xray_structure, fmodel, distance_cutoff=4.0, collect_all=True, molprobity_map_params=None) : validation.__init__(self) from mmtbx.real_space_correlation import extract_map_stats_for_single_atoms from cctbx import adptbx from scitbx.matrix import col self.n_bad = 0 self.n_heavy = 0 pdb_atoms = pdb_hierarchy.atoms() if(len(pdb_atoms)>1): assert (not pdb_atoms.extract_i_seq().all_eq(0)) unit_cell = xray_structure.unit_cell() pair_asu_table = xray_structure.pair_asu_table( distance_cutoff = distance_cutoff) asu_mappings = pair_asu_table.asu_mappings() asu_table = pair_asu_table.table() u_isos = xray_structure.extract_u_iso_or_u_equiv() occupancies = xray_structure.scatterers().extract_occupancies() sites_cart = xray_structure.sites_cart() sites_frac = xray_structure.sites_frac() sel_cache = pdb_hierarchy.atom_selection_cache() water_sel = sel_cache.selection("resname HOH and name O") if (molprobity_map_params is not None): # assume parameters have been validated (symmetry of pdb and map matches) two_fofc_map = None fc_map = None d_min = None crystal_gridding = None # read two_fofc_map if (molprobity_map_params.map_file_name is not None): f = any_file(molprobity_map_params.map_file_name) two_fofc_map = f.file_object.map_data() d_min = molprobity_map_params.d_min crystal_gridding = maptbx.crystal_gridding( f.file_object.unit_cell(), space_group_info=space_group_info(f.file_object.space_group_number), pre_determined_n_real=f.file_object.unit_cell_grid) elif (molprobity_map_params.map_coefficients_file_name is not None): f = any_file(molprobity_map_params.map_coefficients_file_name) fourier_coefficients = f.file_server.get_miller_array( molprobity_map_params.map_coefficients_label) crystal_symmetry = fourier_coefficients.crystal_symmetry() d_min = fourier_coefficients.d_min() crystal_gridding = maptbx.crystal_gridding( crystal_symmetry.unit_cell(), d_min, resolution_factor=0.25, space_group_info=crystal_symmetry.space_group_info()) two_fofc_map = miller.fft_map( crystal_gridding=crystal_gridding, fourier_coefficients=fourier_coefficients).apply_sigma_scaling().\ real_map_unpadded() # calculate fc_map assert( (d_min is not None) and (crystal_gridding is not None) ) f_calc = xray_structure.structure_factors(d_min=d_min).f_calc() fc_map = miller.fft_map(crystal_gridding=crystal_gridding, fourier_coefficients=f_calc) fc_map = fc_map.apply_sigma_scaling().real_map_unpadded() map_stats = extract_map_stats_for_single_atoms( pdb_atoms=pdb_atoms, xray_structure=xray_structure, fmodel=None, selection=water_sel, fc_map=fc_map, two_fofc_map=two_fofc_map) else: map_stats = extract_map_stats_for_single_atoms( pdb_atoms=pdb_atoms, xray_structure=xray_structure, fmodel=fmodel, selection=water_sel) waters = [] for i_seq, atom in enumerate(pdb_atoms) : if (water_sel[i_seq]) : rt_mx_i_inv = asu_mappings.get_rt_mx(i_seq, 0).inverse() self.n_total += 1 asu_dict = asu_table[i_seq] nearest_atom = nearest_contact = None for j_seq, j_sym_groups in asu_dict.items() : atom_j = pdb_atoms[j_seq] site_j = sites_frac[j_seq] # Filter out hydrogens if atom_j.element.upper().strip() in ["H", "D"]: continue for j_sym_group in j_sym_groups: rt_mx = rt_mx_i_inv.multiply(asu_mappings.get_rt_mx(j_seq, j_sym_group[0])) site_ji = rt_mx * site_j site_ji_cart = xray_structure.unit_cell().orthogonalize(site_ji) vec_i = col(atom.xyz) vec_ji = col(site_ji_cart) dxyz = abs(vec_i - vec_ji) if (nearest_contact is None) or (dxyz < nearest_contact) : nearest_contact = dxyz nearest_atom = atom_info(pdb_atom=atom_j, symop=rt_mx) w = water( pdb_atom=atom, b_iso=adptbx.u_as_b(u_isos[i_seq]), occupancy=occupancies[i_seq], nearest_contact=nearest_contact, nearest_atom=nearest_atom, score=map_stats.two_fofc_ccs[i_seq], fmodel=map_stats.fmodel_values[i_seq], two_fofc=map_stats.two_fofc_values[i_seq], fofc=map_stats.fofc_values[i_seq], anom=map_stats.anom_values[i_seq], n_hbonds=None) # TODO if (w.is_bad_water()) : w.outlier = True self.n_bad += 1 elif (w.is_heavy_atom()) : w.outlier = True self.n_heavy += 1 if (w.outlier) or (collect_all) : self.results.append(w) self.n_outliers = len(self.results)
def run(args, out=sys.stdout, validated=False): show_citation(out=out) if (len(args) == 0): master_phil.show(out=out) print >> out,\ '\nUsage: phenix.map_comparison <CCP4> <CCP4>\n',\ ' phenix.map_comparison <CCP4> <MTZ> mtz_label_1=<label>\n',\ ' phenix.map_comparison <MTZ 1> mtz_label_1=<label 1> <MTZ 2> mtz_label_2=<label 2>\n' sys.exit() # process arguments params = None input_attributes = ['map_1', 'mtz_1', 'map_2', 'mtz_2'] try: # automatic parsing params = phil.process_command_line_with_files( args=args, master_phil=master_phil).work.extract() except Exception: # map_file_def only handles one map phil from libtbx.phil.command_line import argument_interpreter arg_int = argument_interpreter(master_phil=master_phil) command_line_args = list() map_files = list() for arg in args: if (os.path.isfile(arg)): map_files.append(arg) else: command_line_args.append(arg_int.process(arg)) params = master_phil.fetch(sources=command_line_args).extract() # check if more files are necessary n_defined = 0 for attribute in input_attributes: if (getattr(params.input, attribute) is not None): n_defined += 1 # matches files to phil scope, stops once there is sufficient data for map_file in map_files: if (n_defined < 2): current_map = file_reader.any_file(map_file) if (current_map.file_type == 'ccp4_map'): n_defined += 1 if (params.input.map_1 is None): params.input.map_1 = map_file elif (params.input.map_2 is None): params.input.map_2 = map_file elif (current_map.file_type == 'hkl'): n_defined += 1 if (params.input.mtz_1 is None): params.input.mtz_1 = map_file elif (params.input.mtz_2 is None): params.input.mtz_2 = map_file else: print >> out, 'WARNING: only the first two files are used' break # validate arguments (GUI sets validated to true, no need to run again) assert (params is not None) if (not validated): validate_params(params) # --------------------------------------------------------------------------- # check if maps need to be generated from mtz n_maps = 0 maps = list() map_names = list() for attribute in input_attributes: filename = getattr(params.input, attribute) if (filename is not None): map_names.append(filename) current_map = file_reader.any_file(filename) maps.append(current_map) if (current_map.file_type == 'ccp4_map'): n_maps += 1 # construct maps, if necessary crystal_gridding = None m1 = None m2 = None # 1 map, 1 mtz file if (n_maps == 1): for current_map in maps: if (current_map.file_type == 'ccp4_map'): uc = current_map.file_object.unit_cell() sg_info = space_group_info(current_map.file_object.space_group_number) n_real = current_map.file_object.unit_cell_grid crystal_gridding = maptbx.crystal_gridding( uc, space_group_info=sg_info, pre_determined_n_real=n_real) m1 = current_map.file_object.map_data() if (crystal_gridding is not None): label = None for attribute in [('mtz_1', 'mtz_label_1'), ('mtz_2', 'mtz_label_2')]: filename = getattr(params.input, attribute[0]) label = getattr(params.input, attribute[1]) if ( (filename is not None) and (label is not None) ): break # labels will match currently open mtz file for current_map in maps: if (current_map.file_type == 'hkl'): m2 = miller.fft_map( crystal_gridding=crystal_gridding, fourier_coefficients=current_map.file_server.get_miller_array( label)).apply_sigma_scaling().real_map_unpadded() else: raise Sorry('Gridding is not defined.') # 2 mtz files elif (n_maps == 0): crystal_symmetry = get_crystal_symmetry(maps[0]) d_min = min(get_d_min(maps[0]), get_d_min(maps[1])) crystal_gridding = maptbx.crystal_gridding( crystal_symmetry.unit_cell(), d_min=d_min, resolution_factor=params.options.resolution_factor, space_group_info=crystal_symmetry.space_group_info()) m1 = miller.fft_map( crystal_gridding=crystal_gridding, fourier_coefficients=maps[0].file_server.get_miller_array( params.input.mtz_label_1)).apply_sigma_scaling().real_map_unpadded() m2 = miller.fft_map( crystal_gridding=crystal_gridding, fourier_coefficients=maps[1].file_server.get_miller_array( params.input.mtz_label_2)).apply_sigma_scaling().real_map_unpadded() # 2 maps else: m1 = maps[0].file_object.map_data() m2 = maps[1].file_object.map_data() # --------------------------------------------------------------------------- # analyze maps assert ( (m1 is not None) and (m2 is not None) ) # show general statistics s1 = maptbx.more_statistics(m1) s2 = maptbx.more_statistics(m2) show_overall_statistics(out=out, s=s1, header="Map 1 (%s):"%map_names[0]) show_overall_statistics(out=out, s=s2, header="Map 2 (%s):"%map_names[1]) cc_input_maps = flex.linear_correlation(x = m1.as_1d(), y = m2.as_1d()).coefficient() print >> out, "CC, input maps: %6.4f" % cc_input_maps # compute CCpeak cc_peaks = list() m1_he = maptbx.volume_scale(map = m1, n_bins = 10000).map_data() m2_he = maptbx.volume_scale(map = m2, n_bins = 10000).map_data() cc_quantile = flex.linear_correlation(x = m1_he.as_1d(), y = m2_he.as_1d()).coefficient() print >> out, "CC, quantile rank-scaled (histogram equalized) maps: %6.4f" % \ cc_quantile print >> out, "Peak correlation:" print >> out, " cutoff CCpeak" cutoffs = [i/100. for i in range(1,90)]+ [i/1000 for i in range(900,1000)] for cutoff in cutoffs: cc_peak = maptbx.cc_peak(map_1=m1_he, map_2=m2_he, cutoff=cutoff) print >> out, " %3.2f %7.4f" % (cutoff, cc_peak) cc_peaks.append((cutoff, cc_peak)) # compute discrepancy function (D-function) discrepancies = list() cutoffs = flex.double(cutoffs) df = maptbx.discrepancy_function(map_1=m1_he, map_2=m2_he, cutoffs=cutoffs) print >> out, "Discrepancy function:" print >> out, " cutoff D" for c, d in zip(cutoffs, df): print >> out, " %3.2f %7.4f" % (c,d) discrepancies.append((c, d)) # compute and output histograms h1 = maptbx.histogram(map=m1, n_bins=10000) h2 = maptbx.histogram(map=m2, n_bins=10000) print >> out, "Map histograms:" print >> out, "Map 1 (%s) Map 2 (%s)"%\ (params.input.map_1,params.input.map_2) print >> out, "(map_value,cdf,frequency) <> (map_value,cdf,frequency)" for a1,c1,v1, a2,c2,v2 in zip(h1.arguments(), h1.c_values(), h1.values(), h2.arguments(), h2.c_values(), h2.values()): print >> out, "(%9.5f %9.5f %9.5f) <> (%9.5f %9.5f %9.5f)"%\ (a1,c1,v1, a2,c2,v2) # store results s1_dict = create_statistics_dict(s=s1) s2_dict = create_statistics_dict(s=s2) results = dict() inputs = list() for attribute in input_attributes: filename = getattr(params.input,attribute) if (filename is not None): inputs.append(filename) assert (len(inputs) == 2) results['map_files'] = inputs results['map_statistics'] = (s1_dict, s2_dict) results['cc_input_maps'] = cc_input_maps results['cc_quantile'] = cc_quantile results['cc_peaks'] = cc_peaks results['discrepancies'] = discrepancies results['map_histograms'] = ( (h1.arguments(), h1.c_values(), h1.values()), (h2.arguments(), h2.c_values(), h2.values()) ) return results
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 _f_ordered_solvent(self): fo = self._fo fc = fo.structure_factors_from_scatterers(xray_structure = self._structure ).f_calc() self._fc = fc if(self.n_real is None): crystal_gridding = maptbx.crystal_gridding(unit_cell = self._structure.unit_cell(), step = self._grid_step) n_real = crystal_gridding.n_real() else: n_real = self.n_real xyzf = flex.vec3_double() atmrad = flex.double() elements = [] for scatterer in self._structure.scatterers(): xyzf.append(list(scatterer.site)) atmrad.append(van_der_waals_radii.vdw.table[scatterer.element_symbol()]) elements.append( scatterer.element_symbol() ) assert xyzf.size() == atmrad.size() sel_flag = flex.int(xyzf.size(),1) # XXX removed 2011-02-14: set sel_flag to zero if resname is HOH assert sel_flag.size() == atmrad.size() self._distribution = wat_dist() self._distribution.do_wat_dist( shell = 0.0, xyzf = xyzf, atmrad = atmrad, element_symbol = elements, uc = self._structure.unit_cell(), sg = self._structure.space_group(), nxnynz = n_real, sel_flag = sel_flag, rad = self.rad, nshells = self.nshells) data = self._distribution.data() ############################### #mask_data = mask(0.0, # 1.0, # 1.0, # xyzf, # atmrad, # self._structure.unit_cell(), # self._structure.space_group(), # crystal_gridding.n_real()).data() # #data.set_selected(mask_data == 0.0, 0.0) ############################### map_of_coeff = map_of_coeff_scaled(data, self._structure, n_real) from_map = maptbx.structure_factors.from_map( space_group=self._structure.space_group_info().group(), anomalous_flag=False, miller_indices=fo.indices(), complex_map=map_of_coeff, conjugate_flag=True) self._f = miller.array( miller_set = miller.set( crystal_symmetry = crystal.symmetry( unit_cell = self._structure.unit_cell(), space_group_info = self._structure.space_group_info()), indices = fo.indices(), anomalous_flag = False), data = from_map.data()) assert fc.indices().all_eq(self._f.indices()) == 1 assert fc.indices().all_eq(fo.indices()) == 1 assert flex.max(abs(self._f).data()) < 1.0 return self
def run(args, log=None, ccp4_map=None, return_as_miller_arrays=False, nohl=False, 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 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 # map_data=m.data shift_needed = not \ (map_data.focus_size_1d() > 0 and map_data.nd() == 3 and map_data.is_0_based()) if(shift_needed): map_data = map_data.shift_origin() # generate complete set of Miller indices up to given high resolution d_min n_real = map_data.focus() cs = crystal.symmetry(m.unit_cell_parameters, 1) crystal_gridding = maptbx.crystal_gridding( unit_cell = cs.unit_cell(), space_group_info = cs.space_group_info(), #symmetry_flags = maptbx.use_space_group_symmetry, 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 max_index = [(i-1)//2 for i in n_real] print >>out,"max_index:", max_index complete_set = miller.build_set( crystal_symmetry = cs, anomalous_flag = False, max_index = max_index) indices = complete_set.indices() indices.append((0,0,0)) complete_set = complete_set.customized_copy(indices = indices) #if(not params.box): # # XXX What is sphere resolution corresponding to given box? # uc = complete_set.unit_cell() # d1 = uc.d([0,0,max_index[2]]) # d2 = uc.d([0,max_index[1],0]) # d3 = uc.d([max_index[0],1,0]) # print >>out, d1,d2,d3 # complete_set_sp = miller.build_set( # crystal_symmetry = cs, # anomalous_flag = False, # d_min = min(d1,d2,d3)) # complete_set = complete_set.common_set(complete_set_sp) else: complete_set = miller.build_set( crystal_symmetry = cs, anomalous_flag = False, d_min = d_min) broadcast(m="Complete set information:", log=log) complete_set.show_comprehensive_summary(prefix=" ",f=out) 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 __init__(self, xray_structure = None, fmodel = None, map_data = None, d_min = None, resolution_factor = 0.25, map_type = "2mFo-DFc"): """ Utility to calculate correlation between two maps: CC(xray_structure, map_data), xray_structure are map_data inputs or CC(2mFo-DFc, Fc), 2mFo-DFc and Fc are from input fmodel . """ assert [fmodel, map_data].count(None) == 1 assert [xray_structure, map_data].count(None) in [0, 2] assert [fmodel, xray_structure].count(None) == 1 assert [d_min, fmodel].count(None) == 1 adopt_init_args(self, locals()) if(fmodel is not None): self.xray_structure = fmodel.xray_structure # get map_data defined if(self.fmodel is not None): e_map_obj = fmodel.electron_density_map() isotropize = True if(fmodel.is_twin_fmodel_manager()): isotropize = False mc = e_map_obj.map_coefficients( map_type = map_type, fill_missing = False, isotropize = isotropize) crystal_gridding = self.fmodel.f_obs().crystal_gridding( d_min = self.fmodel.f_obs().d_min(), resolution_factor = resolution_factor) fft_map = miller.fft_map( crystal_gridding = crystal_gridding, fourier_coefficients = mc) self.map_data = fft_map.real_map_unpadded() # get model map if(self.fmodel is not None): if(fmodel.is_twin_fmodel_manager()): f_model = self.fmodel.f_model() else: f_model = self.fmodel.f_model_scaled_with_k1() fft_map = miller.fft_map( crystal_gridding = crystal_gridding, fourier_coefficients = f_model) fft_map.apply_sigma_scaling() self.map_model = fft_map.real_map_unpadded() else: 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.accessor().all()) f_model = self.xray_structure.structure_factors(d_min=self.d_min).f_calc() fft_map = miller.fft_map( crystal_gridding = crystal_gridding, fourier_coefficients = f_model) fft_map.apply_sigma_scaling() self.map_model = fft_map.real_map_unpadded() if(self.fmodel is not None): self.sites_cart = self.fmodel.xray_structure.sites_cart() self.sites_frac = self.fmodel.xray_structure.sites_frac() self.weights = self.fmodel.xray_structure.atomic_weights() else: self.sites_cart = self.xray_structure.sites_cart() self.sites_frac = self.xray_structure.sites_frac() self.weights = self.xray_structure.atomic_weights()