Example #1
0
def build_cycle(pdb_hierarchy,
                fmodel,
                geometry_restraints_manager,
                params,
                selection=None,
                cif_objects=(),
                nproc=Auto,
                out=sys.stdout,
                verbose=False,
                debug=None,
                i_cycle=0):
    from mmtbx import restraints
    from scitbx.array_family import flex
    t_start = time.time()
    hd_sel = fmodel.xray_structure.hd_selection()
    n_hydrogen = hd_sel.count(True)
    if (n_hydrogen > 0) and (True):  #params.building.delete_hydrogens):
        print("WARNING: %d hydrogen atoms will be removed!" % n_hydrogen,
              file=out)
        non_hd_sel = ~hd_sel
        # XXX it's better to do this in-place for the hierarchy, because calling
        # pdb_hierarchy.select(non_hd_sel) will not remove parent-child
        # relationships involving hydrogens, which causes problems when running
        # the MolProbity validation.
        pdb_hierarchy.remove_hd(reset_i_seq=True)
        xray_structure = fmodel.xray_structure.select(non_hd_sel)
        assert (
            pdb_hierarchy.atoms_size() == xray_structure.scatterers().size())
        fmodel.update_xray_structure(xray_structure)
        geometry_restraints_manager = geometry_restraints_manager.select(
            non_hd_sel)
    pdb_atoms = pdb_hierarchy.atoms()
    segids = pdb_atoms.extract_segid().strip()
    if (not segids.all_eq("")):
        print("WARNING: resetting segids to blank", file=out)
        for i_seq, atom in enumerate(pdb_atoms):
            atom.segid = ""
            sc = fmodel.xray_structure.scatterers()[i_seq]
            sc.label = atom.id_str()
    if isinstance(selection, str):
        sele_cache = pdb_hierarchy.atom_selection_cache()
        selection = sele_cache.selection(selection)
    make_header("Build cycle %d" % (i_cycle + 1), out=out)
    fmodel.info().show_rfactors_targets_scales_overall(out=out)
    if (debug > 0):
        from mmtbx.maps.utils import get_maps_from_fmodel
        from iotbx.map_tools import write_map_coeffs
        two_fofc, fofc = get_maps_from_fmodel(fmodel,
                                              exclude_free_r_reflections=True)
        write_map_coeffs(fwt_coeffs=two_fofc,
                         delfwt_coeffs=fofc,
                         file_name="cycle_%d_start.mtz" % (i_cycle + 1))
    candidate_residues = alt_confs.filter_before_build(
        pdb_hierarchy=pdb_hierarchy,
        fmodel=fmodel,
        geometry_restraints_manager=geometry_restraints_manager,
        selection=selection,
        params=params.prefilter,
        verbose=verbose,
        log=out)
    t1 = time.time()
    print("filtering: %.3fs" % (t1 - t_start), file=out)
    restraints_manager = restraints.manager(
        geometry=geometry_restraints_manager, normalization=True)
    make_sub_header("Finding alternate conformations", out=out)
    building_trials = find_all_alternates(
        residues=candidate_residues,
        pdb_hierarchy=pdb_hierarchy,
        restraints_manager=restraints_manager,
        fmodel=fmodel,
        params=params.residue_fitting,
        nproc=params.nproc,
        verbose=verbose,
        debug=debug,
        log=out).results
    t2 = time.time()
    print("  building: %.3fs" % (t2 - t1), file=out)
    make_sub_header("Scoring and assembling alternates", out=out)
    n_alternates = process_results(pdb_hierarchy=pdb_hierarchy,
                                   fmodel=fmodel,
                                   residues_in=candidate_residues,
                                   building_trials=building_trials,
                                   params=params.residue_fitting,
                                   verbose=verbose,
                                   log=out)
    if (n_alternates > 0):
        print("", file=out)
        print("  %d disordered residues built" % n_alternates, file=out)
        n_split = alt_confs.spread_alternates(
            pdb_hierarchy,
            new_occupancy=params.residue_fitting.expected_occupancy,
            split_all_adjacent=True,
            log=out)
        assert (n_split > 0)
        print("  %d adjacent residues split" % n_split, file=out)
    else:
        print("No alternates built this round.", file=out)
    t3 = time.time()
    print("  assembly: %.3fs" % (t3 - t2), file=out)
    if (not params.cleanup.rsr_after_build):
        if (n_alternates > 0):
            print("Skipping final RSR step (rsr_after_build=False).", file=out)
        else:
            print("No refinement needs to be performed.", file=out)
    else:
        make_sub_header("Real-space refinement", out=out)
        print("", file=out)
        pdb_hierarchy = real_space_refine(pdb_hierarchy=pdb_hierarchy,
                                          fmodel=fmodel,
                                          cif_objects=cif_objects,
                                          params=params,
                                          nproc=params.nproc,
                                          remediate=True,
                                          out=out)
        t4 = time.time()
        print("", file=out)
        print("RSR: %.3fs" % (t4 - t3), file=out)
    fmodel.info().show_targets(out=out, text="Rebuilt model")
    t_end = time.time()
    alt_confs.finalize_model(
        pdb_hierarchy=pdb_hierarchy,
        xray_structure=pdb_hierarchy.extract_xray_structure(
            crystal_symmetry=fmodel.xray_structure),
        set_b_iso=params.cleanup.set_b_iso,
        convert_to_isotropic=params.cleanup.convert_to_isotropic,
        selection="altloc A or altloc B")
    t_end = time.time()
    print("Total runtime for cycle: %.3fs" % (t_end - t_start), file=out)
    return pdb_hierarchy, n_alternates
