示例#1
0
 def __init__(self,
              xray_structure=None,
              fmodel=None,
              map_data=None,
              d_min=None,
              resolution_factor=0.25,
              map_type="2mFo-DFc"):
     """
 Utility to calculate correlation between two maps:
   CC(xray_structure, map_data), xray_structure are map_data inputs
 or
   CC(2mFo-DFc, Fc), 2mFo-DFc and Fc are from input fmodel .
 """
     assert [fmodel, map_data].count(None) == 1
     assert [xray_structure, map_data].count(None) in [0, 2]
     assert [fmodel, xray_structure].count(None) == 1
     assert [d_min, fmodel].count(None) == 1
     adopt_init_args(self, locals())
     if (fmodel is not None): self.xray_structure = fmodel.xray_structure
     # get map_data defined
     if (self.fmodel is not None):
         e_map_obj = fmodel.electron_density_map()
         isotropize = True
         if (fmodel.is_twin_fmodel_manager()): isotropize = False
         mc = e_map_obj.map_coefficients(map_type=map_type,
                                         fill_missing=False,
                                         isotropize=isotropize)
         crystal_gridding = self.fmodel.f_obs().crystal_gridding(
             d_min=self.fmodel.f_obs().d_min(),
             resolution_factor=resolution_factor)
         fft_map = miller.fft_map(crystal_gridding=crystal_gridding,
                                  fourier_coefficients=mc)
         self.map_data = fft_map.real_map_unpadded()
     # get model map
     if (self.fmodel is not None):
         if (fmodel.is_twin_fmodel_manager()):
             f_model = self.fmodel.f_model()
         else:
             f_model = self.fmodel.f_model_scaled_with_k1()
         fft_map = miller.fft_map(crystal_gridding=crystal_gridding,
                                  fourier_coefficients=f_model)
         fft_map.apply_sigma_scaling()
         self.map_model = fft_map.real_map_unpadded()
     else:
         crystal_gridding = maptbx.crystal_gridding(
             unit_cell=self.xray_structure.unit_cell(),
             space_group_info=self.xray_structure.space_group_info(),
             pre_determined_n_real=self.map_data.accessor().all())
         f_model = self.xray_structure.structure_factors(
             d_min=self.d_min).f_calc()
         fft_map = miller.fft_map(crystal_gridding=crystal_gridding,
                                  fourier_coefficients=f_model)
         fft_map.apply_sigma_scaling()
         self.map_model = fft_map.real_map_unpadded()
     if (self.fmodel is not None):
         self.sites_cart = self.fmodel.xray_structure.sites_cart()
         self.sites_frac = self.fmodel.xray_structure.sites_frac()
     else:
         self.sites_cart = self.xray_structure.sites_cart()
         self.sites_frac = self.xray_structure.sites_frac()
def get_cc(mc1, mc2, xrs):
  crystal_gridding = mc1.crystal_gridding(
    d_min             = mc1.d_min(),
    symmetry_flags    = maptbx.use_space_group_symmetry,
    resolution_factor = 0.25)
  fft_map = miller.fft_map(
    crystal_gridding     = crystal_gridding,
    fourier_coefficients = mc1)
  fft_map.apply_sigma_scaling()
  m1 = fft_map.real_map_unpadded()
  fft_map = miller.fft_map(
    crystal_gridding     = crystal_gridding,
    fourier_coefficients = mc2)
  fft_map.apply_sigma_scaling()
  m2 = fft_map.real_map_unpadded()
  assert m1.focus()==m2.focus()
  assert m1.all()==m2.all()
  sel = maptbx.grid_indices_around_sites(
    unit_cell  = mc1.unit_cell(),
    fft_n_real = m1.focus(),
    fft_m_real = m1.all(),
    sites_cart = flex.vec3_double(xrs.sites_cart()),
    site_radii = flex.double([1.5]*xrs.scatterers().size()))
  cc = flex.linear_correlation(x=m1.select(sel), y=m2.select(sel)).coefficient()
  def md(m, xrs):
    r = flex.double()
    for sf in xrs.sites_frac():
      r.append(m.eight_point_interpolation(sf))
    return flex.mean(r)
  return cc, md(m=m1, xrs=xrs), md(m=m2, xrs=xrs)
def get_cc(mc1, mc2, xrs):
  crystal_gridding = mc1.crystal_gridding(
    d_min = mc1.d_min(), resolution_factor = 0.25)
  fft_map = miller.fft_map(
    crystal_gridding     = crystal_gridding,
    fourier_coefficients = mc1)
  fft_map.apply_sigma_scaling()
  m1 = fft_map.real_map_unpadded()
  fft_map = miller.fft_map(
    crystal_gridding     = crystal_gridding,
    fourier_coefficients = mc2)
  fft_map.apply_sigma_scaling()
  m2 = fft_map.real_map_unpadded()
  assert m1.focus()==m2.focus()
  assert m1.all()==m2.all()
  ccs = flex.double()
  for site_cart in xrs.sites_cart():
    sel = maptbx.grid_indices_around_sites(
      unit_cell  = mc1.unit_cell(),
      fft_n_real = m1.focus(),
      fft_m_real = m1.all(),
      sites_cart = flex.vec3_double([site_cart]),
      site_radii = flex.double([1.5]))
    cc = flex.linear_correlation(x=m1.select(sel), y=m2.select(sel)).coefficient()
    ccs.append(cc)
  return ccs
示例#4
0
def cc_peak(cutoff,
            map_1=None,
            map_2=None,
            map_coeffs_1=None,
            map_coeffs_2=None):
    """
  Compute CCpeak as described in
    Acta Cryst. (2014). D70, 2593-2606
    Metrics for comparison of crystallographic maps
    A. Urzhumtsev, P. V. Afonine, V. Y. Lunin, T. C. Terwilliger and P. D. Adams
  """
    from cctbx import miller
    assert [map_1, map_2].count(None) in [0, 2]
    assert [map_coeffs_1, map_coeffs_2].count(None) in [0, 2]
    if ([map_1, map_2].count(None) == 0):
        # Maps are assumed to be quantile rank scaled (HE).
        return ext.cc_peak(map_1=map_1, map_2=map_2, cutoff=cutoff)
    elif ([map_coeffs_1, map_coeffs_2].count(None) == 0):
        d_min = min(map_coeffs_1.d_min(), map_coeffs_2.d_min())
        crystal_gridding = map_coeffs_1.crystal_gridding(
            d_min=d_min,
            symmetry_flags=use_space_group_symmetry,
            resolution_factor=0.25)
        fft_map = miller.fft_map(crystal_gridding=crystal_gridding,
                                 fourier_coefficients=map_coeffs_1)
        map_1 = fft_map.real_map_unpadded()
        fft_map = miller.fft_map(crystal_gridding=crystal_gridding,
                                 fourier_coefficients=map_coeffs_2)
        map_2 = fft_map.real_map_unpadded()
        m1_he = volume_scale(map=map_1, n_bins=10000).map_data()
        m2_he = volume_scale(map=map_2, n_bins=10000).map_data()
        return ext.cc_peak(map_1=m1_he, map_2=m2_he, cutoff=cutoff)
    else:
        raise RuntimeError("Combination of inputs not supported.")
示例#5
0
def good_atoms_selection(crystal_gridding, map_coeffs, xray_structure):
    #XXX copy from model_missing_reflections map_tools.py, consolidate later
    #XXX Also look for similar crap in f_model.py
    fft_map = miller.fft_map(crystal_gridding=crystal_gridding,
                             fourier_coefficients=map_coeffs)
    fft_map.apply_sigma_scaling()
    map_data = fft_map.real_map_unpadded()
    rho_atoms = flex.double()
    for site_frac in xray_structure.sites_frac():
        rho_atoms.append(map_data.eight_point_interpolation(site_frac))
    #rho_mean = flex.mean_default(rho_atoms.select(rho_atoms>1.0), 1.0)
    sel_exclude = rho_atoms < 1.0  # XXX ??? TRY 0.5!
    sites_cart = xray_structure.sites_cart()
    #
    f_calc = map_coeffs.structure_factors_from_scatterers(
        xray_structure=xray_structure).f_calc()
    fft_map = miller.fft_map(crystal_gridding=crystal_gridding,
                             fourier_coefficients=f_calc)
    fft_map.apply_sigma_scaling()
    map_data2 = fft_map.real_map_unpadded()
    #
    hd_sel = xray_structure.hd_selection()
    for i_seq, site_cart in enumerate(sites_cart):
        selection = maptbx.grid_indices_around_sites(
            unit_cell=map_coeffs.unit_cell(),
            fft_n_real=map_data.focus(),
            fft_m_real=map_data.all(),
            sites_cart=flex.vec3_double([site_cart]),
            site_radii=flex.double([1.5]))
        cc = flex.linear_correlation(
            x=map_data.select(selection),
            y=map_data2.select(selection)).coefficient()
        if (cc < 0.7 or hd_sel[i_seq]): sel_exclude[i_seq] = True
    return ~sel_exclude
示例#6
0
def get_cc(mc1, mc2, xrs):
    crystal_gridding = mc1.crystal_gridding(d_min=mc1.d_min(),
                                            resolution_factor=0.25)
    fft_map = miller.fft_map(crystal_gridding=crystal_gridding,
                             fourier_coefficients=mc1)
    fft_map.apply_sigma_scaling()
    m1 = fft_map.real_map_unpadded()
    fft_map = miller.fft_map(crystal_gridding=crystal_gridding,
                             fourier_coefficients=mc2)
    fft_map.apply_sigma_scaling()
    m2 = fft_map.real_map_unpadded()
    assert m1.focus() == m2.focus()
    assert m1.all() == m2.all()
    ccs = flex.double()
    for site_cart in xrs.sites_cart():
        sel = maptbx.grid_indices_around_sites(unit_cell=mc1.unit_cell(),
                                               fft_n_real=m1.focus(),
                                               fft_m_real=m1.all(),
                                               sites_cart=flex.vec3_double(
                                                   [site_cart]),
                                               site_radii=flex.double([1.5]))
        cc = flex.linear_correlation(x=m1.select(sel),
                                     y=m2.select(sel)).coefficient()
        ccs.append(cc)
    return ccs
