Example #1
0
def test_reference_model(mon_lib_srv, ener_lib, prefix="tst_reference_model"):
    from mmtbx.geometry_restraints.torsion_restraints.tst_reference_model import \
        model_raw_records, reference_raw_records
    from mmtbx.geometry_restraints.torsion_restraints.reference_model import \
      reference_model
    import mmtbx.model
    # mstream = StringIO()
    from libtbx.utils import multi_out
    mstream = multi_out()
    mstream.register("stdout", sys.stdout)
    mstream_file_name = "polder.log"
    mstreamfile = open(mstream_file_name, "w")
    mstream.register("logfile", mstreamfile)

    work_params = mmtbx.model.manager.get_default_pdb_interpretation_params()
    work_params.reference_model.enabled = True
    work_params.reference_model.fix_outliers = False
    pdb_inp = iotbx.pdb.input(lines=model_raw_records.split('\n'),
                              source_info=None)
    model = mmtbx.model.manager(model_input=pdb_inp,
                                process_input=True,
                                pdb_interpretation_params=work_params)
    reference_hierarchy_list = []
    tmp_hierarchy = iotbx.pdb.input(
        source_info=None,
        lines=reference_raw_records.split('\n')).construct_hierarchy()
    reference_hierarchy_list.append(tmp_hierarchy)
    rm = reference_model(model=model,
                         reference_hierarchy_list=reference_hierarchy_list,
                         params=work_params.reference_model,
                         log=mstream)
    assert rm.get_n_proxies() == 5, "Got %d, expected 5" % rm.get_n_proxies()
    geometry, xrs = make_initial_grm(mon_lib_srv, ener_lib, model_raw_records)
    geometry.adopt_reference_dihedral_manager(rm)
    make_geo_pickle_unpickle(geometry, xrs, prefix)
def test_reference_model(mon_lib_srv, ener_lib, prefix="tst_reference_model"):
  from mmtbx.geometry_restraints.torsion_restraints.tst_reference_model import \
      model_raw_records, reference_raw_records
  from mmtbx.geometry_restraints.torsion_restraints.reference_model import \
    reference_model, reference_model_params
  work_params = reference_model_params.extract()
  work_params.reference_model.enabled = True
  work_params.reference_model.fix_outliers = False
  processed_pdb_file = monomer_library.pdb_interpretation.process(
      mon_lib_srv=mon_lib_srv,
      ener_lib=ener_lib,
      raw_records=model_raw_records.split('\n'))
  pdb_h = processed_pdb_file.all_chain_proxies.pdb_hierarchy
  reference_hierarchy_list = []
  tmp_hierarchy = iotbx.pdb.input(
    source_info=None,
    lines=reference_raw_records.split('\n')).construct_hierarchy()
  reference_hierarchy_list.append(tmp_hierarchy)
  rm = reference_model(
         processed_pdb_file=processed_pdb_file,
         reference_hierarchy_list=reference_hierarchy_list,
         params=work_params.reference_model,
         log=None)
  assert rm.get_n_proxies() == 5, "Got %d, expected 5" % rm.get_n_proxies()
  geometry, xrs = make_initial_grm(mon_lib_srv, ener_lib, model_raw_records)
  geometry.adopt_reference_dihedral_manager(rm)
  make_geo_pickle_unpickle(geometry, xrs, prefix)
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 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 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)