def __call__ (self, fragment) :
   from scitbx.array_family import flex
   frag_selection = flex.bool(self.pdb_atoms.size(), fragment)
   sites_cart_orig = self.fmodel.xray_structure.sites_cart()
   sites_start = sites_cart_orig.select(fragment)
   atom_start = self.pdb_atoms[fragment[0]].fetch_labels()
   atom_end = self.pdb_atoms[fragment[-1]].fetch_labels()
   label = "%s%s-%s" % (atom_start.chain_id, atom_start.resid(),
     atom_end.resid())
   # XXX should the partial occupancy be determined automatically?
   two_fofc_map, fofc_map = alt_confs.get_partial_omit_map(
     fmodel=self.fmodel.deep_copy(),
     selection=(frag_selection & self.sele_main_conf),
     selection_delete=(frag_selection & ~(self.sele_main_conf)),
     partial_occupancy=0.6)
   target_map = two_fofc_map
   if (self.rsr_fofc_map_target) :
     target_map = fofc_map
   box = self.make_box(selection=frag_selection, target_map=target_map)
   box.restrain_atoms(
     selection=alt_confs.SELECTION_OLD,
     reference_sigma=0.02,
     reset_first=True)
   box.restrain_atoms(
     selection=alt_confs.SELECTION_NEW_REBUILT,
     reference_sigma=0.05,
     reset_first=False)
   # first fix the geometry of adjacent residues
   box.real_space_refine(alt_confs.SELECTION_NEW_SPLIT)
   # now the entire B conformer
   box.real_space_refine(alt_confs.SELECTION_NEW)
   sites_cart_refined = box.update_original_coordinates()
   sites_new = sites_cart_refined.select(fragment)
   self.fmodel.xray_structure.set_sites_cart(sites_cart_orig)
   self.pdb_atoms.set_xyz(sites_cart_orig)
   return alt_confs.refined_fragment(
     label=label,
     selection=frag_selection,
     sites_cart=sites_cart_refined,
     rmsd=sites_new.rms_difference(sites_start))
 def __call__(self, fragment):
     from scitbx.array_family import flex
     frag_selection = flex.bool(self.pdb_atoms.size(), fragment)
     sites_cart_orig = self.fmodel.xray_structure.sites_cart()
     sites_start = sites_cart_orig.select(fragment)
     atom_start = self.pdb_atoms[fragment[0]].fetch_labels()
     atom_end = self.pdb_atoms[fragment[-1]].fetch_labels()
     label = "%s%s-%s" % (atom_start.chain_id, atom_start.resid(),
                          atom_end.resid())
     # XXX should the partial occupancy be determined automatically?
     two_fofc_map, fofc_map = alt_confs.get_partial_omit_map(
         fmodel=self.fmodel.deep_copy(),
         selection=(frag_selection & self.sele_main_conf),
         selection_delete=(frag_selection & ~(self.sele_main_conf)),
         partial_occupancy=0.6)
     target_map = two_fofc_map
     if (self.rsr_fofc_map_target):
         target_map = fofc_map
     box = self.make_box(selection=frag_selection, target_map=target_map)
     box.restrain_atoms(selection=alt_confs.SELECTION_OLD,
                        reference_sigma=0.02,
                        reset_first=True)
     box.restrain_atoms(selection=alt_confs.SELECTION_NEW_REBUILT,
                        reference_sigma=0.05,
                        reset_first=False)
     # first fix the geometry of adjacent residues
     box.real_space_refine(alt_confs.SELECTION_NEW_SPLIT)
     # now the entire B conformer
     box.real_space_refine(alt_confs.SELECTION_NEW)
     sites_cart_refined = box.update_original_coordinates()
     sites_new = sites_cart_refined.select(fragment)
     self.fmodel.xray_structure.set_sites_cart(sites_cart_orig)
     self.pdb_atoms.set_xyz(sites_cart_orig)
     return alt_confs.refined_fragment(
         label=label,
         selection=frag_selection,
         sites_cart=sites_cart_refined,
         rmsd=sites_new.rms_difference(sites_start))
