def get_model_stat(file_name): grm = restraints.get_grm(file_name = file_name) r = model_statistics.geometry( pdb_hierarchy = grm.pdb_hierarchy, restraints_manager = grm.restraints_manager, molprobity_scores = True) # XXX very runtime inefficient distances = flex.double() xyz = grm.pdb_hierarchy.atoms().extract_xyz() bond_proxies_simple = grm.restraints_manager.geometry.pair_proxies( sites_cart = xyz).bond_proxies.simple for i, site_i in enumerate(xyz): for j, site_j in enumerate(xyz): if(j>i): bonded = False for proxy in bond_proxies_simple: p1 = list(proxy.i_seqs) p2 = [i,j] p1.sort() p2.sort() if(p1==p2): bonded=True if(not bonded): dist_ij = math.sqrt( (site_i[0]-site_j[0])**2+ (site_i[1]-site_j[1])**2+ (site_i[2]-site_j[2])**2) distances.append(dist_ij) min_nonbonded_distance = flex.min(distances) # bond(rmsd), bond(max), angle(rmsd), angle(max), etc.. #print r.b_mean, r.b_max, r.a_min, r.a_max, r.clashscore, min_nonbonded_distance return min_nonbonded_distance, r.b_max , r.a_max
def get_model_stat(pdb_file_name=None, pdb_hierarchy=None, crystal_symmetry=None, show=True): mon_lib_srv = mmtbx.monomer_library.server.server() ener_lib = mmtbx.monomer_library.server.ener_lib() params = mmtbx.monomer_library.pdb_interpretation.master_params.extract() params.use_neutron_distances = True params.restraints_library.cdl = False params.sort_atoms = False params.clash_guard.nonbonded_distance_threshold=None # assert [pdb_file_name, pdb_hierarchy].count(None)==1 assert [crystal_symmetry, pdb_hierarchy].count(None) in [0,2] if(pdb_file_name is not None): pdb_inp = iotbx.pdb.input(file_name=pdb_file_name) crystal_symmetry = pdb_inp.crystal_symmetry() pdb_hierarchy = \ iotbx.pdb.input(file_name=pdb_file_name).construct_hierarchy() # XXX centralize. 3rd copy-paste! params = monomer_library.pdb_interpretation.master_params.extract() params.clash_guard.nonbonded_distance_threshold=None params.disable_uc_volume_vs_n_atoms_check=False params.use_neutron_distances = True params.restraints_library.cdl = False processed_pdb_file = mmtbx.monomer_library.pdb_interpretation.process( mon_lib_srv = mon_lib_srv, ener_lib = ener_lib, params = params, pdb_inp = pdb_hierarchy.as_pdb_input(), strict_conflict_handling = False, crystal_symmetry = crystal_symmetry, force_symmetry = True, log = null_out()) xrs = processed_pdb_file.xray_structure() sctr_keys = xrs.scattering_type_registry().type_count_dict().keys() has_hd = "H" in sctr_keys or "D" in sctr_keys # restraints_manager = processed_pdb_file.geometry_restraints_manager( show_energies = False, assume_hydrogens_all_missing = not has_hd, plain_pairs_radius = 5.0) # stats = model_statistics.geometry( pdb_hierarchy = pdb_hierarchy, restraints_manager = restraints_manager, molprobity_scores = True) if(show): print("-"*79) stats.show(out=sys.stdout) print("-"*79) # return stats
def show(self, prefix="", log=None): self.assert_pdb_hierarchy_xray_structure_sync() if(log is None): log = sys.stdout fmt = """%s Map CC (whole unit cell): %-6.3f %s Map CC (around atoms): %-6.3f %s rmsd (bonds): %-s %s rmsd (angles): %-s %s Dist. moved from start: %-6.3f %s Dist. moved from previous: %-6.3f %s All-atom clashscore %-s %s Ramachandran plot: %s outliers: %-s %% %s allowed: %-s %% %s favored: %-s %% %s Rotamer outliers: %-s %% %s C-beta deviations: %-s """ if(self.geometry_restraints_manager is not None): mso = model_statistics.geometry( pdb_hierarchy = self.pdb_hierarchy, molprobity_scores = libtbx.env.has_module("probe"), restraints_manager = self.geometry_restraints_manager) print >> log, fmt%( prefix, self.map_cc_whole_unit_cell, prefix, self.map_cc_around_atoms, prefix, format_value("%-6.2f", self.rmsd_b).strip(), prefix, format_value("%-6.2f", self.rmsd_a).strip(), prefix, self.dist_from_start, prefix, self.dist_from_previous, prefix, format_value("%-6.2f", mso.clashscore), prefix, prefix, format_value("%-5.2f", mso.ramachandran_outliers), prefix, format_value("%-5.2f", mso.ramachandran_allowed), prefix, format_value("%-5.2f", mso.ramachandran_favored), prefix, format_value("%6.2f", mso.rotamer_outliers).strip(), prefix, format_value("%-3d", mso.c_beta_dev)) else: print >> log, fmt%( prefix, self.map_cc_whole_unit_cell, prefix, self.map_cc_around_atoms, prefix, "None", prefix, "None", prefix, self.dist_from_start, prefix, self.dist_from_previous, prefix, "None", prefix, prefix, "None", prefix, "None", prefix, "None", prefix, "None", prefix, "None")
def show(self, prefix="", log=None): self.assert_pdb_hierarchy_xray_structure_sync() if (log is None): log = sys.stdout fmt = """%s Map CC (whole unit cell): %-6.3f %s Map CC (around atoms): %-6.3f %s rmsd (bonds): %-s %s rmsd (angles): %-s %s Dist. moved from start: %-6.3f %s Dist. moved from previous: %-6.3f %s All-atom clashscore %-s %s Ramachandran plot: %s outliers: %-s %% %s allowed: %-s %% %s favored: %-s %% %s Rotamer outliers: %-s %% %s C-beta deviations: %-s """ if (self.geometry_restraints_manager is not None): mso = model_statistics.geometry( pdb_hierarchy=self.pdb_hierarchy, molprobity_scores=libtbx.env.has_module("probe"), restraints_manager=self.geometry_restraints_manager) print >> log, fmt % ( prefix, self.map_cc_whole_unit_cell, prefix, self.map_cc_around_atoms, prefix, format_value("%-6.2f", self.rmsd_b).strip(), prefix, format_value("%-6.2f", self.rmsd_a).strip(), prefix, self.dist_from_start, prefix, self.dist_from_previous, prefix, format_value("%-6.2f", mso.clashscore), prefix, prefix, format_value("%-5.2f", mso.ramachandran_outliers), prefix, format_value("%-5.2f", mso.ramachandran_allowed), prefix, format_value("%-5.2f", mso.ramachandran_favored), prefix, format_value("%6.2f", mso.rotamer_outliers).strip(), prefix, format_value("%-3d", mso.c_beta_dev)) else: print >> log, fmt % ( prefix, self.map_cc_whole_unit_cell, prefix, self.map_cc_around_atoms, prefix, "None", prefix, "None", prefix, self.dist_from_start, prefix, self.dist_from_previous, prefix, "None", prefix, prefix, "None", prefix, "None", prefix, "None", prefix, "None", prefix, "None")
def show_geometry( xray_structures, processed_pdb_file, scattering_table, hierarchy, model_selections, show_geometry_statistics, mvd_obj, atom_selections): if(len(xray_structures)>1): tmp = xray_structures[0] for xi in xray_structures[1:]: tmp = tmp.concatenate(xi) xray_structures = tmp else: xray_structures = xray_structures[0] ## utils.assert_xray_structures_equal( x1 = xray_structures, x2 = processed_pdb_file.xray_structure(), sites = True, adp = False, occupancies = True) ## hd_sel_all = xray_structures.hd_selection() if(show_geometry_statistics): sctr_keys = \ xray_structures.scattering_type_registry().type_count_dict().keys() has_hd = "H" in sctr_keys or "D" in sctr_keys geometry = processed_pdb_file.geometry_restraints_manager( show_energies = False, plain_pairs_radius = 5.0, assume_hydrogens_all_missing = not has_hd) restraints_manager_all = mmtbx.restraints.manager( geometry = geometry, normalization = True) models = hierarchy.models() geometry_statistics = [] n_residues_in_altlocs = None for i_seq, model_selection in enumerate(model_selections): hierarchy_i_seq = pdb.hierarchy.root() hierarchy_i_seq.append_model(models[i_seq].detached_copy()) # overall_counts_i_seq = hierarchy_i_seq.overall_counts() n_residues_in_altlocs = \ overall_counts_i_seq.n_alt_conf_pure + \ overall_counts_i_seq.n_alt_conf_proper + \ overall_counts_i_seq.n_alt_conf_improper # resname_classes = [] for k,v in zip(overall_counts_i_seq.resname_classes.keys(), overall_counts_i_seq.resname_classes.values()): resname_classes.append(" ".join([k.replace("common_",""), str(v)])) # xray_structure = xray_structures.select(model_selection) assert hierarchy_i_seq.atoms().size() == xray_structure.scatterers().size() hd_sel = xray_structure.hd_selection() def select_atom_selections(selection = model_selection, atom_selections = atom_selections): result = group_args() result.all = atom_selections.all .select(selection) result.macromolecule = atom_selections.macromolecule.select(selection) result.solvent = atom_selections.solvent .select(selection) result.ligand = atom_selections.ligand .select(selection) result.backbone = atom_selections.backbone .select(selection) result.sidechain = atom_selections.sidechain .select(selection) return result atom_selections_i_model = select_atom_selections( selection = model_selection, atom_selections = atom_selections) if(hd_sel.count(True) > 0 and scattering_table != "neutron"): xray_structure_stat = show_xray_structure_statistics( xray_structure = xray_structure, hd_sel = hd_sel, atom_selections = atom_selections_i_model) else: xray_structure_stat = show_xray_structure_statistics( xray_structure = xray_structure, atom_selections=atom_selections_i_model) model_statistics_geometry_macromolecule = None model_statistics_geometry_solvent = None model_statistics_geometry_ligand = None model_statistics_geometry_all = None molprobity_stats_i_seq = None rms_b_iso_or_b_equiv_bonded = None if(show_geometry_statistics): # exclude hydrogens if(hd_sel.count(True) > 0 and scattering_table != "neutron"): xray_structure = xray_structure.select(~hd_sel) model_selection = model_selection.select(~hd_sel) hierarchy_i_seq = hierarchy_i_seq.select(~hd_sel) geometry = restraints_manager_all.geometry.select(selection=~hd_sel_all) atom_selections_i_model = select_atom_selections(selection =~hd_sel_all, atom_selections = atom_selections_i_model) model_selection_as_bool = flex.bool(xray_structures.scatterers().size(), model_selection) geometry = restraints_manager_all.geometry.select(selection = model_selection_as_bool) restraints_manager = mmtbx.restraints.manager( geometry = geometry, normalization = True) restraints_manager.geometry.pair_proxies(sites_cart = xray_structure.sites_cart()) ### model_statistics_geometry_all = model_statistics.geometry( pdb_hierarchy = hierarchy_i_seq, molprobity_scores = True, restraints_manager = restraints_manager) # if(atom_selections.macromolecule.count(True)>0): mac_sel = atom_selections_i_model.macromolecule model_statistics_geometry_macromolecule = model_statistics.geometry( pdb_hierarchy = hierarchy_i_seq.select(mac_sel), molprobity_scores = True, restraints_manager = restraints_manager.select(mac_sel)) # if(atom_selections.solvent.count(True)>0): sol_sel = atom_selections_i_model.solvent model_statistics_geometry_solvent = model_statistics.geometry( pdb_hierarchy = hierarchy_i_seq.select(sol_sel), molprobity_scores = True, restraints_manager = restraints_manager.select(sol_sel)) # if(atom_selections.ligand.count(True)>0): lig_sel = atom_selections_i_model.ligand model_statistics_geometry_ligand = model_statistics.geometry( pdb_hierarchy = hierarchy_i_seq.select(lig_sel), molprobity_scores = True, restraints_manager = restraints_manager.select(lig_sel)) ### rms_b_iso_or_b_equiv_bonded = utils.rms_b_iso_or_b_equiv_bonded( restraints_manager = restraints_manager, xray_structure = xray_structure) # molprobity_stats_i_seq = molprobity_stats( model_statistics_geometry = model_statistics_geometry_all, resname_classes = overall_counts_i_seq.resname_classes) geometry_statistics.append(group_args( n_residues_in_altlocs = n_residues_in_altlocs, resname_classes = resname_classes, xray_structure_stat = xray_structure_stat, rms_b_iso_or_b_equiv_bonded = rms_b_iso_or_b_equiv_bonded, geometry_all = model_statistics_geometry_all, geometry_macromolecule = model_statistics_geometry_macromolecule, geometry_solvent = model_statistics_geometry_solvent, geometry_ligand = model_statistics_geometry_ligand, molprobity = molprobity_stats_i_seq)) mvd_obj.collect(models = geometry_statistics) return geometry_statistics
def run(args, log=sys.stdout): print >> log, "-"*79 print >> log, legend print >> log, "-"*79 inputs = mmtbx.utils.process_command_line_args(args = args, master_params = master_params()) params = inputs.params.extract() # estimate resolution d_min = params.resolution broadcast(m="Map resolution:", log=log) if(d_min is None): raise Sorry("Resolution is required.") print >> log, " d_min: %6.4f"%d_min # model broadcast(m="Input PDB:", log=log) file_names = inputs.pdb_file_names if(len(file_names) != 1): raise Sorry("PDB file has to given.") if(inputs.crystal_symmetry is None): raise Sorry("No crystal symmetry defined.") processed_pdb_file = monomer_library.pdb_interpretation.process( mon_lib_srv = monomer_library.server.server(), ener_lib = monomer_library.server.ener_lib(), file_name = file_names[0], crystal_symmetry = inputs.crystal_symmetry, force_symmetry = True, log = None) ph = processed_pdb_file.all_chain_proxies.pdb_hierarchy if(len(ph.models())>1): raise Sorry("Only one model allowed.") xrs = processed_pdb_file.xray_structure() xrs.scattering_type_registry(table = params.scattering_table) xrs.show_summary(f=log, prefix=" ") # restraints sctr_keys = xrs.scattering_type_registry().type_count_dict().keys() has_hd = "H" in sctr_keys or "D" in sctr_keys geometry = processed_pdb_file.geometry_restraints_manager( show_energies = False, assume_hydrogens_all_missing = not has_hd, plain_pairs_radius = 5.0) # map broadcast(m="Input map:", log=log) if(inputs.ccp4_map is None): raise Sorry("Map file has to given.") inputs.ccp4_map.show_summary(prefix=" ") map_data = inputs.ccp4_map.map_data() print >> log, " Actual map (min,max,mean):", \ map_data.as_1d().min_max_mean().as_tuple() make_sub_header("Histogram of map values", out=log) md = map_data.as_1d() show_histogram(data=md, n_slots=10, data_min=flex.min(md), data_max=flex.max(md), log=log) # shift origin if needed shift_needed = not \ (map_data.focus_size_1d() > 0 and map_data.nd() == 3 and map_data.is_0_based()) if(shift_needed): N = map_data.all() O=map_data.origin() map_data = map_data.shift_origin() # apply same shift to the model a,b,c = xrs.crystal_symmetry().unit_cell().parameters()[:3] sites_cart = xrs.sites_cart() sx,sy,sz = a/N[0]*O[0], b/N[1]*O[1], c/N[2]*O[2] sites_cart_shifted = sites_cart-\ flex.vec3_double(sites_cart.size(), [sx,sy,sz]) xrs.set_sites_cart(sites_cart_shifted) #### # Compute and show all stats #### broadcast(m="Model statistics:", log=log) make_sub_header("Overall", out=log) ms = model_statistics.geometry( pdb_hierarchy = ph, restraints_manager = geometry, molprobity_scores = True) ms.show() make_sub_header("Histogram of devations from ideal bonds", out=log) show_histogram(data=ms.bond_deltas, n_slots=10, data_min=0, data_max=0.2, log=log) # make_sub_header("Histogram of devations from ideal angles", out=log) show_histogram(data=ms.angle_deltas, n_slots=10, data_min=0, data_max=30., log=log) # make_sub_header("Histogram of non-bonded distances", out=log) show_histogram(data=ms.nonbonded_distances, n_slots=10, data_min=0, data_max=5., log=log) # make_sub_header("Histogram of ADPs", out=log) bs = xrs.extract_u_iso_or_u_equiv()*adptbx.u_as_b(1.) show_histogram(data=bs, n_slots=10, data_min=flex.min(bs), data_max=flex.max(bs), log=log) # # Compute FSC(map, model) broadcast(m="Map-model FSC:", log=log) mmtbx.maps.correlation.fsc_model_map( xray_structure=xrs, map=map_data, d_min=d_min, log=log) # # various CC cc_calculator = mmtbx.maps.correlation.from_map_and_xray_structure_or_fmodel( xray_structure = xrs, map_data = map_data, d_min = d_min) broadcast(m="Map-model CC:", log=log) print >> log, "Overall (entire box): %6.4f"%cc_calculator.cc() print >> log, "Around atoms (masked): %6.4f"%cc_calculator.cc( selection=flex.bool(xrs.scatterers().size(), True)) # per chain print >> log, "Per chain:" for chain in ph.chains(): print >> log, " chain %s: %6.4f"%(chain.id, cc_calculator.cc( selection=chain.atoms().extract_i_seq())) # per residue print >> log, "Per residue:" for rg in ph.residue_groups(): cc = cc_calculator.cc(selection=rg.atoms().extract_i_seq()) print >> log, " chain id: %s resid %s: %6.4f"%( rg.parent().id, rg.resid(), cc) # per residue detailed counts print >> log, "Per residue (histogram):" crystal_gridding = maptbx.crystal_gridding( unit_cell = xrs.unit_cell(), space_group_info = xrs.space_group_info(), pre_determined_n_real = map_data.accessor().all()) f_calc = xrs.structure_factors(d_min=d_min).f_calc() fft_map = miller.fft_map( crystal_gridding = crystal_gridding, fourier_coefficients = f_calc) fft_map.apply_sigma_scaling() map_model = fft_map.real_map_unpadded() sites_cart = xrs.sites_cart() cc_per_residue = flex.double() for rg in ph.residue_groups(): cc = mmtbx.maps.correlation.from_map_map_atoms( map_1 = map_data, map_2 = map_model, sites_cart = sites_cart.select(rg.atoms().extract_i_seq()), unit_cell = xrs.unit_cell(), radius = 2.) cc_per_residue.append(cc) show_histogram(data=cc_per_residue, n_slots=10, data_min=-1., data_max=1.0, log=log)
def show_geometry(xray_structures, processed_pdb_file, scattering_table, hierarchy, model_selections, show_geometry_statistics, mvd_obj, atom_selections): if (len(xray_structures) > 1): tmp = xray_structures[0] for xi in xray_structures[1:]: tmp = tmp.concatenate(xi) xray_structures = tmp else: xray_structures = xray_structures[0] ## utils.assert_xray_structures_equal(x1=xray_structures, x2=processed_pdb_file.xray_structure(), sites=True, adp=False, occupancies=True) ## hd_sel_all = xray_structures.hd_selection() if (show_geometry_statistics): sctr_keys = \ xray_structures.scattering_type_registry().type_count_dict().keys() has_hd = "H" in sctr_keys or "D" in sctr_keys geometry = processed_pdb_file.geometry_restraints_manager( show_energies=False, plain_pairs_radius=5.0, assume_hydrogens_all_missing=not has_hd) restraints_manager_all = mmtbx.restraints.manager(geometry=geometry, normalization=True) models = hierarchy.models() geometry_statistics = [] n_residues_in_altlocs = None for i_seq, model_selection in enumerate(model_selections): hierarchy_i_seq = pdb.hierarchy.root() hierarchy_i_seq.append_model(models[i_seq].detached_copy()) # overall_counts_i_seq = hierarchy_i_seq.overall_counts() n_residues_in_altlocs = \ overall_counts_i_seq.n_alt_conf_pure + \ overall_counts_i_seq.n_alt_conf_proper + \ overall_counts_i_seq.n_alt_conf_improper # resname_classes = [] for k, v in zip(overall_counts_i_seq.resname_classes.keys(), overall_counts_i_seq.resname_classes.values()): resname_classes.append(" ".join([k.replace("common_", ""), str(v)])) # xray_structure = xray_structures.select(model_selection) assert hierarchy_i_seq.atoms_size() == xray_structure.scatterers( ).size() hd_sel = xray_structure.hd_selection() def select_atom_selections(selection=model_selection, atom_selections=atom_selections): result = group_args() result.all = atom_selections.all.select(selection) result.macromolecule = atom_selections.macromolecule.select( selection) result.solvent = atom_selections.solvent.select(selection) result.ligand = atom_selections.ligand.select(selection) result.backbone = atom_selections.backbone.select(selection) result.sidechain = atom_selections.sidechain.select(selection) return result atom_selections_i_model = select_atom_selections( selection=model_selection, atom_selections=atom_selections) if (hd_sel.count(True) > 0 and scattering_table != "neutron"): xray_structure_stat = show_xray_structure_statistics( xray_structure=xray_structure, hd_sel=hd_sel, atom_selections=atom_selections_i_model) else: xray_structure_stat = show_xray_structure_statistics( xray_structure=xray_structure, atom_selections=atom_selections_i_model) model_statistics_geometry_macromolecule = None model_statistics_geometry_solvent = None model_statistics_geometry_ligand = None model_statistics_geometry_all = None molprobity_stats_i_seq = None rms_b_iso_or_b_equiv_bonded = None if (show_geometry_statistics): # exclude hydrogens if (hd_sel.count(True) > 0 and scattering_table != "neutron"): xray_structure = xray_structure.select(~hd_sel) model_selection = model_selection.select(~hd_sel) hierarchy_i_seq = hierarchy_i_seq.select(~hd_sel) geometry = restraints_manager_all.geometry.select( selection=~hd_sel_all) atom_selections_i_model = select_atom_selections( selection=~hd_sel_all, atom_selections=atom_selections_i_model) model_selection_as_bool = flex.bool( xray_structures.scatterers().size(), model_selection) geometry = restraints_manager_all.geometry.select( selection=model_selection_as_bool) restraints_manager = mmtbx.restraints.manager(geometry=geometry, normalization=True) restraints_manager.geometry.pair_proxies( sites_cart=xray_structure.sites_cart()) ### model_statistics_geometry_all = model_statistics.geometry( pdb_hierarchy=hierarchy_i_seq, molprobity_scores=True, restraints_manager=restraints_manager) # if (atom_selections.macromolecule.count(True) > 0): mac_sel = atom_selections_i_model.macromolecule model_statistics_geometry_macromolecule = model_statistics.geometry( pdb_hierarchy=hierarchy_i_seq.select(mac_sel), molprobity_scores=True, restraints_manager=restraints_manager.select(mac_sel)) # if (atom_selections.solvent.count(True) > 0): sol_sel = atom_selections_i_model.solvent model_statistics_geometry_solvent = model_statistics.geometry( pdb_hierarchy=hierarchy_i_seq.select(sol_sel), molprobity_scores=True, restraints_manager=restraints_manager.select(sol_sel)) # if (atom_selections.ligand.count(True) > 0): lig_sel = atom_selections_i_model.ligand model_statistics_geometry_ligand = model_statistics.geometry( pdb_hierarchy=hierarchy_i_seq.select(lig_sel), molprobity_scores=True, restraints_manager=restraints_manager.select(lig_sel)) ### rms_b_iso_or_b_equiv_bonded = utils.rms_b_iso_or_b_equiv_bonded( restraints_manager=restraints_manager, xray_structure=xray_structure) # molprobity_stats_i_seq = molprobity_stats( model_statistics_geometry=model_statistics_geometry_all, resname_classes=overall_counts_i_seq.resname_classes) geometry_statistics.append( group_args( n_residues_in_altlocs=n_residues_in_altlocs, resname_classes=resname_classes, xray_structure_stat=xray_structure_stat, rms_b_iso_or_b_equiv_bonded=rms_b_iso_or_b_equiv_bonded, geometry_all=model_statistics_geometry_all, geometry_macromolecule=model_statistics_geometry_macromolecule, geometry_solvent=model_statistics_geometry_solvent, geometry_ligand=model_statistics_geometry_ligand, molprobity=molprobity_stats_i_seq)) mvd_obj.collect(models=geometry_statistics) return geometry_statistics