Example #1
0
    def _mask(self):

        # Calculate the masked indices defined by max distance from protein atoms
        self._outer_mask_indices = maptbx.grid_indices_around_sites(
            unit_cell=self.parent.unit_cell(),
            fft_n_real=self.parent.grid_size(),
            fft_m_real=self.parent.grid_size(),
            # Masking is performed relative to the grid origin, so need to apply origin shift
            sites_cart=self.sites_cart - self.parent.cart_origin(),
            site_radii=flex.double(self.sites_cart.size(), self._max_dist))
        self._outer_mask_binary = numpy.zeros(self.parent.grid_size_1d(),
                                              dtype=bool)
        self._outer_mask_binary.put(self._outer_mask_indices, True)

        # Calculate the masked indices defined by min distance from protein atoms
        self._inner_mask_indices = maptbx.grid_indices_around_sites(
            unit_cell=self.parent.unit_cell(),
            fft_n_real=self.parent.grid_size(),
            fft_m_real=self.parent.grid_size(),
            # Masking is performed relative to the grid origin, so need to apply origin shift
            sites_cart=self.sites_cart - self.parent.cart_origin(),
            site_radii=flex.double(self.sites_cart.size(), self._min_dist))
        self._inner_mask_binary = numpy.zeros(self.parent.grid_size_1d(),
                                              dtype=bool)
        self._inner_mask_binary.put(self._inner_mask_indices, True)

        # Calculate the combination of these masks
        self._total_mask_binary = numpy.zeros(self.parent.grid_size_1d(), bool)
        self._total_mask_binary.put(self._outer_mask_indices, True)
        self._total_mask_binary.put(self._inner_mask_indices, False)
        self._total_mask_indices = numpy.where(self._total_mask_binary)[0]
Example #2
0
 def get_sites_cc (self, atoms, sites=None) :
   from cctbx import maptbx
   from scitbx.array_family import flex
   radii = flex.double()
   for atom in atoms :
     if (atom.element.strip() in ["H", "D"]) :
       radii.append(1.)
     else :
       radii.append(1.5)
   fcalc_map = self.fcalc_real_map
   if (sites is None) :
     sites = atoms.extract_xyz()
   else :
     fcalc_map = self.get_new_fcalc_map(
       sites_new=sites,
       i_seqs=atoms.extract_i_seq())
   sel = maptbx.grid_indices_around_sites(
     unit_cell  = self.unit_cell,
     fft_n_real = self.n_real,
     fft_m_real = self.m_real,
     sites_cart = sites,
     site_radii = radii)
   m1 = self.real_map.select(sel)
   m2 = fcalc_map.select(sel)
   cc = flex.linear_correlation(x=m1, y=m2).coefficient()
   return group_args(
     cc=cc,
     map_mean=flex.mean(m1.as_1d()))
Example #3
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
def run(args):
  assert len(args) == 1
  timer = time_log("pdb.input").start()
  pdb_inp = iotbx.pdb.input(file_name=args[0])
  print "number of pdb atoms:", pdb_inp.atoms().size()
  print timer.log()
  crystal_symmetry = pdb_inp.crystal_symmetry()
  assert crystal_symmetry is not None
  crystal_symmetry.show_summary()
  assert crystal_symmetry.unit_cell() is not None
  assert crystal_symmetry.space_group_info() is not None
  sites_cart = pdb_inp.atoms().extract_xyz()
  site_radii = flex.double(sites_cart.size(), 2.5)
  crystal_gridding = maptbx.crystal_gridding(
    unit_cell=crystal_symmetry.unit_cell(),
    d_min=2,
    resolution_factor=1/3)
  fft = fftpack.real_to_complex_3d(crystal_gridding.n_real())
  print "n_real:", fft.n_real()
  print "m_real:", fft.m_real()
  timer = time_log("grid_indices_around_sites").start()
  grid_indices = maptbx.grid_indices_around_sites(
    unit_cell=crystal_symmetry.unit_cell(),
    fft_n_real=fft.n_real(),
    fft_m_real=fft.m_real(),
    sites_cart=sites_cart,
    site_radii=site_radii)
  print "grid_indices.size():", grid_indices.size()
  print timer.log()
  print "grid fraction:", \
    grid_indices.size() / matrix.col(fft.n_real()).product()
Example #5
0
 def select(self, sites_cart, atom_radius=2.0):
   return maptbx.grid_indices_around_sites(
     unit_cell  = self.unit_cell,
     fft_n_real = self.fft_n_real,
     fft_m_real = self.fft_m_real,
     sites_cart = sites_cart,
     site_radii = flex.double(sites_cart.size(), atom_radius))
Example #6
0
 def get_map_stats_for_atoms(self, atoms):
     from cctbx import maptbx
     from scitbx.array_family import flex
     sites_cart = flex.vec3_double()
     sites_cart_nonH = flex.vec3_double()
     values_2fofc = flex.double()
     values_fofc = flex.double()
     for atom in atoms:
         sites_cart.append(atom.xyz)
         if (not atom.element.strip() in ["H", "D"]):  #XXX trap: neutrons?
             sites_cart_nonH.append(atom.xyz)
             site_frac = self.unit_cell.fractionalize(atom.xyz)
             values_2fofc.append(
                 self.f_map.eight_point_interpolation(site_frac))
             values_fofc.append(
                 self.diff_map.eight_point_interpolation(site_frac))
     if (len(sites_cart_nonH) == 0):
         return None
     sel = maptbx.grid_indices_around_sites(unit_cell=self.unit_cell,
                                            fft_n_real=self.f_map.focus(),
                                            fft_m_real=self.f_map.all(),
                                            sites_cart=sites_cart,
                                            site_radii=get_atom_radii(
                                                atoms, self.atom_radius))
     f_map_sel = self.f_map.select(sel)
     model_map_sel = self.model_map.select(sel)
     diff_map_sel = self.diff_map.select(sel)
     cc = flex.linear_correlation(x=f_map_sel,
                                  y=model_map_sel).coefficient()
     return group_args(cc=cc,
                       mean_2fofc=flex.mean(values_2fofc),
                       mean_fofc=flex.mean(values_fofc))
Example #7
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
Example #8
0
 def get_sites_cc(self, atoms, sites=None):
   from cctbx import maptbx
   from scitbx.array_family import flex
   radii = flex.double()
   for atom in atoms :
     if (atom.element.strip() in ["H", "D"]):
       radii.append(1.)
     else :
       radii.append(1.5)
   fcalc_map = self.fcalc_real_map
   if (sites is None):
     sites = atoms.extract_xyz()
   else :
     fcalc_map = self.get_new_fcalc_map(
       sites_new=sites,
       i_seqs=atoms.extract_i_seq())
   sel = maptbx.grid_indices_around_sites(
     unit_cell  = self.unit_cell,
     fft_n_real = self.n_real,
     fft_m_real = self.m_real,
     sites_cart = sites,
     site_radii = radii)
   m1 = self.real_map.select(sel)
   m2 = fcalc_map.select(sel)
   cc = flex.linear_correlation(x=m1, y=m2).coefficient()
   return group_args(
     cc=cc,
     map_mean=flex.mean(m1.as_1d()))
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 __init__(self,
              atom_selection,
              xray_structure,
              fft_m_real,
              fft_n_real,
              atom_radius=1.5,
              exclude_hydrogens=False):
     self.fft_m_real = fft_m_real
     self.fft_n_real = fft_n_real
     if (isinstance(atom_selection, flex.bool)):
         atom_selection = atom_selection.iselection()
     assert (len(atom_selection) == 1) or (not atom_selection.all_eq(0))
     if (exclude_hydrogens):
         not_hd_selection = (~(xray_structure.hd_selection())).iselection()
         atom_selection = atom_selection.intersection(not_hd_selection)
     assert (len(atom_selection) != 0)
     self.atom_selection = atom_selection
     self.sites = xray_structure.sites_cart().select(atom_selection)
     self.sites_frac = xray_structure.sites_frac().select(atom_selection)
     scatterers = xray_structure.scatterers().select(atom_selection)
     self.atom_radii = flex.double(self.sites.size(), atom_radius)
     for i_seq, sc in enumerate(scatterers):
         if (sc.element_symbol().strip().lower() in ["h", "d"]):
             assert (not exclude_hydrogens)
             self.atom_radii[i_seq] = 1.0
     self.map_sel = maptbx.grid_indices_around_sites(
         unit_cell=xray_structure.unit_cell(),
         fft_n_real=fft_n_real,
         fft_m_real=fft_m_real,
         sites_cart=self.sites,
         site_radii=self.atom_radii)
 def collect_map_values(map, get_selections=False):
     values = []
     selections = []
     if (map is None):
         assert (not get_selections)
         return [None] * len(pdb_atoms)
     for i_seq, atom in enumerate(pdb_atoms):
         if (selection[i_seq]):
             site_frac = sites_frac[i_seq]
             values.append(map.eight_point_interpolation(site_frac))
             if (get_selections):
                 sel = maptbx.grid_indices_around_sites(
                     unit_cell=unit_cell,
                     fft_n_real=map.focus(),
                     fft_m_real=map.all(),
                     sites_cart=flex.vec3_double([sites_cart[i_seq]]),
                     site_radii=flex.double([1.5]))
                 selections.append(map.select(sel))
         else:
             values.append(None)
             selections.append(None)
     if (get_selections):
         return values, selections
     else:
         return values
