Esempio n. 1
0
 def apply_back_trace_of_overall_exp_scale_matrix(self, xray_structure=None):
   k,b=self.overall_isotropic_kb_estimate()
   k_total = self.core.k_isotropic * self.core.k_anisotropic * \
     self.core.k_isotropic_exp
   k,b,r = mmtbx.bulk_solvent.fit_k_exp_b_to_k_total(k_total, self.ss, k, b)
   if(r<0.7): self.k_exp_overall,self.b_exp_overall = k,b
   if(xray_structure is None): return None
   b_adj = 0
   if([self.k_exp_overall,self.b_exp_overall].count(None)==0 and k != 0):
     bs1 = xray_structure.extract_u_iso_or_u_equiv()*adptbx.u_as_b(1.)
     def split(b_trace, xray_structure):
       b_min = xray_structure.min_u_cart_eigenvalue()*adptbx.u_as_b(1.)
       b_res = min(0, b_min + b_trace+1.e-6)
       b_adj = b_trace-b_res
       xray_structure.shift_us(b_shift = b_adj)
       return b_adj, b_res
     b_adj,b_res=split(b_trace=self.b_exp_overall,xray_structure=xray_structure)
     k_new = self.k_exp_overall*flex.exp(-self.ss*b_adj)
     bs2 = xray_structure.extract_u_iso_or_u_equiv()*adptbx.u_as_b(1.)
     diff = bs2-bs1
     assert approx_equal(flex.min(diff), flex.max(diff))
     assert approx_equal(flex.max(diff), b_adj)
     self.core = self.core.update(
       k_isotropic = self.core.k_isotropic,
       k_isotropic_exp = self.core.k_isotropic_exp/k_new,
       k_masks = [m*flex.exp(-self.ss*b_adj) for m in self.core.k_masks])
   return group_args(
     xray_structure = xray_structure,
     k_isotropic    = self.k_isotropic(),
     k_anisotropic  = self.k_anisotropic(),
     k_mask         = self.k_masks(),
     b_adj          = b_adj)
Esempio n. 2
0
def check_adp_set_b_iso(
      cmd, xrsp_init, output, selection, selection_str, verbose,
      tolerance=1.e-3):
  remove_files(output)
  run_command(command=cmd, verbose=verbose)
  xrsp = xray_structure_plus(file_name = output)
  assert approx_equal(xrsp.occ,        xrsp_init.occ,tolerance)
  assert approx_equal(xrsp.sites_cart, xrsp_init.sites_cart,tolerance)
  assert approx_equal(xrsp.use_u_iso,  xrsp_init.use_u_iso,tolerance)
  assert approx_equal(xrsp.use_u_aniso,xrsp_init.use_u_aniso,tolerance)
  assert approx_equal(xrsp.u_iso_not_used,  xrsp_init.u_iso_not_used,tolerance)
  assert approx_equal(xrsp.u_cart_not_used,xrsp_init.u_cart_not_used,tolerance)
  if(selection_str is None):
    assert not_approx_equal(xrsp.u_iso_used,  xrsp_init.u_iso_used,tolerance)
    for ucart in xrsp.u_cart:
      b_iso = adptbx.u_as_b(adptbx.u_cart_as_u_iso(ucart))
      if b_iso > 0: assert approx_equal(b_iso, 10, 0.005)
      else: assert approx_equal(b_iso, -78.956, 0.005)
  else:
    arg1 = xrsp.u_iso_used.select(selection.select(xrsp.use_u_iso))
    arg2 = xrsp_init.u_iso_used.select(selection.select(xrsp_init.use_u_iso))
    if(arg1.size() > 0): assert not_approx_equal(arg1, arg2,tolerance)
    for ucart in xrsp.u_cart:
      b_iso = adptbx.u_as_b(adptbx.u_cart_as_u_iso(ucart))
      if b_iso > 0: assert approx_equal(b_iso, 10, 0.005)
      else: assert approx_equal(b_iso, -78.956, 0.005)
Esempio n. 3
0
 def __init__(self, xray_structure, k_anisotropic, k_masks, ss):
   self.xray_structure = xray_structure
   self.k_anisotropic  = k_anisotropic
   self.k_masks        = k_masks
   self.ss             = ss
   #
   k_total = self.k_anisotropic
   r = scitbx.math.gaussian_fit_1d_analytical(x=flex.sqrt(self.ss), y=k_total)
   k,b = r.a, r.b
   #
   k,b,r = mmtbx.bulk_solvent.fit_k_exp_b_to_k_total(k_total, self.ss, k, b)
   k_exp_overall, b_exp_overall = None,None
   if(r<0.7): k_exp_overall, b_exp_overall = k,b
   if(self.xray_structure is None): return None
   b_adj = 0
   if([k_exp_overall, b_exp_overall].count(None)==0 and k != 0):
     bs1 = self.xray_structure.extract_u_iso_or_u_equiv()*adptbx.u_as_b(1.)
     def split(b_trace, xray_structure):
       b_min = xray_structure.min_u_cart_eigenvalue()*adptbx.u_as_b(1.)
       b_res = min(0, b_min + b_trace+1.e-6)
       b_adj = b_trace-b_res
       xray_structure.shift_us(b_shift = b_adj)
       return b_adj, b_res
     b_adj,b_res=split(b_trace=b_exp_overall,xray_structure=self.xray_structure)
     k_new = k_exp_overall*flex.exp(-self.ss*b_adj)
     bs2 = self.xray_structure.extract_u_iso_or_u_equiv()*adptbx.u_as_b(1.)
     diff = bs2-bs1
     assert approx_equal(flex.min(diff), flex.max(diff))
     assert approx_equal(flex.max(diff), b_adj)
     self.k_anisotropic = self.k_anisotropic/k_new
     self.k_masks = [m*flex.exp(-self.ss*b_adj) for m in self.k_masks]
Esempio n. 4
0
def as_pdb_file(self,
      remark,
      remarks,
      fractional_coordinates,
      resname,
      connect):
  if (remark is not None):
    remarks.insert(0, remark)
  s = StringIO()
  for remark in remarks:
    print >> s, "REMARK", remark
  print >> s, "REMARK Number of scatterers:", self.scatterers().size()
  print >> s, "REMARK At special positions:", \
    self.special_position_indices().size()
  if (fractional_coordinates):
    print >> s, "REMARK Fractional coordinates"
  else:
    print >> s, "REMARK Cartesian coordinates"
  print >> s, iotbx.pdb.format_cryst1_record(crystal_symmetry=self)
  print >> s, iotbx.pdb.format_scale_records(unit_cell=self.unit_cell())
  atom = iotbx.pdb.hierarchy.atom_with_labels()
  if (resname is not None):
    atom.resname = resname.upper()
  serial = 0
  for scatterer in self.scatterers():
    serial += 1
    atom.serial = iotbx.pdb.hy36encode(width=5, value=serial)
    if (scatterer.flags.use_u_aniso_only()):
      atom.uij = adptbx.u_star_as_u_cart(self.unit_cell(), scatterer.u_star)
      atom.b = adptbx.u_as_b(adptbx.u_cart_as_u_iso(atom.uij))
    else:
      atom.uij_erase()
      atom.b = adptbx.u_as_b(scatterer.u_iso)
    if (fractional_coordinates):
      atom.xyz = scatterer.site
    else:
      atom.xyz = self.unit_cell().orthogonalize(scatterer.site)
    atom.occ = scatterer.occupancy
    label = scatterer.label.upper()
    atom.name = label[:4]
    if (resname is None):
      atom.resname = label[:3]
    element_symbol = scatterer.element_symbol()
    if (element_symbol is None): element_symbol = "Q"
    assert len(element_symbol) in (1,2)
    atom.element = element_symbol.upper()
    atom.resseq = iotbx.pdb.hy36encode(width=4, value=serial)
    print >> s, atom.format_atom_record_group()
  if (connect is not None):
    assert len(connect) == self.scatterers().size()
    i = 0
    for bonds in connect:
      i += 1
      l = "CONNECT%5d" % i
      for bond in bonds:
        l += "%5d" % (bond+1)
      print >> s, l
  print >> s, "END"
  return s.getvalue()
Esempio n. 5
0
 def remove_common_isotropic_adp(self):
   xrs = self.xray_structure
   b_iso_min = flex.min(xrs.extract_u_iso_or_u_equiv()*adptbx.u_as_b(1))
   self.b_overall = b_iso_min
   print >> self.log, "Max B subtracted from atoms and used to sharpen map:", b_iso_min
   xrs.shift_us(b_shift=-b_iso_min)
   b_iso_min = flex.min(xrs.extract_u_iso_or_u_equiv()*adptbx.u_as_b(1))
   assert approx_equal(b_iso_min, 0, 1.e-3)
Esempio n. 6
0
def run_00():
  time_aniso_u_scaler = 0
  for symbol in sgtbx.bravais_types.acentric + sgtbx.bravais_types.centric:
    #print symbol, "-"*50
    space_group_info = sgtbx.space_group_info(symbol = symbol)
    xrs = random_structure.xray_structure(
      space_group_info  = space_group_info,
      elements          = ["N"]*100,
      volume_per_atom   = 50.0,
      random_u_iso      = True)
    # XXX ad a method to adptbx to do this
    point_group = sgtbx.space_group_info(
      symbol=symbol).group().build_derived_point_group()
    adp_constraints = sgtbx.tensor_rank_2_constraints(
      space_group=point_group,
      reciprocal_space=True)
    u_star = adptbx.u_cart_as_u_star(xrs.unit_cell(),
      adptbx.random_u_cart(u_scale=1,u_min=0.1))
    u_indep = adp_constraints.independent_params(all_params=u_star)
    u_star = adp_constraints.all_params(independent_params=u_indep)
    b_cart_start=adptbx.u_as_b(adptbx.u_star_as_u_cart(xrs.unit_cell(), u_star))
    #
    tr = (b_cart_start[0]+b_cart_start[1]+b_cart_start[2])/3
    b_cart_start = [b_cart_start[0]-tr,b_cart_start[1]-tr,b_cart_start[2]-tr,
           b_cart_start[3],b_cart_start[4],b_cart_start[5]]
    tr = (b_cart_start[0]+b_cart_start[1]+b_cart_start[2])/3
    #
    #print "Input b_cart :", " ".join(["%8.4f"%i for i in b_cart_start]), "tr:", tr
    F = xrs.structure_factors(d_min = 2.0).f_calc()
    u_star = adptbx.u_cart_as_u_star(
      F.unit_cell(), adptbx.b_as_u(b_cart_start))
    fbc = mmtbx.f_model.ext.k_anisotropic(F.indices(), u_star)
    fc = F.structure_factors_from_scatterers(xray_structure=xrs).f_calc()
    f_obs = F.customized_copy(data = flex.abs(fc.data()*fbc))
    t0 = time.time()
    #
    obj = bulk_solvent.aniso_u_scaler(
      f_model_abs    = flex.abs(fc.data()),
      f_obs          = f_obs.data(),
      miller_indices = f_obs.indices(),
      adp_constraint_matrix = adp_constraints.gradient_sum_matrix())
    time_aniso_u_scaler += (time.time()-t0)
    b_cart_final = adptbx.u_as_b(adptbx.u_star_as_u_cart(f_obs.unit_cell(),
      adp_constraints.all_params(tuple(obj.u_star_independent))))
    #
    obj = bulk_solvent.aniso_u_scaler(
      f_model_abs    = flex.abs(fc.data()),
      f_obs          = f_obs.data(),
      miller_indices = f_obs.indices())
    b_cart_final2 = adptbx.u_as_b(adptbx.u_star_as_u_cart(f_obs.unit_cell(),
      tuple(obj.u_star)))
    #
    assert approx_equal(b_cart_final, b_cart_final2)
    #print "Output b_cart:", " ".join(["%8.4f"%i for i in b_cart_final])
    assert approx_equal(b_cart_start, b_cart_final, 1.e-4)
  print "Time (aniso_u_scaler only): %6.4f"%time_aniso_u_scaler
Esempio n. 7
0
 def show_selection(i_ncs, pair):
   print >> out, prefix + "NCS selection:", \
     show_string(self.group.selection_strings[i_ncs])
   print >> out, prefix + " "*(max_label_size+2) \
     + "    B-iso   NCS ave  Difference"
   u_isos_current = self.u_isos.select(pair[1])
   u_isos_average = self.u_isos_average.select(pair[0])
   for i,c,a in zip(pair[1], u_isos_current, u_isos_average):
     c = adptbx.u_as_b(c)
     a = adptbx.u_as_b(a)
     print >> out, prefix + fmt % (site_labels[i], c, a, c-a)
Esempio n. 8
0
def create_da_xray_structures(xray_structure, params):
  def grid(sphere, gap, overlap):
    c = flex.double(sphere.center)
    x_start, y_start, z_start = c - float(sphere.radius)
    x_end, y_end, z_end       = c + float(sphere.radius)
    x_range = frange(c[0], c[0]+gap, overlap)
    y_range = frange(c[1], c[1]+gap, overlap)
    z_range = frange(c[2], c[2]+gap, overlap)
    return group_args(x_range = x_range, y_range = y_range, z_range = z_range)
  grids = []
  for sphere in params.sphere:
    grids.append(grid(sphere = sphere, gap = params.atom_gap,
      overlap = params.overlap_interval))
  initial_b_factor = params.initial_b_factor
  if(initial_b_factor is None):
    initial_b_factor = flex.mean(
      xray_structure.extract_u_iso_or_u_equiv()*adptbx.u_as_b(1.))
  da_xray_structures = []
  counter = 0
  for grid, sphere in zip(grids, params.sphere):
    cntr_g = 0 # XXX
    for x_start in grid.x_range:
      for y_start in grid.y_range:
        for z_start in grid.z_range:
          cntr_g += 1
          if(cntr_g>1): continue # XXX
          counter += 1
          new_center = flex.double([x_start, y_start, z_start])
          atom_grid = make_grid(
            center          = new_center,
            radius          = sphere.radius,
            gap             = params.atom_gap,
            occupancy       = params.initial_occupancy,
            b_factor        = initial_b_factor,
            atom_name       = params.atom_name,
            scattering_type = params.atom_type,
            resname         = params.residue_name)
          da_xray_structure = pdb_atoms_as_xray_structure(pdb_atoms = atom_grid,
            crystal_symmetry = xray_structure.crystal_symmetry())
          closest_distances_result = xray_structure.closest_distances(
            sites_frac      = da_xray_structure.sites_frac(),
            distance_cutoff = 5)
          selection = closest_distances_result.smallest_distances > 0
          selection &= closest_distances_result.smallest_distances < 1
          da_xray_structure = da_xray_structure.select(~selection)
          #print counter, da_xray_structure.scatterers().size()
          if(cntr_g==1): # XXX
            da_xray_structures.append(da_xray_structure)
  ###
  result = []
  for i, x1 in enumerate(da_xray_structures):
    for j, x2 in enumerate(da_xray_structures):
      if(x1 is not x2):
        closest_distances_result = x1.closest_distances(
          sites_frac      = x2.sites_frac(),
          distance_cutoff = 5) # XXX ???
        selection = closest_distances_result.smallest_distances > 0
        selection &= closest_distances_result.smallest_distances < params.atom_gap
        da_xray_structures[j] = x2.select(~selection)
  return da_xray_structures
Esempio n. 9
0
 def show(self, weight = None, prefix = "", show_neutron=True,
     print_stats=True):
   deltab = self.model.rms_b_iso_or_b_equiv_bonded()
   r_work = self.fmodels.fmodel_xray().r_work()*100.
   r_free = self.fmodels.fmodel_xray().r_free()*100.
   mean_b = flex.mean(
     self.model.xray_structure.extract_u_iso_or_u_equiv())*adptbx.u_as_b(1)
   if(deltab is None):
     print >> self.log, "  r_work=%5.2f r_free=%5.2f"%(r_work, r_free)
     return None
   neutron_r_work = neutron_r_free = None
   if (show_neutron) and (self.fmodels.fmodel_neutron() is not None) :
     neutron_r_work = self.fmodels.fmodel_neutron().r_work()*100.
     neutron_r_free = self.fmodels.fmodel_neutron().r_free()*100.
   xrs = self.fmodels.fmodel_xray().xray_structure
   result = weight_result(
     r_work=r_work,
     r_free=r_free,
     delta_b=deltab,
     mean_b=mean_b,
     weight=weight,
     xray_target=self.fmodels.fmodel_xray().target_w(),
     neutron_r_work=neutron_r_work,
     neutron_r_free=neutron_r_free,
     u_star=xrs.scatterers().extract_u_star(),
     u_iso=xrs.scatterers().extract_u_iso())
   if (print_stats) :
     result.show(out=self.log)
   return result
Esempio n. 10
0
 def debye_waller_factors(self,
       miller_index=None,
       miller_indices=None,
       u_iso=None,
       b_iso=None,
       u_cart=None,
       b_cart=None,
       u_cif=None,
       u_star=None,
       exp_arg_limit=50,
       truncate_exp_arg=False):
   assert [miller_index, miller_indices].count(None) == 1
   assert [u_iso, b_iso, u_cart, b_cart, u_cif, u_star].count(None) == 5
   from cctbx import adptbx
   h = miller_index
   if (h is None): h = miller_indices
   if (u_iso is not None):
     b_iso = adptbx.u_as_b(u_iso)
   if (b_iso is not None):
     return adptbx.debye_waller_factor_b_iso(
       self.stol_sq(h),
       b_iso, exp_arg_limit, truncate_exp_arg)
   if (b_cart is not None):
     u_cart = adptbx.b_as_u(b_cart)
   if (u_cart is not None):
     u_star = adptbx.u_cart_as_u_star(self, u_cart)
   if (u_cif is not None):
     u_star = adptbx.u_cif_as_u_star(self, u_cif)
   assert u_star is not None
   return adptbx.debye_waller_factor_u_star(
     h, u_star, exp_arg_limit, truncate_exp_arg)
Esempio n. 11
0
def finalize_model (pdb_hierarchy,
    xray_structure,
    set_b_iso=None,
    convert_to_isotropic=None,
    selection=None) :
  """
  Prepare a rebuilt model for refinement, optionally including B-factor reset.
  """
  from cctbx import adptbx
  from scitbx.array_family import flex
  pdb_atoms = pdb_hierarchy.atoms()
  if (selection is None) :
    selection = flex.bool(pdb_atoms.size(), True)
  elif isinstance(selection, str) :
    sel_cache = pdb_hierarchy.atom_selection_cache()
    selection = sel_cache.selection(selection)
  for i_seq, atom in enumerate(pdb_atoms) :
    assert (atom.parent() is not None)
    atom.segid = ""
    sc = xray_structure.scatterers()[i_seq]
    sc.label = atom.id_str()
  if (convert_to_isotropic) :
    xray_structure.convert_to_isotropic(selection=selection.iselection())
  if (set_b_iso is not None) :
    if (set_b_iso is Auto) :
      u_iso = xray_structure.extract_u_iso_or_u_equiv()
      set_b_iso = adptbx.u_as_b(flex.mean(u_iso)) / 2
    xray_structure.set_b_iso(value=set_b_iso, selection=selection)
  pdb_hierarchy.adopt_xray_structure(xray_structure)
  pdb_atoms.reset_serial()
  pdb_atoms.reset_i_seq()
  pdb_atoms.reset_tmp()
