Пример #1
0
 def _minimize(self):
     m1 = self.model.deep_copy()
     rama_params = rama_master_phil.fetch().extract(
     ).ramachandran_plot_restraints
     rama_params.favored = 'oldfield'
     rama_params.allowed = 'oldfield'
     rama_params.outlier = 'oldfield'
     m1.set_ramachandran_plot_restraints(rama_params=rama_params)
     run2(
         restraints_manager=m1.get_restraints_manager(),
         pdb_hierarchy=m1.get_hierarchy(),
         correct_special_position_tolerance=1.0,
         riding_h_manager=None,
         ncs_restraints_group_list=
         [],  # These are actually for NCS CONSTRAINTS!
         max_number_of_iterations=500,
         number_of_macro_cycles=5,
         selection=None,
         bond=True,
         nonbonded=True,
         angle=True,
         dihedral=True,
         chirality=True,
         planarity=True,
         parallelity=True,
         log=null_out())
     m1.set_sites_cart_from_hierarchy(multiply_ncs=True)
     return m1
Пример #2
0
    def minimize(self, ca_only=False):
        sel = "all"
        restraint_sites_cart = self.model.get_sites_cart().deep_copy()
        if ca_only:
            restraint_sites_cart = self.model.get_sites_cart().\
                deep_copy().select(self.model.get_atom_selection_cache().selection("name CA"))
            sel = "name CA"
        self.model.set_reference_coordinate_restraints(ref_model=self.model,
                                                       selection=sel,
                                                       exclude_outliers=False)
        ##### sanity check #####
        # How this check was supposed to hold up when ca_only=True???
        # When ca_only=True, number of restraints will be less than number of atoms.
        # Good that users never change defaults (minimize_c_alpha_only = False).
        #
        # XXX finally, removing this assertion. On top of c_alpha_only case,
        # it also fails (rightfully) when there are water molecules in the model,
        # they are excluded. The same goes to hydrogens, but it seems no one yet
        # tried den_refine for model with hydrogens on.
        # n_rcp = self.model.get_restraints_manager().geometry.get_n_reference_coordinate_proxies()
        # n_sc = len(restraint_sites_cart)
        # assert n_rcp >= n_sc, "%d >= %d fail" % (n_rcp, n_sc)
        ########################

        # selection = self.model.selection_moving
        geometry_minimization.run2(
            restraints_manager=self.model.get_restraints_manager(),
            pdb_hierarchy=self.model.get_hierarchy(),
            max_number_of_iterations=500,
            correct_special_position_tolerance=1.0,
            number_of_macro_cycles=1,
            bond=True,
            nonbonded=True,
            angle=True,
            dihedral=True,
            chirality=True,
            planarity=True)
        self.model.set_sites_cart_from_hierarchy()

        utils.assert_xray_structures_equal(
            x1=self.fmodels.fmodel_xray().xray_structure,
            x2=self.model.get_xray_structure())
        self.model.restraints_manager.geometry.\
            remove_reference_coordinate_restraints_in_place()
Пример #3
0
  def minimize(self, ca_only=False):
    pdb_hierarchy = self.model.get_hierarchy(sync_with_xray_structure=True)
    if ca_only:
      ca_selection = pdb_hierarchy.get_peptide_c_alpha_selection()
      restraint_sites_cart = self.model.get_sites_cart().\
        deep_copy().select(ca_selection)
      restraint_selection = ca_selection
    else:
      restraint_sites_cart = self.model.get_sites_cart().deep_copy()
      restraint_selection = pdb_hierarchy.atoms().extract_i_seq()
    self.model.restraints_manager.geometry.\
        add_reference_coordinate_restraints_in_place(
            pdb_hierarchy=pdb_hierarchy,
            selection=restraint_selection,
            n_atoms_in_target_model=self.model.get_number_of_atoms())
    ##### sanity check #####
    assert(self.model.restraints_manager.geometry.
           get_n_reference_coordinate_proxies() >= len(restraint_sites_cart))
    ########################

    # selection = self.model.selection_moving
    geometry_minimization.run2(
        restraints_manager = self.model.get_restraints_manager(),
        pdb_hierarchy = self.model.get_hierarchy(),
        max_number_of_iterations       = 500,
        correct_special_position_tolerance=1.0,
        number_of_macro_cycles         = 1,
        bond                           = True,
        nonbonded                      = True,
        angle                          = True,
        dihedral                       = True,
        chirality                      = True,
        planarity                      = True)
    self.model.set_sites_cart_from_hierarchy()

    utils.assert_xray_structures_equal(
      x1 = self.fmodels.fmodel_xray().xray_structure,
      x2 = self.model.get_xray_structure())
    self.model.restraints_manager.geometry.\
        remove_reference_coordinate_restraints_in_place()
