def scale_amplitudes(model_map_coeffs=None, map_coeffs=None, external_map_coeffs=None, first_half_map_coeffs=None, second_half_map_coeffs=None, si=None,resolution=None,overall_b=None, fraction_complete=None, min_fraction_complete=0.05, map_calculation=True, verbose=False, out=sys.stdout): # Figure out resolution_dependent sharpening to optimally # match map and model. Then apply it as usual. # if second_half_map_coeffs instead of model, use second_half_map_coeffs same as # normalized model map_coeffs, except that the target fall-off should be # skipped (could use fall-off based on a dummy model...) if model_map_coeffs and ( not first_half_map_coeffs or not second_half_map_coeffs): is_model_based=True else: assert si.target_scale_factors or ( first_half_map_coeffs and second_half_map_coeffs) or ( external_map_coeffs) is_model_based=False if si.verbose and not verbose: verbose=True # if si.target_scale_factors is set, just use those scale factors from cctbx.maptbx.segment_and_split_map import map_coeffs_as_fp_phi,get_b_iso f_array,phases=map_coeffs_as_fp_phi(map_coeffs) (d_max,d_min)=f_array.d_max_min() if not f_array.binner(): f_array.setup_binner(n_bins=si.n_bins,d_max=d_max,d_min=d_min) f_array.binner().require_all_bins_have_data(min_counts=1, error_string="Please use a lower value of n_bins") if resolution is None: resolution=si.resolution if resolution is None: raise Sorry("Need resolution for model sharpening") obs_b_iso=get_b_iso(f_array,d_min=resolution) print("\nEffective b_iso of observed data: %6.1f A**2" %(obs_b_iso), file=out) if not si.target_scale_factors: # get scale factors if don't already have them si=calculate_fsc(si=si, f_array=f_array, # just used for binner map_coeffs=map_coeffs, model_map_coeffs=model_map_coeffs, first_half_map_coeffs=first_half_map_coeffs, second_half_map_coeffs=second_half_map_coeffs, external_map_coeffs=external_map_coeffs, resolution=resolution, fraction_complete=fraction_complete, min_fraction_complete=min_fraction_complete, is_model_based=is_model_based, cc_cut=si.cc_cut, scale_using_last=si.scale_using_last, max_cc_for_rescale=si.max_cc_for_rescale, pseudo_likelihood=si.pseudo_likelihood, verbose=verbose, out=out) # now si.target_scale_factors array are the scale factors # Now create resolution-dependent coefficients from the scale factors if not si.target_scale_factors: # nothing to do print("\nNo scaling applied", file=out) map_data=calculate_map(map_coeffs=map_coeffs,n_real=si.n_real) return map_and_b_object(map_data=map_data) elif not map_calculation: return map_and_b_object() else: # apply scaling if si.pseudo_likelihood: print("Normalizing structure factors", file=out) f_array=quasi_normalize_structure_factors(f_array,set_to_minimum=0.01, pseudo_likelihood=si.pseudo_likelihood) f_array.setup_binner(n_bins=si.n_bins,d_max=d_max,d_min=d_min) map_and_b=apply_target_scale_factors( f_array=f_array,phases=phases,resolution=resolution, target_scale_factors=si.target_scale_factors, n_real=si.n_real, out=out) return map_and_b
def run(args=None, params=None, map_data=None, crystal_symmetry=None, write_output_files=True, pdb_inp=None, ncs_obj=None, return_map_data_only=False, half_map_data_list=None, ncs_copies=None, n_residues=None, out=sys.stdout): # Get the parameters if not params: params = get_params(args, out=out) if not ncs_copies: ncs_copies = params.crystal_info.ncs_copies # get map_data and crystal_symmetry pdb_inp,map_data,half_map_data_list,ncs_obj,\ crystal_symmetry,acc=get_map_and_model( map_data=map_data, half_map_data_list=half_map_data_list, pdb_inp=pdb_inp, ncs_obj=ncs_obj, crystal_symmetry=crystal_symmetry, params=params,out=out) # NOTE: map_data is now relative to origin at (0,0,0). # Use map_data.reshape(acc) to put it back where it was if acc is not None # auto-sharpen the map from cctbx.maptbx.segment_and_split_map import auto_sharpen_map_or_map_coeffs si=auto_sharpen_map_or_map_coeffs( resolution=params.crystal_info.resolution, # required crystal_symmetry=crystal_symmetry, is_crystal=params.crystal_info.is_crystal, verbose=params.control.verbose, map=map_data, half_map_data_list=half_map_data_list, solvent_content=params.crystal_info.solvent_content, input_weight_map_pickle_file=\ params.input_files.input_weight_map_pickle_file, output_weight_map_pickle_file=\ params.output_files.output_weight_map_pickle_file, read_sharpened_maps=params.map_modification.read_sharpened_maps, write_sharpened_maps=params.map_modification.write_sharpened_maps, select_sharpened_map=params.map_modification.select_sharpened_map, auto_sharpen=params.map_modification.auto_sharpen, local_sharpening=params.map_modification.local_sharpening, output_directory=params.output_files.output_directory, smoothing_radius=params.map_modification.smoothing_radius, local_aniso_in_local_sharpening=\ params.map_modification.local_aniso_in_local_sharpening, box_in_auto_sharpen=params.map_modification.box_in_auto_sharpen, use_weak_density=params.map_modification.use_weak_density, discard_if_worse=params.map_modification.discard_if_worse, box_center=params.map_modification.box_center, box_size=params.map_modification.box_size, remove_aniso=params.map_modification.remove_aniso, auto_sharpen_methods=params.map_modification.auto_sharpen_methods, residual_target=params.map_modification.residual_target, region_weight=params.map_modification.region_weight, sa_percent=params.map_modification.sa_percent, eps=params.map_modification.eps, n_bins=params.map_modification.n_bins, max_regions_to_test=params.map_modification.max_regions_to_test, fraction_occupied=params.map_modification.fraction_occupied, sharpening_target=params.map_modification.sharpening_target, d_min_ratio=params.map_modification.d_min_ratio, max_box_fraction=params.map_modification.max_box_fraction, mask_atoms=params.map_modification.mask_atoms, mask_atoms_atom_radius=params.map_modification.mask_atoms_atom_radius, value_outside_atoms=params.map_modification.value_outside_atoms, k_sharpen=params.map_modification.k_sharpen, soft_mask=params.map_modification.soft_mask, search_b_min=params.map_modification.search_b_min, search_b_max=params.map_modification.search_b_max, search_b_n=params.map_modification.search_b_n, maximum_low_b_adjusted_sa=\ params.map_modification.maximum_low_b_adjusted_sa, b_iso=params.map_modification.b_iso, b_sharpen=params.map_modification.b_sharpen, resolution_dependent_b=\ params.map_modification.resolution_dependent_b, pdb_inp=pdb_inp, ncs_obj=ncs_obj, rmsd=params.map_modification.rmsd, b_sol=params.map_modification.b_sol, k_sol=params.map_modification.k_sol, fraction_complete=params.map_modification.fraction_complete, seq_file=params.input_files.seq_file, ncs_copies=ncs_copies, n_residues=n_residues, out=out) # get map_data and map_coeffs of final map new_map_data = si.as_map_data() new_map_coeffs = si.as_map_coeffs() from cctbx.maptbx.segment_and_split_map import get_b_iso, map_coeffs_as_fp_phi f, phi = map_coeffs_as_fp_phi(new_map_coeffs) temp_b_iso = get_b_iso(f, d_min=params.crystal_info.resolution) if not si.is_model_sharpening(): print >> out print >> out, 80 * "=", "\n", 80 * "=" print >> out, "\n Summary of sharpening information\n " si.show_summary(verbose=params.control.verbose, out=out) print >> out, 80 * "=", "\n", 80 * "=" # write out the new map_coeffs and map if requested: if write_output_files and params.output_files.sharpened_map_file and \ new_map_data: output_map_file = os.path.join(params.output_files.output_directory, params.output_files.sharpened_map_file) from cctbx.maptbx.segment_and_split_map import write_ccp4_map offset_map_data = new_map_data.deep_copy() if acc is not None: # offset the map to match original if possible offset_map_data.reshape(acc) print >>out,\ "\nWrote sharpened map in original location with origin at %s\nto %s" %( str(offset_map_data.origin()),output_map_file) else: print >>out,"\nWrote sharpened map with origin at 0,0,0 "+\ "(NOTE: may not be \nsame as original location) to %s\n" %( output_map_file) write_ccp4_map(crystal_symmetry, output_map_file, offset_map_data) if write_output_files and params.output_files.shifted_sharpened_map_file: output_map_file = os.path.join( params.output_files.output_directory, params.output_files.shifted_sharpened_map_file) from cctbx.maptbx.segment_and_split_map import write_ccp4_map write_ccp4_map(crystal_symmetry, output_map_file, new_map_data) print >> out, "\nWrote sharpened map (origin at %s)\nto %s" % (str( new_map_data.origin()), output_map_file) if write_output_files and params.output_files.sharpened_map_coeffs_file and \ new_map_coeffs: output_map_coeffs_file = os.path.join( params.output_files.output_directory, params.output_files.sharpened_map_coeffs_file) from cctbx.maptbx.segment_and_split_map import write_ccp4_map new_map_coeffs.as_mtz_dataset( column_root_label='FWT').mtz_object().write( file_name=output_map_coeffs_file) print >> out, "\nWrote sharpened map_coeffs (origin at 0,0,0)\n to %s\n" % ( output_map_coeffs_file) if return_map_data_only: return new_map_data else: #usual return new_map_data, new_map_coeffs, crystal_symmetry, si
def run(args=None, params=None, map_data=None, crystal_symmetry=None, wrapping=None, write_output_files=True, pdb_inp=None, ncs_obj=None, return_map_data_only=False, return_unshifted_map=False, half_map_data_list=None, ncs_copies=None, n_residues=None, out=sys.stdout): # Get the parameters if not params: params = get_params(args, out=out) if not ncs_copies: ncs_copies = params.crystal_info.ncs_copies # get map_data and crystal_symmetry pdb_inp,map_data,half_map_data_list,ncs_obj,crystal_symmetry,acc,\ original_crystal_symmetry,original_unit_cell_grid,map_labels=\ get_map_and_model( map_data=map_data, half_map_data_list=half_map_data_list, pdb_inp=pdb_inp, ncs_obj=ncs_obj, map_coords_inside_cell=False, crystal_symmetry=crystal_symmetry, get_map_labels=True, params=params,out=out) # NOTE: map_data is now relative to origin at (0,0,0). # Use map_data.reshape(acc) to put it back where it was if acc is not None # auto-sharpen the map from cctbx.maptbx.segment_and_split_map import auto_sharpen_map_or_map_coeffs si=auto_sharpen_map_or_map_coeffs( resolution=params.crystal_info.resolution, # required crystal_symmetry=crystal_symmetry, is_crystal=params.crystal_info.is_crystal, verbose=params.control.verbose, resolve_size=params.control.resolve_size, multiprocessing=params.control.multiprocessing, nproc=params.control.nproc, queue_run_command=params.control.queue_run_command, map=map_data, wrapping=wrapping, half_map_data_list=half_map_data_list, solvent_content=params.crystal_info.solvent_content, molecular_mass=params.crystal_info.molecular_mass, input_weight_map_pickle_file=\ params.input_files.input_weight_map_pickle_file, output_weight_map_pickle_file=\ params.output_files.output_weight_map_pickle_file, read_sharpened_maps=params.map_modification.read_sharpened_maps, write_sharpened_maps=params.map_modification.write_sharpened_maps, select_sharpened_map=params.map_modification.select_sharpened_map, auto_sharpen=params.map_modification.auto_sharpen, local_sharpening=params.map_modification.local_sharpening, output_directory=params.output_files.output_directory, smoothing_radius=params.map_modification.smoothing_radius, local_aniso_in_local_sharpening=\ params.map_modification.local_aniso_in_local_sharpening, overall_before_local=\ params.map_modification.overall_before_local, box_in_auto_sharpen=params.map_modification.box_in_auto_sharpen, density_select_in_auto_sharpen=params.map_modification.density_select_in_auto_sharpen, density_select_threshold_in_auto_sharpen=params.map_modification.density_select_threshold_in_auto_sharpen, use_weak_density=params.map_modification.use_weak_density, discard_if_worse=params.map_modification.discard_if_worse, box_center=params.map_modification.box_center, box_size=params.map_modification.box_size, target_n_overlap=params.map_modification.target_n_overlap, restrict_map_size=params.map_modification.restrict_map_size, remove_aniso=params.map_modification.remove_aniso, auto_sharpen_methods=params.map_modification.auto_sharpen_methods, residual_target=params.map_modification.residual_target, region_weight=params.map_modification.region_weight, sa_percent=params.map_modification.sa_percent, eps=params.map_modification.eps, n_bins=params.map_modification.n_bins, max_regions_to_test=params.map_modification.max_regions_to_test, regions_to_keep=params.map_modification.regions_to_keep, fraction_occupied=params.map_modification.fraction_occupied, sharpening_target=params.map_modification.sharpening_target, d_min_ratio=params.map_modification.d_min_ratio, scale_max=params.map_modification.scale_max, input_d_cut=params.map_modification.input_d_cut, b_blur_hires=params.map_modification.b_blur_hires, max_box_fraction=params.map_modification.max_box_fraction, cc_cut=params.map_modification.cc_cut, max_cc_for_rescale=params.map_modification.max_cc_for_rescale, scale_using_last=params.map_modification.scale_using_last, density_select_max_box_fraction=params.map_modification.density_select_max_box_fraction, mask_atoms=params.map_modification.mask_atoms, mask_atoms_atom_radius=params.map_modification.mask_atoms_atom_radius, value_outside_atoms=params.map_modification.value_outside_atoms, k_sharpen=params.map_modification.k_sharpen, optimize_b_blur_hires=params.map_modification.optimize_b_blur_hires, iterate=params.map_modification.iterate, optimize_d_cut=params.map_modification.optimize_d_cut, soft_mask=params.map_modification.soft_mask, allow_box_if_b_iso_set=params.map_modification.allow_box_if_b_iso_set, search_b_min=params.map_modification.search_b_min, search_b_max=params.map_modification.search_b_max, search_b_n=params.map_modification.search_b_n, adjust_region_weight=params.map_modification.adjust_region_weight, region_weight_method=params.map_modification.region_weight_method, region_weight_factor=params.map_modification.region_weight_factor, region_weight_buffer=\ params.map_modification.region_weight_buffer, target_b_iso_ratio=params.map_modification.target_b_iso_ratio, signal_min=params.map_modification.signal_min, buffer_radius=params.crystal_info.buffer_radius, wang_radius=params.crystal_info.wang_radius, pseudo_likelihood=params.crystal_info.pseudo_likelihood, target_b_iso_model_scale=params.map_modification.target_b_iso_model_scale, b_iso=params.map_modification.b_iso, b_sharpen=params.map_modification.b_sharpen, resolution_dependent_b=\ params.map_modification.resolution_dependent_b, normalize_amplitudes_in_resdep=\ params.map_modification.normalize_amplitudes_in_resdep, pdb_inp=pdb_inp, ncs_obj=ncs_obj, rmsd=params.map_modification.rmsd, rmsd_resolution_factor=params.map_modification.rmsd_resolution_factor, b_sol=params.map_modification.b_sol, k_sol=params.map_modification.k_sol, fraction_complete=params.map_modification.fraction_complete, seq_file=params.input_files.seq_file, ncs_copies=ncs_copies, n_residues=n_residues, out=out) # get map_data and map_coeffs of final map new_map_data = si.as_map_data() new_map_coeffs = si.as_map_coeffs() from cctbx.maptbx.segment_and_split_map import get_b_iso, map_coeffs_as_fp_phi f, phi = map_coeffs_as_fp_phi(new_map_coeffs) temp_b_iso = get_b_iso(f, d_min=params.crystal_info.resolution) if not si.is_model_sharpening(): print(file=out) print(80 * "=", "\n", 80 * "=", file=out) print("\n Summary of sharpening information\n ", file=out) si.show_summary(verbose=params.control.verbose, out=out) print(80 * "=", "\n", 80 * "=", file=out) # write out the new map_coeffs and map if requested: offset_map_data = new_map_data.deep_copy() if acc is not None: # offset the map to match original if possible offset_map_data.reshape(acc) if write_output_files and params.output_files.sharpened_map_file and \ offset_map_data: output_map_file = os.path.join(params.output_files.output_directory, params.output_files.sharpened_map_file) from cctbx.maptbx.segment_and_split_map import write_ccp4_map if acc is not None: # we offset the map to match original print( "\nWrote sharpened map in original location with origin at %s\nto %s" % (str(offset_map_data.origin()), output_map_file), file=out) write_ccp4_map(original_crystal_symmetry, output_map_file, offset_map_data, output_unit_cell_grid=original_unit_cell_grid) else: print("\nWrote sharpened map with origin at 0,0,0 "+\ "(NOTE: may be boxed map and may not be "+\ "\nsame as original location) to %s\n" %( output_map_file), file=out) from iotbx.mrcfile import create_output_labels program_name = 'auto_sharpen' limitations = ["map_is_sharpened"] labels = create_output_labels( program_name=program_name, input_file_name=params.input_files.map_file, input_labels=map_labels, limitations=limitations, output_labels=None) write_ccp4_map(crystal_symmetry, output_map_file, offset_map_data, labels=labels) if write_output_files and params.output_files.shifted_sharpened_map_file: output_map_file = os.path.join( params.output_files.output_directory, params.output_files.shifted_sharpened_map_file) from cctbx.maptbx.segment_and_split_map import write_ccp4_map write_ccp4_map(crystal_symmetry, output_map_file, new_map_data) print("\nWrote sharpened map (origin at %s)\nto %s" % (str(new_map_data.origin()), output_map_file), file=out) if write_output_files and params.output_files.sharpened_map_coeffs_file and \ new_map_coeffs: output_map_coeffs_file = os.path.join( params.output_files.output_directory, params.output_files.sharpened_map_coeffs_file) new_map_coeffs.as_mtz_dataset( column_root_label='FWT').mtz_object().write( file_name=output_map_coeffs_file) print("\nWrote sharpened map_coeffs (origin at 0,0,0)\n to %s\n" % (output_map_coeffs_file), file=out) if return_unshifted_map: map_to_return = offset_map_data else: map_to_return = new_map_data if return_map_data_only: return map_to_return else: #usual return map_to_return, new_map_coeffs, crystal_symmetry, si
def scale_amplitudes(model_map_coeffs=None, map_coeffs=None, first_half_map_coeffs=None, second_half_map_coeffs=None, si=None, resolution=None, overall_b=None, fraction_complete=None, min_fraction_complete=0.05, verbose=False, out=sys.stdout): # Figure out resolution_dependent sharpening to optimally # match map and model. Then apply it as usual. # if second_half_map_coeffs instead of model, use second_half_map_coeffs same as # normalized model map_coeffs, except that the target fall-off should be # skipped (could use fall-off based on a dummy model...) if model_map_coeffs: is_model_based = True else: assert si.target_scale_factors or (first_half_map_coeffs and second_half_map_coeffs) is_model_based = False if si.verbose and not verbose: verbose = True # if si.target_scale_factors is set, just use those scale factors from cctbx.maptbx.segment_and_split_map import map_coeffs_as_fp_phi, get_b_iso f_array, phases = map_coeffs_as_fp_phi(map_coeffs) (d_max, d_min) = f_array.d_max_min() if not f_array.binner(): f_array.setup_binner(n_bins=si.n_bins, d_max=d_max, d_min=d_min) if resolution is None: resolution = si.resolution if resolution is None: raise Sorry("Need resolution for model sharpening") obs_b_iso = get_b_iso(f_array, d_min=resolution) print >> out, "\nEffective b_iso of observed data: %6.1f A**2" % ( obs_b_iso) if not si.target_scale_factors: # get scale factors if don't already have them si = calculate_fsc( si=si, f_array=f_array, # just used for binner map_coeffs=map_coeffs, model_map_coeffs=model_map_coeffs, first_half_map_coeffs=first_half_map_coeffs, second_half_map_coeffs=second_half_map_coeffs, resolution=resolution, fraction_complete=fraction_complete, min_fraction_complete=min_fraction_complete, is_model_based=is_model_based, verbose=verbose, out=out) # now si.target_scale_factors array are the scale factors # Now create resolution-dependent coefficients from the scale factors if not si.target_scale_factors: # nothing to do scaled_f_array = f_array f_array_b_iso = get_b_iso(f_array, d_min=resolution) print >> out, "\nNo scaling applied. B_iso=%5.1f A**2\n" % ( f_array_b_iso) else: # apply scaling f_array_b_iso = get_b_iso(f_array, d_min=resolution) scale_array = get_scale_factors( f_array, target_scale_factors=si.target_scale_factors) scaled_f_array = f_array.customized_copy(data=f_array.data() * scale_array) scaled_f_array_b_iso = get_b_iso(scaled_f_array, d_min=resolution) print >>out,"\nInitial b_iso for "+\ "map: %5.1f A**2 After adjustment: %5.1f A**2" %( f_array_b_iso,scaled_f_array_b_iso) new_map_coeffs = scaled_f_array.phase_transfer(phase_source=phases, deg=True) assert si.n_real is not None return calculate_map(map_coeffs=new_map_coeffs, n_real=si.n_real)