コード例 #1
0
def run(args, log=sys.stdout):
    if (len(args) == 0):
        parsed = defaults(log=log)
        parsed.show(prefix="  ", out=log)
        return
    parsed = defaults(log=log)
    processed_args = mmtbx.utils.process_command_line_args(
        args=args, log=sys.stdout, master_params=parsed)
    processed_args.params.show()
    params = processed_args.params.extract()
    if (len(processed_args.pdb_file_names) == 0):
        raise Sorry("No PDB file found.")
    if (len(processed_args.pdb_file_names) > 1):
        raise Sorry("More than one PDB file found.")
    pdb_file_name = processed_args.pdb_file_names[0]
    if (params.f_obs.f_calc.atomic_model.add_hydrogens):
        pdb_file_name_r = os.path.basename(pdb_file_name) + "_reduce"
        # easy_run.go("phenix.reduce %s > %s"% (pdb_file_name, pdb_file_name_r))
        run_reduce_with_timeout(file_name=pdb_file_name,
                                parameters=" > %s" % pdb_file_name_r)
        pdb_file_name = pdb_file_name_r
    pdb_inp = iotbx.pdb.input(file_name=pdb_file_name)
    pdbi_params = mmtbx.monomer_library.pdb_interpretation.master_params.extract(
    )
    if (params.f_obs.f_calc.atomic_model.use_ramachandran_plot_restraints):
        pdbi_params.peptide_link.ramachandran_restraints = True
    processed_pdb_file = monomer_library.pdb_interpretation.process(
        pdb_inp=pdb_inp,
        mon_lib_srv=monomer_library.server.server(),
        ener_lib=monomer_library.server.ener_lib(),
        params=pdbi_params)
    pdb_hierarchy = processed_pdb_file.all_chain_proxies.pdb_hierarchy
    pdb_hierarchy.atoms().reset_i_seq()
    xray_structure = processed_pdb_file.xray_structure()
    mmtbx.utils.assert_xray_structures_equal(
        x1=xray_structure, x2=pdb_inp.xray_structure_simple())
    sctr_keys = xray_structure.scattering_type_registry().type_count_dict(
    ).keys()
    has_hd = "H" in sctr_keys or "D" in sctr_keys
    geometry = processed_pdb_file.geometry_restraints_manager(
        show_energies=False,
        plain_pairs_radius=5,
        assume_hydrogens_all_missing=not has_hd)
    restraints_manager = mmtbx.restraints.manager(geometry=geometry,
                                                  normalization=True)
    root = iotbx.pdb.hierarchy.root()
    loop_1(params=params,
           root=root,
           xray_structure=xray_structure,
           pdb_hierarchy=pdb_hierarchy,
           restraints_manager=restraints_manager)
    root.write_pdb_file(
        file_name=params.f_obs.f_calc.atomic_model.output_file_name,
        crystal_symmetry=xray_structure.crystal_symmetry())
    simulate_f_obs(root=root,
                   crystal_symmetry=xray_structure.crystal_symmetry(),
                   params=params)
