def run(args): assert len(args) == 1 # Read file into pdb_input class inp = iotbx.pdb.input(file_name=args[0]) # create a model manager # Catch Sorry about MTRIX here. model = mmtbx.model.manager( model_input=inp, restraint_objects= None, # these are ligands if any [('fname', cif_object), ()] log=null_out(), ) print("=" * 80) print("number of atoms with MTRIX multiplication:", model.get_number_of_atoms()) show_ss_counts(model) # Expand with BIOMT if needed. MTRIX are already expanded by default # Catch case when both MTRIX and BIOMT present, or other Sorry raised by # BIOMT handling. # LIMITATION: this should be done before any selections made on model.manager double_counter = 0 try: model.expand_with_BIOMT_records() except Sorry as e: if str(e).startswith("Model has been already expanded"): double_counter += 1 print("=" * 80) print("number of atoms with BIOMT multiplication:", model.get_number_of_atoms()) show_ss_counts(model) # Get default params pdb_int_params = mmtbx.model.manager.get_default_pdb_interpretation_params( ) # Set whatever you want pdb_int_params.pdb_interpretation.secondary_structure.protein.enabled = True pdb_int_params.pdb_interpretation.ncs_search.enabled = True pdb_int_params.pdb_interpretation.ncs_search.residue_match_radius = 999 pdb_int_params.pdb_interpretation.clash_guard.nonbonded_distance_threshold = None #pdb_int_params.pdb_interpretation.nonbonded_weight = None # set the params. Note, that GRM would be dropped, even if it was already # constructed. In this example it is not yet constructed. model.set_pdb_interpretation_params(params=pdb_int_params) grm = model.get_restraints_manager() # Not clear which one should be used at the moment gs = model.geometry_statistics() gs.show() # The second way msi = model.get_model_statistics_info() msi.show_remark_3()
def exercise_mtrix(): inp = iotbx.pdb.input(lines=mtrix_txt+ss_txt+atoms_txt, source_info=None) model = mmtbx.model.manager( model_input = inp) assert model.get_number_of_atoms() == 900, model.get_number_of_atoms() assert model.get_hierarchy().atoms_size() == 900 assert model.get_xray_structure().scatterers().size() == 900 ss_ann = model.get_ss_annotation() # print ss_ann.as_pdb_str() assert ss_ann.get_n_helices() == 6 assert ss_ann.get_n_sheets() == 3
def exercise_08(): """ Test for MTRIX record when copies already present in file """ pdb_inp = pdb.input(source_info=None, lines=pdb_str_8) model = mmtbx.model.manager(pdb_inp) assert model.get_number_of_atoms() == 7
def exercise_set_sites_cart_no_ncs(): inp = iotbx.pdb.input(lines=pdb_str_5, source_info=None) model = mmtbx.model.manager(model_input=inp, expand_with_mtrix=False) model.search_for_ncs() nrgl = model.get_ncs_groups() nrgl._show() print('n model atoms:', model.get_number_of_atoms()) print('n master atoms:', model.get_master_selection().count(True)) print('n master atoms:', model.get_master_selection().iselection().size()) print('n master atoms:', model.get_master_hierarchy().atoms_size()) assert model.get_master_selection().count(True) ==\ model.get_master_selection().iselection().size() ==\ model.get_master_hierarchy().atoms_size() h = model.get_hierarchy() # Warning: here here mh is not deep-copy, therefore when we change atom coords # they are changing in model.get_hierarchy() as well mh = model.get_master_hierarchy() new_sites_cart = flex.vec3_double([(1.0, 1.0, 1.0)] * 42) mh.atoms().set_xyz(new_sites_cart) model.set_sites_cart_from_hierarchy(multiply_ncs=True) h = model.get_hierarchy() new_xyz = h.atoms().extract_xyz() # print('h sites:', list(new_xyz)) # checking if setting went as supposed: for j in range(42): assert approx_equal(new_xyz[j], (1.0, 1.0, 1.0), eps=1e-4)
def run(args): assert len(args) == 1 # Read file into pdb_input class inp = iotbx.pdb.input(file_name=args[0]) # create a model manager model = mmtbx.model.manager(model_input=inp) # get number of atoms in the input model n_atoms = model.get_number_of_atoms() # extract atom coordinates old_sites_cart = model.get_sites_cart() # generate random additions random_addition = flex.vec3_double( flex.random_double(size=n_atoms * 3) - 0.5) # actually add them to old coordinates new_xyz = old_sites_cart + random_addition # Update coordinates in model manager model.set_sites_cart(sites_cart=new_xyz) # get xray structure xrs = model.get_xray_structure() # reset B-factors (min=1, max=20) # generate array of new B-factors new_b = flex.random_double(size=n_atoms, factor=19) + 1 # set them in xray structure xrs.set_b_iso(values=new_b) # update model manager with this xray structure model.set_xray_structure(xrs) # output result in PDB format to the screen print model.model_as_pdb() print "END"
def exercise_biomt(): inp = iotbx.pdb.input(lines=biomt_txt+ss_txt+atoms_txt, source_info=None) model = mmtbx.model.manager( model_input = inp) assert model.get_number_of_atoms() == 300, model.get_number_of_atoms() assert model.get_hierarchy().atoms_size() == 300 assert model.get_xray_structure().scatterers().size() == 300 ss_ann = model.get_ss_annotation() assert ss_ann.get_n_helices() == 2 assert ss_ann.get_n_sheets() == 1 model.expand_with_BIOMT_records() assert model.get_number_of_atoms() == 900, model.get_number_of_atoms() assert model.get_hierarchy().atoms_size() == 900 assert model.get_xray_structure().scatterers().size() == 900, model.get_xray_structure().scatterers().size() ss_ann = model.get_ss_annotation() assert ss_ann.get_n_helices() == 6 assert ss_ann.get_n_sheets() == 3
def exercise_dihedral_edits_change(): edits = """\ geometry_restraints.edits { dihedral { action = *change atom_selection_1 = resid 1 and name CA atom_selection_2 = resid 1 and name C atom_selection_3 = resid 2 and name N atom_selection_4 = resid 2 and name CA angle_ideal = 100.00 alt_angle_ideals = 90,110 sigma = 2 periodicity = 2 } }""" def_params = mmtbx.model.manager.get_default_pdb_interpretation_scope() edits_phil = iotbx.phil.parse(edits) working_phil = def_params.fetch(edits_phil) params = working_phil.extract() inp = iotbx.pdb.input(lines=raw_records2, source_info=None) model = mmtbx.model.manager(model_input=inp) model.process(make_restraints=True) grm = model.get_restraints_manager().geometry assert grm.dihedral_proxies.size() == 9 dih1 = grm.dihedral_proxies.proxy_select(n_seq=model.get_number_of_atoms(), iselection=flex.size_t( [1, 2, 5, 6])) assert dih1.size() == 1 dp1 = dih1[0] assert approx_equal(dp1.angle_ideal, 180) assert dp1.alt_angle_ideals == None assert dp1.origin_id == 0 assert dp1.periodicity == 0 assert dp1.slack == 0 assert not dp1.top_out assert approx_equal(dp1.weight, 0.04) # Now with modifications inp = iotbx.pdb.input(lines=raw_records2, source_info=None) model2 = mmtbx.model.manager(model_input=inp) model2.process(pdb_interpretation_params=params, make_restraints=True) grm2 = model2.get_restraints_manager().geometry assert grm2.dihedral_proxies.size() == 9 dih2 = grm2.dihedral_proxies.proxy_select( n_seq=model2.get_number_of_atoms(), iselection=flex.size_t([1, 2, 5, 6])) assert dih2.size() == 1 dp2 = dih2[0] assert approx_equal(dp2.angle_ideal, 100) assert dp2.alt_angle_ideals == (90, 110) assert dp2.origin_id == origin_ids.get_origin_id('edits') assert dp2.periodicity == 2 assert dp2.slack == 0 assert not dp2.top_out assert approx_equal(dp2.weight, 0.25)
def exercise_1(): inp = iotbx.pdb.input(source_info=None, lines=pdb_str_1) model = mmtbx.model.manager(model_input=inp, log=null_out()) model.process(make_restraints=True) assert model.get_number_of_atoms() == 21, model.get_number_of_atoms() assert model.get_hierarchy().atoms_size() == 21 assert model.get_xray_structure().scatterers().size() == 21 ss = model.get_ss_annotation() # print ss.as_pdb_str() # STOP() assert ss.get_n_helices() == 3 # because the second strand contains chain B which is not in ATOM records # whole sheet got discarded. assert ss.get_n_sheets() == 0 rm = model.get_restraints_manager() assert rm.geometry.pair_proxies().bond_proxies.simple.size() == 6 # since No NCS was set, these functions return the whole thing and no # master selection assert model.get_master_hierarchy().atoms_size() == 21 assert model.get_master_selection().size() == 0 # print model.model_as_pdb() # print "="*40 # Here we set NCS constraints inp = iotbx.pdb.input(source_info=None, lines=pdb_str_1) pdb_int_params = mmtbx.model.manager.get_default_pdb_interpretation_params( ) pdb_int_params.pdb_interpretation.ncs_search.enabled = True model = mmtbx.model.manager(model_input=inp, log=null_out()) model.process(pdb_interpretation_params=pdb_int_params) # model.get_xray_structure() assert not model.ncs_constraints_present() assert model.get_ncs_obj() is not None model.setup_ncs_constraints_groups() # print model.get_ncs_obj() assert model.ncs_constraints_present() assert model.get_master_hierarchy().atoms_size() == 7 # print model.get_master_hierarchy().as_pdb_string() # print list(model.get_master_selection()) assert list(model.get_master_selection()).count(True) == 7
def test_1(): inp = iotbx.pdb.input(source_info=None, lines=pdb_str_0) model = mmtbx.model.manager(model_input=inp) model.expand_with_BIOMT_records() model = shift_and_box_model(model) sel = model.selection("chain '0' or chain 'C' or chain 'F' or chain 'I' or chain 'L' or chain 'O' or chain 'R' or chain 'U' or chain 'X'") model = model.select(sel) model.search_for_ncs() model.setup_ncs_constraints_groups(filter_groups=True) n1 = model.get_number_of_atoms() assert n1 == 648, n1 assert model.ncs_constraints_present() nrgl = model.get_ncs_groups() assert len(nrgl[0].master_iselection) == 72 assert len(nrgl[0].copies) == 8 # nrgl._show() # print (model.can_be_unique_with_biomt()) cif_txt = model.model_as_mmcif(try_unique_with_biomt=True) # print (cif_txt) assert_lines_in_text(cif_txt, """ loop_ _pdbx_struct_assembly_gen.assembly_id _pdbx_struct_assembly_gen.oper_expression _pdbx_struct_assembly_gen.asym_id_list 1 (1-9) A""") assert_lines_in_text(cif_txt, """ loop_ _pdbx_struct_assembly.id _pdbx_struct_assembly.details _pdbx_struct_assembly.method_details _pdbx_struct_assembly.oligomeric_details _pdbx_struct_assembly.oligomeric_count 1 'Symmetry assembly' ? ? ? """) assert_lines_in_text(cif_txt, """ _pdbx_struct_oper_list.vector[1] _pdbx_struct_oper_list.vector[2] _pdbx_struct_oper_list.vector[3] 1 'point symmetry operation' ? ? 1.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0""") inp = iotbx.pdb.input(source_info=None, lines=cif_txt) m2 = mmtbx.model.manager(model_input=inp) n2_1 = m2.get_number_of_atoms() assert n2_1 == 72 m2.expand_with_BIOMT_records() n2_2 = m2.get_number_of_atoms() # print (n1, n2) assert n1 == n2_2, "%d, %d" % (n1, n2)
def tst_1(prefix="gm_ncs_constr_tst1"): log = sys.stdout pdb_in = iotbx.pdb.input(source_info=None, lines=pdb_string1.split('\n')) # print dir(pdb_in) pdb_int_params = mmtbx.model.manager.get_default_pdb_interpretation_params( ) pdb_int_params.pdb_interpretation.ncs_search.enabled = True model = mmtbx.model.manager(model_input=pdb_in, pdb_interpretation_params=pdb_int_params, build_grm=True) ncs_obj = iotbx.ncs.input(hierarchy=model.get_hierarchy()) original_ncs_transform = ncs_obj.ncs_transform ncs_restraints_group_list = ncs_obj.get_ncs_restraints_group_list() ncs_obj.show(format='phil') grm = model.get_restraints_manager() tmp_xrs = model.get_xray_structure().deep_copy_scatterers() refine_selection = flex.size_t(xrange(model.get_number_of_atoms())) # print "refining sites" cycle = 0 tfg_obj = mmtbx.refinement.minimization_ncs_constraints.\ target_function_and_grads_geometry_minimization( xray_structure=tmp_xrs, ncs_restraints_group_list=ncs_restraints_group_list, refine_selection=refine_selection, restraints_manager=grm.geometry, refine_sites=True, refine_transformations=False, ) minimized = mmtbx.refinement.minimization_ncs_constraints.lbfgs( target_and_grads_object=tfg_obj, xray_structure=tmp_xrs, ncs_restraints_group_list=ncs_restraints_group_list, refine_selection=refine_selection, finite_grad_differences_test=False, max_iterations=100, refine_sites=True, refine_transformations=False) refined_pdb_h = model.get_hierarchy().deep_copy() refined_pdb_h.adopt_xray_structure(tmp_xrs) refined_pdb_h.write_pdb_file("refined_%d.pdb" % cycle) new_ncs_obj = iotbx.ncs.input(hierarchy=refined_pdb_h) new_ncs_transform = new_ncs_obj.ncs_transform spec = new_ncs_obj.get_ncs_info_as_spec() for k, v in original_ncs_transform.iteritems(): assert approx_equal(v.r.elems, new_ncs_transform[k].r.elems) assert approx_equal(v.t, new_ncs_transform[k].t) overall_rmsd_after = spec.overall_rmsd() assert overall_rmsd_after < 1e-6
def exercise_set_sites_cart_ncs(): """ No extra atoms """ inp = iotbx.pdb.input(lines=pdb_str_5, source_info=None) model = mmtbx.model.manager(model_input=inp) model.search_for_ncs() nrgl = model.get_ncs_groups() nrgl._show() print('n model atoms:', model.get_number_of_atoms()) print('n master atoms:', model.get_master_selection().count(True)) print('n master atoms:', model.get_master_selection().iselection().size()) print('n master atoms:', model.get_master_hierarchy().atoms_size()) assert model.get_master_selection().count(True) ==\ model.get_master_selection().iselection().size() ==\ model.get_master_hierarchy().atoms_size() h = model.get_hierarchy() # print('h sites:', list(h.atoms().extract_xyz())) # Warning: here here mh is not deep-copy, therefore when we change atom coords # they are changing in model.get_hierarchy() as well mh = model.get_master_hierarchy() new_sites_cart = flex.vec3_double([(1.0, 1.0, 1.0)] * 42) mh.atoms().set_xyz(new_sites_cart) model.set_sites_cart_from_hierarchy(multiply_ncs=True) h = model.get_hierarchy() new_xyz = h.atoms().extract_xyz() # print('h sites:', list(new_xyz)) # checking if setting went as supposed: assert approx_equal(new_xyz[0], (1.0, 1.0, 1.0), eps=1e-4) assert approx_equal( new_xyz[42], (-0.6420293330506949, 1.2600792663765976, 7.999997229451341), eps=1e-4) assert approx_equal( new_xyz[84], (-1.396802536808536, -0.22123285934616377, 1.000000038694047), eps=1e-4) for i in range(3): for j in range(42): assert approx_equal(new_xyz[42 * i + j], new_xyz[42 * i], eps=1e-4)
def exercise_2(eps = 1.e-6): ###> Get started from PDB mon_lib_srv = monomer_library.server.server() ener_lib = monomer_library.server.ener_lib() pdb_file = libtbx.env.find_in_repositories( relative_path="phenix_regression/pdb/phe_abc_tlsanl_out_geometry_minimized.pdb", test=os.path.isfile) model = mmtbx.model.manager( model_input=iotbx.pdb.input(file_name=pdb_file), build_grm=True) model.setup_scattering_dictionaries(scattering_table="wk1995") model.get_xray_structure().convert_to_isotropic() u_iso_start = model.get_xray_structure().extract_u_iso_or_u_equiv() model.get_xray_structure().convert_to_anisotropic() selections = [] selection_strings = ["chain A", "chain B", "chain C"] for string in selection_strings: selections.append(model.selection(string = string)) ################ selection = flex.bool(model.get_number_of_atoms(), True) class refinement_flags: pass refinement_flags.adp_tls = selections model.set_refinement_flags(refinement_flags) model.determine_tls_groups(selection_strings=selections, generate_tlsos=selections) model.set_refinement_flags(refinement_flags) xray_structure = model.get_xray_structure() ################ ###> Get TLS <-> Ucart T_initial = [] L_initial = [] S_initial = [] T_initial.append([0.11,0.22,0.33,0.12,0.13,0.23]) L_initial.append([1.11,1.22,1.33,1.12,1.13,1.23]) S_initial.append([0.11,0.12,0.13,0.21,0.22,0.23,0.31,0.32,-0.33]) T_initial.append([0.22,0.44,0.66,0.24,0.26,0.46]) L_initial.append([2.22,2.44,2.66,2.24,2.26,2.46]) S_initial.append([0.22,0.24,0.26,0.42,0.44,0.46,0.62,0.64,-0.66]) T_initial.append([0.33,0.66,0.99,0.36,0.39,0.69]) L_initial.append([2.33,2.66,2.99,2.36,2.39,2.69]) S_initial.append([0.22,0.24,0.26,0.42,0.44,0.46,0.62,0.64,-0.66]) tlsosA = tools.generate_tlsos(selections = selections, xray_structure = xray_structure, T = T_initial, L = L_initial, S = S_initial) tlsos = tools.generate_tlsos(selections = selections, xray_structure = xray_structure, T = T_initial, L = L_initial, S = S_initial) tlsos = tools.make_tlso_compatible_with_u_positive_definite( tlsos = tlsos, xray_structure = xray_structure.deep_copy_scatterers(), selections = selections, max_iterations = 50, number_of_u_nonpositive_definite = 0, eps = eps, number_of_macro_cycles_for_tls_from_uanisos = 30) u_cart_answer = tools.u_cart_from_tls(sites_cart = xray_structure.sites_cart(), selections = selections, tlsos = tlsos) xray_structure.scatterers().set_u_cart(xray_structure.unit_cell(), u_cart_answer) assert approx_equal(u_cart_answer, xray_structure.scatterers().extract_u_cart(xray_structure.unit_cell())) tools.show_tls(tlsos = tlsos, text = "ANSWER") ###> Set up fmodel sfg_params = mmtbx.f_model.sf_and_grads_accuracy_master_params.extract() sfg_params.algorithm = "direct" sfg_params.cos_sin_table = False dummy = xray_structure.structure_factors(algorithm = sfg_params.algorithm, d_min = 2.0).f_calc() f_obs = abs(dummy.structure_factors_from_scatterers( xray_structure = xray_structure, algorithm = sfg_params.algorithm, cos_sin_table = sfg_params.cos_sin_table).f_calc()) flags = f_obs.generate_r_free_flags(fraction=0.01, max_free=2000) fmodel = mmtbx.f_model.manager(xray_structure = xray_structure, f_obs = f_obs, r_free_flags = flags, target_name = "ls_wunit_k1", sf_and_grads_accuracy_params = sfg_params) fmodel.info(free_reflections_per_bin=250, max_number_of_bins=30).show_all() xray_structure.convert_to_isotropic() xray_structure.set_b_iso(value = 25.0) fmodel.update_xray_structure(xray_structure = xray_structure, update_f_calc = True) fmodel.info(free_reflections_per_bin=250, max_number_of_bins=30).show_all() print("*"*80) ###> TLS refinement against xray data if (not "--comprehensive" in sys.argv[1:]): number_of_macro_cycles = 1 max_number_of_iterations = 3 else: number_of_macro_cycles = 100 max_number_of_iterations = 50 for start_tls_value in [None]:#[0.0, tlsosA, None]: #for start_tls_value in [None]: print(" \n "+str(start_tls_value) + " \n ") fmodel_cp = fmodel.deep_copy() #for sc in fmodel_cp.xray_structure.scatterers(): # sc.flags.set_use_u_aniso(True) fmodel_cp.xray_structure.convert_to_anisotropic() if(start_tls_value is None): run_finite_differences_test = True else: run_finite_differences_test = False model.set_xray_structure(fmodel_cp.xray_structure) tls_refinement_manager = tools.tls_refinement( fmodel = fmodel_cp, model = model, selections = selections, selections_1d = None, refine_T = 1, refine_L = 1, refine_S = 1, number_of_macro_cycles = number_of_macro_cycles, max_number_of_iterations = max_number_of_iterations, start_tls_value = start_tls_value, run_finite_differences_test = run_finite_differences_test, eps = eps) u_cart = tls_refinement_manager.fmodel.xray_structure.scatterers().extract_u_cart( xray_structure.unit_cell()) if("--comprehensive" in sys.argv[1:]): format = "%10.6f %10.6f %10.6f %10.6f %10.6f %10.6f" counter = 0 if(start_tls_value == tlsosA): tolerance = 1.e-6 else: tolerance = 0.02 for m1,m2 in zip(u_cart_answer, u_cart): counter += 1 if(counter < 10): print("1=" + format % (m1[0],m1[1],m1[2],m1[3],m1[4],m1[5])) print("2=" + format % (m2[0],m2[1],m2[2],m2[3],m2[4],m2[5])) assert approx_equal(m1,m2, tolerance)
def run(args, command_name="phenix.tls"): if (len(args) == 0): args = ["--help"] usage_fmt = "%s pdb_file [parameters: file or command line string]" des_fmt = "Example: %s model.pdb fit_tls_to.selection='%s' fit_tls_to.selection='%s'" command_line = (iotbx_option_parser( usage=usage_fmt % command_name, description=banner).option( "--show_defaults", action="store_true", help="Do not output to the screen (except errors).").option( "--silent", action="store_true", help="Suppress output to the screen.")).process(args=args) # log = sys.stdout if (not command_line.options.silent): utils.print_header("TLS tools", out=log) if (command_line.options.show_defaults): master_params.show(out=log) print(file=log) return if (not command_line.options.silent): print(banner, file=log) # processed_args = utils.process_command_line_args( args=command_line.args, master_params=master_params, log=log) reflection_files = processed_args.reflection_files if (processed_args.crystal_symmetry is None): raise Sorry("No crystal symmetry found.") if (len(processed_args.pdb_file_names) == 0): raise Sorry("No PDB file found.") params = processed_args.params if (not command_line.options.silent): utils.print_header("Input parameters", out=log) params.show(out=log) params = params.extract() # if (processed_args.crystal_symmetry.unit_cell() is None or processed_args.crystal_symmetry.space_group() is None): raise Sorry("No CRYST1 record found.") pdb_combined = iotbx.pdb.combine_unique_pdb_files( file_names=processed_args.pdb_file_names) pdb_combined.report_non_unique(out=log) if (len(pdb_combined.unique_file_names) == 0): raise Sorry("No coordinate file given.") raw_records = pdb_combined.raw_records try: pdb_inp = iotbx.pdb.input(source_info=None, lines=flex.std_string(raw_records)) except ValueError as e: raise Sorry("Model format (PDB or mmCIF) error:\n%s" % str(e)) model = mmtbx.model.manager( model_input=pdb_inp, restraint_objects=processed_args.cif_objects, crystal_symmetry=processed_args.crystal_symmetry, log=log) if (not command_line.options.silent): utils.print_header("TLS groups from PDB file header", out=log) pdb_inp_tls = mmtbx.tls.tools.tls_from_pdb_inp( remark_3_records=model._model_input.extract_remark_iii_records(3), pdb_hierarchy=model.get_hierarchy()) # tls_groups = [] if (pdb_inp_tls.tls_present): if (pdb_inp_tls.error_string is not None): raise Sorry(pdb_inp_tls.error_string) pdb_tls = mmtbx.tls.tools.extract_tls_from_pdb(pdb_inp_tls=pdb_inp_tls, model=model) tls_groups = pdb_tls.pdb_inp_tls.tls_params # tls_selections_strings = [] # if (len(tls_groups) == 0 and not command_line.options.silent): print("No TLS groups found in PDB file header.", file=log) else: for i_seq, tls_group in enumerate(tls_groups): tls_selections_strings.append(tls_group.selection_string) if (not command_line.options.silent): print("TLS group %d: %s" % (i_seq + 1, tls_group.selection_string), file=log) mmtbx.tls.tools.show_tls_one_group(tlso=tls_group, out=log) print(file=log) # if (len(tls_selections_strings) > 0 and len(params.selection) > 0): raise Sorry( "Two TLS selection sources found: PDB file header and parameters.") if (len(params.selection) > 0): tls_selections_strings = params.selection if ([params.combine_tls, params.extract_tls].count(True) > 1): raise Sorry( "Cannot simultaneously pereform: combine_tls and extract_tls") if ([params.combine_tls, params.extract_tls].count(True) > 0): if (len(tls_selections_strings) == 0): raise Sorry("No TLS selections found.") # if (len(tls_selections_strings)): if (not command_line.options.silent): utils.print_header("TLS groups selections", out=log) selections = utils.get_atom_selections( model=model, selection_strings=tls_selections_strings) if (not command_line.options.silent): print("Number of TLS groups: ", len(selections), file=log) print("Number of atoms: %d" % model.get_number_of_atoms(), file=log) n_atoms_in_tls = 0 for sel_a in selections: n_atoms_in_tls += sel_a.size() if (not command_line.options.silent): print("Number of atoms in TLS groups: %d" % n_atoms_in_tls, file=log) print(file=log) assert len(tls_selections_strings) == len(selections) if (not command_line.options.silent): for sel_a, sel_s in zip(selections, tls_selections_strings): print("Selection string:\n%s" % sel_s, file=log) print("selects %d atoms." % sel_a.size(), file=log) print(file=log) print("Ready-to-use in phenix.refine:\n", file=log) for sel_a, sel_s in zip(selections, tls_selections_strings): print(sel_s, file=log) # ofn = params.output_file_name if (ofn is None): ofn = os.path.splitext( os.path.basename(processed_args.pdb_file_names[0]))[0] if (len(processed_args.pdb_file_names) > 1): ofn = ofn + "_el_al" if (params.combine_tls): ofn = ofn + "_combine_tls.pdb" elif (params.extract_tls): ofn = ofn + "_extract_tls.pdb" else: ofn = None if (ofn is not None): ofo = open(ofn, "w") # if (params.extract_tls): utils.print_header( "Fit TLS matrices to B-factors of selected sets of atoms", out=log) tlsos = mmtbx.tls.tools.generate_tlsos( selections=selections, xray_structure=model.get_xray_structure(), value=0.0) for rt, rl, rs in [[1, 0, 1], [1, 1, 1], [0, 1, 1], [1, 0, 0], [0, 1, 0], [0, 0, 1], [1, 1, 1], [0, 0, 1]] * 10: tlsos = mmtbx.tls.tools.tls_from_uanisos( xray_structure=model.get_xray_structure(), selections=selections, tlsos_initial=tlsos, number_of_macro_cycles=10, max_iterations=100, refine_T=rt, refine_L=rl, refine_S=rs, enforce_positive_definite_TL=params. enforce_positive_definite_TL, verbose=-1, out=log) mmtbx.tls.tools.show_tls(tlsos=tlsos, out=log) u_cart_from_tls = mmtbx.tls.tools.u_cart_from_tls( sites_cart=model.get_sites_cart(), selections=selections, tlsos=tlsos) unit_cell = model.get_xray_structure().unit_cell() for i_seq, sc in enumerate(model.get_xray_structure().scatterers()): if (u_cart_from_tls[i_seq] != (0, 0, 0, 0, 0, 0)): u_star_tls = adptbx.u_cart_as_u_star( unit_cell, tuple(u_cart_from_tls[i_seq])) sc.u_star = tuple( flex.double(sc.u_star) - flex.double(u_star_tls)) for sel in selections: model.get_xray_structure().convert_to_isotropic(selection=sel) mmtbx.tls.tools.remark_3_tls(tlsos=tlsos, selection_strings=tls_selections_strings, out=ofo) # if (params.combine_tls): utils.print_header("Combine B_tls with B_residual", out=log) mmtbx.tls.tools.combine_tls_and_u_local( xray_structure=model.get_xray_structure(), tls_selections=selections, tls_groups=tls_groups) print("All done.", file=log) # if (ofn is not None): utils.print_header("Write output PDB file %s" % ofn, out=log) model.set_sites_cart_from_xrs() pdb_str = model.model_as_pdb() ofo.write(pdb_str) ofo.close() print("All done.", file=log)
def run( pdb_filename=None, raw_records=None, return_formal_charges=False, verbose=False, ): if pdb_filename: # Read file into pdb_input class inp = iotbx.pdb.input(file_name=pdb_filename) elif raw_records: inp = iotbx.pdb.input(lines=raw_records, source_info='lines from PDB') else: assert 0 # create a model manager from io import StringIO log = StringIO() default_scope = mmtbx.model.manager.get_default_pdb_interpretation_scope() working_params = default_scope.extract() # optional??? working_params.pdb_interpretation.automatic_linking.link_metals = True model = mmtbx.model.manager( model_input=inp, log=log, ) model.process(make_restraints=True, pdb_interpretation_params=working_params) # get xray structure xrs = model.get_xray_structure() grm = model.get_restraints_manager() t0 = time.time() atom_valences = electron_distribution( model.get_hierarchy(), # needs to be altloc free model.get_restraints_manager().geometry, verbose=verbose, ) if verbose: print(atom_valences) total_charge = atom_valences.get_total_charge() #print 'total_charge',total_charge #print 'time %0.1f' % (time.time()-t0) rc = atom_valences.validate_atomic_formal_charges() if return_formal_charges: return atom_valences return total_charge # get number of atoms in the input model n_atoms = model.get_number_of_atoms() # extract atom coordinates old_sites_cart = model.get_sites_cart() # generate random additions random_addition = flex.vec3_double( flex.random_double(size=n_atoms * 3) - 0.5) # actually add them to old coordinates new_xyz = old_sites_cart + random_addition # Update coordinates in model manager model.set_sites_cart(sites_cart=new_xyz) # reset B-factors (min=1, max=20) # generate array of new B-factors new_b = flex.random_double(size=n_atoms, factor=19) + 1 # set them in xray structure xrs.set_b_iso(values=new_b) # update model manager with this xray structure model.set_xray_structure(xrs) # output result in PDB format to the screen print(model.model_as_pdb()) print("END")
tls_selections_strings = params.selection if ([params.combine_tls, params.extract_tls].count(True) > 1): raise Sorry( "Cannot simultaneously pereform: combine_tls and extract_tls") if ([params.combine_tls, params.extract_tls].count(True) > 0): if (len(tls_selections_strings) == 0): raise Sorry("No TLS selections found.") # if (len(tls_selections_strings)): if (not command_line.options.silent): utils.print_header("TLS groups selections", out=log) selections = utils.get_atom_selections( model=model, selection_strings=tls_selections_strings) if (not command_line.options.silent): print >> log, "Number of TLS groups: ", len(selections) print >> log, "Number of atoms: %d" % model.get_number_of_atoms() n_atoms_in_tls = 0 for sel_a in selections: n_atoms_in_tls += sel_a.size() if (not command_line.options.silent): print >> log, "Number of atoms in TLS groups: %d" % n_atoms_in_tls print >> log assert len(tls_selections_strings) == len(selections) if (not command_line.options.silent): for sel_a, sel_s in zip(selections, tls_selections_strings): print >> log, "Selection string:\n%s" % sel_s print >> log, "selects %d atoms." % sel_a.size() print >> log print >> log, "Ready-to-use in phenix.refine:\n" for sel_a, sel_s in zip(selections, tls_selections_strings): print >> log, sel_s
def exercise_set_sites_cart_ncs_with_extra_atoms(): inp = iotbx.pdb.input(lines=test_pdb_6, source_info=None) model = mmtbx.model.manager(model_input=inp) model.search_for_ncs() nrgl = model.get_ncs_groups() nrgl._show(brief=False) print('n model atoms:', model.get_number_of_atoms()) print('n master atoms:', model.get_master_selection().count(True)) print('n master atoms:', model.get_master_selection().iselection().size()) print('n master isel:', list(model.get_master_selection().iselection())) print('n master atoms:', model.get_master_hierarchy().atoms_size()) print('n master hierarchy:\n', model.get_master_hierarchy().as_pdb_string()) # Note that "HETATM 32 C2 NDG H" and "HETATM 35 C2 NDG L" # don't belong to any master/copy assert not show_diff( model.get_master_hierarchy().as_pdb_string(), """\ ATOM 1 N ASP H 5 91.286 -31.834 73.572 1.00 77.83 N ATOM 2 CA ASP H 5 90.511 -32.072 72.317 1.00 78.04 C ATOM 3 C ASP H 5 90.136 -30.762 71.617 1.00 77.70 C ATOM 4 O ASP H 5 89.553 -29.857 72.225 1.00 77.56 O ATOM 5 N THR H 6 91.286 -31.834 73.572 1.00 77.83 N ATOM 6 CA THR H 6 90.511 -32.072 72.317 1.00 78.04 C TER ATOM 7 N GLY I 501 91.286 -31.834 73.572 1.00 77.83 N ATOM 8 CA GLY I 501 90.511 -32.072 72.317 1.00 78.04 C ATOM 9 C GLY I 501 90.136 -30.762 71.617 1.00 77.70 C ATOM 10 O GLY I 501 89.553 -29.857 72.225 1.00 77.56 O TER HETATM 31 C1 NDG H 640 91.286 -31.834 73.572 1.00 77.83 C HETATM 32 C2 NDG H 640 91.286 -31.834 73.572 1.00 77.83 C HETATM 35 C2 NDG L 646 61.028 -14.273 81.262 1.00 69.80 C """) mh = model.get_master_hierarchy() # Note that atoms outside NCS are getting 2.0 as xyz new_sites_cart = flex.vec3_double( [(1.0, 1.0, 1.0), (1.0, 1.0, 1.0), (1.0, 1.0, 1.0), (1.0, 1.0, 1.0), (1.0, 1.0, 1.0), (1.0, 1.0, 1.0)] + [(3.0, 3.0, 3.0)] * 4 + [ (1.0, 1.0, 1.0), # <--- Note this atom belongs the first NCS group (2.0, 2.0, 2.0), (2.0, 2.0, 2.0) ]) mh.atoms().set_xyz(new_sites_cart) # print('='*80) # print (mh.as_pdb_string()) model.set_sites_cart_from_hierarchy(multiply_ncs=True) h = model.get_hierarchy() new_xyz = h.atoms().extract_xyz() # print(model.model_as_pdb()) # print (list(new_xyz)) assert approx_equal(new_xyz[31], (2.0, 2.0, 2.0), eps=1e-4) assert approx_equal(new_xyz[34], (2.0, 2.0, 2.0), eps=1e-4) assert approx_equal(new_xyz[0], (1.0, 1.0, 1.0), eps=1e-4) assert approx_equal(new_xyz[5], (1.0, 1.0, 1.0), eps=1e-4) assert approx_equal(new_xyz[6], (3.0, 3.0, 3.0), eps=1e-4) assert approx_equal(new_xyz[9], (3.0, 3.0, 3.0), eps=1e-4) assert nrgl.check_for_max_rmsd(sites_cart=new_xyz, chain_max_rmsd=0.0) for i in [[0, 1, 2, 3, 4, 5, 30], [10, 11, 12, 13, 14, 15, 32], [20, 21, 22, 23, 24, 25, 33]]: for j in i: assert approx_equal(new_xyz[j], new_xyz[i[0]], eps=1e-4) for i in [[6, 7, 8, 9], [16, 17, 18, 19], [26, 27, 28, 29]]: for j in i: assert approx_equal(new_xyz[j], new_xyz[i[0]], eps=1e-4)