Esempio n. 12
0
def exercise_01(grid_step = 0.03, d_min = 1.0, wing_cutoff = 1.e-9):
  xrs = random_structure.xray_structure(
    space_group_info       = sgtbx.space_group_info("P 1"),
    elements               = ["O","N","C","P","S","U","AU"]*1,
    random_u_iso           = True,
    general_positions_only = False)
  # avoid excessive_range_error_limit crash
  bs = xrs.extract_u_iso_or_u_equiv()*adptbx.u_as_b(1)
  sel = bs < 1
  bs = bs.set_selected(sel, 1)
  xrs.set_b_iso(values = bs)
  #
  p = xrs.unit_cell().parameters()
  timer = user_plus_sys_time()
  res = manager(nx = int(p[0]/grid_step),
                ny = int(p[1]/grid_step),
                nz = int(p[2]/grid_step),
                scattering_type_registry = xrs.scattering_type_registry(),
                unit_cell = xrs.unit_cell(),
                scatterers = xrs.scatterers(),
                wing_cutoff = wing_cutoff)
  print "time: %10.4f" % (timer.elapsed())
  f_calc_dir = xrs.structure_factors(
    d_min     = d_min,
    algorithm = "direct").f_calc()
  #
  f_calc_den = f_calc_dir.structure_factors_from_map(map = res.density_array,
    use_scale = True)
  f1 = flex.abs(f_calc_dir.data())
  f2 = flex.abs(f_calc_den.data())
  r = flex.sum(flex.abs(f1-f2))/flex.sum(f2)
  print "r-factor:", r
  assert r < 1.e-4, r
Esempio n. 13
0
  def __init__(self,
               target_map,
               pdb_hierarchy,
               atom_radius,
               use_adp_restraints,
               nproc,
               log=None):
    adopt_init_args(self, locals())
    self.xray_structure = self.pdb_hierarchy.extract_xray_structure(
      crystal_symmetry = self.target_map.miller_array.crystal_symmetry())
    b_isos = self.xray_structure.extract_u_iso_or_u_equiv()*adptbx.u_as_b(1.)

    self.xray_structure = self.xray_structure.set_b_iso(
      value = flex.mean(b_isos))

    #for rg in self.pdb_hierarchy.residue_groups():
    #  sel = rg.atoms().extract_i_seq()
    #  sel = flex.bool(b_isos.size(), sel)
    #  self.xray_structure = self.xray_structure.set_b_iso(
    #    value     = flex.mean(b_isos.select(sel)),
    #    selection = sel)
    self.pdb_hierarchy.adopt_xray_structure(self.xray_structure)
    self.chain_selections = []
    for chain in self.pdb_hierarchy.chains():
      self.chain_selections.append(chain.atoms().extract_i_seq())
Esempio n. 14
0
def run(args):
  for file_name in args:
    print "File name:", file_name
    try:
      pdb_inp = iotbx.pdb.input(file_name=file_name)
    except KeyboardInterrupt: raise
    except Exception:
      libtbx.utils.format_exception()
    isotropic_b_factors = flex.double()
    all_eigenvalues = flex.double()
    for atom in pdb_inp.atoms():
      if (atom.uij == (-1,-1,-1,-1,-1,-1)):
        isotropic_b_factors.append(atom.b)
      else:
        all_eigenvalues.extend(flex.double(adptbx.eigenvalues(atom.uij)))
    all_eigenvalues *= adptbx.u_as_b(1)
    print "Number of isotropic atoms:  ", isotropic_b_factors.size()
    print "Number of anisotropic atoms:", all_eigenvalues.size() // 3
    if (isotropic_b_factors.size() != 0):
      print "Histogram of isotropic B-factors:"
      flex.histogram(data=isotropic_b_factors, n_slots=10).show(
        prefix="  ", format_cutoffs="%7.2f")
    if (all_eigenvalues.size() != 0):
      print "Histogram of eigenvalues of anisotropic B-factors:"
      flex.histogram(data=all_eigenvalues, n_slots=10).show(
        prefix="  ", format_cutoffs="%7.2f")
    print
Esempio n. 15
0
def show_xray_structure_statistics(xray_structure, atom_selections, hd_sel = None):
  result = group_args(
    all           = None,
    macromolecule = None,
    sidechain     = None,
    solvent       = None,
    ligand        = None,
    backbone      = None)
  if(hd_sel is not None):
    xray_structure = xray_structure.select(~hd_sel)
  for key in atom_selections.__dict__.keys():
    value = atom_selections.__dict__[key]
    if(value.count(True) > 0):
      if(hd_sel is not None):
        value = value.select(~hd_sel)
      xrs = xray_structure.select(value)
      atom_counts = xrs.scattering_types_counts_and_occupancy_sums()
      atom_counts_strs = []
      for ac in atom_counts:
        atom_counts_strs.append("%s:%s:%s"%(ac.scattering_type,str(ac.count),
          str("%10.2f"%ac.occupancy_sum).strip()))
      atom_counts_str = " ".join(atom_counts_strs)
      b_isos = xrs.extract_u_iso_or_u_equiv()
      n_aniso = xrs.use_u_aniso().count(True)
      n_not_positive_definite = xrs.is_positive_definite_u().count(False)
      b_mean = format_value("%-6.1f",adptbx.u_as_b(flex.mean(b_isos)))
      b_min = format_value("%-6.1f",adptbx.u_as_b(flex.min(b_isos)))
      b_max = format_value("%-6.1f",adptbx.u_as_b(flex.max(b_isos)))
      n_atoms = format_value("%-8d",xrs.scatterers().size()).strip()
      n_npd = format_value("%-8s",n_not_positive_definite).strip()
      occ = xrs.scatterers().extract_occupancies()
      o_mean = format_value("%-6.2f",flex.mean(occ)).strip()
      o_min = format_value("%-6.2f",flex.min(occ)).strip()
      o_max = format_value("%-6.2f",flex.max(occ)).strip()
      tmp_result = group_args(
        n_atoms         = n_atoms,
        atom_counts_str = atom_counts_str,
        b_min           = b_min,
        b_max           = b_max,
        b_mean          = b_mean,
        o_min           = o_min,
        o_max           = o_max,
        o_mean          = o_mean,
        n_aniso         = n_aniso,
        n_npd           = n_npd)
      setattr(result,key,tmp_result)
  return result
Esempio n. 16
0
 def __init__ (self,
     ligand,
     pdb_hierarchy,
     xray_structure,
     two_fofc_map,
     fofc_map,
     fmodel_map,
     reference_ligands=None,
     two_fofc_map_cutoff=1.5,
     fofc_map_cutoff=-3.0) :
   from mmtbx import real_space_correlation
   from cctbx import adptbx
   from scitbx.array_family import flex
   atom_selection = ligand.atoms().extract_i_seq()
   assert (len(atom_selection) == 1) or (not atom_selection.all_eq(0))
   manager = real_space_correlation.selection_map_statistics_manager(
     atom_selection=atom_selection,
     xray_structure=xray_structure,
     fft_m_real=two_fofc_map.all(),
     fft_n_real=two_fofc_map.focus(),
     exclude_hydrogens=True)
   stats_two_fofc = manager.analyze_map(
     map=two_fofc_map,
     model_map=fmodel_map,
     min=1.5)
   stats_fofc = manager.analyze_map(
     map=fofc_map,
     model_map=fmodel_map,
     min=-3.0)
   self.atom_selection = manager.atom_selection # XXX non-hydrogens only!
   sites_cart = xray_structure.sites_cart().select(self.atom_selection)
   self.xyz_center = sites_cart.mean()
   self.id_str = ligand.id_str()
   self.cc = stats_two_fofc.cc
   self.two_fofc_min = stats_two_fofc.min
   self.two_fofc_max = stats_two_fofc.max
   self.two_fofc_mean = stats_two_fofc.mean
   self.fofc_min = stats_fofc.min
   self.fofc_max = stats_fofc.max
   self.fofc_mean = stats_fofc.mean
   self.n_below_two_fofc_cutoff = stats_two_fofc.n_below_min
   self.n_below_fofc_cutoff = stats_fofc.n_below_min
   u_iso = xray_structure.extract_u_iso_or_u_equiv().select(
     self.atom_selection)
   u_iso_mean = flex.mean(u_iso)
   self.b_iso_mean = adptbx.u_as_b(u_iso_mean)
   occ = xray_structure.scatterers().extract_occupancies().select(
     self.atom_selection)
   self.occupancy_mean = flex.mean(occ)
   self.rmsds = self.pbss = None
   if (reference_ligands is not None) and (len(reference_ligands) > 0) :
     self.rmsds, self.pbss = compare_ligands_impl(ligand=ligand,
       reference_ligands=reference_ligands,
       max_distance_between_centers_of_mass=8.0,
       raise_sorry_if_no_matching_atoms=False,
       verbose=False,
       quiet=True)
Esempio n. 17
0
def reset_adps(fmodels, selection, params):
  xrs = fmodels.fmodel_xray().xray_structure
  b = xrs.extract_u_iso_or_u_equiv()*adptbx.u_as_b(1.)
  sel = b > params.filter.b_iso_max
  sel &= selection
  b = b.set_selected(sel, params.filter.b_iso_max)
  sel = b < params.filter.b_iso_min
  sel &= selection
  b = b.set_selected(sel, params.filter.b_iso_min)
  xrs = xrs.set_b_iso(values=b)
  fmodels.update_xray_structure(xray_structure = xrs, update_f_calc=True,
    update_f_mask=False)
Esempio n. 18
0
 def __init__ (self, pdb_hierarchy, xray_structure, params, out=sys.stdout) :
   from cctbx import adptbx
   from scitbx.array_family import flex
   self.plot_range = params.plot_range
   self.chains = []
   self.residues = []
   b_isos = xray_structure.extract_u_iso_or_u_equiv() * adptbx.u_as_b(1.0)
   occ = pdb_hierarchy.atoms().extract_occ()
   model = pdb_hierarchy.models()[0]
   for chain in model.chains() :
     main_conf = chain.conformers()[0]
     is_na = main_conf.is_na()
     is_protein = main_conf.is_protein()
     if (not is_protein) and (not is_na) :
       print >> out, "Skipping chain '%s' - not protein or DNA/RNA." %chain.id
       continue
     self.chains.append(chain.id)
     self.residues.append([])
     for residue_group in chain.residue_groups() :
       n_conformers = len(residue_group.atom_groups())
       rg_i_seqs = residue_group.atoms().extract_i_seq()
       rg_occ = residue_group.atoms().extract_occ()
       if (params.average_b_over == "residue") :
         use_i_seqs = rg_i_seqs
       elif (params.average_b_over == "mainchain") :
         use_i_seqs = []
         if (is_protein) :
           for j_seq, atom in enumerate(residue_group.atoms()) :
             #alab = atom.fetch_labels()
             if (atom.name in [" N  ", " C  ", " CA ", " O  "]) :
               use_i_seqs.append(rg_i_seqs[j_seq])
         else :
           raise Sorry("Mainchain-only mode not supported for nucleic acids.")
       else :
         use_i_seqs = []
         if (is_protein) :
           for j_seq, atom in enumerate(residue_group.atoms()) :
             if (not atom.name in [" N  ", " C  ", " CA ", " O  "]) :
               use_i_seqs.append(rg_i_seqs[j_seq])
       if (len(use_i_seqs) > 0) :
         has_partocc = ((flex.min(occ.select(use_i_seqs)) < 1.0) and
                        (n_conformers == 1))
         res_info = residue_info(
           chain_id=chain.id,
           resseq=residue_group.resseq_as_int(),
           icode=residue_group.icode,
           has_altconf=(n_conformers > 1),
           has_partocc=has_partocc,
           avg_b=flex.mean(b_isos.select(use_i_seqs)))
         self.residues[-1].append(res_info)
 def apply_back_trace_of_overall_exp_scale_matrix(self, xray_structure=None):
   if(xray_structure is None): return None
   k_sol, b_sol, b_cart = self.k_sols(), self.b_sols(), self.b_cart()
   assert len(k_sol)==1 # XXX Only one mask!
   k_sol = k_sol[0]
   b_sol = b_sol[0]
   #
   xrs = xray_structure
   if(xrs is None): return
   b_min = min(b_sol, xrs.min_u_cart_eigenvalue()*adptbx.u_as_b(1.))
   if(b_min < 0): xrs.tidy_us()
   b_iso = (b_cart[0]+b_cart[1]+b_cart[2])/3.0
   b_test = b_min+b_iso
   if(b_test < 0.0): b_adj = b_iso + abs(b_test) + 0.001
   else: b_adj = b_iso
   b_cart_new = [b_cart[0]-b_adj,b_cart[1]-b_adj,b_cart[2]-b_adj,
                 b_cart[3],      b_cart[4],      b_cart[5]]
   b_sol_new = b_sol + b_adj
   xrs.shift_us(b_shift = b_adj)
   b_min = min(b_sol_new, xrs.min_u_cart_eigenvalue()*adptbx.u_as_b(1.))
   assert b_min >= 0.0
   xrs.tidy_us()
   #
   assert self.fmodel_kbu
   k_masks = [ext.k_mask(self.fmodel_kbu.ss, k_sol, b_sol_new)]
   u_star=adptbx.u_cart_as_u_star(
     self.fmodel_kbu.f_obs.unit_cell(), adptbx.b_as_u(b_cart_new))
   k_anisotropic = ext.k_anisotropic(self.fmodel_kbu.f_obs.indices(), u_star)
   self.fmodel_kbu = self.fmodel_kbu.update(
     b_sols = [b_sol_new],
     b_cart = b_cart_new)
   return group_args(
     xray_structure = xrs,
     b_adj          = b_adj,
     b_sol          = b_sol_new,
     b_cart         = b_cart_new)
Esempio n. 20
0
 def write_pdb_file(self, out = None):
   if (out is None): out = sys.stdout
   sites_cart = self.ias_xray_structure.sites_cart()
   for i_seq, sc in enumerate(self.ias_xray_structure.scatterers()):
     a = pdb.hierarchy.atom_with_labels()
     a.hetero = True
     a.serial = i_seq+1
     a.name = sc.label[:4] # XXX
     a.resname = "IAS"
     a.resseq = i_seq+1
     a.xyz = sites_cart[i_seq]
     a.occ = sc.occupancy
     a.b = adptbx.u_as_b(sc.u_iso)
     a.element = sc.label[:2] # XXX
     print >> out, a.format_atom_record_group()
def write_ensemble_pdb(filename,
                       xrs_list,
                       ens_pdb_hierarchy):
    out = open(filename, 'w')
    crystal_symmetry = xrs_list[0].crystal_symmetry()
    print >> out,  "REMARK   3  TIME-AVERAGED ENSEMBLE REFINEMENT"
    print >> out,  "REMARK   3  OCCUPANCY = MAP SIGMA LEVEL"
    print >> out, pdb.format_cryst1_record(crystal_symmetry = crystal_symmetry)
    print >> out, pdb.format_scale_records(unit_cell = crystal_symmetry.unit_cell())
    atoms_reset_serial = True

    for i_model, xrs in enumerate(xrs_list):
      scatterers = xrs.scatterers()
      sites_cart = xrs.sites_cart()
      u_isos = xrs.extract_u_iso_or_u_equiv()
      occupancies = scatterers.extract_occupancies()
      u_carts = scatterers.extract_u_cart_plus_u_iso(xrs.unit_cell())
      scat_types = scatterers.extract_scattering_types()
      i_model_ens_pdb_hierarchy = ens_pdb_hierarchy.models()[i_model]
      pdb_atoms = i_model_ens_pdb_hierarchy.atoms()
      for j_seq, atom in enumerate(pdb_atoms):
        if j_seq < len(sites_cart):
          atom.xyz = sites_cart[j_seq]
          atom.occ = occupancies[j_seq]
          atom.b = adptbx.u_as_b(u_isos[j_seq])
          e = scat_types[j_seq]
          if (len(e) > 1 and "+-0123456789".find(e[1]) >= 0):
            atom.element = "%2s" % e[:1]
            atom.charge = "%-2s" % e[1:]
          elif (len(e) > 2):
            atom.element = "%2s" % e[:2]
            atom.charge = "%-2s" % e[2:]
          else:
            atom.element = "%2s" % e
            atom.charge = "  "
          if (scatterers[j_seq].flags.use_u_aniso()):
            atom.uij = u_carts[j_seq]
          elif(False):
            atom.uij = self.u_cart
          else:
            atom.uij = (-1,-1,-1,-1,-1,-1)
      if (atoms_reset_serial):
        atoms_reset_serial_first_value = 1
      else:
        atoms_reset_serial_first_value = None
    out.write(ens_pdb_hierarchy.as_pdb_string(
      append_end=False,
      atoms_reset_serial_first_value=atoms_reset_serial_first_value))
Esempio n. 22
0
def get_atom_radius(xray_structure=None, resolution=None, radius=None):
    if (radius is not None): return radius
    radii = []
    if (resolution is not None):
        radii.append(resolution)
    if (xray_structure is not None and resolution is not None):
        b_iso = adptbx.u_as_b(
            flex.mean(xray_structure.extract_u_iso_or_u_equiv()))
        o = maptbx.atom_curves(scattering_type="C",
                               scattering_table="electron")
        rad_image = o.image(d_min=resolution,
                            b_iso=b_iso,
                            radius_max=max(15., resolution),
                            radius_step=0.01).radius
        radii.append(rad_image)
    return max(3, min(10, max(radii)))
 def _b_cart_minimizer_helper(self, n_macro_cycles = 1):
   r_start = self.fmodel_kbu.r_factor()
   b_start = self.fmodel_kbu.b_cart()
   for u_cycle in xrange(n_macro_cycles):
     u_min = k_sol_b_sol_b_cart_minimizer(
       fmodel_kbu    = self.fmodel_kbu,
       params        = self.params,
       refine_u_star = True).kbu.u_star()
     b_cart = adptbx.u_as_b(
       adptbx.u_star_as_u_cart(self.fmodel_kbu.f_obs.unit_cell(),u_min))
     self.fmodel_kbu.update(b_cart = b_cart)
   r_final = self.fmodel_kbu.r_factor()
   if(r_final >= r_start):
     self.fmodel_kbu.update(b_cart = b_start)
     return b_start
   else: return b_cart
