def to_gemmi(self): mtz = gemmi.Mtz(with_base=False) mtz.title = self.mtz_title mtz.history = self.mtz_history spacegroup = self.spacegroup.to_gemmi() mtz.spacegroup = spacegroup unit_cell = self.unit_cell.to_gemmi() mtz.set_cell_for_all(unit_cell) for dataset in self.datasets: mtz.add_dataset(dataset.dataset_name) ds = mtz.dataset(dataset.id) ds.project_name = dataset.project_name ds.crystal_name = dataset.crystal_name ds.wavelength = dataset.wavelength for column in self.columns: mtz.add_column(column.label, column.column_type, dataset_id=column.dataset_id) mtz.set_data(self.array) mtz.update_reso() return mtz
def main(): config = Config.from_config() m = gemmi.read_ccp4_map(str(config.xmap_in)) print(dir(m)) # m.spacegroup = gemmi.find_spacegroup_by_name('P1') print(m.grid.spacegroup) m.grid.spacegroup = gemmi.find_spacegroup_by_name('P1') print(m.grid.spacegroup) m.setup() print(m.grid.spacegroup) m.grid.spacegroup = gemmi.find_spacegroup_by_name('P1') print(m.grid.spacegroup) sf = gemmi.transform_map_to_f_phi(m.grid, half_l=True) print(sf.spacegroup) # data = sf print(dir(sf)) data = sf.prepare_asu_data(dmin=config.resolution, with_000=True) mtz = gemmi.Mtz(with_base=True) # mtz = gemmi.Mtz() print(dir(mtz)) mtz.spacegroup = sf.spacegroup # mtz.spacegroup = gemmi.find_spacegroup_by_name('P1') # mtz.set_cell_for_all(sf.unit_cell) mtz.cell = sf.unit_cell mtz.add_dataset('unknown') mtz.add_column('FWT', 'F') mtz.add_column('PHWT', 'P') mtz.set_data(data) mtz.write_to_file(str(config.mtz_out))
def __setstate__(self, state): data = state["data"] spacegroup = state["spacegroup"] unit_cell = state["unit_cell"] mtz = gemmi.Mtz() mtz.spacegroup = gemmi.find_spacegroup_by_number(spacegroup) mtz.cell.set( unit_cell[0], unit_cell[1], unit_cell[2], unit_cell[3], unit_cell[4], unit_cell[5], ) datasets = state["datasets"] columns = state["columns"] for dataset_id, dataset_label in datasets.items(): mtz.add_dataset(dataset_label) dataset_columns = columns[dataset_id] for column_name, column_type in dataset_columns.items(): mtz.add_column(column_name, column_type) mtz.set_data(data) self.mtz = mtz
def combine_mtz(prefix, columns): """Combine columns from multiple MTZ files with gemmijoin""" result = { "hklout": "%s.mtz" % prefix, "stdout": "%s.log" % prefix, "stderr": "%s.err" % prefix, } reference = columns[0] reference_mtz = gemmi.read_mtz_file(reference) reference_data = numpy.array(reference_mtz, copy=False) reference_df = pandas.DataFrame(data=reference_data, columns=reference_mtz.column_labels()) reference_sel_col_df = reference_df[[ 'H', 'K', 'L', 'FP', 'SIGFP', 'FREE', 'PHWT', 'FOM' ]] model = columns[1] model_mtz = gemmi.read_mtz_file(model) model_data = numpy.array(model_mtz, copy=False) model_df = pandas.DataFrame(data=model_data, columns=model_mtz.column_labels()) model_sel_col_df = model_df[['PHWT', 'FOM']] model_sel_col_df = model_sel_col_df.rename(columns={ 'PHWT': 'PHWT_model', 'FOM': 'FOM_model' }) combined_df = pandas.concat([reference_sel_col_df, model_sel_col_df], ignore_index=True, axis=1) combined_array = combined_df.to_numpy() mtz_new = gemmi.Mtz() mtz_new.add_dataset('combined') mtz_new.add_column('H', 'H', dataset_id=0) mtz_new.add_column('K', 'H', dataset_id=0) mtz_new.add_column('L', 'H', dataset_id=0) mtz_new.add_column('FP', 'F', dataset_id=0) mtz_new.add_column('SIGFP', 'Q', dataset_id=0) mtz_new.add_column('FREE', 'I', dataset_id=0) mtz_new.add_column('PHWT_ref', 'P', dataset_id=0) mtz_new.add_column('FOM_ref', 'W', dataset_id=0) mtz_new.add_column('PHWT_model', 'P', dataset_id=0) mtz_new.add_column('FOM_model', 'W', dataset_id=0) unit_cell = reference_mtz.cell space_group = reference_mtz.spacegroup mtz_new.spacegroup = space_group mtz_new.cell = unit_cell mtz_new.set_data(combined_array) mtz_new.write_to_file(result["hklout"]) if not os.path.exists(result["hklout"]): return {"error": "No reflection data produced"} return result
def build_emptyMTZ(sg): """Build empty gemmi.MTZ""" m = gemmi.Mtz() m.spacegroup = sg m.add_dataset('crystal') m.add_column('H', 'H') m.add_column('K', 'H') m.add_column('L', 'H') m.add_column('M/ISYM', 'Y') return m
#!/usr/bin/env python # Convert CCP4 map to map coefficients in MTZ import sys import gemmi RESOLUTION_LIMIT = 1.5 # set 0 for no limit if len(sys.argv) != 3: sys.exit('Usage: map2mtz.py input.ccp4 output.mtz') m = gemmi.read_ccp4_map(sys.argv[1]) m.setup() sf = gemmi.transform_map_to_f_phi(m.grid, half_l=True) data = sf.prepare_asu_data(dmin=RESOLUTION_LIMIT) mtz = gemmi.Mtz(with_base=True) mtz.spacegroup = sf.spacegroup mtz.set_cell_for_all(sf.unit_cell) mtz.add_dataset('unknown') mtz.add_column('FWT', 'F') mtz.add_column('PHWT', 'P') mtz.set_data(data) mtz.write_to_file(sys.argv[2])
def to_gemmi(dataset, skip_problem_mtztypes=False): """ Construct gemmi.Mtz object from DataSet If ``dataset.merged == False``, the reflections will be mapped to the reciprocal space ASU, and a M/ISYM column will be constructed. If boolean flags with the label ``PARTIAL`` or ``CENTRIC`` are found in the DataSet, these will be cast to the ``MTZInt`` dtype, and included in the gemmi.Mtz object. Parameters ---------- dataset : rs.DataSet DataSet object to convert to gemmi.Mtz skip_problem_mtztypes : bool Whether to skip columns in DataSet that do not have specified mtz datatypes Returns ------- gemmi.Mtz """ # Check that cell and spacegroup are defined if not dataset.cell: raise AttributeError( f"Instance of type {dataset.__class__.__name__} has no unit cell information" ) if not dataset.spacegroup: raise AttributeError( f"Instance of type {dataset.__class__.__name__} has no space group information" ) # Build up a gemmi.Mtz object mtz = gemmi.Mtz() mtz.cell = dataset.cell mtz.spacegroup = dataset.spacegroup # Handle Unmerged data if not dataset.merged: dataset.hkl_to_asu(inplace=True) # Construct data for Mtz object. mtz.add_dataset("reciprocalspaceship") temp = dataset.reset_index() columns = [] for c in temp.columns: cseries = temp[c] if isinstance(cseries.dtype, MTZDtype): mtzcol = mtz.add_column(label=c, type=cseries.dtype.mtztype) columns.append(c) # Special case for CENTRIC and PARTIAL flags elif cseries.dtype.name == "bool" and c in ["CENTRIC", "PARTIAL"]: temp[c] = temp[c].astype("MTZInt") mtzcol = mtz.add_column(label=c, type="I") columns.append(c) elif skip_problem_mtztypes: continue else: raise ValueError( f"column of type {cseries.dtype} cannot be written to an MTZ file. " f"To skip columns without explicit MTZ dtypes, set skip_problem_mtztypes=True" ) mtz.set_data(temp[columns].to_numpy(dtype="float32")) return mtz