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 neutralize(self, model): ''' Neutralize scatterers if necessary ''' xrs = model.get_xray_structure() neutralized = False for scatterer in xrs.scatterers(): type_neutralized = filter(lambda x: x.isalpha(), scatterer.scattering_type) if (type_neutralized != scatterer.scattering_type): neutralized = True scatterer.scattering_type = type_neutralized if neutralized: print('Model was neutralized.', file=self.logger) model.set_xray_structure(xray_structure = xrs) new_pdb_fn = os.path.join(self.dest_dir, self.prefix + '_neut.pdb') #model.get_hierarchy().write_pdb_file(new_pdb_file) pdb_str = model.model_as_pdb() with open(new_pdb_fn, 'w') as fp: fp.write(pdb_str) self.json_data['pdb_file_updated'] = new_pdb_fn return model
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( 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")
def exercise_00(): """ Test """ # shake coordinates --> test does not depend on initial xyz pdb_inp = iotbx.pdb.input(lines=pdb_str.split("\n"), source_info=None) model = mmtbx.model.manager(model_input=pdb_inp, build_grm=False, log=null_out()) xrs = model.get_xray_structure() xrs_shaken = xrs.deep_copy_scatterers() xrs_shaken.shake_sites_in_place(rms_difference=0.3) model.set_xray_structure(xrs_shaken) # save pdb with perturped coordinates f = open("tst_riding_fix_xyz_shaken.pdb", "w") f.write(model.model_as_pdb()) f.close() # save original prefix = 'tst_riding_fix_xyz' f = open("%s.pdb" % prefix, "w") f.write(pdb_str) f.close() # # always use the same Rfree flags def generate_r_free_flags_systematic(miller_array): result = flex.bool() for i in range(miller_array.indices().size()): if (i % 10 == 0): result.append(True) else: result.append(False) return miller_array.array(data=result) fobs_1 = abs(xrs.structure_factors(d_min=2.5).f_calc()) flags_1 = generate_r_free_flags_systematic(miller_array=fobs_1) # Save mtz for refinement mtz = fobs_1.as_mtz_dataset(column_root_label="FP") mtz.add_miller_array(flags_1, column_root_label="R-free-flags") mtz.mtz_object().write(prefix + ".mtz") # run phenix.refine selection_bool = "chain D" cmd = " ".join([ "phenix.refine", "tst_riding_fix_xyz_shaken.pdb", "%s.mtz" % prefix, "refinement.main.number_of_macro_cycles=1", "refinement.refine.strategy=individual_sites", "refinement.refine.sites.individual='%s'" % selection_bool, "write_eff_file=False", "write_geo_file=False", "write_def_file=False", "write_model_cif_file=False", "write_map_coefficients=False", "--overwrite" ]) print(cmd) easy_run.call(cmd) pdb_inp_refined = iotbx.pdb.input( file_name='tst_riding_fix_xyz_shaken_refine_001.pdb', source_info=None) model_refined = mmtbx.model.manager(model_input=pdb_inp_refined, log=null_out()) ph = model.get_hierarchy() ph_refined = model_refined.get_hierarchy() sele_bool = ph.atom_selection_cache().selection(string=selection_bool) sele_bool_refined = ph_refined.atom_selection_cache().selection( string=selection_bool) assert (sele_bool == sele_bool_refined) for a, ar, rbool in zip(ph.atoms(), ph_refined.atoms(), sele_bool): if not rbool: for ax, arx in zip(a.xyz, ar.xyz): #print(ax, arx) assert (approx_equal(ax, arx, eps=0.005))
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) # model.set_xray_structure(model.get_xray_structure()) if (ofn is not None): utils.print_header("Write output PDB file %s" % ofn, out=log) pdb_str = model.model_as_pdb() ofo.write(pdb_str) ofo.close() print("All done.", file=log)
def run(args, prefix="tst_00", validated=False): user_input_pdb = '' user_input_map = '' # very simple parsing of model and map for i, arg in enumerate(args): if arg.endswith('.cif') or arg.endswith('.ent') or arg.endswith('.pdb'): # EMD-3981 has 6exv.ent instead of .pdb user_input_pdb = arg if arg.find('=')==-1: args[i]='model=%s' % arg elif arg.endswith('.ccp4') or arg.endswith('.map'): user_input_map = arg if arg.find('=')==-1: args[i]='map=%s' % arg argument_interpreter = libtbx.phil.command_line.argument_interpreter( master_phil=master_phil, home_scope="cryo_fit2", ) user_input_pdb = clean_pdb_for_phenix(user_input_pdb) pdbs = [] maps = [] phils = [] phil_args = [] for arg in args: if os.path.isfile(arg) : if iotbx.pdb.is_pdb_file(arg): pdbs.append(arg) elif arg.endswith('.ccp4') or arg.endswith('.map'): # not the smartest maps.append(arg) else: try : file_phil = phil.parse(file_name=arg) except RuntimeError : pass else : phils.append(file_phil) else : phil_args.append(arg) phils.append(argument_interpreter.process(arg)) working_phil = master_phil.fetch(sources=phils) working_phil.show() working_params = working_phil.extract() if (not validated): validate_params(working_params) # Compute a target map from iotbx import ccp4_map ccp4_map = ccp4_map.map_reader(user_input_map) print('Map read from %s' %(user_input_map)) target_map_data = ccp4_map.map_data() # initial atomic model that we want to fit to an EM-map pdb_inp = iotbx.pdb.input(file_name=user_input_pdb) model = mmtbx.model.manager(model_input = pdb_inp) # Initialize states accumulator states = mmtbx.utils.states( pdb_hierarchy = model.get_hierarchy(), xray_structure = model.get_xray_structure()) states.add(sites_cart = model.get_xray_structure().sites_cart()) # params = sa.master_params().extract() params.start_temperature=2000 params.final_temperature=0 params.cool_rate = 100 params.number_of_steps = 1000 params.update_grads_shift = 0. params.interleave_minimization=False #Pavel will fix the error that occur when params.interleave_minimization=True print('CC: %s' %(calculate_cc(map_data=target_map_data, model=model, resolution=3.))) #STOP() result = sa.run( params = params, xray_structure = model.get_xray_structure(), restraints_manager = model.get_restraints_manager(), target_map = target_map_data, real_space = True, wx = 100, # wx=5 broke helix conformation of tst_00_poor.pdb, wx=100 kept helix well wc = 1, states_collector = states) states.write(file_name = "all_states.pdb") model.set_xray_structure(result.xray_structure) with open("refined.pdb", "w") as f: f.write(model.model_as_pdb())