コード例 #2
0
ファイル: tst_reduce_timeout.py プロジェクト: dials/cctbx
def exercise(prefix="tst_reduce_timeout_1"):
    fn = "%s.pdb" % prefix
    with open(fn, 'w') as f:
        f.write(model_1yjp)
    t0 = time()
    rr = run_reduce_with_timeout(
        stdin_lines=None,
        file_name=fn,
        parameters="-oh -his -flip -keep -allalt -pen9999",
        override_auto_timeout_with=None)
    t1 = time()
    assert rr.return_code == 0
    stdout_lines_1 = rr.stdout_lines
    rr = run_reduce_with_timeout(
        stdin_lines=model_1yjp,
        file_name=None,
        parameters="-oh -his -flip -keep -allalt -pen9999 -",
        override_auto_timeout_with=None)
    t1 = time()
    stdout_lines_2 = rr.stdout_lines
    assert rr.return_code == 0
    assert len(stdout_lines_1) > 0
    assert len(stdout_lines_1) == len(stdout_lines_2)
    for l1, l2 in zip(stdout_lines_1, stdout_lines_2):
        assert l1 == l2
    assert 'ATOM      0  HB3 GLN A   5       3.844   2.489   7.623  1.00 11.96           H   new' in stdout_lines_1
    assert 'ATOM      0  HB3 GLN A   5       3.844   2.489   7.623  1.00 11.96           H   new' in stdout_lines_2
    t0 = time()
    rr = run_reduce_with_timeout(
        stdin_lines=None,
        file_name=fn,
        parameters="-oh -his -flip -keep -allalt -pen9999",
        override_auto_timeout_with=0.01)
    t1 = time()
    assert t1 - t0 < 0.1
    assert rr.return_code == -15

    t0 = time()
    rr = run_reduce_with_timeout(
        stdin_lines=model_1yjp,
        file_name=None,
        parameters="-oh -his -flip -keep -allalt -pen9999 -",
        override_auto_timeout_with=0.01)
    t1 = time()
    assert t1 - t0 < 0.1
    assert rr.return_code == -15

    print("OK")
コード例 #3
0
def run_reduce(pdb_string, remove_hydrogens=True):
    assert (libtbx.env.has_module(name="reduce"))
    trim = " -quiet -trim -"
    build = " -quiet -build -allalt -"
    input_str = ""
    if (remove_hydrogens):
        clean = run_reduce_with_timeout(parameters=trim,
                                        stdin_lines=pdb_string)
        check_and_report_reduce_failure(clean, pdb_string,
                                        "reduce_failure.pdb")
        input_str = "\n".join(clean.stdout_lines)
    else:
        input_str = pdb_string
    output = run_reduce_with_timeout(parameters=build, stdin_lines=input_str)
    check_and_report_reduce_failure(output, input_str, "reduce_failure.pdb")
    return output
コード例 #4
0
ファイル: fake_f_obs.py プロジェクト: dials/cctbx
def run(args, log=sys.stdout):
    if (len(args) == 0):
        parsed = defaults(log=log)
        parsed.show(prefix="  ", out=log)
        return
    parsed = defaults(log=log)
    processed_args = mmtbx.utils.process_command_line_args(
        args=args, log=sys.stdout, master_params=parsed)
    processed_args.params.show()
    params = processed_args.params.extract()
    if (len(processed_args.pdb_file_names) == 0):
        raise Sorry("No PDB file found.")
    if (len(processed_args.pdb_file_names) > 1):
        raise Sorry("More than one PDB file found.")
    pdb_file_name = processed_args.pdb_file_names[0]
    if (params.f_obs.f_calc.atomic_model.add_hydrogens):
        pdb_file_name_r = os.path.basename(pdb_file_name) + "_reduce"
        # easy_run.go("phenix.reduce %s > %s"% (pdb_file_name, pdb_file_name_r))
        run_reduce_with_timeout(file_name=pdb_file_name,
                                parameters=" > %s" % pdb_file_name_r)
        pdb_file_name = pdb_file_name_r
    pdbi_params = mmtbx.model.manager.get_default_pdb_interpretation_params()
    if (params.f_obs.f_calc.atomic_model.use_ramachandran_plot_restraints):
        pdbi_params.pdb_interpretation.ramachandran_plot_restraints.enabled = True
    model = mmtbx.model.manager(model_input=iotbx.pdb.input(
        file_name=pdb_file_name))
    model.process(make_restraints=True, pdb_interpretation_params=pdbi_params)
    root = iotbx.pdb.hierarchy.root()
    loop_1(params=params,
           root=root,
           xray_structure=model.get_xray_structure(),
           pdb_hierarchy=model.get_hierarchy(),
           restraints_manager=model.get_restraints_manager())
    root.write_pdb_file(
        file_name=params.f_obs.f_calc.atomic_model.output_file_name,
        crystal_symmetry=model.crystal_symmetry())
    simulate_f_obs(root=root,
                   crystal_symmetry=model.crystal_symmetry(),
                   params=params)
