Exemple #1
0
def exercise_map_utils () :
  #
  # UNSTABLE
  #
  hierarchy, fmodel = get_1yjp_pdb_and_fmodel()
  sel_cache = hierarchy.atom_selection_cache()
  sele = sel_cache.selection("resseq 5 and (name CD or name OE1 or name NE2)")
  sele_all = sel_cache.selection("resseq 5")
  fmodel.xray_structure.scale_adp(factor=0.5, selection=sele)
  fmodel.update_xray_structure(update_f_calc=True)
  two_fofc_map, fofc_map = building.get_difference_maps(fmodel)
  map_stats = building.local_density_quality(
    fofc_map=fofc_map,
    two_fofc_map=two_fofc_map,
    atom_selection=sele_all,
    xray_structure=fmodel.xray_structure)
  out = StringIO()
  map_stats.show_atoms_outside_density(out=out, two_fofc_cutoff=3.0)
  pdb_strs = [ l.split(":")[0] for l in out.getvalue().splitlines() ]
  assert len(pdb_strs) > 0
  # XXX there seems to be a stochastic effect here
  #assert (pdb_strs == ['pdb=" CA  GLN A   5 "', 'pdb=" CB  GLN A   5 "',
  #                     'pdb=" CD  GLN A   5 "', 'pdb=" NE2 GLN A   5 "']), \
  #  pdb_strs
  fc_map = fmodel.map_coefficients(map_type="Fc").fft_map(
    resolution_factor=0.25).apply_sigma_scaling().real_map_unpadded()
  assert (map_stats.number_of_atoms_below_fofc_map_level() == 3)
  assert (map_stats.fraction_of_nearby_grid_points_above_cutoff() > 0.0)
  stats = building.get_model_map_stats(
    selection=sele_all,
    target_map=two_fofc_map,
    model_map=fc_map,
    unit_cell=fmodel.xray_structure.unit_cell(),
    sites_cart=fmodel.xray_structure.sites_cart(),
    pdb_atoms=hierarchy.atoms())