示例#7
0
def cc_peak(cutoff, map_1=None,map_2=None, map_coeffs_1=None,map_coeffs_2=None):
  """
  Compute CCpeak as described in
    Acta Cryst. (2014). D70, 2593-2606
    Metrics for comparison of crystallographic maps
    A. Urzhumtsev, P. V. Afonine, V. Y. Lunin, T. C. Terwilliger and P. D. Adams
  """
  from cctbx import miller
  assert [map_1,map_2].count(None) in [0,2]
  assert [map_coeffs_1,map_coeffs_2].count(None) in [0,2]
  if([map_1,map_2].count(None)==0):
    # Maps are assumed to be quantile rank scaled (HE).
    return ext.cc_peak(map_1=map_1, map_2=map_2, cutoff=cutoff)
  elif([map_coeffs_1,map_coeffs_2].count(None)==0):
    d_min = min(map_coeffs_1.d_min(), map_coeffs_2.d_min())
    crystal_gridding = map_coeffs_1.crystal_gridding(
      d_min             = d_min,
      symmetry_flags    = use_space_group_symmetry,
      resolution_factor = 0.25)
    fft_map = miller.fft_map(
      crystal_gridding     = crystal_gridding,
      fourier_coefficients = map_coeffs_1)
    map_1 = fft_map.real_map_unpadded()
    fft_map = miller.fft_map(
      crystal_gridding     = crystal_gridding,
      fourier_coefficients = map_coeffs_2)
    map_2 = fft_map.real_map_unpadded()
    m1_he = volume_scale(map = map_1,  n_bins = 10000).map_data()
    m2_he = volume_scale(map = map_2,  n_bins = 10000).map_data()
    return ext.cc_peak(map_1=m1_he, map_2=m2_he, cutoff=cutoff)
  else:
    raise RuntimeError("Combination of inputs not supported.")
def get_cc(mc1, mc2, xrs):
    crystal_gridding = mc1.crystal_gridding(
        d_min=mc1.d_min(),
        symmetry_flags=maptbx.use_space_group_symmetry,
        resolution_factor=0.25)
    fft_map = miller.fft_map(crystal_gridding=crystal_gridding,
                             fourier_coefficients=mc1)
    fft_map.apply_sigma_scaling()
    m1 = fft_map.real_map_unpadded()
    fft_map = miller.fft_map(crystal_gridding=crystal_gridding,
                             fourier_coefficients=mc2)
    fft_map.apply_sigma_scaling()
    m2 = fft_map.real_map_unpadded()
    assert m1.focus() == m2.focus()
    assert m1.all() == m2.all()
    sel = maptbx.grid_indices_around_sites(
        unit_cell=mc1.unit_cell(),
        fft_n_real=m1.focus(),
        fft_m_real=m1.all(),
        sites_cart=flex.vec3_double(xrs.sites_cart()),
        site_radii=flex.double([1.5] * xrs.scatterers().size()))
    cc = flex.linear_correlation(x=m1.select(sel),
                                 y=m2.select(sel)).coefficient()

    def md(m, xrs):
        r = flex.double()
        for sf in xrs.sites_frac():
            r.append(m.eight_point_interpolation(sf))
        return flex.mean(r)

    return cc, md(m=m1, xrs=xrs), md(m=m2, xrs=xrs)
示例#9
0
 def __init__(
       self,
       fmodel,
       coeffs):
   # XXX see f_model.py: duplication! Consolidate.
   self.fmodel = fmodel
   self.coeffs = coeffs
   crystal_gridding = fmodel.f_obs().crystal_gridding(
     d_min              = self.fmodel.f_obs().d_min(),
     resolution_factor  = 1./3)
   fft_map = miller.fft_map(
     crystal_gridding     = crystal_gridding,
     fourier_coefficients = self.coeffs)
   fft_map.apply_sigma_scaling()
   map_data = fft_map.real_map_unpadded()
   rho_atoms = flex.double()
   for site_frac in self.fmodel.xray_structure.sites_frac():
     rho_atoms.append(map_data.eight_point_interpolation(site_frac))
   rho_mean = flex.mean_default(rho_atoms.select(rho_atoms>0.5), 0.5)
   sel_exclude = rho_atoms > min(rho_mean/2., 1)
   sites_cart = fmodel.xray_structure.sites_cart()
   #
   fft_map = miller.fft_map(
     crystal_gridding     = crystal_gridding,
     fourier_coefficients = self.fmodel.f_model())
   fft_map.apply_sigma_scaling()
   map_data2 = fft_map.real_map_unpadded()
   #
   for i_seq, site_cart in enumerate(sites_cart):
     selection = maptbx.grid_indices_around_sites(
       unit_cell  = self.coeffs.unit_cell(),
       fft_n_real = map_data.focus(),
       fft_m_real = map_data.all(),
       sites_cart = flex.vec3_double([site_cart]),
       site_radii = flex.double([1.5]))
     cc = flex.linear_correlation(x=map_data.select(selection),
       y=map_data2.select(selection)).coefficient()
     if(cc<0.7): sel_exclude[i_seq] = False
   #
   del map_data, fft_map, rho_atoms
   self.d_min = fmodel.f_obs().d_min()
   cs = self.fmodel.f_obs().average_bijvoet_mates().complete_set(d_min=self.d_min)
   self.complete_set = cs.array(data = flex.double(cs.indices().size(), 0))
   self.xray_structure_cut = self.fmodel.xray_structure.select(sel_exclude)
   self.missing_set = self.complete_set.common_set(self.coeffs)
   #
   self.f_calc_missing = self.complete_set.structure_factors_from_scatterers(
     xray_structure = self.xray_structure_cut).f_calc()
   self.ss_missing = 1./flex.pow2(self.f_calc_missing.d_spacings().data()) / 4.
   mask_manager = mmtbx.masks.manager(
     miller_array      = self.f_calc_missing,
     miller_array_twin = None,
     mask_params       = None)
   self.f_mask_missing = mask_manager.shell_f_masks(
     xray_structure = self.xray_structure_cut,
     force_update   = True)
   self.zero_data = flex.complex_double(self.f_calc_missing.data().size(), 0)
示例#10
0
 def __init__(
       self,
       fmodel,
       coeffs):
   # XXX see f_model.py: duplication! Consolidate.
   self.fmodel = fmodel
   self.coeffs = coeffs
   crystal_gridding = fmodel.f_obs().crystal_gridding(
     d_min              = self.fmodel.f_obs().d_min(),
     resolution_factor  = 1./3)
   fft_map = miller.fft_map(
     crystal_gridding     = crystal_gridding,
     fourier_coefficients = self.coeffs)
   fft_map.apply_sigma_scaling()
   map_data = fft_map.real_map_unpadded()
   rho_atoms = flex.double()
   for site_frac in self.fmodel.xray_structure.sites_frac():
     rho_atoms.append(map_data.eight_point_interpolation(site_frac))
   rho_mean = flex.mean_default(rho_atoms.select(rho_atoms>0.5), 0.5)
   sel_exclude = rho_atoms > min(rho_mean/2., 1)
   sites_cart = fmodel.xray_structure.sites_cart()
   #
   fft_map = miller.fft_map(
     crystal_gridding     = crystal_gridding,
     fourier_coefficients = self.fmodel.f_model())
   fft_map.apply_sigma_scaling()
   map_data2 = fft_map.real_map_unpadded()
   #
   for i_seq, site_cart in enumerate(sites_cart):
     selection = maptbx.grid_indices_around_sites(
       unit_cell  = self.coeffs.unit_cell(),
       fft_n_real = map_data.focus(),
       fft_m_real = map_data.all(),
       sites_cart = flex.vec3_double([site_cart]),
       site_radii = flex.double([1.5]))
     cc = flex.linear_correlation(x=map_data.select(selection),
       y=map_data2.select(selection)).coefficient()
     if(cc<0.7): sel_exclude[i_seq] = False
   #
   del map_data, fft_map, rho_atoms
   self.d_min = fmodel.f_obs().d_min()
   cs = self.fmodel.f_obs().average_bijvoet_mates().complete_set(d_min=self.d_min)
   self.complete_set = cs.array(data = flex.double(cs.indices().size(), 0))
   self.xray_structure_cut = self.fmodel.xray_structure.select(sel_exclude)
   self.missing_set = self.complete_set.common_set(self.coeffs)
   #
   self.f_calc_missing = self.complete_set.structure_factors_from_scatterers(
     xray_structure = self.xray_structure_cut).f_calc()
   self.ss_missing = 1./flex.pow2(self.f_calc_missing.d_spacings().data()) / 4.
   mask_manager = mmtbx.masks.manager(
     miller_array      = self.f_calc_missing,
     miller_array_twin = None,
     mask_params       = None)
   self.f_mask_missing = mask_manager.shell_f_masks(
     xray_structure = self.xray_structure_cut,
     force_update   = True)
   self.zero_data = flex.complex_double(self.f_calc_missing.data().size(), 0)
示例#11
0
 def __init__(self, f, f_000, n_cycles=100, resolution_factor=0.25, d_min=None,
              crystal_gridding=None, complete_set=None):
   self.f = f
   self.d_min = d_min
   self.map = None
   self.crystal_gridding = crystal_gridding
   from cctbx import miller
   if(self.d_min is None): self.d_min = self.f.d_min()
   if(complete_set is None):
     complete_set = self.f.complete_set(d_min = self.d_min)
   if(self.crystal_gridding is None):
     self.crystal_gridding = self.f.crystal_gridding(
       d_min                   = d_min,
       resolution_factor       = resolution_factor,
       grid_step               = None,
       symmetry_flags          = None,
       mandatory_factors       = None,
       max_prime               = 5,
       assert_shannon_sampling = True)
   self.f_mod = self.f.deep_copy()
   for i in xrange(n_cycles):
     fft_map = miller.fft_map(
       crystal_gridding     = self.crystal_gridding,
       fourier_coefficients = self.f_mod,
       f_000                = f_000)
     if(f_000 is not None): fft_map.apply_volume_scaling()
     self.map = fft_map.real_map_unpadded()
     convert_to_non_negative(self.map, 0)
     self.f_mod = complete_set.structure_factors_from_map(
       map            = self.map,
       use_scale      = True,
       anomalous_flag = False,
       use_sg         = False)
     self.f_mod = self.f.complete_with(other = self.f_mod, scale=True,
       replace_phases=True)