コード例 #5
0
  def __init__(self, pdb_hierarchy):
    re_flip = re.compile(":FLIP")
    validation.__init__(self)
    in_lines = pdb_hierarchy.as_pdb_string()
    reduce_out = run_reduce_with_timeout(
        parameters="-BUILD -",
        stdin_lines=in_lines)
    check_and_report_reduce_failure(
        fb_object=reduce_out,
        input_lines=in_lines,
        output_fname="reduce_fail.pdb")
    from mmtbx.validation import utils
    use_segids = utils.use_segids_in_place_of_chainids(
      hierarchy=pdb_hierarchy)
    for line in reduce_out.stdout_lines:
    #chain format (2-char chain)
    #USER  MOD Set 1.1: B  49 GLN     :FLIP  amide:sc=    -2.7! C(o=-5.8!,f=-1.3!)
    #segid format (4-char segid)
    #USER  MOD Set 1.1:B     49 GLN     :FLIP  amide:sc=    -2.7! C(o=-5.8!,f=-1.3!)
      if re_flip.search(line):
        resid = line.split(":")[1]
        #reduce has slightly different outputs using chains versus segid
        if len(resid) == 15: #chain
          chain_id = resid[0:2].strip()
          segid = None
          if (len(chain_id) == 0):
            chain_id = ' '
          resid_less_chain = resid[2:]
        elif len(resid) == 17: #segid
          #self.results = []
          #return
          chain_id = None
          segid = resid[0:4].strip()
          #chain_id = resid[0:4].strip()
          resid_less_chain = resid[4:]
        else:
          raise Sorry("unexpected length of residue identifier in reduce USER MODs.")
        resname = resid_less_chain[5:8]

        assert (resname in ["ASN", "GLN", "HIS"])
        flip = nqh_flip(
          chain_id=chain_id,
          segid=segid,
          resseq=resid_less_chain[0:4].strip(),
          icode= resid_less_chain[4:5],
          altloc=resid_less_chain[12:13],
          resname=resname,
          outlier=True)
        flip.set_coordinates_from_hierarchy(pdb_hierarchy)
        self.results.append(flip)
        self.n_outliers += 1
コード例 #6
0
    def add_hydrogen(self, no_reduce_db=False):
        ''' Use reduce program to add hydrogen

        Parameters
        ----------
        obj: file object or parmed.Structure or its derived class

        Returns
        -------
        parm : parmed.Structure

        Requires
        --------
        reduce
        '''
        def touch(fname, times=None):
            with open(fname, 'a'):
                os.utime(fname, times)

        from mmtbx.utils import run_reduce_with_timeout

        parameters = '-BUILD -NUC -NOFLIP'
        if no_reduce_db:
            touch('./dummydb')
            parameters += ' -DB ./dummydb'
        parameters += ' -'

        fileobj = StringIO()
        self.write_pdb(fileobj)
        fileobj.seek(0)

        reduce_out = run_reduce_with_timeout(
            parameters=parameters,
            stdin_lines=fileobj.read(),
            stdout_splitlines=False,
        )
        assert reduce_out.return_code == 0

        pdbh = StringIO()
        pdbh.write(reduce_out.stdout_buffer)
        pdbh.seek(0)
        self.parm = parmed.read_PDB(pdbh)
        return self
