Exemple #1
0
def run(model_filename=None):
    import mmtbx.monomer_library.pdb_interpretation as pdb_inter
    #print_restraints(database)
    if model_filename is not None:
        from iotbx import pdb
        pdb_inp = pdb.input(model_filename)
        pdb_hierarchy = pdb_inp.construct_hierarchy()
        #pdb_hierarchy.show()
        pdb_processed_file = pdb_inter.run(
            args=[model_filename],
            assume_hydrogens_all_missing=False,
            hard_minimum_nonbonded_distance=0.0,
            nonbonded_distance_threshold=None,
            substitute_non_crystallographic_unit_cell_if_necessary=True,
        )
        grm = pdb_processed_file.geometry_restraints_manager()
        xrs = pdb_processed_file.xray_structure()
        sites_cart = xrs.sites_cart()
        site_labels = xrs.scatterers().extract_labels()
        pair_proxies = grm.pair_proxies(sites_cart=sites_cart,
                                        site_labels=site_labels)
        proxies_info_nonbonded = pair_proxies.nonbonded_proxies.get_sorted(
            by_value="delta", sites_cart=sites_cart, site_labels=site_labels)
        rc = get_metal_coordination_proxies(
            pdb_hierarchy,
            pair_proxies.nonbonded_proxies,
        )
        bonds, angles = get_proxies(rc)
        print('\n\tbonds, angles : %d, %d\n\n' % (len(bonds), len(angles)))
def exercise():
  if (not libtbx.env.has_module("mmtbx")):
    print "Skipping exercise(): mmtbx module not available"
    return
  if (libtbx.env.find_in_repositories(relative_path="chem_data") is None):
    print "Skipping exercise(): chem_data directory not available"
    return
  from mmtbx.monomer_library import pdb_interpretation
  file_name = "phe_tst_adp_aniso_restraints.pdb"
  open(file_name, "w").write(phe_pdb)
  out = StringIO()
  processed_pdb_file = pdb_interpretation.run(
                                        args                     = [file_name],
                                        strict_conflict_handling = False,
                                        log                      = out)
  geo = processed_pdb_file.geometry_restraints_manager()
  xray_structure = processed_pdb_file.xray_structure()
  xray_structure.scatterers().flags_set_grads(state=False)
  xray_structure.scatterers().flags_set_grad_u_iso(
    iselection=xray_structure.use_u_iso().iselection())
  xray_structure.scatterers().flags_set_grad_u_aniso(
    iselection=xray_structure.use_u_aniso().iselection())
  adp_rm = cctbx.adp_restraints.adp_aniso_restraints(
                                           xray_structure     = xray_structure,
                                           restraints_manager = geo,
                                           use_hd = False)
  assert approx_equal(flex.mean(adp_rm.gradients_iso), 0.713756592583)
  assert approx_equal(flex.mean(adp_rm.gradients_aniso_cart.as_double()), -0.118959432097)
  assert approx_equal(adp_rm.target, 8.97112989232)
  fd(xray_structure = xray_structure, restraints_manager = geo, eps=1.e-4)
Exemple #3
0
def run(args, out=sys.stdout):
    if (len(args) == 0) or ("--help" in args):
        raise Usage("mmtbx.rigid_bond_test model.pdb")
    from mmtbx.monomer_library import pdb_interpretation
    import mmtbx.restraints
    import mmtbx.model
    import iotbx.phil
    cmdline = iotbx.phil.process_command_line_with_files(
        args=args,
        master_phil=master_phil,
        pdb_file_def="model",
        cif_file_def="restraints")
    params = cmdline.work.extract()
    validate_params(params)
    processed_pdb_file = pdb_interpretation.run(args=[params.model] +
                                                params.restraints)
    geometry = processed_pdb_file.geometry_restraints_manager(
        show_energies=True)
    restraints_manager = mmtbx.restraints.manager(geometry=geometry,
                                                  normalization=True)
    model = mmtbx.model.manager(
        xray_structure=processed_pdb_file.xray_structure(),
        pdb_hierarchy=processed_pdb_file.all_chain_proxies.pdb_hierarchy,
        restraints_manager=restraints_manager,
        log=out)
    make_header("Rigid-bond test", out=out)
    model.show_rigid_bond_test(out=out, use_id_str=True, prefix="  ")
def run (args) :
  from mmtbx.monomer_library import pdb_interpretation
  from cctbx import crystal
  pdb = pdb_interpretation.run(args=args,
    substitute_non_crystallographic_unit_cell_if_necessary=True)
  geo = pdb.geometry_restraints_manager()
  xrs = pdb.xray_structure()
  sites_cart = xrs.sites_frac()
  scatterers = xrs.scatterers()
  hd_sel = xrs.hd_selection()
  table_bonds = geo.shell_sym_tables[0]
  table_1_4 = geo.shell_sym_tables[2]
  assert len(table_1_4) == len(sites_cart)
  for i_seq, sc in enumerate(scatterers) :
    print sc.label
    if (hd_sel[i_seq]) :
      bonded = table_bonds[i_seq].keys()
      for j_seq in bonded :
        for k_seq in table_1_4[j_seq].keys() :
          print "  H/D 1_5 interaction:", scatterers[k_seq].label
    else :
      for j_seq in table_1_4[i_seq].keys() :
        for k_seq in table_bonds[j_seq].keys() :
          if (hd_sel[k_seq]) :
            print "  H/D 1_5 interaction:", scatterers[k_seq].label
def exercise():
    if (not libtbx.env.has_module("mmtbx")):
        print("Skipping exercise(): mmtbx module not available")
        return
    if (libtbx.env.find_in_repositories(relative_path="chem_data") is None):
        print("Skipping exercise(): chem_data directory not available")
        return
    from mmtbx.monomer_library import pdb_interpretation
    file_name = "phe_tst_adp_aniso_restraints.pdb"
    open(file_name, "w").write(phe_pdb)
    out = StringIO()
    processed_pdb_file = pdb_interpretation.run(args=[file_name],
                                                strict_conflict_handling=False,
                                                log=out)
    geo = processed_pdb_file.geometry_restraints_manager()
    xray_structure = processed_pdb_file.xray_structure()
    xray_structure.scatterers().flags_set_grads(state=False)
    xray_structure.scatterers().flags_set_grad_u_iso(
        iselection=xray_structure.use_u_iso().iselection())
    xray_structure.scatterers().flags_set_grad_u_aniso(
        iselection=xray_structure.use_u_aniso().iselection())
    adp_rm = cctbx.adp_restraints.adp_aniso_restraints(
        xray_structure=xray_structure, restraints_manager=geo, use_hd=False)
    assert approx_equal(flex.mean(adp_rm.gradients_iso), 0.713756592583)
    assert approx_equal(flex.mean(adp_rm.gradients_aniso_cart.as_double()),
                        -0.118959432097)
    assert approx_equal(adp_rm.target, 8.97112989232)
    fd(xray_structure=xray_structure, restraints_manager=geo, eps=1.e-4)
Exemple #6
0
def exercise_with_pdb(verbose):
    if (not libtbx.env.has_module(name="mmtbx")):
        print("Skipping exercise_with_pdb():", \
          "mmtbx.monomer_library.pdb_interpretation not available")
        return
    if (libtbx.env.find_in_repositories(relative_path="chem_data") is None):
        print(
            "Skipping exercise_with_pdb(): chem_data directory not available")
        return
    if (verbose):
        out = sys.stdout
    else:
        out = StringIO()
    with open("tmp_cctbx_geometry_restraints.pdb", "w") as f:
        f.write(enk_pdb)
    pdb_interpretation_params = pdb_interpretation.master_params.extract()
    pdb_interpretation_params.sort_atoms = False
    processed_pdb_file = pdb_interpretation.run(
        args=["tmp_cctbx_geometry_restraints.pdb"],
        strict_conflict_handling=False,
        params=pdb_interpretation_params,
        log=out,
    )
    geo = processed_pdb_file.geometry_restraints_manager()
    site_labels = processed_pdb_file.xray_structure().scatterers() \
      .extract_labels()
    #
    assert approx_equal(flex.min(geo.nonbonded_model_distances()), 0.4777342)
    #
    geo._sites_cart_used_for_pair_proxies = None
    #
    sel0 = geo.simple_edge_list()
    assert len(sel0) == 46
    assert sel0[:4] == [(0, 1), (0, 8), (0, 11), (1, 2)]
    assert sel0[-4:] == [(42, 43), (42, 44), (45, 46), (45, 47)]
    geo.bond_params_table[13][14].slack = 0.1
    geo.bond_params_table[28][30].slack = 0.3
    sel = geo.simple_edge_list()
    assert sorted(set(sel0) - set(sel)) == [(13, 14), (28, 30)]
    sel = geo.simple_edge_list(omit_slack_greater_than=0.2)
    assert sorted(set(sel0) - set(sel)) == [(28, 30)]
    #
    d = geo.discard_symmetry(new_unit_cell=(10, 10, 10, 90, 90, 90))
    assert d.site_symmetry_table.special_position_indices().size() == 0
    #
    clusters = geo.rigid_clusters_due_to_dihedrals_and_planes(
        constrain_dihedrals_with_sigma_less_than=10)
    assert sorted([tuple(sorted(c))
                   for c in clusters]) == [(0, 8, 10, 15), (0, 8, 12, 15),
                                           (1, 2, 3, 4, 5, 6, 7, 9),
                                           (5, 6, 7, 9), (12, 13, 14, 19),
                                           (12, 13, 16, 19), (16, 17, 18, 29),
                                           (16, 17, 20, 29), (20, 28, 30, 37),
                                           (20, 28, 31, 37),
                                           (21, 22, 23, 24, 25, 26, 27)]