Example #2
0
def build_cycle (pdb_hierarchy,
    fmodel,
    geometry_restraints_manager,
    params,
    selection=None,
    cif_objects=(),
    nproc=Auto,
    out=sys.stdout,
    verbose=False,
    debug=None,
    i_cycle=0) :
  from mmtbx import restraints
  from scitbx.array_family import flex
  t_start = time.time()
  hd_sel = fmodel.xray_structure.hd_selection()
  n_hydrogen = hd_sel.count(True)
  if (n_hydrogen > 0) and (True) : #params.building.delete_hydrogens) :
    print >> out, "WARNING: %d hydrogen atoms will be removed!" % n_hydrogen
    non_hd_sel = ~hd_sel
    # XXX it's better to do this in-place for the hierarchy, because calling
    # pdb_hierarchy.select(non_hd_sel) will not remove parent-child
    # relationships involving hydrogens, which causes problems when running
    # the MolProbity validation.
    pdb_hierarchy.remove_hd(reset_i_seq=True)
    xray_structure = fmodel.xray_structure.select(non_hd_sel)
    assert (pdb_hierarchy.atoms_size() == xray_structure.scatterers().size())
    fmodel.update_xray_structure(xray_structure)
    geometry_restraints_manager = geometry_restraints_manager.select(non_hd_sel)
  pdb_atoms = pdb_hierarchy.atoms()
  segids = pdb_atoms.extract_segid().strip()
  if (not segids.all_eq("")) :
    print >> out, "WARNING: resetting segids to blank"
    for i_seq, atom in enumerate(pdb_atoms) :
      atom.segid = ""
      sc = fmodel.xray_structure.scatterers()[i_seq]
      sc.label = atom.id_str()
  if isinstance(selection, str) :
    sele_cache = pdb_hierarchy.atom_selection_cache()
    selection = sele_cache.selection(selection)
  make_header("Build cycle %d" % (i_cycle+1), out=out)
  fmodel.info().show_rfactors_targets_scales_overall(out=out)
  if (debug > 0) :
    from mmtbx.maps.utils import get_maps_from_fmodel
    from iotbx.map_tools import write_map_coeffs
    two_fofc, fofc = get_maps_from_fmodel(fmodel,
      exclude_free_r_reflections=True)
    write_map_coeffs(
      fwt_coeffs=two_fofc,
      delfwt_coeffs=fofc,
      file_name="cycle_%d_start.mtz" % (i_cycle+1))
  candidate_residues = alt_confs.filter_before_build(
    pdb_hierarchy=pdb_hierarchy,
    fmodel=fmodel,
    geometry_restraints_manager=geometry_restraints_manager,
    selection=selection,
    params=params.prefilter,
    verbose=verbose,
    log=out)
  t1 = time.time()
  print >> out, "filtering: %.3fs" % (t1-t_start)
  restraints_manager = restraints.manager(
    geometry=geometry_restraints_manager,
    normalization=True)
  make_sub_header("Finding alternate conformations", out=out)
  building_trials = find_all_alternates(
    residues=candidate_residues,
    pdb_hierarchy=pdb_hierarchy,
    restraints_manager=restraints_manager,
    fmodel=fmodel,
    params=params.residue_fitting,
    nproc=params.nproc,
    verbose=verbose,
    debug=debug,
    log=out).results
  t2 = time.time()
  print >> out, "  building: %.3fs" % (t2-t1)
  make_sub_header("Scoring and assembling alternates", out=out)
  n_alternates = process_results(
    pdb_hierarchy=pdb_hierarchy,
    fmodel=fmodel,
    residues_in=candidate_residues,
    building_trials=building_trials,
    params=params.residue_fitting,
    verbose=verbose,
    log=out)
  if (n_alternates > 0) :
    print >> out, ""
    print >> out, "  %d disordered residues built" % n_alternates
    n_split = alt_confs.spread_alternates(pdb_hierarchy,
      new_occupancy=params.residue_fitting.expected_occupancy,
      split_all_adjacent=True,
      log=out)
    assert (n_split > 0)
    print >> out, "  %d adjacent residues split" % n_split
  else :
    print >> out, "No alternates built this round."
  t3 = time.time()
  print >> out, "  assembly: %.3fs" % (t3-t2)
  if (not params.cleanup.rsr_after_build) :
    if (n_alternates > 0) :
      print >> out, "Skipping final RSR step (rsr_after_build=False)."
    else :
      print >> out, "No refinement needs to be performed."
  else :
    make_sub_header("Real-space refinement", out=out)
    print >> out, ""
    pdb_hierarchy = real_space_refine(
      pdb_hierarchy=pdb_hierarchy,
      fmodel=fmodel,
      cif_objects=cif_objects,
      params=params,
      nproc=params.nproc,
      remediate=True,
      out=out)
    t4 = time.time()
    print >> out, ""
    print >> out, "RSR: %.3fs" % (t4-t3)
  fmodel.info().show_targets(out=out, text="Rebuilt model")
  t_end = time.time()
  alt_confs.finalize_model(
    pdb_hierarchy=pdb_hierarchy,
    xray_structure=pdb_hierarchy.extract_xray_structure(
      crystal_symmetry=fmodel.xray_structure),
    set_b_iso=params.cleanup.set_b_iso,
    convert_to_isotropic=params.cleanup.convert_to_isotropic,
    selection="altloc A or altloc B")
  t_end = time.time()
  print >> out, "Total runtime for cycle: %.3fs" % (t_end-t_start)
  return pdb_hierarchy, n_alternates
