def exercise_1(mon_lib_srv, ener_lib):
  pdb_in = simple_pdb()
  params = pdb_interpretation.master_params.extract()
  processed_pdb_file = pdb_interpretation.process(
    mon_lib_srv=mon_lib_srv,
    ener_lib=ener_lib,
    params=params,
    pdb_inp=pdb_in,
    log=StringIO())
  grm = processed_pdb_file.geometry_restraints_manager()
  pdb_hierarchy = processed_pdb_file.all_chain_proxies.pdb_hierarchy
  sites_cart = pdb_hierarchy.atoms().extract_xyz()

  proxies = reference.add_coordinate_restraints(sites_cart=sites_cart)
  assert proxies.size() == 29, "expected 29, got %d" % proxies.size()
  import boost.python
  ext = boost.python.import_ext("mmtbx_reference_coordinate_ext")
  grads = flex.vec3_double(sites_cart.size(), (0.0,0.0,0.0))
  residual = ext.reference_coordinate_residual_sum(
      sites_cart=sites_cart,
      proxies=proxies,
      gradient_array=grads)
  assert approx_equal(residual, 0.0)

  #test selection
  ca_selection = pdb_hierarchy.get_peptide_c_alpha_selection()
  ca_sites_cart = sites_cart.select(ca_selection)
  proxies = reference.add_coordinate_restraints(
      sites_cart=ca_sites_cart,
      selection=ca_selection)
  assert proxies.size() == 3, "expected 3, got %d" % proxies.size()
  tst_iselection = flex.size_t()
  for atom in pdb_hierarchy.atoms():
    if atom.name == " CA " or atom.name == " N  ":
      tst_iselection.append(atom.i_seq)
  tst_sites_cart = sites_cart.select(tst_iselection)
  proxies = reference.add_coordinate_restraints(
      sites_cart=tst_sites_cart,
      selection=tst_iselection)
  assert proxies.size() == 6, "expected 6, got %d" % proxies.size()

  #test remove
  selection = flex.bool([False]*29)
  proxies = proxies.proxy_remove(selection=selection)
  assert proxies.size() == 6, "expected 6, got %d" % proxies.size()
  proxies = proxies.proxy_remove(selection=ca_selection)
  assert proxies.size() == 3, "expected 3, got %d" % proxies.size()
  selection = flex.bool([True]*29)
  proxies = proxies.proxy_remove(selection=selection)
  assert proxies.size() == 0, "expected 0, got %d" % proxies.size()
def exercise_1(mon_lib_srv, ener_lib):
    pdb_in = simple_pdb()
    params = pdb_interpretation.master_params.extract()
    processed_pdb_file = pdb_interpretation.process(mon_lib_srv=mon_lib_srv,
                                                    ener_lib=ener_lib,
                                                    params=params,
                                                    pdb_inp=pdb_in,
                                                    log=StringIO())
    grm = processed_pdb_file.geometry_restraints_manager()
    pdb_hierarchy = processed_pdb_file.all_chain_proxies.pdb_hierarchy
    sites_cart = pdb_hierarchy.atoms().extract_xyz()

    proxies = reference.add_coordinate_restraints(sites_cart=sites_cart)
    assert proxies.size() == 29, "expected 29, got %d" % proxies.size()
    import boost.python
    ext = boost.python.import_ext("mmtbx_reference_coordinate_ext")
    grads = flex.vec3_double(sites_cart.size(), (0.0, 0.0, 0.0))
    residual = ext.reference_coordinate_residual_sum(sites_cart=sites_cart,
                                                     proxies=proxies,
                                                     gradient_array=grads)
    assert approx_equal(residual, 0.0)

    #test selection
    ca_selection = pdb_hierarchy.get_peptide_c_alpha_selection()
    ca_sites_cart = sites_cart.select(ca_selection)
    proxies = reference.add_coordinate_restraints(sites_cart=ca_sites_cart,
                                                  selection=ca_selection)
    assert proxies.size() == 3, "expected 3, got %d" % proxies.size()
    tst_iselection = flex.size_t()
    for atom in pdb_hierarchy.atoms():
        if atom.name == " CA " or atom.name == " N  ":
            tst_iselection.append(atom.i_seq)
    tst_sites_cart = sites_cart.select(tst_iselection)
    proxies = reference.add_coordinate_restraints(sites_cart=tst_sites_cart,
                                                  selection=tst_iselection)
    assert proxies.size() == 6, "expected 6, got %d" % proxies.size()

    #test remove
    selection = flex.bool([False] * 29)
    proxies = proxies.proxy_remove(selection=selection)
    assert proxies.size() == 6, "expected 6, got %d" % proxies.size()
    proxies = proxies.proxy_remove(selection=ca_selection)
    assert proxies.size() == 3, "expected 3, got %d" % proxies.size()
    selection = flex.bool([True] * 29)
    proxies = proxies.proxy_remove(selection=selection)
    assert proxies.size() == 0, "expected 0, got %d" % proxies.size()
Exemplo n.º 3
0
def exercise_1():
  pdb_in = simple_pdb()
  pdb_hierarchy = pdb_in.construct_hierarchy()
  sites_cart = pdb_hierarchy.atoms().extract_xyz()

  proxies = reference.add_coordinate_restraints(sites_cart=sites_cart)
  assert proxies.size() == 29, "expected 29, got %d" % proxies.size()
  import boost.python
  ext = boost.python.import_ext("mmtbx_reference_coordinate_ext")
  grads = flex.vec3_double(sites_cart.size(), (0.0,0.0,0.0))
  residual = ext.reference_coordinate_residual_sum(
      sites_cart=sites_cart,
      proxies=proxies,
      gradient_array=grads)
  assert approx_equal(residual, 0.0)

  #test selection
  ca_selection = pdb_hierarchy.get_peptide_c_alpha_selection()
  ca_sites_cart = sites_cart.select(ca_selection)
  proxies = reference.add_coordinate_restraints(
      sites_cart=ca_sites_cart,
      selection=ca_selection)
  assert proxies.size() == 3, "expected 3, got %d" % proxies.size()
  tst_iselection = flex.size_t()
  for atom in pdb_hierarchy.atoms():
    if atom.name == " CA " or atom.name == " N  ":
      tst_iselection.append(atom.i_seq)
  tst_sites_cart = sites_cart.select(tst_iselection)
  proxies = reference.add_coordinate_restraints(
      sites_cart=tst_sites_cart,
      selection=tst_iselection)
  assert proxies.size() == 6, "expected 6, got %d" % proxies.size()

  #test remove
  selection = flex.bool([False]*29)
  proxies = proxies.proxy_remove(selection=selection)
  assert proxies.size() == 6, "expected 6, got %d" % proxies.size()
  proxies = proxies.proxy_remove(selection=ca_selection)
  assert proxies.size() == 3, "expected 3, got %d" % proxies.size()
  selection = flex.bool([True]*29)
  proxies = proxies.proxy_remove(selection=selection)
  assert proxies.size() == 0, "expected 0, got %d" % proxies.size()
Exemplo n.º 4
0
def minimize_hierarchy(hierarchy, xrs, original_pdb_h, excl_string_selection, log=None):
    from mmtbx.monomer_library.pdb_interpretation import grand_master_phil_str
    from mmtbx.refinement.geometry_minimization import run2
    from mmtbx.geometry_restraints import reference

    if log is None:
        log = null_out()
    params_line = grand_master_phil_str
    params = iotbx.phil.parse(input_string=params_line, process_includes=True).extract()
    params.pdb_interpretation.clash_guard.nonbonded_distance_threshold = None
    params.pdb_interpretation.peptide_link.ramachandran_restraints = True
    params.pdb_interpretation.peptide_link.oldfield.weight_scale = 3
    params.pdb_interpretation.peptide_link.oldfield.plot_cutoff = 0.03
    params.pdb_interpretation.c_beta_restraints = True

    processed_pdb_files_srv = mmtbx.utils.process_pdb_file_srv(
        crystal_symmetry=xrs.crystal_symmetry(),
        pdb_interpretation_params=params.pdb_interpretation,
        stop_for_unknowns=False,
        log=log,
        cif_objects=None,
    )
    processed_pdb_file, junk = processed_pdb_files_srv.process_pdb_files(
        raw_records=flex.split_lines(hierarchy.as_pdb_string())
    )
    grm = get_geometry_restraints_manager(processed_pdb_file, xrs)

    asc = original_pdb_h.atom_selection_cache()
    sel = asc.selection(excl_string_selection)

    grm.geometry.append_reference_coordinate_restraints_in_place(
        reference.add_coordinate_restraints(
            sites_cart=original_pdb_h.atoms().extract_xyz().select(sel), selection=sel, sigma=0.5
        )
    )
    obj = run2(
        restraints_manager=grm,
        pdb_hierarchy=hierarchy,
        correct_special_position_tolerance=1.0,
        max_number_of_iterations=300,
        number_of_macro_cycles=5,
        bond=True,
        nonbonded=True,
        angle=True,
        dihedral=True,
        chirality=True,
        planarity=True,
        fix_rotamer_outliers=True,
        log=log,
    )
def test_reference_coordinate(mon_lib_srv, ener_lib, prefix="tst_grm_pickling_ref_coor"):
  from mmtbx.geometry_restraints import reference
  # for some strange reason without importing this the code doesn't work...
  from cctbx import adp_restraints # import dependency

  pdb_inp = iotbx.pdb.input(source_info=None, lines=raw_records3)
  params = monomer_library.pdb_interpretation.master_params.extract()
  params.reference_coordinate_restraints.enabled=False
  processed_pdb_file = monomer_library.pdb_interpretation.process(
    mon_lib_srv=mon_lib_srv,
    ener_lib=ener_lib,
    params=params,
    strict_conflict_handling=False,
    pdb_inp=pdb_inp,
    log=null_out())
  geo = processed_pdb_file.geometry_restraints_manager()
  pdb_hierarchy = processed_pdb_file.all_chain_proxies.pdb_hierarchy
  sites_cart = pdb_hierarchy.atoms().extract_xyz()
  rcp = reference.add_coordinate_restraints(sites_cart=sites_cart)
  geo.adopt_reference_coordinate_restraints_in_place(rcp)
  # print "number of rcr proxies:", geo.get_n_reference_coordinate_proxies()
  make_geo_pickle_unpickle(geo, processed_pdb_file.xray_structure(), prefix)
Exemplo n.º 6
0
    def make_wat_restraints(self, sigma):
        raw_records = self.std_wat_pdb.as_pdb_string()
        processed_pdb = monomer_library.pdb_interpretation.process(
            mon_lib_srv=monomer_library.server.server(),
            ener_lib=monomer_library.server.ener_lib(),
            file_name=None,
            raw_records=raw_records,
            crystal_symmetry=self.std_wat_pdb.crystal_symmetry(),
            force_symmetry=True)

        geometry = processed_pdb.geometry_restraints_manager(
            show_energies=False, plain_pairs_radius=5.0)
        chain_proxy = processed_pdb.all_chain_proxies
        sites_start = self.std_wat_xrs.sites_cart()
        selection = chain_proxy.selection("Element O")
        isel = selection.iselection()
        harm_proxy = add_coordinate_restraints(
            sites_cart=sites_start.select(isel), selection=isel, sigma=sigma)

        restraints_manager = mmtbx.restraints.manager(geometry=geometry,
                                                      normalization=False)
        restraints_manager.geometry.reference_coordinate_proxies = harm_proxy
        return restraints_manager
Exemplo n.º 7
0
def test_reference_coordinate(mon_lib_srv,
                              ener_lib,
                              prefix="tst_grm_pickling_ref_coor"):
    from mmtbx.geometry_restraints import reference
    # for some strange reason without importing this the code doesn't work...
    from cctbx import adp_restraints  # import dependency

    pdb_inp = iotbx.pdb.input(source_info=None, lines=raw_records3)
    params = monomer_library.pdb_interpretation.master_params.extract()
    params.reference_coordinate_restraints.enabled = False
    processed_pdb_file = monomer_library.pdb_interpretation.process(
        mon_lib_srv=mon_lib_srv,
        ener_lib=ener_lib,
        params=params,
        strict_conflict_handling=False,
        pdb_inp=pdb_inp,
        log=sys.stdout)
    geo = processed_pdb_file.geometry_restraints_manager()
    pdb_hierarchy = processed_pdb_file.all_chain_proxies.pdb_hierarchy
    sites_cart = pdb_hierarchy.atoms().extract_xyz()
    rcp = reference.add_coordinate_restraints(sites_cart=sites_cart)
    geo.adopt_reference_coordinate_restraints_in_place(rcp)
    # print "number of rcr proxies:", geo.get_n_reference_coordinate_proxies()
    make_geo_pickle_unpickle(geo, processed_pdb_file.xray_structure(), prefix)
