Esempio n. 1
0
 def as_p1_map(self, map_asu=None):
   if(map_asu is None): map_asu = self.map_result_asu
   asu_map_omit = asu_map_ext.asymmetric_map(self.sgt, map_asu, self.n_real)
   p1_map = asu_map_omit.symmetry_expanded_map()
   maptbx.unpad_in_place(map=p1_map)
   sd = p1_map.sample_standard_deviation()
   if(sd != 0):
     p1_map = p1_map/sd
   return p1_map
Esempio n. 2
0
 def __init__(self,
              map,
              xray_structure,
              d_min,
              box=None,
              compute_cc_box=False,
              compute_cc_image=False,
              compute_cc_mask=True,
              compute_cc_volume=True,
              compute_cc_peaks=True):
     adopt_init_args(self, locals())
     if (box is None and xray_structure.crystal_symmetry().space_group().
             type().number() == 1):
         box = True
     else:
         box = False
     #
     self.cc_box = None
     self.cc_image = None
     self.cc_mask = None
     self.cc_volume = None
     self.cc_peaks = None
     #
     self.crystal_gridding = maptbx.crystal_gridding(
         unit_cell=xray_structure.unit_cell(),
         space_group_info=xray_structure.space_group_info(),
         pre_determined_n_real=map.accessor().all(),
         symmetry_flags=maptbx.use_space_group_symmetry)
     self.atom_radius = None
     bs_mask = masks.mask_from_xray_structure(
         xray_structure=self.xray_structure,
         p1=True,
         for_structure_factors=False,
         n_real=self.map.accessor().all()).mask_data
     maptbx.unpad_in_place(map=bs_mask)
     self.sel_inside = (bs_mask == 0.).iselection()
     self.n_nodes_inside = self.sel_inside.size()
     del bs_mask
     #
     map_calc = self.get_map_calc()
     #
     if (compute_cc_mask):
         self.cc_mask = from_map_map_selection(map_1=self.map,
                                               map_2=map_calc,
                                               selection=self.sel_inside)
     del self.sel_inside
     if (compute_cc_box):
         self.cc_box = from_map_map(map_1=self.map, map_2=map_calc)
     if (compute_cc_image):
         self.atom_radius = self._atom_radius()
         self.cc_image = self._cc_image(map_calc=map_calc)
     if (compute_cc_volume):
         self.cc_volume = self._cc_volume(map_calc=map_calc)
     if (compute_cc_peaks):
         self.cc_peaks = self._cc_peaks(map_calc=map_calc)
Esempio n. 3
0
def exercise_real_to_complex_3d():
  print "real_to_complex_3d"
  for n_real,n_repeats in [((100,80,90),8),
                           ((200,160,180),2),
                           ((300,240,320),1)]:
    print "  dimensions:", n_real
    print "  repeats:", n_repeats
    fft = fftpack.real_to_complex_3d(n_real)
    m_real = fft.m_real()
    np = n_real[0]*n_real[1]*n_real[2]
    mp = m_real[0]*m_real[1]*m_real[2]
    d0 = flex.random_double(size=mp)*2-1
    d0.reshape(flex.grid(m_real).set_focus(n_real))
    #
    t0 = time.time()
    for i_trial in xrange(n_repeats):
      d = d0.deep_copy()
    overhead = time.time()-t0
    print "    overhead: %.2f seconds" % overhead
    #
    t0 = time.time()
    for i_trial in xrange(n_repeats):
      d = d0.deep_copy()
      c = fftw3tbx.real_to_complex_3d_in_place(data=d)
      assert c.all() == fft.n_complex()
      assert c.focus() == fft.n_complex()
      assert c.id() == d.id()
      r = fftw3tbx.complex_to_real_3d_in_place(data=c, n=n_real)
      assert r.all() == fft.m_real()
      assert r.focus() == fft.n_real()
      assert r.id() == d.id()
    print "    fftw:     %.2f seconds" % (time.time()-t0-overhead)
    if (maptbx is not None):
      maptbx.unpad_in_place(map=d)
      rw = d / np
    #
    t0 = time.time()
    for i_trial in xrange(n_repeats):
      d = d0.deep_copy()
      c = fftpack.real_to_complex_3d(n_real).forward(d)
      assert c.all() == fft.n_complex()
      assert c.focus() == fft.n_complex()
      assert c.id() == d.id()
      r = fftpack.real_to_complex_3d(n_real).backward(c)
      assert r.all() == fft.m_real()
      assert r.focus() == fft.n_real()
      assert r.id() == d.id()
    print "    fftpack:  %.2f seconds" % (time.time()-t0-overhead)
    sys.stdout.flush()
    if (maptbx is not None):
      maptbx.unpad_in_place(map=d)
      rp = d / np
      #
      assert flex.max(flex.abs(rw-rp)) < 1.e-6
Esempio n. 4
0
 def mask_from_xrs_unpadded(self, xray_structure, n_real):
     mask_params = mmtbx.masks.mask_master_params.extract()
     mask = mmtbx.masks.mask_from_xray_structure(
         xray_structure=xray_structure,
         p1=True,
         shrink_truncation_radius=mask_params.shrink_truncation_radius,
         solvent_radius=mask_params.solvent_radius,
         for_structure_factors=True,
         n_real=n_real).mask_data
     maptbx.unpad_in_place(map=mask)
     return mask
Esempio n. 5
0
def mask_from_xrs_unpadded(xray_structure, n_real):
  mask_params = mmtbx.masks.mask_master_params.extract()
  mask = mmtbx.masks.mask_from_xray_structure(
    xray_structure           = xray_structure,
    p1                       = True,
    shrink_truncation_radius = mask_params.shrink_truncation_radius,
    solvent_radius           = mask_params.solvent_radius,
    for_structure_factors    = True,
    n_real                   = n_real).mask_data
  maptbx.unpad_in_place(map = mask)
  return mask
Esempio n. 6
0
 def _compute_mask_in_p1(self):
     mask = mmtbx.masks.mask_from_xray_structure(
         xray_structure=self.xray_structure,
         p1=True,
         for_structure_factors=True,
         solvent_radius=self.r_sol,
         shrink_truncation_radius=self.r_shrink,
         n_real=self.n_real,
         in_asu=False).mask_data
     maptbx.unpad_in_place(map=mask)
     if (self.write_masks):
         write_map_file(
             crystal_symmetry=self.
             crystal_symmetry,  # Is this correct symmetry?
             map_data=mask,
             file_name="mask_whole.mrc")
     return mask
