def run(args, params=None, out=sys.stdout, log=sys.stderr):
  if ( ((len(args) == 0) and (params is None)) or
       ((len(args) > 0) and ((args[0] == "-h") or (args[0] == "--help"))) ):
    show_usage()
    return

  if (params is None):
    pcl = iotbx.phil.process_command_line_with_files(
      args=args,
      master_phil_string=master_phil_str,
      pdb_file_def="file_name")
    work_params = pcl.work.extract()
  # or use parameters defined by GUI
  else:
    work_params = params
  pdb_files = work_params.file_name

  mon_lib_srv = mmtbx.monomer_library.server.server()
  ppf_srv = mmtbx.utils.process_pdb_file_srv(
      crystal_symmetry          = None,
      pdb_interpretation_params = None,
      stop_for_unknowns         = False,
      log                       = log,
      cif_objects               = None, # need to figure out how to get them
      mon_lib_srv               = mon_lib_srv,
      ener_lib                  = None,
      use_neutron_distances     = False)
  processed_pdb_file, pdb_inp = ppf_srv.process_pdb_files(
      pdb_file_names = pdb_files)
  cs = ppf_srv.crystal_symmetry
  xrs = processed_pdb_file.xray_structure(show_summary = True)

  if(xrs is None):
    raise Sorry("Cannot extract xray_structure.")

  grm = get_geometry_restraints_manager(
      processed_pdb_file=processed_pdb_file,
      xray_structure=xrs,
      log=log)

  pdb_h = processed_pdb_file.all_chain_proxies.pdb_hierarchy
  fixed_pdb_h = pdb_h.deep_copy()
  fixed_pdb_h.reset_atom_i_seqs()

  rotamer_manager = RotamerEval(mon_lib_srv=mon_lib_srv)
  fixed_pdb_h = fix_rotamer_outliers(
      pdb_hierarchy=fixed_pdb_h,
      grm=grm.geometry,
      xrs=xrs,
      radius=work_params.radius,
      mon_lib_srv=mon_lib_srv,
      rotamer_manager=rotamer_manager,
      asc=None)

  iotbx.pdb.write_whole_pdb_file(
      file_name="%s.pdb" % work_params.output_prefix,
      processed_pdb_file=processed_pdb_file,
      pdb_hierarchy=fixed_pdb_h,
      crystal_symmetry=cs)
  def get_grm(self):
    # first make whole grm using self.whole_pdb_h
    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.nonbonded_weight = 500
    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= self.whole_xrs.crystal_symmetry(),
            pdb_interpretation_params = params.pdb_interpretation,
            stop_for_unknowns         = False,
            log=self.log,
            cif_objects=None)
    processed_pdb_file, junk = processed_pdb_files_srv.\
        process_pdb_files(raw_records=flex.split_lines(self.whole_pdb_h.as_pdb_string()))

    self.mon_lib_srv = processed_pdb_files_srv.mon_lib_srv
    self.ener_lib = processed_pdb_files_srv.ener_lib
    self.rotamer_manager = RotamerEval(mon_lib_srv=self.mon_lib_srv)

    self.whole_grm = get_geometry_restraints_manager(
        processed_pdb_file, self.whole_xrs, params=params)

    # set SS restratins
    if self.params.use_ss_restraints:
      ss_manager = manager(
          pdb_hierarchy=self.whole_pdb_h,
          geometry_restraints_manager=self.whole_grm.geometry,
          sec_str_from_pdb_file=self.filtered_whole_ann,
          params=None,
          mon_lib_srv=self.mon_lib_srv,
          verbose=-1,
          log=self.log)
      # self.whole_pdb_h.write_pdb_file(file_name="for_ss.pdb")
      self.whole_pdb_h.reset_atom_i_seqs()
      self.whole_grm.geometry.set_secondary_structure_restraints(
          ss_manager=ss_manager,
          hierarchy=self.whole_pdb_h,
          log=self.log)

    # now select part of it for working with master hierarchy
    if self.using_ncs:
      self.master_grm = self.whole_grm.select(self.master_sel)
      self.working_grm = self.master_grm
    else:
      self.working_grm = self.whole_grm
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,
    )
Example #4
0
def run(pdb_filename):
    print "run", pdb_filename
    from mmtbx.command_line.geometry_minimization import \
      get_geometry_restraints_manager, master_params
    import mmtbx.monomer_library.pdb_interpretation
    from mmtbx import monomer_library

    mon_lib_srv = monomer_library.server.server()
    ener_lib = monomer_library.server.ener_lib()
    processed_pdb_file = monomer_library.pdb_interpretation.process(
        mon_lib_srv=mon_lib_srv,
        ener_lib=ener_lib,
        file_name=pdb_filename,
        #force_symmetry = True,
    )
    xrs = processed_pdb_file.xray_structure()
    #work_params = master_params().extract()
    #work_params.reference_model.enabled=True
    #work_params.reference_model.use_starting_model_as_reference=True
    grm = get_geometry_restraints_manager(
        processed_pdb_file,
        xrs,
        #params=work_params,
        #log=null_out(),
    )
    pdb_hierarchy = processed_pdb_file.all_chain_proxies.pdb_hierarchy
    rc = get_sf4_coordination(
        pdb_hierarchy=pdb_hierarchy,
        nonbonded_proxies=grm.geometry.pair_proxies(
            sites_cart=pdb_hierarchy.atoms().extract_xyz()).nonbonded_proxies,
        #verbose=True,
    )
    bproxies, aproxies = get_all_proxies(rc)
    print len(bproxies), len(aproxies)
    grm.geometry.add_new_bond_restraints_in_place(
        proxies=bproxies,
        sites_cart=pdb_hierarchy.atoms().extract_xyz(),
    )
    grm.geometry.add_angles_in_place(aproxies)