Exemplo n.º 8
0
def substitute_ss(real_h,
                    xray_structure,
                    ss_annotation,
                    params = None,
                    grm=None,
                    use_plane_peptide_bond_restr=True,
                    fix_rotamer_outliers=True,
                    cif_objects=None,
                    log=null_out(),
                    rotamer_manager=None,
                    reference_map=None,
                    verbose=False):
  """
  Substitute secondary structure elements in real_h hierarchy with ideal
  ones _in_place_.
  Returns reference torsion proxies - the only thing that cannot be restored
  with little effort outside the procedure.
  real_h - hierarcy to substitute secondary structure elements.
  xray_structure - xray_structure - needed to get crystal symmetry (to
      construct processed_pdb_file and xray_structure is needed to call
      get_geometry_restraints_manager for no obvious reason).
  ss_annotation - iotbx.pdb.annotation object.
  """
  t0 = time()
  if rotamer_manager is None:
    rotamer_manager = RotamerEval()
  for model in real_h.models():
    for chain in model.chains():
      if len(chain.conformers()) > 1:
        raise Sorry("Alternative conformations are not supported.")

  processed_params = process_params(params)
  if not processed_params.enabled:
    return None

  expected_n_hbonds = 0
  ann = ss_annotation
  for h in ann.helices:
    expected_n_hbonds += h.get_n_maximum_hbonds()
  edited_h = real_h.deep_copy()
  n_atoms_in_real_h = real_h.atoms_size()
  selection_cache = real_h.atom_selection_cache()

  # check the annotation for correctness (atoms are actually in hierarchy)
  error_msg = "The following secondary structure annotations result in \n"
  error_msg +="empty atom selections. They don't match the structre: \n"
  t1 = time()
  # Checking for SS selections
  deleted_annotations = ann.remove_empty_annotations(
      hierarchy=real_h,
      asc=selection_cache)
  if not deleted_annotations.is_empty():
    if processed_params.skip_empty_ss_elements:
      if len(deleted_annotations.helices) > 0:
        print >> log, "Removing the following helices because there are"
        print >> log, "no corresponding atoms in the model:"
        for h in deleted_annotations.helices:
          print >> log, h.as_pdb_str()
          error_msg += "  %s\n" % h
      if len(deleted_annotations.sheets) > 0:
        print >> log, "Removing the following sheets because there are"
        print >> log, "no corresponding atoms in the model:"
        for sh in deleted_annotations.sheets:
          print >> log, sh.as_pdb_str()
          error_msg += "  %s\n" % sh.as_pdb_str(strand_id=st.strand_id)
    else:
      raise Sorry(error_msg)
  phil_str = ann.as_restraint_groups()

  # gathering initial special position atoms
  special_position_settings = crystal.special_position_settings(
      crystal_symmetry = xray_structure.crystal_symmetry())
  site_symmetry_table = \
      special_position_settings.site_symmetry_table(
        sites_cart = real_h.atoms().extract_xyz(),
        unconditional_general_position_flags=(
          real_h.atoms().extract_occ() != 1))
  original_spi = site_symmetry_table.special_position_indices()

  t2 = time()
  # Actually idelizing SS elements
  log.write("Replacing ss-elements with ideal ones:\n")
  log.flush()
  for h in ann.helices:
    log.write("  %s\n" % h.as_pdb_str())
    log.flush()
    selstring = h.as_atom_selections()
    isel = selection_cache.iselection(selstring[0])
    all_bsel = flex.bool(n_atoms_in_real_h, False)
    all_bsel.set_selected(isel, True)
    sel_h = real_h.select(all_bsel, copy_atoms=True)
    ideal_h = get_helix(helix_class=h.helix_class,
                        pdb_hierarchy_template=sel_h,
                        rotamer_manager=rotamer_manager)
    # edited_h.select(all_bsel).atoms().set_xyz(ideal_h.atoms().extract_xyz())
    set_xyz_carefully(dest_h=edited_h.select(all_bsel), source_h=ideal_h)
  for sh in ann.sheets:
    s = "  %s\n" % sh.as_pdb_str()
    ss = s.replace("\n", "\n  ")
    log.write(ss[:-2])
    log.flush()
    for st in sh.strands:
      selstring = st.as_atom_selections()
      isel = selection_cache.iselection(selstring)
      all_bsel = flex.bool(n_atoms_in_real_h, False)
      all_bsel.set_selected(isel, True)
      sel_h = real_h.select(all_bsel, copy_atoms=True)
      ideal_h = secondary_structure_from_sequence(
          pdb_str=beta_pdb_str,
          sequence=None,
          pdb_hierarchy_template=sel_h,
          rotamer_manager=rotamer_manager,
          )
      set_xyz_carefully(edited_h.select(all_bsel), ideal_h)
      # edited_h.select(all_bsel).atoms().set_xyz(ideal_h.atoms().extract_xyz())

  t3 = time()
  pre_result_h = edited_h
  pre_result_h.reset_i_seq_if_necessary()
  n_atoms = real_h.atoms_size()
  bsel = flex.bool(n_atoms, False)
  helix_selection = flex.bool(n_atoms, False)
  sheet_selection = flex.bool(n_atoms, False)
  other_selection = flex.bool(n_atoms, False)
  ss_for_tors_selection = flex.bool(n_atoms, False)
  nonss_for_tors_selection = flex.bool(n_atoms, False)
  selection_cache = real_h.atom_selection_cache()
  # set all CA atoms to True for other_selection
  #isel = selection_cache.iselection("name ca")
  isel = selection_cache.iselection("name ca or name n or name o or name c")
  other_selection.set_selected(isel, True)
  n_main_chain_atoms = other_selection.count(True)
  isel = selection_cache.iselection("name ca or name n or name o or name c")
  nonss_for_tors_selection.set_selected(isel, True)
  main_chain_selection_prefix = "(name ca or name n or name o or name c) %s"

  t4 = time()
  print >> log, "Preparing selections..."
  log.flush()
  # Here we are just preparing selections
  for h in ann.helices:
    ss_sels = h.as_atom_selections()[0]
    selstring = main_chain_selection_prefix % ss_sels
    isel = selection_cache.iselection(selstring)
    helix_selection.set_selected(isel, True)
    other_selection.set_selected(isel, False)
    isel = selection_cache.iselection(selstring)
    ss_for_tors_selection.set_selected(isel, True)
    nonss_for_tors_selection.set_selected(isel, False)

  for sheet in ann.sheets:
    for ss_sels in sheet.as_atom_selections():
      selstring = main_chain_selection_prefix % ss_sels
      isel = selection_cache.iselection(selstring)
      sheet_selection.set_selected(isel, True)
      other_selection.set_selected(isel, False)
      isel = selection_cache.iselection(selstring)
      ss_for_tors_selection.set_selected(isel, True)
      nonss_for_tors_selection.set_selected(isel, False)
  t5 = time()
  isel = selection_cache.iselection(
      "not name ca and not name n and not name o and not name c")
  other_selection.set_selected(isel, False)
  helix_sheet_intersection = helix_selection & sheet_selection
  if helix_sheet_intersection.count(True) > 0:
    sheet_selection = sheet_selection & ~helix_sheet_intersection
  assert ((helix_selection | sheet_selection) & other_selection).count(True)==0

  from mmtbx.monomer_library.pdb_interpretation import grand_master_phil_str
  params_line = grand_master_phil_str
  params_line += "secondary_structure {%s}" % secondary_structure.sec_str_master_phil_str
  # print "params_line"
  # print params_line
  params = iotbx.phil.parse(input_string=params_line, process_includes=True)#.extract()
  # This does not the same way for a strange reason. Need to investigate.
  # The number of resulting hbonds is different later.
  # w_params = params.extract()
  # w_params.pdb_interpretation.secondary_structure.protein.remove_outliers = False
  # w_params.pdb_interpretation.peptide_link.ramachandran_restraints = True
  # w_params.pdb_interpretation.c_beta_restraints = True
  # w_params.pdb_interpretation.secondary_structure.enabled = True
  # params.format(python_object=w_params)
  # params.show()
  # print "="*80
  # print "="*80
  # print "="*80
  if grm is None:
    custom_par_text = "\n".join([
        "pdb_interpretation.secondary_structure {protein.remove_outliers = False\n%s}" \
            % phil_str,
        "pdb_interpretation.peptide_link.ramachandran_restraints = True",
        "c_beta_restraints = True",
        "pdb_interpretation.secondary_structure.enabled=True",
        "pdb_interpretation.clash_guard.nonbonded_distance_threshold=None",
        "pdb_interpretation.max_reasonable_bond_distance=None",
        # "pdb_interpretation.nonbonded_weight=500",
        "pdb_interpretation.peptide_link.oldfield.weight_scale=3",
        "pdb_interpretation.peptide_link.oldfield.plot_cutoff=0.03",
        "pdb_interpretation.peptide_link.omega_esd_override_value=3",
        "pdb_interpretation.peptide_link.apply_all_trans=True",
        ])

    if use_plane_peptide_bond_restr:
      custom_par_text += "\npdb_interpretation.peptide_link.apply_peptide_plane=True"

    custom_pars = params.fetch(
        source=iotbx.phil.parse(custom_par_text)).extract()
    # params.format(python_object=custom_pars)
    # params.show()
    # STOP()
    params = custom_pars
    # params = w_params

    t6 = time()
    import mmtbx.utils
    processed_pdb_files_srv = mmtbx.utils.\
        process_pdb_file_srv(
            crystal_symmetry= xray_structure.crystal_symmetry(),
            pdb_interpretation_params = params.pdb_interpretation,
            log=null_out(),
            cif_objects=cif_objects)
    if verbose:
      print >> log, "Processing file..."
      log.flush()
    processed_pdb_file, junk = processed_pdb_files_srv.\
        process_pdb_files(raw_records=flex.split_lines(real_h.as_pdb_string()))
    t7 = time()

    grm = get_geometry_restraints_manager(
      processed_pdb_file, xray_structure)
    t8 = time()
  else:
    ss_params = secondary_structure.default_params
    ss_params.secondary_structure.protein.remove_outliers=False
    ss_manager = secondary_structure.manager(
        pdb_hierarchy=real_h,
        geometry_restraints_manager=grm.geometry,
        sec_str_from_pdb_file=ss_annotation,
        params=ss_params.secondary_structure,
        mon_lib_srv=None,
        verbose=-1,
        log=log)
    grm.geometry.set_secondary_structure_restraints(
        ss_manager=ss_manager,
        hierarchy=real_h,
        log=log)
  real_h.reset_i_seq_if_necessary()
  from mmtbx.geometry_restraints import reference
  if reference_map is None:
    if verbose:
      print >> log, "Adding reference coordinate restraints..."
    grm.geometry.append_reference_coordinate_restraints_in_place(
        reference.add_coordinate_restraints(
            sites_cart = real_h.atoms().extract_xyz().select(helix_selection),
            selection  = helix_selection,
            sigma      = processed_params.sigma_on_reference_helix))
    grm.geometry.append_reference_coordinate_restraints_in_place(
        reference.add_coordinate_restraints(
            sites_cart = real_h.atoms().extract_xyz().select(sheet_selection),
            selection  = sheet_selection,
            sigma      = processed_params.sigma_on_reference_sheet))
    grm.geometry.append_reference_coordinate_restraints_in_place(
        reference.add_coordinate_restraints(
            sites_cart = real_h.atoms().extract_xyz().select(other_selection),
            selection  = other_selection,
            sigma      = processed_params.sigma_on_reference_non_ss))
  if verbose:
    print >> log, "Adding chi torsion restraints..."
  grm.geometry.add_chi_torsion_restraints_in_place(
          pdb_hierarchy   = pre_result_h,
          sites_cart      = pre_result_h.atoms().extract_xyz().\
                                 select(ss_for_tors_selection),
          selection = ss_for_tors_selection,
          chi_angles_only = False,
          sigma           = processed_params.sigma_on_torsion_ss)
  grm.geometry.add_chi_torsion_restraints_in_place(
          pdb_hierarchy   = pre_result_h,
          sites_cart      = real_h.atoms().extract_xyz().\
                                select(nonss_for_tors_selection),
          selection = nonss_for_tors_selection,
          chi_angles_only = False,
          sigma           = processed_params.sigma_on_torsion_nonss)

  real_h.atoms().set_xyz(pre_result_h.atoms().extract_xyz())
  #
  # Check and correct for special positions
  #
  special_position_settings = crystal.special_position_settings(
      crystal_symmetry = xray_structure.crystal_symmetry())
  site_symmetry_table = \
      special_position_settings.site_symmetry_table(
        sites_cart = real_h.atoms().extract_xyz(),
        unconditional_general_position_flags=(
          real_h.atoms().extract_occ() != 1))
  spi = site_symmetry_table.special_position_indices()
  if spi.size() > 0:
    print >> log, "Moving atoms from special positions:"
    for spi_i in spi:
      if spi_i not in original_spi:
        new_coords = (
            real_h.atoms()[spi_i].xyz[0]+0.2,
            real_h.atoms()[spi_i].xyz[1]+0.2,
            real_h.atoms()[spi_i].xyz[2]+0.2)
        print >> log, "  ", real_h.atoms()[spi_i].id_str(),
        print >> log, tuple(real_h.atoms()[spi_i].xyz), "-->", new_coords
        real_h.atoms()[spi_i].set_xyz(new_coords)

  t9 = time()
  if processed_params.file_name_before_regularization is not None:
    grm.geometry.pair_proxies(sites_cart=real_h.atoms().extract_xyz())
    if grm.geometry.ramachandran_manager is not None:
      grm.geometry.ramachandran_manager.update_phi_psi_targets(
          sites_cart=real_h.atoms().extract_xyz())
    print >> log, "Outputting model before regularization %s" % processed_params.file_name_before_regularization
    real_h.write_pdb_file(
        file_name=processed_params.file_name_before_regularization)
    geo_fname = processed_params.file_name_before_regularization[:-4]+'.geo'
    print >> log, "Outputting geo file for regularization %s" % geo_fname
    grm.write_geo_file(
        site_labels=[atom.id_str() for atom in real_h.atoms()],
        file_name=geo_fname)

  #testing number of restraints
  assert grm.geometry.get_n_den_proxies() == 0
  if reference_map is None:
    assert grm.geometry.get_n_reference_coordinate_proxies() == n_main_chain_atoms
  refinement_log = null_out()
  log.write(
      "Refining geometry of substituted secondary structure elements...")
  log.flush()
  if verbose:
    refinement_log = log
  from mmtbx.refinement.geometry_minimization import run2
  t10 = time()
  if reference_map is None:
    obj = run2(
        restraints_manager       = grm,
        pdb_hierarchy            = real_h,
        correct_special_position_tolerance = 1.0,
        max_number_of_iterations = processed_params.n_iter,
        number_of_macro_cycles   = processed_params.n_macro,
        bond                     = True,
        nonbonded                = True,
        angle                    = True,
        dihedral                 = True,
        chirality                = True,
        planarity                = True,
        fix_rotamer_outliers     = fix_rotamer_outliers,
        log                      = refinement_log)
  else:
    ref_xrs = real_h.extract_xray_structure(
        crystal_symmetry=xray_structure.crystal_symmetry())
    minimize_wrapper_with_map(
        pdb_h=real_h,
        xrs=ref_xrs,
        target_map=reference_map,
        grm=grm,
        ncs_restraints_group_list=[],
        mon_lib_srv=None,
        ss_annotation=ss_annotation,
        refine_ncs_operators=False,
        number_of_cycles=processed_params.n_macro,
        log=log)
    real_h.write_pdb_file("after_ss_map_min.pdb")

  log.write(" Done\n")
  log.flush()
  t11 = time()
  # print >> log, "Initial checking, init   : %.4f" % (t1-t0)
  # print >> log, "Checking SS              : %.4f" % (t2-t1)
  # print >> log, "Initializing selections  : %.4f" % (t4-t3)
  # print >> log, "Looping for selections   : %.4f" % (t5-t4)
  # print >> log, "Finalizing selections    : %.4f" % (t6-t5)
  # print >> log, "PDB interpretation       : %.4f" % (t7-t6)
  # print >> log, "Get GRM                  : %.4f" % (t8-t7)
  # print >> log, "Adding restraints to GRM : %.4f" % (t9-t8)
  # print >> log, "Running GM               : %.4f" % (t11-t10)
  # print_hbond_proxies(grm.geometry,real_h)
  return grm.geometry.get_chi_torsion_proxies()