def run(args, out=sys.stdout):
    from mmtbx.building import alternate_conformations
    import mmtbx.command_line
    import mmtbx.building
    import iotbx.pdb.hierarchy
    cmdline = mmtbx.command_line.load_model_and_data(
        args=args,
        master_phil=get_master_phil(),
        process_pdb_file=True,
        create_fmodel=True,
        out=out,
        usage_string="""\
mmtbx.generate_disorder model.pdb data.mtz selection="resname ATP" [occ=0.6]

Perform simulatead annealing against an mFo-DFc map to generate possible
alternate conformations for a selection of atoms.  For development purposes
and experimentation only.
""")
    params = cmdline.params
    fmodel = cmdline.fmodel
    validate_params(params)
    pdb_hierarchy = cmdline.pdb_hierarchy
    make_header("Generating disorder", out=out)
    a_c_p = cmdline.processed_pdb_file.all_chain_proxies
    selection = a_c_p.selection(params.selection)
    if (params.whole_residues):
        selection = iotbx.pdb.atom_selection.expand_selection_to_entire_atom_groups(
            selection=selection, pdb_atoms=pdb_hierarchy.atoms())
    n_sel = selection.count(True)
    assert (n_sel > 0)
    print >> out, "%d atoms selected" % n_sel
    selection_delete = None
    if (params.selection_delete is not None):
        selection_delete = a_c_p.selection(params.selection_delete)
    two_fofc_map, fofc_map = alternate_conformations.get_partial_omit_map(
        fmodel=fmodel.deep_copy(),
        selection=selection,
        selection_delete=selection_delete,
        negate_surrounding=params.negate_surrounding_sites,
        map_file_name=params.output.map_file_name,
        partial_occupancy=params.occ,
        resolution_factor=params.resolution_factor)
    target_map = fofc_map
    if (params.target_map == "2mFo-DFc"):
        target_map = two_fofc_map
    annealer = annealing_manager(xray_structure=fmodel.xray_structure,
                                 pdb_hierarchy=pdb_hierarchy,
                                 processed_pdb_file=cmdline.processed_pdb_file,
                                 target_map=target_map,
                                 two_fofc_map=two_fofc_map,
                                 d_min=fmodel.f_obs().d_min(),
                                 params=params,
                                 selection=selection,
                                 resolution_factor=params.resolution_factor,
                                 out=out,
                                 debug=params.output.debug)
    sites_ref = pdb_hierarchy.atoms().extract_xyz().deep_copy()
    sites_all = easy_mp.pool_map(fixed_func=annealer,
                                 iterable=range(params.n_confs),
                                 processes=params.nproc)
    ensemble = iotbx.pdb.hierarchy.root()
    if (params.output.include_starting_model):
        sites_all.insert(0, sites_ref)
    rmsds = []
    for i_conf, sites_new in enumerate(sites_all):
        assert (sites_new is not None)
        model = pdb_hierarchy.only_model().detached_copy()
        model.atoms().set_xyz(sites_new)
        model.id = str(i_conf + 1)
        rmsd = sites_new.select(selection).rms_difference(
            sites_ref.select(selection))
        print >> out, "Model %d: rmsd=%.3f" % (i_conf + 1, rmsd)
        rmsds.append(rmsd)
        ensemble.append_model(model)
    f = open(params.output.file_name, "w")
    f.write(ensemble.as_pdb_string(crystal_symmetry=fmodel.xray_structure))
    f.close()
    print >> out, "Wrote ensemble model to %s" % params.output.file_name
    return rmsds
