def minimize(self): utils.assert_xray_structures_equal( x1 = self.fmodels.fmodel_xray().xray_structure, x2 = self.model.xray_structure) self.model.set_refine_individual_adp() lbfgs_termination_params = scitbx.lbfgs.termination_parameters( max_iterations = self.individual_adp_params.iso.max_number_of_iterations) is_neutron_scat_table = False if(self.all_params.main.scattering_table == "neutron"): is_neutron_scat_table = True minimized = minimization.lbfgs( restraints_manager = self.model.restraints_manager, fmodels = self.fmodels, model = self.model, refine_adp = True, is_neutron_scat_table = is_neutron_scat_table, lbfgs_termination_params = lbfgs_termination_params, iso_restraints = self.adp_restraints_params.iso, verbose = 0, target_weights = self.target_weights, h_params = self.h_params) self.model.xray_structure = self.fmodels.fmodel_xray().xray_structure assert minimized.xray_structure is self.model.xray_structure utils.assert_xray_structures_equal( x1 = minimized.xray_structure, x2 = self.model.xray_structure) return minimized
def collect( self, step, fmodels = None, model = None, rigid_body_shift_accumulator = None): from mmtbx import utils if(model is not None): self.model = model if(fmodels is not None): self.fmodels = fmodels utils.assert_xray_structures_equal( x1 = self.fmodels.fmodel_xray().xray_structure, x2 = self.model.xray_structure) self.monitor_xray.collect( model = self.model, fmodel = self.fmodels.fmodel_xray(), step = step, wilson_b = self.model.wilson_b, rigid_body_shift_accumulator = rigid_body_shift_accumulator) if(self.monitor_neutron is not None): utils.assert_xray_structures_equal( x1 = self.fmodels.fmodel_neutron().xray_structure, x2 = self.model.xray_structure) self.monitor_neutron.collect( model = self.model, fmodel = self.fmodels.fmodel_neutron(), step = step, wilson_b = self.model.wilson_b, rigid_body_shift_accumulator = rigid_body_shift_accumulator)
def minimize(self): utils.assert_xray_structures_equal( x1 = self.fmodels.fmodel_xray().xray_structure, x2 = self.model.xray_structure) self.model.set_refine_individual_adp() self.run_lbfgs() self.model.xray_structure = self.fmodels.fmodel_xray().xray_structure
def minimize(self, ca_only=False): pdb_hierarchy = self.model.pdb_hierarchy(sync_with_xray_structure=True) if ca_only: ca_selection = pdb_hierarchy.get_peptide_c_alpha_selection() restraint_sites_cart = self.model.xray_structure.sites_cart().\ deep_copy().select(ca_selection) restraint_selection = ca_selection else: restraint_sites_cart = self.model.xray_structure.sites_cart().deep_copy() restraint_selection = pdb_hierarchy.atoms().extract_i_seq() self.model.restraints_manager.geometry.\ add_reference_coordinate_restraints_in_place( pdb_hierarchy=pdb_hierarchy, selection=restraint_selection) ##### sanity check ##### assert(self.model.restraints_manager.geometry. get_n_reference_coordinate_proxies() >= len(restraint_sites_cart)) ######################## selection = self.model.selection_moving minimized = self.model.geometry_minimization( max_number_of_iterations = 500, correct_special_position_tolerance=1.0, number_of_macro_cycles = 1, selection = selection, bond = True, nonbonded = True, angle = True, dihedral = True, chirality = True, planarity = True) utils.assert_xray_structures_equal( x1 = self.fmodels.fmodel_xray().xray_structure, x2 = self.model.xray_structure) self.model.restraints_manager.geometry.\ remove_reference_coordinate_restraints_in_place()
def try_den_weight_cartesian(self, grid_pair): local_seed = int(self.random_seed+grid_pair[1]) flex.set_random_seed(value=local_seed) random.seed(local_seed) self.fmodels.fmodel_xray().xray_structure.replace_scatterers( self.save_scatterers_local.deep_copy()) self.fmodels.update_xray_structure( xray_structure = self.fmodels.fmodel_xray().xray_structure, update_f_calc = True) utils.assert_xray_structures_equal( x1 = self.fmodels.fmodel_xray().xray_structure, x2 = self.model.xray_structure) gamma_local = grid_pair[0] weight_local = grid_pair[1] self.model.restraints_manager.geometry.\ den_manager.gamma = gamma_local self.model.restraints_manager.geometry.\ den_manager.weight = weight_local cycle = 0 self.model.restraints_manager.geometry.\ den_manager.current_cycle = cycle+1 num_den_cycles = self.model.restraints_manager.geometry.\ den_manager.num_cycles if self.params.den.optimize and \ self.nproc != Auto and \ self.nproc > 1: local_log = sys.stdout elif self.params.den.optimize and \ self.nproc == 1: if self.verbose: local_log = self.log else: local_log = StringIO() else: local_log = self.log print >> self.log, " ...trying gamma %f, weight %f" % ( gamma_local, weight_local) while cycle < num_den_cycles: print >> local_log, "DEN cycle %s" % (cycle+1) r_free = self.fmodels.fmodel_xray().r_free() print >> local_log, "rfree at start of SA cycle: %f" % r_free simulated_annealing.manager( params = self.params.simulated_annealing, target_weights = self.target_weights, macro_cycle = self.macro_cycle, h_params = self.params.hydrogens, fmodels = self.fmodels, model = self.model, all_params = self.params, out = local_log) if self.params.den.bulk_solvent_and_scale: self.bulk_solvent_and_scale(log=local_log) if self.params.den.refine_adp: self.adp_refinement(log=local_log) self.model.restraints_manager.geometry.update_dihedral_ncs_restraints( sites_cart=self.model.xray_structure.sites_cart(), pdb_hierarchy=self.model.pdb_hierarchy(sync_with_xray_structure=True), log=local_log) cycle += 1 self.model.restraints_manager.geometry.\ den_manager.current_cycle += 1 r_free = self.fmodels.fmodel_xray().r_free() print >> local_log, "rfree at end of SA cycle: %f" % r_free r_free = self.fmodels.fmodel_xray().r_free() step_xray_structure = self.fmodels.fmodel_xray().\ xray_structure.deep_copy_scatterers().scatterers() step_eq_distances = self.model.restraints_manager.geometry.\ den_manager.get_current_eq_distances() return (gamma_local, weight_local, r_free, step_xray_structure, step_eq_distances)
def try_den_weight_torsion(self, grid_pair): #backup_k_rep = self.params.tardy.\ # prolsq_repulsion_function_changes.k_rep local_seed = int(self.random_seed+grid_pair[1]) flex.set_random_seed(value=local_seed) random.seed(local_seed) self.fmodels.fmodel_xray().xray_structure.replace_scatterers( self.save_scatterers_local.deep_copy()) self.fmodels.update_xray_structure( xray_structure = self.fmodels.fmodel_xray().xray_structure, update_f_calc = True) utils.assert_xray_structures_equal( x1 = self.fmodels.fmodel_xray().xray_structure, x2 = self.model.xray_structure) gamma_local = grid_pair[0] weight_local = grid_pair[1] self.model.restraints_manager.geometry.\ den_manager.gamma = gamma_local self.model.restraints_manager.geometry.\ den_manager.weight = weight_local cycle = 0 self.model.restraints_manager.geometry.\ den_manager.current_cycle = cycle+1 num_den_cycles = self.model.restraints_manager.geometry.\ den_manager.num_cycles if self.params.den.optimize and \ self.nproc != Auto and \ self.nproc > 1: local_log = sys.stdout elif self.params.den.optimize and \ self.nproc == 1: if self.verbose: local_log = self.log else: local_log = StringIO() else: local_log = self.log print >> self.log, " ...trying gamma %.1f, weight %.1f" % ( gamma_local, weight_local) while cycle < num_den_cycles: #if self.model.restraints_manager.geometry.\ # generic_restraints_manager.den_manager.current_cycle == \ # self.model.restraints_manager.geometry.\ # generic_restraints_manager.den_manager.torsion_mid_point+1: # self.params.tardy.\ # prolsq_repulsion_function_changes.k_rep = 1.0 print >> local_log, "DEN cycle %d" % (cycle+1) #print >> local_log, "Random seed: %d" % flex.get_random_seed() r_free = self.fmodels.fmodel_xray().r_free() print >> local_log, "rfree at start of SA cycle: %.4f" % r_free print >> local_log, "k_rep = %.2f" % \ self.params.tardy.\ prolsq_repulsion_function_changes.k_rep tardy.run( fmodels=self.fmodels, model=self.model, target_weights=self.target_weights, params=self.params.tardy, log=local_log, format_for_phenix_refine=True, call_back_after_step=False) if self.params.den.bulk_solvent_and_scale: self.bulk_solvent_and_scale(log=local_log) self.fmodels.fmodel_xray().xray_structure = self.model.xray_structure if self.params.den.refine_adp: self.adp_refinement(log=local_log) self.model.restraints_manager.geometry.update_dihedral_ncs_restraints( sites_cart=self.model.xray_structure.sites_cart(), pdb_hierarchy=self.model.pdb_hierarchy(sync_with_xray_structure=True), log=local_log) cycle += 1 self.model.restraints_manager.geometry.\ den_manager.current_cycle += 1 r_free = self.fmodels.fmodel_xray().r_free() print >> local_log, "rfree at end of SA cycle: %f" % r_free r_free = self.fmodels.fmodel_xray().r_free() step_xray_structure = self.fmodels.fmodel_xray().\ xray_structure.deep_copy_scatterers().scatterers() step_eq_distances = self.model.restraints_manager.geometry.\ den_manager.get_current_eq_distances() return (gamma_local, weight_local, r_free, step_xray_structure, step_eq_distances)
def __init__( self, fmodels, model, params, target_weights, macro_cycle, ncs_manager=None, log=None): if log is None: log = sys.stdout # self.ncs_manager = ncs_manager self.nproc = params.main.nproc if self.nproc is Auto: self.nproc = 1 self.verbose = params.den.verbose self.log = log self.fmodels = fmodels self.model = model self.params = params self.target_weights = target_weights self.adp_refinement_manager = None self.macro_cycle = macro_cycle self.tan_b_iso_max = 0 self.random_seed = params.main.random_seed den_manager = model.restraints_manager. \ geometry.den_manager print_statistics.make_header("DEN refinement", out=self.log) pdb_hierarchy = self.model.pdb_hierarchy(sync_with_xray_structure=True) if den_manager.get_n_proxies() == 0: print_statistics.make_sub_header( "DEN restraint nework", out = self.log) den_manager.build_den_proxies(pdb_hierarchy=pdb_hierarchy) den_manager.build_den_restraints() den_manager.show_den_summary( sites_cart=self.model.xray_structure.sites_cart()) if den_manager.params.output_kinemage: den_manager.output_kinemage( self.model.xray_structure.sites_cart()) print_statistics.make_sub_header( "coordinate minimization before annealing", out=self.log) self.minimize(ca_only=self.params.den.minimize_c_alpha_only) self.save_scatterers_local = fmodels.fmodel_xray().\ xray_structure.deep_copy_scatterers().scatterers() #DEN refinement start, turn on if params.den.optimize: grid = den_manager.get_optimization_grid() print >> log, \ "Running DEN torsion optimization on %d processors..." % \ params.main.nproc else: grid = [(params.den.gamma, params.den.weight)] grid_results = [] grid_so = [] if "torsion" in params.den.annealing_type: print >> self.log, "Running torsion simulated annealing" if ( (params.den.optimize) and ( (self.nproc is Auto) or (self.nproc > 1) )): stdout_and_results = easy_mp.pool_map( processes=params.main.nproc, fixed_func=self.try_den_weight_torsion, args=grid, func_wrapper="buffer_stdout_stderr") for so, r in stdout_and_results: if (r is None): raise RuntimeError(("DEN weight optimization failed:"+ "\n%s\nThis is a "+ "serious error; please contact [email protected].") % so) grid_so.append(so) grid_results.append(r) else: for grid_pair in grid: result = self.try_den_weight_torsion( grid_pair=grid_pair) grid_results.append(result) self.show_den_opt_summary_torsion(grid_results) elif "cartesian" in params.den.annealing_type: print >> self.log, "Running Cartesian simulated annealing" if ( (params.den.optimize) and ( (self.nproc is Auto) or (self.nproc > 1) )): stdout_and_results = easy_mp.pool_map( processes=params.main.nproc, fixed_func=self.try_den_weight_cartesian, args=grid, func_wrapper="buffer_stdout_stderr") for so, r in stdout_and_results: if (r is None): raise RuntimeError(("DEN weight optimization failed:"+ "\n%s\nThis is a "+ "serious error; please contact [email protected].") % so) grid_so.append(so) grid_results.append(r) else: for grid_pair in grid: result = self.try_den_weight_cartesian( grid_pair=grid_pair) grid_results.append(result) self.show_den_opt_summary_cartesian(grid_results) else: raise "error in DEN annealing type" low_r_free = 1.0 best_xray_structure = None best_eq_distances = None best_gamma = None best_weight = None best_so_i = None for i, result in enumerate(grid_results): cur_r_free = result[2] if cur_r_free < low_r_free: low_r_free = cur_r_free best_gamma = result[0] best_weight = result[1] best_xray_structure = result[3] best_eq_distances = result[4] best_so_i = i assert best_xray_structure is not None if params.den.optimize: print >> self.log, "\nbest gamma: %.1f" % best_gamma print >> self.log, "best weight: %.1f\n" % best_weight if params.den.verbose: if len(grid_so) >= (best_so_i+1): print >> self.log, "\nBest annealing results:\n" print >> self.log, grid_so[best_so_i] fmodels.fmodel_xray().xray_structure.replace_scatterers( best_xray_structure.deep_copy()) fmodels.update_xray_structure( xray_structure = fmodels.fmodel_xray().xray_structure, update_f_calc = True) utils.assert_xray_structures_equal( x1 = fmodels.fmodel_xray().xray_structure, x2 = model.xray_structure) model.restraints_manager.geometry.\ den_manager.import_eq_distances(eq_distances=best_eq_distances) self.model.restraints_manager.geometry.update_dihedral_ncs_restraints( sites_cart=self.model.xray_structure.sites_cart(), pdb_hierarchy=self.model.pdb_hierarchy(sync_with_xray_structure=True), log=self.log)
def show_geometry( xray_structures, processed_pdb_file, scattering_table, hierarchy, model_selections, show_geometry_statistics, mvd_obj, atom_selections): if(len(xray_structures)>1): tmp = xray_structures[0] for xi in xray_structures[1:]: tmp = tmp.concatenate(xi) xray_structures = tmp else: xray_structures = xray_structures[0] ## utils.assert_xray_structures_equal( x1 = xray_structures, x2 = processed_pdb_file.xray_structure(), sites = True, adp = False, occupancies = True) ## hd_sel_all = xray_structures.hd_selection() if(show_geometry_statistics): sctr_keys = \ xray_structures.scattering_type_registry().type_count_dict().keys() has_hd = "H" in sctr_keys or "D" in sctr_keys geometry = processed_pdb_file.geometry_restraints_manager( show_energies = False, plain_pairs_radius = 5.0, assume_hydrogens_all_missing = not has_hd) restraints_manager_all = mmtbx.restraints.manager( geometry = geometry, normalization = True) models = hierarchy.models() geometry_statistics = [] n_residues_in_altlocs = None for i_seq, model_selection in enumerate(model_selections): hierarchy_i_seq = pdb.hierarchy.root() hierarchy_i_seq.append_model(models[i_seq].detached_copy()) # overall_counts_i_seq = hierarchy_i_seq.overall_counts() n_residues_in_altlocs = \ overall_counts_i_seq.n_alt_conf_pure + \ overall_counts_i_seq.n_alt_conf_proper + \ overall_counts_i_seq.n_alt_conf_improper # resname_classes = [] for k,v in zip(overall_counts_i_seq.resname_classes.keys(), overall_counts_i_seq.resname_classes.values()): resname_classes.append(" ".join([k.replace("common_",""), str(v)])) # xray_structure = xray_structures.select(model_selection) assert hierarchy_i_seq.atoms().size() == xray_structure.scatterers().size() hd_sel = xray_structure.hd_selection() def select_atom_selections(selection = model_selection, atom_selections = atom_selections): result = group_args() result.all = atom_selections.all .select(selection) result.macromolecule = atom_selections.macromolecule.select(selection) result.solvent = atom_selections.solvent .select(selection) result.ligand = atom_selections.ligand .select(selection) result.backbone = atom_selections.backbone .select(selection) result.sidechain = atom_selections.sidechain .select(selection) return result atom_selections_i_model = select_atom_selections( selection = model_selection, atom_selections = atom_selections) if(hd_sel.count(True) > 0 and scattering_table != "neutron"): xray_structure_stat = show_xray_structure_statistics( xray_structure = xray_structure, hd_sel = hd_sel, atom_selections = atom_selections_i_model) else: xray_structure_stat = show_xray_structure_statistics( xray_structure = xray_structure, atom_selections=atom_selections_i_model) model_statistics_geometry_macromolecule = None model_statistics_geometry_solvent = None model_statistics_geometry_ligand = None model_statistics_geometry_all = None molprobity_stats_i_seq = None rms_b_iso_or_b_equiv_bonded = None if(show_geometry_statistics): # exclude hydrogens if(hd_sel.count(True) > 0 and scattering_table != "neutron"): xray_structure = xray_structure.select(~hd_sel) model_selection = model_selection.select(~hd_sel) hierarchy_i_seq = hierarchy_i_seq.select(~hd_sel) geometry = restraints_manager_all.geometry.select(selection=~hd_sel_all) atom_selections_i_model = select_atom_selections(selection =~hd_sel_all, atom_selections = atom_selections_i_model) model_selection_as_bool = flex.bool(xray_structures.scatterers().size(), model_selection) geometry = restraints_manager_all.geometry.select(selection = model_selection_as_bool) restraints_manager = mmtbx.restraints.manager( geometry = geometry, normalization = True) restraints_manager.geometry.pair_proxies(sites_cart = xray_structure.sites_cart()) ### model_statistics_geometry_all = model_statistics.geometry( pdb_hierarchy = hierarchy_i_seq, molprobity_scores = True, restraints_manager = restraints_manager) # if(atom_selections.macromolecule.count(True)>0): mac_sel = atom_selections_i_model.macromolecule model_statistics_geometry_macromolecule = model_statistics.geometry( pdb_hierarchy = hierarchy_i_seq.select(mac_sel), molprobity_scores = True, restraints_manager = restraints_manager.select(mac_sel)) # if(atom_selections.solvent.count(True)>0): sol_sel = atom_selections_i_model.solvent model_statistics_geometry_solvent = model_statistics.geometry( pdb_hierarchy = hierarchy_i_seq.select(sol_sel), molprobity_scores = True, restraints_manager = restraints_manager.select(sol_sel)) # if(atom_selections.ligand.count(True)>0): lig_sel = atom_selections_i_model.ligand model_statistics_geometry_ligand = model_statistics.geometry( pdb_hierarchy = hierarchy_i_seq.select(lig_sel), molprobity_scores = True, restraints_manager = restraints_manager.select(lig_sel)) ### rms_b_iso_or_b_equiv_bonded = utils.rms_b_iso_or_b_equiv_bonded( restraints_manager = restraints_manager, xray_structure = xray_structure) # molprobity_stats_i_seq = molprobity_stats( model_statistics_geometry = model_statistics_geometry_all, resname_classes = overall_counts_i_seq.resname_classes) geometry_statistics.append(group_args( n_residues_in_altlocs = n_residues_in_altlocs, resname_classes = resname_classes, xray_structure_stat = xray_structure_stat, rms_b_iso_or_b_equiv_bonded = rms_b_iso_or_b_equiv_bonded, geometry_all = model_statistics_geometry_all, geometry_macromolecule = model_statistics_geometry_macromolecule, geometry_solvent = model_statistics_geometry_solvent, geometry_ligand = model_statistics_geometry_ligand, molprobity = molprobity_stats_i_seq)) mvd_obj.collect(models = geometry_statistics) return geometry_statistics
def try_den_weight_torsion(self, grid_pair): #backup_k_rep = self.params.tardy.\ # prolsq_repulsion_function_changes.k_rep local_seed = int(self.random_seed + grid_pair[1]) flex.set_random_seed(value=local_seed) random.seed(local_seed) self.fmodels.fmodel_xray().xray_structure.replace_scatterers( self.save_scatterers_local.deep_copy()) self.fmodels.update_xray_structure( xray_structure=self.fmodels.fmodel_xray().xray_structure, update_f_calc=True) utils.assert_xray_structures_equal( x1=self.fmodels.fmodel_xray().xray_structure, x2=self.model.get_xray_structure()) gamma_local = grid_pair[0] weight_local = grid_pair[1] self.model.restraints_manager.geometry.\ den_manager.gamma = gamma_local self.model.restraints_manager.geometry.\ den_manager.weight = weight_local cycle = 0 self.model.restraints_manager.geometry.\ den_manager.current_cycle = cycle+1 num_den_cycles = self.model.restraints_manager.geometry.\ den_manager.num_cycles if self.params.den.optimize and \ self.nproc != Auto and \ self.nproc > 1: local_log = sys.stdout elif self.params.den.optimize and \ self.nproc == 1: if self.verbose: local_log = self.log else: local_log = StringIO() else: local_log = self.log print >> self.log, " ...trying gamma %.1f, weight %.1f" % ( gamma_local, weight_local) while cycle < num_den_cycles: #if self.model.restraints_manager.geometry.\ # generic_restraints_manager.den_manager.current_cycle == \ # self.model.restraints_manager.geometry.\ # generic_restraints_manager.den_manager.torsion_mid_point+1: # self.params.tardy.\ # prolsq_repulsion_function_changes.k_rep = 1.0 print >> local_log, "DEN cycle %d" % (cycle + 1) #print >> local_log, "Random seed: %d" % flex.get_random_seed() r_free = self.fmodels.fmodel_xray().r_free() print >> local_log, "rfree at start of SA cycle: %.4f" % r_free print >> local_log, "k_rep = %.2f" % \ self.params.tardy.\ prolsq_repulsion_function_changes.k_rep tardy.run(fmodels=self.fmodels, model=self.model, target_weights=self.target_weights, params=self.params.tardy, log=local_log, format_for_phenix_refine=True, call_back_after_step=False) if self.params.den.bulk_solvent_and_scale: self.bulk_solvent_and_scale(log=local_log) self.fmodels.fmodel_xray( ).xray_structure = self.model.get_xray_structure() if self.params.den.refine_adp: self.adp_refinement(log=local_log) self.model.torsion_ncs_restraints_update(log=local_log) cycle += 1 self.model.restraints_manager.geometry.\ den_manager.current_cycle += 1 r_free = self.fmodels.fmodel_xray().r_free() print >> local_log, "rfree at end of SA cycle: %f" % r_free r_free = self.fmodels.fmodel_xray().r_free() step_xray_structure = self.fmodels.fmodel_xray().\ xray_structure.deep_copy_scatterers().scatterers() step_eq_distances = self.model.restraints_manager.geometry.\ den_manager.get_current_eq_distances() return (gamma_local, weight_local, r_free, step_xray_structure, step_eq_distances)
def __init__(self, fmodels, model, params, target_weights, macro_cycle, ncs_manager=None, log=None): if log is None: log = sys.stdout # self.ncs_manager = ncs_manager self.nproc = params.main.nproc if self.nproc is Auto: self.nproc = 1 self.verbose = params.den.verbose self.log = log self.fmodels = fmodels self.model = model self.params = params self.target_weights = target_weights self.adp_refinement_manager = None self.macro_cycle = macro_cycle self.tan_b_iso_max = 0 self.random_seed = params.main.random_seed den_manager = model.restraints_manager. \ geometry.den_manager print_statistics.make_header("DEN refinement", out=self.log) pdb_hierarchy = self.model.get_hierarchy(sync_with_xray_structure=True) if den_manager.get_n_proxies() == 0: print_statistics.make_sub_header("DEN restraint nework", out=self.log) den_manager.build_den_proxies(pdb_hierarchy=pdb_hierarchy) den_manager.build_den_restraints() den_manager.show_den_summary( sites_cart=self.model.get_sites_cart()) if den_manager.params.output_kinemage: den_manager.output_kinemage(self.model.get_sites_cart()) print_statistics.make_sub_header( "coordinate minimization before annealing", out=self.log) self.minimize(ca_only=self.params.den.minimize_c_alpha_only) self.save_scatterers_local = fmodels.fmodel_xray().\ xray_structure.deep_copy_scatterers().scatterers() #DEN refinement start, turn on if params.den.optimize: grid = den_manager.get_optimization_grid() print >> log, \ "Running DEN torsion optimization on %d processors..." % \ params.main.nproc else: grid = [(params.den.gamma, params.den.weight)] grid_results = [] grid_so = [] if "torsion" in params.den.annealing_type: print >> self.log, "Running torsion simulated annealing" if ((params.den.optimize) and ((self.nproc is Auto) or (self.nproc > 1))): stdout_and_results = easy_mp.pool_map( processes=params.main.nproc, fixed_func=self.try_den_weight_torsion, args=grid, func_wrapper="buffer_stdout_stderr") for so, r in stdout_and_results: if (r is None): raise RuntimeError(( "DEN weight optimization failed:" + "\n%s\nThis is a " + "serious error; please contact [email protected]." ) % so) grid_so.append(so) grid_results.append(r) else: for grid_pair in grid: result = self.try_den_weight_torsion(grid_pair=grid_pair) grid_results.append(result) self.show_den_opt_summary_torsion(grid_results) elif "cartesian" in params.den.annealing_type: print >> self.log, "Running Cartesian simulated annealing" if ((params.den.optimize) and ((self.nproc is Auto) or (self.nproc > 1))): stdout_and_results = easy_mp.pool_map( processes=params.main.nproc, fixed_func=self.try_den_weight_cartesian, args=grid, func_wrapper="buffer_stdout_stderr") for so, r in stdout_and_results: if (r is None): raise RuntimeError(( "DEN weight optimization failed:" + "\n%s\nThis is a " + "serious error; please contact [email protected]." ) % so) grid_so.append(so) grid_results.append(r) else: for grid_pair in grid: result = self.try_den_weight_cartesian(grid_pair=grid_pair) grid_results.append(result) self.show_den_opt_summary_cartesian(grid_results) else: raise "error in DEN annealing type" low_r_free = 1.0 best_xray_structure = None best_eq_distances = None best_gamma = None best_weight = None best_so_i = None for i, result in enumerate(grid_results): cur_r_free = result[2] if cur_r_free < low_r_free: low_r_free = cur_r_free best_gamma = result[0] best_weight = result[1] best_xray_structure = result[3] best_eq_distances = result[4] best_so_i = i assert best_xray_structure is not None if params.den.optimize: print >> self.log, "\nbest gamma: %.1f" % best_gamma print >> self.log, "best weight: %.1f\n" % best_weight if params.den.verbose: if len(grid_so) >= (best_so_i + 1): print >> self.log, "\nBest annealing results:\n" print >> self.log, grid_so[best_so_i] fmodels.fmodel_xray().xray_structure.replace_scatterers( best_xray_structure.deep_copy()) fmodels.update_xray_structure( xray_structure=fmodels.fmodel_xray().xray_structure, update_f_calc=True) utils.assert_xray_structures_equal( x1=fmodels.fmodel_xray().xray_structure, x2=model.get_xray_structure()) model.restraints_manager.geometry.\ den_manager.import_eq_distances(eq_distances=best_eq_distances) self.model.torsion_ncs_restraints_update(log=self.log)