def minimize_wrapper_for_ramachandran(
        model,
        original_pdb_h,
        excl_string_selection,
        processed_pdb_file=None,
        log=None,
        reference_rotamers=True,
        number_of_cycles=1,
        run_first_minimization_without_reference=False,
        oldfield_weight_scale=3,
        oldfield_plot_cutoff=0.03,
        nonbonded_weight=500,
        reference_sigma=0.7):
    """ Wrapper around geometry minimization specifically tuned for eliminating
  Ramachandran outliers.
  probably not working anymore... no processed_pdb_file available.
  WARNING: no setting sites_cart at the end...
  """
    grm = model.get_restraints_manager()
    assert grm is not None
    from mmtbx.geometry_restraints import reference
    from mmtbx.geometry_restraints.torsion_restraints.reference_model import \
        reference_model, reference_model_params
    from libtbx.utils import null_out
    from scitbx.array_family import flex
    if log is None:
        log = null_out()
    # assert hierarchy.atoms_size()==xrs.scatterers().size(), "%d %d" % (
    #     hierarchy.atoms_size(), xrs.scatterers().size())

    ncs_restraints_group_list = model.get_ncs_groups()
    if ncs_restraints_group_list is None:
        ncs_restraints_group_list = []

    grm.geometry.pair_proxies(sites_cart=model.get_sites_cart())
    if grm.geometry.ramachandran_manager is not None:
        grm.geometry.ramachandran_manager.update_phi_psi_targets(
            sites_cart=model.get_sites_cart())

    if reference_rotamers and original_pdb_h is not None:
        # make selection excluding rotamer outliers
        from mmtbx.rotamer.rotamer_eval import RotamerEval
        # print "Excluding rotamer outliers"
        rotamer_manager = model.get_rotamer_manager()
        non_rot_outliers_selection = flex.bool(model.get_number_of_atoms(),
                                               False)
        for m in original_pdb_h.models():
            for chain in m.chains():
                for conf in chain.conformers():
                    for res in conf.residues():
                        ev = rotamer_manager.evaluate_residue_2(res)
                        if ev != "OUTLIER" or ev is None:
                            for a in res.atoms():
                                non_rot_outliers_selection[a.i_seq] = True
                        # else:
                        #   print "  ", res.id_str()

        if processed_pdb_file is not None:
            rm_params = reference_model_params.extract()
            rm_params.reference_model.enabled = True
            rm_params.reference_model.strict_rotamer_matching = False
            rm_params.reference_model.main_chain = False
            rm = reference_model(processed_pdb_file=processed_pdb_file,
                                 reference_file_list=None,
                                 reference_hierarchy_list=[original_pdb_h],
                                 mon_lib_srv=model.get_mon_lib_srv(),
                                 ener_lib=model.get_ener_lib(),
                                 has_hd=None,
                                 params=rm_params.reference_model,
                                 selection=non_rot_outliers_selection,
                                 log=log)
            rm.show_reference_summary(log=log)
            grm.geometry.adopt_reference_dihedral_manager(rm)

    # dealing with SS
    if model.get_ss_annotation() is not None:
        from mmtbx.secondary_structure import manager
        ss_manager = manager(pdb_hierarchy=model.get_hierarchy(),
                             geometry_restraints_manager=grm.geometry,
                             sec_str_from_pdb_file=model.get_ss_annotation(),
                             params=None,
                             mon_lib_srv=model.get_mon_lib_srv(),
                             verbose=-1,
                             log=log)
        grm.geometry.set_secondary_structure_restraints(
            ss_manager=ss_manager, hierarchy=model.get_hierarchy(), log=log)

    if run_first_minimization_without_reference:
        obj = run2(restraints_manager=grm,
                   pdb_hierarchy=model.get_hierarchy(),
                   correct_special_position_tolerance=1.0,
                   ncs_restraints_group_list=ncs_restraints_group_list,
                   max_number_of_iterations=300,
                   number_of_macro_cycles=number_of_cycles,
                   bond=True,
                   nonbonded=True,
                   angle=True,
                   dihedral=True,
                   chirality=True,
                   planarity=True,
                   fix_rotamer_outliers=True,
                   log=log)

    if original_pdb_h is not None:
        if not excl_string_selection or len(excl_string_selection) == 0:
            excl_string_selection = "all"
        asc = original_pdb_h.atom_selection_cache()
        sel = asc.selection(
            "(%s) and (name CA or name C or name N or name O)" %
            excl_string_selection)

        grm.geometry.append_reference_coordinate_restraints_in_place(
            reference.add_coordinate_restraints(
                sites_cart=original_pdb_h.atoms().extract_xyz().select(sel),
                selection=sel,
                sigma=reference_sigma,
                top_out_potential=True))

    obj = run2(restraints_manager=grm,
               pdb_hierarchy=model.get_hierarchy(),
               correct_special_position_tolerance=1.0,
               ncs_restraints_group_list=ncs_restraints_group_list,
               max_number_of_iterations=300,
               number_of_macro_cycles=number_of_cycles,
               bond=True,
               nonbonded=True,
               angle=True,
               dihedral=True,
               chirality=True,
               planarity=True,
               fix_rotamer_outliers=True,
               log=log)
    grm.geometry.reference_dihedral_manager = None