Esempio n. 7
0
 def __init__(self, xray_structure, n_real, rad_smooth, atom_radii=None,
                    solvent_radius=None, shrink_truncation_radius=None):
   mask_binary = mask_from_xray_structure(
     xray_structure           = xray_structure,
     p1                       = True,
     for_structure_factors    = False,
     n_real                   = n_real,
     atom_radii               = atom_radii,
     solvent_radius           = solvent_radius,
     shrink_truncation_radius = shrink_truncation_radius,
     rad_extra                = rad_smooth).mask_data
   s = mask_binary>0.5
   mask_binary = mask_binary.set_selected(s, 0)
   mask_binary = mask_binary.set_selected(~s, 1)
   maptbx.unpad_in_place(map=mask_binary)
   self.mask_smooth = maptbx.smooth_map(
     map              = mask_binary,
     crystal_symmetry = xray_structure.crystal_symmetry(),
     rad_smooth       = rad_smooth)
def get_mask_1(fmodel, grid_step_factor):
  grid_step = fmodel.f_obs().d_min()*(1./grid_step_factor)
  if(grid_step < 0.15): grid_step = 0.15
  grid_step = min(0.8, grid_step)
  crystal_gridding = maptbx.crystal_gridding(
    unit_cell = fmodel.xray_structure.unit_cell(),
    space_group_info = fmodel.xray_structure.space_group_info(),
    symmetry_flags   = maptbx.use_space_group_symmetry,
    step             = grid_step)
  n_real = crystal_gridding.n_real()
  # Compute mask in P1
  mask_data_p1 = mmtbx.masks.mask_from_xray_structure(
    xray_structure        = fmodel.xray_structure,
    p1                    = True,
    for_structure_factors = True,
    n_real                = n_real,
    in_asu                = False).mask_data
  maptbx.unpad_in_place(map=mask_data_p1)
  return mask_data_p1, n_real, crystal_gridding
Esempio n. 9
0
def get_mask_1(fmodel, grid_step_factor):
    grid_step = fmodel.f_obs().d_min() * (1. / grid_step_factor)
    if (grid_step < 0.15): grid_step = 0.15
    grid_step = min(0.8, grid_step)
    crystal_gridding = maptbx.crystal_gridding(
        unit_cell=fmodel.xray_structure.unit_cell(),
        space_group_info=fmodel.xray_structure.space_group_info(),
        symmetry_flags=maptbx.use_space_group_symmetry,
        step=grid_step)
    n_real = crystal_gridding.n_real()
    # Compute mask in P1
    mask_data_p1 = mmtbx.masks.mask_from_xray_structure(
        xray_structure=fmodel.xray_structure,
        p1=True,
        for_structure_factors=True,
        n_real=n_real,
        in_asu=False).mask_data
    maptbx.unpad_in_place(map=mask_data_p1)
    return mask_data_p1, n_real, crystal_gridding
Esempio n. 10
0
def get_mask_2(fmodel, grid_step_factor):
    sgt = fmodel.xray_structure.space_group().type()
    mask_params = mmtbx.masks.mask_master_params.extract()
    mask_params.grid_step_factor = grid_step_factor
    asu_mask_obj = mmtbx.masks.asu_mask(xray_structure=fmodel.xray_structure,
                                        d_min=fmodel.f_obs().d_min(),
                                        mask_params=mask_params).asu_mask
    mask_data_p1 = asu_mask_obj.mask_data_whole_uc()
    maptbx.unpad_in_place(map=mask_data_p1)
    n_real = mask_data_p1.all()
    crystal_gridding = maptbx.crystal_gridding(
        unit_cell=fmodel.xray_structure.unit_cell(),
        space_group_info=fmodel.xray_structure.space_group_info(),
        symmetry_flags=maptbx.use_space_group_symmetry,
        pre_determined_n_real=n_real)

    #n_real = mask_data_p1.all()
    #mask_data_p1 = asu_map_ext.asymmetric_map(sgt, mask_data_p1, n_real).symmetry_expanded_map()
    #maptbx.unpad_in_place(map=mask_data_p1)

    return mask_data_p1, n_real, crystal_gridding
def get_mask_2(fmodel, grid_step_factor):
  sgt = fmodel.xray_structure.space_group().type()
  mask_params = mmtbx.masks.mask_master_params.extract()
  mask_params.grid_step_factor = grid_step_factor
  asu_mask_obj = mmtbx.masks.asu_mask(
    xray_structure = fmodel.xray_structure,
    d_min          = fmodel.f_obs().d_min(),
    mask_params    = mask_params).asu_mask
  mask_data_p1 = asu_mask_obj.mask_data_whole_uc()
  maptbx.unpad_in_place(map=mask_data_p1)
  n_real = mask_data_p1.all()
  crystal_gridding = maptbx.crystal_gridding(
    unit_cell             = fmodel.xray_structure.unit_cell(),
    space_group_info      = fmodel.xray_structure.space_group_info(),
    symmetry_flags        = maptbx.use_space_group_symmetry,
    pre_determined_n_real = n_real)

  #n_real = mask_data_p1.all()
  #mask_data_p1 = asu_map_ext.asymmetric_map(sgt, mask_data_p1, n_real).symmetry_expanded_map()
  #maptbx.unpad_in_place(map=mask_data_p1)

  return mask_data_p1, n_real, crystal_gridding
