def get_initial_restraints_weight_scale( initial_fmodel, states, restraints_manager, sites, data_weight, max_bond_rmsd, geometry_rmsd_manager): initial_restraints_weight_scale = 1 b_rmsd = False counter=0 while(b_rmsd is False): counter+=1 print "Initial weight optimization conter:", counter if(counter>10): return 1. fmodel = initial_fmodel.deep_copy() minimizer.run( fmodel = fmodel, states = states, restraints_manager = restraints_manager, sites = True, data_weight = data_weight, restraints_weight_scale = initial_restraints_weight_scale) cctbx_rm_bonds_rmsd = geometry_rmsd_manager.energies_sites( sites_cart = fmodel.xray_structure.sites_cart(), compute_gradients = False).geometry.bond_deviations()[2] if(cctbx_rm_bonds_rmsd<=max_bond_rmsd and fmodel.r_work() < initial_fmodel.r_work()): b_rmsd=True else: initial_restraints_weight_scale *= 2. return initial_restraints_weight_scale
def run(fmodel, restraints_manager, states, pdb_hierarchy, number_of_macro_cycles, max_bond_rmsd, file_label, data_weight = None, file_out_prefix = None, r_tolerance = 0.001, rmsd_tolerance = 0.01, use_convergence_test = False, geometry_rmsd_manager = None): cctbx_rm_bonds_rmsd = geometry_rmsd_manager.energies_sites( sites_cart = fmodel.xray_structure.sites_cart(), compute_gradients = False).geometry.bond_deviations()[2] print "rmsd(b), start: %7.4f"%cctbx_rm_bonds_rmsd conv_test = convergence( fmodel=fmodel, r_tolerance=r_tolerance, rmsd_tolerance=rmsd_tolerance) restraints_weight_scale = 1.0 if(not (data_weight is not None and abs(data_weight)<1.e-9)): restraints_weight_scale = get_initial_restraints_weight_scale( initial_fmodel = fmodel, states = states, restraints_manager = restraints_manager, sites = True, data_weight = data_weight, max_bond_rmsd = max_bond_rmsd, geometry_rmsd_manager = geometry_rmsd_manager) print "initial_restraints_weight_scale", restraints_weight_scale res = results(r=fmodel.r_work(), b=cctbx_rm_bonds_rmsd, xrs=fmodel.xray_structure, max_bond_rmsd=max_bond_rmsd) for macro_cycle in xrange(number_of_macro_cycles): # refine coordinates minimized = minimizer.run( fmodel = fmodel, states = states, restraints_manager = restraints_manager, sites = True, data_weight = data_weight, restraints_weight_scale = restraints_weight_scale) # cctbx_rm_bonds_rmsd = geometry_rmsd_manager.energies_sites( sites_cart = fmodel.xray_structure.sites_cart(), compute_gradients = False).geometry.bond_deviations()[2] if(data_weight is None or abs(data_weight)>1.e-9): if(cctbx_rm_bonds_rmsd>max_bond_rmsd*10.0): print "the structure is distorted" break if(cctbx_rm_bonds_rmsd>max_bond_rmsd): restraints_weight_scale *= 2. if(cctbx_rm_bonds_rmsd<0.01): restraints_weight_scale /= 2. ### if(not (data_weight is not None and abs(data_weight)<1.e-9)): res.update(r=fmodel.r_work(), b=cctbx_rm_bonds_rmsd, xrs=fmodel.xray_structure) ####convergence test fmt="mc: %3d Rw: %6.4f Rf: %6.4f rmsd(b): %7.4f restraint_scale: %4.1f min_steps: %d" print fmt%(macro_cycle, fmodel.r_work(), fmodel.r_free(), cctbx_rm_bonds_rmsd, restraints_weight_scale, minimized.minimizer.nfun()) if(use_convergence_test and conv_test.is_converged(fmodel=fmodel)): print "Convergence reached, stop now." break ### if(not (data_weight is not None and abs(data_weight)<1.e-9)): xrs_best = res.choose_best() if(xrs_best is not None): fmodel.update_xray_structure(xray_structure = xrs_best, update_f_calc=True) pdb_hierarchy.adopt_xray_structure(xrs_best) print " r_factor (best): %7.5f"%fmodel.r_work() else: print " r_factor (best): None" ### if(1): pdb_hierarchy.adopt_xray_structure(fmodel.xray_structure) if(file_out_prefix==None): file_out_prefix="./pdb/" if( os.path.exists(file_out_prefix)) !=True: os.mkdir(file_out_prefix) job_type= "_refined" if (data_weight==None) else "_opt" file_out_prefix=file_out_prefix+file_label+ job_type pdb_hierarchy.write_pdb_file(file_name=file_out_prefix+".pdb", crystal_symmetry = fmodel.xray_structure.crystal_symmetry()) # good for debugging, useless otherwise # states.write(file_name=file_out_prefix+"_all_states.pdb") print "see the result structure: ", file_out_prefix+".pdb"