def set_pdb_data(self, pdb_hierarchy, ignore_unk=False):
     from mmtbx import secondary_structure
     sec_str = secondary_structure.manager(pdb_hierarchy)
     sec_str.show_summary()
     self.set_data(pdb_hierarchy=pdb_hierarchy,
                   sec_str=sec_str,
                   auto_select=True,
                   ignore_unk=ignore_unk)
 def set_pdb_data (self, pdb_hierarchy, ignore_unk=False) :
   from mmtbx import secondary_structure
   sec_str = secondary_structure.manager(pdb_hierarchy)
   sec_str.show_summary()
   self.set_data(
     pdb_hierarchy=pdb_hierarchy,
     sec_str=sec_str,
     auto_select=True,
     ignore_unk=ignore_unk)
Exemple #3
0
 def initialize_cartoon(self, sec_str=None):
     if (sec_str is None):
         from mmtbx import secondary_structure
         manager = secondary_structure.manager(
             pdb_hierarchy=self.pdb_hierarchy, xray_structure=None)
         sec_str = manager.selections_as_ints()
     from crys3d import ribbon
     self.ribbon = ribbon.cartoon(pdb_hierarchy=self.pdb_hierarchy,
                                  sec_str=sec_str)
     self.ribbon.construct_geometry()
  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
Exemple #5
0
 def initialize_cartoon (self, sec_str=None) :
   if (sec_str is None) :
     from mmtbx import secondary_structure
     manager = secondary_structure.manager(
       pdb_hierarchy=self.pdb_hierarchy,
       xray_structure=None)
     sec_str = manager.selections_as_ints()
   from crys3d import ribbon
   self.ribbon = ribbon.cartoon(pdb_hierarchy=self.pdb_hierarchy,
     sec_str=sec_str)
   self.ribbon.construct_geometry()
 def set_ss_restraints(self, ss_annotation, params=None):
   log = self.log
   if not self.verbose:
     log = null_out()
   if self.params.use_ss_restraints and ss_annotation is not None:
     ss_manager = manager(
         pdb_hierarchy=self.model.get_hierarchy(),
         geometry_restraints_manager=self.model.get_restraints_manager().geometry,
         sec_str_from_pdb_file=ss_annotation,
         params=None,
         mon_lib_srv=self.model.get_mon_lib_srv(),
         verbose=-1,
         log=log)
     self.model.get_restraints_manager().geometry.set_secondary_structure_restraints(
         ss_manager=ss_manager,
         hierarchy=self.model.get_hierarchy(),
         log=log)
def run (args, viewer_class=selection_editor_mixin) :
  import cStringIO
  pdb_files = []
  cif_files = []
  show_ss_restraints = False
  fast_connectivity = True
  for arg in args :
    if os.path.isfile(arg) :
      import iotbx.pdb
      if iotbx.pdb.is_pdb_file(arg) :
        pdb_files.append(os.path.abspath(arg))
      elif arg.endswith(".cif") :
        cif_files.append(os.path.abspath(arg))
    elif arg == "--ss" :
      show_ss_restraints = True
    elif arg in ["--thorough", "--slow", "--use_monomer_library"] :
      fast_connectivity = False
  if len(pdb_files) == 0 :
    print "Please specify a PDB file (and optional CIFs) on the command line."
    return
  a = App(viewer_class=viewer_class)
  a.frame.Show()
  out = sys.stdout
  if not "--debug" in args :
    out = cStringIO.StringIO()
  for file_name in pdb_files :
    print "Reading PDB file %s" % file_name
    from iotbx import file_reader
    from mmtbx.monomer_library import pdb_interpretation
    from mmtbx import secondary_structure
    t1 = time.time()
    if fast_connectivity :
      pdb_in = file_reader.any_file(file_name, force_type="pdb")
      pdb_hierarchy = pdb_in.file_object.hierarchy
      atomic_bonds = pdb_hierarchy.distance_based_simple_two_way_bond_sets()
      acp_selection = None
    else :
      processed_pdb_file = pdb_interpretation.run(args=[file_name]+cif_files,
        log=out)
      pdb_hierarchy = processed_pdb_file.all_chain_proxies.pdb_hierarchy
      pdb_hierarchy.atoms().reset_i_seq()
      grm = processed_pdb_file.geometry_restraints_manager()
      acp_selection = processed_pdb_file.all_chain_proxies.selection
      if grm is None or grm.shell_sym_tables is None :
        raise Sorry("Atomic bonds could not be calculated for this model. "+
          "This is probably due to a missing CRYST1 record in the PDB file.")
      atomic_bonds = grm.shell_sym_tables[0].full_simple_connectivity()
    t2 = time.time()
    print "%.2fs" % (t2-t1)
    a.view_objects.add_model(file_name, pdb_hierarchy, atomic_bonds,
      mmtbx_selection_function=acp_selection)
    sec_str = secondary_structure.manager(
      pdb_hierarchy=pdb_hierarchy,
      xray_structure=None)
    a.view_objects.set_sec_str(file_name, sec_str.selections_as_ints())
    if show_ss_restraints and acp_selection is not None :
      bonds_table = secondary_structure.process_structure(params=None,
        processed_pdb_file=processed_pdb_file,
        tmp_dir=os.getcwd(),
        log=sys.stderr)
      a.view_objects.set_noncovalent_bonds(file_name, bonds_table.bonds)
      a.view_objects.flag_show_noncovalent_bonds = True
      a.view_objects.set_model_base_color([1.0,1.0,1.0], file_name)
      a.view_objects.set_color_mode("element")
  a.view_objects.force_update(recenter=True)
  a.MainLoop()
def exercise_helix_bonding_pattern_with_insertions():
  alpha_h1_ends = iotbx.pdb.hierarchy.input(pdb_string="""\
ATOM      1  N   ALA     1       1.643  -2.366  -1.408  1.00  0.00           N
ATOM      2  CA  ALA     1       1.280  -3.608  -2.069  1.00  0.00           C
ATOM      3  C   ALA     1      -0.114  -3.466  -2.684  1.00  0.00           C
ATOM      4  O   ALA     1      -0.327  -3.827  -3.840  1.00  0.00           O
ATOM      5  CB  ALA     1       1.361  -4.762  -1.068  1.00  0.00           C
ATOM      6  N   ALA     1A     -1.028  -2.938  -1.882  1.00  0.00           N
ATOM      7  CA  ALA     1A     -2.395  -2.743  -2.332  1.00  0.00           C
ATOM      8  C   ALA     1A     -2.396  -1.855  -3.579  1.00  0.00           C
ATOM      9  O   ALA     1A     -3.059  -2.167  -4.567  1.00  0.00           O
ATOM     10  CB  ALA     1A     -3.228  -2.150  -1.194  1.00  0.00           C
ATOM     11  N   ALA     3      -1.646  -0.767  -3.491  1.00  0.00           N
ATOM     12  CA  ALA     3      -1.551   0.168  -4.599  1.00  0.00           C
ATOM     13  C   ALA     3      -1.044  -0.568  -5.841  1.00  0.00           C
ATOM     14  O   ALA     3      -1.601  -0.419  -6.927  1.00  0.00           O
ATOM     15  CB  ALA     3      -0.646   1.337  -4.205  1.00  0.00           C
ATOM     16  N   ALA     4       0.008  -1.348  -5.639  1.00  0.00           N
ATOM     17  CA  ALA     4       0.597  -2.109  -6.728  1.00  0.00           C
ATOM     18  C   ALA     4      -0.466  -3.023  -7.340  1.00  0.00           C
ATOM     19  O   ALA     4      -0.611  -3.085  -8.559  1.00  0.00           O
ATOM     20  CB  ALA     4       1.808  -2.887  -6.211  1.00  0.00           C
ATOM     21  N   ALA     5      -1.184  -3.711  -6.463  1.00  0.00           N
ATOM     22  CA  ALA     5      -2.231  -4.619  -6.901  1.00  0.00           C
ATOM     23  C   ALA     5      -3.253  -3.847  -7.737  1.00  0.00           C
ATOM     24  O   ALA     5      -3.647  -4.296  -8.813  1.00  0.00           O
ATOM     25  CB  ALA     5      -2.864  -5.294  -5.683  1.00  0.00           C
ATOM     26  N   ALA     6      -3.654  -2.699  -7.211  1.00  0.00           N
ATOM     27  CA  ALA     6      -4.623  -1.860  -7.896  1.00  0.00           C
ATOM     28  C   ALA     6      -4.090  -1.499  -9.284  1.00  0.00           C
ATOM     29  O   ALA     6      -4.809  -1.602 -10.276  1.00  0.00           O
ATOM     30  CB  ALA     6      -4.919  -0.623  -7.045  1.00  0.00           C
ATOM     31  N   ALA     7      -2.831  -1.084  -9.309  1.00  0.00           N
ATOM     32  CA  ALA     7      -2.192  -0.708 -10.559  1.00  0.00           C
ATOM     33  C   ALA     7      -2.243  -1.890 -11.529  1.00  0.00           C
ATOM     34  O   ALA     7      -2.600  -1.727 -12.695  1.00  0.00           O
ATOM     35  CB  ALA     7      -0.761  -0.243 -10.281  1.00  0.00           C
ATOM     36  N   ALA     8      -1.881  -3.055 -11.012  1.00  0.00           N
ATOM     37  CA  ALA     8      -1.882  -4.264 -11.817  1.00  0.00           C
ATOM     38  C   ALA     8      -3.285  -4.496 -12.382  1.00  0.00           C
ATOM     39  O   ALA     8      -3.442  -4.772 -13.570  1.00  0.00           O
ATOM     40  CB  ALA     8      -1.391  -5.441 -10.972  1.00  0.00           C
ATOM     41  N   ALA     9      -4.269  -4.376 -11.503  1.00  0.00           N
ATOM     42  CA  ALA     9      -5.653  -4.568 -11.898  1.00  0.00           C
ATOM     43  C   ALA     9      -6.000  -3.590 -13.022  1.00  0.00           C
ATOM     44  O   ALA     9      -6.590  -3.978 -14.029  1.00  0.00           O
ATOM     45  CB  ALA     9      -6.561  -4.400 -10.678  1.00  0.00           C
ATOM     46  N   ALA     9A     -5.617  -2.338 -12.812  1.00  0.00           N
ATOM     47  CA  ALA     9A     -5.879  -1.301 -13.795  1.00  0.00           C
ATOM     48  C   ALA     9A     -5.242  -1.695 -15.130  1.00  0.00           C
ATOM     49  O   ALA     9A     -5.880  -1.605 -16.177  1.00  0.00           O
ATOM     50  CB  ALA     9A     -5.358   0.040 -13.274  1.00  0.00           C
TER
""")

  alpha_h1_ac = iotbx.pdb.hierarchy.input(pdb_string="""\
ATOM      1  N  AALA     1       1.643  -2.366  -1.408  0.50  0.00           N
ATOM      2  CA AALA     1       1.280  -3.608  -2.069  0.50  0.00           C
ATOM      3  C  AALA     1      -0.114  -3.466  -2.684  0.50  0.00           C
ATOM      4  O  AALA     1      -0.327  -3.827  -3.840  0.50  0.00           O
ATOM      5  CB AALA     1       1.361  -4.762  -1.068  0.50  0.00           C
ATOM      6  N  BALA     1       1.743  -2.366  -1.408  0.50  0.00           N
ATOM      7  CA BALA     1       1.380  -3.608  -2.069  0.50  0.00           C
ATOM      8  C  BALA     1      -0.014  -3.466  -2.684  0.50  0.00           C
ATOM      9  O  BALA     1      -0.227  -3.827  -3.840  0.50  0.00           O
ATOM     10  CB BALA     1       1.461  -4.762  -1.068  0.50  0.00           C
ATOM     11  N   ALA     1A     -1.028  -2.938  -1.882  1.00  0.00           N
ATOM     12  CA  ALA     1A     -2.395  -2.743  -2.332  1.00  0.00           C
ATOM     13  C   ALA     1A     -2.396  -1.855  -3.579  1.00  0.00           C
ATOM     14  O   ALA     1A     -3.059  -2.167  -4.567  1.00  0.00           O
ATOM     15  CB  ALA     1A     -3.228  -2.150  -1.194  1.00  0.00           C
ATOM     16  N   ALA     3      -1.646  -0.767  -3.491  1.00  0.00           N
ATOM     17  CA  ALA     3      -1.551   0.168  -4.599  1.00  0.00           C
ATOM     18  C   ALA     3      -1.044  -0.568  -5.841  1.00  0.00           C
ATOM     19  O   ALA     3      -1.601  -0.419  -6.927  1.00  0.00           O
ATOM     20  CB  ALA     3      -0.646   1.337  -4.205  1.00  0.00           C
ATOM     21  N   ALA     4       0.008  -1.348  -5.639  1.00  0.00           N
ATOM     22  CA  ALA     4       0.597  -2.109  -6.728  1.00  0.00           C
ATOM     23  C   ALA     4      -0.466  -3.023  -7.340  1.00  0.00           C
ATOM     24  O   ALA     4      -0.611  -3.085  -8.559  1.00  0.00           O
ATOM     25  CB  ALA     4       1.808  -2.887  -6.211  1.00  0.00           C
ATOM     26  N  AALA     5      -1.184  -3.711  -6.463  0.50  0.00           N
ATOM     27  CA AALA     5      -2.231  -4.619  -6.901  0.50  0.00           C
ATOM     28  C  AALA     5      -3.253  -3.847  -7.737  0.50  0.00           C
ATOM     29  O  AALA     5      -3.647  -4.296  -8.813  0.50  0.00           O
ATOM     30  CB AALA     5      -2.864  -5.294  -5.683  0.50  0.00           C
ATOM     31  N  BALA     5      -1.084  -3.711  -6.463  0.50  0.00           N
ATOM     32  CA BALA     5      -2.131  -4.619  -6.901  0.50  0.00           C
ATOM     33  C  BALA     5      -3.153  -3.847  -7.737  0.50  0.00           C
ATOM     34  O  BALA     5      -3.547  -4.296  -8.813  0.50  0.00           O
ATOM     35  CB BALA     5      -2.764  -5.294  -5.683  0.50  0.00           C
ATOM     36  N   ALA     6      -3.654  -2.699  -7.211  1.00  0.00           N
ATOM     37  CA  ALA     6      -4.623  -1.860  -7.896  1.00  0.00           C
ATOM     38  C   ALA     6      -4.090  -1.499  -9.284  1.00  0.00           C
ATOM     39  O   ALA     6      -4.809  -1.602 -10.276  1.00  0.00           O
ATOM     40  CB  ALA     6      -4.919  -0.623  -7.045  1.00  0.00           C
ATOM     41  N   ALA     7      -2.831  -1.084  -9.309  1.00  0.00           N
ATOM     42  CA  ALA     7      -2.192  -0.708 -10.559  1.00  0.00           C
ATOM     43  C   ALA     7      -2.243  -1.890 -11.529  1.00  0.00           C
ATOM     44  O   ALA     7      -2.600  -1.727 -12.695  1.00  0.00           O
ATOM     45  CB  ALA     7      -0.761  -0.243 -10.281  1.00  0.00           C
ATOM     46  N   ALA     8      -1.881  -3.055 -11.012  1.00  0.00           N
ATOM     47  CA  ALA     8      -1.882  -4.264 -11.817  1.00  0.00           C
ATOM     48  C   ALA     8      -3.285  -4.496 -12.382  1.00  0.00           C
ATOM     49  O   ALA     8      -3.442  -4.772 -13.570  1.00  0.00           O
ATOM     50  CB  ALA     8      -1.391  -5.441 -10.972  1.00  0.00           C
ATOM     51  N   ALA     9      -4.269  -4.376 -11.503  1.00  0.00           N
ATOM     52  CA  ALA     9      -5.653  -4.568 -11.898  1.00  0.00           C
ATOM     53  C   ALA     9      -6.000  -3.590 -13.022  1.00  0.00           C
ATOM     54  O   ALA     9      -6.590  -3.978 -14.029  1.00  0.00           O
ATOM     55  CB  ALA     9      -6.561  -4.400 -10.678  1.00  0.00           C
ATOM     56  N  AALA     9A     -5.617  -2.338 -12.812  0.50  0.00           N
ATOM     57  CA AALA     9A     -5.879  -1.301 -13.795  0.50  0.00           C
ATOM     58  C  AALA     9A     -5.242  -1.695 -15.130  0.50  0.00           C
ATOM     59  O  AALA     9A     -5.880  -1.605 -16.177  0.50  0.00           O
ATOM     60  CB AALA     9A     -5.358   0.040 -13.274  0.50  0.00           C
ATOM     62  N  BALA     9A     -5.517  -2.338 -12.812  0.50  0.00           N
ATOM     63  CA BALA     9A     -5.779  -1.301 -13.795  0.50  0.00           C
ATOM     64  C  BALA     9A     -5.142  -1.695 -15.130  0.50  0.00           C
ATOM     65  O  BALA     9A     -5.780  -1.605 -16.177  0.50  0.00           O
ATOM     66  CB BALA     9A     -5.258   0.040 -13.274  0.50  0.00           C
END
""")

  alpha_annot_1_1 = """\
HELIX    1   1 ALA      1  ALA      9A 1                                  10
"""
  alpha_annot_1_2 = """\
HELIX    1   1 ALA      1A ALA      9  1                                  10
"""
  alpha_annot_1_3 = """\
HELIX    1   1 ALA      1A ALA      9A 1                                  10
"""
  alpha_annot_1_4 = """\
HELIX    1   1 ALA      1  ALA      9  1                                  10
"""

  alpha_annot_2 = """\
HELIX    1   1 ALA      2  ALA      5  1                                  10
"""

  log = null_out()
  n_hbonds = []
  for pdb_inp, recs in [
                        (alpha_h1_ends, alpha_annot_1_1), #6
                        (alpha_h1_ends, alpha_annot_1_2), #4
                        (alpha_h1_ends, alpha_annot_1_3), #5
                        (alpha_h1_ends, alpha_annot_1_4), #5
                        (alpha_h1_ac, alpha_annot_1_1), #9
                        (alpha_h1_ac, alpha_annot_1_2), #5
                        (alpha_h1_ac, alpha_annot_1_3), #7
                        (alpha_h1_ac, alpha_annot_1_4), #7
                        ]:
    ioss_annotation = ioss.annotation.from_records(records = recs.split('\n'))
    ann = ioss_annotation.as_restraint_groups(prefix_scope="secondary_structure")
    defpars = iotbx.phil.parse(sec_str_master_phil_str)
    custom_pars = defpars.fetch(iotbx.phil.parse(ann))
    custom_pars_ex = custom_pars.extract()
    ss_manager = manager(
                pdb_inp.hierarchy,
                sec_str_from_pdb_file=None,
                params=custom_pars_ex.secondary_structure,
                verbose=-1)
    proxies_for_grm = ss_manager.create_protein_hbond_proxies(
      annotation= None,
      log          = log)
    n_hbonds.append(proxies_for_grm.size())
  print n_hbonds
  assert n_hbonds == [6, 4, 5, 5, 9, 5, 7, 7]