Esempio n. 12
0
def exercise_work_in_asu():
    pdb_str = """
CRYST1   10.000  10.000   10.000  90.00  90.00  90.00  P 4
HETATM    1  C    C      1       2.000   2.000   2.000  1.00 20.00           C
HETATM    2  C    C      2       4.000   4.000   4.000  1.00 20.00           C
END
"""

    from time import time
    pdb_inp = iotbx.pdb.input(source_info=None, lines=pdb_str)
    xrs = pdb_inp.xray_structure_simple()
    # xrs.show_summary()
    d_min = 1
    fc = xrs.structure_factors(d_min=d_min).f_calc()
    symmetry_flags = maptbx.use_space_group_symmetry
    fftmap = fc.fft_map(symmetry_flags=symmetry_flags)
    # rmup = fftmap.real_map_unpadded()
    rm = fftmap.real_map().deep_copy()
    maptbx.unpad_in_place(rm)
    mmm = rm.as_1d().min_max_mean()
    print(mmm.min, mmm.max, mmm.mean)
    # rmup = fftmap.real_map_unpadded()
    # print (dir(rm))
    print("full size:", fftmap.real_map().accessor().focus())
    print(rm[0, 0, 0])
    # print (type(rm))
    # print (dir(rm))
    # STOP()
    # print(rmup[0,0,0])
    amap0 = asymmetric_map(xrs.space_group().type(), rm)
    # print(dir(amap0))
    mmm = amap0.data().as_1d().min_max_mean()
    print(mmm.min, mmm.max, mmm.mean)
    amap_data = amap0.data()
    write_ccp4_map('amap.ccp4', xrs.unit_cell(), xrs.space_group(), amap_data)
    write_ccp4_map('rm.ccp4', xrs.unit_cell(), xrs.space_group(), rm)
    # for i in range(50):
    #   print(i, amap_data[i,0,0])
    exp_map = amap0.symmetry_expanded_map()
    print(exp_map[0, 0, 0])
    # for i in range(32):
    #   for j in range(32):
    #     for k in range(32):
    #       assert approx_equal(rm[i,j,k], exp_map[i,j,k])

    # print(dir(amap0))
    # STOP()
    # This produces 2 separate blobs
    sg = xrs.space_group()
    print(dir(sg))
    print(sg.all_ops())
    print(sg.info())
    print("amap0 size:", amap0.data().accessor().focus())
    # STOP()
    print(type(amap0.data()))
    threshold = 0.
    preprocess_against_shallow = True
    print('threshold:', threshold)
    print('preprocess_against_shallow', preprocess_against_shallow)
    t0 = time()
    co_amap = maptbx.connectivity(
        map_data=amap0.data(),
        # threshold=threshold,
        # space_group=xrs.space_group(),
        # uc_dimensions=exp_map.accessor().focus(),
        # wrapping=False,
        preprocess_against_shallow=preprocess_against_shallow)
    t1 = time()
    print('amap time:', t1 - t0)
    original_regions = list(co_amap.regions())
    print('start regions:', original_regions)
    print('max coords', list(co_amap.maximum_coors()))
    print('max vals', list(co_amap.maximum_values()))

    # print(dir(exp_map))
    print(type(exp_map))
    print("exp_map size:", exp_map.accessor().focus())
    t0 = time()
    co_full = maptbx.connectivity(
        map_data=rm,
        threshold=threshold,
        wrapping=False,
        preprocess_against_shallow=preprocess_against_shallow)
    t1 = time()
    print('full time:', t1 - t0)

    original_regions = list(co_full.regions())
    print('start regions:', original_regions)
    print('max coords', list(co_full.maximum_coors()))
    print('max vals', list(co_full.maximum_values()))

    # STOP()
    # co.experiment_with_symmetry(
    #     space_group=xrs.space_group(),
    #     uc_dims=exp_map.accessor().focus())

    co_full.merge_symmetry_related_regions(space_group=xrs.space_group(),
                                           uc_dims=exp_map.accessor().focus())
    new_regions = list(co_full.regions())
    print('new regions:', new_regions)
    print('max coords', list(co_full.maximum_coors()))
    print('max vals', list(co_full.maximum_values()))
Esempio n. 13
0
def _exercise_real_to_complex_3d(sizes, benchmark=True):
    from cctbx import maptbx
    from scitbx.array_family import flex
    from cudatbx import cufft
    from scitbx import fftpack
    from libtbx.test_utils import approx_equal
    import time
    for n_real, n_repeats, eps in sizes:
        nx, ny, nz = n_real
        fft = fftpack.real_to_complex_3d((nx, ny, nz))
        mt = flex.mersenne_twister(seed=1)
        g = flex.grid(fft.m_real()).set_focus(fft.n_real())
        map = mt.random_double(size=g.size_1d())
        map.reshape(g)
        sfs = fft.forward(map.deep_copy())
        map2 = fft.backward(sfs)
        fft_cuda = cufft.real_to_complex_3d((nx, ny, nz))
        sfs_cuda = fft_cuda.forward(
            map)  #cufft.real_to_complex_3d_in_place(map)
        map2_cuda = fft_cuda.backward(
            sfs_cuda)  #cufft.complex_to_real_3d_in_place(sfs_cuda, n_real)
        maptbx.unpad_in_place(map=map2)
        maptbx.unpad_in_place(map=map2_cuda)
        map2_values = map2.as_1d()
        map2_cuda_values = map2_cuda.as_1d()
        mmm = map2_values.min_max_mean()
        mmm_cuda = map2_cuda_values.min_max_mean()
        assert (map2.size() == map2_cuda.size())
        assert approx_equal(mmm.min, mmm_cuda.min, eps=eps)
        assert approx_equal(mmm.max, mmm_cuda.max, eps=eps)
        assert approx_equal(mmm.mean, mmm_cuda.mean, eps=eps)
        if (benchmark):
            map_bak = map.deep_copy()
            r2c = [fft.forward, fft_cuda.forward]
            c2r = [fft.backward, fft_cuda.backward]
            modules = ["fftpack:", "cufft:  "]
            last_real = [None, None]
            print "  dimensions:", n_real
            print "  repeats:", n_repeats
            k = 0
            for (r2c_fn, c2r_fn, name) in zip(r2c, c2r, modules):
                t_forward = 0
                t_backward = 0
                map = map_bak.deep_copy()
                for i in range(n_repeats):
                    t1 = time.time()
                    sfs = r2c_fn(map)
                    t2 = time.time()
                    map2 = c2r_fn(sfs)
                    t3 = time.time()
                    t_forward += t2 - t1
                    t_backward += t3 - t2
                    if (i == n_repeats - 1):
                        last_real[k] = map2.deep_copy()
                k += 1
                print "    %s %7.3fs (forward)  %7.3fs (backward)" % (
                    name, t_forward / n_repeats, t_backward / n_repeats)
            last_fftpack, last_cufft = last_real
            maptbx.unpad_in_place(map=last_fftpack)
            maptbx.unpad_in_place(map=last_cufft)
            mmm = last_fftpack.as_1d().min_max_mean()
            mmm_cuda = last_cufft.as_1d().min_max_mean()
            # FIXME why doesn't this work?
            #assert approx_equal(mmm.min, mmm_cuda.min, eps=eps)
            assert approx_equal(mmm.max, mmm_cuda.max, eps=eps)
            assert approx_equal(mmm.mean, mmm_cuda.mean, eps=eps)
            print ""
