def exercise_lbfgs_simple (mon_lib_srv, ener_lib, verbose=False) :
  # three peptides:
  #  1 = poly-ALA, favored
  #  2 = poly-ALA, outlier
  #  3 = poly-TRP, outlier
  #
  # Note that the ramalyze score for the first actually gets slightly worse,
  # but it's still good and we're starting from an excellent score anyway.
  #
  # residuals = [0.00024512, 307.616444, 294.913714]
  residuals = [0.00168766995882, 186.24718562, 177.259069807]
  for i, peptide in enumerate([pdb1, pdb2, pdb3]) :
    pdb_in = iotbx.pdb.input(source_info="peptide",
      lines=flex.split_lines(peptide))
    params = pdb_interpretation.master_params.extract()
    processed_pdb_file = pdb_interpretation.process(
      mon_lib_srv=mon_lib_srv,
      ener_lib=ener_lib,
      params=params,
      pdb_inp=pdb_in,
      log=StringIO())
    log = StringIO()
    pdb_hierarchy = processed_pdb_file.all_chain_proxies.pdb_hierarchy
    atoms = pdb_hierarchy.atoms()
    sites_cart_1 = atoms.extract_xyz().deep_copy()
    gradients_fd = flex.vec3_double(sites_cart_1.size(), (0,0,0))
    gradients_an = flex.vec3_double(sites_cart_1.size(), (0,0,0))
    params = ramachandran.master_phil.fetch().extract()
    rama_manager = ramachandran.ramachandran_manager(
        pdb_hierarchy, None, params, log)
    assert rama_manager.get_n_proxies() == 1
    residual_an = rama_manager.target_and_gradients(
      unit_cell=None,
      sites_cart=sites_cart_1,
      gradient_array=gradients_an)
    # print "comparing", residual_an
    assert approx_equal(residual_an, residuals[i], eps=0.00001)
  if verbose :
    print ""
  for i, peptide in enumerate([pdb1, pdb2, pdb3]) :
    pdb_in = iotbx.pdb.input(source_info="peptide",
      lines=flex.split_lines(peptide))
    o = benchmark_structure(pdb_in, mon_lib_srv, ener_lib, verbose)
    phi0, psi0 = o.r0.results[0].phi, o.r0.results[0].psi
    phi1, psi1 = o.r1.results[0].phi, o.r1.results[0].psi
    phi2, psi2 = o.r2.results[0].phi, o.r2.results[0].psi
    r0 = o.r0.results[0].score
    r1 = o.r1.results[0].score
    r2 = o.r2.results[0].score
    if verbose :
      print "peptide %d" % (i+1)
      print " before: rmsd_bonds=%-6.4f rmsd_angles=%-6.3f" % (o.b0,o.a0)
      print "         phi=%-6.1f psi=%-6.1f score=%-.2f" % (phi0, psi0, r0)
      print " simple: rmsd_bonds=%-6.4f rmsd_angles=%-6.3f" % (o.b1,o.a1)
      print "         phi=%-6.1f psi=%-6.1f score=%-.2f" % (phi1, psi1, r1)
      print " + Rama: rmsd_bonds=%-6.4f rmsd_angles=%-6.3f" % (o.b2,o.a2)
      print "         phi=%-6.1f psi=%-6.1f score=%-.2f" % (phi2, psi2, r2)
      print ""
Exemple #2
0
def exercise_lbfgs_simple(mon_lib_srv, ener_lib, verbose=False):
    # three peptides:
    #  1 = poly-ALA, favored
    #  2 = poly-ALA, outlier
    #  3 = poly-TRP, outlier
    #
    # Note that the ramalyze score for the first actually gets slightly worse,
    # but it's still good and we're starting from an excellent score anyway.
    #
    residuals = [0.00168766995882, 170.84797160, 161.5214609]
    for i, peptide in enumerate([pdb1, pdb2, pdb3]):
        pdb_in = iotbx.pdb.input(source_info="peptide",
                                 lines=flex.split_lines(peptide))
        log = StringIO()
        pdb_hierarchy = pdb_in.construct_hierarchy()
        atoms = pdb_hierarchy.atoms()
        sites_cart_1 = atoms.extract_xyz().deep_copy()
        gradients_fd = flex.vec3_double(sites_cart_1.size(), (0, 0, 0))
        gradients_an = flex.vec3_double(sites_cart_1.size(), (0, 0, 0))
        params = ramachandran.master_phil.fetch().extract(
        ).ramachandran_plot_restraints
        rama_manager = ramachandran.ramachandran_manager(
            pdb_hierarchy, params, log)
        assert rama_manager.get_n_proxies() == 1
        residual_an = rama_manager.target_and_gradients(
            unit_cell=None,
            sites_cart=sites_cart_1,
            gradient_array=gradients_an)
        # print "comparing", residual_an
        assert approx_equal(residual_an, residuals[i], eps=0.001)
        # approx_equal(residual_an, residuals[i], eps=0.001)
    if verbose:
        print("")
    for i, peptide in enumerate([pdb1, pdb2, pdb3]):
        pdb_in = iotbx.pdb.input(source_info="peptide",
                                 lines=flex.split_lines(peptide))
        o = benchmark_structure(pdb_in, mon_lib_srv, ener_lib, verbose)
        phi0, psi0 = o.r0.results[0].phi, o.r0.results[0].psi
        phi1, psi1 = o.r1.results[0].phi, o.r1.results[0].psi
        phi2, psi2 = o.r2.results[0].phi, o.r2.results[0].psi
        r0 = o.r0.results[0].score
        r1 = o.r1.results[0].score
        r2 = o.r2.results[0].score
        if verbose:
            print("peptide %d" % (i + 1))
            print(" before: rmsd_bonds=%-6.4f rmsd_angles=%-6.3f" %
                  (o.b0, o.a0))
            print("         phi=%-6.1f psi=%-6.1f score=%-.2f" %
                  (phi0, psi0, r0))
            print(" simple: rmsd_bonds=%-6.4f rmsd_angles=%-6.3f" %
                  (o.b1, o.a1))
            print("         phi=%-6.1f psi=%-6.1f score=%-.2f" %
                  (phi1, psi1, r1))
            print(" + Rama: rmsd_bonds=%-6.4f rmsd_angles=%-6.3f" %
                  (o.b2, o.a2))
            print("         phi=%-6.1f psi=%-6.1f score=%-.2f" %
                  (phi2, psi2, r2))
            print("")
    def gen_null_peak(self):
        record = self.pput.write_atom(0, "O", "", "NUL", "P", 0, "", 999.99,
                                      999.99, 999.99, 0.0, 30.0, "O", "")

        tmp_hier = iotbx.pdb.input(source_info=None,
                                   lines=flex.split_lines(record))
        pdb_str = tmp_hier.as_pdb_string(crystal_symmetry=self.orig_symmetry)
        null_input = iotbx.pdb.input(source_info=None,
                                     lines=flex.split_lines(pdb_str))
        null_hier = null_input.construct_hierarchy()
        return null_hier
def exercise_phi_psi_extraction():
    for n_prox, raw_records in [
        ([0, 0], rec_1_residue),
        ([0, 0], rec_2_residues),
        ([4, 2], rec_3_residues),
        ([6, 4], rec_4_residues),
        ([0, 0], rec_2_chains),
        ([0, 0], rec_2_segids),
        ([8, 4], rec_2_acs_edge),
        ([8, 4], rec_2_acs_middle),
        ([6, 4], rec_4_residues_isertions),
        ([12, 10], pdb_1yjp),
        ([8, 4], pdb_1yjp_minus_4),
        ([4, 2], rec_3_res_ac_h),
        ([8, 4], rec_2_acs_middle_one_atom_1),
        ([8, 4], rec_2_acs_middle_one_atom_2),
        ([8, 4], rec_2_acs_middle_one_atom_3),
    ]:
        tmp_hierarchy = iotbx.pdb.input(
            source_info=None,
            lines=flex.split_lines(raw_records)).construct_hierarchy()
        for opp in range(2):
            proxies = []
            for three in generate_protein_threes(hierarchy=tmp_hierarchy,
                                                 geometry=None):
                ppp = three.get_dummy_dihedral_proxies(only_psi_phi_pairs=opp)
                print(three, 'ppp', len(ppp))
                proxies.extend(ppp)
            print(len(proxies), n_prox)
            assert len(proxies) == n_prox[opp], \
               "Expected %d, got %d" % (
                 n_prox[opp],
                 len(proxies),
                 )
Exemple #5
0
def extract_unique_part_of_hierarchy(ph,target_ph=None,
    allow_mismatch_in_number_of_copies=True,
    allow_extensions=False,
    min_similarity=1.0,out=sys.stdout):

  # Container for unique chains:

  new_hierarchy=iotbx.pdb.input(
    source_info="Model",lines=flex.split_lines("")).construct_hierarchy()
  mm=iotbx.pdb.hierarchy.model()
  new_hierarchy.append_model(mm)

  # Target location:

  if target_ph:
    target_centroid_list=flex.vec3_double()
    for model in target_ph.models()[:1]:
      target_centroid_list.append(model.atoms().extract_xyz().mean())
  else:
    target_centroid_list=None

  # Get unique set of sequences
  # Also save all the chains associated with each one

  sequences=[]
  chains=[]
  for model in ph.models()[:1]:
    for chain in model.chains():
      try:
        seq=chain.as_padded_sequence()  # has XXX for missing residues
        seq=seq.replace("X","")
        sequences.append(seq)
        chains.append(chain)
      except Exception, e:
        pass
Exemple #6
0
def extract_unique_part_of_hierarchy(ph,target_ph=None,out=sys.stdout):
  new_hierarchy=iotbx.pdb.input(
    source_info="Model",lines=flex.split_lines("")).construct_hierarchy()
  mm=iotbx.pdb.hierarchy.model()
  new_hierarchy.append_model(mm)

  if target_ph:
    target_centroid_list=flex.vec3_double()
    for model in target_ph.models()[:1]:
      target_centroid_list.append(model.atoms().extract_xyz().mean())
  else:
    target_centroid_list=None
  unique_sequences=[]
  best_chain_dict={}
  best_chain_dist_dict={}
  for model in ph.models()[:1]:
    for chain in model.chains():
      try:
        seq=chain.as_padded_sequence()  # has XXX for missing residues
      except Exception, e:
        seq="XXX"
      if not seq in unique_sequences:
        unique_sequences.append(seq)
      if target_centroid_list:
        xx=flex.vec3_double()
        xx.append(chain.atoms().extract_xyz().mean())
        dist=xx.min_distance_between_any_pair(target_centroid_list)
      else:
        dist=0.
      best_dist=best_chain_dist_dict.get(seq)
      if best_dist is None or dist<best_dist:
        best_chain_dist_dict[seq]=dist
        best_chain_dict[seq]=chain
