def find_symmetry_from_crystal_structure(crystal_structure): work_f_calc = crystal_structure.structure_factors(d_min=1).f_calc().expand_to_p1() sg_dict = {} state = False try: sf_symm = symmetry_search.structure_factor_symmetry(f_in_p1=work_f_calc) sgi = sf_symm.space_group_info sg_dict = _space_group_info_to_dict(sgi) state = True except Exception as e: sg_dict = dict(hall='P 1', hermann_mauguin='P 1', number=1, crystal_system='Triclinic', cctbx_name='P 1') #return both the space group and a flag to specify that the assignment was successful return (sg_dict, state)
def run(args): import iotbx.phil pcl = iotbx.phil.process_command_line(args=args, master_string="""\ symmetry_search { structure_factor_d_min = 2 .type = float } """) if (len(pcl.remaining_args) != 1): from libtbx.utils import Usage import libtbx.load_env print pcl.master.show() print raise Usage( "%s pdb_file|poscar_file [parameters]" % libtbx.env.dispatcher_name) inp_file = pcl.remaining_args[0] from libtbx.str_utils import show_string import iotbx.pdb if (iotbx.pdb.is_pdb_file(inp_file)): print "PDB file:", show_string(inp_file) pdb_inp = iotbx.pdb.input(file_name=inp_file) xs = pdb_inp.xray_structure_simple() else: print "POSCAR file:", show_string(inp_file) import iotbx.poscar poscar = iotbx.poscar.reader(lines=open(inp_file).read().splitlines()) poscar.make_up_types_if_necessary() xs = poscar.xray_structure() print pcl.work.show() print params = pcl.work.extract().symmetry_search print "Unit cell:", xs.unit_cell() print fc_p1 = xs.structure_factors( d_min=params.structure_factor_d_min).f_calc().expand_to_p1() from cctbx import symmetry_search sf_symm = symmetry_search.structure_factor_symmetry(f_in_p1=fc_p1) print sf_symm print print sf_symm.space_group_info print
def find_symmetry_from_crystal_structure(crystal_structure): work_f_calc = crystal_structure.structure_factors( d_min=1).f_calc().expand_to_p1() sg_dict = {} state = False try: sf_symm = symmetry_search.structure_factor_symmetry( f_in_p1=work_f_calc) sgi = sf_symm.space_group_info sg_dict = _space_group_info_to_dict(sgi) state = True except Exception as e: sg_dict = dict(hall='P 1', hermann_mauguin='P 1', number=1, crystal_system='Triclinic', cctbx_name='P 1') #return both the space group and a flag to specify that the assignment was successful return (sg_dict, state)
def exercise(space_group_info, fixed_random_seed=True, shifted_origin=None, elements=None, d_min=0.8, grid_resolution_factor=1/3., verbose=False, **kwds): if elements is None: n_C = 5 n_O = 1 n_N = 1 elements = ["C"]*n_C + ["O"]*n_O + ["N"]*n_N if verbose: print elements target_space_group_type = space_group_info.type() hall = sgtbx.space_group_symbols( target_space_group_type.lookup_symbol()).hall() print hall if fixed_random_seed: random.seed(1) flex.set_random_seed(1) # Generate a random structure in real space, # compute its structure factors, # that we will try to recover the symmetry of target_structure = random_structure.xray_structure( space_group_info=space_group_info, elements=elements, use_u_iso=True, random_u_iso=True, random_u_iso_scale=0.04, use_u_aniso=False, ) if shifted_origin is None: shifted_origin = flex.random_double(3) shifted_origin = mat.col(shifted_origin) if verbose: print "new origin = (%.3f, %.3f, %.3f)" % shifted_origin.elems print target_structure_in_p1 = target_structure\ .expand_to_p1().apply_shift(shifted_origin) target_f_in_p1 = miller.build_set( crystal_symmetry=target_structure_in_p1, anomalous_flag=False, d_min=d_min ).structure_factors_from_scatterers( xray_structure=target_structure_in_p1, algorithm="direct").f_calc() # Recover space group? sf_symm = symmetry_search.structure_factor_symmetry(target_f_in_p1) if verbose: print sf_symm solution_hall, target_hall = [ sgi.as_reference_setting().type().hall_symbol() for sgi in (sf_symm.space_group_info, target_structure.space_group_info()) ] assert solution_hall == target_hall, (solution_hall, target_hall) # Shift maximises goodness of symmetry? gos, solution_f = sf_symm.symmetrised_structure_factors() if space_group_info.type().hall_symbol() != ' P 1': assert gos.correlation > 0.99 assert gos.gradient == (0, 0, 0) gos_away_from_max = sf_symm.symmetrised_structure_factors( delta=mat.col((0.1, 0.1, 0.1)))[0] assert gos_away_from_max.correlation < 0.9, gos_away_from_max.correlation # Recovered origin """The sequence of transform read: ----->target structure ^ V ^ V (1, shifted_origin=sigma) ^ V ^ shifted target ^ V ^ V sf_symm.cb_op_to_primitive = (P, 0) ^ V ^ shifted target in primitive cell = shifted solution in primitive cell ^ V ^ V (1, -sf_symm.origin=-s) ^ V ^ solution in primitive cell ^ V ^ V solution_to_target_cb_op = (Q, q) ^ V ^------------ The total transfrom from the target structure back to it reads (QP, q') with q' = (Q,q)(-s + P sigma) = (Q,q)delta_o with delta_o = sf_symm.cb_op_to_primitive(shifted_origin) - sf_symm.origin (Q, q') must leave the target structure space group invariant. Most of the time Q is the unit matrix and the test boils down to check whether delta is an allowed origin shift after changing to the input cell but it does not hurt to do the more general test all the time. """ solution_to_target_cb_op = ( target_structure.space_group_info() .change_of_basis_op_to_reference_setting().inverse() * sf_symm.space_group_info .change_of_basis_op_to_reference_setting()) if verbose: print print "solution -> target: %s" % solution_to_target_cb_op.as_xyz() delta_o = (mat.col(sf_symm.cb_op_to_primitive(shifted_origin)) - sf_symm.origin) delta = mat.col(solution_to_target_cb_op(delta_o)) stabilising_cb_op = sgtbx.change_of_basis_op(sgtbx.rt_mx( (solution_to_target_cb_op*sf_symm.cb_op_to_primitive).c().r(), sgtbx.tr_vec((delta*72).as_int()*2, tr_den=144))) # guarding against rounding errors on some platforms (e.g. FC8) # meaning that (delta*144).as_int() would not work. target_sg = target_structure.space_group() assert target_sg == target_sg.change_basis(stabilising_cb_op)
def exercise(space_group_info, fixed_random_seed=True, shifted_origin=None, elements=None, d_min=0.8, grid_resolution_factor=1/3., verbose=False, **kwds): if elements is None: n_C = 5 n_O = 1 n_N = 1 elements = ["C"]*n_C + ["O"]*n_O + ["N"]*n_N if verbose: print(elements) target_space_group_type = space_group_info.type() hall = sgtbx.space_group_symbols( target_space_group_type.lookup_symbol()).hall() print(hall) if fixed_random_seed: random.seed(1) flex.set_random_seed(1) # Generate a random structure in real space, # compute its structure factors, # that we will try to recover the symmetry of target_structure = random_structure.xray_structure( space_group_info=space_group_info, elements=elements, use_u_iso=True, random_u_iso=True, random_u_iso_scale=0.04, use_u_aniso=False, ) if shifted_origin is None: shifted_origin = flex.random_double(3) shifted_origin = mat.col(shifted_origin) if verbose: print("new origin = (%.3f, %.3f, %.3f)" % shifted_origin.elems) print() target_structure_in_p1 = target_structure\ .expand_to_p1().apply_shift(shifted_origin) target_f_in_p1 = miller.build_set( crystal_symmetry=target_structure_in_p1, anomalous_flag=False, d_min=d_min ).structure_factors_from_scatterers( xray_structure=target_structure_in_p1, algorithm="direct").f_calc() # Recover space group? sf_symm = symmetry_search.structure_factor_symmetry(target_f_in_p1) if verbose: print(sf_symm) solution_hall, target_hall = [ sgi.as_reference_setting().type().hall_symbol() for sgi in (sf_symm.space_group_info, target_structure.space_group_info()) ] assert solution_hall == target_hall, (solution_hall, target_hall) # Shift maximises goodness of symmetry? gos, solution_f = sf_symm.symmetrised_structure_factors() if space_group_info.type().hall_symbol() != ' P 1': assert gos.correlation > 0.99 assert gos.gradient == (0, 0, 0) gos_away_from_max = sf_symm.symmetrised_structure_factors( delta=mat.col((0.1, 0.1, 0.1)))[0] assert gos_away_from_max.correlation < 0.9, gos_away_from_max.correlation # Recovered origin """The sequence of transform read: ----->target structure ^ V ^ V (1, shifted_origin=sigma) ^ V ^ shifted target ^ V ^ V sf_symm.cb_op_to_primitive = (P, 0) ^ V ^ shifted target in primitive cell = shifted solution in primitive cell ^ V ^ V (1, -sf_symm.origin=-s) ^ V ^ solution in primitive cell ^ V ^ V solution_to_target_cb_op = (Q, q) ^ V ^------------ The total transfrom from the target structure back to it reads (QP, q') with q' = (Q,q)(-s + P sigma) = (Q,q)delta_o with delta_o = sf_symm.cb_op_to_primitive(shifted_origin) - sf_symm.origin (Q, q') must leave the target structure space group invariant. Most of the time Q is the unit matrix and the test boils down to check whether delta is an allowed origin shift after changing to the input cell but it does not hurt to do the more general test all the time. """ solution_to_target_cb_op = ( target_structure.space_group_info() .change_of_basis_op_to_reference_setting().inverse() * sf_symm.space_group_info .change_of_basis_op_to_reference_setting()) if verbose: print() print("solution -> target: %s" % solution_to_target_cb_op.as_xyz()) delta_o = (mat.col(sf_symm.cb_op_to_primitive(shifted_origin)) - sf_symm.origin) delta = mat.col(solution_to_target_cb_op(delta_o)) stabilising_cb_op = sgtbx.change_of_basis_op(sgtbx.rt_mx( (solution_to_target_cb_op*sf_symm.cb_op_to_primitive).c().r(), sgtbx.tr_vec((delta*72).as_int()*2, tr_den=144))) # guarding against rounding errors on some platforms (e.g. FC8) # meaning that (delta*144).as_int() would not work. target_sg = target_structure.space_group() assert target_sg == target_sg.change_basis(stabilising_cb_op)