Esempio n. 14
0
def _exercise_real_to_complex_3d (sizes, benchmark=True) :
  from cctbx import maptbx
  from scitbx.array_family import flex
  from cudatbx import cufft
  from scitbx import fftpack
  from libtbx.test_utils import approx_equal
  import time
  for n_real, n_repeats, eps in sizes :
    nx, ny, nz = n_real
    fft = fftpack.real_to_complex_3d((nx,ny,nz))
    mt = flex.mersenne_twister(seed=1)
    g = flex.grid(fft.m_real()).set_focus(fft.n_real())
    map = mt.random_double(size=g.size_1d())
    map.reshape(g)
    sfs = fft.forward(map.deep_copy())
    map2 = fft.backward(sfs)
    fft_cuda = cufft.real_to_complex_3d((nx,ny,nz))
    sfs_cuda = fft_cuda.forward(map)#cufft.real_to_complex_3d_in_place(map)
    map2_cuda = fft_cuda.backward(sfs_cuda)#cufft.complex_to_real_3d_in_place(sfs_cuda, n_real)
    maptbx.unpad_in_place(map=map2)
    maptbx.unpad_in_place(map=map2_cuda)
    map2_values = map2.as_1d()
    map2_cuda_values = map2_cuda.as_1d()
    mmm = map2_values.min_max_mean()
    mmm_cuda = map2_cuda_values.min_max_mean()
    assert (map2.size() == map2_cuda.size())
    assert approx_equal(mmm.min, mmm_cuda.min, eps=eps)
    assert approx_equal(mmm.max, mmm_cuda.max, eps=eps)
    assert approx_equal(mmm.mean, mmm_cuda.mean, eps=eps)
    if (benchmark) :
      map_bak = map.deep_copy()
      r2c = [ fft.forward, fft_cuda.forward ]
      c2r = [ fft.backward, fft_cuda.backward ]
      modules = ["fftpack:", "cufft:  "]
      last_real = [None, None]
      print "  dimensions:", n_real
      print "  repeats:", n_repeats
      k = 0
      for (r2c_fn, c2r_fn, name) in zip(r2c, c2r, modules) :
        t_forward = 0
        t_backward = 0
        map = map_bak.deep_copy()
        for i in range(n_repeats) :
          t1 = time.time()
          sfs = r2c_fn(map)
          t2 = time.time()
          map2 = c2r_fn(sfs)
          t3 = time.time()
          t_forward += t2 - t1
          t_backward += t3 - t2
          if (i == n_repeats - 1) :
            last_real[k] = map2.deep_copy()
        k += 1
        print "    %s %7.3fs (forward)  %7.3fs (backward)" % (name,
          t_forward / n_repeats, t_backward / n_repeats)
      last_fftpack,last_cufft = last_real
      maptbx.unpad_in_place(map=last_fftpack)
      maptbx.unpad_in_place(map=last_cufft)
      mmm = last_fftpack.as_1d().min_max_mean()
      mmm_cuda = last_cufft.as_1d().min_max_mean()
      # FIXME why doesn't this work?
      #assert approx_equal(mmm.min, mmm_cuda.min, eps=eps)
      assert approx_equal(mmm.max, mmm_cuda.max, eps=eps)
      assert approx_equal(mmm.mean, mmm_cuda.mean, eps=eps)
      print ""
Esempio n. 15
0
def run_group(symbol):
    group = space_group_info(symbol)
    print("\n==")
    elements = ('C', 'N', 'O', 'H') * 11
    struc = random_structure.xray_structure(space_group_info=group,
                                            volume_per_atom=25.,
                                            general_positions_only=False,
                                            elements=elements,
                                            min_distance=1.0)
    struc.show_summary()
    d_min = 2.
    fc = struc.structure_factors(d_min=d_min).f_calc()
    symmetry_flags = maptbx.use_space_group_symmetry
    fftmap = fc.fft_map(symmetry_flags=symmetry_flags)
    grid_size = fftmap.real_map().accessor().focus()
    ###
    rm = fftmap.real_map().deep_copy()
    amap0 = asymmetric_map(struc.space_group().type(), rm)
    p1_map00 = amap0.symmetry_expanded_map()
    assert approx_equal(p1_map00, rm)
    #
    maptbx.unpad_in_place(rm)
    amap1 = asymmetric_map(struc.space_group().type(), rm)
    p1_map10 = amap1.symmetry_expanded_map()
    assert approx_equal(p1_map00, p1_map10)
    ###

    grid_tags = maptbx.grid_tags(grid_size)
    grid_tags.build(fftmap.space_group_info().type(), fftmap.symmetry_flags())
    grid_tags.verify(fftmap.real_map())

    print("FFT grid_size = ", grid_size)
    amap = asymmetric_map(struc.space_group().type(), fftmap.real_map())
    afc = amap.structure_factors(fc.indices())
    afftmap = amap.map_for_fft()
    print("whole cell map size: ", afftmap.accessor().focus())
    adata = amap.data()
    acc = adata.accessor()
    print("Asu map size: ", acc.origin(), " ", acc.last(), " ", acc.focus(), \
        " ", acc.all())
    df = flex.abs(afc - fc.data())
    r1 = flex.sum(df) / flex.sum(flex.abs(fc.data()))
    print("R1: ", r1)
    assert r1 < 1.e-5
    # just to prove to myself that I can shift origin to 000 and then reshape back
    adata = adata.shift_origin()
    adata.reshape(acc)
    #
    adata2 = adata.deep_copy() * 2.
    amap2 = asymmetric_map(struc.space_group().type(), adata2, grid_size)
    afc2 = amap2.structure_factors(fc.indices())
    df2 = flex.abs(afc2 * .5 - fc.data())
    r12 = flex.sum(df2) / flex.sum(flex.abs(fc.data()))
    print("R1 again: ", r12)
    assert r12 < 1.e-5

    p1_map = amap.symmetry_expanded_map()
    assert p1_map.accessor().focus() == grid_size

    rel_tol = 1.e-6
    n = 0
    mean_rel_dif = 0.
    for (m1, m2) in zip(fftmap.real_map(), p1_map):
        dif = abs(m1 - m2)
        av = 0.5 * (abs(m1) + abs(m2))
        assert dif <= rel_tol * av, "%f not <= %f * %f" % (dif, rel_tol, av)
        if av != 0:
            mean_rel_dif = mean_rel_dif + dif / av
            n = n + 1
    mean_rel_dif = mean_rel_dif / n
    print("mean rel err: ", mean_rel_dif)
    assert mean_rel_dif < 1.e-6