Example #5
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 exercise_reference_model_restraints(mon_lib_srv, ener_lib):
    # 1yjp
    pdb_str = """\
CRYST1   21.937    4.866   23.477  90.00 107.08  90.00 P 1 21 1      2
ATOM      1  N   GLY A   1      -9.009   4.612   6.102  1.00 16.77           N
ATOM      2  CA  GLY A   1      -9.052   4.207   4.651  1.00 16.57           C
ATOM      3  C   GLY A   1      -8.015   3.140   4.419  1.00 16.16           C
ATOM      4  O   GLY A   1      -7.523   2.521   5.381  1.00 16.78           O
ATOM      5  N   ASN A   2      -7.656   2.923   3.155  1.00 15.02           N
ATOM      6  CA  ASN A   2      -6.522   2.038   2.831  1.00 14.10           C
ATOM      7  C   ASN A   2      -5.241   2.537   3.427  1.00 13.13           C
ATOM      8  O   ASN A   2      -4.978   3.742   3.426  1.00 11.91           O
ATOM      9  CB  ASN A   2      -6.346   1.881   1.341  1.00 15.38           C
ATOM     10  CG  ASN A   2      -7.584   1.342   0.692  1.00 14.08           C
ATOM     11  OD1 ASN A   2      -8.025   0.227   1.016  1.00 17.46           O
ATOM     12  ND2 ASN A   2      -8.204   2.155  -0.169  1.00 11.72           N
ATOM     13  N   ASN A   3      -4.438   1.590   3.905  1.00 12.26           N
ATOM     14  CA  ASN A   3      -3.193   1.904   4.589  1.00 11.74           C
ATOM     15  C   ASN A   3      -1.955   1.332   3.895  1.00 11.10           C
ATOM     16  O   ASN A   3      -1.872   0.119   3.648  1.00 10.42           O
ATOM     17  CB  ASN A   3      -3.259   1.378   6.042  1.00 12.15           C
ATOM     18  CG  ASN A   3      -2.006   1.739   6.861  1.00 12.82           C
ATOM     19  OD1 ASN A   3      -1.702   2.925   7.072  1.00 15.05           O
ATOM     20  ND2 ASN A   3      -1.271   0.715   7.306  1.00 13.48           N
ATOM     21  N   GLN A   4      -1.005   2.228   3.598  1.00 10.29           N
ATOM     22  CA  GLN A   4       0.384   1.888   3.199  1.00 10.53           C
ATOM     23  C   GLN A   4       1.435   2.606   4.088  1.00 10.24           C
ATOM     24  O   GLN A   4       1.547   3.843   4.115  1.00  8.86           O
ATOM     25  CB  GLN A   4       0.656   2.148   1.711  1.00  9.80           C
ATOM     26  CG  GLN A   4       1.944   1.458   1.213  1.00 10.25           C
ATOM     27  CD  GLN A   4       2.504   2.044  -0.089  1.00 12.43           C
ATOM     28  OE1 GLN A   4       2.744   3.268  -0.190  1.00 14.62           O
ATOM     29  NE2 GLN A   4       2.750   1.161  -1.091  1.00  9.05           N
ATOM     30  N   GLN A   5       2.154   1.821   4.871  1.00 10.38           N
ATOM     31  CA  GLN A   5       3.270   2.361   5.640  1.00 11.39           C
ATOM     32  C   GLN A   5       4.594   1.768   5.172  1.00 11.52           C
ATOM     33  O   GLN A   5       4.768   0.546   5.054  1.00 12.05           O
ATOM     34  CB  GLN A   5       3.056   2.183   7.147  1.00 11.96           C
ATOM     35  CG  GLN A   5       1.829   2.950   7.647  1.00 10.81           C
ATOM     36  CD  GLN A   5       1.344   2.414   8.954  1.00 13.10           C
ATOM     37  OE1 GLN A   5       0.774   1.325   9.002  1.00 10.65           O
ATOM     38  NE2 GLN A   5       1.549   3.187  10.039  1.00 12.30           N
ATOM     39  N   ASN A   6       5.514   2.664   4.856  1.00 11.99           N
ATOM     40  CA  ASN A   6       6.831   2.310   4.318  1.00 12.30           C
ATOM     41  C   ASN A   6       7.854   2.761   5.324  1.00 13.40           C
ATOM     42  O   ASN A   6       8.219   3.943   5.374  1.00 13.92           O
ATOM     43  CB  ASN A   6       7.065   3.016   2.993  1.00 12.13           C
ATOM     44  CG  ASN A   6       5.961   2.735   2.003  1.00 12.77           C
ATOM     45  OD1 ASN A   6       5.798   1.604   1.551  1.00 14.27           O
ATOM     46  ND2 ASN A   6       5.195   3.747   1.679  1.00 10.07           N
ATOM     47  N   TYR A   7       8.292   1.817   6.147  1.00 14.70           N
ATOM     48  CA  TYR A   7       9.159   2.144   7.299  1.00 15.18           C
ATOM     49  C   TYR A   7      10.603   2.331   6.885  1.00 15.91           C
ATOM     50  O   TYR A   7      11.041   1.811   5.855  1.00 15.76           O
ATOM     51  CB  TYR A   7       9.061   1.065   8.369  1.00 15.35           C
ATOM     52  CG  TYR A   7       7.665   0.929   8.902  1.00 14.45           C
ATOM     53  CD1 TYR A   7       6.771   0.021   8.327  1.00 15.68           C
ATOM     54  CD2 TYR A   7       7.210   1.756   9.920  1.00 14.80           C
ATOM     55  CE1 TYR A   7       5.480  -0.094   8.796  1.00 13.46           C
ATOM     56  CE2 TYR A   7       5.904   1.649  10.416  1.00 14.33           C
ATOM     57  CZ  TYR A   7       5.047   0.729   9.831  1.00 15.09           C
ATOM     58  OH  TYR A   7       3.766   0.589  10.291  1.00 14.39           O
ATOM     59  OXT TYR A   7      11.358   2.999   7.612  1.00 17.49           O
TER      60      TYR A   7
HETATM   61  O   HOH A   8      -6.471   5.227   7.124  1.00 22.62           O
HETATM   62  O   HOH A   9      10.431   1.858   3.216  1.00 19.71           O
HETATM   63  O   HOH A  10     -11.286   1.756  -1.468  1.00 17.08           O
HETATM   64  O   HOH A  11      11.808   4.179   9.970  1.00 23.99           O
HETATM   65  O   HOH A  12      13.605   1.327   9.198  1.00 26.17           O
HETATM   66  O   HOH A  13      -2.749   3.429  10.024  1.00 39.15           O
HETATM   67  O   HOH A  14      -1.500   0.682  10.967  1.00 43.49           O
MASTER      234    0    0    0    0    0    0    6   66    1    0    1
END """

    processed_pdb_file = monomer_library.pdb_interpretation.process(
        mon_lib_srv=mon_lib_srv,
        ener_lib=ener_lib,
        raw_records=pdb_str,
        force_symmetry=True)
    # print dir(processed_pdb_file)
    xrs = processed_pdb_file.xray_structure()
    work_params = master_params().extract()
    work_params.reference_model.enabled = True
    work_params.reference_model.use_starting_model_as_reference = True
    grm = get_geometry_restraints_manager(processed_pdb_file,
                                          xrs,
                                          params=work_params,
                                          log=null_out())
    n_ref_dih_prox = grm.geometry.get_n_reference_dihedral_proxies()
    assert n_ref_dih_prox == 34, "expected 34, got %d" % n_ref_dih_prox

    work_params = master_params().extract()
    work_params.reference_model.enabled = True
    work_params.reference_model.use_starting_model_as_reference = True
    work_params.reference_model.main_chain = False
    grm = get_geometry_restraints_manager(processed_pdb_file,
                                          xrs,
                                          params=work_params,
                                          log=null_out())
    n_ref_dih_prox = grm.geometry.get_n_reference_dihedral_proxies()
    assert n_ref_dih_prox == 16, "expected 16, got %d" % n_ref_dih_prox

    work_params = master_params().extract()
    work_params.reference_model.enabled = True
    work_params.reference_model.use_starting_model_as_reference = True
    work_params.reference_model.side_chain = False
    grm = get_geometry_restraints_manager(processed_pdb_file,
                                          xrs,
                                          params=work_params,
                                          log=null_out())
    n_ref_dih_prox = grm.geometry.get_n_reference_dihedral_proxies()
    assert n_ref_dih_prox == 18, "expected 18, got %d" % n_ref_dih_prox
