Ejemplo n.º 1
0
    def set_rotamer_to_reference(self,
                                 xray_structure,
                                 mon_lib_srv=None,
                                 log=None,
                                 quiet=False):
        if self.mon_lib_srv is None:
            self.mon_lib_srv = mon_lib_srv
        assert isinstance(self.mon_lib_srv,
                          mmtbx.monomer_library.server.server)
        if (log is None): log = sys.stdout
        make_sub_header("Correcting rotamer outliers to match reference model",
                        out=log)
        sa = SidechainAngles(False)
        r = rotalyze.rotalyze(pdb_hierarchy=self.pdb_hierarchy)
        rot_list_reference = {}
        coot_reference = {}
        for key in self.pdb_hierarchy_ref.keys():
            hierarchy = self.pdb_hierarchy_ref[key]
            rot_list_reference[key] = \
              rotalyze.rotalyze(pdb_hierarchy=hierarchy)
        model_hash = {}
        model_chis = {}
        reference_hash = {}
        reference_chis = {}
        model_outliers = 0
        for rot in r.results:
            model_hash[rot.id_str()] = rot.rotamer_name
            if rot.rotamer_name == "OUTLIER":
                model_outliers += 1

        for key in rot_list_reference.keys():
            reference_hash[key] = {}
            for rot in rot_list_reference[key].results:
                reference_hash[key][rot.id_str()] = rot.rotamer_name

        print >> log, "** evaluating rotamers for working model **"
        for model in self.pdb_hierarchy.models():
            for chain in model.chains():
                for residue_group in chain.residue_groups():
                    all_dict = rotalyze.construct_complete_sidechain(
                        residue_group)
                    for atom_group in residue_group.atom_groups():
                        try:
                            atom_dict = all_dict.get(atom_group.altloc)
                            chis = sa.measureChiAngles(atom_group, atom_dict)
                            if chis is not None:
                                key = utils.id_str(chain_id=chain.id,
                                                   resseq=residue_group.resseq,
                                                   resname=atom_group.resname,
                                                   icode=residue_group.icode,
                                                   altloc=atom_group.altloc)
                                model_chis[key] = chis
                        except Exception:
                            print >> log, \
                              '  %s%5s %s is missing some sidechain atoms, **skipping**' % (
                                  chain.id, residue_group.resid(),
                                  atom_group.altloc+atom_group.resname)
        if model_outliers == 0:
            print >> log, "No rotamer outliers detected in working model"
            return
        else:
            print >> log, "Number of rotamer outliers: %d" % model_outliers

        print >> log, "\n** evaluating rotamers for reference model **"
        for file in self.pdb_hierarchy_ref.keys():
            hierarchy = self.pdb_hierarchy_ref[file]
            reference_chis[file] = {}
            for model in hierarchy.models():
                for chain in model.chains():
                    for residue_group in chain.residue_groups():
                        all_dict = rotalyze.construct_complete_sidechain(
                            residue_group)
                        for atom_group in residue_group.atom_groups():
                            try:
                                atom_dict = all_dict.get(atom_group.altloc)
                                chis = sa.measureChiAngles(
                                    atom_group, atom_dict)
                                if chis is not None:
                                    key = utils.id_str(
                                        chain_id=chain.id,
                                        resseq=residue_group.resseq,
                                        resname=atom_group.resname,
                                        icode=residue_group.icode,
                                        altloc=atom_group.altloc)
                                    reference_chis[file][key] = chis
                            except Exception:
                                print >> log, \
                                  '  %s%5s %s is missing some sidechain atoms, **skipping**' % (
                                      chain.id, residue_group.resid(),
                                      atom_group.altloc+atom_group.resname)

        print >> log, "\n** fixing outliers **"
        sites_cart_start = xray_structure.sites_cart()
        for model in self.pdb_hierarchy.models():
            for chain in model.chains():
                for residue_group in chain.residue_groups():
                    if len(residue_group.conformers()) > 1:
                        print >> log, "  %s%5s %s has multiple conformations, **skipping**" % (
                            chain.id, residue_group.resid(),
                            " " + residue_group.atom_groups()[0].resname)
                        continue
                    for conformer in residue_group.conformers():
                        for residue in conformer.residues():
                            if residue.resname == "PRO":
                                continue
                            key = utils.id_str(
                                chain_id=chain.id,
                                resseq=residue_group.resseq,
                                resname=residue_group.atom_groups()[0].resname,
                                icode=residue_group.icode,
                                altloc=conformer.altloc)
                            if len(chain.id) == 1:
                                chain_id = " " + chain.id
                            else:
                                chain_id = chain.id
                            file_key = '%s%s%s' % (residue.resname, chain_id,
                                                   residue_group.resid())
                            file_key = file_key.strip()
                            file_match = self.residue_match_hash.get(file_key)
                            if file_match is not None:
                                file = file_match[0]
                            else:
                                continue
                            model_rot = model_hash.get(key)
                            reference_rot = reference_hash[file].get(
                                self.one_key_to_another(file_match[1]))
                            m_chis = model_chis.get(key)
                            r_chis = reference_chis[file].get(
                                self.one_key_to_another(file_match[1]))
                            if model_rot is not None and reference_rot is not None and \
                                m_chis is not None and r_chis is not None:
                                if (model_rot == 'OUTLIER' and \
                                    reference_rot != 'OUTLIER'): # or \
                                    #atom_group.resname in ["LEU", "VAL", "THR"]:
                                    self.change_residue_rotamer_in_place(
                                        sites_cart_start, residue, m_chis,
                                        r_chis, self.mon_lib_srv)
                                    xray_structure.set_sites_cart(
                                        sites_cart_start)

                                elif self.params.strict_rotamer_matching and \
                                  (model_rot != 'OUTLIER' and reference_rot != 'OUTLIER'):
                                    if model_rot != reference_rot:
                                        self.change_residue_rotamer_in_place(
                                            sites_cart_start, residue, m_chis,
                                            r_chis, self.mon_lib_srv)
                                        xray_structure.set_sites_cart(
                                            sites_cart_start)