def pdb_interpretation_run(args):
  from mmtbx.monomer_library.pdb_interpretation import run
  all_processed_pdb_files = run(args=args, return_all_processed_pdb_files=True)
  for processed_pdb_file in all_processed_pdb_files:
    all_proxies = processed_pdb_file.all_chain_proxies
    print "update_restraints:", \
      len(all_proxies.conformation_dependent_restraints_list)
    for x in all_proxies.conformation_dependent_restraints_list:
      x.update_restraints(sites_cart=all_proxies.sites_cart,
        dihedral_proxies=all_proxies.geometry_proxy_registries.dihedral.proxies,
        angle_proxies=all_proxies.geometry_proxy_registries.angle.proxies)
 def test_calling_pdb_interpretation(self):
     """ Make sure can create NCS object and change search parameters """
     params_phil = iotbx.phil.parse(input_string=master_params,
                                    process_includes=True)
     params = params_phil.extract()
     # Turn on NCS search and adjust one of the parameters
     params.ncs_search.enabled = True
     pdb_processed_file = pdb_interpretation.run(args=['test.pdb'],
                                                 params=params,
                                                 log=null_out())
     ncs_obj = pdb_processed_file.ncs_obj
     self.assertEqual(ncs_obj.number_of_ncs_groups, 2)
def run (args=(), params=None, out=None) :
  if (out is None) :
    out = sys.stdout
  if (params is None) :
    import iotbx.phil
    cmdline = iotbx.phil.process_command_line_with_files(
      args=args,
      master_phil_string=master_phil,
      pdb_file_def="adp_statistics.pdb_file",
      cif_file_def="adp_statistics.cif_file",
      usage_string="""\
phenix.b_factor_statistics model.pdb [restraints.cif] [selection=...]

Show statistics for atomic displacement parameters (ADPs) or B-factors,
including TLS contribution if present.""")
    params = cmdline.work.extract()
  validate_params(params)
  import mmtbx.model
  import mmtbx.restraints
  from mmtbx.monomer_library import pdb_interpretation
  processed_pdb_file = pdb_interpretation.run(
    args=[params.adp_statistics.pdb_file] + params.adp_statistics.cif_file,
    substitute_non_crystallographic_unit_cell_if_necessary=True,
    log=out)
  geometry = processed_pdb_file.geometry_restraints_manager(show_energies=True)
  restraints_manager = mmtbx.restraints.manager(
    geometry = geometry,
    normalization = True)
  model = mmtbx.model.manager(
    xray_structure     = processed_pdb_file.xray_structure(),
    pdb_hierarchy      = processed_pdb_file.all_chain_proxies.pdb_hierarchy,
    restraints_manager = restraints_manager,
    log                = out)
  make_sub_header("Analyzing model B-factors", out=out)
  if (params.adp_statistics.selection is not None) :
    sel_cache = model.pdb_hierarchy().atom_selection_cache()
    selection = sel_cache.selection(params.adp_statistics.selection)
    n_sel = selection.count(True)
    if (n_sel == 0) :
      raise Sorry("No atoms in selection!")
    else :
      model = model.select(selection)
      print >> out, "Extracted %d atoms in selection:" % n_sel
      print >> out, "  %s" % params.adp_statistics.selection
      print >> out, ""
  stats = model.adp_statistics()
  stats.file_name = params.adp_statistics.pdb_file
  stats.selection = params.adp_statistics.selection
  stats.show_1(out=out)
  return stats
 def test_calling_pdb_interpretation(self):
   """ Make sure can create NCS object and change search parameters """
   params_phil = iotbx.phil.parse(
     input_string=master_params,
     process_includes=True)
   params = params_phil.extract()
   # Turn on NCS search and adjust one of the parameters
   params.ncs_search.enabled = True
   pdb_processed_file = pdb_interpretation.run(
   args=['test.pdb'],
   params=params,
   log=null_out())
   ncs_obj = pdb_processed_file.ncs_obj
   self.assertEqual(ncs_obj.number_of_ncs_groups,2)
def pdb_interpretation_run(args):
    from mmtbx.monomer_library.pdb_interpretation import run
    all_processed_pdb_files = run(args=args,
                                  return_all_processed_pdb_files=True)
    for processed_pdb_file in all_processed_pdb_files:
        all_proxies = processed_pdb_file.all_chain_proxies
        print "update_restraints:", \
          len(all_proxies.conformation_dependent_restraints_list)
        for x in all_proxies.conformation_dependent_restraints_list:
            x.update_restraints(sites_cart=all_proxies.sites_cart,
                                dihedral_proxies=all_proxies.
                                geometry_proxy_registries.dihedral.proxies,
                                angle_proxies=all_proxies.
                                geometry_proxy_registries.angle.proxies)
def run(args=(), params=None, out=None):
    if (out is None):
        out = sys.stdout
    if (params is None):
        import iotbx.phil
        cmdline = iotbx.phil.process_command_line_with_files(
            args=args,
            master_phil_string=master_phil,
            pdb_file_def="adp_statistics.pdb_file",
            cif_file_def="adp_statistics.cif_file",
            usage_string="""\
phenix.b_factor_statistics model.pdb [restraints.cif] [selection=...]

Show statistics for atomic displacement parameters (ADPs) or B-factors,
including TLS contribution if present.""")
        params = cmdline.work.extract()
    validate_params(params)
    import mmtbx.model
    import mmtbx.restraints
    from mmtbx.monomer_library import pdb_interpretation
    processed_pdb_file = pdb_interpretation.run(
        args=[params.adp_statistics.pdb_file] + params.adp_statistics.cif_file,
        substitute_non_crystallographic_unit_cell_if_necessary=True,
        log=out)
    geometry = processed_pdb_file.geometry_restraints_manager(
        show_energies=True)
    restraints_manager = mmtbx.restraints.manager(geometry=geometry,
                                                  normalization=True)
    model = mmtbx.model.manager(
        xray_structure=processed_pdb_file.xray_structure(),
        pdb_hierarchy=processed_pdb_file.all_chain_proxies.pdb_hierarchy,
        restraints_manager=restraints_manager,
        log=out)
    make_sub_header("Analyzing model B-factors", out=out)
    if (params.adp_statistics.selection is not None):
        sel_cache = model.pdb_hierarchy().atom_selection_cache()
        selection = sel_cache.selection(params.adp_statistics.selection)
        n_sel = selection.count(True)
        if (n_sel == 0):
            raise Sorry("No atoms in selection!")
        else:
            model = model.select(selection)
            print >> out, "Extracted %d atoms in selection:" % n_sel
            print >> out, "  %s" % params.adp_statistics.selection
            print >> out, ""
    stats = model.adp_statistics()
    stats.file_name = params.adp_statistics.pdb_file
    stats.selection = params.adp_statistics.selection
    stats.show_1(out=out)
    return stats
def exercise_with_pdb(verbose):
  if (not libtbx.env.has_module(name="mmtbx")):
    print "Skipping exercise_with_pdb():", \
      "mmtbx.monomer_library.pdb_interpretation not available"
    return
  if (libtbx.env.find_in_repositories(relative_path="chem_data") is None):
    print "Skipping exercise_with_pdb(): chem_data directory not available"
    return
  if (verbose):
    out = sys.stdout
  else:
    out = StringIO()
  open("tmp_cctbx_geometry_restraints.pdb", "w").write(enk_pdb)
  pdb_interpretation_params = pdb_interpretation.master_params.extract()
  pdb_interpretation_params.sort_atoms=False
  processed_pdb_file = pdb_interpretation.run(
    args=["tmp_cctbx_geometry_restraints.pdb"],
    strict_conflict_handling=False,
    params=pdb_interpretation_params,
    log=out,)
  geo = processed_pdb_file.geometry_restraints_manager()
  site_labels = processed_pdb_file.xray_structure().scatterers() \
    .extract_labels()
  #
  assert approx_equal(flex.min(geo.nonbonded_model_distances()), 0.4777342)
  #
  geo._sites_cart_used_for_pair_proxies = None
  #
  sel0 = geo.simple_edge_list()
  assert len(sel0) == 46
  assert sel0[:4] == [(0, 1), (0, 8), (0, 11), (1, 2)]
  assert sel0[-4:] == [(42, 43), (42, 44), (45, 46), (45, 47)]
  geo.bond_params_table[13][14].slack = 0.1
  geo.bond_params_table[28][30].slack = 0.3
  sel = geo.simple_edge_list()
  assert sorted(set(sel0) - set(sel)) == [(13, 14), (28, 30)]
  sel = geo.simple_edge_list(omit_slack_greater_than=0.2)
  assert sorted(set(sel0) - set(sel)) == [(28, 30)]
  #
  d = geo.discard_symmetry(new_unit_cell=(10,10,10,90,90,90))
  assert d.site_symmetry_table.special_position_indices().size()==0
  #
  clusters = geo.rigid_clusters_due_to_dihedrals_and_planes(
    constrain_dihedrals_with_sigma_less_than=10)
  assert sorted([tuple(sorted(c)) for c in clusters]) == [
    (0, 8, 10, 15), (0, 8, 12, 15), (1, 2, 3, 4, 5, 6, 7, 9),
    (5, 6, 7, 9), (12, 13, 14, 19), (12, 13, 16, 19), (16, 17, 18, 29),
    (16, 17, 20, 29), (20, 28, 30, 37), (20, 28, 31, 37),
    (21, 22, 23, 24, 25, 26, 27)]
