def exercise () : from mmtbx.ions import utils as ion_utils import iotbx.pdb.hierarchy pdb_in = iotbx.pdb.hierarchy.input(pdb_string="""\ CRYST1 20.000 60.000 50.000 90.00 90.00 90.00 P 1 HETATM 1690 ZN ZN X 1 -14.031 10.147 -2.484 1.00 14.71 ION ZN2+ HETATM 1699 CL CL X 10 -16.305 10.413 -3.294 0.94 15.45 ION CL1- HETATM 1700 CL CL X 11 -13.440 1.272 -23.660 0.64 21.31 ION CL1- HETATM 1691 MG MG X 2 -14.099 16.408 -0.840 1.00 14.87 ION MG2+ HETATM 1692 CD CD X 3 -5.302 -1.809 -1.327 1.00 14.78 ION CD2+ HETATM 1693 CD CD X 4 -8.287 -11.927 -43.776 1.00 14.70 ION CD2+ HETATM 1703 CL CL X 14 -2.713 20.673 -12.004 0.79 20.10 ION CL1- HETATM 1694 NI NI X 5 -5.160 20.798 -11.755 0.93 14.92 ION NI2+ HETATM 1695 CA CA X 6 -7.922 -11.718 -0.402 0.74 16.82 ION CA2+ HETATM 1696 CD CD X 7 -16.886 -19.039 -34.333 0.61 15.22 ION CD2+ HETATM 1701 CL CL X 12 -10.068 -10.650 0.239 0.53 22.83 ION CL1- """) xrs = pdb_in.input.xray_structure_simple() pdb_atoms = pdb_in.hierarchy.atoms() perm = ion_utils.sort_atoms_permutation( pdb_atoms=pdb_atoms, xray_structure=xrs) pdb_atoms = pdb_atoms.select(perm) xrs = xrs.select(perm) hierarchy = iotbx.pdb.hierarchy.root() model = iotbx.pdb.hierarchy.model() hierarchy.append_model(model) chain = iotbx.pdb.hierarchy.chain(id="X") model.append_chain(chain) for k, atom in enumerate(pdb_atoms) : rg = iotbx.pdb.hierarchy.residue_group(resseq="%4d" % (k+1)) chain.append_residue_group(rg) ag = iotbx.pdb.hierarchy.atom_group(resname=atom.parent().resname) rg.append_atom_group(ag) ag.append_atom(atom.detached_copy()) assert (hierarchy.as_pdb_string(crystal_symmetry=xrs) == """\ CRYST1 20.000 60.000 50.000 90.00 90.00 90.00 P 1 SCALE1 0.050000 0.000000 0.000000 0.00000 SCALE2 0.000000 0.016667 0.000000 0.00000 SCALE3 0.000000 0.000000 0.020000 0.00000 HETATM 5 CD CD X 1 -5.302 -1.809 -1.327 1.00 14.78 ION CD2+ HETATM 6 CD CD X 2 -8.287 -11.927 -43.776 1.00 14.70 ION CD2+ HETATM 10 CD CD X 3 -16.886 -19.039 -34.333 0.61 15.22 ION CD2+ HETATM 1 ZN ZN X 4 -14.031 10.147 -2.484 1.00 14.71 ION ZN2+ HETATM 8 NI NI X 5 -5.160 20.798 -11.755 0.93 14.92 ION NI2+ HETATM 9 CA CA X 6 -7.922 -11.718 -0.402 0.74 16.82 ION CA2+ HETATM 2 CL CL X 7 -16.305 10.413 -3.294 0.94 15.45 ION CL1- HETATM 7 CL CL X 8 -2.713 20.673 -12.004 0.79 20.10 ION CL1- HETATM 3 CL CL X 9 -13.440 1.272 -23.660 0.64 21.31 ION CL1- HETATM 11 CL CL X 10 -10.068 -10.650 0.239 0.53 22.83 ION CL1- HETATM 4 MG MG X 11 -14.099 16.408 -0.840 1.00 14.87 ION MG2+ TER """)
def exercise(): from mmtbx.ions import utils as ion_utils import iotbx.pdb.hierarchy pdb_in = iotbx.pdb.hierarchy.input(pdb_string="""\ CRYST1 20.000 60.000 50.000 90.00 90.00 90.00 P 1 HETATM 1690 ZN ZN X 1 -14.031 10.147 -2.484 1.00 14.71 ION ZN2+ HETATM 1699 CL CL X 10 -16.305 10.413 -3.294 0.94 15.45 ION CL1- HETATM 1700 CL CL X 11 -13.440 1.272 -23.660 0.64 21.31 ION CL1- HETATM 1691 MG MG X 2 -14.099 16.408 -0.840 1.00 14.87 ION MG2+ HETATM 1692 CD CD X 3 -5.302 -1.809 -1.327 1.00 14.78 ION CD2+ HETATM 1693 CD CD X 4 -8.287 -11.927 -43.776 1.00 14.70 ION CD2+ HETATM 1703 CL CL X 14 -2.713 20.673 -12.004 0.79 20.10 ION CL1- HETATM 1694 NI NI X 5 -5.160 20.798 -11.755 0.93 14.92 ION NI2+ HETATM 1695 CA CA X 6 -7.922 -11.718 -0.402 0.74 16.82 ION CA2+ HETATM 1696 CD CD X 7 -16.886 -19.039 -34.333 0.61 15.22 ION CD2+ HETATM 1701 CL CL X 12 -10.068 -10.650 0.239 0.53 22.83 ION CL1- """) xrs = pdb_in.input.xray_structure_simple() pdb_atoms = pdb_in.hierarchy.atoms() perm = ion_utils.sort_atoms_permutation(pdb_atoms=pdb_atoms, xray_structure=xrs) pdb_atoms = pdb_atoms.select(perm) xrs = xrs.select(perm) hierarchy = iotbx.pdb.hierarchy.root() model = iotbx.pdb.hierarchy.model() hierarchy.append_model(model) chain = iotbx.pdb.hierarchy.chain(id="X") model.append_chain(chain) for k, atom in enumerate(pdb_atoms): rg = iotbx.pdb.hierarchy.residue_group(resseq="%4d" % (k + 1)) chain.append_residue_group(rg) ag = iotbx.pdb.hierarchy.atom_group(resname=atom.parent().resname) rg.append_atom_group(ag) ag.append_atom(atom.detached_copy()) assert (hierarchy.as_pdb_string(crystal_symmetry=xrs) == """\ CRYST1 20.000 60.000 50.000 90.00 90.00 90.00 P 1 SCALE1 0.050000 0.000000 0.000000 0.00000 SCALE2 0.000000 0.016667 0.000000 0.00000 SCALE3 0.000000 0.000000 0.020000 0.00000 HETATM 5 CD CD X 1 -5.302 -1.809 -1.327 1.00 14.78 ION CD2+ HETATM 6 CD CD X 2 -8.287 -11.927 -43.776 1.00 14.70 ION CD2+ HETATM 10 CD CD X 3 -16.886 -19.039 -34.333 0.61 15.22 ION CD2+ HETATM 1 ZN ZN X 4 -14.031 10.147 -2.484 1.00 14.71 ION ZN2+ HETATM 8 NI NI X 5 -5.160 20.798 -11.755 0.93 14.92 ION NI2+ HETATM 9 CA CA X 6 -7.922 -11.718 -0.402 0.74 16.82 ION CA2+ HETATM 2 CL CL X 7 -16.305 10.413 -3.294 0.94 15.45 ION CL1- HETATM 7 CL CL X 8 -2.713 20.673 -12.004 0.79 20.10 ION CL1- HETATM 3 CL CL X 9 -13.440 1.272 -23.660 0.64 21.31 ION CL1- HETATM 11 CL CL X 10 -10.068 -10.650 0.239 0.53 22.83 ION CL1- HETATM 4 MG MG X 11 -14.099 16.408 -0.840 1.00 14.87 ION MG2+ TER """)
def hierarchy_from_selection(pdb_hierarchy, selection, log): import iotbx.pdb.hierarchy temp_hierarchy = pdb_hierarchy.select(selection) altloc = None hierarchy = iotbx.pdb.hierarchy.root() model = iotbx.pdb.hierarchy.model() for chain in temp_hierarchy.chains(): for conformer in chain.conformers(): if not conformer.is_protein() and not conformer.is_na(): continue elif altloc is None or conformer.altloc == altloc: model.append_chain(chain.detached_copy()) altloc = conformer.altloc else: print >> log, \ "* Multiple alternate conformations found, using altid %s *" \ % altloc continue if len(model.chains()) != 1: raise Sorry("more than one chain in selection") hierarchy.append_model(model) return hierarchy
def exercise_synthetic () : from mmtbx.regression import tst_build_alt_confs pdb_in = iotbx.pdb.hierarchy.input(pdb_string=tst_build_alt_confs.pdb_raw) xrs = pdb_in.input.xray_structure_simple() fc = abs(xrs.structure_factors(d_min=1.5).f_calc()) flags = fc.resolution_filter(d_min=1.6).generate_r_free_flags() ls = fc.lone_set(other=flags) # case 1: no work set in high-res shell flags2 = ls.array(data=flex.bool(ls.size(), True)) flags_all = flags.concatenate(other=flags2) mtz_out = fc.as_mtz_dataset(column_root_label="F") mtz_out.add_miller_array(flags_all, column_root_label="FreeR_flag") mtz_out.mtz_object().write("tst_molprobity_1.mtz") open("tst_molprobity_1.pdb", "w").write(tst_build_alt_confs.pdb_raw) args = [ "tst_molprobity_1.pdb", "tst_molprobity_1.mtz", "--kinemage", "--maps", "flags.clashscore=False", "flags.xtriage=True", ] result = molprobity.run(args=args, ignore_missing_modules=True, out=null_out()).validation out = StringIO() result.show(out=out) # case 2: no test set in high-res shell flags2 = ls.array(data=flex.bool(ls.size(), False)) flags_all = flags.concatenate(other=flags2) mtz_out = fc.as_mtz_dataset(column_root_label="F") mtz_out.add_miller_array(flags_all, column_root_label="FreeR_flag") result = molprobity.run(args=args, ignore_missing_modules=True, out=null_out()).validation out = StringIO() result.show(out=out) # case 3: multi-MODEL structure # XXX This is not a very sophisticated test - it only ensures that the # program does not crash. We need a test for expected output... hierarchy = pdb_in.hierarchy model2 = hierarchy.only_model().detached_copy() hierarchy.append_model(model2) hierarchy.models()[0].id = "1" hierarchy.models()[1].id = "2" open("tst_molprobity_multi_model.pdb", "w").write(hierarchy.as_pdb_string()) args = [ "tst_molprobity_multi_model.pdb", "tst_molprobity_1.mtz", "--kinemage", "--maps", "flags.clashscore=False", ] result = molprobity.run(args=args, ignore_missing_modules=True, out=null_out()).validation out = StringIO() result.show(out=out) # test rotamer distributions open("tst_molprobity_misc1.pdb", "w").write(tst_build_alt_confs.pdb_raw) args = [ "tst_molprobity_1.pdb", "rotamer_library=8000", "outliers_only=False", "flags.clashscore=False", ] out = StringIO() result = molprobity.run(args=args, ignore_missing_modules=True, out=null_out()).validation result.show(outliers_only=False, out=out) assert (""" A 7 TYR m-80 93.10 299.6,92.0""" in out.getvalue()), out.getvalue()
other_sites=sites_cart) rmsd = reference_sites.rms_difference(sup.other_sites_best_fit()) rmsd_list.append(rmsd) oline = " ".join(["%.3f" % rmsd for rmsd in rmsd_list]) print >> cout, oline assert is_below_limit(min(rmsd_list), 1e-3) conformer_counts[flex.min_index(rmsd_list)] += 1 print "conformer_counts:", conformer_counts # if (libtbx.env.has_module("iotbx")): import iotbx.pdb.hierarchy hierarchy = iotbx.pdb.hierarchy.root() model = iotbx.pdb.hierarchy.model(id="") chain = iotbx.pdb.hierarchy.chain(id="A") model.append_chain(chain) hierarchy.append_model(model) # sites_cart_pentagon = pentagon_sites_cart() for i_stack,sites_cart in enumerate([ sites_cart_3p, sites_cart_2p, sites_cart_a, sites_cart_b]): atom_group = iotbx.pdb.hierarchy.atom_group(resname=" U", altloc="") sup = scitbx.math.superpose.least_squares_fit( reference_sites=sites_cart_pentagon, other_sites=sites_cart) sites_cart_out = sup.other_sites_best_fit() for site_label,site_cart in zip(atom_names, sites_cart_out): atom = iotbx.pdb.hierarchy.atom() atom.name = " %-3s" % site_label
def run(args): assert args in [[], ["--verbose"]] if (len(args) != 0): cout = sys.stdout else: cout = null_out() edge_list_bonds = [(0, 1), (0, 4), (1, 2), (2, 3), (3, 4)] bond_list = [(("C1*", "C2*"), 1.529), (("C1*", "O4*"), 1.412), (("C2*", "C3*"), 1.526), (("C3*", "C4*"), 1.520), (("C4*", "O4*"), 1.449)] angle_list = [ (("C1*", "C2*", "C3*"), 101.3), (("C2*", "C3*", "C4*"), 102.3), (("C3*", "C4*", "O4*"), 104.2), (("C4*", "O4*", "C1*"), 110.0) ] sites_cart, geo_manager = cctbx.geometry_restraints.manager \ .construct_non_crystallographic_conserving_bonds_and_angles( sites_cart=sites_cart_3p, edge_list_bonds=edge_list_bonds, edge_list_angles=[]) for bond_atom_names, distance_ideal in bond_list: i, j = [atom_names.index(atom_name) for atom_name in bond_atom_names] bond_params = geo_manager.bond_params_table[i][j] assert approx_equal(bond_params.distance_ideal, distance_ideal, eps=1.e-2) bond_params.distance_ideal = distance_ideal bond_params.weight = 1 / 0.02**2 assert geo_manager.angle_proxies is None geo_manager.angle_proxies = cctbx.geometry_restraints.shared_angle_proxy() for angle_atom_names, angle_ideal in angle_list: i_seqs = [ atom_names.index(atom_name) for atom_name in angle_atom_names ] geo_manager.angle_proxies.append( cctbx.geometry_restraints.angle_proxy(i_seqs=i_seqs, angle_ideal=angle_ideal, weight=1 / 3**2)) geo_manager.show_sorted(site_labels=atom_names, sites_cart=sites_cart, f=cout) def lbfgs(sites_cart): for i_lbfgs_restart in range(3): minimized = cctbx.geometry_restraints.lbfgs.lbfgs( sites_cart=sites_cart, geometry_restraints_manager=geo_manager) assert is_below_limit(value=minimized.final_target_value, limit=1e-10) return minimized lbfgs(sites_cart=sites_cart_3p) lbfgs(sites_cart=sites_cart_2p) conformer_counts = [0] * 4 sites_cart = sites_cart.deep_copy() mt = flex.mersenne_twister(seed=0) for i_trial in range(20): while True: for i in range(sites_cart.size()): sites_cart[i] = mt.random_double_point_on_sphere() try: lbfgs(sites_cart=sites_cart) except RuntimeError as e: if (not str(e).startswith( "Bond distance > max_reasonable_bond_distance: ")): raise else: break rmsd_list = flex.double() for reference_sites in [ sites_cart_3p, sites_cart_2p, sites_cart_a, sites_cart_b ]: sup = scitbx.math.superpose.least_squares_fit( reference_sites=reference_sites, other_sites=sites_cart) rmsd = reference_sites.rms_difference(sup.other_sites_best_fit()) rmsd_list.append(rmsd) oline = " ".join(["%.3f" % rmsd for rmsd in rmsd_list]) print(oline, file=cout) assert is_below_limit(min(rmsd_list), 1e-3) conformer_counts[flex.min_index(rmsd_list)] += 1 print("conformer_counts:", conformer_counts) # if (libtbx.env.has_module("iotbx")): import iotbx.pdb.hierarchy hierarchy = iotbx.pdb.hierarchy.root() model = iotbx.pdb.hierarchy.model(id="") chain = iotbx.pdb.hierarchy.chain(id="A") model.append_chain(chain) hierarchy.append_model(model) # sites_cart_pentagon = pentagon_sites_cart() for i_stack, sites_cart in enumerate( [sites_cart_3p, sites_cart_2p, sites_cart_a, sites_cart_b]): atom_group = iotbx.pdb.hierarchy.atom_group(resname=" U", altloc="") sup = scitbx.math.superpose.least_squares_fit( reference_sites=sites_cart_pentagon, other_sites=sites_cart) sites_cart_out = sup.other_sites_best_fit() for site_label, site_cart in zip(atom_names, sites_cart_out): atom = iotbx.pdb.hierarchy.atom() atom.name = " %-3s" % site_label atom.xyz = matrix.col(site_cart) + matrix.col( (0, 0, i_stack * 1.5)) atom.occ = 1 atom.b = 20 atom.element = " " + site_label[0] atom_group.append_atom(atom) residue_group = iotbx.pdb.hierarchy.residue_group(resseq="%4d" % (i_stack + 1), icode=" ") residue_group.append_atom_group(atom_group) chain.append_residue_group(residue_group) hierarchy.atoms().reset_serial() pdb_str = hierarchy.as_pdb_string(append_end=True) file_name = "puckers.pdb" print("Writing file:", file_name) open(file_name, "w").write("""\ REMARK random_puckers.py REMARK 1 = 3' REMARK 2 = 2' REMARK 3 = A REMARK 4 = B """ + pdb_str) # print("OK")
def exercise_synthetic () : from mmtbx.regression import tst_build_alt_confs pdb_in = iotbx.pdb.hierarchy.input(pdb_string=tst_build_alt_confs.pdb_raw) xrs = pdb_in.input.xray_structure_simple() fc = abs(xrs.structure_factors(d_min=1.5).f_calc()) flags = fc.resolution_filter(d_min=1.6).generate_r_free_flags() ls = fc.lone_set(other=flags) # case 1: no work set in high-res shell flags2 = ls.array(data=flex.bool(ls.size(), True)) flags_all = flags.concatenate(other=flags2) mtz_out = fc.as_mtz_dataset(column_root_label="F") mtz_out.add_miller_array(flags_all, column_root_label="FreeR_flag") mtz_out.mtz_object().write("tst_molprobity_1.mtz") open("tst_molprobity_1.pdb", "w").write(tst_build_alt_confs.pdb_raw) args = [ "tst_molprobity_1.pdb", "tst_molprobity_1.mtz", "--kinemage", "--maps", "flags.clashscore=False", "flags.xtriage=True", ] result = molprobity.run(args=args, ignore_missing_modules=True, out=null_out()).validation out = StringIO() result.show(out=out) # case 2: no test set in high-res shell flags2 = ls.array(data=flex.bool(ls.size(), False)) flags_all = flags.concatenate(other=flags2) mtz_out = fc.as_mtz_dataset(column_root_label="F") mtz_out.add_miller_array(flags_all, column_root_label="FreeR_flag") result = molprobity.run(args=args, ignore_missing_modules=True, out=null_out()).validation out = StringIO() result.show(out=out) # case 3: multi-MODEL structure # XXX This is not a very sophisticated test - it only ensures that the # program does not crash. We need a test for expected output... hierarchy = pdb_in.hierarchy model2 = hierarchy.only_model().detached_copy() hierarchy.append_model(model2) hierarchy.models()[0].id = "1" hierarchy.models()[1].id = "2" open("tst_molprobity_multi_model.pdb", "w").write(hierarchy.as_pdb_string()) args = [ "tst_molprobity_multi_model.pdb", "tst_molprobity_1.mtz", "--kinemage", "--maps", ] result = molprobity.run(args=args, ignore_missing_modules=True, out=null_out()).validation out = StringIO() result.show(out=out) # test rotamer distributions open("tst_molprobity_misc1.pdb", "w").write(tst_build_alt_confs.pdb_raw) args = [ "tst_molprobity_1.pdb", "rotamer_library=8000", ] out = StringIO() result = molprobity.run(args=args, ignore_missing_modules=True, out=null_out()).validation result.show(outliers_only=False, out=out)