def exercise_map_tools(): prefix = "tmp_iotbx_map_tools" pdb_file = prefix + ".pdb" mtz_file = prefix + ".mtz" pdb_in = iotbx.pdb.hierarchy.input(pdb_string="""\ ATOM 1 N GLY P -1 -22.866 -2.627 15.217 1.00 0.00 N ATOM 2 CA GLY P -1 -22.714 -3.068 16.621 1.00 0.00 C ATOM 3 C GLY P -1 -21.276 -3.457 16.936 1.00 0.00 C ATOM 4 O GLY P -1 -20.538 -3.887 16.047 1.00 0.00 O ATOM 5 H1 GLY P -1 -22.583 -3.364 14.590 1.00 0.00 H ATOM 6 H2 GLY P -1 -22.293 -1.817 15.040 1.00 0.00 H ATOM 7 H3 GLY P -1 -23.828 -2.392 15.027 1.00 0.00 H ATOM 8 HA2 GLY P -1 -23.016 -2.261 17.288 1.00 0.00 H ATOM 9 HA3 GLY P -1 -23.352 -3.933 16.803 1.00 0.00 H """) xrs = pdb_in.input.xray_structure_simple() with open(pdb_file, "w") as f: f.write(pdb_in.hierarchy.as_pdb_string(xrs)) fc = xrs.structure_factors(d_min=1.5).f_calc() dec = mtz.label_decorator(phases_prefix="PH") # part 1: phenix.refine style mtz_data = fc.as_mtz_dataset(column_root_label="2FOFCWT", label_decorator=dec) mtz_data.add_miller_array(fc, column_root_label="2FOFCWT_no_fill", label_decorator=dec) mtz_data.add_miller_array(fc, column_root_label="FOFCWT", label_decorator=dec) mtz_data.add_miller_array(fc, column_root_label="ANOM", label_decorator=dec) mtz_data.mtz_object().write(mtz_file) converted = map_tools.auto_convert_map_coefficients(mtz_file=mtz_file, pdb_file=pdb_file) assert (not None in [converted.f_map, converted.diff_map, converted.anom_map]) assert (converted.f_map == "tmp_iotbx_map_tools_2mFo-DFc.ccp4") assert (converted.f_map_type == "2mFo-DFc") server = map_tools.server(mtz_file) files = server.convert_phenix_maps(file_base=prefix) files = [os.path.basename(file_name) for file_name in files] assert (files == [ 'tmp_iotbx_map_tools_anomalous.ccp4', 'tmp_iotbx_map_tools_mFo-DFc.ccp4', 'tmp_iotbx_map_tools_2mFo-DFc.ccp4', 'tmp_iotbx_map_tools_2mFo-DFc_no_fill.ccp4' ]) for fn in files: assert os.path.isfile(fn) os.remove(fn) file_name = server.convert_any_map(f_label="2FOFCWT,PH2FOFCWT", phi_label=None, fom_label=None, use_standard_map_names=True) assert (file_name == "tmp_iotbx_map_tools_2mFo-DFc.ccp4") assert os.path.isfile(file_name) # part 2: Phaser/Refmac style mtz_data = fc.as_mtz_dataset(column_root_label="FWT", label_decorator=mtz.ccp4_label_decorator()) mtz_data.add_miller_array(fc, column_root_label="DELFWT", label_decorator=mtz.ccp4_label_decorator()) mtz_data.mtz_object().write(mtz_file) converted = map_tools.auto_convert_map_coefficients(mtz_file=mtz_file, pdb_file=pdb_file) assert (not None in [converted.f_map, converted.diff_map]) assert (converted.f_map == 'tmp_iotbx_map_tools_FWT.ccp4') assert (converted.f_map_type == "2mFo-DFc") server = map_tools.server(mtz_file) map_files = server.convert_ccp4_map(pdb_file=pdb_file) map_files = [os.path.basename(file_name) for file_name in map_files] assert (map_files == [ 'tmp_iotbx_map_tools_FWT.ccp4', 'tmp_iotbx_map_tools_DELFWT.ccp4' ]) # part 3: Resolve style mtz_data = fc.as_mtz_dataset(column_root_label="FWT", label_decorator=mtz.ccp4_label_decorator()) ampl = abs(fc) phases = fc.phases() fom = fc.customized_copy(data=flex.double(fc.data().size(), 1), observation_type=None) mtz_data.add_miller_array(ampl, column_root_label="FP") mtz_data.add_miller_array(phases, column_root_label="PHIM") mtz_data.add_miller_array(fom, column_root_label="FOMM") mtz_data.mtz_object().write(mtz_file) server = map_tools.server(mtz_file) map_file = server.convert_resolve_map(pdb_file=None, force=False) assert (map_file == 'tmp_iotbx_map_tools.ccp4') assert os.path.exists(map_file) # write_map_coefficients_generic map_tools.write_map_coefficients_generic( map_coeffs=[fc, fc.generate_bijvoet_mates(), fc, fc], map_types=["2mFo-DFc", "mFo-DFc", "anom", "other"], file_name="tmp_iotbx_map_tools2.mtz") mtz_in = file_reader.any_file("tmp_iotbx_map_tools2.mtz") labels = [ a.info().label_string() for a in mtz_in.file_server.miller_arrays ] assert (labels == [ '2FOFCWT,PH2FOFCWT', 'FOFCWT,PHFOFCWT', 'ANOM,PHANOM', 'other,PHother' ])
def exercise_reflections(): hkl_handler = reflections.reflections_handler( allowed_param_names=phil_names) from cctbx import miller from cctbx import crystal from cctbx.array_family import flex symm = crystal.symmetry(unit_cell=(30, 30, 40, 90, 90, 120), space_group_symbol="P 61 2 2") miller_set = miller.build_set(crystal_symmetry=symm, anomalous_flag=True, d_min=1.5) n_refl = miller_set.indices().size() data = flex.random_double(n_refl) sigmas = flex.random_double(n_refl) f_obs = miller_set.array(data=data, sigmas=sigmas) f_obs_merged = f_obs.average_bijvoet_mates() flags = f_obs_merged.generate_r_free_flags() # single dataset mtz_dataset = f_obs_merged.as_mtz_dataset(column_root_label="F-obs", wavelength=1.54) mtz_dataset.add_miller_array(flags, column_root_label="R-free-flags") file_name = "tst_iotbs_gui_tools.mtz" mtz_dataset.mtz_object().write(file_name) assert (hkl_handler.set_param_file( file_name=file_name, file_param_name="refinement.input.xray_data.file_name") == True) assert (hkl_handler.set_param_file( file_name=file_name, file_param_name="refinement.input.xray_data.r_free_flags.file_name") == False) assert (hkl_handler.get_rfree_labels( file_param_name="refinement.input.xray_data.r_free_flags.file_name") == hkl_handler.get_rfree_labels(file_name=file_name) == ['R-free-flags']) assert (hkl_handler.get_rfree_labels(file_name=file_name, neutron=False) == hkl_handler.get_rfree_labels(file_name=file_name, neutron=True) == ['R-free-flags']) assert approx_equal( 1.54, hkl_handler.get_wavelength(file_name=file_name, labels="F-obs,SIGF-obs")) # join X/N datasets hkl_handler = reflections.reflections_handler( allowed_param_names=phil_names) data_neutron = flex.random_double(n_refl) sigmas_neutron = flex.random_double(n_refl) f_obs_neutron = miller_set.array(data=data_neutron, sigmas=sigmas_neutron) mtz_dataset = f_obs_merged.as_mtz_dataset(column_root_label="F-obs-xray") mtz_dataset.add_miller_array(f_obs_neutron, column_root_label="F-obs-neutron") mtz_dataset.add_miller_array(flags, column_root_label="R-free-flags-xray") mtz_dataset.add_miller_array(flags.deep_copy(), column_root_label="R-free-flags-neutron") file_name = "tst_iotbs_gui_tools.mtz" mtz_dataset.mtz_object().write(file_name) assert (hkl_handler.set_param_file( file_name=file_name, file_param_name="refinement.input.xray_data.file_name") == True) for i, phil_name in enumerate(phil_names[1:4]): assert (hkl_handler.set_param_file(file_name=file_name, file_param_name=phil_name) == False) assert (hkl_handler.get_rfree_labels( file_param_name="refinement.input.xray_data.r_free_flags.file_name") == hkl_handler.get_rfree_labels(file_name=file_name) == ['R-free-flags-xray', 'R-free-flags-neutron']) assert (hkl_handler.get_rfree_labels( file_name=file_name, neutron=False) == ['R-free-flags-xray']) assert (hkl_handler.get_rfree_labels( file_name=file_name, neutron=True) == ['R-free-flags-neutron']) hkl_handler.check_symmetry(file_name=file_name) assert (hkl_handler.get_data_labels( file_param_name="refinement.input.xray_data.file_name") == hkl_handler.get_data_labels(file_name=file_name) == hkl_handler.get_amplitude_labels(file_name=file_name) == [ "F-obs-xray,SIGF-obs-xray", "F-obs-neutron(+),SIGF-obs-neutron(+)," + "F-obs-neutron(-),SIGF-obs-neutron(-)" ]) assert (hkl_handler.get_anomalous_data_labels( file_param_name="refinement.input.xray_data.file_name") == [ "F-obs-neutron(+),SIGF-obs-neutron(+)," + "F-obs-neutron(-),SIGF-obs-neutron(-)" ]) assert (hkl_handler.has_anomalous_data( file_param_name="refinement.input.xray_data.file_name")) assert (hkl_handler.get_rfree_flag_value( array_name="F-obs-xray,SIGF-obs-xray", file_param_name="refinement.input.xray_data.file_name") is None) assert (hkl_handler.get_rfree_flag_value( array_name='R-free-flags-xray', file_param_name="refinement.input.xray_data.r_free_flags.file_name") == 1) (d_max, d_min) = hkl_handler.d_max_min() assert approx_equal(d_max, 25.98, eps=0.01) assert approx_equal(d_min, 1.5, eps=0.01) assert hkl_handler.space_group_as_str() == "P 61 2 2" assert (hkl_handler.unit_cell_as_str() == "30 30 40 90 90 120") assert (hkl_handler.unit_cell_as_str( separator=",") == "30,30,40,90,90,120") n_refl_merged = len(f_obs_merged.indices()) phi_array = f_obs_merged.random_phases_compatible_with_phase_restrictions( deg=True) fom_array = phi_array.array(data=flex.random_double(n_refl_merged)) hl_data = flex.hendrickson_lattman(n_refl_merged, (0, 0, 0, 0)) hl_coeffs = phi_array.array(data=hl_data) assert (hl_coeffs.is_hendrickson_lattman_array()) from iotbx.mtz import label_decorator import iotbx.mtz class resolve_label_decorator(label_decorator): def phases(self, *args, **kwds): return label_decorator.phases(self, *args, **kwds) + "M" def hendrickson_lattman(self, *args, **kwds): return label_decorator.hendrickson_lattman(self, *args, ** kwds) + "M" mtz_dataset = f_obs_merged.as_mtz_dataset(column_root_label="FP") mtz_dataset.add_miller_array(phi_array, column_root_label="PHIM", label_decorator=resolve_label_decorator(), column_types="P") mtz_dataset.add_miller_array(fom_array, column_root_label="FOMM", column_types="W") mtz_dataset.add_miller_array(hl_coeffs, column_root_label="HL", label_decorator=resolve_label_decorator()) fwt_map = f_obs_merged.customized_copy( sigmas=None).phase_transfer(phi_array) mtz_dataset.add_miller_array( fwt_map, column_root_label="FWT", label_decorator=iotbx.mtz.ccp4_label_decorator()) mtz_dataset.add_miller_array(flags, column_root_label="FreeR_flag") resolve_file = "tst_iotbx_gui_tools_resolve.mtz" mtz_dataset.mtz_object().write(resolve_file) hkl_handler = reflections.reflections_handler( allowed_param_names=["map_coeffs"]) hkl_handler.set_param_file(file_name=resolve_file, file_param_name="map_coeffs") assert hkl_handler.has_data(file_name=resolve_file) l1 = hkl_handler.get_map_coeff_labels(file_name=resolve_file) assert (l1 == ['FWT,PHWT', 'FP,PHIM,FOMM']) l2 = hkl_handler.get_phase_deg_labels(file_name=resolve_file) assert (l2 == [ 'HLAM,HLBM,HLCM,HLDM', 'FWT,PHWT', 'PHIM', ]), l2 l3 = hkl_handler.get_experimental_phase_labels(file_name=resolve_file) #print l3 l4 = hkl_handler.get_data_labels_for_wizard(file_name=resolve_file) assert (l4 == ['FP SIGFP']) l5 = hkl_handler.get_map_coeff_labels_for_build(file_name=resolve_file) assert (l5 == ['FWT,PHWT', 'FP,PHIM,FOMM']), l5 hkl_in = hkl_handler.get_file(file_name=resolve_file) assert (reflections.get_mtz_label_prefix(hkl_in) == "/crystal/dataset") map_coeffs = reflections.map_coeffs_from_mtz_file(resolve_file, f_label="FP,SIGFP") assert map_coeffs.is_complex_array() try: map_coeffs = reflections.map_coeffs_from_mtz_file(resolve_file) except Sorry: pass else: raise Exception_expected assert (hkl_handler.get_map_coeff_labels_for_fft( file_name=resolve_file) == ['FWT,PHWT', 'FP,SIGFP PHIM FOMM']) # miscellaneous utilities file_name = resolve_file hkl_in = file_reader.any_file(file_name) hkl_server = hkl_in.file_server assert approx_equal(reflections.get_high_resolution(hkl_server), 1.5, eps=0.0001) descriptions = [] for miller_array in hkl_server.miller_arrays: (sg, uc) = reflections.get_miller_array_symmetry(miller_array) assert (uc == "30 30 40 90 90 120") assert (str(sg) == "P 61 2 2") descriptions.append(reflections.get_array_description(miller_array)) assert (descriptions == [ 'Amplitude', 'Phases', 'Weights', 'HL coeffs', 'Map coeffs', 'R-free flag' ]), descriptions handler = reflections.reflections_handler() handler.save_file(input_file=hkl_in) assert (not handler.has_anomalous_data()) assert (handler.get_resolution_range( file_name=file_name) == "(25.981 - 1.500)") assert (handler.get_resolution_limits(file_name=file_name) == ('(25.981)', '(1.500)')) fmodel = phi_array.array( data=flex.complex_double(n_refl_merged, complex(0.5, 0.8))) m1 = phi_array.array( data=flex.complex_double(n_refl_merged, complex(1, 0))) m2 = phi_array.array( data=flex.complex_double(n_refl_merged, complex(0.5, 0))) m3 = phi_array.array(flex.complex_double(n_refl_merged, complex(1, 1))) dec = label_decorator(phases_prefix="PH") mtz_dataset = fmodel.as_mtz_dataset(column_root_label="F-model", label_decorator=dec) mtz_dataset.add_miller_array(m1, column_root_label="2FOFCWT", label_decorator=dec) mtz_dataset.add_miller_array(m2, column_root_label="FOFCWT", label_decorator=dec) mtz_dataset.add_miller_array(m3, column_root_label="2FOFCWT_no_fill", label_decorator=dec) file_name = "tst_iotbx_gui_tools_map_coeffs.mtz" mtz_dataset.mtz_object().write(file_name) hkl_handler = reflections.reflections_handler( allowed_param_names=["fmodel", "map_coeffs"]) hkl_handler.set_param_file(file_name=file_name, file_param_name="fmodel") assert (hkl_handler.get_fmodel_labels(file_name=file_name) == [ 'F-model,PHF-model' ]) assert (hkl_handler.get_amplitude_labels(file_name=file_name) == []) phi_labels = hkl_handler.get_phase_deg_labels(file_name=file_name) assert (len(phi_labels) == 4) phi_cols = hkl_handler.get_phase_column_labels(file_name=file_name) assert (phi_cols == [ 'PHF-model', 'PH2FOFCWT', 'PHFOFCWT', 'PH2FOFCWT_no_fill' ]) assert (len( hkl_handler.get_amplitude_column_labels(file_name=file_name, allow_conversion=True)) == 0) fc_cols = hkl_handler.get_fmodel_labels(file_name=file_name, first_column_only=True) assert (fc_cols == ['F-model']) hkl_server = file_reader.any_file(file_name).file_server map_labels = reflections.get_map_coeff_labels(hkl_server) assert (map_labels == [ '2FOFCWT,PH2FOFCWT', 'FOFCWT,PHFOFCWT', '2FOFCWT_no_fill,PH2FOFCWT_no_fill', ]) map_labels = reflections.get_map_coeffs_for_build(hkl_server) assert map_labels == [ '2FOFCWT,PH2FOFCWT', '2FOFCWT_no_fill,PH2FOFCWT_no_fill' ] map_coeffs = reflections.extract_phenix_refine_map_coeffs(file_name) assert (len(map_coeffs) == 3) hkl_file = file_reader.any_file(file_name) assert reflections.get_mtz_label_prefix(hkl_file) == "/crystal/dataset" # other stuff (fp, fpp) = reflections.get_fp_fpp_from_sasaki("Se", 0.979) assert fp is not None and fpp is not None
def recycle(miller_array, column_root_label, column_types=None, verbose=0): original_dataset = to_mtz(miller_array, column_root_label, column_types) label_decorator = mtz.label_decorator() written = original_dataset.mtz_object() if (0 or verbose): written.show_summary() original_dataset.mtz_object().write(file_name="tmp_iotbx_mtz.mtz") restored = mtz.object(file_name="tmp_iotbx_mtz.mtz") if (0 or verbose): restored.show_summary() assert restored.title() == written.title() assert [line.rstrip() for line in restored.history()] \ == list(written.history()) assert restored.space_group_name() == written.space_group_name() assert restored.space_group_number() == written.space_group_number() assert restored.space_group() == written.space_group() assert restored.point_group_name() == written.point_group_name() assert restored.lattice_centring_type() == written.lattice_centring_type() assert restored.n_batches() == written.n_batches() assert restored.n_reflections() == written.n_reflections() assert eps_eq(restored.max_min_resolution(), written.max_min_resolution(), eps=1.e-5) assert restored.n_crystals() == written.n_crystals() assert restored.n_active_crystals() == written.n_active_crystals() assert restored.n_crystals() == 2 for rx, wx in zip(restored.crystals(), written.crystals()): assert rx.name() == wx.name() assert rx.project_name() == wx.project_name() assert rx.unit_cell().is_similar_to(wx.unit_cell()) assert rx.n_datasets() == wx.n_datasets() for rd, wd in zip(rx.datasets(), wx.datasets()): assert rd.name() == wd.name() assert rd.wavelength() == wd.wavelength() assert rd.n_columns() == wd.n_columns() miller_set = restored.crystals()[1].miller_set() assert miller_set.indices().size() == restored.n_reflections() crystal_symmetry = restored.crystals()[1].crystal_symmetry() restored_dataset = restored.crystals()[1].datasets()[0] if (not miller_array.anomalous_flag()): if (miller_array.sigmas() is None): if (miller_array.is_complex_array()): assert restored_dataset.n_columns() == 3 + 2 group = restored.extract_complex( column_label_ampl=column_root_label, column_label_phi=label_decorator.phases(column_root_label)) elif (miller_array.is_hendrickson_lattman_array()): assert restored_dataset.n_columns() == 3 + 4 deco = label_decorator.hendrickson_lattman group = restored.extract_hendrickson_lattman( column_label_a=deco(column_root_label, 0), column_label_b=deco(column_root_label, 1), column_label_c=deco(column_root_label, 2), column_label_d=deco(column_root_label, 3)) else: assert restored_dataset.n_columns() == 3 + 1 group = restored.extract_reals(column_label=column_root_label) r = miller.array(miller_set=miller.set( crystal_symmetry=crystal_symmetry, indices=group.indices, anomalous_flag=False), data=group.data) else: assert restored_dataset.n_columns() == 3 + 2 group = restored.extract_observations( column_label_data=column_root_label, column_label_sigmas=label_decorator.sigmas(column_root_label)) r = miller.array(miller_set=miller.set( crystal_symmetry=crystal_symmetry, indices=group.indices, anomalous_flag=False), data=group.data, sigmas=group.sigmas) else: if (miller_array.sigmas() is None): if (miller_array.is_complex_array()): assert restored_dataset.n_columns() == 3 + 4 group = restored.extract_complex_anomalous( column_label_ampl_plus=label_decorator.anomalous( column_root_label, "+"), column_label_phi_plus=label_decorator.phases( column_root_label, "+"), column_label_ampl_minus=label_decorator.anomalous( column_root_label, "-"), column_label_phi_minus=label_decorator.phases( column_root_label, "-")) elif (miller_array.is_hendrickson_lattman_array()): assert restored_dataset.n_columns() == 3 + 8 deco = label_decorator.hendrickson_lattman group = restored.extract_hendrickson_lattman_anomalous( column_label_a_plus=deco(column_root_label, 0, "+"), column_label_b_plus=deco(column_root_label, 1, "+"), column_label_c_plus=deco(column_root_label, 2, "+"), column_label_d_plus=deco(column_root_label, 3, "+"), column_label_a_minus=deco(column_root_label, 0, "-"), column_label_b_minus=deco(column_root_label, 1, "-"), column_label_c_minus=deco(column_root_label, 2, "-"), column_label_d_minus=deco(column_root_label, 3, "-")) else: assert restored_dataset.n_columns() == 3 + 2 group = restored.extract_reals_anomalous( column_label_plus=label_decorator.anomalous( column_root_label, "+"), column_label_minus=label_decorator.anomalous( column_root_label, "-")) r = miller.array(miller_set=miller.set( crystal_symmetry=crystal_symmetry, indices=group.indices, anomalous_flag=True), data=group.data) else: assert restored_dataset.n_columns() == 3 + 4 group = restored.extract_observations_anomalous( column_label_data_plus=label_decorator.anomalous( column_root_label, "+"), column_label_sigmas_plus=label_decorator.sigmas( column_root_label, "+"), column_label_data_minus=label_decorator.anomalous( column_root_label, "-"), column_label_sigmas_minus=label_decorator.sigmas( column_root_label, "-")) r = miller.array(miller_set=miller.set( crystal_symmetry=crystal_symmetry, indices=group.indices, anomalous_flag=True), data=group.data, sigmas=group.sigmas) verify_miller_arrays(miller_array, r) restored_miller_arrays = restored.as_miller_arrays() assert len(restored_miller_arrays) == 1 thff = restored_miller_arrays[0].info().type_hints_from_file assert thff is not None assert miller_array.is_hendrickson_lattman_array() \ == (thff == "hendrickson_lattman") verify_miller_arrays(miller_array, restored_miller_arrays[0]) mtz_object = miller_array.as_mtz_dataset( column_root_label=column_root_label).mtz_object() restored_miller_arrays = mtz_object.as_miller_arrays() assert len(restored_miller_arrays) == 1 verify_miller_arrays(miller_array, restored_miller_arrays[0]) if (miller_array.is_bool_array() or miller_array.is_integer_array() or miller_array.is_real_array()): cb_op = miller_array.change_of_basis_op_to_niggli_cell() mtz_object.change_basis_in_place(cb_op=cb_op) cb_array = miller_array.change_basis(cb_op=cb_op) assert mtz_object.space_group() == cb_array.space_group() for mtz_crystal in mtz_object.crystals(): assert mtz_crystal.unit_cell().is_similar_to(cb_array.unit_cell()) restored_miller_arrays = mtz_object.as_miller_arrays() assert len(restored_miller_arrays) == 1 verify_miller_arrays(cb_array, restored_miller_arrays[0]) mtz_object.change_basis_in_place(cb_op=cb_op.inverse()) assert mtz_object.space_group() == miller_array.space_group() for mtz_crystal in mtz_object.crystals(): assert mtz_crystal.unit_cell().is_similar_to( miller_array.unit_cell()) restored_miller_arrays = mtz_object.as_miller_arrays() assert len(restored_miller_arrays) == 1 verify_miller_arrays(miller_array, restored_miller_arrays[0])
def recycle(miller_array, column_root_label, column_types=None, verbose=0): original_dataset = to_mtz(miller_array, column_root_label, column_types) label_decorator = mtz.label_decorator() written = original_dataset.mtz_object() if 0 or verbose: written.show_summary() original_dataset.mtz_object().write(file_name="tmp_iotbx_mtz.mtz") restored = mtz.object(file_name="tmp_iotbx_mtz.mtz") if 0 or verbose: restored.show_summary() assert restored.title() == written.title() assert [line.rstrip() for line in restored.history()] == list(written.history()) assert restored.space_group_name() == written.space_group_name() assert restored.space_group_number() == written.space_group_number() assert restored.space_group() == written.space_group() assert restored.point_group_name() == written.point_group_name() assert restored.lattice_centring_type() == written.lattice_centring_type() assert restored.n_batches() == written.n_batches() assert restored.n_reflections() == written.n_reflections() assert eps_eq(restored.max_min_resolution(), written.max_min_resolution(), eps=1.0e-5) assert restored.n_crystals() == written.n_crystals() assert restored.n_active_crystals() == written.n_active_crystals() assert restored.n_crystals() == 2 for rx, wx in zip(restored.crystals(), written.crystals()): assert rx.name() == wx.name() assert rx.project_name() == wx.project_name() assert rx.unit_cell().is_similar_to(wx.unit_cell()) assert rx.n_datasets() == wx.n_datasets() for rd, wd in zip(rx.datasets(), wx.datasets()): assert rd.name() == wd.name() assert rd.wavelength() == wd.wavelength() assert rd.n_columns() == wd.n_columns() miller_set = restored.crystals()[1].miller_set() assert miller_set.indices().size() == restored.n_reflections() crystal_symmetry = restored.crystals()[1].crystal_symmetry() restored_dataset = restored.crystals()[1].datasets()[0] if not miller_array.anomalous_flag(): if miller_array.sigmas() is None: if miller_array.is_complex_array(): assert restored_dataset.n_columns() == 3 + 2 group = restored.extract_complex( column_label_ampl=column_root_label, column_label_phi=label_decorator.phases(column_root_label) ) elif miller_array.is_hendrickson_lattman_array(): assert restored_dataset.n_columns() == 3 + 4 deco = label_decorator.hendrickson_lattman group = restored.extract_hendrickson_lattman( column_label_a=deco(column_root_label, 0), column_label_b=deco(column_root_label, 1), column_label_c=deco(column_root_label, 2), column_label_d=deco(column_root_label, 3), ) else: assert restored_dataset.n_columns() == 3 + 1 group = restored.extract_reals(column_label=column_root_label) r = miller.array( miller_set=miller.set(crystal_symmetry=crystal_symmetry, indices=group.indices, anomalous_flag=False), data=group.data, ) else: assert restored_dataset.n_columns() == 3 + 2 group = restored.extract_observations( column_label_data=column_root_label, column_label_sigmas=label_decorator.sigmas(column_root_label) ) r = miller.array( miller_set=miller.set(crystal_symmetry=crystal_symmetry, indices=group.indices, anomalous_flag=False), data=group.data, sigmas=group.sigmas, ) else: if miller_array.sigmas() is None: if miller_array.is_complex_array(): assert restored_dataset.n_columns() == 3 + 4 group = restored.extract_complex_anomalous( column_label_ampl_plus=label_decorator.anomalous(column_root_label, "+"), column_label_phi_plus=label_decorator.phases(column_root_label, "+"), column_label_ampl_minus=label_decorator.anomalous(column_root_label, "-"), column_label_phi_minus=label_decorator.phases(column_root_label, "-"), ) elif miller_array.is_hendrickson_lattman_array(): assert restored_dataset.n_columns() == 3 + 8 deco = label_decorator.hendrickson_lattman group = restored.extract_hendrickson_lattman_anomalous( column_label_a_plus=deco(column_root_label, 0, "+"), column_label_b_plus=deco(column_root_label, 1, "+"), column_label_c_plus=deco(column_root_label, 2, "+"), column_label_d_plus=deco(column_root_label, 3, "+"), column_label_a_minus=deco(column_root_label, 0, "-"), column_label_b_minus=deco(column_root_label, 1, "-"), column_label_c_minus=deco(column_root_label, 2, "-"), column_label_d_minus=deco(column_root_label, 3, "-"), ) else: assert restored_dataset.n_columns() == 3 + 2 group = restored.extract_reals_anomalous( column_label_plus=label_decorator.anomalous(column_root_label, "+"), column_label_minus=label_decorator.anomalous(column_root_label, "-"), ) r = miller.array( miller_set=miller.set(crystal_symmetry=crystal_symmetry, indices=group.indices, anomalous_flag=True), data=group.data, ) else: assert restored_dataset.n_columns() == 3 + 4 group = restored.extract_observations_anomalous( column_label_data_plus=label_decorator.anomalous(column_root_label, "+"), column_label_sigmas_plus=label_decorator.sigmas(column_root_label, "+"), column_label_data_minus=label_decorator.anomalous(column_root_label, "-"), column_label_sigmas_minus=label_decorator.sigmas(column_root_label, "-"), ) r = miller.array( miller_set=miller.set(crystal_symmetry=crystal_symmetry, indices=group.indices, anomalous_flag=True), data=group.data, sigmas=group.sigmas, ) verify_miller_arrays(miller_array, r) restored_miller_arrays = restored.as_miller_arrays() assert len(restored_miller_arrays) == 1 thff = restored_miller_arrays[0].info().type_hints_from_file assert thff is not None assert miller_array.is_hendrickson_lattman_array() == (thff == "hendrickson_lattman") verify_miller_arrays(miller_array, restored_miller_arrays[0]) mtz_object = miller_array.as_mtz_dataset(column_root_label=column_root_label).mtz_object() restored_miller_arrays = mtz_object.as_miller_arrays() assert len(restored_miller_arrays) == 1 verify_miller_arrays(miller_array, restored_miller_arrays[0]) if miller_array.is_bool_array() or miller_array.is_integer_array() or miller_array.is_real_array(): cb_op = miller_array.change_of_basis_op_to_niggli_cell() mtz_object.change_basis_in_place(cb_op=cb_op) cb_array = miller_array.change_basis(cb_op=cb_op) assert mtz_object.space_group() == cb_array.space_group() for mtz_crystal in mtz_object.crystals(): assert mtz_crystal.unit_cell().is_similar_to(cb_array.unit_cell()) restored_miller_arrays = mtz_object.as_miller_arrays() assert len(restored_miller_arrays) == 1 verify_miller_arrays(cb_array, restored_miller_arrays[0]) mtz_object.change_basis_in_place(cb_op=cb_op.inverse()) assert mtz_object.space_group() == miller_array.space_group() for mtz_crystal in mtz_object.crystals(): assert mtz_crystal.unit_cell().is_similar_to(miller_array.unit_cell()) restored_miller_arrays = mtz_object.as_miller_arrays() assert len(restored_miller_arrays) == 1 verify_miller_arrays(miller_array, restored_miller_arrays[0])
def exercise_map_tools () : prefix = "tmp_iotbx_map_tools" pdb_file = prefix + ".pdb" mtz_file = prefix + ".mtz" pdb_in = iotbx.pdb.hierarchy.input(pdb_string="""\ ATOM 1 N GLY P -1 -22.866 -2.627 15.217 1.00 0.00 N ATOM 2 CA GLY P -1 -22.714 -3.068 16.621 1.00 0.00 C ATOM 3 C GLY P -1 -21.276 -3.457 16.936 1.00 0.00 C ATOM 4 O GLY P -1 -20.538 -3.887 16.047 1.00 0.00 O ATOM 5 H1 GLY P -1 -22.583 -3.364 14.590 1.00 0.00 H ATOM 6 H2 GLY P -1 -22.293 -1.817 15.040 1.00 0.00 H ATOM 7 H3 GLY P -1 -23.828 -2.392 15.027 1.00 0.00 H ATOM 8 HA2 GLY P -1 -23.016 -2.261 17.288 1.00 0.00 H ATOM 9 HA3 GLY P -1 -23.352 -3.933 16.803 1.00 0.00 H """) xrs = pdb_in.input.xray_structure_simple() open(pdb_file, "w").write(pdb_in.hierarchy.as_pdb_string(xrs)) fc = xrs.structure_factors(d_min=1.5).f_calc() dec = mtz.label_decorator(phases_prefix="PH") # part 1: phenix.refine style mtz_data = fc.as_mtz_dataset( column_root_label="2FOFCWT", label_decorator=dec) mtz_data.add_miller_array(fc, column_root_label="2FOFCWT_no_fill", label_decorator=dec) mtz_data.add_miller_array(fc, column_root_label="FOFCWT", label_decorator=dec) mtz_data.add_miller_array(fc, column_root_label="ANOM", label_decorator=dec) mtz_data.mtz_object().write(mtz_file) converted = map_tools.auto_convert_map_coefficients( mtz_file=mtz_file, pdb_file=pdb_file) assert (not None in [converted.f_map,converted.diff_map,converted.anom_map]) assert (converted.f_map == "tmp_iotbx_map_tools_2mFo-DFc.ccp4") assert (converted.f_map_type == "2mFo-DFc") server = map_tools.server(mtz_file) files = server.convert_phenix_maps(file_base=prefix) files = [ os.path.basename(file_name) for file_name in files ] assert (files == ['tmp_iotbx_map_tools_anomalous.ccp4', 'tmp_iotbx_map_tools_mFo-DFc.ccp4', 'tmp_iotbx_map_tools_2mFo-DFc.ccp4', 'tmp_iotbx_map_tools_2mFo-DFc_no_fill.ccp4']) for fn in files : assert os.path.isfile(fn) os.remove(fn) file_name = server.convert_any_map( f_label="2FOFCWT,PH2FOFCWT", phi_label=None, fom_label=None, use_standard_map_names=True) assert (file_name == "tmp_iotbx_map_tools_2mFo-DFc.ccp4") assert os.path.isfile(file_name) # part 2: Phaser/Refmac style mtz_data = fc.as_mtz_dataset( column_root_label="FWT", label_decorator=mtz.ccp4_label_decorator()) mtz_data.add_miller_array(fc, column_root_label="DELFWT", label_decorator=mtz.ccp4_label_decorator()) mtz_data.mtz_object().write(mtz_file) converted = map_tools.auto_convert_map_coefficients( mtz_file=mtz_file, pdb_file=pdb_file) assert (not None in [converted.f_map, converted.diff_map]) assert (converted.f_map == 'tmp_iotbx_map_tools_FWT.ccp4') assert (converted.f_map_type == "2mFo-DFc") server = map_tools.server(mtz_file) map_files = server.convert_ccp4_map(pdb_file=pdb_file) map_files = [ os.path.basename(file_name) for file_name in map_files ] assert (map_files == ['tmp_iotbx_map_tools_FWT.ccp4', 'tmp_iotbx_map_tools_DELFWT.ccp4']) # part 3: Resolve style mtz_data = fc.as_mtz_dataset( column_root_label="FWT", label_decorator=mtz.ccp4_label_decorator()) ampl = abs(fc) phases = fc.phases() fom = fc.customized_copy( data=flex.double(fc.data().size(), 1), observation_type=None) mtz_data.add_miller_array(ampl, column_root_label="FP") mtz_data.add_miller_array(phases, column_root_label="PHIM") mtz_data.add_miller_array(fom, column_root_label="FOMM") mtz_data.mtz_object().write(mtz_file) server = map_tools.server(mtz_file) map_file = server.convert_resolve_map(pdb_file=None, force=False) assert (map_file == 'tmp_iotbx_map_tools.ccp4') assert os.path.exists(map_file) # write_map_coefficients_generic map_tools.write_map_coefficients_generic( map_coeffs=[fc, fc.generate_bijvoet_mates(), fc, fc], map_types=["2mFo-DFc", "mFo-DFc", "anom", "other"], file_name="tmp_iotbx_map_tools2.mtz") mtz_in = file_reader.any_file("tmp_iotbx_map_tools2.mtz") labels = [a.info().label_string() for a in mtz_in.file_server.miller_arrays] assert (labels == ['2FOFCWT,PH2FOFCWT', 'FOFCWT,PHFOFCWT', 'ANOM,PHANOM', 'other,PHother'])
def exercise_reflections () : hkl_handler = reflections.reflections_handler(allowed_param_names=phil_names) from cctbx import miller from cctbx import crystal from cctbx.array_family import flex symm = crystal.symmetry( unit_cell=(30,30,40,90,90,120), space_group_symbol="P 61 2 2") miller_set = miller.build_set( crystal_symmetry=symm, anomalous_flag=True, d_min=1.5) n_refl = miller_set.indices().size() data = flex.random_double(n_refl) sigmas = flex.random_double(n_refl) f_obs = miller_set.array(data=data, sigmas=sigmas) f_obs_merged = f_obs.average_bijvoet_mates() flags = f_obs_merged.generate_r_free_flags() # single dataset mtz_dataset = f_obs_merged.as_mtz_dataset( column_root_label="F-obs", wavelength=1.54) mtz_dataset.add_miller_array(flags, column_root_label="R-free-flags") file_name = "tst_iotbs_gui_tools.mtz" mtz_dataset.mtz_object().write(file_name) assert (hkl_handler.set_param_file(file_name=file_name, file_param_name="refinement.input.xray_data.file_name") == True) assert (hkl_handler.set_param_file(file_name=file_name, file_param_name="refinement.input.xray_data.r_free_flags.file_name") == False) assert (hkl_handler.get_rfree_labels( file_param_name="refinement.input.xray_data.r_free_flags.file_name") == hkl_handler.get_rfree_labels(file_name=file_name) == ['R-free-flags']) assert (hkl_handler.get_rfree_labels(file_name=file_name, neutron=False) == hkl_handler.get_rfree_labels(file_name=file_name, neutron=True) == ['R-free-flags']) assert approx_equal(1.54, hkl_handler.get_wavelength(file_name=file_name, labels="F-obs,SIGF-obs")) # join X/N datasets hkl_handler = reflections.reflections_handler(allowed_param_names=phil_names) data_neutron = flex.random_double(n_refl) sigmas_neutron = flex.random_double(n_refl) f_obs_neutron = miller_set.array(data=data_neutron, sigmas=sigmas_neutron) mtz_dataset = f_obs_merged.as_mtz_dataset( column_root_label="F-obs-xray") mtz_dataset.add_miller_array(f_obs_neutron, column_root_label="F-obs-neutron") mtz_dataset.add_miller_array(flags, column_root_label="R-free-flags-xray") mtz_dataset.add_miller_array(flags.deep_copy(), column_root_label="R-free-flags-neutron") file_name = "tst_iotbs_gui_tools.mtz" mtz_dataset.mtz_object().write(file_name) assert (hkl_handler.set_param_file(file_name=file_name, file_param_name="refinement.input.xray_data.file_name") == True) for i, phil_name in enumerate(phil_names[1:4]) : assert (hkl_handler.set_param_file(file_name=file_name, file_param_name=phil_name) == False) assert (hkl_handler.get_rfree_labels( file_param_name="refinement.input.xray_data.r_free_flags.file_name") == hkl_handler.get_rfree_labels(file_name=file_name) == ['R-free-flags-xray', 'R-free-flags-neutron']) assert (hkl_handler.get_rfree_labels(file_name=file_name, neutron=False) == ['R-free-flags-xray']) assert (hkl_handler.get_rfree_labels(file_name=file_name, neutron=True) == ['R-free-flags-neutron']) hkl_handler.check_symmetry(file_name=file_name) assert (hkl_handler.get_data_labels( file_param_name="refinement.input.xray_data.file_name") == hkl_handler.get_data_labels(file_name=file_name) == hkl_handler.get_amplitude_labels(file_name=file_name) == ["F-obs-xray,SIGF-obs-xray", "F-obs-neutron(+),SIGF-obs-neutron(+),"+ "F-obs-neutron(-),SIGF-obs-neutron(-)"]) assert (hkl_handler.get_anomalous_data_labels( file_param_name="refinement.input.xray_data.file_name") == ["F-obs-neutron(+),SIGF-obs-neutron(+)," + "F-obs-neutron(-),SIGF-obs-neutron(-)"]) assert (hkl_handler.has_anomalous_data( file_param_name="refinement.input.xray_data.file_name")) assert (hkl_handler.get_rfree_flag_value( array_name="F-obs-xray,SIGF-obs-xray", file_param_name="refinement.input.xray_data.file_name") is None) assert (hkl_handler.get_rfree_flag_value(array_name='R-free-flags-xray', file_param_name="refinement.input.xray_data.r_free_flags.file_name") == 1) (d_max, d_min) = hkl_handler.d_max_min() assert approx_equal(d_max, 25.98, eps=0.01) assert approx_equal(d_min, 1.5, eps=0.01) assert hkl_handler.space_group_as_str() == "P 61 2 2" assert (hkl_handler.unit_cell_as_str() == "30 30 40 90 90 120") assert (hkl_handler.unit_cell_as_str(separator=",") == "30,30,40,90,90,120") n_refl_merged = len(f_obs_merged.indices()) phi_array = f_obs_merged.random_phases_compatible_with_phase_restrictions( deg=True) fom_array = phi_array.array(data=flex.random_double(n_refl_merged)) hl_data = flex.hendrickson_lattman(n_refl_merged, (0,0,0,0)) hl_coeffs = phi_array.array(data=hl_data) assert (hl_coeffs.is_hendrickson_lattman_array()) from iotbx.mtz import label_decorator import iotbx.mtz class resolve_label_decorator (label_decorator) : def phases (self, *args, **kwds) : return label_decorator.phases(self, *args, **kwds) + "M" def hendrickson_lattman (self, *args, **kwds) : return label_decorator.hendrickson_lattman(self, *args, **kwds) + "M" mtz_dataset = f_obs_merged.as_mtz_dataset( column_root_label="FP") mtz_dataset.add_miller_array(phi_array, column_root_label="PHIM", label_decorator=resolve_label_decorator(), column_types="P") mtz_dataset.add_miller_array(fom_array, column_root_label="FOMM", column_types="W") mtz_dataset.add_miller_array(hl_coeffs, column_root_label="HL", label_decorator=resolve_label_decorator()) fwt_map = f_obs_merged.customized_copy(sigmas=None).phase_transfer(phi_array) mtz_dataset.add_miller_array(fwt_map, column_root_label="FWT", label_decorator=iotbx.mtz.ccp4_label_decorator()) mtz_dataset.add_miller_array(flags, column_root_label="FreeR_flag") resolve_file = "tst_iotbx_gui_tools_resolve.mtz" mtz_dataset.mtz_object().write(resolve_file) hkl_handler = reflections.reflections_handler( allowed_param_names=["map_coeffs"]) hkl_handler.set_param_file( file_name=resolve_file, file_param_name="map_coeffs") assert hkl_handler.has_data(file_name=resolve_file) l1 = hkl_handler.get_map_coeff_labels(file_name=resolve_file) assert (l1 == ['FWT,PHWT', 'FP,PHIM,FOMM']) l2 = hkl_handler.get_phase_deg_labels(file_name=resolve_file) assert (l2 == ['HLAM,HLBM,HLCM,HLDM', 'FWT,PHWT', 'PHIM',]), l2 l3 = hkl_handler.get_experimental_phase_labels(file_name=resolve_file) #print l3 l4 = hkl_handler.get_data_labels_for_wizard(file_name=resolve_file) assert (l4 == ['FP SIGFP']) l5 = hkl_handler.get_map_coeff_labels_for_build(file_name=resolve_file) assert (l5 == ['FWT,PHWT', 'FP,PHIM,FOMM']), l5 hkl_in = hkl_handler.get_file(file_name=resolve_file) assert (reflections.get_mtz_label_prefix(hkl_in) == "/crystal/dataset") map_coeffs = reflections.map_coeffs_from_mtz_file(resolve_file, f_label="FP,SIGFP") assert map_coeffs.is_complex_array() try : map_coeffs = reflections.map_coeffs_from_mtz_file(resolve_file) except Sorry : pass else : raise Exception_expected assert (hkl_handler.get_map_coeff_labels_for_fft(file_name=resolve_file) == ['FWT,PHWT', 'FP,SIGFP PHIM FOMM']) # miscellaneous utilities file_name = resolve_file hkl_in = file_reader.any_file(file_name) hkl_server = hkl_in.file_server assert approx_equal(reflections.get_high_resolution(hkl_server), 1.5, eps=0.0001) descriptions = [] for miller_array in hkl_server.miller_arrays : (sg, uc) = reflections.get_miller_array_symmetry(miller_array) assert (uc == "30 30 40 90 90 120") assert (str(sg) == "P 61 2 2") descriptions.append(reflections.get_array_description(miller_array)) assert (descriptions == [ 'Amplitude', 'Phases', 'Weights', 'HL coeffs', 'Map coeffs', 'R-free flag']), descriptions handler = reflections.reflections_handler() handler.save_file(input_file=hkl_in) assert (not handler.has_anomalous_data()) assert (handler.get_resolution_range(file_name=file_name)=="(25.981 - 1.500)") assert (handler.get_resolution_limits(file_name=file_name) == ('(25.981)', '(1.500)')) fmodel = phi_array.array(data=flex.complex_double(n_refl_merged, complex(0.5,0.8))) m1 = phi_array.array(data=flex.complex_double(n_refl_merged, complex(1,0))) m2 = phi_array.array(data=flex.complex_double(n_refl_merged, complex(0.5,0))) m3 = phi_array.array(flex.complex_double(n_refl_merged, complex(1,1))) dec = label_decorator(phases_prefix="PH") mtz_dataset = fmodel.as_mtz_dataset( column_root_label="F-model", label_decorator=dec) mtz_dataset.add_miller_array(m1, column_root_label="2FOFCWT", label_decorator=dec) mtz_dataset.add_miller_array(m2, column_root_label="FOFCWT", label_decorator=dec) mtz_dataset.add_miller_array(m3, column_root_label="2FOFCWT_no_fill", label_decorator=dec) file_name = "tst_iotbx_gui_tools_map_coeffs.mtz" mtz_dataset.mtz_object().write(file_name) hkl_handler = reflections.reflections_handler( allowed_param_names=["fmodel", "map_coeffs"]) hkl_handler.set_param_file( file_name=file_name, file_param_name="fmodel") assert (hkl_handler.get_fmodel_labels(file_name=file_name) == ['F-model,PHF-model']) assert (hkl_handler.get_amplitude_labels(file_name=file_name) == []) phi_labels = hkl_handler.get_phase_deg_labels(file_name=file_name) assert (len(phi_labels) == 4) phi_cols = hkl_handler.get_phase_column_labels(file_name=file_name) assert (phi_cols == ['PHF-model','PH2FOFCWT','PHFOFCWT','PH2FOFCWT_no_fill']) assert (len(hkl_handler.get_amplitude_column_labels(file_name=file_name, allow_conversion=True)) == 0) fc_cols = hkl_handler.get_fmodel_labels(file_name=file_name, first_column_only=True) assert (fc_cols == ['F-model']) hkl_server = file_reader.any_file(file_name).file_server map_labels = reflections.get_map_coeff_labels(hkl_server) assert (map_labels == ['2FOFCWT,PH2FOFCWT', 'FOFCWT,PHFOFCWT', '2FOFCWT_no_fill,PH2FOFCWT_no_fill',]) map_labels = reflections.get_map_coeffs_for_build(hkl_server) assert map_labels == ['2FOFCWT,PH2FOFCWT','2FOFCWT_no_fill,PH2FOFCWT_no_fill'] map_coeffs = reflections.extract_phenix_refine_map_coeffs(file_name) assert (len(map_coeffs) == 3) hkl_file = file_reader.any_file(file_name) assert reflections.get_mtz_label_prefix(hkl_file) == "/crystal/dataset" # other stuff (fp, fpp) = reflections.get_fp_fpp_from_sasaki("Se", 0.979) assert fp is not None and fpp is not None