def geometry_exploded(self, fmodel, geometry_rmsd_manager): result = False cctbx_rm_bonds_rmsd = calculator_module.get_bonds_rmsd( restraints_manager=geometry_rmsd_manager.geometry, xrs=fmodel.xray_structure) if (cctbx_rm_bonds_rmsd > self.max_bond_rmsd * 2.0): result = True return result
def run_collect(n_fev, results, fmodel, geometry_rmsd_manager, calculator): cctbx_rm_bonds_rmsd = calculator_module.get_bonds_rmsd( restraints_manager=geometry_rmsd_manager.geometry, xrs=fmodel.xray_structure) results.update( r_work=fmodel.r_work(), r_free=fmodel.r_free(), b=cctbx_rm_bonds_rmsd, xrs=fmodel.xray_structure, restraints_weight_scale=calculator.weights.restraints_weight_scale, n_fev=n_fev)
def run(model, fmodel, map_data, params, rst_file, prefix, log): validate(model, fmodel, params, rst_file, prefix, log) if(params.cluster.clustering): params.refine.gradient_only = True print >> log, " params.gradient_only", params.refine.gradient_only # RESTART if(os.path.isfile(str(rst_file))): print >> log, "restart info is loaded from %s" % params.rst_file rst_data = easy_pickle.load(params.rst_file) fmodel = rst_data["fmodel"] results_manager = rst_data["results"] results_manager.log = log weights = rst_data["weights"] geometry_rmsd_manager = rst_data["geometry_rmsd_manager"] start_fmodel = rst_data["rst_fmodel"] start_ph = model.pdb_hierarchy.deep_copy().adopt_xray_structure( start_fmodel.xray_structure) else: weights = None if (model.pdb_hierarchy.atoms().size() > params.max_atoms): raise Sorry("Too many atoms.") geometry_rmsd_manager = model.model.get_restraints_manager() cctbx_rm_bonds_rmsd = calculator.get_bonds_rmsd( restraints_manager = geometry_rmsd_manager.geometry, xrs = model.xray_structure) # if(params.refine.dry_run): return # r_work, r_free = None, None if(fmodel is not None): r_work, r_free = fmodel.r_work(), fmodel.r_free() results_manager = results.manager( r_work = r_work, r_free = r_free, b = cctbx_rm_bonds_rmsd, xrs = model.xray_structure, max_bond_rmsd = params.refine.max_bond_rmsd, max_r_work_r_free_gap = params.refine.max_r_work_r_free_gap, pdb_hierarchy = model.pdb_hierarchy, mode = params.refine.mode, log = log, restraints_weight_scale = params.refine.restraints_weight_scale) if(params.rst_file is None): if(params.output_file_name_prefix is None): params.output_file_name_prefix = prefix if(os.path.exists(params.output_folder_name) is False): os.mkdir(params.output_folder_name) params.rst_file = os.path.abspath(params.output_folder_name + "/" + \ params.output_file_name_prefix + ".rst.pickle") if os.path.isfile(params.rst_file): os.remove(params.rst_file) print >> log, "\n***********************************************************" print >> log, "restart info will be stored in %s" % params.rst_file print >> log, "***********************************************************\n" start_fmodel = fmodel start_ph = None # is it used anywhere? I don't see where it is used! if not params.refine.mode=='gtest': fragment_manager = create_fragment_manager( params = params, pdb_hierarchy = model.pdb_hierarchy, cif_objects = model.cif_objects, crystal_symmetry = model.xray_structure.crystal_symmetry()) restraints_manager = create_restraints_manager(params, model) if(params.refine.mode == "gtest"): # needs to be moved! Perhaps also to driver import numpy as np from fragment import fragment_extracts, write_cluster_and_fragments_pdbs from utils.mathbox import get_grad_mad, get_grad_angle # determine what kind of buffer to calculate g_mode=[] if params.cluster.g_mode is None: g_mode.append(1) if params.cluster.charge_embedding: g_mode.append(2) if params.cluster.two_buffers: g_mode.append(3) if params.cluster.two_buffers and params.cluster.charge_embedding: g_mode.append(4) else: g_mode.append(params.cluster.g_mode) # reset flags params.cluster.clustering=True params.cluster.save_clusters=True params.cluster.charge_embedding=False params.cluster.two_buffers=False grad=[] idx=0 idl=[] # input for cluster size cluster_scan=sorted([int(x) for x in params.cluster.g_scan.split()]) # cluster_scan=[2,10] if g_mode[0]==0: print >> log, 'warning: supersphere calculation!' params.cluster.clustering=False params.expansion=True cluster_scan=[0] clusters=[] n_grad=len(cluster_scan)*len(g_mode) print >> log, 'Calculating %3i gradients \n' % (n_grad) print >> log, 'Starting loop over different fragment sizes' for ig in g_mode: print >> log,'loop for g_mode = %i ' % (ig) if ig == 2: print >> log, 'pc on' params.cluster.charge_embedding=True if ig == 3: print >> log, 'two_buffers on, pc off' params.cluster.charge_embedding=False params.cluster.two_buffers=True if ig == 4: print >> log, 'two_buffers on, pc on' params.cluster.charge_embedding=True params.cluster.two_buffers=True for max_cluster in cluster_scan: idl.append([ig,max_cluster]) print >> log, 'g_mode: %s' % (" - ".join(map(str,idl[idx]))) t0 = time.time() print >> log, "~max cluster size ",max_cluster params.cluster.maxnum_residues_in_cluster=max_cluster fragment_manager = create_fragment_manager( params = params, pdb_hierarchy = model.pdb_hierarchy, cif_objects = model.cif_objects, crystal_symmetry = model.xray_structure.crystal_symmetry()) restraints_manager = create_restraints_manager(params, model) if(fragment_manager is not None): cluster_restraints_manager = cluster_restraints.from_cluster( restraints_manager = restraints_manager, fragment_manager = fragment_manager, parallel_params = params.parallel) rm = restraints_manager if(fragment_manager is not None): rm = cluster_restraints_manager print "time taken for fragments",(time.time() - t0) frags=fragment_manager print >> log, '~ # clusters : ',len(frags.clusters) print >> log, '~ list of atoms per cluster:' print >> log, '~ ',[len(x) for x in frags.cluster_atoms] print >> log, '~ list of atoms per fragment:' print >> log, '~ ',[len(x) for x in frags.fragment_super_atoms] # save fragment data. below works # better way is to make a single PDB file with chain IDs label="-".join(map(str,idl[idx])) write_cluster_and_fragments_pdbs(fragments=fragment_extracts(frags),directory=label) calculator_manager = create_calculator( weights = weights, fmodel = start_fmodel, model = model, params = params, restraints_manager = rm) grad=driver.run_gradient(calculator=calculator_manager) print >> log, '~ gnorm',np.linalg.norm(grad) print >> log, '~ max_g', max(abs(i) for i in grad), ' min_g',min(abs(i) for i in grad) name="-".join(map(str,idl[idx])) np.save(name,grad) idx+=1 print >> log, "total time for gradient",(time.time() - t0),'\n\n' print >> log, 'ready to run qr.granalyse!' else: rm = restraints_manager if(fragment_manager is not None): assert not params.expansion cluster_restraints_manager = cluster_restraints.from_cluster( restraints_manager = restraints_manager, fragment_manager = fragment_manager, parallel_params = params.parallel) rm = cluster_restraints_manager if(map_data is not None and params.refine.mode == "refine"): model.model.geometry_statistics(use_hydrogens=False).show() show_cc( map_data = map_data, xray_structure = model.xray_structure, log = log) O = calculator.sites_real_space( model = model.model, geometry_rmsd_manager = geometry_rmsd_manager, max_bond_rmsd = params.refine.max_bond_rmsd, map_data = map_data, data_weight = params.refine.data_weight, refine_cycles = params.refine.number_of_refine_cycles, skip_weight_search = params.refine.skip_weight_search, stpmax = params.refine.stpmax, gradient_only = params.refine.gradient_only, line_search = params.refine.line_search, restraints_manager = rm, max_iterations = params.refine.max_iterations_refine, log = log) model = O.run() of = open("real_space_refined.pdb", "w") print >> of, model.model_as_pdb(output_cs=True) of.close() model.geometry_statistics(use_hydrogens=False).show() show_cc( map_data=map_data, xray_structure=model.get_xray_structure(), log=log) return else: calculator_manager = create_calculator( weights=weights, fmodel=start_fmodel, model=model, params=params, restraints_manager=rm) if(params.refine.mode == "refine"): driver.refine( params = params, fmodel = fmodel, geometry_rmsd_manager = geometry_rmsd_manager, calculator = calculator_manager, results = results_manager) else: driver.opt( params = params, xray_structure = model.xray_structure, geometry_rmsd_manager = geometry_rmsd_manager, calculator = calculator_manager, results = results_manager) xrs_best = results_manager.finalize( input_file_name_prefix = prefix, output_file_name_prefix = params.output_file_name_prefix, output_folder_name = params.output_folder_name, use_r_work = params.refine.choose_best_use_r_work)
def opt(xray_structure, params, results, calculator, geometry_rmsd_manager): log_switch = None if (params.refine.opt_log or params.debug): log_switch = results.log rst_file = params.rst_file rst_data = restart_data(xray_structure, geometry_rmsd_manager) if (os.path.isfile(rst_file)): with open(rst_file, 'rb') as handle: rst_file_data = pickle.load(handle) micro_cycle_start = rst_file_data["micro_cycle"] print >> results.log, "\n***********************************************************" print >> results.log, "restarts from micro_cycle: %d" % ( micro_cycle_start) print >> results.log, "***********************************************************\n" ## check the restart fmodel xray_structure = calculator.xray_structure else: micro_cycle_start = 1 try: clustering = calculator.restraints_manager.clustering except: clustering = False if (clustering): cluster_qm_update = clustering_update( calculator.xray_structure.sites_cart(), results.log, \ params.rmsd_tolerance * 100) print >> results.log, "\ninteracting pairs number: ",\ calculator.restraints_manager.fragments.interacting_pairs results.show(prefix="start") for micro_cycle in xrange( micro_cycle_start, params.refine.number_of_micro_cycles + micro_cycle_start): if (clustering): cluster_qm_update.re_clustering(calculator) conv_test = convergence(xray_structure=calculator.xray_structure, params=params) rst_data.write_rst_file(rst_file, micro_cycle=micro_cycle, xray_structure=xray_structure, results=results) minimized = minimizer(log_switch=log_switch, calculator=calculator, stpmax=params.refine.stpmax, gradient_only=params.refine.gradient_only, line_search=params.refine.line_search, max_iterations=params.refine.max_iterations) calculator.update_xray_structure() cctbx_rm_bonds_rmsd = calculator_module.get_bonds_rmsd( restraints_manager=geometry_rmsd_manager.geometry, xrs=xray_structure) results.update( b=cctbx_rm_bonds_rmsd, xrs=xray_structure, n_fev=minimized.number_of_function_and_gradients_evaluations) results.write_pdb_file(output_folder_name=params.output_folder_name, output_file_name=str(micro_cycle) + "_opt_cycle.pdb") results.show(prefix="micro_cycle") if (conv_test.is_geometry_converged( sites_cart=xray_structure.sites_cart())): print >> results.log, " Convergence at micro_cycle:", micro_cycle break rst_data.write_rst_file(rst_file, micro_cycle=micro_cycle + 1, xray_structure=xray_structure, results=results)
def run(model, fmodel, map_data, params, rst_file, prefix, log): validate(model, fmodel, params, rst_file, prefix, log) if(params.cluster.clustering): params.refine.gradient_only = True print >> log, " params.gradient_only", params.refine.gradient_only # RESTART if(os.path.isfile(str(rst_file))): print >> log, "restart info is loaded from %s" % params.rst_file rst_data = easy_pickle.load(params.rst_file) fmodel = rst_data["fmodel"] results_manager = rst_data["results"] results_manager.log = log weights = rst_data["weights"] geometry_rmsd_manager = rst_data["geometry_rmsd_manager"] start_fmodel = rst_data["rst_fmodel"] start_ph = model.pdb_hierarchy.deep_copy().adopt_xray_structure( start_fmodel.xray_structure) else: weights = None if (model.pdb_hierarchy.atoms().size() > params.max_atoms): raise Sorry("Too many atoms.") geometry_rmsd_manager = restraints.from_cctbx( restraints_manager = model.model.get_restraints_manager(), ).geometry_restraints_manager cctbx_rm_bonds_rmsd = calculator.get_bonds_rmsd( restraints_manager = geometry_rmsd_manager.geometry, xrs = model.xray_structure) # if(params.refine.dry_run): return # r_work, r_free = None, None if(fmodel is not None): r_work, r_free = fmodel.r_work(), fmodel.r_free() results_manager = results.manager( r_work = r_work, r_free = r_free, b = cctbx_rm_bonds_rmsd, xrs = model.xray_structure, max_bond_rmsd = params.refine.max_bond_rmsd, max_r_work_r_free_gap = params.refine.max_r_work_r_free_gap, pdb_hierarchy = model.pdb_hierarchy, mode = params.refine.mode, log = log, restraints_weight_scale = params.refine.restraints_weight_scale) if(params.rst_file is None): if(params.output_file_name_prefix is None): params.output_file_name_prefix = prefix if(os.path.exists(params.output_folder_name) is False): os.mkdir(params.output_folder_name) params.rst_file = os.path.abspath(params.output_folder_name + "/" + \ params.output_file_name_prefix + ".rst.pickle") if os.path.isfile(params.rst_file): os.remove(params.rst_file) print >> log, "\n***********************************************************" print >> log, "restart info will be stored in %s" % params.rst_file print >> log, "***********************************************************\n" start_fmodel = fmodel start_ph = None # is it used anywhere? I don't see where it is used! fragment_manager = create_fragment_manager( params = params, pdb_hierarchy = model.pdb_hierarchy, cif_objects = model.cif_objects, crystal_symmetry = model.xray_structure.crystal_symmetry()) restraints_manager = create_restraints_manager( params = params, model = model) if(map_data is not None and params.refine.mode == "refine"): model.model.geometry_statistics().show() O = calculator.sites_real_space( model = model.model, geometry_rmsd_manager = geometry_rmsd_manager, max_bond_rmsd = params.refine.max_bond_rmsd, map_data = map_data, restraints_manager = restraints_manager, max_iterations = 100) model = O.run() of=open("real_space_refined.pdb", "w") print >> of, model.model_as_pdb(output_cs = True) of.close() model.geometry_statistics().show() else: if(fragment_manager is not None): cluster_restraints_manager = cluster_restraints.from_cluster( restraints_manager = restraints_manager, fragment_manager = fragment_manager, parallel_params = params.parallel) rm = restraints_manager if(fragment_manager is not None): rm = cluster_restraints_manager calculator_manager = create_calculator( weights = weights, fmodel = start_fmodel, model = model, params = params, restraints_manager = rm) if(params.refine.mode == "refine"): driver.refine( params = params, fmodel = fmodel, geometry_rmsd_manager = geometry_rmsd_manager, calculator = calculator_manager, results = results_manager) else: driver.opt( params = params, xray_structure = model.xray_structure, geometry_rmsd_manager = geometry_rmsd_manager, calculator = calculator_manager, results = results_manager) xrs_best = results_manager.finalize( input_file_name_prefix = prefix, output_file_name_prefix = params.output_file_name_prefix, output_folder_name = params.output_folder_name)