示例#12
0
 def _get_map(self):
   f_calc = self.xray_structure.structure_factors(d_min=self.d_min).f_calc()
   fft_map = miller.fft_map(
     crystal_gridding     = self.crystal_gridding,
     fourier_coefficients = f_calc)
   fft_map.apply_sigma_scaling() # XXX not really needed
   return fft_map.real_map_unpadded()
  def prepare_reference_map_3(self, xrs, pdb_h):
    """ with ramachandran outliers """

    print >> self.log, "Preparing reference map, method 3"
    outlier_selection_txt = mmtbx.building.loop_closure.utils. \
          rama_outliers_selection(pdb_h, self.rama_manager, 1)
    asc = pdb_h.atom_selection_cache()
    print >> self.log, "rama outlier selection:", outlier_selection_txt
    rama_out_sel = asc.selection(outlier_selection_txt)
    xrs=xrs.set_b_iso(value=50)

    # side_chain_no_cb_selection = ~ xrs.main_chain_selection()
    side_chain_no_cb_selection = ~ xrs.backbone_selection()
    xrs = xrs.set_b_iso(value=200, selection=side_chain_no_cb_selection)
    xrs = xrs.set_b_iso(value=150, selection=rama_out_sel)
    # xrs = xrs.set_occupancies(value=0.3, selection=rama_out_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,
        d_min             = self.params.reference_map_resolution)
    fc = xrs.structure_factors(d_min = self.params.reference_map_resolution, algorithm = "direct").f_calc()
    fft_map = miller.fft_map(
        crystal_gridding=crystal_gridding,
        fourier_coefficients=fc)
    fft_map.apply_sigma_scaling()
    self.reference_map = fft_map.real_map_unpadded(in_place=False)
    fft_map.as_xplor_map(file_name="%s_3.map" % self.params.output_prefix)
示例#14
0
 def _get_map(self):
   f_calc = self.xray_structure.structure_factors(d_min=self.d_min).f_calc()
   fft_map = miller.fft_map(
     crystal_gridding     = self.crystal_gridding,
     fourier_coefficients = f_calc)
   fft_map.apply_sigma_scaling() # XXX not really needed
   return fft_map.real_map_unpadded()
示例#15
0
 def _get_map_at_d_min(self, d_min):
     done = False
     cntr = 0
     while not done:
         if (cntr > 5):
             raise RuntimeError(
                 "Number of trial resolution increments exceeded.")
         try:
             f_obs_cmpl = self.complete_set.resolution_filter(
                 d_min=d_min).structure_factors_from_map(
                     map=self.map_data,
                     use_scale=True,
                     anomalous_flag=False,
                     use_sg=True)
             done = True
         except KeyboardInterrupt:
             raise
         except Exception as e:
             if (str(
                     e
             ) == "cctbx Error: Miller index not in structure factor map."):
                 d_min += 0.1
         cntr += 1
     fft_map = miller.fft_map(crystal_gridding=self.crystal_gridding,
                              fourier_coefficients=f_obs_cmpl)
     fft_map.apply_sigma_scaling()
     return fft_map.real_map_unpadded()
示例#16
0
 def fft_map(self,
             resolution_factor = 1/3.,
             symmetry_flags = None,
             map_coefficients = None,
             other_fft_map = None,
             map_type = None,
             force_anomalous_flag_false = None,
             acentrics_scale = 2.0,
             centrics_pre_scale = 1.0,
             use_all_data = True):
   if(map_coefficients is None):
     map_coefficients = self.map_coefficients(
       map_type           = map_type,
       acentrics_scale    = acentrics_scale,
       centrics_pre_scale = centrics_pre_scale)
     if(force_anomalous_flag_false):
       map_coefficients = map_coefficients.average_bijvoet_mates()
   if(force_anomalous_flag_false):
     map_coefficients = map_coefficients.average_bijvoet_mates()
   if(not use_all_data):
     map_coefficients = map_coefficients.select(self.fmodel.arrays.work_sel)
   if(other_fft_map is None):
     return map_coefficients.fft_map(
       resolution_factor = resolution_factor,
       symmetry_flags    = symmetry_flags)
   else:
     return miller.fft_map(
       crystal_gridding     = other_fft_map,
       fourier_coefficients = map_coefficients)
示例#17
0
 def compute_electron_density_map(self):
   """ Compute the electron density from the structure factors self.f_calc
   and the 000 component self.f_000, scaling by the unit cell volume """
   self.rho_map = miller.fft_map(self.crystal_gridding,
                                 self.f_calc,
                                 self.f_000)
   self.rho_map.apply_volume_scaling()
示例#18
0
 def iterations(self):
   self.cntr = 0
   while self.cntr < self.max_iterations:
     self.f_mem = self.f.structure_factors_from_map(
       map            = self.rho,
       use_scale      = False,
       anomalous_flag = False,
       use_sg         = False)
     self.f_mem = self.f_mem.customized_copy(data = self.f_mem.data()/self.N)
     fft_map = miller.fft_map(
       crystal_gridding     = self.crystal_gridding,
       fourier_coefficients = self.f_mem)
     rho_mod = fft_map.real_map_unpadded()
     rho_trial = self.rho.deep_copy()
     self.meio_obj = maptbx.mem_iteration(
       rho_mod,
       self.rho_obs,
       rho_trial,
       self.lam*self.N,
       self.n_real,
       self.Agd,
       self.beta,
       self.use_scale)
     if(self.detect_convergence and self.is_converged(rho_trial=rho_trial)):
       break
     else: self.rho = rho_trial
     self.Z = self.meio_obj.z()
     if(self.verbose): self.show()
     if(self.cntr%25==0): self.Agd = self.Agd/self.Z
     self.cntr += 1
     if(self.lambda_increment_factor is not None):
       self.lam *= self.lambda_increment_factor
   self.update_metrics()
示例#19
0
 def __init__(
       self,
       map_data_obs,
       xrs,
       d_min,
       vector_map,
       box_dimension=30):
   adopt_init_args(self, locals())
   self.crystal_gridding = maptbx.crystal_gridding(
     unit_cell             = self.xrs.unit_cell(),
     space_group_info      = self.xrs.space_group_info(),
     pre_determined_n_real = self.map_data_obs.all())
   self.n_real = self.crystal_gridding.n_real()
   crystal_gridding = maptbx.crystal_gridding(
     unit_cell             = self.xrs.unit_cell(),
     space_group_info      = self.xrs.space_group_info(),
     pre_determined_n_real = self.map_data_obs.all())
   mc = xrs.structure_factors(d_min=d_min).f_calc()
   fft_map = miller.fft_map(
     crystal_gridding     = crystal_gridding,
     fourier_coefficients = mc)
   fft_map.apply_sigma_scaling()
   self.map_data_calc = fft_map.real_map_unpadded()
   scale = scale_k1(x=self.map_data_obs, y=self.map_data_calc)
   self.map_data_calc = self.map_data_calc * scale
   #
   # result map
   self.map_result = flex.double(flex.grid(self.map_data_obs.all()))
   # iterate over boxes
   self.box_iterator()
示例#20
0
 def fft_map(self,
             resolution_factor = 1/3.,
             symmetry_flags = None,
             map_coefficients = None,
             other_fft_map = None,
             map_type = None,
             force_anomalous_flag_false = None,
             acentrics_scale = 2.0,
             centrics_pre_scale = 1.0,
             use_all_data = True):
   if(map_coefficients is None):
     map_coefficients = self.map_coefficients(
       map_type           = map_type,
       acentrics_scale    = acentrics_scale,
       centrics_pre_scale = centrics_pre_scale)
     if(force_anomalous_flag_false):
       map_coefficients = map_coefficients.average_bijvoet_mates()
   if(force_anomalous_flag_false):
     map_coefficients = map_coefficients.average_bijvoet_mates()
   if(not use_all_data):
     map_coefficients = map_coefficients.select(self.fmodel.arrays.work_sel)
   if(other_fft_map is None):
     return map_coefficients.fft_map(
       resolution_factor = resolution_factor,
       symmetry_flags    = symmetry_flags)
   else:
     return miller.fft_map(
       crystal_gridding     = other_fft_map,
       fourier_coefficients = map_coefficients)
def compute_map(target_map, xray_structure):
    mc = target_map.miller_array.structure_factors_from_scatterers(
        xray_structure=xray_structure).f_calc()
    fft_map = miller.fft_map(crystal_gridding=target_map.crystal_gridding,
                             fourier_coefficients=mc)
    fft_map.apply_sigma_scaling()
    return fft_map.real_map_unpadded()
def compute_map(target_map, xray_structure):
  mc = target_map.miller_array.structure_factors_from_scatterers(
    xray_structure = xray_structure).f_calc()
  fft_map = miller.fft_map(
    crystal_gridding     = target_map.crystal_gridding,
    fourier_coefficients = mc)
  fft_map.apply_sigma_scaling()
  return fft_map.real_map_unpadded()
 def compute_map(self):
   map_coefficients = self.miller_array.structure_factors_from_scatterers(
     xray_structure = self.xray_structure).f_calc()
   fft_map = miller.fft_map(crystal_gridding     = self.crystal_gridding,
                            fourier_coefficients = map_coefficients)
   fft_map.apply_sigma_scaling()
   result = fft_map.real_map_unpadded()
   return result
示例#24
0
 def compute_map(self):
   map_coefficients = self.miller_array.structure_factors_from_scatterers(
     xray_structure = self.xray_structure).f_calc()
   fft_map = miller.fft_map(crystal_gridding     = self.crystal_gridding,
                            fourier_coefficients = map_coefficients)
   fft_map.apply_sigma_scaling()
   result = fft_map.real_map_unpadded()
   return result