Example #12
0
def exercise_translational_phase_shift(n_sites=100,
                                       d_min=1.5,
                                       resolution_factor=0.3):
    sgi = space_group_info("P1")
    xrs = random_structure.xray_structure(
        space_group_info=sgi,
        elements=(("O", "N", "C") * (n_sites // 3 + 1))[:n_sites],
        volume_per_atom=50,
        min_distance=1.5)
    f_calc = xrs.structure_factors(d_min=d_min).f_calc()
    print f_calc.unit_cell()
    from scitbx.matrix import col
    shift_frac = col((.23984120, .902341127, .51219021))

    # Shift phases directly
    phase_shifted = f_calc.translational_shift(shift_frac=shift_frac)

    # Check that map from phase_shifted FC matches map calculated from
    #   translated xrs

    # Map from phase-shifted FC
    shifted_fft_map = phase_shifted.fft_map(
        resolution_factor=resolution_factor)
    shifted_fft_map.apply_sigma_scaling()
    shifted_map_data = shifted_fft_map.real_map_unpadded()
    cs = xrs.crystal_symmetry()
    from cctbx.maptbx import crystal_gridding
    cg = crystal_gridding(unit_cell=cs.unit_cell(),
                          space_group_info=cs.space_group_info(),
                          pre_determined_n_real=shifted_map_data.all())

    # Map from translated xrs
    sites_shifted = xrs.sites_frac() + shift_frac
    xrs.set_sites_frac(sites_shifted)
    f_calc_from_shifted_xrs = xrs.structure_factors(d_min=d_min).f_calc()
    fft_map_from_shifted_xrs = f_calc_from_shifted_xrs.fft_map(
        resolution_factor=resolution_factor, crystal_gridding=cg)
    map_data_from_shifted_xrs = fft_map_from_shifted_xrs.real_map_unpadded()

    # shifted_map_data (map from phase shifted f_calc),
    # map_data_from_shifted_xrs (recalculated with shifted xrs)

    assert shifted_map_data.all() == map_data_from_shifted_xrs.all()
    from cctbx import maptbx
    sel = maptbx.grid_indices_around_sites(unit_cell=xrs.unit_cell(),
                                           fft_n_real=shifted_map_data.focus(),
                                           fft_m_real=shifted_map_data.all(),
                                           sites_cart=xrs.sites_cart(),
                                           site_radii=flex.double(
                                               xrs.scatterers().size(), 1.5))
    shifted_map_data = shifted_map_data.select(sel)
    map_data_from_shifted_xrs = map_data_from_shifted_xrs.select(sel)

    cc_map_data_from_shifted_xrs_shifted_map_data = flex.linear_correlation(
        x=map_data_from_shifted_xrs.as_1d(),
        y=shifted_map_data.as_1d()).coefficient()
    print "cc_map_data_from_shifted_xrs_shifted_map_data",\
       cc_map_data_from_shifted_xrs_shifted_map_data
    assert cc_map_data_from_shifted_xrs_shifted_map_data > 0.99
    print "*" * 25
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
Example #14
0
 def get_map_stats_for_atoms (self, atoms) :
   from cctbx import maptbx
   from scitbx.array_family import flex
   sites_cart = flex.vec3_double()
   sites_cart_nonH = flex.vec3_double()
   values_2fofc = flex.double()
   values_fofc = flex.double()
   for atom in atoms :
     sites_cart.append(atom.xyz)
     if (not atom.element.strip() in ["H","D"]) : #XXX trap: neutrons?
       sites_cart_nonH.append(atom.xyz)
       site_frac = self.unit_cell.fractionalize(atom.xyz)
       values_2fofc.append(self.f_map.eight_point_interpolation(site_frac))
       values_fofc.append(self.diff_map.eight_point_interpolation(site_frac))
   if (len(sites_cart_nonH) == 0) :
     return None
   sel = maptbx.grid_indices_around_sites(
     unit_cell=self.unit_cell,
     fft_n_real=self.f_map.focus(),
     fft_m_real=self.f_map.all(),
     sites_cart=sites_cart,
     site_radii=get_atom_radii(atoms, self.atom_radius))
   f_map_sel = self.f_map.select(sel)
   model_map_sel = self.model_map.select(sel)
   diff_map_sel = self.diff_map.select(sel)
   cc = flex.linear_correlation(x=f_map_sel, y=model_map_sel).coefficient()
   return group_args(cc=cc,
     mean_2fofc=flex.mean(values_2fofc),
     mean_fofc=flex.mean(values_fofc))
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 show(pdb_hierarchy, tm, xrs, grm, prefix):
  map = compute_map(target_map=tm, xray_structure=xrs)
  cc = flex.linear_correlation(
    x=map.as_1d(),
    y=tm.data.as_1d()).coefficient()
  es = grm.energies_sites(sites_cart = xrs.sites_cart())
  rmsd_a = es.angle_deviations()[2]
  rmsd_b = es.bond_deviations()[2]
  print "%s: overall CC: %6.4f rmsd_bonds=%6.3f rmsd_angles=%6.3f"%(
    prefix, cc, rmsd_b, rmsd_a)
  pdb_hierarchy.adopt_xray_structure(xrs)
  rotamer_manager = RotamerEval()
  for model in pdb_hierarchy.models():
    for chain in model.chains():
      for residue in chain.residues():
        sites_cart = residue.atoms().extract_xyz()
        sel = maptbx.grid_indices_around_sites(
          unit_cell  = xrs.unit_cell(),
          fft_n_real = map.focus(),
          fft_m_real = map.all(),
          sites_cart = sites_cart,
          site_radii = flex.double(sites_cart.size(), 2))
        ccr = flex.linear_correlation(
          x=map.select(sel).as_1d(),
          y=tm.data.select(sel).as_1d()).coefficient()
        fmt = "%s: %4s %10s CC: %6.4f"
        print fmt%(prefix, residue.resname, rotamer_manager.evaluate_residue(residue),ccr)
Example #17
0
def run(args):
    assert len(args) == 1
    timer = time_log("pdb.input").start()
    pdb_inp = iotbx.pdb.input(file_name=args[0])
    print("number of pdb atoms:", pdb_inp.atoms().size())
    print(timer.log())
    crystal_symmetry = pdb_inp.crystal_symmetry()
    assert crystal_symmetry is not None
    crystal_symmetry.show_summary()
    assert crystal_symmetry.unit_cell() is not None
    assert crystal_symmetry.space_group_info() is not None
    sites_cart = pdb_inp.atoms().extract_xyz()
    site_radii = flex.double(sites_cart.size(), 2.5)
    crystal_gridding = maptbx.crystal_gridding(
        unit_cell=crystal_symmetry.unit_cell(),
        d_min=2,
        resolution_factor=1 / 3)
    fft = fftpack.real_to_complex_3d(crystal_gridding.n_real())
    print("n_real:", fft.n_real())
    print("m_real:", fft.m_real())
    timer = time_log("grid_indices_around_sites").start()
    grid_indices = maptbx.grid_indices_around_sites(
        unit_cell=crystal_symmetry.unit_cell(),
        fft_n_real=fft.n_real(),
        fft_m_real=fft.m_real(),
        sites_cart=sites_cart,
        site_radii=site_radii)
    print("grid_indices.size():", grid_indices.size())
    print(timer.log())
    print("grid fraction:", \
      grid_indices.size() / matrix.col(fft.n_real()).product())
 def collect_map_values (map, get_selections=False) :
   values = []
   selections = []
   if (map is None) :
     assert (not get_selections)
     return [ None ] * len(pdb_atoms)
   for i_seq, atom in enumerate(pdb_atoms) :
     if (selection[i_seq]) :
       site_frac = sites_frac[i_seq]
       values.append(map.eight_point_interpolation(site_frac))
       if (get_selections) :
         sel = maptbx.grid_indices_around_sites(
           unit_cell  = unit_cell,
           fft_n_real = map.focus(),
           fft_m_real = map.all(),
           sites_cart = flex.vec3_double([sites_cart[i_seq]]),
           site_radii = flex.double([1.5]))
         selections.append(map.select(sel))
     else :
       values.append(None)
       selections.append(None)
   if (get_selections) :
     return values, selections
   else :
     return values
Example #19
0
 def __init__(self, fmodel, ligands, params, log):
     from cctbx import maptbx
     from scitbx.array_family import flex
     map_helper = fmodel.electron_density_map()
     self.two_fofc_map_coeffs = map_helper.map_coefficients("2mFo-DFc")
     self.fofc_map_coeffs = map_helper.map_coefficients("mFo-DFc")
     fft_map = self.two_fofc_map_coeffs.fft_map(resolution_factor=0.25)
     fft_map.apply_sigma_scaling()
     fcalc = map_helper.map_coefficients("Fc")
     fcalc_map = fcalc.fft_map(resolution_factor=0.25)
     fcalc_map.apply_sigma_scaling()
     real_map = fft_map.real_map()
     fcalc_real_map = fcalc_map.real_map()
     final_cc = []
     for k, ligand in enumerate(ligands):
         atoms = ligand.atoms()
         sites = flex.vec3_double()
         radii = flex.double()
         for atom in atoms:
             if (not atom.element.strip() in ["H", "D"]):
                 sites.append(atom.xyz)
                 radii.append(1.5)
         sel = maptbx.grid_indices_around_sites(
             unit_cell=self.two_fofc_map_coeffs.unit_cell(),
             fft_n_real=real_map.focus(),
             fft_m_real=real_map.all(),
             sites_cart=sites,
             site_radii=radii)
         m1 = real_map.select(sel)
         m2 = fcalc_real_map.select(sel)
         cc = flex.linear_correlation(x=m1, y=m2).coefficient()
         final_cc.append(cc)
         print >> log, "  Ligand %d: CC = %5.3f" % (k + 1, cc)
     print >> log, ""
     self.final_cc = final_cc
def show(pdb_hierarchy, tm, xrs, grm, prefix):
    map = compute_map(target_map=tm, xray_structure=xrs)
    cc = flex.linear_correlation(x=map.as_1d(),
                                 y=tm.data.as_1d()).coefficient()
    es = grm.energies_sites(sites_cart=xrs.sites_cart())
    rmsd_a = es.angle_deviations()[2]
    rmsd_b = es.bond_deviations()[2]
    print "%s: overall CC: %6.4f rmsd_bonds=%6.3f rmsd_angles=%6.3f" % (
        prefix, cc, rmsd_b, rmsd_a)
    pdb_hierarchy.adopt_xray_structure(xrs)
    rotamer_manager = RotamerEval()
    for model in pdb_hierarchy.models():
        for chain in model.chains():
            for residue in chain.residues():
                sites_cart = residue.atoms().extract_xyz()
                sel = maptbx.grid_indices_around_sites(
                    unit_cell=xrs.unit_cell(),
                    fft_n_real=map.focus(),
                    fft_m_real=map.all(),
                    sites_cart=sites_cart,
                    site_radii=flex.double(sites_cart.size(), 2))
                ccr = flex.linear_correlation(
                    x=map.select(sel).as_1d(),
                    y=tm.data.select(sel).as_1d()).coefficient()
                fmt = "%s: %4s %10s CC: %6.4f"
                print fmt % (prefix, residue.resname,
                             rotamer_manager.evaluate_residue(residue), ccr)
 def __init__ (self,
     atom_selection,
     xray_structure,
     fft_m_real,
     fft_n_real,
     atom_radius=1.5,
     exclude_hydrogens=False) :
   self.fft_m_real = fft_m_real
   self.fft_n_real = fft_n_real
   if (isinstance(atom_selection, flex.bool)) :
     atom_selection = atom_selection.iselection()
   assert (len(atom_selection) == 1) or (not atom_selection.all_eq(0))
   if (exclude_hydrogens) :
     not_hd_selection = (~(xray_structure.hd_selection())).iselection()
     atom_selection = atom_selection.intersection(not_hd_selection)
   assert (len(atom_selection) != 0)
   self.atom_selection = atom_selection
   self.sites = xray_structure.sites_cart().select(atom_selection)
   self.sites_frac = xray_structure.sites_frac().select(atom_selection)
   scatterers = xray_structure.scatterers().select(atom_selection)
   self.atom_radii = flex.double(self.sites.size(), atom_radius)
   for i_seq, sc in enumerate(scatterers):
     if (sc.element_symbol().strip().lower() in ["h","d"]):
       assert (not exclude_hydrogens)
       self.atom_radii[i_seq] = 1.0
   self.map_sel = maptbx.grid_indices_around_sites(
     unit_cell  = xray_structure.unit_cell(),
     fft_n_real = fft_n_real,
     fft_m_real = fft_m_real,
     sites_cart = self.sites,
     site_radii = self.atom_radii)
Example #22
0
def print_validation(log, results, debug, pdb_hierarchy_selected):
    box_1 = results.box_1
    box_2 = results.box_2
    box_3 = results.box_3
    sites_cart_box = box_1.xray_structure_box.sites_cart()
    sel = maptbx.grid_indices_around_sites(
        unit_cell=box_1.xray_structure_box.unit_cell(),
        fft_n_real=box_1.map_box.focus(),
        fft_m_real=box_1.map_box.all(),
        sites_cart=sites_cart_box,
        site_radii=flex.double(sites_cart_box.size(), 2.0))
    b1 = box_1.map_box.select(sel).as_1d()
    b2 = box_2.map_box.select(sel).as_1d()
    b3 = box_3.map_box.select(sel).as_1d()
    print >> log, "Map 1: calculated Fobs with ligand"
    print >> log, "Map 2: calculated Fobs without ligand"
    print >> log, "Map 3: real Fobs data"
    cc12 = flex.linear_correlation(x=b1, y=b2).coefficient()
    cc13 = flex.linear_correlation(x=b1, y=b3).coefficient()
    cc23 = flex.linear_correlation(x=b2, y=b3).coefficient()
    print >> log, "CC(1,2): %6.4f" % cc12
    print >> log, "CC(1,3): %6.4f" % cc13
    print >> log, "CC(2,3): %6.4f" % cc23
    #### D-function
    b1 = maptbx.volume_scale_1d(map=b1, n_bins=10000).map_data()
    b2 = maptbx.volume_scale_1d(map=b2, n_bins=10000).map_data()
    b3 = maptbx.volume_scale_1d(map=b3, n_bins=10000).map_data()
    print >> log, "Peak CC:"
    print >> log, "CC(1,2): %6.4f" % flex.linear_correlation(
        x=b1, y=b2).coefficient()
    print >> log, "CC(1,3): %6.4f" % flex.linear_correlation(
        x=b1, y=b3).coefficient()
    print >> log, "CC(2,3): %6.4f" % flex.linear_correlation(
        x=b2, y=b3).coefficient()
    cutoffs = flex.double([i / 10. for i in range(1, 10)] +
                          [i / 100 for i in range(91, 100)])
    d12 = maptbx.discrepancy_function(map_1=b1, map_2=b2, cutoffs=cutoffs)
    d13 = maptbx.discrepancy_function(map_1=b1, map_2=b3, cutoffs=cutoffs)
    d23 = maptbx.discrepancy_function(map_1=b2, map_2=b3, cutoffs=cutoffs)
    print >> log, "q    D(1,2) D(1,3) D(2,3)"
    for c, d12_, d13_, d23_ in zip(cutoffs, d12, d13, d23):
        print >> log, "%4.2f %6.4f %6.4f %6.4f" % (c, d12_, d13_, d23_)
    ###
    if (debug):
        #box_1.write_ccp4_map(file_name="box_1_polder.ccp4")
        #box_2.write_ccp4_map(file_name="box_2_polder.ccp4")
        #box_3.write_ccp4_map(file_name="box_3_polder.ccp4")
        write_map_box(box=box_1, filename="box_1_polder.ccp4")
        write_map_box(box=box_2, filename="box_2_polder.ccp4")
        write_map_box(box=box_3, filename="box_3_polder.ccp4")
        pdb_hierarchy_selected.adopt_xray_structure(box_1.xray_structure_box)
        pdb_hierarchy_selected.write_pdb_file(
            file_name="box_polder.pdb",
            crystal_symmetry=box_1.box_crystal_symmetry)
    #
    print >> log, '*' * 79
    message = result_message(cc12=cc12, cc13=cc13, cc23=cc23)
    print >> log, message
    return message
Example #23
0
 def select(self, sites_cart, atom_radius=2.0):
     return maptbx.grid_indices_around_sites(unit_cell=self.unit_cell,
                                             fft_n_real=self.fft_n_real,
                                             fft_m_real=self.fft_m_real,
                                             sites_cart=sites_cart,
                                             site_radii=flex.double(
                                                 sites_cart.size(),
                                                 atom_radius))
Example #24
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)
Example #25
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)
Example #26
0
def get_model_map_stats (
    selection,
    target_map,
    model_map,
    unit_cell,
    sites_cart,
    pdb_atoms,
    local_sampling=False) :
  """
  Collect basic statistics for a model map and some target map (usually an
  mFo-DFc map), including CC, mean, and minimum density at the atomic
  positions.
  """
  assert (len(target_map) == len(model_map))
  iselection = selection
  if (type(selection).__name__ == 'bool') :
    iselection = selection.iselection()
  from scitbx.array_family import flex
  sites_cart_refined = sites_cart.select(selection)
  sites_selected = flex.vec3_double()
  map1 = flex.double()
  map2 = flex.double()
  min_density = sys.maxint
  sum_density = n_sites = 0
  worst_atom = None
  # XXX I'm not sure the strict density cutoff is a good idea here
  for i_seq, xyz in zip(iselection, sites_cart_refined) :
    if (pdb_atoms[i_seq].element.strip() != "H") :
      sites_selected.append(xyz)
      site_frac = unit_cell.fractionalize(site_cart=xyz)
      target_value = target_map.tricubic_interpolation(site_frac)
      if (target_value < min_density) :
        min_density = target_value
        worst_atom = pdb_atoms[i_seq]
      sum_density += target_value
      n_sites += 1
      if (not local_sampling) :
        map1.append(target_value)
        map2.append(model_map.tricubic_interpolation(site_frac))
  assert (n_sites > 0)
  if (local_sampling) :
    from cctbx import maptbx
    map_sel = maptbx.grid_indices_around_sites(
      unit_cell=unit_cell,
      fft_n_real=target_map.focus(),
      fft_m_real=target_map.all(),
      sites_cart=sites_selected,
      site_radii=flex.double(sites_selected.size(), 1.0))
    map1 = target_map.select(map_sel)
    map2 = model_map.select(map_sel)
  assert (len(map1) > 0) and (len(map1) == len(map2))
  cc = flex.linear_correlation(x=map1, y=map2).coefficient()
  return group_args(
    cc=cc,
    min=min_density,
    mean=sum_density/n_sites)
