def generate_twin_operators(self): from mmtbx.scaling.twin_analyses import twin_laws TL = twin_laws(miller_array=self.data) if len(TL.operators) == 0: from libtbx.utils import Sorry raise Sorry("No twin laws are possible for this crystal lattice.") for twin_law in TL.operators: if self.verbose: print twin_law.operator.r().as_hkl() return TL.operators
def generate_twin_operators(self, obs_in, flag_all=False): #generate only true merohedral twin operators from mmtbx.scaling.twin_analyses import twin_laws TL = twin_laws(miller_array=obs_in) operators = [] if flag_all: operators = TL.operators else: if TL.m > 0: operators = TL.operators return operators
def generate_twin_operators(miller_array, verbose=True): from mmtbx.scaling.twin_analyses import twin_laws TL = twin_laws(miller_array=miller_array) return TL.operators
def determine_reindex_operator_against_reference(test_miller_set, reference_miller_set): """Reindex a miller set to match a reference miller set. This function takes two miller arrays, a reference and a test array. The space group is checked to see if any reindexing may be required to give consistent indexing between both datasets. If possible twin operators exist, the different indexing options are tested against the reference set, using the correlation between datasets as the test. Args: test_miller_set (cctbx.miller.array): The input miller set to be reindexed. reference_miller_set (cctbx.miller.array): The reference miller set. Returns: cctbx.sgtbx.change_of_basis_op: The change of basis operator which should be applied to the test dataset to give consistent indexing with the reference. """ if (reference_miller_set.space_group().type().number() != test_miller_set.space_group().type().number()): raise Sorry("""Space groups are not equal. Can only reindex against a reference dataset if both dataset are in the same spacegroup.""") twin_ops = twin_laws( miller_array=test_miller_set.eliminate_sys_absent()).operators twin_ops = [ sgtbx.change_of_basis_op(op.operator.as_xyz()) for op in twin_ops ] if twin_ops: correlations = [] print("Possible twin operators identified for space group %s:" % test_miller_set.space_group().info()) for op in twin_ops: print(op) # Loop through twin operators, calculating cc between two datasets cc = test_miller_set.correlation(reference_miller_set, assert_is_similar_symmetry=False) correlations.append(cc.coefficient()) for op in twin_ops: reindexed = test_miller_set.change_basis(op) cc = reindexed.correlation(reference_miller_set, assert_is_similar_symmetry=False) correlations.append(cc.coefficient()) # print out table of results and choose best header = ["Reindex op", "CC to reference"] rows = [["a, b, c (no reindex)", f"{correlations[0]:.5f}"]] for i, op in enumerate(twin_ops): rows.append([str(op), f"{correlations[i + 1]:.5f}"]) print(dials.util.tabulate(rows, header)) best_solution_idx = correlations.index(max(correlations)) print("\nOutcome of analysis against reference dataset:") if best_solution_idx == 0: print("No reindexing required \n") change_of_basis_op = sgtbx.change_of_basis_op("a,b,c") else: print( "Reindexing required with the twin operator:", twin_ops[best_solution_idx - 1].as_hkl(), "\n", ) change_of_basis_op = twin_ops[best_solution_idx - 1] else: print("No twin operators found, no reindexing required \n") change_of_basis_op = sgtbx.change_of_basis_op("a,b,c") return change_of_basis_op
def run(args, command_name="phenix.twin_map_utils"): log = sys.stdout params = None if (len(args) == 0 or "--help" in args or "--h" in args or "-h" in args): print_help(command_name=command_name) else: log = multi_out() if (not "--quiet" in args): log.register(label="stdout", file_object=sys.stdout) string_buffer = StringIO() string_buffer_plots = StringIO() log.register(label="log_buffer", file_object=string_buffer) phil_objects = [] argument_interpreter = master_params.command_line_argument_interpreter( home_scope="map_coefs") for arg in args: command_line_params = None arg_is_processed = False # is it a file? if (os.path.isfile(arg)): ## is this a file name? # check if it is a phil file try: command_line_params = iotbx.phil.parse(file_name=arg) if command_line_params is not None: phil_objects.append(command_line_params) arg_is_processed = True except KeyboardInterrupt: raise except Exception: pass else: try: command_line_params = argument_interpreter.process(arg=arg) if command_line_params is not None: phil_objects.append(command_line_params) arg_is_processed = True except KeyboardInterrupt: raise except Exception: pass if not arg_is_processed: print("##----------------------------------------------##", file=log) print("## Unknown file or keyword:", arg, file=log) print("##----------------------------------------------##", file=log) print(file=log) raise Sorry("Unknown file or keyword: %s" % arg) effective_params = master_params.fetch(sources=phil_objects) params = effective_params.extract() """ new_params = master_params.format(python_object=params) new_params.show(out=log) """ # now get the unit cell from the pdb file hkl_xs = crystal_symmetry_from_any.extract_from( file_name=params.twin_utils.input.xray_data.file_name) pdb_xs = crystal_symmetry_from_any.extract_from( file_name=params.twin_utils.input.model.file_name) phil_xs = None if ([ params.twin_utils.input.unit_cell, params.twin_utils.input.space_group ]).count(None) < 2: phil_xs = crystal.symmetry( unit_cell=params.twin_utils.input.unit_cell, space_group_info=params.twin_utils.input.space_group) combined_xs = crystal.select_crystal_symmetry(None, phil_xs, [pdb_xs], [hkl_xs]) # inject the unit cell and symmetry in the phil scope please params.twin_utils.input.unit_cell = combined_xs.unit_cell() params.twin_utils.input.space_group = \ sgtbx.space_group_info( group = combined_xs.space_group() ) new_params = master_params.format(python_object=params) new_params.show(out=log) if params.twin_utils.input.unit_cell is None: raise Sorry("unit cell not specified") if params.twin_utils.input.space_group is None: raise Sorry("space group not specified") if params.twin_utils.input.xray_data.file_name is None: raise Sorry("Xray data not specified") if params.twin_utils.input.model.file_name is None: raise Sorry("pdb file with model not specified") #----------------------------------------------------------- # # step 1: read in the reflection file # phil_xs = crystal.symmetry( unit_cell=params.twin_utils.input.unit_cell, space_group_info=params.twin_utils.input.space_group) xray_data_server = reflection_file_utils.reflection_file_server( crystal_symmetry=phil_xs, force_symmetry=True, reflection_files=[]) miller_array = None free_flags = None tmp_params = utils.data_and_flags_master_params().extract() # insert proper values please tmp_params.file_name = params.twin_utils.input.xray_data.file_name tmp_params.labels = params.twin_utils.input.xray_data.obs_labels tmp_params.r_free_flags.file_name = params.twin_utils.input.xray_data.file_name tmp_params.r_free_flags.label = params.twin_utils.input.xray_data.free_flag tmp_object = utils.determine_data_and_flags( reflection_file_server=xray_data_server, parameters=tmp_params, log=log) miller_array = tmp_object.extract_data() if miller_array.is_xray_intensity_array(): miller_array = miller_array.f_sq_as_f() assert miller_array.is_xray_amplitude_array() free_flags = tmp_object.extract_flags(data=miller_array) print(file=log) print("Attempting to extract Free R flags", file=log) free_flags = free_flags.customized_copy(data=flex.bool( free_flags.data() == 1)) if free_flags is None: free_flags = miller_array.generate_r_free_flags( use_lattice_symmetry=True) assert miller_array.observation_type() is not None print(file=log) print("Summary info of observed data", file=log) print("=============================", file=log) miller_array.show_summary(f=log) print(file=log) if miller_array.indices().size() == 0: raise Sorry("No data available") #---------------------------------------------------------------- # Step 2: get an xray structure from the PDB file # model = pdb.input(file_name=params.twin_utils.input.model.file_name ).xray_structure_simple(crystal_symmetry=phil_xs) print("Atomic model summary", file=log) print("====================", file=log) model.show_summary(f=log) print(file=log) #---------------------------------------------------------------- # step 3: get the twin laws for this xs twin_laws = twin_analyses.twin_laws( miller_array, lattice_symmetry_max_delta=\ params.twin_utils.parameters.twinning.max_delta, out=log) print(file=log) print("Preliminary data analyses", file=log) print("==========================", file=log) twin_laws.show(out=log) #--------- # step 3: # make twin model managers for all twin laws print(file=log) print( "Overall and bulk solvent scale paranmeters and twin fraction estimation", file=log) print( "=======================================================================", file=log) twin_models = [] operator_count = 0 if params.twin_utils.parameters.twinning.twin_law is not None: tmp_law = sgtbx.rt_mx( params.twin_utils.parameters.twinning.twin_law) tmp_law = twin_analyses.twin_law(tmp_law, None, None, None, None, None) twin_laws.operators = [tmp_law] for twin_law in twin_laws.operators: operator_count += 1 operator_hkl = sgtbx.change_of_basis_op(twin_law.operator).as_hkl() twin_model = twin_f_model.twin_model_manager( f_obs=miller_array, r_free_flags=free_flags, xray_structure=model, twin_law=twin_law.operator, detwin_mode=params.twin_utils.parameters.twinning.detwin_mode, out=log) print("--- bulk solvent scaling ---", file=log) twin_model.update_all_scales() twin_model.r_values() twin_model.target() twin_model.show_k_sol_b_sol_b_cart_target() twin_model.show_essential() wfofc = twin_model.map_coefficients(map_type="mFo-DFc") wtfofc = twin_model.map_coefficients(map_type="2mFo-DFc") grad = twin_model.map_coefficients(map_type="gradient") mtz_dataset = wtfofc.as_mtz_dataset(column_root_label="FWT") mtz_dataset = mtz_dataset.add_miller_array( miller_array=wfofc, column_root_label="DFWT") mtz_dataset = mtz_dataset.add_miller_array( miller_array=grad, column_root_label="GRAD") name = params.twin_utils.output.map_coeffs_root + "_" + str( operator_count) + ".mtz" print(file=log) print("Writing %s for twin law %s" % (name, operator_hkl), file=log) print(file=log) mtz_dataset.mtz_object().write(file_name=name) if params.twin_utils.output.obs_and_calc is not None: # i want also a Fobs and Fmodel combined dataset please mtz_dataset = miller_array.as_mtz_dataset( column_root_label="FOBS") mtz_dataset = mtz_dataset.add_miller_array( miller_array=twin_model.f_model(), column_root_label="FMODEL") name = params.twin_utils.output.obs_and_calc mtz_dataset.mtz_object().write(file_name=name) if len(twin_laws.operators) == 0: print(file=log) print("No twin laws were found", file=log) print("Performing maximum likelihood based bulk solvent scaling", file=log) f_model_object = f_model.manager(f_obs=miller_array, r_free_flags=free_flags, xray_structure=model) f_model_object.update_all_scales(log=log) tfofc = f_model_object.map_coefficients(map_type="2mFobs-DFmodel") fofc = f_model_object.map_coefficients(map_type="mFobs-DFmodel") mtz_dataset = tfofc.as_mtz_dataset(column_root_label="FWT") mtz_dataset = mtz_dataset.add_miller_array( miller_array=fofc, column_root_label="DELFWT") name = params.twin_utils.output.map_coeffs_root + "_ML.mtz" mtz_dataset.mtz_object().write(file_name=name) if params.twin_utils.output.obs_and_calc is not None: # i want also a Fobs and Fmodel combined dataset please mtz_dataset = miller_array.as_mtz_dataset( column_root_label="FOBS") mtz_dataset = mtz_dataset.add_miller_array( miller_array=f_model_object.f_model(), column_root_label="FMODEL") name = params.twin_utils.output.obs_and_calc mtz_dataset.mtz_object().write(file_name=name) print(file=log) print(file=log) print("All done \n", file=log) logfile = open(params.twin_utils.output.logfile, 'w') print(string_buffer.getvalue(), file=logfile) print(file=log)
def generate_twin_operators(self): from mmtbx.scaling.twin_analyses import twin_laws TL = twin_laws(miller_array=self.data) for twin_law in TL.operators: if self.verbose: print twin_law.operator.r().as_hkl() return TL.operators
from __future__ import print_function from iotbx import mtz import sys from iotbx.file_reader import any_file f = any_file(sys.argv[1], force_type="hkl", raise_sorry_if_errors=True) # find intensity array - if XDS HKL file need to check label i = [ ma for ma in f.file_content.as_miller_arrays() if (ma.is_xray_intensity_array() or "iobs" in ma.info().label_string()) ] if not i: raise RuntimeError("no intensities found") from mmtbx.scaling.twin_analyses import twin_laws TL = twin_laws(miller_array=i[0]) print("%d possible operators" % len(TL.operators)) for o in TL.operators: print("Le-page delta: %.3f operator: %s" % (o.delta_le_page, o.operator))
def exercise_twin_detection(verbose=False): # simple model with translational pseudosymmetry # XXX one big disadvantage: no centric reflections! pdb_str = """\ CRYST1 12.000 8.000 12.000 90.02 89.96 90.05 P 1 1 ATOM 39 N ASN A 6 5.514 2.664 4.856 1.00 11.99 N ATOM 40 CA ASN A 6 6.831 2.310 4.318 1.00 12.30 C ATOM 41 C ASN A 6 7.854 2.761 5.324 1.00 13.40 C ATOM 42 O ASN A 6 8.219 3.943 5.374 1.00 13.92 O ATOM 43 CB ASN A 6 7.065 3.016 2.993 1.00 12.13 C ATOM 44 CG ASN A 6 5.961 2.735 2.003 1.00 12.77 C ATOM 45 OD1 ASN A 6 5.798 1.604 1.551 1.00 14.27 O ATOM 46 ND2 ASN A 6 5.195 3.747 1.679 1.00 10.07 N ATOM 47 N TYR A 7 8.292 1.817 6.147 1.00 14.70 N ATOM 48 CA TYR A 7 9.159 2.144 7.299 1.00 15.18 C ATOM 49 C TYR A 7 10.603 2.331 6.885 1.00 15.91 C ATOM 50 O TYR A 7 11.041 1.811 5.855 1.00 15.76 O ATOM 51 CB TYR A 7 9.061 1.065 8.369 1.00 15.35 C ATOM 52 CG TYR A 7 7.665 0.929 8.902 1.00 14.45 C ATOM 53 CD1 TYR A 7 6.771 0.021 8.327 1.00 15.68 C ATOM 54 CD2 TYR A 7 7.210 1.756 9.920 1.00 14.80 C ATOM 55 CE1 TYR A 7 5.480 -0.094 8.796 1.00 13.46 C ATOM 56 CE2 TYR A 7 5.904 1.649 10.416 1.00 14.33 C ATOM 57 CZ TYR A 7 5.047 0.729 9.831 1.00 15.09 C ATOM 58 OH TYR A 7 3.766 0.589 10.291 1.00 14.39 O ATOM 59 OXT TYR A 7 11.358 2.999 7.612 1.00 17.49 O TER ATOM 1 N ASN B 6 1.414 5.113 6.019 1.00 12.99 N ATOM 2 CA ASN B 6 2.720 4.776 5.445 1.00 13.30 C ATOM 3 C ASN B 6 3.763 5.209 6.438 1.00 14.40 C ATOM 4 O ASN B 6 4.125 6.391 6.507 1.00 14.92 O ATOM 5 CB ASN B 6 2.922 5.513 4.131 1.00 13.13 C ATOM 6 CG ASN B 6 1.798 5.250 3.160 1.00 13.77 C ATOM 7 OD1 ASN B 6 1.629 4.129 2.686 1.00 15.27 O ATOM 8 ND2 ASN B 6 1.022 6.266 2.875 1.00 11.07 N ATOM 9 N TYR B 7 4.222 4.248 7.230 1.00 15.70 N ATOM 10 CA TYR B 7 5.113 4.552 8.370 1.00 16.18 C ATOM 11 C TYR B 7 6.547 4.754 7.929 1.00 16.91 C ATOM 12 O TYR B 7 6.964 4.259 6.878 1.00 16.76 O ATOM 13 CB TYR B 7 5.042 3.449 9.417 1.00 16.35 C ATOM 14 CG TYR B 7 3.659 3.296 9.977 1.00 15.45 C ATOM 15 CD1 TYR B 7 2.756 2.398 9.402 1.00 16.68 C ATOM 16 CD2 TYR B 7 3.224 4.098 11.023 1.00 15.80 C ATOM 17 CE1 TYR B 7 1.476 2.267 9.896 1.00 14.46 C ATOM 18 CE2 TYR B 7 1.929 3.975 11.545 1.00 15.33 C ATOM 19 CZ TYR B 7 1.063 3.065 10.959 1.00 16.09 C ATOM 20 OH TYR B 7 -0.207 2.910 11.443 1.00 15.39 O ATOM 21 OXT TYR B 7 7.316 5.408 8.654 1.00 18.49 O END """ pdb_in = iotbx.pdb.hierarchy.input(pdb_string=pdb_str) xrs = pdb_in.input.xray_structure_simple() fc = abs(xrs.structure_factors(d_min=2.5).f_calc()) fc = fc.set_observation_type_xray_amplitude() sigf = flex.double(fc.size(), 0.1) + (fc.data() * 0.03) fc = fc.customized_copy(sigmas=sigf) # and now add twinning fc_twin = fc.twin_data(twin_law='-l,-k,-h', alpha=0.4) fc_twin.as_mtz_dataset( column_root_label="F").mtz_object().write("tmp_xtriage_twinned.mtz") # twin_laws laws = twin_analyses.twin_laws(miller_array=fc_twin) assert (len(laws.operators) == 7) delta_santoro = [tl.delta_santoro for tl in laws.operators] assert approx_equal( delta_santoro, [0.104655, 0.104655, 0.066599, 0.104655, 0.076113, 0.066599, 0.028542]) delta_le_page = [tl.delta_le_page for tl in laws.operators] assert approx_equal( delta_le_page, [0.053839, 0.053839, 0.053839, 0.064020, 0.044706, 0.049480, 0.021221]) delta_lebedev = [tl.delta_lebedev for tl in laws.operators] assert approx_equal( delta_lebedev, [0.000787, 0.000787, 0.000767, 0.000912, 0.000637, 0.000705, 0.000302]) # TNCS tps = twin_analyses.detect_pseudo_translations(miller_array=fc_twin, out=null_out()) assert ([tps.mod_h, tps.mod_k, tps.mod_l] == [3, 2, 3]) assert approx_equal(tps.high_peak, 47.152352) assert approx_equal(tps.high_p_value, 0.000103) # L test fc_norm_acentric = twin_analyses.wilson_normalised_intensities( miller_array=fc_twin, normalise=True, out=null_out()).acentric ltest = twin_analyses.l_test(miller_array=fc_norm_acentric, parity_h=3, parity_l=3) assert approx_equal(ltest.mean_l, 0.498974) assert approx_equal(ltest.mean_l2, 0.371674) # we need to go through the wrapper class for a lot of these... out = StringIO() tw = twin_analyses.twin_analyses(miller_array=fc_twin, out=out) tw2 = twin_analyses.twin_analyses(miller_array=fc_twin, out=out, d_hkl_for_l_test=[3, 2, 3]) # Wilson moments wm = tw.wilson_moments assert ([ wm.centric_i_ratio, wm.centric_f_ratio, wm.centric_e_sq_minus_one ] == [None, None, None]) assert approx_equal(wm.acentric_i_ratio, 2.878383) assert approx_equal(wm.acentric_f_ratio, 0.717738) assert approx_equal(wm.acentric_abs_e_sq_minus_one, 0.808899) assert (not wm.centric_present) # Overall analyses out = StringIO() tw.show(out=out) assert ("significantly different than is expected" in out.getvalue()) summary = tw.twin_summary assert approx_equal(summary.l_mean, 0.4989738) assert approx_equal(summary.patterson_height, 47.152352) for k, twin_r_factor in enumerate(summary.r_obs): if (k == 6): assert twin_r_factor < 0.11 else: assert twin_r_factor > 0.3 out2 = StringIO() tw.l_test.show(out=out2) assert ("""\ Mean |L| :0.499 (untwinned: 0.500; perfect twin: 0.375) Mean L^2 :0.372 (untwinned: 0.333; perfect twin: 0.200) """ in out2.getvalue()), out2.getvalue() out3 = StringIO() tw2.l_test.show(out=out3) assert not show_diff(out2.getvalue(), out3.getvalue()) if (verbose): print out.getvalue() # twin_results_interpretation object via cctbx.miller.array API extension # XXX I get slightly different numbers here versus running through the # twin_analyses call above - this seems to be caused by the resolution # limits passed here. Need to confirm these with PHZ. result = fc_twin.analyze_intensity_statistics() assert approx_equal(result.maha_l, 27.580674) assert approx_equal( result.r_obs, [0.514901, 0.514901, 0.397741, 0.580353, 0.579294, 0.420914, 0.114999]) assert approx_equal( result.h_alpha, [0.019980, 0.019980, 0.211788, 0.073926, 0.079920, 0.150849, 0.389610]) assert result.has_twinning()
def exercise_twin_detection (verbose=False) : # simple model with translational pseudosymmetry # XXX one big disadvantage: no centric reflections! pdb_str = """\ CRYST1 12.000 8.000 12.000 90.02 89.96 90.05 P 1 1 ATOM 39 N ASN A 6 5.514 2.664 4.856 1.00 11.99 N ATOM 40 CA ASN A 6 6.831 2.310 4.318 1.00 12.30 C ATOM 41 C ASN A 6 7.854 2.761 5.324 1.00 13.40 C ATOM 42 O ASN A 6 8.219 3.943 5.374 1.00 13.92 O ATOM 43 CB ASN A 6 7.065 3.016 2.993 1.00 12.13 C ATOM 44 CG ASN A 6 5.961 2.735 2.003 1.00 12.77 C ATOM 45 OD1 ASN A 6 5.798 1.604 1.551 1.00 14.27 O ATOM 46 ND2 ASN A 6 5.195 3.747 1.679 1.00 10.07 N ATOM 47 N TYR A 7 8.292 1.817 6.147 1.00 14.70 N ATOM 48 CA TYR A 7 9.159 2.144 7.299 1.00 15.18 C ATOM 49 C TYR A 7 10.603 2.331 6.885 1.00 15.91 C ATOM 50 O TYR A 7 11.041 1.811 5.855 1.00 15.76 O ATOM 51 CB TYR A 7 9.061 1.065 8.369 1.00 15.35 C ATOM 52 CG TYR A 7 7.665 0.929 8.902 1.00 14.45 C ATOM 53 CD1 TYR A 7 6.771 0.021 8.327 1.00 15.68 C ATOM 54 CD2 TYR A 7 7.210 1.756 9.920 1.00 14.80 C ATOM 55 CE1 TYR A 7 5.480 -0.094 8.796 1.00 13.46 C ATOM 56 CE2 TYR A 7 5.904 1.649 10.416 1.00 14.33 C ATOM 57 CZ TYR A 7 5.047 0.729 9.831 1.00 15.09 C ATOM 58 OH TYR A 7 3.766 0.589 10.291 1.00 14.39 O ATOM 59 OXT TYR A 7 11.358 2.999 7.612 1.00 17.49 O TER ATOM 1 N ASN B 6 1.414 5.113 6.019 1.00 12.99 N ATOM 2 CA ASN B 6 2.720 4.776 5.445 1.00 13.30 C ATOM 3 C ASN B 6 3.763 5.209 6.438 1.00 14.40 C ATOM 4 O ASN B 6 4.125 6.391 6.507 1.00 14.92 O ATOM 5 CB ASN B 6 2.922 5.513 4.131 1.00 13.13 C ATOM 6 CG ASN B 6 1.798 5.250 3.160 1.00 13.77 C ATOM 7 OD1 ASN B 6 1.629 4.129 2.686 1.00 15.27 O ATOM 8 ND2 ASN B 6 1.022 6.266 2.875 1.00 11.07 N ATOM 9 N TYR B 7 4.222 4.248 7.230 1.00 15.70 N ATOM 10 CA TYR B 7 5.113 4.552 8.370 1.00 16.18 C ATOM 11 C TYR B 7 6.547 4.754 7.929 1.00 16.91 C ATOM 12 O TYR B 7 6.964 4.259 6.878 1.00 16.76 O ATOM 13 CB TYR B 7 5.042 3.449 9.417 1.00 16.35 C ATOM 14 CG TYR B 7 3.659 3.296 9.977 1.00 15.45 C ATOM 15 CD1 TYR B 7 2.756 2.398 9.402 1.00 16.68 C ATOM 16 CD2 TYR B 7 3.224 4.098 11.023 1.00 15.80 C ATOM 17 CE1 TYR B 7 1.476 2.267 9.896 1.00 14.46 C ATOM 18 CE2 TYR B 7 1.929 3.975 11.545 1.00 15.33 C ATOM 19 CZ TYR B 7 1.063 3.065 10.959 1.00 16.09 C ATOM 20 OH TYR B 7 -0.207 2.910 11.443 1.00 15.39 O ATOM 21 OXT TYR B 7 7.316 5.408 8.654 1.00 18.49 O END """ pdb_in = iotbx.pdb.hierarchy.input(pdb_string=pdb_str) xrs = pdb_in.input.xray_structure_simple() fc = abs(xrs.structure_factors(d_min=2.5).f_calc()) fc = fc.set_observation_type_xray_amplitude() sigf = flex.double(fc.size(), 0.1) + (fc.data() * 0.03) fc = fc.customized_copy(sigmas=sigf) # and now add twinning fc_twin = fc.twin_data(twin_law='-l,-k,-h', alpha=0.4) fc_twin.as_mtz_dataset(column_root_label="F").mtz_object().write( "tmp_xtriage_twinned.mtz") # twin_laws laws = twin_analyses.twin_laws(miller_array=fc_twin) assert (len(laws.operators) == 7) delta_santoro = [ tl.delta_santoro for tl in laws.operators ] assert approx_equal(delta_santoro, [0.104655, 0.104655, 0.066599, 0.104655, 0.076113, 0.066599, 0.028542]) delta_le_page = [ tl.delta_le_page for tl in laws.operators ] assert approx_equal(delta_le_page, [0.053839, 0.053839, 0.053839, 0.064020, 0.044706, 0.049480, 0.021221]) delta_lebedev = [ tl.delta_lebedev for tl in laws.operators ] assert approx_equal(delta_lebedev, [0.000787, 0.000787, 0.000767, 0.000912, 0.000637, 0.000705, 0.000302]) # TNCS tps = twin_analyses.detect_pseudo_translations( miller_array=fc_twin, out=null_out()) assert ([tps.mod_h, tps.mod_k, tps.mod_l] == [3,2,3]) assert approx_equal(tps.high_peak, 47.152352) assert approx_equal(tps.high_p_value, 0.000103) # L test fc_norm_acentric = twin_analyses.wilson_normalised_intensities( miller_array=fc_twin, normalise=True, out=null_out()).acentric ltest = twin_analyses.l_test(miller_array=fc_norm_acentric, parity_h=3, parity_l=3) assert approx_equal(ltest.mean_l, 0.498974) assert approx_equal(ltest.mean_l2, 0.371674) # we need to go through the wrapper class for a lot of these... out = StringIO() tw = twin_analyses.twin_analyses(miller_array=fc_twin, out=out) tw2 = twin_analyses.twin_analyses(miller_array=fc_twin, out=out, d_hkl_for_l_test=[3,2,3]) # Wilson moments wm = tw.wilson_moments assert ([wm.centric_i_ratio, wm.centric_f_ratio, wm.centric_e_sq_minus_one] == [None, None, None]) assert approx_equal(wm.acentric_i_ratio, 2.878383) assert approx_equal(wm.acentric_f_ratio, 0.717738) assert approx_equal(wm.acentric_abs_e_sq_minus_one, 0.808899) assert (not wm.centric_present) # Overall analyses out = StringIO() tw.show(out=out) assert ("significantly different than is expected" in out.getvalue()) summary = tw.twin_summary assert approx_equal(summary.l_mean, 0.4989738) assert approx_equal(summary.patterson_height, 47.152352) for k, twin_r_factor in enumerate(summary.r_obs) : if (k == 6) : assert twin_r_factor < 0.11 else : assert twin_r_factor > 0.3 out2 = StringIO() tw.l_test.show(out=out2) assert ("""\ Mean |L| :0.499 (untwinned: 0.500; perfect twin: 0.375) Mean L^2 :0.372 (untwinned: 0.333; perfect twin: 0.200) """ in out2.getvalue()), out2.getvalue() out3 = StringIO() tw2.l_test.show(out=out3) assert not show_diff(out2.getvalue(), out3.getvalue()) if (verbose) : print out.getvalue() # twin_results_interpretation object via cctbx.miller.array API extension # XXX I get slightly different numbers here versus running through the # twin_analyses call above - this seems to be caused by the resolution # limits passed here. Need to confirm these with PHZ. result = fc_twin.analyze_intensity_statistics() assert approx_equal(result.maha_l, 27.580674) assert approx_equal(result.r_obs, [0.514901, 0.514901, 0.397741, 0.580353, 0.579294, 0.420914, 0.114999]) assert approx_equal(result.h_alpha, [0.019980, 0.019980, 0.211788, 0.073926, 0.079920, 0.150849, 0.389610]) assert result.has_twinning()
def run(args, command_name="phenix.twin_map_utils"): log=sys.stdout params=None if (len(args) == 0 or "--help" in args or "--h" in args or "-h" in args): print_help(command_name=command_name) else: log = multi_out() if (not "--quiet" in args): log.register(label="stdout", file_object=sys.stdout) string_buffer = StringIO() string_buffer_plots = StringIO() log.register(label="log_buffer", file_object=string_buffer) phil_objects = [] argument_interpreter = master_params.command_line_argument_interpreter( home_scope="map_coefs") for arg in args: command_line_params = None arg_is_processed = False # is it a file? if (os.path.isfile(arg)): ## is this a file name? # check if it is a phil file try: command_line_params = iotbx.phil.parse(file_name=arg) if command_line_params is not None: phil_objects.append(command_line_params) arg_is_processed = True except KeyboardInterrupt: raise except Exception : pass else: try: command_line_params = argument_interpreter.process(arg=arg) if command_line_params is not None: phil_objects.append(command_line_params) arg_is_processed = True except KeyboardInterrupt: raise except Exception : pass if not arg_is_processed: print >> log, "##----------------------------------------------##" print >> log, "## Unknown file or keyword:", arg print >> log, "##----------------------------------------------##" print >> log raise Sorry("Unknown file or keyword: %s" % arg) effective_params = master_params.fetch(sources=phil_objects) params = effective_params.extract() """ new_params = master_params.format(python_object=params) new_params.show(out=log) """ # now get the unit cell from the pdb file hkl_xs = crystal_symmetry_from_any.extract_from( file_name=params.twin_utils.input.xray_data.file_name) pdb_xs = crystal_symmetry_from_any.extract_from( file_name=params.twin_utils.input.model.file_name) phil_xs=None if ([params.twin_utils.input.unit_cell, params.twin_utils.input.space_group]).count(None)<2: phil_xs = crystal.symmetry( unit_cell=params.twin_utils.input.unit_cell, space_group_info=params.twin_utils.input.space_group ) combined_xs = crystal.select_crystal_symmetry( None,phil_xs, [pdb_xs],[hkl_xs]) # inject the unit cell and symmetry in the phil scope please params.twin_utils.input.unit_cell = combined_xs.unit_cell() params.twin_utils.input.space_group = \ sgtbx.space_group_info( group = combined_xs.space_group() ) new_params = master_params.format(python_object=params) new_params.show(out=log) if params.twin_utils.input.unit_cell is None: raise Sorry("unit cell not specified") if params.twin_utils.input.space_group is None: raise Sorry("space group not specified") if params.twin_utils.input.xray_data.file_name is None: raise Sorry("Xray data not specified") if params.twin_utils.input.model.file_name is None: raise Sorry("pdb file with model not specified") #----------------------------------------------------------- # # step 1: read in the reflection file # phil_xs = crystal.symmetry( unit_cell=params.twin_utils.input.unit_cell, space_group_info=params.twin_utils.input.space_group ) xray_data_server = reflection_file_utils.reflection_file_server( crystal_symmetry = phil_xs, force_symmetry = True, reflection_files=[]) miller_array = None free_flags = None tmp_params = utils.data_and_flags_master_params().extract() # insert proper values please tmp_params.file_name = params.twin_utils.input.xray_data.file_name tmp_params.labels = params.twin_utils.input.xray_data.obs_labels tmp_params.r_free_flags.file_name=params.twin_utils.input.xray_data.file_name tmp_params.r_free_flags.label=params.twin_utils.input.xray_data.free_flag tmp_object = utils.determine_data_and_flags( reflection_file_server = xray_data_server, parameters = tmp_params, log=log ) miller_array = tmp_object.extract_data() if miller_array.is_xray_intensity_array(): miller_array = miller_array.f_sq_as_f() assert miller_array.is_xray_amplitude_array() free_flags = tmp_object.extract_flags(data = miller_array) print >> log print >> log, "Attempting to extract Free R flags" free_flags = free_flags.customized_copy( data = flex.bool( free_flags.data()==1 ) ) if free_flags is None: free_flags = miller_array.generate_r_free_flags(use_lattice_symmetry=True) assert miller_array.observation_type() is not None print >> log print >> log, "Summary info of observed data" print >> log, "=============================" miller_array.show_summary(f=log) print >> log if miller_array.indices().size() == 0: raise Sorry("No data available") #---------------------------------------------------------------- # Step 2: get an xray structure from the PDB file # model = pdb.input(file_name=params.twin_utils.input.model.file_name).xray_structure_simple( crystal_symmetry=phil_xs) print >> log, "Atomic model summary" print >> log, "====================" model.show_summary(f=log) print >> log #---------------------------------------------------------------- # step 3: get the twin laws for this xs twin_laws = twin_analyses.twin_laws( miller_array, lattice_symmetry_max_delta=\ params.twin_utils.parameters.twinning.max_delta, out=log) print >> log print >> log, "Preliminary data analyses" print >> log, "==========================" twin_laws.show(out=log) #--------- # step 3: # make twin model managers for all twin laws print >> log print >> log, "Overall and bulk solvent scale paranmeters and twin fraction estimation" print >> log, "=======================================================================" twin_models = [] operator_count = 0 if params.twin_utils.parameters.twinning.twin_law is not None: tmp_law = sgtbx.rt_mx( params.twin_utils.parameters.twinning.twin_law ) tmp_law = twin_analyses.twin_law(tmp_law,None,None,None,None,None) twin_laws.operators = [ tmp_law ] for twin_law in twin_laws.operators: operator_count += 1 operator_hkl = sgtbx.change_of_basis_op( twin_law.operator ).as_hkl() twin_model = twin_f_model.twin_model_manager( f_obs=miller_array, r_free_flags = free_flags, xray_structure=model, twin_law = twin_law.operator, detwin_mode = params.twin_utils.parameters.twinning.detwin_mode, out=log) print >> log, "--- bulk solvent scaling ---" twin_model.update_solvent_and_scale(update_f_part1=False) twin_model.r_values() twin_model.target() twin_model.show_k_sol_b_sol_b_cart_target() twin_model.show_essential() wfofc = twin_model.map_coefficients(map_type="mFo-DFc" ) wtfofc = twin_model.map_coefficients(map_type="2mFo-DFc" ) grad = twin_model.map_coefficients(map_type="gradient" ) mtz_dataset = wtfofc.as_mtz_dataset( column_root_label="FWT") mtz_dataset = mtz_dataset.add_miller_array( miller_array = wfofc, column_root_label = "DFWT" ) mtz_dataset = mtz_dataset.add_miller_array( miller_array = grad, column_root_label = "GRAD" ) name = params.twin_utils.output.map_coeffs_root+"_"+str(operator_count)+".mtz" print >> log print >> log, "Writing %s for twin law %s"%(name,operator_hkl) print >> log mtz_dataset.mtz_object().write( file_name=name) if params.twin_utils.output.obs_and_calc is not None: # i want also a Fobs and Fmodel combined dataset please mtz_dataset = miller_array.as_mtz_dataset( column_root_label="FOBS") mtz_dataset = mtz_dataset.add_miller_array( miller_array = twin_model.f_model(), column_root_label="FMODEL") name = params.twin_utils.output.obs_and_calc mtz_dataset.mtz_object().write( file_name=name) if len(twin_laws.operators)==0: print >> log print >> log, "No twin laws were found" print >> log, "Performing maximum likelihood based bulk solvent scaling" f_model_object = f_model.manager( f_obs = miller_array, r_free_flags = free_flags, xray_structure = model ) f_model_object.update_solvent_and_scale(out=log) tfofc = f_model_object.map_coefficients(map_type="2mFobs-DFmodel") fofc = f_model_object.map_coefficients(map_type="mFobs-DFmodel") mtz_dataset = tfofc.as_mtz_dataset( column_root_label="FWT") mtz_dataset = mtz_dataset.add_miller_array( miller_array = fofc, column_root_label = "DELFWT" ) name = params.twin_utils.output.map_coeffs_root+"_ML.mtz" mtz_dataset.mtz_object().write( file_name=name) if params.twin_utils.output.obs_and_calc is not None: # i want also a Fobs and Fmodel combined dataset please mtz_dataset = miller_array.as_mtz_dataset( column_root_label="FOBS") mtz_dataset = mtz_dataset.add_miller_array( miller_array = f_model_object.f_model(), column_root_label="FMODEL") name = params.twin_utils.output.obs_and_calc mtz_dataset.mtz_object().write( file_name=name) print >> log print >> log print >> log, "All done \n" logfile = open(params.twin_utils.output.logfile,'w') print >> logfile, string_buffer.getvalue() print >> log
def run(self, experiments, reflections): self.logger.log_step_time("REINDEX") # Get list of twinning operators for this space group operators = twin_laws(self.params.scaling.i_model).operators if not operators: self.logger.log("No indexing ambiguity. Skipping this step.") return experiments, reflections self.logger.log("Resolving indexing ambiguity using operators h,k,l, %s"%", ".join( \ [op.operator.r().as_hkl() for op in operators])) operators = [ sgtbx.change_of_basis_op(op.operator.r().as_hkl()) for op in operators ] result = flex.reflection_table() scaler = experiment_scaler(self.params, self.mpi_helper, self.logger) model_intensities = self.params.scaling.i_model target_symm = symmetry( unit_cell=self.params.scaling.unit_cell, space_group_info=self.params.scaling.space_group) def get_correlation(cb_op=None): """ Helper function to get CC to the reference given an operator """ # Build a miller array for the experiment reflections exp_miller_indices = miller.set( target_symm, exp_reflections['miller_index_asymmetric'], True) exp_intensities = miller.array( exp_miller_indices, exp_reflections['intensity.sum.value'], flex.sqrt(exp_reflections['intensity.sum.variance'])) if cb_op: exp_intensities = exp_intensities.change_basis( cb_op).map_to_asu() # Extract an array of HKLs from the model to match the experiment HKLs matching_indices = miller.match_multi_indices( miller_indices_unique=model_intensities.indices(), miller_indices=exp_intensities.indices()) # Least squares scaling_result = scaler.fit_experiment_to_reference( model_intensities, exp_intensities, matching_indices) return scaling_result.correlation if scaling_result.correlation is not None else -1 # Test each experiment to see if an operator gives a better CC to the reference, and if it does, apply it for expt_id, experiment in enumerate(experiments): exp_reflections = reflections.select( reflections['exp_id'] == experiment.identifier) all_correlations = [] best_correlation = get_correlation() all_correlations.append(best_correlation) best_op = None for cb_op in operators: test_correlation = get_correlation(cb_op) all_correlations.append(test_correlation) if test_correlation > best_correlation: best_correlation = test_correlation best_op = cb_op if best_op: exp_miller_indices = miller.set( target_symm, exp_reflections['miller_index'], True).change_basis(best_op) exp_reflections[ 'miller_index_asymmetric'] = exp_miller_indices.map_to_asu( ).indices() exp_reflections['miller_index'] = exp_miller_indices.indices() experiment.crystal = MosaicCrystalSauter2014( experiment.crystal.change_basis(best_op) ) # need to use wrapper because of cctbx/dxtbx#5 result.extend(exp_reflections) self.logger.log( "Expt %d, reindexing op correlations: %s" % (expt_id, ", ".join(["%6.3f" % c for c in all_correlations]))) self.logger.log_step_time("REINDEX", True) self.logger.log("Memory usage: %d MB" % get_memory_usage()) return experiments, result