Exemplo n.º 10
0
def substitute_ss(
                    model, # changed in place
                    params = None,
                    use_plane_peptide_bond_restr=True,
                    fix_rotamer_outliers=True,
                    log=null_out(),
                    check_rotamer_clashes=True,
                    reference_map=None,
                    verbose=False):
  """
  Substitute secondary structure elements in real_h hierarchy with ideal
  ones _in_place_.
  Returns reference torsion proxies - the only thing that cannot be restored
  with little effort outside the procedure.
  real_h - hierarcy to substitute secondary structure elements.
  xray_structure - xray_structure - needed to get crystal symmetry (to
      construct processed_pdb_file and xray_structure is needed to call
      get_geometry_restraints_manager for no obvious reason).
  ss_annotation - iotbx.pdb.annotation object.
  """
  import mmtbx.utils

  ss_annotation = model.get_ss_annotation()

  t0 = time()
  if model.get_hierarchy().models_size() > 1:
    raise Sorry("Multi model files are not supported")
  for m in model.get_hierarchy().models():
    for chain in m.chains():
      if len(chain.conformers()) > 1:
        raise Sorry("Alternative conformations are not supported.")

  processed_params = process_params(params)
  if not processed_params.enabled:
    return None
  if ss_annotation is None:
    return None

  ann = ss_annotation
  if model.ncs_constraints_present():
    print >> log, "Using master NCS to reduce amount of work"

  expected_n_hbonds = 0
  for h in ann.helices:
    expected_n_hbonds += h.get_n_maximum_hbonds()
  edited_h = model.get_hierarchy().deep_copy()
  n_atoms_in_real_h = model.get_number_of_atoms()
  selection_cache = model.get_atom_selection_cache()

  # check the annotation for correctness (atoms are actually in hierarchy)
  error_msg = "The following secondary structure annotations result in \n"
  error_msg +="empty atom selections. They don't match the structre: \n"
  t1 = time()
  # Checking for SS selections
  deleted_annotations = ann.remove_empty_annotations(
      hierarchy=model.get_hierarchy(),
      asc=selection_cache)
  if not deleted_annotations.is_empty():
    if processed_params.skip_empty_ss_elements:
      if len(deleted_annotations.helices) > 0:
        print >> log, "Removing the following helices because there are"
        print >> log, "no corresponding atoms in the model:"
        for h in deleted_annotations.helices:
          print >> log, h.as_pdb_str()
          error_msg += "  %s\n" % h
      if len(deleted_annotations.sheets) > 0:
        print >> log, "Removing the following sheets because there are"
        print >> log, "no corresponding atoms in the model:"
        for sh in deleted_annotations.sheets:
          print >> log, sh.as_pdb_str()
          error_msg += "  %s\n" % sh.as_pdb_str(strand_id=st.strand_id)
    else:
      raise Sorry(error_msg)
  phil_str = ann.as_restraint_groups()

  # gathering initial special position atoms
  special_position_settings = crystal.special_position_settings(
      crystal_symmetry = model.crystal_symmetry())
  site_symmetry_table = \
      special_position_settings.site_symmetry_table(
        sites_cart = model.get_sites_cart(),
        unconditional_general_position_flags=(
          model.get_atoms().extract_occ() != 1))
  original_spi = site_symmetry_table.special_position_indices()

  t2 = time()
  # Actually idelizing SS elements
  fixed_ss_selection = flex.bool(n_atoms_in_real_h, False)
  log.write("Replacing ss-elements with ideal ones:\n")
  log.flush()
  ss_stats = gather_ss_stats(pdb_h=model.get_hierarchy())
  n_idealized_elements = 0
  master_bool_sel = model.get_master_selection()
  if master_bool_sel is None or master_bool_sel.size() == 0:
    master_bool_sel = flex.bool(model.get_number_of_atoms(), True)
  elif isinstance(master_bool_sel, flex.size_t):
    master_bool_sel = flex.bool(model.get_number_of_atoms(), master_bool_sel)
  assert master_bool_sel.size() == model.get_number_of_atoms()
  for h in ann.helices:
    log.write("  %s\n" % h.as_pdb_str())
    log.flush()
    if processed_params.skip_good_ss_elements and ss_element_is_good(ss_stats, ([h],[])):
      log.write("    skipping, good element.\n")
    else:
      selstring = h.as_atom_selections()
      sel = selection_cache.selection(selstring[0])
      isel = sel.iselection()
      if (master_bool_sel & sel).iselection().size() == 0:
        log.write("    skipping, not in NCS master.\n")
        continue
      n_idealized_elements += 1
      log.write("    substitute with idealized one.\n")
      fixed_ss_selection.set_selected(isel, True)
      all_bsel = flex.bool(n_atoms_in_real_h, False)
      all_bsel.set_selected(isel, True)
      sel_h = model.get_hierarchy().select(all_bsel, copy_atoms=True)
      ideal_h = get_helix(helix_class=h.helix_class,
                          pdb_hierarchy_template=sel_h,
                          rotamer_manager=model.get_rotamer_manager())
      # edited_h.select(all_bsel).atoms().set_xyz(ideal_h.atoms().extract_xyz())
      set_xyz_carefully(dest_h=edited_h.select(all_bsel), source_h=ideal_h)
      # set_xyz_smart(dest_h=edited_h.select(all_bsel), source_h=ideal_h) # does not work here
  for sh in ann.sheets:
    s = "  %s\n" % sh.as_pdb_str()
    ss = s.replace("\n", "\n  ")
    log.write(ss[:-2])
    log.flush()
    if processed_params.skip_good_ss_elements and ss_element_is_good(ss_stats, ([],[sh])):
      log.write("    skipping, good element.\n")
    else:
      full_sh_selection = flex.bool(n_atoms_in_real_h, False)
      for st in sh.strands:
        selstring = st.as_atom_selections()
        isel = selection_cache.iselection(selstring)
        full_sh_selection.set_selected(isel, True)
      if (master_bool_sel & full_sh_selection).iselection().size() == 0:
        log.write("    skipping, not in NCS master.\n")
        continue
      n_idealized_elements += 1
      log.write("    substitute with idealized one.\n")
      for st in sh.strands:
        selstring = st.as_atom_selections()
        isel = selection_cache.iselection(selstring)
        all_bsel = flex.bool(n_atoms_in_real_h, False)
        all_bsel.set_selected(isel, True)
        fixed_ss_selection.set_selected(isel, True)
        sel_h = model.get_hierarchy().select(all_bsel, copy_atoms=True)
        ideal_h = secondary_structure_from_sequence(
            pdb_str=beta_pdb_str,
            sequence=None,
            pdb_hierarchy_template=sel_h,
            rotamer_manager=model.get_rotamer_manager(),
            )
        set_xyz_carefully(edited_h.select(all_bsel), ideal_h)
        # edited_h.select(all_bsel).atoms().set_xyz(ideal_h.atoms().extract_xyz())
  if n_idealized_elements == 0:
    log.write("Nothing was idealized.\n")
    # Don't do geometry minimization and stuff if nothing was changed.
    return None

  # XXX here we want to adopt new coordinates
  model.set_sites_cart(sites_cart=edited_h.atoms().extract_xyz())
  if model.ncs_constraints_present():
    model.set_sites_cart_from_hierarchy(multiply_ncs=True)

  t3 = time()
  # pre_result_h = edited_h
  # pre_result_h.reset_i_seq_if_necessary()
  bsel = flex.bool(n_atoms_in_real_h, False)
  helix_selection = flex.bool(n_atoms_in_real_h, False)
  sheet_selection = flex.bool(n_atoms_in_real_h, False)
  other_selection = flex.bool(n_atoms_in_real_h, False)
  ss_for_tors_selection = flex.bool(n_atoms_in_real_h, False)
  nonss_for_tors_selection = flex.bool(n_atoms_in_real_h, False)
  # set all CA atoms to True for other_selection
  #isel = selection_cache.iselection("name ca")
  isel = selection_cache.iselection("name ca or name n or name o or name c")
  other_selection.set_selected(isel, True)
  n_main_chain_atoms = other_selection.count(True)
  isel = selection_cache.iselection("name ca or name n or name o or name c")
  nonss_for_tors_selection.set_selected(isel, True)
  main_chain_selection_prefix = "(name ca or name n or name o or name c) %s"

  t4 = time()
  print >> log, "Preparing selections..."
  log.flush()
  # Here we are just preparing selections
  for h in ann.helices:
    ss_sels = h.as_atom_selections()[0]
    selstring = main_chain_selection_prefix % ss_sels
    isel = selection_cache.iselection(selstring)
    helix_selection.set_selected(isel, True)
    other_selection.set_selected(isel, False)
    isel = selection_cache.iselection(selstring)
    ss_for_tors_selection.set_selected(isel, True)
    nonss_for_tors_selection.set_selected(isel, False)

  for sheet in ann.sheets:
    for ss_sels in sheet.as_atom_selections():
      selstring = main_chain_selection_prefix % ss_sels
      isel = selection_cache.iselection(selstring)
      sheet_selection.set_selected(isel, True)
      other_selection.set_selected(isel, False)
      isel = selection_cache.iselection(selstring)
      ss_for_tors_selection.set_selected(isel, True)
      nonss_for_tors_selection.set_selected(isel, False)
  t5 = time()
  isel = selection_cache.iselection(
      "not name ca and not name n and not name o and not name c")
  other_selection.set_selected(isel, False)
  helix_sheet_intersection = helix_selection & sheet_selection
  if helix_sheet_intersection.count(True) > 0:
    sheet_selection = sheet_selection & ~helix_sheet_intersection
  assert ((helix_selection | sheet_selection) & other_selection).count(True)==0

  from mmtbx.monomer_library.pdb_interpretation import grand_master_phil_str
  params_line = grand_master_phil_str
  params_line += "secondary_structure {%s}" % secondary_structure.sec_str_master_phil_str
  # print "params_line"
  # print params_line
  params = iotbx.phil.parse(input_string=params_line, process_includes=True)#.extract()
  # This does not work the same way for a strange reason. Need to investigate.
  # The number of resulting hbonds is different later.
  # w_params = params.extract()
  # w_params.pdb_interpretation.secondary_structure.protein.remove_outliers = False
  # w_params.pdb_interpretation.peptide_link.ramachandran_restraints = True
  # w_params.pdb_interpretation.c_beta_restraints = True
  # w_params.pdb_interpretation.secondary_structure.enabled = True
  # params.format(python_object=w_params)
  # params.show()
  # print "="*80
  # print "="*80
  # print "="*80
  grm = model.get_restraints_manager()
  ssm_log = null_out()
  if verbose:
    ssm_log = log
  ss_params = secondary_structure.sec_str_master_phil.fetch().extract()
  ss_params.secondary_structure.protein.remove_outliers=False
  ss_manager = secondary_structure.manager(
      pdb_hierarchy=model.get_hierarchy(),
      geometry_restraints_manager=grm.geometry,
      sec_str_from_pdb_file=ss_annotation,
      params=ss_params.secondary_structure,
      mon_lib_srv=None,
      verbose=-1,
      log=ssm_log)
  grm.geometry.set_secondary_structure_restraints(
      ss_manager=ss_manager,
      hierarchy=model.get_hierarchy(),
      log=ssm_log)
  model.get_hierarchy().reset_i_seq_if_necessary()
  from mmtbx.geometry_restraints import reference
  if reference_map is None:
    if verbose:
      print >> log, "Adding reference coordinate restraints..."
    grm.geometry.append_reference_coordinate_restraints_in_place(
        reference.add_coordinate_restraints(
            sites_cart = model.get_sites_cart().select(helix_selection),
            selection  = helix_selection,
            sigma      = processed_params.sigma_on_reference_helix))
    grm.geometry.append_reference_coordinate_restraints_in_place(
        reference.add_coordinate_restraints(
            sites_cart = model.get_sites_cart().select(sheet_selection),
            selection  = sheet_selection,
            sigma      = processed_params.sigma_on_reference_sheet))
    grm.geometry.append_reference_coordinate_restraints_in_place(
        reference.add_coordinate_restraints(
            sites_cart = model.get_sites_cart().select(other_selection),
            selection  = other_selection,
            sigma      = processed_params.sigma_on_reference_non_ss))

  # XXX Somewhere here we actually should check placed side-chains for
  # clashes because we used ones that were in original model and just moved
  # them to nearest allowed rotamer. The idealization may affect a lot
  # the orientation of side chain thus justifying changing rotamer on it
  # to avoid clashes.
  if check_rotamer_clashes:
    print >> log, "Fixing/checking rotamers..."
    # pre_result_h.write_pdb_file(file_name="before_rotamers.pdb")
    br_txt = model.model_as_pdb()
    with open("before_rotamers.pdb", 'w') as f:
      f.write(br_txt)
    mmtbx.utils.fix_rotamer_outliers(
      model = model,
      map_data=reference_map,
      radius=5,
      backrub_range=None, # don't sample backrub at this point
      non_outliers_to_check=fixed_ss_selection, # bool selection
      verbose=True,
      log=log)

  if verbose:
    print >> log, "Adding chi torsion restraints..."
  # only backbone
  grm.geometry.add_chi_torsion_restraints_in_place(
          pdb_hierarchy   = model.get_hierarchy(),
          sites_cart      = model.get_sites_cart().\
                                 select(ss_for_tors_selection),
          selection = ss_for_tors_selection,
          chi_angles_only = False,
          sigma           = processed_params.sigma_on_torsion_ss)
  grm.geometry.add_chi_torsion_restraints_in_place(
          pdb_hierarchy   = model.get_hierarchy(),
          sites_cart      = model.get_sites_cart().\
                                select(nonss_for_tors_selection),
          selection = nonss_for_tors_selection,
          chi_angles_only = False,
          sigma           = processed_params.sigma_on_torsion_nonss)

  # real_h.atoms().set_xyz(pre_result_h.atoms().extract_xyz())
  #
  # Check and correct for special positions
  #
  real_h = model.get_hierarchy() # just a shortcut here...
  special_position_settings = crystal.special_position_settings(
      crystal_symmetry = model.crystal_symmetry())
  site_symmetry_table = \
      special_position_settings.site_symmetry_table(
        sites_cart = model.get_sites_cart(),
        unconditional_general_position_flags=(
          model.get_atoms().extract_occ() != 1))
  spi = site_symmetry_table.special_position_indices()
  if spi.size() > 0:
    print >> log, "Moving atoms from special positions:"
    for spi_i in spi:
      if spi_i not in original_spi:
        new_coords = (
            real_h.atoms()[spi_i].xyz[0]+0.2,
            real_h.atoms()[spi_i].xyz[1]+0.2,
            real_h.atoms()[spi_i].xyz[2]+0.2)
        print >> log, "  ", real_h.atoms()[spi_i].id_str(),
        print >> log, tuple(real_h.atoms()[spi_i].xyz), "-->", new_coords
        real_h.atoms()[spi_i].set_xyz(new_coords)
  model.set_sites_cart_from_hierarchy()


  t9 = time()
  if processed_params.file_name_before_regularization is not None:
    grm.geometry.pair_proxies(sites_cart=model.get_sites_cart())
    if grm.geometry.ramachandran_manager is not None:
      grm.geometry.ramachandran_manager.update_phi_psi_targets(
          sites_cart=model.get_sites_cart())
    print >> log, "Outputting model before regularization %s" % processed_params.file_name_before_regularization

    m_txt = model.model_as_pdb()
    g_txt = model.restraints_as_geo()
    with open(processed_params.file_name_before_regularization, 'w') as f:
      f.write(m_txt)

    geo_fname = processed_params.file_name_before_regularization[:-4]+'.geo'
    print >> log, "Outputting geo file for regularization %s" % geo_fname
    with open(geo_fname, 'w') as f:
      f.write(g_txt)

  #testing number of restraints
  assert grm.geometry.get_n_den_proxies() == 0
  if reference_map is None:
    assert grm.geometry.get_n_reference_coordinate_proxies() == n_main_chain_atoms, "" +\
        "%d %d" % (grm.geometry.get_n_reference_coordinate_proxies(), n_main_chain_atoms)
  refinement_log = null_out()
  log.write(
      "Refining geometry of substituted secondary structure elements...")
  log.flush()
  if verbose:
    refinement_log = log
  t10 = time()
  if reference_map is None:
    minimize_wrapper_for_ramachandran(
        model = model,
        original_pdb_h = None,
        excl_string_selection = "",
        log = refinement_log,
        number_of_cycles = processed_params.n_iter)
  else:
    ref_xrs = model.crystal_symmetry()
    minimize_wrapper_with_map(
        model = model,
        target_map=reference_map,
        refine_ncs_operators=False,
        number_of_cycles=processed_params.n_macro,
        log=log)
  model.set_sites_cart_from_hierarchy()

  log.write(" Done\n")
  log.flush()
  t11 = time()
  # print >> log, "Initial checking, init   : %.4f" % (t1-t0)
  # print >> log, "Checking SS              : %.4f" % (t2-t1)
  # print >> log, "Initializing selections  : %.4f" % (t4-t3)
  # print >> log, "Looping for selections   : %.4f" % (t5-t4)
  # print >> log, "Finalizing selections    : %.4f" % (t6-t5)
  # print >> log, "PDB interpretation       : %.4f" % (t7-t6)
  # print >> log, "Get GRM                  : %.4f" % (t8-t7)
  # print >> log, "Adding restraints to GRM : %.4f" % (t9-t8)
  # print >> log, "Running GM               : %.4f" % (t11-t10)
  # print_hbond_proxies(grm.geometry,real_h)
  grm.geometry.remove_reference_coordinate_restraints_in_place()
  return grm.geometry.get_chi_torsion_proxies()