Example #27
0
def get_model_map_stats (
    selection,
    target_map,
    model_map,
    unit_cell,
    sites_cart,
    pdb_atoms,
    local_sampling=False) :
  """
  Collect basic statistics for a model map and some target map (usually an
  mFo-DFc map), including CC, mean, and minimum density at the atomic
  positions.
  """
  assert (len(target_map) == len(model_map))
  iselection = selection
  if (type(selection).__name__ == 'bool') :
    iselection = selection.iselection()
  from scitbx.array_family import flex
  sites_cart_refined = sites_cart.select(selection)
  sites_selected = flex.vec3_double()
  map1 = flex.double()
  map2 = flex.double()
  min_density = sys.maxint
  sum_density = n_sites = 0
  worst_atom = None
  # XXX I'm not sure the strict density cutoff is a good idea here
  for i_seq, xyz in zip(iselection, sites_cart_refined) :
    if (pdb_atoms[i_seq].element.strip() != "H") :
      sites_selected.append(xyz)
      site_frac = unit_cell.fractionalize(site_cart=xyz)
      target_value = target_map.tricubic_interpolation(site_frac)
      if (target_value < min_density) :
        min_density = target_value
        worst_atom = pdb_atoms[i_seq]
      sum_density += target_value
      n_sites += 1
      if (not local_sampling) :
        map1.append(target_value)
        map2.append(model_map.tricubic_interpolation(site_frac))
  assert (n_sites > 0)
  if (local_sampling) :
    from cctbx import maptbx
    map_sel = maptbx.grid_indices_around_sites(
      unit_cell=unit_cell,
      fft_n_real=target_map.focus(),
      fft_m_real=target_map.all(),
      sites_cart=sites_selected,
      site_radii=flex.double(sites_selected.size(), 1.0))
    map1 = target_map.select(map_sel)
    map2 = model_map.select(map_sel)
  assert (len(map1) > 0) and (len(map1) == len(map2))
  cc = flex.linear_correlation(x=map1, y=map2).coefficient()
  return group_args(
    cc=cc,
    min=min_density,
    mean=sum_density/n_sites)
Example #28
0
def from_map_map_atoms(map_1, map_2, sites_cart, unit_cell, radius):
    assert_same_gridding(map_1, map_2)
    sel = maptbx.grid_indices_around_sites(unit_cell=unit_cell,
                                           fft_n_real=map_1.focus(),
                                           fft_m_real=map_1.all(),
                                           sites_cart=sites_cart,
                                           site_radii=flex.double(
                                               sites_cart.size(), radius))
    return flex.linear_correlation(x=map_1.select(sel).as_1d(),
                                   y=map_2.select(sel).as_1d()).coefficient()
Example #29
0
 def modify_mask(self, mask_data, sites_cart):
     sel = maptbx.grid_indices_around_sites(
         unit_cell=self.f_obs.crystal_symmetry().unit_cell(),
         fft_n_real=mask_data.focus(),
         fft_m_real=mask_data.all(),
         sites_cart=sites_cart,
         site_radii=flex.double(sites_cart.size(), self.sphere_radius))
     mask = mask_data.as_1d()
     mask.set_selected(sel, 0)
     mask.reshape(mask_data.accessor())
     return mask
Example #30
0
def mask_modif(f_obs, mask_data, sites_cart, sphere_radius):
  sel = maptbx.grid_indices_around_sites(
    unit_cell  = f_obs.crystal_symmetry().unit_cell(),
    fft_n_real = mask_data.focus(),
    fft_m_real = mask_data.all(),
    sites_cart = sites_cart,
    site_radii = flex.double(sites_cart.size(), sphere_radius))
  mask = mask_data.as_1d()
  mask.set_selected(sel, 0)
  mask.reshape(mask_data.accessor())
  return mask
Example #31
0
def from_map_map_atoms(map_1, map_2, sites_cart, unit_cell, radius):
  assert_same_gridding(map_1, map_2)
  sel = maptbx.grid_indices_around_sites(
    unit_cell  = unit_cell,
    fft_n_real = map_1.focus(),
    fft_m_real = map_1.all(),
    sites_cart = sites_cart,
    site_radii = flex.double(sites_cart.size(), radius))
  return flex.linear_correlation(
    x=map_1.select(sel).as_1d(),
    y=map_2.select(sel).as_1d()).coefficient()
Example #32
0
def negate_map_around_selected_atoms_except_selected_atoms(
        xray_structure, map_data, negate_selection, atom_radius):
    # XXX time and memory inefficient
    sites_cart_p1 = xray_structure.select(negate_selection).expand_to_p1(
        sites_mod_positive=True).sites_cart()
    around_atoms_selections = maptbx.grid_indices_around_sites(
        unit_cell=xray_structure.unit_cell(),
        fft_n_real=map_data.focus(),
        fft_m_real=map_data.all(),
        sites_cart=sites_cart_p1,
        site_radii=flex.double(sites_cart_p1.size(), atom_radius))
    return maptbx.negate_selected_in_place(map_data=map_data,
                                           selection=around_atoms_selections)
Example #33
0
def compute(pdb_hierarchy,
            unit_cell,
            map_1,
            map_2,
            map_3,
            detail,
            atom_radius) :
  results = []
  for chain in pdb_hierarchy.chains():
    for residue_group in chain.residue_groups():
      for conformer in residue_group.conformers():
        for residue in conformer.residues():
          r_id_str = "%2s %1s %3s %4s %1s"%(chain.id, conformer.altloc,
            residue.resname, residue.resseq, residue.icode)
          for atom in residue.atoms():
            a_id_str = "%s %4s"%(r_id_str, atom.name)
            rad = atom_radius
            if not atom.element_is_hydrogen() :
              map_value_1 = map_1.eight_point_interpolation(
                unit_cell.fractionalize(atom.xyz))
              map_value_2 = map_2.eight_point_interpolation(
                unit_cell.fractionalize(atom.xyz))
              map_value_3 = map_3.eight_point_interpolation(
                unit_cell.fractionalize(atom.xyz))
              sel = maptbx.grid_indices_around_sites(
                unit_cell  = unit_cell,
                fft_n_real = map_1.focus(),
                fft_m_real = map_1.all(),
                sites_cart = flex.vec3_double([atom.xyz]),
                site_radii = flex.double([atom_radius]))
              cc = flex.linear_correlation(x=map_1.select(sel),
                y=map_2.select(sel)).coefficient()
              result = group_args(
                chain_id    = chain.id,
                res_num     = residue.resseq,
                res_name    = residue.resname,
                atom_name   = atom.name,
                alt_loc     = conformer.altloc,
                ins_code    = residue.icode,
                atom        = atom,
                id_str      = a_id_str,
                cc          = cc,
                map_value_1 = map_value_1,
                map_value_2 = map_value_2,
                map_value_3 = map_value_3,
                b           = atom.b,
                occupancy   = atom.occ,
                n_atoms     = 1)
              results.append(result)
  return results
 def __init__(self,
              fofc_map,
              two_fofc_map,
              sites_cart=None,
              atom_names=None,
              unit_cell=None,
              atom_selection=None,
              xray_structure=None,
              radius=2.5):
     from cctbx import maptbx
     from scitbx.array_family import flex
     assert ((len(fofc_map) == len(two_fofc_map))
             and (fofc_map.focus() == two_fofc_map.focus()))
     self.atom_selection = atom_selection  # assumes no HD already
     self.sites_cart = sites_cart
     if (atom_names
             is not None) and (type(atom_names).__name__ != 'std_string'):
         atom_names = flex.std_string(atom_names)
     self.atom_names = atom_names
     self.unit_cell = unit_cell
     if (atom_selection is not None):
         assert (xray_structure
                 is not None) and (len(self.atom_selection) > 0)
         self.sites_cart = xray_structure.sites_cart().select(
             self.atom_selection)
         self.unit_cell = xray_structure.unit_cell()
         labels = xray_structure.scatterers().extract_labels()
         self.atom_names = labels.select(self.atom_selection)
     self.map_sel = maptbx.grid_indices_around_sites(
         unit_cell=self.unit_cell,
         fft_n_real=fofc_map.focus(),
         fft_m_real=fofc_map.all(),
         sites_cart=self.sites_cart,
         site_radii=flex.double(self.sites_cart.size(), radius))
     assert (len(self.map_sel) > 0)
     self.fofc_map_sel = fofc_map.select(self.map_sel)
     self.two_fofc_map_sel = two_fofc_map.select(self.map_sel)
     self.fofc_map_levels = flex.double()
     self.two_fofc_map_levels = flex.double()
     for site_cart in self.sites_cart:
         site_frac = self.unit_cell.fractionalize(site_cart=site_cart)
         fofc_map_value = fofc_map.tricubic_interpolation(site_frac)
         two_fofc_map_value = two_fofc_map.tricubic_interpolation(site_frac)
         self.fofc_map_levels.append(fofc_map_value)
         self.two_fofc_map_levels.append(two_fofc_map_value)
     self._atom_lookup = {
         n.strip(): i
         for i, n in enumerate(self.atom_names)
     }
Example #35
0
def negate_map_around_selected_atoms_except_selected_atoms(
      xray_structure,
      map_data,
      negate_selection,
      atom_radius):
  # XXX time and memory inefficient
  sites_cart_p1 = xray_structure.select(negate_selection).expand_to_p1(
    sites_mod_positive=True).sites_cart()
  around_atoms_selections = maptbx.grid_indices_around_sites(
    unit_cell  = xray_structure.unit_cell(),
    fft_n_real = map_data.focus(),
    fft_m_real = map_data.all(),
    sites_cart = sites_cart_p1,
    site_radii = flex.double(sites_cart_p1.size(), atom_radius))
  return maptbx.negate_selected_in_place(map_data=map_data,
    selection=around_atoms_selections)
Example #36
0
 def __init__ (self,
     fofc_map,
     two_fofc_map,
     sites_cart=None,
     atom_names=None,
     unit_cell=None,
     atom_selection=None,
     xray_structure=None,
     radius=2.5) :
   from cctbx import maptbx
   from scitbx.array_family import flex
   assert ((len(fofc_map) == len(two_fofc_map)) and
           (fofc_map.focus() == two_fofc_map.focus()))
   self.atom_selection = atom_selection # assumes no HD already
   self.sites_cart = sites_cart
   if (atom_names is not None) and (type(atom_names).__name__!='std_string'):
     atom_names = flex.std_string(atom_names)
   self.atom_names = atom_names
   self.unit_cell = unit_cell
   if (atom_selection is not None) :
     assert (xray_structure is not None) and (len(self.atom_selection) > 0)
     self.sites_cart = xray_structure.sites_cart().select(self.atom_selection)
     self.unit_cell = xray_structure.unit_cell()
     labels = xray_structure.scatterers().extract_labels()
     self.atom_names = labels.select(self.atom_selection)
   self.map_sel = maptbx.grid_indices_around_sites(
     unit_cell=self.unit_cell,
     fft_n_real=fofc_map.focus(),
     fft_m_real=fofc_map.all(),
     sites_cart=self.sites_cart,
     site_radii=flex.double(self.sites_cart.size(), radius))
   assert (len(self.map_sel) > 0)
   self.fofc_map_sel = fofc_map.select(self.map_sel)
   self.two_fofc_map_sel = two_fofc_map.select(self.map_sel)
   self.fofc_map_levels = flex.double()
   self.two_fofc_map_levels = flex.double()
   for site_cart in self.sites_cart :
     site_frac = self.unit_cell.fractionalize(site_cart=site_cart)
     fofc_map_value = fofc_map.tricubic_interpolation(site_frac)
     two_fofc_map_value = two_fofc_map.tricubic_interpolation(site_frac)
     self.fofc_map_levels.append(fofc_map_value)
     self.two_fofc_map_levels.append(two_fofc_map_value)
   self._atom_lookup = { n.strip():i for i,n in enumerate(self.atom_names) }
def flatten_map (map, xray_structure, selection) :
  from cctbx import maptbx
  from scitbx.array_family import flex
  sites = xray_structure.sites_cart().select(selection)
  hd_sel = xray_structure.hd_selection()
  radii = flex.double()
  for i_seq in selection :
    if (hd_sel[i_seq]) :
      radii.append(1.0)
    else :
      radii.append(1.5)
  sel = maptbx.grid_indices_around_sites(
    unit_cell  = xray_structure.unit_cell(),
    fft_n_real = map.focus(),
    fft_m_real = map.all(),
    sites_cart = sites,
    site_radii = radii)
  bg_sel = flex.bool(map.size(), True)
  bg_sel.set_selected(sel, False)
  map.as_1d().set_selected(bg_sel, 0)
  return map
Example #38
0
def flatten_map(map, xray_structure, selection):
    from cctbx import maptbx
    from scitbx.array_family import flex
    sites = xray_structure.sites_cart().select(selection)
    hd_sel = xray_structure.hd_selection()
    radii = flex.double()
    for i_seq in selection:
        if (hd_sel[i_seq]):
            radii.append(1.0)
        else:
            radii.append(1.5)
    sel = maptbx.grid_indices_around_sites(
        unit_cell=xray_structure.unit_cell(),
        fft_n_real=map.focus(),
        fft_m_real=map.all(),
        sites_cart=sites,
        site_radii=radii)
    bg_sel = flex.bool(map.size(), True)
    bg_sel.set_selected(sel, False)
    map.as_1d().set_selected(bg_sel, 0)
    return map
Example #39
0
 def __init__ (self,
     fmodel,
     ligands,
     params,
     log) :
   from cctbx import maptbx
   from scitbx.array_family import flex
   map_helper = fmodel.electron_density_map()
   self.two_fofc_map_coeffs = map_helper.map_coefficients("2mFo-DFc")
   self.fofc_map_coeffs = map_helper.map_coefficients("mFo-DFc")
   fft_map = self.two_fofc_map_coeffs.fft_map(resolution_factor=0.25)
   fft_map.apply_sigma_scaling()
   fcalc = map_helper.map_coefficients("Fc")
   fcalc_map = fcalc.fft_map(resolution_factor=0.25)
   fcalc_map.apply_sigma_scaling()
   real_map = fft_map.real_map()
   fcalc_real_map = fcalc_map.real_map()
   final_cc = []
   for k, ligand in enumerate(ligands) :
     atoms = ligand.atoms()
     sites = flex.vec3_double()
     radii = flex.double()
     for atom in atoms :
       if (not atom.element.strip() in ["H","D"]) :
         sites.append(atom.xyz)
         radii.append(1.5)
     sel = maptbx.grid_indices_around_sites(
       unit_cell  = self.two_fofc_map_coeffs.unit_cell(),
       fft_n_real = real_map.focus(),
       fft_m_real = real_map.all(),
       sites_cart = sites,
       site_radii = radii)
     m1 = real_map.select(sel)
     m2 = fcalc_real_map.select(sel)
     cc = flex.linear_correlation(x=m1, y=m2).coefficient()
     final_cc.append(cc)
     print >> log, "  Ligand %d: CC = %5.3f" % (k+1, cc)
   print >> log, ""
   self.final_cc = final_cc