Example #7
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()
def tst_1(prefix="gm_ncs_constr_tst1"):
    f = open("%s_start.pdb" % prefix, 'w')
    f.write(pdb_string1)
    f.close()
    log = sys.stdout
    pdb_in = iotbx.pdb.input(source_info=None, lines=pdb_string1.split('\n'))
    # print dir(pdb_in)
    pdb_h = pdb_in.construct_hierarchy()
    xrs = pdb_h.extract_xray_structure()
    cs = pdb_in.crystal_symmetry()
    pdb_int_params = mmtbx.monomer_library.pdb_interpretation.master_params.extract(
    )
    pdb_int_params.ncs_search.enabled = True
    processed_pdb_files_srv = mmtbx.utils.process_pdb_file_srv(
        crystal_symmetry=cs,
        pdb_interpretation_params=pdb_int_params,
        stop_for_unknowns=True,
        log=log,
        cif_objects=None,
        use_neutron_distances=False)
    processed_pdb_file, junk = processed_pdb_files_srv.\
      process_pdb_files(raw_records = pdb_string1.split('\n')) # XXX remove junk
    ncs_obj = iotbx.ncs.input(hierarchy=pdb_h)
    original_ncs_transform = ncs_obj.ncs_transform
    ncs_restraints_group_list = ncs_obj.get_ncs_restraints_group_list()
    ncs_obj.show(format='phil')
    grm = get_geometry_restraints_manager(
        processed_pdb_file=processed_pdb_file,
        xray_structure=xrs,
        log=null_out())
    tmp_xrs = xrs.deep_copy_scatterers()
    refine_selection = flex.size_t(xrange(xrs.scatterers().size()))

    # print "refining sites"
    cycle = 0
    tfg_obj = mmtbx.refinement.minimization_ncs_constraints.\
        target_function_and_grads_geometry_minimization(
            xray_structure=tmp_xrs,
            ncs_restraints_group_list=ncs_restraints_group_list,
            refine_selection=refine_selection,
            restraints_manager=grm.geometry,
            refine_sites=True,
            refine_transformations=False,
            )
    minimized = mmtbx.refinement.minimization_ncs_constraints.lbfgs(
        target_and_grads_object=tfg_obj,
        xray_structure=tmp_xrs,
        ncs_restraints_group_list=ncs_restraints_group_list,
        refine_selection=refine_selection,
        finite_grad_differences_test=False,
        max_iterations=100,
        refine_sites=True,
        refine_transformations=False)
    refined_pdb_h = pdb_h.deep_copy()
    refined_pdb_h.adopt_xray_structure(tmp_xrs)
    refined_pdb_h.write_pdb_file("refined_%d.pdb" % cycle)
    new_ncs_obj = iotbx.ncs.input(hierarchy=refined_pdb_h)
    new_ncs_transform = new_ncs_obj.ncs_transform
    spec = new_ncs_obj.get_ncs_info_as_spec()
    for k, v in original_ncs_transform.iteritems():
        assert approx_equal(v.r.elems, new_ncs_transform[k].r.elems)
        assert approx_equal(v.t, new_ncs_transform[k].t)
    overall_rmsd_after = spec.overall_rmsd()
    assert overall_rmsd_after < 1e-6
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 run(args, params=None, out=sys.stdout, log=sys.stderr):
    if (((len(args) == 0) and (params is None)) or
        ((len(args) > 0) and ((args[0] == "-h") or (args[0] == "--help")))):
        show_usage()
        return

    if (params is None):
        pcl = iotbx.phil.process_command_line_with_files(
            args=args,
            master_phil_string=master_phil_str,
            pdb_file_def="file_name")
        work_params = pcl.work.extract()
    # or use parameters defined by GUI
    else:
        work_params = params
    pdb_files = work_params.file_name

    from mmtbx.monomer_library.pdb_interpretation import grand_master_phil_str
    params = iotbx.phil.parse(input_string=grand_master_phil_str,
                              process_includes=True).extract()
    params.pdb_interpretation.clash_guard.nonbonded_distance_threshold = None

    mon_lib_srv = mmtbx.monomer_library.server.server()
    ppf_srv = mmtbx.utils.process_pdb_file_srv(
        crystal_symmetry=None,
        pdb_interpretation_params=params.pdb_interpretation,
        stop_for_unknowns=False,
        log=log,
        cif_objects=None,  # need to figure out how to get them
        mon_lib_srv=mon_lib_srv,
        ener_lib=None,
        use_neutron_distances=False)
    processed_pdb_file, pdb_inp = ppf_srv.process_pdb_files(
        pdb_file_names=pdb_files)
    cs = ppf_srv.crystal_symmetry
    xrs = processed_pdb_file.xray_structure(show_summary=True)

    if (xrs is None):
        raise Sorry("Cannot extract xray_structure.")

    grm = get_geometry_restraints_manager(
        processed_pdb_file=processed_pdb_file, xray_structure=xrs, log=log)

    pdb_h = processed_pdb_file.all_chain_proxies.pdb_hierarchy
    fixed_pdb_h = pdb_h.deep_copy()
    fixed_pdb_h.reset_atom_i_seqs()

    rotamer_manager = RotamerEval(mon_lib_srv=mon_lib_srv)
    fixed_pdb_h = fix_rotamer_outliers(pdb_hierarchy=fixed_pdb_h,
                                       grm=grm.geometry,
                                       xrs=xrs,
                                       radius=work_params.radius,
                                       mon_lib_srv=mon_lib_srv,
                                       rotamer_manager=rotamer_manager,
                                       asc=None)

    iotbx.pdb.write_whole_pdb_file(file_name="%s.pdb" %
                                   work_params.output_prefix,
                                   processed_pdb_file=processed_pdb_file,
                                   pdb_hierarchy=fixed_pdb_h,
                                   crystal_symmetry=cs)
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)
def tst_1(prefix="gm_ncs_constr_tst1"):
  f = open("%s_start.pdb" % prefix, 'w')
  f.write(pdb_string1)
  f.close()
  log = sys.stdout
  pdb_in = iotbx.pdb.input(source_info=None, lines=pdb_string1.split('\n'))
  # print dir(pdb_in)
  pdb_h = pdb_in.construct_hierarchy()
  xrs = pdb_h.extract_xray_structure()
  cs = pdb_in.crystal_symmetry()
  pdb_int_params = mmtbx.monomer_library.pdb_interpretation.master_params.extract()
  pdb_int_params.ncs_search.enabled=True
  processed_pdb_files_srv = mmtbx.utils.process_pdb_file_srv(
    crystal_symmetry          = cs,
    pdb_interpretation_params = pdb_int_params,
    stop_for_unknowns         = True,
    log                       = log,
    cif_objects               = None,
    use_neutron_distances     = False)
  processed_pdb_file, junk = processed_pdb_files_srv.\
    process_pdb_files(raw_records = pdb_string1.split('\n')) # XXX remove junk
  ncs_obj = iotbx.ncs.input(hierarchy=pdb_h)
  original_ncs_transform = ncs_obj.ncs_transform
  ncs_restraints_group_list = ncs_obj.get_ncs_restraints_group_list()
  ncs_obj.show(format='phil')
  grm = get_geometry_restraints_manager(
      processed_pdb_file=processed_pdb_file,
      xray_structure=xrs,
      log=null_out())
  tmp_xrs = xrs.deep_copy_scatterers()
  refine_selection = flex.size_t(xrange(xrs.scatterers().size()))

  # print "refining sites"
  cycle = 0
  tfg_obj = mmtbx.refinement.minimization_ncs_constraints.\
      target_function_and_grads_geometry_minimization(
          xray_structure=tmp_xrs,
          ncs_restraints_group_list=ncs_restraints_group_list,
          refine_selection=refine_selection,
          restraints_manager=grm.geometry,
          refine_sites=True,
          refine_transformations=False,
          )
  minimized = mmtbx.refinement.minimization_ncs_constraints.lbfgs(
    target_and_grads_object      = tfg_obj,
    xray_structure               = tmp_xrs,
    ncs_restraints_group_list    = ncs_restraints_group_list,
    refine_selection             = refine_selection,
    finite_grad_differences_test = False,
    max_iterations               = 100,
    refine_sites                 = True,
    refine_transformations       = False)
  refined_pdb_h = pdb_h.deep_copy()
  refined_pdb_h.adopt_xray_structure(tmp_xrs)
  refined_pdb_h.write_pdb_file("refined_%d.pdb" % cycle)
  new_ncs_obj = iotbx.ncs.input(hierarchy=refined_pdb_h)
  new_ncs_transform = new_ncs_obj.ncs_transform
  spec =  new_ncs_obj.get_ncs_info_as_spec()
  for k, v in original_ncs_transform.iteritems():
    assert approx_equal(v.r.elems, new_ncs_transform[k].r.elems)
    assert approx_equal(v.t, new_ncs_transform[k].t)
  overall_rmsd_after = spec.overall_rmsd()
  assert overall_rmsd_after < 1e-6