Exemplo n.º 11
0
def exercise_2():
  for use_reference in [True, False, None]:
    pdb_inp = iotbx.pdb.input(
        lines=flex.std_string(pdb_str_2.splitlines()), source_info=None)
    model = manager(
        model_input=pdb_inp,
        log=null_out())
    grm = model.get_restraints_manager().geometry
    xrs2 = model.get_xray_structure()
    awl2 = model.get_hierarchy().atoms_with_labels()
    pdb_inp3 = iotbx.pdb.input(source_info=None, lines=pdb_str_3)
    xrs3 = pdb_inp3.xray_structure_simple()
    ph3 = pdb_inp3.construct_hierarchy()
    ph3.atoms().reset_i_seq()
    awl3 =  ph3.atoms_with_labels()
    sites_cart_reference = flex.vec3_double()
    selection = flex.size_t()
    reference_names = ["CG", "CD", "NE", "CZ", "NH1", "NH2"]
    for a2,a3 in zip(tuple(awl2), tuple(awl3)):
      assert a2.resname == a3.resname
      assert a2.name == a3.name
      assert a2.i_seq == a3.i_seq
      if(a2.resname == "ARG" and a2.name.strip() in reference_names):
        selection.append(a2.i_seq)
        sites_cart_reference.append(a3.xyz)
    assert selection.size() == len(reference_names)
    selection_bool = flex.bool(xrs2.scatterers().size(), selection)
    if(use_reference):
      grm.adopt_reference_coordinate_restraints_in_place(
          reference.add_coordinate_restraints(
              sites_cart = sites_cart_reference,
              selection = selection,
              sigma = 0.01))
    elif(use_reference is None):
      grm.adopt_reference_coordinate_restraints_in_place(
          reference.add_coordinate_restraints(
              sites_cart = sites_cart_reference,
              selection = selection,
              sigma = 0.01))
      grm.remove_reference_coordinate_restraints_in_place(
          selection = selection)
    d1 = flex.mean(flex.sqrt((xrs2.sites_cart().select(selection) -
                              xrs3.sites_cart().select(selection)).dot()))
    print("distance start (use_reference: %s): %6.4f"%(str(use_reference), d1))
    assert d1>4.0
    assert approx_equal(
      flex.max(flex.sqrt((xrs2.sites_cart().select(~selection_bool) -
                          xrs3.sites_cart().select(~selection_bool)).dot())), 0)
    from cctbx import geometry_restraints
    import mmtbx.refinement.geometry_minimization
    import scitbx.lbfgs
    grf = geometry_restraints.flags.flags(default=True)
    sites_cart = xrs2.sites_cart()
    minimized = mmtbx.refinement.geometry_minimization.lbfgs(
      sites_cart                  = sites_cart,
      correct_special_position_tolerance=1.0,
      geometry_restraints_manager = grm,
      sites_cart_selection        = flex.bool(sites_cart.size(), selection),
      geometry_restraints_flags   = grf,
      lbfgs_termination_params = scitbx.lbfgs.termination_parameters(
        max_iterations=5000))
    xrs2.set_sites_cart(sites_cart = sites_cart)
    d2 = flex.mean(flex.sqrt((xrs2.sites_cart().select(selection) -
                              xrs3.sites_cart().select(selection)).dot()))
    print("distance final (use_reference: %s): %6.4f"%(str(use_reference), d2))
    if(use_reference): assert d2<0.005, "failed: %f<0.05" % d2
    else: assert d2>4.0, d2
    assert approx_equal(
      flex.max(flex.sqrt((xrs2.sites_cart().select(~selection_bool) -
                          xrs3.sites_cart().select(~selection_bool)).dot())), 0)
Exemplo n.º 12
0
def substitute_ss(real_h,
                    xray_structure,
                    ss_annotation,
                    params = None,
                    grm=None,
                    use_plane_peptide_bond_restr=True,
                    fix_rotamer_outliers=True,
                    cif_objects=None,
                    log=null_out(),
                    rotamer_manager=None,
                    reference_map=None,
                    verbose=False):
  """
  Substitute secondary structure elements in real_h hierarchy with ideal
  ones _in_place_.
  Returns reference torsion proxies - the only thing that cannot be restored
  with little effort outside the procedure.
  real_h - hierarcy to substitute secondary structure elements.
  xray_structure - xray_structure - needed to get crystal symmetry (to
      construct processed_pdb_file and xray_structure is needed to call
      get_geometry_restraints_manager for no obvious reason).
  ss_annotation - iotbx.pdb.annotation object.
  """
  t0 = time()
  if rotamer_manager is None:
    rotamer_manager = RotamerEval()
  for model in real_h.models():
    for chain in model.chains():
      if len(chain.conformers()) > 1:
        raise Sorry("Alternative conformations are not supported.")

  processed_params = process_params(params)
  if not processed_params.enabled:
    return None

  expected_n_hbonds = 0
  ann = ss_annotation
  for h in ann.helices:
    expected_n_hbonds += h.get_n_maximum_hbonds()
  edited_h = real_h.deep_copy()
  n_atoms_in_real_h = real_h.atoms_size()
  selection_cache = real_h.atom_selection_cache()

  # check the annotation for correctness (atoms are actually in hierarchy)
  error_msg = "The following secondary structure annotations result in \n"
  error_msg +="empty atom selections. They don't match the structre: \n"
  t1 = time()
  # Checking for SS selections
  deleted_annotations = ann.remove_empty_annotations(
      hierarchy=real_h,
      asc=selection_cache)
  if not deleted_annotations.is_empty():
    if processed_params.skip_empty_ss_elements:
      if len(deleted_annotations.helices) > 0:
        print >> log, "Removing the following helices because there are"
        print >> log, "no corresponding atoms in the model:"
        for h in deleted_annotations.helices:
          print >> log, h.as_pdb_str()
          error_msg += "  %s\n" % h
      if len(deleted_annotations.sheets) > 0:
        print >> log, "Removing the following sheets because there are"
        print >> log, "no corresponding atoms in the model:"
        for sh in deleted_annotations.sheets:
          print >> log, sh.as_pdb_str()
          error_msg += "  %s\n" % sh.as_pdb_str(strand_id=st.strand_id)
    else:
      raise Sorry(error_msg)
  phil_str = ann.as_restraint_groups()

  t2 = time()
  # Actually idelizing SS elements
  log.write("Replacing ss-elements with ideal ones:\n")
  log.flush()
  for h in ann.helices:
    log.write("  %s\n" % h.as_pdb_str())
    log.flush()
    selstring = h.as_atom_selections()
    isel = selection_cache.iselection(selstring[0])
    all_bsel = flex.bool(n_atoms_in_real_h, False)
    all_bsel.set_selected(isel, True)
    sel_h = real_h.select(all_bsel, copy_atoms=True)
    ideal_h = get_helix(helix_class=h.helix_class,
                        pdb_hierarchy_template=sel_h,
                        rotamer_manager=rotamer_manager)
    # edited_h.select(all_bsel).atoms().set_xyz(ideal_h.atoms().extract_xyz())
    set_xyz_carefully(dest_h=edited_h.select(all_bsel), source_h=ideal_h)
  for sh in ann.sheets:
    s = "  %s\n" % sh.as_pdb_str()
    ss = s.replace("\n", "\n  ")
    log.write(ss[:-2])
    log.flush()
    for st in sh.strands:
      selstring = st.as_atom_selections()
      isel = selection_cache.iselection(selstring)
      all_bsel = flex.bool(n_atoms_in_real_h, False)
      all_bsel.set_selected(isel, True)
      sel_h = real_h.select(all_bsel, copy_atoms=True)
      ideal_h = secondary_structure_from_sequence(
          pdb_str=beta_pdb_str,
          sequence=None,
          pdb_hierarchy_template=sel_h,
          rotamer_manager=rotamer_manager,
          )
      set_xyz_carefully(edited_h.select(all_bsel), ideal_h)
      # edited_h.select(all_bsel).atoms().set_xyz(ideal_h.atoms().extract_xyz())

  t3 = time()
  pre_result_h = edited_h
  pre_result_h.reset_i_seq_if_necessary()
  n_atoms = real_h.atoms_size()
  bsel = flex.bool(n_atoms, False)
  helix_selection = flex.bool(n_atoms, False)
  sheet_selection = flex.bool(n_atoms, False)
  other_selection = flex.bool(n_atoms, False)
  ss_for_tors_selection = flex.bool(n_atoms, False)
  nonss_for_tors_selection = flex.bool(n_atoms, False)
  selection_cache = real_h.atom_selection_cache()
  # set all CA atoms to True for other_selection
  #isel = selection_cache.iselection("name ca")
  isel = selection_cache.iselection("name ca or name n or name o or name c")
  other_selection.set_selected(isel, True)
  n_main_chain_atoms = other_selection.count(True)
  isel = selection_cache.iselection("name ca or name n or name o or name c")
  nonss_for_tors_selection.set_selected(isel, True)
  main_chain_selection_prefix = "(name ca or name n or name o or name c) %s"

  t4 = time()
  print >> log, "Preparing selections..."
  log.flush()
  # Here we are just preparing selections
  for h in ann.helices:
    ss_sels = h.as_atom_selections()[0]
    selstring = main_chain_selection_prefix % ss_sels
    isel = selection_cache.iselection(selstring)
    helix_selection.set_selected(isel, True)
    other_selection.set_selected(isel, False)
    isel = selection_cache.iselection(selstring)
    ss_for_tors_selection.set_selected(isel, True)
    nonss_for_tors_selection.set_selected(isel, False)

  for sheet in ann.sheets:
    for ss_sels in sheet.as_atom_selections():
      selstring = main_chain_selection_prefix % ss_sels
      isel = selection_cache.iselection(selstring)
      sheet_selection.set_selected(isel, True)
      other_selection.set_selected(isel, False)
      isel = selection_cache.iselection(selstring)
      ss_for_tors_selection.set_selected(isel, True)
      nonss_for_tors_selection.set_selected(isel, False)
  t5 = time()
  isel = selection_cache.iselection(
      "not name ca and not name n and not name o and not name c")
  other_selection.set_selected(isel, False)
  helix_sheet_intersection = helix_selection & sheet_selection
  if helix_sheet_intersection.count(True) > 0:
    sheet_selection = sheet_selection & ~helix_sheet_intersection
  assert ((helix_selection | sheet_selection) & other_selection).count(True)==0

  from mmtbx.monomer_library.pdb_interpretation import grand_master_phil_str
  params_line = grand_master_phil_str
  params_line += "secondary_structure {%s}" % secondary_structure.sec_str_master_phil_str
  # print "params_line"
  # print params_line
  params = iotbx.phil.parse(input_string=params_line, process_includes=True)#.extract()
  # This does not the same way for a strange reason. Need to investigate.
  # The number of resulting hbonds is different later.
  # w_params = params.extract()
  # w_params.pdb_interpretation.secondary_structure.protein.remove_outliers = False
  # w_params.pdb_interpretation.peptide_link.ramachandran_restraints = True
  # w_params.pdb_interpretation.c_beta_restraints = True
  # w_params.pdb_interpretation.secondary_structure.enabled = True
  # params.format(python_object=w_params)
  # params.show()
  # print "="*80
  # print "="*80
  # print "="*80
  if grm is None:
    custom_par_text = "\n".join([
        "pdb_interpretation.secondary_structure {protein.remove_outliers = False\n%s}" \
            % phil_str,
        "pdb_interpretation.peptide_link.ramachandran_restraints = True",
        "c_beta_restraints = True",
        "pdb_interpretation.secondary_structure.enabled=True",
        "pdb_interpretation.clash_guard.nonbonded_distance_threshold=None",
        "pdb_interpretation.max_reasonable_bond_distance=None",
        # "pdb_interpretation.nonbonded_weight=500",
        "pdb_interpretation.peptide_link.oldfield.weight_scale=3",
        "pdb_interpretation.peptide_link.oldfield.plot_cutoff=0.03",
        "pdb_interpretation.peptide_link.omega_esd_override_value=3",
        "pdb_interpretation.peptide_link.apply_all_trans=True",
        ])

    if use_plane_peptide_bond_restr:
      custom_par_text += "\npdb_interpretation.peptide_link.apply_peptide_plane=True"

    custom_pars = params.fetch(
        source=iotbx.phil.parse(custom_par_text)).extract()
    # params.format(python_object=custom_pars)
    # params.show()
    # STOP()
    params = custom_pars
    # params = w_params

    t6 = time()
    import mmtbx.utils
    processed_pdb_files_srv = mmtbx.utils.\
        process_pdb_file_srv(
            crystal_symmetry= xray_structure.crystal_symmetry(),
            pdb_interpretation_params = params.pdb_interpretation,
            log=null_out(),
            cif_objects=cif_objects)
    if verbose:
      print >> log, "Processing file..."
      log.flush()
    processed_pdb_file, junk = processed_pdb_files_srv.\
        process_pdb_files(raw_records=flex.split_lines(real_h.as_pdb_string()))
    t7 = time()

    grm = get_geometry_restraints_manager(
      processed_pdb_file, xray_structure)
    t8 = time()
  else:
    ss_manager = secondary_structure.manager(
        pdb_hierarchy=real_h,
        geometry_restraints_manager=grm.geometry,
        sec_str_from_pdb_file=ss_annotation,
        params=None,
        mon_lib_srv=None,
        verbose=-1,
        log=log)
    grm.geometry.set_secondary_structure_restraints(
        ss_manager=ss_manager,
        hierarchy=real_h,
        log=log)
  real_h.reset_i_seq_if_necessary()
  from mmtbx.geometry_restraints import reference
  if reference_map is None:
    if verbose:
      print >> log, "Adding reference coordinate restraints..."
    grm.geometry.append_reference_coordinate_restraints_in_place(
        reference.add_coordinate_restraints(
            sites_cart = real_h.atoms().extract_xyz().select(helix_selection),
            selection  = helix_selection,
            sigma      = processed_params.sigma_on_reference_helix))
    grm.geometry.append_reference_coordinate_restraints_in_place(
        reference.add_coordinate_restraints(
            sites_cart = real_h.atoms().extract_xyz().select(sheet_selection),
            selection  = sheet_selection,
            sigma      = processed_params.sigma_on_reference_sheet))
    grm.geometry.append_reference_coordinate_restraints_in_place(
        reference.add_coordinate_restraints(
            sites_cart = real_h.atoms().extract_xyz().select(other_selection),
            selection  = other_selection,
            sigma      = processed_params.sigma_on_reference_non_ss))
  if verbose:
    print >> log, "Adding chi torsion restraints..."
  grm.geometry.add_chi_torsion_restraints_in_place(
          pdb_hierarchy   = pre_result_h,
          sites_cart      = pre_result_h.atoms().extract_xyz().\
                                 select(ss_for_tors_selection),
          selection = ss_for_tors_selection,
          chi_angles_only = False,
          sigma           = processed_params.sigma_on_torsion_ss)
  grm.geometry.add_chi_torsion_restraints_in_place(
          pdb_hierarchy   = pre_result_h,
          sites_cart      = real_h.atoms().extract_xyz().\
                                select(nonss_for_tors_selection),
          selection = nonss_for_tors_selection,
          chi_angles_only = False,
          sigma           = processed_params.sigma_on_torsion_nonss)

  real_h.atoms().set_xyz(pre_result_h.atoms().extract_xyz())
  t9 = time()
  if processed_params.file_name_before_regularization is not None:
    grm.geometry.pair_proxies(sites_cart=real_h.atoms().extract_xyz())
    if grm.geometry.ramachandran_manager is not None:
      grm.geometry.ramachandran_manager.update_phi_psi_targets(
          sites_cart=real_h.atoms().extract_xyz())
    print >> log, "Outputting model before regularization %s" % processed_params.file_name_before_regularization
    real_h.write_pdb_file(
        file_name=processed_params.file_name_before_regularization)
    geo_fname = processed_params.file_name_before_regularization[:-4]+'.geo'
    print >> log, "Outputting geo file for regularization %s" % geo_fname
    grm.write_geo_file(
        site_labels=[atom.id_str() for atom in real_h.atoms()],
        file_name=geo_fname)

  #testing number of restraints
  assert grm.geometry.get_n_den_proxies() == 0
  if reference_map is None:
    assert grm.geometry.get_n_reference_coordinate_proxies() == n_main_chain_atoms
  refinement_log = null_out()
  log.write(
      "Refining geometry of substituted secondary structure elements...")
  log.flush()
  if verbose:
    refinement_log = log
  from mmtbx.refinement.geometry_minimization import run2
  t10 = time()
  if reference_map is None:
    obj = run2(
        restraints_manager       = grm,
        pdb_hierarchy            = real_h,
        correct_special_position_tolerance = 1.0,
        max_number_of_iterations = processed_params.n_iter,
        number_of_macro_cycles   = processed_params.n_macro,
        bond                     = True,
        nonbonded                = True,
        angle                    = True,
        dihedral                 = True,
        chirality                = True,
        planarity                = True,
        fix_rotamer_outliers     = fix_rotamer_outliers,
        log                      = refinement_log)
  else:
    ref_xrs = real_h.extract_xray_structure(
        crystal_symmetry=xray_structure.crystal_symmetry())
    minimize_wrapper_with_map(
        pdb_h=real_h,
        xrs=ref_xrs,
        target_map=reference_map,
        grm=grm,
        ncs_restraints_group_list=[],
        mon_lib_srv=None,
        ss_annotation=ss_annotation,
        refine_ncs_operators=False,
        number_of_cycles=processed_params.n_macro,
        log=log)
    real_h.write_pdb_file("after_ss_map_min.pdb")

  log.write(" Done\n")
  log.flush()
  t11 = time()
  # print >> log, "Initial checking, init   : %.4f" % (t1-t0)
  # print >> log, "Checking SS              : %.4f" % (t2-t1)
  # print >> log, "Initializing selections  : %.4f" % (t4-t3)
  # print >> log, "Looping for selections   : %.4f" % (t5-t4)
  # print >> log, "Finalizing selections    : %.4f" % (t6-t5)
  # print >> log, "PDB interpretation       : %.4f" % (t7-t6)
  # print >> log, "Get GRM                  : %.4f" % (t8-t7)
  # print >> log, "Adding restraints to GRM : %.4f" % (t9-t8)
  # print >> log, "Running GM               : %.4f" % (t11-t10)
  # print_hbond_proxies(grm.geometry,real_h)
  return grm.geometry.get_chi_torsion_proxies()