Example #40
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
def get_map_values_and_grid_sites_frac(fmodel, map_type, grid_step, d_min,
                                       apply_sigma_scaling,
                                       apply_volume_scaling, include_f000,
                                       sel_bb, use_exact_phases):
    #
    resolution_factor = grid_step / d_min
    mp = mmtbx.masks.mask_master_params.extract()
    mp.grid_step_factor = 1. / resolution_factor
    mmtbx_masks_asu_mask_obj = mmtbx.masks.asu_mask(
        xray_structure=fmodel.xray_structure, d_min=d_min, mask_params=mp)
    bulk_solvent_mask = mmtbx_masks_asu_mask_obj.mask_data_whole_uc()
    sel = bulk_solvent_mask > 0
    bulk_solvent_mask = bulk_solvent_mask.set_selected(sel, 1)
    cr_gr = maptbx.crystal_gridding(
        unit_cell=fmodel.xray_structure.unit_cell(),
        space_group_info=fmodel.f_obs().space_group_info(),
        pre_determined_n_real=bulk_solvent_mask.focus())
    from mmtbx import map_tools
    from cctbx import miller
    #
    #mc = map_tools.electron_density_map(fmodel = fmodel).map_coefficients(
    #  map_type = map_type,
    #  acentrics_scale = 1.0,
    #  centrics_pre_scale = 1.0)
    if not use_exact_phases:
        k = fmodel.k_isotropic() * fmodel.k_anisotropic()
        print("flex.mean(k):", flex.mean(k))
        f_model = fmodel.f_model()
        mc_data = abs(fmodel.f_obs()).data() / k - abs(f_model).data() / k

        tmp = miller.array(miller_set=f_model,
                           data=flex.double(
                               f_model.indices().size(),
                               1)).phase_transfer(phase_source=f_model)
        mc = miller.array(miller_set=tmp, data=mc_data * tmp.data())
    else:
        fmodel.update_all_scales(fast=True, remove_outliers=False)
        k = fmodel.k_isotropic() * fmodel.k_anisotropic()
        fo = fmodel.f_obs().customized_copy(data=fmodel.f_obs().data() / k)
        fo = fo.phase_transfer(phase_source=fmodel.f_model())
        fc = fmodel.f_calc().customized_copy(data=fmodel.f_calc().data())
        mc = miller.array(miller_set=fo, data=fo.data() - fc.data())

    ######## XXX
    fft_map = miller.fft_map(crystal_gridding=cr_gr, fourier_coefficients=mc)
    fft_map.apply_volume_scaling()
    map_data = fft_map.real_map_unpadded()

    xrs = fmodel.xray_structure
    sites_cart = xrs.sites_cart().select(sel_bb)
    sel = maptbx.grid_indices_around_sites(unit_cell=xrs.unit_cell(),
                                           fft_n_real=map_data.focus(),
                                           fft_m_real=map_data.all(),
                                           sites_cart=sites_cart,
                                           site_radii=flex.double(
                                               sites_cart.size(), 0.5))
    map_in = map_data.select(sel)
    mm = flex.mean(map_in)
    print("mean in (1):", mm)
    #
    #sites_frac = xrs.sites_frac().select(sel_bb)
    #mm = 0
    #for sf in sites_frac:
    #  mm += map_data.eight_point_interpolation(sf)
    #mm = mm/sites_frac.size()
    #print "mean in (2):", mm
    ########

    #
    # Add F000
    #reg = fmodel.xray_structure.scattering_type_registry(table = "wk1995")
    #f_000 = reg.sum_of_scattering_factors_at_diffraction_angle_0() +\
    #  0.4*fmodel.xray_structure.unit_cell().volume()
    if (include_f000):
        #f_000 = include_f000*fmodel.xray_structure.unit_cell().volume()*0.3
        #f_000 = None # XXX
        f_000 = abs(mm * xrs.unit_cell().volume())
        #f_000 = 0.626*fmodel.xray_structure.unit_cell().volume()*0.35
    else:
        f_000 = None
    print("f_000:", f_000)
    #print "XXX", include_f000*fmodel.xray_structure.unit_cell().volume()*0.3
    #
    fft_map = miller.fft_map(crystal_gridding=cr_gr,
                             fourier_coefficients=mc,
                             f_000=f_000)
    #
    assert [apply_sigma_scaling, apply_volume_scaling].count(True) == 1
    if (apply_sigma_scaling): fft_map.apply_sigma_scaling()
    elif (apply_volume_scaling): fft_map.apply_volume_scaling()
    else: assert RuntimeError
    nx, ny, nz = fft_map.n_real()
    map_data = fft_map.real_map_unpadded()

    #map_data = map_data * bulk_solvent_mask
    print("n_real:", nx, ny, nz, map_data.size())
    grid_sites_frac = flex.vec3_double()
    map_values = flex.double()
    for ix in range(nx):
        for iy in range(ny):
            for iz in range(nz):
                mv = map_data[(ix, iy, iz)]
                if 1:  #if(mv != 0):
                    xf, yf, zf = ix / float(nx), iy / float(ny), iz / float(nz)
                    grid_sites_frac.append([xf, yf, zf])
                    map_at_ixiyiz = map_data[(ix, iy, iz)]
                    map_values.append(map_at_ixiyiz)
    return map_values, grid_sites_frac
  def __init__(self, map_1,
                     xray_structure,
                     fft_map,
                     atom_radius,
                     hydrogen_atom_radius,
                     model_i,
                     number_previous_scatters,
                     ignore_hd = False,
                     residue_detail = True,
                     selection = None,
                     pdb_hierarchy = None):
    self.xray_structure = xray_structure
    self.selection = selection
    self.pdb_hierarchy = pdb_hierarchy
    self.result = []
    self.map_1_size = map_1.size()
    self.map_1_stat = maptbx.statistics(map_1)
    self.atoms_with_labels = None
    self.residue_detail = residue_detail
    self.model_i = model_i
    if(pdb_hierarchy is not None):
      self.atoms_with_labels = list(pdb_hierarchy.atoms_with_labels())
    scatterers = self.xray_structure.scatterers()
    sigma_occ = flex.double()
    if(self.selection is None):
      self.selection = flex.bool(scatterers.size(), True)
    real_map_unpadded = fft_map.real_map_unpadded()
    sites_cart = self.xray_structure.sites_cart()

    if not self.residue_detail:
      self.gifes = [None,]*scatterers.size()
      self._result = [None,]*scatterers.size()
      #
      atom_radii = flex.double(scatterers.size(), atom_radius)
      for i_seq, sc in enumerate(scatterers):
        if(self.selection[i_seq]):
          if(sc.element_symbol().strip().lower() in ["h","d"]):
            atom_radii[i_seq] = hydrogen_atom_radius
      #
      for i_seq, site_cart in enumerate(sites_cart):
        if(self.selection[i_seq]):
          sel = maptbx.grid_indices_around_sites(
            unit_cell  = self.xray_structure.unit_cell(),
            fft_n_real = real_map_unpadded.focus(),
            fft_m_real = real_map_unpadded.all(),
            sites_cart = flex.vec3_double([site_cart]),
            site_radii = flex.double([atom_radii[i_seq]]))
          self.gifes[i_seq] = sel
          m1 = map_1.select(sel)
          ed1 = map_1.eight_point_interpolation(scatterers[i_seq].site)
          sigma_occ.append(ed1)
          a = None
          if(self.atoms_with_labels is not None):
            a = self.atoms_with_labels[i_seq]
          self._result[i_seq] = group_args(atom = a, m1 = m1, ed1 = ed1,
            xyz=site_cart)
      self.xray_structure.set_occupancies(sigma_occ)

      ### For testing other residue averaging options
      residues = self.extract_residues(model_i = model_i,
                                       number_previous_scatters = number_previous_scatters)
      self.xray_structure.residue_selections = residues

    # Residue detail
    if self.residue_detail:
      assert self.pdb_hierarchy is not None
      residues = self.extract_residues(model_i = model_i,
                                       number_previous_scatters = number_previous_scatters)
      self.gifes = [None,]*len(residues)
      self._result = [None,]*len(residues)
      for i_seq, residue in enumerate(residues):
        residue_sites_cart = sites_cart.select(residue.selection)
        if 0: print i_seq, list(residue.selection) # DEBUG
        sel = maptbx.grid_indices_around_sites(
          unit_cell  = self.xray_structure.unit_cell(),
          fft_n_real = real_map_unpadded.focus(),
          fft_m_real = real_map_unpadded.all(),
          sites_cart = residue_sites_cart,
          site_radii = flex.double(residue.selection.size(), atom_radius))
        self.gifes[i_seq] = sel
        m1 = map_1.select(sel)
        ed1 = flex.double()
        for i_seq_r in residue.selection:
          ed1.append(map_1.eight_point_interpolation(scatterers[i_seq_r].site))
        self._result[i_seq] = \
          group_args(residue = residue, m1 = m1, ed1 = flex.mean(ed1),
            xyz=residue_sites_cart.mean(), n_atoms=residue_sites_cart.size())

        residue_scatterers = scatterers.select(residue.selection)
        residue_ed1 = flex.double()
        for n,scatter in enumerate(residue_scatterers):
          if ignore_hd:
            if scatter.element_symbol() not in ['H', 'D']:
              residue_ed1.append(ed1[n])
          else:
            residue_ed1.append(ed1[n])
        for x in range(ed1.size()):
          sigma_occ.append(flex.mean(residue_ed1))

      self.xray_structure.set_occupancies(sigma_occ)
      self.xray_structure.residue_selections = residues

    del map_1
def rho_stats(
      xray_structure,
      d_min,
      resolution_factor,
      electron_sum_radius,
      zero_out_f000):
  n_real = []
  n_half_plus = []
  n_half_minus = []
  s2 = d_min * resolution_factor * 2
  for l in xray_structure.unit_cell().parameters()[:3]:
    nh = ifloor(l / s2)
    n_real.append(2*nh+1)
    n_half_plus.append(nh)
    n_half_minus.append(-nh)
  n_real = tuple(n_real)
  n_real_product = matrix.col(n_real).product()
  crystal_gridding = maptbx.crystal_gridding(
    unit_cell=xray_structure.unit_cell(),
    space_group_info=xray_structure.space_group_info(),
    pre_determined_n_real=n_real)
  miller_indices = flex.miller_index()
  miller_indices.reserve(n_real_product)
  for h in flex.nested_loop(n_half_minus, n_half_plus, open_range=False):
    miller_indices.append(h)
  assert miller_indices.size() == n_real_product
  #
  miller_set = miller.set(
    crystal_symmetry=xray_structure,
    anomalous_flag=True,
    indices=miller_indices).sort(by_value="resolution")
  assert miller_set.indices()[0] == (0,0,0)
  f_calc = miller_set.structure_factors_from_scatterers(
    xray_structure=xray_structure,
    algorithm="direct",
    cos_sin_table=False).f_calc()
  if (zero_out_f000):
    f_calc.data()[0] = 0j
  #
  unit_cell_volume = xray_structure.unit_cell().volume()
  voxel_volume = unit_cell_volume / n_real_product
  number_of_miller_indices = []
  rho_max = []
  electron_sums_around_atoms = []
  densities_along_x = []
  for f in [f_calc, f_calc.resolution_filter(d_min=d_min)]:
    assert f.indices()[0] == (0,0,0)
    number_of_miller_indices.append(f.indices().size())
    fft_map = miller.fft_map(
      crystal_gridding=crystal_gridding,
      fourier_coefficients=f)
    assert fft_map.n_real() == n_real
    rho = fft_map.real_map_unpadded() / unit_cell_volume
    assert approx_equal(voxel_volume*flex.sum(rho), f_calc.data()[0])
    if (xray_structure.scatterers().size() == 1):
      assert flex.max_index(rho) == 0
      rho_max.append(rho[0])
    else:
      rho_max.append(flex.max(rho))
    site_cart = xray_structure.sites_cart()[0]
    gias = maptbx.grid_indices_around_sites(
      unit_cell=xray_structure.unit_cell(),
      fft_n_real=n_real,
      fft_m_real=n_real,
      sites_cart=flex.vec3_double([site_cart]),
      site_radii=flex.double([electron_sum_radius]))
    electron_sums_around_atoms.append(
      flex.sum(rho.as_1d().select(gias))*voxel_volume)
    #
    a = xray_structure.unit_cell().parameters()[0]
    nx = n_real[0]
    nxh = nx//2
    x = []
    y = []
    for ix in xrange(-nxh,nxh+1):
      x.append(a*ix/nx)
      y.append(rho[(ix%nx,0,0)])
    densities_along_x.append((x,y))
  #
  print \
    "%3.1f %4.2f %-12s %5d %5d | %6.3f %6.3f | %6.3f %6.3f | %4.2f %5.1f" % (
      d_min,
      resolution_factor,
      n_real,
      number_of_miller_indices[0],
      number_of_miller_indices[1],
      electron_sums_around_atoms[0],
      electron_sums_around_atoms[1],
      rho_max[0],
      rho_max[1],
      f_calc.data()[0].real,
      u_as_b(xray_structure.scatterers()[0].u_iso))
  #
  return densities_along_x
