def test_value_grid(self): #path = full_path('5wkd_phases.mtz.gz') path = full_path('5e5z.mtz') mtz = gemmi.read_mtz_file(path) size = mtz.get_size_for_hkl() if numpy is None: return asu = gemmi.ReciprocalAsu(mtz.spacegroup) mtz_data = numpy.array(mtz, copy=False) fp_idx = mtz.column_labels().index('FP') fp_map = {} for row in mtz_data: fp_map[tuple(row[0:3])] = row[fp_idx] for order in (gemmi.AxisOrder.XYZ, gemmi.AxisOrder.ZYX): for half_l in (True, False): grid = mtz.get_value_on_grid('FP', size, half_l=half_l, order=order) counter = 0 for point in grid: hkl = grid.to_hkl(point) value = fp_map.get(tuple(hkl)) if asu.is_in(hkl): if value is not None: self.assertTrue(point.value == value or (numpy.isnan(point.value) and numpy.isnan(value))) counter += 1 else: self.assertEqual(point.value, 0.) else: self.assertIsNone(value) self.assertEqual(counter, mtz_data.shape[0])
def test_reciprocal_asu_checker(self): sg = gemmi.SpaceGroup('I 1 2 1') checker = gemmi.ReciprocalAsu(sg) self.assertTrue(checker.is_in([-5, 5, 1])) self.assertFalse(checker.is_in([5, 5, -1]))
def phase_graft( initial_mtz_path, event_mtz_path, out_path, ): print( "\tGrafting phases from {} to {}".format( str(event_mtz_path), str(initial_mtz_path), ), ) initial_mtz = gemmi.read_mtz_file(str(initial_mtz_path)) # print("\tInitial mtz spacegroup: {}".format(initial_mtz.spacegroup)) event_mtz = gemmi.read_mtz_file(str(event_mtz_path)) # print("\tEvent mtz spacegroup: {}".format(event_mtz.spacegroup)) initial_mtz_data = np.array(initial_mtz, copy=False) # print("\tShape of initial array is {}".format(initial_mtz_data.shape)) event_mtz_data = np.array(event_mtz, copy=False) # print("\tShape of event array is {}".format(event_mtz_data.shape)) initial_reflections: Reflections = Reflections.from_array(initial_mtz_data) event_reflections: Reflections = Reflections.from_array(event_mtz_data) initial_asu = gemmi.ReciprocalAsu(initial_mtz.spacegroup) operations = initial_mtz.spacegroup.operations() initial_mtz_fwt_index = initial_mtz.column_labels().index("FWT") event_mtz_fwt_index = event_mtz.column_labels().index("FWT") initial_mtz_phwt_index = initial_mtz.column_labels().index("PHWT") event_mtz_phwt_index = event_mtz.column_labels().index("PHWT") fom_index = initial_mtz.column_labels().index("FOM") initial_mtz_fo_index = initial_mtz.column_labels().index("F") initial_mtz_fc_index = initial_mtz.column_labels().index("FC") initial_mtz_phc_index = initial_mtz.column_labels().index("PHIC") initial_mtz_r_index = initial_mtz.column_labels().index("FreeR_flag") initial_mtz_ls_fc_all_index = initial_mtz.column_labels().index( "FC_ALL_LS") initial_mtz_ls_phc_all_index = initial_mtz.column_labels().index( "PHIC_ALL_LS") initial_mtz_fc_all_index = initial_mtz.column_labels().index("FC_ALL") initial_mtz_phc_all_index = initial_mtz.column_labels().index("PHIC_ALL") initial_mtz_delfwt_index = initial_mtz.column_labels().index("DELFWT") initial_mtz_phdelwt_index = initial_mtz.column_labels().index("PHDELWT") initial_mtz_sigf_index = initial_mtz.column_labels().index("SIGF") print("\tBeginning graft...") new_reflections = {} for hkl in event_reflections: event_reflection = event_reflections[hkl] asu_hkl = HKL.from_list(initial_asu.to_asu( hkl.to_list(), operations, )) # print("\t\tasu reflection is {}".format(asu_hkl)) if asu_hkl.is_000(): # print("\t\t\tReflection 000 {}".format(event_reflection)) data = np.zeros( len(list(initial_reflections.reflections.values())[0].data)) # print("\t\t\t{}".format(data.shape)) new_reflection = Reflection(hkl, data) else: initial_reflection: Reflection = initial_reflections[asu_hkl] new_reflection = Reflection(hkl, np.copy(initial_reflection.data)) new_reflection.data[initial_mtz_fwt_index - 3] = event_reflection.data[event_mtz_fwt_index - 3] new_reflection.data[initial_mtz_phwt_index - 3] = event_reflection.data[event_mtz_phwt_index - 3] # new_reflection.data[initial_mtz_fo_index - 3] = 0.0 # new_reflection.data[initial_mtz_fc_index - 3] = 0.0 # new_reflection.data[initial_mtz_phc_index - 3] = 0.0 # new_reflection.data[fom_index - 3] = 0.0 # new_reflection.data[initial_mtz_r_index - 3] = 0.0 # # new_reflection.data[initial_mtz_ls_fc_all_index - 3] = 0.0 # new_reflection.data[initial_mtz_ls_phc_all_index - 3] = 0.0 # new_reflection.data[initial_mtz_fc_all_index - 3] = 0.0 # new_reflection.data[initial_mtz_phc_all_index - 3] = 0.0 # new_reflection.data[initial_mtz_delfwt_index - 3] = 0.0 # new_reflection.data[initial_mtz_phdelwt_index - 3] = 0.0 # # new_reflection.data[initial_mtz_sigf_index - 3] = 0.0 # event_reflection = event_reflections[hkl] # # asu_hkl = HKL.from_list(initial_asu.to_asu(hkl.to_list(), operations, )) # if asu_hkl.is_000(): # print("\t\t{}".format([hkl, asu_hkl])) # # print("#### zeros array, normal array") # print(np.zeros(len(list(initial_reflections.reflections.values())[0].data)).shape) # print(list(initial_reflections.reflections.values())[0].data.shape) # # data = np.zeros(len(list(initial_reflections.reflections.values())[0].data)) # new_reflection = Reflection(hkl, data) # # new_reflection.data[initial_mtz_fwt_index - 3] = event_reflection.data[event_mtz_fwt_index - 3] # new_reflection.data[initial_mtz_phwt_index - 3] = event_reflection.data[event_mtz_phwt_index - 3] new_reflections[hkl] = new_reflection print("\tFinished iterating reflections") new_array = Reflections(new_reflections).to_array() # print("\tShape of new array is {}".format(new_array.shape)) initial_mtz.spacegroup = event_mtz.spacegroup initial_mtz.set_data(new_array) np.core.arrayprint._line_width = 240 # print([initial_mtz_fwt_index, initial_mtz_phwt_index, event_mtz_fwt_index, event_mtz_phwt_index]) # print(new_array.shape) # print(new_array[-5:-1,:]) # print(np.array(initial_mtz)[-5:-1,:]) # print(np.array(event_mtz)[-5:-1,:]) print("\tWriting new reflections to {}".format(str(out_path))) initial_mtz.write_to_file(str(out_path))