Exemplo n.º 13
0
  def whole_minimization(self):
    t3 = time()
    # pre_result_h = edited_h
    # pre_result_h.reset_i_seq_if_necessary()
    bsel = flex.bool(self.model.get_number_of_atoms(), False)
    helix_selection = flex.bool(self.model.get_number_of_atoms(), False)
    sheet_selection = flex.bool(self.model.get_number_of_atoms(), False)
    other_selection = flex.bool(self.model.get_number_of_atoms(), False)
    ss_for_tors_selection = flex.bool(self.model.get_number_of_atoms(), False)
    nonss_for_tors_selection = flex.bool(self.model.get_number_of_atoms(), False)
    # set all CA atoms to True for other_selection
    #isel = self.model.get_atom_selection_cache().iselection("name ca")
    isel = self.model.get_atom_selection_cache().iselection("name ca or name n or name o or name c")
    other_selection.set_selected(isel, True)
    n_main_chain_atoms = other_selection.count(True)
    isel = self.model.get_atom_selection_cache().iselection("name ca or name n or name o or name c")
    nonss_for_tors_selection.set_selected(isel, True)
    main_chain_selection_prefix = "(name ca or name n or name o or name c) %s"

    t4 = time()
    print("Preparing selections...", file=self.log)
    self.log.flush()
    # Here we are just preparing selections
    for h in self.ss_annotation.helices:
      ss_sels = h.as_atom_selections()[0]
      selstring = main_chain_selection_prefix % ss_sels
      isel = self.model.get_atom_selection_cache().iselection(selstring)
      helix_selection.set_selected(isel, True)
      other_selection.set_selected(isel, False)
      isel = self.model.get_atom_selection_cache().iselection(selstring)
      ss_for_tors_selection.set_selected(isel, True)
      nonss_for_tors_selection.set_selected(isel, False)

    for sheet in self.ss_annotation.sheets:
      for ss_sels in sheet.as_atom_selections():
        selstring = main_chain_selection_prefix % ss_sels
        isel = self.model.get_atom_selection_cache().iselection(selstring)
        sheet_selection.set_selected(isel, True)
        other_selection.set_selected(isel, False)
        isel = self.model.get_atom_selection_cache().iselection(selstring)
        ss_for_tors_selection.set_selected(isel, True)
        nonss_for_tors_selection.set_selected(isel, False)
    t5 = time()
    # print("N idealized elements: %d" % n_idealized_elements, file=self.log)
    # print("Initial checking, init   : %.4f" % (t1-t0), file=self.log)
    # print("Checking SS              : %.4f" % (t2-t1), file=self.log)
    # print("Changing SS              : %.4f" % (t3-t2), file=self.log)
    # print("Initializing selections  : %.4f" % (t4-t3), file=self.log)
    # print("Looping for selections   : %.4f" % (t5-t4), file=self.log)
    # with open('idealized.pdb', 'w') as f:
    #   f.write(self.model.model_as_pdb())
    # return
    isel = self.model.get_atom_selection_cache().iselection(
        "not name ca and not name n and not name o and not name c")
    other_selection.set_selected(isel, False)
    helix_sheet_intersection = helix_selection & sheet_selection
    if helix_sheet_intersection.count(True) > 0:
      sheet_selection = sheet_selection & ~helix_sheet_intersection
    assert ((helix_selection | sheet_selection) & other_selection).count(True)==0

    from mmtbx.monomer_library.pdb_interpretation import grand_master_phil_str
    params_line = grand_master_phil_str
    params_line += "secondary_structure {%s}" % secondary_structure.sec_str_master_phil_str
    # print "params_line"
    # print params_line
    params = iotbx.phil.parse(input_string=params_line, process_includes=True)#.extract()
    # This does not work the same way for a strange reason. Need to investigate.
    # The number of resulting hbonds is different later.
    # w_params = params.extract()
    # w_params.pdb_interpretation.secondary_structure.protein.remove_outliers = False
    # w_params.pdb_interpretation.peptide_link.ramachandran_restraints = True
    # w_params.pdb_interpretation.c_beta_restraints = True
    # w_params.pdb_interpretation.secondary_structure.enabled = True
    # params.format(python_object=w_params)
    # params.show()
    # print "="*80
    # print "="*80
    # print "="*80
    grm = self.model.get_restraints_manager()
    ssm_log = null_out()
    if self.processed_params.verbose:
      ssm_log = self.log
    ss_params = secondary_structure.sec_str_master_phil.fetch().extract()
    ss_params.secondary_structure.protein.remove_outliers=False
    ss_manager = secondary_structure.manager(
        pdb_hierarchy=self.model.get_hierarchy(),
        geometry_restraints_manager=grm.geometry,
        sec_str_from_pdb_file=self.ss_annotation,
        params=ss_params.secondary_structure,
        mon_lib_srv=None,
        verbose=-1,
        log=ssm_log)
    grm.geometry.set_secondary_structure_restraints(
        ss_manager=ss_manager,
        hierarchy=self.model.get_hierarchy(),
        log=ssm_log)
    self.model.get_hierarchy().reset_i_seq_if_necessary()
    from mmtbx.geometry_restraints import reference
    if self.reference_map is None:
      if self.processed_params.verbose:
        print("Adding reference coordinate restraints...", file=self.log)
      grm.geometry.append_reference_coordinate_restraints_in_place(
          reference.add_coordinate_restraints(
              sites_cart = self.model.get_sites_cart().select(helix_selection),
              selection  = helix_selection,
              sigma      = self.processed_params.sigma_on_reference_helix))
      grm.geometry.append_reference_coordinate_restraints_in_place(
          reference.add_coordinate_restraints(
              sites_cart = self.model.get_sites_cart().select(sheet_selection),
              selection  = sheet_selection,
              sigma      = self.processed_params.sigma_on_reference_sheet))
      grm.geometry.append_reference_coordinate_restraints_in_place(
          reference.add_coordinate_restraints(
              sites_cart = self.model.get_sites_cart().select(other_selection),
              selection  = other_selection,
              sigma      = self.processed_params.sigma_on_reference_non_ss))

    # XXX Somewhere here we actually should check placed side-chains for
    # clashes because we used ones that were in original model and just moved
    # them to nearest allowed rotamer. The idealization may affect a lot
    # the orientation of side chain thus justifying changing rotamer on it
    # to avoid clashes.
    if self.processed_params.fix_rotamer_outliers:
      print("Fixing/checking rotamers...", file=self.log)
      # pre_result_h.write_pdb_file(file_name="before_rotamers.pdb")
      br_txt = self.model.model_as_pdb()
      with open("before_rotamers.pdb", 'w') as f:
        f.write(br_txt)
      if(self.reference_map is None):
        backbone_sample=False
      else:
        backbone_sample=True
      result = mmtbx.refinement.real_space.fit_residues.run(
          pdb_hierarchy     = self.model.get_hierarchy(),
          crystal_symmetry  = self.model.crystal_symmetry(),
          map_data          = self.reference_map,
          rotamer_manager   = mmtbx.idealized_aa_residues.rotamer_manager.load(
            rotamers="favored"),
          sin_cos_table     = scitbx.math.sin_cos_table(n=10000),
          backbone_sample   = backbone_sample,
          mon_lib_srv       = self.model.get_mon_lib_srv(),
          log               = self.log)
      self.model.set_sites_cart(
          sites_cart = result.pdb_hierarchy.atoms().extract_xyz())

    if self.processed_params.verbose:
      print("Adding chi torsion restraints...", file=self.log)
    # only backbone
    grm.geometry.add_chi_torsion_restraints_in_place(
            pdb_hierarchy   = self.model.get_hierarchy(),
            sites_cart      = self.model.get_sites_cart().\
                                   select(ss_for_tors_selection),
            selection = ss_for_tors_selection,
            chi_angles_only = False,
            sigma           = self.processed_params.sigma_on_torsion_ss)
    grm.geometry.add_chi_torsion_restraints_in_place(
            pdb_hierarchy   = self.model.get_hierarchy(),
            sites_cart      = self.model.get_sites_cart().\
                                  select(nonss_for_tors_selection),
            selection = nonss_for_tors_selection,
            chi_angles_only = False,
            sigma           = self.processed_params.sigma_on_torsion_nonss)

    # real_h.atoms().set_xyz(pre_result_h.atoms().extract_xyz())
    #
    # Check and correct for special positions
    #
    real_h = self.model.get_hierarchy() # just a shortcut here...
    special_position_settings = crystal.special_position_settings(
        crystal_symmetry = self.model.crystal_symmetry())
    site_symmetry_table = \
        special_position_settings.site_symmetry_table(
          sites_cart = self.model.get_sites_cart(),
          unconditional_general_position_flags=(
            self.model.get_atoms().extract_occ() != 1))
    spi = site_symmetry_table.special_position_indices()
    if spi.size() > 0:
      print("Moving atoms from special positions:", file=self.log)
      for spi_i in spi:
        if spi_i not in self.original_spi:
          new_coords = (
              real_h.atoms()[spi_i].xyz[0]+0.2,
              real_h.atoms()[spi_i].xyz[1]+0.2,
              real_h.atoms()[spi_i].xyz[2]+0.2)
          print("  ", real_h.atoms()[spi_i].id_str(), end=' ', file=self.log)
          print(tuple(real_h.atoms()[spi_i].xyz), "-->", new_coords, file=self.log)
          real_h.atoms()[spi_i].set_xyz(new_coords)
    self.model.set_sites_cart_from_hierarchy()

    self.model_before_regularization = self.model.deep_copy()

    t9 = time()

    if self.processed_params.file_name_before_regularization is not None:
      grm.geometry.pair_proxies(sites_cart=self.model.get_sites_cart())
      grm.geometry.update_ramachandran_restraints_phi_psi_targets(
          hierarchy=self.model.get_hierarchy())
      print("Outputting model before regularization %s" % self.processed_params.file_name_before_regularization, file=self.log)

      m_txt = self.model.model_as_pdb()
      g_txt = self.model.restraints_as_geo()
      with open(self.processed_params.file_name_before_regularization, 'w') as f:
        f.write(m_txt)

      geo_fname = self.processed_params.file_name_before_regularization[:-4]+'.geo'
      print("Outputting geo file for regularization %s" % geo_fname, file=self.log)
      with open(geo_fname, 'w') as f:
        f.write(g_txt)

    #testing number of restraints
    assert grm.geometry.get_n_den_proxies() == 0
    if self.reference_map is None:
      assert grm.geometry.get_n_reference_coordinate_proxies() == n_main_chain_atoms, "" +\
          "%d %d" % (grm.geometry.get_n_reference_coordinate_proxies(), n_main_chain_atoms)
    refinement_log = null_out()
    self.log.write(
        "Refining geometry of substituted secondary structure elements\n")
    self.log.write(
        "  for %s macro_cycle(s).\n" % self.processed_params.n_macro)
    self.log.flush()
    if self.processed_params.verbose:
      refinement_log = self.log
    t10 = time()
    if self.reference_map is None:
      n_cycles = self.processed_params.n_macro
      if self.processed_params.n_macro == Auto:
        n_cycles=5
      minimize_wrapper_for_ramachandran(
          model = self.model,
          original_pdb_h = None,
          excl_string_selection = "",
          log = refinement_log,
          number_of_cycles = n_cycles)
    else:
      ref_xrs = self.model.crystal_symmetry()
      minimize_wrapper_with_map(
          model = self.model,
          target_map=self.reference_map,
          refine_ncs_operators=False,
          number_of_cycles=self.processed_params.n_macro,
          min_mode='simple_cycles',
          log=self.log)
    self.model.set_sites_cart_from_hierarchy()

    self.log.write(" Done\n")
    self.log.flush()
    t11 = time()
    # print("Initial checking, init   : %.4f" % (t1-t0), file=self.log)
    # print("Checking SS              : %.4f" % (t2-t1), file=self.log)
    # print("Initializing selections  : %.4f" % (t4-t3), file=self.log)
    # print("Looping for selections   : %.4f" % (t5-t4), file=self.log)
    # print("Finalizing selections    : %.4f" % (t6-t5), file=self.log)
    # print("PDB interpretation       : %.4f" % (t7-t6), file=self.log)
    # print("Get GRM                  : %.4f" % (t8-t7), file=self.log)
    # print("Adding restraints to GRM : %.4f" % (t9-t8), file=self.log)
    # print("Running GM               : %.4f" % (t11-t10), file=self.log)
    # print_hbond_proxies(grm.geometry,real_h)
    grm.geometry.remove_reference_coordinate_restraints_in_place()
    grm.geometry.remove_chi_torsion_restraints_in_place(nonss_for_tors_selection)
    return grm.geometry.get_chi_torsion_proxies()
