def refine_window(O, window):
     processed_pdb_file = O.get_processed_pdb_file()
     assert (processed_pdb_file is not None)
     hierarchy = processed_pdb_file.all_chain_proxies.pdb_hierarchy
     pdb_atoms = hierarchy.atoms()
     sites_cart = pdb_atoms.extract_xyz()
     dxyz = [None] * 3
     dxyz[0] = flex.vec3_double(pdb_atoms.size(), (1.0, 0.2, 0.3))
     dxyz[1] = flex.vec3_double(pdb_atoms.size(), (-1.0, -0.1, 0.0))
     dxyz[2] = dxyz[1]
     trials = []
     if (window.residue_id_str in [" A PHE 113 ", " A SER 110 "]):
         for i in range(3):
             trials.append(
                 alt_confs.trial_result(
                     sites_cart=(sites_cart + dxyz[i]).select(
                         window.selection),
                     min_fofc=4.0,
                     mean_fofc=4.5,
                     rmsd=1.2,
                     max_dev=1.3,
                     cc=0.99))
     elif (window.residue_id_str in [" A ASN 108 "]):
         trials.append(
             alt_confs.trial_result(sites_cart=(sites_cart +
                                                dxyz[0]).select(
                                                    window.selection),
                                    min_fofc=4.0,
                                    mean_fofc=4.5,
                                    rmsd=1.2,
                                    max_dev=1.3,
                                    cc=0.99))
     e = sliding_window.ensemble(window=window, sites_trials=trials)
     n_keep = e.filter_trials(sites_cart=O.sites_cart,
                              min_rmsd=O.params.min_rmsd,
                              min_dev=O.min_required_deviation)
     if (n_keep > 0):
         return e
     return None
 def refine_window (O, window) :
   processed_pdb_file = O.get_processed_pdb_file()
   assert (processed_pdb_file is not None)
   hierarchy = processed_pdb_file.all_chain_proxies.pdb_hierarchy
   pdb_atoms = hierarchy.atoms()
   sites_cart = pdb_atoms.extract_xyz()
   dxyz = [ None ] * 3
   dxyz[0] = flex.vec3_double(pdb_atoms.size(), (1.0,0.2,0.3))
   dxyz[1] = flex.vec3_double(pdb_atoms.size(), (-1.0,-0.1,0.0))
   dxyz[2] = dxyz[1]
   trials = []
   if (window.residue_id_str in [" A PHE 113 ", " A SER 110 "]) :
     for i in range(3) :
       trials.append(alt_confs.trial_result(
         sites_cart=(sites_cart + dxyz[i]).select(window.selection),
         min_fofc=4.0,
         mean_fofc=4.5,
         rmsd=1.2,
         max_dev=1.3,
         cc=0.99))
   elif (window.residue_id_str in [" A ASN 108 "]) :
     trials.append(alt_confs.trial_result(
       sites_cart=(sites_cart + dxyz[0]).select(window.selection),
       min_fofc=4.0,
       mean_fofc=4.5,
       rmsd=1.2,
       max_dev=1.3,
       cc=0.99))
   e = sliding_window.ensemble(window=window, sites_trials=trials)
   n_keep = e.filter_trials(
     sites_cart=O.sites_cart,
     min_rmsd=O.params.min_rmsd,
     min_dev=O.min_required_deviation)
   if (n_keep > 0) :
     return e
   return None
 def run_trial(self, occ):
   """
   Actually run the refinement - called in parallel via easy_mp.pool_map
   """
   from scitbx.array_family import flex
   seed = int(time.time() / os.getpid())
   random.seed(seed)
   flex.set_random_seed(seed)
   fmodel = self.fmodel
   selection = self.selection.deep_copy()
   iselection = self.iselection.deep_copy()
   d_min = fmodel.f_obs().d_min()
   xrs = fmodel.xray_structure.deep_copy_scatterers()
   hd_sel = xrs.hd_selection()
   sites_start = xrs.sites_cart().deep_copy()
   assert (len(selection) == len(sites_start))
   fmodel.update_xray_structure(xrs, update_f_calc=True)
   sites_start_selected = sites_start.select(iselection)
   occ_start = xrs.scatterers().extract_occupancies().deep_copy()
   u_start = xrs.extract_u_cart_plus_u_iso()
   if (self.params.adjust_b_factors_if_poor_density):
     fofc_map = fmodel.map_coefficients(
       map_type="mFo-DFc",
       exclude_free_r_reflections=True).fft_map(
         resolution_factor=1/4.).apply_sigma_scaling().real_map_unpadded()
     sites_frac = xrs.sites_frac()
     b_scale_isel = flex.size_t()
     for i_seq in iselection :
       map_value = fofc_map.tricubic_interpolation(sites_frac[i_seq])
       if (map_value < -2.5):
         b_scale_isel.append(i_seq)
     if (len(b_scale_isel) > 0):
       b_scale_sel = flex.bool(sites_frac.size(), False).set_selected(
         b_scale_isel, True)
       xrs.scale_adp(factor=0.75, selection=b_scale_sel)
   nearby_water_selection = mmtbx.building.get_nearby_water_selection(
     pdb_hierarchy=self.pdb_hierarchy,
     xray_structure=xrs,
     selection=selection)
   two_fofc_map, fofc_map = alt_confs.get_partial_omit_map(
     fmodel=fmodel,
     selection=iselection,
     selection_delete=None,#nearby_water_selection,
     negate_surrounding=self.params.negate_surrounding,
     partial_occupancy=occ)
   #xrs.set_occupancies(occ_start)
   make_sub_header("Simulated annealing into mFo-DFc map", out=self.out)
   if (self.params.shake_sites is not None):
     xrs.shake_sites_in_place(self.params.shake_sites, selection=selection)
   sites_new = mmtbx.building.run_real_space_annealing(
     xray_structure=xrs,
     pdb_hierarchy=self.pdb_hierarchy,
     processed_pdb_file=self.processed_pdb_file,
     selection=selection,
     target_map=fofc_map,
     d_min=d_min,
     params=self.params.simulated_annealing,
     #wc=5, # FIXME why does this need to be scaled?
     target_map_rsr=two_fofc_map,
     rsr_after_anneal=self.params.rsr_after_anneal,
     out=self.out,
     debug=True)
   # now compute CC of refined sites to difference map
   fmodel.update_xray_structure(xrs, update_f_calc=True)
   fc_coeffs = fmodel.f_model()
   fc_fft_map = fc_coeffs.fft_map(resolution_factor=1/4.)
   fc_map = fc_fft_map.apply_sigma_scaling().real_map_unpadded()
   pdb_atoms = self.pdb_hierarchy.atoms()
   # XXX should this only calculate statistics for the central atoms?
   map_stats = mmtbx.building.get_model_map_stats(
     selection=self.selection_score,
     target_map=fofc_map,
     model_map=fc_map,
     unit_cell=xrs.unit_cell(),
     sites_cart=sites_new,
     pdb_atoms=pdb_atoms,
     local_sampling=False)
   # reset xray structure
   xrs.set_sites_cart(sites_start)
   xrs.set_u_cart(u_start)
   fmodel.update_xray_structure(xrs, update_f_calc=True)
   # we may only want the rmsd and max. dev. from a subset of sites, e.g.
   # the central residue of a sliding window (minus hydrogens)
   selection_score = self.selection_score.deep_copy()
   if (type(selection_score).__name__ != 'bool'):
     selection_score = flex.bool(hd_sel.size(), False).set_selected(
       self.selection_score, True)
   selection_score &= ~hd_sel
   site_stats = alt_confs.coord_stats_with_flips(
     sites1=sites_start.select(selection_score),
     sites2=sites_new.select(selection_score),
     atoms=self.pdb_hierarchy.atoms().select(selection_score))
   return alt_confs.trial_result(
     sites_cart=sites_new.select(self.iselection),
     min_fofc=map_stats.min,
     mean_fofc=map_stats.mean,
     rmsd=site_stats.rmsd,
     max_dev=site_stats.max_dev,
     cc=map_stats.cc)
 def run_trial (self, occ) :
   """
   Actually run the refinement - called in parallel via easy_mp.pool_map
   """
   from scitbx.array_family import flex
   seed = int(time.time() / os.getpid())
   random.seed(seed)
   flex.set_random_seed(seed)
   fmodel = self.fmodel
   selection = self.selection.deep_copy()
   iselection = self.iselection.deep_copy()
   d_min = fmodel.f_obs().d_min()
   xrs = fmodel.xray_structure.deep_copy_scatterers()
   hd_sel = xrs.hd_selection()
   sites_start = xrs.sites_cart().deep_copy()
   assert (len(selection) == len(sites_start))
   fmodel.update_xray_structure(xrs, update_f_calc=True)
   sites_start_selected = sites_start.select(iselection)
   occ_start = xrs.scatterers().extract_occupancies().deep_copy()
   u_start = xrs.extract_u_cart_plus_u_iso()
   if (self.params.adjust_b_factors_if_poor_density) :
     fofc_map = fmodel.map_coefficients(
       map_type="mFo-DFc",
       exclude_free_r_reflections=True).fft_map(
         resolution_factor=1/4.).apply_sigma_scaling().real_map_unpadded()
     sites_frac = xrs.sites_frac()
     b_scale_isel = flex.size_t()
     for i_seq in iselection :
       map_value = fofc_map.tricubic_interpolation(sites_frac[i_seq])
       if (map_value < -2.5) :
         b_scale_isel.append(i_seq)
     if (len(b_scale_isel) > 0) :
       b_scale_sel = flex.bool(sites_frac.size(), False).set_selected(
         b_scale_isel, True)
       xrs.scale_adp(factor=0.75, selection=b_scale_sel)
   nearby_water_selection = mmtbx.building.get_nearby_water_selection(
     pdb_hierarchy=self.pdb_hierarchy,
     xray_structure=xrs,
     selection=selection)
   two_fofc_map, fofc_map = alt_confs.get_partial_omit_map(
     fmodel=fmodel,
     selection=iselection,
     selection_delete=None,#nearby_water_selection,
     negate_surrounding=self.params.negate_surrounding,
     partial_occupancy=occ)
   #xrs.set_occupancies(occ_start)
   make_sub_header("Simulated annealing into mFo-DFc map", out=self.out)
   if (self.params.shake_sites is not None) :
     xrs.shake_sites_in_place(self.params.shake_sites, selection=selection)
   sites_new = mmtbx.building.run_real_space_annealing(
     xray_structure=xrs,
     pdb_hierarchy=self.pdb_hierarchy,
     processed_pdb_file=self.processed_pdb_file,
     selection=selection,
     target_map=fofc_map,
     d_min=d_min,
     params=self.params.simulated_annealing,
     #wc=5, # FIXME why does this need to be scaled?
     target_map_rsr=two_fofc_map,
     rsr_after_anneal=self.params.rsr_after_anneal,
     out=self.out,
     debug=True)
   # now compute CC of refined sites to difference map
   fmodel.update_xray_structure(xrs, update_f_calc=True)
   fc_coeffs = fmodel.f_model()
   fc_fft_map = fc_coeffs.fft_map(resolution_factor=1/4.)
   fc_map = fc_fft_map.apply_sigma_scaling().real_map_unpadded()
   pdb_atoms = self.pdb_hierarchy.atoms()
   # XXX should this only calculate statistics for the central atoms?
   map_stats = mmtbx.building.get_model_map_stats(
     selection=self.selection_score,
     target_map=fofc_map,
     model_map=fc_map,
     unit_cell=xrs.unit_cell(),
     sites_cart=sites_new,
     pdb_atoms=pdb_atoms,
     local_sampling=False)
   # reset xray structure
   xrs.set_sites_cart(sites_start)
   xrs.set_u_cart(u_start)
   fmodel.update_xray_structure(xrs, update_f_calc=True)
   # we may only want the rmsd and max. dev. from a subset of sites, e.g.
   # the central residue of a sliding window (minus hydrogens)
   selection_score = self.selection_score.deep_copy()
   if (type(selection_score).__name__ != 'bool') :
     selection_score = flex.bool(hd_sel.size(), False).set_selected(
       self.selection_score, True)
   selection_score &= ~hd_sel
   site_stats = alt_confs.coord_stats_with_flips(
     sites1=sites_start.select(selection_score),
     sites2=sites_new.select(selection_score),
     atoms=self.pdb_hierarchy.atoms().select(selection_score))
   return alt_confs.trial_result(
     sites_cart=sites_new.select(self.iselection),
     min_fofc=map_stats.min,
     mean_fofc=map_stats.mean,
     rmsd=site_stats.rmsd,
     max_dev=site_stats.max_dev,
     cc=map_stats.cc)