def exercise_trigonometric_ff(): from math import cos, sin, pi sgi = sgtbx.space_group_info("P1") cs = sgi.any_compatible_crystal_symmetry(volume=1000) miller_set = miller.build_set(cs, anomalous_flag=False, d_min=1) miller_set = miller_set.select(flex.random_double(miller_set.size()) < 0.2) for i in xrange(5): sites = flex.random_double(9) x1, x2, x3 = (matrix.col(sites[:3]), matrix.col(sites[3:6]), matrix.col(sites[6:])) xs = xray.structure(crystal.special_position_settings(cs)) for x in (x1, x2, x3): sc = xray.scatterer(site=x, scattering_type="const") sc.flags.set_grad_site(True) xs.add_scatterer(sc) f_sq = structure_factors.f_calc_modulus_squared(xs) for h in miller_set.indices(): h = matrix.col(h) phi1, phi2, phi3 = 2 * pi * h.dot(x1), 2 * pi * h.dot(x2), 2 * pi * h.dot(x3) fc_mod_sq = 3 + 2 * (cos(phi1 - phi2) + cos(phi2 - phi3) + cos(phi3 - phi1)) g = [] g.extend(-2 * (sin(phi1 - phi2) - sin(phi3 - phi1)) * 2 * pi * h) g.extend(-2 * (sin(phi2 - phi3) - sin(phi1 - phi2)) * 2 * pi * h) g.extend(-2 * (sin(phi3 - phi1) - sin(phi2 - phi3)) * 2 * pi * h) grad_fc_mod_sq = g f_sq.linearise(h) assert approx_equal(f_sq.observable, fc_mod_sq) assert approx_equal(f_sq.grad_observable, grad_fc_mod_sq)
def iass_as_xray_structure(self, iass): ias_xray_structure = xray.structure( crystal_symmetry = self.xray_structure.crystal_symmetry()) for ias in iass: assert ias.status is not None if(ias.status): if(self.params.use_map): site_cart = ias.peak_position_cart b_iso = ias.b_iso q = ias.q else: site_cart = ias.site_cart_predicted b_iso = (ias.atom_1.b_iso + ias.atom_2.b_iso)*0.5 q = self.params.initial_ias_occupancy if(b_iso is None): b_iso = (ias.atom_1.b_iso + ias.atom_2.b_iso)*0.5 if(q is None): q = self.params.initial_ias_occupancy if(site_cart is None): site_cart = ias.site_cart_predicted assert [b_iso, q].count(None) == 0 if(site_cart is not None): ias_scatterer = xray.scatterer( label = ias.name, scattering_type = ias.name, site = self.xray_structure.unit_cell().fractionalize(site_cart), u = adptbx.b_as_u(b_iso), occupancy = q) ias_xray_structure.add_scatterer(ias_scatterer) ias_xray_structure.scattering_type_registry( custom_dict = ias_scattering_dict) return ias_xray_structure
def xray_structure(O, u_iso=0): from cctbx import xray from cctbx import crystal return xray.structure( crystal_symmetry=crystal.symmetry( unit_cell=O.unit_cell(), space_group_symbol="P1"), scatterers=O.scatterers(u_iso=u_iso))
def move_sites_if_necessary_for_shelx_fvar_encoding(xray_structure): from cctbx import xray from scitbx import matrix xs = xray_structure scs = xs.scatterers().deep_copy() sstab = xs.site_symmetry_table() for i_sc in xs.special_position_indices(): site = scs[i_sc].site ss = sstab.get(i_sc) sos = sos_orig = ss.special_op_simplified() fvars = [None] coded_variables = sos.shelx_fvar_encoding(site=site, fvars=fvars) if coded_variables is None: site_0 = matrix.col(site).each_mod_short() for u in unit_shifts_prioritized: site = site_0 + matrix.col(u) ss = xs.site_symmetry(site) sos = ss.special_op_simplified() coded_variables = sos.shelx_fvar_encoding(site=site, fvars=fvars) if coded_variables is not None: scs[i_sc].site = site break else: raise_special_position_constraints_cannot_be_encoded(sos_orig) result = xray.structure( special_position_settings=xs, scatterers=scs, non_unit_occupancy_implies_min_distance_sym_equiv_zero=xs.non_unit_occupancy_implies_min_distance_sym_equiv_zero(), scattering_type_registry=xs.scattering_type_registry(), ) assert result.special_position_indices().all_eq(xs.special_position_indices()) return result
def exercise_symmetry_equivalent(): xs = xray.structure(crystal_symmetry=crystal.symmetry( unit_cell=(1, 2, 3), space_group_symbol='hall: P 2x'), scatterers=flex.xray_scatterer( (xray.scatterer("C", site=(0.1, 0.2, 0.3)), ))) xs.scatterers()[0].flags.set_grad_site(True) connectivity_table = smtbx.utils.connectivity_table(xs) reparametrisation = constraints.reparametrisation(xs, [], connectivity_table) site_0 = reparametrisation.add(constraints.independent_site_parameter, scatterer=xs.scatterers()[0]) g = sgtbx.rt_mx('x,-y,-z') symm_eq = reparametrisation.add( constraints.symmetry_equivalent_site_parameter, site=site_0, motion=g) reparametrisation.finalise() assert approx_equal(symm_eq.original.scatterers[0].site, (0.1, 0.2, 0.3), eps=1e-15) assert str(symm_eq.motion) == 'x,-y,-z' assert symm_eq.is_variable reparametrisation.linearise() assert approx_equal(symm_eq.value, g * site_0.value, eps=1e-15) reparametrisation.store() assert approx_equal(symm_eq.value, (0.1, -0.2, -0.3), eps=1e-15) assert approx_equal(site_0.value, (0.1, 0.2, 0.3), eps=1e-15)
def exercise_is_simple_interaction(): for space_group_symbol in ["P1", "P41"]: for shifts in flex.nested_loop((-2, -2, -2), (2, 2, 2), False): shifts = matrix.col(shifts) structure = xray.structure( crystal_symmetry=crystal.symmetry( unit_cell=(10, 10, 20, 90, 90, 90), space_group_symbol=space_group_symbol), scatterers=flex.xray_scatterer([ xray.scatterer(label="O", site=shifts + matrix.col((0, 0, 0))), xray.scatterer(label="N", site=shifts + matrix.col((0.5, 0.5, 0))), xray.scatterer(label="C", site=shifts + matrix.col((0.25, 0.25, 0))) ])) asu_mappings = structure.asu_mappings(buffer_thickness=7) pair_generator = crystal.neighbors_simple_pair_generator( asu_mappings=asu_mappings, distance_cutoff=7) simple_interactions = {} for i_pair, pair in enumerate(pair_generator): if (asu_mappings.is_simple_interaction(pair)): assert asu_mappings_is_simple_interaction_emulation( asu_mappings, pair) key = (pair.i_seq, pair.j_seq) assert simple_interactions.get(key, None) is None simple_interactions[key] = 1 else: assert not asu_mappings_is_simple_interaction_emulation( asu_mappings, pair) assert len(simple_interactions) == 2 assert simple_interactions[(0, 2)] == 1 assert simple_interactions[(1, 2)] == 1
def quartz(): return xray.structure( crystal_symmetry=crystal.symmetry( (5.01,5.01,5.47,90,90,120), "P6222"), scatterers=flex.xray_scatterer([ xray.scatterer("Si", (1/2.,1/2.,1/3.)), xray.scatterer("O", (0.197,-0.197,0.83333))]))
def demo(): """ Result of ICSD query: N * -Cr2O3-[R3-CH] Baster, M.;Bouree, F.;Kowalska, A.;Latacz, Z(2000) C 4.961950 4.961950 13.597400 90.000000 90.000000 120.000000 S GRUP R -3 C A Cr1 0.000000 0.000000 0.347570 0.000000 A O1 0.305830 0.000000 0.250000 """ crystal_symmetry = crystal.symmetry( unit_cell="4.961950 4.961950 13.597400 90.000000 90.000000 120.000000", space_group_symbol="R -3 C") scatterers = flex.xray_scatterer() scatterers.append(xray.scatterer( label="Cr1", site=(0.000000,0.000000,0.347570))) scatterers.append(xray.scatterer( label="O1", site=(0.305830,0.000000,0.250000))) icsd_structure = xray.structure( crystal_symmetry=crystal_symmetry, scatterers=scatterers) icsd_structure.show_summary().show_scatterers() print icsd_structure.show_distances(distance_cutoff=2.5) print primitive_structure = icsd_structure.primitive_setting() primitive_structure.show_summary().show_scatterers() print p1_structure = primitive_structure.expand_to_p1() p1_structure.show_summary().show_scatterers() print print "OK"
def exercise_symmetry_equivalent(): xs = xray.structure( crystal_symmetry=crystal.symmetry( unit_cell=(1, 2, 3), space_group_symbol='hall: P 2x'), scatterers=flex.xray_scatterer(( xray.scatterer("C", site=(0.1, 0.2, 0.3)), ))) xs.scatterers()[0].flags.set_grad_site(True) connectivity_table = smtbx.utils.connectivity_table(xs) reparametrisation = constraints.reparametrisation( xs, [], connectivity_table) site_0 = reparametrisation.add(constraints.independent_site_parameter, scatterer=xs.scatterers()[0]) g = sgtbx.rt_mx('x,-y,-z') symm_eq = reparametrisation.add( constraints.symmetry_equivalent_site_parameter, site=site_0, motion=g) reparametrisation.finalise() assert approx_equal(symm_eq.original.scatterers[0].site, (0.1, 0.2, 0.3), eps=1e-15) assert str(symm_eq.motion) == 'x,-y,-z' assert symm_eq.is_variable reparametrisation.linearise() assert approx_equal(symm_eq.value, g*site_0.value, eps=1e-15) reparametrisation.store() assert approx_equal(symm_eq.value, (0.1, -0.2, -0.3), eps=1e-15) assert approx_equal(site_0.value, (0.1, 0.2, 0.3), eps=1e-15)
def extract_structure(self, phase_nr=1): """This method tries to extract the crystal structure from the parsed pcrfile. :returns: the extracted structure :rtype: cctbx.xray.structure """ from cctbx import xray from cctbx import crystal from cctbx.array_family import flex p = self.cfg['phases'][str(phase_nr)] atoms = p['atoms'] unit_cell = [ p['a'], p['b'], p['c'], p['alpha'], p['beta'], p['gamma'] ] special_position_settings=crystal.special_position_settings( crystal_symmetry=crystal.symmetry( unit_cell=unit_cell, space_group_symbol=p['SYMB'])) scatterers = [] for k, a in atoms.iteritems(): scatterers.append(xray.scatterer(label=a['LABEL'], scattering_type=a['NTYP'], site=(a['X'], a['Y'], a['Z']), b=a['B'])) scatterers_flex=flex.xray_scatterer(scatterers) structure = xray.structure(special_position_settings=special_position_settings, scatterers=scatterers_flex) return structure.as_py_code()
def mg_structure_dict_to_cctbx_crystal_structure(d): """ Compatible with pymatgen Structure.to_dict() format to a cctbx crystal structure object """ params = " ".join([ str(o) for o in [ d['lattice']['a'], d['lattice']['b'], d['lattice']['c'], d['lattice']['alpha'], d['lattice']['beta'], d['lattice']['gamma'] ] ]) unit_cell = uctbx.unit_cell(params) symbol = 'P 1' crystal_symmetry = crystal.symmetry(unit_cell=unit_cell, space_group_symbol=symbol) """ site = tuple([0.0, 0.20, 0.3]) scatterers.append(xray.scatterer(label=lable, site=site, occupancy=occupancy) crystal_structure = xray.structure(crystal_symmetry=crystal_symmetry, scatterers=scatterers) """ scatterers = flex.xray_scatterer() for site in d['sites']: abc = tuple(site['abc']) for specie in site['species']: label = specie['element'].encode('utf8') occupancy = float(specie['occu']) scatterers.append( xray.scatterer(label=label, site=abc, occupancy=occupancy)) crystal_structure = xray.structure(crystal_symmetry=crystal_symmetry, scatterers=scatterers) return crystal_structure
def __init__(self,unitcell,spacegroup,scatterers): self.sg=sgtbx.space_group_info(spacegroup) self.uc=uctbx.unit_cell(unitcell) self.scatt=scatterers self.cctbx_scatterers=flex.xray_scatterer() for s in self.scatt: self.cctbx_scatterers.append(xray.scatterer(label=s.label,site=s.site,occupancy=s.occupancy,u=s.u_iso)) try: #old cctbx version xray.add_scatterers_ext(unit_cell=self.uc, space_group=self.sg.group(), scatterers=self.cctbx_scatterers, site_symmetry_table=sgtbx.site_symmetry_table(), site_symmetry_table_for_new=sgtbx.site_symmetry_table(), min_distance_sym_equiv=0.5, u_star_tolerance=0, assert_min_distance_sym_equiv=True) except: # cctbx version >= 2011_04_06_0217 #print "Whoops, cctbx version 2011" xray.add_scatterers_ext(unit_cell=self.uc, space_group=self.sg.group(), scatterers=self.cctbx_scatterers, site_symmetry_table=sgtbx.site_symmetry_table(), site_symmetry_table_for_new=sgtbx.site_symmetry_table(), min_distance_sym_equiv=0.5, u_star_tolerance=0, assert_min_distance_sym_equiv=True, non_unit_occupancy_implies_min_distance_sym_equiv_zero=False) cs=crystal.symmetry(self.uc,spacegroup) sp=crystal.special_position_settings(cs) self.structure=xray.structure(sp,self.cctbx_scatterers) self.structure_as_P1=self.structure.expand_to_p1()
def move_sites_if_necessary_for_shelx_fvar_encoding(xray_structure): from cctbx import xray from scitbx import matrix xs = xray_structure scs = xs.scatterers().deep_copy() sstab = xs.site_symmetry_table() for i_sc in xs.special_position_indices(): site = scs[i_sc].site ss = sstab.get(i_sc) sos = sos_orig = ss.special_op_simplified() fvars = [None] coded_variables = sos.shelx_fvar_encoding(site=site, fvars=fvars) if (coded_variables is None): site_0 = matrix.col(site).each_mod_short() for u in unit_shifts_prioritized: site = site_0 + matrix.col(u) ss = xs.site_symmetry(site) sos = ss.special_op_simplified() coded_variables = sos.shelx_fvar_encoding(site=site, fvars=fvars) if (coded_variables is not None): scs[i_sc].site = site break else: raise_special_position_constraints_cannot_be_encoded(sos_orig) result = xray.structure( special_position_settings=xs, scatterers=scs, non_unit_occupancy_implies_min_distance_sym_equiv_zero=xs. non_unit_occupancy_implies_min_distance_sym_equiv_zero(), scattering_type_registry=xs.scattering_type_registry()) assert result.special_position_indices().all_eq( xs.special_position_indices()) return result
def add_new_solvent(self): b_solv = self.params.b_iso new_scatterers = flex.xray_scatterer( self.sites.size(), xray.scatterer(occupancy = self.params.occupancy, b = b_solv, scattering_type = self.params.scattering_type, label = 'HOH')) new_scatterers.set_sites(self.sites) solvent_xray_structure = xray.structure( special_position_settings = self.model.xray_structure, scatterers = new_scatterers) xrs_sol = self.model.xray_structure.select(self.model.solvent_selection()) xrs_mac = self.model.xray_structure.select(~self.model.solvent_selection()) xrs_sol = xrs_sol.concatenate(other = solvent_xray_structure) sol_sel = flex.bool(xrs_mac.scatterers().size(), False) sol_sel.extend( flex.bool(xrs_sol.scatterers().size(), True) ) self.model.add_solvent( solvent_xray_structure = solvent_xray_structure, residue_name = self.params.output_residue_name, atom_name = self.params.output_atom_name, chain_id = self.params.output_chain_id, refine_occupancies = self.params.refine_occupancies, refine_adp = self.params.new_solvent) self.fmodel.update_xray_structure( xray_structure = self.model.xray_structure, update_f_calc = False)
def exercise_intersection(): sites_frac = flex.vec3_double([ (0.02,0.10,0.02), (0.10,0.02,0.40), (0.98,0.10,0.60), (0.10,0.98,0.80), (0.20,0.50,0.98)]) from cctbx import xray xray_structure = xray.structure( crystal_symmetry=crystal.symmetry( unit_cell=(30,30,50,90,90,120), space_group_symbol="P1"), scatterers=flex.xray_scatterer([ xray.scatterer(label=str(i), scattering_type="Si", site=site_frac) for i,site_frac in enumerate(sites_frac)])) d_min = 0.7 f_calc = xray_structure.structure_factors(d_min=d_min).f_calc() fft_map = f_calc.fft_map(resolution_factor=1/6.) fft_map.apply_sigma_scaling() density_map = fft_map.real_map_unpadded() # cm1 = xray_structure.center_of_mass() cm2 = maptbx.center_of_mass(map_data=density_map, unit_cell=xray_structure.unit_cell(), cutoff=20) #large cutoff to make map look like point scattereres assert approx_equal(cm1, cm2, 0.1)
def __init__(self, n_runs, **kwds): libtbx.adopt_optional_init_args(self, kwds) self.n_runs = n_runs self.crystal_symmetry = crystal.symmetry( unit_cell=uctbx.unit_cell((5.1534, 5.1534, 8.6522, 90, 90, 120)), space_group_symbol='Hall: P 6c') self.structure = xray.structure( self.crystal_symmetry.special_position_settings(), flex.xray_scatterer(( xray.scatterer('K1', site=(0, 0, -0.00195), u=self.u_cif_as_u_star((0.02427, 0.02427, 0.02379, 0.01214, 0.00000, 0.00000))), xray.scatterer('S1', site=(1/3, 2/3, 0.204215), u=self.u_cif_as_u_star((0.01423, 0.01423, 0.01496, 0.00712, 0.00000, 0.00000 ))), xray.scatterer('Li1', site=(1/3, 2/3, 0.815681), u=self.u_cif_as_u_star((0.02132, 0.02132, 0.02256, 0.01066, 0.00000, 0.00000 ))), xray.scatterer('O1', site=(1/3, 2/3, 0.035931), u=self.u_cif_as_u_star((0.06532, 0.06532, 0.01669, 0.03266, 0.00000, 0.00000 ))), xray.scatterer('O2', site=(0.343810, 0.941658, 0.258405), u=self.u_cif_as_u_star((0.02639, 0.02079, 0.05284, 0.01194, -0.00053,-0.01180 ))) ))) mi = self.crystal_symmetry.build_miller_set(anomalous_flag=False, d_min=0.5) fo_sq = mi.structure_factors_from_scatterers( self.structure, algorithm="direct").f_calc().norm() self.fo_sq = fo_sq.customized_copy(sigmas=flex.double(fo_sq.size(), 1))
def add_new_solvent(self): b_solv = self.params.b_iso new_scatterers = flex.xray_scatterer( self.sites.size(), xray.scatterer(occupancy=self.params.occupancy, b=b_solv, scattering_type=self.params.scattering_type, label='HOH')) new_scatterers.set_sites(self.sites) solvent_xray_structure = xray.structure( special_position_settings=self.model.xray_structure, scatterers=new_scatterers) xrs_sol = self.model.xray_structure.select( self.model.solvent_selection()) xrs_mac = self.model.xray_structure.select( ~self.model.solvent_selection()) xrs_sol = xrs_sol.concatenate(other=solvent_xray_structure) sol_sel = flex.bool(xrs_mac.scatterers().size(), False) sol_sel.extend(flex.bool(xrs_sol.scatterers().size(), True)) self.model.add_solvent( solvent_xray_structure=solvent_xray_structure, residue_name=self.params.output_residue_name, atom_name=self.params.output_atom_name, chain_id=self.params.output_chain_id, refine_occupancies=self.params.refine_occupancies, refine_adp=self.params.new_solvent) self.fmodel.update_xray_structure( xray_structure=self.model.xray_structure, update_f_calc=False)
def test_spacegroup_tidy_pickling(): quartz_structure = xray.structure( crystal_symmetry=crystal.symmetry( unit_cell=(5.01,5.01,5.47,90,90,120), space_group_symbol="P6222"), scatterers=flex.xray_scatterer( [ xray.scatterer(label="Si", site=(1/2.,1/2.,1/3.), u=0.2), xray.scatterer(label="O", site=(0.197,-0.197,0.83333), u=0) ]) ) asu_mappings = quartz_structure.asu_mappings(buffer_thickness=2) pair_asu_table = crystal.pair_asu_table(asu_mappings=asu_mappings) pair_asu_table.add_all_pairs(distance_cutoff=1.7) pair_sym_table = pair_asu_table.extract_pair_sym_table() new_asu_mappings = quartz_structure.asu_mappings(buffer_thickness=5) new_pair_asu_table = crystal.pair_asu_table(asu_mappings=new_asu_mappings) new_pair_asu_table.add_pair_sym_table(sym_table=pair_sym_table) spg = new_pair_asu_table.asu_mappings().space_group() pspg = pickle.loads(pickle.dumps(spg)) mstr = "" pmstr = "" for rt in spg.all_ops(): mstr += rt.r().as_xyz() + "\n" for rt in pspg.all_ops(): pmstr += rt.r().as_xyz() + "\n" assert mstr == pmstr
def extract_structure(self, phase_nr=1): """This method tries to extract the crystal structure from the parsed pcrfile. :returns: the extracted structure :rtype: cctbx.xray.structure """ from cctbx import xray from cctbx import crystal from cctbx.array_family import flex p = self.cfg['phases'][str(phase_nr)] atoms = p['atoms'] unit_cell = [p['a'], p['b'], p['c'], p['alpha'], p['beta'], p['gamma']] special_position_settings = crystal.special_position_settings( crystal_symmetry=crystal.symmetry(unit_cell=unit_cell, space_group_symbol=p['SYMB'])) scatterers = [] for k, a in atoms.iteritems(): scatterers.append( xray.scatterer(label=a['LABEL'], scattering_type=a['NTYP'], site=(a['X'], a['Y'], a['Z']), b=a['B'])) scatterers_flex = flex.xray_scatterer(scatterers) structure = xray.structure( special_position_settings=special_position_settings, scatterers=scatterers_flex) return structure.as_py_code()
def exercise_trigonometric_ff(): from math import cos, sin, pi sgi = sgtbx.space_group_info("P1") cs = sgi.any_compatible_crystal_symmetry(volume=1000) miller_set = miller.build_set(cs, anomalous_flag=False, d_min=1) miller_set = miller_set.select(flex.random_double(miller_set.size()) < 0.2) for i in range(5): sites = flex.random_double(9) x1, x2, x3 = (matrix.col(sites[:3]), matrix.col(sites[3:6]), matrix.col(sites[6:])) xs = xray.structure(crystal.special_position_settings(cs)) for x in (x1, x2, x3): sc = xray.scatterer(site=x, scattering_type="const") sc.flags.set_grad_site(True) xs.add_scatterer(sc) f_sq = structure_factors.f_calc_modulus_squared(xs) for h in miller_set.indices(): h = matrix.col(h) phi1, phi2, phi3 = 2 * pi * h.dot(x1), 2 * pi * h.dot( x2), 2 * pi * h.dot(x3) fc_mod_sq = 3 + 2 * (cos(phi1 - phi2) + cos(phi2 - phi3) + cos(phi3 - phi1)) g = [] g.extend(-2 * (sin(phi1 - phi2) - sin(phi3 - phi1)) * 2 * pi * h) g.extend(-2 * (sin(phi2 - phi3) - sin(phi1 - phi2)) * 2 * pi * h) g.extend(-2 * (sin(phi3 - phi1) - sin(phi2 - phi3)) * 2 * pi * h) grad_fc_mod_sq = g f_sq.linearise(h) assert approx_equal(f_sq.observable, fc_mod_sq) assert approx_equal(f_sq.grad_observable, grad_fc_mod_sq)
def _append_to_model(self): mean_b = flex.mean(self.model.get_hierarchy().atoms().extract_b()) self.ma.add(" new water B factors will be set to mean B: %8.3f" % mean_b) sp = crystal.special_position_settings(self.model.crystal_symmetry()) scatterers = flex.xray_scatterer() for site_frac in self.sites_frac_water: sc = xray.scatterer(label="O", site=site_frac, u=adptbx.b_as_u(mean_b), occupancy=1.0) scatterers.append(sc) xrs_water = xray.structure(sp, scatterers) # chain_ids_taken = [] for chain in self.model.get_hierarchy().chains(): chain_ids_taken.append(chain.id) unique_taken = list(set(chain_ids_taken)) if (len(unique_taken) == 1): solvent_chain = unique_taken[0] else: for solvent_chain in all_chain_ids(): if (not solvent_chain in chain_ids_taken): break self.ma.add(" new water will have chain ID: '%s'" % solvent_chain) # self.model.add_solvent(solvent_xray_structure=xrs_water, atom_name="O", residue_name="HOH", chain_id=solvent_chain, refine_adp="isotropic")
def compare_with_cctbx_structure_factors(n_scatt, n_refl, output_lines): from cctbx import xray from cctbx import miller from cctbx import crystal from cctbx.array_family import flex crystal_symmetry = crystal.symmetry(unit_cell=(11, 12, 13, 90, 90, 90), space_group_symbol="P1") scatterers = flex.xray_scatterer() miller_indices = flex.miller_index() f_calc = flex.complex_double() for line in output_lines: flds = line.split() assert len(flds) in [4, 5] if (len(flds) == 4): x, y, z, b_iso = [float(s) for s in flds] scatterers.append( xray.scatterer(site=(x, y, z), b=b_iso, scattering_type="const")) else: miller_indices.append([int(s) for s in flds[:3]]) f_calc.append(complex(float(flds[3]), float(flds[4]))) assert scatterers.size() == n_scatt assert miller_indices.size() == n_refl xs = xray.structure(crystal_symmetry=crystal_symmetry, scatterers=scatterers) fc = miller_array = miller.set(crystal_symmetry=crystal_symmetry, indices=miller_indices, anomalous_flag=False).array(data=f_calc) fc2 = fc.structure_factors_from_scatterers(xray_structure=xs, algorithm="direct", cos_sin_table=False).f_calc() for f1, f2 in zip(fc.data(), fc2.data()): assert approx_equal(f1, f2, eps=1e-5)
def iucr_structure(self): #self._fragments = [Fragment(self._scatterers)] #? self._iucr_structure = xray.structure( special_position_settings = self._special_position_settings, scatterers = self._scatterers ) return self._iucr_structure
def loop(self): for i_position in xrange(self.wyckoff_table.size()): site_symmetry_i = self.wyckoff_table.random_site_symmetry( special_position_settings=self.special_position_settings, i_position=i_position) equiv_sites_i = sgtbx.sym_equiv_sites(site_symmetry_i) for j_position in xrange(self.wyckoff_table.size()): for n_trial in xrange(self.max_trials_per_position): site_j = self.wyckoff_table.random_site_symmetry( special_position_settings=self. special_position_settings, i_position=j_position).exact_site() dist_info = sgtbx.min_sym_equiv_distance_info( equiv_sites_i, site_j) if (dist_info.dist() > self.min_cross_distance): structure = xray.structure( special_position_settings=self. special_position_settings, scatterers=flex.xray_scatterer([ xray.scatterer( scattering_type=self.scattering_type, site=site) for site in [site_symmetry_i.exact_site(), site_j] ])) yield structure, dist_info.dist() break
def fcalc_from_pdb(resolution, algorithm=None, wavelength=0.9, anom=True, ucell=None, symbol=None, as_amplitudes=True): pdb_lines = 'HEADER TEST\nCRYST1 50.000 60.000 70.000 90.00 90.00 90.00 P 1\nATOM 1 O HOH A 1 56.829 2.920 55.702 1.00 20.00 O\nATOM 2 O HOH A 2 49.515 35.149 37.665 1.00 20.00 O\nATOM 3 O HOH A 3 52.667 17.794 69.925 1.00 20.00 O\nATOM 4 O HOH A 4 40.986 20.409 18.309 1.00 20.00 O\nATOM 5 O HOH A 5 46.896 37.790 41.629 1.00 20.00 O\nATOM 6 SED MSE A 6 1.000 2.000 3.000 1.00 20.00 SE\nEND\n' from iotbx import pdb pdb_inp = pdb.input(source_info=None, lines=pdb_lines) xray_structure = pdb_inp.xray_structure_simple() if ucell is not None: assert symbol is not None from cctbx.xray import structure from cctbx import crystal crystal_sym = crystal.symmetry(unit_cell=ucell, space_group_symbol=symbol) xray_structure = structure(scatterers=(xray_structure.scatterers()), crystal_symmetry=crystal_sym) scatterers = xray_structure.scatterers() if anom: from cctbx.eltbx import henke for sc in scatterers: expected_henke = henke.table( sc.element_symbol()).at_angstrom(wavelength) sc.fp = expected_henke.fp() sc.fdp = expected_henke.fdp() primitive_xray_structure = xray_structure.primitive_setting() P1_primitive_xray_structure = primitive_xray_structure.expand_to_p1() fcalc = P1_primitive_xray_structure.structure_factors( d_min=resolution, anomalous_flag=anom, algorithm=algorithm).f_calc() if as_amplitudes: fcalc = fcalc.amplitudes().set_observation_type_xray_amplitude() return fcalc
def _find_peaks(self, min_peak_peak_distance=1.5): cg = maptbx.crystal_gridding( space_group_info=self.model.crystal_symmetry().space_group_info(), symmetry_flags=maptbx.use_space_group_symmetry, unit_cell=self.unit_cell, pre_determined_n_real=self.map_data.accessor().all()) cgt = maptbx.crystal_gridding_tags(gridding=cg) peak_search_parameters = maptbx.peak_search_parameters( peak_search_level=3, max_peaks=0, peak_cutoff=self.cutoff, interpolate=True, min_distance_sym_equiv=0, general_positions_only=False, # ???????XXXXXXXXXXXXXX min_cross_distance=min_peak_peak_distance, min_cubicle_edge=5) psr = cgt.peak_search(parameters=peak_search_parameters, map=self.map_data).all(max_clusters=99999999) # Convert peaks into water xray.structure mean_b = flex.mean(self.model.get_hierarchy().atoms().extract_b()) sp = crystal.special_position_settings(self.model.crystal_symmetry()) scatterers = flex.xray_scatterer() for site_frac in psr.sites(): sc = xray.scatterer(label="O", site=site_frac, u=adptbx.b_as_u(mean_b), occupancy=1.0) scatterers.append(sc) self.xrs_water = xray.structure(sp, scatterers) # self.ma.add(" total peaks found: %d" % self.xrs_water.scatterers().size()) self.ma.add(" B factors set to : %8.3f" % mean_b) if (self.debug): self._write_pdb_file(file_name_prefix="hoh_all_peaks")
def exercise_is_simple_interaction(): for space_group_symbol in ["P1", "P41"]: for shifts in flex.nested_loop((-2,-2,-2),(2,2,2),False): shifts = matrix.col(shifts) structure = xray.structure( crystal_symmetry=crystal.symmetry( unit_cell=(10,10,20,90,90,90), space_group_symbol=space_group_symbol), scatterers=flex.xray_scatterer([ xray.scatterer(label="O", site=shifts+matrix.col((0,0,0))), xray.scatterer(label="N", site=shifts+matrix.col((0.5,0.5,0))), xray.scatterer(label="C", site=shifts+matrix.col((0.25,0.25,0)))])) asu_mappings = structure.asu_mappings(buffer_thickness=7) pair_generator = crystal.neighbors_simple_pair_generator( asu_mappings=asu_mappings, distance_cutoff=7) simple_interactions = {} for i_pair,pair in enumerate(pair_generator): if (asu_mappings.is_simple_interaction(pair)): assert asu_mappings_is_simple_interaction_emulation( asu_mappings, pair) key = (pair.i_seq,pair.j_seq) assert simple_interactions.get(key, None) is None simple_interactions[key] = 1 else: assert not asu_mappings_is_simple_interaction_emulation( asu_mappings, pair) assert len(simple_interactions) == 2 assert simple_interactions[(0,2)] == 1 assert simple_interactions[(1,2)] == 1
def exercise_rigid_pivoted_rotatable(): uc = uctbx.unit_cell((1, 1, 1)) xs = xray.structure( crystal_symmetry=crystal.symmetry( unit_cell=uc, space_group_symbol='hall: P 2x 2y'), scatterers=flex.xray_scatterer(( #triangle xray.scatterer('C0', site=(0,0,0)), xray.scatterer('C1', site=(0,2,0)), xray.scatterer('C2', site=(1,1,0)), ))) r = constraints.ext.reparametrisation(xs.unit_cell()) sc = xs.scatterers() pivot = r.add(constraints.independent_site_parameter, sc[0]) pivot_neighbour = r.add(constraints.independent_site_parameter, sc[1]) azimuth = r.add(constraints.independent_scalar_parameter, value=pi/2, variable=True) size = r.add(constraints.independent_scalar_parameter, value=1, variable=False) rg = r.add(constraints.rigid_pivoted_rotatable_group, pivot=pivot, pivot_neighbour=pivot_neighbour, azimuth=azimuth, size=size, scatterers=(sc[1], sc[2])) site_proxy = r.add(constraints.rigid_site_proxy, rg, 1) r.finalise() r.linearise() r.store() #check that proxy and the final results are the same... assert approx_equal( uc.distance(col(site_proxy.value), col(sc[2].site)), 0, eps=1e-15) #rotation happens around the center of gravity assert approx_equal( uc.distance(col((0,1,1)), col(sc[2].site)), 0, eps=1e-15)
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_SFweight_spline_core(structure, d_min, verbose=0): structure.scattering_type_registry(d_min=d_min) f_obs = abs(structure.structure_factors( d_min=d_min, anomalous_flag=False).f_calc()) if (0 or verbose): f_obs.show_summary() f_obs = miller.array( miller_set=f_obs, data=f_obs.data(), sigmas=flex.sqrt(f_obs.data())) partial_structure = xray.structure( crystal_symmetry=structure, scatterers=structure.scatterers()[:-2]) f_calc = f_obs.structure_factors_from_scatterers( xray_structure=partial_structure).f_calc() test_set_flags = (flex.random_double(size=f_obs.indices().size()) < 0.1) sfweight = clipper.SFweight_spline_interface( unit_cell=f_obs.unit_cell(), space_group=f_obs.space_group(), miller_indices=f_obs.indices(), anomalous_flag=f_obs.anomalous_flag(), f_obs_data=f_obs.data(), f_obs_sigmas=f_obs.sigmas(), f_calc=f_calc.data(), test_set_flags=test_set_flags, n_refln=f_obs.indices().size()//10, n_param=20) if (0 or verbose): print "number_of_spline_parameters:",sfweight.number_of_spline_parameters() print "mean fb: %.8g" % flex.mean(flex.abs(sfweight.fb())) print "mean fd: %.8g" % flex.mean(flex.abs(sfweight.fd())) print "mean phi: %.8g" % flex.mean(sfweight.centroid_phases()) print "mean fom: %.8g" % flex.mean(sfweight.figures_of_merit()) return sfweight
def __init__(self, space_group=SpaceGroup('P 1'), lattice=Lattice(1.0, 1.0, 1.0, 90.0, 90.0, 90.0), sites=[]): """ Crystal object. Functions as a wrapper to a cctbx xray.structure object, but adds additional variables and functionality. Core object of this package. Input: SpaceGroup space_group space group for the current crystal List of Site objects sites List of Site objects. Warning: 'crystal_structure' must be given, or 'space_group', 'lattice', and 'sites' must be given, or creation of the crystal will fail. """ try: crystal_symmetry = crystal.symmetry( unit_cell=str(lattice), space_group_symbol=space_group.cctbx_name) except Exception as e: raise SymmetryError( "SpaceGroup {s} is incompatible with Lattice -> {l} cctbx error {e}" .format(l=str(lattice), s=SpaceGroup.cctbx_name, e=e)) scatterers = flex.xray_scatterer() for i, site in enumerate(sites): # the element name can be pulled from scatterer via the s.element_symbol() method (if it's an element!!!) # example: 'atom-b-12',or 'vacancy-c-3' # we need to set the scattering_type so we can freely use the label to store metadata #scattering_type = something #scatterers.append(xray.scatterer(label=atom.name, site=tuple(atom.abc), occupancy=atom.occupancy), scattering_type=atom.name) #Method #2 # explicitly add metadata to _meta st_temp, dash, num = site.label.partition("-") if dash == "-": k = site.label else: k = "-".join([site.label, str(i)]) # problems here not sure what's up k = str(k) # print "label = ", k, ' type of label = ', type(k) # print "site = ", tuple(site.abc) # print "occupancy = ", site.occupancy scatterers.append( xray.scatterer(label=k, site=tuple(site.abc), occupancy=site.occupancy)) # Test for coherency of Lattice and SpaceGroup self.crystal_structure = xray.structure( crystal_symmetry=crystal_symmetry, scatterers=scatterers)
def as_xray_structure(self, min_distance_sym_equiv=0.5): result = xray.structure( special_position_settings=crystal.special_position_settings( crystal_symmetry=self.crystal_symmetry(), min_distance_sym_equiv=min_distance_sym_equiv)) for atm in self.atoms: result.add_scatterer(atm.as_xray_scatterer()) return result
def get_xrs(): crystal_symmetry = crystal.symmetry( unit_cell=(10,10,10,90,90,90), space_group_symbol="P 1") return xray.structure( crystal_symmetry=crystal_symmetry, scatterers=flex.xray_scatterer([ xray.scatterer(label="C", site=(0,0,0))]))
def exercise_2(): symmetry = crystal.symmetry( unit_cell=(5.67, 10.37, 10.37, 90, 135.49, 90), space_group_symbol="C2") structure = xray.structure(crystal_symmetry=symmetry) atmrad = flex.double() xyzf = flex.vec3_double() for k in xrange(100): scatterer = xray.scatterer( site = ((1.+k*abs(math.sin(k)))/1000.0, (1.+k*abs(math.cos(k)))/1000.0, (1.+ k)/1000.0), scattering_type = "C") structure.add_scatterer(scatterer) atmrad.append(van_der_waals_radii.vdw.table[scatterer.element_symbol()]) xyzf.append(scatterer.site) miller_set = miller.build_set( crystal_symmetry=structure, d_min=1.0, anomalous_flag=False) step = 0.5 crystal_gridding = maptbx.crystal_gridding( unit_cell=structure.unit_cell(), step=step) nxyz = crystal_gridding.n_real() shrink_truncation_radius = 1.0 solvent_radius = 1.0 m1 = around_atoms( structure.unit_cell(), structure.space_group().order_z(), structure.sites_frac(), atmrad, nxyz, solvent_radius, shrink_truncation_radius) assert m1.solvent_radius == 1 assert m1.shrink_truncation_radius == 1 assert flex.max(m1.data) == 1 assert flex.min(m1.data) == 0 assert m1.data.size() == m1.data.count(1) + m1.data.count(0) m2 = mmtbx.masks.bulk_solvent( xray_structure=structure, gridding_n_real=nxyz, ignore_zero_occupancy_atoms = False, solvent_radius=solvent_radius, shrink_truncation_radius=shrink_truncation_radius) assert m2.data.all_eq(m1.data) m3 = mmtbx.masks.bulk_solvent( xray_structure=structure, grid_step=step, ignore_zero_occupancy_atoms = False, solvent_radius=solvent_radius, shrink_truncation_radius=shrink_truncation_radius) assert m3.data.all_eq(m1.data) f_mask2 = m2.structure_factors(miller_set=miller_set) f_mask3 = m3.structure_factors(miller_set=miller_set) assert approx_equal(f_mask2.data(), f_mask3.data()) assert approx_equal(flex.sum(flex.abs(f_mask3.data())), 1095.17999134)
def exercise_real_space_refinement(verbose): if (verbose): out = sys.stdout else: out = StringIO() out_of_bounds_clamp = maptbx.out_of_bounds_clamp(0) out_of_bounds_raise = maptbx.out_of_bounds_raise() crystal_symmetry = crystal.symmetry(unit_cell=(10, 10, 10, 90, 90, 90), space_group_symbol="P 1") xray_structure = xray.structure(crystal_symmetry=crystal_symmetry, scatterers=flex.xray_scatterer([ xray.scatterer(label="C", site=(0, 0, 0)) ])) miller_set = miller.build_set(crystal_symmetry=crystal_symmetry, anomalous_flag=False, d_min=1) f_calc = miller_set.structure_factors_from_scatterers( xray_structure=xray_structure).f_calc() fft_map = f_calc.fft_map() fft_map.apply_sigma_scaling() real_map = fft_map.real_map_unpadded() #### unit_cell test delta_h = .005 basic_map = maptbx.basic_map( maptbx.basic_map_unit_cell_flag(), real_map, real_map.focus(), crystal_symmetry.unit_cell().orthogonalization_matrix(), out_of_bounds_clamp.as_handle(), crystal_symmetry.unit_cell()) testing_function_for_rsfit(basic_map, delta_h, xray_structure, out) ### non_symmetric test # minfrac = crystal_symmetry.unit_cell().fractionalize((-5, -5, -5)) maxfrac = crystal_symmetry.unit_cell().fractionalize((5, 5, 5)) gridding_first = [ifloor(n * b) for n, b in zip(fft_map.n_real(), minfrac)] gridding_last = [iceil(n * b) for n, b in zip(fft_map.n_real(), maxfrac)] data = maptbx.copy(real_map, gridding_first, gridding_last) # basic_map = maptbx.basic_map( maptbx.basic_map_non_symmetric_flag(), data, fft_map.n_real(), crystal_symmetry.unit_cell().orthogonalization_matrix(), out_of_bounds_clamp.as_handle(), crystal_symmetry.unit_cell()) testing_function_for_rsfit(basic_map, delta_h, xray_structure, out) ### asu test # minfrac = crystal_symmetry.unit_cell().fractionalize((0, 0, 0)) maxfrac = crystal_symmetry.unit_cell().fractionalize((10, 10, 10)) gridding_first = [ifloor(n * b) for n, b in zip(fft_map.n_real(), minfrac)] gridding_last = [iceil(n * b) for n, b in zip(fft_map.n_real(), maxfrac)] data = maptbx.copy(real_map, gridding_first, gridding_last) # basic_map = maptbx.basic_map( maptbx.basic_map_asu_flag(), data, crystal_symmetry.space_group(), crystal_symmetry.direct_space_asu().as_float_asu(), real_map.focus(), crystal_symmetry.unit_cell().orthogonalization_matrix(), out_of_bounds_clamp.as_handle(), crystal_symmetry.unit_cell(), 0.5, True) testing_function_for_rsfit(basic_map, delta_h, xray_structure, out)
def __init__(self, xray_structure, solvent_radius, shrink_truncation_radius, ignore_hydrogen_atoms=False, crystal_gridding=None, grid_step=None, d_min=None, resolution_factor=1 / 4, atom_radii_table=None, use_space_group_symmetry=False): self.xray_structure = xray_structure if crystal_gridding is None: self.crystal_gridding = maptbx.crystal_gridding( unit_cell=xray_structure.unit_cell(), space_group_info=xray_structure.space_group_info(), step=grid_step, d_min=d_min, resolution_factor=resolution_factor, symmetry_flags=sgtbx.search_symmetry_flags( use_space_group_symmetry=use_space_group_symmetry)) else: self.crystal_gridding = crystal_gridding if use_space_group_symmetry: atom_radii = cctbx.masks.vdw_radii( xray_structure, table=atom_radii_table).atom_radii asu_mappings = xray_structure.asu_mappings( buffer_thickness=flex.max(atom_radii) + solvent_radius) scatterers_asu_plus_buffer = flex.xray_scatterer() frac = xray_structure.unit_cell().fractionalize for sc, mappings in zip(xray_structure.scatterers(), asu_mappings.mappings()): for mapping in mappings: scatterers_asu_plus_buffer.append( sc.customized_copy(site=frac(mapping.mapped_site()))) xs = xray.structure(crystal_symmetry=xray_structure, scatterers=scatterers_asu_plus_buffer) else: xs = xray_structure.expand_to_p1() self.vdw_radii = cctbx.masks.vdw_radii(xs, table=atom_radii_table) self.mask = cctbx.masks.around_atoms( unit_cell=xs.unit_cell(), space_group_order_z=xs.space_group().order_z(), sites_frac=xs.sites_frac(), atom_radii=self.vdw_radii.atom_radii, gridding_n_real=self.crystal_gridding.n_real(), solvent_radius=solvent_radius, shrink_truncation_radius=shrink_truncation_radius) if use_space_group_symmetry: tags = self.crystal_gridding.tags() tags.tags().apply_symmetry_to_mask(self.mask.data) self.flood_fill = cctbx.masks.flood_fill(self.mask.data, xray_structure.unit_cell()) self.exclude_void_flags = [False] * self.flood_fill.n_voids() self.solvent_accessible_volume = self.n_solvent_grid_points() \ / self.mask.data.size() * xray_structure.unit_cell().volume()
def run(self): # I'm guessing self.data_manager, self.params and self.logger # are already defined here... # this must be mmtbx.model.manager? model = self.data_manager.get_model() atoms = model.get_atoms() all_bsel = flex.bool(atoms.size(), False) for selection_string in self.params.atom_selection_program.inselection: print("Selecting '%s'" % selection_string, file=self.logger) isel = model.iselection(string=selection_string) all_bsel.set_selected(isel, True) if self.params.atom_selection_program.write_pdb_file is None: print(" %d atom%s selected" % plural_s(isel.size()), file=self.logger) for atom in atoms.select(isel): print(" %s" % atom.format_atom_record(), file=self.logger) print("", file=self.logger) if self.params.atom_selection_program.write_pdb_file is not None: print("Writing file:", show_string( self.params.atom_selection_program.write_pdb_file), file=self.logger) ss_ann = model.get_ss_annotation() if not model.crystal_symmetry() or \ (not model.crystal_symmetry().unit_cell()): model = shift_and_box_model(model, shift_model=False) selected_model = model.select(all_bsel) if (ss_ann is not None): selected_model.set_ss_annotation(ss_ann.\ filter_annotation( hierarchy=selected_model.get_hierarchy(), asc=selected_model.get_atom_selection_cache(), remove_short_annotations=False, remove_3_10_helices=False, remove_empty_annotations=True, concatenate_consecutive_helices=False, split_helices_with_prolines=False, filter_sheets_with_long_hbonds=False)) if self.params.atom_selection_program.cryst1_replacement_buffer_layer is not None: box = uctbx.non_crystallographic_unit_cell_with_the_sites_in_its_center( sites_cart=selected_model.get_atoms().extract_xyz(), buffer_layer=self.params.atom_selection_program. cryst1_replacement_buffer_layer) sp = crystal.special_position_settings(box.crystal_symmetry()) sites_frac = box.sites_frac() xrs_box = selected_model.get_xray_structure( ).replace_sites_frac(box.sites_frac()) xray_structure_box = xray.structure(sp, xrs_box.scatterers()) selected_model.set_xray_structure(xray_structure_box) pdb_str = selected_model.model_as_pdb() f = open(self.params.atom_selection_program.write_pdb_file, 'w') f.write(pdb_str) f.close() print("", file=self.logger)
def as_xray_structure(self, scatterer=None): from cctbx import xray if (scatterer is None): scatterer = xray.scatterer(scattering_type="const") result = xray.structure(special_position_settings=self) for position in self.positions(): result.add_scatterer(scatterer.customized_copy( label=position.label, site=position.site)) return result
def as_xray_structure(self, scatterer=None): from cctbx import xray if (scatterer is None): scatterer = xray.scatterer(scattering_type="const") result = xray.structure(special_position_settings=self) for position in self.positions(): result.add_scatterer( scatterer.customized_copy(label=position.label, site=position.site)) return result
def exercise_unmerged () : quartz_structure = xray.structure( special_position_settings=crystal.special_position_settings( crystal_symmetry=crystal.symmetry( unit_cell=(5.01,5.01,5.47,90,90,120), space_group_symbol="P6222")), scatterers=flex.xray_scatterer([ xray.scatterer( label="Si", site=(1/2.,1/2.,1/3.), u=0.2), xray.scatterer( label="O", site=(0.197,-0.197,0.83333), u=0.1)])) quartz_structure.set_inelastic_form_factors( photon=1.54, table="sasaki") fc = abs(quartz_structure.structure_factors(d_min=1.0).f_calc()) symm = fc.crystal_symmetry() icalc = fc.expand_to_p1().f_as_f_sq().set_observation_type_xray_intensity() # generate 'unmerged' data i_obs = icalc.customized_copy(crystal_symmetry=symm) # now make up sigmas and some (hopefully realistic) error flex.set_random_seed(12345) n_refl = i_obs.size() sigmas = flex.random_double(n_refl) * flex.mean(fc.data()) sigmas = icalc.customized_copy(data=sigmas).apply_debye_waller_factors( u_iso=0.15) err = (flex.double(n_refl, 0.5) - flex.random_double(n_refl)) * 2 i_obs = i_obs.customized_copy( sigmas=sigmas.data(), data=i_obs.data() + err) # check for unmerged acentrics assert i_obs.is_unmerged_intensity_array() i_obs_centric = i_obs.select(i_obs.centric_flags().data()) i_obs_acentric = i_obs.select(~(i_obs.centric_flags().data())) i_mrg_acentric = i_obs_acentric.merge_equivalents().array() i_mixed = i_mrg_acentric.concatenate(i_obs_centric) assert not i_mixed.is_unmerged_intensity_array() # XXX These results of these functions are heavily dependent on the # behavior of the random number generator, which is not consistent across # platforms - therefore we can only check for very approximate values. # Exact numerical results are tested with real data (stored elsewhere). # CC1/2, etc. assert approx_equal(i_obs.cc_one_half(), 0.9998, eps=0.001) assert i_obs.resolution_filter(d_max=1.2).cc_one_half() > 0 assert i_obs.cc_anom() > 0.1 r_ano = i_obs.r_anom() assert approx_equal(r_ano, 0.080756, eps=0.0001) # merging stats i_mrg = i_obs.merge_equivalents() assert i_mrg.r_merge() < 0.1 assert i_mrg.r_meas() < 0.1 assert i_mrg.r_pim() < 0.05
def exercise_lbfgs(test_case, use_geo, out, d_min=2): sites_cart, geo_manager = cctbx.geometry_restraints.manager.\ construct_non_crystallographic_conserving_bonds_and_angles( sites_cart=flex.vec3_double(test_case.sites), edge_list_bonds=test_case.bonds, edge_list_angles=test_case.angles()) scatterers = flex.xray_scatterer(sites_cart.size(), xray.scatterer(scattering_type="C", b=20)) for sc, lbl in zip(scatterers, test_case.labels): sc.label = lbl structure = xray.structure(crystal_symmetry=geo_manager.crystal_symmetry, scatterers=scatterers) structure.set_sites_cart(sites_cart=sites_cart) f_calc = structure.structure_factors(d_min=d_min, anomalous_flag=False).f_calc() fft_map = f_calc.fft_map() fft_map.apply_sigma_scaling() if (use_geo): axis = matrix.col(flex.random_double_point_on_sphere()) rot = scitbx.math.r3_rotation_axis_and_angle_as_matrix(axis=axis, angle=25, deg=True) trans = matrix.col(flex.random_double_point_on_sphere()) * 1.0 structure.apply_rigid_body_shift(rot=rot, trans=trans) geo_manager.energies_sites(sites_cart=structure.sites_cart()).show( f=out) minimized = real_space_refinement_simple.lbfgs( sites_cart=structure.sites_cart(), density_map=fft_map.real_map(), geometry_restraints_manager=geo_manager, gradients_method="fd", real_space_target_weight=1, real_space_gradients_delta=d_min / 3) geo_manager.energies_sites(sites_cart=minimized.sites_cart).show(f=out) else: minimized = real_space_refinement_simple.lbfgs( sites_cart=structure.sites_cart(), density_map=fft_map.real_map(), unit_cell=structure.unit_cell(), gradients_method="fd", real_space_gradients_delta=d_min / 3) rmsd_start = sites_cart.rms_difference(structure.sites_cart()) rmsd_final = sites_cart.rms_difference(minimized.sites_cart) print("RMSD start, final:", rmsd_start, rmsd_final, file=out) if (use_geo): assert rmsd_start >= 1 - 1e-6 assert rmsd_final < 0.2 def show_f_g(label, f, g): print(label, "f, |g|:", f, flex.mean_sq(g)**0.5, file=out) show_f_g(label="start", f=minimized.f_start, g=minimized.g_start) show_f_g(label="final", f=minimized.f_final, g=minimized.g_final) assert minimized.f_final <= minimized.f_start return minimized
def exercise_u_iso_proportional_to_pivot_u_iso(): # Test working constraint xs = xray.structure( crystal_symmetry=crystal.symmetry( unit_cell=(), space_group_symbol='hall: P 2x 2y'), scatterers=flex.xray_scatterer(( xray.scatterer('C0', u=0.12), xray.scatterer('C1'), ))) r = constraints.ext.reparametrisation(xs.unit_cell()) sc = xs.scatterers() u_iso = r.add(constraints.independent_u_iso_parameter, sc[0]) u_iso_1 = r.add(constraints.u_iso_proportional_to_pivot_u_iso, pivot_u_iso=u_iso, multiplier=2, scatterer=sc[1]) r.finalise() r.linearise() assert approx_equal(u_iso_1.value, 0.24, eps=1e-15) # Test conflicting constraints xs = xray.structure( crystal_symmetry=crystal.symmetry( unit_cell=(), space_group_symbol='hall: P 2x 2y'), scatterers=flex.xray_scatterer(( xray.scatterer('C0', u=0.12), xray.scatterer('C1', u=0.21), xray.scatterer('C2') ))) with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") r = constraints.reparametrisation( structure=xs, constraints=[constraints.adp.shared_u((0, 2)), constraints.adp.shared_u((1, 2))], connectivity_table=smtbx.utils.connectivity_table(xs)) assert len(w) == 1 assert w[-1].category == constraints.ConflictingConstraintWarning assert w[-1].message.conflicts == set(((2, 'U'),))
def debug_write_reciprocal_lattice_points_as_pdb(self, file_name='reciprocal_lattice.pdb'): from cctbx import crystal, xray cs = crystal.symmetry(unit_cell=(1000,1000,1000,90,90,90), space_group="P1") xs = xray.structure(crystal_symmetry=cs) for site in self.trial_sites: xs.add_scatterer(xray.scatterer("C", site=site)) xs.sites_mod_short() with open(file_name, 'wb') as f: print >> f, xs.as_pdb_file()
def __init__( self, xray_structure, solvent_radius, shrink_truncation_radius, ignore_hydrogen_atoms=False, crystal_gridding=None, grid_step=None, d_min=None, resolution_factor=1 / 4, atom_radii_table=None, use_space_group_symmetry=False, ): self.xray_structure = xray_structure if crystal_gridding is None: self.crystal_gridding = maptbx.crystal_gridding( unit_cell=xray_structure.unit_cell(), space_group_info=xray_structure.space_group_info(), step=grid_step, d_min=d_min, resolution_factor=resolution_factor, symmetry_flags=sgtbx.search_symmetry_flags(use_space_group_symmetry=use_space_group_symmetry), ) else: self.crystal_gridding = crystal_gridding if use_space_group_symmetry: atom_radii = cctbx.masks.vdw_radii(xray_structure, table=atom_radii_table).atom_radii asu_mappings = xray_structure.asu_mappings(buffer_thickness=flex.max(atom_radii) + solvent_radius) scatterers_asu_plus_buffer = flex.xray_scatterer() frac = xray_structure.unit_cell().fractionalize for sc, mappings in zip(xray_structure.scatterers(), asu_mappings.mappings()): for mapping in mappings: scatterers_asu_plus_buffer.append(sc.customized_copy(site=frac(mapping.mapped_site()))) xs = xray.structure(crystal_symmetry=xray_structure, scatterers=scatterers_asu_plus_buffer) else: xs = xray_structure.expand_to_p1() self.vdw_radii = cctbx.masks.vdw_radii(xs, table=atom_radii_table) self.mask = cctbx.masks.around_atoms( unit_cell=xs.unit_cell(), space_group_order_z=xs.space_group().order_z(), sites_frac=xs.sites_frac(), atom_radii=self.vdw_radii.atom_radii, gridding_n_real=self.crystal_gridding.n_real(), solvent_radius=solvent_radius, shrink_truncation_radius=shrink_truncation_radius, ) if use_space_group_symmetry: tags = self.crystal_gridding.tags() tags.tags().apply_symmetry_to_mask(self.mask.data) self.flood_fill = cctbx.masks.flood_fill(self.mask.data, xray_structure.unit_cell()) self.exclude_void_flags = [False] * self.flood_fill.n_voids() self.solvent_accessible_volume = ( self.n_solvent_grid_points() / self.mask.data.size() * xray_structure.unit_cell().volume() )
def demo(): """ Result of ICSD query: N * -Cr2O3-[R3-CH] Baster, M.;Bouree, F.;Kowalska, A.;Latacz, Z(2000) C 4.961950 4.961950 13.597400 90.000000 90.000000 120.000000 S GRUP R -3 C A Cr1 0.000000 0.000000 0.347570 0.000000 A O1 0.305830 0.000000 0.250000 """ crystal_symmetry = crystal.symmetry( unit_cell="4.961950 4.961950 13.597400 90.000000 90.000000 120.000000", space_group_symbol="R -3 C") scatterers = flex.xray_scatterer() scatterers.append(xray.scatterer( label="Cr1", site=(0.000000,0.000000,0.347570))) scatterers.append(xray.scatterer( label="O1", site=(0.305830,0.000000,0.250000))) icsd_structure = xray.structure( crystal_symmetry=crystal_symmetry, scatterers=scatterers) icsd_structure.show_summary().show_scatterers() print icsd_pairs = icsd_structure.show_distances( distance_cutoff=2.5, keep_pair_asu_table=True) print primitive_structure = icsd_structure.primitive_setting() primitive_structure.show_summary().show_scatterers() print p1_structure = primitive_structure.expand_to_p1() p1_structure.show_summary().show_scatterers() print p1_pairs = p1_structure.show_distances( distance_cutoff=2.5, keep_pair_asu_table=True) print for label,structure,pairs in [("ICSD", icsd_structure,icsd_pairs), ("P1", p1_structure,p1_pairs)]: print "Coordination sequences for", label, "structure" term_table = crystal.coordination_sequences.simple( pair_asu_table=pairs.pair_asu_table, max_shell=10) crystal.coordination_sequences.show_terms( structure=structure, term_table=term_table) print icsd_f_calc = icsd_structure.structure_factors( d_min=1, algorithm="direct").f_calc() icsd_f_calc_in_p1 = icsd_f_calc.primitive_setting().expand_to_p1() p1_f_calc = icsd_f_calc_in_p1.structure_factors_from_scatterers( xray_structure=p1_structure, algorithm="direct").f_calc() for h,i,p in zip(icsd_f_calc_in_p1.indices(), icsd_f_calc_in_p1.data(), p1_f_calc.data()): print h, abs(i), abs(p)*3 print "OK"
def run(): print __doc__ crystal_symmetry = crystal.symmetry( unit_cell=(5, 5, 6, 90, 90, 120), space_group_symbol="P 31") distance_cutoff = 2.5 scatterers = flex.xray_scatterer() for i,site in enumerate([(0.7624, 0.5887, 0.3937), (0.2813, 0.9896, 0.9449), (0.4853, 0.8980, 0.4707)]): scatterers.append(xray.scatterer( label="Se%d"%(i+1), site=site)) given_structure = xray.structure( crystal_symmetry=crystal_symmetry, scatterers=scatterers) print "==================" print "Original structure" print "==================" given_structure.show_summary().show_scatterers() print "Interatomic distances:" given_structure.show_distances(distance_cutoff=distance_cutoff) print print print "=====================================================" print "Other hand with sites flipped and space group changed" print "=====================================================" other_hand = given_structure.change_hand() other_hand.show_summary().show_scatterers() print "Interatomic distances:" other_hand.show_distances(distance_cutoff=distance_cutoff) print print "==================================" print "Other hand with sites flipped only" print "==================================" other_sites_orig_symmetry = xray.structure( crystal_symmetry=crystal_symmetry, scatterers=other_hand.scatterers()) other_sites_orig_symmetry.show_summary().show_scatterers() print "Interatomic distances:" other_sites_orig_symmetry.show_distances(distance_cutoff=distance_cutoff) print
def pdb_atoms_as_xray_structure(pdb_atoms, crystal_symmetry): xray_structure = xray.structure(crystal_symmetry = crystal_symmetry) unit_cell = xray_structure.unit_cell() for atom in pdb_atoms: scatterer = xray.scatterer( label = atom.name, site = unit_cell.fractionalize(atom.xyz), b = atom.b, occupancy = atom.occ, scattering_type = atom.element) xray_structure.add_scatterer(scatterer) return xray_structure
def exercise_covariance(): xs = xray.structure( crystal_symmetry=crystal.symmetry( (5.01,5.01,5.47,90,90,120), "P6222"), scatterers=flex.xray_scatterer([ xray.scatterer("Si", (1/2.,1/2.,1/3.)), xray.scatterer("O", (0.197,-0.197,0.83333))])) uc = xs.unit_cell() flags = xs.scatterer_flags() for f in flags: f.set_grad_site(True) xs.set_scatterer_flags(flags) cov = flex.double((1e-8,1e-9,2e-9,3e-9,4e-9,5e-9, 2e-8,1e-9,2e-9,3e-9,4e-9, 3e-8,1e-9,2e-9,3e-9, 2e-8,1e-9,2e-9, 3e-8,1e-9, 4e-8)) param_map = xs.parameter_map() assert approx_equal(cov, covariance.extract_covariance_matrix_for_sites(flex.size_t([0,1]), cov, param_map)) cov_cart = covariance.orthogonalize_covariance_matrix(cov, uc, param_map) O = matrix.sqr(uc.orthogonalization_matrix()) for i in range(param_map.n_scatterers): cov_i = covariance.extract_covariance_matrix_for_sites(flex.size_t([i]), cov, param_map) cov_i_cart = covariance.extract_covariance_matrix_for_sites(flex.size_t([i]), cov_cart, param_map) assert approx_equal( O * matrix.sym(sym_mat3=cov_i) * O.transpose(), matrix.sym(sym_mat3=cov_i_cart).as_mat3()) for f in flags: f.set_grads(False) flags[0].set_grad_u_aniso(True) flags[0].set_use_u_aniso(True) flags[1].set_grad_u_iso(True) flags[1].set_use_u_iso(True) xs.set_scatterer_flags(flags) param_map = xs.parameter_map() cov = flex.double(7*7, 0) cov.reshape(flex.grid(7,7)) cov.matrix_diagonal_set_in_place(flex.double([i for i in range(7)])) cov = cov.matrix_symmetric_as_packed_u() assert approx_equal([i for i in range(6)], covariance.extract_covariance_matrix_for_u_aniso( 0, cov, param_map).matrix_packed_u_diagonal()) assert covariance.variance_for_u_iso(1, cov, param_map) == 6 try: covariance.variance_for_u_iso(0, cov, param_map) except RuntimeError: pass else: raise Exception_expected try: covariance.extract_covariance_matrix_for_u_aniso(1, cov, param_map) except RuntimeError: pass else: raise Exception_expected approx_equal(covariance.extract_covariance_matrix_for_sites( flex.size_t([1]), cov, param_map), (0,0,0,0,0,0))
def build_xray_structure_with_carbon_along_x(a, b_iso, x=[0]): result = xray.structure( crystal_symmetry=crystal.symmetry( unit_cell=(a,a,a,90,90,90), space_group_symbol="P1")) for i,v in enumerate(x): result.add_scatterer(xray.scatterer( label="C%d" % (i+1), scattering_type="C", site=(v,0,0), b=b_iso)) reg = result.scattering_type_registry(table="n_gaussian", d_min=1/12) g = reg.as_type_gaussian_dict()["C"] assert g.n_terms() == 5 assert not g.use_c() return result
def dummy_structure(space_group_info, volume, n_scatterers): structure = xray.structure( special_position_settings=crystal.special_position_settings( crystal_symmetry=crystal.symmetry( unit_cell=space_group_info.any_compatible_unit_cell(volume=volume), space_group_info=space_group_info))) b_iso = 20 u_iso = adptbx.b_as_u(b_iso) u_star = adptbx.u_iso_as_u_star(structure.unit_cell(), u_iso) scatterer = xray.scatterer(label="C", site=(0.123,0.234,0.345), u=u_star) for i in xrange(n_scatterers): structure.add_scatterer(scatterer) return structure
def as_xray_structure(self, crystal_symmetry=None, force_symmetry=False, min_distance_sym_equiv=0.5): crystal_symmetry = self.crystal_symmetry().join_symmetry( other_symmetry=crystal_symmetry, force=force_symmetry) assert crystal_symmetry.unit_cell() is not None assert crystal_symmetry.space_group_info() is not None structure = xray.structure(crystal.special_position_settings( crystal_symmetry=crystal_symmetry, min_distance_sym_equiv=min_distance_sym_equiv)) for site in self.sites: structure.add_scatterer(site.as_xray_scatterer(structure.unit_cell())) return structure
def debug_write_reciprocal_lattice_points_as_pdb( points, file_name='reciprocal_lattice.pdb'): from cctbx import crystal, xray cs = crystal.symmetry(unit_cell=(1000,1000,1000,90,90,90), space_group="P1") xs = xray.structure(crystal_symmetry=cs) sel = flex.random_selection(points.size(), min(20000, points.size())) rsp = points.select(sel) for site in rsp: xs.add_scatterer(xray.scatterer("C", site=site)) xs.sites_mod_short() with open(file_name, 'wb') as f: print >> f, xs.as_pdb_file()