Exemplo n.º 14
0
def minimize_wrapper_for_ramachandran(
        hierarchy,
        xrs,
        original_pdb_h,
        excl_string_selection,
        grm=None,
        log=None,
        ncs_restraints_group_list=[],
        ss_annotation=None,
        mon_lib_srv=None,
        ener_lib=None,
        rotamer_manager=None,
        reference_rotamers=True,
        number_of_cycles=1,
        run_first_minimization_without_reference=False,
        oldfield_weight_scale=3,
        oldfield_plot_cutoff=0.03,
        nonbonded_weight=500,
        reference_sigma=0.7):
    """ Wrapper around geometry minimization specifically tuned for eliminating
  Ramachandran outliers.
  """
    try:
        import cPickle as pickle
    except ImportError:
        import pickle
    from time import time
    from mmtbx.monomer_library.pdb_interpretation import grand_master_phil_str
    from mmtbx.geometry_restraints import reference
    from mmtbx.command_line.geometry_minimization import \
        get_geometry_restraints_manager
    from mmtbx.geometry_restraints.torsion_restraints.reference_model import \
        reference_model, reference_model_params
    from libtbx.utils import null_out
    from scitbx.array_family import flex
    if log is None:
        log = null_out()
    # assert hierarchy.atoms_size()==xrs.scatterers().size(), "%d %d" % (
    #     hierarchy.atoms_size(), xrs.scatterers().size())
    params_line = grand_master_phil_str
    params = iotbx.phil.parse(input_string=params_line,
                              process_includes=True).extract()
    params.pdb_interpretation.clash_guard.nonbonded_distance_threshold = None
    params.pdb_interpretation.peptide_link.ramachandran_restraints = True
    params.pdb_interpretation.peptide_link.oldfield.weight_scale = oldfield_weight_scale
    params.pdb_interpretation.peptide_link.oldfield.plot_cutoff = oldfield_plot_cutoff
    params.pdb_interpretation.nonbonded_weight = nonbonded_weight
    params.pdb_interpretation.c_beta_restraints = True
    params.pdb_interpretation.max_reasonable_bond_distance = None
    params.pdb_interpretation.peptide_link.apply_peptide_plane = True
    params.pdb_interpretation.ncs_search.enabled = True
    params.pdb_interpretation.restraints_library.rdl = True

    processed_pdb_files_srv = mmtbx.utils.\
        process_pdb_file_srv(
            crystal_symmetry= xrs.crystal_symmetry(),
            pdb_interpretation_params = params.pdb_interpretation,
            stop_for_unknowns         = False,
            log=log,
            cif_objects=None)
    processed_pdb_file, junk = processed_pdb_files_srv.\
        process_pdb_files(raw_records=flex.split_lines(hierarchy.as_pdb_string()))

    mon_lib_srv = processed_pdb_files_srv.mon_lib_srv
    ener_lib = processed_pdb_files_srv.ener_lib

    ncs_restraints_group_list = []
    if processed_pdb_file.ncs_obj is not None:
        ncs_restraints_group_list = processed_pdb_file.ncs_obj.get_ncs_restraints_group_list(
        )

    if grm is None:
        grm = get_geometry_restraints_manager(processed_pdb_file,
                                              xrs,
                                              params=params)
    else:
        grm.geometry.pair_proxies(sites_cart=hierarchy.atoms().extract_xyz())
        if grm.geometry.ramachandran_manager is not None:
            grm.geometry.ramachandran_manager.update_phi_psi_targets(
                sites_cart=hierarchy.atoms().extract_xyz())

    if reference_rotamers and original_pdb_h is not None:
        # make selection excluding rotamer outliers
        from mmtbx.rotamer.rotamer_eval import RotamerEval
        # print "Excluding rotamer outliers"
        if rotamer_manager is None:
            rotamer_manager = RotamerEval(mon_lib_srv=mon_lib_srv)
        non_rot_outliers_selection = flex.bool(hierarchy.atoms_size(), False)
        for model in original_pdb_h.models():
            for chain in model.chains():
                for conf in chain.conformers():
                    for res in conf.residues():
                        ev = rotamer_manager.evaluate_residue_2(res)
                        if ev != "OUTLIER" or ev is None:
                            for a in res.atoms():
                                non_rot_outliers_selection[a.i_seq] = True
                        # else:
                        #   print "  ", res.id_str()

        rm_params = reference_model_params.extract()
        rm_params.reference_model.enabled = True
        rm_params.reference_model.strict_rotamer_matching = False
        rm_params.reference_model.main_chain = False
        rm = reference_model(processed_pdb_file=processed_pdb_file,
                             reference_file_list=None,
                             reference_hierarchy_list=[original_pdb_h],
                             mon_lib_srv=mon_lib_srv,
                             ener_lib=ener_lib,
                             has_hd=None,
                             params=rm_params.reference_model,
                             selection=non_rot_outliers_selection,
                             log=log)
        rm.show_reference_summary(log=log)
        grm.geometry.adopt_reference_dihedral_manager(rm)

    # dealing with SS
    if ss_annotation is not None:
        from mmtbx.secondary_structure import manager
        ss_manager = manager(pdb_hierarchy=hierarchy,
                             geometry_restraints_manager=grm.geometry,
                             sec_str_from_pdb_file=ss_annotation,
                             params=None,
                             mon_lib_srv=mon_lib_srv,
                             verbose=-1,
                             log=log)
        grm.geometry.set_secondary_structure_restraints(ss_manager=ss_manager,
                                                        hierarchy=hierarchy,
                                                        log=log)

    # grm pickle-unpickle
    # t0 = time()
    # prefix="grm"
    # pklfile = open("%s.pkl" % prefix, 'wb')
    # pickle.dump(grm.geometry, pklfile)
    # pklfile.close()
    # t1 = time()
    # pklfile = open("%s.pkl" % prefix, 'rb')
    # grm_from_file = pickle.load(pklfile)
    # pklfile.close()
    # t2 = time()
    # print "Time pickling/unpickling: %.4f, %.4f" % (t1-t0, t2-t1)
    # grm.geometry=grm_from_file

    if run_first_minimization_without_reference:
        obj = run2(restraints_manager=grm,
                   pdb_hierarchy=hierarchy,
                   correct_special_position_tolerance=1.0,
                   ncs_restraints_group_list=ncs_restraints_group_list,
                   max_number_of_iterations=300,
                   number_of_macro_cycles=number_of_cycles,
                   bond=True,
                   nonbonded=True,
                   angle=True,
                   dihedral=True,
                   chirality=True,
                   planarity=True,
                   fix_rotamer_outliers=True,
                   log=log)

    if original_pdb_h is not None:
        if len(excl_string_selection) == 0:
            excl_string_selection = "all"
        asc = original_pdb_h.atom_selection_cache()
        sel = asc.selection(
            "(%s) and (name CA or name C or name N or name O)" %
            excl_string_selection)

        grm.geometry.append_reference_coordinate_restraints_in_place(
            reference.add_coordinate_restraints(
                sites_cart=original_pdb_h.atoms().extract_xyz().select(sel),
                selection=sel,
                sigma=reference_sigma,
                top_out_potential=True))
    # grm.geometry.write_geo_file(
    #     sites_cart=hierarchy.atoms().extract_xyz(),
    #     site_labels=[atom.id_str() for atom in hierarchy.atoms()],
    #     file_name="last_gm.geo")
    obj = run2(restraints_manager=grm,
               pdb_hierarchy=hierarchy,
               correct_special_position_tolerance=1.0,
               ncs_restraints_group_list=ncs_restraints_group_list,
               max_number_of_iterations=300,
               number_of_macro_cycles=number_of_cycles,
               bond=True,
               nonbonded=True,
               angle=True,
               dihedral=True,
               chirality=True,
               planarity=True,
               fix_rotamer_outliers=True,
               log=log)
    grm.geometry.reference_dihedral_manager = None
