def test_mtz_best_unit_cell(dials_data, tmp_path): scaled_expt = ( dials_data("x4wide_processed", pathlib=True) / "AUTOMATIC_DEFAULT_scaled.expt" ) scaled_refl = ( dials_data("x4wide_processed", pathlib=True) / "AUTOMATIC_DEFAULT_scaled.refl" ) best_unit_cell = uctbx.unit_cell((42, 42, 39, 90, 90, 90)) d_min = 1.5 result = procrunner.run( [ "dials.export", "format=mtz", scaled_expt, scaled_refl, f"d_min={d_min:f}", "best_unit_cell=%g,%g,%g,%g,%g,%g" % best_unit_cell.parameters(), ], working_directory=tmp_path, ) assert not result.returncode and not result.stderr assert (tmp_path / "scaled.mtz").is_file() # The resulting mtz should have the best_unit_cell as input to dials.export for ma in mtz.object(str(tmp_path / "scaled.mtz")).as_miller_arrays(): assert best_unit_cell.parameters() == pytest.approx(ma.unit_cell().parameters()) assert ma.d_min() >= d_min
def _extract_data_from_mtz(self): try: m = mtz.object(self.params.hklin) except RuntimeError: raise Sorry("Could not read {0}".format(self.params.hklin)) mad = m.as_miller_arrays_dict(merge_equivalents=False) mad = {k[-1]: v for (k, v) in mad.items()} iobs = mad.get(self.params.Io) original_indices = m.extract_original_index_miller_indices() iobs = iobs.customized_copy(indices=original_indices) if iobs is None: raise Sorry( "Intensity column {0} not found in available labels: {1}".format( self.params.Io, ", ".join(m.column_labels()), ) ) if not iobs.is_unmerged_intensity_array(): raise Sorry("Please check that the file contains unmerged intensities") return iobs
def add_dose_time_to_mtz(hklin, hklout, doses, times=None): """Add doses and times from dictionaries doses, times (optional) to hklin to produce hklout. The dictionaries are indexed by the BATCH column in hklin. Will raise exception if no BATCH column.""" # instantiate the MTZ object representation from iotbx import mtz from cctbx.array_family import flex mtz_obj = mtz.object(file_name=hklin) batch_column = None batch_dataset = None for crystal in mtz_obj.crystals(): for dataset in crystal.datasets(): for column in dataset.columns(): if column.label() == "BATCH": batch_column = column batch_dataset = dataset if not batch_column: raise RuntimeError("no BATCH column found in %s" % hklin) # right, so get the values out from the batch column, create a flex # array of the same size and assign DOSE, TIME, then add these to the # same dataset. batch_column_values = batch_column.extract_values( not_a_number_substitute=-1) dose_column = batch_dataset.add_column(label="DOSE", type="R") dose_column_values = flex.float() if times: time_column = batch_dataset.add_column(label="TIME", type="R") time_column_values = flex.float() valid = flex.bool() for b in batch_column_values: valid.append(True) dose_column_values.append(doses.get(b, -1.0)) if times: time_column_values.append(times.get(b, -1.0)) # add the columns back to the MTZ file structure dose_column.set_values(values=dose_column_values, selection_valid=valid) if times: time_column.set_values(values=time_column_values, selection_valid=valid) # and write this lot out as hklout mtz_obj.write(file_name=hklout)
def get_mosflm_coordinate_frame(integrate_mtz): from iotbx import mtz from scitbx import matrix m = mtz.object(integrate_mtz) b = m.batches()[0] return matrix.col(b.source()), matrix.col(b.e1())
def get_res(data_file): """Return resolution limit of dataset""" data_file = convert_unicode(data_file) data = iotbx_mtz.object(data_file) return float(data.max_min_resolution()[-1])
def SetMtzFile(self, file_name): from iotbx import mtz try: self._mtz_obj = mtz.object(file_name=file_name) except RuntimeError, e: raise Sorry(("The file '%s' could not be read as an MTZ file " + "(original error: %s)") % (file_name, str(e)))
def SetMtzFile (self, file_name) : from iotbx import mtz try : self._mtz_obj = mtz.object(file_name=file_name) except RuntimeError, e : raise Sorry(("The file '%s' could not be read as an MTZ file "+ "(original error: %s)") % (file_name, str(e)))
def create_model_from_mtz(self, model_file_path): if self.mpi_helper.rank == 0: from iotbx import mtz data_SR = mtz.object(model_file_path) arrays = data_SR.as_miller_arrays() space_group = data_SR.space_group().info() unit_cell = data_SR.crystals()[0].unit_cell() else: arrays = space_group = unit_cell = None arrays, space_group, unit_cell = self.mpi_helper.comm.bcast((arrays, space_group, unit_cell), root=0) # save space group and unit cell as scaling targets if self.purpose == "scaling": self.params.scaling.space_group = space_group self.params.scaling.unit_cell = unit_cell if self.purpose == "scaling": mtz_column_F = str(self.params.scaling.mtz.mtz_column_F.lower()) elif self.purpose == "statistics": mtz_column_F = str(self.params.statistics.cciso.mtz_column_F.lower()) for array in arrays: this_label = array.info().label_string().lower() if True not in ["sig"+tag in this_label for tag in ["iobs","imean", mtz_column_F]]: continue return array.as_intensity_array().change_basis(self.params.scaling.model_reindex_op).map_to_asu() raise Exception("mtz did not contain expected label Iobs or Imean")
def exercise_miller_array_data_types(): miller_set = crystal.symmetry(unit_cell=(10, 10, 10, 90, 90, 90), space_group_symbol="P1").miller_set( indices=flex.miller_index([(1, 2, 3), (4, 5, 6)]), anomalous_flag=False ) for data in [ flex.bool([False, True]), flex.int([0, 1]), flex.size_t([0, 1]), flex.double([0, 1]), flex.complex_double([0, 1]), ]: miller_array = miller_set.array(data=data) if op.isfile("tmp_iotbx_mtz.mtz"): os.remove("tmp_iotbx_mtz.mtz") assert not op.isfile("tmp_iotbx_mtz.mtz") miller_array.as_mtz_dataset(column_root_label="DATA").mtz_object().write(file_name="tmp_iotbx_mtz.mtz") assert op.isfile("tmp_iotbx_mtz.mtz") mtz_obj = mtz.object(file_name="tmp_iotbx_mtz.mtz") miller_arrays_read_back = mtz_obj.as_miller_arrays() assert len(miller_arrays_read_back) == 1 miller_array_read_back = miller_arrays_read_back[0] assert miller_array_read_back.indices().all_eq(miller_array.indices()) if miller_array.is_integer_array() or miller_array.is_bool_array(): assert miller_array_read_back.data().all_eq(flex.int([0, 1])) elif miller_array.is_real_array(): assert miller_array_read_back.data().all_eq(flex.double([0, 1])) elif miller_array.is_complex_array(): assert miller_array_read_back.data().all_eq(flex.complex_double([0, 1])) else: raise RuntimeError("Programming error.")
def get_hkl_xyz_isigi(mtz_file): m = mtz.object(mtz_file) r = get_phi_range(m) sg = m.space_group() xdet_col = m.get_column('XDET') ydet_col = m.get_column('YDET') rot_col = m.get_column('ROT') i_col = m.get_column('I') sigi_col = m.get_column('SIGI') mi_s = m.extract_miller_indices() map_to_asu(sg.type(), False, mi_s) xdet_s = xdet_col.extract_values(not_a_number_substitute = 0.0) ydet_s = ydet_col.extract_values(not_a_number_substitute = 0.0) rot_s = rot_col.extract_values(not_a_number_substitute = 0.0) i_s = i_col.extract_values(not_a_number_substitute = 0.0) sigi_s = sigi_col.extract_values(not_a_number_substitute = 0.0) hkl_xyz_isigi = [] # N.B. swapping X, Y here for j in range(mi_s.size()): hkl_xyz_isigi.append(((mi_s[j][0], mi_s[j][1], mi_s[j][2]), (ydet_s[j], xdet_s[j], rot_s[j] / r), (i_s[j], sigi_s[j]))) return hkl_xyz_isigi
def run(args): import os if ("--help" in args) or ("-h" in args) or (len(args) == 0): print("""cctbx.riso: a command line script for calculating an R1 factor between two datasets. Example usage: cctbx.riso data_1=5kaf-sf.mtz data_2=5kai-sf.mtz \\ labels_1=Iobs labels_2=Iobs """) return elif ("--config" in args) or ("-c" in args): iotbx.phil.parse(master_phil).show(attributes_level=2) return parser = ArgumentParser(phil=phil_scope) params, options = parser.parse_args(show_diff_phil=True) # XXX TODO: find out how to auto recognize .mtz files as data_X extracted_data = [] for data, label in [(params.input.data_1, params.input.labels_1), (params.input.data_2, params.input.labels_2)]: assert data is not None, "Please supply two mtz files." assert os.path.exists(data), "Cannot access data: %s"%data extracted = mtz.object(data) assert extracted.has_column(label), "%s does not contain column %s"%\ (data, label) extracted_data.append(extracted) riso(extracted_data[0], extracted_data[1], params, show_tables=True)
def run(): for f in os.listdir("DataFiles"): if f.endswith(".mtz"): print(f) for c in mtz.object(os.path.join("DataFiles", f)).crystals(): print("%20s: %7.3f %7.3f %7.3f %7.3f %7.3f %7.3f" % tuple([c.name()] + list(c.unit_cell_parameters())))
def get_mtz_info(data_file): """ Get unit cell and SG from input mtz """ sg = False cell = False vol = False # Convert from unicode data_file = convert_unicode(data_file) # Read data_file data = iotbx_mtz.object(data_file) # Derive space group from data_file sg = fix_R3_sg(data.space_group_name().replace(" ", "")) # Wrangle the cell parameters cell = [round(x, 3) for x in data.crystals()[0].unit_cell_parameters()] # The volume vol = data.crystals()[0].unit_cell().volume() return (sg, cell, vol)
def exercise_miller_array_data_types(): miller_set = crystal.symmetry(unit_cell=(10, 10, 10, 90, 90, 90), space_group_symbol="P1").miller_set( indices=flex.miller_index([(1, 2, 3), (4, 5, 6)]), anomalous_flag=False) for data in [ flex.bool([False, True]), flex.int([0, 1]), flex.size_t([0, 1]), flex.double([0, 1]), flex.complex_double([0, 1]) ]: miller_array = miller_set.array(data=data) if (op.isfile("tmp_iotbx_mtz.mtz")): os.remove("tmp_iotbx_mtz.mtz") assert not op.isfile("tmp_iotbx_mtz.mtz") miller_array.as_mtz_dataset( column_root_label="DATA").mtz_object().write( file_name="tmp_iotbx_mtz.mtz") assert op.isfile("tmp_iotbx_mtz.mtz") mtz_obj = mtz.object(file_name="tmp_iotbx_mtz.mtz") miller_arrays_read_back = mtz_obj.as_miller_arrays() assert len(miller_arrays_read_back) == 1 miller_array_read_back = miller_arrays_read_back[0] assert miller_array_read_back.indices().all_eq(miller_array.indices()) if (miller_array.is_integer_array() or miller_array.is_bool_array()): assert miller_array_read_back.data().all_eq(flex.int([0, 1])) elif (miller_array.is_real_array()): assert miller_array_read_back.data().all_eq(flex.double([0, 1])) elif (miller_array.is_complex_array()): assert miller_array_read_back.data().all_eq( flex.complex_double([0, 1])) else: raise RuntimeError("Programming error.")
def anomalous_signals(hklin): """ Compute some measures of anomalous signal: df / f and di / sig(di). """ m = mtz.object(hklin) mas = m.as_miller_arrays() data = None for ma in mas: if not ma.anomalous_flag(): continue if str(ma.observation_type()) != "xray.intensity": continue data = ma if not data: Debug.write("no anomalous data found") return df_f = data.anomalous_signal() differences = data.anomalous_differences() di_sigdi = sum(abs(differences.data())) / sum(differences.sigmas()) return df_f, di_sigdi
def frame_factory(hklin): '''Create a Frame object from an MTZ file corresponding to one INTEGRATE run in XDS.''' from iotbx import mtz mtz_obj = mtz.object(hklin) mi = mtz_obj.extract_miller_indices() dmax, dmin = mtz_obj.max_min_resolution() sg = mtz_obj.space_group() uc = None # now have a rummage through to get the columns out that I want i_column = None sigi_column = None for crystal in mtz_obj.crystals(): uc = crystal.unit_cell() for dataset in crystal.datasets(): for column in dataset.columns(): if column.label() == 'I': i_column = column elif column.label() == 'SIGI': sigi_column = column return Frame(uc, mi, i_column.extract_values( not_a_number_substitute = 0.0), sigi_column.extract_values( not_a_number_substitute = 0.0))
def test_mtz_best_unit_cell(dials_data, tmpdir): scaled_expt = dials_data("x4wide_processed").join( "AUTOMATIC_DEFAULT_scaled.expt") scaled_refl = dials_data("x4wide_processed").join( "AUTOMATIC_DEFAULT_scaled.refl") best_unit_cell = uctbx.unit_cell((42, 42, 39, 90, 90, 90)) d_min = 1.5 result = procrunner.run( [ "dials.export", "format=mtz", scaled_expt, scaled_refl, "d_min=%f" % d_min, "best_unit_cell=%g,%g,%g,%g,%g,%g" % best_unit_cell.parameters(), ], working_directory=tmpdir, ) assert not result.returncode and not result.stderr assert tmpdir.join("scaled.mtz").check(file=1) # The resulting mtz should have the best_unit_cell as input to dials.export for ma in mtz.object(tmpdir.join("scaled.mtz").strpath).as_miller_arrays(): assert best_unit_cell.parameters() == pytest.approx( ma.unit_cell().parameters()) assert ma.d_min() >= d_min
def erzatz_resolution(reflection_file, batch_ranges): mtz_obj = mtz.object(reflection_file) miller = mtz_obj.extract_miller_indices() dmax, dmin = mtz_obj.max_min_resolution() ipr_column = None sigipr_column = None batch_column = None uc = None for crystal in mtz_obj.crystals(): if crystal.name() == 'HKL_Base': continue uc = crystal.unit_cell() for dataset in crystal.datasets(): for column in dataset.columns(): if column.label() == 'IPR': ipr_column = column elif column.label() == 'SIGIPR': sigipr_column = column elif column.label() == 'BATCH': batch_column = column assert(ipr_column) assert(sigipr_column) assert(batch_column) ipr_values = ipr_column.extract_values() sigipr_values = sigipr_column.extract_values() batch_values = batch_column.extract_values() batches = [nint(b) for b in batch_values] resolutions = { } for start, end in batch_ranges: d = [] isig = [] for j in range(miller.size()): if batches[j] < start: continue if batches[j] > end: continue d.append(uc.d(miller[j])) isig.append(ipr_values[j] / sigipr_values[j]) resolutions[(start, end)] = compute_resolution(dmax, dmin, d, isig) return resolutions
def SetMtzFile(self, file_name): from iotbx import mtz try: self._mtz_obj = mtz.object(file_name=file_name) except RuntimeError as e: raise Sorry(("The file '%s' could not be read as an MTZ file " + "(original error: %s)") % (file_name, str(e))) self.title_txt.SetLabel(self._mtz_obj.title()) self.sg_txt.SetLabel(str(self._mtz_obj.space_group_name())) self.d_max_min_txt.SetLabel("%g - %g Angstrom" % self._mtz_obj.max_min_resolution()) self._dataset_labels = [] self._crystals_and_datasets = [] for i_crystal, crystal in enumerate(self._mtz_obj.crystals()): if (crystal.name() == "HKL_base"): continue for i_dataset, dataset in enumerate(crystal.datasets()): label = "/%s/%s" % (crystal.name(), dataset.name()) self._crystals_and_datasets.append((crystal, dataset)) self._dataset_labels.append(label) self.dataset_chooser.SetItems(self._dataset_labels) p = MtzDatasetPanel(self, style=wx.RAISED_BORDER) self._dataset_panels.append(p) self.sizer.Add(p, 1, wx.ALL | wx.EXPAND, 0) if (len(self._dataset_labels) > 0): self.dataset_chooser.SetSelection(0) self.OnChooseDataset(None)
def anomalous_signals(hklin): ''' Compute some measures of anomalous signal: df / f and di / sig(di). ''' m = mtz.object(hklin) mas = m.as_miller_arrays() data = None for ma in mas: if not ma.anomalous_flag(): continue if str(ma.observation_type()) != 'xray.intensity': continue data = ma if not data: raise RuntimeError, 'no anomalous data found' df_f = data.anomalous_signal() differences = data.anomalous_differences() di_sigdi = (sum(abs(differences.data())) / sum(differences.sigmas())) return df_f, di_sigdi
def get_res(datafile): """Return resolution limit of dataset""" datafile = convert_unicode(datafile) data = iotbx_mtz.object(datafile) return float(data.max_min_resolution()[-1])
def test_merge_dmin_dmax(dials_data, tmpdir): """Test the d_min, d_max""" location = dials_data("l_cysteine_4_sweeps_scaled") refls = location.join("scaled_20_25.refl") expts = location.join("scaled_20_25.expt") mtz_file = tmpdir.join("merge.mtz") command = [ "dials.merge", refls, expts, "truncate=False", "anomalous=False", "d_min=1.0", "d_max=8.0", "output.mtz=%s" % mtz_file.strpath, "project_name=ham", "crystal_name=jam", "dataset_name=spam", ] result = procrunner.run(command, working_directory=tmpdir) assert not result.returncode and not result.stderr # check we only have reflections in range 8 - 1A m = mtz.object(mtz_file.strpath) max_min_resolution = m.max_min_resolution() assert max_min_resolution[0] <= 8 assert max_min_resolution[1] >= 1
def test_mtz_recalculated_cell(dials_data, tmpdir): # First run dials.two_theta_refine to ensure that the crystals have # recalculated_unit_cell set scaled_expt = dials_data("x4wide_processed").join("AUTOMATIC_DEFAULT_scaled.expt") scaled_refl = dials_data("x4wide_processed").join("AUTOMATIC_DEFAULT_scaled.refl") result = procrunner.run( ["dials.two_theta_refine", scaled_expt, scaled_refl], working_directory=tmpdir, ) assert tmpdir.join("refined_cell.expt").check(file=1) refined_expt = load.experiment_list( tmpdir.join("refined_cell.expt").strpath, check_format=False ) ttr_cell = refined_expt.crystals()[0].get_recalculated_unit_cell() d_min = 1.3 result = procrunner.run( [ "dials.export", "format=mtz", tmpdir.join("refined_cell.expt"), scaled_refl, "d_min=%f" % d_min, ], working_directory=tmpdir, ) assert not result.returncode and not result.stderr assert tmpdir.join("scaled.mtz").check(file=1) # The resulting mtz should have the same unit cell set as the recalculated_unit_cell # from dials.two_theta_refine for ma in mtz.object(tmpdir.join("scaled.mtz").strpath).as_miller_arrays(): assert ttr_cell.parameters() == pytest.approx(ma.unit_cell().parameters()) assert ma.d_min() >= d_min
def anomalous_signals(hklin): ''' Compute some measures of anomalous signal: df / f and di / sig(di). ''' m = mtz.object(hklin) mas = m.as_miller_arrays() data = None for ma in mas: if not ma.anomalous_flag(): continue if str(ma.observation_type()) != 'xray.intensity': continue data = ma if not data: raise RuntimeError('no anomalous data found') df_f = data.anomalous_signal() differences = data.anomalous_differences() di_sigdi = (sum(abs(differences.data())) / sum(differences.sigmas())) return df_f, di_sigdi
def get_hkl_xyz_isigi(mtz_file): m = mtz.object(mtz_file) r = get_phi_range(m) sg = m.space_group() xdet_col = m.get_column('XDET') ydet_col = m.get_column('YDET') rot_col = m.get_column('ROT') i_col = m.get_column('I') sigi_col = m.get_column('SIGI') mi_s = m.extract_miller_indices() map_to_asu(sg.type(), False, mi_s) xdet_s = xdet_col.extract_values(not_a_number_substitute=0.0) ydet_s = ydet_col.extract_values(not_a_number_substitute=0.0) rot_s = rot_col.extract_values(not_a_number_substitute=0.0) i_s = i_col.extract_values(not_a_number_substitute=0.0) sigi_s = sigi_col.extract_values(not_a_number_substitute=0.0) hkl_xyz_isigi = [] # N.B. swapping X, Y here for j in range(mi_s.size()): hkl_xyz_isigi.append( ((mi_s[j][0], mi_s[j][1], mi_s[j][2]), (ydet_s[j], xdet_s[j], rot_s[j] / r), (i_s[j], sigi_s[j]))) return hkl_xyz_isigi
def test_mtz_primitive_cell(dials_data, tmpdir): scaled_expt = dials_data("insulin_processed") / "scaled.expt" scaled_refl = dials_data("insulin_processed") / "scaled.refl" # First reindex to the primitive setting expts = ExperimentList.from_file(scaled_expt.strpath, check_format=False) cs = expts[0].crystal.get_crystal_symmetry() cb_op = cs.change_of_basis_op_to_primitive_setting() procrunner.run( [ "dials.reindex", scaled_expt.strpath, scaled_refl.strpath, 'change_of_basis_op="%s"' % cb_op, ], working_directory=tmpdir.strpath, ) # Now export the reindexed experiments/reflections procrunner.run( ["dials.export", "reindexed.expt", "reindexed.refl"], working_directory=tmpdir.strpath, ) mtz_obj = mtz.object(os.path.join(tmpdir.strpath, "scaled.mtz")) cs_primitive = cs.change_basis(cb_op) assert mtz_obj.space_group() == cs_primitive.space_group() refl = flex.reflection_table.from_file(scaled_refl.strpath) refl = refl.select(~refl.get_flags(refl.flags.bad_for_scaling, all=False)) for ma in mtz_obj.as_miller_arrays(): assert ma.crystal_symmetry().is_similar_symmetry(cs_primitive) assert ma.d_max_min() == pytest.approx( (flex.max(refl["d"]), flex.min(refl["d"])) )
def run(sf_file_name,pdb_file_name): # check if files exist if not isfile(sf_file_name): raise Sorry('{} is not a file'.format(sf_file_name)) if not isfile(pdb_file_name): raise Sorry('{} is not a file'.format(pdb_file_name)) # start processing file # convert cif to mtz and process mtz file file_name_mtz = sf_file_name + '.mtz' # creates file in current folder easy_run.call("phenix.cif_as_mtz %s"%sf_file_name) mtz_object = mtz.object(file_name=file_name_mtz) # Process the mtz_object miller_arrays_dict = mtz_object.as_miller_arrays_dict() miller_arrays = mtz_object.as_miller_arrays() for x in miller_arrays_dict: print x print '+'*60 #print miller_arrays_dict[x].show_array() print '='*60 # read pdb info pdb_inp = pdb.input(file_name=pdb_file_name) # read the pdb file data structure = pdb_inp.xray_structure_simple() xray_structure_simple = pdb_inp.xray_structures_simple() # a list of structure factors f_miller = xray_structure_simple[0].structure_factors(d_min=2.85).f_calc() # delete file os.remove(file_name_mtz) # print 'wait here'
def _scale_finish_chunk_7_twinning(self): hklout = self._scalr_scaled_reflection_files['mtz'] m = mtz.object(hklout) # FIXME in here should be able to just drop down to the lowest symmetry # space group with the rotational elements for this calculation? I.e. # P422 for P4/mmm? if not m.space_group().is_centric(): from xia2.Toolkit.E4 import E4_mtz E4s = E4_mtz(hklout, native=True) self._scalr_twinning_score = E4s.items()[0][1] if self._scalr_twinning_score > 1.9: self._scalr_twinning_conclusion = 'Your data do not appear twinned' elif self._scalr_twinning_score < 1.6: self._scalr_twinning_conclusion = 'Your data appear to be twinned' else: self._scalr_twinning_conclusion = 'Ambiguous score (1.6 < score < 1.9)' else: self._scalr_twinning_conclusion = 'Data are centric' self._scalr_twinning_score = 0 Chatter.write('Overall twinning score: %4.2f' % self._scalr_twinning_score) Chatter.write(self._scalr_twinning_conclusion)
def _extract_data_from_mtz(self): try: m = mtz.object(self.params.hklin) except RuntimeError: raise Sorry("Could not read {0}".format(self.params.hklin)) mad = m.as_miller_arrays_dict() mad = {k[-1]: v for (k, v) in mad.items()} fobs = mad.get(self.params.Fo) fc = mad.get(self.params.Fc) if [fobs, fc].count(None) > 0: raise Sorry( "Columns {0} not found in available labels: {1}".format( ", ".join([self.params.Fo, self.params.Fc]), ", ".join(m.column_labels()), )) # Find common reflections (some fobs might be missing) fobs, fc = fobs.common_sets(fc) self.fobs = fobs.data() self.fc = fc.amplitudes().data() return
def get_mtz_info(datafile): """ Get unit cell and SG from input mtz """ sg = False cell = False vol = False # Convert from unicode datafile = convert_unicode(datafile) # Read datafile data = iotbx_mtz.object(datafile) # Derive space group from datafile sg = fix_R3_sg(data.space_group_name().replace(" ", "")) # Wrangle the cell parameters cell = [round(x,3) for x in data.crystals()[0].unit_cell_parameters() ] # The volume vol = data.crystals()[0].unit_cell().volume() return (sg, cell, vol)
def tst_x2tbx(mtz_file): import x2tbx from iotbx import mtz mtz_obj = mtz.object(mtz_file) i_data = None sigi_data = None mi = mtz_obj.extract_miller_indices() unit_cell = None for crystal in mtz_obj.crystals(): unit_cell = crystal.unit_cell() for dataset in crystal.datasets(): for column in dataset.columns(): if column.label() == "I": i_data = column.extract_values(not_a_number_substitute=0.0) if column.label() == "SIGI": sigi_data = column.extract_values( not_a_number_substitute=0.0) assert i_data assert sigi_data r = x2tbx.ReflectionList() r.setup(mi, i_data, sigi_data) r.set_unit_cell(unit_cell.parameters()) r.merge() print(r.i_sigma()) print(r.rmerge()) indices = r.get_indices() print(len(indices), len(mi)) n_shells = 30 r.setup_resolution_shells(n_shells) high = r.shell_high_limits() low = r.shell_low_limits() rmerges = r.rmerge_shells() isigmas = r.i_sigma_shells() tisigmas = r.total_i_sigma_shells() n_tot = 0 for j in range(n_shells): shell = r.get_shell(j) print( "%.3f %6.3f %4d %.3f %6.2f %6.2f" % (high[j], low[j], len(shell), rmerges[j], isigmas[j], tisigmas[j])) n_tot += len(shell) assert n_tot == len(indices) print("OK")
def tst_x2tbx(mtz_file): import x2tbx from iotbx import mtz mtz_obj = mtz.object(mtz_file) i_data = None sigi_data = None mi = mtz_obj.extract_miller_indices() unit_cell = None for crystal in mtz_obj.crystals(): unit_cell = crystal.unit_cell() for dataset in crystal.datasets(): for column in dataset.columns(): if column.label() == 'I': i_data = column.extract_values( not_a_number_substitute = 0.0) if column.label() == 'SIGI': sigi_data = column.extract_values( not_a_number_substitute = 0.0) assert(i_data) assert(sigi_data) r = x2tbx.ReflectionList() r.setup(mi, i_data, sigi_data) r.set_unit_cell(unit_cell.parameters()) r.merge() print r.i_sigma() print r.rmerge() indices = r.get_indices() print len(indices), len(mi) n_shells = 30 r.setup_resolution_shells(n_shells) high = r.shell_high_limits() low = r.shell_low_limits() rmerges = r.rmerge_shells() isigmas = r.i_sigma_shells() tisigmas = r.total_i_sigma_shells() n_tot = 0 for j in range(n_shells): shell = r.get_shell(j) print '%.3f %6.3f %4d %.3f %6.2f %6.2f' % ( high[j], low[j], len(shell), rmerges[j], isigmas[j], tisigmas[j]) n_tot += len(shell) assert(n_tot == len(indices)) print 'OK'
def pull_reference(integrate_mtz): '''Generate reference data set from integrate.hkl, check out the calculated x, y and z centroids as well as the Miller indices as coordinates in some high dimensional space. Only consider measurements with meaningful centroids...''' # prepare input data as # sortmtz hklin thau_2_001.mtz hklout sorted.mtz << eof # H K L M/ISYM BATCH # eof # # scala hklin sorted.mtz hklout summed.mtz << eof # run 1 all # scales constant # output unmerged # sdcorrection noadjust norefine both 1 0 0 # eof from iotbx import mtz m = mtz.object(integrate_mtz) hkl = m.extract_original_index_miller_indices() b0 = m.batches()[0] for c in m.columns(): if c.label() == 'I': i_lp = c.extract_valid_values() elif c.label() == 'SIGI': sigi = c.extract_valid_values() elif c.label() == 'XDET': xdet = c.extract_valid_values() elif c.label() == 'YDET': ydet = c.extract_valid_values() elif c.label() == 'ROT': rot = c.extract_valid_values() elif c.label() == 'LP': lp = c.extract_valid_values() # extract x, y, z positions for this in image address space xyz = [] dx = b0.detlm()[1] dz = b0.phiend() - b0.phistt() z0 = b0.phistt() for x, y, r in zip(xdet, ydet, rot): _x = y _y = x _z = (r - z0) / dz xyz.append((_x, _y, _z)) i = i_lp / lp print 'Reference: %d observations' % len(hkl) return hkl, i, sigi, xyz
def rebatch(hklin, hklout, first_batch=None, include_range=None, exclude_range=None): """Need to implement: include batch range, exclude batches, add N to batches, start batches at N.""" if include_range is None: include_range = [] if exclude_range is None: exclude_range = [] assert not (len(include_range) and len(exclude_range)) assert not (len(include_range) and first_batch) assert not (len(exclude_range) and first_batch) mtz_obj = mtz.object(file_name=hklin) batch_column = None batch_dataset = None for crystal in mtz_obj.crystals(): for dataset in crystal.datasets(): for column in dataset.columns(): if column.label() == "BATCH": batch_column = column batch_dataset = dataset if not batch_column: raise RuntimeError, "no BATCH column found in %s" % hklin batch_column_values = batch_column.extract_values(not_a_number_substitute=-1) valid = flex.bool() if exclude_range: exclude_sel = flex.bool(batch_column_values.size(), False) for (start, end) in exclude_range: exclude_sel.set_selected((batch_column_values >= start) & (batch_column_values <= end), True) mtz_obj.delete_reflections(exclude_sel.iselection()) elif include_range: exclude_sel = flex.bool(batch_column_values.size(), True) for (start, end) in include_range: exclude_sel.set_selected((batch_column_values >= start) & (batch_column_values <= end), False) mtz_obj.delete_reflections(exclude_sel.iselection()) # modify batch columns, and also the batch headers elif first_batch is not None: offset = first_batch - min(batch_column_values) batch_column_values = batch_column_values + offset for batch in mtz_obj.batches(): batch.set_num(int(batch.num() + offset)) # done modifying batch_column.set_values(values=batch_column_values, selection_valid=valid) # and write this lot out as hklout mtz_obj.write(file_name=hklout)
def integrate_mtz_to_A_matrix(integrate_mtz): m = mtz.object(integrate_mtz) b = m.batches()[0] u = matrix.sqr(b.umat()).transpose() c = unit_cell(tuple(b.cell())) f = matrix.sqr(c.fractionalization_matrix()).transpose() return u * f
def integrate_mtz_to_unit_cell(integrate_mtz): """Generate a cctbx unit_cell from an integrate_mtz file.""" m = mtz.object(integrate_mtz) for c in m.crystals(): return c.unit_cell() raise RuntimeError("unit cell not found")
def ersatz_resolution(reflection_file, batch_ranges): mtz_obj = mtz.object(reflection_file) miller = mtz_obj.extract_miller_indices() dmax, dmin = mtz_obj.max_min_resolution() ipr_column = None sigipr_column = None i_column = None sigi_column = None batch_column = None uc = None for crystal in mtz_obj.crystals(): if crystal.name() == "HKL_Base": continue uc = crystal.unit_cell() for dataset in crystal.datasets(): for column in dataset.columns(): if column.label() == "IPR": ipr_column = column elif column.label() == "SIGIPR": sigipr_column = column elif column.label() == "BATCH": batch_column = column elif column.label() == "I": i_column = column elif column.label() == "SIGI": sigi_column = column assert ipr_column or i_column assert sigipr_column or sigi_column assert batch_column if ipr_column is None: ipr_column = i_column if sigipr_column is None: sigipr_column = sigi_column ipr_values = ipr_column.extract_values() sigipr_values = sigipr_column.extract_values() batch_values = batch_column.extract_values() batches = batch_values.as_double().iround() resolutions = {} for start, end in batch_ranges: sel = (batches >= start) & (batches <= end) d = uc.d(miller.select(sel)) isig = ipr_values.select(sel) / sigipr_values.select(sel) resolutions[(start, end)] = compute_resolution(dmax, dmin, d, isig) return resolutions
def get_umat_bmat_lattice_symmetry_from_mtz(mtz_file): """Get the U matrix and lattice symmetry derived from the unit cell constants from an MTZ file.""" m = mtz.object(mtz_file) # assert first U matrix from batches is OK uc = m.crystals()[0].unit_cell() lattice_symm = lattice_symmetry_group(uc, max_delta=0.0) return tuple(m.batches()[0].umat()), mosflm_B_matrix(uc), lattice_symm
def dump_mtz_orientation(mtz_file): from iotbx import mtz from scitbx import matrix from scitbx.math.euler_angles import xyz_angles m = mtz.object(mtz_file) for b in m.batches(): rxyz = tuple(xyz_angles(matrix.sqr(b.umat()))) print(b.num(), '%7.4f %7.4f %7.4f' % rxyz)
def integrate_mtz_to_unit_cell(integrate_mtz): '''Generate a cctbx unit_cell from an integrate_mtz file.''' from iotbx import mtz m = mtz.object(integrate_mtz) for c in m.crystals(): return c.unit_cell() raise RuntimeError, 'unit cell not found'
def dump(self): '''Actually obtain the contents of the mtz file header.''' assert self._hklin, self._hklin assert os.path.exists(self._hklin), self._hklin mtz_obj = mtz.object(self._hklin) # work through the file acculumating the necessary information self._header['datasets'] = [] self._header['dataset_info'] = { } self._batches = [batch.num() for batch in mtz_obj.batches()] self._header['column_labels'] = [column.label() for column in mtz_obj.columns()] self._header['column_types'] = [column.type() for column in mtz_obj.columns()] self._resolution_range = mtz_obj.max_min_resolution() spacegroup_and_no = mtz_obj.space_group().info().symbol_and_number() spacegroup_number = int(spacegroup_and_no.replace(')', '').split()[-1]) from xia2.Handlers.Syminfo import Syminfo spacegroup = Syminfo.spacegroup_number_to_name(spacegroup_number) self._header['spacegroup'] = spacegroup self._reflections = mtz_obj.n_reflections() for crystal in mtz_obj.crystals(): if crystal.name() == 'HKL_base': continue pname = crystal.project_name() xname = crystal.name() cell = crystal.unit_cell().parameters() for dataset in crystal.datasets(): dname = dataset.name() wavelength = dataset.wavelength() dataset_id = '%s/%s/%s' % (pname, xname, dname) dataset_number = dataset.i_dataset() assert(not dataset_id in self._header['datasets']) self._header['datasets'].append(dataset_id) self._header['dataset_info'][dataset_id] = { } self._header['dataset_info'][ dataset_id]['wavelength'] = wavelength self._header['dataset_info'][ dataset_id]['cell'] = cell self._header['dataset_info'][ dataset_id]['id'] = dataset_number return
def erzatz_resolution(reflection_file, batch_ranges): mtz_obj = mtz.object(reflection_file) miller = mtz_obj.extract_miller_indices() dmax, dmin = mtz_obj.max_min_resolution() ipr_column = None sigipr_column = None i_column = None sigi_column = None batch_column = None uc = None for crystal in mtz_obj.crystals(): if crystal.name() == 'HKL_Base': continue uc = crystal.unit_cell() for dataset in crystal.datasets(): for column in dataset.columns(): if column.label() == 'IPR': ipr_column = column elif column.label() == 'SIGIPR': sigipr_column = column elif column.label() == 'BATCH': batch_column = column elif column.label() == 'I': i_column = column elif column.label() == 'SIGI': sigi_column = column assert(ipr_column or i_column) assert(sigipr_column or sigi_column) assert(batch_column) if ipr_column is None: ipr_column = i_column if sigipr_column is None: sigipr_column = sigi_column ipr_values = ipr_column.extract_values() sigipr_values = sigipr_column.extract_values() batch_values = batch_column.extract_values() batches = batch_values.as_double().iround() resolutions = { } for start, end in batch_ranges: sel = (batches >= start) & (batches <= end) d = uc.d(miller.select(sel)) isig = ipr_values.select(sel) / sigipr_values.select(sel) resolutions[(start, end)] = compute_resolution(dmax, dmin, d, isig) return resolutions
def FrameFromReferenceMTZ(): from iotbx import mtz m = mtz.object('reference.mtz') uc = m.crystals()[0].unit_cell() mi = list(m.extract_miller_indices()) mas = m.as_miller_arrays_dict() i = mas[('XDScrystal', 'XDSdataset', 'IMEAN')].data() s = mas[('XDScrystal', 'XDSdataset', 'IMEAN')].sigmas() return Frame(uc, mi, mi, i, s)
def get_umat_bmat_lattice_symmetry_from_mtz(mtz_file): '''Get the U matrix and lattice symmetry derived from the unit cell constants from an MTZ file.''' from iotbx import mtz m = mtz.object(mtz_file) # assert first U matrix from batches is OK uc = m.crystals()[0].unit_cell() from cctbx.sgtbx import lattice_symmetry_group lattice_symm = lattice_symmetry_group(uc, max_delta=0.0) return tuple(m.batches()[0].umat()), mosflm_B_matrix(uc), lattice_symm
def patch_mtz_unit_cell(mtzfile, unit_cell_parameters): """Overwrite unit cell stored in mtz file""" f = mtz.object(file_name=mtzfile) assert f.n_crystals() == 2, "Can only patch .mtz files with 2 crystals" f.crystals()[0].set_unit_cell_parameters(unit_cell_parameters) f.crystals()[1].set_unit_cell_parameters(unit_cell_parameters) f.write(file_name=mtzfile)
def set_freeRS(hklin, fraction, hklout_work, hklout_free): # open up the reflection file mtz_obj = mtz.object(hklin) mi = mtz_obj.extract_miller_indices() # the calculate the list of unique miller indices I want to assign # as "free" free_set = random_selection(fraction, list(set(mi))) # now read through and assign those as free by adding 50 to FLAG flag_column = None for crystal in mtz_obj.crystals(): for dataset in crystal.datasets(): if dataset.name() != 'HKL_base': dataset_name = dataset.name() if crystal.name() != 'HKL_base': crystal_name = crystal.name() for dataset in crystal.datasets(): for column in dataset.columns(): if column.label() == 'FLAG': flag_column = column flag_values = flag_column.extract_values( not_a_number_substitute = 0.0) for j, hkl in enumerate(mi): if hkl in free_set: flag_values[j] += 50 # now write this back out to a test reflection file flag_column.set_values(flag_values) mtz_obj.write(hklout_work) # now write out the test set for j, hkl in enumerate(mi): if hkl in free_set: flag_values[j] -= 50 else: flag_values[j] += 50 flag_column.set_values(flag_values) mtz_obj.write(hklout_free) return
def integrate_mtz_to_A_matrix(integrate_mtz): from iotbx import mtz from cctbx.uctbx import unit_cell from scitbx import matrix m = mtz.object(integrate_mtz) b = m.batches()[0] u = matrix.sqr(b.umat()).transpose() c = unit_cell(tuple(b.cell())) f = matrix.sqr(c.fractionalization_matrix()).transpose() return (u * f)
def __init__(self, args): working_phil = parse(phil_defaults) phil_args = [] for arg in args[1:]: if os.path.exists(arg): working_phil = working_phil.fetch( source = parse(open(arg).read())) else: phil_args.append(arg) for phil_arg in phil_args: interp = working_phil.command_line_argument_interpreter( home_scope = 'resolutionizer') more_phil = interp.process(phil_arg) working_phil = working_phil.fetch(source = more_phil) self._params = working_phil.extract().resolutionizer # look for useful information in the input file mtz_file = args[0] mtz_obj = mtz.object(mtz_file) i_data = None sigi_data = None mi = mtz_obj.extract_miller_indices() unit_cell = None for crystal in mtz_obj.crystals(): unit_cell = crystal.unit_cell() for dataset in crystal.datasets(): for column in dataset.columns(): if column.label() == 'I': i_data = column.extract_values( not_a_number_substitute = 0.0) if column.label() == 'SIGI': sigi_data = column.extract_values( not_a_number_substitute = 0.0) assert(i_data) assert(sigi_data) self._unit_cell = unit_cell self._space_group = mtz_obj.space_group() self._r = x2tbx.ReflectionList() self._r.setup(mi, i_data, sigi_data) self._r.set_unit_cell(unit_cell.parameters()) self._r.merge() return
def dump_mtz_orientation(mtz_file): from iotbx import mtz from scitbx import matrix from scitbx.math.euler_angles import xyz_angles import os.path m = mtz.object(mtz_file) for b in m.batches(): rxyz = tuple(xyz_angles(matrix.sqr(b.umat()))) print b.num(), '%7.4f %7.4f %7.4f' % rxyz return
def exercise_wavelength(): miller_set = crystal.symmetry(unit_cell=(10, 10, 10, 90, 90, 90), space_group_symbol="P1").miller_set( indices=flex.miller_index([(1, 2, 3), (4, 5, 6)]), anomalous_flag=False ) data = flex.double([1, 2]) info = miller.array_info(wavelength=0.9792) miller_array = miller_set.array(data=data).set_info(info) mtz_dataset = miller_array.as_mtz_dataset(column_root_label="F") mtz_dataset.mtz_object().write("tst_iotbx_mtz_wavelength.mtz") mtz_object = mtz.object(file_name="tst_iotbx_mtz_wavelength.mtz") miller_array = mtz_object.as_miller_arrays()[0] assert approx_equal(miller_array.info().wavelength, 0.9792)
def patch_mtz_unit_cell(mtzfile, unit_cell_parameters): '''Overwrite unit cell stored in mtz file''' from iotbx import mtz f = mtz.object(file_name=mtzfile) assert f.n_crystals() == 2, 'Can only patch .mtz files with 2 crystals' f.crystals()[0].set_unit_cell_parameters(unit_cell_parameters) f.crystals()[1].set_unit_cell_parameters(unit_cell_parameters) f.write(file_name=mtzfile)
def get_column_range(self, column): '''Get the value ranges for this column. This now works by reading the file rather than using cached values => could be slow.''' assert self._hklin, self._hklin assert os.path.exists(self._hklin), self._hklin mtz_obj = mtz.object(self._hklin) col = mtz_obj.get_column(column) valid = col.extract_valid_values() return min(valid), max(valid)
def exercise_util(): miller_set = miller.build_set( crystal.symmetry(unit_cell=(10, 10, 10, 90, 90, 90), space_group_symbol="P1"), d_min=1.5, anomalous_flag=True ) f_obs = miller_set.array(data=flex.double(miller_set.size(), 1.0), sigmas=flex.double(miller_set.size(), 0.1)) flags = f_obs.generate_r_free_flags() mtz_dataset = f_obs.as_mtz_dataset(column_root_label="F") mtz_dataset.add_miller_array(flags, column_root_label="FreeR_flag") mtz_dataset.mtz_object().write("tst_mtz_cutoff.mtz") mtz.cutoff_data("tst_mtz_cutoff.mtz", 2.5) mtz_in = mtz.object(file_name="tst_mtz_cutoff.mtz") ma = mtz_in.as_miller_arrays() assert approx_equal(ma[0].d_min(), 2.5)
def guess_the_atom(hklin, nsites): '''Guess the atom which gives rise to the observed anomalous differences in intensities (i.e. I(+) and I(-)) though CCTBX code internally computes F(+) etc.''' mtz_obj = mtz.object(hklin) mi = mtz_obj.extract_miller_indices() sg = mtz_obj.space_group() for crystal in mtz_obj.crystals(): if crystal.name() != 'HKL_base': uc = crystal.unit_cell() n_ops = len(sg.all_ops()) v_asu = uc.volume() / n_ops mw = v_asu / 2.7 atoms = ['Cr', 'Mn', 'Fe', 'Co', 'Ni', 'Cu', 'Zn', 'Se', 'Br', 'S', 'P'] tables = [sasaki.table(atom) for atom in atoms] for crystal in mtz_obj.crystals(): if crystal.name() == 'HKL_base': continue assert(len(crystal.datasets()) == 1) for dataset in crystal.datasets(): wavelength = dataset.wavelength() energy = wavelength_energy(wavelength) mas = mtz_obj.as_miller_arrays() best_atom = None best_diff = 100.0 for ma in mas: columns = ma.info().label_string() if 'I(+)' in columns and 'I(-)' in columns: signal = ma.anomalous_signal() for j, atom in enumerate(atoms): for energy_offset in (-100.0, 0.0, 100.0): wavelength = wavelength_energy(energy + energy_offset) fdp = tables[j].at_angstrom(wavelength).fdp() p_signal = fdp * math.sqrt(nsites / mw) if math.fabs(p_signal - signal) < best_diff: best_diff = math.fabs(p_signal - signal) best_atom = atom return best_atom
def dump_batch_headers(self): '''Actually print the contents of the mtz file batch headers.''' assert self._hklin, self._hklin assert os.path.exists(self._hklin), self._hklin mtz_obj = mtz.object(self._hklin) for batch in mtz_obj.batches(): current_batch = batch.num() umat = batch.umat() self._batch_header[current_batch] = {'umat': umat} return
def remove_absent_reflections(hklin, hklout): m = mtz.object(hklin) s = m.space_group() mi = m.extract_miller_indices() r = [] for j, i in enumerate(mi): if s.is_sys_absent(i): r.append(j) for j in reversed(r): m.delete_reflection(j) m.write(hklout) return len(r)
def test_setup(config): import cctbx.miller from iotbx import mtz, pdb from scitbx.array_family import flex mtz_name = config['mtz_filename'] mtz_file = mtz.object(mtz_filename) pdb_name = config['pdb_name'] pdb_inp = pdb.input(file_name=pdb_name) structure = pdb_inp.xray_structure_simple() miller = structure.structure_factors(d_min=2.85).f_calc() miller_sub = miller[20000:20002] flex.random_generator.seed(82364) size = miller.size() rand_sel_1 = flex.random_bool(size, 0.5) rand_sel_2 = flex.random_bool(size, 0.5) miller_1 = miller.select(rand_sel_1).randomize_phases() miller_2 = miller.select(rand_sel_2).randomize_phases() rand_doub_1 = flex.random_double(miller_1.size(), 0.1) + 0.015 rand_doub_2 = flex.random_double(miller_2.size(), 0.1) + 0.015 sigmas_1 = rand_doub_1 * miller_1.amplitudes().data() sigmas_2 = rand_doub_2 * miller_2.amplitudes().data() miller_1.set_sigmas(sigmas_1) miller_2.set_sigmas(sigmas_2) miller_1.set_observation_type_xray_amplitude() miller_2.set_observation_type_xray_amplitude() miller_1.as_intensity_array().i_over_sig_i() miller_2.as_intensity_array().i_over_sig_i() binner = miller.setup_binner(n_bins=20) indices = miller.indices() mtch_indcs = miller_1.match_indices(miller_2) mset = miller.set() # doc = wikify_all_methods(cctbx.miller.binning, config) # doc = wikify_all_methods(type(mset), config) # doc = wikify_all_methods(type(binner), config) # doc = wikify_all_methods(type(miller), config) # doc = wikify_all_methods(type(miller.data()), config) # doc = wikify_all_methods(type(indices), config) # doc = wikify_all_methods(type(mtch_indcs), config, # module=["cctbx", "miller"]) return (mtch_indcs, ["cctbx", "miller"])
def get_obs(file_name,tag): """Can we scale one amplitude array to another?""" Possible=["i(+)","iobs(+)"] if tag is not None: Possible.append(tag.lower()) from iotbx import mtz data_SR = mtz.object(file_name) for array in data_SR.as_miller_arrays(): this_label = array.info().label_string().lower() array.show_summary(prefix="OBS ") if True in [this_label.find(tag)>=0 for tag in Possible]: break assert True in [this_label.find(tag)>=0 for tag in Possible], \ "Cannot find i(+); use phenix.mtz.dump and give iobs_tag in phil string" wavelength = get_wavelength(data_SR,Possible) f_ampl = array.as_amplitude_array() merged = array.average_bijvoet_mates() f_ampl_merged = merged.as_amplitude_array() return f_ampl,f_ampl_merged,wavelength