Esempio n. 16
0
    def __init__(self,
                 xray_structure,
                 step,
                 volume_cutoff,
                 f_obs,
                 f_calc=None,
                 log=sys.stdout,
                 write_masks=False):
        adopt_init_args(self, locals())
        #
        self.dsel = f_obs.d_spacings().data() >= 0
        self.miller_array = f_obs.select(self.dsel)
        #
        self.crystal_symmetry = self.xray_structure.crystal_symmetry()
        # compute mask in p1 (via ASU)
        self.crystal_gridding = maptbx.crystal_gridding(
            unit_cell=xray_structure.unit_cell(),
            space_group_info=xray_structure.space_group_info(),
            symmetry_flags=maptbx.use_space_group_symmetry,
            step=step)
        self.n_real = self.crystal_gridding.n_real()
        # XXX Where do we want to deal with H and occ==0?
        mask_p1 = mmtbx.masks.mask_from_xray_structure(
            xray_structure=xray_structure,
            p1=True,
            for_structure_factors=True,
            n_real=self.n_real,
            in_asu=False).mask_data
        maptbx.unpad_in_place(map=mask_p1)
        self.solvent_content = 100. * mask_p1.count(1) / mask_p1.size()
        if (write_masks):
            write_map_file(crystal_symmetry=xray_structure.crystal_symmetry(),
                           map_data=mask_p1,
                           file_name="mask_whole.mrc")
        # conn analysis
        co = maptbx.connectivity(map_data=mask_p1,
                                 threshold=0.01,
                                 preprocess_against_shallow=True,
                                 wrapping=True)
        co.merge_symmetry_related_regions(
            space_group=xray_structure.space_group())
        del mask_p1
        self.conn = co.result().as_double()
        z = zip(co.regions(), range(0, co.regions().size()))
        sorted_by_volume = sorted(z, key=lambda x: x[0], reverse=True)
        f_mask_data_0 = flex.complex_double(f_obs.data().size(), 0)
        FM = OrderedDict()
        self.FV = OrderedDict()
        self.mc = None
        diff_map = None
        mean_diff_map = None
        self.regions = OrderedDict()
        print(
            "   volume_p1    uc(%)   volume_asu  id   mFo-DFc: min,max,mean,sd",
            file=log)
        # Check if self.anomaly
        self.anomaly = False
        if (len(sorted_by_volume) > 2):
            uc_fractions = [
                round(p[0] * 100. / self.conn.size(), 0)
                for p in sorted_by_volume[1:]
            ]
            if (uc_fractions[0] / 4 < uc_fractions[1]): self.anomaly = True
        #
        for i_seq, p in enumerate(sorted_by_volume):
            v, i = p
            # skip macromolecule
            if (i == 0): continue
            # skip small volume
            volume = v * step**3
            uc_fraction = v * 100. / self.conn.size()
            if (volume_cutoff is not None):
                if volume < volume_cutoff: continue

            selection = self.conn == i
            mask_i_asu = self.compute_i_mask_asu(selection=selection,
                                                 volume=volume)
            volume_asu = (mask_i_asu > 0).count(True) * step**3

            if (i_seq == 1 or uc_fraction > 5):
                f_mask_i = self.compute_f_mask_i(mask_i_asu)
                if (not self.anomaly):
                    f_mask_data_0 += f_mask_i.data()

            if (uc_fraction < 5 and diff_map is None and not self.anomaly):
                diff_map = self.compute_diff_map(f_mask_data=f_mask_data_0)

            mi, ma, me, sd = None, None, None, None
            if (diff_map is not None):
                blob = diff_map.select(selection.iselection())
                mean_diff_map = flex.mean(
                    diff_map.select(selection.iselection()))
                mi, ma, me = flex.min(blob), flex.max(blob), flex.mean(blob)
                sd = blob.sample_standard_deviation()

            print("%12.3f" % volume,
                  "%8.4f" % round(uc_fraction, 4),
                  "%12.3f" % volume_asu,
                  "%3d" % i,
                  "%7s" % str(None) if diff_map is None else
                  "%7.3f %7.3f %7.3f %7.3f" % (mi, ma, me, sd),
                  file=log)

            if (uc_fraction < 1 and mean_diff_map is not None
                    and mean_diff_map <= 0):
                continue

            self.regions[i_seq] = group_args(id=i,
                                             i_seq=i_seq,
                                             volume=volume,
                                             uc_fraction=uc_fraction,
                                             diff_map=group_args(mi=mi,
                                                                 ma=ma,
                                                                 me=me,
                                                                 sd=sd))

            if (not (i_seq == 1 or uc_fraction > 5)):
                f_mask_i = self.compute_f_mask_i(mask_i_asu)

            FM.setdefault(round(volume, 3), []).append(f_mask_i.data())
            self.FV[f_mask_i] = [round(volume, 3), round(uc_fraction, 1)]
        #
        f_mask_0 = f_obs.customized_copy(data=f_mask_data_0)
        #
        self.f_mask_0 = None
        if (not self.anomaly):
            self.f_mask_0 = f_obs.customized_copy(data=f_mask_data_0)
        self.do_mosaic = False
        if (len(self.FV.keys()) > 1):
            self.do_mosaic = True
Esempio n. 17
0
def get_f_mask(xrs, ma, step, option=2):
    crystal_gridding = maptbx.crystal_gridding(
        unit_cell=xrs.unit_cell(),
        space_group_info=xrs.space_group_info(),
        symmetry_flags=maptbx.use_space_group_symmetry,
        step=step)
    n_real = crystal_gridding.n_real()
    atom_radii = vdw_radii_from_xray_structure(xray_structure=xrs)
    mask_params = masks.mask_master_params.extract()
    grid_step_factor = ma.d_min() / step
    # 1
    if (option == 1):
        asu_mask = ext.atom_mask(
            unit_cell=xrs.unit_cell(),
            group=xrs.space_group(),
            resolution=ma.d_min(),
            grid_step_factor=grid_step_factor,
            solvent_radius=mask_params.solvent_radius,
            shrink_truncation_radius=mask_params.shrink_truncation_radius)
        asu_mask.compute(xrs.sites_frac(), atom_radii)
        fm_asu = asu_mask.structure_factors(ma.indices())
        f_mask = ma.set().array(data=fm_asu)
    # 2
    elif (option == 2):
        asu_mask = ext.atom_mask(
            unit_cell=xrs.unit_cell(),
            space_group=xrs.space_group(),
            gridding_n_real=n_real,
            solvent_radius=mask_params.solvent_radius,
            shrink_truncation_radius=mask_params.shrink_truncation_radius)
        asu_mask.compute(xrs.sites_frac(), atom_radii)
        fm_asu = asu_mask.structure_factors(ma.indices())
        f_mask = ma.set().array(data=fm_asu)
    # 3
    elif (option == 3):
        mask_params.grid_step_factor = grid_step_factor
        mask_manager = masks.manager(miller_array=ma,
                                     miller_array_twin=None,
                                     mask_params=mask_params)
        f_mask = mask_manager.shell_f_masks(xray_structure=xrs,
                                            force_update=True)[0]
    # 4
    elif (option == 4):
        mask_p1 = mmtbx.masks.mask_from_xray_structure(
            xray_structure=xrs,
            p1=True,
            for_structure_factors=True,
            n_real=n_real,
            in_asu=False).mask_data
        maptbx.unpad_in_place(map=mask_p1)
        mask = asu_map_ext.asymmetric_map(
            xrs.crystal_symmetry().space_group().type(), mask_p1).data()
        f_mask = ma.structure_factors_from_asu_map(asu_map_data=mask,
                                                   n_real=n_real)
    elif (option == 5):
        o = mmtbx.masks.bulk_solvent(
            xray_structure=xrs,
            ignore_zero_occupancy_atoms=False,
            solvent_radius=mask_params.solvent_radius,
            shrink_truncation_radius=mask_params.shrink_truncation_radius,
            ignore_hydrogen_atoms=False,
            gridding_n_real=n_real,
            atom_radii=atom_radii)
        f_mask = o.structure_factors(ma)
    else:
        assert 0
    #
    return f_mask