def exercise_reference_model_restraints(mon_lib_srv, ener_lib):
  # 1yjp
  pdb_str = """\
CRYST1   21.937    4.866   23.477  90.00 107.08  90.00 P 1 21 1      2
ATOM      1  N   GLY A   1      -9.009   4.612   6.102  1.00 16.77           N
ATOM      2  CA  GLY A   1      -9.052   4.207   4.651  1.00 16.57           C
ATOM      3  C   GLY A   1      -8.015   3.140   4.419  1.00 16.16           C
ATOM      4  O   GLY A   1      -7.523   2.521   5.381  1.00 16.78           O
ATOM      5  N   ASN A   2      -7.656   2.923   3.155  1.00 15.02           N
ATOM      6  CA  ASN A   2      -6.522   2.038   2.831  1.00 14.10           C
ATOM      7  C   ASN A   2      -5.241   2.537   3.427  1.00 13.13           C
ATOM      8  O   ASN A   2      -4.978   3.742   3.426  1.00 11.91           O
ATOM      9  CB  ASN A   2      -6.346   1.881   1.341  1.00 15.38           C
ATOM     10  CG  ASN A   2      -7.584   1.342   0.692  1.00 14.08           C
ATOM     11  OD1 ASN A   2      -8.025   0.227   1.016  1.00 17.46           O
ATOM     12  ND2 ASN A   2      -8.204   2.155  -0.169  1.00 11.72           N
ATOM     13  N   ASN A   3      -4.438   1.590   3.905  1.00 12.26           N
ATOM     14  CA  ASN A   3      -3.193   1.904   4.589  1.00 11.74           C
ATOM     15  C   ASN A   3      -1.955   1.332   3.895  1.00 11.10           C
ATOM     16  O   ASN A   3      -1.872   0.119   3.648  1.00 10.42           O
ATOM     17  CB  ASN A   3      -3.259   1.378   6.042  1.00 12.15           C
ATOM     18  CG  ASN A   3      -2.006   1.739   6.861  1.00 12.82           C
ATOM     19  OD1 ASN A   3      -1.702   2.925   7.072  1.00 15.05           O
ATOM     20  ND2 ASN A   3      -1.271   0.715   7.306  1.00 13.48           N
ATOM     21  N   GLN A   4      -1.005   2.228   3.598  1.00 10.29           N
ATOM     22  CA  GLN A   4       0.384   1.888   3.199  1.00 10.53           C
ATOM     23  C   GLN A   4       1.435   2.606   4.088  1.00 10.24           C
ATOM     24  O   GLN A   4       1.547   3.843   4.115  1.00  8.86           O
ATOM     25  CB  GLN A   4       0.656   2.148   1.711  1.00  9.80           C
ATOM     26  CG  GLN A   4       1.944   1.458   1.213  1.00 10.25           C
ATOM     27  CD  GLN A   4       2.504   2.044  -0.089  1.00 12.43           C
ATOM     28  OE1 GLN A   4       2.744   3.268  -0.190  1.00 14.62           O
ATOM     29  NE2 GLN A   4       2.750   1.161  -1.091  1.00  9.05           N
ATOM     30  N   GLN A   5       2.154   1.821   4.871  1.00 10.38           N
ATOM     31  CA  GLN A   5       3.270   2.361   5.640  1.00 11.39           C
ATOM     32  C   GLN A   5       4.594   1.768   5.172  1.00 11.52           C
ATOM     33  O   GLN A   5       4.768   0.546   5.054  1.00 12.05           O
ATOM     34  CB  GLN A   5       3.056   2.183   7.147  1.00 11.96           C
ATOM     35  CG  GLN A   5       1.829   2.950   7.647  1.00 10.81           C
ATOM     36  CD  GLN A   5       1.344   2.414   8.954  1.00 13.10           C
ATOM     37  OE1 GLN A   5       0.774   1.325   9.002  1.00 10.65           O
ATOM     38  NE2 GLN A   5       1.549   3.187  10.039  1.00 12.30           N
ATOM     39  N   ASN A   6       5.514   2.664   4.856  1.00 11.99           N
ATOM     40  CA  ASN A   6       6.831   2.310   4.318  1.00 12.30           C
ATOM     41  C   ASN A   6       7.854   2.761   5.324  1.00 13.40           C
ATOM     42  O   ASN A   6       8.219   3.943   5.374  1.00 13.92           O
ATOM     43  CB  ASN A   6       7.065   3.016   2.993  1.00 12.13           C
ATOM     44  CG  ASN A   6       5.961   2.735   2.003  1.00 12.77           C
ATOM     45  OD1 ASN A   6       5.798   1.604   1.551  1.00 14.27           O
ATOM     46  ND2 ASN A   6       5.195   3.747   1.679  1.00 10.07           N
ATOM     47  N   TYR A   7       8.292   1.817   6.147  1.00 14.70           N
ATOM     48  CA  TYR A   7       9.159   2.144   7.299  1.00 15.18           C
ATOM     49  C   TYR A   7      10.603   2.331   6.885  1.00 15.91           C
ATOM     50  O   TYR A   7      11.041   1.811   5.855  1.00 15.76           O
ATOM     51  CB  TYR A   7       9.061   1.065   8.369  1.00 15.35           C
ATOM     52  CG  TYR A   7       7.665   0.929   8.902  1.00 14.45           C
ATOM     53  CD1 TYR A   7       6.771   0.021   8.327  1.00 15.68           C
ATOM     54  CD2 TYR A   7       7.210   1.756   9.920  1.00 14.80           C
ATOM     55  CE1 TYR A   7       5.480  -0.094   8.796  1.00 13.46           C
ATOM     56  CE2 TYR A   7       5.904   1.649  10.416  1.00 14.33           C
ATOM     57  CZ  TYR A   7       5.047   0.729   9.831  1.00 15.09           C
ATOM     58  OH  TYR A   7       3.766   0.589  10.291  1.00 14.39           O
ATOM     59  OXT TYR A   7      11.358   2.999   7.612  1.00 17.49           O
TER      60      TYR A   7
HETATM   61  O   HOH A   8      -6.471   5.227   7.124  1.00 22.62           O
HETATM   62  O   HOH A   9      10.431   1.858   3.216  1.00 19.71           O
HETATM   63  O   HOH A  10     -11.286   1.756  -1.468  1.00 17.08           O
HETATM   64  O   HOH A  11      11.808   4.179   9.970  1.00 23.99           O
HETATM   65  O   HOH A  12      13.605   1.327   9.198  1.00 26.17           O
HETATM   66  O   HOH A  13      -2.749   3.429  10.024  1.00 39.15           O
HETATM   67  O   HOH A  14      -1.500   0.682  10.967  1.00 43.49           O
MASTER      234    0    0    0    0    0    0    6   66    1    0    1
END """

  processed_pdb_file = monomer_library.pdb_interpretation.process(
    mon_lib_srv    = mon_lib_srv,
    ener_lib       = ener_lib,
    raw_records    = pdb_str,
    force_symmetry = True)
  # print dir(processed_pdb_file)
  xrs = processed_pdb_file.xray_structure()
  work_params = master_params().extract()
  work_params.reference_model.enabled=True
  work_params.reference_model.use_starting_model_as_reference=True
  grm = get_geometry_restraints_manager(
      processed_pdb_file,
      xrs,
      params=work_params,
      log=null_out())
  n_ref_dih_prox = grm.geometry.get_n_reference_dihedral_proxies()
  assert n_ref_dih_prox == 34, "expected 34, got %d" % n_ref_dih_prox

  work_params = master_params().extract()
  work_params.reference_model.enabled=True
  work_params.reference_model.use_starting_model_as_reference=True
  work_params.reference_model.main_chain=False
  grm = get_geometry_restraints_manager(
      processed_pdb_file,
      xrs,
      params=work_params,
      log=null_out())
  n_ref_dih_prox = grm.geometry.get_n_reference_dihedral_proxies()
  assert n_ref_dih_prox == 16, "expected 16, got %d" % n_ref_dih_prox

  work_params = master_params().extract()
  work_params.reference_model.enabled=True
  work_params.reference_model.use_starting_model_as_reference=True
  work_params.reference_model.side_chain=False
  grm = get_geometry_restraints_manager(
      processed_pdb_file,
      xrs,
      params=work_params,
      log=null_out())
  n_ref_dih_prox = grm.geometry.get_n_reference_dihedral_proxies()
  assert n_ref_dih_prox == 18, "expected 18, got %d" % n_ref_dih_prox