def run (args, log=sys.stdout) :
  processed_pdb_file = pdb_interpretation.run(
    args=args,
    substitute_non_crystallographic_unit_cell_if_necessary=True,
    log=log)
  xray_structure = processed_pdb_file.xray_structure()
  if xray_structure is None :
    raise Sorry("Could not calculate X-ray structure from this PDB file. "+
      "This is probably due to missing symmetry information (CRYST1 record.")
  geometry = processed_pdb_file.geometry_restraints_manager(
    show_energies=False)
  chain_proxies = processed_pdb_file.all_chain_proxies
  pdb_hierarchy = chain_proxies.pdb_hierarchy
  result = restraints.combined(
    pdb_hierarchy=chain_proxies.pdb_hierarchy,
    xray_structure=xray_structure,
    geometry_restraints_manager=geometry,
    ignore_hd=True)
  result.show(out=log, prefix="  ")
def run(args, log=sys.stdout):
    processed_pdb_file = pdb_interpretation.run(
        args=args,
        substitute_non_crystallographic_unit_cell_if_necessary=True,
        log=log)
    xray_structure = processed_pdb_file.xray_structure()
    if xray_structure is None:
        raise Sorry(
            "Could not calculate X-ray structure from this PDB file. " +
            "This is probably due to missing symmetry information (CRYST1 record."
        )
    geometry = processed_pdb_file.geometry_restraints_manager(
        show_energies=False)
    chain_proxies = processed_pdb_file.all_chain_proxies
    pdb_hierarchy = chain_proxies.pdb_hierarchy
    result = restraints.combined(pdb_hierarchy=chain_proxies.pdb_hierarchy,
                                 xray_structure=xray_structure,
                                 geometry_restraints_manager=geometry,
                                 ignore_hd=True)
    result.show(out=log, prefix="  ")
Exemple #16
0
def process_clash_score(file_name):
  pdb_processed_file = pdb_inter.run(
      args=[file_name],
      assume_hydrogens_all_missing=False,
      hard_minimum_nonbonded_distance=0.0,
      nonbonded_distance_threshold=None,
      substitute_non_crystallographic_unit_cell_if_necessary=True,
      log=null_out())

  grm = pdb_processed_file.geometry_restraints_manager()
  xrs = pdb_processed_file.xray_structure()
  sites_cart = xrs.sites_cart()
  site_labels = xrs.scatterers().extract_labels()
  hd_sel = xrs.hd_selection()
  macro_mol_sel = cs.get_macro_mol_sel(pdb_processed_file)

  clash_score_info = cs.info(
      geometry_restraints_manager=grm,
      macro_molecule_selection=macro_mol_sel,
      sites_cart=sites_cart,
      site_labels=site_labels,
      hd_sel=hd_sel)
  return clash_score_info
def run(args, out=sys.stdout):
    if (len(args) == 0) or ("--help" in args):
        raise Usage("mmtbx.rigid_bond_test model.pdb")
    from mmtbx.monomer_library import pdb_interpretation
    import mmtbx.restraints
    import mmtbx.model
    import iotbx.phil

    cmdline = iotbx.phil.process_command_line_with_files(
        args=args, master_phil=master_phil, pdb_file_def="model", cif_file_def="restraints"
    )
    params = cmdline.work.extract()
    validate_params(params)
    processed_pdb_file = pdb_interpretation.run(args=[params.model] + params.restraints)
    geometry = processed_pdb_file.geometry_restraints_manager(show_energies=True)
    restraints_manager = mmtbx.restraints.manager(geometry=geometry, normalization=True)
    model = mmtbx.model.manager(
        xray_structure=processed_pdb_file.xray_structure(),
        pdb_hierarchy=processed_pdb_file.all_chain_proxies.pdb_hierarchy,
        restraints_manager=restraints_manager,
        log=out,
    )
    make_header("Rigid-bond test", out=out)
    model.show_rigid_bond_test(out=out, use_id_str=True, prefix="  ")