Esempio n. 18
0
    def __init__(self,
                 xray_structure,
                 step,
                 volume_cutoff=None,
                 mean_diff_map_threshold=None,
                 compute_whole=False,
                 largest_only=False,
                 wrapping=True,
                 f_obs=None,
                 r_sol=1.1,
                 r_shrink=0.9,
                 f_calc=None,
                 log=None,
                 write_masks=False):
        adopt_init_args(self, locals())
        #
        self.d_spacings = f_obs.d_spacings().data()
        self.sel_3inf = self.d_spacings >= 3
        self.miller_array = f_obs.select(self.sel_3inf)
        #
        self.crystal_symmetry = self.xray_structure.crystal_symmetry()
        # compute mask in p1 (via ASU)
        self.crystal_gridding = maptbx.crystal_gridding(
            unit_cell=xray_structure.unit_cell(),
            space_group_info=xray_structure.space_group_info(),
            symmetry_flags=maptbx.use_space_group_symmetry,
            step=step)
        self.n_real = self.crystal_gridding.n_real()
        # XXX Where do we want to deal with H and occ==0?
        mask_p1 = mmtbx.masks.mask_from_xray_structure(
            xray_structure=xray_structure,
            p1=True,
            for_structure_factors=True,
            solvent_radius=r_sol,
            shrink_truncation_radius=r_shrink,
            n_real=self.n_real,
            in_asu=False).mask_data
        maptbx.unpad_in_place(map=mask_p1)
        self.f_mask_whole = None
        if (compute_whole):
            mask = asu_map_ext.asymmetric_map(
                xray_structure.crystal_symmetry().space_group().type(),
                mask_p1).data()
            self.f_mask_whole = self._inflate(
                self.miller_array.structure_factors_from_asu_map(
                    asu_map_data=mask, n_real=self.n_real))
        self.solvent_content = 100. * mask_p1.count(1) / mask_p1.size()
        if (write_masks):
            write_map_file(crystal_symmetry=xray_structure.crystal_symmetry(),
                           map_data=mask_p1,
                           file_name="mask_whole.mrc")
        # conn analysis
        co = maptbx.connectivity(map_data=mask_p1,
                                 threshold=0.01,
                                 preprocess_against_shallow=False,
                                 wrapping=wrapping)
        co.merge_symmetry_related_regions(
            space_group=xray_structure.space_group())
        del mask_p1
        self.conn = co.result().as_double()
        z = zip(co.regions(), range(0, co.regions().size()))
        sorted_by_volume = sorted(z, key=lambda x: x[0], reverse=True)
        #
        f_mask_data_0 = flex.complex_double(f_obs.data().size(), 0)
        f_mask_data = flex.complex_double(f_obs.data().size(), 0)
        self.FV = OrderedDict()
        self.mc = None
        diff_map = None
        mean_diff_map = None
        self.regions = OrderedDict()
        self.f_mask_0 = None
        self.f_mask = None
        #
        if (log is not None):
            print("  #    volume_p1    uc(%) mFo-DFc: min,max,mean,sd",
                  file=log)
        #
        for i_seq, p in enumerate(sorted_by_volume):
            v, i = p
            # skip macromolecule
            if (i == 0): continue
            # skip small volume
            volume = v * step**3
            uc_fraction = v * 100. / self.conn.size()
            if (volume_cutoff is not None):
                if volume < volume_cutoff: continue

            self.regions[i_seq] = group_args(id=i,
                                             i_seq=i_seq,
                                             volume=volume,
                                             uc_fraction=uc_fraction)

            selection = self.conn == i
            mask_i_asu = self.compute_i_mask_asu(selection=selection,
                                                 volume=volume)
            volume_asu = (mask_i_asu > 0).count(True) * step**3

            if (uc_fraction >= 1):
                f_mask_i = self.compute_f_mask_i(mask_i_asu)
                f_mask_data_0 += f_mask_i.data()
            elif (largest_only):
                break

            if (uc_fraction < 1 and diff_map is None):
                diff_map = self.compute_diff_map(f_mask_data=f_mask_data_0)

            mi, ma, me, sd = None, None, None, None
            if (diff_map is not None):
                blob = diff_map.select(selection.iselection())
                mean_diff_map = flex.mean(
                    diff_map.select(selection.iselection()))
                mi, ma, me = flex.min(blob), flex.max(blob), flex.mean(blob)
                sd = blob.sample_standard_deviation()

            if (log is not None):
                print("%3d" % i_seq,
                      "%12.3f" % volume,
                      "%8.4f" % round(uc_fraction, 4),
                      "%7s" % str(None) if diff_map is None else
                      "%7.3f %7.3f %7.3f %7.3f" % (mi, ma, me, sd),
                      file=log)

            if (mean_diff_map_threshold is not None
                    and mean_diff_map is not None
                    and mean_diff_map <= mean_diff_map_threshold):
                continue

            f_mask_i = self.compute_f_mask_i(mask_i_asu)
            f_mask_data += f_mask_i.data()

            self.FV[f_mask_i] = [round(volume, 3), round(uc_fraction, 1)]
        #
        self.f_mask_0 = f_obs.customized_copy(data=f_mask_data_0)
        self.f_mask = f_obs.customized_copy(data=f_mask_data)
        self.do_mosaic = False
        # Determine number of secondary regions
        self.n_regions = len(self.FV.values())
        if (self.n_regions > 1):
            self.do_mosaic = True