Exemple #9
0
def run(args, params=None, out=sys.stdout, log=sys.stderr):
    # params keyword is for running program from GUI dialog
    if (((len(args) == 0) and (params is None)) or
        ((len(args) > 0) and ((args[0] == "-h") or (args[0] == "--help")))):
        show_usage()
        return

    # parse command-line arguments
    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

    work_params.secondary_structure.enabled = True
    assert work_params.format in [
        "phenix", "phenix_refine", "phenix_bonds", "pymol", "refmac",
        "kinemage", "pdb"
    ]
    if work_params.quiet:
        out = cStringIO.StringIO()

    pdb_combined = iotbx.pdb.combine_unique_pdb_files(file_names=pdb_files)
    pdb_structure = iotbx.pdb.input(source_info=None,
                                    lines=flex.std_string(
                                        pdb_combined.raw_records))
    cs = pdb_structure.crystal_symmetry()

    corrupted_cs = False
    if cs is not None:
        if [cs.unit_cell(), cs.space_group()].count(None) > 0:
            corrupted_cs = True
            cs = None
        elif cs.unit_cell().volume() < 10:
            corrupted_cs = True
            cs = None

    if cs is None:
        if corrupted_cs:
            print >> out, "Symmetry information is corrupted, "
        else:
            print >> out, "Symmetry information was not found, "
        print >> out, "putting molecule in P1 box."
        from cctbx import uctbx
        atoms = pdb_structure.atoms()
        box = uctbx.non_crystallographic_unit_cell_with_the_sites_in_its_center(
            sites_cart=atoms.extract_xyz(), buffer_layer=3)
        atoms.set_xyz(new_xyz=box.sites_cart)
        cs = box.crystal_symmetry()

    defpars = mmtbx.model.manager.get_default_pdb_interpretation_params()
    defpars.pdb_interpretation.automatic_linking.link_carbohydrates = False
    defpars.pdb_interpretation.c_beta_restraints = False
    defpars.pdb_interpretation.clash_guard.nonbonded_distance_threshold = None
    model = mmtbx.model.manager(model_input=pdb_structure,
                                crystal_symmetry=cs,
                                pdb_interpretation_params=defpars,
                                stop_for_unknowns=False)
    pdb_hierarchy = model.get_hierarchy()
    geometry = model.get_restraints_manager().geometry
    if len(pdb_hierarchy.models()) != 1:
        raise Sorry("Multiple models not supported.")
    ss_from_file = None
    if (hasattr(pdb_structure, "extract_secondary_structure")
            and not work_params.ignore_annotation_in_file):
        ss_from_file = pdb_structure.extract_secondary_structure()
    m = manager(pdb_hierarchy=pdb_hierarchy,
                geometry_restraints_manager=geometry,
                sec_str_from_pdb_file=ss_from_file,
                params=work_params.secondary_structure,
                verbose=work_params.verbose)

    # bp_p = nucleic_acids.get_basepair_plane_proxies(
    #     pdb_hierarchy,
    #     m.params.secondary_structure.nucleic_acid.base_pair,
    #     geometry)
    # st_p = nucleic_acids.get_stacking_proxies(
    #     pdb_hierarchy,
    #     m.params.secondary_structure.nucleic_acid.stacking_pair,
    #     geometry)
    # hb_b, hb_a = nucleic_acids.get_basepair_hbond_proxies(pdb_hierarchy,
    #     m.params.secondary_structure.nucleic_acid.base_pair)
    result_out = cStringIO.StringIO()
    # prefix_scope="refinement.pdb_interpretation"
    # prefix_scope=""
    prefix_scope = ""
    if work_params.format == "phenix_refine":
        prefix_scope = "refinement.pdb_interpretation"
    elif work_params.format == "phenix":
        prefix_scope = "pdb_interpretation"
    ss_phil = None
    working_phil = m.as_phil_str(master_phil=sec_str_master_phil)
    phil_diff = sec_str_master_phil.fetch_diff(source=working_phil)

    if work_params.format in ["phenix", "phenix_refine"]:
        comment = "\n".join([
            "# These parameters are suitable for use in e.g. phenix.real_space_refine",
            "# or geometry_minimization. To use them in phenix.refine add ",
            "# 'refinement.' if front of pdb_interpretation."
        ])
        if work_params.format == "phenix_refine":
            comment = "\n".join([
                "# These parameters are suitable for use in phenix.refine only.",
                "# To use them in other Phenix tools remove ",
                "# 'refinement.' if front of pdb_interpretation."
            ])
        print >> result_out, comment
        if (prefix_scope != ""):
            print >> result_out, "%s {" % prefix_scope
        if work_params.show_all_params:
            working_phil.show(prefix="  ", out=result_out)
        else:
            phil_diff.show(prefix="  ", out=result_out)
        if (prefix_scope != ""):
            print >> result_out, "}"
    elif work_params.format == "pdb":
        print >> result_out, m.actual_sec_str.as_pdb_str()
    elif work_params.format == "phenix_bonds":
        raise Sorry("Not yet implemented.")
    elif work_params.format in ["pymol", "refmac", "kinemage"]:
        m.show_summary(log=out)
        (hb_proxies, hb_angle_proxies, planarity_proxies,
         parallelity_proxies) = m.create_all_new_restraints(
             pdb_hierarchy=pdb_hierarchy, grm=geometry, log=out)
        if hb_proxies.size() > 0:
            if work_params.format == "pymol":
                file_load_add = "load %s" % work_params.file_name[0]
                # surprisingly, pymol handles filenames with whitespaces without quotes...
                print >> result_out, file_load_add
                bonds_in_format = hb_proxies.as_pymol_dashes(
                    pdb_hierarchy=pdb_hierarchy)
            elif work_params.format == "kinemage":
                bonds_in_format = hb_proxies.as_kinemage(
                    pdb_hierarchy=pdb_hierarchy)
            else:
                bonds_in_format = hb_proxies.as_refmac_restraints(
                    pdb_hierarchy=pdb_hierarchy)
            print >> result_out, bonds_in_format
        if hb_angle_proxies.size() > 0:
            if work_params.format == "pymol":
                angles_in_format = hb_angle_proxies.as_pymol_dashes(
                    pdb_hierarchy=pdb_hierarchy)
                print >> result_out, angles_in_format
    result = result_out.getvalue()
    out_prefix = os.path.basename(work_params.file_name[0])
    if work_params.output_prefix is not None:
        out_prefix = work_params.output_prefix
    filename = "%s_ss.eff" % out_prefix
    if work_params.format == "pymol":
        filename = "%s_ss.pml" % out_prefix
    outf = open(filename, "w")
    outf.write(result)
    outf.close()
    print >> out, result

    return os.path.abspath(filename)
    def get_reference_dihedral_proxies(self, processed_pdb_file):
        complete_dihedral_proxies = utils.get_dihedrals_and_phi_psi(
            processed_pdb_file=processed_pdb_file)
        generated_reference_dihedral_proxies = \
          cctbx.geometry_restraints.shared_dihedral_proxy()
        sigma = self.params.sigma
        limit = self.params.limit
        ref_ss_m = None
        ss_selection = None
        if self.params.secondary_structure_only:
            if (not libtbx.env.has_module(name="ksdssp")):
                raise RuntimeError(
                  "ksdssp module is not configured, "+\
                  "cannot generate secondary structure reference")
            ref_ss_m = {}
            ss_selection = {}
            for file in self.reference_file_list:
                ref_ss_m[file] = secondary_structure.manager(
                    pdb_hierarchy=self.pdb_hierarchy_ref[file],
                    sec_str_from_pdb_file=None)
                sec_str_from_pdb_file = ref_ss_m[file].actual_sec_str
                if sec_str_from_pdb_file != None:
                    overall_selection = sec_str_from_pdb_file.overall_selection(
                    )
                    sel_cache_ref = self.pdb_hierarchy_ref[
                        file].atom_selection_cache()
                    bsel = sel_cache_ref.selection(string=overall_selection)
                    if bsel.all_eq(False):
                        raise Sorry("No atom selected")
                    ss_selection[file] = bsel

        t_sum = 0
        for dp in complete_dihedral_proxies:
            key_work = ""
            complete = True
            for i_seq in dp.i_seqs:
                if not self.selection[i_seq]:
                    complete = False
            if not complete:
                continue

            for i_seq in dp.i_seqs:
                key_work = key_work + self.i_seq_name_hash[i_seq]
            #find matching key
            key = None
            file_match = None
            for file in self.reference_file_list:
                if key is not None or file not in self.match_map:
                    continue
                else:
                    key = ""
                    ref_match = True
                    for i_seq in dp.i_seqs:
                        if ref_match:
                            map_part = self.match_map[file].get(i_seq)
                            if map_part is not None:
                                key_part = self.i_seq_name_hash_ref[file].get(
                                    map_part)
                                if key_part is None:
                                    ref_match = False
                                    key = None
                                    file_match = None
                                else:
                                    key = key + key_part
                            else:
                                ref_match = False
                                key = None
                                file_match = None
                        if key is not None:
                            file_match = file
            try:
                reference_angle = self.reference_dihedral_hash[file_match][key]
            except Exception:
                continue
            w_limit = limit
            w_weight = 1 / sigma**2
            if (self.params.secondary_structure_only
                    and ss_selection is not None
                    and ss_selection[file_match] is not None):
                limit2 = 15.0
                w_weight = 0.04
                if (ss_selection[file_match][self.match_map[file_match][
                        dp.i_seqs[0]]] and ss_selection[file_match][
                            self.match_map[file_match][dp.i_seqs[1]]]
                        and ss_selection[file_match][self.match_map[file_match]
                                                     [dp.i_seqs[2]]]
                        and ss_selection[file_match][self.match_map[file_match]
                                                     [dp.i_seqs[3]]]):
                    limit2 = 30.0
                    w_weight = 1
                dp_add = cctbx.geometry_restraints.dihedral_proxy(
                    i_seqs=dp.i_seqs,
                    angle_ideal=reference_angle,
                    weight=w_weight,
                    limit=w_limit,
                    top_out=TOP_OUT_FLAG)
                generated_reference_dihedral_proxies.append(dp_add)
            else:
                dp_add = cctbx.geometry_restraints.dihedral_proxy(
                    i_seqs=dp.i_seqs,
                    angle_ideal=reference_angle,
                    weight=1 / sigma**2,
                    limit=limit,
                    top_out=TOP_OUT_FLAG)
                # print "Already_there:", self._is_proxy_already_present(generated_reference_dihedral_proxies,dp_add)
                # if not self._is_proxy_already_present(generated_reference_dihedral_proxies,dp_add):
                generated_reference_dihedral_proxies.append(dp_add)
        self.reference_dihedral_proxies = generated_reference_dihedral_proxies