def exercise_na_restraints_output_to_geo(verbose=False):
  for dependency in ("chem_data", "ksdssp"):
    if not libtbx.env.has_module(dependency):
      print "Skipping exercise_na_restraints_output_to_geo(): %s not available" %(
        dependency)
      return
  pdb_str_1dpl_cutted="""\
CRYST1   24.627   42.717   46.906  90.00  90.00  90.00 P 21 21 21    8
ATOM    184  P    DG A   9       9.587  13.026  19.037  1.00  6.28           P
ATOM    185  OP1  DG A   9       9.944  14.347  19.602  1.00  8.07           O
ATOM    186  OP2  DG A   9      10.654  12.085  18.639  1.00  8.27           O
ATOM    187  O5'  DG A   9       8.717  12.191  20.048  1.00  5.88           O
ATOM    188  C5'  DG A   9       7.723  12.833  20.854  1.00  5.45           C
ATOM    189  C4'  DG A   9       7.145  11.818  21.807  1.00  5.40           C
ATOM    190  O4'  DG A   9       6.435  10.777  21.087  1.00  5.77           O
ATOM    191  C3'  DG A   9       8.142  11.036  22.648  1.00  5.10           C
ATOM    192  O3'  DG A   9       8.612  11.838  23.723  1.00  5.90           O
ATOM    193  C2'  DG A   9       7.300   9.857  23.068  1.00  5.97           C
ATOM    194  C1'  DG A   9       6.619   9.536  21.805  1.00  5.97           C
ATOM    195  N9   DG A   9       7.390   8.643  20.931  1.00  5.97           N
ATOM    196  C8   DG A   9       8.074   8.881  19.775  1.00  6.62           C
ATOM    197  N7   DG A   9       8.647   7.820  19.249  1.00  6.57           N
ATOM    198  C5   DG A   9       8.308   6.806  20.141  1.00  6.22           C
ATOM    199  C6   DG A   9       8.620   5.431  20.136  1.00  6.03           C
ATOM    200  O6   DG A   9       9.297   4.803  19.296  1.00  7.21           O
ATOM    201  N1   DG A   9       8.101   4.773  21.247  1.00  6.10           N
ATOM    202  C2   DG A   9       7.365   5.351  22.260  1.00  6.24           C
ATOM    203  N2   DG A   9       6.948   4.569  23.241  1.00  7.88           N
ATOM    204  N3   DG A   9       7.051   6.652  22.257  1.00  6.53           N
ATOM    205  C4   DG A   9       7.539   7.295  21.184  1.00  5.69           C
ATOM    206  P    DC A  10      10.081  11.538  24.300  1.00  5.91           P
ATOM    207  OP1  DC A  10      10.273  12.645  25.291  1.00  7.27           O
ATOM    208  OP2  DC A  10      11.063  11.363  23.228  1.00  6.84           O
ATOM    209  O5'  DC A  10       9.953  10.128  25.026  1.00  5.75           O
ATOM    210  C5'  DC A  10       9.077   9.959  26.149  1.00  5.87           C
ATOM    211  C4'  DC A  10       9.188   8.549  26.672  1.00  5.56           C
ATOM    212  O4'  DC A  10       8.708   7.612  25.667  1.00  5.70           O
ATOM    213  C3'  DC A  10      10.580   8.059  27.007  1.00  5.27           C
ATOM    214  O3'  DC A  10      11.010   8.447  28.315  1.00  5.83           O
ATOM    215  C2'  DC A  10      10.422   6.549  26.893  1.00  5.34           C
ATOM    216  C1'  DC A  10       9.436   6.405  25.754  1.00  5.23           C
ATOM    217  N1   DC A  10      10.113   6.168  24.448  1.00  5.30           N
ATOM    218  C2   DC A  10      10.514   4.871  24.152  1.00  5.28           C
ATOM    219  O2   DC A  10      10.283   3.972  25.000  1.00  5.75           O
ATOM    220  N3   DC A  10      11.131   4.627  22.965  1.00  5.65           N
ATOM    221  C4   DC A  10      11.395   5.628  22.138  1.00  5.80           C
ATOM    222  N4   DC A  10      12.034   5.327  21.005  1.00  6.75           N
ATOM    223  C5   DC A  10      11.029   6.970  22.449  1.00  5.99           C
ATOM    224  C6   DC A  10      10.394   7.203  23.612  1.00  5.56           C
ATOM    226  O5'  DG B  11      12.424  -4.393  18.427  1.00 22.70           O
ATOM    227  C5'  DG B  11      12.380  -5.516  19.282  1.00 14.75           C
ATOM    228  C4'  DG B  11      11.969  -5.112  20.676  1.00 10.42           C
ATOM    229  O4'  DG B  11      12.972  -4.192  21.210  1.00 10.51           O
ATOM    230  C3'  DG B  11      10.649  -4.394  20.782  1.00  8.57           C
ATOM    231  O3'  DG B  11       9.618  -5.363  20.846  1.00  8.69           O
ATOM    232  C2'  DG B  11      10.822  -3.597  22.051  1.00  8.63           C
ATOM    233  C1'  DG B  11      12.236  -3.233  21.980  1.00  9.81           C
ATOM    234  N9   DG B  11      12.509  -1.902  21.305  1.00  8.66           N
ATOM    235  C8   DG B  11      13.175  -1.667  20.135  1.00  9.57           C
ATOM    236  N7   DG B  11      13.255  -0.407  19.824  1.00  9.04           N
ATOM    237  C5   DG B  11      12.613   0.235  20.869  1.00  7.63           C
ATOM    238  C6   DG B  11      12.388   1.612  21.119  1.00  7.05           C
ATOM    239  O6   DG B  11      12.723   2.590  20.419  1.00  7.81           O
ATOM    240  N1   DG B  11      11.715   1.819  22.317  1.00  6.27           N
ATOM    241  C2   DG B  11      11.264   0.828  23.159  1.00  6.05           C
ATOM    242  N2   DG B  11      10.611   1.219  24.248  1.00  5.85           N
ATOM    243  N3   DG B  11      11.483  -0.457  22.942  1.00  6.55           N
ATOM    244  C4   DG B  11      12.150  -0.687  21.797  1.00  6.84           C
ATOM    245  P    DC B  12       8.134  -5.009  20.350  1.00  8.13           P
ATOM    246  OP1  DC B  12       7.367  -6.252  20.459  1.00 10.02           O
ATOM    247  OP2  DC B  12       8.172  -4.307  19.052  1.00  9.79           O
ATOM    248  O5'  DC B  12       7.564  -3.912  21.389  1.00  8.18           O
ATOM    249  C5'  DC B  12       7.275  -4.296  22.719  1.00  8.00           C
ATOM    250  C4'  DC B  12       6.856  -3.057  23.487  1.00  8.01           C
ATOM    251  O4'  DC B  12       8.006  -2.146  23.615  1.00  7.35           O
ATOM    252  C3'  DC B  12       5.763  -2.208  22.890  1.00  7.04           C
ATOM    253  O3'  DC B  12       4.456  -2.800  23.100  1.00  9.82           O
ATOM    254  C2'  DC B  12       6.019  -0.916  23.630  1.00  6.50           C
ATOM    255  C1'  DC B  12       7.467  -0.808  23.608  1.00  7.35           C
ATOM    256  N1   DC B  12       8.040  -0.143  22.396  1.00  6.64           N
ATOM    257  C2   DC B  12       8.017   1.257  22.382  1.00  5.68           C
ATOM    258  O2   DC B  12       7.524   1.832  23.357  1.00  6.32           O
ATOM    259  N3   DC B  12       8.543   1.930  21.312  1.00  6.18           N
ATOM    260  C4   DC B  12       9.009   1.236  20.266  1.00  6.48           C
ATOM    261  N4   DC B  12       9.518   1.926  19.243  1.00  7.43           N
ATOM    262  C5   DC B  12       9.012  -0.198  20.248  1.00  6.83           C
ATOM    263  C6   DC B  12       8.502  -0.825  21.311  1.00  6.80           C
  """
  identical_portions = [
  """\
  Histogram of bond lengths:
        1.23 -     1.31: 5
        1.31 -     1.39: 25
        1.39 -     1.46: 27
        1.46 -     1.54: 25
        1.54 -     1.61: 5
  Bond restraints: 87""",
  """\
  Histogram of bond angle deviations from ideal:
       99.49 -   105.87: 23
      105.87 -   112.26: 36
      112.26 -   118.65: 28
      118.65 -   125.04: 30
      125.04 -   131.42: 13
  Bond angle restraints: 130"""
  ]
  open("tst_cctbx_geometry_restraints_2_na.pdb", "w").write(pdb_str_1dpl_cutted)
  out1 = StringIO()
  out2 = StringIO()
  from mmtbx.monomer_library.server import MonomerLibraryServerError
  try:
    processed_pdb_file = pdb_interpretation.run(
      args=["tst_cctbx_geometry_restraints_2_na.pdb"],
      strict_conflict_handling=False,
      log=out1)
  except MonomerLibraryServerError:
    print "Skipping exercise_na_restraints_output_to_geo(): Encountered MonomerLibraryServerError.\n"
    print "Is the CCP4 monomer library installed and made available through environment variables MMTBX_CCP4_MONOMER_LIB or CLIBD_MON?"
    return
  geo1 = processed_pdb_file.geometry_restraints_manager()
  hbp = geo1.get_n_hbond_proxies()
  from mmtbx import monomer_library
  params = monomer_library.pdb_interpretation.master_params.extract()
  params.secondary_structure.enabled=True
  processed_pdb_file = pdb_interpretation.run(
    args=["tst_cctbx_geometry_restraints_2_na.pdb"],
    params=params,
    strict_conflict_handling=False,
    log=out2)
  geo2 = processed_pdb_file.geometry_restraints_manager()
  hbp = geo2.get_n_hbond_proxies()
  v_out1 = out1.getvalue()
  v_out2 = out2.getvalue()
  assert v_out2.find("""\
    Restraints generated for nucleic acids:
      6 hydrogen bonds
      12 hydrogen bond angles
      0 basepair planarities
      2 basepair parallelities
      2 stacking parallelities""") > 0
  for v in [v_out1, v_out2]:
    for portion in identical_portions:
      if not v.find(portion) > 0:
        print "This portion was not found:\n%s\n=====End of portion." % portion
        assert 0, "the portion above does not match expected portion."
  # check .geo output
  geo_identical_portions = ["Bond restraints: 87",
      "Bond angle restraints: 130", "Dihedral angle restraints: 33",
      "Chirality restraints: 15",
      "Planarity restraints: 4"]
  ss_geo_portions = ["Bond-like restraints: 6",
      "Noncovalent bond angle restraints: 12", "Parallelity restraints: 4",
      "Nonbonded interactions: 504"]
  non_ss_geo_portions = ["Bond-like restraints: 0",
      "Noncovalent bond angle restraints: 0", "Parallelity restraints: 0",
      "Nonbonded interactions: 526"]
  acp = processed_pdb_file.all_chain_proxies
  sites_cart = acp.sites_cart_exact()
  site_labels = [atom.id_str() for atom in acp.pdb_atoms]
  geo_out1 = StringIO()
  geo_out2 = StringIO()
  geo1.show_sorted(sites_cart=sites_cart, site_labels=site_labels, f=geo_out1)
  geo2.show_sorted(sites_cart=sites_cart, site_labels=site_labels, f=geo_out2)
  v_geo_out_noss = geo_out1.getvalue()
  v_geo_out_ss = geo_out2.getvalue()
  for portion in geo_identical_portions+ss_geo_portions:
    assert v_geo_out_ss.find(portion) >= 0
  for portion in geo_identical_portions+non_ss_geo_portions:
    assert v_geo_out_noss.find(portion) >= 0
def run(args, out=None):
    """
  Calculates number of non-bonded atoms overlaps in a model

  prints to log:
    When verbose=True the function print detailed results to log
    When verbose=False it will print:
        nb_overlaps_macro_molecule,
        nb_overlaps_due_to_sym_op,
        nb_overlaps_all

  Args:
    args (list): list of options.
      model=input_file          input PDB file
      cif=input_file            input CIF file for additional model information
      keep_hydrogens=True       keep input hydrogen files (otherwise regenerate)
      nuclear=False             use nuclear x-H distances and vdW radii
      verbose=True              verbose text output
      time_limit=120            Time limit (sec) for Reduce optimization
      show_overlap_type=all     what type of overlaps to show
      show_normalized_nbo=False Show non-bonded overlaps per 1000 atoms
      substitute_non_crystallographic_unit_cell_if_necessary=false
                                fix CRYST1 records if needed
    out : where to wrote the output to.

  Returns:
    nb_overlaps (obj): Object containing overlap and overlap per thousand
    atoms information
  """
    if not out: out = sys.stdout
    if not args:
        print >> out, usage_string
        return None
    cmdline = iotbx.phil.process_command_line_with_files(
        args=args,
        master_phil_string=master_phil_str,
        pdb_file_def="model",
        cif_file_def="cif",
        usage_string=usage_string)
    params = cmdline.work.extract()
    if (params.model is None):
        raise Usage(usage_string)

    pdb_file_name = [x for x in args if x.endswith('.pdb')]
    cif_file_name = [x for x in args if x.endswith('.cif')]
    assert pdb_file_name
    pdb_file_name = pdb_file_name[0]
    if not params.skip_hydrogen_test:
        pdb_with_h, h_were_added = mvc.check_and_add_hydrogen(
            file_name=pdb_file_name,
            model_number=0,
            nuclear=params.nuclear,
            verbose=params.verbose,
            time_limit=params.time_limit,
            keep_hydrogens=params.keep_hydrogens,
            allow_multiple_models=False,
            log=out)
        if h_were_added:
            pdb_file_name = pdb_file_name.replace('.pdb', '_with_h.pdb')
            open(pdb_file_name, 'w').write(pdb_with_h)
    files = [pdb_file_name]
    if cif_file_name:
        files += cif_file_name

    pdb_processed_file = pdb_inter.run(
        args=files,
        assume_hydrogens_all_missing=False,
        hard_minimum_nonbonded_distance=0.0,
        nonbonded_distance_threshold=None,
        substitute_non_crystallographic_unit_cell_if_necessary=params.
        substitute_non_crystallographic_unit_cell_if_necessary,
        log=null_out())
    # test that CRYST1 records are ok
    sps = pdb_processed_file.all_chain_proxies.special_position_settings
    if not sps:
        msg = 'None valid CRSYT1 records.\n'
        msg += 'Consider running mmtbx.nonbonded_overlaps with the option:\n'
        msg += 'substitute_non_crystallographic_unit_cell_if_necessary=true'
        raise Sorry(msg)
    grm = pdb_processed_file.geometry_restraints_manager()
    xrs = pdb_processed_file.xray_structure()
    sites_cart = xrs.sites_cart()
    site_labels = xrs.scatterers().extract_labels()
    hd_sel = xrs.hd_selection()
    macro_mol_sel = nbo.get_macro_mol_sel(pdb_processed_file)
    nb_overlaps = nbo.info(geometry_restraints_manager=grm,
                           macro_molecule_selection=macro_mol_sel,
                           sites_cart=sites_cart,
                           site_labels=site_labels,
                           hd_sel=hd_sel)
    if params.verbose:
        nb_overlaps.show(log=out,
                         nbo_type=params.show_overlap_type,
                         normalized_nbo=params.show_normalized_nbo)
    else:
        all = nb_overlaps.result.nb_overlaps_all
        macro_molecule = nb_overlaps.result.nb_overlaps_macro_molecule
        sym = nb_overlaps.result.nb_overlaps_due_to_sym_op
        out_list = map(lambda x: str(round(x, 2)), [macro_molecule, sym, all])
        print >> out, ', '.join(out_list)
    return nb_overlaps