def exercise_phi_psi_extraction():
  for n_prox, raw_records in [
      ([0, 0], rec_1_residue),
      ([0, 0], rec_2_residues),
      ([4, 2], rec_3_residues),
      ([6, 4], rec_4_residues),
      ([0, 0], rec_2_chains),
      ([0, 0], rec_2_segids),
      ([8, 4], rec_2_acs_edge),
      ([8, 4], rec_2_acs_middle),
      ([6, 4], rec_4_residues_isertions),
      ([12, 10], pdb_1yjp),
      ([8, 4], pdb_1yjp_minus_4),
      ([4, 2], rec_3_res_ac_h),
      ([8, 4], rec_2_acs_middle_one_atom_1),
      ([8, 4], rec_2_acs_middle_one_atom_2),
      ([8, 4], rec_2_acs_middle_one_atom_3),
      ]:
    tmp_hierarchy = iotbx.pdb.input(
      source_info=None,
      lines=flex.split_lines(raw_records)).construct_hierarchy()
    for opp in range(2):
      proxies = []
      for three in generate_protein_threes(
          hierarchy=tmp_hierarchy,
          geometry=None):
        ppp = three.get_dummy_dihedral_proxies(only_psi_phi_pairs=opp)
        print three,'ppp',len(ppp)
        proxies.extend(ppp)
      print len(proxies), n_prox
      assert len(proxies) == n_prox[opp], \
         "Expected %d, got %d" % (
           n_prox[opp],
           len(proxies),
           )
def remove_ter(text): # remove blank lines and TER records
  new_lines=[]
  for line in flex.split_lines(text):
    if not line.replace(" ",""): continue
    if line.startswith("TER"): continue
    new_lines.append(line)
  return "\n".join(new_lines)
Exemple #9
0
def remove_ter(text): # remove blank lines and TER records
  new_lines=[]
  for line in flex.split_lines(text):
    if not line.replace(" ",""): continue
    if line.startswith("TER"): continue
    new_lines.append(line)
  return "\n".join(new_lines)
Exemple #10
0
def get_pdb_inp(text=None,file_name=None,source_info="string"):
  import iotbx.pdb
  if file_name:
    text=open(file_name).read()
    source_info="file %s" %(file_name)
  elif not text:
    text=""
  from cctbx.array_family import flex
  return iotbx.pdb.input(source_info=source_info,
       lines=flex.split_lines(text))
  def get_grm(self):
    # first make whole grm using self.whole_pdb_h
    params_line = grand_master_phil_str
    params = iotbx.phil.parse(
        input_string=params_line, process_includes=True).extract()
    params.pdb_interpretation.clash_guard.nonbonded_distance_threshold=None
    params.pdb_interpretation.peptide_link.ramachandran_restraints = True
    params.pdb_interpretation.peptide_link.oldfield.weight_scale=3
    params.pdb_interpretation.peptide_link.oldfield.plot_cutoff=0.03
    params.pdb_interpretation.nonbonded_weight = 500
    params.pdb_interpretation.c_beta_restraints=True
    params.pdb_interpretation.max_reasonable_bond_distance = None
    params.pdb_interpretation.peptide_link.apply_peptide_plane = True
    params.pdb_interpretation.ncs_search.enabled = True
    params.pdb_interpretation.restraints_library.rdl = True
    processed_pdb_files_srv = mmtbx.utils.\
        process_pdb_file_srv(
            crystal_symmetry= self.whole_xrs.crystal_symmetry(),
            pdb_interpretation_params = params.pdb_interpretation,
            stop_for_unknowns         = False,
            log=self.log,
            cif_objects=None)
    processed_pdb_file, junk = processed_pdb_files_srv.\
        process_pdb_files(raw_records=flex.split_lines(self.whole_pdb_h.as_pdb_string()))

    self.mon_lib_srv = processed_pdb_files_srv.mon_lib_srv
    self.ener_lib = processed_pdb_files_srv.ener_lib
    self.rotamer_manager = RotamerEval(mon_lib_srv=self.mon_lib_srv)

    self.whole_grm = get_geometry_restraints_manager(
        processed_pdb_file, self.whole_xrs, params=params)

    # set SS restratins
    if self.params.use_ss_restraints:
      ss_manager = manager(
          pdb_hierarchy=self.whole_pdb_h,
          geometry_restraints_manager=self.whole_grm.geometry,
          sec_str_from_pdb_file=self.filtered_whole_ann,
          params=None,
          mon_lib_srv=self.mon_lib_srv,
          verbose=-1,
          log=self.log)
      # self.whole_pdb_h.write_pdb_file(file_name="for_ss.pdb")
      self.whole_pdb_h.reset_atom_i_seqs()
      self.whole_grm.geometry.set_secondary_structure_restraints(
          ss_manager=ss_manager,
          hierarchy=self.whole_pdb_h,
          log=self.log)

    # now select part of it for working with master hierarchy
    if self.using_ncs:
      self.master_grm = self.whole_grm.select(self.master_sel)
      self.working_grm = self.master_grm
    else:
      self.working_grm = self.whole_grm
Exemple #12
0
 def rotations(self, peak_object, ref_object, write_pdb=False):
     cc_f_sum, cc_f_sq_sum, cc_2_sum, cc_2_sq_sum = 0.0, 0.0, 0.0, 0.0
     for axis in (1, 2, 3, 4):
         rotated_fofc = self.rotate_so4(self.features['so4_fofc_ref_hier'],
                                        axis, 60)
         rotated_2fofc = self.rotate_so4(
             self.features['so4_2fofc_ref_hier'], axis, 60)
         if write_pdb:
             rotated_fofc.write_pdb_file(self.features['db_id'] +
                                         "_rot60_f" + str(axis) + ".pdb")
             rotated_2fofc.write_pdb_file(self.features['db_id'] +
                                          "_rot60_2" + str(axis) + ".pdb")
         #hack to get this back into cctbx?
         rotf_pdb = iotbx.pdb.input(source_info=None,
                                    lines=flex.split_lines(
                                        rotated_fofc.as_pdb_string()))
         rot2f_pdb = iotbx.pdb.input(source_info=None,
                                     lines=flex.split_lines(
                                         rotated_2fofc.as_pdb_string()))
         rotf_xrs = rotf_pdb.xray_structure_simple(
             crystal_symmetry=peak_object.symmetry)
         rot2f_xrs = rot2f_pdb.xray_structure_simple(
             crystal_symmetry=peak_object.symmetry)
         cc60_f = self.rscc(rotated_fofc, rotf_xrs, "resname SO4",
                            ref_object.fofc_map_data)
         cc60_2 = self.rscc(rotated_2fofc, rot2f_xrs, "resname SO4",
                            ref_object.twofofc_map_data)
         cc_f_sum = cc_f_sum + cc60_f
         cc_f_sq_sum = cc_f_sq_sum + cc60_f**2
         cc_2_sum = cc_2_sum + cc60_2
         cc_2_sq_sum = cc_2_sq_sum + cc60_2**2
     meanf = cc_f_sum / 4.0
     mean2 = cc_2_sum / 4.0
     self.features[
         'so4_fofc_mean_cc60'] = meanf  #average cc for all 4 rotations
     self.features['so4_2fofc_mean_cc60'] = mean2
     self.features['so4_fofc_stdev_cc60'] = np.sqrt(cc_f_sq_sum / 4.0 -
                                                    meanf**2)  #std
     self.features['so4_2fofc_stdev_cc60'] = np.sqrt(cc_2_sq_sum / 4.0 -
                                                     mean2**2)
    def place_so4(self, b_fac=35.0, occ=1.00):
        #places a sulfate at the origin of a 10A cubic P1 cell

        pdb_string="CRYST1%9.3f%9.3f%9.3f  90.00  90.00  90.00 P 1            1\n" % \
            (2.0*self.bound,2.0*self.bound,2.0*self.bound)
        x1, y1, z1 = 5.0, 5.0, 5.0
        x2, y2, z2 = x1 + 0.873, y1 + 0.873, z1 + 0.873
        x3, y3, z3 = x1 - 0.873, y1 - 0.873, z1 + 0.873
        x4, y4, z4 = x1 - 0.873, y1 + 0.873, z1 - 0.873
        x5, y5, z5 = x1 + 0.873, y1 - 0.873, z1 - 0.873
        so4_dict = {
            "sx": x1,
            "sy": y1,
            "sz": z1,
            "o1x": x2,
            "o1y": y2,
            "o1z": z2,
            "o2x": x3,
            "o2y": y3,
            "o2z": z3,
            "o3x": x4,
            "o3y": y4,
            "o3z": z4,
            "o4x": x5,
            "o4y": y5,
            "o4z": z5
        }
        pdb_entry = ""
        pdb_entry = pdb_entry + self.write_atom(
            1, "S", "", "SO4", "X", 1, "", so4_dict['sx'], so4_dict['sy'],
            so4_dict['sz'], occ, b_fac, "S", "")
        pdb_entry = pdb_entry + self.write_atom(
            2, "O1", "", "SO4", "X", 1, "", so4_dict['o1x'], so4_dict['o1y'],
            so4_dict['o1z'], occ, b_fac, "O", "")
        pdb_entry = pdb_entry + self.write_atom(
            3, "O2", "", "SO4", "X", 1, "", so4_dict['o2x'], so4_dict['o2y'],
            so4_dict['o2z'], occ, b_fac, "O", "")
        pdb_entry = pdb_entry + self.write_atom(
            4, "O3", "", "SO4", "X", 1, "", so4_dict['o3x'], so4_dict['o3y'],
            so4_dict['o3z'], occ, b_fac, "O", "")
        pdb_entry = pdb_entry + self.write_atom(
            5, "O4", "", "SO4", "X", 1, "", so4_dict['o4x'], so4_dict['o4y'],
            so4_dict['o4z'], occ, b_fac, "O", "")
        sulfate_pdb = pdb_string + pdb_entry
        std_so4_pdb = iotbx.pdb.input(source_info=None,
                                      lines=flex.split_lines(sulfate_pdb))
        local_sym = std_so4_pdb.crystal_symmetry()
        std_so4_hier = std_so4_pdb.construct_hierarchy(set_atom_i_seq=True)
        std_so4_xrs = std_so4_hier.extract_xray_structure(
            crystal_symmetry=local_sym)
        return std_so4_pdb, std_so4_hier, std_so4_xrs