Example #14
0
  def __init__(self,
      pdb_h,
      xrs,
      target_map,
      grm=None,
      ncs_restraints_group_list=[],
      mon_lib_srv=None,
      rotamer_manager=None,
      ss_annotation=None,
      refine_ncs_operators=False,
      number_of_cycles=1,
      log=None):
    from mmtbx.refinement.geometry_minimization import add_rotamer_restraints
    from mmtbx.model_statistics import geometry_no_grm
    self.pdb_h = pdb_h
    self.xrs = xrs
    self.log = log
    self.cs = self.xrs.crystal_symmetry()
    print >> self.log, "Minimizing using reference map..."
    self.log.flush()
    self.grm = grm
    # create a new one
    # copy-paste from cctbx_project/mmtbx/refinement/geometry_minimization.py:
    # minimize_wrapper_for_ramachandran
    if self.grm is None:
      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 libtbx.utils import null_out
      from scitbx.array_family import flex
      import mmtbx.utils
      if self.log is None:
        self.log = null_out()
      params_line = grand_master_phil_str
      import iotbx.phil
      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.nonbonded_weight = 500
      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= self.cs,
              pdb_interpretation_params = params.pdb_interpretation,
              stop_for_unknowns         = False,
              log=self.log,
              cif_objects=None)
      processed_pdb_file, junk = processed_pdb_files_srv.\
          process_pdb_files(raw_records=flex.split_lines(self.pdb_h.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)
      # dealing with SS
      if ss_annotation is not None:
        from mmtbx.secondary_structure import manager
        ss_manager = manager(
            pdb_hierarchy=self.pdb_h,
            geometry_restraints_manager=grm.geometry,
            sec_str_from_pdb_file=ss_annotation,
            params=None,
            mon_lib_srv=mon_lib_srv,
            verbose=-1,
            log=self.log)
        grm.geometry.set_secondary_structure_restraints(
            ss_manager=ss_manager,
            hierarchy=self.pdb_h,
            log=self.log)
    else:
      self.grm.geometry.pair_proxies(
          sites_cart=self.pdb_h.atoms().extract_xyz())
      if self.grm.geometry.ramachandran_manager is not None:
        self.grm.geometry.ramachandran_manager.update_phi_psi_targets(
            sites_cart=self.pdb_h.atoms().extract_xyz())

    ncs_groups=None
    if len(ncs_restraints_group_list) > 0:
      ncs_groups=ncs_restraints_group_list

    if rotamer_manager is None:
      from mmtbx.rotamer.rotamer_eval import RotamerEval
      rotamer_manager = RotamerEval(mon_lib_srv=mon_lib_srv)

    self.pdb_h.write_pdb_file(file_name="rsr_before_rot_fix.pdb",
        crystal_symmetry=self.xrs.crystal_symmetry())
    # STOP()

    selection_real_space = xrs.backbone_selection()
    # selection_real_space = None
    import mmtbx.refinement.real_space.weight
    self.w = None
    for x in xrange(number_of_cycles):
      print >> self.log, "  Updating rotamer restraints..."
      self.pdb_h, grm = add_rotamer_restraints(
        pdb_hierarchy      = self.pdb_h,
        restraints_manager = grm,
        selection          = None,
        sigma              = 5,
        mode               = "fix_outliers",
        accept_allowed     = False,
        mon_lib_srv        = mon_lib_srv,
        rotamer_manager    = rotamer_manager)
      self.xrs = self.pdb_h.extract_xray_structure(crystal_symmetry=self.cs)
      self.pdb_h.write_pdb_file(file_name="rsr_after_rot_fix.pdb",
          crystal_symmetry=self.xrs.crystal_symmetry())
      # if True:
      if ncs_restraints_group_list is None or len(ncs_restraints_group_list)==0:
        #No NCS
        if self.w is None:
          print >> self.log, "  Determining weight..."
          self.log.flush()
          self.weight = mmtbx.refinement.real_space.weight.run(
              map_data                    = target_map,
              xray_structure              = self.xrs,
              pdb_hierarchy               = self.pdb_h,
              geometry_restraints_manager = grm,
              rms_bonds_limit             = 0.015,
              rms_angles_limit            = 1.0)
          # division is to put more weight onto restraints. Checked. Works.
          self.w = self.weight.weight/3.0
          # self.w = self.weight.weight
          # self.w =2
          # print >> self.log, self.w
          for s in self.weight.msg_strings:
            print >> self.log, s
        print >> self.log, "  Minimizing..."
        print >> self.log, "     with weight %f" % self.w
        self.log.flush()
        refine_object = simple(
            target_map                  = target_map,
            selection                   = None,
            max_iterations              = 150,
            geometry_restraints_manager = grm.geometry,
            selection_real_space        = selection_real_space,
            states_accumulator          = None,
            ncs_groups                  = ncs_groups)
        refine_object.refine(weight = self.w, xray_structure = self.xrs)
        self.rmsd_bonds_final, self.rmsd_angles_final = refine_object.rmsds()
        print >> log, "RMSDS:", self.rmsd_bonds_final, self.rmsd_angles_final
        # print >> log, "sizes:", len(refine_object.sites_cart()), len(self.xrs.scatterers())
        self.xrs=self.xrs.replace_sites_cart(
            new_sites=refine_object.sites_cart(), selection=None)
        # print >> log, "sizes", self.xrs.scatterers()
      else:
        # Yes NCS
        # copy-paste from macro_cycle_real_space.py
        import mmtbx.ncs.ncs_utils as nu
        nu.get_list_of_best_ncs_copy_map_correlation(
            ncs_groups     = ncs_restraints_group_list,
            xray_structure = self.xrs,
            map_data       = target_map,
            d_min          = 3)
        if self.w is None:
          print >> self.log, "  Determining weight... (NCS)",
          self.weight = mmtbx.refinement.real_space.weight.run(
              map_data                    = target_map,
              xray_structure              = self.xrs,#.select(sel_master),
              pdb_hierarchy               = self.pdb_h,#.select(sel_master),
              geometry_restraints_manager = grm,
              rms_bonds_limit             = 0.01,
              rms_angles_limit            = 1.0,
              ncs_groups                  = ncs_restraints_group_list)
          # division supposed to put more weight onto restraints. Need checking.
          self.w = self.weight.weight/3.0
          for s in self.weight.msg_strings:
            print >> self.log, s
        print >> self.log, "  Minimizing... (NCS)"

        actions = [[True, False], ]
        if refine_ncs_operators:
          actions = [[False, True], [True, False]]
        for action in actions:
          refine_sites, refine_transformations = action
          tfg_obj = mmtbx.refinement.minimization_ncs_constraints.\
            target_function_and_grads_real_space(
              map_data                   = target_map,
              xray_structure             = self.xrs,
              ncs_restraints_group_list  = ncs_restraints_group_list,
              refine_selection           = None,
              real_space_gradients_delta = 1,
              restraints_manager         = grm,
              data_weight                = self.w,
              refine_sites               = refine_sites,
              refine_transformations     = refine_transformations)
          minimized = mmtbx.refinement.minimization_ncs_constraints.lbfgs(
            target_and_grads_object      = tfg_obj,
            xray_structure               = self.xrs,
            ncs_restraints_group_list    = ncs_restraints_group_list,
            refine_selection             = None,
            finite_grad_differences_test = False,
            max_iterations               = 100,
            refine_sites                 = refine_sites,
            refine_transformations       = refine_transformations)
          self.xrs = tfg_obj.xray_structure
          # self.structure_monitor.update(
          #   xray_structure = tfg_obj.xray_structure,
          #   accept_as_is   = True)
      self.pdb_h.adopt_xray_structure(self.xrs)
      # ms = geometry_no_grm(
      #     pdb_hierarchy=self.pdb_h,
      #     molprobity_scores=True)
      # print >> self.log, ms.format_molprobity_scores(prefix="    ")


    # print >> log, "pdb_h", self.pdb_h.atoms_size()
    self.pdb_h.write_pdb_file("after_map_min.pdb")