Ejemplo n.º 2
0
  def set_rotamer_to_reference(self,
                               xray_structure,
                               mon_lib_srv=None,
                               log=None,
                               quiet=False):
    if self.mon_lib_srv is None:
      self.mon_lib_srv = mon_lib_srv
    assert isinstance(self.mon_lib_srv, mmtbx.monomer_library.server.server)
    if(log is None): log = sys.stdout
    make_sub_header(
      "Correcting rotamer outliers to match reference model",
      out=log)
    sa = SidechainAngles(False)
    r = rotalyze.rotalyze(pdb_hierarchy=self.pdb_hierarchy)
    rot_list_reference = {}
    coot_reference = {}
    for key in self.pdb_hierarchy_ref.keys():
      hierarchy = self.pdb_hierarchy_ref[key]
      rot_list_reference[key] = \
        rotalyze.rotalyze(pdb_hierarchy=hierarchy)
    model_hash = {}
    model_chis = {}
    reference_hash = {}
    reference_chis = {}
    model_outliers = 0
    for rot in r.results:
      model_hash[rot.id_str()] = rot.rotamer_name
      if rot.rotamer_name == "OUTLIER":
        model_outliers += 1

    for key in rot_list_reference.keys():
      reference_hash[key] = {}
      for rot in rot_list_reference[key].results:
        reference_hash[key][rot.id_str()] = rot.rotamer_name

    print >> log, "** evaluating rotamers for working model **"
    for model in self.pdb_hierarchy.models():
      for chain in model.chains():
        for residue_group in chain.residue_groups():
            all_dict = rotalyze.construct_complete_sidechain(residue_group)
            for atom_group in residue_group.atom_groups():
              try:
                atom_dict = all_dict.get(atom_group.altloc)
                chis = sa.measureChiAngles(atom_group, atom_dict)
                if chis is not None:
                  key = utils.id_str(
                          chain_id=chain.id,
                          resseq=residue_group.resseq,
                          resname=atom_group.resname,
                          icode=residue_group.icode,
                          altloc=atom_group.altloc)
                  model_chis[key] = chis
              except Exception:
                print >> log, \
                  '  %s%5s %s is missing some sidechain atoms, **skipping**' % (
                      chain.id, residue_group.resid(),
                      atom_group.altloc+atom_group.resname)
    if model_outliers == 0:
      print >> log, "No rotamer outliers detected in working model"
      return
    else:
      print >> log, "Number of rotamer outliers: %d" % model_outliers

    print >> log, "\n** evaluating rotamers for reference model **"
    for file in self.pdb_hierarchy_ref.keys():
      hierarchy = self.pdb_hierarchy_ref[file]
      reference_chis[file] = {}
      for model in hierarchy.models():
        for chain in model.chains():
          for residue_group in chain.residue_groups():
              all_dict = rotalyze.construct_complete_sidechain(residue_group)
              for atom_group in residue_group.atom_groups():
                try:
                  atom_dict = all_dict.get(atom_group.altloc)
                  chis = sa.measureChiAngles(atom_group, atom_dict)
                  if chis is not None:
                    key = utils.id_str(
                            chain_id=chain.id,
                            resseq=residue_group.resseq,
                            resname=atom_group.resname,
                            icode=residue_group.icode,
                            altloc=atom_group.altloc)
                    reference_chis[file][key] = chis
                except Exception:
                  print >> log, \
                    '  %s%5s %s is missing some sidechain atoms, **skipping**' % (
                        chain.id, residue_group.resid(),
                        atom_group.altloc+atom_group.resname)

    print >> log, "\n** fixing outliers **"
    sites_cart_start = xray_structure.sites_cart()
    for model in self.pdb_hierarchy.models():
      for chain in model.chains():
        for residue_group in chain.residue_groups():
          if len(residue_group.conformers()) > 1:
            print >> log, "  %s%5s %s has multiple conformations, **skipping**" % (
              chain.id, residue_group.resid(),
              " "+residue_group.atom_groups()[0].resname)
            continue
          for conformer in residue_group.conformers():
            for residue in conformer.residues():
              if residue.resname == "PRO":
                continue
              key = utils.id_str(
                      chain_id=chain.id,
                      resseq=residue_group.resseq,
                      resname=residue_group.atom_groups()[0].resname,
                      icode=residue_group.icode,
                      altloc=conformer.altloc)
              if len(chain.id) == 1:
                chain_id = " "+chain.id
              else:
                chain_id = chain.id
              file_key = '%s%s%s' %(residue.resname,
                                    chain_id,
                                    residue_group.resid())
              file_key = file_key.strip()
              file_match = self.residue_match_hash.get(file_key)
              if file_match is not None:
                file = file_match[0]
              else:
                continue
              model_rot = model_hash.get(key)
              reference_rot = reference_hash[file].get(self.one_key_to_another(file_match[1]))
              m_chis = model_chis.get(key)
              r_chis = reference_chis[file].get(self.one_key_to_another(file_match[1]))
              if model_rot is not None and reference_rot is not None and \
                  m_chis is not None and r_chis is not None:
                if (model_rot == 'OUTLIER' and \
                    reference_rot != 'OUTLIER'): # or \
                    #atom_group.resname in ["LEU", "VAL", "THR"]:
                  self.change_residue_rotamer_in_place(
                      sites_cart_start,residue, m_chis,r_chis,self.mon_lib_srv)
                  xray_structure.set_sites_cart(sites_cart_start)

                elif self.params.strict_rotamer_matching and \
                  (model_rot != 'OUTLIER' and reference_rot != 'OUTLIER'):
                  if model_rot != reference_rot:
                    self.change_residue_rotamer_in_place(
                        sites_cart_start,residue, m_chis,r_chis,self.mon_lib_srv)
                    xray_structure.set_sites_cart(sites_cart_start)