def minimize_hierarchy(hierarchy, xrs, original_pdb_h, excl_string_selection, log=None):
    from mmtbx.monomer_library.pdb_interpretation import grand_master_phil_str
    from mmtbx.refinement.geometry_minimization import run2
    from mmtbx.geometry_restraints import reference

    if log is None:
        log = null_out()
    params_line = grand_master_phil_str
    params = iotbx.phil.parse(input_string=params_line, process_includes=True).extract()
    params.pdb_interpretation.clash_guard.nonbonded_distance_threshold = None
    params.pdb_interpretation.peptide_link.ramachandran_restraints = True
    params.pdb_interpretation.peptide_link.oldfield.weight_scale = 3
    params.pdb_interpretation.peptide_link.oldfield.plot_cutoff = 0.03
    params.pdb_interpretation.c_beta_restraints = True

    processed_pdb_files_srv = mmtbx.utils.process_pdb_file_srv(
        crystal_symmetry=xrs.crystal_symmetry(),
        pdb_interpretation_params=params.pdb_interpretation,
        stop_for_unknowns=False,
        log=log,
        cif_objects=None,
    )
    processed_pdb_file, junk = processed_pdb_files_srv.process_pdb_files(
        raw_records=flex.split_lines(hierarchy.as_pdb_string())
    )
    grm = get_geometry_restraints_manager(processed_pdb_file, xrs)

    asc = original_pdb_h.atom_selection_cache()
    sel = asc.selection(excl_string_selection)

    grm.geometry.append_reference_coordinate_restraints_in_place(
        reference.add_coordinate_restraints(
            sites_cart=original_pdb_h.atoms().extract_xyz().select(sel), selection=sel, sigma=0.5
        )
    )
    obj = run2(
        restraints_manager=grm,
        pdb_hierarchy=hierarchy,
        correct_special_position_tolerance=1.0,
        max_number_of_iterations=300,
        number_of_macro_cycles=5,
        bond=True,
        nonbonded=True,
        angle=True,
        dihedral=True,
        chirality=True,
        planarity=True,
        fix_rotamer_outliers=True,
        log=log,
    )
 def place_wat(self, b_fac=35.0, occ=1.00):
     #create a dummy pdb, put water
     pdb_string="CRYST1%9.3f%9.3f%9.3f  90.00  90.00  90.00 P 1            1\n" % \
         (2.0*self.bound,2.0*self.bound,2.0*self.bound)
     x1, y1, z1 = 5.0, 5.0, 5.0
     pdb_entry = ""
     pdb_entry = pdb_entry + self.write_atom(
         1, "O", "", "HOH", "X", 1, "", x1, y1, z1, occ, b_fac, "O", "")
     wat_pdb = pdb_string + pdb_entry
     std_wat_pdb = iotbx.pdb.input(source_info=None,
                                   lines=flex.split_lines(wat_pdb))
     local_sym = std_wat_pdb.crystal_symmetry()
     std_wat_hier = std_wat_pdb.construct_hierarchy()
     std_wat_xrs = std_wat_hier.extract_xray_structure(
         crystal_symmetry=local_sym)
     return std_wat_pdb, std_wat_hier, std_wat_xrs
Exemple #16
0
def extract_copies_identical_to_target_from_hierarchy(ph,
     allow_extensions=False,
     min_similarity=None,target_ph=None,out=sys.stdout):
  new_hierarchy=iotbx.pdb.input(
    source_info="Model",lines=flex.split_lines("")).construct_hierarchy()
  mm=iotbx.pdb.hierarchy.model()
  new_hierarchy.append_model(mm)

  assert target_ph is not None
  target_seq=None
  for model in target_ph.models()[:1]:
    for chain in model.chains():
      try:
        target_seq=chain.as_padded_sequence()  # has XXX for missing residues
        target_seq.replace("X","")
        break
      except Exception, e:
        pass
Exemple #17
0
    def merge_hier(self, hier_list, symmetry):
        pdb2str = lambda hier: hier.as_pdb_string(write_scale_records=False,
                                                  append_end=False,
                                                  interleaved_conf=0,
                                                  atom_hetatm=True,
                                                  sigatm=False,
                                                  anisou=False,
                                                  siguij=False,
                                                  output_break_records=False)

        allpdb = ""
        for index, hier in enumerate(hier_list):
            allpdb = allpdb + "MODEL %s\n" % str(index + 1)
            allpdb = allpdb + pdb2str(hier)
            allpdb = allpdb + "ENDMDL\n"
        dummy_pdb = iotbx.pdb.input(source_info=None,
                                    lines=flex.split_lines(allpdb))
        dummy_hier = dummy_pdb.construct_hierarchy()
        dummy_hier.remove_hd()
        dummy_hier.atoms_reset_serial()
        #dummy_hier.write_pdb_file('merge.pdb')
        return dummy_hier
 def run_resolve(self):
     from solve_resolve.resolve_python import resolve_in_memory
     from iotbx import pdb
     from scitbx.array_family import flex
     make_sub_header("RESOLVE build", out=self.out)
     mean_density_start = self.mean_density_at_sites()
     cc_start = self.cc_model_map()
     sites_start = self.get_selected_sites(hydrogens=False)
     t1 = time.time()
     pdb_inp = self.box_selected_hierarchy.as_pdb_input()
     inp_hierarchy = pdb_inp.construct_hierarchy()
     chain = inp_hierarchy.only_model().only_chain()
     first_resseq = chain.residue_groups()[0].resseq_as_int()
     seq = "".join(
         chain.only_conformer().as_sequence(substitute_unknown='A'))
     resolve_args = [
         "start_chain 1 %d" % first_resseq,
         "extend_only",
         "skip_hetatm",
         "no_merge_ncs_copies",
         "no_optimize_ncs",
         "i_ran_seed %d" % int(time.time() % os.getpid()),
     ]
     if (self.params.build_new_loop):  # XXX not really working...
         n_res = len(chain.residue_groups())
         assert (n_res >= 3)
         k = 0
         for residue_group in chain.residue_groups()[1:-1]:
             print >> self.out, "  removing residue group %s %s" % \
               (chain.id, residue_group.resid())
             chain.remove_residue_group(residue_group)
         resolve_args.extend([
             "loop_only",
             "build_outside_model",
             "no_sub_segments",
             "n_random_loop %d" % self.params.n_random_loop,
             "loop_length %d" % (n_res - 2),
             "rms_random_loop 0.3",
             "rho_min_main_low 0.5",
             "rho_min_main_base 0.5",
             "n_internal_start 0",
         ])
     else:
         resolve_args.extend([
             "rebuild_in_place",
             "replace_existing",
             "richardson_rotamers",
             "min_z_value_rho -3.0",
             "delta_phi   20.00",
             "dist_cut_base 3.0",
             "n_random_frag 0",
             "group_ca_length 4",
             "group_length 2",
         ])
     out = null_out()
     if (self.debug):
         out = self.out
     cmn = resolve_in_memory.run(map_coeffs=self.box_map_coeffs,
                                 pdb_inp=inp_hierarchy.as_pdb_input(),
                                 build=True,
                                 input_text="\n".join(resolve_args),
                                 chain_type="PROTEIN",
                                 seq_file_as_string=seq,
                                 out=out)
     new_pdb_input = pdb.input(source_info='string',
                               lines=flex.split_lines(
                                   cmn.atom_db.pdb_out_as_string))
     new_hierarchy = new_pdb_input.construct_hierarchy()
     print >> self.out, "  %d atoms rebuilt" % len(new_hierarchy.atoms())
     new_hierarchy.write_pdb_file("resolve.pdb")
     selection_moved = flex.size_t()
     sites_new = flex.vec3_double()
     for atom in new_hierarchy.atoms():
         id_str = atom.id_str()
         if (not id_str in self.atom_id_mapping):
             raise KeyError("Atom ID %s not recognized in RESOLVE model." %
                            id_str)
         i_seq = self.atom_id_mapping[id_str]
         selection_moved.append(i_seq)
         sites_new.append(atom.xyz)
     sites_cart_selected = self.box_selected_hierarchy.atoms().extract_xyz()
     sites_cart_selected.set_selected(selection_moved, sites_new)
     self.box_selected_hierarchy.atoms().set_xyz(sites_cart_selected)
     sites_cart_box = self.box.xray_structure_box.sites_cart()
     sites_cart_box.set_selected(self.selection_in_box, sites_cart_selected)
     self.box.xray_structure_box.set_sites_cart(sites_cart_box)
     self.box.pdb_hierarchy_box.atoms().set_xyz(sites_cart_box)
     t2 = time.time()
     print >> self.out, "  RESOLVE time: %.1fs" % (t2 - t1)
     selection_rebuilt = self.selection_in_box.select(selection_moved)
     minimize_sel = flex.bool(self.n_sites_box, False).set_selected(
         self.selection_in_box, True).set_selected(selection_rebuilt, False)
     # atoms present in the selection but not in the RESOLVE model (usually
     # hydrogen atoms) need to be minimized to follow the rebuilt sites
     if (minimize_sel.count(True) > 0):
         print >> self.out, "  Performing geometry minimzation on unbuilt sites"
         self.geometry_minimization(selection=minimize_sel, nonbonded=False)
     self.box.write_pdb_file("box_resolve.pdb")
     # two alternatives here: restrain other atoms tightly, and minimize the
     # entire box, or restrain selected atoms loosely, and refine only those
     self.restrain_atoms(selection=self.others_in_box, reference_sigma=0.02)
     if (self.params.anneal):
         self.anneal(start_temperature=2500)
     else:
         self.real_space_refine(selection=self.selection_all_box)
     self.box.write_pdb_file("box_refined.pdb")
     self.box.write_ccp4_map()
     mean_density_end = self.mean_density_at_sites()
     cc_end = self.cc_model_map()
     print >> self.out, "  mean density level: start=%.2fsigma  end=%.2fsigma" \
       % (mean_density_start, mean_density_end)
     print >> self.out, "  model-map CC: start=%.3f  end=%.3f" % (cc_start,
                                                                  cc_end)
     sites_final = self.get_selected_sites(hydrogens=False)
     print >> self.out, "  rmsd to starting model: %.3f Angstrom" % \
       sites_final.rms_difference(sites_start)
     t3 = time.time()
     print >> self.out, "  Total build and refine time: %.1fs" % (t3 - t1)
Exemple #19
0
  def __init__ (self, fmodel, pdb_hierarchy, crystal_symmetry=None, cc_min=0.8,
                molprobity_map_params=None) :

    from iotbx.pdb.amino_acid_codes import one_letter_given_three_letter
    from mmtbx import real_space_correlation

    validation.__init__(self)

    # arrays for different components
    self.everything = list()
    self.protein = list()
    self.other = list()
    self.water = list()
    aa_codes = one_letter_given_three_letter.keys()

    # redo real_space_corelation.simple to use map objects instead of filenames
    self.overall_rsc = None
    rsc = None
    try :
      rsc_params = real_space_correlation.master_params().extract()
      rsc_params.detail="residue"
      rsc_params.map_1.fill_missing_reflections = False
      rsc_params.map_2.fill_missing_reflections = False
      use_maps = False
      if (molprobity_map_params is not None):
        rsc_params.map_coefficients_file_name = \
          molprobity_map_params.map_coefficients_file_name
        rsc_params.map_coefficients_label = \
          molprobity_map_params.map_coefficients_label
        if (molprobity_map_params.map_file_name is not None):
          use_maps = True
      # use mmtbx/command_line/map_model_cc.py for maps
      self.fsc = None
      if (use_maps):
        from scitbx.array_family import flex
        import iotbx.pdb
        from mmtbx.maps import map_model_cc
        from mmtbx.command_line.map_model_cc import get_fsc
        from iotbx.file_reader import any_file
        from cctbx import crystal, sgtbx
        params = map_model_cc.master_params().extract()
        params.map_model_cc.resolution = molprobity_map_params.d_min
        map_object = any_file(molprobity_map_params.map_file_name).file_object

        # ---------------------------------------------------------------------
        # check that model crystal symmetry matches map crystal symmetry
        # if inconsistent, map parameters take precedence
        # TODO: centralize data consistency checks prior to running validation
        map_crystal_symmetry = crystal.symmetry(
          unit_cell=map_object.unit_cell(),
          space_group=sgtbx.space_group_info(
            map_object.space_group_number).group())
        if (not map_crystal_symmetry.is_similar_symmetry(crystal_symmetry)):
          crystal_symmetry = map_crystal_symmetry

        # ---------------------------------------------------------------------

        map_data = map_object.map_data()
        rsc_object = map_model_cc.map_model_cc(
          map_data, pdb_hierarchy, crystal_symmetry, params.map_model_cc)
        rsc_object.validate()
        rsc_object.run()
        rsc = rsc_object.get_results()
        self.overall_rsc = (rsc.cc_mask, rsc.cc_volume, rsc.cc_peaks)

        # pdb_hierarchy.as_pdb_input is being phased out since that function
        # just re-processes the file from text and can be lossy
        # this is a placeholder until tools get updated to use the model class
        pdb_input = iotbx.pdb.input(
          source_info='pdb_hierarchy',
          lines=flex.split_lines(pdb_hierarchy.as_pdb_string()))
        model = mmtbx.model.manager(model_input = pdb_input)
        self.fsc = get_fsc(map_data, model, params.map_model_cc)
        #

        self.fsc.atom_radius = rsc.atom_radius
        rsc = rsc.cc_per_residue
      # mmtbx/real_space_correlation.py for X-ray/neutron data and map
      # coefficients
      else:
        self.overall_rsc, rsc = real_space_correlation.simple(
          fmodel=fmodel,
          pdb_hierarchy=pdb_hierarchy,
          params=rsc_params,
          log=null_out())
    except Exception, e :
      raise
