def run (args, out=sys.stdout) : from libtbx.utils import Usage if (len(args) == 0) : print "Warning: this is not intended for general use." raise Usage("mmtbx.flip_peptides [model.pdb] [data.mtz] [params ...]") from mmtbx.refinement import flip_peptides, print_statistics import mmtbx.command_line master_phil = mmtbx.command_line.generate_master_phil_with_inputs( enable_automatic_twin_detection=True, phil_string=""" flip_peptides { include scope mmtbx.refinement.flip_peptides.master_params_str }""") cmdline = mmtbx.command_line.load_model_and_data( args=args, master_phil=master_phil, out=out) print >> out, "" print_statistics.make_header("Analyzing peptide bonds", out=out) fmodel = cmdline.fmodel flip_peptides.run( fmodel=fmodel, geometry_restraints_manager=cmdline.geometry, pdb_hierarchy=cmdline.pdb_hierarchy, solvent_selection=None, params=cmdline.params.flip_peptides) if (len(cmdline.params.input.pdb.file_name) == 1) : pdb_file = cmdline.params.input.pdb.file_name[0] pdb_out = os.path.splitext(os.path.basename(pdb_file))[0] + "_new.pdb" else : pdb_out = "flipped.pdb" open(pdb_out, "w").write(cmdline.pdb_hierarchy.as_pdb_string()) print >> out, "" print "wrote new PDB file to %s" % pdb_out
def build_dod_and_od(model, fmodels, log=None, params=None): fmodels.update_xray_structure(xray_structure = model.xray_structure, update_f_calc = True, update_f_mask = True) fmodels.show_short() hbparams = params.hydrogens.build if hbparams is None: hbparams = all_master_params().extract() if log is None: log = fmodels.log if fmodels.fmodel_n is not None: fmodel = fmodels.fmodel_neutron() else: fmodel = fmodels.fmodel_xray() title = "xray" if fmodel.xray_structure.guess_scattering_type_neutron(): title="neutron" print_statistics.make_header("Build water hydrogens into "+title+" difference" " map", out=log) model = build_water_hydrogens_from_map2(model, fmodel, params=hbparams, log=log) # if hbparams.filter_dod: bmax=params.ordered_solvent.b_iso_max water_map_correlations(model, fmodels, log) model = remove_zero_occupancy(model,0.01,bmax) model.reprocess_pdb_hierarchy_inefficient() fmodels.update_xray_structure(xray_structure = model.xray_structure, update_f_calc = True, update_f_mask = True) fmodels.show_short() mmtbx.utils.assert_model_is_consistent(model) sol_sel = model.solvent_selection() hd_sel = sol_sel & model.xray_structure.hd_selection() print>>log, "Final number of water hydrogens: ", hd_sel.count(True) return model, fmodels
def run(fmodel, model, log, params = None): print_statistics.make_header("Fit water hydrogens into residual map", out = log) if(params is None): params = all_master_params().extract() print_statistics.make_sub_header("find peak-candidates", out = log) peaks = find_hydrogen_peaks( fmodel = fmodel, pdb_atoms = model.pdb_atoms, params = params, log = log) waters_and_peaks = extract_hoh_peaks( peaks = peaks, pdb_hierarchy = model.pdb_hierarchy(), pdb_atoms = model.pdb_atoms, xray_structure = model.xray_structure) print_statistics.make_sub_header("6D rigid body fit of HOH", out = log) print >> log, "Fit quality:" for water_and_peaks in waters_and_peaks: fit_water(water_and_peaks = water_and_peaks, xray_structure = model.xray_structure, params = params, log = log) # adjust ADP for H # TODO mrt: probably H bfactors should be equal to those # of the bonded atom u_isos = model.xray_structure.extract_u_iso_or_u_equiv() u_iso_mean = flex.mean(u_isos) sel_big = u_isos > u_iso_mean*2 hd_sel = model.xray_structure.hd_selection() sel_big.set_selected(~hd_sel, False) model.xray_structure.set_u_iso(value = u_iso_mean, selection = sel_big)
def run(args=(), params=None, pdb_hierarchy=None, xray_structure=None, out=None): if (out is None): out = sys.stdout print_statistics.make_header("phenix.find_tls_groups", out=out) default_message = """\ phenix.find_tls_groups: Tool for automated partitioning a model into TLS groups. Usage: phenix.find_tls_groups model.pdb [nproc=...] """ if (len(args) == 0): print(default_message) return cmdline_phil = [] for arg in args: if os.path.isfile(arg): if iotbx.pdb.is_pdb_file(arg) or iotbx.pdb.is_pdb_mmcif_file(arg): pdb_phil = libtbx.phil.parse("pdb_file=%s" % os.path.abspath(arg)) cmdline_phil.append(pdb_phil) else: try: file_phil = libtbx.phil.parse(file_name=arg) except Exception: raise Sorry("Bad parameter file: %s" % arg) cmdline_phil.append(file_phil) else: try: arg_phil = libtbx.phil.parse(arg) except Exception: raise Sorry("Bad parameter: %s" % arg) cmdline_phil.append(arg_phil) working_phil = master_phil.fetch(sources=cmdline_phil) params = working_phil.extract() # XXX params.pdb_file is not used anymore. Maybe should be removed. pdb_file_name = params.pdb_file if ((pdb_file_name is None) or (not iotbx.pdb.is_pdb_file(pdb_file_name) and not iotbx.pdb.is_pdb_mmcif_file(pdb_file_name))): print("A model file is required.") return if (params.nproc is None): params.nproc = 1 pdb_inp = iotbx.pdb.input(file_name=pdb_file_name) pdb_hierarchy = pdb_inp.construct_hierarchy() pdb_atoms = pdb_hierarchy.atoms() pdb_atoms.reset_i_seq() # xray_structure = pdb_inp.xray_structure_simple() return find_tls( params=params, # pdb_inp = pdb_inp, pdb_hierarchy=pdb_hierarchy, xray_structure=xray_structure, out=out)
def run(fmodel, model, log, params=None): print_statistics.make_header("Fit water hydrogens into residual map", out=log) if (params is None): params = all_master_params().extract() print_statistics.make_sub_header("find peak-candidates", out=log) peaks = find_hydrogen_peaks(fmodel=fmodel, pdb_atoms=model.get_atoms(), params=params, log=log) waters_and_peaks = extract_hoh_peaks( peaks=peaks, pdb_hierarchy=model.get_hierarchy(), pdb_atoms=model.get_atoms(), xray_structure=model.get_xray_structure()) print_statistics.make_sub_header("6D rigid body fit of HOH", out=log) print("Fit quality:", file=log) for water_and_peaks in waters_and_peaks: fit_water(water_and_peaks=water_and_peaks, xray_structure=model.get_xray_structure(), params=params, log=log) # adjust ADP for H # TODO mrt: probably H bfactors should be equal to those # of the bonded atom u_isos = model.get_xray_structure().extract_u_iso_or_u_equiv() u_iso_mean = flex.mean(u_isos) sel_big = u_isos > u_iso_mean * 2 hd_sel = model.get_hd_selection() sel_big.set_selected(~hd_sel, False) model.get_xray_structure().set_u_iso(value=u_iso_mean, selection=sel_big)
def update_all_scales(self, update_f_part1, remove_outliers=True, params=None, optimize_mask=False, force_update_f_mask=False, nproc=1, log=None, apply_back_trace=False, refine_hd_scattering=None): """ ... """ if log is None: log = self.log fast = True if (params.mode == "slow"): fast = False from mmtbx.refinement import print_statistics print_statistics.make_header("updating all scales", out=log) self.update_xray_structure(update_f_calc=True, update_f_mask=True, force_update_f_mask=force_update_f_mask) if ([self.fmodel_x, self.fmodel_n].count(None) == 0): apply_back_trace = False if (self.fmodel_x is not None): msg = None if (self.fmodel_n is not None): msg = "X-ray:" print >> log, msg self.fmodel_xray().update_all_scales( update_f_part1=update_f_part1, remove_outliers=remove_outliers, params=params, fast=fast, log=log, show=True, optimize_mask=optimize_mask, nproc=nproc, apply_back_trace=apply_back_trace, refine_hd_scattering=refine_hd_scattering) self.fmodel_x.show(log=log, suffix=msg) if (self.fmodel_n is not None): msg = "Neutron:" print >> log, msg self.fmodel_neutron().update_all_scales( update_f_part1=update_f_part1, remove_outliers=remove_outliers, params=params, fast=fast, log=log, show=True, optimize_mask=optimize_mask, nproc=nproc, apply_back_trace=apply_back_trace, refine_hd_scattering=refine_hd_scattering) self.fmodel_n.show(log=log, suffix=msg)
def update_all_scales( self, update_f_part1, remove_outliers = True, params = None, optimize_mask = False, force_update_f_mask = False, nproc = 1, log = None, apply_back_trace = False, refine_hd_scattering=None): """ ... """ if log is None: log = self.log fast=True if(params.mode=="slow"): fast=False from mmtbx.refinement import print_statistics print_statistics.make_header("updating all scales", out = log) self.update_xray_structure(update_f_calc = True, update_f_mask = True, force_update_f_mask = force_update_f_mask) if([self.fmodel_x, self.fmodel_n].count(None)==0): apply_back_trace=False if(self.fmodel_x is not None): msg = None if(self.fmodel_n is not None): msg = "X-ray:" print >> log, msg self.fmodel_xray().update_all_scales( update_f_part1 = update_f_part1, remove_outliers = remove_outliers, params = params, fast = fast, log = log, show = True, optimize_mask = optimize_mask, nproc = nproc, apply_back_trace = apply_back_trace, refine_hd_scattering = refine_hd_scattering) self.fmodel_x.show(log = log, suffix = msg) if(self.fmodel_n is not None): msg = "Neutron:" print >> log, msg self.fmodel_neutron().update_all_scales( update_f_part1 = update_f_part1, remove_outliers = remove_outliers, params = params, fast = fast, log = log, show = True, optimize_mask = optimize_mask, nproc = nproc, apply_back_trace = apply_back_trace, refine_hd_scattering = refine_hd_scattering) self.fmodel_n.show(log = log, suffix = msg)
def run(args, crystal_symmetry=None, ncs_object=None, pdb_hierarchy=None, map_data=None, lower_bounds=None, upper_bounds=None, write_output_files=True, log=None): h = "phenix.map_box: extract box with model and map around selected atoms" if (log is None): log = sys.stdout print_statistics.make_header(h, out=log) default_message = """\ %s. Usage: phenix.map_box model.pdb map_coefficients.mtz selection="chain A and resseq 1:10" or phenix.map_box map.ccp4 density_select=True Parameters:""" % h if (len(args) == 0 and not pdb_hierarchy): print default_message master_phil.show(prefix=" ") return # Process inputs ignoring symmetry conflicts just to get the value of # ignore_symmetry_conflicts... inputs = mmtbx.utils.process_command_line_args( args=args, cmd_cs=crystal_symmetry, master_params=master_phil, suppress_symmetry_related_errors=True) params = inputs.params.extract() # Now process inputs for real and write a nice error message if necessary. try: inputs = mmtbx.utils.process_command_line_args( args=args, cmd_cs=crystal_symmetry, master_params=master_phil, suppress_symmetry_related_errors=params.ignore_symmetry_conflicts) except Exception, e: if str(e).find("symmetry mismatch ") > 1: raise Sorry(str(e) + "\nTry 'ignore_symmetry_conflicts=True'") else: raise e
def manager(params, target_weights, all_params, macro_cycle, h_params, fmodels, model, out=None, states_collector=None, callback=None): if (out is None): out = sys.stdout if (states_collector is not None): assert hasattr(states_collector, "add") print_statistics.make_header("simulated annealing refinement", out=out) model.set_refine_individual_sites() fmodel = fmodels.fmodel_xray() # XXX use only xray data fmodel.xray_structure = model.get_xray_structure( ) # XXX use only xray data if (params.max_number_of_iterations >= 0): print_statistics.make_sub_header( "lbfgs minimization: before simulated annealing", out=out) is_neutron_scat_table = False if (all_params.main.scattering_table == "neutron"): is_neutron_scat_table = True import scitbx.lbfgs minimized = mmtbx.refinement.minimization.lbfgs( restraints_manager=model.restraints_manager, refine_xyz=True, fmodels=fmodels, is_neutron_scat_table=is_neutron_scat_table, model=model, lbfgs_termination_params=scitbx.lbfgs.termination_parameters( max_iterations=params.max_number_of_iterations), target_weights=target_weights, h_params=h_params, verbose=0) fmodel.update_xray_structure(xray_structure=model.get_xray_structure(), update_f_calc=True, update_f_mask=True) print_statistics.make_header("simulated annealing", out=out) wx = target_weights.xyz_weights_result.wx * \ target_weights.xyz_weights_result.wx_scale run(params=params, restraints_manager=model.restraints_manager, fmodel=fmodel, wx=wx, wc=target_weights.xyz_weights_result.w, states_collector=states_collector, callback=callback, log=out)
def manager(params, target_weights, all_params, macro_cycle, h_params, fmodels, model, out = None, states_collector=None, callback=None): if(out is None): out = sys.stdout if (states_collector is not None) : assert hasattr(states_collector, "add") print_statistics.make_header("simulated annealing refinement", out = out) model.set_refine_individual_sites() fmodel = fmodels.fmodel_xray() # XXX use only xray data fmodel.xray_structure = model.xray_structure # XXX use only xray data if (params.max_number_of_iterations >= 0): print_statistics.make_sub_header( "lbfgs minimization: before simulated annealing", out = out) is_neutron_scat_table = False if(all_params.main.scattering_table == "neutron"): is_neutron_scat_table = True import scitbx.lbfgs minimized = mmtbx.refinement.minimization.lbfgs( restraints_manager = model.restraints_manager, refine_xyz = True, fmodels = fmodels, is_neutron_scat_table = is_neutron_scat_table, model = model, lbfgs_termination_params = scitbx.lbfgs.termination_parameters( max_iterations = params.max_number_of_iterations), target_weights = target_weights, h_params = h_params, verbose = 0) fmodel.update_xray_structure(xray_structure = model.xray_structure, update_f_calc = True, update_f_mask = True) print_statistics.make_header("simulated annealing", out = out) wx = target_weights.xyz_weights_result.wx * \ target_weights.xyz_weights_result.wx_scale run( params = params, restraints_manager = model.restraints_manager, fmodel = fmodel, wx = wx, wc = target_weights.xyz_weights_result.w, states_collector = states_collector, callback = callback, log = out)
def run (args=(), params=None, pdb_hierarchy=None, xray_structure=None, out=None) : if (out is None) : out = sys.stdout print_statistics.make_header("phenix.find_tls_groups", out=out) default_message="""\ phenix.find_tls_groups: Tool for automated partitioning a model into TLS groups. Usage: phenix.find_tls_groups model.pdb [nproc=...] """ if(len(args) == 0): print default_message return cmdline_phil = [] for arg in args : if os.path.isfile(arg): if iotbx.pdb.is_pdb_file(arg): pdb_phil = libtbx.phil.parse("pdb_file=%s" % os.path.abspath(arg)) cmdline_phil.append(pdb_phil) else: try: file_phil = libtbx.phil.parse(file_name=arg) except Exception: raise Sorry("Bad parameter file: %s"%arg) cmdline_phil.append(file_phil) else: try: arg_phil = libtbx.phil.parse(arg) except Exception: raise Sorry("Bad parameter: %s"%arg) cmdline_phil.append(arg_phil) working_phil = master_phil.fetch(sources=cmdline_phil) params = working_phil.extract() pdb_file_name = params.pdb_file if (pdb_file_name is None) or (not iotbx.pdb.is_pdb_file(pdb_file_name)) : print "A model file is required." return if (params.nproc is None) : params.nproc = 1 pdb_inp = iotbx.pdb.input(file_name=pdb_file_name) pdb_hierarchy = pdb_inp.construct_hierarchy() pdb_atoms = pdb_hierarchy.atoms() pdb_atoms.reset_i_seq() # xray_structure = pdb_inp.xray_structure_simple() return find_tls( params = params, pdb_inp = pdb_inp, pdb_hierarchy = pdb_hierarchy, xray_structure = xray_structure, out = out)
def updaterdl(self, prefix): if self.restraints_manager is None: return from mmtbx.conformation_dependent_library import rotamers from mmtbx.refinement import print_statistics print_statistics.make_header(prefix, out=self.log) self.rdl_proxies = None #rotamers.setup_restraints(result) rc = rotamers.update_restraints( self.pdb_hierarchy, self.restraints_manager.geometry, current_geometry=self.pdb_hierarchy.extract_xray_structure(), rdl_proxies=self.rdl_proxies, log=self.log, verbose=False, ) print >> self.log, "=" * 79 return rc
def updaterdl(self, prefix): if self.restraints_manager is None: return from mmtbx.conformation_dependent_library import rotamers from mmtbx.refinement import print_statistics print_statistics.make_header(prefix, out=self.log) self.rdl_proxies = None #rotamers.setup_restraints(result) rc = rotamers.update_restraints( self.pdb_hierarchy, self.restraints_manager.geometry, current_geometry=self.pdb_hierarchy.extract_xray_structure(), rdl_proxies=self.rdl_proxies, log=self.log, verbose=False, ) print >> self.log, "="*79 return rc
def external_tls(pdb_inp, pdb_hierarchy, sites_cart, u_iso, out=None): if (out is None): out = sys.stdout pdb_inp_tls = pdb_inp.extract_tls_params(pdb_hierarchy) print_statistics.make_header("TLS groups from PDB file header", out=out) selection_strings = [] if (len(pdb_inp_tls.tls_params) > 0): for tp in pdb_inp_tls.tls_params: print >> out, " ", tp.selection_string selection_strings.append(tp.selection_string) else: print >> out, " ... none found." if (len(selection_strings) > 0): total_target = total_score(pdb_hierarchy=pdb_hierarchy, sites_cart=sites_cart, u_iso=u_iso, selection_strings=selection_strings) print >> out print >> out, "Total target for groups from PDB file header: %10.1f" % total_target
def water_map_correlations(model, fmodels, log=None): print_statistics.make_header("Water real space correlations", out=log) fmodels.update_xray_structure(xray_structure=model.get_xray_structure(), update_f_calc=True, update_f_mask=True) fmodels.show_short() scatterers = model.get_xray_structure().scatterers() waters = model.solvent_selection() water_rgs = model.extract_water_residue_groups() n_atoms = len(scatterers) for rg in water_rgs: o_atom = get_pdb_oxygen(rg) o_scat = scatterers[o_atom.i_seq] water = select_one_water(rg, n_atoms) if water.count(True) < 2: continue for s, u in zip(scatterers, water): if u: e = s.element_symbol().strip() if e == 'D': if s.occupancy <= 0.02 or s.occupancy >= 0.98: keep_occ = s.occupancy neutron_cc = one_water_correlation( model, fmodels, water) if s.occupancy <= 0.02: s.occupancy = 1.0 s.u_iso = o_scat.u_iso else: s.occupancy = 0.0 fmodels.update_xray_structure( xray_structure=model.get_xray_structure(), update_f_calc=True, update_f_mask=True) ncc = one_water_correlation(model, fmodels, water) if ncc < neutron_cc: s.occupancy = keep_occ fmodels.update_xray_structure( xray_structure=model.get_xray_structure(), update_f_calc=True, update_f_mask=True) else: neutron_cc = ncc return True
def show_den_opt_summary_cartesian(self, grid_results): print_statistics.make_header( "DEN Cartesian weight optimization results", out=self.log) print >>self.log,"|---------------------------------------"+\ "--------------------------------------|" print >>self.log,"| Gamma Weight R-free "+\ " |" for result in grid_results: if result == None: raise RuntimeError("Parallel DEN job failed: %s" % str(out)) cur_gamma = result[0] cur_weight = result[1] cur_r_free = result[2] print >> self.log, "| %6.1f %6.1f %6.4f " %\ (cur_gamma, cur_weight, cur_r_free)+\ " |" print >>self.log,"|---------------------------------------"+\ "--------------------------------------|"
def summarize_contacts_by_residue(residue_contacts, pdb_hierarchy, out=sys.stdout): from mmtbx.refinement.print_statistics import make_header summary = extract_closest_contacting_residues(residue_contacts, pdb_hierarchy.atoms()) make_header("Crystal contacts by residue", out=out) print >> out, " %-16s %-16s %-16s %-16s" % ("residue", "closest contact", "symop", "distance (A)") print >> out, "-" * 72 for (residue_key, contact_key, sym_op, distance) in summary: (chain_id, resname, resid, altloc) = residue_key id_str = "%s%5s %3s %s" % (chain_id, resid, resname, altloc) if (contact_key is None): print >> out, " %-16s %-16s %-16s %-4s" % (id_str, "*", "*", "*") else: (chain_id, resname, resid, altloc) = contact_key id_str_2 = "%s%5s %3s %s" % (chain_id, resid, resname, altloc) print >> out, " %-16s %-16s %-16s %-4.2f" % (id_str, id_str_2, sym_op, distance)
def summarize_contacts_by_residue (residue_contacts, pdb_hierarchy, out=sys.stdout) : from mmtbx.refinement.print_statistics import make_header summary = extract_closest_contacting_residues(residue_contacts, pdb_hierarchy.atoms()) make_header("Crystal contacts by residue", out=out) print >> out, " %-16s %-16s %-16s %-16s" % ("residue", "closest contact", "symop", "distance (A)") print >> out, "-"*72 for (residue_key, contact_key, sym_op, distance) in summary : (chain_id, resname, resid, altloc) = residue_key id_str = "%s%5s %3s %s" % (chain_id, resid, resname, altloc) if (contact_key is None) : print >> out, " %-16s %-16s %-16s %-4s" % (id_str, "*","*","*") else : (chain_id, resname, resid, altloc) = contact_key id_str_2 = "%s%5s %3s %s" % (chain_id, resid, resname, altloc) print >> out, " %-16s %-16s %-16s %-4.2f" % (id_str, id_str_2, sym_op, distance)
def external_tls(pdb_inp, pdb_hierarchy, sites_cart, u_iso, out=None) : if (out is None) : out = sys.stdout pdb_inp_tls = pdb_inp.extract_tls_params(pdb_hierarchy) print_statistics.make_header("TLS groups from PDB file header", out = out) selection_strings = [] if(len(pdb_inp_tls.tls_params)>0): for tp in pdb_inp_tls.tls_params: print >> out, " ", tp.selection_string selection_strings.append(tp.selection_string) else: print >> out, " ... none found." if(len(selection_strings)>0): total_target = total_score( pdb_hierarchy = pdb_hierarchy, sites_cart = sites_cart, u_iso = u_iso, selection_strings = selection_strings) print >> out print >> out, "Total target for groups from PDB file header: %10.1f"%total_target
def build_dod_and_od(model, fmodels, log=None, params=None): fmodels.update_xray_structure(xray_structure=model.get_xray_structure(), update_f_calc=True, update_f_mask=True) fmodels.show_short() hbparams = params.hydrogens.build if hbparams is None: hbparams = all_master_params().extract() if log is None: log = fmodels.log if fmodels.fmodel_n is not None: fmodel = fmodels.fmodel_neutron() else: fmodel = fmodels.fmodel_xray() title = "xray" if fmodel.xray_structure.guess_scattering_type_neutron(): title = "neutron" print_statistics.make_header("Build water hydrogens into " + title + " difference" " map", out=log) model = build_water_hydrogens_from_map2(model, fmodel, params=hbparams, log=log) # if hbparams.filter_dod: bmax = params.ordered_solvent.b_iso_max water_map_correlations(model, fmodels, log) model = remove_zero_occupancy(model, 0.01, bmax) model.reprocess_pdb_hierarchy_inefficient() fmodels.update_xray_structure(xray_structure=model.get_xray_structure(), update_f_calc=True, update_f_mask=True) fmodels.show_short() mmtbx.utils.assert_model_is_consistent(model) sol_sel = model.solvent_selection() hd_sel = sol_sel & model.get_hd_selection() print("Final number of water hydrogens: ", hd_sel.count(True), file=log) return model, fmodels
def refine_adp_for_h_only(fmodels, model, params, prefix, log): print_statistics.make_header(prefix, out = log) xrs_start = model.xray_structure.deep_copy_scatterers() fmodels.show_targets(log = log, text = "start") fmodels.fmodel_xray().xray_structure.scatterers().flags_set_grads( state = False) i_selection = model.xray_structure.hd_selection().iselection() fmodels.fmodel_xray().xray_structure.scatterers( ).flags_set_grad_u_iso(iselection = i_selection) lbfgs_termination_params = scitbx.lbfgs.termination_parameters( max_iterations = params.main.max_number_of_iterations) is_neutron_scat_table = False if(params.main.scattering_table == "neutron"): is_neutron_scat_table = True minimized = mmtbx.refinement.minimization.lbfgs( restraints_manager = None, is_neutron_scat_table = is_neutron_scat_table, fmodels = fmodels, model = model, refine_adp = True, lbfgs_termination_params = lbfgs_termination_params) fmodels.show_targets(log = log, text = "final")
def refine_adp_for_ias_only(fmodels, model, params, prefix, log): print_statistics.make_header(prefix, out = log) xrs_start = model.xray_structure.deep_copy_scatterers() fmodels.show_targets(log = log, text = "start") fmodels.fmodel_xray().xray_structure.scatterers().flags_set_grads( state = False) i_selection = model.ias_selection.iselection() scatterers = fmodels.fmodel_xray().xray_structure.scatterers() for i_sel in i_selection: if(scatterers[i_sel].flags.use_u_iso()): scatterers[i_sel].flags.set_grad_u_iso(True) if(scatterers[i_sel].flags.use_u_aniso()): scatterers[i_sel].flags.set_grad_u_aniso(True) # lbfgs_termination_params = scitbx.lbfgs.termination_parameters( max_iterations = params.main.max_number_of_iterations) minimized = mmtbx.refinement.minimization.lbfgs( restraints_manager = None, fmodels = fmodels, model = model, refine_adp = True, lbfgs_termination_params = lbfgs_termination_params) fmodels.show_targets(log = log, text = "final")
def show_highest_peaks_and_deepest_holes(fmodel, pdb_atoms, map_type, map_cutoff_plus, map_cutoff_minus, log=None): if (log is None): log = sys.stdout print_statistics.make_header( "residual map %s: highest peaks and deepst holes" % map_type, out=log) fp_params = master_params.extract() fp_params.map_next_to_model.min_model_peak_dist = 0.7 fp_params.map_next_to_model.min_peak_peak_dist = 0.7 fp_params.peak_search.min_cross_distance = 0.7 fp_params.map_next_to_model.use_hydrogens = True for par in [(map_cutoff_plus, "peaks"), (map_cutoff_minus, "holes")]: print_statistics.make_sub_header(par[1], out=log) result = manager(fmodel=fmodel, map_type="mFobs-DFmodel", map_cutoff=par[0], params=fp_params, log=log) result.peaks_mapped() result.show_mapped(pdb_atoms=pdb_atoms)
def show_highest_peaks_and_deepest_holes(fmodel, pdb_atoms, map_type, map_cutoff_plus, map_cutoff_minus, log = None): if(log is None): log = sys.stdout print_statistics.make_header( "residual map %s: highest peaks and deepst holes"%map_type, out = log) fp_params = master_params.extract() fp_params.map_next_to_model.min_model_peak_dist = 0.7 fp_params.map_next_to_model.min_peak_peak_dist = 0.7 fp_params.peak_search.min_cross_distance = 0.7 fp_params.map_next_to_model.use_hydrogens = True for par in [(map_cutoff_plus,"peaks"), (map_cutoff_minus,"holes")]: print_statistics.make_sub_header(par[1], out = log) result = manager(fmodel = fmodel, map_type = "mFobs-DFmodel", map_cutoff = par[0], params = fp_params, log = log) result.peaks_mapped() result.show_mapped(pdb_atoms = pdb_atoms)
def water_map_correlations(model, fmodels, log=None): print_statistics.make_header("Water real space correlations", out=log) fmodels.update_xray_structure(xray_structure = model.xray_structure, update_f_calc=True, update_f_mask=True) fmodels.show_short() scatterers = model.xray_structure.scatterers() waters = model.solvent_selection() water_rgs = model.extract_water_residue_groups() n_atoms = len(scatterers) for rg in water_rgs: o_atom = get_pdb_oxygen(rg) o_scat = scatterers[o_atom.i_seq] water = select_one_water(rg, n_atoms) if water.count(True) < 2: continue for s,u in zip(scatterers,water): if u: e = s.element_symbol().strip() if e=='D': if s.occupancy <= 0.02 or s.occupancy >=0.98: keep_occ = s.occupancy neutron_cc = one_water_correlation(model, fmodels, water) if s.occupancy <= 0.02: s.occupancy = 1.0 s.u_iso = o_scat.u_iso else: s.occupancy = 0.0 fmodels.update_xray_structure(xray_structure = model.xray_structure, update_f_calc=True, update_f_mask=True) ncc = one_water_correlation(model, fmodels, water) if ncc < neutron_cc: s.occupancy = keep_occ fmodels.update_xray_structure(xray_structure = model.xray_structure, update_f_calc=True, update_f_mask=True) else: neutron_cc = ncc return True
def run(args, crystal_symmetry=None, ncs_object=None, pdb_hierarchy=None, map_data=None, lower_bounds=None, upper_bounds=None, write_output_files=True, log=None): h = "phenix.map_box: extract box with model and map around selected atoms" if (log is None): log = sys.stdout print_statistics.make_header(h, out=log) default_message = """\ %s. Usage: phenix.map_box model.pdb map_coefficients.mtz selection="chain A and resseq 1:10" or phenix.map_box map.ccp4 density_select=True Parameters:""" % h if (len(args) == 0 and not pdb_hierarchy): print default_message master_phil.show(prefix=" ") return inputs = mmtbx.utils.process_command_line_args(args=args, cmd_cs=crystal_symmetry, master_params=master_phil) params = inputs.params.extract() master_phil.format(python_object=params).show(out=log) # Overwrite params with parameters in call if available if lower_bounds: params.lower_bounds = lower_bounds if upper_bounds: params.upper_bounds = upper_bounds # PDB file if params.pdb_file and not inputs.pdb_file_names and not pdb_hierarchy: inputs.pdb_file_names = [params.pdb_file] if (len(inputs.pdb_file_names) != 1 and not params.density_select and not pdb_hierarchy and not params.keep_map_size and not params.upper_bounds and not params.extract_unique): raise Sorry("PDB file is needed unless extract_unique, " + "density_select, keep_map_size \nor bounds are set .") if (len(inputs.pdb_file_names)!=1 and not pdb_hierarchy and \ (params.mask_atoms or params.soft_mask )): raise Sorry("PDB file is needed for mask_atoms or soft_mask") if (params.density_select and params.keep_map_size): raise Sorry("Cannot set both density_select and keep_map_size") if (params.density_select and params.upper_bounds): raise Sorry("Cannot set both density_select and bounds") if (params.keep_map_size and params.upper_bounds): raise Sorry("Cannot set both keep_map_size and bounds") if (params.upper_bounds and not params.lower_bounds): raise Sorry("Please set lower_bounds if you set upper_bounds") if (params.extract_unique and not params.resolution): raise Sorry("Please set resolution for extract_unique") print_statistics.make_sub_header("pdb model", out=log) if len(inputs.pdb_file_names) > 0: pdb_inp = iotbx.pdb.input(file_name=inputs.pdb_file_names[0]) pdb_hierarchy = pdb_inp.construct_hierarchy() if pdb_hierarchy: pdb_atoms = pdb_hierarchy.atoms() pdb_atoms.reset_i_seq() else: pdb_hierarchy = None # Map or map coefficients map_coeff = None if (not map_data): # read first mtz file if ((len(inputs.reflection_file_names) > 0) or (params.map_coefficients_file is not None)): # file in phil takes precedent if (params.map_coefficients_file is not None): if (len(inputs.reflection_file_names) == 0): inputs.reflection_file_names.append( params.map_coefficients_file) else: inputs.reflection_file_names[ 0] = params.map_coefficients_file map_coeff = reflection_file_utils.extract_miller_array_from_file( file_name=inputs.reflection_file_names[0], label=params.label, type="complex", log=log) if not crystal_symmetry: crystal_symmetry = map_coeff.crystal_symmetry() fft_map = map_coeff.fft_map( resolution_factor=params.resolution_factor) fft_map.apply_sigma_scaling() map_data = fft_map.real_map_unpadded() map_or_map_coeffs_prefix = os.path.basename( inputs.reflection_file_names[0][:-4]) # or read CCP4 map elif ((inputs.ccp4_map is not None) or (params.ccp4_map_file is not None)): if (params.ccp4_map_file is not None): af = any_file(params.ccp4_map_file) if (af.file_type == 'ccp4_map'): inputs.ccp4_map = af.file_content inputs.ccp4_map_file_name = params.ccp4_map_file print_statistics.make_sub_header("CCP4 map", out=log) ccp4_map = inputs.ccp4_map ccp4_map.show_summary(prefix=" ", out=log) if not crystal_symmetry: crystal_symmetry = ccp4_map.crystal_symmetry() map_data = ccp4_map.data #map_data() if inputs.ccp4_map_file_name.endswith(".ccp4"): map_or_map_coeffs_prefix = os.path.basename( inputs.ccp4_map_file_name[:-5]) else: map_or_map_coeffs_prefix = os.path.basename( inputs.ccp4_map_file_name[:-4]) else: # have map_data map_or_map_coeffs_prefix = None if crystal_symmetry and not inputs.crystal_symmetry: inputs.crystal_symmetry = crystal_symmetry # final check that map_data exists if (map_data is None): raise Sorry("Map or map coefficients file is needed.") if len(inputs.pdb_file_names) > 0: output_prefix = os.path.basename(inputs.pdb_file_names[0])[:-4] else: output_prefix = map_or_map_coeffs_prefix if not pdb_hierarchy: # get an empty hierarchy from cctbx.array_family import flex pdb_hierarchy = iotbx.pdb.input( source_info='', lines=flex.split_lines('')).construct_hierarchy() xray_structure = pdb_hierarchy.extract_xray_structure( crystal_symmetry=inputs.crystal_symmetry) xray_structure.show_summary(f=log) # selection = pdb_hierarchy.atom_selection_cache().selection( string=params.selection) if selection.size(): print_statistics.make_sub_header("atom selection", out=log) print >> log, "Selection string: selection='%s'" % params.selection print >> log, \ " selects %d atoms from total %d atoms."%(selection.count(True), selection.size()) sites_cart_all = xray_structure.sites_cart() sites_cart = sites_cart_all.select(selection) selection = xray_structure.selection_within(radius=params.selection_radius, selection=selection) if not ncs_object: from mmtbx.ncs.ncs import ncs ncs_object = ncs() if params.symmetry_file: ncs_object.read_ncs(params.symmetry_file, log=log) print >> log, "Total of %s operators read" % ( ncs_object.max_operators()) if not ncs_object or ncs_object.max_operators() < 1: print >> log, "No symmetry available" if ncs_object: n_ops = max(1, ncs_object.max_operators()) else: n_ops = 1 # Get sequence if extract_unique is set sequence = None if params.extract_unique: if params.sequence_file: if n_ops > 1: # get unique part of sequence and multiply remove_duplicates = True else: remove_duplicates = False from iotbx.bioinformatics import get_sequences sequence = n_ops * (" ".join( get_sequences(file_name=params.sequence_file, remove_duplicates=remove_duplicates))) if sequence and not params.molecular_mass: # get molecular mass from sequence from iotbx.bioinformatics import text_from_chains_matching_chain_type if params.chain_type in [None, 'PROTEIN']: n_protein = len( text_from_chains_matching_chain_type(text=sequence, chain_type='PROTEIN')) else: n_protein = 0 if params.chain_type in [None, 'RNA']: n_rna = len( text_from_chains_matching_chain_type(text=sequence, chain_type='RNA')) else: n_rna = 0 if params.chain_type in [None, 'DNA']: n_dna = len( text_from_chains_matching_chain_type(text=sequence, chain_type='DNA')) else: n_dna = 0 params.molecular_mass = n_protein * 110 + (n_rna + n_dna) * 330 elif not params.molecular_mass: raise Sorry( "Need a sequence file or molecular mass for extract_unique") else: molecular_mass = None # if params.density_select: print_statistics.make_sub_header( "Extracting box around selected density and writing output files", out=log) else: print_statistics.make_sub_header( "Extracting box around selected atoms and writing output files", out=log) # if params.value_outside_atoms == 'mean': print >> log, "\nValue outside atoms mask will be set to mean inside mask" if params.get_half_height_width and params.density_select: print >> log, "\nHalf width at half height will be used to id boundaries" if params.soft_mask and sites_cart_all.size() > 0: print >> log, "\nSoft mask will be applied to model-based mask" if params.keep_map_size: print >> log, "\nEntire map will be kept (not cutting out region)" if params.restrict_map_size: print >> log, "\nOutput map will be within input map" if params.lower_bounds and params.upper_bounds: print >> log, "Bounds for cut out map are (%s,%s,%s) to (%s,%s,%s)" % ( tuple(list(params.lower_bounds) + list(params.upper_bounds))) box = mmtbx.utils.extract_box_around_model_and_map( xray_structure=xray_structure, map_data=map_data.as_double(), box_cushion=params.box_cushion, selection=selection, density_select=params.density_select, threshold=params.density_select_threshold, get_half_height_width=params.get_half_height_width, mask_atoms=params.mask_atoms, soft_mask=params.soft_mask, soft_mask_radius=params.soft_mask_radius, mask_atoms_atom_radius=params.mask_atoms_atom_radius, value_outside_atoms=params.value_outside_atoms, keep_map_size=params.keep_map_size, restrict_map_size=params.restrict_map_size, lower_bounds=params.lower_bounds, upper_bounds=params.upper_bounds, extract_unique=params.extract_unique, chain_type=params.chain_type, sequence=sequence, solvent_content=params.solvent_content, molecular_mass=params.molecular_mass, resolution=params.resolution, ncs_object=ncs_object, symmetry=params.symmetry, ) ph_box = pdb_hierarchy.select(selection) ph_box.adopt_xray_structure(box.xray_structure_box) box.hierarchy = ph_box if (inputs and inputs.crystal_symmetry and inputs.ccp4_map and inputs.crystal_symmetry.unit_cell().parameters() and inputs.ccp4_map.unit_cell_parameters) and ( inputs.crystal_symmetry.unit_cell().parameters() != inputs.ccp4_map.unit_cell_parameters): print >> log, "\nNOTE: Mismatch of unit cell parameters from CCP4 map:" print >>log,"Unit cell from CCP4 map 'unit cell parameters': "+\ "%.1f, %.1f, %.1f, %.1f, %.1f, %.1f)" %tuple( inputs.ccp4_map.unit_cell_parameters) print >>log,"Unit cell from CCP4 map 'map grid': "+\ "%.1f, %.1f, %.1f, %.1f, %.1f, %.1f)" %tuple( inputs.crystal_symmetry.unit_cell().parameters()) print >>log,"\nInterpreting this as the 'unit cell parameters' was "+\ "original map \ndimension and 'map grid' is the "+\ "portion actually in the map that was supplied here.\n" box.unit_cell_parameters_from_ccp4_map = inputs.ccp4_map.unit_cell_parameters box.unit_cell_parameters_deduced_from_map_grid=\ inputs.crystal_symmetry.unit_cell().parameters() else: box.unit_cell_parameters_from_ccp4_map = None box.unit_cell_parameters_deduced_from_map_grid = None # ncs_object is original # box.ncs_object is shifted by shift_cart print >> log, "Box cell dimensions: (%.2f, %.2f, %.2f) A" % ( box.box_crystal_symmetry.unit_cell().parameters()[:3]) if params.keep_origin: print >> log, "Box origin is at grid position of : (%d, %d, %d) " % ( tuple(box.origin_shift_grid_units(reverse=True))) print >> log, "Box origin is at coordinates: (%.2f, %.2f, %.2f) A" % ( tuple(-col(box.shift_cart))) if box.pdb_outside_box_msg: print >> log, box.pdb_outside_box_msg # NOTE: box object is always shifted to place origin at (0,0,0) # For output files ONLY: # keep_origin==False leave origin at (0,0,0) # keep_origin==True: we shift everything back to where it was, if (not params.keep_origin): if box.shift_cart: print >>log,\ "Final coordinate shift for output files: (%.2f,%.2f,%.2f) A" %( tuple(box.shift_cart)) else: print >>log,"\nOutput files are in same location as original: origin "+\ "is at (0,0,0)" else: # keep_origin print >> log, "\nOutput files are in same location as original, just cut out." print >> log, "Note that output maps are only valid in the cut out region.\n" if params.keep_origin: ph_box_original_location = ph_box.deep_copy() sites_cart = box.shift_sites_cart_back( box.xray_structure_box.sites_cart()) xrs_offset = ph_box_original_location.extract_xray_structure( crystal_symmetry=box.xray_structure_box.crystal_symmetry( )).replace_sites_cart(new_sites=sites_cart) ph_box_original_location.adopt_xray_structure(xrs_offset) box.hierarchy_original_location = ph_box_original_location else: box.hierarchy_original_location = None if write_output_files: # Write PDB file if ph_box.overall_counts().n_residues > 0: if (params.output_file_name_prefix is None): file_name = "%s_box.pdb" % output_prefix else: file_name = "%s.pdb" % params.output_file_name_prefix if params.keep_origin: # Keeping origin print >> log, "Writing boxed PDB with box unit cell and in "+\ "original\n position to: %s"%( file_name) ph_box_original_location.write_pdb_file( file_name=file_name, crystal_symmetry=box.xray_structure_box.crystal_symmetry()) else: # write box PDB in box cell print >> log, "Writing shifted boxed PDB to file: %s" % file_name ph_box.write_pdb_file( file_name=file_name, crystal_symmetry=box.xray_structure_box.crystal_symmetry()) # Write NCS file if NCS if ncs_object and ncs_object.max_operators() > 0: if (params.output_file_name_prefix is None): output_symmetry_file = "%s_box.ncs_spec" % output_prefix else: output_symmetry_file = "%s.ncs_spec" % params.output_file_name_prefix if params.keep_origin: if params.symmetry_file: print >> log, "\nDuplicating symmetry in %s and writing to %s" % ( params.symmetry_file, output_symmetry_file) else: print >> log, "\nWriting symmetry to %s" % ( output_symmetry_file) ncs_object.format_all_for_group_specification( file_name=output_symmetry_file) else: print >> log, "\nOffsetting symmetry in %s and writing to %s" % ( params.symmetry_file, output_symmetry_file) box.ncs_object.format_all_for_group_specification( file_name=output_symmetry_file) # Write ccp4 map. Shift back to original location if keep_origin=True if ("ccp4" in params.output_format): if (params.output_file_name_prefix is None): file_name = "%s_box.ccp4" % output_prefix else: file_name = "%s.ccp4" % params.output_file_name_prefix if params.keep_origin: print >> log, "Writing boxed map with box unit_cell and "+\ "original\n position to CCP4 formatted file: %s"%file_name else: print >> log, "Writing box map shifted to (0,0,0) to CCP4 "+\ "formatted file: %s"%file_name box.write_ccp4_map(file_name=file_name, shift_back=params.keep_origin) # Write xplor map. Shift back if keep_origin=True if ("xplor" in params.output_format): if (params.output_file_name_prefix is None): file_name = "%s_box.xplor" % output_prefix else: file_name = "%s.xplor" % params.output_file_name_prefix if params.keep_origin: print >> log, "Writing boxed map with box unit_cell and original "+\ "position\n to X-plor formatted file: %s"%file_name else: print >> log, "Writing box_map shifted to (0,0,0) to X-plor "+\ "formatted file: %s"%file_name box.write_xplor_map(file_name=file_name, shift_back=params.keep_origin) # Write mtz map coeffs. Shift back if keep_origin=True if ("mtz" in params.output_format): if (params.output_file_name_prefix is None): file_name = "%s_box.mtz" % output_prefix else: file_name = "%s.mtz" % params.output_file_name_prefix if params.keep_origin: print >> log, "Writing map coefficients with box_map unit_cell"+\ " but position matching\n "+\ " original position to MTZ file: %s"%file_name else: print >> log, "Writing box_map coefficients shifted to (0,0,0) "+\ "to MTZ file: %s"%file_name if (map_coeff is not None): d_min = map_coeff.d_min() elif params.resolution is not None: d_min = params.resolution else: d_min = maptbx.d_min_from_map( map_data=box.map_box, unit_cell=box.xray_structure_box.unit_cell()) box.map_coefficients(d_min=d_min, resolution_factor=params.resolution_factor, file_name=file_name, shift_back=params.keep_origin) print >> log return box
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 run(args, crystal_symmetry=None, pdb_hierarchy=None, map_data=None, write_output_files=True, log=None): h = "phenix.map_box: extract box with model and map around selected atoms" if (log is None): log = sys.stdout print_statistics.make_header(h, out=log) default_message = """\ %s. Usage: phenix.map_box model.pdb map_coefficients.mtz selection="chain A and resseq 1:10" or phenix.map_box map.ccp4 density_select=True Parameters:""" % h if (len(args) == 0 and not pdb_hierarchy): print default_message master_phil.show(prefix=" ") return inputs = mmtbx.utils.process_command_line_args(args=args, cmd_cs=crystal_symmetry, master_params=master_phil) params = inputs.params.extract() # PDB file if params.pdb_file and not inputs.pdb_file_names and not pdb_hierarchy: inputs.pdb_file_names = [params.pdb_file] if (len(inputs.pdb_file_names) != 1 and not params.density_select and not pdb_hierarchy): raise Sorry("PDB file is needed unless density_select is set.") print_statistics.make_sub_header("pdb model", out=log) if len(inputs.pdb_file_names) > 0: pdb_inp = iotbx.pdb.input(file_name=inputs.pdb_file_names[0]) pdb_hierarchy = pdb_inp.construct_hierarchy() if pdb_hierarchy: pdb_atoms = pdb_hierarchy.atoms() pdb_atoms.reset_i_seq() else: pdb_hierarchy = None # Map or map coefficients map_coeff = None if (not map_data): # read first mtz file if ((len(inputs.reflection_file_names) > 0) or (params.map_coefficients_file is not None)): # file in phil takes precedent if (params.map_coefficients_file is not None): if (len(inputs.reflection_file_names) == 0): inputs.reflection_file_names.append( params.map_coefficients_file) else: inputs.reflection_file_names[ 0] = params.map_coefficients_file map_coeff = reflection_file_utils.extract_miller_array_from_file( file_name=inputs.reflection_file_names[0], label=params.label, type="complex", log=log) fft_map = map_coeff.fft_map( resolution_factor=params.resolution_factor) fft_map.apply_sigma_scaling() map_data = fft_map.real_map_unpadded() map_or_map_coeffs_prefix = os.path.basename( inputs.reflection_file_names[0][:-4]) # or read CCP4 map elif ((inputs.ccp4_map is not None) or (params.ccp4_map_file is not None)): if (params.ccp4_map_file is not None): af = any_file(params.ccp4_map_file) if (af.file_type == 'ccp4_map'): inputs.ccp4_map = af.file_content inputs.ccp4_map_file_name = params.ccp4_map_file print_statistics.make_sub_header("CCP4 map", out=log) ccp4_map = inputs.ccp4_map ccp4_map.show_summary(prefix=" ", out=log) map_data = ccp4_map.data #map_data() if inputs.ccp4_map_file_name.endswith(".ccp4"): map_or_map_coeffs_prefix = os.path.basename( inputs.ccp4_map_file_name[:-5]) else: map_or_map_coeffs_prefix = os.path.basename( inputs.ccp4_map_file_name[:-4]) else: # have map_data map_or_map_coeffs_prefix = None # final check that map_data exists if (map_data is None): raise Sorry("Map or map coefficients file is needed.") if len(inputs.pdb_file_names) > 0: output_prefix = os.path.basename(inputs.pdb_file_names[0])[:-4] else: output_prefix = map_or_map_coeffs_prefix if not pdb_hierarchy: # get an empty hierarchy from cctbx.array_family import flex pdb_hierarchy = iotbx.pdb.input( source_info='', lines=flex.split_lines('')).construct_hierarchy() xray_structure = pdb_hierarchy.extract_xray_structure( crystal_symmetry=inputs.crystal_symmetry) xray_structure.show_summary(f=log) # selection = pdb_hierarchy.atom_selection_cache().selection( string=params.selection) if selection.size(): print_statistics.make_sub_header("atom selection", out=log) print >> log, "Selection string: selection='%s'" % params.selection print >> log, \ " selects %d atoms from total %d atoms."%(selection.count(True), selection.size()) sites_cart_all = xray_structure.sites_cart() sites_cart = sites_cart_all.select(selection) selection = xray_structure.selection_within(radius=params.selection_radius, selection=selection) # if params.density_select: print_statistics.make_sub_header( "Extracting box around selected density and writing output files", out=log) else: print_statistics.make_sub_header( "Extracting box around selected atoms and writing output files", out=log) # if params.value_outside_atoms == 'mean': print >> log, "\nValue outside atoms mask will be set to mean inside mask" if params.get_half_height_width: print >> log, "\nHalf width at half height will be used to id boundaries" box = mmtbx.utils.extract_box_around_model_and_map( xray_structure=xray_structure, map_data=map_data.as_double(), box_cushion=params.box_cushion, selection=selection, density_select=params.density_select, threshold=params.density_select_threshold, get_half_height_width=params.get_half_height_width, mask_atoms=params.mask_atoms, soft_mask=params.soft_mask, soft_mask_radius=params.soft_mask_radius, mask_atoms_atom_radius=params.mask_atoms_atom_radius, value_outside_atoms=params.value_outside_atoms, ) if box.initial_shift_cart: print >> log, "\nInitial coordinate shift will be (%.1f,%.1f,%.1f)\n" % ( box.initial_shift_cart) if box.total_shift_cart: print >> log, "Final coordinate shift: (%.1f,%.1f,%.1f)" % ( box.total_shift_cart) print >> log, "Final cell dimensions: (%.1f,%.1f,%.1f)\n" % ( box.box_crystal_symmetry.unit_cell().parameters()[:3]) if box.pdb_outside_box_msg: print >> log, box.pdb_outside_box_msg ph_box = pdb_hierarchy.select(selection) ph_box.adopt_xray_structure(box.xray_structure_box) box.hierarchy = ph_box if not write_output_files: return box if (params.output_file_name_prefix is None): file_name = "%s_box.pdb" % output_prefix else: file_name = "%s.pdb" % params.output_file_name_prefix ph_box = pdb_hierarchy.select(selection) ph_box.adopt_xray_structure(box.xray_structure_box) ph_box.write_pdb_file( file_name=file_name, crystal_symmetry=box.xray_structure_box.crystal_symmetry()) if ("ccp4" in params.output_format): if (params.output_file_name_prefix is None): file_name = "%s_box.ccp4" % output_prefix else: file_name = "%s.ccp4" % params.output_file_name_prefix print >> log, "writing map to CCP4 formatted file: %s" % file_name box.write_ccp4_map(file_name=file_name) if ("xplor" in params.output_format): if (params.output_file_name_prefix is None): file_name = "%s_box.xplor" % output_prefix else: file_name = "%s.xplor" % params.output_file_name_prefix print >> log, "writing map to X-plor formatted file: %s" % file_name box.write_xplor_map(file_name=file_name) if ("mtz" in params.output_format): if (params.output_file_name_prefix is None): file_name = "%s_box.mtz" % output_prefix else: file_name = "%s.mtz" % params.output_file_name_prefix print >> log, "writing map coefficients to MTZ file: %s" % file_name if (map_coeff is not None): d_min = map_coeff.d_min() else: d_min = maptbx.d_min_from_map( map_data=box.map_box, unit_cell=box.xray_structure_box.unit_cell()) box.map_coefficients(d_min=d_min, resolution_factor=params.resolution_factor, file_name=file_name) if params.ncs_file: if (params.output_file_name_prefix is None): output_ncs_file = "%s_box.ncs_spec" % output_prefix else: output_ncs_file = "%s.ncs_spec" % params.output_file_name_prefix print >> log, "\nOffsetting NCS in %s and writing to %s" % ( params.ncs_file, output_ncs_file) from mmtbx.ncs.ncs import ncs ncs_object = ncs() ncs_object.read_ncs(params.ncs_file, log=log) ncs_object.display_all(log=log) if not ncs_object or ncs_object.max_operators() < 1: print >> log, "Skipping...no NCS available" elif box.total_shift_cart: from scitbx.math import matrix print >>log,"Shifting NCS operators "+\ "based on coordinate shift of (%7.1f,%7.1f,%7.1f)" %( tuple(box.total_shift_cart)) ncs_object = ncs_object.coordinate_offset( coordinate_offset=matrix.col(box.total_shift_cart)) ncs_object.display_all(log=log) ncs_object.format_all_for_group_specification( file_name=output_ncs_file) box.ncs_object = ncs_object else: box.ncs_object = None print >> log return box
def run(args, crystal_symmetry=None, log=None): h = "phenix.map_box: extract box with model and map around selected atoms" if(log is None): log = sys.stdout print_statistics.make_header(h, out=log) default_message="""\ %s. Usage: phenix.map_box model.pdb map_coefficients.mtz selection="chain A and resseq 1:10" or phenix.map_box map.ccp4 density_select=True Parameters:"""%h if(len(args) == 0): print default_message master_phil.show(prefix=" ") return inputs = mmtbx.utils.process_command_line_args(args = args, cmd_cs=crystal_symmetry, master_params = master_phil) params = inputs.params.extract() # PDB file if params.pdb_file and not inputs.pdb_file_names: inputs.pdb_file_names=[params.pdb_file] if(len(inputs.pdb_file_names)!=1 and not params.density_select): raise Sorry("PDB file is needed unless density_select is set.") print_statistics.make_sub_header("pdb model", out=log) if len(inputs.pdb_file_names)>0: pdb_inp = iotbx.pdb.input(file_name=inputs.pdb_file_names[0]) pdb_hierarchy = pdb_inp.construct_hierarchy() pdb_atoms = pdb_hierarchy.atoms() pdb_atoms.reset_i_seq() else: pdb_hierarchy=None # Map or map coefficients map_coeff = None if(inputs.ccp4_map is None): if(len(inputs.reflection_file_names)!=1): raise Sorry("Map or map coefficients file is needed.") map_coeff = reflection_file_utils.extract_miller_array_from_file( file_name = inputs.reflection_file_names[0], label = params.label, type = "complex", log = log) fft_map = map_coeff.fft_map(resolution_factor=params.resolution_factor) fft_map.apply_sigma_scaling() map_data = fft_map.real_map_unpadded() map_or_map_coeffs_prefix=os.path.basename( inputs.reflection_file_names[0][:-4]) else: print_statistics.make_sub_header("CCP4 map", out=log) ccp4_map = inputs.ccp4_map ccp4_map.show_summary(prefix=" ") map_data = ccp4_map.map_data() if inputs.ccp4_map_file_name.endswith(".ccp4"): map_or_map_coeffs_prefix=os.path.basename( inputs.ccp4_map_file_name[:-5]) else: map_or_map_coeffs_prefix=os.path.basename( inputs.ccp4_map_file_name[:-4]) # if len(inputs.pdb_file_names)>0: output_prefix=os.path.basename(inputs.pdb_file_names[0])[:-4] else: output_prefix=map_or_map_coeffs_prefix if not pdb_hierarchy: # get an empty hierarchy from cctbx.array_family import flex pdb_hierarchy=iotbx.pdb.input( source_info='',lines=flex.split_lines('')).construct_hierarchy() xray_structure = pdb_hierarchy.extract_xray_structure( crystal_symmetry=inputs.crystal_symmetry) xray_structure.show_summary(f=log) # selection = pdb_hierarchy.atom_selection_cache().selection( string = params.selection) if selection.size(): print_statistics.make_sub_header("atom selection", out=log) print >> log, "Selection string: selection='%s'"%params.selection print >> log, \ " selects %d atoms from total %d atoms."%(selection.count(True), selection.size()) sites_cart_all = xray_structure.sites_cart() sites_cart = sites_cart_all.select(selection) selection = xray_structure.selection_within( radius = params.selection_radius, selection = selection) # if params.density_select: print_statistics.make_sub_header( "Extracting box around selected density and writing output files", out=log) else: print_statistics.make_sub_header( "Extracting box around selected atoms and writing output files", out=log) # box = mmtbx.utils.extract_box_around_model_and_map( xray_structure = xray_structure, map_data = map_data.as_double(), box_cushion = params.box_cushion, selection = selection, density_select = params.density_select, threshold = params.density_select_threshold) if box.initial_shift_cart: print >>log,"\nInitial coordinate shift will be (%.1f,%.1f,%.1f)\n" %( box.initial_shift_cart) if box.total_shift_cart: print >>log,"Final coordinate shift: (%.1f,%.1f,%.1f)" %( box.total_shift_cart) print >>log,"Final cell dimensions: (%.1f,%.1f,%.1f)\n" %( box.box_crystal_symmetry.unit_cell().parameters()[:3]) if box.pdb_outside_box_msg: print >> log, box.pdb_outside_box_msg if(params.output_file_name_prefix is None): file_name = "%s_box.pdb"%output_prefix else: file_name = "%s.pdb"%params.output_file_name_prefix ph_box = pdb_hierarchy.select(selection) ph_box.adopt_xray_structure(box.xray_structure_box) ph_box.write_pdb_file(file_name=file_name, crystal_symmetry = box.xray_structure_box.crystal_symmetry()) if("ccp4" in params.output_format): if(params.output_file_name_prefix is None): file_name = "%s_box.ccp4"%output_prefix else: file_name = "%s.ccp4"%params.output_file_name_prefix print >> log, "writing map to CCP4 formatted file: %s"%file_name box.write_ccp4_map(file_name=file_name) if("xplor" in params.output_format): if(params.output_file_name_prefix is None): file_name = "%s_box.xplor"%output_prefix else: file_name = "%s.xplor"%params.output_file_name_prefix print >> log, "writing map to X-plor formatted file: %s"%file_name box.write_xplor_map(file_name=file_name) if("mtz" in params.output_format): if(params.output_file_name_prefix is None): file_name = "%s_box.mtz"%output_prefix else: file_name = "%s.mtz"%params.output_file_name_prefix print >> log, "writing map coefficients to MTZ file: %s"%file_name if(map_coeff is not None): d_min = map_coeff.d_min() else: d_min = maptbx.d_min_from_map(map_data=box.map_box, unit_cell=box.xray_structure_box.unit_cell()) box.map_coefficients(d_min=d_min, resolution_factor=params.resolution_factor, file_name=file_name) if params.ncs_file: if(params.output_file_name_prefix is None): output_ncs_file = "%s_box.ncs_spec"%output_prefix else: output_ncs_file = "%s.ncs_spec"%params.output_file_name_prefix print >>log,"\nOffsetting NCS in %s and writing to %s" %( params.ncs_file,output_ncs_file) from mmtbx.ncs.ncs import ncs ncs_object=ncs() ncs_object.read_ncs(params.ncs_file,log=log) ncs_object.display_all(log=log) if not ncs_object or ncs_object.max_operators()<1: print >>log,"Skipping...no NCS available" elif box.total_shift_cart: from scitbx.math import matrix print >>log,"Shifting NCS operators "+\ "based on coordinate shift of (%7.1f,%7.1f,%7.1f)" %( tuple(box.total_shift_cart)) ncs_object=ncs_object.coordinate_offset( coordinate_offset=matrix.col(box.total_shift_cart)) ncs_object.display_all(log=log) ncs_object.format_all_for_group_specification( file_name=output_ncs_file) box.ncs_object=ncs_object else: box.ncs_object=None print >> log return box
def run(args, crystal_symmetry=None, ncs_object=None, pdb_hierarchy=None, map_data=None, mask_data=None, half_map_data_list=None, half_map_labels_list=None, lower_bounds=None, upper_bounds=None, write_output_files=True, log=None): h = "phenix.map_box: extract box with model and map around selected atoms" if(log is None): log = sys.stdout print_statistics.make_header(h, out=log) default_message="""\ %s. Usage: phenix.map_box model.pdb map_coefficients.mtz selection="chain A and resseq 1:10" or phenix.map_box map.ccp4 density_select=True Parameters:"""%h if(len(args) == 0 and not pdb_hierarchy): print(default_message) master_phil.show(prefix=" ") return # Process inputs ignoring symmetry conflicts just to get the value of # ignore_symmetry_conflicts... inputs = mmtbx.utils.process_command_line_args(args = args, cmd_cs=crystal_symmetry, master_params = master_phil, suppress_symmetry_related_errors=True) params = inputs.params.extract() # Now process inputs for real and write a nice error message if necessary. try: inputs = mmtbx.utils.process_command_line_args(args = args, cmd_cs=crystal_symmetry, master_params = master_phil, suppress_symmetry_related_errors=params.ignore_symmetry_conflicts) except Exception as e: if str(e).find("symmetry mismatch ")>1: raise Sorry(str(e)+"\nTry 'ignore_symmetry_conflicts=True'") else: raise e params = inputs.params.extract() master_phil.format(python_object=params).show(out=log) # Overwrite params with parameters in call if available if lower_bounds: params.lower_bounds=lower_bounds if upper_bounds: params.upper_bounds=upper_bounds # PDB file if params.pdb_file and not inputs.pdb_file_names and not pdb_hierarchy: inputs.pdb_file_names=[params.pdb_file] if(len(inputs.pdb_file_names)!=1 and not params.density_select and not params.mask_select and not pdb_hierarchy and not params.keep_map_size and not params.upper_bounds and not params.extract_unique and not params.bounds_match_this_file): raise Sorry("PDB file is needed unless extract_unique, "+ "density_select, mask_select, keep_map_size \nor bounds are set .") if (len(inputs.pdb_file_names)!=1 and not pdb_hierarchy and \ (params.mask_atoms )): raise Sorry("PDB file is needed for mask_atoms") if params.soft_mask and (not params.resolution) and \ (len(inputs.pdb_file_names)!=1 and not pdb_hierarchy): raise Sorry("Need resolution for soft_mask without PDB file") if ((params.density_select or params.mask_select) and params.keep_map_size): raise Sorry("Cannot set both density_select/mask_select and keep_map_size") if ((params.density_select or params.mask_select) and params.upper_bounds): raise Sorry("Cannot set both density_select/mask_select and bounds") if (params.keep_map_size and params.upper_bounds): raise Sorry("Cannot set both keep_map_size and bounds") if (params.upper_bounds and not params.lower_bounds): raise Sorry("Please set lower_bounds if you set upper_bounds") if (params.extract_unique): if (not params.resolution): raise Sorry("Please set resolution for extract_unique") if (not params.symmetry) and (not params.symmetry_file) and \ (not ncs_object): raise Sorry( "Please supply a symmetry file or symmetry for extract_unique (you "+ "\ncan try symmetry=ALL if you do not know your symmetry or "+ "symmetry=C1 if \nthere is none)") from mmtbx.ncs.ncs import ncs ncs_object=ncs() ncs_object.set_unit_ncs() if params.keep_input_unit_cell_and_grid and ( (params.output_unit_cell_grid is not None ) or (params.output_unit_cell is not None ) ): raise Sorry("If you set keep_input_unit_cell_and_grid then you cannot "+\ "set \noutput_unit_cell_grid or output_unit_cell") if (write_output_files) and ("mtz" in params.output_format) and ( (params.keep_origin) and (not params.keep_map_size)): print("\nNOTE: Skipping write of mtz file as keep_origin=True and \n"+\ "keep_map_size is False\n") params.output_format=remove_element(params.output_format,element='mtz') if (write_output_files) and ("mtz" in params.output_format) and ( (params.extract_unique)): print("\nNOTE: Skipping write of mtz file as extract_unique=True\n") params.output_format=remove_element(params.output_format,element='mtz') if params.output_origin_match_this_file or params.bounds_match_this_file: if params.output_origin_match_this_file: fn=params.output_origin_match_this_file if params.bounds_match_this_file: raise Sorry("Cannot match origin and bounds at same time") else: fn=params.bounds_match_this_file if not params.ccp4_map_file: raise Sorry( "Need to specify your input file with ccp4_map_file=xxx if you use "+ "output_origin_match_this_file=xxxx or bounds_match_this_file=xxxx") af = any_file(fn) if (af.file_type == 'ccp4_map'): origin=af.file_content.data.origin() if params.output_origin_match_this_file: params.output_origin_grid_units=origin print("Origin of (%s,%s,%s) taken from %s" %( origin[0],origin[1],origin[2],fn)) else: all=af.file_content.data.all() params.lower_bounds=origin print("Lower bounds of (%s,%s,%s) taken from %s" %( params.lower_bounds[0],params.lower_bounds[1], params.lower_bounds[2],fn)) params.upper_bounds=list(col(origin)+col(all)-col((1,1,1))) print("upper bounds of (%s,%s,%s) taken from %s" %( params.upper_bounds[0],params.upper_bounds[1], params.upper_bounds[2],fn)) params.bounds_are_absolute=True else: raise Sorry("Unable to interpret %s as map file" %(fn)) if params.output_origin_grid_units is not None and params.keep_origin: params.keep_origin=False print("Setting keep_origin=False as output_origin_grid_units is set") print_statistics.make_sub_header("pdb model", out=log) if len(inputs.pdb_file_names)>0: pdb_inp = iotbx.pdb.input(file_name=inputs.pdb_file_names[0]) pdb_hierarchy = pdb_inp.construct_hierarchy() if pdb_hierarchy: pdb_atoms = pdb_hierarchy.atoms() pdb_atoms.reset_i_seq() else: pdb_hierarchy=None # Map or map coefficients map_coeff = None input_unit_cell_grid=None input_unit_cell=None input_map_labels=None if (not map_data): # read first mtz file if ( (len(inputs.reflection_file_names) > 0) or (params.map_coefficients_file is not None) ): # file in phil takes precedent if (params.map_coefficients_file is not None): if (len(inputs.reflection_file_names) == 0): inputs.reflection_file_names.append(params.map_coefficients_file) else: inputs.reflection_file_names[0] = params.map_coefficients_file map_coeff = reflection_file_utils.extract_miller_array_from_file( file_name = inputs.reflection_file_names[0], label = params.label, type = "complex", log = log) if not crystal_symmetry: crystal_symmetry=map_coeff.crystal_symmetry() fft_map = map_coeff.fft_map(resolution_factor=params.resolution_factor) fft_map.apply_sigma_scaling() map_data = fft_map.real_map_unpadded() map_or_map_coeffs_prefix=os.path.basename( inputs.reflection_file_names[0][:-4]) # or read CCP4 map elif ( (inputs.ccp4_map is not None) or (params.ccp4_map_file is not None) ): if (params.ccp4_map_file is not None): af = any_file(params.ccp4_map_file) if (af.file_type == 'ccp4_map'): inputs.ccp4_map = af.file_content inputs.ccp4_map_file_name = params.ccp4_map_file print_statistics.make_sub_header("CCP4 map", out=log) ccp4_map = inputs.ccp4_map ccp4_map.show_summary(prefix=" ",out=log) if not crystal_symmetry: crystal_symmetry=ccp4_map.crystal_symmetry() map_data = ccp4_map.data #map_data() input_unit_cell_grid=ccp4_map.unit_cell_grid input_unit_cell=ccp4_map.unit_cell_parameters input_map_labels=ccp4_map.get_labels() if inputs.ccp4_map_file_name.endswith(".ccp4"): map_or_map_coeffs_prefix=os.path.basename( inputs.ccp4_map_file_name[:-5]) else: map_or_map_coeffs_prefix=os.path.basename( inputs.ccp4_map_file_name[:-4]) else: # have map_data map_or_map_coeffs_prefix=None if params.half_map_list and (not half_map_data_list): if not params.extract_unique: raise Sorry("Can only use half_map_with extract_unique") print ("Reading half-maps",params.half_map_list) half_map_data_list=[] half_map_labels_list=[] for fn in params.half_map_list: print("Reading half map from %s" %(fn),file=log) af = any_file(fn) print_statistics.make_sub_header("CCP4 map", out=log) h_ccp4_map = af.file_content h_ccp4_map.show_summary(prefix=" ",out=log) h_map_data = h_ccp4_map.data half_map_data_list.append(h_map_data) half_map_labels_list.append(h_ccp4_map.get_labels()) if params.map_scale_factor: print("Applying scale factor of %s to map data on read-in" %( params.map_scale_factor)) map_data=map_data*params.map_scale_factor if params.output_origin_grid_units is not None: origin_to_match=tuple(params.output_origin_grid_units) else: origin_to_match=None if origin_to_match: sc=[] for x,o,a in zip(crystal_symmetry.unit_cell().parameters()[:3], origin_to_match, map_data.all()): sc.append(-x*o/a) shift_cart_for_origin_to_match=tuple(sc) else: origin_to_match=None shift_cart_for_origin_to_match=None if crystal_symmetry and not inputs.crystal_symmetry: inputs.crystal_symmetry=crystal_symmetry # final check that map_data exists if(map_data is None): raise Sorry("Map or map coefficients file is needed.") if len(inputs.pdb_file_names)>0: output_prefix=os.path.basename(inputs.pdb_file_names[0])[:-4] else: output_prefix=map_or_map_coeffs_prefix if not pdb_hierarchy: # get an empty hierarchy from cctbx.array_family import flex pdb_hierarchy=iotbx.pdb.input( source_info='',lines=flex.split_lines('')).construct_hierarchy() xray_structure = pdb_hierarchy.extract_xray_structure( crystal_symmetry=inputs.crystal_symmetry) xray_structure.show_summary(f=log) # if not params.selection: params.selection="all" selection = pdb_hierarchy.atom_selection_cache().selection( string = params.selection) if selection.size(): print_statistics.make_sub_header("atom selection", out=log) print("Selection string: selection='%s'"%params.selection, file=log) print(" selects %d atoms from total %d atoms."%(selection.count(True), selection.size()), file=log) sites_cart_all = xray_structure.sites_cart() sites_cart = sites_cart_all.select(selection) selection = xray_structure.selection_within( radius = params.selection_radius, selection = selection) if not ncs_object: from mmtbx.ncs.ncs import ncs ncs_object=ncs() if params.symmetry_file: ncs_object.read_ncs(params.symmetry_file,log=log) print("Total of %s operators read" %(ncs_object.max_operators()), file=log) if not ncs_object or ncs_object.max_operators()<1: print("No symmetry available", file=log) if ncs_object: n_ops=max(1,ncs_object.max_operators()) else: n_ops=1 # Get sequence if extract_unique is set sequence=None if params.extract_unique or params.mask_select: if params.sequence_file: if n_ops > 1: # get unique part of sequence remove_duplicates=True else: remove_duplicates=False from iotbx.bioinformatics import get_sequences sequence=(" ".join(get_sequences(file_name=params.sequence_file, remove_duplicates=remove_duplicates))) if params.chain_type in ['None',None]: params.chain_type=None if sequence and not params.molecular_mass: # get molecular mass from sequence from iotbx.bioinformatics import text_from_chains_matching_chain_type if params.chain_type in [None,'PROTEIN']: n_protein=len(text_from_chains_matching_chain_type( text=sequence,chain_type='PROTEIN')) else: n_protein=0 if params.chain_type in [None,'RNA']: n_rna=len(text_from_chains_matching_chain_type( text=sequence,chain_type='RNA')) else: n_rna=0 if params.chain_type in [None,'DNA']: n_dna=len(text_from_chains_matching_chain_type( text=sequence,chain_type='DNA')) else: n_dna=0 params.molecular_mass=n_ops*(n_protein*110+(n_rna+n_dna)*330) print("\nEstimate of molecular mass is %.0f " %(params.molecular_mass), file=log) if params.density_select or params.mask_select: print_statistics.make_sub_header( "Extracting box around selected density and writing output files", out=log) else: print_statistics.make_sub_header( "Extracting box around selected atoms and writing output files", out=log) # if params.value_outside_atoms=='mean': print("\nValue outside atoms mask will be set to mean inside mask", file=log) if params.get_half_height_width and params.density_select: print("\nHalf width at half height will be used to id boundaries", file=log) if params.soft_mask and sites_cart_all.size()>0: print("\nSoft mask will be applied to model-based mask", file=log) elif params.soft_mask: print ("\nSoft mask will be applied to outside of map box",file=log) if params.keep_map_size: print("\nEntire map will be kept (not cutting out region)", file=log) if params.restrict_map_size: print("\nOutput map will be within input map", file=log) if params.lower_bounds and params.upper_bounds: print("Bounds for cut out map are (%s,%s,%s) to (%s,%s,%s)" %( tuple(list(params.lower_bounds)+list(params.upper_bounds))), file=log) if mask_data: mask_data=mask_data.as_double() box = mmtbx.utils.extract_box_around_model_and_map( xray_structure = xray_structure, map_data = map_data.as_double(), mask_data = mask_data, box_cushion = params.box_cushion, selection = selection, mask_select = params.mask_select, density_select = params.density_select, threshold = params.density_select_threshold, get_half_height_width = params.get_half_height_width, mask_atoms = params.mask_atoms, soft_mask = params.soft_mask, soft_mask_radius = params.soft_mask_radius, mask_atoms_atom_radius = params.mask_atoms_atom_radius, value_outside_atoms = params.value_outside_atoms, keep_map_size = params.keep_map_size, restrict_map_size = params.restrict_map_size, lower_bounds = params.lower_bounds, upper_bounds = params.upper_bounds, bounds_are_absolute = params.bounds_are_absolute, zero_outside_original_map = params.zero_outside_original_map, extract_unique = params.extract_unique, target_ncs_au_file = params.target_ncs_au_file, regions_to_keep = params.regions_to_keep, box_buffer = params.box_buffer, soft_mask_extract_unique = params.soft_mask_extract_unique, mask_expand_ratio = params.mask_expand_ratio, keep_low_density = params.keep_low_density, chain_type = params.chain_type, sequence = sequence, solvent_content = params.solvent_content, molecular_mass = params.molecular_mass, resolution = params.resolution, ncs_object = ncs_object, symmetry = params.symmetry, half_map_data_list = half_map_data_list, ) ph_box = pdb_hierarchy.select(selection) ph_box.adopt_xray_structure(box.xray_structure_box) box.hierarchy=ph_box if params.mask_select: print("\nSolvent content used in mask_select: %.3f " %( box.get_solvent_content()),file=log) if (inputs and inputs.crystal_symmetry and inputs.ccp4_map and inputs.crystal_symmetry.unit_cell().parameters() and inputs.ccp4_map.unit_cell_parameters ) and ( inputs.crystal_symmetry.unit_cell().parameters() != inputs.ccp4_map.unit_cell_parameters): print("\nNOTE: Input CCP4 map is only part of unit cell:", file=log) print("Full unit cell ('unit cell parameters'): "+\ "(%.1f, %.1f, %.1f, %.1f, %.1f, %.1f) A" %tuple( inputs.ccp4_map.unit_cell_parameters), file=log) print("Size of CCP4 map 'map unit cell': "+\ "(%.1f, %.1f, %.1f, %.1f, %.1f, %.1f) A" %tuple( inputs.crystal_symmetry.unit_cell().parameters()), file=log) print("Full unit cell as grid units: (%s, %s, %s)" %( inputs.ccp4_map.unit_cell_grid), file=log) print("Map unit cell as grid units: (%s, %s, %s)" %( map_data.all()), file=log) box.unit_cell_parameters_from_ccp4_map=inputs.ccp4_map.unit_cell_parameters box.unit_cell_parameters_deduced_from_map_grid=\ inputs.crystal_symmetry.unit_cell().parameters() else: box.unit_cell_parameters_from_ccp4_map=None box.unit_cell_parameters_deduced_from_map_grid=None if box.pdb_outside_box_msg: print(box.pdb_outside_box_msg, file=log) # NOTE: box object is always shifted to place origin at (0,0,0) # NOTE ON ORIGIN SHIFTS: The shifts are applied locally here. The box # object is not affected and always has the origin at (0,0,0) # output_box is copy of box with shift_cart corresponding to the output # files. Normally this is the same as the original shift_cart. However # if user has specified a new output origin it will differ. # For output files ONLY: # keep_origin==False leave origin at (0,0,0) # keep_origin==True: we shift everything back to where it was, # output_origin_grid_units=10,10,10: output origin is at (10,10,10) # ncs_object is original # box.ncs_object is shifted by shift_cart # output_box.ncs_object is shifted back by -new shift_cart # Additional note on output unit_cell and grid_units. # The ccp4-style output map can specify the unit cell and grid units # corresponding to that cell. This can be separate from the origin and # number of grid points in the map as written. If specified, write these # out to the output ccp4 map and also use this unit cell for writing # any output PDB files from copy import deepcopy output_box=deepcopy(box) # won't use box below here except to return it print("\nBox cell dimensions: (%.2f, %.2f, %.2f) A" %( box.box_crystal_symmetry.unit_cell().parameters()[:3]), file=log) if box.shift_cart: print("Working origin moved from grid position of"+\ ": (%d, %d, %d) to (0,0,0) " %( tuple(box.origin_shift_grid_units(reverse=True))), file=log) print("Working origin moved from coordinates of:"+\ " (%.2f, %.2f, %.2f) A to (0,0,0)\n" %( tuple(-col(box.shift_cart))), file=log) if (params.keep_origin): print("\nRestoring original position for output files", file=log) print("Origin will be at grid position of"+\ ": (%d, %d, %d) " %( tuple(box.origin_shift_grid_units(reverse=True))), file=log) print("\nOutput files will be in same location as original", end=' ', file=log) if not params.keep_map_size: print("just cut out.", file=log) else: print("keeping entire map", file=log) print("Note that output maps are only valid in the cut out region.\n", file=log) else: if origin_to_match: output_box.shift_cart=shift_cart_for_origin_to_match if params.output_origin_grid_units: print("Output map origin to be shifted to match target", file=log) print("Placing origin at grid point (%s, %s, %s)" %( origin_to_match)+"\n"+ \ "Final coordinate shift for output files: (%.2f,%.2f,%.2f) A\n" %( tuple(col(output_box.shift_cart)-col(box.shift_cart))), file=log) elif box.shift_cart: output_box.shift_cart=(0,0,0) # not shifting back print("Final origin will be at (0,0,0)", file=log) print("Final coordinate shift for output files: (%.2f,%.2f,%.2f) A\n" %( tuple(col(output_box.shift_cart)-col(box.shift_cart))), file=log) else: print("\nOutput files are in same location as original and origin "+\ "is at (0,0,0)\n", file=log) print("\nBox grid: (%s, %s, %s) " %(output_box.map_box.all()),file=log) ph_output_box_output_location = ph_box.deep_copy() if output_box.shift_cart: # shift coordinates and NCS back by shift_cart # NOTE output_box.shift_cart could be different than box.shift_cart if # there is a target position for the origin and it is not the same as the # original origin. sites_cart = output_box.shift_sites_cart_back( output_box.xray_structure_box.sites_cart()) xrs_offset = ph_output_box_output_location.extract_xray_structure( crystal_symmetry=output_box.xray_structure_box.crystal_symmetry() ).replace_sites_cart(new_sites = sites_cart) ph_output_box_output_location.adopt_xray_structure(xrs_offset) if output_box.ncs_object: output_box.ncs_object=output_box.ncs_object.coordinate_offset( tuple(-col(output_box.shift_cart))) shift_back=True else: shift_back=False if params.keep_input_unit_cell_and_grid and \ (input_unit_cell_grid is not None) and \ (input_unit_cell is not None): params.output_unit_cell=input_unit_cell params.output_unit_cell_grid=input_unit_cell_grid print("Setting output unit cell parameters and unit cell grid to"+\ " match\ninput map file", file=log) if params.output_unit_cell: # Set output unit cell parameters from cctbx import crystal output_crystal_symmetry=crystal.symmetry( unit_cell=params.output_unit_cell, space_group="P1") output_unit_cell=output_crystal_symmetry.unit_cell() print("Output unit cell set to: %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)" %tuple( output_crystal_symmetry.unit_cell().parameters()), file=log) else: output_crystal_symmetry=None # ============= Check/set output unit cell grid and cell parameters ======= if params.output_unit_cell_grid or output_crystal_symmetry: if params.output_unit_cell_grid: output_unit_cell_grid=params.output_unit_cell_grid else: output_unit_cell_grid=output_box.map_box.all() print("Output unit cell grid set to: (%s, %s, %s)" %tuple( output_unit_cell_grid), file=log) expected_output_abc=[] box_spacing=[] output_spacing=[] box_abc=output_box.xray_structure_box.\ crystal_symmetry().unit_cell().parameters()[:3] if output_crystal_symmetry: output_abc=output_crystal_symmetry.unit_cell().parameters()[:3] else: output_abc=[None,None,None] for a_box,a_output,n_box,n_output in zip( box_abc, output_abc, output_box.map_box.all(), output_unit_cell_grid): expected_output_abc.append(a_box*n_output/n_box) box_spacing.append(a_box/n_box) if output_crystal_symmetry: output_spacing.append(a_output/n_output) else: output_spacing.append(a_box/n_box) if output_crystal_symmetry: # make sure it is compatible... r0=expected_output_abc[0]/output_abc[0] r1=expected_output_abc[1]/output_abc[1] r2=expected_output_abc[2]/output_abc[2] from libtbx.test_utils import approx_equal if not approx_equal(r0,r1,eps=0.001) or not approx_equal(r0,r2,eps=0.001): print("WARNING: output_unit_cell and cell_grid will "+\ "change ratio of grid spacing.\nOld spacings: "+\ "(%.2f, %.2f, %.2f) A " %(tuple(box_spacing))+\ "\nNew spacings: (%.2f, %.2f, %.2f) A \n" %(tuple(output_spacing)), file=log) else: output_abc=expected_output_abc from cctbx import crystal output_crystal_symmetry=crystal.symmetry( unit_cell=list(output_abc)+[90,90,90], space_group="P1") print("Output unit cell will be: (%.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n"%( tuple(output_crystal_symmetry.unit_cell().parameters())), file=log) else: output_unit_cell_grid = output_box.map_box.all() output_crystal_symmetry=output_box.xray_structure_box.crystal_symmetry() # ========== Done check/set output unit cell grid and cell parameters ===== if write_output_files: # Write PDB file if ph_box.overall_counts().n_residues>0: if(params.output_file_name_prefix is None): file_name = "%s_box.pdb"%output_prefix else: file_name = "%s.pdb"%params.output_file_name_prefix ph_output_box_output_location.write_pdb_file(file_name=file_name, crystal_symmetry = output_crystal_symmetry) print("Writing boxed PDB with box unit cell to %s" %( file_name), file=log) # Write NCS file if NCS if output_box.ncs_object and output_box.ncs_object.max_operators()>0: if(params.output_file_name_prefix is None): output_symmetry_file = "%s_box.ncs_spec"%output_prefix else: output_symmetry_file = "%s.ncs_spec"%params.output_file_name_prefix output_box.ncs_object.format_all_for_group_specification( file_name=output_symmetry_file) print("\nWriting symmetry to %s" %( output_symmetry_file), file=log) # Write ccp4 map. if("ccp4" in params.output_format): if(params.output_file_name_prefix is None): file_name = "%s_box.ccp4"%output_prefix else: file_name = "%s.ccp4"%params.output_file_name_prefix from iotbx.mrcfile import create_output_labels if params.extract_unique: program_name='map_box using extract_unique' limitations=["extract_unique"] else: program_name='map_box' limitations=[] labels=create_output_labels(program_name=program_name, input_file_name=inputs.ccp4_map_file_name, input_labels=input_map_labels, limitations=limitations, output_labels=params.output_map_labels) output_box.write_ccp4_map(file_name=file_name, output_crystal_symmetry=output_crystal_symmetry, output_mean=params.output_ccp4_map_mean, output_sd=params.output_ccp4_map_sd, output_unit_cell_grid=output_unit_cell_grid, shift_back=shift_back, output_map_labels=labels, output_external_origin=params.output_external_origin) print("Writing boxed map "+\ "to CCP4 formatted file: %s"%file_name, file=log) if not params.half_map_list: params.half_map_list=[] if not output_box.map_box_half_map_list: output_box.map_box_half_map_list=[] if not half_map_labels_list: half_map_labels_list=len(output_box.map_box_half_map_list)*[None] for hm,labels,fn in zip( output_box.map_box_half_map_list, half_map_labels_list, params.half_map_list): # half maps matching labels=create_output_labels(program_name=program_name, input_file_name=fn, input_labels=labels, limitations=limitations, output_labels=params.output_map_labels) hm_fn="%s_box.ccp4" %( ".".join(os.path.basename(fn).split(".")[:-1])) output_box.write_ccp4_map(file_name=hm_fn, map_data=hm, output_crystal_symmetry=output_crystal_symmetry, output_mean=params.output_ccp4_map_mean, output_sd=params.output_ccp4_map_sd, output_unit_cell_grid=output_unit_cell_grid, shift_back=shift_back, output_map_labels=labels, output_external_origin=params.output_external_origin) print ("Writing boxed half map to: %s " %(hm_fn),file=log) # Write xplor map. Shift back if keep_origin=True if("xplor" in params.output_format): if(params.output_file_name_prefix is None): file_name = "%s_box.xplor"%output_prefix else: file_name = "%s.xplor"%params.output_file_name_prefix output_box.write_xplor_map(file_name=file_name, output_crystal_symmetry=output_crystal_symmetry, output_unit_cell_grid=output_unit_cell_grid, shift_back=shift_back,) print("Writing boxed map "+\ "to X-plor formatted file: %s"%file_name, file=log) # Write mtz map coeffs. Shift back if keep_origin=True if("mtz" in params.output_format): if(params.output_file_name_prefix is None): file_name = "%s_box.mtz"%output_prefix else: file_name = "%s.mtz"%params.output_file_name_prefix print("Writing map coefficients "+\ "to MTZ file: %s"%file_name, file=log) if(map_coeff is not None): d_min = map_coeff.d_min() elif params.resolution is not None: d_min = params.resolution else: d_min = maptbx.d_min_from_map(map_data=output_box.map_box, unit_cell=output_box.xray_structure_box.unit_cell()) output_box.map_coefficients(d_min=d_min, scale_max=params.scale_max, resolution_factor=params.resolution_factor, file_name=file_name, shift_back=shift_back) print(file=log) return box
def find_tls (params, pdb_inp, pdb_hierarchy, xray_structure, return_as_list=False, ignore_pdb_header_groups=False, out=None) : if (out is None) : out = sys.stdout print_statistics.make_header("Analyzing inputs", out=out) if (params.random_seed is None) : params.random_seed = flex.get_random_seed() random.seed(params.random_seed) flex.set_random_seed(params.random_seed) xray_structure.convert_to_isotropic() sites_cart = xray_structure.sites_cart() u_cart = None u_iso = xray_structure.extract_u_iso_or_u_equiv()#*adptbx.u_as_b(1.) # ? bad_i_seqs = check_adp(u_iso=u_iso, out=out) if (bad_i_seqs is not None) : atoms = pdb_hierarchy.atoms() bad_atom_strings = [] for i_seq in bad_i_seqs[:10] : atom_str = atoms[i_seq].format_atom_record() bad_atom_strings.append(atom_str) if (len(bad_i_seqs) > 10) : bad_atom_strings.append("... (remaining %d not shown)" % (len(bad_i_seqs)-10)) raise Sorry(("%d atoms in the model contain isotropic B-factors <= 0:\n"+ "\n".join(bad_atom_strings)) % (len(bad_i_seqs))) # ssm = mmtbx.secondary_structure.manager( pdb_hierarchy = pdb_hierarchy, sec_str_from_pdb_file = None, params = None) alpha_h_selection = ssm.alpha_selection() secondary_structure_selection = ssm.alpha_selection() | \ ssm.beta_selection() | ssm.base_pair_selection() if(u_cart is not None): assert secondary_structure_selection.size() == u_cart.size() else: assert secondary_structure_selection.size() == u_iso.size() ssm.show_summary(log=out) chains_and_residue_selections, secondary_structure_selection = chains_and_atoms( pdb_hierarchy = pdb_hierarchy, secondary_structure_selection = secondary_structure_selection, out = out) chains_and_permutations = [] chains_and_atom_selection_strings = [] print_statistics.make_header("Processing chains", out=out) if (params.nproc is None) : params.nproc = 1 for crs in chains_and_residue_selections: print_statistics.make_sub_header("Processing chain '%s'"%crs[0], out=out) chain_selection = chain_selection_from_residues(crs[1]) groups, perms = get_model_partitioning(residues = crs[1], secondary_structure_selection = secondary_structure_selection, out = out) # #print #selection_arrays = sels_as_selection_arrays(sels = groups) #merge_groups_by_connectivity( # pdb_hierarchy = pdb_hierarchy, # xray_structure = xray_structure, # selection_arrays = selection_arrays) #assert 0 # if(len(perms)==1): print >> out, " Whole chain is considered as one TLS group." chains_and_atom_selection_strings.append([crs[0], permutations_as_atom_selection_string(groups, perms[0])]) else: print >> out, " Fitting TLS matrices..." dic = {} target_best = 1.e+9 if (params.nproc is Auto) or (params.nproc > 1) : process_perms = analyze_permutations( groups=groups, sites_cart=sites_cart, u_cart=u_cart, u_iso=u_iso) from libtbx import easy_mp stdout_and_targets = easy_mp.pool_map( processes=params.nproc, fixed_func=process_perms, args=perms, chunksize=100, func_wrapper="buffer_stdout_stderr") targets = [ t for so, t in stdout_and_targets ] for (perm, target) in zip(perms, targets) : dic.setdefault(len(perm), []).append([target,perm]) else : for i_perm, perm in enumerate(perms): if i_perm%500==0: print >> out, " ...perm %d of %d"%(i_perm, len(perms)) selections = tls_group_selections(groups, perm) target = 0 for selection in selections: mo = tls_refinery( u_cart = u_cart, u_iso = u_iso, sites_cart = sites_cart, selection = selection) target += mo.f dic.setdefault(len(perm), []).append([target,perm]) #print " perm %d of %d: target=%8.3f (TLS groups: %s), permutation:"%( # i_perm, len(perms),target,len(perm)),perm print >> out, " Best fits:" print >> out, " No. of Targets" print >> out, " groups best rand.pick diff. score permutation" score_best = -1.e+9 perm_choice = None for k, v in zip(dic.keys(),dic.values()): t_best = v[0][0] perm_best = v[0][1] for v_ in v: if(v_[0]<t_best): t_best = v_[0] perm_best = v_[1] if(u_cart is not None): u_cart_ = u_cart.select(chain_selection) else: u_cart_ = None if(u_iso is not None): u_iso_ = u_iso.select(chain_selection) else: u_iso_ = None r = tls_refinery_random_groups( u_cart = u_cart_, u_iso = u_iso_, sites_cart = sites_cart.select(chain_selection), n_groups = k) score = (r-t_best)/(r+t_best)*100. print >> out, " %3d %6.3f %6.3f %6.2f %6.3f"%( k,t_best, r, r-t_best, score), perm_best if(score > score_best): score_best = score perm_choice = perm_best[:] # chains_and_permutations.append([crs[0],perm_choice]) chains_and_atom_selection_strings.append([crs[0], permutations_as_atom_selection_string(groups, perm_choice)]) # if (pdb_inp is not None) and (not ignore_pdb_header_groups) : external_tls_selections = external_tls( pdb_inp = pdb_inp, pdb_hierarchy = pdb_hierarchy, sites_cart = sites_cart, u_iso = u_iso, out = out) print_statistics.make_header("SUMMARY", out=out) #print "Optimal TLS groups:" #for chain_and_permutation in chains_and_permutations: # print chain_and_permutation #print print >> out, "TLS atom selections for phenix.refine:" groups_out = cStringIO.StringIO() selection_strings = [] print >> groups_out, "refinement.refine.adp {" for r in chains_and_atom_selection_strings: prefix = "chain '%s'"%r[0] if(len(r[1])>0 and len(r[1:])>0): prefix += " and " for r_ in r[1:]: for r__ in r_: if(len(r__)>0): group_selection = prefix+"(%s)"%r__ print >> groups_out, " tls = \"%s\"" % group_selection selection_strings.append("%s" % group_selection) else: print >> groups_out, " tls = \"%s\"" % prefix selection_strings.append("%s" % prefix) print >> groups_out, "}" print >> out, groups_out.getvalue() print >> out #XXX if 0: merge_groups_by_connectivity( pdb_hierarchy = pdb_hierarchy, xray_structure = xray_structure, selection_strings = selection_strings) #XXX if(len(selection_strings)>0): total_target = total_score( pdb_hierarchy = pdb_hierarchy, sites_cart = sites_cart, u_iso = u_iso, selection_strings = selection_strings) print >> out, "Overall best total target for automatically found groups: %10.1f"%total_target print >> out if (return_as_list) : return selection_strings else : return groups_out.getvalue()
def run(args, crystal_symmetry=None, ncs_object=None, pdb_hierarchy=None, map_data=None, lower_bounds=None, upper_bounds=None, write_output_files=True, log=None): h = "phenix.map_box: extract box with model and map around selected atoms" if(log is None): log = sys.stdout print_statistics.make_header(h, out=log) default_message="""\ %s. Usage: phenix.map_box model.pdb map_coefficients.mtz selection="chain A and resseq 1:10" or phenix.map_box map.ccp4 density_select=True Parameters:"""%h if(len(args) == 0 and not pdb_hierarchy): print default_message master_phil.show(prefix=" ") return inputs = mmtbx.utils.process_command_line_args(args = args, cmd_cs=crystal_symmetry, master_params = master_phil) params = inputs.params.extract() master_phil.format(python_object=params).show(out=log) # Overwrite params with parameters in call if available if lower_bounds: params.lower_bounds=lower_bounds if upper_bounds: params.upper_bounds=upper_bounds # PDB file if params.pdb_file and not inputs.pdb_file_names and not pdb_hierarchy: inputs.pdb_file_names=[params.pdb_file] if(len(inputs.pdb_file_names)!=1 and not params.density_select and not pdb_hierarchy and not params.keep_map_size and not params.upper_bounds and not params.extract_unique): raise Sorry("PDB file is needed unless extract_unique, "+ "density_select, keep_map_size \nor bounds are set .") if (len(inputs.pdb_file_names)!=1 and not pdb_hierarchy and \ (params.mask_atoms or params.soft_mask )): raise Sorry("PDB file is needed for mask_atoms or soft_mask") if (params.density_select and params.keep_map_size): raise Sorry("Cannot set both density_select and keep_map_size") if (params.density_select and params.upper_bounds): raise Sorry("Cannot set both density_select and bounds") if (params.keep_map_size and params.upper_bounds): raise Sorry("Cannot set both keep_map_size and bounds") if (params.upper_bounds and not params.lower_bounds): raise Sorry("Please set lower_bounds if you set upper_bounds") if (params.extract_unique and not params.resolution): raise Sorry("Please set resolution for extract_unique") if (write_output_files) and ("mtz" in params.output_format) and ( (params.keep_origin) and (not params.keep_map_size)): raise Sorry("Please set output_format=ccp4 to skip mtz or set "+\ "keep_origin=False or keep_map_size=True") if params.keep_input_unit_cell_and_grid and ( (params.output_unit_cell_grid is not None ) or (params.output_unit_cell is not None ) ): raise Sorry("If you set keep_input_unit_cell_and_grid then you cannot "+\ "set \noutput_unit_cell_grid or output_unit_cell") if params.output_origin_grid_units is not None and params.keep_origin: params.keep_origin=False print "Setting keep_origin=False as output_origin_grid_units is set" print_statistics.make_sub_header("pdb model", out=log) if len(inputs.pdb_file_names)>0: pdb_inp = iotbx.pdb.input(file_name=inputs.pdb_file_names[0]) pdb_hierarchy = pdb_inp.construct_hierarchy() if pdb_hierarchy: pdb_atoms = pdb_hierarchy.atoms() pdb_atoms.reset_i_seq() else: pdb_hierarchy=None # Map or map coefficients map_coeff = None input_unit_cell_grid=None input_unit_cell=None if (not map_data): # read first mtz file if ( (len(inputs.reflection_file_names) > 0) or (params.map_coefficients_file is not None) ): # file in phil takes precedent if (params.map_coefficients_file is not None): if (len(inputs.reflection_file_names) == 0): inputs.reflection_file_names.append(params.map_coefficients_file) else: inputs.reflection_file_names[0] = params.map_coefficients_file map_coeff = reflection_file_utils.extract_miller_array_from_file( file_name = inputs.reflection_file_names[0], label = params.label, type = "complex", log = log) if not crystal_symmetry: crystal_symmetry=map_coeff.crystal_symmetry() fft_map = map_coeff.fft_map(resolution_factor=params.resolution_factor) fft_map.apply_sigma_scaling() map_data = fft_map.real_map_unpadded() map_or_map_coeffs_prefix=os.path.basename( inputs.reflection_file_names[0][:-4]) # or read CCP4 map elif ( (inputs.ccp4_map is not None) or (params.ccp4_map_file is not None) ): if (params.ccp4_map_file is not None): af = any_file(params.ccp4_map_file) if (af.file_type == 'ccp4_map'): inputs.ccp4_map = af.file_content inputs.ccp4_map_file_name = params.ccp4_map_file print_statistics.make_sub_header("CCP4 map", out=log) ccp4_map = inputs.ccp4_map ccp4_map.show_summary(prefix=" ",out=log) if not crystal_symmetry: crystal_symmetry=ccp4_map.crystal_symmetry() map_data = ccp4_map.data #map_data() input_unit_cell_grid=ccp4_map.unit_cell_grid input_unit_cell=ccp4_map.unit_cell_parameters if inputs.ccp4_map_file_name.endswith(".ccp4"): map_or_map_coeffs_prefix=os.path.basename( inputs.ccp4_map_file_name[:-5]) else: map_or_map_coeffs_prefix=os.path.basename( inputs.ccp4_map_file_name[:-4]) else: # have map_data map_or_map_coeffs_prefix=None if params.output_origin_grid_units is not None: origin_to_match=tuple(params.output_origin_grid_units) else: origin_to_match=None if origin_to_match: sc=[] for x,o,a in zip(crystal_symmetry.unit_cell().parameters()[:3], origin_to_match, map_data.all()): sc.append(-x*o/a) shift_cart_for_origin_to_match=tuple(sc) else: origin_to_match=None shift_cart_for_origin_to_match=None if crystal_symmetry and not inputs.crystal_symmetry: inputs.crystal_symmetry=crystal_symmetry # final check that map_data exists if(map_data is None): raise Sorry("Map or map coefficients file is needed.") if len(inputs.pdb_file_names)>0: output_prefix=os.path.basename(inputs.pdb_file_names[0])[:-4] else: output_prefix=map_or_map_coeffs_prefix if not pdb_hierarchy: # get an empty hierarchy from cctbx.array_family import flex pdb_hierarchy=iotbx.pdb.input( source_info='',lines=flex.split_lines('')).construct_hierarchy() xray_structure = pdb_hierarchy.extract_xray_structure( crystal_symmetry=inputs.crystal_symmetry) xray_structure.show_summary(f=log) # selection = pdb_hierarchy.atom_selection_cache().selection( string = params.selection) if selection.size(): print_statistics.make_sub_header("atom selection", out=log) print >> log, "Selection string: selection='%s'"%params.selection print >> log, \ " selects %d atoms from total %d atoms."%(selection.count(True), selection.size()) sites_cart_all = xray_structure.sites_cart() sites_cart = sites_cart_all.select(selection) selection = xray_structure.selection_within( radius = params.selection_radius, selection = selection) if not ncs_object: from mmtbx.ncs.ncs import ncs ncs_object=ncs() if params.symmetry_file: ncs_object.read_ncs(params.symmetry_file,log=log) print >>log,"Total of %s operators read" %(ncs_object.max_operators()) if not ncs_object or ncs_object.max_operators()<1: print >>log,"No symmetry available" if ncs_object: n_ops=max(1,ncs_object.max_operators()) else: n_ops=1 # Get sequence if extract_unique is set sequence=None if params.extract_unique: if params.sequence_file: if n_ops > 1: # get unique part of sequence and multiply remove_duplicates=True else: remove_duplicates=False from iotbx.bioinformatics import get_sequences sequence=n_ops * (" ".join(get_sequences(file_name=params.sequence_file, remove_duplicates=remove_duplicates))) if sequence and not params.molecular_mass: # get molecular mass from sequence from iotbx.bioinformatics import text_from_chains_matching_chain_type if params.chain_type in [None,'PROTEIN']: n_protein=len(text_from_chains_matching_chain_type( text=sequence,chain_type='PROTEIN')) else: n_protein=0 if params.chain_type in [None,'RNA']: n_rna=len(text_from_chains_matching_chain_type( text=sequence,chain_type='RNA')) else: n_rna=0 if params.chain_type in [None,'DNA']: n_dna=len(text_from_chains_matching_chain_type( text=sequence,chain_type='DNA')) else: n_dna=0 params.molecular_mass=n_protein*110+(n_rna+n_dna)*330 elif not params.molecular_mass: raise Sorry("Need a sequence file or molecular mass for extract_unique") else: molecular_mass=None # if params.density_select: print_statistics.make_sub_header( "Extracting box around selected density and writing output files", out=log) else: print_statistics.make_sub_header( "Extracting box around selected atoms and writing output files", out=log) # if params.value_outside_atoms=='mean': print >>log,"\nValue outside atoms mask will be set to mean inside mask" if params.get_half_height_width and params.density_select: print >>log,"\nHalf width at half height will be used to id boundaries" if params.soft_mask and sites_cart_all.size()>0: print >>log,"\nSoft mask will be applied to model-based mask" if params.keep_map_size: print >>log,"\nEntire map will be kept (not cutting out region)" if params.restrict_map_size: print >>log,"\nOutput map will be within input map" if params.lower_bounds and params.upper_bounds: print >>log,"Bounds for cut out map are (%s,%s,%s) to (%s,%s,%s)" %( tuple(list(params.lower_bounds)+list(params.upper_bounds))) box = mmtbx.utils.extract_box_around_model_and_map( xray_structure = xray_structure, map_data = map_data.as_double(), box_cushion = params.box_cushion, selection = selection, density_select = params.density_select, threshold = params.density_select_threshold, get_half_height_width = params.get_half_height_width, mask_atoms = params.mask_atoms, soft_mask = params.soft_mask, soft_mask_radius = params.soft_mask_radius, mask_atoms_atom_radius = params.mask_atoms_atom_radius, value_outside_atoms = params.value_outside_atoms, keep_map_size = params.keep_map_size, restrict_map_size = params.restrict_map_size, lower_bounds = params.lower_bounds, upper_bounds = params.upper_bounds, extract_unique = params.extract_unique, chain_type = params.chain_type, sequence = sequence, solvent_content = params.solvent_content, molecular_mass = params.molecular_mass, resolution = params.resolution, ncs_object = ncs_object, symmetry = params.symmetry, ) ph_box = pdb_hierarchy.select(selection) ph_box.adopt_xray_structure(box.xray_structure_box) box.hierarchy=ph_box if (inputs and # XXX fix or remove this inputs.crystal_symmetry and inputs.ccp4_map and inputs.crystal_symmetry.unit_cell().parameters() and inputs.ccp4_map.unit_cell_parameters ) and ( inputs.crystal_symmetry.unit_cell().parameters() != inputs.ccp4_map.unit_cell_parameters): print >>log,"\nNOTE: Mismatch of unit cell parameters from CCP4 map:" print >>log,"Unit cell from CCP4 map 'unit cell parameters': "+\ "%.1f, %.1f, %.1f, %.1f, %.1f, %.1f)" %tuple( inputs.ccp4_map.unit_cell_parameters) print >>log,"Unit cell from CCP4 map 'map grid': "+\ "%.1f, %.1f, %.1f, %.1f, %.1f, %.1f)" %tuple( inputs.crystal_symmetry.unit_cell().parameters()) print >>log,"\nInterpreting this as the 'unit cell parameters' was "+\ "original map \ndimension and 'map grid' is the "+\ "portion actually in the map that was supplied here.\n" box.unit_cell_parameters_from_ccp4_map=inputs.ccp4_map.unit_cell_parameters box.unit_cell_parameters_deduced_from_map_grid=\ inputs.crystal_symmetry.unit_cell().parameters() else: box.unit_cell_parameters_from_ccp4_map=None box.unit_cell_parameters_deduced_from_map_grid=None if box.pdb_outside_box_msg: print >> log, box.pdb_outside_box_msg # NOTE: box object is always shifted to place origin at (0,0,0) # NOTE ON ORIGIN SHIFTS: The shifts are applied locally here. The box # object is not affected and always has the origin at (0,0,0) # output_box is copy of box with shift_cart corresponding to the output # files. Normally this is the same as the original shift_cart. However # if user has specified a new output origin it will differ. # For output files ONLY: # keep_origin==False leave origin at (0,0,0) # keep_origin==True: we shift everything back to where it was, # output_origin_grid_units=10,10,10: output origin is at (10,10,10) # ncs_object is original # box.ncs_object is shifted by shift_cart # output_box.ncs_object is shifted back by -new shift_cart # Additional note on output unit_cell and grid_units. # The ccp4-style output map can specify the unit cell and grid units # corresponding to that cell. This can be separate from the origin and # number of grid points in the map as written. If specified, write these # out to the output ccp4 map and also use this unit cell for writing # any output PDB files from copy import deepcopy output_box=deepcopy(box) # won't use box below here except to return it print >>log,"\nBox cell dimensions: (%.2f, %.2f, %.2f) A" %( box.box_crystal_symmetry.unit_cell().parameters()[:3]) if box.shift_cart: print>>log,"Working origin moved from grid position of"+\ ": (%d, %d, %d) to (0,0,0) " %( tuple(box.origin_shift_grid_units(reverse=True))) print>>log,"Working origin moved from coordinates of:"+\ " (%.2f, %.2f, %.2f) A to (0,0,0)\n" %( tuple(-col(box.shift_cart))) if (params.keep_origin): print >>log,"\nRestoring original position for output files" print >>log,"Origin will be at grid position of"+\ ": (%d, %d, %d) " %( tuple(box.origin_shift_grid_units(reverse=True))) print >>log,\ "\nOutput files will be in same location as original", if not params.keep_map_size: print >>log,"just cut out." else: print >>log,"keeping entire map" print >>log,"Note that output maps are only valid in the cut out region.\n" else: if origin_to_match: output_box.shift_cart=shift_cart_for_origin_to_match if params.output_origin_grid_units: print >>log,"Output map origin to be shifted to match target" print >>log, "Placing origin at grid point (%s, %s, %s)" %( origin_to_match)+"\n"+ \ "Final coordinate shift for output files: (%.2f,%.2f,%.2f) A\n" %( tuple(col(output_box.shift_cart)-col(box.shift_cart))) elif box.shift_cart: output_box.shift_cart=(0,0,0) # not shifting back print >>log,"Final origin will be at (0,0,0)" print >>log,\ "Final coordinate shift for output files: (%.2f,%.2f,%.2f) A\n" %( tuple(col(output_box.shift_cart)-col(box.shift_cart))) else: print >>log,\ "\nOutput files are in same location as original and origin "+\ "is at (0,0,0)\n" ph_output_box_output_location = ph_box.deep_copy() if output_box.shift_cart: # shift coordinates and NCS back by shift_cart # NOTE output_box.shift_cart could be different than box.shift_cart if # there is a target position for the origin and it is not the same as the # original origin. sites_cart = output_box.shift_sites_cart_back( output_box.xray_structure_box.sites_cart()) xrs_offset = ph_output_box_output_location.extract_xray_structure( crystal_symmetry=output_box.xray_structure_box.crystal_symmetry() ).replace_sites_cart(new_sites = sites_cart) ph_output_box_output_location.adopt_xray_structure(xrs_offset) if output_box.ncs_object: output_box.ncs_object=output_box.ncs_object.coordinate_offset( tuple(-col(output_box.shift_cart))) shift_back=True else: shift_back=False if params.keep_input_unit_cell_and_grid and \ (input_unit_cell_grid is not None) and \ (input_unit_cell is not None): params.output_unit_cell=input_unit_cell params.output_unit_cell_grid=input_unit_cell_grid print >>log,"Setting output unit cell parameters and unit cell grid to"+\ " match\ninput map file" if params.output_unit_cell: # Set output unit cell parameters from cctbx import crystal output_crystal_symmetry=crystal.symmetry( unit_cell=params.output_unit_cell, space_group="P1") output_unit_cell=output_crystal_symmetry.unit_cell() print >>log,\ "Output unit cell set to: %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)" %tuple( output_crystal_symmetry.unit_cell().parameters()) else: output_crystal_symmetry=None # ============= Check/set output unit cell grid and cell parameters ======= if params.output_unit_cell_grid or output_crystal_symmetry: if params.output_unit_cell_grid: output_unit_cell_grid=params.output_unit_cell_grid else: output_unit_cell_grid=output_box.map_box.all() print >>log,\ "Output unit cell grid set to: (%s, %s, %s)" %tuple( output_unit_cell_grid) expected_output_abc=[] box_spacing=[] output_spacing=[] box_abc=output_box.xray_structure_box.\ crystal_symmetry().unit_cell().parameters()[:3] if output_crystal_symmetry: output_abc=output_crystal_symmetry.unit_cell().parameters()[:3] else: output_abc=[None,None,None] for a_box,a_output,n_box,n_output in zip( box_abc, output_abc, output_box.map_box.all(), output_unit_cell_grid): expected_output_abc.append(a_box*n_output/n_box) box_spacing.append(a_box/n_box) if output_crystal_symmetry: output_spacing.append(a_output/n_output) else: output_spacing.append(a_box/n_box) if output_crystal_symmetry: # make sure it is compatible... r0=expected_output_abc[0]/output_abc[0] r1=expected_output_abc[1]/output_abc[1] r2=expected_output_abc[2]/output_abc[2] from libtbx.test_utils import approx_equal if not approx_equal(r0,r1,eps=0.001) or not approx_equal(r0,r2,eps=0.001): print >>log,"WARNING: output_unit_cell and cell_grid will "+\ "change ratio of grid spacing.\nOld spacings: "+\ "(%.2f, %.2f, %.2f) A " %(tuple(box_spacing))+\ "\nNew spacings: (%.2f, %.2f, %.2f) A \n" %(tuple(output_spacing)) else: output_abc=expected_output_abc from cctbx import crystal output_crystal_symmetry=crystal.symmetry( unit_cell=list(output_abc)+[90,90,90], space_group="P1") print >>log, \ "Output unit cell will be: (%.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n"%( tuple(output_crystal_symmetry.unit_cell().parameters())) else: output_unit_cell_grid = map_data=output_box.map_box.all() output_crystal_symmetry=output_box.xray_structure_box.crystal_symmetry() # ========== Done check/set output unit cell grid and cell parameters ===== if write_output_files: # Write PDB file if ph_box.overall_counts().n_residues>0: if(params.output_file_name_prefix is None): file_name = "%s_box.pdb"%output_prefix else: file_name = "%s.pdb"%params.output_file_name_prefix ph_output_box_output_location.write_pdb_file(file_name=file_name, crystal_symmetry = output_crystal_symmetry) print >> log, "Writing boxed PDB with box unit cell to %s" %( file_name) # Write NCS file if NCS if output_box.ncs_object and output_box.ncs_object.max_operators()>0: if(params.output_file_name_prefix is None): output_symmetry_file = "%s_box.ncs_spec"%output_prefix else: output_symmetry_file = "%s.ncs_spec"%params.output_file_name_prefix output_box.ncs_object.format_all_for_group_specification( file_name=output_symmetry_file) print >>log,"\nWriting symmetry to %s" %( output_symmetry_file) # Write ccp4 map. if("ccp4" in params.output_format): if(params.output_file_name_prefix is None): file_name = "%s_box.ccp4"%output_prefix else: file_name = "%s.ccp4"%params.output_file_name_prefix output_box.write_ccp4_map(file_name=file_name, output_crystal_symmetry=output_crystal_symmetry, output_unit_cell_grid=output_unit_cell_grid, shift_back=shift_back) print >> log, "Writing boxed map "+\ "to CCP4 formatted file: %s"%file_name # Write xplor map. Shift back if keep_origin=True if("xplor" in params.output_format): if(params.output_file_name_prefix is None): file_name = "%s_box.xplor"%output_prefix else: file_name = "%s.xplor"%params.output_file_name_prefix output_box.write_xplor_map(file_name=file_name, output_crystal_symmetry=output_crystal_symmetry, output_unit_cell_grid=output_unit_cell_grid, shift_back=shift_back,) print >> log, "Writing boxed map "+\ "to X-plor formatted file: %s"%file_name # Write mtz map coeffs. Shift back if keep_origin=True if("mtz" in params.output_format): if(params.output_file_name_prefix is None): file_name = "%s_box.mtz"%output_prefix else: file_name = "%s.mtz"%params.output_file_name_prefix print >> log, "Writing map coefficients "+\ "to MTZ file: %s"%file_name if(map_coeff is not None): d_min = map_coeff.d_min() elif params.resolution is not None: d_min = params.resolution else: d_min = maptbx.d_min_from_map(map_data=output_box.map_box, unit_cell=output_box.xray_structure_box.unit_cell()) output_box.map_coefficients(d_min=d_min, resolution_factor=params.resolution_factor, file_name=file_name, shift_back=shift_back) print >> log return box
def run( model, params, fmodels, save_optimize_adp_weight, log, macro_cycle, prefix, target_weights): target_weights_params = params.target_weights h_params = params.hydrogens print_statistics.make_header(prefix, out = log) if(save_optimize_adp_weight): r_work = fmodels.fmodel_xray().r_work() r_free = fmodels.fmodel_xray().r_free() if ((r_free < r_work or (r_free-r_work)<0.01) and (not target_weights_params.force_optimize_weights)) : target_weights_params.optimize_adp_weight = False else: target_weights_params.optimize_adp_weight=save_optimize_adp_weight fmodels.fmodel_xray().xray_structure.scatterers().flags_set_grads( state = False) # Exclude H from ADP refinement if ((params.hydrogens.refine == "riding") or (params.hydrogens.force_riding_adp)) : excl_h = model.xray_structure.hd_selection().count(True) > 0 if(excl_h and model.refinement_flags.adp_individual_iso is not None): model.refinement_flags.adp_individual_iso.set_selected( model.xray_structure.hd_selection(), False) if(excl_h and model.refinement_flags.adp_individual_aniso is not None): model.refinement_flags.adp_individual_aniso.set_selected( model.xray_structure.hd_selection(), False) # save_xray_structure = \ fmodels.fmodel_xray().xray_structure.deep_copy_scatterers() # XXX ? ###> Make ADP of H/D sites equal model.reset_adp_of_hd_sites_to_be_equal() fmodels.update_xray_structure(xray_structure = model.xray_structure, update_f_calc = True) tidy_us(params=params, fmodels=fmodels, model=model, log=log) adp_refinement_manager = adp_refinement.manager( fmodels = fmodels, model = model, group_adp_selections = model.refinement_flags.adp_group, group_adp_selections_h = model.refinement_flags.group_h, group_adp_params = params.group_b_iso, tls_selections = model.refinement_flags.adp_tls, nm_selections = model.refinement_flags.adp_nm, all_params = params, tls_params = params.tls, nm_params = params.nm, individual_adp_params = params.adp, adp_restraints_params = params.adp_restraints, refine_adp_individual = model.refinement_flags.individual_adp, refine_adp_group = model.refinement_flags.group_adp, refine_tls = model.refinement_flags.tls, refine_nm = model.refinement_flags.nm, tan_b_iso_max = 0, restraints_manager = model.restraints_manager, macro_cycle = macro_cycle, target_weights = target_weights, log = log, h_params = h_params) assert fmodels.fmodel_xray().xray_structure.min_u_cart_eigenvalue()>=0 print >> log model.show_adp_statistics(padded = True) print >> log fmodels.update_xray_structure(update_f_calc = True) ###> Make ADP of H/D sites equal model.reset_adp_of_hd_sites_to_be_equal() fmodels.update_xray_structure(xray_structure = model.xray_structure, update_f_calc = True) assert fmodels.fmodel_xray().xray_structure.min_u_cart_eigenvalue()>=0
def find_tls(params, pdb_hierarchy, xray_structure, return_as_list=False, ignore_pdb_header_groups=False, out=None): """ !!! WARNING! incoming xray_structure here gets converted to isotropic B-factors IN PLACE. """ if (out is None): out = sys.stdout print_statistics.make_header("Analyzing inputs", out=out) if (params.random_seed is None): params.random_seed = flex.get_random_seed() random.seed(params.random_seed) flex.set_random_seed(params.random_seed) xray_structure.convert_to_isotropic() sites_cart = xray_structure.sites_cart() u_cart = None u_iso = xray_structure.extract_u_iso_or_u_equiv() #*adptbx.u_as_b(1.) # ? bad_i_seqs = check_adp(u_iso=u_iso, out=out) if (bad_i_seqs is not None): atoms = pdb_hierarchy.atoms() bad_atom_strings = [] for i_seq in bad_i_seqs[:10]: atom_str = atoms[i_seq].format_atom_record() bad_atom_strings.append(atom_str) if (len(bad_i_seqs) > 10): bad_atom_strings.append("... (remaining %d not shown)" % (len(bad_i_seqs) - 10)) raise Sorry( ("%d atoms in the model contain isotropic B-factors <= 0:\n" + "\n".join(bad_atom_strings)) % (len(bad_i_seqs))) # ssm = mmtbx.secondary_structure.manager(pdb_hierarchy=pdb_hierarchy, sec_str_from_pdb_file=None, params=None, log=out) alpha_h_selection = ssm.helix_selection() secondary_structure_selection = ssm.helix_selection() | \ ssm.beta_selection() | ssm.base_pair_selection() if (u_cart is not None): assert secondary_structure_selection.size() == u_cart.size() else: assert secondary_structure_selection.size() == u_iso.size() ssm.show_summary(log=out) chains_and_residue_selections, secondary_structure_selection = chains_and_atoms( pdb_hierarchy=pdb_hierarchy, secondary_structure_selection=secondary_structure_selection, out=out) chains_and_permutations = [] chains_and_atom_selection_strings = [] print_statistics.make_header("Processing chains", out=out) if (params.nproc is None): params.nproc = 1 for crs in chains_and_residue_selections: print_statistics.make_sub_header("Processing chain '%s'" % crs[0], out=out) chain_selection = chain_selection_from_residues(crs[1]) groups, perms = get_model_partitioning( residues=crs[1], secondary_structure_selection=secondary_structure_selection, out=out) # if (len(perms) == 1): print(" Whole chain is considered as one TLS group.", file=out) chains_and_atom_selection_strings.append([ crs[0], permutations_as_atom_selection_string(groups, perms[0]) ]) else: print(" Fitting TLS matrices...", file=out) dic = {} target_best = 1.e+9 if (params.nproc is Auto) or (params.nproc > 1): process_perms = analyze_permutations(groups=groups, sites_cart=sites_cart, u_cart=u_cart, u_iso=u_iso) from libtbx import easy_mp stdout_and_targets = easy_mp.pool_map( processes=params.nproc, fixed_func=process_perms, args=perms, chunksize=100, func_wrapper="buffer_stdout_stderr") targets = [t for so, t in stdout_and_targets] for (perm, target) in zip(perms, targets): dic.setdefault(len(perm), []).append([target, perm]) else: for i_perm, perm in enumerate(perms): if i_perm % 500 == 0: print(" ...perm %d of %d" % (i_perm, len(perms)), file=out) selections = tls_group_selections(groups, perm) target = 0 for selection in selections: mo = tls_refinery(u_cart=u_cart, u_iso=u_iso, sites_cart=sites_cart, selection=selection) target += mo.f dic.setdefault(len(perm), []).append([target, perm]) #print " perm %d of %d: target=%8.3f (TLS groups: %s), permutation:"%( # i_perm, len(perms),target,len(perm)),perm print(" Best fits:", file=out) print(" No. of Targets", file=out) print(" groups best rand.pick diff. score permutation", file=out) score_best = -1.e+9 perm_choice = None for k, v in six.iteritems(dic): t_best = v[0][0] perm_best = v[0][1] for v_ in v: if (v_[0] < t_best): t_best = v_[0] perm_best = v_[1] if (u_cart is not None): u_cart_ = u_cart.select(chain_selection) else: u_cart_ = None if (u_iso is not None): u_iso_ = u_iso.select(chain_selection) else: u_iso_ = None r = tls_refinery_random_groups( u_cart=u_cart_, u_iso=u_iso_, sites_cart=sites_cart.select(chain_selection), n_groups=k) score = (r - t_best) / (r + t_best) * 100. print(" %3d %6.3f %6.3f %6.2f %6.3f" % (k, t_best, r, r - t_best, score), perm_best, file=out) if (score > score_best): score_best = score perm_choice = perm_best[:] # chains_and_permutations.append([crs[0], perm_choice]) chains_and_atom_selection_strings.append([ crs[0], permutations_as_atom_selection_string(groups, perm_choice) ]) # print_statistics.make_header("SUMMARY", out=out) #print "Optimal TLS groups:" #for chain_and_permutation in chains_and_permutations: # print chain_and_permutation #print print("TLS atom selections for phenix.refine:", file=out) groups_out = StringIO() selection_strings = [] print("refinement.refine.adp {", file=groups_out) for r in chains_and_atom_selection_strings: prefix = "chain '%s'" % r[0] if (len(r[1]) > 0 and len(r[1:]) > 0): prefix += " and " for r_ in r[1:]: for r__ in r_: if (len(r__) > 0): group_selection = prefix + "(%s)" % r__ print(" tls = \"%s\"" % group_selection, file=groups_out) selection_strings.append("%s" % group_selection) else: print(" tls = \"%s\"" % prefix, file=groups_out) selection_strings.append("%s" % prefix) print("}", file=groups_out) print(groups_out.getvalue(), file=out) print(file=out) #XXX if 0: merge_groups_by_connectivity(pdb_hierarchy=pdb_hierarchy, xray_structure=xray_structure, selection_strings=selection_strings) #XXX if (len(selection_strings) > 0): total_target = total_score(pdb_hierarchy=pdb_hierarchy, sites_cart=sites_cart, u_iso=u_iso, selection_strings=selection_strings) print( "Overall best total target for automatically found groups: %10.1f" % total_target, file=out) print(file=out) if (return_as_list): return selection_strings else: return groups_out.getvalue()
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)