def main(filename=None):
    if filename is None:
        filename = 'ala_term.pdb'
        with open(filename, 'w') as f:
            f.write(pdb_str)
    pdb_inp = pdb.input(filename)
    hierarchy = pdb_inp.construct_hierarchy()
    params = mmtbx.model.manager.get_default_pdb_interpretation_params()
    params.pdb_interpretation.use_neutron_distances = True
    params.pdb_interpretation.restraints_library.cdl = False
    print(len(hierarchy.atoms()))
    asc = hierarchy.atom_selection_cache()
    sel = asc.selection("element H or element D")
    print('sel len', len(sel))
    print(len(hierarchy.atoms()))
    hierarchy.show()

    model = mmtbx.model.manager(
        model_input=None,
        pdb_hierarchy=hierarchy,
        crystal_symmetry=pdb_inp.crystal_symmetry(),
        log=null_out(),
    )
    print('m1', len(model.get_hierarchy().atoms()))
    for atom in model.get_hierarchy().atoms():
        print(atom.format_atom_record())
    model.process(make_restraints=True,
                  grm_normalization=True,
                  pdb_interpretation_params=params)
    print('m2', len(model.get_hierarchy().atoms()))
    for atom in model.get_hierarchy().atoms():
        print(atom.format_atom_record())
    model.idealize_h_riding()
    model.set_occupancies(0., selection=sel)
Exemple #2
0
def run(
    pdb_filename,
    model_completion=True,
    keep_alt_loc=False,
    skip_validation=False,
    calculate_charge=False,
    append_to_end_of_model=False,
):
    #
    # do all *.pdb in a directory
    #
    if os.path.isdir(pdb_filename):
        loop_over_dir(pdb_filename)
        return
    #
    # fetch a PDB code
    #
    if pdb_filename.find(".pdb") == -1:
        if not os.path.exists('%s.pdb' % pdb_filename):
            run_fetch_pdb(pdb_filename)
        pdb_filename = '%s.pdb' % pdb_filename

    if 1:  # read file option
        ppf = hierarchy_utils.get_processed_pdb(pdb_filename=pdb_filename)
    elif 0:  # raw records example
        pdb_inp = iotbx.pdb.input(pdb_filename)
        hierarchy = pdb_inp.construct_hierarchy()
        raw_records = []
        for atom in hierarchy.atoms():
            raw_records.append(atom.format_atom_record())
        ppf = hierarchy_utils.get_processed_pdb(raw_records=raw_records)
    else:  # from hierarchy
        pdb_inp = iotbx.pdb.input(pdb_filename)
        hierarchy = pdb_inp.construct_hierarchy()
        ppf = hierarchy_utils.get_processed_pdb(
            pdb_inp=hierarchy.as_pdb_input())

    if not skip_validation:
        initial_model_statistics = mmtbx.model.statistics.geometry(
            pdb_hierarchy=ppf.all_chain_proxies.pdb_hierarchy)

    # should use cctbx
    if keep_alt_loc: pass
    else:
        hierarchy = remove_alternative_locations(
            ppf.all_chain_proxies.pdb_hierarchy)

    ppf.all_chain_proxies.pdb_hierarchy.shift_to_origin(
        ppf.all_chain_proxies.pdb_inp.crystal_symmetry())
    ppf.all_chain_proxies.pdb_hierarchy.remove_residue_groups_with_atoms_on_special_positions_selective(
        ppf.all_chain_proxies.pdb_inp.crystal_symmetry())
    if 0:
        output = hierarchy_utils.write_hierarchy(
            pdb_filename,  # uses to get output filename
            ppf.all_chain_proxies.pdb_inp,
            ppf.all_chain_proxies.pdb_hierarchy,
            "temp0")
        ppf = hierarchy_utils.get_processed_pdb(pdb_filename=output)
    else:
        raw_records = hierarchy_utils.get_raw_records(
            ppf.all_chain_proxies.pdb_inp,
            ppf.all_chain_proxies.pdb_hierarchy,
        )
        ppf = hierarchy_utils.get_processed_pdb(raw_records=raw_records)
    hetero_charges = charges.get_hetero_charges(
        ppf.all_chain_proxies.pdb_inp,
        ppf.all_chain_proxies.pdb_hierarchy,
    )

    if not hetero_charges:
        # some defaults
        hetero_charges = charges.default_ion_charges
    inter_residue_bonds = charges.get_inter_residue_bonds(ppf)
    #
    # extends side chains and add hydrogens
    #
    if model_completion:
        use_capping_hydrogens = False
        fname = 'complete'
    else:
        use_capping_hydrogens = True
        fname = 'capping'
        #assert 0 # model has H
    ppf = completion.complete_pdb_hierarchy(
        ppf.all_chain_proxies.pdb_hierarchy,
        ppf.geometry_restraints_manager(),
        pdb_filename=pdb_filename,
        pdb_inp=ppf.all_chain_proxies.pdb_inp,
        use_capping_hydrogens=use_capping_hydrogens,
        append_to_end_of_model=append_to_end_of_model,
    )

    if calculate_charge:
        new_pdb_filename = hierarchy_utils.write_hierarchy(
            pdb_filename,  # uses to get output filename
            ppf.all_chain_proxies.pdb_inp,
            ppf.all_chain_proxies.pdb_hierarchy,
            fname)
        ## need now inter_residue_bonds because of added hydrogens
        ##  maybe update the bond table!!!
        ppf = hierarchy_utils.get_processed_pdb(pdb_filename=new_pdb_filename)
        inter_residue_bonds = charges.get_inter_residue_bonds(ppf,
                                                              verbose=True)
        total_charge = charges.calculate_pdb_hierarchy_charge(
            ppf.all_chain_proxies.pdb_hierarchy,
            hetero_charges=hetero_charges,
            inter_residue_bonds=inter_residue_bonds,
        )
        print "total_charge", total_charge

    # Idealize H as riding
    params = mmtbx.model.manager.get_default_pdb_interpretation_params()
    params.pdb_interpretation.use_neutron_distances = True
    h = ppf.all_chain_proxies.pdb_hierarchy
    asc = h.atom_selection_cache()
    sel = asc.selection("element H or element D")
    model = mmtbx.model.manager(
        model_input=None,
        build_grm=True,
        pdb_hierarchy=ppf.all_chain_proxies.pdb_hierarchy,
        pdb_interpretation_params=params,
        crystal_symmetry=ppf.all_chain_proxies.pdb_inp.crystal_symmetry(),
        log=null_out())
    model.idealize_h_riding()
    model.set_occupancies(value=0, selection=sel)

    ## after no error getting total charge, write the completed pdb file
    hierarchy_utils.write_hierarchy(
        pdb_filename,  # uses to get output filename
        ppf.all_chain_proxies.pdb_inp,
        model.get_hierarchy(),
        fname)
    if not skip_validation:
        final_model_statistics = mmtbx.model.statistics.geometry(
            pdb_hierarchy=ppf.all_chain_proxies.pdb_hierarchy)