def compute(pdb_hierarchy, unit_cell, fft_n_real, fft_m_real, map_1, map_2,
            detail, atom_radius, use_hydrogens, hydrogen_atom_radius):
    assert detail in ["atom", "residue"]
    results = []
    for chain in pdb_hierarchy.chains():
        for residue_group in chain.residue_groups():
            for conformer in residue_group.conformers():
                for residue in conformer.residues():
                    r_id_str = "%2s %1s %3s %4s %1s" % (
                        chain.id, conformer.altloc, residue.resname,
                        residue.resseq, residue.icode)
                    r_sites_cart = flex.vec3_double()
                    r_b = flex.double()
                    r_occ = flex.double()
                    r_mv1 = flex.double()
                    r_mv2 = flex.double()
                    r_rad = flex.double()
                    for atom in residue.atoms():
                        a_id_str = "%s %4s" % (r_id_str, atom.name)
                        if (atom.element_is_hydrogen()):
                            rad = hydrogen_atom_radius
                        else:
                            rad = atom_radius
                        if (not (atom.element_is_hydrogen()
                                 and not use_hydrogens)):
                            map_value_1 = map_1.eight_point_interpolation(
                                unit_cell.fractionalize(atom.xyz))
                            map_value_2 = map_2.eight_point_interpolation(
                                unit_cell.fractionalize(atom.xyz))
                            r_sites_cart.append(atom.xyz)
                            r_b.append(atom.b)
                            r_occ.append(atom.occ)
                            r_mv1.append(map_value_1)
                            r_mv2.append(map_value_2)
                            r_rad.append(rad)
                            if (detail == "atom"):
                                sel = maptbx.grid_indices_around_sites(
                                    unit_cell=unit_cell,
                                    fft_n_real=fft_n_real,
                                    fft_m_real=fft_m_real,
                                    sites_cart=flex.vec3_double([atom.xyz]),
                                    site_radii=flex.double([rad]))
                                cc = flex.linear_correlation(
                                    x=map_1.select(sel),
                                    y=map_2.select(sel)).coefficient()
                                result = group_args(chain_id=chain.id,
                                                    atom=atom,
                                                    id_str=a_id_str,
                                                    cc=cc,
                                                    map_value_1=map_value_1,
                                                    map_value_2=map_value_2,
                                                    b=atom.b,
                                                    occupancy=atom.occ,
                                                    n_atoms=1)
                                results.append(result)
                    if (detail == "residue") and (len(r_mv1) > 0):
                        sel = maptbx.grid_indices_around_sites(
                            unit_cell=unit_cell,
                            fft_n_real=fft_n_real,
                            fft_m_real=fft_m_real,
                            sites_cart=r_sites_cart,
                            site_radii=r_rad)
                        cc = flex.linear_correlation(
                            x=map_1.select(sel),
                            y=map_2.select(sel)).coefficient()
                        result = group_args(residue=residue,
                                            chain_id=chain.id,
                                            id_str=r_id_str,
                                            cc=cc,
                                            map_value_1=flex.mean(r_mv1),
                                            map_value_2=flex.mean(r_mv2),
                                            b=flex.mean(r_b),
                                            occupancy=flex.mean(r_occ),
                                            n_atoms=r_sites_cart.size())
                        results.append(result)
    return results
Example #45
0
    def validate_polder_map(self,
                            selection_bool,
                            xray_structure_noligand,
                            mask_data_polder,
                            box_cushion=2.1):
        '''
    The parameter box_cushion is hardcoded to be 2.1
    The value is related to the site_radii used for CC calculation (box_cushion - 0.1)
    Ideally the site_radii are calculated according to resolution, atom type and B factor for each atom
    However, for the purpose of polder map validation, it is a reasonable approximation
    to use 2.0.
    If this value is changed, it will affect the values of the CCs and therefore also the
    output messages (see mmtbx/programs/polder.py --> result_message)
    So modify this value with caution.
    '''
        # Significance check
        fmodel = mmtbx.f_model.manager(f_obs=self.f_obs,
                                       r_free_flags=self.r_free_flags,
                                       xray_structure=self.xray_structure)
        fmodel.update_all_scales(remove_outliers=False, fast=True)
        f_obs_1 = abs(fmodel.f_model())
        fmodel.update_xray_structure(xray_structure=xray_structure_noligand,
                                     update_f_calc=True,
                                     update_f_mask=True,
                                     force_update_f_mask=True)
        ## PVA: do we need it? fmodel.update_all_scales(remove_outliers=False)
        f_obs_2 = abs(fmodel.f_model())
        pdb_hierarchy_selected = self.pdb_hierarchy.select(selection_bool)
        xrs_selected = pdb_hierarchy_selected.extract_xray_structure(
            crystal_symmetry=self.cs)
        f_calc = fmodel.f_obs().structure_factors_from_scatterers(
            xray_structure=xray_structure_noligand).f_calc()
        f_mask = fmodel.f_obs().structure_factors_from_map(
            map=mask_data_polder,
            use_scale=True,
            anomalous_flag=False,
            use_sg=False)
        box_1 = self.get_polder_diff_map(f_obs=f_obs_1,
                                         r_free_flags=fmodel.r_free_flags(),
                                         f_calc=f_calc,
                                         f_mask=f_mask,
                                         xrs_selected=xrs_selected,
                                         box_cushion=box_cushion)
        box_2 = self.get_polder_diff_map(f_obs=f_obs_2,
                                         r_free_flags=fmodel.r_free_flags(),
                                         f_calc=f_calc,
                                         f_mask=f_mask,
                                         xrs_selected=xrs_selected,
                                         box_cushion=box_cushion)
        box_3 = self.get_polder_diff_map(f_obs=fmodel.f_obs(),
                                         r_free_flags=fmodel.r_free_flags(),
                                         f_calc=f_calc,
                                         f_mask=f_mask,
                                         xrs_selected=xrs_selected,
                                         box_cushion=box_cushion)

        sites_cart_box = box_1.xray_structure_box.sites_cart()
        sel = maptbx.grid_indices_around_sites(
            unit_cell=box_1.xray_structure_box.unit_cell(),
            fft_n_real=box_1.map_box.focus(),
            fft_m_real=box_1.map_box.all(),
            sites_cart=sites_cart_box,
            site_radii=flex.double(sites_cart_box.size(), box_cushion - 0.1))
        b1 = box_1.map_box.select(sel).as_1d()
        b2 = box_2.map_box.select(sel).as_1d()
        b3 = box_3.map_box.select(sel).as_1d()
        # Map 1: calculated Fobs with ligand
        # Map 2: calculated Fobs without ligand
        # Map 3: real Fobs data
        cc12 = flex.linear_correlation(x=b1, y=b2).coefficient()
        cc13 = flex.linear_correlation(x=b1, y=b3).coefficient()
        cc23 = flex.linear_correlation(x=b2, y=b3).coefficient()
        #### D-function
        b1 = maptbx.volume_scale_1d(map=b1, n_bins=10000).map_data()
        b2 = maptbx.volume_scale_1d(map=b2, n_bins=10000).map_data()
        b3 = maptbx.volume_scale_1d(map=b3, n_bins=10000).map_data()
        cc12_peak = flex.linear_correlation(x=b1, y=b2).coefficient()
        cc13_peak = flex.linear_correlation(x=b1, y=b3).coefficient()
        cc23_peak = flex.linear_correlation(x=b2, y=b3).coefficient()
        #### Peak CC:
        cutoffs = flex.double([i / 10. for i in range(1, 10)] +
                              [i / 100 for i in range(91, 100)])
        d12 = maptbx.discrepancy_function(map_1=b1, map_2=b2, cutoffs=cutoffs)
        d13 = maptbx.discrepancy_function(map_1=b1, map_2=b3, cutoffs=cutoffs)
        d23 = maptbx.discrepancy_function(map_1=b2, map_2=b3, cutoffs=cutoffs)
        pdb_hierarchy_selected.adopt_xray_structure(box_1.xray_structure_box)
        return group_args(box_1=box_1,
                          box_2=box_2,
                          box_3=box_3,
                          cc12=cc12,
                          cc13=cc13,
                          cc23=cc23,
                          cc12_peak=cc12_peak,
                          cc13_peak=cc13_peak,
                          cc23_peak=cc23_peak,
                          d12=d12,
                          d13=d13,
                          d23=d23,
                          cutoffs=cutoffs,
                          ph_selected=pdb_hierarchy_selected)
Example #46
0
 def find_peaks_2fofc(self):
     if (self.fmodel.twin
         ):  # XXX Make it possible when someone consolidates fmodels.
         print >> self.log, "Map CC and map value based filtering is disabled for twin refinement."
         return
     print >> self.log, "Before RSCC filtering: ", \
       self.model.solvent_selection().count(True)
     assert self.fmodel.xray_structure is self.model.get_xray_structure()
     assert len(list(self.model.get_hierarchy().atoms_with_labels())) == \
       self.model.get_number_of_atoms()
     par = self.params.secondary_map_and_map_cc_filter
     selection = self.model.solvent_selection()
     # filter by map cc and value
     e_map_obj = self.fmodel.electron_density_map()
     coeffs_1 = e_map_obj.map_coefficients(map_type=par.cc_map_1_type,
                                           fill_missing=False,
                                           isotropize=True)
     coeffs_2 = e_map_obj.map_coefficients(map_type=par.cc_map_2_type,
                                           fill_missing=False,
                                           isotropize=True)
     fft_map_1 = coeffs_1.fft_map(resolution_factor=1. / 4)
     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()
     sites_cart = self.fmodel.xray_structure.sites_cart()
     sites_frac = self.fmodel.xray_structure.sites_frac()
     scatterers = self.model.get_xray_structure().scatterers()
     assert approx_equal(self.model.get_xray_structure().sites_frac(),
                         sites_frac)
     unit_cell = self.fmodel.xray_structure.unit_cell()
     for i, sel_i in enumerate(selection):
         if (sel_i):
             sel = maptbx.grid_indices_around_sites(
                 unit_cell=unit_cell,
                 fft_n_real=map_1.focus(),
                 fft_m_real=map_1.all(),
                 sites_cart=flex.vec3_double([sites_cart[i]]),
                 site_radii=flex.double([1.5]))
             cc = flex.linear_correlation(
                 x=map_1.select(sel), y=map_2.select(sel)).coefficient()
             map_value_1 = map_1.eight_point_interpolation(sites_frac[i])
             map_value_2 = map_2.eight_point_interpolation(sites_frac[i])
             if ((cc < par.poor_cc_threshold
                  or map_value_1 < par.poor_map_value_threshold
                  or map_value_2 < par.poor_map_value_threshold)
                     and not scatterers[i].element_symbol().strip().upper()
                     in ["H", "D"]):
                 selection[i] = False
     #
     sol_sel = self.model.solvent_selection()
     hd_sel = self.model.get_hd_selection()
     selection.set_selected(hd_sel, True)
     selection.set_selected(~sol_sel, True)
     xht = self.model.xh_connectivity_table()
     if (xht is not None):
         for ti in xht:
             if (not selection[ti[0]]): selection[ti[1]] = False
             if (selection[ti[0]]): selection[ti[1]] = True
     if (selection.size() != selection.count(True)):
         self.model = self.model.select(selection)
         self.fmodel.update_xray_structure(
             xray_structure=self.model.get_xray_structure(),
             update_f_calc=True)
     print >> self.log, "After RSCC filtering: ", \
       self.model.solvent_selection().count(True)
Example #47
0
def cmd_run(args, validated=False, out=sys.stdout):
    if (len(args) == 0):
        print >> out, "-" * 79
        print >> out, "                               phenix.polder"
        print >> out, "-" * 79
        print >> out, legend
        print >> out, "-" * 79
        master_params.show(out=out)
        return
    log = multi_out()
    log.register("stdout", out)
    log_file_name = "polder.log"
    logfile = open(log_file_name, "w")
    log.register("logfile", logfile)
    print >> log, "phenix.polder is running..."
    print >> log, "input parameters:\n", args
    parsed = master_params
    inputs = mmtbx.utils.process_command_line_args(args=args,
                                                   master_params=parsed)
    #inputs.params.show() #check
    params = inputs.params.extract()
    # check model file
    if len(inputs.pdb_file_names) == 0:
        if (params.model_file_name is None):
            raise Sorry("No model file found.")
    elif (len(inputs.pdb_file_names) == 1):
        params.model_file_name = inputs.pdb_file_names[0]
    else:
        raise Sorry("Only one model file should be given")
    # check reflection file
    reflection_files = inputs.reflection_files
    if (len(reflection_files) == 0):
        if (params.reflection_file_name is None):
            raise Sorry("No reflection file found.")
        else:
            hkl_in = file_reader.any_file(params.reflection_file_name,
                                          force_type="hkl")
            hkl_in.assert_file_type("hkl")
            reflection_files = [hkl_in.file_object]
    # crystal symmetry
    crystal_symmetry = None
    crystal_symmetry = inputs.crystal_symmetry
    if (crystal_symmetry is None):
        crystal_symmetries = []
        for f in [
                str(params.model_file_name),
                str(params.reflection_file_name)
        ]:
            cs = crystal_symmetry_from_any.extract_from(f)
            if (cs is not None): crystal_symmetries.append(cs)
        if (len(crystal_symmetries) == 1):
            crystal_symmetry = crystal_symmetries[0]
        elif (len(crystal_symmetries) == 0):
            raise Sorry("No crystal symmetry found.")
        else:
            if (not crystal_symmetries[0].is_similar_symmetry(
                    crystal_symmetries[1])):
                raise Sorry(
                    "Crystal symmetry mismatch between different files.")
            crystal_symmetry = crystal_symmetries[0]
    f_obs, r_free_flags = None, None
    rfs = reflection_file_utils.reflection_file_server(
        crystal_symmetry=crystal_symmetry,
        force_symmetry=True,
        reflection_files=reflection_files,
        err=StringIO())
    parameters = mmtbx.utils.data_and_flags_master_params().extract()
    if (params.data_labels is not None):
        parameters.labels = params.data_labels
    if (params.r_free_flags_labels is not None):
        parameters.r_free_flags.label = params.r_free_flags_labels
    determined_data_and_flags = mmtbx.utils.determine_data_and_flags(
        reflection_file_server=rfs,
        parameters=parameters,
        keep_going=True,
        log=StringIO())
    f_obs = determined_data_and_flags.f_obs
    if (params.data_labels is None):
        params.data_labels = f_obs.info().label_string()
    if (params.reflection_file_name is None):
        params.reflection_file_name = parameters.file_name
    r_free_flags = determined_data_and_flags.r_free_flags
    assert f_obs is not None
    print >> log, "Input data:"
    print >> log, "  Iobs or Fobs:", f_obs.info().labels
    if (r_free_flags is not None):
        print >> log, "  Free-R flags:", r_free_flags.info().labels
        params.r_free_flags_labels = r_free_flags.info().label_string()
    else:
        print >> log, "  Free-R flags: Not present"
    model_basename = os.path.basename(params.model_file_name.split(".")[0])
    if (len(model_basename) > 0 and params.output_file_name_prefix is None):
        params.output_file_name_prefix = model_basename
    print params.output_file_name_prefix
    new_params = master_params.format(python_object=params)
    new_params.show()
    if (not validated):
        validate_params(params)
    pdb_input = iotbx.pdb.input(file_name=params.model_file_name)
    pdb_hierarchy = pdb_input.construct_hierarchy()
    xray_structure = pdb_hierarchy.extract_xray_structure(
        crystal_symmetry=crystal_symmetry)
    # DON'T USE:
    # xray_structure = pdb_input.xray_structure_simple()
    # atom order might be wrong
    mmtbx.utils.setup_scattering_dictionaries(
        scattering_table=params.scattering_table,
        xray_structure=xray_structure,
        d_min=f_obs.d_min())
    #if f_obs is not None:
    f_obs = f_obs.resolution_filter(d_min=params.high_resolution,
                                    d_max=params.low_resolution)
    if (r_free_flags is not None):
        r_free_flags = r_free_flags.resolution_filter(
            d_min=params.high_resolution, d_max=params.low_resolution)