Beispiel #4
0
def exercise_utils():
    #--- coord_stats_with_flips
    pdb_1 = iotbx.pdb.hierarchy.input(pdb_string="""\
ATOM   1639  N   PHE A 113      18.514  41.994  54.886  1.00 12.36           N
ATOM   1640  CA  PHE A 113      19.737  42.742  54.898  1.00 12.97           C
ATOM   1641  C   PHE A 113      19.514  44.206  55.111  1.00 11.88           C
ATOM   1642  O   PHE A 113      18.442  44.758  54.823  1.00 13.75           O
ATOM   1643  CB  PHE A 113      20.587  42.460  53.684  1.00 20.08           C
ATOM   1644  CG  PHE A 113      19.893  42.692  52.399  1.00 17.15           C
ATOM   1645  CD1 PHE A 113      19.182  41.689  51.798  1.00 20.34           C
ATOM   1646  CD2 PHE A 113      20.029  43.895  51.745  1.00 21.22           C
ATOM   1647  CE1 PHE A 113      18.577  41.887  50.601  1.00 23.49           C
ATOM   1648  CE2 PHE A 113      19.411  44.105  50.542  1.00 24.87           C
ATOM   1649  CZ  PHE A 113      18.675  43.097  49.976  1.00 21.81           C
ATOM   1650  H   PHE A 113      17.875  42.342  54.427  1.00 14.83           H
ATOM   1651  HA  PHE A 113      20.250  42.435  55.661  1.00 15.57           H
ATOM   1652  HB2 PHE A 113      21.367  43.036  53.708  1.00 24.10           H
ATOM   1653  HB3 PHE A 113      20.865  41.531  53.707  1.00 24.10           H
ATOM   1654  HD1 PHE A 113      19.099  40.866  52.224  1.00 24.40           H
ATOM   1655  HD2 PHE A 113      20.518  44.581  52.138  1.00 25.47           H
ATOM   1656  HE1 PHE A 113      18.077  41.204  50.215  1.00 28.18           H
ATOM   1657  HE2 PHE A 113      19.488  44.927  50.113  1.00 29.84           H
ATOM   1658  HZ  PHE A 113      18.261  43.230  49.154  1.00 26.17           H
""")
    pdb_2 = iotbx.pdb.hierarchy.input(pdb_string="""\
ATOM   1639  N   PHE A 113      18.514  41.994  54.886  1.00 12.36           N
ATOM   1640  CA  PHE A 113      19.737  42.742  54.898  1.00 12.97           C
ATOM   1641  C   PHE A 113      19.514  44.206  55.111  1.00 11.88           C
ATOM   1642  O   PHE A 113      18.442  44.758  54.823  1.00 13.75           O
ATOM   1643  CB  PHE A 113      20.587  42.460  53.684  1.00 20.08           C
ATOM   1644  CG  PHE A 113      19.893  42.692  52.399  1.00 17.15           C
ATOM   1645  CD1 PHE A 113      20.174  43.797  51.643  1.00 20.34           C
ATOM   1646  CD2 PHE A 113      18.888  41.845  51.991  1.00 21.22           C
ATOM   1647  CE1 PHE A 113      19.512  44.033  50.483  1.00 23.49           C
ATOM   1648  CE2 PHE A 113      18.224  42.071  50.816  1.00 24.87           C
ATOM   1649  CZ  PHE A 113      18.548  43.166  50.057  1.00 21.81           C
ATOM   1650  H   PHE A 113      17.875  42.342  54.427  1.00 14.83           H
ATOM   1651  HA  PHE A 113      20.250  42.435  55.661  1.00 15.57           H
ATOM   1652  HB2 PHE A 113      21.367  43.036  53.708  1.00 24.10           H
ATOM   1653  HB3 PHE A 113      20.865  41.531  53.707  1.00 24.10           H
ATOM   1654  HD1 PHE A 113      20.840  44.386  51.919  1.00 24.40           H
ATOM   1655  HD2 PHE A 113      18.681  41.096  52.501  1.00 25.47           H
ATOM   1656  HE1 PHE A 113      19.730  44.776  49.967  1.00 28.18           H
ATOM   1657  HE2 PHE A 113      17.560  41.484  50.533  1.00 29.84           H
ATOM   1658  HZ  PHE A 113      18.093  43.330  49.263  1.00 26.17           H
""")
    hierarchy_1 = pdb_1.hierarchy
    hierarchy_2 = pdb_2.hierarchy
    pdb_atoms_1 = hierarchy_1.atoms()
    sites_1 = pdb_atoms_1.extract_xyz()
    sites_2 = hierarchy_2.atoms().extract_xyz()
    result = alternate_conformations.coord_stats_with_flips(
        sites_1, sites_2, pdb_atoms_1)
    assert (approx_equal(result.rmsd, 0.2, eps=0.001))
    assert (approx_equal(result.max_dev, 0.452427, eps=0.00001))
    #--- get_selection_gap
    assert (alternate_conformations.get_selection_gap([1, 2, 3, 4, 5],
                                                      [6, 7, 8, 9]) == 0)
    assert (alternate_conformations.get_selection_gap([6, 7],
                                                      [1, 2, 3, 4, 5]) == 0)
    assert (alternate_conformations.get_selection_gap([1, 2, 3, 4, 5],
                                                      [7, 8, 9]) == 1)
    assert (alternate_conformations.get_selection_gap([1, 2, 3, 4, 5],
                                                      [4, 5, 7, 8, 9]) == -2)
    assert (alternate_conformations.get_selection_gap([4, 5, 7, 8, 9],
                                                      [1, 2, 3, 4]) == -1)
    #--- score_rotamers
    n_outliers = alternate_conformations.score_rotamers(
        hierarchy_2, flex.bool(sites_2.size(), True))
    assert (n_outliers == 0)
    #--- get_partial_omit_map
    xrs = pdb_1.input.xray_structure_simple()
    f_calc = abs(xrs.structure_factors(d_min=1.5).f_calc())
    f_calc.set_observation_type_xray_amplitude()
    flags = f_calc.generate_r_free_flags()
    fmodel = mmtbx.utils.fmodel_simple(xray_structures=[xrs],
                                       f_obs=f_calc,
                                       r_free_flags=flags,
                                       scattering_table="n_gaussian",
                                       bulk_solvent_and_scaling=False,
                                       skip_twin_detection=True)
    sel = pdb_1.hierarchy.atom_selection_cache().selection("name CZ")
    assert (sel.count(True) == 1)
    two_fofc_map, fofc_map = alternate_conformations.get_partial_omit_map(
        fmodel=fmodel, selection=sel)
    site = xrs.sites_frac()[sel.iselection()[0]]  # CZ coordinate
    assert (fofc_map.tricubic_interpolation(site) > 20)
 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 (args, out=sys.stdout) :
  from mmtbx.building import alternate_conformations
  import mmtbx.command_line
  import mmtbx.building
  import iotbx.pdb.hierarchy
  cmdline = mmtbx.command_line.load_model_and_data(
    args=args,
    master_phil=get_master_phil(),
    process_pdb_file=True,
    create_fmodel=True,
    out=out,
    usage_string="""\
mmtbx.generate_disorder model.pdb data.mtz selection="resname ATP" [occ=0.6]

Perform simulatead annealing against an mFo-DFc map to generate possible
alternate conformations for a selection of atoms.  For development purposes
and experimentation only.
""")
  params = cmdline.params
  fmodel = cmdline.fmodel
  validate_params(params)
  pdb_hierarchy = cmdline.pdb_hierarchy
  make_header("Generating disorder", out=out)
  a_c_p = cmdline.processed_pdb_file.all_chain_proxies
  selection = a_c_p.selection(params.selection)
  if (params.whole_residues) :
    selection = iotbx.pdb.atom_selection.expand_selection_to_entire_atom_groups(
      selection=selection,
      pdb_atoms=pdb_hierarchy.atoms())
  n_sel = selection.count(True)
  assert (n_sel > 0)
  print >> out, "%d atoms selected" % n_sel
  selection_delete = None
  if (params.selection_delete is not None) :
    selection_delete = a_c_p.selection(params.selection_delete)
  two_fofc_map, fofc_map = alternate_conformations.get_partial_omit_map(
    fmodel=fmodel.deep_copy(),
    selection=selection,
    selection_delete=selection_delete,
    negate_surrounding=params.negate_surrounding_sites,
    map_file_name=params.output.map_file_name,
    partial_occupancy=params.occ,
    resolution_factor=params.resolution_factor)
  target_map = fofc_map
  if (params.target_map == "2mFo-DFc") :
    target_map = two_fofc_map
  annealer = annealing_manager(
    xray_structure=fmodel.xray_structure,
    pdb_hierarchy=pdb_hierarchy,
    processed_pdb_file=cmdline.processed_pdb_file,
    target_map=target_map,
    two_fofc_map=two_fofc_map,
    d_min=fmodel.f_obs().d_min(),
    params=params,
    selection=selection,
    resolution_factor=params.resolution_factor,
    out=out,
    debug=params.output.debug)
  sites_ref = pdb_hierarchy.atoms().extract_xyz().deep_copy()
  sites_all = easy_mp.pool_map(
    fixed_func=annealer,
    iterable=range(params.n_confs),
    processes=params.nproc)
  ensemble = iotbx.pdb.hierarchy.root()
  if (params.output.include_starting_model) :
    sites_all.insert(0, sites_ref)
  rmsds = []
  for i_conf, sites_new in enumerate(sites_all) :
    assert (sites_new is not None)
    model = pdb_hierarchy.only_model().detached_copy()
    model.atoms().set_xyz(sites_new)
    model.id = str(i_conf+1)
    rmsd = sites_new.select(selection).rms_difference(
      sites_ref.select(selection))
    print >> out, "Model %d: rmsd=%.3f" % (i_conf+1, rmsd)
    rmsds.append(rmsd)
    ensemble.append_model(model)
  f = open(params.output.file_name, "w")
  f.write(ensemble.as_pdb_string(
    crystal_symmetry=fmodel.xray_structure))
  f.close()
  print >> out, "Wrote ensemble model to %s" % params.output.file_name
  return rmsds