Exemple #3
0
def add(model,
        use_neutron_distances=False,
        adp_scale=1,
        exclude_water=True,
        protein_only=False,
        stop_for_unknowns=True,
        remove_first=True):
    if (remove_first):
        model = model.select(~model.get_hd_selection())
    pdb_hierarchy = model.get_hierarchy()
    mon_lib_srv = model.get_mon_lib_srv()
    get_class = iotbx.pdb.common_residue_names_get_class
    """
  for pmodel in pdb_hierarchy.models():
    for chain in pmodel.chains():
      for residue_group in chain.residue_groups():
        for conformer in residue_group.conformers():
          for residue in conformer.residues():
            print list(residue.atoms().extract_name())
  """
    #XXX This breaks for 1jxt, residue 2, TYR
    for chain in pdb_hierarchy.only_model().chains():
        for rg in chain.residue_groups():
            for ag in rg.atom_groups():
                #print list(ag.atoms().extract_name())
                if (get_class(name=ag.resname) == "common_water"): continue
                if (protein_only
                        and not ag.resname.strip().upper() in aa_codes):
                    continue
                actual = [a.name.strip().upper() for a in ag.atoms()]
                mlq = mon_lib_query(residue=ag.resname,
                                    mon_lib_srv=mon_lib_srv)
                expected_all = mlq.atom_dict().keys()
                expected_h = []
                for k, v in mlq.atom_dict().iteritems():
                    if (v.type_symbol == "H"): expected_h.append(k)
                missing_h = list(set(expected_h).difference(set(actual)))
                if 0: print ag.resname, missing_h
                new_xyz = ag.atoms().extract_xyz().mean()
                hetero = ag.atoms()[0].hetero
                for mh in missing_h:
                    # TODO: this should be probably in a central place
                    if len(mh) < 4: mh = (' ' + mh).ljust(4)
                    a = (iotbx.pdb.hierarchy.atom().set_name(
                        new_name=mh).set_element(new_element="H").set_xyz(
                            new_xyz=new_xyz).set_hetero(new_hetero=hetero))
                    ag.append_atom(a)
    pdb_hierarchy.atoms().reset_serial()
    #pdb_hierarchy.sort_atoms_in_place()
    p = mmtbx.model.manager.get_default_pdb_interpretation_params()
    p.pdb_interpretation.clash_guard.nonbonded_distance_threshold = None
    p.pdb_interpretation.use_neutron_distances = use_neutron_distances
    p.pdb_interpretation.proceed_with_excessive_length_bonds = True
    #p.pdb_interpretation.restraints_library.cdl=False # XXX this triggers a bug !=360
    ro = model.get_restraint_objects()
    model = mmtbx.model.manager(model_input=None,
                                pdb_hierarchy=pdb_hierarchy,
                                build_grm=True,
                                stop_for_unknowns=stop_for_unknowns,
                                crystal_symmetry=model.crystal_symmetry(),
                                restraint_objects=ro,
                                pdb_interpretation_params=p,
                                log=null_out())
    #  # Remove lone H
    #  sel_h = model.get_hd_selection()
    #  sel_isolated = model.isolated_atoms_selection()
    #  sel_lone = sel_h & sel_isolated
    #  model = model.select(~sel_lone)
    # Only keep H which have been parameterized in riding H procedure
    sel_h = model.get_hd_selection()
    model.setup_riding_h_manager()
    sel_h_in_para = flex.bool(
        [bool(x) for x in model.riding_h_manager.h_parameterization])
    sel_h_not_in_para = sel_h_in_para.exclusive_or(sel_h)
    model = model.select(~sel_h_not_in_para)

    model = exclude_h_on_SS(model=model)
    model = exclude_h_on_coordinated_S(model=model)
    # Reset occupancies, ADPs and idealize
    model.reset_adp_for_hydrogens(scale=adp_scale)
    model.reset_occupancy_for_hydrogens_simple()
    model.idealize_h_riding()
    return model