# Grab case that data are anomalous
    if (f_obs.anomalous_flag()):
        f_obs, r_free_flags = prepare_f_obs_and_flags(
            f_obs=f_obs, r_free_flags=r_free_flags)
    cpm_obj = compute_polder_map(f_obs=f_obs,
                                 r_free_flags=r_free_flags,
                                 xray_structure=xray_structure,
                                 pdb_hierarchy=pdb_hierarchy,
                                 params=params,
                                 log=log)
    # Significance check
    fmodel = mmtbx.f_model.manager(f_obs=f_obs,
                                   r_free_flags=r_free_flags,
                                   xray_structure=xray_structure)
    fmodel.update_all_scales(remove_outliers=False, fast=True)
    f_obs_1 = abs(fmodel.f_model())
    fmodel.update_xray_structure(
        xray_structure=cpm_obj.xray_structure_noligand,
        update_f_calc=True,
        update_f_mask=True,
        force_update_f_mask=True)
    # PVA: do we need it? fmodel.update_all_scales(remove_outliers=False)
    f_obs_2 = abs(fmodel.f_model())
    xrs_selected = cpm_obj.pdb_hierarchy_selected.extract_xray_structure(
        crystal_symmetry=f_obs.crystal_symmetry())
    f_calc = f_obs.structure_factors_from_scatterers(
        xray_structure=cpm_obj.xray_structure_noligand).f_calc()
    f_mask = f_obs.structure_factors_from_map(map=cpm_obj.mask_polder,
                                              use_scale=True,
                                              anomalous_flag=False,
                                              use_sg=False)

    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)

    box_1 = get_poler_diff_map(f_obs=f_obs_1)
    box_2 = get_poler_diff_map(f_obs=f_obs_2)
    box_3 = get_poler_diff_map(f_obs=f_obs)
    sites_cart_box = box_1.xray_structure_box.sites_cart()
    sel = maptbx.grid_indices_around_sites(
        unit_cell=box_1.xray_structure_box.unit_cell(),
        fft_n_real=box_1.map_box.focus(),
        fft_m_real=box_1.map_box.all(),
        sites_cart=sites_cart_box,
        site_radii=flex.double(sites_cart_box.size(), 2.0))
    b1 = box_1.map_box.select(sel).as_1d()
    b2 = box_2.map_box.select(sel).as_1d()
    b3 = box_3.map_box.select(sel).as_1d()
    print >> log, "Map 1: calculated Fobs with ligand"
    print >> log, "Map 2: calculated Fobs without ligand"
    print >> log, "Map 3: real Fobs data"
    print >> log, "CC(1,2): %6.4f" % flex.linear_correlation(
        x=b1, y=b2).coefficient()
    print >> log, "CC(1,3): %6.4f" % flex.linear_correlation(
        x=b1, y=b3).coefficient()
    print >> log, "CC(2,3): %6.4f" % flex.linear_correlation(
        x=b2, y=b3).coefficient()
    ### D-function
    b1 = maptbx.volume_scale_1d(map=b1, n_bins=10000).map_data()
    b2 = maptbx.volume_scale_1d(map=b2, n_bins=10000).map_data()
    b3 = maptbx.volume_scale_1d(map=b3, n_bins=10000).map_data()
    print >> log, "Peak CC:"
    print >> log, "CC(1,2): %6.4f" % flex.linear_correlation(
        x=b1, y=b2).coefficient()
    print >> log, "CC(1,3): %6.4f" % flex.linear_correlation(
        x=b1, y=b3).coefficient()
    print >> log, "CC(2,3): %6.4f" % flex.linear_correlation(
        x=b2, y=b3).coefficient()
    cutoffs = flex.double([i / 10. for i in range(1, 10)] +
                          [i / 100 for i in range(91, 100)])
    d12 = maptbx.discrepancy_function(map_1=b1, map_2=b2, cutoffs=cutoffs)
    d13 = maptbx.discrepancy_function(map_1=b1, map_2=b3, cutoffs=cutoffs)
    d23 = maptbx.discrepancy_function(map_1=b2, map_2=b3, cutoffs=cutoffs)
    print >> log, "q    D(1,2) D(1,3) D(2,3)"
    for c, d12_, d13_, d23_ in zip(cutoffs, d12, d13, d23):
        print >> log, "%4.2f %6.4f %6.4f %6.4f" % (c, d12_, d13_, d23_)
    ###
    if (params.debug):
        box_1.write_ccp4_map(file_name="box_1_polder.ccp4")
        box_2.write_ccp4_map(file_name="box_2_polder.ccp4")
        box_3.write_ccp4_map(file_name="box_3_polder.ccp4")
        cpm_obj.pdb_hierarchy_selected.adopt_xray_structure(
            box_1.xray_structure_box)
        cpm_obj.pdb_hierarchy_selected.write_pdb_file(
            file_name="box_polder.pdb",
            crystal_symmetry=box_1.box_crystal_symmetry)
    #
    polder_file_name = "polder_map_coeffs.mtz"
    if (params.output_file_name_prefix is not None):
        polder_file_name = params.output_file_name_prefix + "_" + polder_file_name
    #
    print >> log, '*' * 79
    print >> log, 'File %s was written.' % polder_file_name
    print >> log, "Finished."
    return True
Example #48
0
def cmd_run(args, validated=False, out=sys.stdout):
  if (len(args) == 0):
    print >> out, "-"*79
    print >> out, "                               phenix.polder"
    print >> out, "-"*79
    print >> out, legend
    print >> out, "-"*79
    master_params.show(out=out)
    return
  log = multi_out()
  log.register("stdout", out)
  log_file_name = "polder.log"
  logfile = open(log_file_name, "w")
  log.register("logfile", logfile)
  print >> log, "phenix.polder is running..."
  print >> log, "input parameters:\n", args
  parsed = master_params
  inputs = mmtbx.utils.process_command_line_args(args = args,
    master_params = parsed)
  #inputs.params.show() #check
  params = inputs.params.extract()
  # check model file
  if len(inputs.pdb_file_names) == 0:
    if (params.model_file_name is None):
      raise Sorry("No model file found.")
  elif (len(inputs.pdb_file_names) == 1):
    params.model_file_name = inputs.pdb_file_names[0]
  else:
    raise Sorry("Only one model file should be given")
  # check reflection file
  reflection_files = inputs.reflection_files
  if (len(reflection_files) == 0):
    if (params.reflection_file_name is None):
      raise Sorry("No reflection file found.")
    else:
      hkl_in = file_reader.any_file(params.reflection_file_name,
        force_type="hkl")
      hkl_in.assert_file_type("hkl")
      reflection_files = [ hkl_in.file_object ]
  # crystal symmetry
  crystal_symmetry = None
  crystal_symmetry = inputs.crystal_symmetry
  if (crystal_symmetry is None):
    crystal_symmetries = []
    for f in [str(params.model_file_name), str(params.reflection_file_name)]:
      cs = crystal_symmetry_from_any.extract_from(f)
      if(cs is not None): crystal_symmetries.append(cs)
    if(len(crystal_symmetries) == 1): crystal_symmetry = crystal_symmetries[0]
    elif(len(crystal_symmetries) == 0):
      raise Sorry("No crystal symmetry found.")
    else:
      if(not crystal_symmetries[0].is_similar_symmetry(crystal_symmetries[1])):
        raise Sorry("Crystal symmetry mismatch between different files.")
      crystal_symmetry = crystal_symmetries[0]
  f_obs, r_free_flags = None, None
  rfs = reflection_file_utils.reflection_file_server(
    crystal_symmetry = crystal_symmetry,
    force_symmetry   = True,
    reflection_files = reflection_files,
    err              = StringIO())
  parameters = mmtbx.utils.data_and_flags_master_params().extract()
  if (params.data_labels is not None):
    parameters.labels = params.data_labels
  if (params.r_free_flags_labels is not None):
    parameters.r_free_flags.label = params.r_free_flags_labels
  determined_data_and_flags = mmtbx.utils.determine_data_and_flags(
    reflection_file_server = rfs,
    parameters             = parameters,
    keep_going             = True,
    log                    = StringIO())
  f_obs = determined_data_and_flags.f_obs
  if (params.data_labels is None):
    params.data_labels = f_obs.info().label_string()
  if (params.reflection_file_name is None):
    params.reflection_file_name = parameters.file_name
  r_free_flags = determined_data_and_flags.r_free_flags
  assert f_obs is not None
  print >> log,  "Input data:"
  print >> log, "  Iobs or Fobs:", f_obs.info().labels
  if (r_free_flags is not None):
    print >> log, "  Free-R flags:", r_free_flags.info().labels
    params.r_free_flags_labels = r_free_flags.info().label_string()
  else:
    print >> log, "  Free-R flags: Not present"
  model_basename = os.path.basename(params.model_file_name.split(".")[0])
  if (len(model_basename) > 0 and
    params.output_file_name_prefix is None):
    params.output_file_name_prefix = model_basename
  print params.output_file_name_prefix
  new_params =  master_params.format(python_object=params)
  new_params.show()
  if (not validated):
    validate_params(params)
  pdb_input = iotbx.pdb.input(file_name = params.model_file_name)
  pdb_hierarchy = pdb_input.construct_hierarchy()
  xray_structure = pdb_hierarchy.extract_xray_structure(
    crystal_symmetry = crystal_symmetry)
  # DON'T USE:
  # xray_structure = pdb_input.xray_structure_simple()
  # atom order might be wrong
  mmtbx.utils.setup_scattering_dictionaries(
    scattering_table = params.scattering_table,
    xray_structure   = xray_structure,
    d_min            = f_obs.d_min())
  #if f_obs is not None:
  f_obs = f_obs.resolution_filter(
    d_min = params.high_resolution,
    d_max = params.low_resolution)
  if (r_free_flags is not None):
    r_free_flags = r_free_flags.resolution_filter(
      d_min = params.high_resolution,
      d_max = params.low_resolution)
# Grab case that data are anomalous
  if (f_obs.anomalous_flag()):
    f_obs, r_free_flags = prepare_f_obs_and_flags(
      f_obs        = f_obs,
      r_free_flags = r_free_flags)
  cpm_obj = compute_polder_map(
    f_obs          = f_obs,
    r_free_flags   = r_free_flags,
    xray_structure = xray_structure,
    pdb_hierarchy  = pdb_hierarchy,
    params         = params,
    log            = log)
# Significance check
  fmodel = mmtbx.f_model.manager(
    f_obs          = f_obs,
    r_free_flags   = r_free_flags,
    xray_structure = xray_structure)
  fmodel.update_all_scales(remove_outliers=False, fast=True)
  f_obs_1 = abs(fmodel.f_model())
  fmodel.update_xray_structure(xray_structure=cpm_obj.xray_structure_noligand,
    update_f_calc=True, update_f_mask=True, force_update_f_mask=True)
  # PVA: do we need it? fmodel.update_all_scales(remove_outliers=False)
  f_obs_2 = abs(fmodel.f_model())
  xrs_selected = cpm_obj.pdb_hierarchy_selected.extract_xray_structure(
    crystal_symmetry = f_obs.crystal_symmetry())
  f_calc = f_obs.structure_factors_from_scatterers(
    xray_structure = cpm_obj.xray_structure_noligand).f_calc()
  f_mask = f_obs.structure_factors_from_map(
    map            = cpm_obj.mask_polder,
    use_scale      = True,
    anomalous_flag = False,
    use_sg         = False)
  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)
  box_1=get_poler_diff_map(f_obs = f_obs_1)
  box_2=get_poler_diff_map(f_obs = f_obs_2)
  box_3=get_poler_diff_map(f_obs = f_obs)
  sites_cart_box = box_1.xray_structure_box.sites_cart()
  sel = maptbx.grid_indices_around_sites(
    unit_cell  = box_1.xray_structure_box.unit_cell(),
    fft_n_real = box_1.map_box.focus(),
    fft_m_real = box_1.map_box.all(),
    sites_cart = sites_cart_box,
    site_radii = flex.double(sites_cart_box.size(), 2.0))
  b1 = box_1.map_box.select(sel).as_1d()
  b2 = box_2.map_box.select(sel).as_1d()
  b3 = box_3.map_box.select(sel).as_1d()
  print >> log, "Map 1: calculated Fobs with ligand"
  print >> log, "Map 2: calculated Fobs without ligand"
  print >> log, "Map 3: real Fobs data"
  print >>log, "CC(1,2): %6.4f"%flex.linear_correlation(x=b1,y=b2).coefficient()
  print >>log, "CC(1,3): %6.4f"%flex.linear_correlation(x=b1,y=b3).coefficient()
  print >>log, "CC(2,3): %6.4f"%flex.linear_correlation(x=b2,y=b3).coefficient()
  ### D-function
  b1 = maptbx.volume_scale_1d(map=b1, n_bins=10000).map_data()
  b2 = maptbx.volume_scale_1d(map=b2, n_bins=10000).map_data()
  b3 = maptbx.volume_scale_1d(map=b3, n_bins=10000).map_data()
  print >> log, "Peak CC:"
  print >>log, "CC(1,2): %6.4f"%flex.linear_correlation(x=b1,y=b2).coefficient()
  print >>log, "CC(1,3): %6.4f"%flex.linear_correlation(x=b1,y=b3).coefficient()
  print >>log, "CC(2,3): %6.4f"%flex.linear_correlation(x=b2,y=b3).coefficient()
  cutoffs = flex.double(
    [i/10. for i in range(1,10)]+[i/100 for i in range(91,100)])
  d12 = maptbx.discrepancy_function(map_1=b1, map_2=b2, cutoffs=cutoffs)
  d13 = maptbx.discrepancy_function(map_1=b1, map_2=b3, cutoffs=cutoffs)
  d23 = maptbx.discrepancy_function(map_1=b2, map_2=b3, cutoffs=cutoffs)
  print >> log, "q    D(1,2) D(1,3) D(2,3)"
  for c,d12_,d13_,d23_ in zip(cutoffs,d12,d13,d23):
    print >> log, "%4.2f %6.4f %6.4f %6.4f"%(c,d12_,d13_,d23_)
  ###
  if(params.debug):
    box_1.write_ccp4_map(file_name="box_1_polder.ccp4")
    box_2.write_ccp4_map(file_name="box_2_polder.ccp4")
    box_3.write_ccp4_map(file_name="box_3_polder.ccp4")
    cpm_obj.pdb_hierarchy_selected.adopt_xray_structure(
      box_1.xray_structure_box)
    cpm_obj.pdb_hierarchy_selected.write_pdb_file(file_name="box_polder.pdb",
      crystal_symmetry=box_1.box_crystal_symmetry)
  #
  print >> log, "Finished."
  return True
