def get_pdb_inputs(pdb_str): pdb_inp = iotbx.pdb.input(source_info=None, lines=pdb_str.split('\n')) model = mmtbx.model.manager(model_input=pdb_inp) model.process(make_restraints=True) return group_args(ph=model.get_hierarchy(), grm=model.get_restraints_manager(), xrs=model.get_xray_structure())
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(pdb_str, use_ideal_bonds_angles): # -------------------------------------------------------------- # code to switch off CDL # -------------------------------------------------------------- #params_line = grand_master_phil_str #params = iotbx.phil.parse( # input_string=params_line, process_includes=True).extract() #params.pdb_interpretation.restraints_library.cdl=False # --------------------------------------------------------------- pdb_inp = iotbx.pdb.input(lines=pdb_str.split("\n"), source_info=None) model = mmtbx.model.manager(model_input=pdb_inp, log=null_out(), build_grm=True) pdb_hierarchy = model.get_hierarchy() geometry_restraints = model.get_restraints_manager().geometry xray_structure = model.get_xray_structure() sites_cart = model.get_sites_cart() grf = cctbx.geometry_restraints.flags.flags(default=True) minimized = mmtbx.refinement.geometry_minimization.lbfgs( sites_cart=sites_cart, correct_special_position_tolerance=1.0, geometry_restraints_manager=geometry_restraints, geometry_restraints_flags=grf, lbfgs_termination_params=scitbx.lbfgs.termination_parameters( max_iterations=500)) xray_structure.set_sites_cart(sites_cart) pdb_hierarchy.adopt_xray_structure(xray_structure) atoms = pdb_hierarchy.atoms() sites_cart = xray_structure.sites_cart() riding_h_manager = riding.manager( pdb_hierarchy=pdb_hierarchy, geometry_restraints=geometry_restraints, use_ideal_bonds_angles=use_ideal_bonds_angles) h_parameterization = riding_h_manager.h_parameterization diagnostics = riding_h_manager.diagnostics(sites_cart=sites_cart, threshold=0.05) h_distances = diagnostics.h_distances number_h = model.get_hd_selection().count(True) number_h_para = len(h_parameterization) - h_parameterization.count(None) assert (number_h_para == number_h), 'Not all H atoms are parameterized' for ih in h_distances: labels = atoms[ih].fetch_labels() if use_ideal_bonds_angles: assert (h_distances[ih] < 0.03), \ 'distance too large: %s atom: %s (%s) residue: %s ' \ % (h_parameterization[ih].htype, atoms[ih].name, ih, labels.resseq.strip()) else: assert (h_distances[ih] < 1e-7), \ 'distance too large: %s atom: %s (%s) residue: %s distance %s' \ % (h_parameterization[ih].htype, atoms[ih].name, ih, labels.resseq.strip(), h_distances[ih])
def get_pdb_inputs(pdb_str): pdb_inp = iotbx.pdb.input(source_info=None, lines=pdb_str) model = mmtbx.model.manager(model_input = pdb_inp, log = null_out(), build_grm=True) return group_args( ph = model.get_hierarchy(), grm = model.get_restraints_manager(), xrs = model.get_xray_structure())
def exercise_02(): for file_name, input_model in [("m_good.pdb", model_good), ("m_bad.pdb", model_bad)]: tmp_f = open(file_name, "w") tmp_f.write(input_model) tmp_f.close() xrs_exact = iotbx.pdb.pdb_input( file_name="m_good.pdb").xray_structure_simple() model = mmtbx.model.manager(model_input=iotbx.pdb.input( file_name="m_bad.pdb")) xrs_part = model.get_xray_structure() miller_set = miller.build_set( crystal_symmetry=xrs_exact.crystal_symmetry(), anomalous_flag=False, d_min=0.6) f_obs = abs( miller_set.structure_factors_from_scatterers( xray_structure=xrs_exact, algorithm="direct", cos_sin_table=False).f_calc()) sf_par = mmtbx.f_model.sf_and_grads_accuracy_master_params.extract() sf_par.algorithm = "direct" sf_par.cos_sin_table = False fmodel = mmtbx.f_model.manager(xray_structure=xrs_part, sf_and_grads_accuracy_params=sf_par, target_name="ls_wunit_k1", f_obs=f_obs) # out = StringIO() params = find_hydrogens.all_master_params().extract() params.map_cutoff = 6.5 find_hydrogens.run(fmodel=fmodel, model=model, log=out, params=params)
def test_addition_scatterers(): ''' Test overlaps when adding and moving scatterers Test water scatterers with and without labels ''' clashes = get_clashes_result(raw_records=raw_records_3) results = clashes.get_results() assert(results.n_clashes == 3) assert approx_equal(results.clashscore, 1000, eps=0.1) # Add water scatterers model = clashes.model xrs = model.get_xray_structure() new_scatterers = flex.xray_scatterer( xrs.scatterers().size(), xray.scatterer(occupancy = 1, b = 10, scattering_type = "O")) new_sites_frac = xrs.unit_cell().fractionalize(xrs.sites_cart()+[0.5,0,0]) new_scatterers.set_sites(new_sites_frac) new_xrs = xray.structure( special_position_settings = xrs, scatterers = new_scatterers) model.add_solvent( solvent_xray_structure = new_xrs, refine_occupancies = False, refine_adp = "isotropic") pnps = pnp.manager(model = model) clashes = pnps.get_clashes() results = clashes.get_results() assert(results.n_clashes == 15) assert approx_equal(results.clashscore, 2500, eps=5)
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 run(args, out=sys.stdout): if (len(args) == 0) or ("--help" in args): raise Usage("mmtbx.rigid_bond_test model.pdb") import mmtbx.restraints import mmtbx.model import iotbx.phil cmdline = iotbx.phil.process_command_line_with_files( args=args, master_phil=master_phil, pdb_file_def="model", cif_file_def="restraints") params = cmdline.work.extract() validate_params(params) model = mmtbx.model.manager( model_input=iotbx.pdb.input(file_name=params.model), build_grm=True) model.get_xray_structure() model.show_rigid_bond_test(out=out, use_id_str=True, prefix=" ")
def exercise_bond_over_symmetry(mon_lib_srv, ener_lib): from cctbx.geometry_restraints.linking_class import linking_class origin_ids = linking_class() pdb_inp = iotbx.pdb.input(source_info=None, lines=raw_records9) params = mmtbx.model.manager.get_default_pdb_interpretation_params() params.pdb_interpretation.restraints_library.mcl = False model = mmtbx.model.manager(model_input=pdb_inp, pdb_interpretation_params=params, log=null_out(), build_grm=True) grm = model.get_restraints_manager().geometry simple, asu = grm.get_all_bond_proxies() assert (simple.size(), asu.size()) == (29, 0) h = model.get_hierarchy() proxy = geometry_restraints.bond_simple_proxy( i_seqs=(32, 4), distance_ideal=2.9, weight=400, origin_id=origin_ids.get_origin_id('hydrogen bonds')) proxy2 = geometry_restraints.bond_simple_proxy( i_seqs=(32, 24), distance_ideal=2.9, weight=400, origin_id=origin_ids.get_origin_id('hydrogen bonds')) grm.add_new_bond_restraints_in_place(proxies=[proxy, proxy2], sites_cart=h.atoms().extract_xyz()) simple, asu = grm.get_all_bond_proxies() assert (simple.size(), asu.size()) == (30, 2) sites_cart = h.atoms().extract_xyz() site_labels = model.get_xray_structure().scatterers().extract_labels() pair_proxies = grm.pair_proxies(flags=None, sites_cart=sites_cart) out = StringIO() pair_proxies.bond_proxies.show_sorted(by_value="residual", sites_cart=sites_cart, site_labels=site_labels, f=out, prefix="") outtxt = out.getvalue() # print(outtxt) # # Not clear why ZN-NE2 bond adds as 2 bonds. assert_lines_in_text( outtxt, """\ bond pdb="ZN ZN A 8 " pdb=" NE2 HIS B 304 " ideal model delta sigma weight residual sym.op. 2.900 2.969 -0.069 5.00e-02 4.00e+02 1.92e+00 -x-1/2,y+1/2,-z+3/4 """) assert_lines_in_text( outtxt, """\ bond pdb=" NE2 HIS B 304 " pdb="ZN ZN A 8 " ideal model delta sigma weight residual sym.op. 2.900 2.969 -0.069 5.00e-02 4.00e+02 1.92e+00 -x-1/2,y-1/2,-z+3/4 """)
def calculate_cc(map_data, model, resolution): xrs = model.get_xray_structure() fc = xrs.structure_factors(d_min = resolution).f_calc() f_map = fc.structure_factors_from_map( map = map_data, use_scale = True, anomalous_flag = False, use_sg = False) return fc.map_correlation(other = f_map)
def exercise(pdb_str, eps): pdb_inp = iotbx.pdb.input(lines=pdb_str.split("\n"), source_info=None) model = mmtbx.model.manager( model_input = pdb_inp, build_grm = True, log = null_out()) geometry_restraints = model.restraints_manager.geometry xray_structure = model.get_xray_structure() model.setup_riding_h_manager() riding_h_manager = model.get_riding_h_manager() riding_h_manager.idealize(xray_structure = xray_structure) sites_cart = xray_structure.sites_cart() g_analytical = geometry_restraints.energies_sites( sites_cart = sites_cart, compute_gradients = True).gradients hd_selection = xray_structure.hd_selection() g_analytical_reduced = riding_h_manager.gradients_reduced_cpp( gradients = g_analytical, sites_cart = sites_cart, hd_selection = hd_selection) # ex = [eps,0,0] ey = [0,eps,0] ez = [0,0,eps] g_fd = flex.vec3_double() for i_site in xrange(sites_cart.size()): g_fd_i = [] for e in [ex,ey,ez]: ts = [] for sign in [-1,1]: sites_cart_ = sites_cart.deep_copy() xray_structure_ = xray_structure.deep_copy_scatterers() sites_cart_[i_site] = [ sites_cart_[i_site][j]+e[j]*sign for j in xrange(3)] xray_structure_.set_sites_cart(sites_cart_) # after shift, recalculate H position riding_h_manager.idealize( xray_structure=xray_structure_) sites_cart_ = xray_structure_.sites_cart() ts.append(geometry_restraints.energies_sites( sites_cart = sites_cart_, compute_gradients = False).target) g_fd_i.append((ts[1]-ts[0])/(2*eps)) g_fd.append(g_fd_i) g_fd_reduced = g_fd.select(~hd_selection) for g1, g2 in zip(g_analytical_reduced, g_fd_reduced): assert approx_equal(g1,g2, 1.e-4)
def exercise_bond_near_symmetry3(mon_lib_srv, ener_lib): """ Since neighbors_fast_pair_generator for non-symmetry interactions provides only (i,j) pair and not (j,i), and there's no sorting involved (no way to guess what to check), there was a bug where non-symmetry interaction was missed in add_new_bond_restraints_in_place. Actually testing that both bonds are added without symmetry operators. """ from cctbx.geometry_restraints.linking_class import linking_class origin_ids = linking_class() pdb_inp = iotbx.pdb.input(source_info=None, lines=raw_records8) model = mmtbx.model.manager(model_input=pdb_inp, log=null_out(), build_grm=True) grm = model.get_restraints_manager().geometry h = model.get_hierarchy() proxy = geometry_restraints.bond_simple_proxy( i_seqs=(64, 37), distance_ideal=2.9, weight=400, origin_id=origin_ids.get_origin_id('hydrogen bonds')) proxy2 = geometry_restraints.bond_simple_proxy( i_seqs=(72, 37), distance_ideal=2.9, weight=400, origin_id=origin_ids.get_origin_id('hydrogen bonds')) grm.add_new_hbond_restraints_in_place( proxies=[proxy, proxy2], sites_cart=h.atoms().extract_xyz(), max_distance_between_connecting_atoms=10) sites_cart = h.atoms().extract_xyz() site_labels = model.get_xray_structure().scatterers().extract_labels() pair_proxies = grm.pair_proxies(flags=None, sites_cart=sites_cart) out = StringIO() pair_proxies.bond_proxies.show_sorted( by_value="residual", sites_cart=sites_cart, site_labels=site_labels, f=out, prefix="", origin_id=origin_ids.get_origin_id('hydrogen bonds')) outtxt = out.getvalue() assert not show_diff( outtxt, """\ Bond restraints: 2 Sorted by residual: bond pdb=" O ARG A 25 " pdb=" N AASN A 29 " ideal model delta sigma weight residual 2.900 2.934 -0.034 5.00e-02 4.00e+02 4.53e-01 bond pdb=" O ARG A 25 " pdb=" N BASN A 29 " ideal model delta sigma weight residual 2.900 2.888 0.012 5.00e-02 4.00e+02 5.59e-02 """)
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 check_scattering_type_registry(): pdb_inp = iotbx.pdb.input(source_info=None, lines=pdb_str) model = mmtbx.model.manager(model_input=pdb_inp, log=null_out()) model.process(make_restraints=True) xrs1 = model.get_xray_structure() xrs2 = model.get_hierarchy().extract_xray_structure( crystal_symmetry=model.crystal_symmetry()) xrs1.scattering_type_registry(table="electron") xrs2.scattering_type_registry(table="electron") xrs1.scattering_type_registry().show() xrs2.scattering_type_registry().show() # TODO: Assert to the same value once added assert (xrs1.scattering_type_registry().gaussian("O1-") is None) assert (xrs2.scattering_type_registry().gaussian("O1-") is None)
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 run(args): if (len(args) != 2): raise RuntimeError("Please specify two file names.") fn_1 = args[0] fn_2 = args[1] model_filename, reflection_filename = None, None for fn in (fn_1, fn_2): if os.path.splitext(fn)[1] == '.mtz' and reflection_filename is None: reflection_filename = fn elif os.path.splitext(fn)[1] == '.pdb' and model_filename is None: model_filename = fn else: raise RuntimeError("Please specify one model file and one reflection file.") print('Model file name: ', model_filename) print('Reflection file name: ', reflection_filename) pdb_inp = iotbx.pdb.input(file_name=model_filename) model = mmtbx.model.manager(model_input = pdb_inp) miller_arrays = reflection_file_reader.any_reflection_file( file_name = reflection_filename).as_miller_arrays() for ma in miller_arrays: if (ma.info().label_string()=="FOBS,SIGFOBS"): f_obs = ma if (ma.info().label_string() in ['R-free-flags','FreeR_flag']): r_free = ma f_obs, r_free = f_obs.common_sets(r_free) r_free_flags = r_free.array(data = r_free.data()==0) xray_structure = model.get_xray_structure() f_model = mmtbx.f_model.manager( f_obs = f_obs, r_free_flags = r_free_flags, xray_structure = xray_structure) f_model.update_all_scales() print('\nRwork: ', f_model.r_work()) print('\nRfree:', f_model.r_free())
def run_tst_xrs_and_hierarchy(): # Check if scatterers in xrs are neutral after calling function pdb_inp = iotbx.pdb.input(source_info=None, lines=pdb_str) model = mmtbx.model.manager(model_input=pdb_inp, log=null_out()) model.neutralize_scatterers() xrs = model.get_xray_structure() for scatterer in xrs.scatterers(): assert (scatterer.scattering_type in neutral_atoms_list) # Check if pdb_hierarchy has neutral atoms as well pdb_str_neutralized = model.model_as_pdb() pdb_inp_neutralized = iotbx.pdb.input(source_info=None, lines=pdb_str_neutralized) model_neutralized = mmtbx.model.manager(model_input=pdb_inp_neutralized, log=null_out()) xrs_neutralized = model_neutralized.get_xray_structure() for scatterer in xrs_neutralized.scatterers(): assert (scatterer.scattering_type in neutral_atoms_list)
def exercise_1(): inp = iotbx.pdb.input(source_info=None, lines=pdb_str_1) model = mmtbx.model.manager( model_input = inp, log = null_out()) assert model.get_number_of_atoms() == 21 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, pdb_interpretation_params = pdb_int_params, process_input=True, log = null_out()) # 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 get_fsc(map_data, model, params): result = None if (params.compute.fsc): mtriage_params = mtriage.master_params().extract() mtriage_params.scattering_table = params.scattering_table mtriage_params.compute.map_counts = False mtriage_params.compute.fsc_curve_model = True mtriage_params.compute.d_fsc_model_05 = False mtriage_params.compute.d_fsc_model_0 = False mtriage_params.compute.d_fsc_model_0143 = False mtriage_params.compute.d_model = False mtriage_params.compute.d_model_b0 = False mtriage_params.compute.d99 = False mtriage_params.mask_maps = True #mtriage_params.radius_smooth = self.atom_radius mtriage_params.resolution = params.resolution result = mtriage.mtriage( map_data=map_data, xray_structure=model.get_xray_structure(), params=mtriage_params).get_results().masked.fsc_curve_model return result
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 run(args, log=sys.stdout): if (len(args) == 0): parsed = defaults(log=log) parsed.show(prefix=" ", out=log) return parsed = defaults(log=log) processed_args = mmtbx.utils.process_command_line_args( args=args, log=sys.stdout, master_params=parsed) processed_args.params.show() params = processed_args.params.extract() if (len(processed_args.pdb_file_names) == 0): raise Sorry("No PDB file found.") if (len(processed_args.pdb_file_names) > 1): raise Sorry("More than one PDB file found.") pdb_file_name = processed_args.pdb_file_names[0] if (params.f_obs.f_calc.atomic_model.add_hydrogens): pdb_file_name_r = os.path.basename(pdb_file_name) + "_reduce" # easy_run.go("phenix.reduce %s > %s"% (pdb_file_name, pdb_file_name_r)) run_reduce_with_timeout(file_name=pdb_file_name, parameters=" > %s" % pdb_file_name_r) pdb_file_name = pdb_file_name_r pdbi_params = mmtbx.model.manager.get_default_pdb_interpretation_params() if (params.f_obs.f_calc.atomic_model.use_ramachandran_plot_restraints): pdbi_params.pdb_interpretation.ramachandran_plot_restraints.enabled = True model = mmtbx.model.manager(model_input=iotbx.pdb.input( file_name=pdb_file_name)) model.process(make_restraints=True, pdb_interpretation_params=pdbi_params) root = iotbx.pdb.hierarchy.root() loop_1(params=params, root=root, xray_structure=model.get_xray_structure(), pdb_hierarchy=model.get_hierarchy(), restraints_manager=model.get_restraints_manager()) root.write_pdb_file( file_name=params.f_obs.f_calc.atomic_model.output_file_name, crystal_symmetry=model.crystal_symmetry()) simulate_f_obs(root=root, crystal_symmetry=model.crystal_symmetry(), params=params)
def run(args): # processing command-line stuff, out of the object log = multi_out() log.register("stdout", sys.stdout) if len(args) == 0: format_usage_message(log) return input_objects = process_command_line_with_files( args=args, master_phil=master_params(), pdb_file_def="model_file_name", map_file_def="map_file_name", reflection_file_def="hkl_file_name", cif_file_def="ligands_file_name") work_params = input_objects.work.extract() if [work_params.map_file_name, work_params.hkl_file_name].count(None) < 1: raise Sorry("Only one source of map could be supplied.") input_objects.work.show(prefix=" ", out=log) if len(work_params.model_file_name) == 0: raise Sorry("No PDB file specified") if work_params.output_prefix is None: work_params.output_prefix = os.path.basename( work_params.model_file_name[0]) log_file_name = "%s.log" % work_params.output_prefix logfile = open(log_file_name, "w") log.register("logfile", logfile) err_log = multi_out() err_log.register(label="log", file_object=log) # err_log.register(label="stderr", file_object=sys.stderr) sys.stderr = err_log if work_params.loop_idealization.output_prefix is None: work_params.loop_idealization.output_prefix = "%s_rama_fixed" % work_params.output_prefix # Here we start opening files provided, # collect crystal symmetries pdb_combined = iotbx.pdb.combine_unique_pdb_files( file_names=work_params.model_file_name) pdb_input = iotbx.pdb.input(source_info=None, lines=flex.std_string( pdb_combined.raw_records)) pdb_cs = pdb_input.crystal_symmetry() crystal_symmetry = None map_cs = None map_content = input_objects.get_file(work_params.map_file_name) if map_content is not None: try: map_cs = map_content.crystal_symmetry() except NotImplementedError as e: pass try: crystal_symmetry = crystal.select_crystal_symmetry( from_command_line=None, from_parameter_file=None, from_coordinate_files=[pdb_cs], from_reflection_files=[map_cs], enforce_similarity=True) except AssertionError as e: if len(e.args) > 0 and e.args[0].startswith( "No unit cell and symmetry information supplied"): pass else: raise e model = mmtbx.model.manager(model_input=pdb_input, restraint_objects=input_objects.cif_objects, crystal_symmetry=crystal_symmetry, process_input=False, log=log) map_data = None shift_manager = None if map_content is not None: map_data, map_cs, shift_manager = get_map_from_map( map_content, work_params, xrs=model.get_xray_structure(), log=log) model.shift_model_and_set_crystal_symmetry( shift_cart=shift_manager.shift_cart) # model.get_hierarchy().write_pdb_file("junk_shift.pdb") hkl_content = input_objects.get_file(work_params.hkl_file_name) if hkl_content is not None: map_data, map_cs = get_map_from_hkl( hkl_content, work_params, xrs=model.get_xray_structure( ), # here we don't care about atom order log=log) mi_object = model_idealization(model=model, map_data=map_data, params=work_params, log=log, verbose=False) mi_object.run() mi_object.print_stat_comparison() print("RMSD from starting model (backbone, all): %.4f, %.4f" % (mi_object.get_rmsd_from_start(), mi_object.get_rmsd_from_start2()), file=log) mi_object.print_runtime() # add hydrogens if needed ? print("All done.", file=log) log.flush() sys.stderr = sys.__stderr__ log.close()
def find_ligands_and_get_cif(self): if self.json_data['success_composition'] is False: # ready set did not fail, so don't store as failed return make_sub_header('Search for ligands', out=self.logger) self.save_json() fn = self.json_data['pdb_file'] pdb_inp = iotbx.pdb.input(file_name = fn) try: model = mmtbx.model.manager( model_input = pdb_inp, log = null_out()) except Exception as e: self.json_data['success_readyset'] = False msg = traceback.format_exc() print(msg, file=self.logger) self.write_log( step='Model class without restraints from initial pdb ', msg=msg) self.save_json() return model = self.neutralize(model = model) sctr_keys = model.get_xray_structure().scattering_type_registry().type_count_dict() has_hd = "H" in sctr_keys or "D" in sctr_keys #exclude = ["common_amino_acid", "modified_amino_acid", "common_rna_dna", # "modified_rna_dna", "ccp4_mon_lib_rna_dna", "common_water", # "common_element", "common_small_molecule"] exclude = ["common_amino_acid", "common_rna_dna", "ccp4_mon_lib_rna_dna", "common_water", "common_element"] ph = model.get_hierarchy() self.pickle_data.chain_dict = ph.overall_counts().chain_ids ligands = [] needs_cif = False get_class = iotbx.pdb.common_residue_names_get_class n_removed = 0 n_removed_total = 0 for chain in ph.chains(): for rg in chain.residue_groups(): for resname in rg.unique_resnames(): if (not get_class(name=resname) in exclude): if has_hd and not self.params.add_hydrogen: n_removed = self.remove_hd_on_ligands(residue_group = rg) if n_removed > 0: print('Ligand %s has H atoms. %s were removed.' % (resname, n_removed), file=self.logger) if (resname == 'UNK'): continue if (resname == 'UNL'): continue if (resname == 'UNX'): continue # if (resname == 'IOD'): continue # if (resname == ' I'): continue # if (resname == 'NCO'): continue if resname not in ligands: ligands.append(resname) needs_cif = True n_removed_total = n_removed_total + n_removed # save new model if ligands have H atoms if n_removed_total > 0: print('Model has ligands with H atoms. Removed %s H atoms in total.' % n_removed_total, file=self.logger) new_fn = os.path.splitext(os.path.basename(self.json_data['pdb_file_updated']))[0] + '_trim.pdb' new_pdb_fn = os.path.join(self.dest_dir, new_fn) pdb_str = model.model_as_pdb() with open(new_pdb_fn, 'w') as fp: fp.write(pdb_str) #ph.write_pdb_file(new_pdb_fn) self.json_data['pdb_file_updated'] = new_pdb_fn print('New model (ligands without H atoms) saved to %s' % new_fn, file=self.logger) # Use the neutralized file where ligand H have been removed (if present) fn = self.json_data['pdb_file_updated'] if needs_cif: print('Ligands found in the input model:', file=self.logger) for ligand in ligands: print(ligand, file=self.logger) print('Run ready_set using %s ...' % os.path.basename(fn), file=self.logger) self.run_ready_set() else: print('No ligands found', file=self.logger) if self.params.add_hydrogen: print('Add hydrogens.', file=self.logger) print('Run ready_set using %s ...' % os.path.basename(fn), file=self.logger) self.run_ready_set() else: self.json_data['success_readyset'] = True
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, out=None, log=sys.stdout): if (len(args) == 0) or (args == ["--help"]): print(msg, file=log) defaults(log=log, silent=False) return parsed = defaults(log=log, silent=True) # processed_args = utils.process_command_line_args(args=args, log=log, master_params=parsed) params = processed_args.params.extract() # reflection_files = processed_args.reflection_files if (len(reflection_files) == 0): raise Sorry("No reflection file found.") crystal_symmetry = processed_args.crystal_symmetry if (crystal_symmetry is None): raise Sorry("No crystal symmetry found.") if (len(processed_args.pdb_file_names) == 0): raise Sorry("No PDB file found.") pdb_file_names = processed_args.pdb_file_names # rfs = reflection_file_server(crystal_symmetry=crystal_symmetry, reflection_files=reflection_files) parameters = extract_xtal_data.data_and_flags_master_params().extract() if (params.f_obs_label is not None): parameters.labels = params.f_obs_label if (params.r_free_flags_label is not None): parameters.r_free_flags.label = params.r_free_flags_label if (params.high_resolution is not None): parameters.high_resolution = params.high_resolution determine_data_and_flags_result = extract_xtal_data.run( reflection_file_server=rfs, parameters=parameters, data_parameter_scope="refinement.input.xray_data", flags_parameter_scope="refinement.input.xray_data.r_free_flags", data_description="X-ray data", keep_going=True, log=StringIO()) f_obs = determine_data_and_flags_result.f_obs # Data show_header(l="Data:", log=log) f_obs.show_comprehensive_summary(prefix=" ", f=log) # R-free-flags show_header(l="R-free-flags:", log=log) r_free_flags = determine_data_and_flags_result.r_free_flags test_flag_value = determine_data_and_flags_result.test_flag_value if (r_free_flags is None): r_free_flags = f_obs.array(data=flex.bool(f_obs.data().size(), False)) test_flag_value = None print(" not available", file=log) else: print(" flag value:", test_flag_value, file=log) # Model 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, crystal_symmetry=crystal_symmetry, log=StringIO()) # scattering_table = params.scattering_table exptl_method = pdb_inp.get_experiment_type() if (exptl_method is not None) and ("NEUTRON" in exptl_method): scattering_table = "neutron" model.setup_scattering_dictionaries(scattering_table=scattering_table, d_min=f_obs.d_min()) # # Model vs data # show_header(l="Model vs Data:", log=log) fmodel = mmtbx.f_model.manager(xray_structure=model.get_xray_structure(), f_obs=f_obs, r_free_flags=r_free_flags, twin_law=params.twin_law) fmodel.update_all_scales(update_f_part1=True) fmodel.show(log=log, show_header=False, show_approx=False) print(" r_work: %6.4f" % fmodel.r_work(), file=log) if (test_flag_value is not None): print(" r_free: %6.4f" % fmodel.r_free(), file=log) else: print(" r_free: None", file=log) print(file=log) n_outl = f_obs.data().size() - fmodel.f_obs().data().size() print(" Number of F-obs outliers:", n_outl, file=log) # # Extract information from PDB file header and output (if any) # pub_r_work = None pub_r_free = None pub_high = None pub_low = None pub_sigma = None pub_program_name = None pub_solv_cont = None pub_matthews = None published_results = pdb_inp.get_r_rfree_sigma(file_name=pdb_file_names[0]) if (published_results is not None): pub_r_work = published_results.r_work pub_r_free = published_results.r_free pub_high = published_results.high pub_low = published_results.low pub_sigma = published_results.sigma pub_program_name = pdb_inp.get_program_name() pub_solv_cont = pdb_inp.get_solvent_content() pub_matthews = pdb_inp.get_matthews_coeff() # show_header(l="Information extracted from PDB file header:", log=log) print(" program_name : %-s" % format_value("%s", pub_program_name), file=log) print(" year : %-s" % format_value("%s", pdb_inp.extract_header_year()), file=log) print(" r_work : %-s" % format_value("%s", pub_r_work), file=log) print(" r_free : %-s" % format_value("%s", pub_r_free), file=log) print(" high_resolution : %-s" % format_value("%s", pub_high), file=log) print(" low_resolution : %-s" % format_value("%s", pub_low), file=log) print(" sigma_cutoff : %-s" % format_value("%s", pub_sigma), file=log) print(" matthews_coeff : %-s" % format_value("%s", pub_matthews), file=log) print(" solvent_cont : %-s" % format_value("%s", pub_solv_cont), file=log) if (exptl_method is not None): print(" exptl_method : %-s" % format_value("%s", exptl_method), file=log) # # Recompute R-factors using published cutoffs fmodel_cut = fmodel tmp_sel = flex.bool(fmodel.f_obs().data().size(), True) if (pub_sigma is not None and fmodel.f_obs().sigmas() is not None): tmp_sel &= fmodel.f_obs().data() > fmodel.f_obs().sigmas() * pub_sigma if (pub_high is not None and abs(pub_high - fmodel.f_obs().d_min()) > 0.03): tmp_sel &= fmodel.f_obs().d_spacings().data() > pub_high if (pub_low is not None and abs(pub_low - fmodel.f_obs().d_max_min()[0]) > 0.03): tmp_sel &= fmodel.f_obs().d_spacings().data() < pub_low if (tmp_sel.count(True) != tmp_sel.size() and tmp_sel.count(True) > 0): show_header(l="After applying resolution and sigma cutoffs:", log=log) fmodel = mmtbx.f_model.manager( xray_structure=model.get_xray_structure(), f_obs=fmodel.f_obs().select(tmp_sel), r_free_flags=fmodel.r_free_flags().select(tmp_sel), twin_law=params.twin_law) fmodel.update_all_scales(update_f_part1=True) fmodel.show(log=log, show_header=False, show_approx=False) print(" r_work: %6.4f" % fmodel.r_work(), file=log) if (test_flag_value is not None): print(" r_free: %6.4f" % fmodel.r_free(), file=log) else: print(" r_free: None", file=log) print(file=log) n_outl = f_obs.data().size() - fmodel.f_obs().data().size() print(" Number of F-obs outliers:", n_outl, file=log)
def run(args, log=sys.stdout): if (len(args) == 0): print >> log, legend defaults(log=log) return # parsed = defaults(log=log) processed_args = mmtbx.utils.process_command_line_args( args=args, log=sys.stdout, master_params=parsed) params = processed_args.params.extract() reflection_files = processed_args.reflection_files if (len(reflection_files) == 0): if (params.hkl_file is None): raise Sorry("No reflection file found.") else: hkl_in = file_reader.any_file(params.hkl_file, force_type="hkl") hkl_in.assert_file_type("hkl") reflection_files = [hkl_in.file_object] crystal_symmetry = processed_args.crystal_symmetry if (crystal_symmetry is None): if (params.space_group is not None) and (params.unit_cell is not None): from cctbx import crystal crystal_symmetry = crystal.symmetry( space_group_info=params.space_group, unit_cell=params.unit_cell) else: raise Sorry("No crystal symmetry found.") if (len(processed_args.pdb_file_names) == 0): if (params.pdb_file is None): raise Sorry("No model file found.") else: pdb_file_names = [params.pdb_file] else: pdb_file_names = processed_args.pdb_file_names # rfs = reflection_file_utils.reflection_file_server( crystal_symmetry=crystal_symmetry, force_symmetry=True, reflection_files=reflection_files, err=StringIO()) parameters = mmtbx.utils.data_and_flags_master_params().extract() parameters.labels = params.f_obs_label parameters.r_free_flags.label = params.r_free_flags_label determine_data_and_flags_result = mmtbx.utils.determine_data_and_flags( reflection_file_server=rfs, parameters=parameters, keep_going=True, log=StringIO()) f_obs = determine_data_and_flags_result.f_obs print "Input data:" print " Iobs or Fobs:", f_obs.info().labels r_free_flags = determine_data_and_flags_result.r_free_flags print " Free-R flags:", r_free_flags.info().labels # parameters = mmtbx.utils.experimental_phases_params.extract() parameters.labels = params.hendrickson_lattman_coefficients_label experimental_phases_result = mmtbx.utils.determine_experimental_phases( reflection_file_server=rfs, parameters=parameters, log=StringIO(), parameter_scope="", working_point_group=None, symmetry_safety_check=True, ignore_all_zeros=True) if (experimental_phases_result is not None): print " HL coefficients:", experimental_phases_result.info().labels experimental_phases = extract_experimental_phases( experimental_phases=experimental_phases_result, f_obs=f_obs) # if (r_free_flags is None): r_free_flags = f_obs.array(data=flex.bool(f_obs.data().size(), False)) # pdb_inp = mmtbx.utils.pdb_inp_from_multiple_files(pdb_file_names, log=sys.stdout) model = mmtbx.model.manager(model_input=pdb_inp, process_input=False, crystal_symmetry=crystal_symmetry, log=sys.stdout) if (model.get_number_of_models() > 1): #XXX support multi-models raise Sorry("Multiple model file not supported in this tool.") # XXX Twining not supported xray_structure = model.get_xray_structure() if (not xray_structure.unit_cell().is_similar_to(f_obs.unit_cell())): raise Sorry( "The unit cells in the model and reflections files are not " + "isomorphous.") print "Input model:" print " number of atoms:", xray_structure.scatterers().size() fmodel = mmtbx.f_model.manager(xray_structure=xray_structure, r_free_flags=r_free_flags, f_obs=f_obs, abcd=experimental_phases) fmodel.update_all_scales( update_f_part1=True, remove_outliers=params.remove_f_obs_outliers, bulk_solvent_and_scaling=params.bulk_solvent_and_scaling) print "Overall statistics:" fmodel.info().show_all() # print "Output data:" if (params.output_file_name is not None): output_file_name = params.output_file_name else: pdb_file_bn = os.path.basename(pdb_file_names[0]) hkl_file_bn = os.path.basename(reflection_files[0].file_name()) try: pdb_file_prefix = pdb_file_bn[:pdb_file_bn.index(".")] except ValueError: pdb_file_prefix = pdb_file_bn try: hkl_file_prefix = hkl_file_bn[:hkl_file_bn.index(".")] except ValueError: hkl_file_prefix = hkl_file_bn output_file_name = "%s_%s.mtz" % (pdb_file_prefix, hkl_file_prefix) print " file name:", output_file_name print " to see the contnt of %s:" % output_file_name print " phenix.mtz.dump %s" % output_file_name out = open(output_file_name, "w") fmodel.export(out=out) out.close() print "All done." return output_file_name
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 __init__(self, model, target_map, refine_ncs_operators=False, number_of_cycles=1, cycles_to_converge=2, log=None): # completely new way of doing this. using RSR macro-cycle # for test compatibility: print("Minimizing using reference map...", file=log) if model.ncs_constraints_present(): print(" Minimizing... (NCS)", file=log) else: print(" Minimizing...", file=log) from phenix.refinement.macro_cycle_real_space import run as rsr_mc_run import scitbx.math from phenix.command_line.real_space_refine import extract_rigid_body_selections from phenix.command_line.real_space_refine import master_params as rsr_master_params from mmtbx.refinement.real_space.utils import target_map as rsr_target_map import mmtbx.idealized_aa_residues.rotamer_manager sin_cos_table = scitbx.math.sin_cos_table(n=10000) params = rsr_master_params().extract() params.pdb_interpretation = model._pdb_interpretation_params.pdb_interpretation params.refinement.run = "minimization_global+local_grid_search" params.refine_ncs_operators = False params.output.write_all_states = True rotamer_manager = mmtbx.idealized_aa_residues.rotamer_manager.load( rotamers="favored") rigid_body_selections = extract_rigid_body_selections( params=params, ncs_groups=model.get_ncs_groups(), pdb_hierarchy=model.get_hierarchy()) rsr_tm = rsr_target_map(map_data=target_map, xray_structure=model.get_xray_structure(), d_min=3.8, atom_radius=params.refinement.atom_radius) res = rsr_mc_run(params=params, model=model, target_map=rsr_tm, log=log, ncs_groups=model.get_ncs_groups(), rotamer_manager=rotamer_manager, sin_cos_table=sin_cos_table, rigid_body_selections=rigid_body_selections) model.set_sites_cart_from_hierarchy(res.model.get_hierarchy()) res.structure_monitor.states_collector.write( file_name="rsr_all_states.pdb") return # end =================================================== from mmtbx.refinement.geometry_minimization import add_rotamer_restraints from mmtbx.refinement.minimization_monitor import minimization_monitor self.model = model self.log = log print("Minimizing using reference map...", file=self.log) self.log.flush() # copy-paste from cctbx_project/mmtbx/refinement/geometry_minimization.py: # minimize_wrapper_for_ramachandran self.model.get_restraints_manager().geometry.pair_proxies( sites_cart=self.model.get_sites_cart()) ncs_restraints_group_list = self.model.get_ncs_groups() if ncs_restraints_group_list is None: ncs_restraints_group_list = [] ncs_groups = None if len(ncs_restraints_group_list) > 0: ncs_groups = ncs_restraints_group_list min_monitor = minimization_monitor( number_of_cycles=number_of_cycles, max_number_of_cycles=20, cycles_to_converge=cycles_to_converge, mode="no_outliers") selection_real_space = None import mmtbx.refinement.real_space.weight self.w = 1 print("number_of_cycles", number_of_cycles, file=log) print("Stats before minimization:", file=log) ms = self.model.geometry_statistics() ms.show(log=log) while min_monitor.need_more_cycles(): print("Cycle number", min_monitor.get_current_cycle_n(), file=self.log) self.model.get_restraints_manager().geometry.\ update_ramachandran_restraints_phi_psi_targets( hierarchy=self.model.get_hierarchy()) print(" Updating rotamer restraints...", file=self.log) add_rotamer_restraints( pdb_hierarchy=self.model.get_hierarchy(), restraints_manager=self.model.get_restraints_manager(), selection=None, sigma=5, mode="fix_outliers", accept_allowed=False, mon_lib_srv=self.model.get_mon_lib_srv(), rotamer_manager=self.model.get_rotamer_manager()) self.model.set_sites_cart_from_hierarchy() if min_monitor.need_weight_optimization(): # if self.w is None: print(" Determining weight...", file=self.log) self.log.flush() self.weight = mmtbx.refinement.real_space.weight.run( map_data=target_map, xray_structure=self.model.get_xray_structure(), pdb_hierarchy=self.model.get_hierarchy(), geometry_restraints_manager=self.model. get_restraints_manager(), rms_bonds_limit=0.015, rms_angles_limit=1.0, ncs_groups=ncs_groups) # division is to put more weight onto restraints. Checked. Works. self.w = self.weight.weight / 3.0 # self.w = self.weight.weight/15.0 # self.w = 0 # self.w = self.weight.weight for s in self.weight.msg_strings: print(s, file=self.log) if ncs_restraints_group_list is None or len( ncs_restraints_group_list) == 0: #No NCS print(" Minimizing...", file=self.log) print(" with weight %f" % self.w, file=self.log) self.log.flush() refine_object = simple( target_map=target_map, selection=None, max_iterations=150, geometry_restraints_manager=self.model. get_restraints_manager().geometry, selection_real_space=selection_real_space, states_accumulator=None, ncs_groups=ncs_groups) refine_object.refine( weight=self.w, xray_structure=self.model.get_xray_structure()) self.rmsd_bonds_final, self.rmsd_angles_final = refine_object.rmsds( ) print("RMSDS:", self.rmsd_bonds_final, self.rmsd_angles_final, file=log) # print >> log, "sizes:", len(refine_object.sites_cart()), len(self.xrs.scatterers()) self.model.set_sites_cart(refine_object.sites_cart(), update_grm=True) # print >> log, "sizes", self.xrs.scatterers() else: # Yes NCS # copy-paste from macro_cycle_real_space.py # !!! Don't rearrange NCS groups here because master was just fixed! # import mmtbx.ncs.ncs_utils as nu # nu.get_list_of_best_ncs_copy_map_correlation( # ncs_groups = ncs_restraints_group_list, # xray_structure = self.model.get_xray_structure(), # map_data = target_map, # d_min = 3) print(" Minimizing... (NCS)", file=self.log) tfg_obj = mmtbx.refinement.minimization_ncs_constraints.\ target_function_and_grads_real_space( map_data = target_map, xray_structure = self.model.get_xray_structure(), ncs_restraints_group_list = ncs_restraints_group_list, refine_selection = None, real_space_gradients_delta = 1, restraints_manager = self.model.get_restraints_manager(), data_weight = self.w, refine_sites = True) minimized = mmtbx.refinement.minimization_ncs_constraints.lbfgs( target_and_grads_object=tfg_obj, xray_structure=self.model.get_xray_structure(), ncs_restraints_group_list=ncs_restraints_group_list, refine_selection=None, finite_grad_differences_test=False, max_iterations=100, refine_sites=True) self.model.set_sites_cart(tfg_obj.xray_structure.sites_cart()) ncs_restraints_group_list.recalculate_ncs_transforms( self.model.get_sites_cart()) ms = self.model.geometry_statistics() min_monitor.save_cycle_results(geometry=ms) ms.show(log=log)
def exercise_bond_over_symmetry_2(mon_lib_srv, ener_lib): """ This test is to illustrate that bond over symmetry actually adds 2 proxies. """ from cctbx.geometry_restraints.linking_class import linking_class origin_ids = linking_class() pdb_inp = iotbx.pdb.input(source_info=None, lines=raw_records10) params = mmtbx.model.manager.get_default_pdb_interpretation_params() params.pdb_interpretation.restraints_library.mcl = False model = mmtbx.model.manager(model_input=pdb_inp, pdb_interpretation_params=params, log=null_out(), build_grm=True) grm = model.get_restraints_manager().geometry simple, asu = grm.get_all_bond_proxies() assert (simple.size(), asu.size()) == (0, 0) h = model.get_hierarchy() sites_cart = h.atoms().extract_xyz() site_labels = model.get_xray_structure().scatterers().extract_labels() pair_proxies = grm.pair_proxies(flags=None, sites_cart=sites_cart) out = StringIO() pair_proxies.bond_proxies.show_sorted(by_value="residual", sites_cart=sites_cart, site_labels=site_labels, f=out, prefix="") outtxt = out.getvalue() # print(outtxt) proxy = geometry_restraints.bond_simple_proxy( i_seqs=(0, 1), distance_ideal=2.9, weight=400, origin_id=origin_ids.get_origin_id('hydrogen bonds')) grm.add_new_bond_restraints_in_place(proxies=[proxy], sites_cart=h.atoms().extract_xyz()) simple, asu = grm.get_all_bond_proxies() # print(simple.size(), asu.size()) assert (simple.size(), asu.size()) == (0, 2) sites_cart = h.atoms().extract_xyz() site_labels = model.get_xray_structure().scatterers().extract_labels() pair_proxies = grm.pair_proxies(flags=None, sites_cart=sites_cart) out = StringIO() pair_proxies.bond_proxies.show_sorted(by_value="residual", sites_cart=sites_cart, site_labels=site_labels, f=out, prefix="") outtxt = out.getvalue() # print(outtxt) assert_lines_in_text( outtxt, """\ bond pdb=" CA HIS A 2 " pdb=" N MET A 1 " ideal model delta sigma weight residual sym.op. 2.900 1.998 0.902 5.00e-02 4.00e+02 3.25e+02 x,y+1,z bond pdb=" N MET A 1 " pdb=" CA HIS A 2 " ideal model delta sigma weight residual sym.op. 2.900 1.998 0.902 5.00e-02 4.00e+02 3.25e+02 x,y-1,z """) es = grm.energies_sites(sites_cart=sites_cart, compute_gradients=True) out = StringIO() es.show(f=out) outtxt = out.getvalue() # print(outtxt) # do for x coordinate # ATOM 1 N MET A 1 9.821 1.568 5.000 1.00 66.07 N # ATOM 2 CA HIS A 2 9.946 12.171 5.357 1.00 66.55 C # calculation is from geometry_restraints/bond.h: gradient_0() # weight * 2 * delta_slack * d_distance_d_site_0(epsilon); # print("X gradient:", 400*2*0.902*(9.946-9.821)) # 90 # Note that n=2 but residual sum is 325.349. 349 is chopped off in rounding in # cctbx/geometry_restraints/__init__py, def _bond_show_sorted_impl(...) # where %6.2e is used. in cctbx/geometry_restraints/energies.py: def show() # %.6g is used which is showing more numbers. assert_lines_in_text(outtxt, """\ bond_residual_sum (n=2): 325.349""") # print("Gradients:", list(es.gradients)) # Seems that gradients were splitted in half (note the X gradient is 90 8 lines above) assert approx_equal( list(es.gradients), [(45.135801792665134, -708.451544937652, 128.90784991984805), (-45.13580179266516, 708.4515449376522, -128.90784991984813)])
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")