Exemple #11
0
def run(args, viewer_class=selection_editor_mixin):
    import cStringIO
    pdb_files = []
    cif_files = []
    show_ss_restraints = False
    fast_connectivity = True
    for arg in args:
        if os.path.isfile(arg):
            import iotbx.pdb
            if iotbx.pdb.is_pdb_file(arg):
                pdb_files.append(os.path.abspath(arg))
            elif arg.endswith(".cif"):
                cif_files.append(os.path.abspath(arg))
        elif arg == "--ss":
            show_ss_restraints = True
        elif arg in ["--thorough", "--slow", "--use_monomer_library"]:
            fast_connectivity = False
    if len(pdb_files) == 0:
        print(
            "Please specify a PDB file (and optional CIFs) on the command line."
        )
        return
    a = App(viewer_class=viewer_class)
    a.frame.Show()
    out = sys.stdout
    if not "--debug" in args:
        out = cStringIO.StringIO()
    for file_name in pdb_files:
        print("Reading PDB file %s" % file_name)
        from iotbx import file_reader
        from mmtbx.monomer_library import pdb_interpretation
        from mmtbx import secondary_structure
        t1 = time.time()
        if fast_connectivity:
            pdb_in = file_reader.any_file(file_name, force_type="pdb")
            pdb_hierarchy = pdb_in.file_object.hierarchy
            atomic_bonds = pdb_hierarchy.distance_based_simple_two_way_bond_sets(
            )
            acp_selection = None
        else:
            processed_pdb_file = pdb_interpretation.run(args=[file_name] +
                                                        cif_files,
                                                        log=out)
            pdb_hierarchy = processed_pdb_file.all_chain_proxies.pdb_hierarchy
            pdb_hierarchy.atoms().reset_i_seq()
            grm = processed_pdb_file.geometry_restraints_manager()
            acp_selection = processed_pdb_file.all_chain_proxies.selection
            if grm is None or grm.shell_sym_tables is None:
                raise Sorry(
                    "Atomic bonds could not be calculated for this model. " +
                    "This is probably due to a missing CRYST1 record in the PDB file."
                )
            atomic_bonds = grm.shell_sym_tables[0].full_simple_connectivity()
        t2 = time.time()
        print("%.2fs" % (t2 - t1))
        a.view_objects.add_model(file_name,
                                 pdb_hierarchy,
                                 atomic_bonds,
                                 mmtbx_selection_function=acp_selection)
        sec_str = secondary_structure.manager(pdb_hierarchy=pdb_hierarchy,
                                              xray_structure=None)
        a.view_objects.set_sec_str(file_name, sec_str.selections_as_ints())
        if show_ss_restraints and acp_selection is not None:
            bonds_table = secondary_structure.process_structure(
                params=None,
                processed_pdb_file=processed_pdb_file,
                tmp_dir=os.getcwd(),
                log=sys.stderr)
            a.view_objects.set_noncovalent_bonds(file_name, bonds_table.bonds)
            a.view_objects.flag_show_noncovalent_bonds = True
            a.view_objects.set_model_base_color([1.0, 1.0, 1.0], file_name)
            a.view_objects.set_color_mode("element")
    a.view_objects.force_update(recenter=True)
    a.MainLoop()
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)
Exemple #13
0
def exercise_helix_bonding_pattern_with_insertions():
    alpha_h1_ends = iotbx.pdb.hierarchy.input(pdb_string="""\
ATOM      1  N   ALA     1       1.643  -2.366  -1.408  1.00  0.00           N
ATOM      2  CA  ALA     1       1.280  -3.608  -2.069  1.00  0.00           C
ATOM      3  C   ALA     1      -0.114  -3.466  -2.684  1.00  0.00           C
ATOM      4  O   ALA     1      -0.327  -3.827  -3.840  1.00  0.00           O
ATOM      5  CB  ALA     1       1.361  -4.762  -1.068  1.00  0.00           C
ATOM      6  N   ALA     1A     -1.028  -2.938  -1.882  1.00  0.00           N
ATOM      7  CA  ALA     1A     -2.395  -2.743  -2.332  1.00  0.00           C
ATOM      8  C   ALA     1A     -2.396  -1.855  -3.579  1.00  0.00           C
ATOM      9  O   ALA     1A     -3.059  -2.167  -4.567  1.00  0.00           O
ATOM     10  CB  ALA     1A     -3.228  -2.150  -1.194  1.00  0.00           C
ATOM     11  N   ALA     3      -1.646  -0.767  -3.491  1.00  0.00           N
ATOM     12  CA  ALA     3      -1.551   0.168  -4.599  1.00  0.00           C
ATOM     13  C   ALA     3      -1.044  -0.568  -5.841  1.00  0.00           C
ATOM     14  O   ALA     3      -1.601  -0.419  -6.927  1.00  0.00           O
ATOM     15  CB  ALA     3      -0.646   1.337  -4.205  1.00  0.00           C
ATOM     16  N   ALA     4       0.008  -1.348  -5.639  1.00  0.00           N
ATOM     17  CA  ALA     4       0.597  -2.109  -6.728  1.00  0.00           C
ATOM     18  C   ALA     4      -0.466  -3.023  -7.340  1.00  0.00           C
ATOM     19  O   ALA     4      -0.611  -3.085  -8.559  1.00  0.00           O
ATOM     20  CB  ALA     4       1.808  -2.887  -6.211  1.00  0.00           C
ATOM     21  N   ALA     5      -1.184  -3.711  -6.463  1.00  0.00           N
ATOM     22  CA  ALA     5      -2.231  -4.619  -6.901  1.00  0.00           C
ATOM     23  C   ALA     5      -3.253  -3.847  -7.737  1.00  0.00           C
ATOM     24  O   ALA     5      -3.647  -4.296  -8.813  1.00  0.00           O
ATOM     25  CB  ALA     5      -2.864  -5.294  -5.683  1.00  0.00           C
ATOM     26  N   ALA     6      -3.654  -2.699  -7.211  1.00  0.00           N
ATOM     27  CA  ALA     6      -4.623  -1.860  -7.896  1.00  0.00           C
ATOM     28  C   ALA     6      -4.090  -1.499  -9.284  1.00  0.00           C
ATOM     29  O   ALA     6      -4.809  -1.602 -10.276  1.00  0.00           O
ATOM     30  CB  ALA     6      -4.919  -0.623  -7.045  1.00  0.00           C
ATOM     31  N   ALA     7      -2.831  -1.084  -9.309  1.00  0.00           N
ATOM     32  CA  ALA     7      -2.192  -0.708 -10.559  1.00  0.00           C
ATOM     33  C   ALA     7      -2.243  -1.890 -11.529  1.00  0.00           C
ATOM     34  O   ALA     7      -2.600  -1.727 -12.695  1.00  0.00           O
ATOM     35  CB  ALA     7      -0.761  -0.243 -10.281  1.00  0.00           C
ATOM     36  N   ALA     8      -1.881  -3.055 -11.012  1.00  0.00           N
ATOM     37  CA  ALA     8      -1.882  -4.264 -11.817  1.00  0.00           C
ATOM     38  C   ALA     8      -3.285  -4.496 -12.382  1.00  0.00           C
ATOM     39  O   ALA     8      -3.442  -4.772 -13.570  1.00  0.00           O
ATOM     40  CB  ALA     8      -1.391  -5.441 -10.972  1.00  0.00           C
ATOM     41  N   ALA     9      -4.269  -4.376 -11.503  1.00  0.00           N
ATOM     42  CA  ALA     9      -5.653  -4.568 -11.898  1.00  0.00           C
ATOM     43  C   ALA     9      -6.000  -3.590 -13.022  1.00  0.00           C
ATOM     44  O   ALA     9      -6.590  -3.978 -14.029  1.00  0.00           O
ATOM     45  CB  ALA     9      -6.561  -4.400 -10.678  1.00  0.00           C
ATOM     46  N   ALA     9A     -5.617  -2.338 -12.812  1.00  0.00           N
ATOM     47  CA  ALA     9A     -5.879  -1.301 -13.795  1.00  0.00           C
ATOM     48  C   ALA     9A     -5.242  -1.695 -15.130  1.00  0.00           C
ATOM     49  O   ALA     9A     -5.880  -1.605 -16.177  1.00  0.00           O
ATOM     50  CB  ALA     9A     -5.358   0.040 -13.274  1.00  0.00           C
TER
""")

    alpha_h1_ac = iotbx.pdb.hierarchy.input(pdb_string="""\
ATOM      1  N  AALA     1       1.643  -2.366  -1.408  0.50  0.00           N
ATOM      2  CA AALA     1       1.280  -3.608  -2.069  0.50  0.00           C
ATOM      3  C  AALA     1      -0.114  -3.466  -2.684  0.50  0.00           C
ATOM      4  O  AALA     1      -0.327  -3.827  -3.840  0.50  0.00           O
ATOM      5  CB AALA     1       1.361  -4.762  -1.068  0.50  0.00           C
ATOM      6  N  BALA     1       1.743  -2.366  -1.408  0.50  0.00           N
ATOM      7  CA BALA     1       1.380  -3.608  -2.069  0.50  0.00           C
ATOM      8  C  BALA     1      -0.014  -3.466  -2.684  0.50  0.00           C
ATOM      9  O  BALA     1      -0.227  -3.827  -3.840  0.50  0.00           O
ATOM     10  CB BALA     1       1.461  -4.762  -1.068  0.50  0.00           C
ATOM     11  N   ALA     1A     -1.028  -2.938  -1.882  1.00  0.00           N
ATOM     12  CA  ALA     1A     -2.395  -2.743  -2.332  1.00  0.00           C
ATOM     13  C   ALA     1A     -2.396  -1.855  -3.579  1.00  0.00           C
ATOM     14  O   ALA     1A     -3.059  -2.167  -4.567  1.00  0.00           O
ATOM     15  CB  ALA     1A     -3.228  -2.150  -1.194  1.00  0.00           C
ATOM     16  N   ALA     3      -1.646  -0.767  -3.491  1.00  0.00           N
ATOM     17  CA  ALA     3      -1.551   0.168  -4.599  1.00  0.00           C
ATOM     18  C   ALA     3      -1.044  -0.568  -5.841  1.00  0.00           C
ATOM     19  O   ALA     3      -1.601  -0.419  -6.927  1.00  0.00           O
ATOM     20  CB  ALA     3      -0.646   1.337  -4.205  1.00  0.00           C
ATOM     21  N   ALA     4       0.008  -1.348  -5.639  1.00  0.00           N
ATOM     22  CA  ALA     4       0.597  -2.109  -6.728  1.00  0.00           C
ATOM     23  C   ALA     4      -0.466  -3.023  -7.340  1.00  0.00           C
ATOM     24  O   ALA     4      -0.611  -3.085  -8.559  1.00  0.00           O
ATOM     25  CB  ALA     4       1.808  -2.887  -6.211  1.00  0.00           C
ATOM     26  N  AALA     5      -1.184  -3.711  -6.463  0.50  0.00           N
ATOM     27  CA AALA     5      -2.231  -4.619  -6.901  0.50  0.00           C
ATOM     28  C  AALA     5      -3.253  -3.847  -7.737  0.50  0.00           C
ATOM     29  O  AALA     5      -3.647  -4.296  -8.813  0.50  0.00           O
ATOM     30  CB AALA     5      -2.864  -5.294  -5.683  0.50  0.00           C
ATOM     31  N  BALA     5      -1.084  -3.711  -6.463  0.50  0.00           N
ATOM     32  CA BALA     5      -2.131  -4.619  -6.901  0.50  0.00           C
ATOM     33  C  BALA     5      -3.153  -3.847  -7.737  0.50  0.00           C
ATOM     34  O  BALA     5      -3.547  -4.296  -8.813  0.50  0.00           O
ATOM     35  CB BALA     5      -2.764  -5.294  -5.683  0.50  0.00           C
ATOM     36  N   ALA     6      -3.654  -2.699  -7.211  1.00  0.00           N
ATOM     37  CA  ALA     6      -4.623  -1.860  -7.896  1.00  0.00           C
ATOM     38  C   ALA     6      -4.090  -1.499  -9.284  1.00  0.00           C
ATOM     39  O   ALA     6      -4.809  -1.602 -10.276  1.00  0.00           O
ATOM     40  CB  ALA     6      -4.919  -0.623  -7.045  1.00  0.00           C
ATOM     41  N   ALA     7      -2.831  -1.084  -9.309  1.00  0.00           N
ATOM     42  CA  ALA     7      -2.192  -0.708 -10.559  1.00  0.00           C
ATOM     43  C   ALA     7      -2.243  -1.890 -11.529  1.00  0.00           C
ATOM     44  O   ALA     7      -2.600  -1.727 -12.695  1.00  0.00           O
ATOM     45  CB  ALA     7      -0.761  -0.243 -10.281  1.00  0.00           C
ATOM     46  N   ALA     8      -1.881  -3.055 -11.012  1.00  0.00           N
ATOM     47  CA  ALA     8      -1.882  -4.264 -11.817  1.00  0.00           C
ATOM     48  C   ALA     8      -3.285  -4.496 -12.382  1.00  0.00           C
ATOM     49  O   ALA     8      -3.442  -4.772 -13.570  1.00  0.00           O
ATOM     50  CB  ALA     8      -1.391  -5.441 -10.972  1.00  0.00           C
ATOM     51  N   ALA     9      -4.269  -4.376 -11.503  1.00  0.00           N
ATOM     52  CA  ALA     9      -5.653  -4.568 -11.898  1.00  0.00           C
ATOM     53  C   ALA     9      -6.000  -3.590 -13.022  1.00  0.00           C
ATOM     54  O   ALA     9      -6.590  -3.978 -14.029  1.00  0.00           O
ATOM     55  CB  ALA     9      -6.561  -4.400 -10.678  1.00  0.00           C
ATOM     56  N  AALA     9A     -5.617  -2.338 -12.812  0.50  0.00           N
ATOM     57  CA AALA     9A     -5.879  -1.301 -13.795  0.50  0.00           C
ATOM     58  C  AALA     9A     -5.242  -1.695 -15.130  0.50  0.00           C
ATOM     59  O  AALA     9A     -5.880  -1.605 -16.177  0.50  0.00           O
ATOM     60  CB AALA     9A     -5.358   0.040 -13.274  0.50  0.00           C
ATOM     62  N  BALA     9A     -5.517  -2.338 -12.812  0.50  0.00           N
ATOM     63  CA BALA     9A     -5.779  -1.301 -13.795  0.50  0.00           C
ATOM     64  C  BALA     9A     -5.142  -1.695 -15.130  0.50  0.00           C
ATOM     65  O  BALA     9A     -5.780  -1.605 -16.177  0.50  0.00           O
ATOM     66  CB BALA     9A     -5.258   0.040 -13.274  0.50  0.00           C
END
""")

    alpha_annot_1_1 = """\
HELIX    1   1 ALA      1  ALA      9A 1                                  10
"""
    alpha_annot_1_2 = """\
HELIX    1   1 ALA      1A ALA      9  1                                  10
"""
    alpha_annot_1_3 = """\
HELIX    1   1 ALA      1A ALA      9A 1                                  10
"""
    alpha_annot_1_4 = """\
HELIX    1   1 ALA      1  ALA      9  1                                  10
"""

    alpha_annot_2 = """\
HELIX    1   1 ALA      2  ALA      5  1                                  10
"""

    log = null_out()
    n_hbonds = []
    for pdb_inp, recs in [
        (alpha_h1_ends, alpha_annot_1_1),  #6
        (alpha_h1_ends, alpha_annot_1_2),  #4
        (alpha_h1_ends, alpha_annot_1_3),  #5
        (alpha_h1_ends, alpha_annot_1_4),  #5
        (alpha_h1_ac, alpha_annot_1_1),  #9
        (alpha_h1_ac, alpha_annot_1_2),  #5
        (alpha_h1_ac, alpha_annot_1_3),  #7
        (alpha_h1_ac, alpha_annot_1_4),  #7
    ]:
        ioss_annotation = ioss.annotation.from_records(
            records=recs.split('\n'))
        ann = ioss_annotation.as_restraint_groups(
            prefix_scope="secondary_structure")
        defpars = iotbx.phil.parse(sec_str_master_phil_str)
        custom_pars = defpars.fetch(iotbx.phil.parse(ann))
        custom_pars_ex = custom_pars.extract()
        ss_manager = manager(pdb_inp.hierarchy,
                             sec_str_from_pdb_file=None,
                             params=custom_pars_ex.secondary_structure,
                             verbose=-1)
        proxies_for_grm = ss_manager.create_protein_hbond_proxies(
            annotation=None, log=log)
        n_hbonds.append(proxies_for_grm.size())
    print n_hbonds
    assert n_hbonds == [6, 4, 5, 5, 9, 5, 7, 7]