Пример #4
0
    def minimize(self, ca_only=False):
        sel = "all"
        restraint_sites_cart = self.model.get_sites_cart().deep_copy()
        if ca_only:
            restraint_sites_cart = self.model.get_sites_cart().\
                deep_copy().select(ca_selection)
            sel = "name CA"
        self.model.set_reference_coordinate_restraints(ref_model=self.model,
                                                       selection=sel,
                                                       exclude_outliers=False)
        ##### sanity check #####
        # How this check was supposed to hold up when ca_only=True???
        # When ca_only=True, number of restraints will be less than number of atoms.
        # Good that users never change defaults (minimize_c_alpha_only = False).
        n_rcp = self.model.get_restraints_manager(
        ).geometry.get_n_reference_coordinate_proxies()
        n_sc = len(restraint_sites_cart)
        assert n_rcp >= n_sc, "%d >= %d fail" % (n_rcp, n_sc)
        ########################

        # selection = self.model.selection_moving
        geometry_minimization.run2(
            restraints_manager=self.model.get_restraints_manager(),
            pdb_hierarchy=self.model.get_hierarchy(),
            max_number_of_iterations=500,
            correct_special_position_tolerance=1.0,
            number_of_macro_cycles=1,
            bond=True,
            nonbonded=True,
            angle=True,
            dihedral=True,
            chirality=True,
            planarity=True)
        self.model.set_sites_cart_from_hierarchy()

        utils.assert_xray_structures_equal(
            x1=self.fmodels.fmodel_xray().xray_structure,
            x2=self.model.get_xray_structure())
        self.model.restraints_manager.geometry.\
            remove_reference_coordinate_restraints_in_place()
Пример #5
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,
    )
Пример #6
0
def exercise(tolerance=0.01):
    for use_neutron_distances in [True, False]:
        mon_lib_srv = monomer_library.server.server()
        ener_lib = monomer_library.server.ener_lib()
        inp = iotbx.pdb.input(lines=pdb_str.splitlines(), source_info=None)
        params = iotbx.phil.parse(input_string=grand_master_phil_str,
                                  process_includes=True).extract()
        params.pdb_interpretation.use_neutron_distances = use_neutron_distances
        model = mmtbx.model.manager(model_input=inp)
        model.process(pdb_interpretation_params=params, make_restraints=True)
        ph = model.get_hierarchy()
        ph.write_pdb_file(file_name="input.pdb")

        m = geometry_minimization.run2(
            restraints_manager=model.get_restraints_manager(),
            pdb_hierarchy=ph,
            correct_special_position_tolerance=1.0,
            bond=True,
            nonbonded=True,
            angle=True,
            dihedral=True,
            chirality=True,
            planarity=True)
        model.set_sites_cart_from_hierarchy()
        suffix = "X"
        if (use_neutron_distances): suffix = "N"
        ph.write_pdb_file(file_name="output_%s.pdb" % suffix)
    # check X-H distances: x-ray
    cntr = 0
    awl = iotbx.pdb.input(file_name="output_X.pdb").atoms_with_labels()
    for a1 in awl:
        n1 = a1.name.strip()
        for a2 in awl:
            n2 = a2.name.strip()
            if ([n1, n2] == ["CD1", "HD1"] or [n1, n2] == ["CD2", "HD2"]
                    or [n1, n2] == ["CE1", "HE1"]
                    or [n1, n2] == ["CE2", "HE2"]):
                assert approx_equal(dist(a1.xyz, a2.xyz), 0.93, tolerance)
                cntr += 1
            if (n1 == "OH" and n2 in ["HH", "DH"]):
                assert approx_equal(dist(a1.xyz, a2.xyz), 0.84, tolerance)
                cntr += 1
            if ([n1, n2] == ["CB", "HB2"] or [n1, n2] == ["CB", "HB3"]
                    or [n1, n2] == ["CA", "HA"]):
                assert approx_equal(dist(a1.xyz, a2.xyz), 0.97, tolerance)
                cntr += 1
            if (n1 == "O" and n2 in ["D1", "D2"] and a1.resname == "HOH"):
                assert approx_equal(dist(a1.xyz, a2.xyz), 0.85, tolerance)
                cntr += 1
    assert cntr == 11, cntr
    # check X-H distances: neutron
    cntr = 0
    awl = iotbx.pdb.input(file_name="output_N.pdb").atoms_with_labels()
    for a1 in awl:
        n1 = a1.name.strip()
        for a2 in awl:
            n2 = a2.name.strip()
            if ([n1, n2] == ["CD1", "HD1"] or [n1, n2] == ["CD2", "HD2"]
                    or [n1, n2] == ["CE1", "HE1"]
                    or [n1, n2] == ["CE2", "HE2"]):
                assert approx_equal(dist(a1.xyz, a2.xyz), 1.08, tolerance), """
        dist(a1.xyz, a2.xyz) : %f
        tolerance            : %f
        """ % (
                    dist(a1.xyz, a2.xyz),
                    tolerance,
                )
                cntr += 1
            if (n1 == "OH" and n2 in ["HH", "DH"]):
                assert approx_equal(dist(a1.xyz, a2.xyz), 0.98, tolerance)
                cntr += 1
            if ([n1, n2] == ["CB", "HB2"] or [n1, n2] == ["CB", "HB3"]
                    or [n1, n2] == ["CA", "HA"]):
                assert approx_equal(dist(a1.xyz, a2.xyz), 1.09, tolerance)
                cntr += 1
            if (n1 == "O" and n2 in ["D1", "D2"] and a1.resname == "HOH"):
                assert approx_equal(dist(a1.xyz, a2.xyz), 0.98, tolerance)
                cntr += 1
    assert cntr == 11, cntr
Пример #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()

  # 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()
Пример #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()

  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()