Exemple #20
0
    def contacts_to_coord(self, coord, pdb_hier, symmetry, cutoff=6.0):
        """
        Used to use extract map_model, but that caused problems
        This function takes cartesian coordinate, pdb_hier, and symmetry and returns
        sorted list of dictionaries, each a particular contact

        very hacked together, jams pdb strings together
        create a dummy pdb, put water at the peak site in the original coordinate system
        then use fast pair generater to get all contacts to our peak atom
        """
        dummy_atom = self.pput.write_atom(1, "O", "", "HOH", "ZZ", 9999, "",
                                          coord[0], coord[1], coord[2], 1.0,
                                          35.0, "O", "")
        #hack to add an atom to a pdb
        orig_str = pdb_hier.as_pdb_string(write_scale_records=False,
                                          append_end=False,
                                          interleaved_conf=0,
                                          atoms_reset_serial_first_value=1,
                                          atom_hetatm=True,
                                          sigatm=False,
                                          anisou=False,
                                          siguij=False,
                                          output_break_records=False)
        nmodels = len(pdb_hier.models())
        if nmodels == 1:
            comb = orig_str + dummy_atom
        else:
            newmodel = nmodels + 1
            comb = orig_str + "MODEL %s\n" % newmodel
            comb = comb + dummy_atom
            comb = comb + "ENDMDL\n"
        dummy_pdb = iotbx.pdb.input(source_info=None,
                                    lines=flex.split_lines(comb))
        dummy_hier = dummy_pdb.construct_hierarchy()
        atsel = "chain ZZ and resid 9999"
        dummy_select = dummy_hier.atom_selection_cache().selection(
            string=atsel)
        atsel_index = np.argwhere(dummy_select.as_numpy_array())
        dummy_xrs = dummy_pdb.xray_structure_simple(crystal_symmetry=symmetry)
        #find neighbors with symmetry -- use pair_finding with asu_mappings
        #doing this for each peak may be a bad idea, use all_contacts version instead if possible
        asu_mappings = dummy_xrs.asu_mappings(buffer_thickness=cutoff)
        pair_generator = crystal.neighbors_fast_pair_generator(
            asu_mappings, distance_cutoff=cutoff)

        peak_vector_list = []
        #neighbor_mask = pair_generator.neighbors_of(atsel_arr)
        #neighbors = pair_generator.select(neighbor_mask)
        for pair in pair_generator:
            if pair.i_seq == atsel_index:
                #our peak is first atom, but we want pairs in both directions
                #to provide exhaustive list of contacts
                #store index number and difference vector and sym to make unique
                #we don't care which symop, just how far away
                rdist = int(
                    np.sqrt(pair.dist_sq) * 1000) / 1000.0  #avoid float error
                peak_vector_list.append((pair.j_seq, rdist, pair.j_sym))
            if pair.j_seq == atsel_index:
                rdist = int(np.sqrt(pair.dist_sq) * 1000) / 1000.0
                peak_vector_list.append((pair.i_seq, rdist, pair.j_sym))

        unique = set(peak_vector_list)
        dummy_atoms = dummy_hier.atoms()

        #selection is boolean flex array, sizeof no atoms, can be indexed directly
        #get awl for our dummy atom
        s_awl = dummy_atoms.select(dummy_select)[0].fetch_labels()
        #list((awl_db[c_at[0]],c_at[1],c_at[2]) for c_at in uni_c )
        #next, get list of contacts (awl,dist)
        selection = dummy_hier.atom_selection_cache().selection("not all")
        if len(unique) == 0:
            return []
        cont_list = []
        for conti in unique:
            selection[conti[0]] = True
            sel_awl = dummy_atoms.select(selection)[0].fetch_labels()
            cont_list.append(
                (sel_awl, conti[1],
                 conti[2]))  #pass source awl, index for target, distance
            selection[conti[0]] = False  #unselect
        contacts, s_unal = self.get_contacts(s_awl, cont_list, cutoff=cutoff)
        return contacts