Exemple #2
0
def exercise_map_utils () :
  #
  # UNSTABLE 2x
  #
  hierarchy, fmodel = get_1yjp_pdb_and_fmodel()
  sel_cache = hierarchy.atom_selection_cache()
  sele = sel_cache.selection("resseq 5 and (name CD or name OE1 or name NE2)")
  sele_all = sel_cache.selection("resseq 5")
  fmodel.xray_structure.scale_adp(factor=0.5, selection=sele)
  fmodel.update_xray_structure(update_f_calc=True)
  two_fofc_map, fofc_map = building.get_difference_maps(fmodel)
  map_stats = building.local_density_quality(
    fofc_map=fofc_map,
    two_fofc_map=two_fofc_map,
    atom_selection=sele_all,
    xray_structure=fmodel.xray_structure)
  out = StringIO()
  map_stats.show_atoms_outside_density(out=out, two_fofc_cutoff=3.0)
  pdb_strs = [ l.split(":")[0] for l in out.getvalue().splitlines() ]
  assert len(pdb_strs) > 0
  # XXX there seems to be a stochastic effect here
  #assert (pdb_strs == ['pdb=" CA  GLN A   5 "', 'pdb=" CB  GLN A   5 "',
  #                     'pdb=" CD  GLN A   5 "', 'pdb=" NE2 GLN A   5 "']), \
  #  pdb_strs
  fc_map = fmodel.map_coefficients(map_type="Fc").fft_map(
    resolution_factor=0.25).apply_sigma_scaling().real_map_unpadded()
  assert (map_stats.number_of_atoms_below_fofc_map_level() == 3)
  assert (map_stats.fraction_of_nearby_grid_points_above_cutoff() > 0.0)
  stats = building.get_model_map_stats(
    selection=sele_all,
    target_map=two_fofc_map,
    model_map=fc_map,
    unit_cell=fmodel.xray_structure.unit_cell(),
    sites_cart=fmodel.xray_structure.sites_cart(),
    pdb_atoms=hierarchy.atoms())
    def __init__(
        self,
        hierarchy=None,
        # XXX warning, ncs_phil_groups can be changed inside...
        ncs_phil_groups=None,
        params=None,
        log=None,
    ):
        """
    TODO:
    1. Transfer get_ncs_info_as_spec() to ncs/ncs.py:ncs

    Select method to build ncs_group_object

    order of implementation:
    1) ncs_phil_groups - user-supplied definitions are filtered
    2) hierarchy only - Performing NCS search

    Args:
    -----
      ncs_phil_groups:  iotbx.phil.parse(ncs_group_phil_str).extract().ncs_group
      chain_max_rmsd (float): limit of rms difference between chains to be considered
        as copies
      min_percent (float): Threshold for similarity between chains
        similarity define as:
        (number of matching res) / (number of res in longer chain)
      chain_similarity_threshold (float): min similarity between matching chains
      residue_match_radius (float): max allow distance difference between pairs of matching
        atoms of two residues
    """

        self.number_of_ncs_groups = 0  # consider removing/replacing with function

        self.ncs_restraints_group_list = class_ncs_restraints_group_list()
        # keep hierarchy for writing (To have a source of atoms labels)
        self.hierarchy = hierarchy
        # residues common to NCS copies. Used for .spec representation
        self.common_res_dict = {}
        # Collect messages, recommendation and errors
        self.messages = ''  # Not used outside...
        self.old_i_seqs = None
        self.original_hierarchy = None
        self.truncated_hierarchy = None
        self.truncated_h_asc = None
        self.chains_info = None

        extension = ''
        # set search parameters
        self.params = params
        if self.params is None:
            self.params = input.get_default_params().ncs_search
        #
        if log is None:
            self.log = sys.stdout
        else:
            self.log = log

        if hierarchy:
            # for a in hierarchy.atoms():
            #   print "oo", a.i_seq, a.id_str()
            # print "====="
            hierarchy.atoms().reset_i_seq()
            self.original_hierarchy = hierarchy.deep_copy()
            self.original_hierarchy.reset_atom_i_seqs()
            if self.params.exclude_selection is not None:
                # pdb_hierarchy_inp.hierarchy.write_pdb_file("in_ncs_pre_before.pdb")
                cache = hierarchy.atom_selection_cache()
                sel = cache.selection("not (%s)" %
                                      self.params.exclude_selection)
                self.truncated_hierarchy = hierarchy.select(sel)
            else:
                # this could be to save iseqs but I'm not sure
                self.truncated_hierarchy = hierarchy.select(
                    flex.size_t_range(hierarchy.atoms_size()))
            self.old_i_seqs = self.truncated_hierarchy.atoms().extract_i_seq()
            # print "self.old_i_seqs", list(self.old_i_seqs)
            # self.truncated_hierarchy.atoms().reset_i_seq()
            self.truncated_hierarchy.reset_atom_i_seqs()
            self.truncated_h_asc = self.truncated_hierarchy.atom_selection_cache(
            )
            # self.truncated_hierarchy.write_pdb_file("in_ncs_pre_after.pdb")
            self.chains_info = ncs_search.get_chains_info(
                self.truncated_hierarchy)

            if self.truncated_hierarchy.atoms_size() == 0:
                return

        #
        # print "ncs_groups before validation", ncs_phil_groups
        validated_ncs_phil_groups = None
        validated_ncs_phil_groups = self.validate_ncs_phil_groups(
            pdb_h=self.truncated_hierarchy,
            ncs_phil_groups=ncs_phil_groups,
            asc=self.truncated_h_asc)
        if validated_ncs_phil_groups is None:
            # print "Last chance, building from hierarchy"
            self.build_ncs_obj_from_pdb_asu(pdb_h=self.truncated_hierarchy,
                                            asc=self.truncated_h_asc)

        # error handling
        if self.ncs_restraints_group_list.get_n_groups() == 0:
            print >> self.log, '========== WARNING! ============\n'
            print >> self.log, '  No NCS relation were found !!!\n'
            print >> self.log, '================================\n'
        if self.messages != '':
            print >> self.log, self.messages
 import mmtbx.utils
 from mmtbx.monomer_library import server
 import iotbx.pdb.hierarchy
 get_class = iotbx.pdb.common_residue_names_get_class
 mon_lib_srv = server.server()
 cmdline = mmtbx.utils.cmdline_load_pdb_and_data(
   args=args,
   master_phil=master_phil,
   process_pdb_file=False,
   scattering_table="n_gaussian")
 params = cmdline.params
 working_phil = master_phil.format(python_object=params)
 master_phil.fetch_diff(source=working_phil).show(out=out)
 fmodel = cmdline.fmodel
 hierarchy = cmdline.pdb_hierarchy
 sele_cache = hierarchy.atom_selection_cache()
 assert (params.selection is not None)
 selection = sele_cache.selection(params.selection)
 assert (selection.count(True) > 0)
 have_results = False
 pdb_atoms = hierarchy.atoms()
 sites_cart = pdb_atoms.extract_xyz()
 ensembles = []
 t1 = time.time()
 for chain in hierarchy.only_model().chains() :
   prev_residue = next_residue = None
   residue_groups = chain.residue_groups()
   n_groups = len(residue_groups)
   for i_res, residue_group in enumerate(residue_groups) :
     if (i_res < n_groups - 1) :
       next_residue = residue_groups[i_res+1].atom_groups()[0]