示例#25
0
def exercise_sampled_model_density_1():
    import iotbx.pdb
    pdb_str1 = """
CRYST1   10.000   10.000   10.000  90.00  90.00  90.00 P 1
ATOM      1  CB  PHE A   1       5.000   5.000   5.000  1.00 15.00           C
ANISOU    1  CB  PHE A   1      900   2900    100      0      0      0       C
TER
END
"""
    pdb_str2 = """
CRYST1   10.000   10.000   10.000  90.00  90.00  90.00 P 1
ATOM      1  CB  PHE A   1       5.000   5.000   5.000  1.00 15.00           C
TER
END
"""
    #
    for pdb_str in [pdb_str1, pdb_str2]:
        print
        pdb_inp = iotbx.pdb.input(source_info=None, lines=pdb_str)
        xrs = pdb_inp.xray_structure_simple()
        #
        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=0.1)
        m = mmtbx.real_space.sampled_model_density(
            xray_structure=xrs, n_real=crystal_gridding.n_real()).data()
        #
        max_index = [(i - 1) // 2 for i in crystal_gridding.n_real()]
        complete_set = miller.build_set(
            crystal_symmetry=xrs.crystal_symmetry(),
            anomalous_flag=False,
            max_index=max_index)
        indices = complete_set.indices()
        indices.append((0, 0, 0))
        #
        complete_set = complete_set.customized_copy(indices=indices)
        f_obs_cmpl = complete_set.structure_factors_from_map(
            map=m, use_scale=True, anomalous_flag=False, use_sg=False)
        fc = complete_set.structure_factors_from_scatterers(
            xray_structure=xrs).f_calc()
        #
        f1 = abs(fc).data()
        f2 = abs(f_obs_cmpl).data()
        r = 200 * flex.sum(flex.abs(f1 - f2)) / flex.sum(f1 + f2)
        assert r < 0.5
        print r
        #
        fft_map = miller.fft_map(crystal_gridding=crystal_gridding,
                                 fourier_coefficients=f_obs_cmpl)
        fft_map.apply_volume_scaling()
        m_ = fft_map.real_map_unpadded()
        print m.as_1d().min_max_mean().as_tuple()
        print m_.as_1d().min_max_mean().as_tuple()
        assert approx_equal(m.as_1d().min_max_mean().as_tuple(),
                            m_.as_1d().min_max_mean().as_tuple(),
                            1.e-3)  # Must be smaller!?
def simple(fmodel, pdb_hierarchy, params=None, log=None, show_results=False):
  if(params is None): params =master_params().extract()
  if(log is None): log = sys.stdout
  # compute map coefficients
  e_map_obj = fmodel.electron_density_map()
  coeffs_1 = e_map_obj.map_coefficients(
    map_type     = params.map_1.type,
    fill_missing = params.map_1.fill_missing_reflections,
    isotropize   = params.map_1.isotropize)
  coeffs_2 = e_map_obj.map_coefficients(
    map_type     = params.map_2.type,
    fill_missing = params.map_2.fill_missing_reflections,
    isotropize   = params.map_2.isotropize)
  # compute maps
  fft_map_1 = coeffs_1.fft_map(resolution_factor = params.resolution_factor)
  fft_map_1.apply_sigma_scaling()
  map_1 = fft_map_1.real_map_unpadded()
  fft_map_2 = miller.fft_map(
    crystal_gridding     = fft_map_1,
    fourier_coefficients = coeffs_2)
  fft_map_2.apply_sigma_scaling()
  map_2 = fft_map_2.real_map_unpadded()
  # compute cc
  broadcast(m="Map correlation and map values", log=log)
  overall_cc = flex.linear_correlation(x = map_1.as_1d(),
    y = map_2.as_1d()).coefficient()
  print >> log, "  Overall map cc(%s,%s): %6.4f"%(params.map_1.type,
    params.map_2.type, overall_cc)
  detail, atom_radius = params.detail, params.atom_radius
  detail, atom_radius = set_detail_level_and_radius(detail=detail,
    atom_radius=atom_radius, d_min=fmodel.f_obs().d_min())
  use_hydrogens = params.use_hydrogens
  if(use_hydrogens is None):
    if(params.scattering_table == "neutron" or fmodel.f_obs().d_min() <= 1.2):
      use_hydrogens = True
    else:
      use_hydrogens = False
  hydrogen_atom_radius = params.hydrogen_atom_radius
  if(hydrogen_atom_radius is None):
    if(params.scattering_table == "neutron"):
      hydrogen_atom_radius = atom_radius
    else:
      hydrogen_atom_radius = 1
  results = compute(
    pdb_hierarchy        = pdb_hierarchy,
    unit_cell            = fmodel.xray_structure.unit_cell(),
    fft_n_real           = map_1.focus(),
    fft_m_real           = map_1.all(),
    map_1                = map_1,
    map_2                = map_2,
    detail               = detail,
    atom_radius          = atom_radius,
    use_hydrogens        = use_hydrogens,
    hydrogen_atom_radius = hydrogen_atom_radius)
  if(show_results):
    show(log=log, results=results, params=params, detail=detail)
  return results
示例#27
0
 def compute_map(self, xray_structure):
   self.assert_pdb_hierarchy_xray_structure_sync()
   mc = self.target_map_object.miller_array.structure_factors_from_scatterers(
     xray_structure = xray_structure).f_calc()
   fft_map = miller.fft_map(
     crystal_gridding     = self.target_map_object.crystal_gridding,
     fourier_coefficients = mc)
   fft_map.apply_sigma_scaling()
   return fft_map.real_map_unpadded()
示例#28
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)
示例#29
0
 def compute_map(self, xray_structure):
     self.assert_pdb_hierarchy_xray_structure_sync()
     mc = self.target_map_object.miller_array.structure_factors_from_scatterers(
         xray_structure=xray_structure).f_calc()
     fft_map = miller.fft_map(
         crystal_gridding=self.target_map_object.crystal_gridding,
         fourier_coefficients=mc)
     fft_map.apply_sigma_scaling()
     return fft_map.real_map_unpadded()
 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()
示例#31
0
def get_map_data(xrs, d_min):
    cg = maptbx.crystal_gridding(unit_cell=xrs.unit_cell(),
                                 space_group_info=xrs.space_group_info(),
                                 step=0.1)
    fc = xrs.structure_factors(d_min=d_min, algorithm="direct").f_calc()
    fft_map = miller.fft_map(crystal_gridding=cg,
                             fourier_coefficients=fc,
                             f_000=xrs.f_000())
    fft_map.apply_volume_scaling()
    return fft_map.real_map_unpadded()
def map_cc(map_coeffs_1, map_coeffs_2):
    fft_map_1 = map_coeffs_1.fft_map(resolution_factor=0.25)
    map_1 = fft_map_1.real_map_unpadded()
    fft_map_2 = miller.fft_map(crystal_gridding=fft_map_1,
                               fourier_coefficients=map_coeffs_2)
    map_2 = fft_map_2.real_map_unpadded()
    assert map_1.size() == map_2.size()
    m1 = map_1.as_1d()
    m2 = map_2.as_1d()
    return flex.linear_correlation(x=m1, y=m2).coefficient()
示例#33
0
 def _get_map_at_d_min(self, d_min):
     f_obs_cmpl = self.complete_set.resolution_filter(
         d_min=d_min).structure_factors_from_map(map=self.map_data,
                                                 use_scale=True,
                                                 anomalous_flag=False,
                                                 use_sg=True)
     fft_map = miller.fft_map(crystal_gridding=self.crystal_gridding,
                              fourier_coefficients=f_obs_cmpl)
     fft_map.apply_sigma_scaling()
     return fft_map.real_map_unpadded()
def compute_map_from_model(high_resolution, low_resolution, xray_structure,
                           grid_resolution_factor, crystal_gridding = None):
  f_calc = xray_structure.structure_factors(d_min = high_resolution).f_calc()
  f_calc = f_calc.resolution_filter(d_max = low_resolution)
  if(crystal_gridding is None):
    return f_calc.fft_map(
      resolution_factor = min(0.5,grid_resolution_factor),
      symmetry_flags    = None)
  return miller.fft_map(
    crystal_gridding     = crystal_gridding,
    fourier_coefficients = f_calc)
def map_cc(map_coeffs_1, map_coeffs_2):
  fft_map_1 = map_coeffs_1.fft_map(resolution_factor=0.25)
  map_1 = fft_map_1.real_map_unpadded()
  fft_map_2 = miller.fft_map(
    crystal_gridding = fft_map_1,
    fourier_coefficients = map_coeffs_2)
  map_2 = fft_map_2.real_map_unpadded()
  assert map_1.size() == map_2.size()
  m1 = map_1.as_1d()
  m2 = map_2.as_1d()
  return flex.linear_correlation(x = m1, y = m2).coefficient()
示例#36
0
 def _get_map_at_d_min(self, d_min):
   f_obs_cmpl = self.complete_set.resolution_filter(
     d_min=d_min).structure_factors_from_map(
       map            = self.map_data,
       use_scale      = True,
       anomalous_flag = False,
       use_sg         = True)
   fft_map = miller.fft_map(
     crystal_gridding     = self.crystal_gridding,
     fourier_coefficients = f_obs_cmpl)
   fft_map.apply_sigma_scaling()
   return fft_map.real_map_unpadded()
示例#37
0
 def ft_dp(self, dp, u_extra):
     multiplier = (self.unit_cell().volume() /
                   matrix.row(self.rfft().n_real()).product() *
                   self.space_group().order_z() /
                   dp.multiplicities().data().as_double())
     coeff = dp.deep_copy()
     xray.apply_u_extra(self.unit_cell(), u_extra, coeff.indices(),
                        coeff.data())
     coeff_data = coeff.data()
     coeff_data *= flex.polar(multiplier, 0)
     return miller.fft_map(crystal_gridding=self.crystal_gridding(),
                           fourier_coefficients=coeff)
  def prepare_reference_map_3(self):
    """ with ramachandran outliers """
    xrs = self.model.get_xray_structure().deep_copy_scatterers()
    pdb_h = self.model.get_hierarchy()
    print("Preparing reference map, method 3", file=self.log)
    outlier_selection_txt = mmtbx.building.loop_closure.utils. \
          rama_score_selection(pdb_h, self.model.get_ramachandran_manager(), "outlier",1)
    asc = self.model.get_atom_selection_cache()
    # print >> self.log, "rama outlier selection:", outlier_selection_txt
    rama_out_sel = asc.selection(outlier_selection_txt)
    xrs=xrs.set_b_iso(value=50)

    # side_chain_no_cb_selection = ~ xrs.main_chain_selection()
    side_chain_no_cb_selection = ~ xrs.backbone_selection()
    xrs = xrs.set_b_iso(value=200, selection=side_chain_no_cb_selection)
    xrs = xrs.set_b_iso(value=150, selection=rama_out_sel)
    # xrs = xrs.set_occupancies(value=0.3, selection=rama_out_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,
        d_min             = self.params.reference_map_resolution)
    fc = xrs.structure_factors(
        d_min = self.params.reference_map_resolution,
        algorithm = "direct").f_calc()
    fft_map = miller.fft_map(
        crystal_gridding=crystal_gridding,
        fourier_coefficients=fc)
    fft_map.apply_sigma_scaling()
    self.reference_map = fft_map.real_map_unpadded(in_place=False)
    if self.params.debug:
      fft_map.as_xplor_map(file_name="%s_3.map" % self.params.output_prefix)
    self.master_map = self.reference_map.deep_copy()
    if self.model.ncs_constraints_present():
      # here we are negating non-master part of the model
      # self.master_sel=master_sel
      # self.master_map = self.reference_map.deep_copy()
      mask = maptbx.mask(
              xray_structure=xrs.select(self.model.get_master_selection()),
              n_real=self.master_map.focus(),
              mask_value_inside_molecule=1,
              mask_value_outside_molecule=-1,
              solvent_radius=0,
              atom_radius=1.)
      self.master_map = self.reference_map * mask
      if self.params.debug:
        iotbx.mrcfile.write_ccp4_map(
            file_name="%s_3_master.map" % self.params.output_prefix,
            unit_cell=xrs.unit_cell(),
            space_group=xrs.space_group(),
            map_data=self.master_map,
            labels=flex.std_string([""]))