Exemple #21
0
def exercise_geo_output(mon_lib_srv, ener_lib):
  pdb_str = """\
CRYST1   18.879   16.714   25.616  90.00  90.00  90.00 P 1
ATOM      1  N   ALA     1      13.515   7.809  20.095  1.00  0.00           N
ATOM      2  CA  ALA     1      13.087   6.532  19.536  1.00  0.00           C
ATOM      3  C   ALA     1      11.716   6.653  18.880  1.00  0.00           C
ATOM      4  O   ALA     1      11.425   5.972  17.896  1.00  0.00           O
ATOM      5  CB  ALA     1      13.065   5.461  20.616  1.00  0.00           C
ATOM      6  N   ALA     2      10.876   7.524  19.431  1.00  0.00           N
ATOM      7  CA  ALA     2       9.535   7.735  18.900  1.00  0.00           C
ATOM      8  C   ALA     2       9.565   8.647  17.678  1.00  0.00           C
ATOM      9  O   ALA     2       8.787   8.471  16.741  1.00  0.00           O
ATOM     10  CB  ALA     2       8.626   8.316  19.973  1.00  0.00           C
ATOM     11  N   ALA     3      10.469   9.622  17.697  1.00  0.00           N
ATOM     12  CA  ALA     3      10.606  10.565  16.593  1.00  0.00           C
ATOM     13  C   ALA     3      11.132   9.871  15.340  1.00  0.00           C
ATOM     14  O   ALA     3      10.687  10.157  14.228  1.00  0.00           O
ATOM     15  CB  ALA     3      11.520  11.714  16.987  1.00  0.00           C
ATOM     16  N   ALA     4      12.081   8.960  15.530  1.00  0.00           N
ATOM     17  CA  ALA     4      12.656   8.209  14.421  1.00  0.00           C
ATOM     18  C   ALA     4      11.624   7.266  13.812  1.00  0.00           C
ATOM     19  O   ALA     4      11.570   7.090  12.595  1.00  0.00           O
ATOM     20  CB  ALA     4      13.879   7.432  14.883  1.00  0.00           C
ATOM     21  N   ALA     5      10.805   6.663  14.669  1.00  0.00           N
ATOM     22  CA  ALA     5       9.753   5.761  14.218  1.00  0.00           C
ATOM     23  C   ALA     5       8.662   6.528  13.481  1.00  0.00           C
ATOM     24  O   ALA     5       8.107   6.045  12.494  1.00  0.00           O
ATOM     25  CB  ALA     5       9.165   5.000  15.396  1.00  0.00           C
ATOM     26  N   ALA     6       8.360   7.728  13.967  1.00  0.00           N
ATOM     27  CA  ALA     6       7.358   8.580  13.338  1.00  0.00           C
ATOM     28  C   ALA     6       7.860   9.106  11.998  1.00  0.00           C
ATOM     29  O   ALA     6       7.078   9.322  11.072  1.00  0.00           O
ATOM     30  CB  ALA     6       6.986   9.732  14.257  1.00  0.00           C
ATOM     31  N   ALA     7       9.169   9.311  11.903  1.00  0.00           N
ATOM     32  CA  ALA     7       9.781   9.787  10.668  1.00  0.00           C
ATOM     33  C   ALA     7       9.912   8.655   9.655  1.00  0.00           C
ATOM     34  O   ALA     7       9.905   8.886   8.446  1.00  0.00           O
ATOM     35  CB  ALA     7      11.141  10.405  10.952  1.00  0.00           C
ATOM     36  N   ALA     8      10.030   7.429  10.157  1.00  0.00           N
ATOM     37  CA  ALA     8      10.152   6.258   9.297  1.00  0.00           C
ATOM     38  C   ALA     8       8.788   5.809   8.786  1.00  0.00           C
ATOM     39  O   ALA     8       8.667   5.312   7.666  1.00  0.00           O
ATOM     40  CB  ALA     8      10.839   5.124  10.041  1.00  0.00           C
ATOM     41  N   ALA     9       7.762   5.988   9.613  1.00  0.00           N
ATOM     42  CA  ALA     9       6.405   5.603   9.243  1.00  0.00           C
ATOM     43  C   ALA     9       5.816   6.576   8.228  1.00  0.00           C
ATOM     44  O   ALA     9       5.000   6.195   7.389  1.00  0.00           O
ATOM     45  CB  ALA     9       5.521   5.526  10.478  1.00  0.00           C
ATOM     46  N   ALA    10       6.235   7.835   8.309  1.00  0.00           N
ATOM     47  CA  ALA    10       5.751   8.864   7.397  1.00  0.00           C
ATOM     48  C   ALA    10       6.434   8.760   6.038  1.00  0.00           C
ATOM     49  O   ALA    10       5.773   8.734   5.000  1.00  0.00           O
ATOM     50  CB  ALA    10       5.966  10.246   7.995  1.00  0.00           C
TER
END
"""
  pdb_inp = iotbx.pdb.input(source_info="peptide",lines=flex.split_lines(pdb_str))
  hierarchy = pdb_inp.construct_hierarchy()
  atoms = hierarchy.atoms()
  sites_cart = atoms.extract_xyz()
  params = ramachandran.master_phil.fetch().extract()
  params.rama_potential = "emsley"
  rama_manager = ramachandran.ramachandran_manager(
      hierarchy, params, StringIO())
  out = StringIO()
  rama_manager.show_sorted(
      by_value="residual",
      sites_cart=sites_cart,
      site_labels=[a.id_str() for a in atoms],
      f=out)
  gv = out.getvalue()
  # print out.getvalue()
  # STOP()
  assert not show_diff(gv, """\
Ramachandran plot restraints (Oldfield): 0
Sorted by residual:

Ramachandran plot restraints (Emsley): 8
Sorted by residual:
phi-psi angles formed by             residual
    pdb=" C   ALA     7 "            1.53e+01
    pdb=" N   ALA     8 "
    pdb=" CA  ALA     8 "
    pdb=" C   ALA     8 "
    pdb=" N   ALA     9 "
phi-psi angles formed by             residual
    pdb=" C   ALA     1 "            1.52e+01
    pdb=" N   ALA     2 "
    pdb=" CA  ALA     2 "
    pdb=" C   ALA     2 "
    pdb=" N   ALA     3 "
phi-psi angles formed by             residual
    pdb=" C   ALA     6 "            1.20e+01
    pdb=" N   ALA     7 "
    pdb=" CA  ALA     7 "
    pdb=" C   ALA     7 "
    pdb=" N   ALA     8 "
phi-psi angles formed by             residual
    pdb=" C   ALA     2 "            1.14e+01
    pdb=" N   ALA     3 "
    pdb=" CA  ALA     3 "
    pdb=" C   ALA     3 "
    pdb=" N   ALA     4 "
phi-psi angles formed by             residual
    pdb=" C   ALA     8 "            1.06e+01
    pdb=" N   ALA     9 "
    pdb=" CA  ALA     9 "
    pdb=" C   ALA     9 "
    pdb=" N   ALA    10 "
phi-psi angles formed by             residual
    pdb=" C   ALA     4 "            1.06e+01
    pdb=" N   ALA     5 "
    pdb=" CA  ALA     5 "
    pdb=" C   ALA     5 "
    pdb=" N   ALA     6 "
phi-psi angles formed by             residual
    pdb=" C   ALA     3 "            1.03e+01
    pdb=" N   ALA     4 "
    pdb=" CA  ALA     4 "
    pdb=" C   ALA     4 "
    pdb=" N   ALA     5 "
phi-psi angles formed by             residual
    pdb=" C   ALA     5 "            7.58e+00
    pdb=" N   ALA     6 "
    pdb=" CA  ALA     6 "
    pdb=" C   ALA     6 "
    pdb=" N   ALA     7 "

""")

  params.rama_potential = "oldfield"
  rama_manager = ramachandran.ramachandran_manager(
      hierarchy, params, StringIO())
  out = StringIO()
  rama_manager.show_sorted(
      by_value="residual",
      sites_cart=sites_cart,
      site_labels=[a.id_str() for a in atoms],
      f=out)
  gv = out.getvalue()
  assert not show_diff(gv, """\
Ramachandran plot restraints (Oldfield): 8
Sorted by residual:
phi-psi angles formed by             residual
    pdb=" C   ALA     5 "            3.46e-02
    pdb=" N   ALA     6 "
    pdb=" CA  ALA     6 "
    pdb=" C   ALA     6 "
    pdb=" N   ALA     7 "
phi-psi angles formed by             residual
    pdb=" C   ALA     6 "            2.86e-02
    pdb=" N   ALA     7 "
    pdb=" CA  ALA     7 "
    pdb=" C   ALA     7 "
    pdb=" N   ALA     8 "
phi-psi angles formed by             residual
    pdb=" C   ALA     2 "            2.54e-02
    pdb=" N   ALA     3 "
    pdb=" CA  ALA     3 "
    pdb=" C   ALA     3 "
    pdb=" N   ALA     4 "
phi-psi angles formed by             residual
    pdb=" C   ALA     8 "            2.00e-02
    pdb=" N   ALA     9 "
    pdb=" CA  ALA     9 "
    pdb=" C   ALA     9 "
    pdb=" N   ALA    10 "
phi-psi angles formed by             residual
    pdb=" C   ALA     1 "            1.21e-02
    pdb=" N   ALA     2 "
    pdb=" CA  ALA     2 "
    pdb=" C   ALA     2 "
    pdb=" N   ALA     3 "
phi-psi angles formed by             residual
    pdb=" C   ALA     4 "            1.00e-02
    pdb=" N   ALA     5 "
    pdb=" CA  ALA     5 "
    pdb=" C   ALA     5 "
    pdb=" N   ALA     6 "
phi-psi angles formed by             residual
    pdb=" C   ALA     3 "            9.28e-03
    pdb=" N   ALA     4 "
    pdb=" CA  ALA     4 "
    pdb=" C   ALA     4 "
    pdb=" N   ALA     5 "
phi-psi angles formed by             residual
    pdb=" C   ALA     7 "            3.90e-03
    pdb=" N   ALA     8 "
    pdb=" CA  ALA     8 "
    pdb=" C   ALA     8 "
    pdb=" N   ALA     9 "

Ramachandran plot restraints (Emsley): 0
Sorted by residual:

""")
def minimize_wrapper_for_ramachandran(
    hierarchy,
    xrs,
    original_pdb_h,
    excl_string_selection,
    log=None,
    ss_annotation = None,
    reference_rotamers = True,
    run_first_minimization_without_reference=False,
    oldfield_weight_scale=3,
    oldfield_plot_cutoff=0.03,
    nonbonded_weight=500,
    reference_sigma=0.7):
  """ Wrapper around geometry minimization specifically tuned for eliminating
  Ramachandran outliers.
  """
  import pickle
  from time import time
  from mmtbx.monomer_library.pdb_interpretation import grand_master_phil_str
  from mmtbx.geometry_restraints import reference
  from mmtbx.command_line.geometry_minimization import \
      get_geometry_restraints_manager
  from mmtbx.geometry_restraints.torsion_restraints.reference_model import \
      reference_model, reference_model_params
  from libtbx.utils import null_out
  from scitbx.array_family import flex
  if log is None:
    log = null_out()
  params_line = grand_master_phil_str
  params = iotbx.phil.parse(
      input_string=params_line, process_includes=True).extract()
  params.pdb_interpretation.clash_guard.nonbonded_distance_threshold=None
  params.pdb_interpretation.peptide_link.ramachandran_restraints = True
  params.pdb_interpretation.peptide_link.oldfield.weight_scale=oldfield_weight_scale
  params.pdb_interpretation.peptide_link.oldfield.plot_cutoff=oldfield_plot_cutoff
  params.pdb_interpretation.nonbonded_weight = nonbonded_weight
  params.pdb_interpretation.c_beta_restraints=True
  params.pdb_interpretation.max_reasonable_bond_distance = None
  params.pdb_interpretation.peptide_link.apply_peptide_plane = True
  params.pdb_interpretation.ncs_search.enabled = True
  params.pdb_interpretation.restraints_library.rdl = True

  processed_pdb_files_srv = mmtbx.utils.\
      process_pdb_file_srv(
          crystal_symmetry= xrs.crystal_symmetry(),
          pdb_interpretation_params = params.pdb_interpretation,
          stop_for_unknowns         = False,
          log=log,
          cif_objects=None)
  processed_pdb_file, junk = processed_pdb_files_srv.\
      process_pdb_files(raw_records=flex.split_lines(hierarchy.as_pdb_string()))

  mon_lib_srv = processed_pdb_files_srv.mon_lib_srv
  ener_lib = processed_pdb_files_srv.ener_lib

  ncs_restraints_group_list = []
  if processed_pdb_file.ncs_obj is not None:
    ncs_restraints_group_list = processed_pdb_file.ncs_obj.get_ncs_restraints_group_list()

  grm = get_geometry_restraints_manager(
      processed_pdb_file, xrs, params=params)

  if reference_rotamers and original_pdb_h is not None:
    # make selection excluding rotamer outliers
    from mmtbx.rotamer.rotamer_eval import RotamerEval
    rotamer_manager = RotamerEval(mon_lib_srv=mon_lib_srv)
    non_rot_outliers_selection = flex.bool([False]*hierarchy.atoms().size())
    for model in original_pdb_h.models():
      for chain in model.chains():
        for conf in chain.conformers():
          for res in conf.residues():
            ev = rotamer_manager.evaluate_residue_2(res)
            if ev != "OUTLIER" or ev is None:
              for a in res.atoms():
                non_rot_outliers_selection[a.i_seq] = True


    rm_params = reference_model_params.extract()
    rm_params.reference_model.enabled=True
    rm_params.reference_model.strict_rotamer_matching=False
    rm_params.reference_model.main_chain=False
    rm = reference_model(
      processed_pdb_file=processed_pdb_file,
      reference_file_list=None,
      reference_hierarchy_list=[original_pdb_h],
      mon_lib_srv=mon_lib_srv,
      ener_lib=ener_lib,
      has_hd=None,
      params=rm_params.reference_model,
      selection=non_rot_outliers_selection,
      log=log)
    rm.show_reference_summary(log=log)
    grm.geometry.adopt_reference_dihedral_manager(rm)

  # dealing with SS
  if ss_annotation is not None:
    from mmtbx.secondary_structure import manager
    ss_manager = manager(
        pdb_hierarchy=hierarchy,
        geometry_restraints_manager=grm.geometry,
        sec_str_from_pdb_file=ss_annotation,
        params=None,
        mon_lib_srv=mon_lib_srv,
        verbose=-1,
        log=log)
    grm.geometry.set_secondary_structure_restraints(
        ss_manager=ss_manager,
        hierarchy=hierarchy,
        log=log)

  # grm pickle-unpickle
  # t0 = time()
  # prefix="grm"
  # pklfile = open("%s.pkl" % prefix, 'wb')
  # pickle.dump(grm.geometry, pklfile)
  # pklfile.close()
  # t1 = time()
  # pklfile = open("%s.pkl" % prefix, 'rb')
  # grm_from_file = pickle.load(pklfile)
  # pklfile.close()
  # t2 = time()
  # print "Time pickling/unpickling: %.4f, %.4f" % (t1-t0, t2-t1)
  # grm.geometry=grm_from_file


  if run_first_minimization_without_reference:
    obj = run2(
      restraints_manager=grm,
      pdb_hierarchy=hierarchy,
      correct_special_position_tolerance=1.0,
      ncs_restraints_group_list=ncs_restraints_group_list,
      max_number_of_iterations=300,
      number_of_macro_cycles=5,
      bond=True,
      nonbonded=True,
      angle=True,
      dihedral=True,
      chirality=True,
      planarity=True,
      fix_rotamer_outliers=True,
      log=log)


  if original_pdb_h is not None:
    if len(excl_string_selection) == 0:
      excl_string_selection = "all"
    asc = original_pdb_h.atom_selection_cache()
    sel = asc.selection("(%s) and (name CA or name C or name N or name O)" % excl_string_selection)


    grm.geometry.append_reference_coordinate_restraints_in_place(
        reference.add_coordinate_restraints(
            sites_cart = original_pdb_h.atoms().extract_xyz().select(sel),
            selection  = sel,
            sigma      = reference_sigma,
            top_out_potential=True))
  obj = run2(
      restraints_manager       = grm,
      pdb_hierarchy            = hierarchy,
      correct_special_position_tolerance = 1.0,
      ncs_restraints_group_list=ncs_restraints_group_list,
      max_number_of_iterations = 300,
      number_of_macro_cycles   = 5,
      bond                     = True,
      nonbonded                = True,
      angle                    = True,
      dihedral                 = True,
      chirality                = True,
      planarity                = True,
      fix_rotamer_outliers     = True,
      log                      = log)
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 #24
0
    for chain in model.chains():
      try:
        target_seq=chain.as_padded_sequence()  # has XXX for missing residues
        target_seq.replace("X","")
        chain_length=len(target_seq)
      except Exception, e:
        chain_length=0
      if chain_length and (longest_chain is None or chain_length>longest_chain):
        longest_chain=chain_length
        biggest_chain=chain
  if biggest_chain is None:
    print >>out,"Unable to extract unique part of hierarchy"
    return None

  biggest_chain_hierarchy=iotbx.pdb.input(
    source_info="Model",lines=flex.split_lines("")).construct_hierarchy()
  mm=iotbx.pdb.hierarchy.model()
  biggest_chain_hierarchy.append_model(mm)
  mm.append_chain(biggest_chain.detached_copy())

  # ready with biggest chain

  copies_of_biggest_chain_ph=extract_copies_identical_to_target_from_hierarchy(
     ph,target_ph=biggest_chain_hierarchy,
     allow_extensions=False,out=sys.stdout)
  return copies_of_biggest_chain_ph

