def box_iterator(self):
     p = self.xrs.unit_cell().parameters()
     b = maptbx.boxes_by_dimension(n_real=self.n_real,
                                   dim=self.box_dimension,
                                   abc=p[:3])
     i_box = 0
     for s, e in zip(b.starts, b.ends):
         i_box += 1
         map_box_obs = maptbx.copy(self.map_data_obs, s, e)
         map_box_calc = maptbx.copy(self.map_data_calc, s, e)
         map_box_obs.reshape(flex.grid(map_box_obs.all()))
         map_box_calc.reshape(flex.grid(map_box_calc.all()))
         #######
         # XXX Copy-paste from map_box
         abc = []
         for i in range(3):
             abc.append(p[i] * map_box_calc.all()[i] / self.n_real[i])
         ucb = uctbx.unit_cell(parameters=(abc[0], abc[1], abc[2], p[3],
                                           p[4], p[5]))
         cs = crystal.symmetry(unit_cell=ucb, space_group="P1")
         #######
         diff_map = scale_two_real_maps_in_fourier_space(
             m1=map_box_obs,
             m2=map_box_calc,
             cs=cs,
             d_min=self.d_min,
             vector_map=self.vector_map)
         maptbx.set_box(map_data_from=diff_map,
                        map_data_to=self.map_result,
                        start=s,
                        end=e)
     sd = self.map_result.sample_standard_deviation()
     if (sd != 0):
         self.map_result = self.map_result / sd
Пример #2
0
def exercise_real_space_refinement(verbose):
    if (verbose):
        out = sys.stdout
    else:
        out = StringIO()
    out_of_bounds_clamp = maptbx.out_of_bounds_clamp(0)
    out_of_bounds_raise = maptbx.out_of_bounds_raise()
    crystal_symmetry = crystal.symmetry(unit_cell=(10, 10, 10, 90, 90, 90),
                                        space_group_symbol="P 1")
    xray_structure = xray.structure(crystal_symmetry=crystal_symmetry,
                                    scatterers=flex.xray_scatterer([
                                        xray.scatterer(label="C",
                                                       site=(0, 0, 0))
                                    ]))
    miller_set = miller.build_set(crystal_symmetry=crystal_symmetry,
                                  anomalous_flag=False,
                                  d_min=1)
    f_calc = miller_set.structure_factors_from_scatterers(
        xray_structure=xray_structure).f_calc()
    fft_map = f_calc.fft_map()
    fft_map.apply_sigma_scaling()
    real_map = fft_map.real_map_unpadded()
    #### unit_cell test
    delta_h = .005
    basic_map = maptbx.basic_map(
        maptbx.basic_map_unit_cell_flag(), real_map, real_map.focus(),
        crystal_symmetry.unit_cell().orthogonalization_matrix(),
        out_of_bounds_clamp.as_handle(), crystal_symmetry.unit_cell())
    testing_function_for_rsfit(basic_map, delta_h, xray_structure, out)
    ### non_symmetric test
    #
    minfrac = crystal_symmetry.unit_cell().fractionalize((-5, -5, -5))
    maxfrac = crystal_symmetry.unit_cell().fractionalize((5, 5, 5))
    gridding_first = [ifloor(n * b) for n, b in zip(fft_map.n_real(), minfrac)]
    gridding_last = [iceil(n * b) for n, b in zip(fft_map.n_real(), maxfrac)]
    data = maptbx.copy(real_map, gridding_first, gridding_last)
    #
    basic_map = maptbx.basic_map(
        maptbx.basic_map_non_symmetric_flag(), data, fft_map.n_real(),
        crystal_symmetry.unit_cell().orthogonalization_matrix(),
        out_of_bounds_clamp.as_handle(), crystal_symmetry.unit_cell())
    testing_function_for_rsfit(basic_map, delta_h, xray_structure, out)
    ### asu test
    #
    minfrac = crystal_symmetry.unit_cell().fractionalize((0, 0, 0))
    maxfrac = crystal_symmetry.unit_cell().fractionalize((10, 10, 10))
    gridding_first = [ifloor(n * b) for n, b in zip(fft_map.n_real(), minfrac)]
    gridding_last = [iceil(n * b) for n, b in zip(fft_map.n_real(), maxfrac)]
    data = maptbx.copy(real_map, gridding_first, gridding_last)
    #
    basic_map = maptbx.basic_map(
        maptbx.basic_map_asu_flag(), data, crystal_symmetry.space_group(),
        crystal_symmetry.direct_space_asu().as_float_asu(), real_map.focus(),
        crystal_symmetry.unit_cell().orthogonalization_matrix(),
        out_of_bounds_clamp.as_handle(), crystal_symmetry.unit_cell(), 0.5,
        True)
    testing_function_for_rsfit(basic_map, delta_h, xray_structure, out)
Пример #3
0
def calculate_exp_i_two_phi_peaks(xray_structure, d_min,
                                  min_peak_distance,
                                  max_reduced_peaks):
  f_h = xray_structure.structure_factors(
    anomalous_flag=False,
    d_min=d_min).f_calc()
  two_i_phi_h = miller.array(
    miller_set=f_h,
    data=flex.polar(1, flex.arg(f_h.data())*2))
  fft_map = two_i_phi_h.fft_map(
    d_min=d_min,
    symmetry_flags=maptbx.use_space_group_symmetry)
  real_map = fft_map.real_map()
  real_map = maptbx.copy(real_map, flex.grid(real_map.focus()))
  stats = maptbx.statistics(real_map)
  if (stats.max() != 0):
    real_map /= abs(stats.max())
  grid_tags = maptbx.grid_tags(real_map.focus())
  grid_tags.build(fft_map.space_group_info().type(), fft_map.symmetry_flags())
  grid_tags.verify(real_map)
  peak_list = maptbx.peak_list(
    data=real_map,
    tags=grid_tags.tag_array(),
    max_peaks=10*max_reduced_peaks,
    interpolate=True)
  reduced_peaks = peak_cluster_reduction(
    crystal_symmetry=xray_structure,
    peak_list=peak_list,
    min_peak_distance=min_peak_distance,
    max_reduced_peaks=max_reduced_peaks)
  return reduced_peaks
Пример #4
0
    def apply_to_map(self, map_manager):
        assert map_manager is not None
        # Apply to a map_manager that is similar to the one used to generate
        #   this around_model object
        assert map_manager.crystal_symmetry().is_similar_symmetry(
            self.original_crystal_symmetry)

        ma1 = map_manager.map_data().accessor()
        ma2 = self.original_accessor
        assert ma1.all() == ma2.all()
        assert ma1.origin() == ma2.origin()
        assert ma1.focus() == ma2.focus()
        map_data = map_manager.map_data()
        # Check if map is all valid
        bounds_info = get_bounds_of_valid_region(
            map_data=map_data,
            gridding_first=self.gridding_first,
            gridding_last=self.gridding_last)
        if self.wrapping or bounds_info.inside_allowed_bounds:
            # Just copy everything
            map_box = maptbx.copy(map_data, self.gridding_first,
                                  self.gridding_last)
            # Note: map_box gridding is self.gridding_first to self.gridding_last
        else:  # Need to copy and then zero outside of defined region
            map_box = copy_and_zero_map_outside_bounds(map_data=map_data,
                                                       bounds_info=bounds_info)
        #  Now reshape map_box to put origin at (0,0,0)
        map_box.reshape(flex.grid(self.box_all))

        # Create new map_manager object:
        #   Use original values for:
        #     unit_cell_grid    (gridding of original full unit cell)
        #     unit_cell_crystal_symmetry  (symmetry of original full unit cell)
        #     input_file_name
        #   Use new (boxed) values for:
        #     map_data
        #     crystal_symmetry   (symmetry of the part of the map that is present)
        #   Update:
        #     origin_shift_grid_units  (position in the original map of the
        #                                 (0,0,0) grid point in map_box)
        #     labels  (add label specifying boxing operation)
        #
        # New origin_shift_grid_units:
        origin_shift_grid_units = [
            self.gridding_first[i] + map_manager.origin_shift_grid_units[i]
            for i in range(3)
        ]
        # New labels:
        new_label = "Boxed %s to %s %s" % (str(tuple(
            self.gridding_first)), str(tuple(
                self.gridding_last)), self.basis_for_boxing_string)
        #  Set up new map_manager.
        #  NOTE: origin_shift_grid_units is required as bounds have changed
        new_map_manager = map_manager.customized_copy(
            map_data=map_box, origin_shift_grid_units=origin_shift_grid_units)
        # Add the label
        new_map_manager.add_label(new_label)
        return new_map_manager
Пример #5
0
def get_diff_map(symmetry, fcalc_map, expt_map, d_min):
    """Compute a difference map between the calculated and experimental maps.
  Modified from mmtbx.command_line.real_space_diff_map class compdiff."""
    scale = scale_k1(x=expt_map, y=fcalc_map)
    scaled_fcalc_map = fcalc_map * scale  # should already be scaled but okay
    origin = expt_map.origin()
    shifted_expt_map = expt_map.shift_origin()
    shifted_diff_map = flex.double(flex.grid(expt_map.all()))
    diff_map = flex.double(flex.grid(expt_map.origin(), expt_map.focus()))
    ucell_params = symmetry.unit_cell().parameters()
    boxes = maptbx.boxes_by_dimension(n_real=expt_map.all(),
                                      dim=30,
                                      abc=ucell_params[:3])
    i_box = 0
    test_map_box_obs_all = None
    for start, end in zip(boxes.starts, boxes.ends):
        i_box += 1
        map_box_obs = maptbx.copy(shifted_expt_map, start, end)
        map_box_calc = maptbx.copy(scaled_fcalc_map, start, end)
        map_box_obs.reshape(flex.grid(map_box_obs.all()))
        map_box_calc.reshape(flex.grid(map_box_calc.all()))
        # abc = [ucell_params[i]/expt_map.all()[i] for i in range(3)]
        # ucb = uctbx.unit_cell(parameters=(
        # abc[0],abc[1],abc[2],ucell_params[3],ucell_params[4],ucell_params[5]))
        # cs = crystal.symmetry(unit_cell=ucb, space_group="P1")
        cs = symmetry
        diff_map_part = scale_two_real_maps_in_fourier_space(
            m1=map_box_obs,
            m2=map_box_calc,
            cs=cs,
            d_min=d_min,
            vector_map=True)  # this means yes, use the phases from fcalc
        maptbx.set_box(map_data_from=diff_map_part,
                       map_data_to=shifted_diff_map,
                       start=start,
                       end=end)
    # skip this additional scaling step -- it explodes everything
    # sd = diff_map.sample_standard_deviation()
    # if(sd!=0):
    # diff_map = diff_map/sd
    # somehow put the contents of shifted_diff_map into diff_map but keep origin?
    for i in xrange(len(shifted_diff_map)):
        diff_map[i] = shifted_diff_map[i]
        # FIXME SUPER SLOW but it should work for now
    return diff_map