Exemple #20
0
def run_test(params, pdb_files, other_files, callback=None, log=None):
  if (log is None): log = sys.stdout
  if (params.random_seed is not None):
    random.seed(params.random_seed)
    flex.set_random_seed(value=params.random_seed)
  #
  if (len(pdb_files) != 0):
    print >> log, "PDB files:"
    for file_name in pdb_files:
      print >> log, " ", file_name
    print >> log
  if (len(other_files) != 0):
    print >> log, "Other files:"
    for file_name in other_files:
      print >> log, " ", file_name
    print >> log
  #
  assert len(pdb_files) in [1, 2]
  #
  pdb_interpretation_params = pdb_interpretation.master_params.extract()
  pdb_interpretation_params.dihedral_function_type \
    = params.dihedral_function_type
  processed_pdb_files = pdb_interpretation.run(
    args=pdb_files[-1:]+other_files,
    params=pdb_interpretation_params,
    strict_conflict_handling=False,
    substitute_non_crystallographic_unit_cell_if_necessary=True,
    return_all_processed_pdb_files=True,
    log=log)
  assert len(processed_pdb_files) == 1
  print >> log
  #
  xs = processed_pdb_files[0].xray_structure()
  geo_manager = processed_pdb_files[0].geometry_restraints_manager()
  labels = [sc.label for sc in xs.scatterers()]
  ideal_sites_cart = xs.sites_cart()
  sites = ideal_sites_cart
  masses = xs.atomic_weights()
  tardy_tree_simple_connectivity = geo_manager.construct_tardy_tree(sites=sites)
  rmsd_calculator = tardy_tree_simple_connectivity.rmsd_calculator()
  #
  if (params.tardy_displacements is not None):
    def get_tardy_model_no_potential():
      return scitbx.rigid_body.tardy_model(
        labels=labels,
        sites=sites,
        masses=masses,
        tardy_tree=tardy_tree_simple_connectivity,
        potential_obj=None)
    def get_tardy_model_no_density():
      tardy_tree = scitbx.graph.tardy_tree.construct(
        n_vertices=len(sites), edge_list=[])
      tardy_tree.build_tree()
      potential_obj = potential_object(
        density_map=None,
        geo_manager=geo_manager,
        reduced_geo_manager=None,
        prolsq_repulsion_function_changes=
          params.prolsq_repulsion_function_changes,
        real_space_gradients_delta=None,
        real_space_target_weight=None,
        ideal_sites_cart=None)
      return scitbx.rigid_body.tardy_model(
        labels=labels,
        sites=sites,
        masses=masses,
        tardy_tree=tardy_tree,
        potential_obj=potential_obj)
    if (params.tardy_displacements is Auto):
      auto_params = params.tardy_displacements_auto
      target_rmsd = \
          params.structure_factors_high_resolution \
        * auto_params.rmsd_vs_high_resolution_factor
      target_rmsd_tol = \
          params.structure_factors_high_resolution \
        * auto_params.rmsd_tolerance
      assert target_rmsd > 0
      assert target_rmsd_tol > 0
      print >> log, "Random displacements (%s):" \
        % params.tardy_displacements_auto.parameterization
      print >> log, "  high resolution: %.6g" \
        % params.structure_factors_high_resolution
      print >> log, "  target rmsd: %.6g" % target_rmsd
      print >> log, "  target rmsd tolerance: %.6g" % target_rmsd_tol
      log.flush()
      def raise_max_steps_exceeded(var_name, rmsd_history):
        msg = [
          "tardy_displacements_auto.max_steps exceeded:",
          "  %        -13s  rmsd" % var_name]
        for var_rmsd in rmsd_history:
          msg.append("  %13.6e  %13.6e" % var_rmsd)
        raise Sorry("\n".join(msg))
      if (params.tardy_displacements_auto.parameterization == "cartesian"):
        multiplier = 1.5
        rmsd_history = []
        for i_step in xrange(auto_params.max_steps):
          sites = cartesian_random_displacements(
            sites_cart=ideal_sites_cart,
            target_rmsd=target_rmsd*multiplier)
          tardy_model = get_tardy_model_no_density()
          tardy_model.minimization(max_iterations=20)
          sites = tardy_model.sites_moved()
          sites_moved = sites
          rmsd = rmsd_calculator(sites_moved, ideal_sites_cart)
          rmsd_history.append((multiplier, rmsd))
          print >> log, "    multiplier, rmsd: %13.6e, %13.6e" \
            % rmsd_history[-1]
          log.flush()
          if (rmsd < target_rmsd - target_rmsd_tol):
            if (rmsd != 0):
              multiplier = min(
                multiplier*2, max(
                  multiplier*1.2,
                    target_rmsd / rmsd))
            else:
              multiplier *= 2
          else:
            if (rmsd <= target_rmsd + target_rmsd_tol):
              tardy_model.minimization(max_iterations=500)
              sites = tardy_model.sites_moved()
              sites_moved = sites
              rmsd = rmsd_calculator(sites_moved, ideal_sites_cart)
              rmsd_history.append((0, rmsd))
              print >> log, "    multiplier, rmsd: %13.6e, %13.6e" \
                % rmsd_history[-1]
              log.flush()
              break
            multiplier *= max(0.5, target_rmsd/rmsd)
        else:
          raise_max_steps_exceeded(
            var_name="multiplier", rmsd_history=rmsd_history)
        del rmsd_history
        print >> log, "  actual rmsd: %.6g" % rmsd
        print >> log
      elif (params.tardy_displacements_auto.parameterization == "constrained"):
        tardy_model = get_tardy_model_no_potential()
        tardy_model.assign_random_velocities()
        delta_t = auto_params.first_delta_t
        rmsd_history = []
        assert auto_params.max_steps > 0
        for i_step in xrange(auto_params.max_steps):
          prev_q = tardy_model.pack_q()
          prev_qd = tardy_model.pack_qd()
          tardy_model.dynamics_step(delta_t=delta_t)
          sites_moved = tardy_model.sites_moved()
          rmsd = rmsd_calculator(sites_moved, ideal_sites_cart)
          rmsd_history.append((delta_t, rmsd))
          if (rmsd < target_rmsd - target_rmsd_tol):
            delta_t *= 2 - rmsd / target_rmsd
          else:
            if (rmsd <= target_rmsd + target_rmsd_tol):
              break
            tardy_model.unpack_q(q_packed=prev_q)
            tardy_model.unpack_qd(qd_packed=prev_qd)
            delta_t *= 0.5
          prev_q = None
          prev_qd = None
        else:
          raise_max_steps_exceeded(
            var_name="delta_t", rmsd_history=rmsd_history)
        del rmsd_history
        print >> log, "  actual rmsd: %.6g" % rmsd
        print >> log, "  tardy_displacements=%s" % ",".join(
          ["%.6g" % v for v in tardy_model.pack_q()])
        print >> log
        sites = tardy_model.sites_moved()
      else:
        raise AssertionError
    else:
      tardy_model = get_tardy_model_no_potential()
      q = tardy_model.pack_q()
      if (len(params.tardy_displacements) != len(q)):
        print >> log, "tardy_displacements:", params.tardy_displacements
        hinge_edges = tardy_model.tardy_tree.cluster_manager.hinge_edges
        assert len(hinge_edges) == tardy_model.bodies_size()
        dofej = tardy_model.degrees_of_freedom_each_joint()
        qsej = tardy_model.q_size_each_joint()
        for ib,(i,j) in enumerate(hinge_edges):
          if (i == -1): si = "root"
          else: si = tardy_model.labels[i]
          sj = tardy_model.labels[j]
          print >> log, "%21s - %-21s: %d dof, %d q_size" % (
            si, sj, dofej[ib], qsej[ib])
        print >> log, "Zero displacements:"
        print >> log, "  tardy_displacements=%s" % ",".join(
          [str(v) for v in q])
        raise Sorry("Incompatible tardy_displacements.")
      tardy_model.unpack_q(q_packed=flex.double(params.tardy_displacements))
      sites = tardy_model.sites_moved()
  #
  if (params.emulate_cartesian):
    tardy_tree = scitbx.graph.tardy_tree.construct(
      n_vertices=len(sites), edge_list=[])
    tardy_tree.build_tree()
  else:
    tardy_tree = tardy_tree_simple_connectivity
  print >> log, "tardy_tree summary:"
  tardy_tree.show_summary(vertex_labels=labels, out=log, prefix="  ")
  print >> log
  #
  if (len(pdb_files) == 2):
    ideal_pdb_inp = iotbx.pdb.input(file_name=pdb_files[0])
    ideal_pdb_hierarchy = ideal_pdb_inp.construct_hierarchy()
    assert ideal_pdb_hierarchy.is_similar_hierarchy(
      processed_pdb_files[0].all_chain_proxies.pdb_hierarchy)
    ideal_sites_cart = ideal_pdb_hierarchy.atoms().extract_xyz()
    xs.set_sites_cart(sites_cart=ideal_sites_cart)
  fft_map = xs.structure_factors(
    d_min=params.structure_factors_high_resolution).f_calc().fft_map()
  fft_map.apply_sigma_scaling()
  #
  assert not params.orca_experiments or not params.emulate_cartesian
  if (params.orca_experiments):
    from mmtbx.refinement import orca
    x = orca.expand(
      labels=labels,
      sites_cart=sites,
      masses=masses,
      geo_manager=geo_manager)
    labels = x.labels
    sites = x.sites_cart
    masses = x.masses
    geo_manager = x.geo_manager
    tardy_tree = x.tardy_tree
    x_ideal_sites_cart = flex.vec3_double()
    for i_orc,i_seq in x.indices:
      x_ideal_sites_cart.append(ideal_sites_cart[i_seq])
    ideal_sites_cart = x_ideal_sites_cart
    rmsd_calculator = x.rmsd_calculator(
      tardy_tree_rmsd_calculator=rmsd_calculator)
  #
  if (params.emulate_cartesian or params.keep_all_restraints):
    reduced_geo_manager = None
  else:
    reduced_geo_manager = geo_manager.reduce_for_tardy(tardy_tree=tardy_tree)
  real_space_gradients_delta = \
      params.structure_factors_high_resolution \
    * params.real_space_gradients_delta_resolution_factor
  potential_obj = potential_object(
    density_map=fft_map.real_map(),
    geo_manager=geo_manager,
    reduced_geo_manager=reduced_geo_manager,
    prolsq_repulsion_function_changes=params.prolsq_repulsion_function_changes,
    real_space_gradients_delta=real_space_gradients_delta,
    real_space_target_weight=params.real_space_target_weight,
    ideal_sites_cart=ideal_sites_cart,
    site_labels=labels,
    orca_experiments=params.orca_experiments)
  tardy_model = scitbx.rigid_body.tardy_model(
    labels=labels,
    sites=sites,
    masses=masses,
    tardy_tree=tardy_tree,
    potential_obj=potential_obj)
  mmtbx.refinement.tardy.action(
    tardy_model=tardy_model,
    params=params,
    rmsd_calculator=rmsd_calculator,
    callback=callback,
    log=log)
  print >> 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 () :
  from mmtbx.monomer_library import pdb_interpretation
  import cStringIO
  open("tmp.pdb", "w").write("""\
CRYST1   50.800   50.800  155.300  90.00  90.00  90.00 P 43 21 2     8
ATOM      4  N   SER A   1       8.753  29.755  61.685  1.00 49.13
ATOM      5  CA  SER A   1       9.242  30.200  62.974  1.00 46.62
ANISOU    5  CA  SER A   1    343    490   2719    -45   -169    617
ATOM      6  C   SER A   1      10.453  29.500  63.579  1.00 41.99
ATOM      7  O   SER A   1      10.593  29.607  64.814  1.00 43.24
ANISOU    7  O   SER A   1    343    490   2719    -45   -169    617
ATOM      8  CB  SER A   1       8.052  30.189  63.974  1.00 53.00
ATOM      9  OG  SER A   1       7.294  31.409  63.930  1.00 57.79
ATOM     10  N   ARG A   2      11.360  28.819  62.827  1.00 36.48
ATOM     11  CA  ARG A   2      12.548  28.316  63.532  1.00 30.20
ATOM     12  C   ARG A   2      13.502  29.501  63.500  1.00 25.54
ATOM     13  O   ARG A   2      13.730  30.037  62.407  1.00 23.86
ATOM     14  CB  ARG A   2      13.241  27.119  62.861  1.00 27.44
ATOM     15  CG  ARG A   2      12.412  25.849  62.964  1.00 23.66
ATOM     16  CD  ARG A   2      13.267  24.651  63.266  1.00 23.98
ATOM     17  NE  ARG A   2      13.948  24.115  62.135  1.00 22.71
ATOM     18  CZ  ARG A   2      15.114  23.487  62.201  1.00 21.38
ATOM     19  NH1 ARG A   2      15.845  23.331  63.301  1.00 19.34
ATOM     20  NH2 ARG A   2      15.575  23.030  61.051  1.00 26.66
ATOM     21  N   PRO A   3J     13.947  29.997  64.680  1.00 22.94
ATOM     22  CA  PRO A   3J     14.902  31.100  64.827  1.00 20.19
ATOM     23  C   PRO A   3J     16.195  30.718  64.086  1.00 18.44
ATOM     24  O   PRO A   3J     16.545  29.521  64.086  1.00 19.76
ATOM     25  CB  PRO A   3J     15.133  31.218  66.313  1.00 19.17
ATOM     26  CG  PRO A   3J     14.065  30.364  66.951  1.00 15.12
ATOM     27  CD  PRO A   3J     13.816  29.289  65.966  1.00 19.56
ATOM     28  N  AILE A   4      16.953  31.648  63.512  1.00 15.29
ATOM     29  CA AILE A   4      18.243  31.372  62.859  1.00 14.32
ATOM     30  C  AILE A   4      19.233  32.112  63.743  1.00 13.54
ATOM     31  O  AILE A   4      19.105  33.315  64.009  1.00 11.84
ATOM     32  CB AILE A   4      18.298  31.951  61.406  1.00 13.62
ATOM     33  CG1AILE A   4      17.157  31.300  60.620  1.00 18.39
ATOM     34  CG2AILE A   4      19.661  31.747  60.743  1.00 13.64
ATOM     35  CD1AILE A   4      16.879  32.102  59.355  1.00 16.69
ATOM     28  N  BILE A   4      16.953  31.648  63.512  1.00 15.29
ATOM     29  CA BILE A   4      18.243  31.372  62.859  1.00 14.32
ATOM     30  C  BILE A   4      19.233  32.112  63.743  1.00 13.54
ATOM     31  O  BILE A   4      19.105  33.315  64.009  1.00 11.84
ATOM     32  CB BILE A   4      18.298  31.951  61.406  1.00 13.62
ATOM     33  CG1BILE A   4      17.157  31.300  60.620  1.00 18.39
ATOM     34  CG2BILE A   4      19.661  31.747  60.743  1.00 13.64
ATOM1200035  CD1BILE A   4      16.879  32.102  59.355  1.00 16.69
HETATM 1475  S   SO4 S 188      31.424  42.923  60.396  1.00 55.69           S4+
HETATM 1476  O1  SO4 S 188      31.631  41.513  60.336  1.00 59.84           O1-
HETATM 1477  O2  SO4 S 188      32.533  43.699  59.932  1.00 49.98           O1-
HETATM 1478  O3  SO4 S 188      31.128  43.217  61.738  1.00 59.44           O1-
HETATM 1479  O4  SO4 S 188      30.353  43.201  59.539  1.00 60.54           O1-
HETATM 1480  O   HOH W 200      29.478  23.354  61.364  1.00  8.67      WATE
END""")
  out = cStringIO.StringIO()
  processed_pdb_file = pdb_interpretation.run(args=["tmp.pdb"], log=out)
  m = mouse_selection_manager()
  pdb_hierarchy = processed_pdb_file.all_chain_proxies.pdb_hierarchy
  pdb_hierarchy.atoms().reset_i_seq()
  m.update_selection_handlers(
    pdb_hierarchy=pdb_hierarchy,
    mmtbx_selection_function=processed_pdb_file.all_chain_proxies.selection)
  assert m.selection_size() == 0
  m.apply_selection("chain A")
  assert m.selection_size() == 40
  m.clear_selection()
  m.toggle_chain_selection(5)
  assert m.selection_size() == 40
  m.toggle_residue_selection(10)
  assert m.selection_size() == 29
  m.toggle_atom_selection(10) # XXX: doesn't work!
  assert m.selection_size() == 29
  m.toggle_atom_selection(20)
  assert m.selection_size() == 28

  from iotbx import pdb
  from scitbx.array_family import flex
  pdb_hierarchy = pdb.input(source_info=None, lines=flex.split_lines("""\
HETATM 4049  O   HOH W   1       2.954  13.042  11.632  1.00 37.53           O
HETATM 4050  O   HOH W   2       5.539  14.595  10.951  1.00 31.25           O
HETATM 4051  O   HOH W   3      -2.971  14.661  14.669  1.00 38.68           O
HETATM 4052  O   HOH W   4       6.281  34.000   7.684  1.00 39.58           O
HETATM 4053  O   HOH W   5      16.004   9.039  10.335  1.00 37.31           O
HETATM 4054  O   HOH W   6       2.144   5.718  20.447  1.00 49.77           O
HETATM 4055  O   HOH W   7      -1.180  10.517  14.630  1.00 32.95           O
HETATM 4056  O  AHOH W   8       9.227   8.636  12.535  1.00 32.52           O
HETATM 4056  O  BHOH W   8       9.227   8.636  12.535  1.00 32.52           O
HETATM 4057  O  AHOH W   9      11.070  -0.570  15.047  1.00 30.24           O
HETATM 4057  O  BHOH W   9      11.070  -0.570  15.047  1.00 30.24           O
HETATM 4058  O  AHOH W  10      15.630  -6.169  12.853  1.00 31.08           O
HETATM 4058  O  BHOH W  10      15.630  -6.169  12.853  1.00 31.08           O
HETATM 4059  O   HOH W  11      14.854  -8.299  16.887  1.00 32.65           O
HETATM 4060  O   HOH W  12      27.586   0.391  24.184  1.00 31.29           O
HETATM 4061  O   HOH W  13       3.240   7.801  38.401  1.00 32.09           O
END""")).construct_hierarchy()
  m = mouse_selection_manager()
  pdb_hierarchy.atoms().reset_i_seq()
  m.update_selection_handlers(pdb_hierarchy=pdb_hierarchy,
    mmtbx_selection_function=None)
  assert m.selection_size() == 0
  m.apply_selection("chain W")
  assert m.selection_size() == 16
  m.start_range_selection(5)
  m.end_range_selection(15, deselect=False, ignore_altloc=True)
  assert m.selection_size() == 11
  m.start_range_selection(6)
  m.end_range_selection(10, deselect=False, ignore_altloc=True)
  assert m.selection_size() == 11 # no change because of deselect=False
  m.start_range_selection(6)
  m.end_range_selection(9, deselect=True, ignore_altloc=True)
  assert m.selection_size() == 6
  m.start_range_selection(10)
  m.end_range_selection(12, deselect=True, ignore_altloc=False)
  assert m.selection_size() == 5
  print "OK"