def run(args, out=None):
    if (out is None): out = sys.stdout
    from mmtbx.building.alternate_conformations import density_sampling
    import mmtbx.maps.utils
    import mmtbx.utils
    from mmtbx.monomer_library import server
    import iotbx.pdb.hierarchy
    get_class = iotbx.pdb.common_residue_names_get_class
    mon_lib_srv = server.server()
    cmdline = mmtbx.utils.cmdline_load_pdb_and_data(
        args=args,
        master_phil=master_phil,
        process_pdb_file=False,
        scattering_table="n_gaussian")
    params = cmdline.params
    working_phil = master_phil.format(python_object=params)
    master_phil.fetch_diff(source=working_phil).show(out=out)
    fmodel = cmdline.fmodel
    hierarchy = cmdline.pdb_hierarchy
    sele_cache = hierarchy.atom_selection_cache()
    assert (params.selection is not None)
    selection = sele_cache.selection(params.selection)
    assert (selection.count(True) > 0)
    have_results = False
    pdb_atoms = hierarchy.atoms()
    sites_cart = pdb_atoms.extract_xyz()
    ensembles = []
    t1 = time.time()
    for chain in hierarchy.only_model().chains():
        prev_residue = next_residue = None
        residue_groups = chain.residue_groups()
        n_groups = len(residue_groups)
        for i_res, residue_group in enumerate(residue_groups):
            if (i_res < n_groups - 1):
                next_residue = residue_groups[i_res + 1].atom_groups()[0]
            i_seqs = residue_group.atoms().extract_i_seq()
            if (selection.select(i_seqs).all_eq(True)):
                atom_group = residue_group.only_atom_group()
                if (get_class(atom_group.resname) != "common_amino_acid"):
                    continue
                confs = density_sampling.screen_residue(
                    residue=atom_group,
                    prev_residue=prev_residue,
                    next_residue=next_residue,
                    sites_cart=sites_cart,
                    fmodel=fmodel,
                    mon_lib_srv=mon_lib_srv,
                    params=params,
                    map_file_name="map_coeffs.mtz",
                    out=out)
                ensemble = []
                for conf in confs:
                    conf_atoms = conf.set_atom_sites(pdb_atoms)
                    pdb_lines = []
                    for atom in conf_atoms:
                        pdb_lines.append(atom.format_atom_record())
                    ensemble.append("\n".join(pdb_lines))
                ensembles.append(ensemble)
            prev_residue = residue_group.atom_groups()[0]
    if (len(ensembles) == 0):
        raise Sorry("No alternate conformations found.")
    t2 = time.time()
    print("search time: %.1fs" % (t2 - t1), file=out)
    for i_ens, ensemble in enumerate(ensembles):
        ensemble_hierarchy = iotbx.pdb.hierarchy.root()
        for k, model_str in enumerate(ensemble):
            input = iotbx.pdb.hierarchy.input(pdb_string=model_str)
            model = input.hierarchy.only_model().detached_copy()
            model.id = str(k + 1)
            ensemble_hierarchy.append_model(model)
        f = open("ensemble_%d.pdb" % (i_ens + 1), "w")
        f.write(ensemble_hierarchy.as_pdb_string())
        f.close()
        print("wrote ensemble_%d.pdb" % (i_ens + 1))
    if (params.coot):
        easy_run.call(
            "coot --pdb %s --auto map_coeffs.mtz --pdb ensemble.pdb" %
            params.input.pdb.file_name[0])