def compute(pdb_hierarchy,
            unit_cell,
            fft_n_real,
            fft_m_real,
            map_1,
            map_2,
            detail,
            atom_radius,
            use_hydrogens,
            hydrogen_atom_radius):
  assert detail in ["atom", "residue"]
  results = []
  for chain in pdb_hierarchy.chains():
    for residue_group in chain.residue_groups():
      for conformer in residue_group.conformers():
        for residue in conformer.residues():
          r_id_str = "%2s %1s %3s %4s %1s"%(chain.id, conformer.altloc,
            residue.resname, residue.resseq, residue.icode)
          r_sites_cart = flex.vec3_double()
          r_b          = flex.double()
          r_occ        = flex.double()
          r_mv1        = flex.double()
          r_mv2        = flex.double()
          r_rad        = flex.double()
          for atom in residue.atoms():
            a_id_str = "%s %4s"%(r_id_str, atom.name)
            if(atom.element_is_hydrogen()): rad = hydrogen_atom_radius
            else: rad = atom_radius
            if(not (atom.element_is_hydrogen() and not use_hydrogens)):
              map_value_1 = map_1.eight_point_interpolation(
                unit_cell.fractionalize(atom.xyz))
              map_value_2 = map_2.eight_point_interpolation(
                unit_cell.fractionalize(atom.xyz))
              r_sites_cart.append(atom.xyz)
              r_b         .append(atom.b)
              r_occ       .append(atom.occ)
              r_mv1       .append(map_value_1)
              r_mv2       .append(map_value_2)
              r_rad       .append(rad)
              if(detail == "atom"):
                sel = maptbx.grid_indices_around_sites(
                  unit_cell  = unit_cell,
                  fft_n_real = fft_n_real,
                  fft_m_real = fft_m_real,
                  sites_cart = flex.vec3_double([atom.xyz]),
                  site_radii = flex.double([rad]))
                cc = flex.linear_correlation(x=map_1.select(sel),
                  y=map_2.select(sel)).coefficient()
                result = group_args(
                  chain_id    = chain.id,
                  atom        = atom,
                  id_str      = a_id_str,
                  cc          = cc,
                  map_value_1 = map_value_1,
                  map_value_2 = map_value_2,
                  b           = atom.b,
                  occupancy   = atom.occ,
                  n_atoms     = 1)
                results.append(result)
          if(detail == "residue") and (len(r_mv1) > 0) :
            sel = maptbx.grid_indices_around_sites(
              unit_cell  = unit_cell,
              fft_n_real = fft_n_real,
              fft_m_real = fft_m_real,
              sites_cart = r_sites_cart,
              site_radii = r_rad)
            cc = flex.linear_correlation(x=map_1.select(sel),
              y=map_2.select(sel)).coefficient()
            result = group_args(
              residue     = residue,
              chain_id    = chain.id,
              id_str      = r_id_str,
              cc          = cc,
              map_value_1 = flex.mean(r_mv1),
              map_value_2 = flex.mean(r_mv2),
              b           = flex.mean(r_b),
              occupancy   = flex.mean(r_occ),
              n_atoms     = r_sites_cart.size())
            results.append(result)
  return results
Example #50
0
def exercise_grid_indices_around_sites():
  unit_cell = uctbx.unit_cell((5,5,5))
  fft_n_real = (5,5,5)
  fft_m_real = (5,5,5)
  site_radii = flex.double([0.5*3**0.5+1e-6])
  def get():
    grid_indices = maptbx.grid_indices_around_sites(
      unit_cell=unit_cell, fft_n_real=fft_n_real, fft_m_real=fft_m_real,
      sites_cart=sites_cart, site_radii=site_radii)
    return list(grid_indices)
  sites_cart = flex.vec3_double([(0.5,0.5,0.5)])
  assert get() == [0, 1, 5, 6, 25, 26, 30, 31]
  sites_cart = flex.vec3_double([(1.5,1.5,1.5)])
  assert get() == [31, 32, 36, 37, 56, 57, 61, 62]
  def sample():
    for i in xrange(-2,7):
      for j in xrange(-2,7):
        for k in xrange(-2,7):
          sites_cart = flex.vec3_double([(i+.5,j+.5,k+.5)])
          assert len(get()) == 8
  sample()
  #
  unit_cell = uctbx.unit_cell((5,6,7))
  fft_n_real = (5,6,7)
  fft_m_real = (5,6,7)
  sites_cart = flex.vec3_double([(0.5,0.5,0.5)])
  assert get() == [0, 1, 7, 8, 42, 43, 49, 50]
  fft_m_real = (5,6,8)
  assert get() == [0, 1, 8, 9, 48, 49, 56, 57]
  fft_m_real = (5,7,8)
  assert get() == [0, 1, 8, 9, 56, 57, 64, 65]
  sample()
  #
  site_radii = flex.double([2])
  assert len(get()) == 8 + 6*4
  site_radii = flex.double([1000])
  assert len(get()) == 5*6*7
  #
  unit_cell = uctbx.unit_cell((18,26,27))
  fft_n_real = (18,26,27)
  fft_m_real = (18,27,28)
  for ish in xrange(5):
    x = 2*ish+.5
    sites_cart = flex.vec3_double([[x]*3])
    sh = 3**0.5*(ish+0.5)
    site_radii = flex.double([sh-1e-6])
    s1 = set(get())
    site_radii = flex.double([sh+1e-6])
    s2 = set(get())
    for gi in sorted(s2-s1):
      i,j,k = n_dim_index_from_one_dim(gi, fft_m_real)
      assert approx_equal(abs(matrix.col((i-x,j-x,k-x))), sh)
    assert len(s1) == [0, 56, 304, 912, 1904][ish]
    assert len(s2) == [8, 88, 360, 968, 2008][ish]
  #
  unit_cell = uctbx.unit_cell((8,9,7,80,100,110))
  fft_n_real = (11,13,15)
  fft_m_real = (18,26,19)
  sites_cart = flex.vec3_double([(3,11,5)])
  ls = []
  prev = 0
  for r in itertools.count(1):
    site_radii = flex.double([r])
    l = len(get())
    assert l > prev
    ls.append(l)
    if (l == 11*13*15):
      break
    assert r < 7
    prev = l
  assert ls == [18, 155, 524, 1225, 1940, 2139, 2145]
  #
  fft_m_real = (1073741824, 1073741824, 1073741824)
  try:
    maptbx.grid_indices_around_sites(
      unit_cell=unit_cell, fft_n_real=fft_n_real, fft_m_real=fft_m_real,
      sites_cart=sites_cart, site_radii=site_radii)
  except RuntimeError, e:
    assert str(e).startswith("product of fft_m_real")
Example #51
0
 def get():
   grid_indices = maptbx.grid_indices_around_sites(
     unit_cell=unit_cell, fft_n_real=fft_n_real, fft_m_real=fft_m_real,
     sites_cart=sites_cart, site_radii=site_radii)
   return list(grid_indices)
Example #52
0
 def __call__ (self,
               map_coeffs,
               fmodel,
               generate_new_mask=False,
               map_type=None) :
   # XXX probably not a good idea to average anomalous maps
   #if (map_type is not None) and (map_type.lower().startswith("anom")) :
   #  return map_coeffs
   from solve_resolve.resolve_python.resolve_utils import get_map_mask_sg_cell
   from solve_resolve.resolve_python.ncs_average import ncs_average
   from cctbx import maptbx
   from scitbx.array_family import flex
   if (map_coeffs.anomalous_flag()) :
     map_coeffs = map_coeffs.average_bijvoet_mates()
   fft_map = map_coeffs.fft_map(
     symmetry_flags=maptbx.use_space_group_symmetry,
     resolution_factor=self.params.resolution_factor)
   map = fft_map.apply_volume_scaling().real_map_unpadded().as_float()
   if (self.verbose) :
     out = self.log
   else :
     out = null_out()
   if (self.mask is None) or (generate_new_mask) :
     if (self.params.use_molecule_mask) :
       self.mask = flex.float(real_map.size(), 0)
       sites_cart = fmodel.xray_structure.sites_cart()
       if (self.params.exclude_hd) :
         sites_cart = sites_cart.select(~fmodel.xray_structure.hd_selection())
       indices = maptbx.grid_indices_around_sites(
         unit_cell=map_coeffs.unit_cell(),
         fft_n_real=real_map.focus(),
         fft_m_real=real_map.all(),
         sites_cart=sites_cart,
         site_radii=flex.double(sites_cart.size(),
           self.params.averaging_radius))
       mask.set_selected(indices, 1)
       mask.reshape(real_map.accessor())
     else :
       mask_map_coeffs = fmodel.electron_density_map().map_coefficients(
           map_type="2mFo-DFc")
       mask_fft_map = mask_map_coeffs.fft_map(
         symmetry_flags=maptbx.use_space_group_symmetry,
         resolution_factor=self.params.resolution_factor)
       mask_map = mask_fft_map.apply_volume_scaling().real_map_unpadded().as_float()
       map_db,mask_map_db,space_group_object,unit_cell_object=\
         get_map_mask_sg_cell(
           map_coeffs=mask_map_coeffs,
           map=mask_map,
           space_group=map_coeffs.space_group(),
           unit_cell=map_coeffs.unit_cell(),
           solvent_content=self.params.solvent_content,
           wang_radius=self.params.averaging_radius,
           resolution=map_coeffs.d_min(),
           out=out,
           resolve_command_list=None)
       #map = map_db.map
       self.mask = mask_map_db.map
   averaged = ncs_average(
     map=map,
     mask=self.mask,
     ncs_object=self.ncs_object,
     space_group=map_coeffs.space_group(),
     unit_cell=map_coeffs.unit_cell(),
     resolution=map_coeffs.d_min(),
     out=out)
   new_map_coeffs = map_coeffs.structure_factors_from_map(
     map=averaged.average_map.as_double(),
     use_sg=True)
   return new_map_coeffs
Example #53
0
 def find_peaks_2fofc(self):
   if(self.fmodel.twin): # XXX Make it possible when someone consolidates fmodels.
     print >> self.log, "Map CC and map value based filtering is disabled for twin refinement."
     return
   print >> self.log, "Before RSCC filtering: ", \
     self.model.solvent_selection().count(True)
   assert self.fmodel.xray_structure is self.model.xray_structure
   assert len(list(self.model.pdb_hierarchy().atoms_with_labels())) == \
     self.model.xray_structure.scatterers().size()
   par = self.params.secondary_map_and_map_cc_filter
   selection = self.model.solvent_selection()
   # filter by map cc and value
   e_map_obj = self.fmodel.electron_density_map()
   coeffs_1 = e_map_obj.map_coefficients(
     map_type     = par.cc_map_1_type,
     fill_missing = False,
     isotropize   = True)
   coeffs_2 = e_map_obj.map_coefficients(
     map_type     = par.cc_map_2_type,
     fill_missing = False,
     isotropize   = True)
   fft_map_1 = coeffs_1.fft_map(resolution_factor = 1./4)
   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()
   sites_cart = self.fmodel.xray_structure.sites_cart()
   sites_frac = self.fmodel.xray_structure.sites_frac()
   scatterers = self.model.xray_structure.scatterers()
   assert approx_equal(self.model.xray_structure.sites_frac(), sites_frac)
   unit_cell = self.fmodel.xray_structure.unit_cell()
   for i, sel_i in enumerate(selection):
     if(sel_i):
       sel = maptbx.grid_indices_around_sites(
         unit_cell  = unit_cell,
         fft_n_real = map_1.focus(),
         fft_m_real = map_1.all(),
         sites_cart = flex.vec3_double([sites_cart[i]]),
         site_radii = flex.double([1.5]))
       cc = flex.linear_correlation(x=map_1.select(sel),
         y=map_2.select(sel)).coefficient()
       map_value_1 = map_1.eight_point_interpolation(sites_frac[i])
       map_value_2 = map_2.eight_point_interpolation(sites_frac[i])
       if((cc < par.poor_cc_threshold or
          map_value_1 < par.poor_map_value_threshold or
          map_value_2 < par.poor_map_value_threshold) and not
          scatterers[i].element_symbol().strip().upper() in ["H","D"]):
         selection[i]=False
   #
   sol_sel = self.model.solvent_selection()
   hd_sel = self.model.xray_structure.hd_selection()
   selection.set_selected(hd_sel, True)
   selection.set_selected(~sol_sel, True)
   xht = self.model.xh_connectivity_table()
   if(xht is not None):
     for ti in xht:
       if(not selection[ti[0]]): selection[ti[1]]=False
       if(selection[ti[0]]): selection[ti[1]]=True
   if(selection.size() != selection.count(True)):
     self.model = self.model.select(selection)
     self.fmodel.update_xray_structure(
       xray_structure = self.model.xray_structure,
       update_f_calc  = True)
   print >> self.log, "After RSCC filtering: ", \
     self.model.solvent_selection().count(True)