Exemple #14
0
def exercise_sheets_bonding_pattern_with_insertions():
    pdb_apar_input = iotbx.pdb.hierarchy.input(pdb_string="""\
SCRYST1   46.460   46.460  193.210  90.00  90.00 120.00 P 31 2 1
SCALE1      0.021524  0.012427  0.000000        0.00000
SCALE2      0.000000  0.024854  0.000000        0.00000
SCALE3      0.000000  0.000000  0.005176        0.00000
ATOM      5  N   TYR A   2      27.208 -20.701   0.590  1.00  7.29           N
ATOM      6  CA  TYR A   2      27.617 -19.424   0.052  1.00  7.96           C
ATOM      7  C   TYR A   2      26.483 -18.436   0.263  1.00  6.87           C
ATOM      8  O   TYR A   2      25.303 -18.771   0.249  1.00  6.97           O
ATOM      9  CB  TYR A   2      27.861 -19.541  -1.451  1.00  7.90           C
ATOM     10  CG  TYR A   2      28.902 -20.556  -1.857  1.00  9.09           C
ATOM     11  CD1 TYR A   2      30.255 -20.336  -1.592  1.00 11.43           C
ATOM     12  CD2 TYR A   2      28.566 -21.697  -2.545  1.00 10.59           C
ATOM     13  CE1 TYR A   2      31.227 -21.244  -1.987  1.00 13.93           C
ATOM     14  CE2 TYR A   2      29.518 -22.630  -2.915  1.00 11.76           C
ATOM     15  CZ  TYR A   2      30.847 -22.395  -2.659  1.00 13.67           C
ATOM     16  OH  TYR A   2      31.792 -23.309  -3.059  1.00 18.26           O
ATOM     17  N   SER A   2A     26.854 -17.177   0.412  1.00  6.66           N
ATOM     18  CA  SER A   2A     25.899 -16.083   0.383  0.53  6.83           C
ATOM     20  C   SER A   2A     26.500 -14.946  -0.440  1.00  5.61           C
ATOM     21  O   SER A   2A     27.729 -14.822  -0.565  1.00  8.14           O
ATOM     22  CB  SER A   2A     25.569 -15.634   1.795  0.53  7.38           C
ATOM     24  OG  SER A   2A     26.740 -15.136   2.390  0.53  9.79           O
ATOM     26  N   CYS A   2B     25.627 -14.135  -0.995  1.00  5.11           N
ATOM     27  CA  CYS A   2B     26.070 -13.062  -1.865  1.00  5.65           C
ATOM     28  C   CYS A   2B     25.043 -11.934  -1.798  1.00  4.54           C
ATOM     29  O   CYS A   2B     23.856 -12.180  -1.528  1.00  5.60           O
ATOM     30  CB  CYS A   2B     26.253 -13.557  -3.295  1.00  7.00           C
ATOM     31  SG  CYS A   2B     24.806 -14.269  -4.119  1.00  8.88           S
ATOM     32  N   ARG A   5      25.486 -10.691  -2.002  1.00  4.69           N
ATOM     33  CA  ARG A   5      24.558  -9.545  -2.064  1.00  4.87           C
ATOM     34  C   ARG A   5      25.196  -8.395  -2.796  1.00  4.57           C
ATOM     35  O   ARG A   5      26.416  -8.238  -2.793  1.00  5.50           O
ATOM     36  CB  ARG A   5      24.061  -9.108  -0.700  1.00  6.43           C
ATOM     37  CG  ARG A   5      25.121  -8.566   0.219  1.00  6.96           C
ATOM     38  CD  ARG A   5      24.461  -8.032   1.494  1.00  7.25           C
ATOM     39  NE  ARG A   5      25.452  -7.547   2.440  1.00  7.63           N
ATOM     40  CZ  ARG A   5      26.107  -8.341   3.280  1.00  9.10           C
ATOM     41  NH1 ARG A   5      25.867  -9.642   3.297  1.00  9.68           N
ATOM     42  NH2 ARG A   5      26.974  -7.836   4.146  1.00 10.30           N
ATOM     43  N   ALA A   6      24.325  -7.563  -3.358  1.00  4.39           N
ATOM     44  CA  ALA A   6      24.723  -6.362  -4.067  1.00  4.73           C
ATOM     45  C   ALA A   6      23.693  -5.275  -3.769  1.00  4.15           C
ATOM     46  O   ALA A   6      22.482  -5.458  -3.987  1.00  4.96           O
ATOM     47  CB  ALA A   6      24.831  -6.626  -5.558  1.00  5.96           C
ATOM     48  N   VAL A   7      24.165  -4.139  -3.284  1.00  4.97           N
ATOM     49  CA  VAL A   7      23.374  -2.917  -3.085  1.00  4.42           C
ATOM     50  C   VAL A   7      23.482  -2.046  -4.325  1.00  4.45           C
ATOM     51  O   VAL A   7      24.589  -1.663  -4.717  1.00  5.55           O
ATOM     52  CB  VAL A   7      23.830  -2.159  -1.806  1.00  5.09           C
ATOM     53  CG1 VAL A   7      23.111  -0.842  -1.686  1.00  5.65           C
ATOM     54  CG2 VAL A   7      23.612  -2.998  -0.570  1.00  6.88           C
ATOM    204  N   MET A  31      18.177  -3.966  -4.656  1.00  4.72           N
ATOM    205  CA  MET A  31      18.833  -4.887  -3.744  1.00  5.29           C
ATOM    206  C   MET A  31      18.765  -6.294  -4.322  1.00  4.60           C
ATOM    207  O   MET A  31      17.661  -6.738  -4.657  1.00  4.98           O
ATOM    208  CB  MET A  31      18.097  -4.868  -2.387  1.00  6.39           C
ATOM    209  CG  MET A  31      18.723  -5.755  -1.334  1.00  8.61           C
ATOM    210  SD  MET A  31      20.248  -5.074  -0.655  1.00 11.04           S
ATOM    211  CE  MET A  31      21.358  -6.427  -0.777  1.00  8.94           C
ATOM    212  N   ALA A  32      19.899  -6.986  -4.412  1.00  3.90           N
ATOM    213  CA  ALA A  32      19.934  -8.380  -4.864  1.00  3.55           C
ATOM    214  C   ALA A  32      20.720  -9.194  -3.858  1.00  3.90           C
ATOM    215  O   ALA A  32      21.762  -8.763  -3.393  1.00  5.01           O
ATOM    216  CB  ALA A  32      20.552  -8.500  -6.255  1.00  4.55           C
ATOM    217  N   SER A  33      20.230 -10.407  -3.567  1.00  4.02           N
ATOM    218  CA  SER A  33      20.980 -11.288  -2.645  1.00  3.79           C
ATOM    219  C   SER A  33      20.591 -12.727  -2.850  1.00  4.33           C
ATOM    220  O   SER A  33      19.532 -13.045  -3.412  1.00  4.84           O
ATOM    221  CB  SER A  33      20.830 -10.880  -1.167  1.00  4.55           C
ATOM    222  OG  SER A  33      19.498 -11.105  -0.710  1.00  5.25           O
ATOM    223  N   GLY A  34      21.415 -13.600  -2.283  1.00  4.96           N
ATOM    224  CA  GLY A  34      21.104 -14.997  -2.335  1.00  4.50           C
ATOM    225  C   GLY A  34      21.914 -15.837  -1.397  1.00  4.64           C
ATOM    226  O   GLY A  34      22.836 -15.343  -0.732  1.00  4.96           O
ATOM    227  N   THR A  35      21.521 -17.105  -1.329  1.00  4.35           N
ATOM    228  CA  THR A  35      22.277 -18.138  -0.654  1.00  4.02           C
ATOM    229  C   THR A  35      22.226 -19.392  -1.495  1.00  4.55           C
ATOM    230  O   THR A  35      21.221 -19.652  -2.170  1.00  4.35           O
ATOM    231  CB  THR A  35      21.715 -18.436   0.762  1.00  5.12           C
ATOM    232  OG1 THR A  35      20.356 -18.929   0.668  1.00  5.51           O
ATOM    233  CG2 THR A  35      21.733 -17.222   1.670  1.00  5.97           C
ATOM    234  N   SER A  35A     23.294 -20.178  -1.426  1.00  4.63           N
ATOM    235  CA  SER A  35A     23.402 -21.406  -2.221  1.00  4.58           C
ATOM    236  C   SER A  35A     24.387 -22.368  -1.553  1.00  5.05           C
ATOM    237  O   SER A  35A     24.929 -22.095  -0.497  1.00  6.22           O
ATOM    238  CB  SER A  35A     23.881 -21.071  -3.639  1.00  5.69           C
ATOM    239  OG  SER A  35A     25.213 -20.561  -3.633  1.00  7.12           O
""")

    pdb_par_input = iotbx.pdb.hierarchy.input(pdb_string="""\
CRYST1   46.460   46.460  193.210  90.00  90.00 120.00 P 31 2 1
SCALE1      0.021524  0.012427  0.000000        0.00000
SCALE2      0.000000  0.024854  0.000000        0.00000
SCALE3      0.000000  0.000000  0.005176        0.00000
ATOM     67  N   ALA A  15       5.011  -5.031  -8.967  1.00  5.73           N
ATOM     68  CA  ALA A  15       4.943  -6.455  -9.287  1.00  6.16           C
ATOM     69  C   ALA A  15       5.610  -7.252  -8.166  1.00  6.39           C
ATOM     70  O   ALA A  15       6.751  -7.007  -7.857  1.00  9.87           O
ATOM     71  CB  ALA A  15       5.684  -6.739 -10.604  1.00  7.46           C
ATOM     72  N   THR A  15A      4.929  -8.263  -7.636  1.00  5.49           N
ATOM     73  C   THR A  15A      5.316 -10.600  -7.084  1.00  5.07           C
ATOM     74  O   THR A  15A      4.214 -11.002  -7.422  1.00  6.51           O
ATOM     75  CA  THR A  15A      5.513  -9.172  -6.657  1.00  5.70           C
ATOM     76  CB  THR A  15A      4.864  -9.001  -5.276  1.00  8.31           C
ATOM     77  N   GLY A  17       6.393 -11.375  -7.067  1.00  4.56           N
ATOM     78  CA  GLY A  17       6.325 -12.770  -7.439  1.00  4.26           C
ATOM     79  C   GLY A  17       7.219 -13.654  -6.621  1.00  4.41           C
ATOM     80  O   GLY A  17       8.263 -13.233  -6.114  1.00  5.01           O
ATOM     81  N   SER A  18       6.827 -14.921  -6.561  1.00  4.24           N
ATOM     82  CA  SER A  18       7.657 -15.945  -5.959  1.00  4.02           C
ATOM     83  C   SER A  18       7.539 -17.244  -6.724  1.00  3.64           C
ATOM     84  O   SER A  18       6.482 -17.526  -7.331  1.00  4.07           O
ATOM     85  CB  SER A  18       7.335 -16.157  -4.481  1.00  5.86           C
ATOM     86  N   ALA A  19       8.573 -18.049  -6.627  1.00  3.19           N
ATOM     87  CA  ALA A  19       8.578 -19.414  -7.195  1.00  3.31           C
ATOM     88  C   ALA A  19       9.370 -20.307  -6.238  1.00  3.20           C
ATOM     89  O   ALA A  19      10.484 -19.956  -5.817  1.00  4.21           O
ATOM     90  CB  ALA A  19       9.235 -19.415  -8.574  1.00  3.85           C
ATOM     91  N   THR A  20       8.825 -21.476  -5.940  1.00  3.77           N
ATOM     92  CA  THR A  20       9.478 -22.432  -5.047  1.00  3.70           C
ATOM     93  C   THR A  20       9.444 -23.827  -5.640  1.00  3.56           C
ATOM     94  O   THR A  20       8.383 -24.281  -6.108  1.00  4.14           O
ATOM     95  CB  THR A  20       8.787 -22.430  -3.673  1.00  4.76           C
ATOM     96  N   THR A  20A     10.560 -24.542  -5.569  1.00  4.00           N
ATOM     97  CA  THR A  20A     10.597 -25.962  -5.876  1.00  4.05           C
ATOM     98  C   THR A  20A     10.984 -26.770  -4.636  1.00  4.53           C
ATOM     99  O   THR A  20A     11.770 -26.361  -3.802  1.00  5.04           O
ATOM    100  CB  THR A  20A     11.488 -26.293  -7.083  1.00  4.38           C
ATOM    189  N   GLN A  40       0.280  -6.099  -9.049  1.00  6.35           N
ATOM    190  CA  GLN A  40       0.087  -7.454  -9.580  1.00  6.35           C
ATOM    191  C   GLN A  40       0.964  -8.417  -8.788  1.00  6.09           C
ATOM    192  O   GLN A  40       2.080  -8.093  -8.393  1.00  6.87           O
ATOM    193  CB  GLN A  40       0.461  -7.523 -11.060  1.00  7.52           C
ATOM    194  N   THR A  41       0.419  -9.596  -8.544  1.00  6.66           N
ATOM    195  CA  THR A  41       1.108 -10.640  -7.800  1.00  6.93           C
ATOM    196  C   THR A  41       0.932 -12.005  -8.414  1.00  6.82           C
ATOM    197  O   THR A  41      -0.069 -12.258  -9.104  1.00  8.79           O
ATOM    198  CB  THR A  41       0.633 -10.636  -6.352  1.00 10.84           C
ATOM    199  N   ALA A  42       1.951 -12.847  -8.263  1.00  6.44           N
ATOM    200  CA  ALA A  42       1.923 -14.209  -8.797  1.00  6.59           C
ATOM    201  C   ALA A  42       2.829 -15.117  -7.992  1.00  5.51           C
ATOM    202  O   ALA A  42       3.835 -14.684  -7.420  1.00  5.94           O
ATOM    203  CB  ALA A  42       2.327 -14.218 -10.264  1.00  9.02           C
ATOM    204  N   LYS A  42A      2.479 -16.398  -7.978  1.00  6.26           N
ATOM    205  CA  LYS A  42A      3.247 -17.395  -7.256  1.00  6.48           C
ATOM    206  C   LYS A  42A      3.186 -18.741  -7.955  1.00  5.78           C
ATOM    207  O   LYS A  42A      2.206 -19.041  -8.623  1.00  9.40           O
ATOM    208  CB  LYS A  42A      2.727 -17.535  -5.836  1.00  8.81           C
ATOM    209  N   SER A  44       4.243 -19.534  -7.818  1.00  4.43           N
ATOM    210  CA  SER A  44       4.241 -20.890  -8.325  1.00  4.28           C
ATOM    211  C   SER A  44       4.998 -21.811  -7.358  1.00  4.09           C
ATOM    212  O   SER A  44       5.865 -21.377  -6.584  1.00  4.53           O
ATOM    213  CB  SER A  44       4.831 -20.949  -9.731  1.00  5.33           C
ATOM    214  N   PHE A  45       4.660 -23.091  -7.444  1.00  4.39           N
ATOM    215  CA  PHE A  45       5.198 -24.135  -6.576  1.00  4.44           C
ATOM    216  C   PHE A  45       5.222 -25.415  -7.389  1.00  4.16           C
ATOM    217  O   PHE A  45       4.183 -25.754  -7.979  1.00  5.11           O
ATOM    218  CB  PHE A  45       4.254 -24.281  -5.370  1.00  5.22           C
ATOM    219  N   ALA A  45A      6.347 -26.119  -7.403  1.00  3.62           N
ATOM    220  CA  ALA A  45A      6.443 -27.338  -8.202  1.00  3.99           C
ATOM    221  C   ALA A  45A      7.579 -28.205  -7.717  1.00  4.57           C
ATOM    222  O   ALA A  45A      8.479 -27.750  -7.000  1.00  4.79           O
ATOM    223  CB  ALA A  45A      6.607 -27.026  -9.678  1.00  4.52           C
TER
END
""")

    pdb_par_ac_input = iotbx.pdb.hierarchy.input(pdb_string="""\
CRYST1   46.460   46.460  193.210  90.00  90.00 120.00 P 31 2 1
SCALE1      0.021524  0.012427  0.000000        0.00000
SCALE2      0.000000  0.024854  0.000000        0.00000
SCALE3      0.000000  0.000000  0.005176        0.00000
ATOM      1  N  AALA A  15       5.011  -5.031  -8.967  0.50  5.73           N
ATOM      2  CA AALA A  15       4.943  -6.455  -9.287  0.50  6.16           C
ATOM      3  C  AALA A  15       5.610  -7.252  -8.166  0.50  6.39           C
ATOM      4  O  AALA A  15       6.751  -7.007  -7.857  0.50  9.87           O
ATOM      5  CB AALA A  15       5.684  -6.739 -10.604  0.50  7.46           C
ATOM      6  N  BALA A  15       5.111  -5.031  -8.967  0.50  5.73           N
ATOM      7  CA BALA A  15       5.043  -6.455  -9.287  0.50  6.16           C
ATOM      8  C  BALA A  15       5.710  -7.252  -8.166  0.50  6.39           C
ATOM      9  O  BALA A  15       6.851  -7.007  -7.857  0.50  9.87           O
ATOM     10  CB BALA A  15       5.784  -6.739 -10.604  0.50  7.46           C
ATOM     11  N  ATHR A  15A      4.929  -8.263  -7.636  0.50  5.49           N
ATOM     12  C  ATHR A  15A      5.316 -10.600  -7.084  0.50  5.07           C
ATOM     13  O  ATHR A  15A      4.214 -11.002  -7.422  0.50  6.51           O
ATOM     14  CA ATHR A  15A      5.513  -9.172  -6.657  0.50  5.70           C
ATOM     15  CB ATHR A  15A      4.864  -9.001  -5.276  0.50  8.31           C
ATOM     16  N  BTHR A  15A      5.029  -8.263  -7.636  0.50  5.49           N
ATOM     17  C  BTHR A  15A      5.416 -10.600  -7.084  0.50  5.07           C
ATOM     18  O  BTHR A  15A      4.314 -11.002  -7.422  0.50  6.51           O
ATOM     19  CA BTHR A  15A      5.613  -9.172  -6.657  0.50  5.70           C
ATOM     20  CB BTHR A  15A      4.964  -9.001  -5.276  0.50  8.31           C
ATOM     21  N   GLY A  17       6.393 -11.375  -7.067  1.00  4.56           N
ATOM     22  CA  GLY A  17       6.325 -12.770  -7.439  1.00  4.26           C
ATOM     23  C   GLY A  17       7.219 -13.654  -6.621  1.00  4.41           C
ATOM     24  O   GLY A  17       8.263 -13.233  -6.114  1.00  5.01           O
ATOM     25  N   SER A  18       6.827 -14.921  -6.561  1.00  4.24           N
ATOM     26  CA  SER A  18       7.657 -15.945  -5.959  1.00  4.02           C
ATOM     27  C   SER A  18       7.539 -17.244  -6.724  1.00  3.64           C
ATOM     28  O   SER A  18       6.482 -17.526  -7.331  1.00  4.07           O
ATOM     29  CB  SER A  18       7.335 -16.157  -4.481  1.00  5.86           C
ATOM     30  N   ALA A  19       8.573 -18.049  -6.627  1.00  3.19           N
ATOM     31  CA  ALA A  19       8.578 -19.414  -7.195  1.00  3.31           C
ATOM     32  C   ALA A  19       9.370 -20.307  -6.238  1.00  3.20           C
ATOM     33  O   ALA A  19      10.484 -19.956  -5.817  1.00  4.21           O
ATOM     34  CB  ALA A  19       9.235 -19.415  -8.574  1.00  3.85           C
ATOM     35  N   THR A  20       8.825 -21.476  -5.940  1.00  3.77           N
ATOM     36  CA  THR A  20       9.478 -22.432  -5.047  1.00  3.70           C
ATOM     37  C   THR A  20       9.444 -23.827  -5.640  1.00  3.56           C
ATOM     38  O   THR A  20       8.383 -24.281  -6.108  1.00  4.14           O
ATOM     39  CB  THR A  20       8.787 -22.430  -3.673  1.00  4.76           C
ATOM     40  N   THR A  21      10.560 -24.542  -5.569  1.00  4.00           N
ATOM     41  CA  THR A  21      10.597 -25.962  -5.876  1.00  4.05           C
ATOM     42  C   THR A  21      10.984 -26.770  -4.636  1.00  4.53           C
ATOM     43  O   THR A  21      11.770 -26.361  -3.802  1.00  5.04           O
ATOM     44  CB  THR A  21      11.488 -26.293  -7.083  1.00  4.38           C
ATOM     45  N   GLN A  40       0.280  -6.099  -9.049  1.00  6.35           N
ATOM     46  CA  GLN A  40       0.087  -7.454  -9.580  1.00  6.35           C
ATOM     47  C   GLN A  40       0.964  -8.417  -8.788  1.00  6.09           C
ATOM     48  O   GLN A  40       2.080  -8.093  -8.393  1.00  6.87           O
ATOM     49  CB  GLN A  40       0.461  -7.523 -11.060  1.00  7.52           C
ATOM     50  N   THR A  41       0.419  -9.596  -8.544  1.00  6.66           N
ATOM     51  CA  THR A  41       1.108 -10.640  -7.800  1.00  6.93           C
ATOM     52  C   THR A  41       0.932 -12.005  -8.414  1.00  6.82           C
ATOM     53  O   THR A  41      -0.069 -12.258  -9.104  1.00  8.79           O
ATOM     54  CB  THR A  41       0.633 -10.636  -6.352  1.00 10.84           C
ATOM     55  N   ALA A  42       1.951 -12.847  -8.263  1.00  6.44           N
ATOM     56  CA  ALA A  42       1.923 -14.209  -8.797  1.00  6.59           C
ATOM     57  C   ALA A  42       2.829 -15.117  -7.992  1.00  5.51           C
ATOM     58  O   ALA A  42       3.835 -14.684  -7.420  1.00  5.94           O
ATOM     59  CB  ALA A  42       2.327 -14.218 -10.264  1.00  9.02           C
ATOM     60  N   LYS A  42A      2.479 -16.398  -7.978  1.00  6.26           N
ATOM     61  CA  LYS A  42A      3.247 -17.395  -7.256  1.00  6.48           C
ATOM     62  C   LYS A  42A      3.186 -18.741  -7.955  1.00  5.78           C
ATOM     63  O   LYS A  42A      2.206 -19.041  -8.623  1.00  9.40           O
ATOM     64  CB  LYS A  42A      2.727 -17.535  -5.836  1.00  8.81           C
ATOM     65  N   SER A  44       4.243 -19.534  -7.818  1.00  4.43           N
ATOM     66  CA  SER A  44       4.241 -20.890  -8.325  1.00  4.28           C
ATOM     67  C   SER A  44       4.998 -21.811  -7.358  1.00  4.09           C
ATOM     68  O   SER A  44       5.865 -21.377  -6.584  1.00  4.53           O
ATOM     69  CB  SER A  44       4.831 -20.949  -9.731  1.00  5.33           C
ATOM     70  N   PHE A  45       4.660 -23.091  -7.444  1.00  4.39           N
ATOM     71  CA  PHE A  45       5.198 -24.135  -6.576  1.00  4.44           C
ATOM     72  C   PHE A  45       5.222 -25.415  -7.389  1.00  4.16           C
ATOM     73  O   PHE A  45       4.183 -25.754  -7.979  1.00  5.11           O
ATOM     74  CB  PHE A  45       4.254 -24.281  -5.370  1.00  5.22           C
ATOM     75  N   ALA A  46       6.347 -26.119  -7.403  1.00  3.62           N
ATOM     76  CA  ALA A  46       6.443 -27.338  -8.202  1.00  3.99           C
ATOM     77  C   ALA A  46       7.579 -28.205  -7.717  1.00  4.57           C
ATOM     78  O   ALA A  46       8.479 -27.750  -7.000  1.00  4.79           O
ATOM     79  CB  ALA A  46       6.607 -27.026  -9.678  1.00  4.52           C
TER      80      ALA A  46
END
""")

    s_apar_records1 = """\
SHEET    1   A 2 TYR A   2  VAL A   7  0
SHEET    2   A 2 MET A  31  SER A  35A-1  O  ALA A  35A  N  ALA A   2
"""
    s_apar_records2 = """\
SHEET    1   A 2 TYR A   2  VAL A   7  0
SHEET    2   A 2 MET A  31  SER A  35A-1  O  ALA A  32   N  ALA A   6
"""
    s_apar_records3 = """\
SHEET    1   A 2 SER A   2A VAL A   7  0
SHEET    2   A 2 MET A  31  SER A  35A-1  O  ALA A  32   N  ALA A   6
"""

    s_par_records1 = """\
SHEET    1   B 2 ALA A  15  THR A  20A 0
SHEET    2   B 2 GLN A  40  ALA A  45A 1  O  GLN A  40   N  THR A  15A
"""
    s_par_records2 = """\
SHEET    1   B 2 ALA A  15  THR A  20A 0
SHEET    2   B 2 GLN A  40  ALA A  45A 1  O  GLN A  44   N  THR A  20
"""
    s_par_records3 = """\
SHEET    1   B 2 ALA A  15A THR A  20A 0
SHEET    2   B 2 GLN A  40  ALA A  45  1  O  GLN A  44   N  THR A  20
"""

    log = null_out()
    # defpars = sec_str_master_phil
    n_hbonds = []
    for pdb_inp, recs in [
        (pdb_apar_input, s_apar_records1),  # 6
        (pdb_apar_input, s_apar_records2),  # 6
        (pdb_apar_input, s_apar_records3),  # 4
        (pdb_par_input, s_par_records1),  # 6
        (pdb_par_input, s_par_records2),  # 6
        (pdb_par_input, s_par_records3),  # 5
        (pdb_par_ac_input, s_par_records1),  # 8 hbonds
    ]:
        ioss_annotation = ioss.annotation.from_records(
            records=recs.split('\n'))
        ann = ioss_annotation.as_restraint_groups(
            prefix_scope="secondary_structure")
        defpars = iotbx.phil.parse(sec_str_master_phil_str)
        custom_pars = defpars.fetch(iotbx.phil.parse(ann))
        custom_pars_ex = custom_pars.extract()
        ss_manager = manager(pdb_inp.hierarchy,
                             sec_str_from_pdb_file=None,
                             params=custom_pars_ex.secondary_structure,
                             verbose=-1)
        proxies_for_grm = ss_manager.create_protein_hbond_proxies(
            annotation=None, log=log)
        # print proxies_for_grm.size()
        n_hbonds.append(proxies_for_grm.size())
    assert n_hbonds == [6, 6, 4, 6, 6, 5, 8]
  def get_reference_dihedral_proxies(self, processed_pdb_file):
    complete_dihedral_proxies = utils.get_dihedrals_and_phi_psi(
        processed_pdb_file=processed_pdb_file)
    generated_reference_dihedral_proxies = \
      cctbx.geometry_restraints.shared_dihedral_proxy()
    sigma = self.params.sigma
    limit = self.params.limit
    ref_ss_m = None
    ss_selection = None
    if self.params.secondary_structure_only:
      if (not libtbx.env.has_module(name="ksdssp")):
        raise RuntimeError(
          "ksdssp module is not configured, "+\
          "cannot generate secondary structure reference")
      ref_ss_m = {}
      ss_selection = {}
      for file in self.reference_file_list:
        ref_ss_m[file] = secondary_structure.manager(
          pdb_hierarchy=self.pdb_hierarchy_ref[file],
          sec_str_from_pdb_file=None)
        sec_str_from_pdb_file = ref_ss_m[file].actual_sec_str
        if sec_str_from_pdb_file != None:
          overall_selection = sec_str_from_pdb_file.overall_selection()
          sel_cache_ref = self.pdb_hierarchy_ref[file].atom_selection_cache()
          bsel = sel_cache_ref.selection(string=overall_selection)
          if bsel.all_eq(False):
            raise Sorry("No atom selected")
          ss_selection[file] = bsel

    t_sum = 0
    for dp in complete_dihedral_proxies:
      key_work = ""
      complete = True
      for i_seq in dp.i_seqs:
        if not self.selection[i_seq]:
          complete = False
      if not complete:
        continue

      for i_seq in dp.i_seqs:
        key_work = key_work + self.i_seq_name_hash[i_seq]
      #find matching key
      key = None
      file_match = None
      for file in self.reference_file_list:
        if key is not None or file not in self.match_map:
          continue
        else:
          key = ""
          ref_match = True
          for i_seq in dp.i_seqs:
            if ref_match:
              map_part = self.match_map[file].get(i_seq)
              if map_part is not None:
                key_part = self.i_seq_name_hash_ref[file].get(map_part)
                if key_part is None:
                  ref_match = False
                  key = None
                  file_match = None
                else:
                  key = key+key_part
              else:
                ref_match = False
                key = None
                file_match = None
            if key is not None:
              file_match = file
      try:
        reference_angle = self.reference_dihedral_hash[file_match][key]
      except Exception:
        continue
      w_limit = limit
      w_weight = 1/sigma**2
      if (self.params.secondary_structure_only and ss_selection is not None
          and ss_selection[file_match] is not None):
        limit2 = 15.0
        w_weight = 0.04
        if (ss_selection[file_match][self.match_map[file_match][dp.i_seqs[0]]] and
            ss_selection[file_match][self.match_map[file_match][dp.i_seqs[1]]] and
            ss_selection[file_match][self.match_map[file_match][dp.i_seqs[2]]] and
            ss_selection[file_match][self.match_map[file_match][dp.i_seqs[3]]]):
          limit2 = 30.0
          w_weight = 1
        dp_add = cctbx.geometry_restraints.dihedral_proxy(
            i_seqs=dp.i_seqs,
            angle_ideal=reference_angle,
            weight=w_weight,
            limit=w_limit,
            top_out=TOP_OUT_FLAG)
        generated_reference_dihedral_proxies.append(dp_add)
      else:
        dp_add = cctbx.geometry_restraints.dihedral_proxy(
            i_seqs=dp.i_seqs,
            angle_ideal=reference_angle,
            weight=1/sigma**2,
            limit=limit,
            top_out=TOP_OUT_FLAG)
        # print "Already_there:", self._is_proxy_already_present(generated_reference_dihedral_proxies,dp_add)
        # if not self._is_proxy_already_present(generated_reference_dihedral_proxies,dp_add):
        generated_reference_dihedral_proxies.append(dp_add)
    self.reference_dihedral_proxies = generated_reference_dihedral_proxies