Exemple #23
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 exercise_na_restraints_output_to_geo(verbose=False):
    for dependency in ("chem_data", "ksdssp"):
        if not libtbx.env.has_module(dependency):
            print(
                "Skipping exercise_na_restraints_output_to_geo(): %s not available"
                % (dependency))
            return
    pdb_str_1dpl_cutted = """\
CRYST1   24.627   42.717   46.906  90.00  90.00  90.00 P 21 21 21    8
ATOM    184  P    DG A   9       9.587  13.026  19.037  1.00  6.28           P
ATOM    185  OP1  DG A   9       9.944  14.347  19.602  1.00  8.07           O
ATOM    186  OP2  DG A   9      10.654  12.085  18.639  1.00  8.27           O
ATOM    187  O5'  DG A   9       8.717  12.191  20.048  1.00  5.88           O
ATOM    188  C5'  DG A   9       7.723  12.833  20.854  1.00  5.45           C
ATOM    189  C4'  DG A   9       7.145  11.818  21.807  1.00  5.40           C
ATOM    190  O4'  DG A   9       6.435  10.777  21.087  1.00  5.77           O
ATOM    191  C3'  DG A   9       8.142  11.036  22.648  1.00  5.10           C
ATOM    192  O3'  DG A   9       8.612  11.838  23.723  1.00  5.90           O
ATOM    193  C2'  DG A   9       7.300   9.857  23.068  1.00  5.97           C
ATOM    194  C1'  DG A   9       6.619   9.536  21.805  1.00  5.97           C
ATOM    195  N9   DG A   9       7.390   8.643  20.931  1.00  5.97           N
ATOM    196  C8   DG A   9       8.074   8.881  19.775  1.00  6.62           C
ATOM    197  N7   DG A   9       8.647   7.820  19.249  1.00  6.57           N
ATOM    198  C5   DG A   9       8.308   6.806  20.141  1.00  6.22           C
ATOM    199  C6   DG A   9       8.620   5.431  20.136  1.00  6.03           C
ATOM    200  O6   DG A   9       9.297   4.803  19.296  1.00  7.21           O
ATOM    201  N1   DG A   9       8.101   4.773  21.247  1.00  6.10           N
ATOM    202  C2   DG A   9       7.365   5.351  22.260  1.00  6.24           C
ATOM    203  N2   DG A   9       6.948   4.569  23.241  1.00  7.88           N
ATOM    204  N3   DG A   9       7.051   6.652  22.257  1.00  6.53           N
ATOM    205  C4   DG A   9       7.539   7.295  21.184  1.00  5.69           C
ATOM    206  P    DC A  10      10.081  11.538  24.300  1.00  5.91           P
ATOM    207  OP1  DC A  10      10.273  12.645  25.291  1.00  7.27           O
ATOM    208  OP2  DC A  10      11.063  11.363  23.228  1.00  6.84           O
ATOM    209  O5'  DC A  10       9.953  10.128  25.026  1.00  5.75           O
ATOM    210  C5'  DC A  10       9.077   9.959  26.149  1.00  5.87           C
ATOM    211  C4'  DC A  10       9.188   8.549  26.672  1.00  5.56           C
ATOM    212  O4'  DC A  10       8.708   7.612  25.667  1.00  5.70           O
ATOM    213  C3'  DC A  10      10.580   8.059  27.007  1.00  5.27           C
ATOM    214  O3'  DC A  10      11.010   8.447  28.315  1.00  5.83           O
ATOM    215  C2'  DC A  10      10.422   6.549  26.893  1.00  5.34           C
ATOM    216  C1'  DC A  10       9.436   6.405  25.754  1.00  5.23           C
ATOM    217  N1   DC A  10      10.113   6.168  24.448  1.00  5.30           N
ATOM    218  C2   DC A  10      10.514   4.871  24.152  1.00  5.28           C
ATOM    219  O2   DC A  10      10.283   3.972  25.000  1.00  5.75           O
ATOM    220  N3   DC A  10      11.131   4.627  22.965  1.00  5.65           N
ATOM    221  C4   DC A  10      11.395   5.628  22.138  1.00  5.80           C
ATOM    222  N4   DC A  10      12.034   5.327  21.005  1.00  6.75           N
ATOM    223  C5   DC A  10      11.029   6.970  22.449  1.00  5.99           C
ATOM    224  C6   DC A  10      10.394   7.203  23.612  1.00  5.56           C
ATOM    226  O5'  DG B  11      12.424  -4.393  18.427  1.00 22.70           O
ATOM    227  C5'  DG B  11      12.380  -5.516  19.282  1.00 14.75           C
ATOM    228  C4'  DG B  11      11.969  -5.112  20.676  1.00 10.42           C
ATOM    229  O4'  DG B  11      12.972  -4.192  21.210  1.00 10.51           O
ATOM    230  C3'  DG B  11      10.649  -4.394  20.782  1.00  8.57           C
ATOM    231  O3'  DG B  11       9.618  -5.363  20.846  1.00  8.69           O
ATOM    232  C2'  DG B  11      10.822  -3.597  22.051  1.00  8.63           C
ATOM    233  C1'  DG B  11      12.236  -3.233  21.980  1.00  9.81           C
ATOM    234  N9   DG B  11      12.509  -1.902  21.305  1.00  8.66           N
ATOM    235  C8   DG B  11      13.175  -1.667  20.135  1.00  9.57           C
ATOM    236  N7   DG B  11      13.255  -0.407  19.824  1.00  9.04           N
ATOM    237  C5   DG B  11      12.613   0.235  20.869  1.00  7.63           C
ATOM    238  C6   DG B  11      12.388   1.612  21.119  1.00  7.05           C
ATOM    239  O6   DG B  11      12.723   2.590  20.419  1.00  7.81           O
ATOM    240  N1   DG B  11      11.715   1.819  22.317  1.00  6.27           N
ATOM    241  C2   DG B  11      11.264   0.828  23.159  1.00  6.05           C
ATOM    242  N2   DG B  11      10.611   1.219  24.248  1.00  5.85           N
ATOM    243  N3   DG B  11      11.483  -0.457  22.942  1.00  6.55           N
ATOM    244  C4   DG B  11      12.150  -0.687  21.797  1.00  6.84           C
ATOM    245  P    DC B  12       8.134  -5.009  20.350  1.00  8.13           P
ATOM    246  OP1  DC B  12       7.367  -6.252  20.459  1.00 10.02           O
ATOM    247  OP2  DC B  12       8.172  -4.307  19.052  1.00  9.79           O
ATOM    248  O5'  DC B  12       7.564  -3.912  21.389  1.00  8.18           O
ATOM    249  C5'  DC B  12       7.275  -4.296  22.719  1.00  8.00           C
ATOM    250  C4'  DC B  12       6.856  -3.057  23.487  1.00  8.01           C
ATOM    251  O4'  DC B  12       8.006  -2.146  23.615  1.00  7.35           O
ATOM    252  C3'  DC B  12       5.763  -2.208  22.890  1.00  7.04           C
ATOM    253  O3'  DC B  12       4.456  -2.800  23.100  1.00  9.82           O
ATOM    254  C2'  DC B  12       6.019  -0.916  23.630  1.00  6.50           C
ATOM    255  C1'  DC B  12       7.467  -0.808  23.608  1.00  7.35           C
ATOM    256  N1   DC B  12       8.040  -0.143  22.396  1.00  6.64           N
ATOM    257  C2   DC B  12       8.017   1.257  22.382  1.00  5.68           C
ATOM    258  O2   DC B  12       7.524   1.832  23.357  1.00  6.32           O
ATOM    259  N3   DC B  12       8.543   1.930  21.312  1.00  6.18           N
ATOM    260  C4   DC B  12       9.009   1.236  20.266  1.00  6.48           C
ATOM    261  N4   DC B  12       9.518   1.926  19.243  1.00  7.43           N
ATOM    262  C5   DC B  12       9.012  -0.198  20.248  1.00  6.83           C
ATOM    263  C6   DC B  12       8.502  -0.825  21.311  1.00  6.80           C
  """
    identical_portions = [
        """\
  Histogram of bond lengths:
        1.23 -     1.31: 5
        1.31 -     1.39: 25
        1.39 -     1.46: 27
        1.46 -     1.54: 25
        1.54 -     1.61: 5
  Bond restraints: 87""",
        '''\
  Histogram of bond angle deviations from ideal:
       99.49 -   105.87: 23
      105.87 -   112.26: 36
      112.26 -   118.65: 28
      118.65 -   125.04: 30
      125.04 -   131.42: 13
  Bond angle restraints: 130''',
    ]
    open("tst_cctbx_geometry_restraints_2_na.pdb",
         "w").write(pdb_str_1dpl_cutted)
    out1 = StringIO()
    out2 = StringIO()
    from mmtbx.monomer_library.server import MonomerLibraryServerError
    try:
        processed_pdb_file = pdb_interpretation.run(
            args=["tst_cctbx_geometry_restraints_2_na.pdb"],
            strict_conflict_handling=False,
            log=out1)
    except MonomerLibraryServerError:
        print(
            "Skipping exercise_na_restraints_output_to_geo(): Encountered MonomerLibraryServerError.\n"
        )
        print(
            "Is the CCP4 monomer library installed and made available through environment variables MMTBX_CCP4_MONOMER_LIB or CLIBD_MON?"
        )
        return
    geo1 = processed_pdb_file.geometry_restraints_manager()
    hbp = geo1.get_n_hbond_proxies()
    from mmtbx import monomer_library
    params = monomer_library.pdb_interpretation.master_params.extract()
    params.secondary_structure.enabled = True
    processed_pdb_file = pdb_interpretation.run(
        args=["tst_cctbx_geometry_restraints_2_na.pdb"],
        params=params,
        strict_conflict_handling=False,
        log=out2)
    geo2 = processed_pdb_file.geometry_restraints_manager()
    hbp = geo2.get_n_hbond_proxies()
    v_out1 = out1.getvalue()
    v_out2 = out2.getvalue()
    assert v_out2.find("""\
    Restraints generated for nucleic acids:
      6 hydrogen bonds
      12 hydrogen bond angles
      0 basepair planarities
      2 basepair parallelities
      2 stacking parallelities""") > 0
    for v in [v_out1, v_out2]:
        for portion in identical_portions:
            if not v.find(portion) > 0:
                print("This portion was not found:\n%s\n=====End of portion." %
                      portion)
                assert 0, "the portion above does not match expected portion."
    # check .geo output
    geo_identical_portions = [
        "Bond restraints: 87", "Bond angle restraints: 130",
        "Dihedral angle restraints: 33", "Chirality restraints: 15",
        "Planarity restraints: 4"
    ]
    ss_geo_portions = [
        "Bond-like restraints: 6",
        'Secondary Structure restraints around h-bond angle restraints: 12',
        "Parallelity restraints: 4", "Nonbonded interactions: 504"
    ]
    non_ss_geo_portions = [
        #"Bond-like restraints: 0",
        #'Secondary Structure restraints around h-bond angle restraints: 0',
        "Parallelity restraints: 0",
        "Nonbonded interactions: 526"
    ]
    acp = processed_pdb_file.all_chain_proxies
    sites_cart = acp.sites_cart_exact()
    site_labels = [atom.id_str() for atom in acp.pdb_atoms]
    geo_out1 = StringIO()
    geo_out2 = StringIO()
    geo1.show_sorted(sites_cart=sites_cart,
                     site_labels=site_labels,
                     f=geo_out1)
    geo2.show_sorted(sites_cart=sites_cart,
                     site_labels=site_labels,
                     f=geo_out2)
    v_geo_out_noss = geo_out1.getvalue()
    v_geo_out_ss = geo_out2.getvalue()
    for portion in geo_identical_portions + ss_geo_portions:
        assert v_geo_out_ss.find(portion) >= 0
    for portion in geo_identical_portions + non_ss_geo_portions:
        assert v_geo_out_noss.find(portion) >= 0