Example #3
0
def real_space_refine(
        pdb_hierarchy,
        fmodel,
        cif_objects,
        params,
        out,
        nproc=None,
        max_cycles=100,  # arbitrarily large
        remediate=False):
    from scitbx.array_family import flex
    i_cycle = 0
    while (i_cycle < max_cycles):
        print("  Cycle %d:" % (i_cycle + 1), file=out)
        # this keeps track of which residues were split in the previous cycle -
        # we only refine segments that have had residues added
        rebuilt_flags = pdb_hierarchy.atoms().extract_tmp_as_size_t()
        processed_pdb_file = building.reprocess_pdb(
            pdb_hierarchy=pdb_hierarchy,
            cif_objects=cif_objects,
            crystal_symmetry=fmodel.xray_structure,
            out=null_out())
        # get the 2mFo-DFc map without new alternates!
        #two_fofc_map = fmodel.two_fofc_map(exclude_free_r_reflections=True)
        pdb_hierarchy = processed_pdb_file.all_chain_proxies.pdb_hierarchy
        pdb_atoms = pdb_hierarchy.atoms()
        xray_structure = processed_pdb_file.xray_structure()
        geometry_restraints_manager = \
          processed_pdb_file.geometry_restraints_manager(show_energies=False)
        fmodel.update_xray_structure(xray_structure)
        sele_cache = pdb_hierarchy.atom_selection_cache()
        # FIXME very inefficient when looping!
        # this will include both the newly built residues and the original atoms,
        # including residues split to allow for backbone flexibility.
        sele_split = sele_cache.selection(alt_confs.SELECTION_MODIFIED)
        sele_main_conf = sele_cache.selection(alt_confs.SELECTION_OLD)
        assert (len(sele_split) > 0)
        k = 0
        fragments = []
        while (k < len(sele_split)):
            if (sele_split[k]):
                current_fragment = flex.size_t()
                while (sele_split[k]):
                    current_fragment.append(k)
                    k += 1
                atom_start = pdb_atoms[current_fragment[0]].fetch_labels()
                atom_end = pdb_atoms[current_fragment[-1]].fetch_labels()
                frag_selection = flex.bool(sele_split.size(), current_fragment)
                if (i_cycle > 0):
                    flags = rebuilt_flags.select(frag_selection)
                    if flags.all_eq(0):
                        continue
                fragments.append(current_fragment)
            else:
                k += 1
        if (len(fragments) == 0):
            pass
        refine_fragments = rsr_fragments_parallel(
            pdb_hierarchy=pdb_hierarchy,
            fmodel=fmodel,
            processed_pdb_file=processed_pdb_file,
            sele_main_conf=sele_main_conf,
            rsr_fofc_map_target=(i_cycle == 0
                                 and params.cleanup.rsr_fofc_map_target))
        refined = easy_mp.pool_map(fixed_func=refine_fragments,
                                   iterable=fragments,
                                   processes=nproc)
        sites_refined = pdb_atoms.extract_xyz()
        for result in refined:
            assert (result is not None)
            result.show(out=out, prefix="    ")
            sites_refined.set_selected(result.selection, result.sites_cart)
        pdb_atoms.set_xyz(sites_refined)
        xray_structure.set_sites_cart(sites_refined)
        fmodel.update_xray_structure(xray_structure)
        if (not remediate) or (max_cycles == 1):
            break
        else:
            for atom in pdb_hierarchy.atoms():
                if (atom.segid == alt_confs.SEGID_NEW_SPLIT):
                    atom.segid = alt_confs.SEGID_NEW_REBUILT
            print("    checking for conformational strain...", file=out)
            n_split = alt_confs.spread_alternates(
                pdb_hierarchy=pdb_hierarchy,
                new_occupancy=params.residue_fitting.expected_occupancy,
                split_all_adjacent=False,
                selection=alt_confs.SELECTION_NEW_REBUILT)
            if (n_split > 0):
                print("    split another %d residue(s) - will re-run RSR" % \
                  n_split, file=out)
            else:
                break
        i_cycle += 1
    xray_structure = pdb_hierarchy.extract_xray_structure(
        crystal_symmetry=fmodel.xray_structure)
    fmodel.update_xray_structure(xray_structure,
                                 update_f_mask=True,
                                 update_f_calc=True)
    #fmodel.info().show_targets(out=out, text="After real-space refinement")
    t2 = time.time()
    return pdb_hierarchy