Beispiel #7
0
def exercise_utils () :
  #--- coord_stats_with_flips
  pdb_1 = iotbx.pdb.hierarchy.input(pdb_string="""\
ATOM   1639  N   PHE A 113      18.514  41.994  54.886  1.00 12.36           N
ATOM   1640  CA  PHE A 113      19.737  42.742  54.898  1.00 12.97           C
ATOM   1641  C   PHE A 113      19.514  44.206  55.111  1.00 11.88           C
ATOM   1642  O   PHE A 113      18.442  44.758  54.823  1.00 13.75           O
ATOM   1643  CB  PHE A 113      20.587  42.460  53.684  1.00 20.08           C
ATOM   1644  CG  PHE A 113      19.893  42.692  52.399  1.00 17.15           C
ATOM   1645  CD1 PHE A 113      19.182  41.689  51.798  1.00 20.34           C
ATOM   1646  CD2 PHE A 113      20.029  43.895  51.745  1.00 21.22           C
ATOM   1647  CE1 PHE A 113      18.577  41.887  50.601  1.00 23.49           C
ATOM   1648  CE2 PHE A 113      19.411  44.105  50.542  1.00 24.87           C
ATOM   1649  CZ  PHE A 113      18.675  43.097  49.976  1.00 21.81           C
ATOM   1650  H   PHE A 113      17.875  42.342  54.427  1.00 14.83           H
ATOM   1651  HA  PHE A 113      20.250  42.435  55.661  1.00 15.57           H
ATOM   1652  HB2 PHE A 113      21.367  43.036  53.708  1.00 24.10           H
ATOM   1653  HB3 PHE A 113      20.865  41.531  53.707  1.00 24.10           H
ATOM   1654  HD1 PHE A 113      19.099  40.866  52.224  1.00 24.40           H
ATOM   1655  HD2 PHE A 113      20.518  44.581  52.138  1.00 25.47           H
ATOM   1656  HE1 PHE A 113      18.077  41.204  50.215  1.00 28.18           H
ATOM   1657  HE2 PHE A 113      19.488  44.927  50.113  1.00 29.84           H
ATOM   1658  HZ  PHE A 113      18.261  43.230  49.154  1.00 26.17           H
""")
  pdb_2 = iotbx.pdb.hierarchy.input(pdb_string="""\
ATOM   1639  N   PHE A 113      18.514  41.994  54.886  1.00 12.36           N
ATOM   1640  CA  PHE A 113      19.737  42.742  54.898  1.00 12.97           C
ATOM   1641  C   PHE A 113      19.514  44.206  55.111  1.00 11.88           C
ATOM   1642  O   PHE A 113      18.442  44.758  54.823  1.00 13.75           O
ATOM   1643  CB  PHE A 113      20.587  42.460  53.684  1.00 20.08           C
ATOM   1644  CG  PHE A 113      19.893  42.692  52.399  1.00 17.15           C
ATOM   1645  CD1 PHE A 113      20.174  43.797  51.643  1.00 20.34           C
ATOM   1646  CD2 PHE A 113      18.888  41.845  51.991  1.00 21.22           C
ATOM   1647  CE1 PHE A 113      19.512  44.033  50.483  1.00 23.49           C
ATOM   1648  CE2 PHE A 113      18.224  42.071  50.816  1.00 24.87           C
ATOM   1649  CZ  PHE A 113      18.548  43.166  50.057  1.00 21.81           C
ATOM   1650  H   PHE A 113      17.875  42.342  54.427  1.00 14.83           H
ATOM   1651  HA  PHE A 113      20.250  42.435  55.661  1.00 15.57           H
ATOM   1652  HB2 PHE A 113      21.367  43.036  53.708  1.00 24.10           H
ATOM   1653  HB3 PHE A 113      20.865  41.531  53.707  1.00 24.10           H
ATOM   1654  HD1 PHE A 113      20.840  44.386  51.919  1.00 24.40           H
ATOM   1655  HD2 PHE A 113      18.681  41.096  52.501  1.00 25.47           H
ATOM   1656  HE1 PHE A 113      19.730  44.776  49.967  1.00 28.18           H
ATOM   1657  HE2 PHE A 113      17.560  41.484  50.533  1.00 29.84           H
ATOM   1658  HZ  PHE A 113      18.093  43.330  49.263  1.00 26.17           H
""")
  hierarchy_1 = pdb_1.hierarchy
  hierarchy_2 = pdb_2.hierarchy
  pdb_atoms_1 = hierarchy_1.atoms()
  sites_1 = pdb_atoms_1.extract_xyz()
  sites_2 = hierarchy_2.atoms().extract_xyz()
  result = alternate_conformations.coord_stats_with_flips(sites_1, sites_2, pdb_atoms_1)
  assert (approx_equal(result.rmsd, 0.2, eps=0.001))
  assert (approx_equal(result.max_dev, 0.452427, eps=0.00001))
  #--- get_selection_gap
  assert (alternate_conformations.get_selection_gap([1,2,3,4,5],[6,7,8,9]) == 0)
  assert (alternate_conformations.get_selection_gap([6,7], [1,2,3,4,5]) == 0)
  assert (alternate_conformations.get_selection_gap([1,2,3,4,5],[7,8,9]) == 1)
  assert (alternate_conformations.get_selection_gap([1,2,3,4,5],[4,5,7,8,9]) == -2)
  assert (alternate_conformations.get_selection_gap([4,5,7,8,9],[1,2,3,4]) == -1)
  #--- score_rotamers
  n_outliers = alternate_conformations.score_rotamers(hierarchy_2,
    flex.bool(sites_2.size(), True))
  assert (n_outliers == 0)
  #--- get_partial_omit_map
  xrs = pdb_1.input.xray_structure_simple()
  f_calc = abs(xrs.structure_factors(d_min=1.5).f_calc())
  f_calc.set_observation_type_xray_amplitude()
  flags = f_calc.generate_r_free_flags()
  fmodel = mmtbx.utils.fmodel_simple(
    xray_structures=[xrs],
    f_obs=f_calc,
    r_free_flags=flags,
    scattering_table="n_gaussian",
    bulk_solvent_and_scaling=False,
    skip_twin_detection=True)
  sel = pdb_1.hierarchy.atom_selection_cache().selection("name CZ")
  assert (sel.count(True) == 1)
  two_fofc_map, fofc_map = alternate_conformations.get_partial_omit_map(
    fmodel=fmodel,
    selection=sel)
  site = xrs.sites_frac()[sel.iselection()[0]] # CZ coordinate
  assert (fofc_map.tricubic_interpolation(site) > 20)