def run (args, out=None) :
  """
  Calculates number of non-bonded atoms overlaps in a model

  prints to log:
    When verbose=True the function print detailed results to log
    When verbose=False it will print:
        nb_overlaps_macro_molecule,
        nb_overlaps_due_to_sym_op,
        nb_overlaps_all

  Args:
    args (list): list of options.
      model=input_file          input PDB file
      cif=input_file            input CIF file for additional model information
      keep_hydrogens=True       keep input hydrogen files (otherwise regenerate)
      nuclear=False             use nuclear x-H distances and vdW radii
      verbose=True              verbose text output
      time_limit=120            Time limit (sec) for Reduce optimization
      show_overlap_type=all     what type of overlaps to show
      show_normalized_nbo=False Show non-bonded overlaps per 1000 atoms
      substitute_non_crystallographic_unit_cell_if_necessary=false
                                fix CRYST1 records if needed
    out : where to wrote the output to.

  Returns:
    nb_overlaps (obj): Object containing overlap and overlap per thousand
    atoms information
  """
  if not out: out = sys.stdout
  if not args:
    print >> out,usage_string
    return None
  cmdline = iotbx.phil.process_command_line_with_files(
    args=args,
    master_phil_string=master_phil_str,
    pdb_file_def="model",
    cif_file_def="cif",
    usage_string=usage_string)
  params = cmdline.work.extract()
  if (params.model is None) :
    raise Usage(usage_string)

  pdb_file_name = [x for x in args if x.endswith('.pdb')]
  cif_file_name = [x for x in args if x.endswith('.cif')]
  assert pdb_file_name
  pdb_file_name = pdb_file_name[0]
  if not params.skip_hydrogen_test:
    pdb_with_h, h_were_added = mvc.check_and_add_hydrogen(
        file_name=pdb_file_name,
        model_number=0,
        nuclear=params.nuclear,
        verbose=params.verbose,
        time_limit=params.time_limit,
        keep_hydrogens=params.keep_hydrogens,
        allow_multiple_models=False,
        log=out)
    if h_were_added:
      pdb_file_name = pdb_file_name.replace('.pdb','_with_h.pdb')
      open(pdb_file_name,'w').write(pdb_with_h)
  files = [pdb_file_name]
  if cif_file_name:
      files += cif_file_name

  pdb_processed_file = pdb_inter.run(
    args=files,
    assume_hydrogens_all_missing=False,
    hard_minimum_nonbonded_distance=0.0,
    nonbonded_distance_threshold=None,
    substitute_non_crystallographic_unit_cell_if_necessary=
    params.substitute_non_crystallographic_unit_cell_if_necessary,
    log=null_out()    )
  # test that CRYST1 records are ok
  sps = pdb_processed_file.all_chain_proxies.special_position_settings
  if not sps:
    msg = 'None valid CRSYT1 records.\n'
    msg += 'Consider running phenix.clashscore with the option:\n'
    msg += 'substitute_non_crystallographic_unit_cell_if_necessary=true'
    raise Sorry(msg)
  grm = pdb_processed_file.geometry_restraints_manager()
  xrs = pdb_processed_file.xray_structure()
  sites_cart = xrs.sites_cart()
  site_labels = xrs.scatterers().extract_labels()
  hd_sel = xrs.hd_selection()
  macro_mol_sel = nbo.get_macro_mol_sel(pdb_processed_file)
  nb_overlaps = nbo.info(
    geometry_restraints_manager=grm,
    macro_molecule_selection=macro_mol_sel,
    sites_cart=sites_cart,
    site_labels=site_labels,
    hd_sel=hd_sel)
  if params.verbose:
    nb_overlaps.show(
      log=out,
      nbo_type=params.show_overlap_type,
      normalized_nbo=params.show_normalized_nbo)
  else:
    all = nb_overlaps.result.nb_overlaps_all
    macro_molecule = nb_overlaps.result.nb_overlaps_macro_molecule
    sym = nb_overlaps.result.nb_overlaps_due_to_sym_op
    out_list = map(lambda x: str(round(x,2)),[macro_molecule,sym,all])
    print >> out,', '.join(out_list)
  return nb_overlaps