Esempio n. 24
0
def exercise_4_f_hydrogens():
    for d_min in [1, 2, 3]:
        for q in [0, 0.9, 1]:
            random.seed(0)
            flex.set_random_seed(0)
            x = random_structure.xray_structure(
                space_group_info=sgtbx.space_group_info("P 4"),
                elements=(("O", "N", "C") * 5 + ("H", ) * 95),
                volume_per_atom=200,
                min_distance=1.5,
                general_positions_only=True,
                random_u_iso=True,
                random_occupancy=False)
            hd_sel = x.hd_selection()
            b_isos = x.select(
                ~hd_sel).extract_u_iso_or_u_equiv() * adptbx.u_as_b(1.)
            mmm = b_isos.min_max_mean().as_tuple()
            b_mean = int(mmm[2])
            x = x.set_b_iso(value=b_mean, selection=hd_sel)
            x.scattering_type_registry(table="wk1995")
            x.set_occupancies(value=q, selection=hd_sel)
            fc = x.structure_factors(d_min=d_min, algorithm="direct").f_calc()
            f_obs = abs(fc)
            x = x.deep_copy_scatterers()
            x.set_occupancies(value=0.0, selection=x.hd_selection())
            sfg_params = mmtbx.f_model.sf_and_grads_accuracy_master_params.extract(
            )
            sfg_params.algorithm = "direct"
            r_free_flags = f_obs.generate_r_free_flags(fraction=0.1)
            fmodel = mmtbx.f_model.manager(
                xray_structure=x,
                f_obs=f_obs,
                r_free_flags=r_free_flags,
                sf_and_grads_accuracy_params=sfg_params)
            if (q == 0):
                assert approx_equal(fmodel.r_work(), 0)
            else:
                assert fmodel.r_work() > 0.05, fmodel.r_work()
            params = bss.master_params.extract()
            params.bulk_solvent = False
            params.anisotropic_scaling = False
            o = fmodel.update_all_scales(fast=False, params=params)
            assert approx_equal(o.k_sol[0], 0)
            assert approx_equal(o.b_sol[0], 0)
            assert approx_equal(o.b_cart, [0, 0, 0, 0, 0, 0])
            assert approx_equal(o.k_h, q)
            assert approx_equal(fmodel.r_work(), 0)
Esempio n. 25
0
def filter_da(fmodels, selection, da_sel_refinable, params):
  xrs = fmodels.fmodel_xray().xray_structure
  xrs_d = xrs.select(selection)
  xrs_m = xrs.select(~selection)
  #
  occ = xrs_d.scatterers().extract_occupancies()
  adp = xrs_d.extract_u_iso_or_u_equiv()*adptbx.u_as_b(1.)
  sel  = occ < params.filter.occupancy_max
  sel &= occ > params.filter.occupancy_min
  sel &= adp < params.filter.b_iso_max
  sel &= adp > params.filter.b_iso_min
  uc = xrs_d.unit_cell()
  sphere_defined = True
  if(len(params.sphere)==0): sphere_defined = False
  for sphere in params.sphere:
    if([sphere.center, sphere.radius].count(None) != 0): sphere_defined = False
  for i_seq, site_frac_d in enumerate(xrs_d.sites_frac()):
    if(sphere_defined):
      inside = False
      for sphere in params.sphere:
        if([sphere.center, sphere.radius].count(None)==0):
          dist = uc.distance(site_frac_d, uc.fractionalize(sphere.center))
          if(dist <= sphere.radius*params.filter.inside_sphere_scale):
            inside = True
            break
      if(not inside): sel[i_seq] = False
    for site_frac_m in xrs_m.sites_frac():
      dist = uc.distance(site_frac_d, site_frac_m)
      if(dist < params.filter.da_model_min_dist):
        sel[i_seq] = False
        break
  #
  sel_all = flex.bool(xrs_m.scatterers().size(),True)
  sel_all.extend(sel)
  da_sel_refinable = da_sel_refinable.select(sel_all)
  #
  xrs_d = xrs_d.select(sel)
  xrs = xrs_m.concatenate(xrs_d)
  selection = flex.bool(xrs_m.scatterers().size(), False)
  selection.extend(flex.bool(xrs_d.scatterers().size(), True))
  fmodels.update_xray_structure(xray_structure = xrs, update_f_calc=True,
    update_f_mask=False)
  assert selection.size() == da_sel_refinable.size()
  #XXXX
  da_sel_refinable = selection
  #XXXX
  return fmodels, selection, da_sel_refinable
Esempio n. 26
0
def filter_da(fmodels, selection, da_sel_refinable, params):
  xrs = fmodels.fmodel_xray().xray_structure
  xrs_d = xrs.select(selection)
  xrs_m = xrs.select(~selection)
  #
  occ = xrs_d.scatterers().extract_occupancies()
  adp = xrs_d.extract_u_iso_or_u_equiv()*adptbx.u_as_b(1.)
  sel  = occ < params.filter.occupancy_max
  sel &= occ > params.filter.occupancy_min
  sel &= adp < params.filter.b_iso_max
  sel &= adp > params.filter.b_iso_min
  uc = xrs_d.unit_cell()
  sphere_defined = True
  if(len(params.sphere)==0): sphere_defined = False
  for sphere in params.sphere:
    if([sphere.center, sphere.radius].count(None) != 0): sphere_defined = False
  for i_seq, site_frac_d in enumerate(xrs_d.sites_frac()):
    if(sphere_defined):
      inside = False
      for sphere in params.sphere:
        if([sphere.center, sphere.radius].count(None)==0):
          dist = uc.distance(site_frac_d, uc.fractionalize(sphere.center))
          if(dist <= sphere.radius*params.filter.inside_sphere_scale):
            inside = True
            break
      if(not inside): sel[i_seq] = False
    for site_frac_m in xrs_m.sites_frac():
      dist = uc.distance(site_frac_d, site_frac_m)
      if(dist < params.filter.da_model_min_dist):
        sel[i_seq] = False
        break
  #
  sel_all = flex.bool(xrs_m.scatterers().size(),True)
  sel_all.extend(sel)
  da_sel_refinable = da_sel_refinable.select(sel_all)
  #
  xrs_d = xrs_d.select(sel)
  xrs = xrs_m.concatenate(xrs_d)
  selection = flex.bool(xrs_m.scatterers().size(), False)
  selection.extend(flex.bool(xrs_d.scatterers().size(), True))
  fmodels.update_xray_structure(xray_structure = xrs, update_f_calc=True,
    update_f_mask=False)
  assert selection.size() == da_sel_refinable.size()
  #XXXX
  da_sel_refinable = selection
  #XXXX
  return fmodels, selection, da_sel_refinable
Esempio n. 27
0
def quality_factor_from_any(d_min=None,
                            grid_resolution_factor=None,
                            quality_factor=None,
                            u_base=None,
                            b_base=None):
  assert [quality_factor, u_base, b_base].count(None) >= 2
  if (u_base is not None):
    b_base = adptbx.u_as_b(u_base)
  if (b_base is not None):
    assert [d_min, grid_resolution_factor].count(None) == 0
    assert d_min > 0
    sigma = 1 / (2. * grid_resolution_factor)
    log_quality_factor = b_base * sigma * (sigma - 1) / (d_min * d_min)
    quality_factor = 10**log_quality_factor
  elif (quality_factor is None):
    quality_factor = 100
  return quality_factor
Esempio n. 28
0
def structure_from_inp(inp, status, special_position_settings):
    wyckoff_table = special_position_settings.space_group_info().wyckoff_table()
    print "</pre><table border=2 cellpadding=2>"
    status.in_table = True
    print "<tr>"
    print "<th>Label"
    print "<th>Scattering<br>factor<br>label"
    print "<th>Multiplicty"
    print "<th>Wyckoff<br>position"
    print "<th>Site<br>symmetry"
    print "<th colspan=3>Fractional coordinates"
    print "<th>Occupancy<br>factor"
    print "<th>Biso"
    print "<tr>"
    structure = xray.structure(special_position_settings)
    print
    for line in inp.coordinates:
        scatterer = read_scatterer(line.split())
        if inp.coor_type != "Fractional":
            scatterer.site = structure.unit_cell().fractionalize(scatterer.site)
        structure.add_scatterer(scatterer)
        site_symmetry = structure.site_symmetry(scatterer.site)
        wyckoff_mapping = wyckoff_table.mapping(site_symmetry)
        wyckoff_position = wyckoff_mapping.position()
        print "<tr>"
        print (
            "<td>%s<td>%s"
            + "<td align=center>%d<td align=center>%s<td align=center>%s"
            + "<td><tt>%.6g</tt><td><tt>%.6g</tt><td><tt>%.6g</tt>"
            + "<td align=center><tt>%.6g</tt>"
            + "<td align=center><tt>%.6g</tt>"
        ) % (
            (
                scatterer.label,
                scatterer.scattering_type,
                wyckoff_position.multiplicity(),
                wyckoff_position.letter(),
                site_symmetry.point_group_type(),
            )
            + scatterer.site
            + (scatterer.occupancy, adptbx.u_as_b(scatterer.u_iso))
        )
    print "</table><pre>"
    status.in_table = False
    print
    return structure
Esempio n. 29
0
def quality_factor_from_any(d_min=None,
                            grid_resolution_factor=None,
                            quality_factor=None,
                            u_base=None,
                            b_base=None):
    assert [quality_factor, u_base, b_base].count(None) >= 2
    if (u_base is not None):
        b_base = adptbx.u_as_b(u_base)
    if (b_base is not None):
        assert [d_min, grid_resolution_factor].count(None) == 0
        assert d_min > 0
        sigma = 1 / (2. * grid_resolution_factor)
        log_quality_factor = b_base * sigma * (sigma - 1) / (d_min * d_min)
        quality_factor = 10**log_quality_factor
    elif (quality_factor is None):
        quality_factor = 100
    return quality_factor
Esempio n. 30
0
def exercise_4_f_hydrogens():
  for d_min in [1,2,3]:
    for q in [0, 0.9, 1]:
      random.seed(0)
      flex.set_random_seed(0)
      x = random_structure.xray_structure(
        space_group_info       = sgtbx.space_group_info("P 4"),
        elements               =(("O","N","C")*5 + ("H",)*95),
        volume_per_atom        = 200,
        min_distance           = 1.5,
        general_positions_only = True,
        random_u_iso           = True,
        random_occupancy       = False)
      hd_sel = x.hd_selection()
      b_isos = x.select(~hd_sel).extract_u_iso_or_u_equiv()*adptbx.u_as_b(1.)
      mmm = b_isos.min_max_mean().as_tuple()
      b_mean = int(mmm[2])
      x = x.set_b_iso(value=b_mean, selection = hd_sel)
      x.scattering_type_registry(table="wk1995")
      x.set_occupancies(value=q, selection = hd_sel)
      fc = x.structure_factors(d_min = d_min, algorithm="direct").f_calc()
      f_obs = abs(fc)
      x = x.deep_copy_scatterers()
      x.set_occupancies(value=0.0, selection = x.hd_selection())
      sfg_params = mmtbx.f_model.sf_and_grads_accuracy_master_params.extract()
      sfg_params.algorithm = "direct"
      r_free_flags = f_obs.generate_r_free_flags(fraction = 0.1)
      fmodel = mmtbx.f_model.manager(
        xray_structure = x,
        f_obs          = f_obs,
        r_free_flags   = r_free_flags,
        sf_and_grads_accuracy_params = sfg_params)
      if(q==0):
        assert approx_equal(fmodel.r_work(), 0)
      else:
        assert fmodel.r_work() > 0.05, fmodel.r_work()
      params = bss.master_params.extract()
      params.bulk_solvent=False
      params.anisotropic_scaling=False
      o = fmodel.update_all_scales(fast=False, params=params)
      assert approx_equal(o.k_sol[0],0)
      assert approx_equal(o.b_sol[0],0)
      assert approx_equal(o.b_cart,[0,0,0,0,0,0])
      assert approx_equal(o.k_h, q)
      assert approx_equal(fmodel.r_work(), 0)
Esempio n. 31
0
 def _b_cart_minimizer_helper(self, n_macro_cycles=1):
     r_start = self.fmodel_kbu.r_factor()
     b_start = self.fmodel_kbu.b_cart()
     for u_cycle in range(n_macro_cycles):
         u_min = k_sol_b_sol_b_cart_minimizer(
             fmodel_kbu=self.fmodel_kbu,
             params=self.params,
             refine_u_star=True).kbu.u_star()
         b_cart = adptbx.u_as_b(
             adptbx.u_star_as_u_cart(self.fmodel_kbu.f_obs.unit_cell(),
                                     u_min))
         self.fmodel_kbu.update(b_cart=b_cart)
     r_final = self.fmodel_kbu.r_factor()
     if (r_final >= r_start):
         self.fmodel_kbu.update(b_cart=b_start)
         return b_start
     else:
         return b_cart
Esempio n. 32
0
 def refine(self):
   b_isos = self.xray_structure.extract_u_iso_or_u_equiv()*adptbx.u_as_b(1.)
   if(self.nproc==1):
     for sel in self.chain_selections:
       b_isos_refined = self.refine_box_with_selected(selection=sel)
       b_isos = b_isos.set_selected(sel, b_isos_refined)
   else:
     stdout_and_results = easy_mp.pool_map(
       processes    = self.nproc,
       fixed_func   = self.refine_box_with_selected,
       args         = self.chain_selections,
       func_wrapper = "buffer_stdout_stderr")
     for i, it in enumerate(stdout_and_results):
       so, b_isos_refined = it
       b_isos = b_isos.set_selected(self.chain_selections[i], b_isos_refined)
       print >> self.log, so
   self.xray_structure = self.xray_structure.set_b_iso(values = b_isos)
   self.pdb_hierarchy.adopt_xray_structure(self.xray_structure)
Esempio n. 33
0
def loop_2(params, xray_structure, pdb_hierarchy, restraints_manager, root):
    print("model:")
    amp = params.f_obs.f_calc.atomic_model
    grm = restraints_manager
    xrs = xray_structure.deep_copy_scatterers()
    show(xrs=xrs, xrs_start=xrs, grm=grm, prefix="start:")
    xrs_sh = xrs.deep_copy_scatterers()
    if (amp.shake_sites_rmsd is not None):
        xrs_sh.shake_sites_in_place(rms_difference=amp.shake_sites_rmsd)
    if (amp.apply_cartesian_dynamics):
        cd(xray_structure=xrs_sh, restraints_manager=grm, params=amp)
        show(xrs=xrs_sh, xrs_start=xrs, grm=grm, prefix="cd:   ")
    if ([
            amp.regularize_geometry.rmsd_bonds_target,
            amp.regularize_geometry.rmsd_angles_target
    ].count(None) == 0):
        xrs_sh = regularize_geometry(xray_structure=xrs_sh,
                                     restraints_manager=grm,
                                     params=amp.regularize_geometry)
        show(xrs=xrs_sh, xrs_start=xrs, grm=grm, prefix="min:  ")
    if (amp.ladp_angle is not None):
        xrs_sh = set_ladp(xray_structure=xrs_sh,
                          pdb_hierarchy=pdb_hierarchy,
                          angle=amp.ladp_angle)
    if ([amp.tls.max_tl, amp.tls.min_tl].count(None) == 0):
        xrs_sh = apply_tls(xray_structure=xrs_sh, params=amp.tls)
    if ([
            amp.rigid_body_shift.rotation_angle,
            amp.rigid_body_shift.translation_length
    ].count(None) == 0):
        xrs_sh = apply_rigid_body_shift(xray_structure=xrs_sh,
                                        params=amp.rigid_body_shift)
        show(xrs=xrs_sh, xrs_start=xrs, grm=grm, prefix="rb:   ")
    #
    h = pdb_hierarchy.deep_copy()
    h.atoms().reset_i_seq()  # XXX
    h.atoms().set_xyz(xrs_sh.sites_cart().deep_copy())
    h.atoms().set_uij(xrs_sh.scatterers().extract_u_cart(xrs_sh.unit_cell()))
    h.atoms().set_b(xrs_sh.extract_u_iso_or_u_equiv() * adptbx.u_as_b(1.))
    m = h.models()[0].detached_copy()
    m.id = str(None)
    root.append_model(m)
Esempio n. 34
0
 def refine(self):
     b_isos = self.xray_structure.extract_u_iso_or_u_equiv(
     ) * adptbx.u_as_b(1.)
     if (self.nproc == 1):
         for sel in self.chain_selections:
             b_isos_refined = self.refine_box_with_selected(selection=sel)
             b_isos = b_isos.set_selected(sel, b_isos_refined)
     else:
         stdout_and_results = easy_mp.pool_map(
             processes=self.nproc,
             fixed_func=self.refine_box_with_selected,
             args=self.chain_selections,
             func_wrapper="buffer_stdout_stderr")
         for i, it in enumerate(stdout_and_results):
             so, b_isos_refined = it
             b_isos = b_isos.set_selected(self.chain_selections[i],
                                          b_isos_refined)
             print >> self.log, so
     self.xray_structure = self.xray_structure.set_b_iso(values=b_isos)
     self.pdb_hierarchy.adopt_xray_structure(self.xray_structure)
Esempio n. 35
0
def write_scatterer(s, running_index, scatterer,
                       action=None, segid=None, group=None):
  if (action is None): action = "refine"
  if (segid is None): segid = "SITE"
  if (group is None): group = ""
  assert running_index > 0
  assert scatterer.flags.use_u_iso_only()
  assert action in ("refine", "fix", "ignore")
  i = running_index
  print >> s, """\
{+ choice: "refine" "fix" "ignore" +}
{===>} site.action_%d="%s";
{===>} site.segid_%d="%s"; site.type_%d="%s";
{===>} site.x_%d=%.6g; site.y_%d=%.6g; site.z_%d=%.6g;
{===>} site.b_%d=%.6g; site.q_%d=%.6g; site.g_%d="%s";
""" % (i,action,
       i,segid, i,scatterer.scattering_type,
       i,scatterer.site[0],
       i,scatterer.site[1],
       i,scatterer.site[2],
       i,adptbx.u_as_b(scatterer.u_iso), i,scatterer.occupancy, i,group)
Esempio n. 36
0
def structure_from_inp(inp, status, special_position_settings):
    wyckoff_table = special_position_settings.space_group_info().wyckoff_table(
    )
    print "</pre><table border=2 cellpadding=2>"
    status.in_table = True
    print "<tr>"
    print "<th>Label"
    print "<th>Scattering<br>factor<br>label"
    print "<th>Multiplicty"
    print "<th>Wyckoff<br>position"
    print "<th>Site<br>symmetry"
    print "<th colspan=3>Fractional coordinates"
    print "<th>Occupancy<br>factor"
    print "<th>Biso"
    print "<tr>"
    structure = xray.structure(special_position_settings)
    print
    for line in inp.coordinates:
        scatterer = read_scatterer(line.split())
        if (inp.coor_type != "Fractional"):
            scatterer.site = structure.unit_cell().fractionalize(
                scatterer.site)
        structure.add_scatterer(scatterer)
        site_symmetry = structure.site_symmetry(scatterer.site)
        wyckoff_mapping = wyckoff_table.mapping(site_symmetry)
        wyckoff_position = wyckoff_mapping.position()
        print "<tr>"
        print("<td>%s<td>%s" +
              "<td align=center>%d<td align=center>%s<td align=center>%s" +
              "<td><tt>%.6g</tt><td><tt>%.6g</tt><td><tt>%.6g</tt>" +
              "<td align=center><tt>%.6g</tt>" +
              "<td align=center><tt>%.6g</tt>") % (
                  (scatterer.label, scatterer.scattering_type,
                   wyckoff_position.multiplicity(), wyckoff_position.letter(),
                   site_symmetry.point_group_type()) + scatterer.site +
                  (scatterer.occupancy, adptbx.u_as_b(scatterer.u_iso)))
    print "</table><pre>"
    status.in_table = False
    print
    return structure