def exercise_2(mon_lib_srv, ener_lib):
  for use_reference in [True, False, None]:
    processed_pdb_file = pdb_interpretation.process(
      mon_lib_srv              = mon_lib_srv,
      ener_lib                 = ener_lib,
      raw_records              = flex.std_string(pdb_str_2.splitlines()),
      strict_conflict_handling = True,
      force_symmetry           = True,
      log                      = None)
    grm = processed_pdb_file.geometry_restraints_manager()
    xrs2 = processed_pdb_file.xray_structure(show_summary = False)
    awl2 = processed_pdb_file.all_chain_proxies.pdb_hierarchy.atoms_with_labels()
    pdb_inp3 = iotbx.pdb.input(source_info=None, lines=pdb_str_3)
    xrs3 = pdb_inp3.xray_structure_simple()
    ph3 = pdb_inp3.construct_hierarchy()
    ph3.atoms().reset_i_seq()
    awl3 =  ph3.atoms_with_labels()
    sites_cart_reference = flex.vec3_double()
    selection = flex.size_t()
    reference_names = ["CG", "CD", "NE", "CZ", "NH1", "NH2"]
    for a2,a3 in zip(tuple(awl2), tuple(awl3)):
      assert a2.resname == a3.resname
      assert a2.name == a3.name
      assert a2.i_seq == a3.i_seq
      if(a2.resname == "ARG" and a2.name.strip() in reference_names):
        selection.append(a2.i_seq)
        sites_cart_reference.append(a3.xyz)
    assert selection.size() == len(reference_names)
    selection_bool = flex.bool(xrs2.scatterers().size(), selection)
    if(use_reference):
      grm.adopt_reference_coordinate_restraints_in_place(
          reference.add_coordinate_restraints(
              sites_cart = sites_cart_reference,
              selection = selection,
              sigma = 0.01))
    elif(use_reference is None):
      grm.adopt_reference_coordinate_restraints_in_place(
          reference.add_coordinate_restraints(
              sites_cart = sites_cart_reference,
              selection = selection,
              sigma = 0.01))
      grm.remove_reference_coordinate_restraints_in_place(
          selection = selection)
    d1 = flex.mean(flex.sqrt((xrs2.sites_cart().select(selection) -
                              xrs3.sites_cart().select(selection)).dot()))
    print "distance start (use_reference: %s): %6.4f"%(str(use_reference), d1)
    assert d1>4.0
    assert approx_equal(
      flex.max(flex.sqrt((xrs2.sites_cart().select(~selection_bool) -
                          xrs3.sites_cart().select(~selection_bool)).dot())), 0)
    from cctbx import geometry_restraints
    import mmtbx.refinement.geometry_minimization
    import scitbx.lbfgs
    grf = geometry_restraints.flags.flags(default=True)
    sites_cart = xrs2.sites_cart()
    minimized = mmtbx.refinement.geometry_minimization.lbfgs(
      sites_cart                  = sites_cart,
      correct_special_position_tolerance=1.0,
      geometry_restraints_manager = grm,
      sites_cart_selection        = flex.bool(sites_cart.size(), selection),
      geometry_restraints_flags   = grf,
      lbfgs_termination_params = scitbx.lbfgs.termination_parameters(
        max_iterations=5000))
    xrs2.set_sites_cart(sites_cart = sites_cart)
    d2 = flex.mean(flex.sqrt((xrs2.sites_cart().select(selection) -
                              xrs3.sites_cart().select(selection)).dot()))
    print "distance final (use_reference: %s): %6.4f"%(str(use_reference), d2)
    if(use_reference): assert d2<0.005, "failed: %f<0.05" % d2
    else: assert d2>4.0, d2
    assert approx_equal(
      flex.max(flex.sqrt((xrs2.sites_cart().select(~selection_bool) -
                          xrs3.sites_cart().select(~selection_bool)).dot())), 0)
def minimize_wrapper_for_ramachandran(
    hierarchy,
    xrs,
    original_pdb_h,
    excl_string_selection,
    log=None,
    ss_annotation = None,
    reference_rotamers = True,
    run_first_minimization_without_reference=False,
    oldfield_weight_scale=3,
    oldfield_plot_cutoff=0.03,
    nonbonded_weight=500,
    reference_sigma=0.7):
  """ Wrapper around geometry minimization specifically tuned for eliminating
  Ramachandran outliers.
  """
  import pickle
  from time import time
  from mmtbx.monomer_library.pdb_interpretation import grand_master_phil_str
  from mmtbx.geometry_restraints import reference
  from mmtbx.command_line.geometry_minimization import \
      get_geometry_restraints_manager
  from mmtbx.geometry_restraints.torsion_restraints.reference_model import \
      reference_model, reference_model_params
  from libtbx.utils import null_out
  from scitbx.array_family import flex
  if log is None:
    log = null_out()
  params_line = grand_master_phil_str
  params = iotbx.phil.parse(
      input_string=params_line, process_includes=True).extract()
  params.pdb_interpretation.clash_guard.nonbonded_distance_threshold=None
  params.pdb_interpretation.peptide_link.ramachandran_restraints = True
  params.pdb_interpretation.peptide_link.oldfield.weight_scale=oldfield_weight_scale
  params.pdb_interpretation.peptide_link.oldfield.plot_cutoff=oldfield_plot_cutoff
  params.pdb_interpretation.nonbonded_weight = nonbonded_weight
  params.pdb_interpretation.c_beta_restraints=True
  params.pdb_interpretation.max_reasonable_bond_distance = None
  params.pdb_interpretation.peptide_link.apply_peptide_plane = True
  params.pdb_interpretation.ncs_search.enabled = True
  params.pdb_interpretation.restraints_library.rdl = True

  processed_pdb_files_srv = mmtbx.utils.\
      process_pdb_file_srv(
          crystal_symmetry= xrs.crystal_symmetry(),
          pdb_interpretation_params = params.pdb_interpretation,
          stop_for_unknowns         = False,
          log=log,
          cif_objects=None)
  processed_pdb_file, junk = processed_pdb_files_srv.\
      process_pdb_files(raw_records=flex.split_lines(hierarchy.as_pdb_string()))

  mon_lib_srv = processed_pdb_files_srv.mon_lib_srv
  ener_lib = processed_pdb_files_srv.ener_lib

  ncs_restraints_group_list = []
  if processed_pdb_file.ncs_obj is not None:
    ncs_restraints_group_list = processed_pdb_file.ncs_obj.get_ncs_restraints_group_list()

  grm = get_geometry_restraints_manager(
      processed_pdb_file, xrs, params=params)

  if reference_rotamers and original_pdb_h is not None:
    # make selection excluding rotamer outliers
    from mmtbx.rotamer.rotamer_eval import RotamerEval
    rotamer_manager = RotamerEval(mon_lib_srv=mon_lib_srv)
    non_rot_outliers_selection = flex.bool([False]*hierarchy.atoms().size())
    for model in original_pdb_h.models():
      for chain in model.chains():
        for conf in chain.conformers():
          for res in conf.residues():
            ev = rotamer_manager.evaluate_residue_2(res)
            if ev != "OUTLIER" or ev is None:
              for a in res.atoms():
                non_rot_outliers_selection[a.i_seq] = True


    rm_params = reference_model_params.extract()
    rm_params.reference_model.enabled=True
    rm_params.reference_model.strict_rotamer_matching=False
    rm_params.reference_model.main_chain=False
    rm = reference_model(
      processed_pdb_file=processed_pdb_file,
      reference_file_list=None,
      reference_hierarchy_list=[original_pdb_h],
      mon_lib_srv=mon_lib_srv,
      ener_lib=ener_lib,
      has_hd=None,
      params=rm_params.reference_model,
      selection=non_rot_outliers_selection,
      log=log)
    rm.show_reference_summary(log=log)
    grm.geometry.adopt_reference_dihedral_manager(rm)

  # dealing with SS
  if ss_annotation is not None:
    from mmtbx.secondary_structure import manager
    ss_manager = manager(
        pdb_hierarchy=hierarchy,
        geometry_restraints_manager=grm.geometry,
        sec_str_from_pdb_file=ss_annotation,
        params=None,
        mon_lib_srv=mon_lib_srv,
        verbose=-1,
        log=log)
    grm.geometry.set_secondary_structure_restraints(
        ss_manager=ss_manager,
        hierarchy=hierarchy,
        log=log)

  # grm pickle-unpickle
  # t0 = time()
  # prefix="grm"
  # pklfile = open("%s.pkl" % prefix, 'wb')
  # pickle.dump(grm.geometry, pklfile)
  # pklfile.close()
  # t1 = time()
  # pklfile = open("%s.pkl" % prefix, 'rb')
  # grm_from_file = pickle.load(pklfile)
  # pklfile.close()
  # t2 = time()
  # print "Time pickling/unpickling: %.4f, %.4f" % (t1-t0, t2-t1)
  # grm.geometry=grm_from_file


  if run_first_minimization_without_reference:
    obj = run2(
      restraints_manager=grm,
      pdb_hierarchy=hierarchy,
      correct_special_position_tolerance=1.0,
      ncs_restraints_group_list=ncs_restraints_group_list,
      max_number_of_iterations=300,
      number_of_macro_cycles=5,
      bond=True,
      nonbonded=True,
      angle=True,
      dihedral=True,
      chirality=True,
      planarity=True,
      fix_rotamer_outliers=True,
      log=log)


  if original_pdb_h is not None:
    if len(excl_string_selection) == 0:
      excl_string_selection = "all"
    asc = original_pdb_h.atom_selection_cache()
    sel = asc.selection("(%s) and (name CA or name C or name N or name O)" % excl_string_selection)


    grm.geometry.append_reference_coordinate_restraints_in_place(
        reference.add_coordinate_restraints(
            sites_cart = original_pdb_h.atoms().extract_xyz().select(sel),
            selection  = sel,
            sigma      = reference_sigma,
            top_out_potential=True))
  obj = run2(
      restraints_manager       = grm,
      pdb_hierarchy            = hierarchy,
      correct_special_position_tolerance = 1.0,
      ncs_restraints_group_list=ncs_restraints_group_list,
      max_number_of_iterations = 300,
      number_of_macro_cycles   = 5,
      bond                     = True,
      nonbonded                = True,
      angle                    = True,
      dihedral                 = True,
      chirality                = True,
      planarity                = True,
      fix_rotamer_outliers     = True,
      log                      = log)