コード例 #7
0
def run(file_name):
    pdb_inp = iotbx.pdb.input(file_name=file_name)
    hierarchy = pdb_inp.construct_hierarchy()
    asc = hierarchy.atom_selection_cache()
    sel = asc.selection("not(element H or element D)")
    hierarchy = hierarchy.select(sel)
    rr = run_reduce_with_timeout(
        stdin_lines=hierarchy.as_pdb_string().splitlines(),
        file_name=None,  #"model.pdb.gz",
        parameters="-oh -his -flip -keep -allalt -pen9999 -",
        override_auto_timeout_with=None)
    pdb_inp = iotbx.pdb.input(source_info=None, lines=rr.stdout_lines)
    hierarchy = pdb_inp.construct_hierarchy()
    asc = hierarchy.atom_selection_cache()
    sel = asc.selection("protein")
    hierarchy = hierarchy.select(sel)
    model = mmtbx.model.manager(model_input=None,
                                pdb_hierarchy=hierarchy,
                                crystal_symmetry=pdb_inp.crystal_symmetry(),
                                process_input=True,
                                log=null_out())
    return hbond.find(model=model)
コード例 #8
0
def check_and_add_hydrogen(
        pdb_hierarchy=None,
        file_name=None,
        nuclear=False,
        keep_hydrogens=True,
        verbose=False,
        model_number=0,
        n_hydrogen_cut_off=0,
        time_limit=120,
        allow_multiple_models=True,
        crystal_symmetry=None,
        do_flips=False,
        log=None):
  """
  If no hydrogens present, force addition for clashscore calculation.
  Use REDUCE to add the hydrogen atoms.

  Args:
    pdb_hierarchy : pdb hierarchy
    file_name (str): pdb file name
    nuclear (bool): When True use nuclear cloud x-H distances and vdW radii,
      otherwise use electron cloud x-H distances and vdW radii
    keep_hydrogens (bool): when True, if there are hydrogen atoms, keep them
    verbose (bool): verbosity of printout
    model_number (int): the number of model to use
    time_limit (int): limit the time it takes to add hydrogen atoms
    n_hydrogen_cut_off (int): when number of hydrogen atoms < n_hydrogen_cut_off
      force keep_hydrogens tp True
    allow_multiple_models (bool): Allow models that contain more than one model
    crystal_symmetry : must provide crystal symmetry when using pdb_hierarchy

  Returns:
    (str): PDB string
    (bool): True when PDB string was updated
  """
  if not log: log = sys.stdout
  if file_name:
    pdb_inp = iotbx.pdb.input(file_name=file_name)
    pdb_hierarchy = pdb_inp.construct_hierarchy()
    cryst_sym = pdb_inp.crystal_symmetry()
  elif not allow_multiple_models:
    assert crystal_symmetry
    cryst_sym = crystal_symmetry
  else:
    cryst_sym = None
  assert pdb_hierarchy
  assert model_number < len(pdb_hierarchy.models())
  models = pdb_hierarchy.models()
  if (len(models) > 1) and (not allow_multiple_models):
    raise Sorry("When using CCTBX clashscore, provide only a single model.")
  model = models[model_number]
  r = iotbx.pdb.hierarchy.root()
  mdc = model.detached_copy()
  r.append_model(mdc)
  if keep_hydrogens:
    elements = r.atoms().extract_element()
    # strangely the elements can have a space when coming from phenix.clashscore
    # but no space when coming from phenix.molprobity
    h_count = elements.count('H')
    if h_count <= n_hydrogen_cut_off: h_count += elements.count(' H')
    if h_count <= n_hydrogen_cut_off: h_count += elements.count('D')
    if h_count <= n_hydrogen_cut_off: h_count += elements.count(' D')
    if h_count > n_hydrogen_cut_off:
      has_hd = True
    else:
      has_hd = False
    if not has_hd:
      if verbose:
        print("\nNo H/D atoms detected - forcing hydrogen addition!\n", file=log)
      keep_hydrogens = False
  import libtbx.load_env
  has_reduce = libtbx.env.has_module(name="reduce")
  # add hydrogen if needed
  if has_reduce and (not keep_hydrogens):
    # set reduce running parameters
    if verbose:
      print("\nAdding H/D atoms with reduce...\n")
    build = "-oh -his -flip -keep -allalt -limit{}"
    if not do_flips : build += " -pen9999"
    if nuclear:
      build += " -nuc -"
    else:
      build += " -"
    build = build.format(time_limit)
    trim = " -quiet -trim -"
    stdin_lines = r.as_pdb_string(cryst_sym)
    clean_out = run_reduce_with_timeout(
        parameters=trim,
        stdin_lines=stdin_lines)
    stdin_fname = "reduce_fail.pdb"
    check_and_report_reduce_failure(
        fb_object=clean_out,
        input_lines=stdin_lines,
        output_fname="reduce_fail.pdb")
    build_out = run_reduce_with_timeout(
        parameters=build,
        stdin_lines=clean_out.stdout_lines)
    check_and_report_reduce_failure(
        fb_object=build_out,
        input_lines=stdin_lines,
        output_fname="reduce_fail.pdb")
    reduce_str = '\n'.join(build_out.stdout_lines)
    return reduce_str,True
  else:
    if not has_reduce:
      msg = 'molprobity.reduce could not be detected on your system.\n'
      msg += 'Cannot add hydrogen to PDB file'
      print(msg, file=log)
    if verbose:
      print("\nUsing input model H/D atoms...\n")
    return r.as_pdb_string(cryst_sym),False