示例#39
0
def run():
  target_structure = random_structure.xray_structure(
    space_group_info=sgtbx.space_group_info("I432"),
    elements=['C']*6+['O'],
    use_u_iso=False,
    use_u_aniso=True,
  )
  shift = tuple(flex.random_double(3))
  print "shift to be found: (%.3f, %.3f, %.3f)" % shift
  target_structure_in_p1 = target_structure.expand_to_p1().apply_shift(shift)
  miller_indices = miller.build_set(
    crystal_symmetry=target_structure,
    anomalous_flag=True,
    d_min=0.8)
  f_obs = miller_indices.structure_factors_from_scatterers(
    xray_structure=target_structure,
    algorithm="direct").f_calc().amplitudes()
  miller_indices_in_p1 = miller.build_set(
    crystal_symmetry=target_structure_in_p1,
    anomalous_flag=True,
    d_min=0.8)
  f_calc = miller_indices_in_p1.structure_factors_from_scatterers(
      xray_structure=target_structure_in_p1,
      algorithm="direct").f_calc()
  crystal_gridding = f_calc.crystal_gridding(
    symmetry_flags=translation_search.symmetry_flags(
      is_isotropic_search_model=False,
      have_f_part=False),
    resolution_factor=1/2
  )
  omptbx.env.num_threads = 1
  t_fast_tf = show_times()
  fast_tf_map = translation_search.fast_nv1995(
    gridding=crystal_gridding.n_real(),
    space_group=f_obs.space_group(),
    anomalous_flag=f_obs.anomalous_flag(),
    miller_indices_f_obs=f_obs.indices(),
    f_obs=f_obs.data(),
    f_part=flex.complex_double(), ## no sub-structure is already fixed
    miller_indices_p1_f_calc=f_calc.indices(),
    p1_f_calc=f_calc.data()).target_map()
  print
  print "Fast translation function"
  t_fast_tf()
  t_cross_corr = show_times()
  for op in target_structure.space_group():
    f, op_times_f = f_calc.original_and_transformed(op)
    cross_corr_map = miller.fft_map(crystal_gridding,
                                    f * op_times_f.conjugate().data())
  print
  print "Traditional cross-correlation"
  t_cross_corr()
示例#40
0
def compute_map_from_model(high_resolution, low_resolution, xray_structure,
                           grid_resolution_factor=None,
                           crystal_gridding = None):
  f_calc = xray_structure.structure_factors(d_min = high_resolution).f_calc()
  if (low_resolution is not None):
    f_calc = f_calc.resolution_filter(d_max = low_resolution)
  if (crystal_gridding is None):
    return f_calc.fft_map(
      resolution_factor = min(0.5,grid_resolution_factor),
      symmetry_flags    = None)
  return miller.fft_map(
    crystal_gridding     = crystal_gridding,
    fourier_coefficients = f_calc)
示例#41
0
def good_atoms_selection(
      crystal_gridding,
      map_coeffs,
      xray_structure):
  #XXX copy from model_missing_reflections map_tools.py, consolidate later
  #XXX Also look for similar crap in f_model.py
  fft_map = miller.fft_map(
    crystal_gridding     = crystal_gridding,
    fourier_coefficients = map_coeffs)
  fft_map.apply_sigma_scaling()
  map_data = fft_map.real_map_unpadded()
  rho_atoms = flex.double()
  for site_frac in xray_structure.sites_frac():
    rho_atoms.append(map_data.eight_point_interpolation(site_frac))
  #rho_mean = flex.mean_default(rho_atoms.select(rho_atoms>1.0), 1.0)
  sel_exclude = rho_atoms < 1.0 # XXX ??? TRY 0.5!
  sites_cart = xray_structure.sites_cart()
  #
  f_calc = map_coeffs.structure_factors_from_scatterers(
    xray_structure = xray_structure).f_calc()
  fft_map = miller.fft_map(
    crystal_gridding     = crystal_gridding,
    fourier_coefficients = f_calc)
  fft_map.apply_sigma_scaling()
  map_data2 = fft_map.real_map_unpadded()
  #
  hd_sel = xray_structure.hd_selection()
  for i_seq, site_cart in enumerate(sites_cart):
    selection = maptbx.grid_indices_around_sites(
      unit_cell  = map_coeffs.unit_cell(),
      fft_n_real = map_data.focus(),
      fft_m_real = map_data.all(),
      sites_cart = flex.vec3_double([site_cart]),
      site_radii = flex.double([1.5]))
    cc = flex.linear_correlation(x=map_data.select(selection),
      y=map_data2.select(selection)).coefficient()
    if(cc<0.7 or hd_sel[i_seq]): sel_exclude[i_seq] = True
  return ~sel_exclude
示例#42
0
def get_map(fmodel):
  map_coeffs = map_tools.electron_density_map(fmodel = fmodel).map_coefficients(
    map_type     = "2mFo-DFc",
    isotropize   = True,
    fill_missing = False)
  crystal_gridding = fmodel.f_obs().crystal_gridding(
    d_min             = fmodel.f_obs().d_min(),
    symmetry_flags    = maptbx.use_space_group_symmetry,
    resolution_factor = 1./4)
  fft_map = miller.fft_map(
    crystal_gridding     = crystal_gridding,
    fourier_coefficients = map_coeffs)
  fft_map.apply_sigma_scaling()
  return fft_map.real_map_unpadded()
示例#43
0
 def _filter_by_map_model_cc(self):
     self.ma.add("  start: %d" % self.xrs_water.scatterers().size())
     # Compute model Fourier map
     m_all = self.model.deep_copy()
     m_all.add_solvent(solvent_xray_structure=self.xrs_water,
                       atom_name="O",
                       residue_name="HOH",
                       chain_id="S",
                       refine_adp="isotropic")
     m_all.setup_scattering_dictionaries(
         scattering_table=self.scattering_table)
     xrs_all = m_all.get_xray_structure()
     f_calc = xrs_all.structure_factors(d_min=self.resolution).f_calc()
     crystal_gridding = maptbx.crystal_gridding(
         unit_cell=xrs_all.unit_cell(),
         space_group_info=xrs_all.space_group_info(),
         pre_determined_n_real=self.map_data.accessor().all())
     fft_map = miller.fft_map(crystal_gridding=crystal_gridding,
                              fourier_coefficients=f_calc)
     map_calc = fft_map.real_map_unpadded()
     sites_cart = xrs_all.sites_cart()
     # Remove water by CC
     wsel = m_all.selection(string="water")
     for i, s in enumerate(wsel):
         if not s: continue  # XXX
         cc = mmtbx.maps.correlation.from_map_map_atom(
             map_1=self.map_data_resampled,
             map_2=map_calc,
             site_cart=sites_cart[i],
             unit_cell=xrs_all.unit_cell(),
             radius=self.atom_radius)
         if cc < self.cc_mask_filter_threshold: wsel[i] = False
     self.xrs_water = m_all.select(wsel).get_xray_structure()
     # Exclude poor macro-molecule atoms from interaction analysis
     sites_cart = self.model.get_sites_cart()
     for i, s in enumerate(self.interaction_selection):
         if not s: continue  # XXX
         cc = mmtbx.maps.correlation.from_map_map_atom(
             map_1=self.map_data_resampled,
             map_2=map_calc,
             site_cart=sites_cart[i],
             unit_cell=xrs_all.unit_cell(),
             radius=self.atom_radius)
         if cc < self.cc_mask_threshold_interacting_atoms:
             self.interaction_selection[i] = False
     self.sites_frac_interaction = self.model.get_sites_frac().select(
         self.interaction_selection)
     #
     self.ma.add("  final: %d" % self.xrs_water.scatterers().size())
示例#44
0
 def _get_map_calc(self):
   if(self.box is True):
     f_calc = miller.structure_factor_box_from_map(
       crystal_symmetry = self.xray_structure.crystal_symmetry(),
       n_real           = self.map.focus()).structure_factors_from_scatterers(
         xray_structure = self.xray_structure).f_calc()
     d_spacings = f_calc.d_spacings().data()
     sel = d_spacings > d_min
     f_calc = f_calc.select(sel)
   else:
     f_calc = self.xray_structure.structure_factors(d_min=self.d_min).f_calc()
   fft_map = miller.fft_map(
     crystal_gridding     = self.crystal_gridding,
     fourier_coefficients = f_calc)
   return fft_map.real_map_unpadded()
示例#45
0
def run():
    target_structure = random_structure.xray_structure(
        space_group_info=sgtbx.space_group_info("I432"),
        elements=['C'] * 6 + ['O'],
        use_u_iso=False,
        use_u_aniso=True,
    )
    shift = tuple(flex.random_double(3))
    print "shift to be found: (%.3f, %.3f, %.3f)" % shift
    target_structure_in_p1 = target_structure.expand_to_p1().apply_shift(shift)
    miller_indices = miller.build_set(crystal_symmetry=target_structure,
                                      anomalous_flag=True,
                                      d_min=0.8)
    f_obs = miller_indices.structure_factors_from_scatterers(
        xray_structure=target_structure,
        algorithm="direct").f_calc().amplitudes()
    miller_indices_in_p1 = miller.build_set(
        crystal_symmetry=target_structure_in_p1,
        anomalous_flag=True,
        d_min=0.8)
    f_calc = miller_indices_in_p1.structure_factors_from_scatterers(
        xray_structure=target_structure_in_p1, algorithm="direct").f_calc()
    crystal_gridding = f_calc.crystal_gridding(
        symmetry_flags=translation_search.symmetry_flags(
            is_isotropic_search_model=False, have_f_part=False),
        resolution_factor=1 / 2)
    omptbx.env.num_threads = 1
    t_fast_tf = show_times()
    fast_tf_map = translation_search.fast_nv1995(
        gridding=crystal_gridding.n_real(),
        space_group=f_obs.space_group(),
        anomalous_flag=f_obs.anomalous_flag(),
        miller_indices_f_obs=f_obs.indices(),
        f_obs=f_obs.data(),
        f_part=flex.complex_double(),  ## no sub-structure is already fixed
        miller_indices_p1_f_calc=f_calc.indices(),
        p1_f_calc=f_calc.data()).target_map()
    print
    print "Fast translation function"
    t_fast_tf()
    t_cross_corr = show_times()
    for op in target_structure.space_group():
        f, op_times_f = f_calc.original_and_transformed(op)
        cross_corr_map = miller.fft_map(crystal_gridding,
                                        f * op_times_f.conjugate().data())
    print
    print "Traditional cross-correlation"
    t_cross_corr()
 def ft_dp(self, dp, u_extra):
   multiplier = (  self.unit_cell().volume()
                 / matrix.row(self.rfft().n_real()).product()
                 * self.space_group().order_z()
                 / dp.multiplicities().data().as_double())
   coeff = dp.deep_copy()
   xray.apply_u_extra(
     self.unit_cell(),
     u_extra,
     coeff.indices(),
     coeff.data())
   coeff_data = coeff.data()
   coeff_data *= flex.polar(multiplier, 0)
   return miller.fft_map(
     crystal_gridding=self.crystal_gridding(),
     fourier_coefficients=coeff)