Exemple #4
0
def add(model,
        use_neutron_distances = False,
        adp_scale             = 1,
        exclude_water         = True,
        protein_only          = False,
        stop_for_unknowns     = False,
        keep_existing_H       = False):
  """
  Add H atoms to a model

  Parameters
  ----------
  use_neutron_distances : bool
    use neutron distances instead of X-ray

  adp_scale : float
    scale factor for isotropic B of H atoms.
    B(H-atom) = adp_scale * B(parent non-H atom)

  keep_existing_H : bool
    keep existing H atoms in model, only place missing H

  Returns
  -------
  model
      mmtbx model object with H atoms
  """
  model_has_bogus_cs = False

  # TODO temporary fix until the code is moved to model class
  # check if box cussion of 5 A is enough to prevent symm contacts
  cs = model.crystal_symmetry()
  if cs is None:
    model = shift_and_box_model(model = model)
    model_has_bogus_cs = True

  # Remove existing H if requested
  if( not keep_existing_H):
    model = model.select(~model.get_hd_selection())

  pdb_hierarchy = model.get_hierarchy()
  mon_lib_srv = model.get_mon_lib_srv()
  """
  for pmodel in pdb_hierarchy.models():
    for chain in pmodel.chains():
      for residue_group in chain.residue_groups():
        for conformer in residue_group.conformers():
          for residue in conformer.residues():
            print list(residue.atoms().extract_name())
  """
  add_missing_H_atoms_at_bogus_position(pdb_hierarchy = pdb_hierarchy,
                                        mon_lib_srv   = mon_lib_srv,
                                        protein_only = protein_only)
  pdb_hierarchy.atoms().reset_serial()
  #pdb_hierarchy.sort_atoms_in_place()
  p = mmtbx.model.manager.get_default_pdb_interpretation_params()
  p.pdb_interpretation.clash_guard.nonbonded_distance_threshold=None
  p.pdb_interpretation.use_neutron_distances = use_neutron_distances
  p.pdb_interpretation.proceed_with_excessive_length_bonds=True
  #p.pdb_interpretation.restraints_library.cdl=False # XXX this triggers a bug !=360
  ro = model.get_restraint_objects()
  model = mmtbx.model.manager(
    model_input               = None,
    pdb_hierarchy             = pdb_hierarchy,
    build_grm                 = True,
    stop_for_unknowns         = stop_for_unknowns,
    crystal_symmetry          = model.crystal_symmetry(),
    restraint_objects         = ro,
    pdb_interpretation_params = p,
    log                       = null_out())

#  f = open("intermediate1.pdb","w")
#  f.write(model.model_as_pdb())

#  # Remove lone H
#  sel_h = model.get_hd_selection()
#  sel_isolated = model.isolated_atoms_selection()
#  sel_lone = sel_h & sel_isolated
#  model = model.select(~sel_lone)
#
  # Only keep H that have been parameterized in riding H procedure
  sel_h = model.get_hd_selection()
  model.setup_riding_h_manager(use_ideal_dihedral = True)
  sel_h_in_para = flex.bool(
    [bool(x) for x in model.riding_h_manager.h_parameterization])
  sel_h_not_in_para = sel_h_in_para.exclusive_or(sel_h)
  model = model.select(~sel_h_not_in_para)

  model = exclude_h_on_SS(model = model)
  model = exclude_h_on_coordinated_S(model = model)

#  f = open("intermediate2.pdb","w")
#  f.write(model.model_as_pdb())

  # Reset occupancies, ADPs and idealize
  model.reset_adp_for_hydrogens(scale = adp_scale)
  model.reset_occupancy_for_hydrogens_simple()
  model.idealize_h_riding()
  #
  return model