Esempio n. 37
0
def write_scatterer(s,
                    running_index,
                    scatterer,
                    action=None,
                    segid=None,
                    group=None):
    if (action is None): action = "refine"
    if (segid is None): segid = "SITE"
    if (group is None): group = ""
    assert running_index > 0
    assert scatterer.flags.use_u_iso_only()
    assert action in ("refine", "fix", "ignore")
    i = running_index
    print >> s, """\
{+ choice: "refine" "fix" "ignore" +}
{===>} site.action_%d="%s";
{===>} site.segid_%d="%s"; site.type_%d="%s";
{===>} site.x_%d=%.6g; site.y_%d=%.6g; site.z_%d=%.6g;
{===>} site.b_%d=%.6g; site.q_%d=%.6g; site.g_%d="%s";
""" % (i, action, i, segid, i, scatterer.scattering_type, i, scatterer.site[0],
       i, scatterer.site[1], i, scatterer.site[2], i,
       adptbx.u_as_b(scatterer.u_iso), i, scatterer.occupancy, i, group)
Esempio n. 38
0
    def get_adps(self):
        #print('Extracting ADPs', file=self.log)
        if self._adps is None:
            b_isos = self._xrs.extract_u_iso_or_u_equiv() * adptbx.u_as_b(1.)
            n_iso = self._xrs.use_u_iso().count(True)
            n_aniso = self._xrs.use_u_aniso().count(True)
            n_zero = (b_isos < 0.01).count(True)
            # TODO: what number as cutoff?
            n_above_100 = (b_isos > 100).count(True)
            isel_above_100 = (b_isos > 100).iselection()
            b_min, b_max, b_mean = b_isos.min_max_mean().as_tuple()
            # TODO: Get adp from surrounding residues

            self._adps = group_args(n_iso=n_iso,
                                    n_aniso=n_aniso,
                                    n_zero=n_zero,
                                    n_above_100=n_above_100,
                                    isel_above_100=isel_above_100,
                                    b_min=b_min,
                                    b_max=b_max,
                                    b_mean=b_mean)
        return self._adps
Esempio n. 39
0
def get_atom_radius(xray_structure=None, d_min=None, map_data=None,
                    crystal_symmetry=None, radius=None):
  if(radius is not None): return radius
  radii = []
  if(d_min is not None):
    radii.append(d_min)
  if([xray_structure, crystal_symmetry].count(None)==0):
    assert crystal_symmetry.is_similar_symmetry(
      xray_structure.crystal_symmetry())
  if([map_data, crystal_symmetry].count(None)==0):
    d99 = maptbx.d99(
      map              = map_data,
      crystal_symmetry = crystal_symmetry).result.d99
    radii.append(d99)
  if(xray_structure is not None and d_min is not None):
    b_iso = adptbx.u_as_b(
      flex.mean(xray_structure.extract_u_iso_or_u_equiv()))
    o = maptbx.atom_curves(scattering_type="C", scattering_table="electron")
    rad_image = o.image(d_min=d_min, b_iso=b_iso,
      radius_max=max(15.,d_min), radius_step=0.01).radius
    radii.append(rad_image)
  return max(3, min(10, max(radii)))
Esempio n. 40
0
    def refine_box_with_selected(self, selection=None):
        if (selection is None):
            selections = self.xray_structure.all_selection()
        ph_box = self.pdb_hierarchy.select(selection)
        ph_box.atoms().reset_i_seq()
        box = mmtbx.utils.extract_box_around_model_and_map(
            xray_structure=self.xray_structure.select(selection),
            map_data=self.target_map.map_data,
            box_cushion=self.atom_radius,
            mask_atoms=True,
            mask_atoms_atom_radius=self.atom_radius)
        ph_box.adopt_xray_structure(box.xray_structure_box)
        group_adp_sel = []
        for rg in ph_box.residue_groups():
            group_adp_sel.append(rg.atoms().extract_i_seq())
        f_obs_box = abs(box.box_map_coefficients(d_min=self.target_map.d_min))
        fmodel = mmtbx.f_model.manager(f_obs=f_obs_box,
                                       xray_structure=box.xray_structure_box)

        #XXX
        fmodel.update_all_scales(update_f_part1=False, apply_back_trace=True)
        #XXX

        fmodel.update(target_name="ls_wunit_k1")
        if (self.nproc > 1): log = None
        else: log = self.log
        group_b_manager = mmtbx.refinement.group.manager(
            fmodel=fmodel,
            selections=group_adp_sel,
            convergence_test=False,
            max_number_of_iterations=50,
            number_of_macro_cycles=3,
            run_finite_differences_test=False,
            use_restraints=self.use_adp_restraints,
            refine_adp=True,
            log=log)
        return fmodel.xray_structure.extract_u_iso_or_u_equiv(
        ) * adptbx.u_as_b(1.)
Esempio n. 41
0
def show_refinement_update(fmodels, selection, da_sel_refinable, prefix):
  fmt1 = "%s Rwork= %8.6f Rfree= %8.6f Number of: non-DA= %d DA= %d all= %d"
  print(fmt1%(prefix, fmodels.fmodel_xray().r_work(),
    fmodels.fmodel_xray().r_free(),
    selection.count(False),selection.count(True),
    fmodels.fmodel_xray().xray_structure.scatterers().size()))
  occ = fmodels.fmodel_xray().xray_structure.scatterers().extract_occupancies()
  occ_da = occ.select(selection)
  if(occ_da.size()>0):
    occ_ma = occ.select(~selection)
    print("         non-da: occ(min,max,mean)= %6.3f %6.3f %6.3f"%(
      flex.min(occ_ma),flex.max(occ_ma),flex.mean(occ_ma)))
    print("             da: occ(min,max,mean)= %6.3f %6.3f %6.3f"%(
      flex.min(occ_da),flex.max(occ_da),flex.mean(occ_da)))
    b = fmodels.fmodel_xray().xray_structure.extract_u_iso_or_u_equiv()*\
      adptbx.u_as_b(1.)
    b_da = b.select(selection)
    b_ma = b.select(~selection)
    print("         non-da: ADP(min,max,mean)= %7.2f %7.2f %7.2f"%(
      flex.min(b_ma),flex.max(b_ma),flex.mean(b_ma)))
    print("             da: ADP(min,max,mean)= %7.2f %7.2f %7.2f"%(
      flex.min(b_da),flex.max(b_da),flex.mean(b_da)))
    print("da_sel_refinable:", da_sel_refinable.size(), da_sel_refinable.count(True))
Esempio n. 42
0
def show_refinement_update(fmodels, selection, da_sel_refinable, prefix):
  fmt1 = "%s Rwork= %8.6f Rfree= %8.6f Number of: non-DA= %d DA= %d all= %d"
  print fmt1%(prefix, fmodels.fmodel_xray().r_work(),
    fmodels.fmodel_xray().r_free(),
    selection.count(False),selection.count(True),
    fmodels.fmodel_xray().xray_structure.scatterers().size())
  occ = fmodels.fmodel_xray().xray_structure.scatterers().extract_occupancies()
  occ_da = occ.select(selection)
  if(occ_da.size()>0):
    occ_ma = occ.select(~selection)
    print "         non-da: occ(min,max,mean)= %6.3f %6.3f %6.3f"%(
      flex.min(occ_ma),flex.max(occ_ma),flex.mean(occ_ma))
    print "             da: occ(min,max,mean)= %6.3f %6.3f %6.3f"%(
      flex.min(occ_da),flex.max(occ_da),flex.mean(occ_da))
    b = fmodels.fmodel_xray().xray_structure.extract_u_iso_or_u_equiv()*\
      adptbx.u_as_b(1.)
    b_da = b.select(selection)
    b_ma = b.select(~selection)
    print "         non-da: ADP(min,max,mean)= %7.2f %7.2f %7.2f"%(
      flex.min(b_ma),flex.max(b_ma),flex.mean(b_ma))
    print "             da: ADP(min,max,mean)= %7.2f %7.2f %7.2f"%(
      flex.min(b_da),flex.max(b_da),flex.mean(b_da))
    print "da_sel_refinable:", da_sel_refinable.size(), da_sel_refinable.count(True)
Esempio n. 43
0
  def refine_box_with_selected(self, selection=None):
    if(selection is None): selections = self.xray_structure.all_selection()
    ph_box = self.pdb_hierarchy.select(selection)
    ph_box.atoms().reset_i_seq()
    box = mmtbx.utils.extract_box_around_model_and_map(
      xray_structure         = self.xray_structure.select(selection),
      map_data               = self.target_map.map_data,
      box_cushion            = self.atom_radius,
      mask_atoms             = True,
      mask_atoms_atom_radius = self.atom_radius)
    ph_box.adopt_xray_structure(box.xray_structure_box)
    group_adp_sel = []
    for rg in ph_box.residue_groups():
      group_adp_sel.append(rg.atoms().extract_i_seq())
    f_obs_box = abs(box.box_map_coefficients(d_min = self.target_map.d_min))
    fmodel = mmtbx.f_model.manager(
      f_obs          = f_obs_box,
      xray_structure = box.xray_structure_box)

    #XXX
    fmodel.update_all_scales(update_f_part1=False, apply_back_trace=True)
    #XXX

    fmodel.update(target_name="ls_wunit_k1")
    if(self.nproc>1): log = None
    else:             log = self.log
    group_b_manager = mmtbx.refinement.group.manager(
      fmodel                   = fmodel,
      selections               = group_adp_sel,
      convergence_test         = False,
      max_number_of_iterations = 50,
      number_of_macro_cycles   = 3,
      run_finite_differences_test = False,
      use_restraints           = self.use_adp_restraints,
      refine_adp               = True,
      log                      = log)
    return fmodel.xray_structure.extract_u_iso_or_u_equiv()*adptbx.u_as_b(1.)
Esempio n. 44
0
def coordinates(scatterers, xyz_only=False):
  cns_input = []
  a = cns_input.append
  resid = 1
  for scatterer in scatterers:
    x = scatterer.site
    q = scatterer.occupancy
    assert scatterer.flags.use_u_iso_only()
    b = adptbx.u_as_b(scatterer.u_iso)
    gaussian = eltbx.xray_scattering.it1992(scatterer.scattering_type).fetch()
    fp = scatterer.fp
    fdp = scatterer.fdp
    for i in range(3):
      a("do (%s=%.12g) (resid=%d)" % ("xyz"[i], x[i], resid))
    if (not xyz_only):
      a("do (q=%.12g) (resid=%d)" % (q, resid))
      a("do (b=%.12g) (resid=%d)" % (b, resid))
      a("xray")
      a("  scatter (chemical=%s)" % scatterer.label.replace(" ","")[-4:])
      for i in range(4):
        a("    %.6g %.6g" %
          (gaussian.array_of_a()[i], gaussian.array_of_b()[i]))
      a("    %.6g" % (gaussian.c(),))
      a("end")
      a("do (scatter_fp=%.12g) (resid=%d)" % (fp, resid))
      a("do (scatter_fdp=%.12g) (resid=%d)" % (fdp, resid))
    a("")
    resid += 1
  a("coordinates orthogonalize end")
  a("show (name) (all)")
  a("show (chemical) (all)")
  if (not xyz_only):
    a("show (scatter_fp) (all)")
    a("show (scatter_fdp) (all)")
  a("")
  return cns_input
Esempio n. 45
0
 def run_test(self):
   # Refinement
   params = mmtbx.f_model.sf_and_grads_accuracy_master_params.extract()
   params.algorithm = "direct"
   # Get the xray_structure of the shaken ASU
   xrs_shaken_asu, dummy, transform_info, ph, ph2 = step_1(
     file_name="one_ncs_in_asu_shaken.pdb",
     crystal_symmetry=self.xrs_one_ncs.crystal_symmetry(),
     write_name='asu_shaken.pdb')
   tr_obj = iotbx.ncs.input(
     hierarchy = ph,
     transform_info = transform_info,
     exclude_selection=None)
   self.ncs_restraints_group_list = tr_obj.get_ncs_restraints_group_list()
   # refine both ncs related and not related atoms
   self.refine_selection = flex.size_t(range(tr_obj.total_asu_length))
   self.extended_ncs_selection = nu.get_extended_ncs_selection(
     ncs_restraints_group_list=tr_obj.get_ncs_restraints_group_list(),
     refine_selection=self.refine_selection)
   assert self.refine_selection.size() == 21, self.refine_selection.size()
   self.fmodel = mmtbx.f_model.manager(
     f_obs                        = self.f_obs,
     r_free_flags                 = self.r_free_flags,
     xray_structure               = xrs_shaken_asu,
     sf_and_grads_accuracy_params = params,
     target_name                  = "ls_wunit_k1")
   r_start = self.fmodel.r_work()
   assert r_start > 0.1, r_start
   print "start r_factor: %6.4f" % r_start
   for macro_cycle in xrange(self.n_macro_cycle):
     data_weight = None
     if(self.use_geometry_restraints):
       self.transformations=False
       data_weight = nu.get_weight(minimized_obj=self)
     target_and_grads_object = mmtbx.refinement.minimization_ncs_constraints.\
       target_function_and_grads_reciprocal_space(
         fmodel                 = self.fmodel,
         ncs_restraints_group_list = self.ncs_restraints_group_list,
         refine_selection     = self.refine_selection,
         restraints_manager     = self.grm,
         data_weight            = data_weight,
         refine_sites           = self.sites,
         refine_u_iso           = self.u_iso,
         iso_restraints         = self.iso_restraints)
     minimized = mmtbx.refinement.minimization_ncs_constraints.lbfgs(
       target_and_grads_object      = target_and_grads_object,
       xray_structure               = self.fmodel.xray_structure,
       ncs_restraints_group_list    = self.ncs_restraints_group_list,
       refine_selection             = self.refine_selection,
       finite_grad_differences_test = self.finite_grad_differences_test,
       max_iterations               = 100,
       refine_sites                 = self.sites,
       refine_u_iso                 = self.u_iso)
     refine_type = 'adp'*self.u_iso + 'sites'*self.sites
     outstr = "  macro_cycle {0:3} ({1})   r_factor: {2:6.4f}   " + \
           self.finite_grad_differences_test * \
           "finite_grad_difference_val: {3:.4f}"
     print outstr.format(
       macro_cycle, refine_type,self.fmodel.r_work(),
       minimized.finite_grad_difference_val)
     assert (minimized.finite_grad_difference_val < 1.0e-3)
     assert approx_equal(self.fmodel.r_work(), target_and_grads_object.fmodel.r_work())
     # break test if r_work is very small
     if target_and_grads_object.fmodel.r_work() < 1.0e-6: break
   # check results
   if(self.u_iso):
     assert approx_equal(self.fmodel.r_work(), 0, 1.e-5)
   elif(self.sites):
     if(self.use_geometry_restraints):
       assert approx_equal(self.fmodel.r_work(), 0, 0.00018)
     else:
       assert approx_equal(self.fmodel.r_work(), 0, 3.e-4)
   else: assert 0
   # output refined model
   xrs_refined = self.fmodel.xray_structure
   output_file_name = "refined_u_iso%s_sites%s.pdb"%(str(self.u_iso),
     str(self.sites))
   assert ph2.atoms().size() == self.fmodel.xray_structure.scatterers().size()
   ph2.atoms().set_xyz(self.fmodel.xray_structure.sites_cart())
   ph2.atoms().set_b(
     self.fmodel.xray_structure.extract_u_iso_or_u_equiv()*adptbx.u_as_b(1.))
   ph2.write_pdb_file(file_name = output_file_name,
     crystal_symmetry=self.fmodel.xray_structure.crystal_symmetry())
   self.test_files_names.append(output_file_name)
   # check final model
   if(not self.use_geometry_restraints):
     # XXX fix later for case self.use_geometry_restraints=True
     pdb_inp_answer = iotbx.pdb.input(source_info=None, lines=ncs_1_copy)
     pdb_inp_refined = iotbx.pdb.input(file_name=output_file_name)
     xrs1 = pdb_inp_answer.xray_structure_simple(
       crystal_symmetry=self.fmodel.xray_structure.crystal_symmetry())
     xrs2 = pdb_inp_refined.xray_structure_simple().select(
       minimized.extended_ncs_selection)
     print xrs1.crystal_symmetry().unit_cell().parameters()
     print xrs2.crystal_symmetry().unit_cell().parameters()
     mmtbx.utils.assert_xray_structures_equal(
       x1 = xrs1,
       x2 = xrs2,
       sites = False)
     delta = flex.vec3_double([xrs1.center_of_mass()]*xrs2.scatterers().size())-\
             flex.vec3_double([xrs2.center_of_mass()]*xrs2.scatterers().size())
     xrs2.set_sites_cart(sites_cart = xrs2.sites_cart()+delta)
     mmtbx.utils.assert_xray_structures_equal(
       x1 = xrs1,
       x2 = xrs2,
       eps=1.e-4)