Пример #6
0
    def __init__(self,
                 crystal_gridding,
                 fmodel,
                 map_type,
                 max_boxes,
                 box_size_as_fraction=None):
        sgt = fmodel.f_obs().space_group().type()
        assert sgt.number() == 1

        def get_map(fmodel, map_type, external_complete_set=None):
            f_map = fmodel.electron_density_map().map_coefficients(
                map_type=map_type, isotropize=True, fill_missing=False)
            fft_map = miller.fft_map(crystal_gridding=crystal_gridding,
                                     fourier_coefficients=f_map)
            return fft_map.real_map_unpadded()

        f_model = fmodel.f_model_scaled_with_k1()
        fft_map = miller.fft_map(crystal_gridding=crystal_gridding,
                                 fourier_coefficients=f_model)
        f_model_map_data = fft_map.real_map_unpadded()
        zero_complex_ma = f_model.customized_copy(
            data=flex.complex_double(f_model.data().size(), 0))
        b = maptbx.boxes(fraction=0.3,
                         n_real=f_model_map_data.focus(),
                         max_boxes=max_boxes,
                         log=sys.stdout)
        self.map_result = flex.double(flex.grid(b.n_real))
        self.r = flex.double()
        for s, e in zip(b.starts, b.ends):
            f_model_map_data_omit = maptbx.set_box_copy(
                value=0, map_data_to=f_model_map_data, start=s, end=e)
            f_model_omit = f_model.structure_factors_from_map(
                map=f_model_map_data_omit,
                use_scale=True,
                anomalous_flag=False,
                use_sg=False)
            fmodel_ = mmtbx.f_model.manager(f_obs=fmodel.f_obs(),
                                            r_free_flags=fmodel.r_free_flags(),
                                            f_calc=f_model_omit,
                                            f_mask=zero_complex_ma)
            self.r.append(fmodel_.r_work())
            f_map_data = get_map(fmodel=fmodel_, map_type=map_type)
            etmp = [e[0] - 1, e[1] - 1,
                    e[2] - 1]  # because .copy() includes right edge
            box = maptbx.copy(f_map_data, s, etmp)
            box.reshape(flex.grid(box.all()))
            maptbx.set_box(map_data_from=box,
                           map_data_to=self.map_result,
                           start=s,
                           end=e)
        sd = self.map_result.sample_standard_deviation()
        self.map_result = self.map_result / sd
        self.map_coefficients = fmodel.f_obs().structure_factors_from_map(
            map=self.map_result,
            use_scale=True,
            anomalous_flag=False,
            use_sg=False)
Пример #7
0
def exercise_writer():
    from iotbx import file_reader
    from cctbx import uctbx, sgtbx
    from scitbx.array_family import flex
    file_name = libtbx.env.find_in_repositories(
        relative_path=
        "phenix_regression/wizards/partial_refine_001_map_coeffs.mtz",
        test=os.path.isfile)
    if file_name is None:
        print "Can't find map coefficients file, skipping."
        return
    mtz_in = file_reader.any_file(file_name, force_type="hkl").file_object
    miller_arrays = mtz_in.as_miller_arrays()
    map_coeffs = miller_arrays[0]
    fft_map = map_coeffs.fft_map(resolution_factor=1 / 3.0)
    fft_map.apply_sigma_scaling()
    fft_map.as_ccp4_map(file_name="2mFo-DFc.map")
    m = iotbx.ccp4_map.map_reader(file_name="2mFo-DFc.map")
    real_map = fft_map.real_map_unpadded()
    mmm = flex.double(list(real_map)).min_max_mean()
    assert approx_equal(m.unit_cell_parameters,
                        map_coeffs.unit_cell().parameters())
    assert approx_equal(mmm.min, m.header_min)
    assert approx_equal(mmm.max, m.header_max)
    #assert approx_equal(mmm.mean, m.header_mean)
    # random small maps of different sizes
    for nxyz in flex.nested_loop((1, 1, 1), (4, 4, 4)):
        mt = flex.mersenne_twister(0)
        grid = flex.grid(nxyz)
        map = mt.random_double(size=grid.size_1d())
        map.reshape(grid)
        real_map = fft_map.real_map_unpadded()
        iotbx.ccp4_map.write_ccp4_map(
            file_name="random.map",
            unit_cell=uctbx.unit_cell((1, 1, 1, 90, 90, 90)),
            space_group=sgtbx.space_group_info("P1").group(),
            gridding_first=(0, 0, 0),
            gridding_last=tuple(fft_map.n_real()),
            map_data=real_map,
            labels=flex.std_string(["iotbx.ccp4_map.tst"]))
        m = iotbx.ccp4_map.map_reader(file_name="random.map")
        mmm = flex.double(list(real_map)).min_max_mean()
        assert approx_equal(m.unit_cell_parameters, (1, 1, 1, 90, 90, 90))
        assert approx_equal(mmm.min, m.header_min)
        assert approx_equal(mmm.max, m.header_max)
        #
        gridding_first = (0, 0, 0)
        gridding_last = tuple(fft_map.n_real())
        map_box = maptbx.copy(map, gridding_first, gridding_last)
        map_box.reshape(flex.grid(map_box.all()))
        iotbx.ccp4_map.write_ccp4_map(
            file_name="random_box.map",
            unit_cell=uctbx.unit_cell((1, 1, 1, 90, 90, 90)),
            space_group=sgtbx.space_group_info("P1").group(),
            map_data=map_box,
            labels=flex.std_string(["iotbx.ccp4_map.tst"]))
Пример #8
0
def copy_and_zero_map_outside_bounds(map_data=None, bounds_info=None):
    '''
     Copy part of a map and zero outside valid region

     Goes with get_bounds_of_valid_region

     First copy requested part of map, wrapping if request goes outside
     of supplied map.

     Then zero out everything that was from outside the available region.

     Returns map with bounds from bounds_info.gridding_first to
     bounds_info.gridding_last, but everything outside of
     lower_allowed_bounds to upper_allowed_bounds is zeroed out.
  '''

    # First copy the entire requested region:

    map_copy = maptbx.copy(map_data, bounds_info.gridding_first,
                           bounds_info.gridding_last)
    # Note: the origin of map_copy is at gridding_first and goes to last

    # Make sure we are working with a flex.double array
    if type(map_copy) != type(flex.double()):  # must be double
        map_copy = map_copy.as_double()

    # Now zero out everything outside of the valid region

    # Make sure this map matches the bounds_info
    assert tuple(map_copy.origin()) == tuple(bounds_info.gridding_first)

    # We are going to shift the origin in this copy, so shift the bounds to match

    lower_bounds_after_shift = []
    upper_bounds_after_shift = []
    for l, u, f in zip(bounds_info.lower_allowed_bounds,
                       bounds_info.upper_allowed_bounds,
                       bounds_info.gridding_first):
        lower_bounds_after_shift.append(l - f)
        upper_bounds_after_shift.append(u - f)

    acc = map_copy.accessor()  # save where the origin is

    map_copy = map_copy.shift_origin()  # put origin at (0,0,0)
    map_copy_all = map_copy.all()  # save size of map
    # XXX work-around for set_box does not allow offset origin
    map_copy.resize(flex.grid(map_copy_all))
    new_map = maptbx.set_box_copy_inside(
        0,  # copies inside, zero outside bounds
        map_data_to=map_copy,
        start=tuple(lower_bounds_after_shift),
        end=tuple(upper_bounds_after_shift))
    # XXX and shift map back
    new_map = new_map.as_1d()
    new_map.reshape(acc)
    return new_map
Пример #9
0
def exercise_writer () :
  from iotbx import file_reader
  from cctbx import uctbx, sgtbx
  from scitbx.array_family import flex
  file_name = libtbx.env.find_in_repositories(
    relative_path="phenix_regression/wizards/partial_refine_001_map_coeffs.mtz",
    test=os.path.isfile)
  if file_name is None :
    print "Can't find map coefficients file, skipping."
    return
  mtz_in = file_reader.any_file(file_name, force_type="hkl").file_object
  miller_arrays = mtz_in.as_miller_arrays()
  map_coeffs = miller_arrays[0]
  fft_map = map_coeffs.fft_map(resolution_factor=1/3.0)
  fft_map.apply_sigma_scaling()
  fft_map.as_ccp4_map(file_name="2mFo-DFc.map")
  m = iotbx.ccp4_map.map_reader(file_name="2mFo-DFc.map")
  real_map = fft_map.real_map_unpadded()
  mmm = flex.double(list(real_map)).min_max_mean()
  assert approx_equal(m.unit_cell_parameters,
                      map_coeffs.unit_cell().parameters())
  assert approx_equal(mmm.min, m.header_min)
  assert approx_equal(mmm.max, m.header_max)
  #assert approx_equal(mmm.mean, m.header_mean)
  # random small maps of different sizes
  for nxyz in flex.nested_loop((1,1,1),(4,4,4)):
    mt = flex.mersenne_twister(0)
    grid = flex.grid(nxyz)
    map = mt.random_double(size=grid.size_1d())
    map.reshape(grid)
    real_map = fft_map.real_map_unpadded()
    iotbx.ccp4_map.write_ccp4_map(
      file_name="random.map",
      unit_cell=uctbx.unit_cell((1,1,1,90,90,90)),
      space_group=sgtbx.space_group_info("P1").group(),
      gridding_first=(0,0,0),
      gridding_last=tuple(fft_map.n_real()),
      map_data=real_map,
      labels=flex.std_string(["iotbx.ccp4_map.tst"]))
    m = iotbx.ccp4_map.map_reader(file_name="random.map")
    mmm = flex.double(list(real_map)).min_max_mean()
    assert approx_equal(m.unit_cell_parameters, (1,1,1,90,90,90))
    assert approx_equal(mmm.min, m.header_min)
    assert approx_equal(mmm.max, m.header_max)
    #
    gridding_first = (0,0,0)
    gridding_last = tuple(fft_map.n_real())
    map_box = maptbx.copy(map, gridding_first, gridding_last)
    map_box.reshape(flex.grid(map_box.all()))
    iotbx.ccp4_map.write_ccp4_map(
      file_name="random_box.map",
      unit_cell=uctbx.unit_cell((1,1,1,90,90,90)),
      space_group=sgtbx.space_group_info("P1").group(),
      map_data=map_box,
      labels=flex.std_string(["iotbx.ccp4_map.tst"]))
Пример #10
0
def map_of_coeff_scaled(mask_map,structure,nxyz):
     assert mask_map.is_0_based()
     assert not mask_map.is_padded()
     fft_manager = fftpack.real_to_complex_3d(mask_map.focus())
     padded_data = maptbx.copy(mask_map,
                               flex.grid(fft_manager.m_real()
                              ).set_focus(fft_manager.n_real()))
     map_of_coeff = fft_manager.forward(padded_data)
     scale = matrix.col(nxyz).product()/structure.unit_cell().volume()
     sc = matrix.col(mask_map.focus()).product()/structure.unit_cell().volume()
     assert sc == scale
     map_of_coeff /= scale
     return map_of_coeff