def scale_two_real_maps_in_fourier_space(m1, m2, cs, d_min, vector_map):
    f1 = maptbx.map_to_map_coefficients(m=m1, cs=cs, d_min=d_min)
    f2 = maptbx.map_to_map_coefficients(m=m2, cs=cs, d_min=d_min)
    if (vector_map):
        f2 = f2.phase_transfer(phase_source=f1)
    ss = 1. / flex.pow2(f1.d_spacings().data()) / 4.
    bs = flex.double([i for i in xrange(0, 100)])
    mc = mmtbx.bulk_solvent.complex_f_minus_f_kb_scaled(
        f1.data(), f2.data(), bs, ss)
    crystal_gridding = maptbx.crystal_gridding(
        unit_cell=cs.unit_cell(),
        space_group_info=cs.space_group_info(),
        pre_determined_n_real=m1.all())
    fft_map = miller.fft_map(crystal_gridding=crystal_gridding,
                             fourier_coefficients=f1.array(data=mc))
    return fft_map.real_map_unpadded()
示例#48
0
 def get_poler_diff_map(f_obs):
     fmodel = mmtbx.f_model.manager(f_obs=f_obs,
                                    r_free_flags=r_free_flags,
                                    f_calc=f_calc,
                                    f_mask=f_mask)
     fmodel.update_all_scales(remove_outliers=False)
     mc_diff = map_tools.electron_density_map(
         fmodel=fmodel).map_coefficients(map_type="mFo-DFc",
                                         isotropize=True,
                                         fill_missing=False)
     fft_map = miller.fft_map(crystal_gridding=cpm_obj.crystal_gridding,
                              fourier_coefficients=mc_diff)
     fft_map.apply_sigma_scaling()
     map_data = fft_map.real_map_unpadded()
     return mmtbx.utils.extract_box_around_model_and_map(
         xray_structure=xrs_selected, map_data=map_data, box_cushion=2.1)
示例#49
0
 def optimize(d_min_min, d_min_max, step):
     d_min = d_min_min
     d_min_result = None
     cc_best = -1.e6
     while d_min < d_min_max + 1.e-6:
         f_calc = xrs.structure_factors(d_min=d_min).f_calc()
         fft_map = miller.fft_map(crystal_gridding=cg,
                                  fourier_coefficients=f_calc)
         map_data_ = fft_map.real_map_unpadded()
         cc = flex.linear_correlation(
             x=map_data_selected_as_1d,
             y=map_data_.select(sel).as_1d()).coefficient()
         if (cc > cc_best):
             cc_best = cc
             d_min_result = d_min
         d_min += step
     return d_min_result
示例#50
0
 def optimize(d_min_min, d_min_max, step):
   d_min = d_min_min
   d_min_result = None
   cc_best=-1.e6
   while d_min < d_min_max+1.e-6:
     f_calc = xrs.structure_factors(d_min=d_min).f_calc()
     fft_map = miller.fft_map(
       crystal_gridding     = cg,
       fourier_coefficients = f_calc)
     map_data_ = fft_map.real_map_unpadded()
     cc = flex.linear_correlation(
       x=map_data_selected_as_1d,
       y=map_data_.select(sel).as_1d()).coefficient()
     if(cc > cc_best):
       cc_best = cc
       d_min_result = d_min
     d_min += step
   return d_min_result
 def prepare_reference_map(self, xrs, pdb_h):
   print >> self.log, "Preparing reference map"
   # new_h = pdb_h.deep_copy()
   # truncate_to_poly_gly(new_h)
   # xrs = new_h.extract_xray_structure(crystal_symmetry=xrs.crystal_symmetry())
   xrs=xrs.set_b_iso(value=50)
   crystal_gridding = maptbx.crystal_gridding(
       unit_cell        = xrs.unit_cell(),
       space_group_info = xrs.space_group_info(),
       symmetry_flags   = maptbx.use_space_group_symmetry,
       d_min             = self.params.reference_map_resolution)
   fc = xrs.structure_factors(d_min = self.params.reference_map_resolution, algorithm = "fft").f_calc()
   fft_map = miller.fft_map(
       crystal_gridding=crystal_gridding,
       fourier_coefficients=fc)
   fft_map.apply_sigma_scaling()
   self.reference_map = fft_map.real_map_unpadded(in_place=False)
   fft_map.as_xplor_map(file_name="%s.map" % self.params.output_prefix)
def run(pdb_str, d_min, b):
    #
    pdb_inp = iotbx.pdb.input(source_info=None, lines=pdb_str_box)
    cs = pdb_inp.crystal_symmetry()
    ph = pdb_inp.construct_hierarchy()
    xrs = ph.extract_xray_structure(crystal_symmetry=cs)
    xrs = xrs.set_b_iso(value=b)
    fc = xrs.structure_factors(d_min=d_min).f_calc()
    cg = maptbx.crystal_gridding(unit_cell=cs.unit_cell(),
                                 space_group_info=cs.space_group_info(),
                                 d_min=d_min)
    fft_map = miller.fft_map(crystal_gridding=cg, fourier_coefficients=fc)
    map_obs = fft_map.real_map_unpadded()
    #
    pdb_inp = iotbx.pdb.input(source_info=None, lines=pdb_str)
    xrs = ph.extract_xray_structure(crystal_symmetry=cs)
    o = maptbx.resolution_from_map_and_model.run(map_data=map_obs,
                                                 xray_structure=xrs)
    return o.d_min, o.b_iso, o.d_fsc_model
示例#53
0
 def get_poler_diff_map(f_obs):
   fmodel = mmtbx.f_model.manager(
     f_obs        = f_obs,
     r_free_flags = r_free_flags,
     f_calc       = f_calc,
     f_mask       = f_mask)
   fmodel.update_all_scales(remove_outliers=False)
   mc_diff = map_tools.electron_density_map(
     fmodel = fmodel).map_coefficients(
       map_type         = "mFo-DFc",
       isotropize       = True,
       fill_missing     = False)
   fft_map = miller.fft_map(
     crystal_gridding     = cpm_obj.crystal_gridding,
     fourier_coefficients = mc_diff)
   fft_map.apply_sigma_scaling()
   map_data = fft_map.real_map_unpadded()
   return mmtbx.utils.extract_box_around_model_and_map(
     xray_structure = xrs_selected,
     map_data       = map_data,
     box_cushion    = 2.1)
示例#54
0
def exercise1():
  pdb_str="""
CRYST1   10.000  10.000   10.000  90.00  90.00  90.00 P 1
HETATM    1  C    C      1       2.000   2.000   2.000  1.00 20.00           C
END
"""
  pdb_inp = iotbx.pdb.input(source_info=None, lines=pdb_str)
  xrs = pdb_inp.xray_structure_simple()
  cg = maptbx.crystal_gridding(unit_cell=xrs.unit_cell(),
      pre_determined_n_real=(100,100,100),
      space_group_info=xrs.space_group_info())
  fc = xrs.structure_factors(d_min = 1., algorithm = "direct").f_calc()
  fft_map = miller.fft_map(crystal_gridding=cg, fourier_coefficients=fc)
  map_data = fft_map.real_map_unpadded()
  # pass map and threshold value
  co = maptbx.connectivity(map_data=map_data, threshold=100.)
  # get 'map' of the same size with integers: 0 where below threshold,
  # 1,2,3... - for connected regions
  map_result = co.result()
  # to find out the number of connected region for particular point:
  assert map_result[0,0,0] == 0    # means under threshold
  assert map_result[20,20,20] == 1 # blob 1

  # get 1d array of integer volumes and transform it to list.
  volumes = list(co.regions())
  # find max volume (except volume of 0-region which will be probably max)
  max_volume = max(volumes[1:])
  # find number of the region with max volume
  max_index = volumes.index(max_volume)
  v=[0,0,0]
  for i in range(3):
    # !!! Do not do this because it's extremely slow! Used for test purposes.
    v[i] = (map_result==i).count(True)

  assert v[2] == 0
  assert v[1] < 15000
  assert v[0]+v[1]+v[2] == 1000000
  assert volumes == v[:2]
示例#55
0
def exercise3():
  pdb_str="""
CRYST1   10.000  10.000   10.000  90.00  90.00  90.00 P 1
HETATM    1  C    C      1       2.000   2.000   2.000  1.00  2.00           C
HETATM    1  C    C      1       3.500   2.000   2.000  1.00  2.00           C
END
"""

  pdb_inp = iotbx.pdb.input(source_info=None, lines=pdb_str)
  xrs = pdb_inp.xray_structure_simple()
  cg = maptbx.crystal_gridding(unit_cell=xrs.unit_cell(),
      pre_determined_n_real=(100,100,100),
      space_group_info=xrs.space_group_info())
  fc = xrs.structure_factors(d_min = 1., algorithm = "direct").f_calc()
  fft_map = miller.fft_map(crystal_gridding=cg, fourier_coefficients=fc)
  fft_map.apply_sigma_scaling()
  map_data = fft_map.real_map_unpadded()
  #all filled
  v, volumes = getvs(map_data, -100)
  v2, volumes2 = getvs(map_data, -100, False)
  assert v == v2 == [0, 1000000, 0]
  assert v[:2] == v2[:2] == volumes == volumes2
  # can see one blob
  v, volumes = getvs(map_data, 5)
  assert v[0]+v[1]+v[2] == 1000000
  assert v[2] == 0
  assert v[:2] == volumes
  # can see separate, approx equal volume bloobs
  v, volumes = getvs(map_data, 10)
  assert v[0]+v[1]+v[2] == 1000000
  assert abs(v[1] - v[2]) < 5
  assert v == volumes
  # nothing to see
  v, volumes = getvs(map_data, 1000)
  assert v == [1000000, 0, 0]
  assert v[:1] == volumes