Esempio n. 19
0
def remove_model_density(map_data, xrs, rad_inside=2):
  #
  map_data = map_data - flex.mean(map_data)
  map_data = map_data.set_selected(map_data < 0, 0)
  sd = map_data.sample_standard_deviation()
  assert sd != 0
  map_data = map_data / sd
  #
  map_at_atoms = flex.double()
  for site_frac in xrs.sites_frac():
    mv = map_data.tricubic_interpolation(site_frac)
    map_at_atoms.append( mv )
  print (flex.mean(map_at_atoms), flex.max(map_at_atoms))
  mmax = flex.max(map_at_atoms)
  cut = 0
  print (dir(map_data))
  while cut<mmax:
    map_data_ = map_data.deep_copy()
    map_data_ = map_data_.set_selected(map_data<cut, 0)
    map_data_ = map_data_.set_selected(map_data>=cut, 1)
    cut+=1

    zz = flex.double()
    for site_frac in xrs.sites_frac():
      mv = map_data_.value_at_closest_grid_point(site_frac)
      zz.append( mv )
    print(cut,  (zz==1).count(True)/zz.size()*100. )

  #
  #radii = flex.double(xrs.sites_frac().size(), rad_inside)
  #mask = cctbx_maptbx_ext.mask(
  #  sites_frac                  = xrs.sites_frac(),
  #  unit_cell                   = xrs.unit_cell(),
  #  n_real                      = map_data.all(),
  #  mask_value_inside_molecule  = 0,
  #  mask_value_outside_molecule = 1,
  #  radii                       = radii)

  mask = mmtbx.masks.mask_from_xray_structure(
    xray_structure           = xrs,
    p1                       = True,
    for_structure_factors    = True,
    solvent_radius           = None,
    shrink_truncation_radius = None,
    n_real                   = map_data.accessor().all(),
    in_asu                   = False).mask_data
  maptbx.unpad_in_place(map=mask)


  map_data = map_data * mask
  map_data = map_data.set_selected(map_data < flex.mean(map_at_atoms)/6, 0)
  #
  n = map_data.accessor().all()
  abc = xrs.unit_cell().parameters()[:3]
  print(abc[0]/n[0], abc[1]/n[1], abc[2]/n[2])

  step = abc[0]/n[0]

  co = maptbx.connectivity(
    map_data                   = map_data.deep_copy(),
    threshold                  = 0.0,
    preprocess_against_shallow = True,
    wrapping                   = False)
  conn = co.result().as_double()
  z = zip(co.regions(),range(0,co.regions().size()))
  sorted_by_volume = sorted(z, key=lambda x: x[0], reverse=True)
  mask_ = flex.double(flex.grid(n), 0)
  for i_seq, p in enumerate(sorted_by_volume):
    v, i = p
    if i_seq==0: continue
    volume = v*step**3
    print(v, volume)
    if 1:#(volume<3):
      sel = conn==i
      mask_ = mask_.set_selected(sel, 1)

  #
  return map_data*mask_
Esempio n. 20
0
def get_f_mask(xrs, ma, step, option=2, r_shrink=None, r_sol=None):
    #
    result = ma.deep_copy()
    sel = ma.d_spacings().data() >= 3
    ma = ma.select(sel)
    #
    crystal_gridding = maptbx.crystal_gridding(
        unit_cell=xrs.unit_cell(),
        space_group_info=xrs.space_group_info(),
        symmetry_flags=maptbx.use_space_group_symmetry,
        step=step)
    n_real = crystal_gridding.n_real()
    atom_radii = vdw_radii_from_xray_structure(xray_structure=xrs)
    mask_params = masks.mask_master_params.extract()
    grid_step_factor = ma.d_min() / step
    if (r_shrink is not None): mask_params.shrink_truncation_radius = r_shrink
    if (r_sol is not None): mask_params.solvent_radius = r_sol
    mask_params.grid_step_factor = grid_step_factor
    # 1
    if (option == 1):
        asu_mask = ext.atom_mask(
            unit_cell=xrs.unit_cell(),
            group=xrs.space_group(),
            resolution=ma.d_min(),
            grid_step_factor=grid_step_factor,
            solvent_radius=mask_params.solvent_radius,
            shrink_truncation_radius=mask_params.shrink_truncation_radius)
        asu_mask.compute(xrs.sites_frac(), atom_radii)
        fm_asu = asu_mask.structure_factors(ma.indices())
        f_mask = ma.set().array(data=fm_asu)
    # 2
    elif (option == 2):
        asu_mask = ext.atom_mask(
            unit_cell=xrs.unit_cell(),
            space_group=xrs.space_group(),
            gridding_n_real=n_real,
            solvent_radius=mask_params.solvent_radius,
            shrink_truncation_radius=mask_params.shrink_truncation_radius)
        asu_mask.compute(xrs.sites_frac(), atom_radii)
        fm_asu = asu_mask.structure_factors(ma.indices())
        f_mask = ma.set().array(data=fm_asu)
    # 3
    elif (option == 3):
        mask_p1 = mmtbx.masks.mask_from_xray_structure(
            xray_structure=xrs,
            p1=True,
            for_structure_factors=True,
            solvent_radius=mask_params.solvent_radius,
            shrink_truncation_radius=mask_params.shrink_truncation_radius,
            n_real=n_real,
            in_asu=False).mask_data
        maptbx.unpad_in_place(map=mask_p1)
        mask = asu_map_ext.asymmetric_map(
            xrs.crystal_symmetry().space_group().type(), mask_p1).data()
        f_mask = ma.structure_factors_from_asu_map(asu_map_data=mask,
                                                   n_real=n_real)
    # 4
    elif (option == 4):
        f_mask = masks.bulk_solvent(
            xray_structure=xrs,
            ignore_zero_occupancy_atoms=False,
            solvent_radius=mask_params.solvent_radius,
            shrink_truncation_radius=mask_params.shrink_truncation_radius,
            ignore_hydrogen_atoms=False,
            grid_step=step,
            atom_radii=atom_radii).structure_factors(miller_set=ma)
    elif (option == 5):
        o = mmtbx.masks.bulk_solvent(
            xray_structure=xrs,
            ignore_zero_occupancy_atoms=False,
            solvent_radius=mask_params.solvent_radius,
            shrink_truncation_radius=mask_params.shrink_truncation_radius,
            ignore_hydrogen_atoms=False,
            gridding_n_real=n_real,
            atom_radii=atom_radii)
        assert approx_equal(n_real, o.data.accessor().all())
        f_mask = o.structure_factors(ma)
    elif (option == 6):
        # XXX No control over n_real, so results with others don't match
        mask_manager = masks.manager(miller_array=ma,
                                     miller_array_twin=None,
                                     mask_params=mask_params)
        f_mask = mask_manager.shell_f_masks(xray_structure=xrs,
                                            force_update=True)[0]
    else:
        assert 0
    #
    data = flex.complex_double(result.indices().size(), 0)
    data = data.set_selected(sel, f_mask.data())
    result = result.array(data=data)
    return result