Exemple #16
0
def substitute_ss(
                    model, # changed in place
                    params = None,
                    use_plane_peptide_bond_restr=True,
                    fix_rotamer_outliers=True,
                    log=null_out(),
                    check_rotamer_clashes=True,
                    reference_map=None,
                    verbose=False):
  """
  Substitute secondary structure elements in real_h hierarchy with ideal
  ones _in_place_.
  Returns reference torsion proxies - the only thing that cannot be restored
  with little effort outside the procedure.
  real_h - hierarcy to substitute secondary structure elements.
  xray_structure - xray_structure - needed to get crystal symmetry (to
      construct processed_pdb_file and xray_structure is needed to call
      get_geometry_restraints_manager for no obvious reason).
  ss_annotation - iotbx.pdb.annotation object.
  """
  import mmtbx.utils

  ss_annotation = model.get_ss_annotation()

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


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

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

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

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

  log.write(" Done\n")
  log.flush()
  t11 = time()
  # print >> log, "Initial checking, init   : %.4f" % (t1-t0)
  # print >> log, "Checking SS              : %.4f" % (t2-t1)
  # print >> log, "Initializing selections  : %.4f" % (t4-t3)
  # print >> log, "Looping for selections   : %.4f" % (t5-t4)
  # print >> log, "Finalizing selections    : %.4f" % (t6-t5)
  # print >> log, "PDB interpretation       : %.4f" % (t7-t6)
  # print >> log, "Get GRM                  : %.4f" % (t8-t7)
  # print >> log, "Adding restraints to GRM : %.4f" % (t9-t8)
  # print >> log, "Running GM               : %.4f" % (t11-t10)
  # print_hbond_proxies(grm.geometry,real_h)
  grm.geometry.remove_reference_coordinate_restraints_in_place()
  return grm.geometry.get_chi_torsion_proxies()
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) :
  # params keyword is for running program from GUI dialog
  if ( ((len(args) == 0) and (params is None)) or
       ((len(args) > 0) and ((args[0] == "-h") or (args[0] == "--help"))) ):
    show_usage()
    return

  # parse command-line arguments
  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

  work_params.secondary_structure.enabled=True
  assert work_params.format in ["phenix", "phenix_refine", "phenix_bonds",
      "pymol", "refmac", "kinemage", "pdb"]
  if work_params.quiet :
    out = cStringIO.StringIO()

  pdb_combined = iotbx.pdb.combine_unique_pdb_files(file_names=pdb_files)
  pdb_structure = iotbx.pdb.input(source_info=None,
    lines=flex.std_string(pdb_combined.raw_records))
  cs = pdb_structure.crystal_symmetry()

  corrupted_cs = False
  if cs is not None:
    if [cs.unit_cell(), cs.space_group()].count(None) > 0:
      corrupted_cs = True
      cs = None
    elif cs.unit_cell().volume() < 10:
      corrupted_cs = True
      cs = None

  if cs is None:
    if corrupted_cs:
      print >> out, "Symmetry information is corrupted, "
    else:
      print >> out, "Symmetry information was not found, "
    print >> out, "putting molecule in P1 box."
    from cctbx import uctbx
    atoms = pdb_structure.atoms()
    box = uctbx.non_crystallographic_unit_cell_with_the_sites_in_its_center(
      sites_cart=atoms.extract_xyz(),
      buffer_layer=3)
    atoms.set_xyz(new_xyz=box.sites_cart)
    cs = box.crystal_symmetry()
  from mmtbx.monomer_library import pdb_interpretation, server
  import mmtbx
  import mmtbx.command_line.geometry_minimization

  mon_lib_srv = server.server()
  ener_lib = server.ener_lib()
  defpars = mmtbx.command_line.geometry_minimization.master_params().extract()
  defpars.pdb_interpretation.automatic_linking.link_carbohydrates=False
  defpars.pdb_interpretation.c_beta_restraints=False
  defpars.pdb_interpretation.clash_guard.nonbonded_distance_threshold=None
  processed_pdb_file = pdb_interpretation.process(
    mon_lib_srv    = mon_lib_srv,
    ener_lib       = ener_lib,
    pdb_inp        = pdb_structure,
    crystal_symmetry = cs,
    params         = defpars.pdb_interpretation,
    force_symmetry = True)
  pdb_hierarchy = processed_pdb_file.all_chain_proxies.pdb_hierarchy
  geometry = processed_pdb_file.geometry_restraints_manager()
  geometry.pair_proxies(processed_pdb_file.xray_structure().sites_cart())
  pdb_hierarchy.atoms().reset_i_seq()
  if len(pdb_hierarchy.models()) != 1 :
    raise Sorry("Multiple models not supported.")
  ss_from_file = None
  if hasattr(pdb_structure, "extract_secondary_structure"):
    ss_from_file = pdb_structure.extract_secondary_structure()
  m = manager(pdb_hierarchy=pdb_hierarchy,
    geometry_restraints_manager=geometry,
    sec_str_from_pdb_file=ss_from_file,
    params=work_params.secondary_structure,
    verbose=work_params.verbose)

  # bp_p = nucleic_acids.get_basepair_plane_proxies(
  #     pdb_hierarchy,
  #     m.params.secondary_structure.nucleic_acid.base_pair,
  #     geometry)
  # st_p = nucleic_acids.get_stacking_proxies(
  #     pdb_hierarchy,
  #     m.params.secondary_structure.nucleic_acid.stacking_pair,
  #     geometry)
  # hb_b, hb_a = nucleic_acids.get_basepair_hbond_proxies(pdb_hierarchy,
  #     m.params.secondary_structure.nucleic_acid.base_pair)
  result_out = cStringIO.StringIO()
  # prefix_scope="refinement.pdb_interpretation"
  # prefix_scope=""
  prefix_scope=""
  if work_params.format == "phenix_refine":
    prefix_scope = "refinement.pdb_interpretation"
  elif work_params.format == "phenix":
    prefix_scope = "pdb_interpretation"
  ss_phil = None
  working_phil = m.as_phil_str(master_phil=sec_str_master_phil)
  phil_diff = sec_str_master_phil.fetch_diff(source=working_phil)

  if work_params.format in ["phenix", "phenix_refine"]:
    comment = "\n".join([
      "# These parameters are suitable for use in e.g. phenix.real_space_refine",
      "# or geometry_minimization. To use them in phenix.refine add ",
      "# 'refinement.' if front of pdb_interpretation."])
    if work_params.format == "phenix_refine":
      comment = "\n".join([
      "# These parameters are suitable for use in phenix.refine only.",
      "# To use them in other Phenix tools remove ",
      "# 'refinement.' if front of pdb_interpretation."])
    print >> result_out, comment
    if (prefix_scope != "") :
      print >> result_out, "%s {" % prefix_scope
    if work_params.show_all_params :
      working_phil.show(prefix="  ", out=result_out)
    else :
      phil_diff.show(prefix="  ", out=result_out)
    if (prefix_scope != "") :
      print >> result_out, "}"
  elif work_params.format == "pdb":
    print >> result_out, m.actual_sec_str.as_pdb_str()
  elif work_params.format == "phenix_bonds" :
    raise Sorry("Not yet implemented.")
  elif work_params.format in ["pymol", "refmac", "kinemage"] :
    m.show_summary(log=out)
    (hb_proxies, hb_angle_proxies, planarity_proxies,
        parallelity_proxies) = m.create_all_new_restraints(
        pdb_hierarchy=pdb_hierarchy,
        grm=geometry,
        log=out)
    if hb_proxies.size() > 0:
      if work_params.format == "pymol" :
        bonds_in_format = hb_proxies.as_pymol_dashes(
            pdb_hierarchy=pdb_hierarchy)
      elif work_params.format == "kinemage" :
        bonds_in_format = hb_proxies.as_kinemage(
            pdb_hierarchy=pdb_hierarchy)
      else :
        bonds_in_format = hb_proxies.as_refmac_restraints(
            pdb_hierarchy=pdb_hierarchy)
      print >> result_out, bonds_in_format
  result = result_out.getvalue()
  filename = "%s_ss.eff" %os.path.basename(work_params.file_name[0])
  outf = open(filename, "w")
  outf.write(result)
  outf.close()
  print >> out, result

  return os.path.abspath(filename)