def get_map_values_and_grid_sites_frac(
      fmodel,
      map_type,
      grid_step,
      d_min,
      apply_sigma_scaling,
      apply_volume_scaling,
      include_f000,
      sel_bb,
      use_exact_phases):
  #
  resolution_factor = grid_step/d_min
  mp = mmtbx.masks.mask_master_params.extract()
  mp.grid_step_factor = 1./resolution_factor
  mmtbx_masks_asu_mask_obj = mmtbx.masks.asu_mask(
    xray_structure = fmodel.xray_structure,
    d_min          = d_min,
    mask_params    = mp)
  bulk_solvent_mask = mmtbx_masks_asu_mask_obj.mask_data_whole_uc()
  sel = bulk_solvent_mask > 0
  bulk_solvent_mask = bulk_solvent_mask.set_selected(sel, 1)
  cr_gr = maptbx.crystal_gridding(
    unit_cell             = fmodel.xray_structure.unit_cell(),
    space_group_info      = fmodel.f_obs().space_group_info(),
    pre_determined_n_real = bulk_solvent_mask.focus())
  from mmtbx import map_tools
  from cctbx import miller
  #
  #mc = map_tools.electron_density_map(fmodel = fmodel).map_coefficients(
  #  map_type = map_type,
  #  acentrics_scale = 1.0,
  #  centrics_pre_scale = 1.0)
  if not use_exact_phases:
    k = fmodel.k_isotropic()*fmodel.k_anisotropic()
    print "flex.mean(k):", flex.mean(k)
    f_model = fmodel.f_model()
    mc_data = abs(fmodel.f_obs()).data()/k - abs(f_model).data()/k

    tmp = miller.array(miller_set = f_model,
      data = flex.double(f_model.indices().size(), 1)
      ).phase_transfer(phase_source = f_model)
    mc = miller.array(miller_set = tmp,
      data = mc_data * tmp.data())
  else:
    fmodel.update_all_scales(fast=True, remove_outliers=False)
    k = fmodel.k_isotropic()*fmodel.k_anisotropic()
    fo = fmodel.f_obs().customized_copy(data = fmodel.f_obs().data()/k)
    fo = fo.phase_transfer(phase_source = fmodel.f_model())
    fc = fmodel.f_calc().customized_copy(data = fmodel.f_calc().data())
    mc = miller.array(miller_set = fo,
      data = fo.data()-fc.data())




  ######## XXX
  fft_map = miller.fft_map(
    crystal_gridding     = cr_gr,
    fourier_coefficients = mc)
  fft_map.apply_volume_scaling()
  map_data = fft_map.real_map_unpadded()

  xrs = fmodel.xray_structure
  sites_cart = xrs.sites_cart().select(sel_bb)
  sel = maptbx.grid_indices_around_sites(
    unit_cell  = xrs.unit_cell(),
    fft_n_real = map_data.focus(),
    fft_m_real = map_data.all(),
    sites_cart = sites_cart,
    site_radii = flex.double(sites_cart.size(), 0.5))
  map_in  = map_data.select(sel)
  mm = flex.mean(map_in)
  print "mean in (1):", mm
  #
  #sites_frac = xrs.sites_frac().select(sel_bb)
  #mm = 0
  #for sf in sites_frac:
  #  mm += map_data.eight_point_interpolation(sf)
  #mm = mm/sites_frac.size()
  #print "mean in (2):", mm
  ########

  #
  # Add F000
  #reg = fmodel.xray_structure.scattering_type_registry(table = "wk1995")
  #f_000 = reg.sum_of_scattering_factors_at_diffraction_angle_0() +\
  #  0.4*fmodel.xray_structure.unit_cell().volume()
  if(include_f000):
    #f_000 = include_f000*fmodel.xray_structure.unit_cell().volume()*0.3
    #f_000 = None # XXX
    f_000 = abs(mm * xrs.unit_cell().volume())
    #f_000 = 0.626*fmodel.xray_structure.unit_cell().volume()*0.35
  else:
    f_000 = None
  print "f_000:", f_000
  #print "XXX", include_f000*fmodel.xray_structure.unit_cell().volume()*0.3
  #
  fft_map = miller.fft_map(
    crystal_gridding     = cr_gr,
    fourier_coefficients = mc,
    f_000 = f_000)
  #
  assert [apply_sigma_scaling, apply_volume_scaling].count(True) == 1
  if(apply_sigma_scaling):    fft_map.apply_sigma_scaling()
  elif(apply_volume_scaling): fft_map.apply_volume_scaling()
  else: assert RuntimeError
  nx,ny,nz = fft_map.n_real()
  map_data = fft_map.real_map_unpadded()

  #map_data = map_data * bulk_solvent_mask
  print "n_real:", nx,ny,nz, map_data.size()
  grid_sites_frac = flex.vec3_double()
  map_values = flex.double()
  for ix in xrange(nx):
    for iy in xrange(ny):
      for iz in xrange(nz):
        mv = map_data[(ix,iy,iz)]
        if 1: #if(mv != 0):
          xf,yf,zf = ix/float(nx), iy/float(ny), iz/float(nz)
          grid_sites_frac.append([xf,yf,zf])
          map_at_ixiyiz = map_data[(ix,iy,iz)]
          map_values.append(map_at_ixiyiz)
  return map_values, grid_sites_frac
Example #55
0
    def __init__(self,
                 map_1,
                 xray_structure,
                 fft_map,
                 atom_radius,
                 hydrogen_atom_radius,
                 model_i,
                 number_previous_scatters,
                 ignore_hd=False,
                 residue_detail=True,
                 selection=None,
                 pdb_hierarchy=None):
        self.xray_structure = xray_structure
        self.selection = selection
        self.pdb_hierarchy = pdb_hierarchy
        self.result = []
        self.map_1_size = map_1.size()
        self.map_1_stat = maptbx.statistics(map_1)
        self.atoms_with_labels = None
        self.residue_detail = residue_detail
        self.model_i = model_i
        if (pdb_hierarchy is not None):
            self.atoms_with_labels = list(pdb_hierarchy.atoms_with_labels())
        scatterers = self.xray_structure.scatterers()
        sigma_occ = flex.double()
        if (self.selection is None):
            self.selection = flex.bool(scatterers.size(), True)
        real_map_unpadded = fft_map.real_map_unpadded()
        sites_cart = self.xray_structure.sites_cart()

        if not self.residue_detail:
            self.gifes = [
                None,
            ] * scatterers.size()
            self._result = [
                None,
            ] * scatterers.size()
            #
            atom_radii = flex.double(scatterers.size(), atom_radius)
            for i_seq, sc in enumerate(scatterers):
                if (self.selection[i_seq]):
                    if (sc.element_symbol().strip().lower() in ["h", "d"]):
                        atom_radii[i_seq] = hydrogen_atom_radius
            #
            for i_seq, site_cart in enumerate(sites_cart):
                if (self.selection[i_seq]):
                    sel = maptbx.grid_indices_around_sites(
                        unit_cell=self.xray_structure.unit_cell(),
                        fft_n_real=real_map_unpadded.focus(),
                        fft_m_real=real_map_unpadded.all(),
                        sites_cart=flex.vec3_double([site_cart]),
                        site_radii=flex.double([atom_radii[i_seq]]))
                    self.gifes[i_seq] = sel
                    m1 = map_1.select(sel)
                    ed1 = map_1.eight_point_interpolation(
                        scatterers[i_seq].site)
                    sigma_occ.append(ed1)
                    a = None
                    if (self.atoms_with_labels is not None):
                        a = self.atoms_with_labels[i_seq]
                    self._result[i_seq] = group_args(atom=a,
                                                     m1=m1,
                                                     ed1=ed1,
                                                     xyz=site_cart)
            self.xray_structure.set_occupancies(sigma_occ)

            ### For testing other residue averaging options
            residues = self.extract_residues(
                model_i=model_i,
                number_previous_scatters=number_previous_scatters)
            self.xray_structure.residue_selections = residues

        # Residue detail
        if self.residue_detail:
            assert self.pdb_hierarchy is not None
            residues = self.extract_residues(
                model_i=model_i,
                number_previous_scatters=number_previous_scatters)
            self.gifes = [
                None,
            ] * len(residues)
            self._result = [
                None,
            ] * len(residues)
            for i_seq, residue in enumerate(residues):
                residue_sites_cart = sites_cart.select(residue.selection)
                if 0: print(i_seq, list(residue.selection))  # DEBUG
                sel = maptbx.grid_indices_around_sites(
                    unit_cell=self.xray_structure.unit_cell(),
                    fft_n_real=real_map_unpadded.focus(),
                    fft_m_real=real_map_unpadded.all(),
                    sites_cart=residue_sites_cart,
                    site_radii=flex.double(residue.selection.size(),
                                           atom_radius))
                self.gifes[i_seq] = sel
                m1 = map_1.select(sel)
                ed1 = flex.double()
                for i_seq_r in residue.selection:
                    ed1.append(
                        map_1.eight_point_interpolation(
                            scatterers[i_seq_r].site))
                self._result[i_seq] = \
                  group_args(residue = residue, m1 = m1, ed1 = flex.mean(ed1),
                    xyz=residue_sites_cart.mean(), n_atoms=residue_sites_cart.size())

                residue_scatterers = scatterers.select(residue.selection)
                residue_ed1 = flex.double()
                for n, scatter in enumerate(residue_scatterers):
                    if ignore_hd:
                        if scatter.element_symbol() not in ['H', 'D']:
                            residue_ed1.append(ed1[n])
                    else:
                        residue_ed1.append(ed1[n])
                for x in range(ed1.size()):
                    sigma_occ.append(flex.mean(residue_ed1))

            self.xray_structure.set_occupancies(sigma_occ)
            self.xray_structure.residue_selections = residues

        del map_1
def rho_stats(xray_structure, d_min, resolution_factor, electron_sum_radius,
              zero_out_f000):
    n_real = []
    n_half_plus = []
    n_half_minus = []
    s2 = d_min * resolution_factor * 2
    for l in xray_structure.unit_cell().parameters()[:3]:
        nh = ifloor(l / s2)
        n_real.append(2 * nh + 1)
        n_half_plus.append(nh)
        n_half_minus.append(-nh)
    n_real = tuple(n_real)
    n_real_product = matrix.col(n_real).product()
    crystal_gridding = maptbx.crystal_gridding(
        unit_cell=xray_structure.unit_cell(),
        space_group_info=xray_structure.space_group_info(),
        pre_determined_n_real=n_real)
    miller_indices = flex.miller_index()
    miller_indices.reserve(n_real_product)
    for h in flex.nested_loop(n_half_minus, n_half_plus, open_range=False):
        miller_indices.append(h)
    assert miller_indices.size() == n_real_product
    #
    miller_set = miller.set(crystal_symmetry=xray_structure,
                            anomalous_flag=True,
                            indices=miller_indices).sort(by_value="resolution")
    assert miller_set.indices()[0] == (0, 0, 0)
    f_calc = miller_set.structure_factors_from_scatterers(
        xray_structure=xray_structure, algorithm="direct",
        cos_sin_table=False).f_calc()
    if (zero_out_f000):
        f_calc.data()[0] = 0j
    #
    unit_cell_volume = xray_structure.unit_cell().volume()
    voxel_volume = unit_cell_volume / n_real_product
    number_of_miller_indices = []
    rho_max = []
    electron_sums_around_atoms = []
    densities_along_x = []
    for f in [f_calc, f_calc.resolution_filter(d_min=d_min)]:
        assert f.indices()[0] == (0, 0, 0)
        number_of_miller_indices.append(f.indices().size())
        fft_map = miller.fft_map(crystal_gridding=crystal_gridding,
                                 fourier_coefficients=f)
        assert fft_map.n_real() == n_real
        rho = fft_map.real_map_unpadded() / unit_cell_volume
        assert approx_equal(voxel_volume * flex.sum(rho), f_calc.data()[0])
        if (xray_structure.scatterers().size() == 1):
            assert flex.max_index(rho) == 0
            rho_max.append(rho[0])
        else:
            rho_max.append(flex.max(rho))
        site_cart = xray_structure.sites_cart()[0]
        gias = maptbx.grid_indices_around_sites(
            unit_cell=xray_structure.unit_cell(),
            fft_n_real=n_real,
            fft_m_real=n_real,
            sites_cart=flex.vec3_double([site_cart]),
            site_radii=flex.double([electron_sum_radius]))
        electron_sums_around_atoms.append(
            flex.sum(rho.as_1d().select(gias)) * voxel_volume)
        #
        a = xray_structure.unit_cell().parameters()[0]
        nx = n_real[0]
        nxh = nx // 2
        x = []
        y = []
        for ix in range(-nxh, nxh + 1):
            x.append(a * ix / nx)
            y.append(rho[(ix % nx, 0, 0)])
        densities_along_x.append((x, y))
    #
    print(
        "%3.1f %4.2f %-12s %5d %5d | %6.3f %6.3f | %6.3f %6.3f | %4.2f %5.1f" %
        (d_min, resolution_factor, n_real, number_of_miller_indices[0],
         number_of_miller_indices[1], electron_sums_around_atoms[0],
         electron_sums_around_atoms[1], rho_max[0], rho_max[1],
         f_calc.data()[0].real, u_as_b(xray_structure.scatterers()[0].u_iso)))
    #
    return densities_along_x