def extract_copies_identical_to_target_from_hierarchy(ph,
     allow_extensions=False,
     min_similarity=None,target_ph=None,out=sys.stdout):
  new_hierarchy=iotbx.pdb.input(
Exemple #25
0
def exercise_geo_output(mon_lib_srv, ener_lib):
    pdb_inp = iotbx.pdb.input(source_info="peptide",
                              lines=flex.split_lines(pdb_str))
    hierarchy = pdb_inp.construct_hierarchy()
    atoms = hierarchy.atoms()
    sites_cart = atoms.extract_xyz()
    params = ramachandran.master_phil.fetch().extract()
    params = params.ramachandran_plot_restraints
    params.favored = 'emsley'
    params.allowed = 'emsley'
    params.outlier = 'emsley'
    params.inject_emsley8k_into_oldfield_favored = False
    rama_manager = ramachandran.ramachandran_manager(hierarchy, params,
                                                     StringIO())
    out = StringIO()
    rama_manager.show_sorted(by_value="residual",
                             sites_cart=sites_cart,
                             site_labels=[a.id_str() for a in atoms],
                             f=out)
    gv = out.getvalue()
    assert not show_diff(
        gv, """\
Ramachandran plot restraints (Oldfield): 0
Sorted by residual:

Ramachandran plot restraints (Emsley): 8
Sorted by residual:
phi-psi angles formed by             residual
    pdb=" C   ALA     7 "            1.53e+01
    pdb=" N   ALA     8 "
    pdb=" CA  ALA     8 "
    pdb=" C   ALA     8 "
    pdb=" N   ALA     9 "
phi-psi angles formed by             residual
    pdb=" C   ALA     1 "            1.52e+01
    pdb=" N   ALA     2 "
    pdb=" CA  ALA     2 "
    pdb=" C   ALA     2 "
    pdb=" N   ALA     3 "
phi-psi angles formed by             residual
    pdb=" C   ALA     6 "            1.20e+01
    pdb=" N   ALA     7 "
    pdb=" CA  ALA     7 "
    pdb=" C   ALA     7 "
    pdb=" N   ALA     8 "
phi-psi angles formed by             residual
    pdb=" C   ALA     2 "            1.14e+01
    pdb=" N   ALA     3 "
    pdb=" CA  ALA     3 "
    pdb=" C   ALA     3 "
    pdb=" N   ALA     4 "
phi-psi angles formed by             residual
    pdb=" C   ALA     8 "            1.06e+01
    pdb=" N   ALA     9 "
    pdb=" CA  ALA     9 "
    pdb=" C   ALA     9 "
    pdb=" N   ALA    10 "
phi-psi angles formed by             residual
    pdb=" C   ALA     4 "            1.06e+01
    pdb=" N   ALA     5 "
    pdb=" CA  ALA     5 "
    pdb=" C   ALA     5 "
    pdb=" N   ALA     6 "
phi-psi angles formed by             residual
    pdb=" C   ALA     3 "            1.03e+01
    pdb=" N   ALA     4 "
    pdb=" CA  ALA     4 "
    pdb=" C   ALA     4 "
    pdb=" N   ALA     5 "
phi-psi angles formed by             residual
    pdb=" C   ALA     5 "            7.58e+00
    pdb=" N   ALA     6 "
    pdb=" CA  ALA     6 "
    pdb=" C   ALA     6 "
    pdb=" N   ALA     7 "

Ramachandran plot restraints (emsley8k): 0
Sorted by residual:

Ramachandran plot restraints (phi/psi/2): 0
Sorted by residual:

""")

    params.favored = 'oldfield'
    params.allowed = 'oldfield'
    params.outlier = 'oldfield'
    params.inject_emsley8k_into_oldfield_favored = False
    rama_manager = ramachandran.ramachandran_manager(hierarchy, params,
                                                     StringIO())
    out = StringIO()
    rama_manager.show_sorted(by_value="residual",
                             sites_cart=sites_cart,
                             site_labels=[a.id_str() for a in atoms],
                             f=out)
    gv = out.getvalue()
    #print(gv)
    #STOP()
    assert not show_diff(
        gv, """\
Ramachandran plot restraints (Oldfield): 8
Sorted by residual:
phi-psi angles formed by             residual
    pdb=" C   ALA     5 "            3.46e-02
    pdb=" N   ALA     6 "
    pdb=" CA  ALA     6 "
    pdb=" C   ALA     6 "
    pdb=" N   ALA     7 "
phi-psi angles formed by             residual
    pdb=" C   ALA     6 "            2.86e-02
    pdb=" N   ALA     7 "
    pdb=" CA  ALA     7 "
    pdb=" C   ALA     7 "
    pdb=" N   ALA     8 "
phi-psi angles formed by             residual
    pdb=" C   ALA     2 "            2.54e-02
    pdb=" N   ALA     3 "
    pdb=" CA  ALA     3 "
    pdb=" C   ALA     3 "
    pdb=" N   ALA     4 "
phi-psi angles formed by             residual
    pdb=" C   ALA     8 "            2.00e-02
    pdb=" N   ALA     9 "
    pdb=" CA  ALA     9 "
    pdb=" C   ALA     9 "
    pdb=" N   ALA    10 "
phi-psi angles formed by             residual
    pdb=" C   ALA     1 "            1.21e-02
    pdb=" N   ALA     2 "
    pdb=" CA  ALA     2 "
    pdb=" C   ALA     2 "
    pdb=" N   ALA     3 "
phi-psi angles formed by             residual
    pdb=" C   ALA     4 "            1.00e-02
    pdb=" N   ALA     5 "
    pdb=" CA  ALA     5 "
    pdb=" C   ALA     5 "
    pdb=" N   ALA     6 "
phi-psi angles formed by             residual
    pdb=" C   ALA     3 "            9.28e-03
    pdb=" N   ALA     4 "
    pdb=" CA  ALA     4 "
    pdb=" C   ALA     4 "
    pdb=" N   ALA     5 "
phi-psi angles formed by             residual
    pdb=" C   ALA     7 "            3.90e-03
    pdb=" N   ALA     8 "
    pdb=" CA  ALA     8 "
    pdb=" C   ALA     8 "
    pdb=" N   ALA     9 "

Ramachandran plot restraints (Emsley): 0
Sorted by residual:

Ramachandran plot restraints (emsley8k): 0
Sorted by residual:

Ramachandran plot restraints (phi/psi/2): 0
Sorted by residual:

""")
Exemple #26
0
def exercise_manager_selection(mon_lib_srv, ener_lib):
    pdb_inp = iotbx.pdb.input(source_info="peptide",
                              lines=flex.split_lines(pdb_str))
    hierarchy = pdb_inp.construct_hierarchy()
    atoms = hierarchy.atoms()
    sites_cart = atoms.extract_xyz()
    params = ramachandran.master_phil.fetch().extract()
    params = params.ramachandran_plot_restraints
    params.favored = 'emsley'
    params.allowed = 'emsley'
    params.outlier = 'emsley'
    params.inject_emsley8k_into_oldfield_favored = False
    rama_manager = ramachandran.ramachandran_manager(hierarchy, params,
                                                     StringIO())
    out = StringIO()
    s_out = StringIO()
    rama_manager.show_sorted(by_value="residual",
                             sites_cart=sites_cart,
                             site_labels=[a.id_str() for a in atoms],
                             f=out)
    selected_m = rama_manager.proxy_select(n_seq=hierarchy.atoms_size(),
                                           iselection=flex.size_t(range(40)))
    selected_m.show_sorted(by_value="residual",
                           sites_cart=sites_cart,
                           site_labels=[a.id_str() for a in atoms],
                           f=s_out)
    assert not show_diff(
        s_out.getvalue(), """\
Ramachandran plot restraints (Oldfield): 0
Sorted by residual:

Ramachandran plot restraints (Emsley): 6
Sorted by residual:
phi-psi angles formed by             residual
    pdb=" C   ALA     1 "            1.52e+01
    pdb=" N   ALA     2 "
    pdb=" CA  ALA     2 "
    pdb=" C   ALA     2 "
    pdb=" N   ALA     3 "
phi-psi angles formed by             residual
    pdb=" C   ALA     6 "            1.20e+01
    pdb=" N   ALA     7 "
    pdb=" CA  ALA     7 "
    pdb=" C   ALA     7 "
    pdb=" N   ALA     8 "
phi-psi angles formed by             residual
    pdb=" C   ALA     2 "            1.14e+01
    pdb=" N   ALA     3 "
    pdb=" CA  ALA     3 "
    pdb=" C   ALA     3 "
    pdb=" N   ALA     4 "
phi-psi angles formed by             residual
    pdb=" C   ALA     4 "            1.06e+01
    pdb=" N   ALA     5 "
    pdb=" CA  ALA     5 "
    pdb=" C   ALA     5 "
    pdb=" N   ALA     6 "
phi-psi angles formed by             residual
    pdb=" C   ALA     3 "            1.03e+01
    pdb=" N   ALA     4 "
    pdb=" CA  ALA     4 "
    pdb=" C   ALA     4 "
    pdb=" N   ALA     5 "
phi-psi angles formed by             residual
    pdb=" C   ALA     5 "            7.58e+00
    pdb=" N   ALA     6 "
    pdb=" CA  ALA     6 "
    pdb=" C   ALA     6 "
    pdb=" N   ALA     7 "

Ramachandran plot restraints (emsley8k): 0
Sorted by residual:

Ramachandran plot restraints (phi/psi/2): 0
Sorted by residual:

""")
 def run_resolve (self) :
   from solve_resolve.resolve_python import resolve_in_memory
   from iotbx import pdb
   from scitbx.array_family import flex
   make_sub_header("RESOLVE build", out=self.out)
   mean_density_start = self.mean_density_at_sites()
   cc_start = self.cc_model_map()
   sites_start = self.get_selected_sites(hydrogens=False)
   t1 = time.time()
   pdb_inp = self.box_selected_hierarchy.as_pdb_input()
   inp_hierarchy = pdb_inp.construct_hierarchy()
   chain = inp_hierarchy.only_model().only_chain()
   first_resseq = chain.residue_groups()[0].resseq_as_int()
   seq = "".join(chain.only_conformer().as_sequence(substitute_unknown='A'))
   resolve_args = [
     "start_chain 1 %d" % first_resseq,
     "extend_only",
     "skip_hetatm",
     "no_merge_ncs_copies",
     "no_optimize_ncs",
     "i_ran_seed %d" % int(time.time() % os.getpid()),
   ]
   if (self.params.build_new_loop) : # XXX not really working...
     n_res = len(chain.residue_groups())
     assert (n_res >= 3)
     k = 0
     for residue_group in chain.residue_groups()[1:-1] :
       print >> self.out, "  removing residue group %s %s" % \
         (chain.id, residue_group.resid())
       chain.remove_residue_group(residue_group)
     resolve_args.extend([
       "loop_only",
       "build_outside_model",
       "no_sub_segments",
       "n_random_loop %d" % self.params.n_random_loop,
       "loop_length %d" % (n_res - 2),
       "rms_random_loop 0.3",
       "rho_min_main_low 0.5",
       "rho_min_main_base 0.5",
       "n_internal_start 0",
     ])
   else :
     resolve_args.extend([
       "rebuild_in_place",
       "replace_existing",
       "richardson_rotamers",
       "min_z_value_rho -3.0",
       "delta_phi   20.00",
       "dist_cut_base 3.0",
       "n_random_frag 0",
       "group_ca_length 4",
       "group_length 2",
     ])
   out = null_out()
   if (self.debug) :
     out = self.out
   cmn = resolve_in_memory.run(
     map_coeffs=self.box_map_coeffs,
     pdb_inp=inp_hierarchy.as_pdb_input(),
     build=True,
     input_text="\n".join(resolve_args),
     chain_type="PROTEIN",
     seq_file_as_string=seq,
     out=out)
   new_pdb_input = pdb.input(
     source_info='string',
     lines=flex.split_lines(cmn.atom_db.pdb_out_as_string))
   new_hierarchy = new_pdb_input.construct_hierarchy()
   print >> self.out, "  %d atoms rebuilt" % len(new_hierarchy.atoms())
   new_hierarchy.write_pdb_file("resolve.pdb")
   selection_moved = flex.size_t()
   sites_new = flex.vec3_double()
   for atom in new_hierarchy.atoms() :
     id_str = atom.id_str()
     if (not id_str in self.atom_id_mapping) :
       raise KeyError("Atom ID %s not recognized in RESOLVE model." % id_str)
     i_seq = self.atom_id_mapping[id_str]
     selection_moved.append(i_seq)
     sites_new.append(atom.xyz)
   sites_cart_selected = self.box_selected_hierarchy.atoms().extract_xyz()
   sites_cart_selected.set_selected(selection_moved, sites_new)
   self.box_selected_hierarchy.atoms().set_xyz(sites_cart_selected)
   sites_cart_box = self.box.xray_structure_box.sites_cart()
   sites_cart_box.set_selected(self.selection_in_box, sites_cart_selected)
   self.box.xray_structure_box.set_sites_cart(sites_cart_box)
   self.box.pdb_hierarchy_box.atoms().set_xyz(sites_cart_box)
   t2 = time.time()
   print >> self.out, "  RESOLVE time: %.1fs" % (t2-t1)
   selection_rebuilt = self.selection_in_box.select(selection_moved)
   minimize_sel = flex.bool(self.n_sites_box, False).set_selected(
     self.selection_in_box, True).set_selected(selection_rebuilt, False)
   # atoms present in the selection but not in the RESOLVE model (usually
   # hydrogen atoms) need to be minimized to follow the rebuilt sites
   if (minimize_sel.count(True) > 0) :
     print >> self.out, "  Performing geometry minimzation on unbuilt sites"
     self.geometry_minimization(
       selection=minimize_sel,
       nonbonded=False)
   self.box.write_pdb_file("box_resolve.pdb")
   # two alternatives here: restrain other atoms tightly, and minimize the
   # entire box, or restrain selected atoms loosely, and refine only those
   self.restrain_atoms(
     selection=self.others_in_box,
     reference_sigma=0.02)
   if (self.params.anneal) :
     self.anneal(start_temperature=2500)
   else :
     self.real_space_refine(selection=self.selection_all_box)
   self.box.write_pdb_file("box_refined.pdb")
   self.box.write_ccp4_map()
   mean_density_end = self.mean_density_at_sites()
   cc_end = self.cc_model_map()
   print >> self.out, "  mean density level: start=%.2fsigma  end=%.2fsigma" \
     % (mean_density_start, mean_density_end)
   print >> self.out, "  model-map CC: start=%.3f  end=%.3f" % (cc_start,
     cc_end)
   sites_final = self.get_selected_sites(hydrogens=False)
   print >> self.out, "  rmsd to starting model: %.3f Angstrom" % \
     sites_final.rms_difference(sites_start)
   t3 = time.time()
   print >> self.out, "  Total build and refine time: %.1fs" % (t3-t1)
def exercise_geo_output(mon_lib_srv, ener_lib):
  pdb_str = """\
CRYST1   18.879   16.714   25.616  90.00  90.00  90.00 P 1
ATOM      1  N   ALA     1      13.515   7.809  20.095  1.00  0.00           N
ATOM      2  CA  ALA     1      13.087   6.532  19.536  1.00  0.00           C
ATOM      3  C   ALA     1      11.716   6.653  18.880  1.00  0.00           C
ATOM      4  O   ALA     1      11.425   5.972  17.896  1.00  0.00           O
ATOM      5  CB  ALA     1      13.065   5.461  20.616  1.00  0.00           C
ATOM      6  N   ALA     2      10.876   7.524  19.431  1.00  0.00           N
ATOM      7  CA  ALA     2       9.535   7.735  18.900  1.00  0.00           C
ATOM      8  C   ALA     2       9.565   8.647  17.678  1.00  0.00           C
ATOM      9  O   ALA     2       8.787   8.471  16.741  1.00  0.00           O
ATOM     10  CB  ALA     2       8.626   8.316  19.973  1.00  0.00           C
ATOM     11  N   ALA     3      10.469   9.622  17.697  1.00  0.00           N
ATOM     12  CA  ALA     3      10.606  10.565  16.593  1.00  0.00           C
ATOM     13  C   ALA     3      11.132   9.871  15.340  1.00  0.00           C
ATOM     14  O   ALA     3      10.687  10.157  14.228  1.00  0.00           O
ATOM     15  CB  ALA     3      11.520  11.714  16.987  1.00  0.00           C
ATOM     16  N   ALA     4      12.081   8.960  15.530  1.00  0.00           N
ATOM     17  CA  ALA     4      12.656   8.209  14.421  1.00  0.00           C
ATOM     18  C   ALA     4      11.624   7.266  13.812  1.00  0.00           C
ATOM     19  O   ALA     4      11.570   7.090  12.595  1.00  0.00           O
ATOM     20  CB  ALA     4      13.879   7.432  14.883  1.00  0.00           C
ATOM     21  N   ALA     5      10.805   6.663  14.669  1.00  0.00           N
ATOM     22  CA  ALA     5       9.753   5.761  14.218  1.00  0.00           C
ATOM     23  C   ALA     5       8.662   6.528  13.481  1.00  0.00           C
ATOM     24  O   ALA     5       8.107   6.045  12.494  1.00  0.00           O
ATOM     25  CB  ALA     5       9.165   5.000  15.396  1.00  0.00           C
ATOM     26  N   ALA     6       8.360   7.728  13.967  1.00  0.00           N
ATOM     27  CA  ALA     6       7.358   8.580  13.338  1.00  0.00           C
ATOM     28  C   ALA     6       7.860   9.106  11.998  1.00  0.00           C
ATOM     29  O   ALA     6       7.078   9.322  11.072  1.00  0.00           O
ATOM     30  CB  ALA     6       6.986   9.732  14.257  1.00  0.00           C
ATOM     31  N   ALA     7       9.169   9.311  11.903  1.00  0.00           N
ATOM     32  CA  ALA     7       9.781   9.787  10.668  1.00  0.00           C
ATOM     33  C   ALA     7       9.912   8.655   9.655  1.00  0.00           C
ATOM     34  O   ALA     7       9.905   8.886   8.446  1.00  0.00           O
ATOM     35  CB  ALA     7      11.141  10.405  10.952  1.00  0.00           C
ATOM     36  N   ALA     8      10.030   7.429  10.157  1.00  0.00           N
ATOM     37  CA  ALA     8      10.152   6.258   9.297  1.00  0.00           C
ATOM     38  C   ALA     8       8.788   5.809   8.786  1.00  0.00           C
ATOM     39  O   ALA     8       8.667   5.312   7.666  1.00  0.00           O
ATOM     40  CB  ALA     8      10.839   5.124  10.041  1.00  0.00           C
ATOM     41  N   ALA     9       7.762   5.988   9.613  1.00  0.00           N
ATOM     42  CA  ALA     9       6.405   5.603   9.243  1.00  0.00           C
ATOM     43  C   ALA     9       5.816   6.576   8.228  1.00  0.00           C
ATOM     44  O   ALA     9       5.000   6.195   7.389  1.00  0.00           O
ATOM     45  CB  ALA     9       5.521   5.526  10.478  1.00  0.00           C
ATOM     46  N   ALA    10       6.235   7.835   8.309  1.00  0.00           N
ATOM     47  CA  ALA    10       5.751   8.864   7.397  1.00  0.00           C
ATOM     48  C   ALA    10       6.434   8.760   6.038  1.00  0.00           C
ATOM     49  O   ALA    10       5.773   8.734   5.000  1.00  0.00           O
ATOM     50  CB  ALA    10       5.966  10.246   7.995  1.00  0.00           C
TER
END
"""
  pdb_inp = iotbx.pdb.input(source_info="peptide",lines=flex.split_lines(pdb_str))
  hierarchy = pdb_inp.construct_hierarchy()
  atoms = hierarchy.atoms()
  sites_cart = atoms.extract_xyz()
  params = ramachandran.master_phil.fetch().extract()
  params.rama_potential = "emsley"
  rama_manager = ramachandran.ramachandran_manager(
      hierarchy, None, params, StringIO())
  out = StringIO()
  rama_manager.show_sorted(
      by_value="residual",
      sites_cart=sites_cart,
      site_labels=[a.id_str() for a in atoms],
      f=out)
  gv = out.getvalue()
  # print out.getvalue()
  # STOP()
  assert gv == """\
Ramachandran plot restraints: 8
Sorted by residual:
phi-psi angles formed by             residual
    pdb=" C   ALA     7 "            1.53e+01
    pdb=" N   ALA     8 "
    pdb=" CA  ALA     8 "
    pdb=" C   ALA     8 "
    pdb=" N   ALA     9 "
phi-psi angles formed by             residual
    pdb=" C   ALA     1 "            1.52e+01
    pdb=" N   ALA     2 "
    pdb=" CA  ALA     2 "
    pdb=" C   ALA     2 "
    pdb=" N   ALA     3 "
phi-psi angles formed by             residual
    pdb=" C   ALA     6 "            1.20e+01
    pdb=" N   ALA     7 "
    pdb=" CA  ALA     7 "
    pdb=" C   ALA     7 "
    pdb=" N   ALA     8 "
phi-psi angles formed by             residual
    pdb=" C   ALA     2 "            1.14e+01
    pdb=" N   ALA     3 "
    pdb=" CA  ALA     3 "
    pdb=" C   ALA     3 "
    pdb=" N   ALA     4 "
phi-psi angles formed by             residual
    pdb=" C   ALA     8 "            1.06e+01
    pdb=" N   ALA     9 "
    pdb=" CA  ALA     9 "
    pdb=" C   ALA     9 "
    pdb=" N   ALA    10 "
phi-psi angles formed by             residual
    pdb=" C   ALA     4 "            1.06e+01
    pdb=" N   ALA     5 "
    pdb=" CA  ALA     5 "
    pdb=" C   ALA     5 "
    pdb=" N   ALA     6 "
phi-psi angles formed by             residual
    pdb=" C   ALA     3 "            1.03e+01
    pdb=" N   ALA     4 "
    pdb=" CA  ALA     4 "
    pdb=" C   ALA     4 "
    pdb=" N   ALA     5 "
phi-psi angles formed by             residual
    pdb=" C   ALA     5 "            7.58e+00
    pdb=" N   ALA     6 "
    pdb=" CA  ALA     6 "
    pdb=" C   ALA     6 "
    pdb=" N   ALA     7 "

"""

  params.rama_potential = "oldfield"
  rama_manager = ramachandran.ramachandran_manager(
      hierarchy, None, params, StringIO())
  out = StringIO()
  rama_manager.show_sorted(
      by_value="residual",
      sites_cart=sites_cart,
      site_labels=[a.id_str() for a in atoms],
      f=out)
  gv = out.getvalue()
  # print out.getvalue()
  # STOP()
  assert not show_diff(gv, """\
Ramachandran plot restraints: 8
Sorted by residual:
phi-psi angles formed by             residual
    pdb=" C   ALA     7 "            1.52e-01
    pdb=" N   ALA     8 "
    pdb=" CA  ALA     8 "
    pdb=" C   ALA     8 "
    pdb=" N   ALA     9 "
phi-psi angles formed by             residual
    pdb=" C   ALA     4 "            1.28e-01
    pdb=" N   ALA     5 "
    pdb=" CA  ALA     5 "
    pdb=" C   ALA     5 "
    pdb=" N   ALA     6 "
phi-psi angles formed by             residual
    pdb=" C   ALA     6 "            5.19e-02
    pdb=" N   ALA     7 "
    pdb=" CA  ALA     7 "
    pdb=" C   ALA     7 "
    pdb=" N   ALA     8 "
phi-psi angles formed by             residual
    pdb=" C   ALA     5 "            3.46e-02
    pdb=" N   ALA     6 "
    pdb=" CA  ALA     6 "
    pdb=" C   ALA     6 "
    pdb=" N   ALA     7 "
phi-psi angles formed by             residual
    pdb=" C   ALA     2 "            2.54e-02
    pdb=" N   ALA     3 "
    pdb=" CA  ALA     3 "
    pdb=" C   ALA     3 "
    pdb=" N   ALA     4 "
phi-psi angles formed by             residual
    pdb=" C   ALA     8 "            2.00e-02
    pdb=" N   ALA     9 "
    pdb=" CA  ALA     9 "
    pdb=" C   ALA     9 "
    pdb=" N   ALA    10 "
phi-psi angles formed by             residual
    pdb=" C   ALA     1 "            1.21e-02
    pdb=" N   ALA     2 "
    pdb=" CA  ALA     2 "
    pdb=" C   ALA     2 "
    pdb=" N   ALA     3 "
phi-psi angles formed by             residual
    pdb=" C   ALA     3 "            9.28e-03
    pdb=" N   ALA     4 "
    pdb=" CA  ALA     4 "
    pdb=" C   ALA     4 "
    pdb=" N   ALA     5 "

""")
def minimize_wrapper_for_ramachandran(
        hierarchy,
        xrs,
        original_pdb_h,
        excl_string_selection,
        grm=None,
        log=None,
        ncs_restraints_group_list=[],
        ss_annotation=None,
        mon_lib_srv=None,
        ener_lib=None,
        rotamer_manager=None,
        reference_rotamers=True,
        number_of_cycles=1,
        run_first_minimization_without_reference=False,
        oldfield_weight_scale=3,
        oldfield_plot_cutoff=0.03,
        nonbonded_weight=500,
        reference_sigma=0.7):
    """ Wrapper around geometry minimization specifically tuned for eliminating
  Ramachandran outliers.
  """
    try:
        import cPickle as pickle
    except ImportError:
        import pickle
    from time import time
    from mmtbx.monomer_library.pdb_interpretation import grand_master_phil_str
    from mmtbx.geometry_restraints import reference
    from mmtbx.command_line.geometry_minimization import \
        get_geometry_restraints_manager
    from mmtbx.geometry_restraints.torsion_restraints.reference_model import \
        reference_model, reference_model_params
    from libtbx.utils import null_out
    from scitbx.array_family import flex
    if log is None:
        log = null_out()
    # assert hierarchy.atoms_size()==xrs.scatterers().size(), "%d %d" % (
    #     hierarchy.atoms_size(), xrs.scatterers().size())
    params_line = grand_master_phil_str
    params = iotbx.phil.parse(input_string=params_line,
                              process_includes=True).extract()
    params.pdb_interpretation.clash_guard.nonbonded_distance_threshold = None
    params.pdb_interpretation.peptide_link.ramachandran_restraints = True
    params.pdb_interpretation.peptide_link.oldfield.weight_scale = oldfield_weight_scale
    params.pdb_interpretation.peptide_link.oldfield.plot_cutoff = oldfield_plot_cutoff
    params.pdb_interpretation.nonbonded_weight = nonbonded_weight
    params.pdb_interpretation.c_beta_restraints = True
    params.pdb_interpretation.max_reasonable_bond_distance = None
    params.pdb_interpretation.peptide_link.apply_peptide_plane = True
    params.pdb_interpretation.ncs_search.enabled = True
    params.pdb_interpretation.restraints_library.rdl = True

    processed_pdb_files_srv = mmtbx.utils.\
        process_pdb_file_srv(
            crystal_symmetry= xrs.crystal_symmetry(),
            pdb_interpretation_params = params.pdb_interpretation,
            stop_for_unknowns         = False,
            log=log,
            cif_objects=None)
    processed_pdb_file, junk = processed_pdb_files_srv.\
        process_pdb_files(raw_records=flex.split_lines(hierarchy.as_pdb_string()))

    mon_lib_srv = processed_pdb_files_srv.mon_lib_srv
    ener_lib = processed_pdb_files_srv.ener_lib

    ncs_restraints_group_list = []
    if processed_pdb_file.ncs_obj is not None:
        ncs_restraints_group_list = processed_pdb_file.ncs_obj.get_ncs_restraints_group_list(
        )

    if grm is None:
        grm = get_geometry_restraints_manager(processed_pdb_file,
                                              xrs,
                                              params=params)
    else:
        grm.geometry.pair_proxies(sites_cart=hierarchy.atoms().extract_xyz())
        if grm.geometry.ramachandran_manager is not None:
            grm.geometry.ramachandran_manager.update_phi_psi_targets(
                sites_cart=hierarchy.atoms().extract_xyz())

    if reference_rotamers and original_pdb_h is not None:
        # make selection excluding rotamer outliers
        from mmtbx.rotamer.rotamer_eval import RotamerEval
        # print "Excluding rotamer outliers"
        if rotamer_manager is None:
            rotamer_manager = RotamerEval(mon_lib_srv=mon_lib_srv)
        non_rot_outliers_selection = flex.bool(hierarchy.atoms_size(), False)
        for model in original_pdb_h.models():
            for chain in model.chains():
                for conf in chain.conformers():
                    for res in conf.residues():
                        ev = rotamer_manager.evaluate_residue_2(res)
                        if ev != "OUTLIER" or ev is None:
                            for a in res.atoms():
                                non_rot_outliers_selection[a.i_seq] = True
                        # else:
                        #   print "  ", res.id_str()

        rm_params = reference_model_params.extract()
        rm_params.reference_model.enabled = True
        rm_params.reference_model.strict_rotamer_matching = False
        rm_params.reference_model.main_chain = False
        rm = reference_model(processed_pdb_file=processed_pdb_file,
                             reference_file_list=None,
                             reference_hierarchy_list=[original_pdb_h],
                             mon_lib_srv=mon_lib_srv,
                             ener_lib=ener_lib,
                             has_hd=None,
                             params=rm_params.reference_model,
                             selection=non_rot_outliers_selection,
                             log=log)
        rm.show_reference_summary(log=log)
        grm.geometry.adopt_reference_dihedral_manager(rm)

    # dealing with SS
    if ss_annotation is not None:
        from mmtbx.secondary_structure import manager
        ss_manager = manager(pdb_hierarchy=hierarchy,
                             geometry_restraints_manager=grm.geometry,
                             sec_str_from_pdb_file=ss_annotation,
                             params=None,
                             mon_lib_srv=mon_lib_srv,
                             verbose=-1,
                             log=log)
        grm.geometry.set_secondary_structure_restraints(ss_manager=ss_manager,
                                                        hierarchy=hierarchy,
                                                        log=log)

    # grm pickle-unpickle
    # t0 = time()
    # prefix="grm"
    # pklfile = open("%s.pkl" % prefix, 'wb')
    # pickle.dump(grm.geometry, pklfile)
    # pklfile.close()
    # t1 = time()
    # pklfile = open("%s.pkl" % prefix, 'rb')
    # grm_from_file = pickle.load(pklfile)
    # pklfile.close()
    # t2 = time()
    # print "Time pickling/unpickling: %.4f, %.4f" % (t1-t0, t2-t1)
    # grm.geometry=grm_from_file

    if run_first_minimization_without_reference:
        obj = run2(restraints_manager=grm,
                   pdb_hierarchy=hierarchy,
                   correct_special_position_tolerance=1.0,
                   ncs_restraints_group_list=ncs_restraints_group_list,
                   max_number_of_iterations=300,
                   number_of_macro_cycles=number_of_cycles,
                   bond=True,
                   nonbonded=True,
                   angle=True,
                   dihedral=True,
                   chirality=True,
                   planarity=True,
                   fix_rotamer_outliers=True,
                   log=log)

    if original_pdb_h is not None:
        if len(excl_string_selection) == 0:
            excl_string_selection = "all"
        asc = original_pdb_h.atom_selection_cache()
        sel = asc.selection(
            "(%s) and (name CA or name C or name N or name O)" %
            excl_string_selection)

        grm.geometry.append_reference_coordinate_restraints_in_place(
            reference.add_coordinate_restraints(
                sites_cart=original_pdb_h.atoms().extract_xyz().select(sel),
                selection=sel,
                sigma=reference_sigma,
                top_out_potential=True))
    # grm.geometry.write_geo_file(
    #     sites_cart=hierarchy.atoms().extract_xyz(),
    #     site_labels=[atom.id_str() for atom in hierarchy.atoms()],
    #     file_name="last_gm.geo")
    obj = run2(restraints_manager=grm,
               pdb_hierarchy=hierarchy,
               correct_special_position_tolerance=1.0,
               ncs_restraints_group_list=ncs_restraints_group_list,
               max_number_of_iterations=300,
               number_of_macro_cycles=number_of_cycles,
               bond=True,
               nonbonded=True,
               angle=True,
               dihedral=True,
               chirality=True,
               planarity=True,
               fix_rotamer_outliers=True,
               log=log)
    grm.geometry.reference_dihedral_manager = None