def find_alternate_residue(residue,
                           pdb_hierarchy,
                           fmodel,
                           restraints_manager,
                           params,
                           verbose=False,
                           debug=None,
                           log=None):
    if (log is None):
        log = null_out()
    t1 = time.time()
    from scitbx.array_family import flex
    selection = flex.size_t()
    window = building.get_window_around_residue(residue,
                                                window_size=params.window_size)
    for pdb_object in window:
        selection.extend(pdb_object.atoms().extract_i_seq())
    assert (len(selection) > 0) and (not selection.all_eq(0))
    occupancies = []
    if (params.expected_occupancy is not None):
        assert (0.0 <= params.expected_occupancy <= 1.0)
        occupancies = [params.expected_occupancy]
    else:
        occupancies = [0.2, 0.3, 0.4, 0.5]
    trials = []
    sites_start_1d = pdb_hierarchy.atoms().extract_xyz().as_double()
    from mmtbx.rotamer import rotamer_eval
    rotamer_manager = rotamer_eval.RotamerEval(data_version="8000")
    id_str = residue.id_str()
    delete_selection = None
    if (params.omit_waters):
        delete_selection = building.get_nearby_water_selection(
            pdb_hierarchy=pdb_hierarchy,
            xray_structure=fmodel.xray_structure,
            selection=selection)
    for occupancy in occupancies:
        prefix = "%s_%.2f" % (id_str.replace(" ", "_"), occupancy)
        map_file_name = None
        if (debug > 1):
            map_file_name = prefix + ".mtz"
        two_fofc_map, fofc_map = alt_confs.get_partial_omit_map(
            fmodel=fmodel.deep_copy(),
            selection=selection,
            selection_delete=delete_selection,
            negate_surrounding=True,
            map_file_name=map_file_name,
            partial_occupancy=1.0 - occupancy)
        rebuild = rebuild_residue(
            target_map=fofc_map,
            pdb_hierarchy=pdb_hierarchy,
            xray_structure=fmodel.xray_structure,
            geometry_restraints_manager=restraints_manager,
            rotamer_eval=rotamer_manager,
            d_min=fmodel.f_obs().d_min())
        new_hierarchy = rebuild(
            atom_group=residue,
            window_size=params.window_size,
            backbone_sample_angle=params.backbone_sample_angle,
            anneal=params.anneal,
            annealing_temperature=params.annealing_temperature,
            use_chi1_sampling=params.simple_chi1_sampling,
            log=log)
        trial = residue_trial(residue=residue,
                              new_hierarchy=new_hierarchy,
                              occupancy=occupancy,
                              rotamer_eval=rotamer_manager,
                              fmodel=fmodel,
                              two_fofc_map=two_fofc_map,
                              fofc_map=fofc_map)
        trials.append(trial)
        if (debug > 1):
            open("%s.pdb" % prefix,
                 "w").write(trial.new_hierarchy.as_pdb_string())
    sites_end_1d = pdb_hierarchy.atoms().extract_xyz().as_double()
    assert sites_start_1d.all_eq(sites_end_1d)
    t2 = time.time()
    if (debug > 1):
        print("  %d build trials (%s): %.3fs" %
              (len(occupancies), residue.id_str(), t2 - t1),
              file=log)
    return trials