Esempio n. 46
0
    def __init__(self,
                 pdb_hierarchy,
                 xray_structure,
                 fmodel,
                 distance_cutoff=4.0,
                 collect_all=True,
                 molprobity_map_params=None):
        validation.__init__(self)
        from mmtbx.real_space_correlation import extract_map_stats_for_single_atoms
        from cctbx import adptbx
        from scitbx.matrix import col
        self.n_bad = 0
        self.n_heavy = 0
        pdb_atoms = pdb_hierarchy.atoms()
        if (len(pdb_atoms) > 1):
            assert (not pdb_atoms.extract_i_seq().all_eq(0))
        unit_cell = xray_structure.unit_cell()
        pair_asu_table = xray_structure.pair_asu_table(
            distance_cutoff=distance_cutoff)
        asu_mappings = pair_asu_table.asu_mappings()
        asu_table = pair_asu_table.table()
        u_isos = xray_structure.extract_u_iso_or_u_equiv()
        occupancies = xray_structure.scatterers().extract_occupancies()
        sites_frac = xray_structure.sites_frac()
        sel_cache = pdb_hierarchy.atom_selection_cache()
        water_sel = sel_cache.selection("water")

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

            # read two_fofc_map
            if (molprobity_map_params.map_file_name is not None):
                f = any_file(molprobity_map_params.map_file_name)
                two_fofc_map = f.file_object.map_data()
                d_min = molprobity_map_params.d_min
                crystal_gridding = maptbx.crystal_gridding(
                    f.file_object.unit_cell(),
                    space_group_info=space_group_info(
                        f.file_object.space_group_number),
                    pre_determined_n_real=f.file_object.unit_cell_grid)

                pdb_atoms = pdb_hierarchy.atoms()
                xray_structure = pdb_hierarchy.extract_xray_structure(
                    crystal_symmetry=f.crystal_symmetry())
                unit_cell = xray_structure.unit_cell()
                # check for origin shift
                # ---------------------------------------------------------------------
                soin = maptbx.shift_origin_if_needed(
                    map_data=two_fofc_map,
                    sites_cart=xray_structure.sites_cart(),
                    crystal_symmetry=xray_structure.crystal_symmetry())
                two_fofc_map = soin.map_data
                xray_structure.set_sites_cart(soin.sites_cart)
                # ---------------------------------------------------------------------
                pair_asu_table = xray_structure.pair_asu_table(
                    distance_cutoff=distance_cutoff)
                asu_mappings = pair_asu_table.asu_mappings()
                asu_table = pair_asu_table.table()
                u_isos = xray_structure.extract_u_iso_or_u_equiv()
                occupancies = xray_structure.scatterers().extract_occupancies()
                sites_frac = xray_structure.sites_frac()
                sel_cache = pdb_hierarchy.atom_selection_cache()
                water_sel = sel_cache.selection("water")

            elif (molprobity_map_params.map_coefficients_file_name
                  is not None):
                f = any_file(molprobity_map_params.map_coefficients_file_name)
                fourier_coefficients = f.file_server.get_miller_array(
                    molprobity_map_params.map_coefficients_label)
                crystal_symmetry = fourier_coefficients.crystal_symmetry()
                d_min = fourier_coefficients.d_min()
                crystal_gridding = maptbx.crystal_gridding(
                    crystal_symmetry.unit_cell(),
                    d_min,
                    resolution_factor=0.25,
                    space_group_info=crystal_symmetry.space_group_info())
                two_fofc_map = miller.fft_map(
                  crystal_gridding=crystal_gridding,
                  fourier_coefficients=fourier_coefficients).apply_sigma_scaling().\
                  real_map_unpadded()

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

            map_stats = extract_map_stats_for_single_atoms(
                pdb_atoms=pdb_atoms,
                xray_structure=xray_structure,
                fmodel=None,
                selection=water_sel,
                fc_map=fc_map,
                two_fofc_map=two_fofc_map)
        else:
            map_stats = extract_map_stats_for_single_atoms(
                pdb_atoms=pdb_atoms,
                xray_structure=xray_structure,
                fmodel=fmodel,
                selection=water_sel)
        waters = []
        for i_seq, atom in enumerate(pdb_atoms):
            if (water_sel[i_seq]):
                rt_mx_i_inv = asu_mappings.get_rt_mx(i_seq, 0).inverse()
                self.n_total += 1
                asu_dict = asu_table[i_seq]
                nearest_atom = nearest_contact = None
                for j_seq, j_sym_groups in asu_dict.items():
                    atom_j = pdb_atoms[j_seq]
                    site_j = sites_frac[j_seq]
                    # Filter out hydrogens
                    if atom_j.element.upper().strip() in ["H", "D"]:
                        continue
                    for j_sym_group in j_sym_groups:
                        rt_mx = rt_mx_i_inv.multiply(
                            asu_mappings.get_rt_mx(j_seq, j_sym_group[0]))
                        site_ji = rt_mx * site_j
                        site_ji_cart = xray_structure.unit_cell(
                        ).orthogonalize(site_ji)
                        vec_i = col(atom.xyz)
                        vec_ji = col(site_ji_cart)
                        dxyz = abs(vec_i - vec_ji)
                        if (nearest_contact is None) or (dxyz <
                                                         nearest_contact):
                            nearest_contact = dxyz
                            nearest_atom = atom_info(pdb_atom=atom_j,
                                                     symop=rt_mx)
                w = water(pdb_atom=atom,
                          b_iso=adptbx.u_as_b(u_isos[i_seq]),
                          occupancy=occupancies[i_seq],
                          nearest_contact=nearest_contact,
                          nearest_atom=nearest_atom,
                          score=map_stats.two_fofc_ccs[i_seq],
                          fmodel=map_stats.fmodel_values[i_seq],
                          two_fofc=map_stats.two_fofc_values[i_seq],
                          fofc=map_stats.fofc_values[i_seq],
                          anom=map_stats.anom_values[i_seq],
                          n_hbonds=None)  # TODO
                if (w.is_bad_water()):
                    w.outlier = True
                    self.n_bad += 1
                elif (w.is_heavy_atom()):
                    w.outlier = True
                    self.n_heavy += 1
                if (w.outlier) or (collect_all):
                    self.results.append(w)
        self.n_outliers = len(self.results)
Esempio n. 47
0
 def residual_contribution(u_isos_current, u_isos_average):
     diff = u_isos_current - u_isos_average
     self.rms_with_respect_to_average.append(
         adptbx.u_as_b(flex.mean_sq(diff)**0.5))
     self.number_of_restraints += diff.size()
Esempio n. 48
0
 def map_coefficients(self,
                      map_type,
                      acentrics_scale = 2.0,
                      centrics_pre_scale = 1.0,
                      exclude_free_r_reflections=False,
                      fill_missing=False,
                      fill_missing_method="f_model",
                      isotropize=True,
                      sharp=False,
                      pdb_hierarchy=None, # XXX required for map_type=llg
                      merge_anomalous=None,
                      use_shelx_weight=False,
                      shelx_weight_parameter=1.5):
   map_name_manager = mmtbx.map_names(map_name_string = map_type)
   # Special case #1: anomalous map
   if(map_name_manager.anomalous):
     if(self.anom_diff is not None):
       # Formula from page 141 in "The Bijvoet-Difference Fourier Synthesis",
       # Jeffrey Roach, METHODS IN ENZYMOLOGY, VOL. 374
       return miller.array(miller_set = self.anom_diff,
                           data       = self.anom_diff.data()/(2j))
     else: return None
   # Special case #2: anomalous residual map
   elif (map_name_manager.anomalous_residual):
     if (self.anom_diff is not None):
       return anomalous_residual_map_coefficients(
         fmodel=self.fmodel,
         exclude_free_r_reflections=exclude_free_r_reflections)
     else : return None
   # Special case #3: Phaser SAD LLG map
   elif (map_name_manager.phaser_sad_llg):
     if (pdb_hierarchy is None):
       raise RuntimeError("pdb_hierarchy must not be None when a Phaser SAD "+
         "LLG map is requested.")
     if (self.anom_diff is not None):
       return get_phaser_sad_llg_map_coefficients(
         fmodel=self.fmodel,
         pdb_hierarchy=pdb_hierarchy)
     else :
       return None
   # Special case #4: Fcalc map
   mnm = mmtbx.map_names(map_name_string = map_type)
   if(mnm.k==0 and abs(mnm.n)==1):
     if(fill_missing):
       return self.fmodel.xray_structure.structure_factors(
         d_min = self.fmodel.f_obs().d_min()).f_calc()
     else:
       return self.fmodel.f_obs().structure_factors_from_scatterers(
         xray_structure = self.fmodel.xray_structure).f_calc()
   #
   if(self.mch is None):
     self.mch = self.fmodel.map_calculation_helper()
   ffs = fo_fc_scales(
     fmodel          = self.fmodel,
     map_type_str    = map_type,
     acentrics_scale = acentrics_scale,
     centrics_scale  = centrics_pre_scale)
   fo_scale, fc_scale = ffs.fo_scale, ffs.fc_scale
   coeffs = combine(
     fmodel                 = self.fmodel,
     map_type_str           = map_type,
     fo_scale               = fo_scale,
     fc_scale               = fc_scale,
     map_calculation_helper = self.mch,
     use_shelx_weight       = use_shelx_weight,
     shelx_weight_parameter = shelx_weight_parameter).map_coefficients()
   r_free_flags = None
   # XXX the default scale array (used for the isotropize option) needs to be
   # calculated and processed now to avoid array size errors
   scale_default = 1. / (self.fmodel.k_isotropic()*self.fmodel.k_anisotropic())
   scale_array = coeffs.customized_copy(data=scale_default)
   if (exclude_free_r_reflections):
     if (coeffs.anomalous_flag()):
       coeffs = coeffs.average_bijvoet_mates()
     r_free_flags = self.fmodel.r_free_flags()
     if (r_free_flags.anomalous_flag()):
       r_free_flags = r_free_flags.average_bijvoet_mates()
       scale_array = scale_array.average_bijvoet_mates()
     coeffs = coeffs.select(~r_free_flags.data())
     scale_array = scale_array.select(~r_free_flags.data())
   scale=None
   if(isotropize):
     if(scale is None):
       if (scale_array.anomalous_flag()) and (not coeffs.anomalous_flag()):
         scale_array = scale_array.average_bijvoet_mates()
       scale = scale_array.data()
     coeffs = coeffs.customized_copy(data = coeffs.data()*scale)
   if(fill_missing):
     if(coeffs.anomalous_flag()):
       coeffs = coeffs.average_bijvoet_mates()
     coeffs = fill_missing_f_obs(
       coeffs = coeffs,
       fmodel = self.fmodel,
       method = fill_missing_method)
   if(sharp):
     ss = 1./flex.pow2(coeffs.d_spacings().data()) / 4.
     from cctbx import adptbx
     b = flex.mean(self.fmodel.xray_structure.extract_u_iso_or_u_equiv() *
       adptbx.u_as_b(1))/2
     k_sharp = 1./flex.exp(-ss * b)
     coeffs = coeffs.customized_copy(data = coeffs.data()*k_sharp)
   if (merge_anomalous) and (coeffs.anomalous_flag()):
     return coeffs.average_bijvoet_mates()
   return coeffs
Esempio n. 49
0
def create_da_xray_structures(xray_structure, params):
    def grid(sphere, gap, overlap):
        c = flex.double(sphere.center)
        x_start, y_start, z_start = c - float(sphere.radius)
        x_end, y_end, z_end = c + float(sphere.radius)
        x_range = frange(c[0], c[0] + gap, overlap)
        y_range = frange(c[1], c[1] + gap, overlap)
        z_range = frange(c[2], c[2] + gap, overlap)
        return group_args(x_range=x_range, y_range=y_range, z_range=z_range)

    grids = []
    for sphere in params.sphere:
        grids.append(
            grid(sphere=sphere,
                 gap=params.atom_gap,
                 overlap=params.overlap_interval))
    initial_b_factor = params.initial_b_factor
    if (initial_b_factor is None):
        initial_b_factor = flex.mean(
            xray_structure.extract_u_iso_or_u_equiv() * adptbx.u_as_b(1.))
    da_xray_structures = []
    counter = 0
    for grid, sphere in zip(grids, params.sphere):
        cntr_g = 0  # XXX
        for x_start in grid.x_range:
            for y_start in grid.y_range:
                for z_start in grid.z_range:
                    cntr_g += 1
                    if (cntr_g > 1): continue  # XXX
                    counter += 1
                    new_center = flex.double([x_start, y_start, z_start])
                    atom_grid = make_grid(center=new_center,
                                          radius=sphere.radius,
                                          gap=params.atom_gap,
                                          occupancy=params.initial_occupancy,
                                          b_factor=initial_b_factor,
                                          atom_name=params.atom_name,
                                          scattering_type=params.atom_type,
                                          resname=params.residue_name)
                    da_xray_structure = pdb_atoms_as_xray_structure(
                        pdb_atoms=atom_grid,
                        crystal_symmetry=xray_structure.crystal_symmetry())
                    closest_distances_result = xray_structure.closest_distances(
                        sites_frac=da_xray_structure.sites_frac(),
                        distance_cutoff=5)
                    selection = closest_distances_result.smallest_distances > 0
                    selection &= closest_distances_result.smallest_distances < 1
                    da_xray_structure = da_xray_structure.select(~selection)
                    #print counter, da_xray_structure.scatterers().size()
                    if (cntr_g == 1):  # XXX
                        da_xray_structures.append(da_xray_structure)
    ###
    result = []
    for i, x1 in enumerate(da_xray_structures):
        for j, x2 in enumerate(da_xray_structures):
            if (x1 is not x2):
                closest_distances_result = x1.closest_distances(
                    sites_frac=x2.sites_frac(), distance_cutoff=5)  # XXX ???
                selection = closest_distances_result.smallest_distances > 0
                selection &= closest_distances_result.smallest_distances < params.atom_gap
                da_xray_structures[j] = x2.select(~selection)
    return da_xray_structures
Esempio n. 50
0
 def update_solvent_and_scale_2(self, fast, params, apply_back_trace,
                                refine_hd_scattering, log):
     if (params is None): params = bss.master_params.extract()
     if (self.xray_structure is not None):
         # Figure out Fcalc and Fmask based on presence of H
         hd_selection = self.xray_structure.hd_selection()
         xrs_no_h = self.xray_structure.select(~hd_selection)
         xrs_h = self.xray_structure.select(hd_selection)
     # Create data container for scalers. If H scattering is refined then it is
     # assumed that self.f_calc() does not contain H contribution at all.
     fmodel_kbu = mmtbx.f_model.manager_kbu(f_obs=self.f_obs(),
                                            f_calc=self.f_calc(),
                                            f_masks=self.f_masks(),
                                            ss=self.ss)
     # Compute k_total and k_mask using one of the two methods (anal or min).
     # Note: this intentionally ignores previously existing f_part1 and f_part2.
     #
     k_sol, b_sol, b_cart, b_adj = [
         None,
     ] * 4
     if (fast):  # analytical
         assert len(fmodel_kbu.f_masks) == 1
         result = mmtbx.bulk_solvent.scaler.run_simple(
             fmodel_kbu=fmodel_kbu,
             r_free_flags=self.r_free_flags(),
             bulk_solvent=params.bulk_solvent,
             bin_selections=self.bin_selections)
         r_all_from_scaler = result.r_all(
         )  # must be here, before apply_back_trace
     else:  # using minimization: exp solvent and scale model (k_sol,b_sol,b_cart)
         result = bss.bulk_solvent_and_scales(fmodel_kbu=fmodel_kbu,
                                              params=params)
         k_sol, b_sol, b_cart = result.k_sols(), result.b_sols(
         ), result.b_cart()
         r_all_from_scaler = result.r_all(
         )  # must be here, before apply_back_trace
     if (apply_back_trace and len(fmodel_kbu.f_masks) == 1
             and self.xray_structure is not None):
         o = result.apply_back_trace_of_overall_exp_scale_matrix(
             xray_structure=self.xray_structure)
         b_adj = o.b_adj
         if (not fast): b_sol, b_cart = [o.b_sol], o.b_cart
         self.update_xray_structure(xray_structure=o.xray_structure,
                                    update_f_calc=True)
         fmodel_kbu = fmodel_kbu.update(f_calc=self.f_calc())
         self.show(prefix="overall B=%s to atoms" %
                   str("%7.2f" % o.b_adj).strip(),
                   log=log)
     # Update self with new arrays so that H correction knows current R factor.
     # If no H to account for, then this is the final result.
     k_masks = result.k_masks()
     k_anisotropic = result.k_anisotropic()
     k_isotropic = result.k_isotropic()
     self.update_core(k_mask=k_masks,
                      k_anisotropic=k_anisotropic,
                      k_isotropic=k_isotropic)
     self.show(prefix="bulk-solvent and scaling", log=log)
     # Consistency check
     if (not apply_back_trace):
         assert approx_equal(self.r_all(), r_all_from_scaler)
     # Add contribution from H (if present and riding). This goes to f_part2.
     kh, bh = 0, 0
     if (refine_hd_scattering
             and self.need_to_refine_hd_scattering_contribution()):
         # Obsolete previous contribution f_part2
         f_part2 = fmodel_kbu.f_calc.array(data=fmodel_kbu.f_calc.data() *
                                           0)
         self.update_core(f_part2=f_part2)
         xrs_h = xrs_h.set_occupancies(value=1).set_b_iso(value=0)
         f_h = self.compute_f_calc(xray_structure=xrs_h)
         # Accumulate all mask contributions: Fcalc_atoms+Fbulk_1+...+Fbulk_N
         data = fmodel_kbu.f_calc.data()
         for k_mask_, f_mask_ in zip(k_masks, fmodel_kbu.f_masks):
             data = data + k_mask_ * f_mask_.data()
         f_calc_plus_f_bulk_no_scales = fmodel_kbu.f_calc.array(data=data)
         # Consistency check
         assert approx_equal(
             self.f_model().data(),
             f_calc_plus_f_bulk_no_scales.data() * k_isotropic *
             k_anisotropic)
         assert approx_equal(self.f_model_no_scales().data(),
                             f_calc_plus_f_bulk_no_scales.data())
         #
         # Compute contribution from H (F_H)
         #
         # Coarse sampling
         b_mean = flex.mean(
             xrs_no_h.extract_u_iso_or_u_equiv()) * adptbx.u_as_b(1.)
         b_min = int(max(0, b_mean) * 0.5)
         b_max = int(b_mean * 1.5)
         sc = 1000.
         kr = [
             i / sc for i in range(ifloor(0 * sc),
                                   iceil(1.5 * sc) + 1, int(0.1 * sc))
         ]
         br = [
             i / sc for i in range(ifloor(b_min * sc),
                                   iceil(b_max * sc) + 1, int(5. * sc))
         ]
         o = bulk_solvent.k_sol_b_sol_k_anisotropic_scaler_twin(
             f_obs=fmodel_kbu.f_obs.data(),
             f_calc=f_calc_plus_f_bulk_no_scales.data(),
             f_mask=f_h.data(),
             k_total=k_isotropic * k_anisotropic,
             ss=fmodel_kbu.ss,
             k_sol_range=flex.double(kr),
             b_sol_range=flex.double(br),
             r_ref=self.r_work())
         if (o.updated()):
             f_part2 = f_h.array(data=o.k_mask() * f_h.data())
             kh, bh = o.k_sol(), o.b_sol()
             self.show(prefix="add H (%4.2f, %6.2f)" % (kh, bh),
                       log=log,
                       r=o.r())
         # Fine sampling
         k_min = max(0, o.k_sol() - 0.1)
         k_max = o.k_sol() + 0.1
         b_min = max(0, o.b_sol() - 5.)
         b_max = o.b_sol() + 5.
         kr = [
             i / sc for i in range(ifloor(k_min * sc),
                                   iceil(k_max * sc) + 1, int(0.01 * sc))
         ]
         br = [
             i / sc for i in range(ifloor(b_min * sc),
                                   iceil(b_max * sc) + 1, int(1. * sc))
         ]
         o = bulk_solvent.k_sol_b_sol_k_anisotropic_scaler_twin(
             f_obs=fmodel_kbu.f_obs.data(),
             f_calc=f_calc_plus_f_bulk_no_scales.data(),
             f_mask=f_h.data(),
             k_total=k_isotropic * k_anisotropic,
             ss=fmodel_kbu.ss,
             k_sol_range=flex.double(kr),
             b_sol_range=flex.double(br),
             r_ref=o.r())
         if (o.updated()):
             f_part2 = f_h.array(data=o.k_mask() * f_h.data())
             kh, bh = o.k_sol(), o.b_sol()
             self.show(prefix="add H (%4.2f, %6.2f)" % (kh, bh),
                       log=log,
                       r=o.r())
         # THIS HELPS if fast=true is used, see how it works in reality
         #
         if (fast):
             fmodel_kbu_ = mmtbx.f_model.manager_kbu(
                 f_obs=self.f_obs(),
                 f_calc=f_calc_plus_f_bulk_no_scales,
                 f_masks=[f_part2],
                 ss=self.ss)
             result = mmtbx.bulk_solvent.scaler.run_simple(
                 fmodel_kbu=fmodel_kbu_,
                 r_free_flags=self.r_free_flags(),
                 bulk_solvent=params.bulk_solvent,
                 bin_selections=self.bin_selections)
             f_part2 = f_part2.array(data=result.core.k_mask() *
                                     f_part2.data())
             k_isotropic = result.core.k_isotropic * result.core.k_isotropic_exp
             k_anisotropic = result.core.k_anisotropic
         # Update self with final scales
         self.update_core(k_mask=k_masks,
                          k_anisotropic=k_anisotropic,
                          k_isotropic=k_isotropic,
                          f_part2=f_part2)
         # Make sure what came out of scaling matches what self thinks it really is
         # It must match at least up to 1.e-6.
         self.show(prefix="add H (%4.2f, %6.2f)" % (kh, bh), log=log)
         if (fast):
             assert approx_equal(result.r_work(), self.r_work(), 1.e-4)
         else:
             assert approx_equal(self.r_all(), o.r()), [self.r_all(), o.r()]
     return group_args(k_sol=k_sol,
                       b_sol=b_sol,
                       b_cart=b_cart,
                       k_h=kh,
                       b_h=bh,
                       b_adj=b_adj)
