def bond_deviations_weighted(self): if(self.n_bond_proxies is not None): bond_deltas = self.bond_proxies.deltas( sites_cart=self.sites_cart, origin_id=0) if len(bond_deltas) >0: sigmas = flex.double([geometry_restraints.weight_as_sigma(x.weight) for x in self.bond_proxies.simple]) sigma_mean = flex.mean_default(sigmas, 0) z_scores = flex.double([(bond_delta/sigma*sigma_mean) for bond_delta,sigma in zip(bond_deltas,sigmas)]) b_rmsz = math.sqrt(flex.mean_default(z_scores*z_scores,0)) b_z_max = flex.max_default(flex.abs(z_scores), 0) b_z_min = flex.min_default(flex.abs(z_scores), 0) return b_z_min, b_z_max, b_rmsz else: return 0,0,0
def angle_deviations_weighted(self): if(self.n_angle_proxies is not None): angle_deltas = self.angle_proxies.proxy_select(origin_id=0).deltas( sites_cart=self.sites_cart) if len(angle_deltas) > 0: sigmas = flex.double([geometry_restraints.weight_as_sigma(x.weight) for x in self.angle_proxies]) sigma_mean = flex.mean_default(sigmas, 0) z_scores = flex.double([(angle_delta/sigma*sigma_mean) for angle_delta,sigma in zip(angle_deltas,sigmas)]) a_rmsz = math.sqrt(flex.mean_default(z_scores*z_scores,0)) a_z_max = flex.max_default(flex.abs(z_scores), 0) a_z_min = flex.min_default(flex.abs(z_scores), 0) return a_z_min, a_z_max, a_rmsz else: return 0,0,0
def bond_deviations_z(self): ''' Calculate rmsz of bond deviations Compute rmsz, the Root-Mean-Square of the z-scors for a set of data using z_i = {x_i - mu / sigma} and rmsz = sqrt(mean(z*z)) x_i: atcual bond length mu: geometry restraints mean sigma: geometry restraints standard deviation z_i: z-score for bond i z: array of z_i The sigma and the (x_i - mu) are model constrains, geometry restraints. This function extracts from self, not calculated from data. :returns: b_rmsz: rmsz, root mean square of the z-scors of all bonds b_z_min/max: min/max abolute values of z-scors ''' if(self.n_bond_proxies is not None): bond_deltas = self.bond_proxies.deltas( sites_cart=self.sites_cart, origin_id=0) if len(bond_deltas) >0: sigmas = [geometry_restraints.weight_as_sigma(x.weight) for x in self.bond_proxies.simple] z_scores = flex.double([(bond_delta/sigma) for bond_delta,sigma in zip(bond_deltas,sigmas)]) b_rmsz = math.sqrt(flex.mean_default(z_scores*z_scores,0)) b_z_max = flex.max_default(flex.abs(z_scores), 0) b_z_min = flex.min_default(flex.abs(z_scores), 0) return b_z_min, b_z_max, b_rmsz else: return 0,0,0
def bond_deviations_z(self): ''' Calculate rmsz of bond deviations Compute rmsz, the Root-Mean-Square of the z-scors for a set of data using z_i = {x_i - mu / sigma} and rmsz = sqrt(mean(z*z)) x_i: atcual bond length mu: geometry restraints mean sigma: geometry restraints standard deviation z_i: z-score for bond i z: array of z_i The sigma and the (x_i - mu) are model constrains, geometry restraints. They function extracts from self, not calculated from data. :returns: b_rmsz: rmsz, root mean square of the z-scors of all bonds b_z_min/max: min/max abolute values of z-scors ''' if(self.n_bond_proxies is not None): bond_deltas = self.bond_proxies.deltas( sites_cart=self.sites_cart, origin_id=0) if len(bond_deltas) >0: sigmas = [geometry_restraints.weight_as_sigma(x.weight) for x in self.bond_proxies.simple] z_scores = flex.double([(bond_delta/sigma) for bond_delta,sigma in zip(bond_deltas,sigmas)]) b_rmsz = math.sqrt(flex.mean_default(z_scores*z_scores,0)) b_z_max = flex.max_default(flex.abs(z_scores), 0) b_z_min = flex.min_default(flex.abs(z_scores), 0) return b_z_min, b_z_max, b_rmsz else: return 0,0,0
def angle_deviations_z(self): ''' Calculate rmsz of angles deviations Compute rmsz, the Root-Mean-Square of the z-scors for a set of data using z_i = {x_i - mu / sigma} and rmsz = sqrt(mean(z*z)) Compute rmsz, the Root-Mean-Square of the z-scors for a set of data using z_i = {x_i - mu / sigma} and rmsz = sqrt(mean(z*z)) x_i: atcual bond angle mu: geometry restraints mean sigma: geometry restraints standard deviation z_i: z-score for bond i z: array of z_i The sigma and the (x_i - mu) are model constrains, geometry restraints. They function extracts from self, not calculated from data. :returns: a_rmsz: rmsz, root mean square of the z-scors of all angles a_z_min/max: min/max values of z-scors ''' if(self.n_angle_proxies is not None): angle_deltas = self.angle_proxies.proxy_select(origin_id=0).deltas( sites_cart=self.sites_cart) if len(angle_deltas) > 0: sigmas = [geometry_restraints.weight_as_sigma(x.weight) for x in self.angle_proxies] z_scores = flex.double([(angle_delta/sigma) for angle_delta,sigma in zip(angle_deltas,sigmas)]) a_rmsz = math.sqrt(flex.mean_default(z_scores*z_scores,0)) a_z_max = flex.max_default(flex.abs(z_scores), 0) a_z_min = flex.min_default(flex.abs(z_scores), 0) return a_z_min, a_z_max, a_rmsz else: return 0,0,0
def nonbonded_deviations(self): if (self.n_nonbonded_proxies is not None): nonbonded_deltas = self.nonbonded_distances() r_sq = nonbonded_deltas * nonbonded_deltas r_ave = math.sqrt(flex.mean_default(r_sq, 0)) r_max = math.sqrt(flex.max_default(r_sq, 0)) r_min = math.sqrt(flex.min_default(r_sq, 0)) return r_min, r_max, r_ave
def nonbonded_deviations(self): if(self.n_nonbonded_proxies is not None): nonbonded_deltas = self.nonbonded_distances() r_sq = nonbonded_deltas * nonbonded_deltas r_ave = math.sqrt(flex.mean_default(r_sq, 0)) r_max = math.sqrt(flex.max_default(r_sq, 0)) r_min = math.sqrt(flex.min_default(r_sq, 0)) return r_min, r_max, r_ave
def show(self, message): print >> self.log, message sol_sel = self.model.solvent_selection() xrs_mac_h = self.model.get_xray_structure().select(~sol_sel) xrs_sol_h = self.model.get_xray_structure().select(sol_sel) hd_sol = self.model.get_hd_selection().select(sol_sel) hd_mac = self.model.get_hd_selection().select(~sol_sel) xrs_sol = xrs_sol_h.select(~hd_sol) xrs_mac = xrs_mac_h.select(~hd_mac) scat = xrs_sol.scatterers() occ = scat.extract_occupancies() b_isos = scat.extract_u_iso_or_u_equiv( self.model.get_xray_structure().unit_cell()) * math.pi**2 * 8 smallest_distances = xrs_mac.closest_distances( sites_frac = xrs_sol.sites_frac(), distance_cutoff = self.find_peaks_params.map_next_to_model.\ max_model_peak_dist).smallest_distances number = format_value("%-7d", scat.size()) b_min = format_value("%-7.2f", flex.min_default(b_isos, None)) b_max = format_value("%-7.2f", flex.max_default(b_isos, None)) b_ave = format_value("%-7.2f", flex.mean_default(b_isos, None)) bl_min = format_value("%-7.2f", self.params.b_iso_min).strip() bl_max = format_value("%-7.2f", self.params.b_iso_max).strip() o_min = format_value("%-7.2f", flex.min_default(occ, None)) o_max = format_value("%-7.2f", flex.max_default(occ, None)) ol_min = format_value("%-7.2f", self.params.occupancy_min).strip() ol_max = format_value("%-7.2f", self.params.occupancy_max).strip() d_min = format_value("%-7.2f", flex.min_default(smallest_distances, None)) d_max = format_value("%-7.2f", flex.max_default(smallest_distances, None)) dl_min = format_value( "%-7.2f", self.find_peaks_params.map_next_to_model. min_model_peak_dist).strip() dl_max = format_value( "%-7.2f", self.find_peaks_params.map_next_to_model. max_model_peak_dist).strip() ani_min = format_value( "%-7.2f", flex.min_default(scat.anisotropy(unit_cell=xrs_sol.unit_cell()), None)) ani_min_l = format_value("%-7.2f", self.params.anisotropy_min).strip() print >> self.log, " number = %s" % number print >> self.log, " b_iso_min = %s (limit = %s)" % (b_min, bl_min) print >> self.log, " b_iso_max = %s (limit = %s)" % (b_max, bl_max) print >> self.log, " b_iso_mean = %s " % (b_ave) print >> self.log, " anisotropy_min = %s (limit = %s)" % (ani_min, ani_min_l) print >> self.log, " occupancy_min = %s (limit = %s)" % (o_min, ol_min) print >> self.log, " occupancy_max = %s (limit = %s)" % (o_max, ol_max) print >> self.log, " dist_sol_mol_min = %s (limit = %s)" % (d_min, dl_min) print >> self.log, " dist_sol_mol_max = %s (limit = %s)" % (d_max, dl_max)
def __init__( self, fmodel, coeffs): # XXX see f_model.py: duplication! Consolidate. self.fmodel = fmodel self.coeffs = coeffs crystal_gridding = fmodel.f_obs().crystal_gridding( d_min = self.fmodel.f_obs().d_min(), resolution_factor = 1./3) fft_map = miller.fft_map( crystal_gridding = crystal_gridding, fourier_coefficients = self.coeffs) fft_map.apply_sigma_scaling() map_data = fft_map.real_map_unpadded() rho_atoms = flex.double() for site_frac in self.fmodel.xray_structure.sites_frac(): rho_atoms.append(map_data.eight_point_interpolation(site_frac)) rho_mean = flex.mean_default(rho_atoms.select(rho_atoms>0.5), 0.5) sel_exclude = rho_atoms > min(rho_mean/2., 1) sites_cart = fmodel.xray_structure.sites_cart() # fft_map = miller.fft_map( crystal_gridding = crystal_gridding, fourier_coefficients = self.fmodel.f_model()) fft_map.apply_sigma_scaling() map_data2 = fft_map.real_map_unpadded() # for i_seq, site_cart in enumerate(sites_cart): selection = maptbx.grid_indices_around_sites( unit_cell = self.coeffs.unit_cell(), fft_n_real = map_data.focus(), fft_m_real = map_data.all(), sites_cart = flex.vec3_double([site_cart]), site_radii = flex.double([1.5])) cc = flex.linear_correlation(x=map_data.select(selection), y=map_data2.select(selection)).coefficient() if(cc<0.7): sel_exclude[i_seq] = False # del map_data, fft_map, rho_atoms self.d_min = fmodel.f_obs().d_min() cs = self.fmodel.f_obs().average_bijvoet_mates().complete_set(d_min=self.d_min) self.complete_set = cs.array(data = flex.double(cs.indices().size(), 0)) self.xray_structure_cut = self.fmodel.xray_structure.select(sel_exclude) self.missing_set = self.complete_set.common_set(self.coeffs) # self.f_calc_missing = self.complete_set.structure_factors_from_scatterers( xray_structure = self.xray_structure_cut).f_calc() self.ss_missing = 1./flex.pow2(self.f_calc_missing.d_spacings().data()) / 4. mask_manager = mmtbx.masks.manager( miller_array = self.f_calc_missing, miller_array_twin = None, mask_params = None) self.f_mask_missing = mask_manager.shell_f_masks( xray_structure = self.xray_structure_cut, force_update = True) self.zero_data = flex.complex_double(self.f_calc_missing.data().size(), 0)
def chirality_deviations(self): if (self.n_chirality_proxies is not None): chirality_deltas = geometry_restraints.chirality_deltas( sites_cart=self.sites_cart, proxies=self.chirality_proxies) c_sq = chirality_deltas * chirality_deltas c_ave = math.sqrt(flex.mean_default(c_sq, 0)) c_max = math.sqrt(flex.max_default(c_sq, 0)) c_min = math.sqrt(flex.min_default(c_sq, 0)) return c_min, c_max, c_ave
def isotropic_adp_deviation(self): if (self.n_isotropic_adp_proxies is not None): isotropic_adp_deltas_rms = adp_restraints.isotropic_adp_deltas_rms( u_cart=self.u_cart, proxies=self.isotropic_adp_proxies) i_sq = isotropic_adp_deltas_rms * isotropic_adp_deltas_rms i_ave = math.sqrt(flex.mean_default(i_sq, 0)) i_max = math.sqrt(flex.max_default(i_sq, 0)) i_min = math.sqrt(flex.min_default(i_sq, 0)) return i_min, i_max, i_ave
def parallelity_deviations(self): if self.n_parallelity_proxies is not None: parallelity_deltas = geometry_restraints.parallelity_deltas( sites_cart=self.sites_cart, proxies=self.parallelity_proxies) p_sq = parallelity_deltas * parallelity_deltas p_ave = math.sqrt(flex.mean_default(p_sq, 0)) p_max = math.sqrt(flex.max_default(p_sq, 0)) p_min = math.sqrt(flex.min_default(p_sq, 0)) return p_min, p_max, p_ave
def _need_update_mask(self, sites_cart_new): if(self.sites_cart is not None and self.sites_cart.size() != sites_cart_new.size()): return True if(self.sites_cart is not None): atom_atom_distances = flex.sqrt((sites_cart_new - self.sites_cart).dot()) mean_shift = flex.mean_default(atom_atom_distances,0) if(mean_shift > self.mask_params.mean_shift_for_mask_update): return True else: return False else: return True
def chirality_deviations(self): if(self.n_chirality_proxies is not None): chirality_deltas = geometry_restraints.chirality_deltas( sites_cart = self.sites_cart, proxies = self.chirality_proxies) c_sq = chirality_deltas * chirality_deltas c_ave = math.sqrt(flex.mean_default(c_sq, 0)) c_max = math.sqrt(flex.max_default(c_sq, 0)) c_min = math.sqrt(flex.min_default(c_sq, 0)) return c_min, c_max, c_ave
def parallelity_deviations(self): if self.n_parallelity_proxies is not None: parallelity_deltas = geometry_restraints.parallelity_deltas( sites_cart = self.sites_cart, proxies = self.parallelity_proxies) p_sq = parallelity_deltas * parallelity_deltas p_ave = math.sqrt(flex.mean_default(p_sq, 0)) p_max = math.sqrt(flex.max_default(p_sq, 0)) p_min = math.sqrt(flex.min_default(p_sq, 0)) return p_min, p_max, p_ave
def dihedral_deviations(self): # !!!XXX!!! Warning! this works wrong because it is not aware of origin_id! if (self.n_dihedral_proxies is not None): dihedral_deltas = geometry_restraints.dihedral_deltas( sites_cart=self.sites_cart, proxies=self.dihedral_proxies) d_sq = dihedral_deltas * dihedral_deltas d_ave = math.sqrt(flex.mean_default(d_sq, 0)) d_max = math.sqrt(flex.max_default(d_sq, 0)) d_min = math.sqrt(flex.min_default(d_sq, 0)) return d_min, d_max, d_ave
def rigid_bond_deviation(self): if (self.n_rigid_bond_proxies is not None): rigid_bond_deltas = adp_restraints.rigid_bond_deltas( sites_cart=self.sites_cart, u_cart=self.u_cart, proxies=self.rigid_bond_proxies) r_sq = rigid_bond_deltas * rigid_bond_deltas r_ave = math.sqrt(flex.mean_default(r_sq, 0)) r_max = math.sqrt(flex.max_default(r_sq, 0)) r_min = math.sqrt(flex.min_default(r_sq, 0)) return r_min, r_max, r_ave
def planarity_deviations(self): # XXX Need update, does not respect origin_id # assert 0, "Not counting for origin_id" if (self.n_planarity_proxies is not None): planarity_deltas = geometry_restraints.planarity_deltas_rms( sites_cart=self.sites_cart, proxies=self.planarity_proxies) p_sq = planarity_deltas * planarity_deltas p_ave = math.sqrt(flex.mean_default(p_sq, 0)) p_max = math.sqrt(flex.max_default(p_sq, 0)) p_min = math.sqrt(flex.min_default(p_sq, 0)) return p_min, p_max, p_ave
def reference_dihedral_deviations(self): assert 0, "Not working" if (self.n_reference_dihedral_proxies is not None): reference_dihedral_deltas = geometry_restraints.reference_dihedral_deltas( sites_cart=self.sites_cart, proxies=self.reference_dihedral_proxies) d_sq = reference_dihedral_deltas * reference_dihedral_deltas d_ave = math.sqrt(flex.mean_default(d_sq, 0)) d_max = math.sqrt(flex.max_default(d_sq, 0)) d_min = math.sqrt(flex.min_default(d_sq, 0)) return d_min, d_max, d_ave
def dihedral_deviations(self): # !!!XXX!!! Warnign! this works wrong because it is not aware of origin_id! if(self.n_dihedral_proxies is not None): dihedral_deltas = geometry_restraints.dihedral_deltas( sites_cart = self.sites_cart, proxies = self.dihedral_proxies) d_sq = dihedral_deltas * dihedral_deltas d_ave = math.sqrt(flex.mean_default(d_sq, 0)) d_max = math.sqrt(flex.max_default(d_sq, 0)) d_min = math.sqrt(flex.min_default(d_sq, 0)) return d_min, d_max, d_ave
def dihedral_deviations(self): if (self.n_dihedral_proxies is not None): covalent_dihedrals = self.dihedral_proxies.proxy_select( origin_id=0) dihedral_deltas = geometry_restraints.dihedral_deltas( sites_cart=self.sites_cart, proxies=covalent_dihedrals) d_sq = dihedral_deltas * dihedral_deltas d_ave = math.sqrt(flex.mean_default(d_sq, 0)) d_max = math.sqrt(flex.max_default(d_sq, 0)) d_min = math.sqrt(flex.min_default(d_sq, 0)) return d_min, d_max, d_ave
def reference_dihedral_deviations(self): assert 0, "Not working" if(self.n_reference_dihedral_proxies is not None): reference_dihedral_deltas = geometry_restraints.reference_dihedral_deltas( sites_cart = self.sites_cart, proxies = self.reference_dihedral_proxies) d_sq = reference_dihedral_deltas * reference_dihedral_deltas d_ave = math.sqrt(flex.mean_default(d_sq, 0)) d_max = math.sqrt(flex.max_default(d_sq, 0)) d_min = math.sqrt(flex.min_default(d_sq, 0)) return d_min, d_max, d_ave
def angle_deviations(self): if (self.n_angle_proxies is not None): angle_deltas = self.angle_proxies.proxy_select(origin_id=0).deltas( sites_cart=self.sites_cart) if len(angle_deltas) > 0: a_sq = angle_deltas * angle_deltas a_ave = math.sqrt(flex.mean_default(a_sq, 0)) a_max = math.sqrt(flex.max_default(a_sq, 0)) a_min = math.sqrt(flex.min_default(a_sq, 0)) return a_min, a_max, a_ave else: return 0, 0, 0
def bond_deviations(self): if (self.n_bond_proxies is not None): bond_deltas = self.bond_proxies.deltas(sites_cart=self.sites_cart, origin_id=0) if len(bond_deltas) > 0: b_sq = bond_deltas * bond_deltas b_ave = math.sqrt(flex.mean_default(b_sq, 0)) b_max = math.sqrt(flex.max_default(b_sq, 0)) b_min = math.sqrt(flex.min_default(b_sq, 0)) return b_min, b_max, b_ave else: return 0, 0, 0
def planarity_deviations(self): # XXX Need update, does not respect origin_id # assert 0, "Not counting for origin_id" if(self.n_planarity_proxies is not None): planarity_deltas = geometry_restraints.planarity_deltas_rms( sites_cart = self.sites_cart, proxies = self.planarity_proxies) p_sq = planarity_deltas * planarity_deltas p_ave = math.sqrt(flex.mean_default(p_sq, 0)) p_max = math.sqrt(flex.max_default(p_sq, 0)) p_min = math.sqrt(flex.min_default(p_sq, 0)) return p_min, p_max, p_ave
def adp_similarity_deviation(self): if (self.n_adp_similarity_proxies is not None): adp_similarity_deltas_rms = adp_restraints.adp_similarity_deltas_rms( u_cart=self.u_cart, u_iso=self.u_iso, use_u_aniso=self.use_u_aniso, proxies=self.adp_similarity_proxies) a_sq = adp_similarity_deltas_rms * adp_similarity_deltas_rms a_ave = math.sqrt(flex.mean_default(a_sq, 0)) a_max = math.sqrt(flex.max_default(a_sq, 0)) a_min = math.sqrt(flex.min_default(a_sq, 0)) return a_min, a_max, a_ave
def angle_deviations(self): if(self.n_angle_proxies is not None): angle_deltas = self.angle_proxies.proxy_select(origin_id=0).deltas( sites_cart=self.sites_cart) if len(angle_deltas) > 0: a_sq = angle_deltas * angle_deltas a_ave = math.sqrt(flex.mean_default(a_sq, 0)) a_max = math.sqrt(flex.max_default(a_sq, 0)) a_min = math.sqrt(flex.min_default(a_sq, 0)) return a_min, a_max, a_ave else: return 0,0,0
def bond_deviations(self): if(self.n_bond_proxies is not None): bond_deltas = self.bond_proxies.deltas( sites_cart=self.sites_cart, origin_id=0) if len(bond_deltas) >0: b_sq = bond_deltas * bond_deltas b_ave = math.sqrt(flex.mean_default(b_sq, 0)) b_max = math.sqrt(flex.max_default(b_sq, 0)) b_min = math.sqrt(flex.min_default(b_sq, 0)) return b_min, b_max, b_ave else: return 0,0,0
def r_work_and_completeness_in_resolution_bins(fmodel, reflections_per_bin=500, out=None, prefix=""): if (out is None): out = sys.stdout from mmtbx import bulk_solvent from cctbx.array_family import flex fo_w = fmodel.f_obs_work() fc_w = fmodel.f_model_scaled_with_k1_w() reflections_per_bin = min(reflections_per_bin, fo_w.data().size()) fo_w.setup_binner(reflections_per_bin=reflections_per_bin) fc_w.use_binning_of(fo_w) result = [] for i_bin in fo_w.binner().range_used(): sel_w = fo_w.binner().selection(i_bin) sel_all = fo_w.binner().selection(i_bin) sel_fo_all = fo_w.select(sel_all) sel_fo_w = fo_w.select(sel_w) sel_fc_w = fc_w.select(sel_w) d_max_, d_min_ = sel_fo_all.d_max_min() completeness = sel_fo_all.completeness(d_max=d_max_) d_range = fo_w.binner().bin_legend(i_bin=i_bin, show_bin_number=False, show_counts=False) s_fo_w_d = sel_fo_w.data() s_fc_w_d = sel_fc_w.data() assert s_fo_w_d.size() == s_fc_w_d.size() s_fc_w_d_a = flex.abs(s_fc_w_d) if (s_fo_w_d.size() > 0): bin = resolution_bin( i_bin=i_bin, d_range=d_range, completeness=completeness, r_work=bulk_solvent.r_factor(s_fo_w_d, s_fc_w_d, 1), n_work=sel_fo_w.data().size(), scale_k1_work=_scale_helper(num=s_fo_w_d, den=s_fc_w_d_a), mean_f_obs=flex.mean_default(sel_fo_all.data(), None)) result.append(bin) print(prefix + " Bin Resolution Range Compl. Nwork Rwork Scale <Fobs>", file=out) for bin in result: fmt = " %s %s %s %s %s %s %s" print( prefix + fmt % (format_value("%3d", bin.i_bin), format_value( "%-17s", bin.d_range), format_value("%4.2f", bin.completeness), format_value("%8d", bin.n_work), format_value( "%6.4f", bin.r_work), format_value("%6.4f", bin.scale_k1_work), format_value("%10.3f", bin.mean_f_obs)), file=out)
def add_new_solvent(self): if (self.params.b_iso is None): sol_sel = self.model.solvent_selection() xrs_mac_h = self.model.get_xray_structure().select(~sol_sel) hd_mac = self.model.get_hd_selection().select(~sol_sel) xrs_mac = xrs_mac_h.select(~hd_mac) b = xrs_mac.extract_u_iso_or_u_equiv() * math.pi**2 * 8 b_solv = flex.mean_default(b, None) if (b_solv is not None and b_solv < self.params.b_iso_min or b_solv > self.params.b_iso_max): b_solv = (self.params.b_iso_min + self.params.b_iso_max) / 2. else: b_solv = self.params.b_iso if (self.params.new_solvent == "isotropic"): new_scatterers = flex.xray_scatterer( self.sites.size(), xray.scatterer(occupancy=self.params.occupancy, b=b_solv, scattering_type=self.params.scattering_type)) elif (self.params.new_solvent == "anisotropic"): u_star = adptbx.u_iso_as_u_star( self.model.get_xray_structure().unit_cell(), adptbx.b_as_u(b_solv)) new_scatterers = flex.xray_scatterer( self.sites.size(), xray.scatterer(occupancy=self.params.occupancy, u=u_star, scattering_type=self.params.scattering_type)) else: raise RuntimeError new_scatterers.set_sites(self.sites) solvent_xray_structure = xray.structure( special_position_settings=self.model.get_xray_structure(), scatterers=new_scatterers) xrs_sol = self.model.get_xray_structure().select( self.model.solvent_selection()) xrs_mac = self.model.get_xray_structure().select( ~self.model.solvent_selection()) xrs_sol = xrs_sol.concatenate(other=solvent_xray_structure) sol_sel = flex.bool(xrs_mac.scatterers().size(), False) sol_sel.extend(flex.bool(xrs_sol.scatterers().size(), True)) self.model.add_solvent( solvent_xray_structure=solvent_xray_structure, residue_name=self.params.output_residue_name, atom_name=self.params.output_atom_name, chain_id=self.params.output_chain_id, refine_occupancies=self.params.refine_occupancies, refine_adp=self.params.new_solvent) self.fmodel.update_xray_structure( xray_structure=self.model.get_xray_structure(), update_f_calc=True)
def add_new_solvent(self): if(self.params.b_iso is None): sol_sel = self.model.solvent_selection() xrs_mac_h = self.model.xray_structure.select(~sol_sel) hd_mac = self.model.xray_structure.hd_selection().select(~sol_sel) xrs_mac = xrs_mac_h.select(~hd_mac) b = xrs_mac.extract_u_iso_or_u_equiv() * math.pi**2*8 b_solv = flex.mean_default(b, None) if(b_solv is not None and b_solv < self.params.b_iso_min or b_solv > self.params.b_iso_max): b_solv = (self.params.b_iso_min + self.params.b_iso_max) / 2. else: b_solv = self.params.b_iso if(self.params.new_solvent == "isotropic"): new_scatterers = flex.xray_scatterer( self.sites.size(), xray.scatterer(occupancy = self.params.occupancy, b = b_solv, scattering_type = self.params.scattering_type)) elif(self.params.new_solvent == "anisotropic"): u_star = adptbx.u_iso_as_u_star(self.model.xray_structure.unit_cell(), adptbx.b_as_u(b_solv)) new_scatterers = flex.xray_scatterer( self.sites.size(), xray.scatterer( occupancy = self.params.occupancy, u = u_star, scattering_type = self.params.scattering_type)) else: raise RuntimeError new_scatterers.set_sites(self.sites) solvent_xray_structure = xray.structure( special_position_settings = self.model.xray_structure, scatterers = new_scatterers) xrs_sol = self.model.xray_structure.select(self.model.solvent_selection()) xrs_mac = self.model.xray_structure.select(~self.model.solvent_selection()) xrs_sol = xrs_sol.concatenate(other = solvent_xray_structure) sol_sel = flex.bool(xrs_mac.scatterers().size(), False) sol_sel.extend( flex.bool(xrs_sol.scatterers().size(), True) ) self.model.add_solvent( solvent_xray_structure = solvent_xray_structure, residue_name = self.params.output_residue_name, atom_name = self.params.output_atom_name, chain_id = self.params.output_chain_id, refine_occupancies = self.params.refine_occupancies, refine_adp = self.params.new_solvent) self.fmodel.update_xray_structure( xray_structure = self.model.xray_structure, update_f_calc = True)
def get_mean_side_chain_density_value_residue(residue, map_data, unit_cell): results = flex.double() get_class = iotbx.pdb.common_residue_names_get_class main_chain = ["N", "CA", "CB", "C", "O"] if (get_class(residue.resname) != "common_amino_acid"): return None sites_cart = flex.vec3_double() atoms = residue.atoms() for a in atoms: if (a.name.strip() in main_chain): continue sites_cart.append(a.xyz) if (sites_cart.size() == 0): return None mv = maptbx.real_space_target_simple(unit_cell=unit_cell, density_map=map_data, sites_cart=sites_cart) results.append(mv) return flex.mean_default(results, 0)
def r_work_and_completeness_in_resolution_bins(fmodel, reflections_per_bin=500, out = None, prefix=""): if(out is None): out = sys.stdout from mmtbx import bulk_solvent from cctbx.array_family import flex fo_w = fmodel.f_obs_work() fc_w = fmodel.f_model_scaled_with_k1_w() reflections_per_bin = min(reflections_per_bin, fo_w.data().size()) fo_w.setup_binner(reflections_per_bin = reflections_per_bin) fc_w.use_binning_of(fo_w) result = [] for i_bin in fo_w.binner().range_used(): sel_w = fo_w.binner().selection(i_bin) sel_all = fo_w.binner().selection(i_bin) sel_fo_all = fo_w.select(sel_all) sel_fo_w = fo_w.select(sel_w) sel_fc_w = fc_w.select(sel_w) d_max_,d_min_ = sel_fo_all.d_max_min() completeness = sel_fo_all.completeness(d_max = d_max_) d_range = fo_w.binner().bin_legend( i_bin=i_bin, show_bin_number=False, show_counts=False) s_fo_w_d = sel_fo_w.data() s_fc_w_d = sel_fc_w.data() assert s_fo_w_d.size() == s_fc_w_d.size() s_fc_w_d_a = flex.abs(s_fc_w_d) if(s_fo_w_d.size() > 0): bin = resolution_bin( i_bin = i_bin, d_range = d_range, completeness = completeness, r_work = bulk_solvent.r_factor(s_fo_w_d, s_fc_w_d, 1), n_work = sel_fo_w.data().size(), scale_k1_work= _scale_helper(num=s_fo_w_d, den=s_fc_w_d_a), mean_f_obs = flex.mean_default(sel_fo_all.data(),None)) result.append(bin) print >> out,\ prefix+" Bin Resolution Range Compl. Nwork Rwork Scale <Fobs>" for bin in result: fmt = " %s %s %s %s %s %s %s" print >> out,prefix+fmt%( format_value("%3d", bin.i_bin), format_value("%-17s", bin.d_range), format_value("%4.2f", bin.completeness), format_value("%8d", bin.n_work), format_value("%6.4f", bin.r_work), format_value("%6.4f", bin.scale_k1_work), format_value("%10.3f", bin.mean_f_obs))
def show(self, message): print >> self.log, message sol_sel = self.model.solvent_selection() xrs_mac_h = self.model.xray_structure.select(~sol_sel) xrs_sol_h = self.model.xray_structure.select(sol_sel) hd_sol = self.model.xray_structure.hd_selection().select(sol_sel) hd_mac = self.model.xray_structure.hd_selection().select(~sol_sel) xrs_sol = xrs_sol_h.select(~hd_sol) xrs_mac = xrs_mac_h.select(~hd_mac) scat = xrs_sol.scatterers() occ = scat.extract_occupancies() b_isos = scat.extract_u_iso_or_u_equiv( self.model.xray_structure.unit_cell()) * math.pi**2*8 smallest_distances = xrs_mac.closest_distances( sites_frac = xrs_sol.sites_frac(), distance_cutoff = self.find_peaks_params.map_next_to_model.\ max_model_peak_dist).smallest_distances number = format_value("%-7d",scat.size()) b_min = format_value("%-7.2f", flex.min_default( b_isos, None)) b_max = format_value("%-7.2f", flex.max_default( b_isos, None)) b_ave = format_value("%-7.2f", flex.mean_default(b_isos, None)) bl_min = format_value("%-7.2f", self.params.b_iso_min).strip() bl_max = format_value("%-7.2f", self.params.b_iso_max).strip() o_min = format_value("%-7.2f", flex.min_default(occ, None)) o_max = format_value("%-7.2f", flex.max_default(occ, None)) ol_min = format_value("%-7.2f", self.params.occupancy_min).strip() ol_max = format_value("%-7.2f", self.params.occupancy_max).strip() d_min = format_value("%-7.2f", flex.min_default(smallest_distances, None)) d_max = format_value("%-7.2f", flex.max_default(smallest_distances, None)) dl_min = format_value("%-7.2f", self.find_peaks_params.map_next_to_model.min_model_peak_dist).strip() dl_max = format_value("%-7.2f", self.find_peaks_params.map_next_to_model.max_model_peak_dist).strip() ani_min = format_value("%-7.2f", flex.min_default(scat.anisotropy( unit_cell = xrs_sol.unit_cell()), None)) ani_min_l = format_value("%-7.2f",self.params.anisotropy_min).strip() print >>self.log," number = %s"%number print >>self.log," b_iso_min = %s (limit = %s)"%(b_min, bl_min) print >>self.log," b_iso_max = %s (limit = %s)"%(b_max, bl_max) print >>self.log," b_iso_mean = %s "%(b_ave) print >>self.log," anisotropy_min = %s (limit = %s)"%(ani_min,ani_min_l) print >>self.log," occupancy_min = %s (limit = %s)"%(o_min, ol_min) print >>self.log," occupancy_max = %s (limit = %s)"%(o_max, ol_max) print >>self.log," dist_sol_mol_min = %s (limit = %s)"%(d_min, dl_min) print >>self.log," dist_sol_mol_max = %s (limit = %s)"%(d_max, dl_max)
def load_refinement(self, ref, stats): pdb_file = ref.replace('.dat', '.pdb') pdb_io = pdb.input(file_name=pdb_file) m_stats = self._stats() self.model_stats = { "r_work" : stats['r'], "r_free" : stats['rfree'], "adp_mean_all" : flex.mean_default(pdb_io.xray_structure_simple().extract_u_iso_or_u_equiv()*(math.pi**2*8), None), "bond_rmsd" : m_stats[1][0], "angle_rmsd" : m_stats[1][1], "clashscore" : m_stats[0] } self.hist_data = polygon.output.get_basic_histogram_data(d_min=stats['res']) self.renderer = pgn.wx_renderer(self.hist_data, self.model_stats) self.polygon_panel = P8PolygonPanel(self, self.renderer) self.polygon_panel.set_callback(self._on_click) self.histogram = polygon_db_viewer.HistogramPlot(self, figure_size=(5,4), font_size=6, title_font_size=6) col = wx.SystemSettings.GetColour(wx.SYS_COLOUR_BACKGROUND) self.histogram.figure.patch.set_facecolor((col[0]/255.0, col[1]/255.0, col[2]/255.0)) self.histogram.show_histogram(data=self.hist_data[0][1], n_bins=10, reference_value=self.model_stats['r_work'], xlabel='r_work') self.pg_sizer = wx.BoxSizer(wx.VERTICAL) self.pg_sizer.Add(self.histogram, 1, wx.ALL, 10) self.pg_sizer.Add(self.draw_color_key()) self.sizer.Add(self.pg_sizer, 1, wx.EXPAND) self.sizer.Add(self.polygon_panel, 2, wx.EXPAND, wx.ALL, 10) self.sizer.Layout()
def get_mean_side_chain_density_value(hierarchy, map_data, unit_cell): results = flex.double() get_class = iotbx.pdb.common_residue_names_get_class main_chain = ["N", "CA", "CB", "C", "O"] for model in hierarchy.models(): for chain in model.chains(): for residue_group in chain.residue_groups(): conformers = residue_group.conformers() if (len(conformers) > 1): continue for conformer in residue_group.conformers(): residue = conformer.only_residue() if (get_class(residue.resname) != "common_amino_acid"): continue sites_cart = flex.vec3_double() atoms = residue.atoms() for a in atoms: if (a.name.strip() in main_chain): continue sites_cart.append(a.xyz) if (sites_cart.size() == 0): continue mv = maptbx.real_space_target_simple(unit_cell=unit_cell, density_map=map_data, sites_cart=sites_cart) results.append(mv) return flex.mean_default(results, 0)
def prepare_target_map(self): # XXX This may need to go external if (self.map_data is None): return None map_data = self.map_data # truncate map selection = self.atom_selection_cache.selection( string="element C or element O or element N") mean_atom = flex.double() for i_a, a in enumerate(list(self.pdb_hierarchy.atoms())): if (selection[i_a]): site_frac = self.crystal_symmetry.unit_cell().fractionalize( a.xyz) mean_atom.append(map_data.eight_point_interpolation(site_frac)) mean_atom = flex.mean_default(mean_atom, 0) map_data = map_data.set_selected(map_data > mean_atom, mean_atom) # Blend maps if applicable if (self.diff_map_data is not None): diff_map_data = self.diff_map_data.deep_copy() sel = diff_map_data < 2. diff_map_data = diff_map_data.set_selected(sel, 0) sel = diff_map_data > 3. diff_map_data = diff_map_data.set_selected(sel, 3) diff_map_data = diff_map_data / 3. maptbx.combine_1(map_data=map_data, diff_map=diff_map_data) return map_data
def collect(self, model, fmodel, step, wilson_b = None, rigid_body_shift_accumulator = None): global time_collect_and_process t1 = time.time() if(self.sites_cart_start is None): self.sites_cart_start = model.xray_structure.sites_cart() sites_cart_curr = model.xray_structure.sites_cart() if(sites_cart_curr.size()==self.sites_cart_start.size()): self.shifts.append( flex.mean(flex.sqrt((self.sites_cart_start-sites_cart_curr).dot()))) else: self.shifts.append("n/a") if(wilson_b is not None): self.wilson_b = wilson_b self.steps.append(step) self.r_works.append(fmodel.r_work()) self.r_frees.append(fmodel.r_free()) use_amber = False if hasattr(self.params, "amber"): # loaded amber scope use_amber = self.params.amber.use_amber self.is_amber_monitor=use_amber ignore_hd = True if self.neutron_refinement or use_amber: ignore_hd = False use_afitt = False if hasattr(self.params, "afitt"): # loaded amber scope use_afitt = self.params.afitt.use_afitt general_selection = None if use_afitt: from mmtbx.geometry_restraints import afitt if 0: general_selection = afitt.get_afitt_selection(model.restraints_manager, model.xray_structure, ignore_hd) afitt_geom = model.geometry_statistics( ignore_hd = ignore_hd, cdl_restraints=self.params.pdb_interpretation.restraints_library.cdl, general_selection=general_selection, ) afitt_geom.show() general_selection = afitt.get_non_afitt_selection( model.restraints_manager, model.xray_structure.sites_cart(), model.xray_structure.hd_selection(), ignore_hd) geom = model.geometry_statistics( ignore_hd = ignore_hd, cdl_restraints=self.params.pdb_interpretation.restraints_library.cdl, general_selection=general_selection, ) if(geom is not None): self.geom.append(geom) hd_sel = None if(not self.neutron_refinement and not self.is_neutron_monitor): hd_sel = model.xray_structure.hd_selection() b_isos = model.xray_structure.extract_u_iso_or_u_equiv() * math.pi**2*8 if(hd_sel is not None): b_isos = b_isos.select(~hd_sel) self.bs_iso_max_a.append(flex.max_default( b_isos, 0)) self.bs_iso_min_a.append(flex.min_default( b_isos, 0)) self.bs_iso_ave_a.append(flex.mean_default(b_isos, 0)) self.n_solv.append(model.number_of_ordered_solvent_molecules()) if (len(self.geom)>0 and getattr(self.geom[0], "b", None) and getattr(self.geom[0], "a", None) ): if([self.bond_start,self.angle_start].count(None) == 2): if(len(self.geom)>0): self.bond_start = self.geom[0].b[2] self.angle_start = self.geom[0].a[2] if(len(self.geom)>0): self.bond_final = self.geom[len(self.geom)-1].b[2] self.angle_final = self.geom[len(self.geom)-1].a[2] elif(len(self.geom)==1): self.bond_final = self.geom[0].b[2] self.angle_final = self.geom[0].a[2] if(rigid_body_shift_accumulator is not None): self.rigid_body_shift_accumulator = rigid_body_shift_accumulator t2 = time.time() time_collect_and_process += (t2 - t1) self.call_back(model, fmodel, method=step)
def __init__(self, map_data, xray_structure, pdb_hierarchy, geometry_restraints_manager, gradients_method="fd", ncs_groups=None, rms_bonds_limit=0.015, rms_angles_limit=2.0, real_space_gradients_delta=1. / 4, max_iterations=100, range_size=10, n_ranges=10, default_weight=50): """ Fast determination of optimal data/restraints weight for real-space refinement of individual sites. """ self.msg_strings = [] # split chains into chunks result = [] for model in pdb_hierarchy.models(): for chain in model.chains(): if (chain.is_protein() or chain.is_na()): residue_range_sel = flex.size_t() cntr = 0 for rg in chain.residue_groups(): i_seqs = rg.atoms().extract_i_seq() cntr += 1 if (cntr < 10): residue_range_sel.extend(i_seqs) else: result.append(residue_range_sel) residue_range_sel = flex.size_t() residue_range_sel.extend(i_seqs) cntr = 0 if (len(result) == 0): assert residue_range_sel.size() > 0 result.append(residue_range_sel) self.msg_strings.append("number of chunks: %d" % len(result)) # randomly pick chunks random_chunks = [] if (len(result) > 0): for i in xrange(n_ranges): random_chunks.append(random.choice(xrange(len(result)))) self.msg_strings.append("random chunks:" % random_chunks) # setup refinery xrs_dc = xray_structure.deep_copy_scatterers() sel_all = flex.bool(xrs_dc.scatterers().size(), True) grm_dc = geometry_restraints_manager.select(sel_all) ro = mmtbx.refinement.real_space.individual_sites.box_refinement_manager( xray_structure=xrs_dc, target_map=map_data, geometry_restraints_manager=grm_dc.geometry, real_space_gradients_delta=real_space_gradients_delta, max_iterations=max_iterations, ncs_groups=ncs_groups, gradients_method=gradients_method) optimal_weights = flex.double() # loop over chunks: determine best weight for each chunk if (len(result) == 0): random_chunks = [None] for chunk in random_chunks: if (chunk is None): sel = flex.bool(xrs_dc.scatterers().size(), True) else: sel = result[chunk] sel = flex.bool(xrs_dc.scatterers().size(), sel) ro.refine(selection=sel, rms_bonds_limit=rms_bonds_limit, rms_angles_limit=rms_angles_limit) self.msg_strings.append("chunk %s optimal weight: %9.4f" % (str(chunk), ro.weight_optimal)) if (ro.weight_optimal is not None): optimal_weights.append(ro.weight_optimal) # select overall best weight mean = flex.mean(optimal_weights) sel = optimal_weights < mean * 3 sel &= optimal_weights > mean / 3 if (sel.count(True) > 0): optimal_weights = optimal_weights.select(sel) self.weight = flex.mean_default(optimal_weights, default_weight) self.msg_strings.append("overall best weight: %9.4f" % self.weight)
def mmmd(self, values, eps = None): if(eps is not None): values = values * eps return flex.min_default(values, None), \ flex.max_default(values, None), \ flex.mean_default(values, None)
def ensemble_mean_geometry_stats(self, restraints_manager, xray_structure, ensemble_xray_structures, ignore_hd = True, verbose = False, out = None, return_pdb_string = False): if (out is None): out = sys.stdout if verbose: utils.print_header("Ensemble mean geometry statistics", out = out) ensemble_size = len(ensemble_xray_structures) print >> out, "Ensemble size : ", ensemble_size # Dictionaries to store deltas ensemble_bond_deltas = {} ensemble_angle_deltas = {} ensemble_chirality_deltas = {} ensemble_planarity_deltas = {} ensemble_dihedral_deltas = {} # List to store rmsd of each model structures_bond_rmsd = flex.double() structures_angle_rmsd = flex.double() structures_chirality_rmsd = flex.double() structures_planarity_rmsd = flex.double() structures_dihedral_rmsd = flex.double() # Remove water and hd atoms from global restraints manager selection = flex.bool() for sc in xray_structure.scatterers(): if sc.label.find('HOH') > -1: selection.append(True) else: selection.append(False) if ignore_hd: hd_selection = xray_structure.hd_selection() assert hd_selection.size() == selection.size() for n in xrange(hd_selection.size()): if hd_selection[n] or selection[n]: selection[n] = True restraints_manager = restraints_manager.select(selection = ~selection) # Get all deltas for n, structure in enumerate(ensemble_xray_structures): if verbose: print >> out, "\nModel : ", n+1 sites_cart = structure.sites_cart() # Remove water and hd atoms from individual structures sites cart selection = flex.bool() for sc in structure.scatterers(): if sc.label.find('HOH') > -1: selection.append(True) else: selection.append(False) if ignore_hd: hd_selection = structure.hd_selection() assert hd_selection.size() == selection.size() for n in xrange(hd_selection.size()): if hd_selection[n] or selection[n]: selection[n] = True sites_cart = sites_cart.select(~selection) assert sites_cart is not None site_labels = None energies_sites = restraints_manager.energies_sites( sites_cart = sites_cart, compute_gradients = False) # Rmsd of individual model bond_rmsd = energies_sites.geometry.bond_deviations()[2] angle_rmsd = energies_sites.geometry.angle_deviations()[2] chirality_rmsd = energies_sites.geometry.chirality_deviations()[2] planarity_rmsd = energies_sites.geometry.planarity_deviations()[2] dihedral_rmsd = energies_sites.geometry.dihedral_deviations()[2] structures_bond_rmsd.append(bond_rmsd) structures_angle_rmsd.append(angle_rmsd) structures_chirality_rmsd.append(chirality_rmsd) structures_planarity_rmsd.append(planarity_rmsd) structures_dihedral_rmsd.append(dihedral_rmsd) if verbose: print >> out, " Model RMSD" print >> out, " bond : %.6g" % bond_rmsd print >> out, " angle : %.6g" % angle_rmsd print >> out, " chirality : %.6g" % chirality_rmsd print >> out, " planarity : %.6g" % planarity_rmsd print >> out, " dihedral : %.6g" % dihedral_rmsd # Bond pair_proxies = restraints_manager.geometry.pair_proxies(flags=None, sites_cart=sites_cart) assert pair_proxies is not None if verbose: pair_proxies.bond_proxies.show_histogram_of_deltas( sites_cart = sites_cart, n_slots = 10, f = out) for proxy in pair_proxies.bond_proxies.simple: bond_simple_proxy = geometry_restraints.bond( sites_cart = sites_cart, proxy = proxy) if proxy.i_seqs in ensemble_bond_deltas: ensemble_bond_deltas[proxy.i_seqs][0]+=bond_simple_proxy.delta ensemble_bond_deltas[proxy.i_seqs][1]+=1 else: ensemble_bond_deltas[proxy.i_seqs] = [bond_simple_proxy.delta, 1] if verbose: print >> out, "bond simple :", proxy.i_seqs print >> out, " distance_ideal : %.6g" % proxy.distance_ideal print >> out, " distance_model : %.6g" % bond_simple_proxy.distance_model print >> out, " detla : %.6g" % bond_simple_proxy.delta if (pair_proxies.bond_proxies.asu.size() > 0): asu_mappings = pair_proxies.bond_proxies.asu_mappings() for proxy in pair_proxies.bond_proxies.asu: rt_mx = asu_mappings.get_rt_mx_ji(pair=proxy) bond_asu_proxy = geometry_restraints.bond( sites_cart = sites_cart, asu_mappings = asu_mappings, proxy = proxy) proxy_i_seqs = (proxy.i_seq, proxy.j_seq) if proxy_i_seqs in ensemble_bond_deltas: ensemble_bond_deltas[proxy_i_seqs][0]+=bond_asu_proxy.delta ensemble_bond_deltas[proxy_i_seqs][1]+=1 else: ensemble_bond_deltas[proxy_i_seqs] = [bond_asu_proxy.delta, 1] if verbose: print >> out, "bond asu :", (proxy.i_seq, proxy.j_seq), rt_mx print >> out, " distance_ideal : %.6g" % proxy.distance_ideal print >> out, " distance_model : %.6g" % bond_asu_proxy.distance_model print >> out, " delta : %.6g" % bond_asu_proxy.delta # Angle if verbose: restraints_manager.geometry.angle_proxies.show_histogram_of_deltas( sites_cart = sites_cart, n_slots = 10, f = out) for proxy in restraints_manager.geometry.angle_proxies: angle_proxy = geometry_restraints.angle( sites_cart = sites_cart, proxy = proxy) if proxy.i_seqs in ensemble_angle_deltas: ensemble_angle_deltas[proxy.i_seqs][0]+=angle_proxy.delta ensemble_angle_deltas[proxy.i_seqs][1]+=1 else: ensemble_angle_deltas[proxy.i_seqs] = [angle_proxy.delta, 1] if verbose: print >> out, "angle : ", proxy.i_seqs print >> out, " angle_ideal : %.6g" % proxy.angle_ideal print >> out, " angle_model : %.6g" % angle_proxy.angle_model print >> out, " delta : %.6g" % angle_proxy.delta # Chirality if verbose: restraints_manager.geometry.chirality_proxies.show_histogram_of_deltas( sites_cart = sites_cart, n_slots = 10, f = out) for proxy in restraints_manager.geometry.chirality_proxies: chirality_proxy = geometry_restraints.chirality( sites_cart = sites_cart, proxy = proxy) if proxy.i_seqs in ensemble_chirality_deltas: ensemble_chirality_deltas[proxy.i_seqs][0]+=chirality_proxy.delta ensemble_chirality_deltas[proxy.i_seqs][1]+=1 else: ensemble_chirality_deltas[proxy.i_seqs] = [chirality_proxy.delta, 1] if verbose: print >> out, "chirality : ", proxy.i_seqs print >> out, " chirality_ideal : %.6g" % proxy.volume_ideal print >> out, " chirality_model : %.6g" % chirality_proxy.volume_model print >> out, " chirality : %.6g" % chirality_proxy.delta # Planarity for proxy in restraints_manager.geometry.planarity_proxies: planarity_proxy = geometry_restraints.planarity( sites_cart = sites_cart, proxy = proxy) proxy_i_seqs = [] for i_seq in proxy.i_seqs: proxy_i_seqs.append(i_seq) proxy_i_seqs = tuple(proxy_i_seqs) if proxy_i_seqs in ensemble_planarity_deltas: ensemble_planarity_deltas[proxy_i_seqs][0]+=planarity_proxy.rms_deltas() ensemble_planarity_deltas[proxy_i_seqs][1]+=1 else: ensemble_planarity_deltas[proxy_i_seqs] = [planarity_proxy.rms_deltas(), 1] if verbose: print >> out, "planarity : ", proxy_i_seqs print >> out, " planarity rms_deltas : %.6g" % planarity_proxy.rms_deltas() # Dihedral if verbose: restraints_manager.geometry.dihedral_proxies.show_histogram_of_deltas( sites_cart = sites_cart, n_slots = 10, f = out) for proxy in restraints_manager.geometry.dihedral_proxies: dihedral_proxy = geometry_restraints.dihedral( sites_cart = sites_cart, proxy = proxy) if proxy.i_seqs in ensemble_dihedral_deltas: ensemble_dihedral_deltas[proxy.i_seqs][0]+=dihedral_proxy.delta ensemble_dihedral_deltas[proxy.i_seqs][1]+=1 else: ensemble_dihedral_deltas[proxy.i_seqs] = [dihedral_proxy.delta, 1] if verbose: print >> out, "dihedral : ", proxy.i_seqs print >> out, " dihedral_ideal : %.6g" % proxy.angle_ideal print >> out, " periodicity : %.6g" % proxy.periodicity print >> out, " dihedral_model : %.6g" % dihedral_proxy.angle_model print >> out, " delta : %.6g" % dihedral_proxy.delta # Calculate RMSDs for ensemble model # Bond mean_bond_delta = flex.double() for proxy, info in ensemble_bond_deltas.iteritems(): assert info[1] == ensemble_size mean_delta = info[0] / info[1] mean_bond_delta.append(mean_delta) bond_delta_sq = mean_bond_delta * mean_bond_delta ensemble_bond_rmsd = math.sqrt(flex.mean_default(bond_delta_sq, 0)) # Angle mean_angle_delta = flex.double() for proxy, info in ensemble_angle_deltas.iteritems(): assert info[1] == ensemble_size mean_delta = info[0] / info[1] mean_angle_delta.append(mean_delta) angle_delta_sq = mean_angle_delta * mean_angle_delta ensemble_angle_rmsd = math.sqrt(flex.mean_default(angle_delta_sq, 0)) # Chirality mean_chirality_delta = flex.double() for proxy, info in ensemble_chirality_deltas.iteritems(): assert info[1] == ensemble_size mean_delta = info[0] / info[1] mean_chirality_delta.append(mean_delta) chirality_delta_sq = mean_chirality_delta * mean_chirality_delta ensemble_chirality_rmsd = math.sqrt(flex.mean_default(chirality_delta_sq, 0)) # Planarity mean_planarity_delta = flex.double() for proxy, info in ensemble_planarity_deltas.iteritems(): assert info[1] == ensemble_size mean_delta = info[0] / info[1] mean_planarity_delta.append(mean_delta) planarity_delta_sq = mean_planarity_delta * mean_planarity_delta ensemble_planarity_rmsd = math.sqrt(flex.mean_default(planarity_delta_sq, 0)) # Dihedral mean_dihedral_delta = flex.double() for proxy, info in ensemble_dihedral_deltas.iteritems(): assert info[1] == ensemble_size mean_delta = info[0] / info[1] mean_dihedral_delta.append(mean_delta) dihedral_delta_sq = mean_dihedral_delta * mean_dihedral_delta ensemble_dihedral_rmsd = math.sqrt(flex.mean_default(dihedral_delta_sq, 0)) # Calculate <structure rmsd> assert ensemble_size == structures_bond_rmsd assert ensemble_size == structures_angle_rmsd assert ensemble_size == structures_chirality_rmsd assert ensemble_size == structures_planarity_rmsd assert ensemble_size == structures_dihedral_rmsd structure_bond_rmsd_mean = structures_bond_rmsd.min_max_mean().mean structure_angle_rmsd_mean = structures_angle_rmsd.min_max_mean().mean structure_chirality_rmsd_mean = structures_chirality_rmsd.min_max_mean().mean structure_planarity_rmsd_mean = structures_planarity_rmsd.min_max_mean().mean structure_dihedral_rmsd_mean = structures_dihedral_rmsd.min_max_mean().mean # Show summary utils.print_header("Ensemble RMSD summary", out = out) print >> out, " RMSD (mean delta per restraint)" print >> out, " bond : %.6g" % ensemble_bond_rmsd print >> out, " angle : %.6g" % ensemble_angle_rmsd print >> out, " chirality : %.6g" % ensemble_chirality_rmsd print >> out, " planarity : %.6g" % ensemble_planarity_rmsd print >> out, " dihedral : %.6g" % ensemble_dihedral_rmsd print >> out, " RMSD (mean RMSD per structure)" print >> out, " bond : %.6g" % structure_bond_rmsd_mean print >> out, " angle : %.6g" % structure_angle_rmsd_mean print >> out, " chirality : %.6g" % structure_chirality_rmsd_mean print >> out, " planarity : %.6g" % structure_planarity_rmsd_mean print >> out, " dihedral : %.6g" % structure_dihedral_rmsd_mean if ignore_hd: print >> out, "\n Calculated excluding H/D" else: print >> out, "\n Calculated including H/D" if return_pdb_string: ens_geo_pdb_string = "REMARK 3" ens_geo_pdb_string += "\nREMARK 3 NUMBER STRUCTURES IN ENSEMBLE : {0:5d}".format(ensemble_size) if ignore_hd: ens_geo_pdb_string += "\nREMARK 3 RMS DEVIATIONS FROM IDEAL VALUES (EXCLUDING H/D)" else: ens_geo_pdb_string += "\nREMARK 3 RMS DEVIATIONS FROM IDEAL VALUES (INCLUDING H/D)" ens_geo_pdb_string += "\nREMARK 3 RMSD (MEAN DELTA PER RESTRAINT)" ens_geo_pdb_string += "\nREMARK 3 BOND : {0:5.3f}".format(ensemble_bond_rmsd) ens_geo_pdb_string += "\nREMARK 3 ANGLE : {0:5.3f}".format(ensemble_angle_rmsd) ens_geo_pdb_string += "\nREMARK 3 CHIRALITY : {0:5.3f}".format(ensemble_chirality_rmsd) ens_geo_pdb_string += "\nREMARK 3 PLANARITY : {0:5.3f}".format(ensemble_planarity_rmsd) ens_geo_pdb_string += "\nREMARK 3 DIHEDRAL : {0:5.2f}".format(ensemble_dihedral_rmsd) ens_geo_pdb_string += "\nREMARK 3 RMSD (MEAN RMSD PER STRUCTURE)" ens_geo_pdb_string += "\nREMARK 3 BOND : {0:5.3f}".format(structure_bond_rmsd_mean) ens_geo_pdb_string += "\nREMARK 3 ANGLE : {0:5.3f}".format(structure_angle_rmsd_mean) ens_geo_pdb_string += "\nREMARK 3 CHIRALITY : {0:5.3f}".format(structure_chirality_rmsd_mean) ens_geo_pdb_string += "\nREMARK 3 PLANARITY : {0:5.3f}".format(structure_planarity_rmsd_mean) ens_geo_pdb_string += "\nREMARK 3 DIHEDRAL : {0:5.2f}".format(structure_dihedral_rmsd_mean) ens_geo_pdb_string += "\nREMARK 3" return ens_geo_pdb_string
def __init__( self, map_data, xray_structure, pdb_hierarchy, geometry_restraints_manager, ncs_groups=None, rms_bonds_limit=0.015, rms_angles_limit=2.0, real_space_gradients_delta=1./4, max_iterations = 100, range_size=10, n_ranges=10, default_weight=50): """ Fast determination of optimal data/restraints weight for real-space refinement of individual sites. """ self.msg_strings = [] # split chains into chunks result = [] for model in pdb_hierarchy.models(): for chain in model.chains(): if(chain.is_protein() or chain.is_na()): residue_range_sel = flex.size_t() cntr = 0 for rg in chain.residue_groups(): i_seqs = rg.atoms().extract_i_seq() cntr += 1 if(cntr<10): residue_range_sel.extend(i_seqs) else: result.append(residue_range_sel) residue_range_sel = flex.size_t() residue_range_sel.extend(i_seqs) cntr = 0 if(len(result)==0): assert residue_range_sel.size()>0 result.append(residue_range_sel) self.msg_strings.append("number of chunks: %d"%len(result)) # randomly pick chunks random_chunks = [] if(len(result)>0): for i in xrange(n_ranges): random_chunks.append(random.choice(xrange(len(result)))) self.msg_strings.append("random chunks:"%random_chunks) # setup refinery xrs_dc = xray_structure.deep_copy_scatterers() sel_all = flex.bool(xrs_dc.scatterers().size(), True) grm_dc = geometry_restraints_manager.select(sel_all) ro = mmtbx.refinement.real_space.individual_sites.box_refinement_manager( xray_structure = xrs_dc, target_map = map_data, geometry_restraints_manager = grm_dc.geometry, real_space_gradients_delta = real_space_gradients_delta, max_iterations = max_iterations, ncs_groups = ncs_groups) optimal_weights = flex.double() # loop over chunks: determine best weight for each chunk if(len(result)==0): random_chunks = [None] for chunk in random_chunks: if(chunk is None): sel = flex.bool(xrs_dc.scatterers().size(), True) else: sel = result[chunk] sel = flex.bool(xrs_dc.scatterers().size(), sel) ro.refine( selection = sel, rms_bonds_limit = rms_bonds_limit, rms_angles_limit = rms_angles_limit) self.msg_strings.append("chunk %s optimal weight: %9.4f"%( str(chunk), ro.weight_optimal)) if(ro.weight_optimal is not None): optimal_weights.append(ro.weight_optimal) # select overall best weight mean = flex.mean(optimal_weights) sel = optimal_weights < mean*3 sel &= optimal_weights > mean/3 if(sel.count(True)>0): optimal_weights = optimal_weights.select(sel) self.weight = flex.mean_default(optimal_weights, default_weight) self.msg_strings.append("overall best weight: %9.4f"%self.weight)
def collect(self, model, fmodel, step, wilson_b=None, rigid_body_shift_accumulator=None): global time_collect_and_process t1 = time.time() if (self.sites_cart_start is None): self.sites_cart_start = model.xray_structure.sites_cart() sites_cart_curr = model.xray_structure.sites_cart() if (sites_cart_curr.size() == self.sites_cart_start.size()): self.shifts.append( flex.mean( flex.sqrt( (self.sites_cart_start - sites_cart_curr).dot()))) else: self.shifts.append("n/a") if (wilson_b is not None): self.wilson_b = wilson_b self.steps.append(step) self.r_works.append(fmodel.r_work()) self.r_frees.append(fmodel.r_free()) use_amber = False if hasattr(self.params, "amber"): # loaded amber scope use_amber = self.params.amber.use_amber self.is_amber_monitor = use_amber ignore_hd = True if self.neutron_refinement or use_amber: ignore_hd = False use_afitt = False if hasattr(self.params, "afitt"): # loaded amber scope use_afitt = self.params.afitt.use_afitt general_selection = None if use_afitt: from mmtbx.geometry_restraints import afitt if 0: general_selection = afitt.get_afitt_selection( model.restraints_manager, model.xray_structure, ignore_hd) afitt_geom = model.geometry_statistics( ignore_hd=ignore_hd, cdl_restraints=self.params.pdb_interpretation. restraints_library.cdl, general_selection=general_selection, ) afitt_geom.show() general_selection = afitt.get_non_afitt_selection( model.restraints_manager, model.xray_structure.sites_cart(), model.xray_structure.hd_selection(), ignore_hd) geom = model.geometry_statistics( ignore_hd=ignore_hd, cdl_restraints=self.params.pdb_interpretation.restraints_library. cdl, general_selection=general_selection, ) if (geom is not None): self.geom.append(geom) hd_sel = None if (not self.neutron_refinement and not self.is_neutron_monitor): hd_sel = model.xray_structure.hd_selection() b_isos = model.xray_structure.extract_u_iso_or_u_equiv( ) * math.pi**2 * 8 if (hd_sel is not None): b_isos = b_isos.select(~hd_sel) self.bs_iso_max_a.append(flex.max_default(b_isos, 0)) self.bs_iso_min_a.append(flex.min_default(b_isos, 0)) self.bs_iso_ave_a.append(flex.mean_default(b_isos, 0)) self.n_solv.append(model.number_of_ordered_solvent_molecules()) if (len(self.geom) > 0 and getattr(self.geom[0], "b", None) and getattr(self.geom[0], "a", None)): if ([self.bond_start, self.angle_start].count(None) == 2): if (len(self.geom) > 0): self.bond_start = self.geom[0].b[2] self.angle_start = self.geom[0].a[2] if (len(self.geom) > 0): self.bond_final = self.geom[len(self.geom) - 1].b[2] self.angle_final = self.geom[len(self.geom) - 1].a[2] elif (len(self.geom) == 1): self.bond_final = self.geom[0].b[2] self.angle_final = self.geom[0].a[2] if (rigid_body_shift_accumulator is not None): self.rigid_body_shift_accumulator = rigid_body_shift_accumulator t2 = time.time() time_collect_and_process += (t2 - t1) self.call_back(model, fmodel, method=step)
def statistics_in_resolution_bins(self, fmodel, free_reflections_per_bin, max_number_of_bins, n_bins=None): from mmtbx import bulk_solvent from cctbx.array_family import flex if(self.target_name == "twin_lsq_f"): return fmodel.statistics_in_resolution_bins() result = [] target_functor = fmodel.target_functor() target_result = target_functor(compute_gradients=False) tpr = target_result.target_per_reflection() if(tpr.size() != 0): tpr_w = tpr.select(fmodel.arrays.work_sel) tpr_t = tpr.select(fmodel.arrays.free_sel) fo_t = fmodel.f_obs_free() fc_t = fmodel.f_model_scaled_with_k1_t() fo_w = fmodel.f_obs_work() fc_w = fmodel.f_model_scaled_with_k1_w() alpha_w, beta_w = fmodel.alpha_beta_w() alpha_t, beta_t = fmodel.alpha_beta_t() pher_w = fmodel.phase_errors_work() pher_t = fmodel.phase_errors_test() fom = fmodel.figures_of_merit_work() if (n_bins is None) or (n_bins < 1) : n_bins = fmodel.determine_n_bins( free_reflections_per_bin=free_reflections_per_bin, max_n_bins=max_number_of_bins) fmodel.f_obs().setup_binner(n_bins=n_bins) fo_t.use_binning_of(fmodel.f_obs()) fc_t.use_binning_of(fo_t) fo_w.use_binning_of(fo_t) fc_w.use_binning_of(fo_t) alpha_w.use_binning_of(fo_t) alpha_t.use_binning_of(fo_t) beta_w.use_binning_of(fo_t) beta_t.use_binning_of(fo_t) for i_bin in fo_t.binner().range_used(): sel_t = fo_t.binner().selection(i_bin) sel_w = fo_w.binner().selection(i_bin) sel_all = fmodel.f_obs().binner().selection(i_bin) sel_fo_all = fmodel.f_obs().select(sel_all) sel_fo_t = fo_t.select(sel_t) sel_fc_t = fc_t.select(sel_t) sel_fo_w = fo_w.select(sel_w) sel_fc_w = fc_w.select(sel_w) if (tpr.size() == 0): sel_tpr_w = None sel_tpr_t = None else: denom_w = sel_fo_w.data().size() denom_t = sel_fo_t.data().size() if(denom_w != 0): sel_tpr_w = flex.sum(tpr_w.select(sel_w))/denom_w else: sel_tpr_w = flex.sum(tpr_w.select(sel_w)) if(denom_t != 0): sel_tpr_t = flex.sum(tpr_t.select(sel_t))/denom_t else: sel_tpr_t = flex.sum(tpr_t.select(sel_t)) d_max_,d_min_ = sel_fo_all.d_max_min() completeness = sel_fo_all.completeness(d_max = d_max_) d_range = fo_t.binner().bin_legend( i_bin=i_bin, show_bin_number=False, show_counts=False) s_fo_w_d = sel_fo_w.data() s_fc_w_d = sel_fc_w.data() assert s_fo_w_d.size() == s_fc_w_d.size() s_fc_w_d_a = flex.abs(s_fc_w_d) if(s_fo_w_d.size() > 0): bin = resolution_bin( i_bin = i_bin, d_range = d_range, completeness = completeness, alpha_work = flex.mean_default(alpha_w.select(sel_w).data(),None), beta_work = flex.mean_default(beta_w.select(sel_w).data(),None), r_work = bulk_solvent.r_factor(s_fo_w_d, s_fc_w_d, 1), r_free = bulk_solvent.r_factor(sel_fo_t.data(), sel_fc_t.data(), 1), target_work = sel_tpr_w, target_free = sel_tpr_t, n_work = sel_fo_w.data().size(), n_free = sel_fo_t.data().size(), scale_k1_work= _scale_helper(num=s_fo_w_d, den=s_fc_w_d_a), mean_f_obs = flex.mean_default(sel_fo_all.data(),None), fom_work = flex.mean_default(fom.select(sel_w),None), pher_work = flex.mean_default(pher_w.select(sel_w),None), pher_free = flex.mean_default(pher_t.select(sel_t),None), cc_work = cc(s_fo_w_d, s_fc_w_d_a), cc_free = cc(sel_fo_t.data(), flex.abs(sel_fc_t.data()))) result.append(bin) return result
def ensemble_mean_geometry_stats(self, restraints_manager, xray_structure, ensemble_xray_structures, ignore_hd = True, verbose = False, out = None, return_pdb_string = False): if (out is None): out = sys.stdout if verbose: utils.print_header("Ensemble mean geometry statistics", out = out) ensemble_size = len(ensemble_xray_structures) print("Ensemble size : ", ensemble_size, file=out) # Dictionaries to store deltas ensemble_bond_deltas = {} ensemble_angle_deltas = {} ensemble_chirality_deltas = {} ensemble_planarity_deltas = {} ensemble_dihedral_deltas = {} # List to store rmsd of each model structures_bond_rmsd = flex.double() structures_angle_rmsd = flex.double() structures_chirality_rmsd = flex.double() structures_planarity_rmsd = flex.double() structures_dihedral_rmsd = flex.double() # Remove water and hd atoms from global restraints manager selection = flex.bool() for sc in xray_structure.scatterers(): if sc.label.find('HOH') > -1: selection.append(True) else: selection.append(False) if ignore_hd: hd_selection = xray_structure.hd_selection() assert hd_selection.size() == selection.size() for n in range(hd_selection.size()): if hd_selection[n] or selection[n]: selection[n] = True restraints_manager = restraints_manager.select(selection = ~selection) # Get all deltas for n, structure in enumerate(ensemble_xray_structures): if verbose: print("\nModel : ", n+1, file=out) sites_cart = structure.sites_cart() # Remove water and hd atoms from individual structures sites cart selection = flex.bool() for sc in structure.scatterers(): if sc.label.find('HOH') > -1: selection.append(True) else: selection.append(False) if ignore_hd: hd_selection = structure.hd_selection() assert hd_selection.size() == selection.size() for n in range(hd_selection.size()): if hd_selection[n] or selection[n]: selection[n] = True sites_cart = sites_cart.select(~selection) assert sites_cart is not None site_labels = None energies_sites = restraints_manager.energies_sites( sites_cart = sites_cart, compute_gradients = False) # Rmsd of individual model bond_rmsd = energies_sites.geometry.bond_deviations()[2] angle_rmsd = energies_sites.geometry.angle_deviations()[2] chirality_rmsd = energies_sites.geometry.chirality_deviations()[2] planarity_rmsd = energies_sites.geometry.planarity_deviations()[2] dihedral_rmsd = energies_sites.geometry.dihedral_deviations()[2] structures_bond_rmsd.append(bond_rmsd) structures_angle_rmsd.append(angle_rmsd) structures_chirality_rmsd.append(chirality_rmsd) structures_planarity_rmsd.append(planarity_rmsd) structures_dihedral_rmsd.append(dihedral_rmsd) if verbose: print(" Model RMSD", file=out) print(" bond : %.6g" % bond_rmsd, file=out) print(" angle : %.6g" % angle_rmsd, file=out) print(" chirality : %.6g" % chirality_rmsd, file=out) print(" planarity : %.6g" % planarity_rmsd, file=out) print(" dihedral : %.6g" % dihedral_rmsd, file=out) # Bond pair_proxies = restraints_manager.geometry.pair_proxies(flags=None, sites_cart=sites_cart) assert pair_proxies is not None if verbose: pair_proxies.bond_proxies.show_histogram_of_deltas( sites_cart = sites_cart, n_slots = 10, f = out) for proxy in pair_proxies.bond_proxies.simple: bond_simple_proxy = geometry_restraints.bond( sites_cart = sites_cart, proxy = proxy) if proxy.i_seqs in ensemble_bond_deltas: ensemble_bond_deltas[proxy.i_seqs][0]+=bond_simple_proxy.delta ensemble_bond_deltas[proxy.i_seqs][1]+=1 else: ensemble_bond_deltas[proxy.i_seqs] = [bond_simple_proxy.delta, 1] if verbose: print("bond simple :", proxy.i_seqs, file=out) print(" distance_ideal : %.6g" % proxy.distance_ideal, file=out) print(" distance_model : %.6g" % bond_simple_proxy.distance_model, file=out) print(" detla : %.6g" % bond_simple_proxy.delta, file=out) if (pair_proxies.bond_proxies.asu.size() > 0): asu_mappings = pair_proxies.bond_proxies.asu_mappings() for proxy in pair_proxies.bond_proxies.asu: rt_mx = asu_mappings.get_rt_mx_ji(pair=proxy) bond_asu_proxy = geometry_restraints.bond( sites_cart = sites_cart, asu_mappings = asu_mappings, proxy = proxy) proxy_i_seqs = (proxy.i_seq, proxy.j_seq) if proxy_i_seqs in ensemble_bond_deltas: ensemble_bond_deltas[proxy_i_seqs][0]+=bond_asu_proxy.delta ensemble_bond_deltas[proxy_i_seqs][1]+=1 else: ensemble_bond_deltas[proxy_i_seqs] = [bond_asu_proxy.delta, 1] if verbose: print("bond asu :", (proxy.i_seq, proxy.j_seq), rt_mx, file=out) print(" distance_ideal : %.6g" % proxy.distance_ideal, file=out) print(" distance_model : %.6g" % bond_asu_proxy.distance_model, file=out) print(" delta : %.6g" % bond_asu_proxy.delta, file=out) # Angle if verbose: restraints_manager.geometry.angle_proxies.show_histogram_of_deltas( sites_cart = sites_cart, n_slots = 10, f = out) for proxy in restraints_manager.geometry.angle_proxies: angle_proxy = geometry_restraints.angle( sites_cart = sites_cart, proxy = proxy) if proxy.i_seqs in ensemble_angle_deltas: ensemble_angle_deltas[proxy.i_seqs][0]+=angle_proxy.delta ensemble_angle_deltas[proxy.i_seqs][1]+=1 else: ensemble_angle_deltas[proxy.i_seqs] = [angle_proxy.delta, 1] if verbose: print("angle : ", proxy.i_seqs, file=out) print(" angle_ideal : %.6g" % proxy.angle_ideal, file=out) print(" angle_model : %.6g" % angle_proxy.angle_model, file=out) print(" delta : %.6g" % angle_proxy.delta, file=out) # Chirality if verbose: restraints_manager.geometry.chirality_proxies.show_histogram_of_deltas( sites_cart = sites_cart, n_slots = 10, f = out) for proxy in restraints_manager.geometry.chirality_proxies: chirality_proxy = geometry_restraints.chirality( sites_cart = sites_cart, proxy = proxy) if proxy.i_seqs in ensemble_chirality_deltas: ensemble_chirality_deltas[proxy.i_seqs][0]+=chirality_proxy.delta ensemble_chirality_deltas[proxy.i_seqs][1]+=1 else: ensemble_chirality_deltas[proxy.i_seqs] = [chirality_proxy.delta, 1] if verbose: print("chirality : ", proxy.i_seqs, file=out) print(" chirality_ideal : %.6g" % proxy.volume_ideal, file=out) print(" chirality_model : %.6g" % chirality_proxy.volume_model, file=out) print(" chirality : %.6g" % chirality_proxy.delta, file=out) # Planarity for proxy in restraints_manager.geometry.planarity_proxies: planarity_proxy = geometry_restraints.planarity( sites_cart = sites_cart, proxy = proxy) proxy_i_seqs = [] for i_seq in proxy.i_seqs: proxy_i_seqs.append(i_seq) proxy_i_seqs = tuple(proxy_i_seqs) if proxy_i_seqs in ensemble_planarity_deltas: ensemble_planarity_deltas[proxy_i_seqs][0]+=planarity_proxy.rms_deltas() ensemble_planarity_deltas[proxy_i_seqs][1]+=1 else: ensemble_planarity_deltas[proxy_i_seqs] = [planarity_proxy.rms_deltas(), 1] if verbose: print("planarity : ", proxy_i_seqs, file=out) print(" planarity rms_deltas : %.6g" % planarity_proxy.rms_deltas(), file=out) # Dihedral if verbose: restraints_manager.geometry.dihedral_proxies.show_histogram_of_deltas( sites_cart = sites_cart, n_slots = 10, f = out) for proxy in restraints_manager.geometry.dihedral_proxies: dihedral_proxy = geometry_restraints.dihedral( sites_cart = sites_cart, proxy = proxy) if proxy.i_seqs in ensemble_dihedral_deltas: ensemble_dihedral_deltas[proxy.i_seqs][0]+=dihedral_proxy.delta ensemble_dihedral_deltas[proxy.i_seqs][1]+=1 else: ensemble_dihedral_deltas[proxy.i_seqs] = [dihedral_proxy.delta, 1] if verbose: print("dihedral : ", proxy.i_seqs, file=out) print(" dihedral_ideal : %.6g" % proxy.angle_ideal, file=out) print(" periodicity : %.6g" % proxy.periodicity, file=out) print(" dihedral_model : %.6g" % dihedral_proxy.angle_model, file=out) print(" delta : %.6g" % dihedral_proxy.delta, file=out) # Calculate RMSDs for ensemble model # Bond mean_bond_delta = flex.double() for proxy, info in six.iteritems(ensemble_bond_deltas): # assert info[1] == ensemble_size if info[1]!=ensemble_size: print('skipping bond RMSD calns of ensemble %s' % info, file=out) continue mean_delta = info[0] / info[1] mean_bond_delta.append(mean_delta) bond_delta_sq = mean_bond_delta * mean_bond_delta ensemble_bond_rmsd = math.sqrt(flex.mean_default(bond_delta_sq, 0)) # Angle mean_angle_delta = flex.double() for proxy, info in six.iteritems(ensemble_angle_deltas): assert info[1] == ensemble_size mean_delta = info[0] / info[1] mean_angle_delta.append(mean_delta) angle_delta_sq = mean_angle_delta * mean_angle_delta ensemble_angle_rmsd = math.sqrt(flex.mean_default(angle_delta_sq, 0)) # Chirality mean_chirality_delta = flex.double() for proxy, info in six.iteritems(ensemble_chirality_deltas): assert info[1] == ensemble_size mean_delta = info[0] / info[1] mean_chirality_delta.append(mean_delta) chirality_delta_sq = mean_chirality_delta * mean_chirality_delta ensemble_chirality_rmsd = math.sqrt(flex.mean_default(chirality_delta_sq, 0)) # Planarity mean_planarity_delta = flex.double() for proxy, info in six.iteritems(ensemble_planarity_deltas): assert info[1] == ensemble_size mean_delta = info[0] / info[1] mean_planarity_delta.append(mean_delta) planarity_delta_sq = mean_planarity_delta * mean_planarity_delta ensemble_planarity_rmsd = math.sqrt(flex.mean_default(planarity_delta_sq, 0)) # Dihedral mean_dihedral_delta = flex.double() for proxy, info in six.iteritems(ensemble_dihedral_deltas): assert info[1] == ensemble_size mean_delta = info[0] / info[1] mean_dihedral_delta.append(mean_delta) dihedral_delta_sq = mean_dihedral_delta * mean_dihedral_delta ensemble_dihedral_rmsd = math.sqrt(flex.mean_default(dihedral_delta_sq, 0)) # Calculate <structure rmsd> assert ensemble_size == structures_bond_rmsd assert ensemble_size == structures_angle_rmsd assert ensemble_size == structures_chirality_rmsd assert ensemble_size == structures_planarity_rmsd assert ensemble_size == structures_dihedral_rmsd structure_bond_rmsd_mean = structures_bond_rmsd.min_max_mean().mean structure_angle_rmsd_mean = structures_angle_rmsd.min_max_mean().mean structure_chirality_rmsd_mean = structures_chirality_rmsd.min_max_mean().mean structure_planarity_rmsd_mean = structures_planarity_rmsd.min_max_mean().mean structure_dihedral_rmsd_mean = structures_dihedral_rmsd.min_max_mean().mean # Show summary utils.print_header("Ensemble RMSD summary", out = out) print(" RMSD (mean delta per restraint)", file=out) print(" bond : %.6g" % ensemble_bond_rmsd, file=out) print(" angle : %.6g" % ensemble_angle_rmsd, file=out) print(" chirality : %.6g" % ensemble_chirality_rmsd, file=out) print(" planarity : %.6g" % ensemble_planarity_rmsd, file=out) print(" dihedral : %.6g" % ensemble_dihedral_rmsd, file=out) print(" RMSD (mean RMSD per structure)", file=out) print(" bond : %.6g" % structure_bond_rmsd_mean, file=out) print(" angle : %.6g" % structure_angle_rmsd_mean, file=out) print(" chirality : %.6g" % structure_chirality_rmsd_mean, file=out) print(" planarity : %.6g" % structure_planarity_rmsd_mean, file=out) print(" dihedral : %.6g" % structure_dihedral_rmsd_mean, file=out) if ignore_hd: print("\n Calculated excluding H/D", file=out) else: print("\n Calculated including H/D", file=out) if return_pdb_string: ens_geo_pdb_string = "REMARK 3" ens_geo_pdb_string += "\nREMARK 3 NUMBER STRUCTURES IN ENSEMBLE : {0:5d}".format(ensemble_size) if ignore_hd: ens_geo_pdb_string += "\nREMARK 3 RMS DEVIATIONS FROM IDEAL VALUES (EXCLUDING H/D)" else: ens_geo_pdb_string += "\nREMARK 3 RMS DEVIATIONS FROM IDEAL VALUES (INCLUDING H/D)" ens_geo_pdb_string += "\nREMARK 3 RMSD (MEAN DELTA PER RESTRAINT)" ens_geo_pdb_string += "\nREMARK 3 BOND : {0:5.3f}".format(ensemble_bond_rmsd) ens_geo_pdb_string += "\nREMARK 3 ANGLE : {0:5.3f}".format(ensemble_angle_rmsd) ens_geo_pdb_string += "\nREMARK 3 CHIRALITY : {0:5.3f}".format(ensemble_chirality_rmsd) ens_geo_pdb_string += "\nREMARK 3 PLANARITY : {0:5.3f}".format(ensemble_planarity_rmsd) ens_geo_pdb_string += "\nREMARK 3 DIHEDRAL : {0:5.2f}".format(ensemble_dihedral_rmsd) ens_geo_pdb_string += "\nREMARK 3 RMSD (MEAN RMSD PER STRUCTURE)" ens_geo_pdb_string += "\nREMARK 3 BOND : {0:5.3f}".format(structure_bond_rmsd_mean) ens_geo_pdb_string += "\nREMARK 3 ANGLE : {0:5.3f}".format(structure_angle_rmsd_mean) ens_geo_pdb_string += "\nREMARK 3 CHIRALITY : {0:5.3f}".format(structure_chirality_rmsd_mean) ens_geo_pdb_string += "\nREMARK 3 PLANARITY : {0:5.3f}".format(structure_planarity_rmsd_mean) ens_geo_pdb_string += "\nREMARK 3 DIHEDRAL : {0:5.2f}".format(structure_dihedral_rmsd_mean) ens_geo_pdb_string += "\nREMARK 3" return ens_geo_pdb_string
def statistics_in_resolution_bins(self, fmodel, free_reflections_per_bin, max_number_of_bins, n_bins=None): from mmtbx import bulk_solvent from cctbx.array_family import flex if (self.target_name == "twin_lsq_f"): return fmodel.statistics_in_resolution_bins() result = [] target_functor = fmodel.target_functor() target_result = target_functor(compute_gradients=False) tpr = target_result.target_per_reflection() if (tpr.size() != 0): tpr_w = tpr.select(fmodel.arrays.work_sel) tpr_t = tpr.select(fmodel.arrays.free_sel) fo_t = fmodel.f_obs_free() fc_t = fmodel.f_model_scaled_with_k1_t() fo_w = fmodel.f_obs_work() fc_w = fmodel.f_model_scaled_with_k1_w() alpha_t, beta_t = fmodel.alpha_beta_t() if (n_bins is None) or (n_bins < 1): n_bins = fmodel.determine_n_bins( free_reflections_per_bin=free_reflections_per_bin, max_n_bins=max_number_of_bins) fmodel.f_obs().setup_binner(n_bins=n_bins) fo_t.use_binning_of(fmodel.f_obs()) fc_t.use_binning_of(fo_t) fo_w.use_binning_of(fo_t) fc_w.use_binning_of(fo_t) self.alpha_w.use_binning_of(fo_t) alpha_t.use_binning_of(fo_t) self.beta_w.use_binning_of(fo_t) beta_t.use_binning_of(fo_t) for i_bin in fo_t.binner().range_used(): sel_t = fo_t.binner().selection(i_bin) sel_w = fo_w.binner().selection(i_bin) sel_all = fmodel.f_obs().binner().selection(i_bin) sel_fo_all = fmodel.f_obs().select(sel_all) sel_fo_t = fo_t.select(sel_t) sel_fc_t = fc_t.select(sel_t) sel_fo_w = fo_w.select(sel_w) sel_fc_w = fc_w.select(sel_w) if (tpr.size() == 0): sel_tpr_w = None sel_tpr_t = None else: denom_w = sel_fo_w.data().size() denom_t = sel_fo_t.data().size() if (denom_w != 0): sel_tpr_w = flex.sum(tpr_w.select(sel_w)) / denom_w else: sel_tpr_w = flex.sum(tpr_w.select(sel_w)) if (denom_t != 0): sel_tpr_t = flex.sum(tpr_t.select(sel_t)) / denom_t else: sel_tpr_t = flex.sum(tpr_t.select(sel_t)) d_max_, d_min_ = sel_fo_all.d_max_min() completeness = sel_fo_all.completeness(d_max=d_max_) d_range = "%7.2f - %7.2f" % (d_max_, d_min_) s_fo_w_d = sel_fo_w.data() s_fc_w_d = sel_fc_w.data() assert s_fo_w_d.size() == s_fc_w_d.size() s_fc_w_d_a = flex.abs(s_fc_w_d) if (s_fo_w_d.size() > 0): bin = resolution_bin( i_bin=i_bin, d_range=d_range, completeness=completeness, alpha_work=flex.mean_default( self.alpha_w.select(sel_w).data(), None), beta_work=flex.mean_default( self.beta_w.select(sel_w).data(), None), r_work=bulk_solvent.r_factor(s_fo_w_d, s_fc_w_d, 1), r_free=bulk_solvent.r_factor(sel_fo_t.data(), sel_fc_t.data(), 1), target_work=sel_tpr_w, target_free=sel_tpr_t, n_work=sel_fo_w.data().size(), n_free=sel_fo_t.data().size(), scale_k1_work=_scale_helper(num=s_fo_w_d, den=s_fc_w_d_a), mean_f_obs=flex.mean_default(sel_fo_all.data(), None), fom_work=flex.mean_default(self.fom.select(sel_w), None), pher_work=flex.mean_default(self.pher_w.select(sel_w), None), pher_free=flex.mean_default(self.pher_t.select(sel_t), None), cc_work=cc(s_fo_w_d, s_fc_w_d_a), cc_free=cc(sel_fo_t.data(), flex.abs(sel_fc_t.data()))) result.append(bin) return result
def run_coordinate_refinement( geometry_restraints_manager, selection_variable, density_map, real_space_gradients_delta, work_params, pdb_atoms=None, sites_cart=None, home_restraints_list=[], work_scatterers=None, unit_cell=None, d_min=None, weight_map=None, write_pdb_callback=None, log=None): assert [pdb_atoms, sites_cart].count(None)==1 if (log is None): log = null_out() if (work_scatterers is not None): assert unit_cell is not None assert d_min is not None best_info = None if (pdb_atoms is not None): sites_cart_start = pdb_atoms.extract_xyz() site_labels = [atom.id_str() for atom in pdb_atoms] else: sites_cart_start = sites_cart site_labels = None grmp = geometry_restraints_manager_plus( manager=geometry_restraints_manager, home_sites_cart=sites_cart_start, home_restraints_list=home_restraints_list) print >> log, "Before coordinate refinement:" grmp.energies_sites(sites_cart=sites_cart_start).show(f=log) print >> log log.flush() rstw_params = work_params.coordinate_refinement.real_space_target_weights if (rstw_params.number_of_samples is None): rstw_list = [work_params.real_space_target_weight] else: rstw_list = [rstw_params.first_sample + i * rstw_params.sampling_step for i in xrange(rstw_params.number_of_samples)] lbfgs_termination_params = scitbx.lbfgs.termination_parameters( max_iterations=work_params.coordinate_refinement .lbfgs_max_iterations) lbfgs_exception_handling_params = \ scitbx.lbfgs.exception_handling_parameters( ignore_line_search_failed_step_at_lower_bound=True) bond_rmsd_list = [] for rstw in rstw_list: refined = maptbx.real_space_refinement_simple.lbfgs( sites_cart=sites_cart_start, density_map=density_map, weight_map=weight_map, selection_variable=selection_variable, geometry_restraints_manager=grmp, real_space_target_weight=rstw, real_space_gradients_delta=real_space_gradients_delta, local_standard_deviations_radius= work_params.local_standard_deviations_radius, weight_map_scale_factor=work_params.weight_map_scale_factor, lbfgs_termination_params=lbfgs_termination_params, lbfgs_exception_handling_params=lbfgs_exception_handling_params) print >> log, "After coordinate refinement" \ " with real-space target weight %.1f:" % rstw grmp.energies_sites(sites_cart=refined.sites_cart).show(f=log) bond_proxies = grmp.pair_proxies().bond_proxies bond_proxies.show_sorted( by_value="residual", sites_cart=refined.sites_cart, site_labels=site_labels, f=log, prefix=" ", max_items=3) print >> log print >> log, " number_of_function_evaluations:", \ refined.number_of_function_evaluations print >> log, " real+geo target start: %.6g" % refined.f_start print >> log, " real+geo target final: %.6g" % refined.f_final deltas = bond_proxies.deltas(sites_cart=refined.sites_cart) deltas_abs = flex.abs(deltas) deltas_abs_sorted = deltas_abs.select( flex.sort_permutation(deltas_abs), reverse=True) wabr_params = rstw_params.worst_acceptable_bond_rmsd acceptable = ( flex.mean_default(deltas_abs_sorted[:wabr_params.pool_size],0) < wabr_params.max_pool_average) if(deltas.size()>0): bond_rmsd = flex.mean_sq(deltas)**0.5 else: bond_rmsd=0 bond_rmsd_list.append(bond_rmsd) print >> log, " Bond RMSD: %.3f" % bond_rmsd # if (work_scatterers is None): region_cc = None else: region_cc = maptbx.region_density_correlation( large_unit_cell=unit_cell, large_d_min=d_min, large_density_map=density_map, sites_cart=refined.sites_cart_variable, site_radii=flex.double(refined.sites_cart_variable.size(), 1), work_scatterers=work_scatterers) # rmsd_diff = abs(rstw_params.bond_rmsd_target - bond_rmsd) if ( best_info is None or best_info.rmsd_diff > rmsd_diff and (acceptable or not best_info.acceptable)): best_info = group_args( rstw=rstw, refined=refined, acceptable=acceptable, rmsd_diff=rmsd_diff, region_cc=region_cc, sites_cart=refined.sites_cart) print >> log if (best_info is not None): print >> log, "Table of real-space target weights vs. bond RMSD:" print >> log, " weight RMSD" for w,d in zip(rstw_list, bond_rmsd_list): print >> log, " %6.1f %5.3f" % (w,d) print >> log, "Best real-space target weight: %.1f" % best_info.rstw print >> log, \ "Associated refined final value: %.6g" % best_info.refined.f_final if (best_info.region_cc is not None): print >> log, "Associated region correlation: %.4f" % best_info.region_cc print >> log if (pdb_atoms is not None): pdb_atoms.set_xyz(new_xyz=refined.sites_cart) if (write_pdb_callback is not None): write_pdb_callback(situation="after_best_weight_determination") # fgm_params = work_params.coordinate_refinement \ .finishing_geometry_minimization if (fgm_params.cycles_max is not None and fgm_params.cycles_max > 0): print >> log, "As previously obtained with target weight %.1f:" \ % best_info.rstw grmp.energies_sites(sites_cart=refined.sites_cart).show( f=log, prefix=" ") print >> log, "Finishing refinement to idealize geometry:" print >> log, " number of function" print >> log, " weight evaluations cycle RMSD" number_of_fgm_cycles = 0 rstw = best_info.rstw * fgm_params.first_weight_scale sites_cart_start = best_info.refined.sites_cart.deep_copy() for i_cycle in xrange(fgm_params.cycles_max): fgm_refined = maptbx.real_space_refinement_simple.lbfgs( sites_cart=sites_cart_start, density_map=density_map, selection_variable=selection_variable, geometry_restraints_manager=grmp, energies_sites_flags=cctbx.geometry_restraints.flags.flags( default=True, dihedral=fgm_params.dihedral_restraints), real_space_target_weight=rstw, real_space_gradients_delta=real_space_gradients_delta, local_standard_deviations_radius= work_params.local_standard_deviations_radius, weight_map_scale_factor=work_params.weight_map_scale_factor, lbfgs_termination_params=lbfgs_termination_params, lbfgs_exception_handling_params=lbfgs_exception_handling_params) cycle_rmsd = sites_cart_start.rms_difference(fgm_refined.sites_cart) print >> log, " %6.1f %10d %6.3f" % ( rstw, fgm_refined.number_of_function_evaluations, cycle_rmsd) number_of_fgm_cycles += 1 rstw *= fgm_params.cycle_weight_multiplier if (cycle_rmsd < 1.e-4): break if (fgm_params.superpose_cycle_end_with_cycle_start): fit = scitbx.math.superpose.least_squares_fit( reference_sites=best_info.refined.sites_cart, other_sites=fgm_refined.sites_cart) fgm_refined.sites_cart = fit.other_sites_best_fit() sites_cart_start = fgm_refined.sites_cart.deep_copy() print >> log, "After %d refinements to idealize geometry:" % ( number_of_fgm_cycles) grmp.energies_sites(sites_cart=fgm_refined.sites_cart).show( f=log, prefix=" ") if (work_scatterers is None): fgm_region_cc = None else: fgm_region_cc = maptbx.region_density_correlation( large_unit_cell=unit_cell, large_d_min=d_min, large_density_map=density_map, sites_cart=fgm_refined.sites_cart_variable, site_radii=flex.double(fgm_refined.sites_cart_variable.size(), 1), work_scatterers=work_scatterers) print >> log, " Associated region correlation: %.4f" % fgm_region_cc print >> log best_info.fgm_refined = fgm_refined best_info.fgm_region_cc = fgm_region_cc if (pdb_atoms is not None): pdb_atoms.set_xyz(new_xyz=fgm_refined.sites_cart) best_info.sites_cart = fgm_refined.sites_cart if (write_pdb_callback is not None): write_pdb_callback(situation="after_finishing_geo_min") else: best_info.fgm_refined = None best_info.fgm_region_cc = None return best_info