def find_alternate_residue (residue,
    pdb_hierarchy,
    fmodel,
    restraints_manager,
    params,
    verbose=False,
    debug=None,
    log=None) :
  if (log is None) :
    log = null_out()
  t1 = time.time()
  from scitbx.array_family import flex
  selection = flex.size_t()
  window = building.get_window_around_residue(residue,
    window_size=params.window_size)
  for pdb_object in window :
    selection.extend(pdb_object.atoms().extract_i_seq())
  assert (len(selection) > 0) and (not selection.all_eq(0))
  occupancies = []
  if (params.expected_occupancy is not None) :
    assert (0.0 <= params.expected_occupancy <= 1.0)
    occupancies = [ params.expected_occupancy ]
  else :
    occupancies = [ 0.2, 0.3, 0.4, 0.5 ]
  trials = []
  sites_start_1d = pdb_hierarchy.atoms().extract_xyz().as_double()
  from mmtbx.rotamer import rotamer_eval
  rotamer_manager = rotamer_eval.RotamerEval(data_version="8000")
  id_str = residue.id_str()
  delete_selection = None
  if (params.omit_waters) :
    delete_selection = building.get_nearby_water_selection(
      pdb_hierarchy=pdb_hierarchy,
      xray_structure=fmodel.xray_structure,
      selection=selection)
  for occupancy in occupancies :
    prefix = "%s_%.2f" % (id_str.replace(" ", "_"), occupancy)
    map_file_name = None
    if (debug > 1) :
      map_file_name = prefix + ".mtz"
    two_fofc_map, fofc_map = alt_confs.get_partial_omit_map(
      fmodel=fmodel.deep_copy(),
      selection=selection,
      selection_delete=delete_selection,
      negate_surrounding=True,
      map_file_name=map_file_name,
      partial_occupancy=1.0 - occupancy)
    rebuild = rebuild_residue(
      target_map=fofc_map,
      pdb_hierarchy=pdb_hierarchy,
      xray_structure=fmodel.xray_structure,
      geometry_restraints_manager=restraints_manager,
      rotamer_eval=rotamer_manager,
      d_min=fmodel.f_obs().d_min())
    new_hierarchy = rebuild(atom_group=residue,
      window_size=params.window_size,
      backbone_sample_angle=params.backbone_sample_angle,
      anneal=params.anneal,
      annealing_temperature=params.annealing_temperature,
      use_chi1_sampling=params.simple_chi1_sampling,
      log=log)
    trial = residue_trial(
      residue=residue,
      new_hierarchy=new_hierarchy,
      occupancy=occupancy,
      rotamer_eval=rotamer_manager,
      fmodel=fmodel,
      two_fofc_map=two_fofc_map,
      fofc_map=fofc_map)
    trials.append(trial)
    if (debug > 1) :
      open("%s.pdb" % prefix, "w").write(trial.new_hierarchy.as_pdb_string())
  sites_end_1d = pdb_hierarchy.atoms().extract_xyz().as_double()
  assert sites_start_1d.all_eq(sites_end_1d)
  t2 = time.time()
  if (debug > 1) :
    print >> log, "  %d build trials (%s): %.3fs" % (len(occupancies),
      residue.id_str(), t2 - t1)
  return trials
 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)