def run (args, out=None ):
  if (out is None) : out = sys.stdout
  from mmtbx.building.alternate_conformations import density_sampling
  import mmtbx.maps.utils
  import mmtbx.utils
  from mmtbx.monomer_library import server
  import iotbx.pdb.hierarchy
  get_class = iotbx.pdb.common_residue_names_get_class
  mon_lib_srv = server.server()
  cmdline = mmtbx.utils.cmdline_load_pdb_and_data(
    args=args,
    master_phil=master_phil,
    process_pdb_file=False,
    scattering_table="n_gaussian")
  params = cmdline.params
  working_phil = master_phil.format(python_object=params)
  master_phil.fetch_diff(source=working_phil).show(out=out)
  fmodel = cmdline.fmodel
  hierarchy = cmdline.pdb_hierarchy
  sele_cache = hierarchy.atom_selection_cache()
  assert (params.selection is not None)
  selection = sele_cache.selection(params.selection)
  assert (selection.count(True) > 0)
  have_results = False
  pdb_atoms = hierarchy.atoms()
  sites_cart = pdb_atoms.extract_xyz()
  ensembles = []
  t1 = time.time()
  for chain in hierarchy.only_model().chains() :
    prev_residue = next_residue = None
    residue_groups = chain.residue_groups()
    n_groups = len(residue_groups)
    for i_res, residue_group in enumerate(residue_groups) :
      if (i_res < n_groups - 1) :
        next_residue = residue_groups[i_res+1].atom_groups()[0]
      i_seqs = residue_group.atoms().extract_i_seq()
      if (selection.select(i_seqs).all_eq(True)) :
        atom_group = residue_group.only_atom_group()
        if (get_class(atom_group.resname) != "common_amino_acid") :
          continue
        confs = density_sampling.screen_residue(
          residue=atom_group,
          prev_residue=prev_residue,
          next_residue=next_residue,
          sites_cart=sites_cart,
          fmodel=fmodel,
          mon_lib_srv=mon_lib_srv,
          params=params,
          map_file_name="map_coeffs.mtz",
          out=out)
        ensemble = []
        for conf in confs :
          conf_atoms = conf.set_atom_sites(pdb_atoms)
          pdb_lines = []
          for atom in conf_atoms :
            pdb_lines.append(atom.format_atom_record())
          ensemble.append("\n".join(pdb_lines))
        ensembles.append(ensemble)
      prev_residue = residue_group.atom_groups()[0]
  if (len(ensembles) == 0) :
    raise Sorry("No alternate conformations found.")
  t2 = time.time()
  print >> out, "search time: %.1fs" % (t2-t1)
  for i_ens, ensemble in enumerate(ensembles) :
    ensemble_hierarchy = iotbx.pdb.hierarchy.root()
    for k, model_str in enumerate(ensemble) :
      input = iotbx.pdb.hierarchy.input(pdb_string=model_str)
      model = input.hierarchy.only_model().detached_copy()
      model.id = str(k+1)
      ensemble_hierarchy.append_model(model)
    f = open("ensemble_%d.pdb" % (i_ens+1), "w")
    f.write(ensemble_hierarchy.as_pdb_string())
    f.close()
    print "wrote ensemble_%d.pdb" % (i_ens+1)
  if (params.coot) :
    easy_run.call("coot --pdb %s --auto map_coeffs.mtz --pdb ensemble.pdb" %
      params.input.pdb.file_name[0])