コード例 #9
0
def stats(model, prefix, no_ticks=True):
    # Get rid of H, multi-model, no-protein and single-atom residue models
    if (model.percent_of_single_atom_residues() > 20):
        return None
    sel = model.selection(string="protein")
    if (sel.count(True) == 0):
        return None
    ssr = "protein and not (element H or element D or resname UNX or resname UNK or resname UNL)"
    sel = model.selection(string=ssr)
    model = model.select(sel)
    if (len(model.get_hierarchy().models()) > 1):
        return None
    # Add H; this looses CRYST1 !
    rr = run_reduce_with_timeout(
        stdin_lines=model.get_hierarchy().as_pdb_string().splitlines(),
        file_name=None,
        parameters="-oh -his -flip -keep -allalt -pen9999 -",
        override_auto_timeout_with=None)
    # Create model; this is a single-model pure protein with new H added
    pdb_inp = iotbx.pdb.input(source_info=None, lines=rr.stdout_lines)
    model = mmtbx.model.manager(model_input=None,
                                build_grm=True,
                                pdb_hierarchy=pdb_inp.construct_hierarchy(),
                                process_input=True,
                                log=null_out())
    box = uctbx.non_crystallographic_unit_cell_with_the_sites_in_its_center(
        sites_cart=model.get_sites_cart(), buffer_layer=5)
    model.set_sites_cart(box.sites_cart)
    model._crystal_symmetry = box.crystal_symmetry()
    #
    N = 10
    SS = get_ss_selections(hierarchy=model.get_hierarchy())
    HB_all = find(
        model=model.select(flex.bool(model.size(), True)),
        a_DHA_cutoff=90).get_params_as_arrays(replace_with_empty_threshold=N)
    HB_alpha = find(
        model=model.select(SS.both.h_sel),
        a_DHA_cutoff=90).get_params_as_arrays(replace_with_empty_threshold=N)
    HB_beta = find(
        model=model.select(SS.both.s_sel),
        a_DHA_cutoff=90).get_params_as_arrays(replace_with_empty_threshold=N)
    print(HB_all.d_HA.size())
    result_dict = {}
    result_dict["all"] = HB_all
    result_dict["alpha"] = HB_alpha
    result_dict["beta"] = HB_beta
    #  result_dict["loop"]  = get_selected(sel=loop_sel)
    # Load histograms for reference high-resolution d_HA and a_DHA
    pkl_fn = libtbx.env.find_in_repositories(
        relative_path="mmtbx") + "/nci/d_HA_and_a_DHA_high_res.pkl"
    assert os.path.isfile(pkl_fn)
    ref = easy_pickle.load(pkl_fn)
    #
    import matplotlib as mpl
    mpl.use('Agg')
    import matplotlib.pyplot as plt
    fig = plt.figure(figsize=(10, 10))
    kwargs = dict(histtype='bar', bins=20, range=[1.6, 3.0], alpha=.8)
    for j, it in enumerate([["alpha", 1], ["beta", 3], ["all", 5]]):
        key, i = it
        ax = plt.subplot(int("32%d" % i))
        if (no_ticks):
            #ax.set_xticks([])
            ax.set_yticks([])
        if (j in [0, 1]):
            ax.tick_params(bottom=False)
            ax.set_xticklabels([])
        ax.tick_params(axis="x", labelsize=12)
        ax.tick_params(axis="y", labelsize=12, left=False, pad=-2)
        ax.text(0.98,
                0.92,
                key,
                size=12,
                horizontalalignment='right',
                transform=ax.transAxes)
        HB = result_dict[key]
        if HB is None: continue
        w1 = np.ones_like(HB.d_HA) / HB.d_HA.size()
        ax.hist(HB.d_HA, color="orangered", weights=w1, rwidth=0.3, **kwargs)
        #
        start, end1, end2 = 0, max(ref.distances[key].vals), \
          round(max(ref.distances[key].vals),2)
        if (not no_ticks):
            plt.yticks([0.01, end1], ["0", end2],
                       visible=True,
                       rotation="horizontal")

        if (key == "alpha"): plt.ylim(0, end2 + 0.02)
        elif (key == "beta"): plt.ylim(0, end2 + 0.02)
        elif (key == "all"): plt.ylim(0, end2 + 0.02)
        else: assert 0
        #
        if (j == 0): ax.set_title("Distance", size=15)
        bins = list(flex.double(ref.distances[key].bins))
        ax.bar(bins, ref.distances[key].vals, alpha=.3, width=0.07)
    #
    kwargs = dict(histtype='bar', bins=20, range=[90, 180], alpha=.8)
    for j, it in enumerate([["alpha", 2], ["beta", 4], ["all", 6]]):
        key, i = it
        ax = plt.subplot(int("32%d" % i))
        if (j in [0, 1]):
            ax.tick_params(bottom=False)
            ax.set_xticklabels([])
        if (no_ticks):
            #ax.set_xticks([])
            ax.set_yticks([])
        ax.tick_params(axis="x", labelsize=12)
        ax.tick_params(axis="y", labelsize=12, left=False, pad=-2)
        ax.text(0.98,
                0.92,
                key,
                size=12,
                horizontalalignment='right',
                transform=ax.transAxes)

        ax.text(0.98,
                0.92,
                key,
                size=12,
                horizontalalignment='right',
                transform=ax.transAxes)
        #if(j in [0,1]): ax.plot_params(bottom=False)
        HB = result_dict[key]
        if HB is None: continue
        w1 = np.ones_like(HB.a_DHA) / HB.a_DHA.size()
        ax.hist(HB.a_DHA, color="orangered", weights=w1, rwidth=0.3, **kwargs)
        #
        start, end1, end2 = 0, max(ref.angles[key].vals), \
          round(max(ref.angles[key].vals),2)
        if (not no_ticks):
            plt.yticks([0.01, end1], ["0", end2],
                       visible=True,
                       rotation="horizontal")

        if (key == "alpha"): plt.ylim(0, end2 + 0.02)
        elif (key == "beta"): plt.ylim(0, end2 + 0.02)
        elif (key == "all"): plt.ylim(0, end2 + 0.02)
        else: assert 0
        #
        if (j == 0): ax.set_title("Angle", size=15)
        ax.bar(ref.angles[key].bins, ref.angles[key].vals, width=4.5, alpha=.3)
    plt.subplots_adjust(wspace=0.12, hspace=0.025)
    if (no_ticks):
        plt.subplots_adjust(wspace=0.025, hspace=0.025)
    #fig.savefig("%s.png"%prefix, dpi=1000)
    fig.savefig("%s.pdf" % prefix)