Esempio n. 51
0
 def anisotropic_scaling(self, r_start):
     r_expanal, r_poly, r_expmin = None, None, None
     k_anisotropic_expanal, k_anisotropic_poly, \
       k_anisotropic_expmin = None, None, None
     scale_matrix_expanal, scale_matrix_poly, scale_matrix_expmin = None, None, None
     sel = self.selection_work.data()
     f_model_abs = flex.abs(
         self.core.f_model_no_aniso_scale.data().select(sel))
     f_obs = self.f_obs.data().select(sel)
     mi = self.f_obs.indices().select(sel)
     uc = self.f_obs.unit_cell()
     mi_all = self.f_obs.indices()
     # try exp_anal
     if (self.try_expanal):
         obj = bulk_solvent.aniso_u_scaler(
             f_model_abs=f_model_abs,
             f_obs=f_obs,
             miller_indices=mi,
             adp_constraint_matrix=self.adp_constraints.gradient_sum_matrix(
             ))
         u_star = self.adp_constraints.all_params(
             tuple(obj.u_star_independent))
         scale_matrix_expanal = adptbx.u_as_b(
             adptbx.u_star_as_u_cart(uc, u_star))
         k_anisotropic_expanal = ext.k_anisotropic(mi_all, u_star)
         r_expanal = self.try_scale(k_anisotropic=k_anisotropic_expanal)
         if (self.verbose):
             print >> self.log, "      r_expanal: %6.4f" % r_expanal
     # try poly
     if (self.try_poly):
         obj = bulk_solvent.aniso_u_scaler(f_model_abs=f_model_abs,
                                           f_obs=f_obs,
                                           miller_indices=mi,
                                           unit_cell=uc)
         scale_matrix_poly = obj.a
         k_anisotropic_poly = ext.k_anisotropic(mi_all, obj.a, uc)
         r_poly = self.try_scale(k_anisotropic=k_anisotropic_poly)
         if (self.verbose):
             print >> self.log, "      r_poly   : %6.4f" % r_poly
     # pre-analyze
     force_to_use_expmin = False
     if (k_anisotropic_poly is not None and self.auto and r_poly < r_expanal
             and (k_anisotropic_poly <= 0).count(True) > 0):
         force_to_use_expmin = True
         self.try_expmin = True
     # try expmin
     if (self.try_expmin):
         zero = self.f_obs.select(sel).customized_copy(
             data=flex.complex_double(f_obs.size(), 0))
         fm = mmtbx.f_model.manager_kbu(
             f_obs=self.f_obs.select(sel),
             f_calc=self.core.f_model_no_aniso_scale.select(sel),
             f_masks=[zero],
             f_part1=zero,
             f_part2=zero,
             ss=self.ss)
         obj = kbu_refinery.tgc(
             f_obs=self.f_obs.select(sel),
             f_calc=self.core.f_model_no_aniso_scale.select(sel),
             f_masks=[zero],
             ss=self.ss,
             k_sols=[
                 0,
             ],
             b_sols=[
                 0,
             ],
             u_star=[0, 0, 0, 0, 0, 0])
         obj.minimize_u()
         u_star = obj.kbu.u_star()
         scale_matrix_expmin = adptbx.u_as_b(
             adptbx.u_star_as_u_cart(uc, u_star))
         k_anisotropic_expmin = ext.k_anisotropic(mi_all, u_star)
         r_expmin = self.try_scale(k_anisotropic=k_anisotropic_expmin)
         if (self.verbose):
             print >> self.log, "    r_expmin   : %6.4f" % r_expmin
         if (force_to_use_expmin):
             self.core = self.core.update(
                 k_anisotropic=k_anisotropic_expmin)
             if (self.verbose):
                 self.format_scale_matrix(m=scale_matrix_expmin)
             return
     # select best
     r = [(r_expanal, k_anisotropic_expanal, scale_matrix_expanal),
          (r_poly, k_anisotropic_poly, scale_matrix_poly),
          (r_expmin, k_anisotropic_expmin, scale_matrix_expmin)]
     r_best = r_start
     k_anisotropic_best = None
     scale_matrix_best = None
     for result in r:
         r_factor, k_anisotropic, scale_matrix = result
         if (r_factor is not None and r_factor < r_best):
             r_best = r_factor
             k_anisotropic_best = k_anisotropic.deep_copy()
             scale_matrix_best = scale_matrix[:]
     if (scale_matrix_best is None):
         if (self.verbose):
             print >> self.log, "      result rejected due to r-factor increase"
     else:
         self.scale_matrices = scale_matrix_best
         self.core = self.core.update(k_anisotropic=k_anisotropic_best)
         r_aniso = self.r_factor()
         if (self.verbose):
             self.format_scale_matrix()
             print >> self.log, "      r_final  : %6.4f" % r_aniso
Esempio n. 52
0
    def update_solvent_and_scale_twin(self, refine_hd_scattering, log):
        if (not self.twinned()): return
        assert len(self.f_masks()) == 1
        # Re-set all scales to unit or zero
        self.show(prefix="update scales twin start", log=log)
        self.reset_all_scales()
        self.show(prefix="reset f_part, k_(total,mask)", log=log)
        f_calc_data = self.f_calc().data()
        f_calc_data_twin = self.f_calc_twin().data()
        # Initial trial set
        sc = 1000.
        ksr = [
            i / sc for i in range(ifloor(0 * sc),
                                  iceil(0.6 * sc) + 1, int(0.05 * sc))
        ]
        bsr = [
            i / sc for i in range(ifloor(0 * sc),
                                  iceil(150. * sc) + 1, int(10. * sc))
        ]
        o_kbu_sol = bulk_solvent.k_sol_b_sol_k_anisotropic_scaler_twin(
            f_obs=self.f_obs().data(),
            f_calc_1=f_calc_data,
            f_calc_2=f_calc_data_twin,
            f_mask_1=self.arrays.core.f_masks[0].data(),
            f_mask_2=self.arrays.core_twin.f_masks[0].data(),
            ss=self.ss,
            twin_fraction=self.twin_fraction,
            k_sol_range=flex.double(ksr),
            b_sol_range=flex.double(bsr),
            miller_indices=self.f_obs().indices(
            ),  #XXX ??? What about twin-related?
            unit_cell=self.f_obs().unit_cell(),
            r_ref=self.r_all())
        if (o_kbu_sol.updated()):
            self.update(k_mask=o_kbu_sol.k_mask(),
                        k_anisotropic=o_kbu_sol.k_anisotropic())
        # Second (finer) trial set
        k_min = max(o_kbu_sol.k_sol() - 0.05, 0)
        k_max = min(o_kbu_sol.k_sol() + 0.05, 0.6)
        ksr = [
            i / sc for i in range(ifloor(k_min * sc),
                                  iceil(k_max * sc) + 1, int(0.01 * sc))
        ]
        b_min = max(o_kbu_sol.b_sol() - 10, 0)
        b_max = min(o_kbu_sol.b_sol() + 10, 150)
        bsr = [
            i / sc for i in range(ifloor(b_min * sc),
                                  iceil(b_max * sc) + 1, int(1. * sc))
        ]
        o_kbu_sol = bulk_solvent.k_sol_b_sol_k_anisotropic_scaler_twin(
            f_obs=self.f_obs().data(),
            f_calc_1=f_calc_data,
            f_calc_2=f_calc_data_twin,
            f_mask_1=self.arrays.core.f_masks[0].data(),
            f_mask_2=self.arrays.core_twin.f_masks[0].data(),
            ss=self.ss,
            twin_fraction=self.twin_fraction,
            k_sol_range=flex.double(ksr),
            b_sol_range=flex.double(bsr),
            miller_indices=self.f_obs().indices(
            ),  #XXX ??? What about twin-related?
            unit_cell=self.f_obs().unit_cell(),
            r_ref=o_kbu_sol.r())
        if (o_kbu_sol.updated()):
            self.update(k_mask=o_kbu_sol.k_mask(),
                        k_anisotropic=o_kbu_sol.k_anisotropic())
            # Disable due to rare failures. Technically they should always match. But
            # since different routines are used tiny disagreements are possible.
            # See examples in : /net/anaconda/raid1/afonine/work/bugs/twin_refinement
            #assert approx_equal(self.r_all(), o_kbu_sol.r(), 1.e-5)
            ##############
            # use apply_back_trace in if below
            if (self.xray_structure is not None):
                o = mmtbx.bulk_solvent.scaler.tmp(
                    xray_structure=self.xray_structure,
                    k_anisotropic=o_kbu_sol.k_anisotropic(),
                    k_masks=[o_kbu_sol.k_mask()],
                    ss=self.ss)
                self.update_xray_structure(xray_structure=o.xray_structure,
                                           update_f_calc=True)
                #############
                self.update(k_mask=o.k_masks, k_anisotropic=o.k_anisotropic)

        self.show(prefix="bulk-solvent and scaling", log=log)
        #
        # Add contribution from H (if present and riding). This goes to f_part2.
        #
        kh, bh = 0, 0
        if (refine_hd_scattering
                and self.need_to_refine_hd_scattering_contribution()):
            hd_selection = self.xray_structure.hd_selection()
            xrs_no_h = self.xray_structure.select(~hd_selection)
            xrs_h = self.xray_structure.select(hd_selection)
            # Accumulate all mask contributions: Fcalc_atoms+Fbulk_1+...+Fbulk_N
            data = self.f_calc().data(
            ) + self.f_masks()[0].data() * self.k_masks()[0]
            f_calc_plus_f_bulk_no_scales = self.f_calc().array(data=data)
            data = self.f_calc_twin().data()+\
              self.f_masks_twin()[0].data()*self.k_masks_twin()[0]
            f_calc_plus_f_bulk_no_scales_twin = self.f_calc_twin().array(
                data=data)
            # Initial FH contribution
            xrs_h = xrs_h.set_occupancies(value=1).set_b_iso(value=0)
            f_h = self.compute_f_calc(xray_structure=xrs_h)
            f_h_twin = self.compute_f_calc(xray_structure=xrs_h,
                                           miller_array=self.f_calc_twin())
            # Coarse sampling
            b_mean = flex.mean(
                xrs_no_h.extract_u_iso_or_u_equiv()) * adptbx.u_as_b(1.)
            b_min = int(max(0, b_mean) * 0.5)
            b_max = int(b_mean * 1.5)
            sc = 1000.
            kr = [
                i / sc for i in range(ifloor(0 * sc),
                                      iceil(1.5 * sc) + 1, int(0.1 * sc))
            ]
            br = [
                i / sc for i in range(ifloor(b_min * sc),
                                      iceil(b_max * sc) + 1, int(5. * sc))
            ]
            obj = bulk_solvent.k_sol_b_sol_k_anisotropic_scaler_twin(
                f_obs=self.f_obs().data(),
                f_calc_1=f_calc_plus_f_bulk_no_scales.data(),
                f_calc_2=f_calc_plus_f_bulk_no_scales_twin.data(),
                f_mask_1=f_h.data(),
                f_mask_2=f_h_twin.data(),
                ss=self.ss,
                twin_fraction=self.twin_fraction,
                k_sol_range=flex.double(kr),
                b_sol_range=flex.double(br),
                miller_indices=self.f_obs().indices(
                ),  # XXX What about twin-related?
                unit_cell=self.f_obs().unit_cell(),
                r_ref=self.r_work())
            if (obj.updated()):
                f_part2 = f_h.array(data=obj.k_mask() * f_h.data())
                f_part2_twin = f_h_twin.array(data=obj.k_mask() *
                                              f_h_twin.data())
                kh, bh = obj.k_sol(), obj.b_sol()
            # Fine sampling
            k_min = max(0, obj.k_sol() - 0.1)
            k_max = obj.k_sol() + 0.1
            b_min = max(0, obj.b_sol() - 5.)
            b_max = obj.b_sol() + 5.
            kr = [
                i / sc for i in range(ifloor(k_min * sc),
                                      iceil(k_max * sc) + 1, int(0.01 * sc))
            ]
            br = [
                i / sc for i in range(ifloor(b_min * sc),
                                      iceil(b_max * sc) + 1, int(5. * sc))
            ]
            obj = bulk_solvent.k_sol_b_sol_k_anisotropic_scaler_twin(
                f_obs=self.f_obs().data(),
                f_calc_1=f_calc_plus_f_bulk_no_scales.data(),
                f_calc_2=f_calc_plus_f_bulk_no_scales_twin.data(),
                f_mask_1=f_h.data(),
                f_mask_2=f_h_twin.data(),
                ss=self.ss,
                twin_fraction=self.twin_fraction,
                k_sol_range=flex.double(kr),
                b_sol_range=flex.double(br),
                miller_indices=self.f_obs().indices(
                ),  # XXX What about twin-related?
                unit_cell=self.f_obs().unit_cell(),
                r_ref=obj.r())
            if (obj.updated()):
                f_part2 = f_h.array(data=obj.k_mask() * f_h.data())
                f_part2_twin = f_h_twin.array(data=obj.k_mask() *
                                              f_h_twin.data())
                kh, bh = obj.k_sol(), obj.b_sol()
            self.update_core(f_part2=f_part2,
                             f_part2_twin=f_part2_twin,
                             k_anisotropic=obj.k_anisotropic())
            self.show(prefix="add H (%4.2f, %6.2f)" % (kh, bh), log=log)
        b_cart = adptbx.u_as_b(
            adptbx.u_star_as_u_cart(self.f_obs().unit_cell(),
                                    o_kbu_sol.u_star()))
        return group_args(k_sol=o_kbu_sol.k_sol(),
                          b_sol=o_kbu_sol.b_sol(),
                          b_cart=b_cart,
                          k_h=kh,
                          b_h=bh)
Esempio n. 53
0
    def __init__(self,
                 miller_array,
                 n_residues=None,
                 n_bases=None,
                 asu_contents=None,
                 prot_frac=1.0,
                 nuc_frac=0.0):
        """ Maximum likelihood anisotropic wilson scaling"""
        #Checking input
        if (n_residues is None):
            if (n_bases is None):
                assert asu_contents is not None
                assert (type(asu_contents) == type({}))
        if asu_contents is None:
            assert ((n_residues is not None) or (n_bases is not None))
        assert (prot_frac + nuc_frac <= 1.0)
        assert (miller_array.is_real_array())

        self.info = miller_array.info()
        if (miller_array.is_xray_intensity_array()):
            miller_array = miller_array.f_sq_as_f()

        work_array = miller_array.resolution_filter(
            d_max=1.0 / math.sqrt(scaling.get_d_star_sq_low_limit()),
            d_min=1.0 / math.sqrt(scaling.get_d_star_sq_high_limit()))
        work_array = work_array.select(work_array.data() > 0)
        self.d_star_sq = work_array.d_star_sq().data()
        self.scat_info = None
        if asu_contents is None:
            self.scat_info = scattering_information(n_residues=n_residues,
                                                    n_bases=n_bases)
        else:
            self.scat_info = scattering_information(asu_contents=asu_contents,
                                                    fraction_protein=prot_frac,
                                                    fraction_nucleic=nuc_frac)
        self.scat_info.scat_data(self.d_star_sq)
        self.b_cart = None
        if (work_array.size() > 0):
            self.hkl = work_array.indices()
            self.f_obs = work_array.data()
            self.unit_cell = uctbx.unit_cell(
                miller_array.unit_cell().parameters())
            ## Make sure sigma's are used when available
            if (work_array.sigmas() is not None):
                self.sigma_f_obs = work_array.sigmas()
            else:
                self.sigma_f_obs = flex.double(self.f_obs.size(), 0.0)
            if (flex.min(self.sigma_f_obs) < 0):
                self.sigma_f_obs = self.sigma_f_obs * 0.0

            ## multiplicities
            self.epsilon = work_array.epsilons().data().as_double()
            ## Determine Wilson parameters
            self.gamma_prot = self.scat_info.gamma_tot
            self.sigma_prot_sq = self.scat_info.sigma_tot_sq
            ## centric flags
            self.centric = flex.bool(work_array.centric_flags().data())
            ## Symmetry stuff
            self.sg = work_array.space_group()
            self.adp_constraints = self.sg.adp_constraints()
            self.dim_u = self.adp_constraints.n_independent_params()
            ## Setup number of parameters
            assert self.dim_u <= 6
            ## Optimisation stuff
            self.x = flex.double(self.dim_u + 1,
                                 0.0)  ## B-values and scale factor!
            exception_handling_params = scitbx.lbfgs.exception_handling_parameters(
                ignore_line_search_failed_step_at_lower_bound=False,
                ignore_line_search_failed_step_at_upper_bound=False,
                ignore_line_search_failed_maxfev=False)
            term_parameters = scitbx.lbfgs.termination_parameters(
                max_iterations=50)

            minimizer = scitbx.lbfgs.run(
                target_evaluator=self,
                termination_params=term_parameters,
                exception_handling_params=exception_handling_params)

            ## Done refining
            Vrwgk = math.pow(self.unit_cell.volume(), 2.0 / 3.0)
            self.p_scale = self.x[0]
            self.u_star = self.unpack()
            self.u_star = list(flex.double(self.u_star) / Vrwgk)
            self.b_cart = adptbx.u_as_b(
                adptbx.u_star_as_u_cart(self.unit_cell, self.u_star))
            self.u_cif = adptbx.u_star_as_u_cif(self.unit_cell, self.u_star)
            #get eigenvalues of B-cart
            eigen = eigensystem.real_symmetric(self.b_cart)
            self.eigen_values = eigen.values()
            self.eigen_vectors = eigen.vectors()

            self.work_array = work_array  # i need this for further analyses
            self.analyze_aniso_correction()
            # FIXME see 3ihm:IOBS4,SIGIOBS4
            if (self.eigen_values[0] != 0):
                self.anirat = (
                    abs(self.eigen_values[0] - self.eigen_values[2]) /
                    self.eigen_values[0])
            else:
                self.anirat = None

            del self.x
            del self.f_obs
            del self.sigma_f_obs
            del self.epsilon
            del self.gamma_prot
            del self.sigma_prot_sq
            del self.centric
            del self.hkl
            del self.d_star_sq
            del self.adp_constraints