Example #4
0
def real_space_refine (
    pdb_hierarchy,
    fmodel,
    cif_objects,
    params,
    out,
    nproc=None,
    max_cycles=100, # arbitrarily large
    remediate=False) :
  from scitbx.array_family import flex
  i_cycle = 0
  while (i_cycle < max_cycles) :
    print >> out, "  Cycle %d:" % (i_cycle+1)
    # this keeps track of which residues were split in the previous cycle -
    # we only refine segments that have had residues added
    rebuilt_flags = pdb_hierarchy.atoms().extract_tmp_as_size_t()
    processed_pdb_file = building.reprocess_pdb(
      pdb_hierarchy=pdb_hierarchy,
      cif_objects=cif_objects,
      crystal_symmetry=fmodel.xray_structure,
      out=null_out())
    # get the 2mFo-DFc map without new alternates!
    #two_fofc_map = fmodel.two_fofc_map(exclude_free_r_reflections=True)
    pdb_hierarchy = processed_pdb_file.all_chain_proxies.pdb_hierarchy
    pdb_atoms = pdb_hierarchy.atoms()
    xray_structure = processed_pdb_file.xray_structure()
    geometry_restraints_manager = \
      processed_pdb_file.geometry_restraints_manager(show_energies=False)
    fmodel.update_xray_structure(xray_structure)
    sele_cache = pdb_hierarchy.atom_selection_cache()
    # FIXME very inefficient when looping!
    # this will include both the newly built residues and the original atoms,
    # including residues split to allow for backbone flexibility.
    sele_split = sele_cache.selection(alt_confs.SELECTION_MODIFIED)
    sele_main_conf = sele_cache.selection(alt_confs.SELECTION_OLD)
    assert (len(sele_split) > 0)
    k = 0
    fragments = []
    while (k < len(sele_split)) :
      if (sele_split[k]) :
        current_fragment = flex.size_t()
        while (sele_split[k]) :
          current_fragment.append(k)
          k += 1
        atom_start = pdb_atoms[current_fragment[0]].fetch_labels()
        atom_end = pdb_atoms[current_fragment[-1]].fetch_labels()
        frag_selection = flex.bool(sele_split.size(), current_fragment)
        if (i_cycle > 0) :
          flags = rebuilt_flags.select(frag_selection)
          if flags.all_eq(0) :
            continue
        fragments.append(current_fragment)
      else :
        k += 1
    if (len(fragments) == 0) :
      pass
    refine_fragments = rsr_fragments_parallel(
      pdb_hierarchy=pdb_hierarchy,
      fmodel=fmodel,
      processed_pdb_file=processed_pdb_file,
      sele_main_conf=sele_main_conf,
      rsr_fofc_map_target=(i_cycle==0 and params.cleanup.rsr_fofc_map_target))
    refined = easy_mp.pool_map(
      fixed_func=refine_fragments,
      iterable=fragments,
      processes=nproc)
    sites_refined = pdb_atoms.extract_xyz()
    for result in refined :
      assert (result is not None)
      result.show(out=out, prefix="    ")
      sites_refined.set_selected(result.selection, result.sites_cart)
    pdb_atoms.set_xyz(sites_refined)
    xray_structure.set_sites_cart(sites_refined)
    fmodel.update_xray_structure(xray_structure)
    if (not remediate) or (max_cycles == 1) :
      break
    else :
      for atom in pdb_hierarchy.atoms() :
        if (atom.segid == alt_confs.SEGID_NEW_SPLIT) :
          atom.segid = alt_confs.SEGID_NEW_REBUILT
      print >> out, "    checking for conformational strain..."
      n_split = alt_confs.spread_alternates(
        pdb_hierarchy=pdb_hierarchy,
        new_occupancy=params.residue_fitting.expected_occupancy,
        split_all_adjacent=False,
        selection=alt_confs.SELECTION_NEW_REBUILT)
      if (n_split > 0) :
        print >> out, "    split another %d residue(s) - will re-run RSR" % \
          n_split
      else :
        break
    i_cycle += 1
  xray_structure = pdb_hierarchy.extract_xray_structure(
    crystal_symmetry=fmodel.xray_structure)
  fmodel.update_xray_structure(xray_structure,
    update_f_mask=True,
    update_f_calc=True)
  #fmodel.info().show_targets(out=out, text="After real-space refinement")
  t2 = time.time()
  return pdb_hierarchy