Exemple #19
0
  def whole_minimization(self):
    t3 = time()
    # pre_result_h = edited_h
    # pre_result_h.reset_i_seq_if_necessary()
    bsel = flex.bool(self.model.get_number_of_atoms(), False)
    helix_selection = flex.bool(self.model.get_number_of_atoms(), False)
    sheet_selection = flex.bool(self.model.get_number_of_atoms(), False)
    other_selection = flex.bool(self.model.get_number_of_atoms(), False)
    ss_for_tors_selection = flex.bool(self.model.get_number_of_atoms(), False)
    nonss_for_tors_selection = flex.bool(self.model.get_number_of_atoms(), False)
    # set all CA atoms to True for other_selection
    #isel = self.model.get_atom_selection_cache().iselection("name ca")
    isel = self.model.get_atom_selection_cache().iselection("name ca or name n or name o or name c")
    other_selection.set_selected(isel, True)
    n_main_chain_atoms = other_selection.count(True)
    isel = self.model.get_atom_selection_cache().iselection("name ca or name n or name o or name c")
    nonss_for_tors_selection.set_selected(isel, True)
    main_chain_selection_prefix = "(name ca or name n or name o or name c) %s"

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

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

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

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

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

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

    self.model_before_regularization = self.model.deep_copy()

    t9 = time()

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

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

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

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

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

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

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

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

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

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

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

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

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

    obj = run2(restraints_manager=grm,
               pdb_hierarchy=model.get_hierarchy(),
               correct_special_position_tolerance=1.0,
               ncs_restraints_group_list=ncs_restraints_group_list,
               max_number_of_iterations=300,
               number_of_macro_cycles=number_of_cycles,
               bond=True,
               nonbonded=True,
               angle=True,
               dihedral=True,
               chirality=True,
               planarity=True,
               fix_rotamer_outliers=True,
               log=log)
    grm.geometry.reference_dihedral_manager = None
  def run(self):
    t_0 = time()

    ncs_obj = iotbx.ncs.input(
        hierarchy=self.whole_pdb_h,
        chain_max_rmsd=4.0,
        chain_similarity_threshold=0.99,
        residue_match_radius=999.0)
    print >> self.log, "Found NCS groups:"
    ncs_obj.show(format='phil', log=self.log)
    ncs_restr_group_list = ncs_obj.get_ncs_restraints_group_list(
        raise_sorry=False)
    self.using_ncs = False
    total_ncs_selected_atoms = 0
    master_sel = flex.size_t([])
    filtered_ncs_restr_group_list = self.filter_ncs_restraints_group_list(
        self.whole_pdb_h, ncs_restr_group_list)
    if len(filtered_ncs_restr_group_list) > 0:
      self.using_ncs = True
      master_sel = flex.bool(self.whole_pdb_h.atoms_size(), True)
      for ncs_gr in filtered_ncs_restr_group_list:
        for copy in ncs_gr.copies:
          master_sel.set_selected(copy.iselection, False)
      self.master_pdb_h = self.whole_pdb_h.select(master_sel)
      self.master_sel=master_sel
      self.master_pdb_h.reset_atom_i_seqs()

    if self.using_ncs:
      self.master_pdb_h.write_pdb_file("%s_master_h.pdb" % self.params.output_prefix)
      self.working_pdb_h = self.master_pdb_h
    else:
      self.working_pdb_h = self.whole_pdb_h
    self.working_pdb_h.reset_atom_i_seqs()

    self.ann = ioss.annotation.from_phil(
        phil_helices=self.params.secondary_structure.protein.helix,
        phil_sheets=self.params.secondary_structure.protein.sheet,
        pdb_hierarchy=self.whole_pdb_h)

    self.working_xrs = self.working_pdb_h.extract_xray_structure(crystal_symmetry=self.cs)
    if self.using_ncs:
      self.whole_xrs = self.whole_pdb_h.extract_xray_structure(crystal_symmetry=self.cs)
    else:
      self.whole_xrs = self.working_xrs

    if self.params.use_map_for_reference:
      # self.prepare_reference_map(xrs=self.whole_xrs, pdb_h=self.whole_pdb_h)
      # self.prepare_reference_map_2(xrs=self.whole_xrs, pdb_h=self.whole_pdb_h)
      self.prepare_reference_map_3(xrs=self.whole_xrs, pdb_h=self.whole_pdb_h)
    # STOP()

    if self.ann.get_n_helices() + self.ann.get_n_sheets() == 0:
      self.ann = self.pdb_input.extract_secondary_structure()
    self.original_ann = None
    self.filtered_whole_ann = None
    if self.ann is not None:
      self.original_ann = self.ann.deep_copy()
      print >> self.log, "Original SS annotation"
      print >> self.log, self.original_ann.as_pdb_str()
      self.ann.remove_short_annotations()
      self.filtered_whole_ann = self.ann.deep_copy()
      self.ann.remove_empty_annotations(
          hierarchy=self.working_pdb_h)
      self.filtered_whole_ann.remove_empty_annotations(
          hierarchy=self.whole_pdb_h)
      # self.ann.concatenate_consecutive_helices()
      self.ann.split_helices_with_prolines(
          hierarchy=self.working_pdb_h,
          asc=None)
      self.filtered_whole_ann.split_helices_with_prolines(
          hierarchy=self.whole_pdb_h,
          asc=None)
      # print >> self.log, "Splitted SS annotation"
      # print >> self.log, ann.as_pdb_str()
      print >> self.log, "Filtered SS annotation"
      print >> self.log, self.ann.as_pdb_str()

    # getting grm with SS restraints
    self.get_grm()

    if (self.ann is None or
        self.ann.get_n_helices() + self.ann.get_n_sheets() == 0 or
        not self.params.ss_idealization.enabled):
      print >> self.log, "No secondary structure annotations found or SS idealization is disabled."
      print >> self.log, "Secondary structure substitution step will be skipped"
      self.log.flush()
      # here we want to do geometry minimization anyway!
      negate_selection = None
      if self.reference_map is None:
        outlier_selection_txt = mmtbx.building.loop_closure.utils. \
          rama_outliers_selection(self.working_pdb_h, self.rama_manager, 1)
        print >> self.log, "outlier_selection_txt", outlier_selection_txt
        negate_selection = "all"
        if outlier_selection_txt != "" and outlier_selection_txt is not None:
          negate_selection = "not (%s)" % outlier_selection_txt
      self.minimize(
          hierarchy=self.whole_pdb_h,
          xrs=self.whole_xrs,
          original_pdb_h=self.whole_pdb_h,
          grm=self.whole_grm,
          ncs_restraints_group_list=filtered_ncs_restr_group_list,
          excl_string_selection=negate_selection,
          ss_annotation=self.ann,
          reference_map=self.reference_map)
      # self.original_boxed_hierarchy.write_pdb_file(file_name="original_boxed_h_1.pdb")
    else:
      self.params.ss_idealization.file_name_before_regularization = \
          "%s_ss_before_reg.pdb" % self.params.output_prefix
      ssb.substitute_ss(
          real_h=self.working_pdb_h,
          xray_structure=self.working_xrs,
          ss_annotation=self.ann,
          params=self.params.ss_idealization,
          grm=self.working_grm,
          fix_rotamer_outliers=True,
          cif_objects=self.cif_objects,
          verbose=True,
          reference_map=self.reference_map,
          rotamer_manager=self.rotamer_manager,
          log=self.log)
      self.log.flush()

    self.after_ss_idealization = geometry_no_grm(
        pdb_hierarchy=iotbx.pdb.input(
          source_info=None,
          lines=self.working_pdb_h.as_pdb_string()).construct_hierarchy(),
        molprobity_scores=True)

    # Write resulting pdb file.
    self.shift_and_write_result(
        hierarchy=self.working_pdb_h,
        fname_suffix="ss_ideal",
        grm=self.working_grm)
    # STOP()
    self.params.loop_idealization.minimize_whole = not self.using_ncs
    # self.params.loop_idealization.enabled = False
    # self.params.loop_idealization.variant_search_level = 0
    loop_ideal = loop_idealization(
        pdb_hierarchy=self.working_pdb_h,
        params=self.params.loop_idealization,
        secondary_structure_annotation=self.ann,
        reference_map=self.reference_map,
        crystal_symmetry=self.working_xrs.crystal_symmetry(),
        grm=self.working_grm,
        rama_manager=self.rama_manager,
        rotamer_manager=self.rotamer_manager,
        log=self.log,
        verbose=True)
    self.log.flush()
    # STOP()
    self.shift_and_write_result(
        hierarchy=loop_ideal.resulting_pdb_h,
        fname_suffix="rama_ideal",
        grm=self.working_grm)
    self.after_loop_idealization = geometry_no_grm(
        pdb_hierarchy=iotbx.pdb.input(
          source_info=None,
          lines=loop_ideal.resulting_pdb_h.as_pdb_string()).construct_hierarchy(),
        molprobity_scores=True)

    # fixing remaining rotamer outliers
    fixed_rot_pdb_h = loop_ideal.resulting_pdb_h.deep_copy()
    fixed_rot_pdb_h.reset_atom_i_seqs()
    if (self.params.additionally_fix_rotamer_outliers and
        self.after_loop_idealization.rotamer_outliers > 0.004):
      print >> self.log, "Processing pdb file again for fixing rotamers..."
      self.log.flush()
      print >> self.log, "Fixing rotamers..."
      self.log.flush()
      self.shift_and_write_result(
        hierarchy=fixed_rot_pdb_h,
        fname_suffix="just_before_rota")
      fixed_rot_pdb_h = fix_rotamer_outliers(
          pdb_hierarchy=fixed_rot_pdb_h,
          grm=self.working_grm.geometry,
          xrs=self.working_xrs,
          map_data=self.reference_map,
          mon_lib_srv=self.mon_lib_srv,
          rotamer_manager=self.rotamer_manager,
          verbose=True)

    self.shift_and_write_result(
        hierarchy=fixed_rot_pdb_h,
        fname_suffix="rota_ideal",
        grm=self.working_grm)
    cs_to_write = self.cs if self.shift_vector is None else None
    self.after_rotamer_fixing = geometry_no_grm(
        pdb_hierarchy=iotbx.pdb.input(
          source_info=None,
          lines=fixed_rot_pdb_h.as_pdb_string()).construct_hierarchy(),
        molprobity_scores=True)

    ref_hierarchy_for_final_gm = self.original_boxed_hierarchy
    if not self.params.use_starting_model_for_final_gm:
      ref_hierarchy_for_final_gm = self.whole_pdb_h
    ref_hierarchy_for_final_gm.reset_atom_i_seqs()
    if self.params.additionally_fix_rotamer_outliers:
      ssb.set_xyz_smart(self.working_pdb_h, fixed_rot_pdb_h)
    if self.using_ncs:
      print >> self.log, "Using ncs"
      # multiply back and do geometry_minimization for the whole molecule
      for ncs_gr in ncs_restr_group_list:
        master_h = self.whole_pdb_h.select(ncs_gr.master_iselection)
        for c in ncs_gr.copies:
          new_sites = master_h.atoms().extract_xyz()
          new_c_sites = c.r.elems * new_sites + c.t
          self.whole_pdb_h.select(c.iselection).atoms().set_xyz(new_c_sites)
      self.log.flush()
    else:
      # still need to run gm if rotamers were fixed
      print >> self.log, "Not using ncs"

    # need to update SS manager for the whole model here.
    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_grm.geometry.set_secondary_structure_restraints(
          ss_manager=ss_manager,
          hierarchy=self.whole_pdb_h,
          log=self.log)
    print >> self.log, "loop_ideal.ref_exclusion_selection", loop_ideal.ref_exclusion_selection
    print >> self.log, "Minimizing whole model"
    self.minimize(
        hierarchy=self.whole_pdb_h,
        xrs=self.whole_xrs,
        grm=self.whole_grm,
        ncs_restraints_group_list=filtered_ncs_restr_group_list,
        original_pdb_h=ref_hierarchy_for_final_gm,
        excl_string_selection=loop_ideal.ref_exclusion_selection,
        ss_annotation=self.ann,
        reference_map = self.reference_map)
    self.shift_and_write_result(
        hierarchy=self.whole_pdb_h,
        fname_suffix="all_idealized",
        grm=self.whole_grm)
    self.final_model_statistics = geometry_no_grm(
        pdb_hierarchy=iotbx.pdb.input(
          source_info=None,
          lines=self.whole_pdb_h.as_pdb_string()).construct_hierarchy(),
        molprobity_scores=True)
    # self.original_boxed_hierarchy.write_pdb_file(file_name="original_boxed_end.pdb")
    self.time_for_run = time() - t_0