Esempio n. 54
0
 def __init__(
           self,
           model,
           fmodels,
           target_weights,
           individual_adp_params,
           adp_restraints_params,
           h_params,
           log,
           all_params,
           nproc=None):
   adopt_init_args(self, locals())
   d_min = fmodels.fmodel_xray().f_obs().d_min()
   # initialize with defaults...
   if(target_weights is not None):
     import mmtbx.refinement.weights_params
     wcp = mmtbx.refinement.weights_params.tw_customizations_params.extract()
     for w_s_c in wcp.weight_selection_criteria:
       if(d_min >= w_s_c.d_min and d_min < w_s_c.d_max):
         r_free_range_width = w_s_c.r_free_range_width
         r_free_r_work_gap = w_s_c.r_free_minus_r_work
         mean_diff_b_iso_bonded_fraction = w_s_c.mean_diff_b_iso_bonded_fraction
         min_diff_b_iso_bonded = w_s_c.min_diff_b_iso_bonded
         break
     # ...then customize
     wsc = all_params.target_weights.weight_selection_criteria
     if(wsc.r_free_minus_r_work is not None):
       r_free_r_work_gap = wsc.r_free_minus_r_work
     if(wsc.r_free_range_width is not None):
       r_free_range_width = wsc.r_free_range_width
     if(wsc.mean_diff_b_iso_bonded_fraction is not None):
       mean_diff_b_iso_bonded_fraction = wsc.mean_diff_b_iso_bonded_fraction
     if(wsc.min_diff_b_iso_bonded is not None):
       min_diff_b_iso_bonded = wsc.min_diff_b_iso_bonded
   #
   print_statistics.make_sub_header(text="Individual ADP refinement", out = log)
   assert fmodels.fmodel_xray().xray_structure is model.get_xray_structure()
   #
   fmodels.create_target_functors()
   assert approx_equal(self.fmodels.fmodel_xray().target_w(),
     self.fmodels.target_functor_result_xray(
       compute_gradients=False).target_work())
   rw     = flex.double()
   rf     = flex.double()
   rfrw   = flex.double()
   deltab = flex.double()
   w      = flex.double()
   if(self.target_weights is not None):
     fmth ="    R-FACTORS      <Bi-Bj>  <B>   WEIGHT       TARGETS"
     print(fmth, file=self.log)
     print(" work  free  delta                           data restr", file=self.log)
   else:
     print("Unresrained refinement...", file=self.log)
   self.save_scatterers = self.fmodels.fmodel_xray().xray_structure.\
       deep_copy_scatterers().scatterers()
   if(self.target_weights is not None):
     default_weight = self.target_weights.adp_weights_result.wx*\
         self.target_weights.adp_weights_result.wx_scale
     if(self.target_weights.twp.optimize_adp_weight):
       wx_scale = [0.03,0.125,0.5,1.,1.5,2.,2.5,3.,3.5,4.,4.5,5.]
       trial_weights = list( flex.double(wx_scale)*self.target_weights.adp_weights_result.wx )
       self.wx_scale = 1
     else:
       trial_weights = [self.target_weights.adp_weights_result.wx]
       self.wx_scale = self.target_weights.adp_weights_result.wx_scale
   else:
     default_weight = 1
     trial_weights = [1]
     self.wx_scale = 1
   self.show(weight=default_weight)
   trial_results = []
   if nproc is None:
     nproc =  all_params.main.nproc
   parallel = False
   if (len(trial_weights) > 1) and ((nproc is Auto) or (nproc > 1)):
     parallel = True
     from libtbx import easy_mp
     stdout_and_results = easy_mp.pool_map(
       processes=nproc,
       fixed_func=self.try_weight,
       args=trial_weights,
       func_wrapper="buffer_stdout_stderr") # XXX safer for phenix GUI
     trial_results = [ r for so, r in stdout_and_results ]
   else :
     for weight in trial_weights:
       result = self.try_weight(weight, print_stats=True)
       trial_results.append(result)
   for result in trial_results :
     if(result is not None) and (result.r_work is not None):
       if (parallel):
         result.show(out=self.log)
       rw     .append(result.r_work)
       rf     .append(result.r_free)
       rfrw   .append(result.r_gap)
       deltab .append(result.delta_b)
       w      .append(result.weight)
   #
   if(len(trial_weights)>1 and rw.size()>0):
     # filter by rfree-rwork
     rw,rf,rfrw,deltab,w = self.score(rw=rw,rf=rf,rfrw=rfrw,deltab=deltab,w=w,
       score_target=rfrw,score_target_value=r_free_r_work_gap,
       secondary_target=deltab)
     # filter by rfree
     rw,rf,rfrw,deltab,w = self.score(rw=rw,rf=rf,rfrw=rfrw,deltab=deltab,w=w,
       score_target=rf,score_target_value=flex.min(rf)+r_free_range_width)
     # filter by <Bi-Bj>
     delta_b_target = max(min_diff_b_iso_bonded, flex.mean(self.fmodels.
       fmodel_xray().xray_structure.extract_u_iso_or_u_equiv()*
         adptbx.u_as_b(1))*mean_diff_b_iso_bonded_fraction)
     print("  max suggested <Bi-Bj> for this run: %7.2f"%delta_b_target, file=log)
     print("  max allowed Rfree-Rwork gap: %5.1f"%r_free_r_work_gap, file=log)
     print("  range of equivalent Rfree: %5.1f"%r_free_range_width, file=log)
     rw,rf,rfrw,deltab,w = self.score(rw=rw,rf=rf,rfrw=rfrw,deltab=deltab,w=w,
       score_target=deltab,score_target_value=delta_b_target)
     # select the result with lowest rfree
     sel = flex.sort_permutation(rf)
     rw,rf,rfrw,deltab,w= self.select(
       rw=rw,rf=rf,rfrw=rfrw,deltab=deltab,w=w,sel=sel)
     #
     w_best = w[0]
     rw_best = rw[0]
     print("Best ADP weight: %8.3f"%w_best, file=self.log)
     #
     self.target_weights.adp_weights_result.wx = w_best
     self.target_weights.adp_weights_result.wx_scale = 1
     best_u_star = None
     best_u_iso = None
     for result in trial_results :
       if(abs(result.weight-w_best)<=1.e-8):
         best_u_star = result.u_star
         best_u_iso = result.u_iso
         break
     if(best_u_iso is None) : # XXX this probably shouldn't happen...
       self.fmodels.fmodel_xray().xray_structure.replace_scatterers(
         self.save_scatterers.deep_copy())
     else :
       assert (best_u_star is not None)
       xrs = self.fmodels.fmodel_xray().xray_structure
       xrs.set_u_iso(values=best_u_iso)
       xrs.scatterers().set_u_star(best_u_star)
       new_u_iso = xrs.scatterers().extract_u_iso()
       assert (new_u_iso.all_eq(best_u_iso))
     self.fmodels.update_xray_structure(
       xray_structure = self.fmodels.fmodel_xray().xray_structure,
       update_f_calc  = True)
     print("Accepted refinement result:", file=self.log)
     # reset alpha/beta parameters - if this is not done, the assertion
     # below will fail
     fmodels.create_target_functors()
     if(self.fmodels.fmodel_neutron() is None):
       assert approx_equal(self.fmodels.fmodel_xray().r_work()*100, rw_best,
         eps=0.001)
       # this needs to be done again again, just in case
       fmodels.create_target_functors()
     self.show(weight=w_best)
   self.fmodels.fmodel_xray().xray_structure.tidy_us()
   self.fmodels.update_xray_structure(
     xray_structure = self.fmodels.fmodel_xray().xray_structure,
     update_f_calc  = True)
   fmodels.create_target_functors()
   assert approx_equal(self.fmodels.fmodel_xray().target_w(),
      self.fmodels.target_functor_result_xray(
        compute_gradients=False).target_work())
   self.model.set_xray_structure(self.fmodels.fmodel_xray().xray_structure)
Esempio n. 55
0
    def __init__(self,
                 pdb_hierarchy,
                 xray_structure,
                 ignore_hd=True,
                 collect_outliers=True):
        for name in self.__slots__:
            setattr(self, name, None)
        validation.__init__(self)
        assert len(xray_structure.scatterers()) != 0
        from cctbx import adptbx
        from scitbx.array_family import flex
        xrs = xray_structure
        self.n_total = xrs.scatterers().size()  # always include H/D
        self.results = None
        pdb_atoms = pdb_hierarchy.atoms()
        pdb_atoms.reset_i_seq()
        hd_selection = xrs.hd_selection()
        subtract_hd = True
        self.n_all = hd_selection.size()
        self.n_hd = hd_selection.count(True)
        if (ignore_hd) and (0 < self.n_hd < self.n_all):
            xrs = xrs.select(~hd_selection)
            subtract_hd = False
        u_isos = xrs.extract_u_iso_or_u_equiv()
        occ = xrs.scatterers().extract_occupancies()
        self.n_atoms = xrs.scatterers().size()
        self.n_non_hd = self.n_all - self.n_hd
        self.n_aniso = xrs.use_u_aniso().count(True)
        self.n_aniso_h = (xray_structure.use_u_aniso()
                          & hd_selection).count(True)
        self.n_npd = xrs.is_positive_definite_u().count(False)
        self.n_zero_b = (u_isos == 0).count(True)
        self.n_zero_occ = (occ == 0).count(True)
        u_cutoff_high = sys.maxsize
        u_cutoff_low = 0
        u_non_zero = u_isos.select(u_isos > 0)
        if (len(u_non_zero) > 1):
            mv = flex.mean_and_variance(u_non_zero)
            sigma = mv.unweighted_sample_standard_deviation()
            u_cutoff_high = mv.mean() + (4.0 * sigma)
            u_cutoff_low = mv.mean() - (4.0 * sigma)
        self.b_mean = adptbx.u_as_b(flex.mean(u_isos))
        self.b_min = adptbx.u_as_b(flex.min(u_isos))
        self.b_max = adptbx.u_as_b(flex.max(u_isos))
        self.o_mean = flex.mean(occ)
        self.o_min = flex.min(occ)
        self.o_max = flex.max(occ)
        self.n_outliers = self.n_aniso_h + self.n_npd
        self.zero_occ = []
        self.partial_occ = []
        self.different_occ = []
        self.bad_adps = []
        self.b_histogram = None  # TODO

        def is_u_iso_outlier(u):
            return (u < u_cutoff_low) or (u > u_cutoff_high) or (u <= 0)

        # these statistics cover all atoms!
        occupancies = xray_structure.scatterers().extract_occupancies()
        u_isos = xray_structure.extract_u_iso_or_u_equiv()
        collected = flex.bool(occupancies.size(), False)
        if (collect_outliers):
            for i_seq, occ in enumerate(occupancies):
                if (hd_selection[i_seq] and ignore_hd) or collected[i_seq]:
                    continue
                pdb_atom = pdb_atoms[i_seq]
                parent = pdb_atom.parent()
                if (occ <= 0):
                    group_atoms = parent.atoms()
                    labels = pdb_atom.fetch_labels()
                    if (len(group_atoms) >
                            1) and (group_atoms.extract_occ().all_eq(0)):
                        i_seqs = group_atoms.extract_i_seq()
                        b_mean = adptbx.u_as_b(flex.mean(
                            u_isos.select(i_seqs)))
                        outlier = residue_occupancy(
                            chain_id=labels.chain_id,
                            resseq=labels.resseq,
                            icode=labels.icode,
                            altloc=labels.altloc,
                            resname=labels.resname,
                            occupancy=occ,
                            outlier=True,
                            xyz=group_atoms.extract_xyz().mean(),
                            b_iso=b_mean)
                        self.zero_occ.append(outlier)
                        self.n_outliers += 1
                        collected.set_selected(i_seqs, True)
                    else:
                        assert (pdb_atom.occ == occ), "%s: %s <--> %s" % (
                            pdb_atom.id_str(), pdb_atom.occ, occ)
                        outlier = atom_occupancy(pdb_atom=pdb_atom,
                                                 occupancy=occ,
                                                 b_iso=adptbx.u_as_b(
                                                     u_isos[i_seq]),
                                                 xyz=pdb_atom.xyz,
                                                 outlier=True)
                        self.zero_occ.append(outlier)
                        self.n_outliers += 1
                elif is_u_iso_outlier(u_isos[i_seq]):
                    # zero displacements will always be recorded on a per-atom basis
                    if (u_isos[i_seq] <= 0):
                        outlier = atom_bfactor(pdb_atom=pdb_atom,
                                               occupancy=occ,
                                               b_iso=adptbx.u_as_b(
                                                   u_isos[i_seq]),
                                               xyz=pdb_atom.xyz,
                                               outlier=True)
                        self.bad_adps.append(outlier)
                        self.n_outliers += 1
                    else:
                        # if the average displacement for the entire residue falls outside
                        # the cutoffs, save as a single residue outlier
                        group_atoms = parent.atoms()
                        i_seqs = group_atoms.extract_i_seq()
                        u_mean = flex.mean(u_isos.select(i_seqs))
                        if is_u_iso_outlier(u_mean):
                            labels = pdb_atom.fetch_labels()
                            outlier = residue_bfactor(
                                chain_id=labels.chain_id,
                                resseq=labels.resseq,
                                icode=labels.icode,
                                altloc=labels.altloc,
                                resname=labels.resname,
                                occupancy=occ,
                                outlier=True,
                                xyz=group_atoms.extract_xyz().mean(),
                                b_iso=adptbx.u_as_b(u_mean))
                            self.bad_adps.append(outlier)
                            self.n_outliers += 1
                            collected.set_selected(i_seqs, True)
                        # otherwise, just save this atom
                        else:
                            outlier = atom_bfactor(pdb_atom=pdb_atom,
                                                   occupancy=occ,
                                                   b_iso=adptbx.u_as_b(
                                                       u_isos[i_seq]),
                                                   xyz=pdb_atom.xyz,
                                                   outlier=True)
                            self.bad_adps.append(outlier)
                            self.n_outliers += 1

            # analyze occupancies for first model
            model = pdb_hierarchy.models()[0]
            for chain in model.chains():
                residue_groups = chain.residue_groups()
                for residue_group in chain.residue_groups():
                    # get unique set of atom names
                    atom_names = set()
                    for atom in residue_group.atoms():
                        atom_names.add(atom.name.strip())

                    # check total occupancy for each atom
                    for name in atom_names:
                        occupancy = 0.0
                        atoms = list()
                        for atom_group in residue_group.atom_groups():
                            atom = atom_group.get_atom(name)
                            if (atom is not None):
                                occupancy += atom.occ
                                atoms.append(atom)

                        if (not approx_equal(
                                occupancy, 1.0, out=None, eps=1.0e-3)):
                            for atom in atoms:
                                outlier = atom_occupancy(pdb_atom=atom,
                                                         occupancy=atom.occ,
                                                         b_iso=adptbx.u_as_b(
                                                             atom.b),
                                                         xyz=atom.xyz,
                                                         outlier=True)
                                self.partial_occ.append(outlier)
                                self.n_outliers += 1

                    # check that atoms in an atom group have the same occupancy
                    for atom_group in residue_group.atom_groups():
                        residue_is_okay = True
                        base_occupancy = atom_group.atoms()[0].occ
                        for atom in atom_group.atoms():
                            if (not approx_equal(
                                    base_occupancy, atom.occ, out=None,
                                    eps=1.0e-3)):
                                labels = atom.fetch_labels()
                                i_seqs = atom_group.atoms().extract_i_seq()
                                b_mean = adptbx.u_as_b(
                                    flex.mean(u_isos.select(i_seqs)))
                                outlier = residue_occupancy(
                                    chain_id=labels.chain_id,
                                    resseq=labels.resseq,
                                    icode=labels.icode,
                                    altloc=labels.altloc,
                                    resname=labels.resname,
                                    occupancy=occ,
                                    outlier=True,
                                    xyz=atom_group.atoms().extract_xyz().mean(
                                    ),
                                    b_iso=b_mean)
                                self.different_occ.append(outlier)
                                self.n_outliers += 1
                                residue_is_okay = False
                                break
                        if (not residue_is_okay):
                            break