示例#56
0
  def __init__ (self, pdb_hierarchy, xray_structure, fmodel,
                distance_cutoff=4.0, collect_all=True,
                molprobity_map_params=None) :
    validation.__init__(self)
    from mmtbx.real_space_correlation import extract_map_stats_for_single_atoms
    from cctbx import adptbx
    from scitbx.matrix import col
    self.n_bad = 0
    self.n_heavy = 0
    pdb_atoms = pdb_hierarchy.atoms()
    if(len(pdb_atoms)>1):
      assert (not pdb_atoms.extract_i_seq().all_eq(0))
    unit_cell = xray_structure.unit_cell()
    pair_asu_table = xray_structure.pair_asu_table(
      distance_cutoff = distance_cutoff)
    asu_mappings = pair_asu_table.asu_mappings()
    asu_table = pair_asu_table.table()
    u_isos = xray_structure.extract_u_iso_or_u_equiv()
    occupancies = xray_structure.scatterers().extract_occupancies()
    sites_cart = xray_structure.sites_cart()
    sites_frac = xray_structure.sites_frac()
    sel_cache = pdb_hierarchy.atom_selection_cache()
    water_sel = sel_cache.selection("resname HOH and name O")

    if (molprobity_map_params is not None):
      # assume parameters have been validated (symmetry of pdb and map matches)
      two_fofc_map = None
      fc_map = None
      d_min = None
      crystal_gridding = None

      # read two_fofc_map
      if (molprobity_map_params.map_file_name is not None):
        f = any_file(molprobity_map_params.map_file_name)
        two_fofc_map = f.file_object.map_data()
        d_min = molprobity_map_params.d_min
        crystal_gridding = maptbx.crystal_gridding(
          f.file_object.unit_cell(),
          space_group_info=space_group_info(f.file_object.space_group_number),
          pre_determined_n_real=f.file_object.unit_cell_grid)
      elif (molprobity_map_params.map_coefficients_file_name is not None):
        f = any_file(molprobity_map_params.map_coefficients_file_name)
        fourier_coefficients = f.file_server.get_miller_array(
          molprobity_map_params.map_coefficients_label)
        crystal_symmetry = fourier_coefficients.crystal_symmetry()
        d_min = fourier_coefficients.d_min()
        crystal_gridding = maptbx.crystal_gridding(
          crystal_symmetry.unit_cell(), d_min, resolution_factor=0.25,
          space_group_info=crystal_symmetry.space_group_info())
        two_fofc_map = miller.fft_map(
          crystal_gridding=crystal_gridding,
          fourier_coefficients=fourier_coefficients).apply_sigma_scaling().\
          real_map_unpadded()

      # calculate fc_map
      assert( (d_min is not None) and (crystal_gridding is not None) )
      f_calc = xray_structure.structure_factors(d_min=d_min).f_calc()
      fc_map = miller.fft_map(crystal_gridding=crystal_gridding,
                              fourier_coefficients=f_calc)
      fc_map = fc_map.apply_sigma_scaling().real_map_unpadded()

      map_stats = extract_map_stats_for_single_atoms(
        pdb_atoms=pdb_atoms,
        xray_structure=xray_structure,
        fmodel=None,
        selection=water_sel,
        fc_map=fc_map,
        two_fofc_map=two_fofc_map)
    else:
      map_stats = extract_map_stats_for_single_atoms(
        pdb_atoms=pdb_atoms,
        xray_structure=xray_structure,
        fmodel=fmodel,
        selection=water_sel)
    waters = []
    for i_seq, atom in enumerate(pdb_atoms) :
      if (water_sel[i_seq]) :
        rt_mx_i_inv = asu_mappings.get_rt_mx(i_seq, 0).inverse()
        self.n_total += 1
        asu_dict = asu_table[i_seq]
        nearest_atom = nearest_contact = None
        for j_seq, j_sym_groups in asu_dict.items() :
          atom_j = pdb_atoms[j_seq]
          site_j = sites_frac[j_seq]
          # Filter out hydrogens
          if atom_j.element.upper().strip() in ["H", "D"]:
            continue
          for j_sym_group in j_sym_groups:
            rt_mx = rt_mx_i_inv.multiply(asu_mappings.get_rt_mx(j_seq,
              j_sym_group[0]))
            site_ji = rt_mx * site_j
            site_ji_cart = xray_structure.unit_cell().orthogonalize(site_ji)
            vec_i = col(atom.xyz)
            vec_ji = col(site_ji_cart)
            dxyz = abs(vec_i - vec_ji)
            if (nearest_contact is None) or (dxyz < nearest_contact) :
              nearest_contact = dxyz
              nearest_atom = atom_info(pdb_atom=atom_j, symop=rt_mx)
        w = water(
          pdb_atom=atom,
          b_iso=adptbx.u_as_b(u_isos[i_seq]),
          occupancy=occupancies[i_seq],
          nearest_contact=nearest_contact,
          nearest_atom=nearest_atom,
          score=map_stats.two_fofc_ccs[i_seq],
          fmodel=map_stats.fmodel_values[i_seq],
          two_fofc=map_stats.two_fofc_values[i_seq],
          fofc=map_stats.fofc_values[i_seq],
          anom=map_stats.anom_values[i_seq],
          n_hbonds=None) # TODO
        if (w.is_bad_water()) :
          w.outlier = True
          self.n_bad += 1
        elif (w.is_heavy_atom()) :
          w.outlier = True
          self.n_heavy += 1
        if (w.outlier) or (collect_all) :
          self.results.append(w)
    self.n_outliers = len(self.results)
示例#57
0
def get_map(map_coeffs, crystal_gridding):
  fft_map = miller.fft_map(
    crystal_gridding     = crystal_gridding,
    fourier_coefficients = map_coeffs)
  fft_map.apply_sigma_scaling()
  return fft_map.real_map_unpadded()
示例#58
0
 def __init__ (self,
               f,
               f_000             = None,
               lam               = None,
               start_map         = "lde",
               resolution_factor = 0.25,
               mean_density      = 0.375,
               max_iterations    = 2000,
               beta              = 0.9,
               use_modification  = True,
               xray_structure    = None,
               verbose           = False,
               lambda_increment_factor = None,
               convergence_at_r_factor = 0,
               convergence_r_threshold = 0.1,
               detect_convergence = True,
               crystal_gridding  = None,
               use_scale         = True,
               log               = None):
   if (log is None) : log = sys.stdout
   self.log              = log
   self.start_map        = start_map
   assert start_map in ["flat", "lde", "min_shifted"]
   self.lam              = lam
   self.max_iterations   = max_iterations
   self.f_000            = f_000
   self.verbose          = verbose
   self.beta             = beta
   self.f                = f
   self.use_modification = use_modification
   self.meio_obj         = None
   self.xray_structure   = xray_structure
   self.lambda_increment_factor = lambda_increment_factor
   self.convergence_at_r_factor = convergence_at_r_factor
   self.detect_convergence      = detect_convergence
   self.crystal_gridding        = crystal_gridding
   self.use_scale               = use_scale
   self.convergence_r_threshold = convergence_r_threshold
   #
   if(self.f.anomalous_flag()):
      merged = self.f.as_non_anomalous_array().merge_equivalents()
      self.f = merged.array().set_observation_type( self.f )
   # current monitor and optimized functional values
   self.cntr         = None
   self.r            = None
   self.h_n          = None
   self.h_w          = None
   self.Z            = None
   self.scale_kc     = None
   self.a_gd         = None
   self.q_x          = None
   self.q_tot        = None
   self.tp           = None
   self.f_mem        = None
   self.header_shown = False
   self.cc           = None
   self.r_factors    = flex.double()
   self.cc_to_answer = flex.double()
   #
   if(self.crystal_gridding is None):
     self.crystal_gridding = self.f.crystal_gridding(
       d_min                   = self.f.d_min(),
       resolution_factor       = resolution_factor,
       grid_step               = None,
       symmetry_flags          = None,
       mandatory_factors       = None,
       max_prime               = 5,
       assert_shannon_sampling = True)
   self.n_real = self.crystal_gridding.n_real()
   max_index = [int((i-1)/2.) for i in self.n_real]
   self.N = self.n_real[0]*self.n_real[1]*self.n_real[2]
   self.full_set = self.f.complete_set(max_index=max_index)
   self.f_calc = None
   if(self.xray_structure is not None):
     self.f_calc = self.full_set.structure_factors_from_scatterers(
       xray_structure = self.xray_structure).f_calc()
   if(verbose):
     print >> self.log, "Resolution factor: %-6.4f"%resolution_factor
     print >> self.log, \
       "  N, n1,n2,n3:",self.N,self.n_real[0],self.n_real[1],self.n_real[2]
     print >> self.log, "Box: "
     print >> self.log, "  resolution: %6.4f"%self.full_set.d_min()
     print >> self.log, "  max. index |h|,|k|,|l|<nreal/2:", max_index
     print >> self.log, "  n.refl.:", self.full_set.indices().size()
   # STEP 1
   if(self.f_000 is None): self.f_000=mean_density*self.f.unit_cell().volume()
   Cobs = 1
   Ca = 0.37
   self.Agd = Ca/self.N
   if(verbose):
     print >> self.log, \
       "Cobs, Ca, Agd, f_000:", Cobs, Ca, self.Agd, "%6.3f"%self.f_000
     print >> self.log, "Cobs/(N*f_000): ",Cobs/(self.N*self.f_000)
     print >> self.log, "memory factor (beta):", self.beta
   self.f = self.f.customized_copy(data=self.f.data()*Cobs/(self.N*self.f_000))
   fft_map = miller.fft_map(
     crystal_gridding     = self.crystal_gridding,
     fourier_coefficients = self.f)
   self.rho_obs = fft_map.real_map_unpadded()
   if(verbose):
     show_map_stat(m = self.rho_obs, prefix = "rho_obs (formula #13)",
       out=self.log)
   # STEP 2
   self.rho = self.normalize_start_map()
   if(verbose):
     show_map_stat(m = self.rho, prefix = "rho_0 (initial approximation)",
       out=self.log)
   # STEP 3
   self.iterations()