Esempio n. 21
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]
Esempio n. 22
0
def run_group(symbol, preprocess_against_shallow):
  group = space_group_info(symbol)
  print("\n== space group %d"%symbol)
  xrs = random_structure.xray_structure(
    space_group_info       = group,
    volume_per_atom        = 15.,
    general_positions_only = False,
    elements               = ('C', 'N', 'O', 'H')*31,
    min_distance           = 1.0)
  sgt = xrs.space_group().type()
  #
  cg = maptbx.crystal_gridding(
    unit_cell        = xrs.unit_cell(),
    space_group_info = xrs.space_group_info(),
    symmetry_flags   = maptbx.use_space_group_symmetry,
    step             = 0.4)
  n_real = cg.n_real()
  mask_p1 = mmtbx.masks.mask_from_xray_structure(
    xray_structure        = xrs,
    p1                    = True,
    for_structure_factors = True,
    n_real                = n_real,
    in_asu                = False).mask_data
  maptbx.unpad_in_place(map=mask_p1)
  assert flex.min(mask_p1)==0
  assert flex.max(mask_p1)==1
  #
  co = maptbx.connectivity(
    map_data                   = mask_p1,
    threshold                  = 0.01,
    preprocess_against_shallow = preprocess_against_shallow,
    wrapping                   = True)
  #
  print("Regions in P1")
  regions_p1 = list(co.regions())
  s1 = flex.sum(flex.int(regions_p1))
  print(regions_p1, s1)
  conn_map_p1 = co.result().as_double()
  print(flex.min(conn_map_p1), flex.max(conn_map_p1))
  #
  print("Merge symmetry related")
  co.merge_symmetry_related_regions(space_group = xrs.space_group())
  conn_map_p1_merged = co.result().as_double()
  regions_p1_merged = list(co.regions())
  s2 = flex.sum(flex.int(regions_p1_merged))
  print(list(regions_p1_merged), s2)
  amap = asu_map_ext.asymmetric_map(sgt, conn_map_p1_merged)
  conn_map_asu = amap.data()
  conn_map_p1_restored = amap.symmetry_expanded_map()
  print(flex.min(conn_map_asu), flex.max(conn_map_asu))

  #
  mask_p1_1 = conn_map_p1_restored.set_selected(conn_map_p1_restored>0.01, 1)
  maptbx.unpad_in_place(map=mask_p1_1)
  co = maptbx.connectivity(
    map_data                   = mask_p1_1,
    threshold                  = 0.01,
    preprocess_against_shallow = preprocess_against_shallow,
    wrapping                   = True)
  print("Restored")
  regions_p1_restored = list(co.regions())
  s3 = flex.sum(flex.int(regions_p1_restored))
  print(regions_p1_restored, s3)
  conn_map_p1_restored = co.result().as_double()
  print(flex.min(conn_map_p1_restored), flex.max(conn_map_p1_restored))
  assert regions_p1 == regions_p1_restored
  #
  assert s1 == s2
  assert s2 == s3
Esempio n. 23
0
    def __init__(self,
                 miller_array,
                 xray_structure,
                 step,
                 volume_cutoff,
                 f_obs=None,
                 r_free_flags=None,
                 f_calc=None,
                 write_masks=False):
        adopt_init_args(self, locals())
        assert [f_obs, f_calc, r_free_flags].count(None) in [0, 3]
        self.crystal_symmetry = self.xray_structure.crystal_symmetry()
        # compute mask in p1 (via ASU)
        self.crystal_gridding = maptbx.crystal_gridding(
            unit_cell=xray_structure.unit_cell(),
            space_group_info=xray_structure.space_group_info(),
            symmetry_flags=maptbx.use_space_group_symmetry,
            step=step)
        self.n_real = self.crystal_gridding.n_real()
        mask_p1 = mmtbx.masks.mask_from_xray_structure(
            xray_structure=xray_structure,
            p1=True,
            for_structure_factors=True,
            n_real=self.n_real,
            in_asu=False).mask_data
        maptbx.unpad_in_place(map=mask_p1)
        solvent_content = 100. * mask_p1.count(1) / mask_p1.size()
        if (write_masks):
            write_map_file(crystal_symmetry=xray_structure.crystal_symmetry(),
                           map_data=mask_p1,
                           file_name="mask_whole.mrc")
        # conn analysis
        co = maptbx.connectivity(map_data=mask_p1,
                                 threshold=0.01,
                                 preprocess_against_shallow=True,
                                 wrapping=True)
        del mask_p1
        self.conn = co.result().as_double()
        z = zip(co.regions(), range(0, co.regions().size()))
        sorted_by_volume = sorted(z, key=lambda x: x[0], reverse=True)
        f_mask_data = flex.complex_double(miller_array.data().size(), 0)
        f_mask_data_0 = flex.complex_double(miller_array.data().size(), 0)
        #f_masks  = []
        FM = OrderedDict()
        diff_map = None
        mean_diff_map = None
        print("   volume_p1    uc(%)   volume_asu  id  <mFo-DFc>")
        for p in sorted_by_volume:
            v, i = p
            volume = v * step**3
            uc_fraction = v * 100. / self.conn.size()
            if (volume_cutoff is not None):
                if volume < volume_cutoff: continue
            if (i == 0): continue

            selection = self.conn == i
            mask_i_asu = self.compute_i_mask_asu(selection=selection,
                                                 volume=volume)
            volume_asu = (mask_i_asu > 0).count(True) * step**3
            if (volume_asu < 1.e-6): continue

            if (i == 1 or uc_fraction > 5):
                f_mask_i = miller_array.structure_factors_from_asu_map(
                    asu_map_data=mask_i_asu, n_real=self.n_real)
                f_mask_data_0 += f_mask_i.data()
                f_mask_data += f_mask_i.data()
            if (uc_fraction < 5 and diff_map is None):
                diff_map = self.compute_diff_map(f_mask_data=f_mask_data_0)
            if (diff_map is not None):
                mean_diff_map = flex.mean(
                    diff_map.select(selection.iselection()))

            print(
                "%12.3f" % volume, "%8.4f" % round(uc_fraction, 4),
                "%12.3f" % volume_asu, "%3d" % i, "%7s" %
                str(None) if diff_map is None else "%7.3f" % mean_diff_map)

            #if(mean_diff_map is not None and mean_diff_map<=0): continue

            if (not (i == 1 or uc_fraction > 5)):
                f_mask_i = miller_array.structure_factors_from_asu_map(
                    asu_map_data=mask_i_asu, n_real=self.n_real)
                f_mask_data += f_mask_i.data()

            FM.setdefault(round(volume, 3), []).append(f_mask_i.data())

        # group asu pices corresponding to the same region in P1
        F_MASKS = []
        for k, v in zip(FM.keys(), FM.values()):
            tmp = flex.complex_double(miller_array.data().size(), 0)
            for v_ in v:
                tmp += v_
            F_MASKS.append(miller_array.customized_copy(data=tmp))
        #
        f_mask = miller_array.customized_copy(data=f_mask_data)
        #
        self.f_mask = f_mask
        self.f_masks = F_MASKS