Esempio n. 56
0
 def from_pdb(self):
     out = self.out
     params = self.params
     pdb_hierarchy = self.pdb_hierarchy
     pdb_sg, pdb_uc = None, None
     pdb_symm = self.pdb_in.crystal_symmetry()
     if (pdb_symm is not None):
         pdb_sg = pdb_symm.space_group_info()
         pdb_uc = pdb_symm.unit_cell()
     apply_sg = pdb_sg
     apply_uc = pdb_uc
     if (params.crystal_symmetry.space_group is not None):
         apply_sg = params.crystal_symmetry.space_group
     else:
         params.crystal_symmetry.space_group = pdb_sg
     if (params.crystal_symmetry.unit_cell is not None):
         apply_uc = params.crystal_symmetry.unit_cell
     else:
         params.crystal_symmetry.unit_cell = pdb_uc
     if (apply_sg is None) or (apply_uc is None):
         raise Sorry(
             "Incomplete symmetry information - please specify a space " +
             "group and unit cell for this structure.")
     from cctbx import crystal, adptbx
     from scitbx.array_family import flex
     apply_symm = crystal.symmetry(unit_cell=apply_uc,
                                   space_group_info=apply_sg)
     if (params.modify_pdb.remove_waters):
         print("  Removing solvent atoms...", file=out)
         for model in pdb_hierarchy.models():
             for chain in model.chains():
                 for residue_group in chain.residue_groups():
                     for atom_group in residue_group.atom_groups():
                         if (atom_group.resname in ["HOH", "WAT"]):
                             residue_group.remove_atom_group(
                                 atom_group=atom_group)
                     if (len(residue_group.atom_groups()) == 0):
                         chain.remove_residue_group(
                             residue_group=residue_group)
                 if (len(chain.atoms()) == 0):
                     model.remove_chain(chain=chain)
     if (params.modify_pdb.remove_alt_confs):
         print(
             "  Removing all alternate conformations and resetting occupancies...",
             file=out)
         from mmtbx import pdbtools
         pdbtools.remove_alt_confs(hierarchy=pdb_hierarchy)
     xray_structure = self.pdb_in.xray_structure_simple(
         crystal_symmetry=apply_symm)
     sctr_keys = xray_structure.scattering_type_registry().type_count_dict()
     hd_selection = xray_structure.hd_selection()
     if (not (("H" in sctr_keys) or ("D" in sctr_keys))):
         print("  WARNING: this model does not contain hydrogen atoms!",
               file=out)
         print("           strongly recommend running phenix.ready_set or",
               file=out)
         print("           equivalent to ensure realistic simulated data.",
               file=out)
         print("", file=out)
     if (params.modify_pdb.convert_to_isotropic):
         xray_structure.convert_to_isotropic()
     set_b = None
     if (params.modify_pdb.set_mean_b_iso is not None):
         assert (not params.modify_pdb.set_wilson_b)
         print("  Scaling B-factors to have mean of %.2f" % \
           params.modify_pdb.set_mean_b_iso, file=out)
         assert (params.modify_pdb.set_mean_b_iso > 0)
         set_b = params.modify_pdb.set_mean_b_iso
     elif (params.modify_pdb.set_wilson_b):
         print(
             "  Scaling B-factors to match mean Wilson B for this resolution",
             file=out)
         set_b = get_mean_statistic_for_resolution(d_min=params.d_min,
                                                   stat_type="wilson_b")
         print("", file=out)
     if (set_b is not None):
         u_iso = xray_structure.extract_u_iso_or_u_equiv()
         u_iso = u_iso.select(~hd_selection)
         u_mean = flex.mean(u_iso)
         b_mean = adptbx.u_as_b(u_mean)
         scale = set_b / b_mean
         xray_structure.scale_adps(scale)
         pdb_hierarchy.atoms().set_adps_from_scatterers(
             scatterers=xray_structure.scatterers(),
             unit_cell=xray_structure.unit_cell())
     import mmtbx.command_line.fmodel
     from mmtbx import utils
     fmodel_params = mmtbx.command_line.fmodel.fmodel_from_xray_structure_master_params.extract(
     )
     fmodel_params.high_resolution = params.d_min
     fake_data = params.fake_data_from_fmodel
     fmodel_params.fmodel = fake_data.fmodel
     if (fmodel_params.fmodel.b_sol == 0):
         print("  b_sol is zero - will use mean value for d_min +/- 0.2A",
               file=out)
         print("   (this is not strongly correlated with resolution, but",
               file=out)
         print(
             "    it is preferrable to use a real value instead of leaving",
             file=out)
         print("    it set to 0)", file=out)
         fmodel_params.fmodel.b_sol = 46.0
         print("", file=out)
     if (fmodel_params.fmodel.k_sol == 0):
         print("  k_sol is zero - will use mean value for d_min +/- 0.2A",
               file=out)
         print("   (this is not strongly correlated with resolution, but",
               file=out)
         print(
             "    it is preferrable to use a real value instead of leaving",
             file=out)
         print("     it set to 0)", file=out)
         fmodel_params.fmodel.k_sol = 0.35
         print("", file=out)
     fmodel_params.structure_factors_accuracy = fake_data.structure_factors_accuracy
     fmodel_params.mask = fake_data.mask
     fmodel_params.r_free_flags_fraction = params.r_free_flags.fraction
     fmodel_params.add_sigmas = False
     fmodel_params.output.type = "real"
     fmodel_ = utils.fmodel_from_xray_structure(
         xray_structure=xray_structure, params=fmodel_params)
     f_model = fmodel_.f_model
     r_free_flags = fmodel_.r_free_flags
     return (f_model, r_free_flags)
Esempio n. 57
0
def run_01():
    time_aniso_u_scaler = 0
    for symbol in sgtbx.bravais_types.acentric + sgtbx.bravais_types.centric:
        #print symbol, "-"*50
        space_group_info = sgtbx.space_group_info(symbol=symbol)
        xrs = random_structure.xray_structure(
            space_group_info=space_group_info,
            elements=["N"] * 100,
            volume_per_atom=50.0,
            random_u_iso=True)
        # XXX ad a method to adptbx to do this
        point_group = sgtbx.space_group_info(
            symbol=symbol).group().build_derived_point_group()
        adp_constraints = sgtbx.tensor_rank_2_constraints(
            space_group=point_group, reciprocal_space=True)
        u_star = adptbx.u_cart_as_u_star(
            xrs.unit_cell(), adptbx.random_u_cart(u_scale=1, u_min=0.1))
        u_indep = adp_constraints.independent_params(all_params=u_star)
        u_star = adp_constraints.all_params(independent_params=u_indep)
        b_cart_start = adptbx.u_as_b(
            adptbx.u_star_as_u_cart(xrs.unit_cell(), u_star))
        #
        tr = (b_cart_start[0] + b_cart_start[1] + b_cart_start[2]) / 3
        b_cart_start = [
            b_cart_start[0] - tr, b_cart_start[1] - tr, b_cart_start[2] - tr,
            b_cart_start[3], b_cart_start[4], b_cart_start[5]
        ]
        tr = (b_cart_start[0] + b_cart_start[1] + b_cart_start[2]) / 3
        #
        #print "Input b_cart :", " ".join(["%8.4f"%i for i in b_cart_start]), "tr:", tr
        F = xrs.structure_factors(d_min=2.0).f_calc()
        F = xrs.structure_factors(d_min=2.0).f_calc()
        u_star = adptbx.u_cart_as_u_star(F.unit_cell(),
                                         adptbx.b_as_u(b_cart_start))
        fbc = mmtbx.f_model.ext.k_anisotropic(F.indices(), u_star)
        fc = F.structure_factors_from_scatterers(xray_structure=xrs).f_calc()
        f_obs = F.customized_copy(data=flex.abs(fc.data() * fbc))
        #print bulk_solvent.r_factor(f_obs.data(), fmodel.f_model().data())
        obj = bulk_solvent.aniso_u_scaler(f_model_abs=flex.abs(fc.data()),
                                          f_obs=f_obs.data(),
                                          miller_indices=f_obs.indices(),
                                          unit_cell=f_obs.unit_cell())
        a = obj.a
        ####
        #print "Input a :", " ".join(["%7.3f"%i for i in a])
        overall_anisotropic_scale = mmtbx.f_model.ext.k_anisotropic(
            f_obs.indices(), a, f_obs.unit_cell())
        #print bulk_solvent.r_factor(f_obs.data(), fmodel.f_model().data()*overall_anisotropic_scale)
        f_obs = abs(fc)
        f_obs = f_obs.customized_copy(data=f_obs.data() *
                                      overall_anisotropic_scale)
        #print bulk_solvent.r_factor(f_obs.data(), fmodel.f_model().data())
        #print bulk_solvent.r_factor(f_obs.data(), fmodel.f_model().data())
        t0 = time.time()
        obj = bulk_solvent.aniso_u_scaler(f_model_abs=flex.abs(fc.data()),
                                          f_obs=f_obs.data(),
                                          miller_indices=f_obs.indices(),
                                          unit_cell=f_obs.unit_cell())
        time_aniso_u_scaler += (time.time() - t0)
        overall_anisotropic_scale = mmtbx.f_model.ext.k_anisotropic(
            f_obs.indices(), obj.a, f_obs.unit_cell())
        assert approx_equal(
            bulk_solvent.r_factor(f_obs.data(),
                                  fc.data() * overall_anisotropic_scale), 0.0,
            1.e-2)  # XXX seems to be low
        #print "Output a:", " ".join(["%7.3f"%i for i in obj.a])
        assert approx_equal(a, obj.a, 1.e-3)  # XXX can it be smaller?
    print("Time (aniso_u_scaler only): %6.4f" % time_aniso_u_scaler)
Esempio n. 58
0
 def _atom_radius(self):
   b_iso = adptbx.u_as_b(
     flex.mean(self.xray_structure.extract_u_iso_or_u_equiv()))
   o = maptbx.atom_curves(scattering_type="C", scattering_table="electron")
   return o.image(d_min=self.d_min, b_iso=b_iso,
     radius_max=max(15.,self.d_min), radius_step=0.01).radius
Esempio n. 59
0
 def prepare_for_minimization(self):
     rso = self.refine_sad_object
     rso.refine_variance_terms()
     self.refined_overall_b_iso = adptbx.u_as_b(
         rso.refine_sad_instance.get_refined_scaleU())
     rso.refine_sad_instance.set_scaleU(0.)
Esempio n. 60
0
def find_and_build_ions(manager,
                        fmodels,
                        model,
                        wavelength,
                        params,
                        nproc=1,
                        elements=Auto,
                        out=None,
                        run_ordered_solvent=False,
                        occupancy_strategy_enabled=False,
                        group_anomalous_strategy_enabled=False,
                        use_svm=None):
    """
  Analyzes the water molecules in a structure and re-labels them as ions if
  they scatter and bind environments that we expect of that ion.

  Parameters
  ----------
  manager : mmtbx.ions.identity.manager
  fmodels : mmtbx.fmodels
  model : mmtbx.model.manager
  wavelength : float
  params : libtbx.phil.scope_extract
  nproc : int, optional
  elements : list of str, optional
  out : file, optional
  run_ordered_solvent : bool, optional
  occupancy_strategy_enabled : bool, optional
  group_anomalous_strategy_enabled : bool, optional
  use_svm : bool, optional

  See Also
  --------
  mmtbx.ions.identify.manager.analyze_waters
  """
    import mmtbx.refinement.minimization
    from mmtbx.refinement.anomalous_scatterer_groups import \
      get_single_atom_selection_string
    from mmtbx.refinement import anomalous_scatterer_groups
    import mmtbx.ions.identify
    import mmtbx.ions.svm
    from cctbx.eltbx import sasaki
    from cctbx import crystal
    from cctbx import adptbx
    from cctbx import xray
    from scitbx.array_family import flex
    import scitbx.lbfgs
    if (use_svm is None):
        use_svm = getattr(params, "use_svm", False)
    assert (1.0 >= params.initial_occupancy >= 0)
    fmodel = fmodels.fmodel_xray()
    anomalous_flag = fmodel.f_obs().anomalous_flag()
    if (out is None): out = sys.stdout
    model.set_xray_structure(fmodel.xray_structure)
    model.get_xray_structure().tidy_us()
    pdb_hierarchy = model.get_hierarchy()
    pdb_atoms = pdb_hierarchy.atoms()
    pdb_atoms.reset_i_seq()
    # FIXME why does B for anisotropic waters end up negative?
    u_iso = model.get_xray_structure().extract_u_iso_or_u_equiv()
    for i_seq, atom in enumerate(pdb_atoms):
        labels = atom.fetch_labels()
        if (labels.resname == "HOH") and (atom.b < 0):
            assert (u_iso[i_seq] >= 0)
            atom.b = adptbx.u_as_b(u_iso[i_seq])
    if (manager is None):
        manager_class = None
        if (use_svm):
            manager_class = mmtbx.ions.svm.manager
            if params.svm.svm_name == "merged_high_res":
                params.find_anomalous_substructure = False
                params.use_phaser = False
        manager = mmtbx.ions.identify.create_manager(
            pdb_hierarchy=pdb_hierarchy,
            geometry_restraints_manager=model.restraints_manager.geometry,
            fmodel=fmodel,
            wavelength=wavelength,
            params=params,
            nproc=nproc,
            verbose=params.debug,
            log=out,
            manager_class=manager_class)
    else:
        grm = model.get_restraints_manager().geometry
        connectivity = grm.shell_sym_tables[0].full_simple_connectivity()
        manager.update_structure(pdb_hierarchy=pdb_hierarchy,
                                 xray_structure=fmodel.xray_structure,
                                 connectivity=connectivity,
                                 log=out)
        manager.update_maps()
    model.update_anomalous_groups(out=out)
    make_sub_header("Analyzing water molecules", out=out)
    manager.show_current_scattering_statistics(out=out)
    anomalous_groups = []
    # XXX somehow comma-separation of phil strings fields doesn't work
    if (isinstance(elements, list)) and (len(elements) == 1):
        elements = elements[0].split(",")
    water_ion_candidates = manager.analyze_waters(out=out, candidates=elements)
    modified_iselection = flex.size_t()
    default_b_iso = manager.get_initial_b_iso()
    # Build in the identified ions
    for_building = []
    if (use_svm):
        for result in water_ion_candidates:
            for_building.append((result.i_seq, result.final_choice))
    else:
        for i_seq, final_choices, two_fofc in water_ion_candidates:
            if (len(final_choices) == 1):
                for_building.append((i_seq, final_choices[0]))
    skipped = []
    if (len(for_building) > 0):
        make_sub_header("Adding %d ions to model" % len(for_building), out)
        for k, (i_seq, final_choice) in enumerate(for_building):
            atom = manager.pdb_atoms[i_seq]
            skip = False
            for other_i_seq, other_ion in for_building[:k]:
                if (other_i_seq in skipped): continue
                if (((other_ion.charge > 0) and (final_choice.charge > 0)) or
                    ((other_ion.charge < 0) and (final_choice.charge < 0))):
                    other_atom = manager.pdb_atoms[other_i_seq]
                    dxyz = atom.distance(other_atom)
                    if (dxyz < params.max_distance_between_like_charges):
                        print("  %s (%s%+d) is only %.3fA from %s (%s%+d), skipping for now" %\
                          (atom.id_str(), final_choice.element, final_choice.charge, dxyz,
                           other_atom.id_str(), other_ion.element, other_ion.charge), file=out)
                        skipped.append(i_seq)
                        skip = True
                        break
            if (skip): continue
            print("  %s becomes %s%+d" % \
                (atom.id_str(), final_choice.element, final_choice.charge), file=out)
            refine_adp = params.refine_ion_adp
            if (refine_adp == "Auto"):
                if (fmodel.f_obs().d_min() <= 1.5):
                    refine_adp = "anisotropic"
                elif (fmodel.f_obs().d_min() < 2.5):
                    atomic_number = sasaki.table(
                        final_choice.element).atomic_number()
                    if (atomic_number >= 19):
                        refine_adp = "anisotropic"
            # Modify the atom object - this is clumsy but they will be grouped into
            # a single chain at the end of refinement
            initial_b_iso = params.initial_b_iso
            if (initial_b_iso is Auto):
                initial_b_iso = manager.guess_b_iso_real(i_seq)
            element = final_choice.element
            if (element == "IOD"):  # FIXME
                element = "I"
            modified_atom = model.convert_atom(
                i_seq=i_seq,
                scattering_type=final_choice.scattering_type(),
                atom_name=element,
                element=element,
                charge=final_choice.charge,
                residue_name=final_choice.element,
                initial_occupancy=params.initial_occupancy,
                initial_b_iso=initial_b_iso,
                chain_id=params.ion_chain_id,
                segid="ION",
                refine_adp=refine_adp,
                refine_occupancies=False)  #params.refine_ion_occupancies)
            if (params.refine_anomalous) and (anomalous_flag):
                scatterer = model.get_xray_structure().scatterers()[i_seq]
                if (wavelength is not None):
                    fp_fdp_info = sasaki.table(
                        final_choice.element).at_angstrom(wavelength)
                    scatterer.fp = fp_fdp_info.fp()
                    scatterer.fdp = fp_fdp_info.fdp()
                    print("    setting f'=%g, f''=%g" %
                          (scatterer.fp, scatterer.fdp),
                          file=out)
                group = xray.anomalous_scatterer_group(
                    iselection=flex.size_t([i_seq]),
                    f_prime=scatterer.fp,
                    f_double_prime=scatterer.fdp,
                    refine=["f_prime", "f_double_prime"],
                    selection_string=get_single_atom_selection_string(
                        modified_atom),
                    update_from_selection=True)
                anomalous_groups.append(group)
            modified_iselection.append(i_seq)
    if (len(modified_iselection) > 0):
        scatterers = model.get_xray_structure().scatterers()
        # FIXME not sure this is actually working as desired...
        site_symmetry_table = model.get_xray_structure().site_symmetry_table()
        for i_seq in site_symmetry_table.special_position_indices():
            scatterers[i_seq].site = crystal.correct_special_position(
                crystal_symmetry=model.get_xray_structure(),
                special_op=site_symmetry_table.get(i_seq).special_op(),
                site_frac=scatterers[i_seq].site,
                site_label=scatterers[i_seq].label,
                tolerance=1.0)
        model.get_xray_structure().replace_scatterers(scatterers=scatterers)
        model.set_xray_structure(model.get_xray_structure())

        def show_r_factors():
            return "r_work=%6.4f r_free=%6.4f" % (fmodel.r_work(),
                                                  fmodel.r_free())

        fmodel.update_xray_structure(xray_structure=model.get_xray_structure(),
                                     update_f_calc=True,
                                     update_f_mask=True)
        n_anom = len(anomalous_groups)
        refine_anomalous = anomalous_flag and params.refine_anomalous and n_anom > 0
        refine_occupancies = (
            (params.refine_ion_occupancies or refine_anomalous)
            and ((not occupancy_strategy_enabled) or
                 (model.refinement_flags.s_occupancies is None) or
                 (len(model.refinement_flags.s_occupancies) == 0)))
        if (refine_anomalous):
            if (model.have_anomalous_scatterer_groups()
                    and (group_anomalous_strategy_enabled)):
                model.set_anomalous_scatterer_groups(
                    model.get_anomalous_scatterer_groups() + anomalous_groups)
                refine_anomalous = False
        if (refine_occupancies) or (refine_anomalous):
            print("", file=out)
            print("  occupancy refinement (new ions only): start %s" % \
              show_r_factors(), file=out)
            fmodel.xray_structure.scatterers().flags_set_grads(state=False)
            fmodel.xray_structure.scatterers().flags_set_grad_occupancy(
                iselection=modified_iselection)
            lbfgs_termination_params = scitbx.lbfgs.termination_parameters(
                max_iterations=25)
            minimized = mmtbx.refinement.minimization.lbfgs(
                restraints_manager=None,
                fmodels=fmodels,
                model=model,
                is_neutron_scat_table=False,
                lbfgs_termination_params=lbfgs_termination_params)
            fmodel.xray_structure.adjust_occupancy(
                occ_max=1.0, occ_min=0, selection=modified_iselection)
            zero_occ = []
            for i_seq in modified_iselection:
                occ = fmodel.xray_structure.scatterers()[i_seq].occupancy
                if (occ == 0):
                    zero_occ.append(i_seq)
            fmodel.update_xray_structure(update_f_calc=True,
                                         update_f_mask=True)
            print("                                        final %s" % \
              show_r_factors(), file=out)
            if (len(zero_occ) > 0):
                print("  WARNING: occupancy dropped to zero for %d atoms:",
                      file=out)
                atoms = model.get_atoms()
                for i_seq in zero_occ:
                    print("    %s" % atoms[i_seq].id_str(suppress_segid=True),
                          file=out)
            print("", file=out)
        if (refine_anomalous):
            assert fmodel.f_obs().anomalous_flag()
            print("  anomalous refinement (new ions only): start %s" % \
              show_r_factors(), file=out)
            fmodel.update(target_name="ls")
            anomalous_scatterer_groups.minimizer(fmodel=fmodel,
                                                 groups=anomalous_groups)
            fmodel.update(target_name="ml")
            print("                                        final %s" % \
              show_r_factors(), file=out)
            print("", file=out)
    return manager