def run(args, log = sys.stdout, as_gui_program=False):
  if(len(args)==0):
    parsed = defaults(log=log)
    parsed.show(prefix="  ", out=log)
    return
  command_line = (option_parser()
                  .enable_symmetry_comprehensive()
                  .option("-q", "--quiet",
                          action="store_true",
                          default=False,
                          help="suppress output")
                  .option("--output_plots",
                          action="store_true",
                          default=False)
                  ).process(args=args)
  parsed = defaults(log=log)
  processed_args = mmtbx.utils.process_command_line_args(
    args=command_line.args,
    cmd_cs=command_line.symmetry,
    master_params=parsed,
    log=log,
    suppress_symmetry_related_errors=True)
  processed_args.params.show(out=log)
  params = processed_args.params.extract().density_modification
  output_plots = command_line.options.output_plots

  crystal_symmetry = crystal.symmetry(
    unit_cell=params.input.unit_cell,
    space_group_info=params.input.space_group)
  reflection_files = {}
  for rfn in (params.input.reflection_data.file_name,
              params.input.experimental_phases.file_name,
              params.input.map_coefficients.file_name):
    if os.path.isfile(str(rfn)) and rfn not in reflection_files:
      reflection_files.setdefault(
        rfn, iotbx.reflection_file_reader.any_reflection_file(
          file_name=rfn, ensure_read_access=False))
  server = iotbx.reflection_file_utils.reflection_file_server(
    crystal_symmetry=crystal_symmetry,
    reflection_files=reflection_files.values())
  fo = mmtbx.utils.determine_data_and_flags(
    server,
    parameters=params.input.reflection_data,
    extract_r_free_flags=False,log=log).f_obs
  hl_coeffs = mmtbx.utils.determine_experimental_phases(
    server,
    params.input.experimental_phases,
    log=log,
    parameter_scope="",
    working_point_group=None,
    symmetry_safety_check=True,
    ignore_all_zeros=True)
  if params.input.map_coefficients.file_name is not None:
    map_coeffs = server.get_phases_deg(
      file_name=params.input.map_coefficients.file_name,
      labels=params.input.map_coefficients.labels,
      convert_to_phases_if_necessary=False,
      original_phase_units=None,
      parameter_scope="",
      parameter_name="labels").map_to_asu()
  else:
    map_coeffs = None
  ncs_object = None
  if params.input.ncs_file_name is not None:
    ncs_object = ncs.ncs()
    ncs_object.read_ncs(params.input.ncs_file_name)
    ncs_object.display_all(log=log)

  fo = fo.map_to_asu()
  hl_coeffs = hl_coeffs.map_to_asu()

  fo = fo.eliminate_sys_absent().average_bijvoet_mates()
  hl_coeffs = hl_coeffs.eliminate_sys_absent().average_bijvoet_mates()

  model_map = None
  model_map_coeffs = None
  if len(processed_args.pdb_file_names):
    pdb_file = mmtbx.utils.pdb_file(
      pdb_file_names=processed_args.pdb_file_names)
    xs = pdb_file.pdb_inp.xray_structure_simple()
    fo_, hl_ = fo, hl_coeffs
    if params.change_basis_to_niggli_cell:
      change_of_basis_op = xs.change_of_basis_op_to_niggli_cell()
      xs = xs.change_basis(change_of_basis_op)
      fo_ = fo_.change_basis(change_of_basis_op).map_to_asu()
      hl_ = hl_.change_basis(change_of_basis_op).map_to_asu()
    #fo_, hl_ = fo_.common_sets(hl_)
    fmodel_refined = mmtbx.utils.fmodel_simple(
      f_obs=fo_,
      scattering_table="wk1995",#XXX pva: 1) neutrons? 2) move up as a parameter.
      xray_structures=[xs],
      bulk_solvent_correction=True,
      anisotropic_scaling=True,
      r_free_flags=fo_.array(data=flex.bool(fo_.size(), False)))
    fmodel_refined.update(abcd=hl_)

    master_phil = mmtbx.maps.map_and_map_coeff_master_params()
    map_params = master_phil.fetch(iotbx.phil.parse("""\
map_coefficients {
  map_type = 2mFo-DFc
  isotropize = True
}
""")).extract().map_coefficients[0]
    model_map_coeffs = mmtbx.maps.map_coefficients_from_fmodel(
      fmodel=fmodel_refined, params=map_params)
    model_map = model_map_coeffs.fft_map(
      resolution_factor=params.grid_resolution_factor).real_map_unpadded()

  import time

  t0 = time.time()
  dm = density_modify(
    params,
    fo,
    hl_coeffs,
    ncs_object=ncs_object,
    map_coeffs=map_coeffs,
    model_map_coeffs=model_map_coeffs,
    log=log,
    as_gui_program=as_gui_program)
  time_dm = time.time()-t0
  print >> log, "Time taken for density modification: %.2fs" %time_dm
  # run cns
  if 0:
    from cctbx.development import cns_density_modification
    cns_result = cns_density_modification.run(params, fo, hl_coeffs)
    print cns_result.modified_map.all()
    print dm.map.all()
    dm_map_coeffs = dm.map_coeffs_in_original_setting
    from cctbx import maptbx, miller
    crystal_gridding = maptbx.crystal_gridding(
      dm_map_coeffs.unit_cell(),
      space_group_info=dm_map_coeffs.space_group().info(),
      pre_determined_n_real=cns_result.modified_map.all())
    dm_map = miller.fft_map(crystal_gridding, dm_map_coeffs).apply_sigma_scaling()
    corr = flex.linear_correlation(cns_result.modified_map.as_1d(), dm_map.real_map_unpadded().as_1d())
    print "CNS dm/mmtbx dm correlation:"
    corr.show_summary()
    if dm.model_map_coeffs is not None:
      model_map = miller.fft_map(
        crystal_gridding,
        dm.miller_array_in_original_setting(dm.model_map_coeffs)).apply_sigma_scaling()
      corr = flex.linear_correlation(cns_result.modified_map.as_1d(), model_map.real_map_unpadded().as_1d())
      print "CNS dm/model correlation:"
      corr.show_summary()

  if output_plots:
    plots_to_make = (
      "fom", "skewness",
      "r1_factor", "r1_factor_fom", "mean_solvent_density", "mean_protein_density",
      "f000_over_v", "k_flip", "rms_solvent_density", "rms_protein_density",
      "standard_deviation_local_rms", "mean_delta_phi", "mean_delta_phi_initial",
      )
    from matplotlib.backends.backend_pdf import PdfPages
    from libtbx import pyplot

    stats = dm.get_stats()
    pdf = PdfPages("density_modification.pdf")

    if len(dm.correlation_coeffs) > 1:
      if 0:
        start_coeffs, model_coeffs = dm.map_coeffs_start.common_sets(model_map_coeffs)
        model_phases = model_coeffs.phases(deg=True).data()
        exptl_phases = nearest_phase(
          model_phases, start_coeffs.phases(deg=True).data(), deg=True)
        corr = flex.linear_correlation(exptl_phases, model_phases)
        corr.show_summary()
        fig = pyplot.figure()
        ax = fig.add_subplot(1,1,1)
        ax.set_title("phases start")
        ax.set_xlabel("Experimental phases")
        ax.set_ylabel("Phases from refined model")
        ax.scatter(exptl_phases,
                   model_phases,
                   marker="x", s=10)
        pdf.savefig(fig)
        #
        dm_coeffs, model_coeffs = dm.map_coeffs.common_sets(model_map_coeffs)
        model_phases = model_coeffs.phases(deg=True).data()
        dm_phases = nearest_phase(
          model_phases, dm_coeffs.phases(deg=True).data(), deg=True)
        corr = flex.linear_correlation(dm_phases, model_phases)
        corr.show_summary()
        fig = pyplot.figure()
        ax = fig.add_subplot(1,1,1)
        ax.set_title("phases dm")
        ax.set_xlabel("Phases from density modification")
        ax.set_ylabel("Phases from refined model")
        ax.scatter(dm_phases,
                   model_phases,
                   marker="x", s=10)
        pdf.savefig(fig)
      #
      data = dm.correlation_coeffs
      fig = pyplot.figure()
      ax = fig.add_subplot(1,1,1)
      ax.set_title("correlation coefficient")
      ax.plot(range(1, dm.i_cycle+2), data)
      pdf.savefig(fig)
      #
      data = dm.mean_phase_errors
      fig = pyplot.figure()
      ax = fig.add_subplot(1,1,1)
      ax.set_title("Mean effective phase errors")
      ax.plot(range(1, dm.i_cycle+2), data)
      pdf.savefig(fig)

    for plot in plots_to_make:
      data = [getattr(stats.get_cycle_stats(i), plot) for i in range(1, dm.i_cycle+2)]
      fig = pyplot.figure()
      ax = fig.add_subplot(1,1,1)
      ax.set_title(plot.replace("_", " "))
      ax.plot(range(1, dm.i_cycle+2), data)
      pdf.savefig(fig)

    data = [stats.get_cycle_stats(i).rms_solvent_density/
            stats.get_cycle_stats(i).rms_protein_density
            for i in range(1, dm.i_cycle+2)]
    fig = pyplot.figure()
    ax = fig.add_subplot(1,1,1)
    ax.set_title("RMS solvent/protein density ratio")
    ax.plot(range(1, dm.i_cycle+2), data)
    pdf.savefig(fig)

    pdf.close()

  dm_map_coeffs = dm.map_coeffs_in_original_setting
  dm_hl_coeffs = dm.hl_coeffs_in_original_setting

  # output map if requested
  map_params = params.output.map
  if map_params.file_name is not None:
    fft_map = dm_map_coeffs.fft_map(resolution_factor=params.grid_resolution_factor)
    if map_params.scale == "sigma":
      fft_map.apply_sigma_scaling()
    else:
      fft_map.apply_volume_scaling()
    gridding_first = gridding_last = None
    title_lines = []
    if map_params.format == "xplor":
      fft_map.as_xplor_map(
        file_name      = map_params.file_name,
        title_lines    = title_lines,
        gridding_first = gridding_first,
        gridding_last  = gridding_last)
    else :
      fft_map.as_ccp4_map(
        file_name      = map_params.file_name,
        gridding_first = gridding_first,
        gridding_last  = gridding_last,
        labels=title_lines)

  # output map coefficients if requested
  mtz_params = params.output.mtz

  # Decide if we are going to actually write the mtz
  if mtz_params.file_name is not None:
    orig_fom,final_fom=dm.start_and_end_fom()
    if mtz_params.skip_output_if_worse and final_fom < orig_fom:
      ok_to_write_mtz=False
      print "Not writing out mtz. Final FOM (%7.3f) worse than start (%7.3f)" %(
        final_fom,orig_fom)
    else:  # usual
      ok_to_write_mtz=True
  else:
      ok_to_write_mtz=True

  if mtz_params.file_name is not None and ok_to_write_mtz:
    label_decorator=iotbx.mtz.ccp4_label_decorator()
    fo = dm.miller_array_in_original_setting(dm.f_obs_complete).common_set(dm_map_coeffs)
    mtz_dataset = fo.as_mtz_dataset(
      column_root_label="F",
      label_decorator=label_decorator)
    mtz_dataset.add_miller_array(
      dm_map_coeffs,
      column_root_label="FWT",
      label_decorator=label_decorator)
    phase_source = dm.miller_array_in_original_setting(dm.phase_source).common_set(dm_map_coeffs)
    mtz_dataset.add_miller_array(
      phase_source.array(data=flex.abs(phase_source.data())),
      column_root_label="FOM",
      column_types='W',
      label_decorator=label_decorator)
    mtz_dataset.add_miller_array(
      phase_source.array(data=phase_source.phases(deg=True).data()),
      column_root_label="PHIB",
      column_types='P',
      label_decorator=None)
    if mtz_params.output_hendrickson_lattman_coefficients:
      mtz_dataset.add_miller_array(
        dm_hl_coeffs,
        column_root_label="HL",
        label_decorator=label_decorator)
    mtz_dataset.mtz_object().write(mtz_params.file_name)

  return result(
    map_file=map_params.file_name,
    mtz_file=mtz_params.file_name,
    stats=dm.get_stats())