def exercise_sheets_bonding_pattern_with_insertions():
  pdb_apar_input = iotbx.pdb.hierarchy.input(pdb_string = """\
SCRYST1   46.460   46.460  193.210  90.00  90.00 120.00 P 31 2 1
SCALE1      0.021524  0.012427  0.000000        0.00000
SCALE2      0.000000  0.024854  0.000000        0.00000
SCALE3      0.000000  0.000000  0.005176        0.00000
ATOM      5  N   TYR A   2      27.208 -20.701   0.590  1.00  7.29           N
ATOM      6  CA  TYR A   2      27.617 -19.424   0.052  1.00  7.96           C
ATOM      7  C   TYR A   2      26.483 -18.436   0.263  1.00  6.87           C
ATOM      8  O   TYR A   2      25.303 -18.771   0.249  1.00  6.97           O
ATOM      9  CB  TYR A   2      27.861 -19.541  -1.451  1.00  7.90           C
ATOM     10  CG  TYR A   2      28.902 -20.556  -1.857  1.00  9.09           C
ATOM     11  CD1 TYR A   2      30.255 -20.336  -1.592  1.00 11.43           C
ATOM     12  CD2 TYR A   2      28.566 -21.697  -2.545  1.00 10.59           C
ATOM     13  CE1 TYR A   2      31.227 -21.244  -1.987  1.00 13.93           C
ATOM     14  CE2 TYR A   2      29.518 -22.630  -2.915  1.00 11.76           C
ATOM     15  CZ  TYR A   2      30.847 -22.395  -2.659  1.00 13.67           C
ATOM     16  OH  TYR A   2      31.792 -23.309  -3.059  1.00 18.26           O
ATOM     17  N   SER A   2A     26.854 -17.177   0.412  1.00  6.66           N
ATOM     18  CA  SER A   2A     25.899 -16.083   0.383  0.53  6.83           C
ATOM     20  C   SER A   2A     26.500 -14.946  -0.440  1.00  5.61           C
ATOM     21  O   SER A   2A     27.729 -14.822  -0.565  1.00  8.14           O
ATOM     22  CB  SER A   2A     25.569 -15.634   1.795  0.53  7.38           C
ATOM     24  OG  SER A   2A     26.740 -15.136   2.390  0.53  9.79           O
ATOM     26  N   CYS A   2B     25.627 -14.135  -0.995  1.00  5.11           N
ATOM     27  CA  CYS A   2B     26.070 -13.062  -1.865  1.00  5.65           C
ATOM     28  C   CYS A   2B     25.043 -11.934  -1.798  1.00  4.54           C
ATOM     29  O   CYS A   2B     23.856 -12.180  -1.528  1.00  5.60           O
ATOM     30  CB  CYS A   2B     26.253 -13.557  -3.295  1.00  7.00           C
ATOM     31  SG  CYS A   2B     24.806 -14.269  -4.119  1.00  8.88           S
ATOM     32  N   ARG A   5      25.486 -10.691  -2.002  1.00  4.69           N
ATOM     33  CA  ARG A   5      24.558  -9.545  -2.064  1.00  4.87           C
ATOM     34  C   ARG A   5      25.196  -8.395  -2.796  1.00  4.57           C
ATOM     35  O   ARG A   5      26.416  -8.238  -2.793  1.00  5.50           O
ATOM     36  CB  ARG A   5      24.061  -9.108  -0.700  1.00  6.43           C
ATOM     37  CG  ARG A   5      25.121  -8.566   0.219  1.00  6.96           C
ATOM     38  CD  ARG A   5      24.461  -8.032   1.494  1.00  7.25           C
ATOM     39  NE  ARG A   5      25.452  -7.547   2.440  1.00  7.63           N
ATOM     40  CZ  ARG A   5      26.107  -8.341   3.280  1.00  9.10           C
ATOM     41  NH1 ARG A   5      25.867  -9.642   3.297  1.00  9.68           N
ATOM     42  NH2 ARG A   5      26.974  -7.836   4.146  1.00 10.30           N
ATOM     43  N   ALA A   6      24.325  -7.563  -3.358  1.00  4.39           N
ATOM     44  CA  ALA A   6      24.723  -6.362  -4.067  1.00  4.73           C
ATOM     45  C   ALA A   6      23.693  -5.275  -3.769  1.00  4.15           C
ATOM     46  O   ALA A   6      22.482  -5.458  -3.987  1.00  4.96           O
ATOM     47  CB  ALA A   6      24.831  -6.626  -5.558  1.00  5.96           C
ATOM     48  N   VAL A   7      24.165  -4.139  -3.284  1.00  4.97           N
ATOM     49  CA  VAL A   7      23.374  -2.917  -3.085  1.00  4.42           C
ATOM     50  C   VAL A   7      23.482  -2.046  -4.325  1.00  4.45           C
ATOM     51  O   VAL A   7      24.589  -1.663  -4.717  1.00  5.55           O
ATOM     52  CB  VAL A   7      23.830  -2.159  -1.806  1.00  5.09           C
ATOM     53  CG1 VAL A   7      23.111  -0.842  -1.686  1.00  5.65           C
ATOM     54  CG2 VAL A   7      23.612  -2.998  -0.570  1.00  6.88           C
ATOM    204  N   MET A  31      18.177  -3.966  -4.656  1.00  4.72           N
ATOM    205  CA  MET A  31      18.833  -4.887  -3.744  1.00  5.29           C
ATOM    206  C   MET A  31      18.765  -6.294  -4.322  1.00  4.60           C
ATOM    207  O   MET A  31      17.661  -6.738  -4.657  1.00  4.98           O
ATOM    208  CB  MET A  31      18.097  -4.868  -2.387  1.00  6.39           C
ATOM    209  CG  MET A  31      18.723  -5.755  -1.334  1.00  8.61           C
ATOM    210  SD  MET A  31      20.248  -5.074  -0.655  1.00 11.04           S
ATOM    211  CE  MET A  31      21.358  -6.427  -0.777  1.00  8.94           C
ATOM    212  N   ALA A  32      19.899  -6.986  -4.412  1.00  3.90           N
ATOM    213  CA  ALA A  32      19.934  -8.380  -4.864  1.00  3.55           C
ATOM    214  C   ALA A  32      20.720  -9.194  -3.858  1.00  3.90           C
ATOM    215  O   ALA A  32      21.762  -8.763  -3.393  1.00  5.01           O
ATOM    216  CB  ALA A  32      20.552  -8.500  -6.255  1.00  4.55           C
ATOM    217  N   SER A  33      20.230 -10.407  -3.567  1.00  4.02           N
ATOM    218  CA  SER A  33      20.980 -11.288  -2.645  1.00  3.79           C
ATOM    219  C   SER A  33      20.591 -12.727  -2.850  1.00  4.33           C
ATOM    220  O   SER A  33      19.532 -13.045  -3.412  1.00  4.84           O
ATOM    221  CB  SER A  33      20.830 -10.880  -1.167  1.00  4.55           C
ATOM    222  OG  SER A  33      19.498 -11.105  -0.710  1.00  5.25           O
ATOM    223  N   GLY A  34      21.415 -13.600  -2.283  1.00  4.96           N
ATOM    224  CA  GLY A  34      21.104 -14.997  -2.335  1.00  4.50           C
ATOM    225  C   GLY A  34      21.914 -15.837  -1.397  1.00  4.64           C
ATOM    226  O   GLY A  34      22.836 -15.343  -0.732  1.00  4.96           O
ATOM    227  N   THR A  35      21.521 -17.105  -1.329  1.00  4.35           N
ATOM    228  CA  THR A  35      22.277 -18.138  -0.654  1.00  4.02           C
ATOM    229  C   THR A  35      22.226 -19.392  -1.495  1.00  4.55           C
ATOM    230  O   THR A  35      21.221 -19.652  -2.170  1.00  4.35           O
ATOM    231  CB  THR A  35      21.715 -18.436   0.762  1.00  5.12           C
ATOM    232  OG1 THR A  35      20.356 -18.929   0.668  1.00  5.51           O
ATOM    233  CG2 THR A  35      21.733 -17.222   1.670  1.00  5.97           C
ATOM    234  N   SER A  35A     23.294 -20.178  -1.426  1.00  4.63           N
ATOM    235  CA  SER A  35A     23.402 -21.406  -2.221  1.00  4.58           C
ATOM    236  C   SER A  35A     24.387 -22.368  -1.553  1.00  5.05           C
ATOM    237  O   SER A  35A     24.929 -22.095  -0.497  1.00  6.22           O
ATOM    238  CB  SER A  35A     23.881 -21.071  -3.639  1.00  5.69           C
ATOM    239  OG  SER A  35A     25.213 -20.561  -3.633  1.00  7.12           O
""")

  pdb_par_input = iotbx.pdb.hierarchy.input(pdb_string = """\
CRYST1   46.460   46.460  193.210  90.00  90.00 120.00 P 31 2 1
SCALE1      0.021524  0.012427  0.000000        0.00000
SCALE2      0.000000  0.024854  0.000000        0.00000
SCALE3      0.000000  0.000000  0.005176        0.00000
ATOM     67  N   ALA A  15       5.011  -5.031  -8.967  1.00  5.73           N
ATOM     68  CA  ALA A  15       4.943  -6.455  -9.287  1.00  6.16           C
ATOM     69  C   ALA A  15       5.610  -7.252  -8.166  1.00  6.39           C
ATOM     70  O   ALA A  15       6.751  -7.007  -7.857  1.00  9.87           O
ATOM     71  CB  ALA A  15       5.684  -6.739 -10.604  1.00  7.46           C
ATOM     72  N   THR A  15A      4.929  -8.263  -7.636  1.00  5.49           N
ATOM     73  C   THR A  15A      5.316 -10.600  -7.084  1.00  5.07           C
ATOM     74  O   THR A  15A      4.214 -11.002  -7.422  1.00  6.51           O
ATOM     75  CA  THR A  15A      5.513  -9.172  -6.657  1.00  5.70           C
ATOM     76  CB  THR A  15A      4.864  -9.001  -5.276  1.00  8.31           C
ATOM     77  N   GLY A  17       6.393 -11.375  -7.067  1.00  4.56           N
ATOM     78  CA  GLY A  17       6.325 -12.770  -7.439  1.00  4.26           C
ATOM     79  C   GLY A  17       7.219 -13.654  -6.621  1.00  4.41           C
ATOM     80  O   GLY A  17       8.263 -13.233  -6.114  1.00  5.01           O
ATOM     81  N   SER A  18       6.827 -14.921  -6.561  1.00  4.24           N
ATOM     82  CA  SER A  18       7.657 -15.945  -5.959  1.00  4.02           C
ATOM     83  C   SER A  18       7.539 -17.244  -6.724  1.00  3.64           C
ATOM     84  O   SER A  18       6.482 -17.526  -7.331  1.00  4.07           O
ATOM     85  CB  SER A  18       7.335 -16.157  -4.481  1.00  5.86           C
ATOM     86  N   ALA A  19       8.573 -18.049  -6.627  1.00  3.19           N
ATOM     87  CA  ALA A  19       8.578 -19.414  -7.195  1.00  3.31           C
ATOM     88  C   ALA A  19       9.370 -20.307  -6.238  1.00  3.20           C
ATOM     89  O   ALA A  19      10.484 -19.956  -5.817  1.00  4.21           O
ATOM     90  CB  ALA A  19       9.235 -19.415  -8.574  1.00  3.85           C
ATOM     91  N   THR A  20       8.825 -21.476  -5.940  1.00  3.77           N
ATOM     92  CA  THR A  20       9.478 -22.432  -5.047  1.00  3.70           C
ATOM     93  C   THR A  20       9.444 -23.827  -5.640  1.00  3.56           C
ATOM     94  O   THR A  20       8.383 -24.281  -6.108  1.00  4.14           O
ATOM     95  CB  THR A  20       8.787 -22.430  -3.673  1.00  4.76           C
ATOM     96  N   THR A  20A     10.560 -24.542  -5.569  1.00  4.00           N
ATOM     97  CA  THR A  20A     10.597 -25.962  -5.876  1.00  4.05           C
ATOM     98  C   THR A  20A     10.984 -26.770  -4.636  1.00  4.53           C
ATOM     99  O   THR A  20A     11.770 -26.361  -3.802  1.00  5.04           O
ATOM    100  CB  THR A  20A     11.488 -26.293  -7.083  1.00  4.38           C
ATOM    189  N   GLN A  40       0.280  -6.099  -9.049  1.00  6.35           N
ATOM    190  CA  GLN A  40       0.087  -7.454  -9.580  1.00  6.35           C
ATOM    191  C   GLN A  40       0.964  -8.417  -8.788  1.00  6.09           C
ATOM    192  O   GLN A  40       2.080  -8.093  -8.393  1.00  6.87           O
ATOM    193  CB  GLN A  40       0.461  -7.523 -11.060  1.00  7.52           C
ATOM    194  N   THR A  41       0.419  -9.596  -8.544  1.00  6.66           N
ATOM    195  CA  THR A  41       1.108 -10.640  -7.800  1.00  6.93           C
ATOM    196  C   THR A  41       0.932 -12.005  -8.414  1.00  6.82           C
ATOM    197  O   THR A  41      -0.069 -12.258  -9.104  1.00  8.79           O
ATOM    198  CB  THR A  41       0.633 -10.636  -6.352  1.00 10.84           C
ATOM    199  N   ALA A  42       1.951 -12.847  -8.263  1.00  6.44           N
ATOM    200  CA  ALA A  42       1.923 -14.209  -8.797  1.00  6.59           C
ATOM    201  C   ALA A  42       2.829 -15.117  -7.992  1.00  5.51           C
ATOM    202  O   ALA A  42       3.835 -14.684  -7.420  1.00  5.94           O
ATOM    203  CB  ALA A  42       2.327 -14.218 -10.264  1.00  9.02           C
ATOM    204  N   LYS A  42A      2.479 -16.398  -7.978  1.00  6.26           N
ATOM    205  CA  LYS A  42A      3.247 -17.395  -7.256  1.00  6.48           C
ATOM    206  C   LYS A  42A      3.186 -18.741  -7.955  1.00  5.78           C
ATOM    207  O   LYS A  42A      2.206 -19.041  -8.623  1.00  9.40           O
ATOM    208  CB  LYS A  42A      2.727 -17.535  -5.836  1.00  8.81           C
ATOM    209  N   SER A  44       4.243 -19.534  -7.818  1.00  4.43           N
ATOM    210  CA  SER A  44       4.241 -20.890  -8.325  1.00  4.28           C
ATOM    211  C   SER A  44       4.998 -21.811  -7.358  1.00  4.09           C
ATOM    212  O   SER A  44       5.865 -21.377  -6.584  1.00  4.53           O
ATOM    213  CB  SER A  44       4.831 -20.949  -9.731  1.00  5.33           C
ATOM    214  N   PHE A  45       4.660 -23.091  -7.444  1.00  4.39           N
ATOM    215  CA  PHE A  45       5.198 -24.135  -6.576  1.00  4.44           C
ATOM    216  C   PHE A  45       5.222 -25.415  -7.389  1.00  4.16           C
ATOM    217  O   PHE A  45       4.183 -25.754  -7.979  1.00  5.11           O
ATOM    218  CB  PHE A  45       4.254 -24.281  -5.370  1.00  5.22           C
ATOM    219  N   ALA A  45A      6.347 -26.119  -7.403  1.00  3.62           N
ATOM    220  CA  ALA A  45A      6.443 -27.338  -8.202  1.00  3.99           C
ATOM    221  C   ALA A  45A      7.579 -28.205  -7.717  1.00  4.57           C
ATOM    222  O   ALA A  45A      8.479 -27.750  -7.000  1.00  4.79           O
ATOM    223  CB  ALA A  45A      6.607 -27.026  -9.678  1.00  4.52           C
TER
END
""")

  pdb_par_ac_input = iotbx.pdb.hierarchy.input(pdb_string = """\
CRYST1   46.460   46.460  193.210  90.00  90.00 120.00 P 31 2 1
SCALE1      0.021524  0.012427  0.000000        0.00000
SCALE2      0.000000  0.024854  0.000000        0.00000
SCALE3      0.000000  0.000000  0.005176        0.00000
ATOM      1  N  AALA A  15       5.011  -5.031  -8.967  0.50  5.73           N
ATOM      2  CA AALA A  15       4.943  -6.455  -9.287  0.50  6.16           C
ATOM      3  C  AALA A  15       5.610  -7.252  -8.166  0.50  6.39           C
ATOM      4  O  AALA A  15       6.751  -7.007  -7.857  0.50  9.87           O
ATOM      5  CB AALA A  15       5.684  -6.739 -10.604  0.50  7.46           C
ATOM      6  N  BALA A  15       5.111  -5.031  -8.967  0.50  5.73           N
ATOM      7  CA BALA A  15       5.043  -6.455  -9.287  0.50  6.16           C
ATOM      8  C  BALA A  15       5.710  -7.252  -8.166  0.50  6.39           C
ATOM      9  O  BALA A  15       6.851  -7.007  -7.857  0.50  9.87           O
ATOM     10  CB BALA A  15       5.784  -6.739 -10.604  0.50  7.46           C
ATOM     11  N  ATHR A  15A      4.929  -8.263  -7.636  0.50  5.49           N
ATOM     12  C  ATHR A  15A      5.316 -10.600  -7.084  0.50  5.07           C
ATOM     13  O  ATHR A  15A      4.214 -11.002  -7.422  0.50  6.51           O
ATOM     14  CA ATHR A  15A      5.513  -9.172  -6.657  0.50  5.70           C
ATOM     15  CB ATHR A  15A      4.864  -9.001  -5.276  0.50  8.31           C
ATOM     16  N  BTHR A  15A      5.029  -8.263  -7.636  0.50  5.49           N
ATOM     17  C  BTHR A  15A      5.416 -10.600  -7.084  0.50  5.07           C
ATOM     18  O  BTHR A  15A      4.314 -11.002  -7.422  0.50  6.51           O
ATOM     19  CA BTHR A  15A      5.613  -9.172  -6.657  0.50  5.70           C
ATOM     20  CB BTHR A  15A      4.964  -9.001  -5.276  0.50  8.31           C
ATOM     21  N   GLY A  17       6.393 -11.375  -7.067  1.00  4.56           N
ATOM     22  CA  GLY A  17       6.325 -12.770  -7.439  1.00  4.26           C
ATOM     23  C   GLY A  17       7.219 -13.654  -6.621  1.00  4.41           C
ATOM     24  O   GLY A  17       8.263 -13.233  -6.114  1.00  5.01           O
ATOM     25  N   SER A  18       6.827 -14.921  -6.561  1.00  4.24           N
ATOM     26  CA  SER A  18       7.657 -15.945  -5.959  1.00  4.02           C
ATOM     27  C   SER A  18       7.539 -17.244  -6.724  1.00  3.64           C
ATOM     28  O   SER A  18       6.482 -17.526  -7.331  1.00  4.07           O
ATOM     29  CB  SER A  18       7.335 -16.157  -4.481  1.00  5.86           C
ATOM     30  N   ALA A  19       8.573 -18.049  -6.627  1.00  3.19           N
ATOM     31  CA  ALA A  19       8.578 -19.414  -7.195  1.00  3.31           C
ATOM     32  C   ALA A  19       9.370 -20.307  -6.238  1.00  3.20           C
ATOM     33  O   ALA A  19      10.484 -19.956  -5.817  1.00  4.21           O
ATOM     34  CB  ALA A  19       9.235 -19.415  -8.574  1.00  3.85           C
ATOM     35  N   THR A  20       8.825 -21.476  -5.940  1.00  3.77           N
ATOM     36  CA  THR A  20       9.478 -22.432  -5.047  1.00  3.70           C
ATOM     37  C   THR A  20       9.444 -23.827  -5.640  1.00  3.56           C
ATOM     38  O   THR A  20       8.383 -24.281  -6.108  1.00  4.14           O
ATOM     39  CB  THR A  20       8.787 -22.430  -3.673  1.00  4.76           C
ATOM     40  N   THR A  21      10.560 -24.542  -5.569  1.00  4.00           N
ATOM     41  CA  THR A  21      10.597 -25.962  -5.876  1.00  4.05           C
ATOM     42  C   THR A  21      10.984 -26.770  -4.636  1.00  4.53           C
ATOM     43  O   THR A  21      11.770 -26.361  -3.802  1.00  5.04           O
ATOM     44  CB  THR A  21      11.488 -26.293  -7.083  1.00  4.38           C
ATOM     45  N   GLN A  40       0.280  -6.099  -9.049  1.00  6.35           N
ATOM     46  CA  GLN A  40       0.087  -7.454  -9.580  1.00  6.35           C
ATOM     47  C   GLN A  40       0.964  -8.417  -8.788  1.00  6.09           C
ATOM     48  O   GLN A  40       2.080  -8.093  -8.393  1.00  6.87           O
ATOM     49  CB  GLN A  40       0.461  -7.523 -11.060  1.00  7.52           C
ATOM     50  N   THR A  41       0.419  -9.596  -8.544  1.00  6.66           N
ATOM     51  CA  THR A  41       1.108 -10.640  -7.800  1.00  6.93           C
ATOM     52  C   THR A  41       0.932 -12.005  -8.414  1.00  6.82           C
ATOM     53  O   THR A  41      -0.069 -12.258  -9.104  1.00  8.79           O
ATOM     54  CB  THR A  41       0.633 -10.636  -6.352  1.00 10.84           C
ATOM     55  N   ALA A  42       1.951 -12.847  -8.263  1.00  6.44           N
ATOM     56  CA  ALA A  42       1.923 -14.209  -8.797  1.00  6.59           C
ATOM     57  C   ALA A  42       2.829 -15.117  -7.992  1.00  5.51           C
ATOM     58  O   ALA A  42       3.835 -14.684  -7.420  1.00  5.94           O
ATOM     59  CB  ALA A  42       2.327 -14.218 -10.264  1.00  9.02           C
ATOM     60  N   LYS A  42A      2.479 -16.398  -7.978  1.00  6.26           N
ATOM     61  CA  LYS A  42A      3.247 -17.395  -7.256  1.00  6.48           C
ATOM     62  C   LYS A  42A      3.186 -18.741  -7.955  1.00  5.78           C
ATOM     63  O   LYS A  42A      2.206 -19.041  -8.623  1.00  9.40           O
ATOM     64  CB  LYS A  42A      2.727 -17.535  -5.836  1.00  8.81           C
ATOM     65  N   SER A  44       4.243 -19.534  -7.818  1.00  4.43           N
ATOM     66  CA  SER A  44       4.241 -20.890  -8.325  1.00  4.28           C
ATOM     67  C   SER A  44       4.998 -21.811  -7.358  1.00  4.09           C
ATOM     68  O   SER A  44       5.865 -21.377  -6.584  1.00  4.53           O
ATOM     69  CB  SER A  44       4.831 -20.949  -9.731  1.00  5.33           C
ATOM     70  N   PHE A  45       4.660 -23.091  -7.444  1.00  4.39           N
ATOM     71  CA  PHE A  45       5.198 -24.135  -6.576  1.00  4.44           C
ATOM     72  C   PHE A  45       5.222 -25.415  -7.389  1.00  4.16           C
ATOM     73  O   PHE A  45       4.183 -25.754  -7.979  1.00  5.11           O
ATOM     74  CB  PHE A  45       4.254 -24.281  -5.370  1.00  5.22           C
ATOM     75  N   ALA A  46       6.347 -26.119  -7.403  1.00  3.62           N
ATOM     76  CA  ALA A  46       6.443 -27.338  -8.202  1.00  3.99           C
ATOM     77  C   ALA A  46       7.579 -28.205  -7.717  1.00  4.57           C
ATOM     78  O   ALA A  46       8.479 -27.750  -7.000  1.00  4.79           O
ATOM     79  CB  ALA A  46       6.607 -27.026  -9.678  1.00  4.52           C
TER      80      ALA A  46
END
""")

  s_apar_records1 = """\
SHEET    1   A 2 TYR A   2  VAL A   7  0
SHEET    2   A 2 MET A  31  SER A  35A-1  O  ALA A  35A  N  ALA A   2
"""
  s_apar_records2 = """\
SHEET    1   A 2 TYR A   2  VAL A   7  0
SHEET    2   A 2 MET A  31  SER A  35A-1  O  ALA A  32   N  ALA A   6
"""
  s_apar_records3 = """\
SHEET    1   A 2 SER A   2A VAL A   7  0
SHEET    2   A 2 MET A  31  SER A  35A-1  O  ALA A  32   N  ALA A   6
"""

  s_par_records1 = """\
SHEET    1   B 2 ALA A  15  THR A  20A 0
SHEET    2   B 2 GLN A  40  ALA A  45A 1  O  GLN A  40   N  THR A  15A
"""
  s_par_records2 = """\
SHEET    1   B 2 ALA A  15  THR A  20A 0
SHEET    2   B 2 GLN A  40  ALA A  45A 1  O  GLN A  44   N  THR A  20
"""
  s_par_records3 = """\
SHEET    1   B 2 ALA A  15A THR A  20A 0
SHEET    2   B 2 GLN A  40  ALA A  45  1  O  GLN A  44   N  THR A  20
"""

  log = null_out()
  # defpars = sec_str_master_phil
  n_hbonds = []
  for pdb_inp, recs in [
                        (pdb_apar_input, s_apar_records1), # 6
                        (pdb_apar_input, s_apar_records2), # 6
                        (pdb_apar_input, s_apar_records3), # 4
                        (pdb_par_input,  s_par_records1), # 6
                        (pdb_par_input,  s_par_records2), # 6
                        (pdb_par_input,  s_par_records3), # 5
                        (pdb_par_ac_input,  s_par_records1), # 8 hbonds
                        ]:
    ioss_annotation = ioss.annotation.from_records(records = recs.split('\n'))
    ann = ioss_annotation.as_restraint_groups(prefix_scope="secondary_structure")
    defpars = iotbx.phil.parse(sec_str_master_phil_str)
    custom_pars = defpars.fetch(iotbx.phil.parse(ann))
    custom_pars_ex = custom_pars.extract()
    ss_manager = manager(
                pdb_inp.hierarchy,
                sec_str_from_pdb_file=None,
                params=custom_pars_ex.secondary_structure,
                verbose=-1)
    proxies_for_grm = ss_manager.create_protein_hbond_proxies(
      annotation= None,
      log          = log)
    # print proxies_for_grm.size()
    n_hbonds.append(proxies_for_grm.size())
  assert n_hbonds == [6, 6, 4, 6, 6, 5, 8]
Exemple #23
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()
Exemple #24
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 __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")