Пример #11
0
def exercise_mask_data_2(space_group_info,
                         n_sites=100,
                         d_min=2.0,
                         resolution_factor=1. / 4):
    from cctbx import maptbx
    from cctbx.masks import vdw_radii_from_xray_structure
    for yn2 in [0, 1]:
        for yn in [0, 1]:
            xrs = random_structure.xray_structure(
                space_group_info=space_group_info,
                elements=(("O", "N", "C") * (n_sites // 3 + 1))[:n_sites],
                volume_per_atom=50,
                min_distance=1.5)
            xrs.shake_sites_in_place(mean_distance=10)
            if (yn2): xrs = xrs.expand_to_p1(sites_mod_positive=True)
            atom_radii = vdw_radii_from_xray_structure(xray_structure=xrs)
            asu_mask = masks.atom_mask(unit_cell=xrs.unit_cell(),
                                       group=xrs.space_group(),
                                       resolution=d_min,
                                       grid_step_factor=resolution_factor,
                                       solvent_radius=1.0,
                                       shrink_truncation_radius=1.0)
            asu_mask.compute(xrs.sites_frac(), atom_radii)
            mask_data = asu_mask.mask_data_whole_uc()
            #
            xrs_p1 = xrs.expand_to_p1(sites_mod_positive=True)
            for site_frac in xrs_p1.sites_frac():
                mv = mask_data.value_at_closest_grid_point(site_frac)
                assert mv == 0
            #
            mask_data = mask_data / xrs.space_group().order_z()
            if (yn == 1):
                mask_data = maptbx.copy(mask_data,
                                        flex.grid(mask_data.focus()))
            #
            for site_frac in xrs_p1.sites_frac():
                mv = mask_data.value_at_closest_grid_point(site_frac)
                assert mv == 0
            #
            fc = xrs.structure_factors(d_min=d_min).f_calc()
            f_mask_1 = fc.set().array(
                data=asu_mask.structure_factors(fc.indices()))
            f_mask_2 = f_mask_1.structure_factors_from_map(
                map=mask_data,
                use_scale=True,
                anomalous_flag=False,
                use_sg=True)
            fm1 = abs(f_mask_1).data()
            fm2 = abs(f_mask_2).data()
            r = flex.sum(flex.abs(fm1 - fm2)) / flex.sum(fm1 + fm2)
            assert approx_equal(r, 0.0)
Пример #12
0
def map_of_coeff_scaled(mask_map, structure, nxyz):
    assert mask_map.is_0_based()
    assert not mask_map.is_padded()
    fft_manager = fftpack.real_to_complex_3d(mask_map.focus())
    padded_data = maptbx.copy(
        mask_map,
        flex.grid(fft_manager.m_real()).set_focus(fft_manager.n_real()))
    map_of_coeff = fft_manager.forward(padded_data)
    scale = matrix.col(nxyz).product() / structure.unit_cell().volume()
    sc = matrix.col(
        mask_map.focus()).product() / structure.unit_cell().volume()
    assert sc == scale
    map_of_coeff /= scale
    return map_of_coeff
Пример #13
0
def exercise_mask_data_2(space_group_info, n_sites=100, d_min=2.0,
                         resolution_factor=1./4):
  from cctbx import maptbx
  from cctbx.masks import vdw_radii_from_xray_structure
  for yn2 in [0,1]:
    for yn in [0,1]:
      xrs = random_structure.xray_structure(
        space_group_info=space_group_info,
        elements=(("O","N","C")*(n_sites//3+1))[:n_sites],
        volume_per_atom=50,
        min_distance=1.5)
      xrs.shake_sites_in_place(mean_distance=10)
      if(yn2): xrs = xrs.expand_to_p1(sites_mod_positive=True)
      atom_radii = vdw_radii_from_xray_structure(xray_structure = xrs)
      asu_mask = masks.atom_mask(
        unit_cell                = xrs.unit_cell(),
        group                    = xrs.space_group(),
        resolution               = d_min,
        grid_step_factor         = resolution_factor,
        solvent_radius           = 1.0,
        shrink_truncation_radius = 1.0)
      asu_mask.compute(xrs.sites_frac(), atom_radii)
      mask_data = asu_mask.mask_data_whole_uc()
      #
      xrs_p1 = xrs.expand_to_p1(sites_mod_positive=True)
      for site_frac in xrs_p1.sites_frac():
        mv = mask_data.value_at_closest_grid_point(site_frac)
        assert mv == 0
      #
      mask_data = mask_data / xrs.space_group().order_z()
      if(yn == 1):
        mask_data = maptbx.copy(mask_data, flex.grid(mask_data.focus()))
      #
      for site_frac in xrs_p1.sites_frac():
        mv = mask_data.value_at_closest_grid_point(site_frac)
        assert mv == 0
      #
      fc = xrs.structure_factors(d_min = d_min).f_calc()
      f_mask_1 = fc.set().array(data = asu_mask.structure_factors(fc.indices()))
      f_mask_2 = f_mask_1.structure_factors_from_map(map=mask_data,
        use_scale = True, anomalous_flag = False, use_sg = True)
      fm1 = abs(f_mask_1).data()
      fm2 = abs(f_mask_2).data()
      r = flex.sum( flex.abs( fm1 - fm2 ) ) / flex.sum( fm1 + fm2 )
      assert approx_equal(r, 0.0)
Пример #14
0
def exercise_set_box():
  n_real = (60, 100, 160)
  n = n_real[0]*n_real[1]*n_real[2]
  cs=crystal.symmetry(
    unit_cell=(21,37,58,80,111,117),
    space_group_symbol="P1")
  be = maptbx.boxes(n_real = n_real, fraction=0.1)
  #
  m1 = flex.double([-1 for i in xrange(n)])
  m1.resize(flex.grid(n_real))
  m2 = flex.double([1 for i in xrange(n)])
  m2.resize(flex.grid(n_real))
  #
  for s,e in zip(be.starts, be.ends):
    box = maptbx.copy(m2, s, e)
    box.reshape(flex.grid(box.all()))
    maptbx.set_box(
      map_data_from = box,
      map_data_to   = m1,
      start         = s,
      end           = e)
  assert m2.as_1d().min_max_mean().as_tuple() == (1.,1.,1.)
Пример #15
0
 def __init__(self,
              map_data,
              cushion,
              xray_structure=None,
              pdb_hierarchy=None,
              crystal_symmetry=None):
     # safeguards
     if (xray_structure is None):
         assert [pdb_hierarchy, crystal_symmetry].count(None) == 0
         cs = crystal_symmetry
         uc = cs.unit_cell()
         sites_frac = uc.fractionalize(pdb_hierarchy.atoms().extract_xyz())
     else:
         if (crystal_symmetry is not None):
             assert crystal_symmetry.is_similar_symmetry(
                 xray_structure.crystal_symmetry())
         if (pdb_hierarchy is not None):
             assert approx_equal(xray_structure.sites_cart(),
                                 pdb_hierarchy.atoms().extract_xyz())
         cs = xray_structure.crystal_symmetry()
         uc = cs.unit_cell()
         sites_frac = xray_structure.sites_frac()
     assert cushion >= 0
     # convert cushion into fractional vector
     cushion_frac = flex.double(uc.fractionalize((cushion, ) * 3))
     # find fractional corners
     frac_min = sites_frac.min()
     frac_max = sites_frac.max()
     frac_max = list(flex.double(frac_max) + cushion_frac)
     frac_min = list(flex.double(frac_min) - cushion_frac)
     # find corner grid nodes
     na = map_data.all()
     self.gridding_first = [ifloor(f * n) for f, n in zip(frac_min, na)]
     self.gridding_last = [iceil(f * n) for f, n in zip(frac_max, na)]
     # extract map box
     self.map_data = maptbx.copy(map_data, self.gridding_first,
                                 self.gridding_last)
     # get shift vector as result of boxing
     self.shift_frac = [
         -self.map_data.origin()[i] / na[i] for i in range(3)
     ]
     self.shift_cart = cs.unit_cell().orthogonalize(self.shift_frac)
     # this makes the box 'forget' about its parent -- the original box
     self.map_data.reshape(flex.grid(self.map_data.all()))
     # get crystal symmetry of the box
     p = uc.parameters()
     abc = [p[i] * self.map_data.all()[i] / na[i] for i in range(3)]
     box_uc = uctbx.unit_cell(parameters=(abc[0], abc[1], abc[2], p[3],
                                          p[4], p[5]))
     self.crystal_symmetry = crystal.symmetry(unit_cell=box_uc,
                                              space_group="P1")
     # new xray_structure or pdb_hierarchy
     sites_frac_new = box_uc.fractionalize(
         uc.orthogonalize(sites_frac + self.shift_frac))
     sites_cart_new = box_uc.orthogonalize(sites_frac_new)
     self.xray_structure, self.pdb_hierarchy = None, None
     if (xray_structure is not None):
         scatterers = xray_structure.scatterers().deep_copy()
         scatterers.set_sites(sites_frac_new)
         sp = crystal.special_position_settings(self.crystal_symmetry)
         self.xray_structure = xray.structure(sp, scatterers)
     if (pdb_hierarchy is not None):
         self.pdb_hierarchy = pdb_hierarchy.deep_copy()
         self.pdb_hierarchy.atoms().set_xyz(sites_cart_new)
Пример #16
0
def exercise_writer():
    from cctbx import uctbx, sgtbx
    from scitbx.array_family import flex
    mt = flex.mersenne_twister(0)
    nxyz = (
        4,
        4,
        4,
    )
    grid = flex.grid(nxyz)
    real_map_data = mt.random_double(size=grid.size_1d())
    real_map_data.reshape(grid)
    unit_cell = uctbx.unit_cell((10, 10, 10, 90, 90, 90))
    iotbx.ccp4_map.write_ccp4_map(
        file_name="four_by_four.map",
        unit_cell=unit_cell,
        space_group=sgtbx.space_group_info("P1").group(),
        map_data=real_map_data,
        labels=flex.std_string(["iotbx.ccp4_map.tst"]))
    input_real_map = iotbx.ccp4_map.map_reader(file_name="four_by_four.map")
    input_map_data = input_real_map.map_data()
    real_map_mmm = real_map_data.as_1d().min_max_mean()
    input_map_mmm = input_map_data.as_1d().min_max_mean()
    cc = flex.linear_correlation(real_map_data.as_1d(),
                                 input_map_data.as_1d()).coefficient()
    assert cc > 0.999

    assert approx_equal(input_real_map.unit_cell_parameters,
                        unit_cell.parameters())
    assert approx_equal(real_map_mmm.min, input_real_map.header_min, eps=0.001)
    assert approx_equal(real_map_mmm.min, input_map_mmm.min, eps=0.001)

    # random small maps of different sizes
    for nxyz in flex.nested_loop((2, 1, 1), (4, 4, 4)):
        mt = flex.mersenne_twister(0)
        grid = flex.grid(nxyz)
        real_map = mt.random_double(size=grid.size_1d())
        real_map = real_map - 0.5
        real_map.reshape(grid)
        iotbx.ccp4_map.write_ccp4_map(
            file_name="random.map",
            unit_cell=uctbx.unit_cell((1, 1, 1, 90, 90, 90)),
            space_group=sgtbx.space_group_info("P1").group(),
            gridding_first=(0, 0, 0),
            gridding_last=tuple(grid.last(False)),
            map_data=real_map,
            labels=flex.std_string(["iotbx.ccp4_map.tst"]))
        m = iotbx.ccp4_map.map_reader(file_name="random.map")
        mmm = flex.double(list(real_map)).min_max_mean()
        m1 = real_map.as_1d()
        m2 = m.map_data().as_1d()
        cc = flex.linear_correlation(m1, m2).coefficient()
        assert cc > 0.999
        assert approx_equal(m.unit_cell_parameters, (1, 1, 1, 90, 90, 90))
        assert approx_equal(mmm.min, m.header_min)
        assert approx_equal(mmm.max, m.header_max)
        #
        # write unit_cell_grid explicitly to map
        iotbx.ccp4_map.write_ccp4_map(
            file_name="random_b.map",
            unit_cell=uctbx.unit_cell((1, 1, 1, 90, 90, 90)),
            space_group=sgtbx.space_group_info("P1").group(),
            unit_cell_grid=real_map.all(),
            map_data=real_map,
            labels=flex.std_string(["iotbx.ccp4_map.tst"]))
        m = iotbx.ccp4_map.map_reader(file_name="random_b.map")
        m1 = real_map.as_1d()
        m2 = m.map_data().as_1d()
        cc = flex.linear_correlation(m1, m2).coefficient()
        assert cc > 0.999

        mmm = flex.double(list(real_map)).min_max_mean()
        assert approx_equal(m.unit_cell_parameters, (1, 1, 1, 90, 90, 90))
        assert approx_equal(mmm.min, m.header_min)
        assert approx_equal(mmm.max, m.header_max)
        #

        #
        gridding_first = (0, 0, 0)
        gridding_last = tuple(grid.last(False))
        map_box = maptbx.copy(real_map, gridding_first, gridding_last)
        map_box.reshape(flex.grid(map_box.all()))
        iotbx.ccp4_map.write_ccp4_map(
            file_name="random_box.map",
            unit_cell=uctbx.unit_cell((1, 1, 1, 90, 90, 90)),
            space_group=sgtbx.space_group_info("P1").group(),
            map_data=map_box,
            labels=flex.std_string(["iotbx.ccp4_map.tst"]))
    print "OK"
Пример #17
0
 def real_map_unpadded(self):
   from cctbx import maptbx
   result = self.real_map()
   if (not result.is_padded()): return result
   return maptbx.copy(result, flex.grid(result.focus()))
Пример #18
0
def run_test(space_group_info,
             n_elements=5,
             d_min=1.5,
             grid_resolution_factor=1. / 3,
             max_prime=5,
             verbose=0):
    structure = random_structure.xray_structure(space_group_info,
                                                elements=["Si"] * n_elements,
                                                volume_per_atom=200,
                                                min_distance=3.,
                                                general_positions_only=False)
    miller_set_f_obs = miller.build_set(crystal_symmetry=structure,
                                        anomalous_flag=(random.random() > 0.5),
                                        d_min=d_min)
    f_obs = miller_set_f_obs.structure_factors_from_scatterers(
        xray_structure=structure, algorithm="direct").f_calc()
    structure_factor_utils.check_phase_restrictions(f_obs, verbose=verbose)
    if (0 or verbose):
        f_obs.show_summary()
    if (0 or verbose):
        f_obs.show_array()
    fft_map = f_obs.fft_map(resolution_factor=grid_resolution_factor,
                            symmetry_flags=maptbx.use_space_group_symmetry)
    p = pickle.dumps(fft_map)
    l = pickle.loads(p)
    s1 = StringIO()
    fft_map.statistics().show_summary(f=s1)
    s2 = StringIO()
    l.statistics().show_summary(f=s2)
    assert not show_diff(s2.getvalue(), s1.getvalue())
    #
    if (not f_obs.anomalous_flag()):
        maptbx_fft_map = maptbx.fft_to_real_map_unpadded(
            space_group=fft_map.space_group(),
            n_real=fft_map.n_real(),
            miller_indices=f_obs.indices(),
            data=f_obs.data())
        fft_map_unpadded = fft_map.real_map_unpadded(in_place=False)
        assert approx_equal(
            flex.linear_correlation(fft_map_unpadded.as_1d(),
                                    maptbx_fft_map.as_1d()).coefficient(), 1)
        assert approx_equal(
            flex.max(flex.abs(maptbx_fft_map - fft_map_unpadded)), 0)
    #
    fft_map.apply_sigma_scaling()
    real_map = maptbx.copy(fft_map.real_map(),
                           flex.grid(fft_map.real_map().focus()))
    grid_tags = maptbx.grid_tags(real_map.focus())
    grid_tags.build(fft_map.space_group_info().type(),
                    fft_map.symmetry_flags())
    assert grid_tags.n_grid_misses() == 0
    assert grid_tags.verify(real_map)
    rms = []
    for interpolate in (False, True):
        peak_list = maptbx.peak_list(data=real_map,
                                     tags=grid_tags.tag_array(),
                                     peak_search_level=1,
                                     max_peaks=2 * n_elements,
                                     interpolate=interpolate)
        assert peak_list.gridding() == real_map.focus()
        check_peaks(structure, peak_list.sites(),
                    d_min * grid_resolution_factor)
        crystal_gridding_tags = fft_map.tags()
        cluster_analysis = maptbx.peak_cluster_analysis(
            peak_list=peak_list,
            special_position_settings=structure,
            general_positions_only=False,
            effective_resolution=d_min,
            min_cross_distance=2,
            max_clusters=n_elements).all()
        check_peaks(
            structure, cluster_analysis.sites(),
            cluster_analysis.min_cross_distance() +
            d_min * grid_resolution_factor)
        structure_from_peaks = xray.structure(structure)
        for site in cluster_analysis.sites():
            structure_from_peaks.add_scatterer(
                xray.scatterer(label="site", scattering_type="", site=site))
        emma_matches = emma.model_matches(structure.as_emma_model(),
                                          structure_from_peaks.as_emma_model(),
                                          tolerance=d_min * 2)
        rms.append(emma_matches.refined_matches[0].rms)
        assert len(emma_matches.refined_matches[0].pairs) == n_elements
    # exercise interpolation vs. summation
    map_coeffs = f_obs.expand_to_p1()
    fft_map = f_obs.fft_map(resolution_factor=grid_resolution_factor,
                            symmetry_flags=maptbx.use_space_group_symmetry)
    fft_map.apply_volume_scaling()
    real_map = fft_map.real_map_unpadded()
    sum1 = sum2 = 0
    for scatterer in structure.scatterers():
        v1 = real_map.eight_point_interpolation(scatterer.site)
        v2 = real_map.tricubic_interpolation(scatterer.site)
        v3 = map_coeffs.direct_summation_at_point(scatterer.site)
        sum1 += abs(v1 - v3.real)
        sum2 += abs(v2 - v3.real)
    mean_delta_linear = sum1 / n_elements
    mean_delta_cubic = sum2 / n_elements
    assert (mean_delta_cubic < mean_delta_linear)
    if (0 or verbose):
        print("emma rms grid, interpolated: %.2f %.2f" % tuple(rms))
    assert rms[0] >= rms[1]
    map_1 = fft_map.real_map_unpadded(in_place=False)
    map_2 = fft_map.real_map_unpadded(in_place=True)
    assert (map_1.all_eq(map_2))
def exercise_under_sampled(space_group_info, anomalous_flag, conjugate_flag,
                           under_sampling,
                           d_min=2., resolution_factor=0.5, max_prime=5,
                           verbose=0):
  structure_factors = random_structure.xray_structure(
    space_group_info,
    elements=("N", "C", "C", "O"),
    random_f_prime_d_min=1,
    random_f_double_prime=anomalous_flag,
    use_u_aniso=True,
    random_u_iso=True,
    random_occupancy=True
    ).structure_factors(
        anomalous_flag=anomalous_flag, d_min=d_min, algorithm="direct")
  f_calc = structure_factors.f_calc()
  n_real = maptbx.crystal_gridding(
    unit_cell=f_calc.unit_cell(),
    d_min=d_min,
    resolution_factor=resolution_factor,
    max_prime=max_prime,
    mandatory_factors=(under_sampling,)*3).n_real()
  if (not anomalous_flag):
    rfft = fftpack.real_to_complex_3d(n_real)
    n_complex = rfft.n_complex()
  else:
    cfft = fftpack.complex_to_complex_3d(n_real)
    n_complex = cfft.n()
  map = maptbx.structure_factors.to_map(
    space_group=f_calc.space_group(),
    anomalous_flag=anomalous_flag,
    miller_indices=f_calc.indices(),
    structure_factors=f_calc.data(),
    n_real=n_real,
    map_grid=flex.grid(n_complex),
    conjugate_flag=conjugate_flag)
  f_calc_p1 = f_calc.expand_to_p1()
  map_p1 = maptbx.structure_factors.to_map(
    space_group=f_calc_p1.space_group(),
    anomalous_flag=anomalous_flag,
    miller_indices=f_calc_p1.indices(),
    structure_factors=f_calc_p1.data(),
    n_real=n_real,
    map_grid=flex.grid(n_complex),
    conjugate_flag=conjugate_flag)
  assert flex.max(flex.abs(map_p1.complex_map() - map.complex_map())) < 1.e-10
  if (not anomalous_flag):
    real_map = rfft.backward(map.complex_map())
    assert real_map.all() == rfft.m_real()
  else:
    real_map = cfft.backward(map.complex_map())
    assert not real_map.is_padded()
  if (0 or verbose):
    if (not anomalous_flag):
      maptbx.statistics(real_map).show_summary()
      maptbx.statistics(real_map).show_summary()
    else:
      maptbx.statistics(flex.real(real_map)).show_summary()
      maptbx.statistics(flex.imag(real_map)).show_summary()
  n_real_under_sampled = [n//under_sampling for n in n_real]
  if (not anomalous_flag):
    rfft = fftpack.real_to_complex_3d(n_real_under_sampled)
    n_complex_under_sampled = rfft.n_complex()
  else:
    cfft = fftpack.complex_to_complex_3d(n_real_under_sampled)
    n_complex_under_sampled = cfft.n()
  under_sampled_map = maptbx.structure_factors.to_map(
    space_group=f_calc.space_group(),
    anomalous_flag=anomalous_flag,
    miller_indices=f_calc.indices(),
    structure_factors=f_calc.data(),
    n_real=n_real_under_sampled,
    map_grid=flex.grid(n_complex_under_sampled),
    conjugate_flag=conjugate_flag)
  under_sampled_map_p1 = maptbx.structure_factors.to_map(
    space_group=f_calc_p1.space_group(),
    anomalous_flag=anomalous_flag,
    miller_indices=f_calc_p1.indices(),
    structure_factors=f_calc_p1.data(),
    n_real=n_real_under_sampled,
    map_grid=flex.grid(n_complex_under_sampled),
    conjugate_flag=conjugate_flag)
  assert flex.max(flex.abs(under_sampled_map_p1.complex_map()
                         - under_sampled_map.complex_map())) < 1.e-10
  if (not anomalous_flag):
    under_sampled_map_before_fft = under_sampled_map.complex_map().deep_copy()
    under_sampled_real_map = rfft.backward(under_sampled_map.complex_map())
    assert under_sampled_real_map.all() == rfft.m_real()
  else:
    under_sampled_real_map = cfft.backward(under_sampled_map.complex_map())
    assert not under_sampled_real_map.is_padded()
  if (0 or verbose):
    if (not anomalous_flag):
      maptbx.statistics(under_sampled_real_map).show_summary()
      maptbx.statistics(under_sampled_real_map).show_summary()
    else:
      maptbx.statistics(flex.real(under_sampled_real_map)).show_summary()
      maptbx.statistics(flex.imag(under_sampled_real_map)).show_summary()
  if (0 or verbose):
    print real_map.all(), n_complex
    print under_sampled_real_map.all(), n_complex_under_sampled
  if (not anomalous_flag):
    x_source = real_map
    y_source = under_sampled_real_map
  else:
    x_source = flex.real(real_map)
    y_source = flex.real(under_sampled_real_map)
  x = flex.double()
  n = x_source.focus()
  for i in xrange(0, n[0], under_sampling):
    for j in xrange(0, n[1], under_sampling):
      for k in xrange(0, n[2], under_sampling):
        x.append(x_source[(i,j,k)])
  y = maptbx.copy(y_source, flex.grid(y_source.focus())).as_1d()
  if (0 or verbose):
    print "x:", tuple(x)
    print "y:", tuple(y)
  assert flex.max(flex.abs(x-y)) \
      < (flex.max(flex.abs(x))+flex.max(flex.abs(y)))/2*1.e-6
  if (under_sampling == 1):
    x = maptbx.copy(x_source, flex.grid(x_source.focus())).as_1d()
    c = flex.linear_correlation(x, y)
    assert c.coefficient() >= 0.9999
Пример #20
0
    def __init__(self,
                 map_manager,
                 threshold=0.05,
                 box_cushion=3.,
                 get_half_height_width=True,
                 model=None,
                 wrapping=None,
                 model_can_be_outside_bounds=False,
                 log=sys.stdout):

        self._map_manager = map_manager
        self._model = model
        self.model_can_be_outside_bounds = model_can_be_outside_bounds

        # safeguards
        assert threshold is not None
        assert box_cushion is not None
        assert isinstance(map_manager, iotbx.map_manager.map_manager)
        assert self._map_manager.map_data().accessor().origin() == (0, 0, 0)
        if self.map_manager().wrapping():
            assert map_manager.unit_cell_grid == map_manager.map_data().all()

        self._force_wrapping = wrapping
        if wrapping is None:
            wrapping = self.map_manager().wrapping()
        self.basis_for_boxing_string = 'around_density, wrapping = %s' % (
            wrapping)

        # Select box where data are positive (> threshold*max)
        map_data = map_manager.map_data()
        origin = list(map_data.origin())
        assert origin == [0, 0, 0]
        all = list(map_data.all())
        # Get max value vs x, y, z
        value_list = flex.double()
        for i in range(0, all[0]):
            new_map_data = maptbx.copy(map_data, tuple((i, 0, 0)),
                                       tuple((i, all[1], all[2])))
            value_list.append(
                new_map_data.as_1d().as_double().min_max_mean().max)
        ii = 0
        for z in value_list:
            ii += 1
        x_min, x_max = get_range(value_list,
                                 threshold=threshold,
                                 get_half_height_width=get_half_height_width)

        value_list = flex.double()
        for j in range(0, all[1]):
            new_map_data = maptbx.copy(map_data, tuple((0, j, 0)),
                                       tuple((all[0], j, all[2])))
            value_list.append(
                new_map_data.as_1d().as_double().min_max_mean().max)
        y_min, y_max = get_range(value_list,
                                 threshold=threshold,
                                 get_half_height_width=get_half_height_width)

        value_list = flex.double()
        for j in range(0, all[1]):
            new_map_data = maptbx.copy(map_data, tuple((0, j, 0)),
                                       tuple((all[0], j, all[2])))
            value_list.append(
                new_map_data.as_1d().as_double().min_max_mean().max)
        y_min, y_max = get_range(value_list,
                                 threshold=threshold,
                                 get_half_height_width=get_half_height_width)

        value_list = flex.double()
        for k in range(0, all[2]):
            new_map_data = maptbx.copy(map_data, tuple((0, 0, k)),
                                       tuple((all[0], all[1], k)))
            value_list.append(
                new_map_data.as_1d().as_double().min_max_mean().max)
        z_min, z_max = get_range(value_list,
                                 threshold=threshold,
                                 get_half_height_width=get_half_height_width)

        # Get lower and upper bounds of this region in grid units
        frac_min = (x_min, y_min, z_min)
        frac_max = (x_max, y_max, z_max)
        cs = map_manager.crystal_symmetry()
        cushion = flex.double(cs.unit_cell().fractionalize(
            (box_cushion, ) * 3))
        all_orig = map_data.all()
        self.gridding_first = [
            max(0, ifloor((f - c) * n))
            for c, f, n in zip(cushion, frac_min, all_orig)
        ]
        self.gridding_last = [
            min(n - 1, iceil((f + c) * n))
            for c, f, n in zip(cushion, frac_max, all_orig)
        ]

        # Ready with gridding...set up shifts and box crystal_symmetry
        self.set_shifts_and_crystal_symmetry()

        # Apply boxing to model, ncs, and map (if available)
        self.apply_to_model_ncs_and_map()
Пример #21
0
    def __init__(
        self,
        file_name=None,
        crystal_symmetry=None,
        unit_cell=None,
        space_group=None,
        map_data=None,
        labels=None,
        gridding_first=None,
        gridding_last=None,
        unit_cell_grid=None,
        origin_shift_grid_units=None,  # origin shift (grid units) to be applied
        external_origin=None,  # Do not use this unless required
        output_axis_order=INTERNAL_STANDARD_ORDER,
        internal_standard_order=INTERNAL_STANDARD_ORDER,
        verbose=None,
    ):

        assert map_data  # should never be called without map_data

        if map_data.is_padded():  # copied from cctbx/miller/__init__.py
            map_data = maptbx.copy(map_data, flex.grid(map_data.focus()))

        # Get unit_cell and space_group if crystal_symmetry is supplied:
        if unit_cell is None:
            assert crystal_symmetry is not None
            unit_cell = crystal_symmetry.unit_cell()

        if space_group is None:  # use P1 if not supplied
            if crystal_symmetry is not None:
                space_group = crystal_symmetry.space_group()
            else:
                from cctbx import sgtbx
                space_group = sgtbx.space_group_info("P1").group()

        # Get empty labels if not supplied
        if not labels:
            labels = create_output_labels()

        if gridding_first is not None and gridding_last is not None:

            # Writing box from gridding_first to gridding_last (inclusive)
            # NOTE: This is not a common use of this routine

            assert len(gridding_first) == 3 and len(gridding_last) == 3
            assert unit_cell_grid is None
            assert map_data.origin() == (0, 0, 0)  # unshifted maps only

            nxyz_start = gridding_first
            nxyz_end = gridding_last
            unit_cell_grid = map_data.all()

            new_map_data = maptbx.copy(map_data, tuple(nxyz_start),
                                       tuple(nxyz_end))
            #   NOTE: end point of map is nxyz_end, so size of map (new all()) is
            #   (nxyz_end-nxyz_start+ (1,1,1))
            assert tuple(new_map_data.origin()) == tuple(nxyz_start)
            assert new_map_data.all() == tuple(
                add_list((1, 1, 1), subtract_list(gridding_last,
                                                  gridding_first)))

            new_map_data = new_map_data.shift_origin(
            )  # this is the map we will pass in

        else:

            # This is the recommended way to use this writer
            # Optionally uses supplied unit_cell_grid.
            # Allows writing just a part of a map to a file, but retaining
            #   information on size of whole map if unit_cell_grid is supplied.
            # Takes origin and size of map from the map_data array
            # Optionally shifts origin based on input "origin_shift_grid_units"

            assert gridding_first is None and gridding_last is None

            if unit_cell_grid is None:
                # Assumes map_data.all() is the entire unit cell
                unit_cell_grid = map_data.all()
                # Note: if map_data.origin()==(0,0,0) this grid corresponds to the
                #  box of density that is present.
            else:
                assert len(unit_cell_grid) == 3
                # Assumes unit_cell_grid is the entire unit cell

            if origin_shift_grid_units:  # Supplied non-zero origin. Input map must be
                #   at (0,0,0).  Use supplied origin_shift_grid_units as origin
                assert map_data.origin() == (0, 0, 0)
                nxyz_start = origin_shift_grid_units
                new_map_data = map_data
            else:
                nxyz_start = map_data.origin()
                new_map_data = map_data.deep_copy().shift_origin()

        # Ready to write the map

        assert new_map_data.origin() == (0, 0, 0
                                         )  # must not be shifted at this point
        assert space_group is not None
        assert unit_cell is not None

        # Open file for writing
        mrc = mrcfile.new(file_name, overwrite=True)

        # Convert flex array to the numpy array required for mrcfile
        if hasattr(new_map_data, 'as_float'):
            numpy_data = new_map_data.as_float().as_numpy_array()
        else:  # was float
            numpy_data = new_map_data.as_numpy_array()

        # This numpy_data array is always in the order (3,2,1): columns are Z,
        #  rows are Y, sections in X.  This comes from the shape of flex arrays.

        # To write with another order, call with values for output_axis_order

        if output_axis_order != internal_standard_order:
            i_order = get_standard_order(
                output_axis_order[0],
                output_axis_order[1],
                output_axis_order[2],
                internal_standard_order=internal_standard_order,
                reverse=True)
            numpy_data_output_axis_order = np.transpose(numpy_data, i_order)
        else:
            numpy_data_output_axis_order = numpy_data
        mrc.header.mapc = output_axis_order[0]
        mrc.header.mapr = output_axis_order[1]
        mrc.header.maps = output_axis_order[2]

        mrc.set_data(numpy_data_output_axis_order)  # numpy array

        # Labels
        # Keep all limitations labels and other labels up to total of 10 or fewer
        output_labels = select_output_labels(labels)

        mrc.header.nlabl = len(output_labels)
        for i in range(min(10, len(output_labels))):
            mrc.header.label[i] = output_labels[i]
        mrc.update_header_from_data(
        )  # don't move this later as we overwrite values

        # Unit cell parameters and space group
        abc = unit_cell.parameters()[:3]
        angles = unit_cell.parameters()[3:]
        mrc.header.cella = abc
        mrc.header.cellb = angles
        space_group_number = space_group.info().type().number()
        mrc.header.ispg = space_group_number

        # Start point of the supplied map in grid units

        # nxyz_start is the origin (grid units) in XYZ coordinate system.
        # The mrc header needs the origin along columns, rows, sections which
        #  is represented here as nxstart_nystart_nzstart

        nxstart_nystart_nzstart = origin_as_crs(origin=nxyz_start,
                                                mapc=mrc.header.mapc,
                                                mapr=mrc.header.mapr,
                                                maps=mrc.header.maps)

        mrc.header.nxstart = nxstart_nystart_nzstart[0]
        mrc.header.nystart = nxstart_nystart_nzstart[1]
        mrc.header.nzstart = nxstart_nystart_nzstart[2]

        # Size of entire unit cell in grid units
        # This is ALWAYS along X,Y,Z regardless of the sectioning of the map
        # Note that this can cause confusion as mrc.header.nx may be a different
        #   axis than mrc.header.mx (mx is always X, nx is whatever axis is
        #   specified by mapc)
        mrc.header.mx = unit_cell_grid[0]
        mrc.header.my = unit_cell_grid[1]
        mrc.header.mz = unit_cell_grid[2]

        # External origin
        # NOTE: External origin should rarely be used.  It is a poorly-defined
        #   element that refers to the position of a non-defined external model
        #   (PDB file).   For origin purposes use instead "origin".

        if external_origin is None:
            external_origin = (
                0.,
                0.,
                0.,
            )
        # This also is ALWAYS along X,Y,Z regardless of the sectioning of the map
        mrc.header.origin.x = external_origin[0]
        mrc.header.origin.y = external_origin[1]
        mrc.header.origin.z = external_origin[2]

        # Update header
        mrc.update_header_stats()

        if verbose:
            mrc.print_header()

        # Write the file
        mrc.close()
Пример #22
0
 def mask_data_whole_uc(self):
     mask_data = self.asu_mask.mask_data_whole_uc() / \
       self.xray_structure.space_group().order_z()
     return maptbx.copy(mask_data, flex.grid(mask_data.focus()))
Пример #23
0
    def apply_to_map(self, map_manager):
        '''
     Apply boxing to a map_manager that is similar to the one used to generate
       this around_model object

     Also apply to its ncs_object, if any

    '''
        assert isinstance(map_manager, iotbx.map_manager.map_manager)

        # This one should just have similar unit_cell_crystal_symmetry
        # crystal_symmetry should match self.map_crystal_symmetry_at_initialization

        assert map_manager.unit_cell_crystal_symmetry().is_similar_symmetry(
            self._map_manager.unit_cell_crystal_symmetry())
        assert map_manager.crystal_symmetry().is_similar_symmetry(
            self.map_crystal_symmetry_at_initialization)

        ma1 = map_manager.map_data().accessor()
        ma2 = self.accessor_at_initialization
        assert ma1.all() == ma2.all()
        assert ma1.origin() == ma2.origin()
        assert ma1.focus() == ma2.focus()
        map_data = map_manager.map_data()
        # Check if map is all valid
        bounds_info = get_bounds_of_valid_region(map_data, self.gridding_first,
                                                 self.gridding_last)

        # Allow override of wrapping
        if isinstance(self._force_wrapping, bool):
            wrapping = self._force_wrapping
        else:
            # Get wrapping from map_manager. If it is not defined and
            #  bounds are outside allowed, try to get the wrapping
            wrapping = map_manager.wrapping()

        if wrapping or bounds_info.inside_allowed_bounds:
            # Just copy everything
            map_box = maptbx.copy(map_data, self.gridding_first,
                                  self.gridding_last)
            # Note: map_box gridding is self.gridding_first to self.gridding_last
        elif not bounds_info.some_valid_points:
            # No valid points, Just copy everything and zero
            map_box = maptbx.copy(map_data, self.gridding_first,
                                  self.gridding_last)
            map_box = map_box * 0.
            self._warning_message += "\nWARNING: boxed map is entirely outside map"+\
               " and wrapping=%s\n...setting all values to zero" %(wrapping)

        else:  # Need to copy and then zero outside of defined region
            map_box = copy_and_zero_map_outside_bounds(map_data, bounds_info)
            self._warning_message += \
                  "\nWARNING: boxed map goes outside original map"+\
               " and wrapping=%s\n...setting unknown values to zero" %(wrapping)
        #  Now reshape map_box to put origin at (0, 0, 0)
        map_box.reshape(flex.grid(self.box_all))

        # Create new map_manager object:
        #   Use original values for:
        #     unit_cell_grid    (gridding of original full unit cell)
        #     unit_cell_crystal_symmetry  (symmetry of original full unit cell)
        #     input_file_name
        #   Use new (boxed) values for:
        #     map_data
        #     crystal_symmetry   (symmetry of the part of the map that is present)
        #   Update:
        #     origin_shift_grid_units  (position in the original map of the
        #                                 (0, 0, 0) grid point in map_box)
        #     labels  (add label specifying boxing operation)
        #
        # New origin_shift_grid_units:
        origin_shift_grid_units = [
            self.gridding_first[i] + map_manager.origin_shift_grid_units[i]
            for i in range(3)
        ]
        # New labels:
        new_label = "Boxed %s to %s %s" % (str(tuple(
            self.gridding_first)), str(tuple(
                self.gridding_last)), self.basis_for_boxing_string)
        #  Set up new map_manager. This will contain new data and not overwrite
        #   original
        #  NOTE: origin_shift_grid_units is required as bounds have changed
        new_map_manager = map_manager.customized_copy(
            map_data=map_box, origin_shift_grid_units=origin_shift_grid_units)
        if self._force_wrapping:
            # Set the wrapping of the new map if it is possible
            if (self._force_wrapping and (new_map_manager.is_full_size())) or \
             ( (not self._force_wrapping) and (not new_map_manager.is_full_size())):
                new_map_manager.set_wrapping(self._force_wrapping)

        # Add the label
        new_map_manager.add_label(new_label)
        return new_map_manager
Пример #24
0
def run(single_side=False, quick=True):
    result = flex.bool()
    periodic_list = []
    aperiodic_list = []
    if quick:
        rf_list = [0.33]
    else:
        rf_list = [
            0.33,
            0.48,
        ]
    for resolution_factor in rf_list:
        for sgn in range(1, 231):
            kk = random.randint(1, 99999)
            random.seed(kk)
            flex.set_random_seed(kk)
            group = space_group_info(sgn)
            xrs = random_structure.xray_structure(
                space_group_info=group,
                volume_per_atom=25.,
                general_positions_only=False,
                elements=('C', 'N', 'O', 'H') * 30,
                min_distance=1.0)
            sgt = xrs.space_group().type()
            fc = xrs.structure_factors(d_min=2).f_calc()
            fft_map = fc.fft_map(
                symmetry_flags=maptbx.use_space_group_symmetry,
                resolution_factor=resolution_factor)
            map_data = fft_map.real_map_unpadded()
            xx = is_periodic(map_data)
            periodic_list.append(xx)
            assert xx in [True, None]

            # Now cut off edges and should not work:
            min_size = int(flex.double(map_data.all()).min_max_mean().min)
            assert min_size >= 12
            if quick:
                range_to_use = [1]
                k_list = [1]
            else:
                range_to_use = list(
                    range(1, max(2, min(10, 1 + min_size // 12))))
                k_list = [0, 1]
            for i in range_to_use:
                for k in k_list:
                    if single_side:
                        lower_bounds = (k, k, k)
                        upper_bounds = tuple(
                            [x - i - 1 for x in map_data.all()])
                    else:
                        lower_bounds = (k, k, k)
                        upper_bounds = (map_data.all()[0] - i - 1,
                                        map_data.all()[1], map_data.all()[2])

                    map_box = maptbx.copy(map_data, lower_bounds, upper_bounds)
                    new_map_box = map_box.shift_origin()
                    xx = is_periodic(new_map_box)
                    aperiodic_list.append(xx)
                    assert xx in [False, None]

    print("For periodic.. True:", periodic_list.count(True), "None:",
          periodic_list.count(None), " False:", periodic_list.count(False))
    print("For aperiodic.. True:", aperiodic_list.count(True), "None:",
          aperiodic_list.count(None), " False:", aperiodic_list.count(False))
    assert periodic_list.count(False) == 0
    assert periodic_list.count(None) <= 0.05 * len(periodic_list)
    assert aperiodic_list.count(True) == 0
    assert aperiodic_list.count(None) <= 0.05 * len(aperiodic_list)
Пример #25
0
def exercise_writer(use_mrcfile=None,output_axis_order=[3,2,1]):
  from cctbx import uctbx, sgtbx
  from scitbx.array_family import flex
  mt = flex.mersenne_twister(0)
  nxyz = (4,5,6,)
  grid = flex.grid(nxyz)
  real_map_data = mt.random_double(size=grid.size_1d())
  real_map_data.reshape(grid)
  unit_cell=uctbx.unit_cell((10,10,10,90,90,90))

  if use_mrcfile:
    from iotbx.mrcfile import create_output_labels
    labels=create_output_labels(
       program_name='test',
       limitations=['extract_unique'],
       output_labels=['test label'],
       )
    iotbx.mrcfile.write_ccp4_map(
      file_name="four_five_six.mrc",
      unit_cell=unit_cell,
      space_group=sgtbx.space_group_info("P1").group(),
      map_data=real_map_data,
      labels=labels,
      output_axis_order=output_axis_order)
    input_real_map = iotbx.mrcfile.map_reader(file_name="four_five_six.mrc")
    for x in input_real_map.labels:
      print("LABEL: ",x)
    assert str(input_real_map.labels).find('extract_unique')>-1
  else:
    iotbx.ccp4_map.write_ccp4_map(
      file_name="four_five_six.map",
      unit_cell=unit_cell,
      space_group=sgtbx.space_group_info("P1").group(),
      map_data=real_map_data,
      labels=flex.std_string(["iotbx.ccp4_map.tst"]))
    input_real_map = iotbx.ccp4_map.map_reader(file_name="four_five_six.map")

  input_map_data=input_real_map.map_data()
  real_map_mmm = real_map_data.as_1d().min_max_mean()
  input_map_mmm = input_map_data.as_double().as_1d().min_max_mean()
  cc=flex.linear_correlation(real_map_data.as_1d(),input_map_data.as_double().as_1d()).coefficient()
  assert cc > 0.999
  print("\nMRCFILE with 4x5x6 map and axis order %s %s" %(output_axis_order,cc))

  assert approx_equal(input_real_map.unit_cell().parameters(), unit_cell.parameters())
  assert approx_equal(real_map_mmm.min, input_real_map.header_min,eps=0.001)
  assert approx_equal(real_map_mmm.min, input_map_mmm.min,eps=0.001)


  # random small maps of different sizes
  for nxyz in flex.nested_loop((2,1,1),(4,4,4)):
    mt = flex.mersenne_twister(0)
    grid = flex.grid(nxyz)
    real_map = mt.random_double(size=grid.size_1d())
    real_map=real_map-0.5
    real_map.reshape(grid)
    if use_mrcfile:
      iotbx.mrcfile.write_ccp4_map(
        file_name="random.mrc",
        unit_cell=uctbx.unit_cell((1,1,1,90,90,90)),
        space_group=sgtbx.space_group_info("P1").group(),
        gridding_first=(0,0,0),
        gridding_last=tuple(grid.last(False)),
        map_data=real_map,
        labels=flex.std_string(["iotbx.ccp4_map.tst"]))
      m = iotbx.mrcfile.map_reader(file_name="random.mrc")
    else:
      iotbx.ccp4_map.write_ccp4_map(
        file_name="random.map",
        unit_cell=uctbx.unit_cell((1,1,1,90,90,90)),
        space_group=sgtbx.space_group_info("P1").group(),
        gridding_first=(0,0,0),
        gridding_last=tuple(grid.last(False)),
        map_data=real_map,
        labels=flex.std_string(["iotbx.ccp4_map.tst"]))
      m = iotbx.ccp4_map.map_reader(file_name="random.map")

    mmm = flex.double(list(real_map)).min_max_mean()
    m1=real_map.as_1d()
    m2=m.map_data().as_double().as_1d()
    cc=flex.linear_correlation(m1,m2).coefficient()
    assert cc > 0.999
    assert approx_equal(m.unit_cell().parameters(), (1,1,1,90,90,90))
    assert approx_equal(mmm.min, m.header_min)
    assert approx_equal(mmm.max, m.header_max)
    #
    # write unit_cell_grid explicitly to map
    iotbx.ccp4_map.write_ccp4_map(
      file_name="random_b.map",
      unit_cell=uctbx.unit_cell((1,1,1,90,90,90)),
      space_group=sgtbx.space_group_info("P1").group(),
      unit_cell_grid=real_map.all(),
      map_data=real_map,
      labels=flex.std_string(["iotbx.ccp4_map.tst"]))
    m = iotbx.ccp4_map.map_reader(file_name="random_b.map")
    m1=real_map.as_1d()
    m2=m.map_data().as_double().as_1d()
    cc=flex.linear_correlation(m1,m2).coefficient()
    assert cc > 0.999

    mmm = flex.double(list(real_map)).min_max_mean()
    assert approx_equal(m.unit_cell_parameters, (1,1,1,90,90,90))
    assert approx_equal(mmm.min, m.header_min)
    assert approx_equal(mmm.max, m.header_max)
    #

    #
    gridding_first = (0,0,0)
    gridding_last=tuple(grid.last(False))
    map_box = maptbx.copy(real_map, gridding_first, gridding_last)
    map_box.reshape(flex.grid(map_box.all()))

    if use_mrcfile:
      iotbx.mrcfile.write_ccp4_map(
        file_name="random_box.mrc",
        unit_cell=uctbx.unit_cell((1,1,1,90,90,90)),
        space_group=sgtbx.space_group_info("P1").group(),
        map_data=map_box,
        labels=flex.std_string(["iotbx.ccp4_map.tst"]))
    else:
      iotbx.ccp4_map.write_ccp4_map(
        file_name="random_box.map",
        unit_cell=uctbx.unit_cell((1,1,1,90,90,90)),
        space_group=sgtbx.space_group_info("P1").group(),
        map_data=map_box,
        labels=flex.std_string(["iotbx.ccp4_map.tst"]))
  print("OK")
Пример #26
0
 def mask_data_whole_uc(self):
   mask_data = self.asu_mask.mask_data_whole_uc() / \
     self.xray_structure.space_group().order_z()
   return maptbx.copy(mask_data, flex.grid(mask_data.focus()))
def exercise_real_space_refinement(verbose):
  if (verbose):
    out = sys.stdout
  else:
    out = StringIO()
  out_of_bounds_clamp = maptbx.out_of_bounds_clamp(0)
  out_of_bounds_raise = maptbx.out_of_bounds_raise()
  crystal_symmetry = crystal.symmetry(
    unit_cell=(10,10,10,90,90,90),
    space_group_symbol="P 1")
  xray_structure = xray.structure(
    crystal_symmetry=crystal_symmetry,
    scatterers=flex.xray_scatterer([
      xray.scatterer(label="C", site=(0,0,0))]))
  miller_set = miller.build_set(
    crystal_symmetry=crystal_symmetry,
    anomalous_flag=False,
    d_min=1)
  f_calc = miller_set.structure_factors_from_scatterers(
    xray_structure=xray_structure).f_calc()
  fft_map = f_calc.fft_map()
  fft_map.apply_sigma_scaling()
  real_map = fft_map.real_map_unpadded()
  #### unit_cell test
  delta_h = .005
  basic_map = maptbx.basic_map(
    maptbx.basic_map_unit_cell_flag(),
    real_map,
    real_map.focus(),
    crystal_symmetry.unit_cell().orthogonalization_matrix(),
    out_of_bounds_clamp.as_handle(),
    crystal_symmetry.unit_cell())
  testing_function_for_rsfit(basic_map,delta_h,xray_structure,out)
  ### non_symmetric test
  #
  minfrac = crystal_symmetry.unit_cell().fractionalize((-5,-5,-5))
  maxfrac = crystal_symmetry.unit_cell().fractionalize((5,5,5))
  gridding_first = [ifloor(n*b) for n,b in zip(fft_map.n_real(), minfrac)]
  gridding_last = [iceil(n*b) for n,b in zip(fft_map.n_real(), maxfrac)]
  data=maptbx.copy(real_map, gridding_first, gridding_last)
  #
  basic_map = maptbx.basic_map(
    maptbx.basic_map_non_symmetric_flag(),
    data,
    fft_map.n_real(),
    crystal_symmetry.unit_cell().orthogonalization_matrix(),
    out_of_bounds_clamp.as_handle(),
    crystal_symmetry.unit_cell())
  testing_function_for_rsfit(basic_map,delta_h,xray_structure,out)
  ### asu test
  #
  minfrac = crystal_symmetry.unit_cell().fractionalize((0,0,0))
  maxfrac = crystal_symmetry.unit_cell().fractionalize((10,10,10))
  gridding_first = [ifloor(n*b) for n,b in zip(fft_map.n_real(), minfrac)]
  gridding_last = [iceil(n*b) for n,b in zip(fft_map.n_real(), maxfrac)]
  data=maptbx.copy(real_map, gridding_first, gridding_last)
  #
  basic_map = maptbx.basic_map(
    maptbx.basic_map_asu_flag(),
    data,
    crystal_symmetry.space_group(),
    crystal_symmetry.direct_space_asu().as_float_asu(),
    real_map.focus(),
    crystal_symmetry.unit_cell().orthogonalization_matrix(),
    out_of_bounds_clamp.as_handle(),
    crystal_symmetry.unit_cell(),
    0.5,
    True)
  testing_function_for_rsfit(basic_map,delta_h,xray_structure,out)
Пример #28
0
    def __init__(self,
                 map_manager,
                 wrapping,
                 threshold=0.05,
                 get_half_height_width=True,
                 log=sys.stdout):
        adopt_init_args(self, locals())

        # safeguards
        assert threshold is not None
        assert isinstance(wrapping, bool)
        assert isinstance(map_manager, iotbx.map_manager.map_manager)
        assert self.map_manager.map_data().accessor().origin() == (0, 0, 0)
        if wrapping:
            assert map_manager.unit_cell_grid == map_manager.map_data().all()

        self.basis_for_boxing_string = 'around_density, wrapping=%s' % (
            wrapping)

        # Select box where data are positive (> threshold*max)
        map_data = map_manager.map_data()
        origin = list(map_data.origin())
        assert origin == [0, 0, 0]
        all = list(map_data.all())
        # Get max value vs x,y,z
        value_list = flex.double()
        for i in range(0, all[0]):
            new_map_data = maptbx.copy(map_data, tuple((i, 0, 0)),
                                       tuple((i, all[1], all[2])))
            value_list.append(
                new_map_data.as_1d().as_double().min_max_mean().max)
        ii = 0
        for z in value_list:
            ii += 1
        x_min, x_max = get_range(value_list,
                                 threshold=threshold,
                                 get_half_height_width=get_half_height_width)

        value_list = flex.double()
        for j in range(0, all[1]):
            new_map_data = maptbx.copy(map_data, tuple((0, j, 0)),
                                       tuple((all[0], j, all[2])))
            value_list.append(
                new_map_data.as_1d().as_double().min_max_mean().max)
        y_min, y_max = get_range(value_list,
                                 threshold=threshold,
                                 get_half_height_width=get_half_height_width)

        value_list = flex.double()
        for j in range(0, all[1]):
            new_map_data = maptbx.copy(map_data, tuple((0, j, 0)),
                                       tuple((all[0], j, all[2])))
            value_list.append(
                new_map_data.as_1d().as_double().min_max_mean().max)
        y_min, y_max = get_range(value_list,
                                 threshold=threshold,
                                 get_half_height_width=get_half_height_width)

        value_list = flex.double()
        for k in range(0, all[2]):
            new_map_data = maptbx.copy(map_data, tuple((0, 0, k)),
                                       tuple((all[0], all[1], k)))
            value_list.append(
                new_map_data.as_1d().as_double().min_max_mean().max)
        z_min, z_max = get_range(value_list,
                                 threshold=threshold,
                                 get_half_height_width=get_half_height_width)

        # Get lower and upper bounds of this region in grid units
        frac_min = (x_min, y_min, z_min)
        frac_max = (x_max, y_max, z_max)
        all_orig = map_data.all()
        self.gridding_first = [
            ifloor(f * n) for f, n in zip(frac_min, all_orig)
        ]
        self.gridding_last = [iceil(f * n) for f, n in zip(frac_max, all_orig)]

        # Ready with gridding...set up shifts and box crystal_symmetry
        self.set_shifts_and_crystal_symmetry()

        # Apply to map_manager so that self.map_manager is boxed version

        self.map_manager = self.apply_to_map(self.map_manager)
def exercise_under_sampled(space_group_info,
                           anomalous_flag,
                           conjugate_flag,
                           under_sampling,
                           d_min=2.,
                           resolution_factor=0.5,
                           max_prime=5,
                           verbose=0):
    structure_factors = random_structure.xray_structure(
        space_group_info,
        elements=("N", "C", "C", "O"),
        random_f_prime_d_min=1,
        random_f_double_prime=anomalous_flag,
        use_u_aniso=True,
        random_u_iso=True,
        random_occupancy=True).structure_factors(anomalous_flag=anomalous_flag,
                                                 d_min=d_min,
                                                 algorithm="direct")
    f_calc = structure_factors.f_calc()
    n_real = maptbx.crystal_gridding(unit_cell=f_calc.unit_cell(),
                                     d_min=d_min,
                                     resolution_factor=resolution_factor,
                                     max_prime=max_prime,
                                     mandatory_factors=(under_sampling, ) *
                                     3).n_real()
    if (not anomalous_flag):
        rfft = fftpack.real_to_complex_3d(n_real)
        n_complex = rfft.n_complex()
    else:
        cfft = fftpack.complex_to_complex_3d(n_real)
        n_complex = cfft.n()
    map = maptbx.structure_factors.to_map(space_group=f_calc.space_group(),
                                          anomalous_flag=anomalous_flag,
                                          miller_indices=f_calc.indices(),
                                          structure_factors=f_calc.data(),
                                          n_real=n_real,
                                          map_grid=flex.grid(n_complex),
                                          conjugate_flag=conjugate_flag)
    f_calc_p1 = f_calc.expand_to_p1()
    map_p1 = maptbx.structure_factors.to_map(
        space_group=f_calc_p1.space_group(),
        anomalous_flag=anomalous_flag,
        miller_indices=f_calc_p1.indices(),
        structure_factors=f_calc_p1.data(),
        n_real=n_real,
        map_grid=flex.grid(n_complex),
        conjugate_flag=conjugate_flag)
    assert flex.max(
        flex.abs(map_p1.complex_map() - map.complex_map())) < 1.e-10
    if (not anomalous_flag):
        real_map = rfft.backward(map.complex_map())
        assert real_map.all() == rfft.m_real()
    else:
        real_map = cfft.backward(map.complex_map())
        assert not real_map.is_padded()
    if (0 or verbose):
        if (not anomalous_flag):
            maptbx.statistics(real_map).show_summary()
            maptbx.statistics(real_map).show_summary()
        else:
            maptbx.statistics(flex.real(real_map)).show_summary()
            maptbx.statistics(flex.imag(real_map)).show_summary()
    n_real_under_sampled = [n // under_sampling for n in n_real]
    if (not anomalous_flag):
        rfft = fftpack.real_to_complex_3d(n_real_under_sampled)
        n_complex_under_sampled = rfft.n_complex()
    else:
        cfft = fftpack.complex_to_complex_3d(n_real_under_sampled)
        n_complex_under_sampled = cfft.n()
    under_sampled_map = maptbx.structure_factors.to_map(
        space_group=f_calc.space_group(),
        anomalous_flag=anomalous_flag,
        miller_indices=f_calc.indices(),
        structure_factors=f_calc.data(),
        n_real=n_real_under_sampled,
        map_grid=flex.grid(n_complex_under_sampled),
        conjugate_flag=conjugate_flag)
    under_sampled_map_p1 = maptbx.structure_factors.to_map(
        space_group=f_calc_p1.space_group(),
        anomalous_flag=anomalous_flag,
        miller_indices=f_calc_p1.indices(),
        structure_factors=f_calc_p1.data(),
        n_real=n_real_under_sampled,
        map_grid=flex.grid(n_complex_under_sampled),
        conjugate_flag=conjugate_flag)
    assert flex.max(
        flex.abs(under_sampled_map_p1.complex_map() -
                 under_sampled_map.complex_map())) < 1.e-10
    if (not anomalous_flag):
        under_sampled_map_before_fft = under_sampled_map.complex_map(
        ).deep_copy()
        under_sampled_real_map = rfft.backward(under_sampled_map.complex_map())
        assert under_sampled_real_map.all() == rfft.m_real()
    else:
        under_sampled_real_map = cfft.backward(under_sampled_map.complex_map())
        assert not under_sampled_real_map.is_padded()
    if (0 or verbose):
        if (not anomalous_flag):
            maptbx.statistics(under_sampled_real_map).show_summary()
            maptbx.statistics(under_sampled_real_map).show_summary()
        else:
            maptbx.statistics(flex.real(under_sampled_real_map)).show_summary()
            maptbx.statistics(flex.imag(under_sampled_real_map)).show_summary()
    if (0 or verbose):
        print(real_map.all(), n_complex)
        print(under_sampled_real_map.all(), n_complex_under_sampled)
    if (not anomalous_flag):
        x_source = real_map
        y_source = under_sampled_real_map
    else:
        x_source = flex.real(real_map)
        y_source = flex.real(under_sampled_real_map)
    x = flex.double()
    n = x_source.focus()
    for i in range(0, n[0], under_sampling):
        for j in range(0, n[1], under_sampling):
            for k in range(0, n[2], under_sampling):
                x.append(x_source[(i, j, k)])
    y = maptbx.copy(y_source, flex.grid(y_source.focus())).as_1d()
    if (0 or verbose):
        print("x:", tuple(x))
        print("y:", tuple(y))
    assert flex.max(flex.abs(x-y)) \
        < (flex.max(flex.abs(x))+flex.max(flex.abs(y)))/2*1.e-6
    if (under_sampling == 1):
        x = maptbx.copy(x_source, flex.grid(x_source.focus())).as_1d()
        c = flex.linear_correlation(x, y)
        assert c.coefficient() >= 0.9999
Пример #30
0
def exercise_copy():
  for flex_type in flex_types():
    m = flex_type((1,2,3,4))
    m.resize(flex.grid(2,2))
    c = maptbx.copy(map=m, result_grid=m.accessor())
    assert tuple(m) == tuple(c)
    c = maptbx.copy(map=m, result_grid=flex.grid(2,3).set_focus(2,2))
    assert approx_equal(tuple(c), (1,2,0,3,4,0))
    n = maptbx.copy(c, result_grid=m.accessor())
    assert approx_equal(tuple(m), tuple(n))
    c = maptbx.copy(m, flex.grid(3,2).set_focus(2,2))
    assert approx_equal(tuple(c), (1,2,3,4,0,0))
    n = maptbx.copy(c, m.accessor())
    assert approx_equal(tuple(m), tuple(n))
    m = flex_type((1,2,3,4,5,6))
    m.resize(flex.grid((1,2),(3,5)))
    c = maptbx.copy(m, m.accessor())
    assert approx_equal(tuple(m), tuple(c))
    c = maptbx.copy(m, flex.grid((1,2),(3,6)).set_focus(3,5))
    assert approx_equal(tuple(c), (1,2,3,0,4,5,6,0))
    n = maptbx.copy(c, m.accessor())
    assert approx_equal(tuple(m), tuple(n))
    c = maptbx.copy(m, flex.grid((1,2),(4,5)).set_focus(3,5))
    assert approx_equal(tuple(c), (1,2,3,4,5,6,0,0,0))
    n = maptbx.copy(c, m.accessor())
    assert approx_equal(tuple(m), tuple(n))
    #
    m = flex_type()
    for i in xrange(2):
      for j in xrange(3):
        for k in xrange(5):
          m.append(i*100+j*10+k)
    m.resize(flex.grid(2,3,5).set_focus((2,3,4)))
    for i in xrange(-5,5):
      for j in xrange(-5,5):
        for k in xrange(-5,5):
          c = maptbx.copy(map_unit_cell=m, first=(i,j,k), last=(i,j,k))
          assert c.size() == 1
          assert c[(i,j,k)] == m[(i%2,j%3,k%4)]
    c = maptbx.copy(map_unit_cell=m, first=(-1,1,-2), last=(1,2,0))
    assert list(c) == [112, 113, 110, 122, 123, 120,  12,  13,  10,
                        22,  23,  20, 112, 113, 110, 122, 123, 120]
    #
    m2 = m.deep_copy()
    grid = flex.grid( (-1,-1,-1), (1,2,4) ).set_focus( (1,2,3) )
    m2.resize(grid)
    for i in xrange(-1,1):
      for j in xrange(-1,2):
        for k in xrange(-1,3):
          # aperiodic copy
          c = maptbx.copy_box(map=m2, first=(i,j,k), last=(i,j,k))
          assert c.size() == 1
          ind = ((i+1)%2-1,(j+1)%3-1,(k+1)%4-1)
          assert c[(i,j,k)] == m2[ind]
    c = maptbx.copy_box(map=m2, first=(-1,0,-1), last=(0,1,2))
    assert list(c) == [10, 11, 12, 13, 20, 21,  22,  23,  110,
                       111, 112, 113, 120, 121, 122, 123]
    #
    for n0 in xrange(4):
      for n1 in xrange(4):
        for n2 in xrange(4):
          for d2 in xrange(3):
            g = flex.grid((n0,n1,n2+d2)).set_focus((n0,n1,n2))
            map1 = flex_type(range(1,1+g.size_1d()))
            map1.resize(g)
            map2 = map1.deep_copy()
            maptbx.unpad_in_place(map=map2)
            assert map2.all() == (n0,n1,n2)
            assert not map2.is_padded()
            if (n0*n1*n2 != 0):
              for i in flex.nested_loop((n0,n1,n2)):
                assert map2[i] == map1[i]
    n0,n1,n2,d2 = 2,3,4,1
    g = flex.grid((n0,n1,n2+d2)).set_focus((n0,n1,n2))
    map1 = flex_type(range(1,1+g.size_1d()))
    map1.resize(g)
    map2 = map1.deep_copy()
    maptbx.unpad_in_place(map=map2)
    assert map2.all() == (n0,n1,n2)
    assert not map2.is_padded()
    assert list(map2) == [
       1, 2, 3, 4,
       6, 7, 8, 9,
      11,12,13,14,
      16,17,18,19,
      21,22,23,24,
      26,27,28,29]
Пример #31
0
def run_test(space_group_info, n_elements=5, d_min=1.5,
             grid_resolution_factor=1./3, max_prime=5, verbose=0):
  structure = random_structure.xray_structure(
    space_group_info,
    elements=["Si"]*n_elements,
    volume_per_atom=200,
    min_distance=3.,
    general_positions_only=False)
  miller_set_f_obs = miller.build_set(
    crystal_symmetry=structure,
    anomalous_flag=(random.random()>0.5),
    d_min=d_min)
  f_obs = miller_set_f_obs.structure_factors_from_scatterers(
    xray_structure=structure,
    algorithm="direct").f_calc()
  structure_factor_utils.check_phase_restrictions(f_obs, verbose=verbose)
  if (0 or verbose):
    f_obs.show_summary()
  if (0 or verbose):
    f_obs.show_array()
  fft_map = f_obs.fft_map(
    resolution_factor=grid_resolution_factor,
    symmetry_flags=maptbx.use_space_group_symmetry)
  p = pickle.dumps(fft_map)
  l = pickle.loads(p)
  s1 = StringIO()
  fft_map.statistics().show_summary(f=s1)
  s2 = StringIO()
  l.statistics().show_summary(f=s2)
  assert not show_diff(s2.getvalue(), s1.getvalue())
  #
  if (not f_obs.anomalous_flag()):
    maptbx_fft_map = maptbx.fft_to_real_map_unpadded(
      space_group=fft_map.space_group(),
      n_real=fft_map.n_real(),
      miller_indices=f_obs.indices(),
      data=f_obs.data())
    fft_map_unpadded = fft_map.real_map_unpadded(in_place=False)
    assert approx_equal(
      flex.linear_correlation(
        fft_map_unpadded.as_1d(), maptbx_fft_map.as_1d()).coefficient(), 1)
    assert approx_equal(
      flex.max(flex.abs(maptbx_fft_map - fft_map_unpadded)), 0)
  #
  fft_map.apply_sigma_scaling()
  real_map = maptbx.copy(
    fft_map.real_map(),
    flex.grid(fft_map.real_map().focus()))
  grid_tags = maptbx.grid_tags(real_map.focus())
  grid_tags.build(
    fft_map.space_group_info().type(),
    fft_map.symmetry_flags())
  assert grid_tags.n_grid_misses() == 0
  assert grid_tags.verify(real_map)
  rms = []
  for interpolate in (False,True):
    peak_list = maptbx.peak_list(
      data=real_map,
      tags=grid_tags.tag_array(),
      peak_search_level=1,
      max_peaks=2*n_elements,
      interpolate=interpolate)
    assert peak_list.gridding() == real_map.focus()
    check_peaks(structure, peak_list.sites(), d_min * grid_resolution_factor)
    crystal_gridding_tags = fft_map.tags()
    cluster_analysis = maptbx.peak_cluster_analysis(
      peak_list=peak_list,
      special_position_settings=structure,
      general_positions_only=False,
      effective_resolution=d_min,
      min_cross_distance=2,
      max_clusters=n_elements).all()
    check_peaks(
      structure,
      cluster_analysis.sites(),
      cluster_analysis.min_cross_distance() + d_min * grid_resolution_factor)
    structure_from_peaks = xray.structure(structure)
    for site in cluster_analysis.sites():
      structure_from_peaks.add_scatterer(
        xray.scatterer(label="site", scattering_type="", site=site))
    emma_matches = emma.model_matches(
      structure.as_emma_model(),
      structure_from_peaks.as_emma_model(),
      tolerance=d_min*2)
    rms.append(emma_matches.refined_matches[0].rms)
    assert len(emma_matches.refined_matches[0].pairs) == n_elements
  # exercise interpolation vs. summation
  map_coeffs = f_obs.expand_to_p1()
  fft_map = f_obs.fft_map(
    resolution_factor=grid_resolution_factor,
    symmetry_flags=maptbx.use_space_group_symmetry)
  fft_map.apply_volume_scaling()
  real_map = fft_map.real_map_unpadded()
  sum1 = sum2 = 0
  for scatterer in structure.scatterers() :
    v1 = real_map.eight_point_interpolation(scatterer.site)
    v2 = real_map.tricubic_interpolation(scatterer.site)
    v3 = map_coeffs.direct_summation_at_point(scatterer.site)
    sum1 += abs(v1 - v3.real)
    sum2 += abs(v2 - v3.real)
  mean_delta_linear = sum1 / n_elements
  mean_delta_cubic = sum2 / n_elements
  assert (mean_delta_cubic < mean_delta_linear)
  if (0 or verbose):
    print "emma rms grid, interpolated: %.2f %.2f" % tuple(rms)
  assert rms[0] >= rms[1]
  map_1 = fft_map.real_map_unpadded(in_place=False)
  map_2 = fft_map.real_map_unpadded(in_place=True)
  assert (map_1.all_eq(map_2))
 def __init__(
       self,
       crystal_gridding,
       fmodel,
       map_type,
       max_boxes,
       box_size_as_fraction=None):
   sgt = fmodel.f_obs().space_group().type()
   assert sgt.number() == 1
   def get_map(fmodel, map_type, external_complete_set=None):
     f_map = fmodel.electron_density_map().map_coefficients(
         map_type     = map_type,
         isotropize   = True,
         fill_missing = False)
     fft_map = miller.fft_map(
       crystal_gridding     = crystal_gridding,
       fourier_coefficients = f_map)
     return fft_map.real_map_unpadded()
   f_model = fmodel.f_model_scaled_with_k1()
   fft_map = miller.fft_map(
     crystal_gridding     = crystal_gridding,
     fourier_coefficients = f_model)
   f_model_map_data = fft_map.real_map_unpadded()
   zero_complex_ma = f_model.customized_copy(
     data = flex.complex_double(f_model.data().size(), 0))
   b = maptbx.boxes(
     fraction = 0.3,
     n_real   = f_model_map_data.focus(),
     max_boxes=max_boxes,
     log=sys.stdout)
   self.map_result = flex.double(flex.grid(b.n_real))
   self.r = flex.double()
   for s,e in zip(b.starts, b.ends):
     f_model_map_data_omit = maptbx.set_box_copy(
       value         = 0,
       map_data_to   = f_model_map_data,
       start         = s,
       end           = e)
     f_model_omit = f_model.structure_factors_from_map(
       map            = f_model_map_data_omit,
       use_scale      = True,
       anomalous_flag = False,
       use_sg         = False)
     fmodel_ = mmtbx.f_model.manager(
       f_obs        = fmodel.f_obs(),
       r_free_flags = fmodel.r_free_flags(),
       f_calc       = f_model_omit,
       f_mask       = zero_complex_ma)
     self.r.append(fmodel_.r_work())
     f_map_data = get_map(fmodel=fmodel_, map_type=map_type)
     etmp = [e[0]-1, e[1]-1, e[2]-1] # because .copy() includes right edge
     box = maptbx.copy(f_map_data, s, etmp)
     box.reshape(flex.grid(box.all()))
     maptbx.set_box(
       map_data_from = box,
       map_data_to   = self.map_result,
       start         = s,
       end           = e)
   sd = self.map_result.sample_standard_deviation()
   self.map_result = self.map_result/sd
   self.map_coefficients = fmodel.f_obs().structure_factors_from_map(
     map            = self.map_result,
     use_scale      = True,
     anomalous_flag = False,
     use_sg         = False)