def run(args):
    log = sys.stdout
    if (len(args) == 0): args = ["--help"]
    command_line = (option_parser(usage="%s [options] pdb_file" %
                                  libtbx.env.dispatcher_name).option(
                                      None,
                                      "--buffer_layer",
                                      action="store",
                                      type="float",
                                      default=5)).process(args=args, nargs=1)
    pdb_inp = iotbx.pdb.input(file_name=command_line.args[0])
    model = mmtbx.model.manager(model_input=pdb_inp)
    box = uctbx.non_crystallographic_unit_cell_with_the_sites_in_its_center(
        sites_cart=model.get_sites_cart(),
        buffer_layer=command_line.options.buffer_layer)
    model.set_sites_cart(box.sites_cart)
    # Bad hack, never repeat. In fact, all the boxing functionality should
    # go into mmtbx.model.manager
    model._crystal_symmetry = box.crystal_symmetry()
    print('REMARK %s --buffer-layer=%.6g %s' %
          (libtbx.env.dispatcher_name, command_line.options.buffer_layer,
           show_string(command_line.args[0])),
          file=log)
    print('REMARK %s' % date_and_time(), file=log)
    print(model.model_as_pdb(), file=log)
Example #2
0
def get_inputs(args, log, master_params):
    """
  Eventually, this will be centralized.
  """
    cmdline = process_command_line_with_files(args=args,
                                              master_phil=master_params,
                                              pdb_file_def='model_file_name')
    params = cmdline.work.extract()
    # Model
    file_names = params.model_file_name
    pdb_combined = combine_unique_pdb_files(file_names=file_names)
    pdb_inp = iotbx.pdb.input(source_info=None,
                              lines=flex.std_string(pdb_combined.raw_records),
                              raise_sorry_if_format_error=True)
    # Crystal symmetry
    fake_crystal_symmetry = False
    crystal_symmetry = pdb_inp.crystal_symmetry()
    if (crystal_symmetry is None or crystal_symmetry.is_empty()
            or crystal_symmetry.is_nonsence()):
        fake_crystal_symmetry = True
        from cctbx import uctbx
        crystal_symmetry = \
          uctbx.non_crystallographic_unit_cell_with_the_sites_in_its_center(
            sites_cart=pdb_inp.atoms().extract_xyz(),
            buffer_layer=5).crystal_symmetry()
    #
    model = mmtbx.model.manager(model_input=pdb_inp,
                                crystal_symmetry=crystal_symmetry)
    #
    return group_args(params=params,
                      pdb_file_names=file_names,
                      model=model,
                      fake_crystal_symmetry=fake_crystal_symmetry)
def run(args):
  if (len(args) == 0): args = ["--help"]
  from libtbx.option_parser import option_parser
  import libtbx.load_env
  command_line = (option_parser(
    usage="%s [options] pdb_file" % libtbx.env.dispatcher_name)
    .option(None, "--buffer_layer",
      action="store",
      type="float",
      default=5)
  ).process(args=args, nargs=1)
  import iotbx.pdb
  pdb_inp = iotbx.pdb.input(file_name=command_line.args[0])
  atoms = pdb_inp.atoms()
  from cctbx import uctbx
  box = uctbx.non_crystallographic_unit_cell_with_the_sites_in_its_center(
    sites_cart=atoms.extract_xyz(),
    buffer_layer=command_line.options.buffer_layer)
  atoms.set_xyz(new_xyz=box.sites_cart)
  from libtbx.str_utils import show_string
  print 'REMARK %s --buffer-layer=%.6g %s' % (
    libtbx.env.dispatcher_name,
    command_line.options.buffer_layer,
    show_string(command_line.args[0]))
  from libtbx.utils import date_and_time
  print 'REMARK %s' % date_and_time()
  print iotbx.pdb.format_cryst1_record(crystal_symmetry=box.crystal_symmetry())
  print pdb_inp.construct_hierarchy().as_pdb_string(append_end=True),
Example #4
0
def reorder(path, prefix):
  pdb_inp = iotbx.pdb.input(file_name="%s%s.pdb"%(path,prefix))
  h = pdb_inp.construct_hierarchy()
  rg = list(h.residue_groups())
  rg.reverse()
  #
  c = iotbx.pdb.hierarchy.chain(id="A")
  for rg_ in rg:
    c.append_residue_group(rg_.detached_copy())
  #
  r = iotbx.pdb.hierarchy.root()
  m = iotbx.pdb.hierarchy.model()
  r.append_model(m)
  m.append_chain(c)  
  atoms = r.atoms()
  #
  box = uctbx.non_crystallographic_unit_cell_with_the_sites_in_its_center(
    sites_cart=atoms.extract_xyz(),
    buffer_layer=3.)
  atoms.set_xyz(new_xyz=box.sites_cart)
  #
  fo = open("%s-new.pdb"%prefix,"w")
  print >> fo, iotbx.pdb.format_cryst1_record(
    crystal_symmetry=box.crystal_symmetry())
  for a in atoms:
    a = a.set_b(30.)
    print >> fo, a.format_atom_record()
  fo.close()
def run(args):
    log = sys.stdout
    if (len(args) == 0): args = ["--help"]
    command_line = (option_parser(usage="%s [options] pdb_file" %
                                  libtbx.env.dispatcher_name).option(
                                      None,
                                      "--buffer_layer",
                                      action="store",
                                      type="float",
                                      default=5)).process(args=args, nargs=1)
    pdb_inp = iotbx.pdb.input(file_name=command_line.args[0])
    atoms = pdb_inp.atoms()
    box = uctbx.non_crystallographic_unit_cell_with_the_sites_in_its_center(
        sites_cart=atoms.extract_xyz(),
        buffer_layer=command_line.options.buffer_layer)
    atoms.set_xyz(new_xyz=box.sites_cart)
    print >> log, 'REMARK %s --buffer-layer=%.6g %s' % (
        libtbx.env.dispatcher_name, command_line.options.buffer_layer,
        show_string(command_line.args[0]))
    print >> log, 'REMARK %s' % date_and_time()
    iotbx.pdb.write_whole_pdb_file(
        output_file=log,
        pdb_hierarchy=pdb_inp.construct_hierarchy(),
        crystal_symmetry=box.crystal_symmetry(),
        ss_annotation=pdb_inp.extract_secondary_structure(log=null_out()))
Example #6
0
def generate_perfect_helix(
    rs_values,
    ts_values,
    angular_rs_values,
    radial_eta,
    angular_eta,
    angular_zeta,
    n_residues=10,
    residue_code="G",
):
    """
  Compute AEV values for the perfect helix.
  """
    perfect_helix_ph = ssb.secondary_structure_from_sequence(
        ssb.alpha_helix_str, residue_code * n_residues)
    box = uctbx.non_crystallographic_unit_cell_with_the_sites_in_its_center(
        sites_cart=perfect_helix_ph.atoms().extract_xyz(), buffer_layer=5)
    model = mmtbx.model.manager(model_input=None,
                                crystal_symmetry=box.crystal_symmetry(),
                                pdb_hierarchy=perfect_helix_ph,
                                build_grm=True,
                                log=null_out())
    return AEV(
        model=model,
        rs_values=rs_values,
        ts_values=ts_values,
        angular_rs_values=angular_rs_values,
        radial_eta=radial_eta,
        angular_eta=angular_eta,
        angular_zeta=angular_zeta,
    ).get_values()
Example #7
0
def exercise_non_crystallographic_unit_cell_with_the_sites_in_its_center():
    sites_cart = flex.vec3_double([(-5., -5., -5.)])
    box = uctbx.non_crystallographic_unit_cell_with_the_sites_in_its_center(
        sites_cart=sites_cart, buffer_layer=5)
    assert approx_equal(box.unit_cell.parameters(), (10, 10, 10, 90, 90, 90))
    assert approx_equal(box.sites_cart, [(5.0, 5.0, 5.0)])
    assert box.crystal_symmetry().space_group_info().type().number() == 1
Example #8
0
def exercise_non_crystallographic_unit_cell_with_the_sites_in_its_center():
  sites_cart = flex.vec3_double([(-5.,-5.,-5.)])
  box = uctbx.non_crystallographic_unit_cell_with_the_sites_in_its_center(
                    sites_cart   = sites_cart,
                    buffer_layer = 5)
  assert approx_equal(box.unit_cell.parameters(), (10, 10, 10, 90, 90, 90))
  assert approx_equal(box.sites_cart, [(5.0, 5.0, 5.0)])
  assert box.crystal_symmetry().space_group_info().type().number() == 1
Example #9
0
    def run(self):
        # I'm guessing self.data_manager, self.params and self.logger
        # are already defined here...

        # this must be mmtbx.model.manager?
        model = self.data_manager.get_model()
        atoms = model.get_atoms()
        all_bsel = flex.bool(atoms.size(), False)
        for selection_string in self.params.atom_selection_program.inselection:
            print("Selecting '%s'" % selection_string, file=self.logger)
            isel = model.iselection(string=selection_string)
            all_bsel.set_selected(isel, True)
            if self.params.atom_selection_program.write_pdb_file is None:
                print("  %d atom%s selected" % plural_s(isel.size()),
                      file=self.logger)
                for atom in atoms.select(isel):
                    print("    %s" % atom.format_atom_record(),
                          file=self.logger)
        print("", file=self.logger)
        if self.params.atom_selection_program.write_pdb_file is not None:
            print("Writing file:",
                  show_string(
                      self.params.atom_selection_program.write_pdb_file),
                  file=self.logger)
            ss_ann = model.get_ss_annotation()
            if not model.crystal_symmetry() or \
              (not model.crystal_symmetry().unit_cell()):
                model = shift_and_box_model(model, shift_model=False)
            selected_model = model.select(all_bsel)
            if (ss_ann is not None):
                selected_model.set_ss_annotation(ss_ann.\
                    filter_annotation(
                        hierarchy=selected_model.get_hierarchy(),
                        asc=selected_model.get_atom_selection_cache(),
                        remove_short_annotations=False,
                        remove_3_10_helices=False,
                        remove_empty_annotations=True,
                        concatenate_consecutive_helices=False,
                        split_helices_with_prolines=False,
                        filter_sheets_with_long_hbonds=False))
            if self.params.atom_selection_program.cryst1_replacement_buffer_layer is not None:
                box = uctbx.non_crystallographic_unit_cell_with_the_sites_in_its_center(
                    sites_cart=selected_model.get_atoms().extract_xyz(),
                    buffer_layer=self.params.atom_selection_program.
                    cryst1_replacement_buffer_layer)
                sp = crystal.special_position_settings(box.crystal_symmetry())
                sites_frac = box.sites_frac()
                xrs_box = selected_model.get_xray_structure(
                ).replace_sites_frac(box.sites_frac())
                xray_structure_box = xray.structure(sp, xrs_box.scatterers())
                selected_model.set_xray_structure(xray_structure_box)
            pdb_str = selected_model.model_as_pdb()
            f = open(self.params.atom_selection_program.write_pdb_file, 'w')
            f.write(pdb_str)
            f.close()
            print("", file=self.logger)
def run(args, command_name=libtbx.env.dispatcher_name):
    parser = argparse.ArgumentParser(
        prog=command_name,
        usage='%s pdb_file "atom_selection" [...]' % command_name)
    parser.add_argument("file_name",
                        nargs=1,
                        help="File name of the model file")
    parser.add_argument(
        "inselections",
        help="Atom selection strings",
        nargs='+',
    )
    parser.add_argument("--write-pdb-file",
                        action="store",
                        help="write selected atoms to new PDB file",
                        default=None)
    parser.add_argument(
        "--cryst1-replacement-buffer-layer",
        action="store",
        type=float,
        help="replace CRYST1 with pseudo unit cell covering the selected"
        " atoms plus a surrounding buffer layer",
        default=None)
    co = parser.parse_args(args)
    pdb_inp = iotbx.pdb.input(file_name=co.file_name[0])
    model = mmtbx.model.manager(model_input=pdb_inp, process_input=True)
    atoms = model.get_atoms()
    all_bsel = flex.bool(atoms.size(), False)
    for selection_string in co.inselections:
        print selection_string
        isel = model.iselection(selstr=selection_string)
        all_bsel.set_selected(isel, True)
        if (not co.write_pdb_file):
            print "  %d atom%s selected" % plural_s(isel.size())
            for atom in atoms.select(isel):
                print "    %s" % atom.format_atom_record()
    print
    if (co.write_pdb_file):
        print "Writing file:", show_string(co.write_pdb_file)
        selected_model = model.select(all_bsel)
        if (co.cryst1_replacement_buffer_layer is not None):
            box = uctbx.non_crystallographic_unit_cell_with_the_sites_in_its_center(
                sites_cart=selected_model.get_atoms().extract_xyz(),
                buffer_layer=co.cryst1_replacement_buffer_layer)
            sp = crystal.special_position_settings(box.crystal_symmetry())
            sites_frac = box.sites_frac()
            xrs_box = selected_model.get_xray_structure().replace_sites_frac(
                box.sites_frac())
            xray_structure_box = xray.structure(sp, xrs_box.scatterers())
            selected_model.set_xray_structure(xray_structure_box)
        pdb_str = selected_model.model_as_pdb()
        f = open(co.write_pdb_file, 'w')
        f.write(pdb_str)
        f.close()
        print
Example #11
0
    def process_inputs(self, prefix):
        broadcast(m=prefix, log=self.log)
        self.pdb_file_names = list(self.inputs.pdb_file_names)
        if (self.params.file_name is not None):
            self.pdb_file_names.append(self.params.file_name)

        #=================================================
        cs = self.inputs.crystal_symmetry
        is_non_crystallographic_unit_cell = False
        import iotbx.pdb
        pdb_combined = combine_unique_pdb_files(file_names=self.pdb_file_names)
        pdb_inp = iotbx.pdb.input(lines=pdb_combined.raw_records,
                                  source_info=None)
        if (cs is None):
            cs = pdb_inp.crystal_symmetry()
        if (cs is None):
            is_non_crystallographic_unit_cell = True
            box = uctbx.non_crystallographic_unit_cell_with_the_sites_in_its_center(
                sites_cart=pdb_inp.atoms().extract_xyz(), buffer_layer=10)
            cs = box.crystal_symmetry()
        cif_objects = list(self.inputs.cif_objects)
        if (len(self.params.restraints) > 0):
            import iotbx.cif
            for file_name in self.params.restraints:
                cif_object = iotbx.cif.reader(file_path=file_name,
                                              strict=False).model()
                cif_objects.append((file_name, cif_object))
        if (self.params.restraints_directory is not None):
            restraint_files = os.listdir(self.params.restraints_directory)
            for file_name in restraint_files:
                if (file_name.endswith(".cif")):
                    full_path = os.path.join(self.params.restraints_directory,
                                             file_name)
                    cif_object = iotbx.cif.reader(file_path=full_path,
                                                  strict=False).model()
                    cif_objects.append((full_path, cif_object))
        self.model = mmtbx.model.manager(
            model_input=pdb_inp,
            crystal_symmetry=cs,
            restraint_objects=cif_objects,
            stop_for_unknowns=self.params.stop_for_unknowns,
            log=self.log)
        self.model.process(pdb_interpretation_params=self.params,
                           make_restraints=True)
        self.ncs_obj = self.model.get_ncs_obj()
        self.output_crystal_symmetry = not is_non_crystallographic_unit_cell
        self.sites_cart_start = self.model.get_xray_structure().sites_cart(
        ).deep_copy()
        if (self.params.show_states):
            self.states_collector = mmtbx.utils.states(
                xray_structure=self.model.get_xray_structure(),
                pdb_hierarchy=self.model.get_hierarchy())
        self.setup_output_file_names()
Example #12
0
    def run(self):
        # I'm guessing self.data_manager, self.params and self.logger
        # are already defined here...

        # this must be mmtbx.model.manager?
        model = self.data_manager.get_model()
        atoms = model.get_atoms()
        all_bsel = flex.bool(atoms.size(), False)
        for selection_string in self.params.atom_selection_program.inselection:
            print("Selecting '%s'" % selection_string, file=self.logger)
            isel = model.iselection(selstr=selection_string)
            all_bsel.set_selected(isel, True)
            if self.params.atom_selection_program.write_pdb_file is None:
                print("  %d atom%s selected" % plural_s(isel.size()),
                      file=self.logger)
                for atom in atoms.select(isel):
                    print("    %s" % atom.format_atom_record(),
                          file=self.logger)
        print("", file=self.logger)
        if self.params.atom_selection_program.write_pdb_file is not None:
            print("Writing file:",
                  show_string(
                      self.params.atom_selection_program.write_pdb_file),
                  file=self.logger)
            selected_model = model.select(all_bsel)
            if self.params.atom_selection_program.cryst1_replacement_buffer_layer is not None:
                box = uctbx.non_crystallographic_unit_cell_with_the_sites_in_its_center(
                    sites_cart=selected_model.get_atoms().extract_xyz(),
                    buffer_layer=self.params.atom_selection_program.
                    cryst1_replacement_buffer_layer)
                sp = crystal.special_position_settings(box.crystal_symmetry())
                sites_frac = box.sites_frac()
                xrs_box = selected_model.get_xray_structure(
                ).replace_sites_frac(box.sites_frac())
                xray_structure_box = xray.structure(sp, xrs_box.scatterers())
                selected_model.set_xray_structure(xray_structure_box)
            pdb_str = selected_model.model_as_pdb()
            f = open(self.params.atom_selection_program.write_pdb_file, 'w')
            f.write(pdb_str)
            f.close()
            print("", file=self.logger)
Example #13
0
def xray_structure_of_one_atom(site_cart,
                               buffer_layer,
                               a,
                               b,
                               scatterer_chemical_type = "C"):
  custom_dict = \
             {scatterer_chemical_type: eltbx.xray_scattering.gaussian([a],[b])}

  box = uctbx.non_crystallographic_unit_cell_with_the_sites_in_its_center(
                                                   sites_cart   = site_cart,
                                                   buffer_layer = buffer_layer)
  crystal_symmetry = crystal.symmetry(unit_cell          = box.unit_cell,
                                      space_group_symbol = "p 1")
  site_frac =crystal_symmetry.unit_cell().fractionalization_matrix()*site_cart
  scatterer = flex.xray_scatterer((
    [xray.scatterer(scatterer_chemical_type, site = site_frac[0], u = 0.0)]))
  xray_structure = xray.structure(special_position_settings = None,
                                  scatterers                = scatterer,
                                  crystal_symmetry          = crystal_symmetry)
  xray_structure.scattering_type_registry(custom_dict = custom_dict)
  return xray_structure
Example #14
0
def xray_structure_of_one_atom(site_cart,
                               buffer_layer,
                               a,
                               b,
                               scatterer_chemical_type = "C"):
  custom_dict = \
             {scatterer_chemical_type: eltbx.xray_scattering.gaussian([a],[b])}

  box = uctbx.non_crystallographic_unit_cell_with_the_sites_in_its_center(
                                                   sites_cart   = site_cart,
                                                   buffer_layer = buffer_layer)
  crystal_symmetry = crystal.symmetry(unit_cell          = box.unit_cell,
                                      space_group_symbol = "p 1")
  site_frac =crystal_symmetry.unit_cell().fractionalization_matrix()*site_cart
  scatterer = flex.xray_scatterer((
    [xray.scatterer(scatterer_chemical_type, site = site_frac[0], u = 0.0)]))
  xray_structure = xray.structure(special_position_settings = None,
                                  scatterers                = scatterer,
                                  crystal_symmetry          = crystal_symmetry)
  xray_structure.scattering_type_registry(custom_dict = custom_dict)
  return xray_structure
Example #15
0
 def run(self):
   self.model = self.data_manager.get_model()
   cs = self.model.crystal_symmetry()
   if(cs is None or cs.is_empty() or cs.is_nonsense()):
     print("Crystal symmetry undefined, creating fake P1 box.")
     box_crystal_symmetry = \
       uctbx.non_crystallographic_unit_cell_with_the_sites_in_its_center(
         sites_cart   = self.model.get_sites_cart(),
         buffer_layer = 5).crystal_symmetry()
     self.model.set_crystal_symmetry_if_undefined(cs = box_crystal_symmetry)
   print('Performing manipulations', file=self.logger)
   self.model = mmtbx.pdbtools.modify(
     model  = self.model,
     params = self.params.modify,
     log    = self.logger).get_results().model
   # Write output model file
   input_file_name_base = os.path.basename(
     self.data_manager.get_default_model_name())[:-4]
   if(  self.model.input_model_format_cif()): extension = ".cif"
   elif(self.model.input_model_format_pdb()): extension = ".pdb"
   if(self.params.output.prefix is not None):
     output_file_name = self.params.output.prefix
     if(self.params.output.suffix is not None):
       output_file_name = output_file_name + self.params.output.suffix
   else:
     output_file_name = input_file_name_base + self.params.output.suffix
   output_file_name = output_file_name + extension
   ofn = self.get_default_output_filename(
     prefix=output_file_name,
     suffix=None,
     serial=Auto)
   print('Writing output model', file=self.logger)
   output_cs=True
   if(cs is None): output_cs = False
   self.data_manager.write_model_file(self.model.model_as_str(
     output_cs=output_cs), ofn)
   self.result = ofn
Example #16
0
    def __init__(
            self,
            model,  # shifted, with shift_manager
            map_data=None,  # shifted map_data
            params=None,
            log=sys.stdout,
            verbose=True):
        t_0 = time()
        self.model = model
        # self.cif_objects = cif_objects
        self.params = params
        self.log = log
        self.verbose = verbose

        # self.shift_manager = self.model.get_shift_manager()

        self.rmsd_from_start = None
        self.init_model_statistics = None
        self.init_gm_model_statistics = None
        self.after_ss_idealization = None
        self.after_loop_idealization = None
        self.after_rotamer_fixing = None
        self.final_model_statistics = None
        self.user_supplied_map = map_data
        self.reference_map = None  # Whole map for all NCS copies
        self.master_map = None  # Map for only one NCS copy, or == reference_map if no NCS
        self.init_ref_map = None  # separate map for initial GM. Should be tighter than the 2 above

        params = mmtbx.model.manager.get_default_pdb_interpretation_params()
        params.pdb_interpretation.clash_guard.nonbonded_distance_threshold = None

        params.pdb_interpretation.peptide_link.ramachandran_restraints = True
        params.pdb_interpretation.peptide_link.restrain_rama_outliers = self.params.restrain_rama_outliers
        params.pdb_interpretation.peptide_link.restrain_rama_allowed = self.params.restrain_rama_allowed
        params.pdb_interpretation.peptide_link.restrain_allowed_outliers_with_emsley = self.params.restrain_allowed_outliers_with_emsley
        params.pdb_interpretation.peptide_link.rama_weight = self.params.rama_weight
        params.pdb_interpretation.peptide_link.oldfield.weight_scale = self.params.oldfield.weight_scale
        params.pdb_interpretation.peptide_link.oldfield.plot_cutoff = self.params.oldfield.plot_cutoff

        params.pdb_interpretation.peptide_link.apply_peptide_plane = True
        if self.params.loop_idealization.make_all_trans:
            params.pdb_interpretation.peptide_link.apply_all_trans = self.params.apply_all_trans
        params.pdb_interpretation.nonbonded_weight = self.params.nonbonded_weight
        params.pdb_interpretation.c_beta_restraints = True
        params.pdb_interpretation.max_reasonable_bond_distance = None
        params.pdb_interpretation.ncs_search.enabled = True
        params.pdb_interpretation.ncs_search.chain_max_rmsd = 4.0
        params.pdb_interpretation.ncs_search.chain_similarity_threshold = 0.99
        params.pdb_interpretation.ncs_search.residue_match_radius = 999.0
        params.pdb_interpretation.restraints_library.rdl = True
        params.pdb_interpretation.secondary_structure = self.params.secondary_structure
        self.params_for_model = params
        self.model.set_pdb_interpretation_params(params)
        self.model.process_input_model(make_restraints=True)

        self.original_hierarchy = self.model.get_hierarchy().deep_copy(
        )  # original pdb_h, without any processing
        self.original_boxed_hierarchy = None  # original and boxed (if needed)

        self.filtered_ncs_restr_group_list = []

        self.init_ss_annotation = self.model.get_ss_annotation()

        # various checks, shifts, trims
        self.cs = self.original_cs = self.model.crystal_symmetry()

        # check self.cs (copy-paste from secondary_sturcure_restraints)
        corrupted_cs = False
        if self.cs is not None:
            if [self.cs.unit_cell(), self.cs.space_group()].count(None) > 0:
                corrupted_cs = True
                self.cs = None
            elif self.cs.unit_cell().volume() < 10:
                corrupted_cs = True
                self.cs = None
        # couple checks if pdb_h is ok
        o_c = self.original_hierarchy.overall_counts()
        o_c.raise_duplicate_atom_labels_if_necessary()
        # o_c.raise_residue_groups_with_multiple_resnames_using_same_altloc_if_necessary()
        o_c.raise_chains_with_mix_of_proper_and_improper_alt_conf_if_necessary(
        )
        o_c.raise_improper_alt_conf_if_necessary()
        if len(self.original_hierarchy.models()) > 1:
            raise Sorry("Multi model files are not supported")
        ca_only_present = False
        for c in self.original_hierarchy.only_model().chains():
            if c.is_ca_only():
                ca_only_present = True
        if ca_only_present:
            raise Sorry(
                "Don't support models with chains containing only CA atoms.")

        self.original_boxed_hierarchy = self.model.get_hierarchy().deep_copy()
        self.shift_vector = None
        if self.cs is None:
            assert self.model.get_shift_manager() is None
            # should it happen here?
            if corrupted_cs:
                print("Symmetry information is corrupted, ", file=self.log)
            else:
                print("Symmetry information was not found, ", file=self.log)
            print("putting molecule in P1 box.", file=self.log)
            self.log.flush()
            from cctbx import uctbx
            box = uctbx.non_crystallographic_unit_cell_with_the_sites_in_its_center(
                sites_cart=self.model.get_sites_cart(), buffer_layer=3)
            # Creating new xrs from box, inspired by extract_box_around_model_and_map
            sp = crystal.special_position_settings(box.crystal_symmetry())
            sites_frac = box.sites_frac()
            xrs_box = self.model.get_xray_structure().replace_sites_frac(
                box.sites_frac())
            xray_structure_box = xray.structure(sp, xrs_box.scatterers())
            self.model.set_xray_structure(xray_structure_box)
            self.cs = box.crystal_symmetry()
            self.shift_vector = box.shift_vector

        if self.shift_vector is not None and self.params.debug:
            txt = self.model.model_as_pdb()
            with open("%s_boxed.pdb" % self.params.output_prefix, 'w') as f:
                f.write(txt)

        if self.params.trim_alternative_conformations:
            self.model.remove_alternative_conformations(
                always_keep_one_conformer=True)

        self.model = self.model.remove_hydrogens()
        self.model_h = None

        self.time_for_init = time() - t_0
Example #17
0
def run(args, params=None, out=sys.stdout, log=sys.stderr):
    # params keyword is for running program from GUI dialog
    if (((len(args) == 0) and (params is None)) or
        ((len(args) > 0) and ((args[0] == "-h") or (args[0] == "--help")))):
        show_usage()
        return

    # parse command-line arguments
    if (params is None):
        pcl = iotbx.phil.process_command_line_with_files(
            args=args,
            master_phil_string=master_phil_str,
            pdb_file_def="file_name")
        work_params = pcl.work.extract()
    # or use parameters defined by GUI
    else:
        work_params = params
    pdb_files = work_params.file_name

    work_params.secondary_structure.enabled = True
    assert work_params.format in [
        "phenix", "phenix_refine", "phenix_bonds", "pymol", "refmac",
        "kinemage", "pdb"
    ]
    if work_params.quiet:
        out = cStringIO.StringIO()

    pdb_combined = iotbx.pdb.combine_unique_pdb_files(file_names=pdb_files)
    pdb_structure = iotbx.pdb.input(source_info=None,
                                    lines=flex.std_string(
                                        pdb_combined.raw_records))
    cs = pdb_structure.crystal_symmetry()

    corrupted_cs = False
    if cs is not None:
        if [cs.unit_cell(), cs.space_group()].count(None) > 0:
            corrupted_cs = True
            cs = None
        elif cs.unit_cell().volume() < 10:
            corrupted_cs = True
            cs = None

    if cs is None:
        if corrupted_cs:
            print >> out, "Symmetry information is corrupted, "
        else:
            print >> out, "Symmetry information was not found, "
        print >> out, "putting molecule in P1 box."
        from cctbx import uctbx
        atoms = pdb_structure.atoms()
        box = uctbx.non_crystallographic_unit_cell_with_the_sites_in_its_center(
            sites_cart=atoms.extract_xyz(), buffer_layer=3)
        atoms.set_xyz(new_xyz=box.sites_cart)
        cs = box.crystal_symmetry()

    defpars = mmtbx.model.manager.get_default_pdb_interpretation_params()
    defpars.pdb_interpretation.automatic_linking.link_carbohydrates = False
    defpars.pdb_interpretation.c_beta_restraints = False
    defpars.pdb_interpretation.clash_guard.nonbonded_distance_threshold = None
    model = mmtbx.model.manager(model_input=pdb_structure,
                                crystal_symmetry=cs,
                                pdb_interpretation_params=defpars,
                                stop_for_unknowns=False)
    pdb_hierarchy = model.get_hierarchy()
    geometry = model.get_restraints_manager().geometry
    if len(pdb_hierarchy.models()) != 1:
        raise Sorry("Multiple models not supported.")
    ss_from_file = None
    if (hasattr(pdb_structure, "extract_secondary_structure")
            and not work_params.ignore_annotation_in_file):
        ss_from_file = pdb_structure.extract_secondary_structure()
    m = manager(pdb_hierarchy=pdb_hierarchy,
                geometry_restraints_manager=geometry,
                sec_str_from_pdb_file=ss_from_file,
                params=work_params.secondary_structure,
                verbose=work_params.verbose)

    # bp_p = nucleic_acids.get_basepair_plane_proxies(
    #     pdb_hierarchy,
    #     m.params.secondary_structure.nucleic_acid.base_pair,
    #     geometry)
    # st_p = nucleic_acids.get_stacking_proxies(
    #     pdb_hierarchy,
    #     m.params.secondary_structure.nucleic_acid.stacking_pair,
    #     geometry)
    # hb_b, hb_a = nucleic_acids.get_basepair_hbond_proxies(pdb_hierarchy,
    #     m.params.secondary_structure.nucleic_acid.base_pair)
    result_out = cStringIO.StringIO()
    # prefix_scope="refinement.pdb_interpretation"
    # prefix_scope=""
    prefix_scope = ""
    if work_params.format == "phenix_refine":
        prefix_scope = "refinement.pdb_interpretation"
    elif work_params.format == "phenix":
        prefix_scope = "pdb_interpretation"
    ss_phil = None
    working_phil = m.as_phil_str(master_phil=sec_str_master_phil)
    phil_diff = sec_str_master_phil.fetch_diff(source=working_phil)

    if work_params.format in ["phenix", "phenix_refine"]:
        comment = "\n".join([
            "# These parameters are suitable for use in e.g. phenix.real_space_refine",
            "# or geometry_minimization. To use them in phenix.refine add ",
            "# 'refinement.' if front of pdb_interpretation."
        ])
        if work_params.format == "phenix_refine":
            comment = "\n".join([
                "# These parameters are suitable for use in phenix.refine only.",
                "# To use them in other Phenix tools remove ",
                "# 'refinement.' if front of pdb_interpretation."
            ])
        print >> result_out, comment
        if (prefix_scope != ""):
            print >> result_out, "%s {" % prefix_scope
        if work_params.show_all_params:
            working_phil.show(prefix="  ", out=result_out)
        else:
            phil_diff.show(prefix="  ", out=result_out)
        if (prefix_scope != ""):
            print >> result_out, "}"
    elif work_params.format == "pdb":
        print >> result_out, m.actual_sec_str.as_pdb_str()
    elif work_params.format == "phenix_bonds":
        raise Sorry("Not yet implemented.")
    elif work_params.format in ["pymol", "refmac", "kinemage"]:
        m.show_summary(log=out)
        (hb_proxies, hb_angle_proxies, planarity_proxies,
         parallelity_proxies) = m.create_all_new_restraints(
             pdb_hierarchy=pdb_hierarchy, grm=geometry, log=out)
        if hb_proxies.size() > 0:
            if work_params.format == "pymol":
                file_load_add = "load %s" % work_params.file_name[0]
                # surprisingly, pymol handles filenames with whitespaces without quotes...
                print >> result_out, file_load_add
                bonds_in_format = hb_proxies.as_pymol_dashes(
                    pdb_hierarchy=pdb_hierarchy)
            elif work_params.format == "kinemage":
                bonds_in_format = hb_proxies.as_kinemage(
                    pdb_hierarchy=pdb_hierarchy)
            else:
                bonds_in_format = hb_proxies.as_refmac_restraints(
                    pdb_hierarchy=pdb_hierarchy)
            print >> result_out, bonds_in_format
        if hb_angle_proxies.size() > 0:
            if work_params.format == "pymol":
                angles_in_format = hb_angle_proxies.as_pymol_dashes(
                    pdb_hierarchy=pdb_hierarchy)
                print >> result_out, angles_in_format
    result = result_out.getvalue()
    out_prefix = os.path.basename(work_params.file_name[0])
    if work_params.output_prefix is not None:
        out_prefix = work_params.output_prefix
    filename = "%s_ss.eff" % out_prefix
    if work_params.format == "pymol":
        filename = "%s_ss.pml" % out_prefix
    outf = open(filename, "w")
    outf.write(result)
    outf.close()
    print >> out, result

    return os.path.abspath(filename)
Example #18
0
def run(args=None,
        pdb_inp=None,
        pdb_hierarchy=None,
        cs=None,
        params=None,
        out=sys.stdout,
        log=sys.stderr):
    if (pdb_hierarchy is None):
        assert args is not None
        # params keyword is for running program from GUI dialog
        if (((len(args) == 0) and (params is None))
                or ((len(args) > 0) and ((args[0] == "-h") or
                                         (args[0] == "--help")))):
            show_usage()
            return
        # parse command-line arguments
        if (params is None):
            pcl = iotbx.phil.process_command_line_with_files(
                args=args,
                master_phil_string=master_phil_str,
                pdb_file_def="file_name")
            work_params = pcl.work.extract()
        # or use parameters defined by GUI
        else:
            work_params = params
        pdb_files = work_params.file_name

        pdb_combined = iotbx.pdb.combine_unique_pdb_files(file_names=pdb_files)
        pdb_structure = iotbx.pdb.input(source_info=None,
                                        lines=flex.std_string(
                                            pdb_combined.raw_records))
        pdb_h = pdb_structure.construct_hierarchy()
    else:
        work_params = params
        if work_params is None:
            work_params = master_phil.extract()
        pdb_h = pdb_hierarchy
    atoms = pdb_h.atoms()
    ss_log = cStringIO.StringIO()
    try:
        if (pdb_inp is not None): pdb_structure = pdb_inp
        ss_annot = pdb_structure.extract_secondary_structure(log=ss_log)
    except Sorry as e:
        print >> out, " Syntax error in SS: %s" % e.message
        return
    if work_params.nproc < 1:
        work_params.nproc = 1

    ss_log_cont = ss_log.getvalue()
    n_bad_helices = ss_log_cont.count("Bad HELIX")
    n_bad_sheets = ss_log_cont.count("Bad SHEET")
    if ss_annot is None or ss_annot.is_empty():
        print >> out, "No SS annotation, nothing to analyze"
        return
    if n_bad_helices > 0:
        print >> out, "Number of bad helices: %d" % n_bad_helices
    if n_bad_helices > 0:
        print >> out, "Number of bad sheets: %d" % n_bad_sheets
    if len(pdb_h.models()) != 1:
        raise Sorry("Multiple models not supported.")
    if not pdb_h.contains_protein():
        print >> out, "Protein is not found in the model"
        return
    if pdb_h.is_ca_only():
        print >> out, "Error: CA-only model"
        return
    if is_ca_and_something(pdb_h):
        print >> out, "CA-only and something model"
        return
    if some_chains_are_ca(pdb_h):
        print >> out, "some chains are CA-only"
        return

    corrupted_cs = False
    if cs is not None:
        if [cs.unit_cell(), cs.space_group()].count(None) > 0:
            corrupted_cs = True
            cs = None
        elif cs.unit_cell().volume() < 10:
            corrupted_cs = True
            cs = None

    if cs is None:
        if corrupted_cs:
            print >> out, "Symmetry information is corrupted, "
        else:
            print >> out, "Symmetry information was not found, "
        print >> out, "putting molecule in P1 box."
        from cctbx import uctbx
        atoms = pdb_structure.atoms()
        box = uctbx.non_crystallographic_unit_cell_with_the_sites_in_its_center(
            sites_cart=atoms.extract_xyz(), buffer_layer=3)
        atoms.set_xyz(new_xyz=box.sites_cart)
        cs = box.crystal_symmetry()

    n_total_helix_sheet_records = ss_annot.get_n_helices(
    ) + ss_annot.get_n_sheets()
    n_bad_helix_sheet_records = 0
    # Empty stuff:
    empty_annots = ss_annot.remove_empty_annotations(pdb_h)
    number_of_empty_helices = empty_annots.get_n_helices()
    number_of_empty_sheets = empty_annots.get_n_sheets()
    n_bad_helix_sheet_records += (number_of_empty_helices +
                                  number_of_empty_sheets)
    if number_of_empty_helices > 0:
        print >> out, "Helices without corresponding atoms in the model (%d):" % number_of_empty_helices
        for h in empty_annots.helices:
            print >> out, "  ", h.as_pdb_str()
    if number_of_empty_sheets > 0:
        print >> out, "Sheets without corresponding atoms in the model (%d):" % number_of_empty_sheets
        for sh in empty_annots.sheets:
            print >> out, "  ", sh.as_pdb_str()

    print >> out, "Checking annotations thoroughly, use nproc=<number> if it is too slow..."

    hsh_tuples = []
    for h in ss_annot.helices:
        hsh_tuples.append(([h], []))
    for sh in ss_annot.sheets:
        hsh_tuples.append(([], [sh]))
    calc_ss_stats = gather_ss_stats(
        pdb_h,
        mediocre_hbond_cutoff=work_params.mediocre_hbond_cutoff,
        bad_hbond_cutoff=work_params.bad_hbond_cutoff)
    results = []
    if len(hsh_tuples) > 0:
        results = easy_mp.pool_map(processes=work_params.nproc,
                                   fixed_func=calc_ss_stats,
                                   args=hsh_tuples)

    cumm_n_hbonds = 0
    cumm_n_bad_hbonds = 0
    cumm_n_mediocre_hbonds = 0
    cumm_n_rama_out = 0
    cumm_n_wrong_reg = 0

    n_elem_with_wrong_rama = 0
    n_elem_with_rama_out = 0
    n_elem_with_bad_hbond = 0
    #
    # Hydrogen Bonds in Proteins: Role and Strength
    # Roderick E Hubbard, Muhammad Kamran Haider
    # ENCYCLOPEDIA OF LIFE SCIENCES & 2010, John Wiley & Sons, Ltd. www.els.net
    #
    # See also: http://proteopedia.org/wiki/index.php/Hydrogen_bonds
    #
    for ss_elem, r in zip(ss_annot.helices + ss_annot.sheets, results):
        if r is not None:
            n_hbonds, n_bad_hbonds, n_mediocre_hbonds, hb_lens, n_outliers, n_wrong_region = r
            cumm_n_hbonds += n_hbonds
            cumm_n_bad_hbonds += n_bad_hbonds
            cumm_n_mediocre_hbonds += n_mediocre_hbonds
            cumm_n_rama_out += n_outliers
            cumm_n_wrong_reg += n_wrong_region
            if n_wrong_region > 0:
                n_elem_with_wrong_rama += 1
            if n_outliers > 0:
                n_elem_with_rama_out += 1
            if n_bad_hbonds > 0:
                n_elem_with_bad_hbond += 1
            if n_bad_hbonds + n_outliers + n_wrong_region > 0:
                n_bad_helix_sheet_records += 1
            if n_bad_hbonds + n_mediocre_hbonds + n_outliers + n_wrong_region > 0:
                # this is bad annotation, printing it to log with separate stats:
                print >> out, "Bad annotation found:"
                print >> out, "%s" % ss_elem.as_pdb_str()
                print >> out, "  Total hb: %d, mediocre: %d, bad: %d, Rama outliers: %d, Rama wrong %d" % (
                    n_hbonds, n_mediocre_hbonds, n_bad_hbonds, n_outliers,
                    n_wrong_region)
                print >> out, "-" * 80

    # n1 = percentage of bad SS elements (per given model);
    # bad here means: n_bad_hbonds + n_outliers + n_wrong_region > 0
    n1 = safe_div(n_bad_helix_sheet_records,
                  n_total_helix_sheet_records) * 100.
    # n2 = percentage of SS elements that have at least one residue belonging to a wrong region of Ramachandran plot (per given model);
    n2 = safe_div(n_elem_with_wrong_rama, n_total_helix_sheet_records) * 100.
    # n3 = percentage of SS elements that have at least one residue being a Ramachandran plot outlier (per given model);
    n3 = safe_div(n_elem_with_rama_out, n_total_helix_sheet_records) * 100.
    # n4 = percentage of bad H bonds (per given model).
    n4 = safe_div(cumm_n_bad_hbonds,
                  cumm_n_hbonds) * 100.  # No per SS element separation
    # percentage of SS elements that have at least one bad H bond (per given model)
    n5 = safe_div(n_elem_with_bad_hbond, n_total_helix_sheet_records) * 100.
    print >> out, "Overall info:"
    print >> out, "  Total HELIX+SHEET recods       :", n_total_helix_sheet_records
    print >> out, "  Total bad HELIX+SHEET recods   :", n_bad_helix_sheet_records
    print >> out, "  Total declared H-bonds         :", cumm_n_hbonds
    print >> out, "  Total mediocre H-bonds (%.1f-%.1fA):" % (
        work_params.mediocre_hbond_cutoff, work_params.bad_hbond_cutoff), \
        cumm_n_mediocre_hbonds
    print >> out, "  Total bad H-bonds (>%.1fA)      :" % work_params.bad_hbond_cutoff, \
        cumm_n_bad_hbonds
    print >> out, "  Total Ramachandran outliers    :", cumm_n_rama_out
    print >> out, "  Total wrong Ramachandrans      :", cumm_n_wrong_reg
    print >> out, "All done."

    if work_params.filter_annotation:
        filtered_ann = ss_annot.filter_annotation(hierarchy=pdb_h)
        print >> out, "Filtered annotation:"
        print >> out, filtered_ann.as_pdb_str()

    return group_args(n_total_helix_sheet_records=n_total_helix_sheet_records,
                      n_bad_helix_sheet_records=n_bad_helix_sheet_records,
                      n_hbonds=cumm_n_hbonds,
                      n_mediocre_hbonds=cumm_n_mediocre_hbonds,
                      n_bad_hbonds=cumm_n_bad_hbonds,
                      n_rama_out=cumm_n_rama_out,
                      n_wrong_reg=cumm_n_wrong_reg,
                      n1=n1,
                      n2=n2,
                      n3=n3,
                      n4=n4,
                      n5=n5)
def run (args, params=None, out=sys.stdout, log=sys.stderr) :
  # params keyword is for running program from GUI dialog
  if ( ((len(args) == 0) and (params is None)) or
       ((len(args) > 0) and ((args[0] == "-h") or (args[0] == "--help"))) ):
    show_usage()
    return

  # parse command-line arguments
  if (params is None):
    pcl = iotbx.phil.process_command_line_with_files(
      args=args,
      master_phil_string=master_phil_str,
      pdb_file_def="file_name")
    work_params = pcl.work.extract()
  # or use parameters defined by GUI
  else:
    work_params = params
  pdb_files = work_params.file_name

  work_params.secondary_structure.enabled=True
  assert work_params.format in ["phenix", "phenix_refine", "phenix_bonds",
      "pymol", "refmac", "kinemage", "pdb"]
  if work_params.quiet :
    out = cStringIO.StringIO()

  pdb_combined = iotbx.pdb.combine_unique_pdb_files(file_names=pdb_files)
  pdb_structure = iotbx.pdb.input(source_info=None,
    lines=flex.std_string(pdb_combined.raw_records))
  cs = pdb_structure.crystal_symmetry()

  corrupted_cs = False
  if cs is not None:
    if [cs.unit_cell(), cs.space_group()].count(None) > 0:
      corrupted_cs = True
      cs = None
    elif cs.unit_cell().volume() < 10:
      corrupted_cs = True
      cs = None

  if cs is None:
    if corrupted_cs:
      print >> out, "Symmetry information is corrupted, "
    else:
      print >> out, "Symmetry information was not found, "
    print >> out, "putting molecule in P1 box."
    from cctbx import uctbx
    atoms = pdb_structure.atoms()
    box = uctbx.non_crystallographic_unit_cell_with_the_sites_in_its_center(
      sites_cart=atoms.extract_xyz(),
      buffer_layer=3)
    atoms.set_xyz(new_xyz=box.sites_cart)
    cs = box.crystal_symmetry()
  from mmtbx.monomer_library import pdb_interpretation, server
  import mmtbx
  import mmtbx.command_line.geometry_minimization

  mon_lib_srv = server.server()
  ener_lib = server.ener_lib()
  defpars = mmtbx.command_line.geometry_minimization.master_params().extract()
  defpars.pdb_interpretation.automatic_linking.link_carbohydrates=False
  defpars.pdb_interpretation.c_beta_restraints=False
  defpars.pdb_interpretation.clash_guard.nonbonded_distance_threshold=None
  processed_pdb_file = pdb_interpretation.process(
    mon_lib_srv    = mon_lib_srv,
    ener_lib       = ener_lib,
    pdb_inp        = pdb_structure,
    crystal_symmetry = cs,
    params         = defpars.pdb_interpretation,
    force_symmetry = True)
  pdb_hierarchy = processed_pdb_file.all_chain_proxies.pdb_hierarchy
  geometry = processed_pdb_file.geometry_restraints_manager()
  geometry.pair_proxies(processed_pdb_file.xray_structure().sites_cart())
  pdb_hierarchy.atoms().reset_i_seq()
  if len(pdb_hierarchy.models()) != 1 :
    raise Sorry("Multiple models not supported.")
  ss_from_file = None
  if hasattr(pdb_structure, "extract_secondary_structure"):
    ss_from_file = pdb_structure.extract_secondary_structure()
  m = manager(pdb_hierarchy=pdb_hierarchy,
    geometry_restraints_manager=geometry,
    sec_str_from_pdb_file=ss_from_file,
    params=work_params.secondary_structure,
    verbose=work_params.verbose)

  # bp_p = nucleic_acids.get_basepair_plane_proxies(
  #     pdb_hierarchy,
  #     m.params.secondary_structure.nucleic_acid.base_pair,
  #     geometry)
  # st_p = nucleic_acids.get_stacking_proxies(
  #     pdb_hierarchy,
  #     m.params.secondary_structure.nucleic_acid.stacking_pair,
  #     geometry)
  # hb_b, hb_a = nucleic_acids.get_basepair_hbond_proxies(pdb_hierarchy,
  #     m.params.secondary_structure.nucleic_acid.base_pair)
  result_out = cStringIO.StringIO()
  # prefix_scope="refinement.pdb_interpretation"
  # prefix_scope=""
  prefix_scope=""
  if work_params.format == "phenix_refine":
    prefix_scope = "refinement.pdb_interpretation"
  elif work_params.format == "phenix":
    prefix_scope = "pdb_interpretation"
  ss_phil = None
  working_phil = m.as_phil_str(master_phil=sec_str_master_phil)
  phil_diff = sec_str_master_phil.fetch_diff(source=working_phil)

  if work_params.format in ["phenix", "phenix_refine"]:
    comment = "\n".join([
      "# These parameters are suitable for use in e.g. phenix.real_space_refine",
      "# or geometry_minimization. To use them in phenix.refine add ",
      "# 'refinement.' if front of pdb_interpretation."])
    if work_params.format == "phenix_refine":
      comment = "\n".join([
      "# These parameters are suitable for use in phenix.refine only.",
      "# To use them in other Phenix tools remove ",
      "# 'refinement.' if front of pdb_interpretation."])
    print >> result_out, comment
    if (prefix_scope != "") :
      print >> result_out, "%s {" % prefix_scope
    if work_params.show_all_params :
      working_phil.show(prefix="  ", out=result_out)
    else :
      phil_diff.show(prefix="  ", out=result_out)
    if (prefix_scope != "") :
      print >> result_out, "}"
  elif work_params.format == "pdb":
    print >> result_out, m.actual_sec_str.as_pdb_str()
  elif work_params.format == "phenix_bonds" :
    raise Sorry("Not yet implemented.")
  elif work_params.format in ["pymol", "refmac", "kinemage"] :
    m.show_summary(log=out)
    (hb_proxies, hb_angle_proxies, planarity_proxies,
        parallelity_proxies) = m.create_all_new_restraints(
        pdb_hierarchy=pdb_hierarchy,
        grm=geometry,
        log=out)
    if hb_proxies.size() > 0:
      if work_params.format == "pymol" :
        bonds_in_format = hb_proxies.as_pymol_dashes(
            pdb_hierarchy=pdb_hierarchy)
      elif work_params.format == "kinemage" :
        bonds_in_format = hb_proxies.as_kinemage(
            pdb_hierarchy=pdb_hierarchy)
      else :
        bonds_in_format = hb_proxies.as_refmac_restraints(
            pdb_hierarchy=pdb_hierarchy)
      print >> result_out, bonds_in_format
  result = result_out.getvalue()
  filename = "%s_ss.eff" %os.path.basename(work_params.file_name[0])
  outf = open(filename, "w")
  outf.write(result)
  outf.close()
  print >> out, result

  return os.path.abspath(filename)
Example #20
0
def box_pdb(pdb_hierarchy):
    sites_cart = pdb_hierarchy.atoms().extract_xyz()
    box = uctbx.non_crystallographic_unit_cell_with_the_sites_in_its_center(
        sites_cart=sites_cart, buffer_layer=10)
    pdb_hierarchy.atoms().set_xyz(box.sites_cart)
    return group_args(pdb_hierarchy=pdb_hierarchy, cs=box.crystal_symmetry())
Example #21
0
    def __init__(self,
                 args,
                 master_phil,
                 out=sys.stdout,
                 process_pdb_file=True,
                 require_data=True,
                 create_fmodel=True,
                 prefer_anomalous=None,
                 force_non_anomalous=False,
                 set_wavelength_from_model_header=False,
                 set_inelastic_form_factors=None,
                 usage_string=None,
                 create_log_buffer=False,
                 remove_unknown_scatterers=False,
                 generate_input_phil=False):
        import mmtbx.monomer_library.pdb_interpretation
        import mmtbx.monomer_library.server
        import mmtbx.utils
        import mmtbx.model
        from iotbx import crystal_symmetry_from_any
        import iotbx.phil
        if generate_input_phil:
            assert isinstance(master_phil, basestring)
            master_phil = generate_master_phil_with_inputs(
                phil_string=master_phil)
        if isinstance(master_phil, str):
            master_phil = iotbx.phil.parse(master_phil)
        if (usage_string is not None):
            if (len(args) == 0) or ("--help" in args):
                raise Usage("""%s\n\nFull parameters:\n%s""" %
                            (usage_string, master_phil.as_str(prefix="  ")))
        if (force_non_anomalous):
            assert (not prefer_anomalous)
        assert (set_inelastic_form_factors in [None, "sasaki", "henke"])
        self.args = args
        self.master_phil = master_phil
        self.processed_pdb_file = self.pdb_inp = None
        self.pdb_hierarchy = self.xray_structure = None
        self.geometry = None
        self.sequence = None
        self.fmodel = None
        self.f_obs = None
        self.r_free_flags = None
        self.intensity_flag = None
        self.raw_data = None
        self.raw_flags = None
        self.test_flag_value = None
        self.miller_arrays = None
        self.hl_coeffs = None
        self.cif_objects = []
        self.log = out
        if ("--quiet" in args) or ("quiet=True" in args):
            self.log = null_out()
        elif create_log_buffer:
            self.log = multi_out()
            self.log.register(label="stdout", file_object=out)
            self.log.register(label="log_buffer", file_object=StringIO())
        make_header("Collecting inputs", out=self.log)
        cmdline = iotbx.phil.process_command_line_with_files(
            args=args,
            master_phil=master_phil,
            pdb_file_def="input.pdb.file_name",
            reflection_file_def="input.xray_data.file_name",
            cif_file_def="input.monomers.file_name",
            seq_file_def="input.sequence")
        self.working_phil = cmdline.work
        params = self.working_phil.extract()
        if len(params.input.pdb.file_name) == 0:
            raise Sorry("At least one PDB file is required as input.")
        self.cif_file_names = params.input.monomers.file_name
        self.pdb_file_names = params.input.pdb.file_name
        # SYMMETRY HANDLING - PDB FILES
        self.crystal_symmetry = pdb_symm = None
        for pdb_file_name in params.input.pdb.file_name:
            pdb_symm = crystal_symmetry_from_any.extract_from(pdb_file_name)
            if (pdb_symm is not None):
                break
        # DATA INPUT
        data_and_flags = hkl_symm = hkl_in = None
        if (params.input.xray_data.file_name is None):
            if (require_data):
                raise Sorry(
                    "At least one reflections file is required as input.")
        else:
            # FIXME this may still require that the data file has full crystal
            # symmetry defined (although for MTZ input this will not be a problem)
            make_sub_header("Processing X-ray data", out=self.log)
            hkl_in = file_reader.any_file(params.input.xray_data.file_name)
            hkl_in.check_file_type("hkl")
            hkl_server = hkl_in.file_server
            symm = hkl_server.miller_arrays[0].crystal_symmetry()
            if ((symm is None) or (symm.space_group() is None)
                    or (symm.unit_cell() is None)):
                if (pdb_symm is not None):
                    from iotbx.reflection_file_utils import reflection_file_server
                    print >> self.log, \
                      "No symmetry in X-ray data file - using PDB symmetry:"
                    pdb_symm.show_summary(f=out, prefix="  ")
                    hkl_server = reflection_file_server(
                        crystal_symmetry=pdb_symm,
                        reflection_files=[hkl_in.file_object])
                else:
                    raise Sorry(
                        "No crystal symmetry information found in input files."
                    )
            if (hkl_server is None):
                hkl_server = hkl_in.file_server
            data_and_flags = mmtbx.utils.determine_data_and_flags(
                reflection_file_server=hkl_server,
                parameters=params.input.xray_data,
                data_parameter_scope="input.xray_data",
                flags_parameter_scope="input.xray_data.r_free_flags",
                prefer_anomalous=prefer_anomalous,
                force_non_anomalous=force_non_anomalous,
                log=self.log)
            self.intensity_flag = data_and_flags.intensity_flag
            self.raw_data = data_and_flags.raw_data
            self.raw_flags = data_and_flags.raw_flags
            self.test_flag_value = data_and_flags.test_flag_value
            self.f_obs = data_and_flags.f_obs
            self.r_free_flags = data_and_flags.r_free_flags
            self.miller_arrays = hkl_in.file_server.miller_arrays
            hkl_symm = self.raw_data.crystal_symmetry()
        if len(self.cif_file_names) > 0:
            for file_name in self.cif_file_names:
                cif_obj = mmtbx.monomer_library.server.read_cif(
                    file_name=file_name)
                self.cif_objects.append((file_name, cif_obj))
        # SYMMETRY HANDLING - COMBINED
        if (hkl_symm is not None):
            use_symmetry = hkl_symm

        # check for weird crystal symmetry
        # modified from mmtbx.command_line.secondary_structure_restraints
        # plan to centralize functionality in another location
        # -------------------------------------------------------------------------
        cs = pdb_symm

        corrupted_cs = False
        if cs is not None:
            if [cs.unit_cell(), cs.space_group()].count(None) > 0:
                corrupted_cs = True
                cs = None
            elif cs.unit_cell().volume() < 10:
                corrupted_cs = True
                cs = None

        if cs is None:
            if corrupted_cs:
                print >> out, "Symmetry information is corrupted,",
            else:
                print >> out, "Symmetry information was not found,",

            if (hkl_symm is not None):
                print >> out, "using symmetry from data."
                cs = hkl_symm
            else:
                print >> out, "putting molecule in P1 box."
                pdb_combined = iotbx.pdb.combine_unique_pdb_files(
                    file_names=self.pdb_file_names)
                pdb_structure = iotbx.pdb.input(source_info=None,
                                                lines=flex.std_string(
                                                    pdb_combined.raw_records))
                atoms = pdb_structure.atoms()
                box = uctbx.non_crystallographic_unit_cell_with_the_sites_in_its_center(
                    sites_cart=atoms.extract_xyz(), buffer_layer=3)
                atoms.set_xyz(new_xyz=box.sites_cart)
                cs = box.crystal_symmetry()

        pdb_symm = cs
        # -------------------------------------------------------------------------

        from iotbx.symmetry import combine_model_and_data_symmetry
        self.crystal_symmetry = combine_model_and_data_symmetry(
            model_symmetry=pdb_symm, data_symmetry=hkl_symm)
        if (self.crystal_symmetry is not None) and (self.f_obs is not None):
            self.f_obs = self.f_obs.customized_copy(
                crystal_symmetry=self.crystal_symmetry).eliminate_sys_absent(
                ).set_info(self.f_obs.info())
            self.r_free_flags = self.r_free_flags.customized_copy(
                crystal_symmetry=self.crystal_symmetry).eliminate_sys_absent(
                ).set_info(self.r_free_flags.info())
        # EXPERIMENTAL PHASES
        target_name = "ml"
        if hasattr(params.input, "experimental_phases"):
            flag = params.input.use_experimental_phases
            if (flag in [True, Auto]):
                phases_file = params.input.experimental_phases.file_name
                if (phases_file is None):
                    phases_file = params.input.xray_data.file_name
                    phases_in = hkl_in
                else:
                    phases_in = file_reader.any_file(phases_file)
                    phases_in.check_file_type("hkl")
                phases_in.file_server.err = self.log  # redirect error output
                space_group = self.crystal_symmetry.space_group()
                point_group = space_group.build_derived_point_group()
                hl_coeffs = mmtbx.utils.determine_experimental_phases(
                    reflection_file_server=phases_in.file_server,
                    parameters=params.input.experimental_phases,
                    log=self.log,
                    parameter_scope="input.experimental_phases",
                    working_point_group=point_group,
                    symmetry_safety_check=True)
                if (hl_coeffs is not None):
                    hl_coeffs = hl_coeffs.map_to_asu()
                    if hl_coeffs.anomalous_flag():
                        if (not self.f_obs.anomalous_flag()):
                            hl_coeffs = hl_coeffs.average_bijvoet_mates()
                    elif self.f_obs.anomalous_flag():
                        hl_coeffs = hl_coeffs.generate_bijvoet_mates()
                    self.hl_coeffs = hl_coeffs.matching_set(
                        other=self.f_obs, data_substitute=(0, 0, 0, 0))
                    target_name = "mlhl"
        # PDB INPUT
        self.unknown_residues_flag = False
        self.unknown_residues_error_message = False

        pdb_combined = mmtbx.utils.combine_unique_pdb_files(
            file_names=params.input.pdb.file_name, )
        pdb_combined.report_non_unique(out=self.log)
        pdb_raw_records = pdb_combined.raw_records
        try:
            self.pdb_inp = iotbx.pdb.input(
                source_info=None, lines=flex.std_string(pdb_raw_records))
        except ValueError, e:
            raise Sorry("Model format (PDB or mmCIF) error:\n%s" % str(e))
Example #22
0
    def __init__(self,
                 args,
                 master_phil,
                 out=sys.stdout,
                 process_pdb_file=True,
                 require_data=True,
                 create_fmodel=True,
                 prefer_anomalous=None,
                 force_non_anomalous=False,
                 set_wavelength_from_model_header=False,
                 set_inelastic_form_factors=None,
                 usage_string=None,
                 create_log_buffer=False,
                 remove_unknown_scatterers=False,
                 generate_input_phil=False):
        import mmtbx.monomer_library.pdb_interpretation
        import mmtbx.monomer_library.server
        import mmtbx.utils
        import mmtbx.model
        from iotbx import crystal_symmetry_from_any
        import iotbx.phil
        if generate_input_phil:
            from six import string_types
            assert isinstance(master_phil, string_types)
            master_phil = generate_master_phil_with_inputs(
                phil_string=master_phil)
        if isinstance(master_phil, str):
            master_phil = iotbx.phil.parse(master_phil)
        if (usage_string is not None):
            if (len(args) == 0) or ("--help" in args):
                raise Usage("""%s\n\nFull parameters:\n%s""" %
                            (usage_string, master_phil.as_str(prefix="  ")))
        if (force_non_anomalous):
            assert (not prefer_anomalous)
        assert (set_inelastic_form_factors in [None, "sasaki", "henke"])
        self.args = args
        self.master_phil = master_phil
        self.processed_pdb_file = self.pdb_inp = None
        self.pdb_hierarchy = self.xray_structure = None
        self.geometry = None
        self.sequence = None
        self.fmodel = None
        self.f_obs = None
        self.r_free_flags = None
        self.intensity_flag = None
        self.raw_data = None
        self.raw_flags = None
        self.test_flag_value = None
        self.miller_arrays = None
        self.hl_coeffs = None
        self.cif_objects = []
        self.log = out
        if ("--quiet" in args) or ("quiet=True" in args):
            self.log = null_out()
        elif create_log_buffer:
            self.log = multi_out()
            self.log.register(label="stdout", file_object=out)
            self.log.register(label="log_buffer", file_object=StringIO())
        make_header("Collecting inputs", out=self.log)
        cmdline = iotbx.phil.process_command_line_with_files(
            args=args,
            master_phil=master_phil,
            pdb_file_def="input.pdb.file_name",
            reflection_file_def="input.xray_data.file_name",
            cif_file_def="input.monomers.file_name",
            seq_file_def="input.sequence")
        self.working_phil = cmdline.work
        params = self.working_phil.extract()
        if len(params.input.pdb.file_name) == 0:
            raise Sorry("At least one PDB file is required as input.")
        self.cif_file_names = params.input.monomers.file_name
        self.pdb_file_names = params.input.pdb.file_name
        # SYMMETRY HANDLING - PDB FILES
        self.crystal_symmetry = pdb_symm = None
        for pdb_file_name in params.input.pdb.file_name:
            pdb_symm = crystal_symmetry_from_any.extract_from(pdb_file_name)
            if (pdb_symm is not None):
                break
        # DATA INPUT
        data_and_flags = hkl_symm = hkl_in = None
        if (params.input.xray_data.file_name is None):
            if (require_data):
                raise Sorry(
                    "At least one reflections file is required as input.")
        else:
            # FIXME this may still require that the data file has full crystal
            # symmetry defined (although for MTZ input this will not be a problem)
            make_sub_header("Processing X-ray data", out=self.log)
            hkl_in = file_reader.any_file(params.input.xray_data.file_name)
            hkl_in.check_file_type("hkl")
            hkl_server = hkl_in.file_server
            symm = hkl_server.miller_arrays[0].crystal_symmetry()
            if ((symm is None) or (symm.space_group() is None)
                    or (symm.unit_cell() is None)):
                if (pdb_symm is not None):
                    from iotbx.reflection_file_utils import reflection_file_server
                    print(
                        "No symmetry in X-ray data file - using PDB symmetry:",
                        file=self.log)
                    pdb_symm.show_summary(f=out, prefix="  ")
                    hkl_server = reflection_file_server(
                        crystal_symmetry=pdb_symm,
                        reflection_files=[hkl_in.file_object])
                else:
                    raise Sorry(
                        "No crystal symmetry information found in input files."
                    )
            if (hkl_server is None):
                hkl_server = hkl_in.file_server
            data_and_flags = mmtbx.utils.determine_data_and_flags(
                reflection_file_server=hkl_server,
                parameters=params.input.xray_data,
                data_parameter_scope="input.xray_data",
                flags_parameter_scope="input.xray_data.r_free_flags",
                prefer_anomalous=prefer_anomalous,
                force_non_anomalous=force_non_anomalous,
                log=self.log)
            self.intensity_flag = data_and_flags.intensity_flag
            self.raw_data = data_and_flags.raw_data
            self.raw_flags = data_and_flags.raw_flags
            self.test_flag_value = data_and_flags.test_flag_value
            self.f_obs = data_and_flags.f_obs
            self.r_free_flags = data_and_flags.r_free_flags
            self.miller_arrays = hkl_in.file_server.miller_arrays
            hkl_symm = self.raw_data.crystal_symmetry()
        if len(self.cif_file_names) > 0:
            for file_name in self.cif_file_names:
                cif_obj = mmtbx.monomer_library.server.read_cif(
                    file_name=file_name)
                self.cif_objects.append((file_name, cif_obj))
        # SYMMETRY HANDLING - COMBINED
        if (hkl_symm is not None):
            use_symmetry = hkl_symm

        # check for weird crystal symmetry
        # modified from mmtbx.command_line.secondary_structure_restraints
        # plan to centralize functionality in another location
        # -------------------------------------------------------------------------
        cs = pdb_symm

        corrupted_cs = False
        if cs is not None:
            if [cs.unit_cell(), cs.space_group()].count(None) > 0:
                corrupted_cs = True
                cs = None
            elif cs.unit_cell().volume() < 10:
                corrupted_cs = True
                cs = None

        if cs is None:
            if corrupted_cs:
                print("Symmetry information is corrupted,", end=' ', file=out)
            else:
                print("Symmetry information was not found,", end=' ', file=out)

            if (hkl_symm is not None):
                print("using symmetry from data.", file=out)
                cs = hkl_symm
            else:
                print("putting molecule in P1 box.", file=out)
                pdb_combined = iotbx.pdb.combine_unique_pdb_files(
                    file_names=self.pdb_file_names)
                pdb_structure = iotbx.pdb.input(source_info=None,
                                                lines=flex.std_string(
                                                    pdb_combined.raw_records))
                atoms = pdb_structure.atoms()
                box = uctbx.non_crystallographic_unit_cell_with_the_sites_in_its_center(
                    sites_cart=atoms.extract_xyz(), buffer_layer=3)
                atoms.set_xyz(new_xyz=box.sites_cart)
                cs = box.crystal_symmetry()

        pdb_symm = cs
        # -------------------------------------------------------------------------

        from iotbx.symmetry import combine_model_and_data_symmetry
        self.crystal_symmetry = combine_model_and_data_symmetry(
            model_symmetry=pdb_symm, data_symmetry=hkl_symm)
        if (self.crystal_symmetry is not None) and (self.f_obs is not None):
            self.f_obs = self.f_obs.customized_copy(
                crystal_symmetry=self.crystal_symmetry).eliminate_sys_absent(
                ).set_info(self.f_obs.info())
            self.r_free_flags = self.r_free_flags.customized_copy(
                crystal_symmetry=self.crystal_symmetry).eliminate_sys_absent(
                ).set_info(self.r_free_flags.info())
        # EXPERIMENTAL PHASES
        target_name = "ml"
        if hasattr(params.input, "experimental_phases"):
            flag = params.input.use_experimental_phases
            if (flag in [True, Auto]):
                phases_file = params.input.experimental_phases.file_name
                if (phases_file is None):
                    phases_file = params.input.xray_data.file_name
                    phases_in = hkl_in
                else:
                    phases_in = file_reader.any_file(phases_file)
                    phases_in.check_file_type("hkl")
                phases_in.file_server.err = self.log  # redirect error output
                space_group = self.crystal_symmetry.space_group()
                point_group = space_group.build_derived_point_group()
                hl_coeffs = mmtbx.utils.determine_experimental_phases(
                    reflection_file_server=phases_in.file_server,
                    parameters=params.input.experimental_phases,
                    log=self.log,
                    parameter_scope="input.experimental_phases",
                    working_point_group=point_group,
                    symmetry_safety_check=True)
                if (hl_coeffs is not None):
                    hl_coeffs = hl_coeffs.map_to_asu()
                    if hl_coeffs.anomalous_flag():
                        if (not self.f_obs.anomalous_flag()):
                            hl_coeffs = hl_coeffs.average_bijvoet_mates()
                    elif self.f_obs.anomalous_flag():
                        hl_coeffs = hl_coeffs.generate_bijvoet_mates()
                    self.hl_coeffs = hl_coeffs.matching_set(
                        other=self.f_obs, data_substitute=(0, 0, 0, 0))
                    target_name = "mlhl"
        # PDB INPUT
        self.unknown_residues_flag = False
        self.unknown_residues_error_message = False

        pdb_combined = mmtbx.utils.combine_unique_pdb_files(
            file_names=params.input.pdb.file_name, )
        pdb_combined.report_non_unique(out=self.log)
        pdb_raw_records = pdb_combined.raw_records
        try:
            self.pdb_inp = iotbx.pdb.input(
                source_info=None, lines=flex.std_string(pdb_raw_records))
        except ValueError as e:
            raise Sorry("Model format (PDB or mmCIF) error:\n%s" % str(e))

        if (remove_unknown_scatterers):
            h = self.pdb_inp.construct_hierarchy()
            known_sel = h.atom_selection_cache().selection("not element X")
            if known_sel.count(False) > 0:
                self.pdb_inp = iotbx.pdb.input(
                    source_info=None,
                    lines=h.select(known_sel).as_pdb_string())

        model_params = mmtbx.model.manager.get_default_pdb_interpretation_params(
        )
        pdb_interp_params = getattr(params, "pdb_interpretation", None)
        if pdb_interp_params is None:
            pdb_interp_params = iotbx.phil.parse(
                input_string=mmtbx.monomer_library.pdb_interpretation.
                grand_master_phil_str,
                process_includes=True).extract()
            pdb_interp_params = pdb_interp_params.pdb_interpretation
        model_params.pdb_interpretation = pdb_interp_params
        stop_for_unknowns = getattr(pdb_interp_params, "stop_for_unknowns",
                                    False) or remove_unknown_scatterers
        if not process_pdb_file:
            stop_for_unknowns = True and not remove_unknown_scatterers
        self.model = mmtbx.model.manager(
            model_input=self.pdb_inp,
            crystal_symmetry=self.crystal_symmetry,
            restraint_objects=self.cif_objects,
            pdb_interpretation_params=model_params,
            process_input=False,
            stop_for_unknowns=stop_for_unknowns,
            log=self.log)
        if process_pdb_file:
            make_sub_header("Processing PDB file(s)", out=self.log)
            self.model.process_input_model(make_restraints=True)
            full_grm = self.model.get_restraints_manager()
            self.geometry = full_grm.geometry
            self.processed_pdb_file = self.model._processed_pdb_file  # to remove later XXX
        self.xray_structure = self.model.get_xray_structure()
        self.pdb_hierarchy = self.model.get_hierarchy()
        self.pdb_hierarchy.atoms().reset_i_seq()
        # wavelength
        if (params.input.energy is not None):
            if (params.input.wavelength is not None):
                raise Sorry("Both wavelength and energy have been specified!")
            params.input.wavelength = 12398.424468024265 / params.input.energy
        if (set_wavelength_from_model_header
                and params.input.wavelength is None):
            wavelength = self.pdb_inp.extract_wavelength()
            if (wavelength is not None):
                print("", file=self.log)
                print("Using wavelength = %g from PDB header" % wavelength,
                      file=self.log)
                params.input.wavelength = wavelength
        # set scattering table
        if (data_and_flags is not None):
            self.model.setup_scattering_dictionaries(
                scattering_table=params.input.scattering_table,
                d_min=self.f_obs.d_min(),
                log=self.log,
                set_inelastic_form_factors=set_inelastic_form_factors,
                iff_wavelength=params.input.wavelength)
            self.xray_structure.show_summary(f=self.log)

        # FMODEL SETUP
        if (create_fmodel) and (data_and_flags is not None):
            make_sub_header("F(model) initialization", out=self.log)
            skip_twin_detection = getattr(params.input, "skip_twin_detection",
                                          True)
            twin_law = getattr(params.input, "twin_law", None)
            if (twin_law is Auto):
                if (self.hl_coeffs is not None):
                    raise Sorry(
                        "Automatic twin law determination not supported when "
                        + "experimental phases are used.")
            elif (not skip_twin_detection):
                twin_law = Auto
            if (twin_law is Auto):
                print("Twinning will be detected automatically.",
                      file=self.log)
                self.fmodel = mmtbx.utils.fmodel_simple(
                    xray_structures=[self.xray_structure],
                    scattering_table=params.input.scattering_table,
                    f_obs=self.f_obs,
                    r_free_flags=self.r_free_flags,
                    skip_twin_detection=skip_twin_detection,
                    target_name=target_name,
                    log=self.log)
            else:
                if ((twin_law is not None) and (self.hl_coeffs is not None)):
                    raise Sorry(
                        "Automatic twin law determination not supported when "
                        + "experimental phases are used.")
                self.fmodel = mmtbx.utils.fmodel_manager(
                    f_obs=self.f_obs,
                    xray_structure=self.xray_structure,
                    r_free_flags=self.r_free_flags,
                    twin_law=params.input.twin_law,
                    hl_coeff=self.hl_coeffs,
                    target_name=target_name)
                self.fmodel.update_all_scales(params=None,
                                              log=self.log,
                                              optimize_mask=True,
                                              show=True)
            self.fmodel.info().show_rfactors_targets_scales_overall(
                out=self.log)
        # SEQUENCE
        if (params.input.sequence is not None):
            seq_file = file_reader.any_file(params.input.sequence,
                                            force_type="seq",
                                            raise_sorry_if_errors=True)
            self.sequence = seq_file.file_object
        # UNMERGED DATA
        self.unmerged_i_obs = None
        if hasattr(params.input, "unmerged_data"):
            if (params.input.unmerged_data.file_name is not None):
                self.unmerged_i_obs = load_and_validate_unmerged_data(
                    f_obs=self.f_obs,
                    file_name=params.input.unmerged_data.file_name,
                    data_labels=params.input.unmerged_data.labels,
                    log=self.log)
        self.params = params
        print("", file=self.log)
        print("End of input processing", file=self.log)
Example #23
0
def get_model_stat(file_name):
    pdb_inp = iotbx.pdb.input(file_name=file_name)
    atoms = pdb_inp.atoms()
    box = uctbx.non_crystallographic_unit_cell_with_the_sites_in_its_center(
        sites_cart=atoms.extract_xyz(), buffer_layer=5)
    atoms.set_xyz(new_xyz=box.sites_cart)
    ph = pdb_inp.construct_hierarchy()
    if (all_single_atom_residues(ph=ph)): return None
    raw_recs = ph.as_pdb_string(
        crystal_symmetry=box.crystal_symmetry()).splitlines()
    #
    params = monomer_library.pdb_interpretation.master_params.extract()
    params.clash_guard.nonbonded_distance_threshold = None
    params.disable_uc_volume_vs_n_atoms_check = False
    params.use_neutron_distances = True
    params.restraints_library.cdl = False
    processed_pdb_file = monomer_library.pdb_interpretation.process(
        mon_lib_srv=mon_lib_srv,
        ener_lib=ener_lib,
        raw_records=raw_recs,
        params=params,
        log=null_out())
    xrs = processed_pdb_file.xray_structure()
    sctr_keys = xrs.scattering_type_registry().type_count_dict().keys()
    has_hd = "H" in sctr_keys or "D" in sctr_keys
    restraints_manager = processed_pdb_file.geometry_restraints_manager(
        show_energies=False,
        assume_hydrogens_all_missing=not has_hd,
        plain_pairs_radius=5.0)
    a_mean, b_mean = get_bonds_angles_rmsd(
        restraints_manager=restraints_manager, xrs=xrs)
    energies_sites = \
      restraints_manager.energies_sites(
        sites_cart        = xrs.sites_cart(),
        compute_gradients = False)
    nonbonded_distances = energies_sites.nonbonded_distances()
    number_of_worst_clashes = (nonbonded_distances < 0.5).count(True)
    #
    ramalyze_obj = ramalyze(pdb_hierarchy=ph, outliers_only=False)
    ramachandran_outliers = ramalyze_obj.percent_outliers
    rotamer_outliers = rotalyze(pdb_hierarchy=ph,
                                outliers_only=False).percent_outliers
    c_beta_dev = cbetadev(pdb_hierarchy=ph, outliers_only=True,
                          out=null_out()).get_outlier_count()
    omglz = omegalyze.omegalyze(pdb_hierarchy=ph, quiet=True)
    n_cis_proline = omglz.n_cis_proline()
    n_cis_general = omglz.n_cis_general()
    n_twisted_proline = omglz.n_twisted_proline()
    n_twisted_general = omglz.n_twisted_general()
    #
    clsc = clashscore(pdb_hierarchy=ph).get_clashscore()
    mpscore = molprobity_score(clashscore=clsc,
                               rota_out=rotamer_outliers,
                               rama_fav=ramalyze_obj.percent_favored)
    #
    occ = atoms.extract_occ()
    bs = atoms.extract_b()
    #
    return group_args(b_mean=b_mean,
                      a_mean=a_mean,
                      number_of_worst_clashes=number_of_worst_clashes,
                      ramachandran_outliers=ramachandran_outliers,
                      rotamer_outliers=rotamer_outliers,
                      c_beta_dev=c_beta_dev,
                      n_cis_proline=n_cis_proline,
                      n_cis_general=n_cis_general,
                      n_twisted_proline=n_twisted_proline,
                      n_twisted_general=n_twisted_general,
                      o=occ.min_max_mean().as_tuple(),
                      b=bs.min_max_mean().as_tuple(),
                      mpscore=mpscore,
                      clsc=clsc,
                      n_atoms=atoms.size())
Example #24
0
def create_super_sphere(pdb_hierarchy,
                        crystal_symmetry,
                        select_within_radius,
                        link_min=1.0,
                        link_max=2.0,
                        r=None):
    if (r is None):
        # This is equivalent to (but much faster):
        #
        #r = grm.geometry.pair_proxies().nonbonded_proxies.\
        #  get_symmetry_interacting_indices_unique(
        #    sites_cart = pdb_hierarchy.atoms().extract_xyz())
        #
        sites_cart = pdb_hierarchy.atoms().extract_xyz()
        sst = crystal_symmetry.special_position_settings().site_symmetry_table(
            sites_cart=sites_cart)
        r = {}
        from cctbx import crystal
        cutoff = select_within_radius + 1  # +1 is nonbonded buffer
        conn_asu_mappings = crystal_symmetry.special_position_settings().\
          asu_mappings(buffer_thickness=cutoff)
        conn_asu_mappings.process_sites_cart(original_sites=sites_cart,
                                             site_symmetry_table=sst)
        conn_pair_asu_table = crystal.pair_asu_table(
            asu_mappings=conn_asu_mappings)
        conn_pair_asu_table.add_all_pairs(distance_cutoff=cutoff)
        pair_generator = crystal.neighbors_fast_pair_generator(
            conn_asu_mappings, distance_cutoff=cutoff)
        for pair in pair_generator:
            rt_mx_i = conn_asu_mappings.get_rt_mx_i(pair)
            rt_mx_j = conn_asu_mappings.get_rt_mx_j(pair)
            rt_mx_ji = rt_mx_i.inverse().multiply(rt_mx_j)
            if str(rt_mx_ji) == "x,y,z": continue
            r.setdefault(pair.j_seq, []).append(rt_mx_ji)
        for k, v in zip(r.keys(), r.values()):  # remove duplicates!
            r[k] = list(set(v))
    c = iotbx.pdb.hierarchy.chain(
        id="SS")  # all symmetry related full residues
    fm = crystal_symmetry.unit_cell().fractionalization_matrix()
    om = crystal_symmetry.unit_cell().orthogonalization_matrix()
    selection = r.keys()
    for rg in pdb_hierarchy.residue_groups():
        keep = False
        for i in rg.atoms().extract_i_seq():
            if (i in selection):
                keep = True
                break
        if (keep):
            ops = r[i]
            for op in ops:
                rg_ = rg.detached_copy()
                xyz = rg_.atoms().extract_xyz()
                new_xyz = flex.vec3_double()
                for xyz_ in xyz:
                    t1 = fm * flex.vec3_double([xyz_])
                    t2 = op * t1[0]
                    t3 = om * flex.vec3_double([t2])
                    new_xyz.append(t3[0])
                rg_.atoms().set_xyz(new_xyz)
                rg_.link_to_previous = True
                c.append_residue_group(rg_)
    resseq = 0
    for rg in c.residue_groups():
        rg.resseq = "%4d" % resseq
        resseq += 1
    # Now we need to re-order residues and group by chains so that they can be
    # linked by pdb interpretation.
    super_sphere_hierarchy = pdb_hierarchy.deep_copy()
    #
    all_chains = iotbx.pdb.utils.all_chain_ids()
    all_chains = [chid.strip() for chid in all_chains]
    for cid in list(set([i.id for i in pdb_hierarchy.chains()])):
        all_chains.remove(cid)
    all_chains = iter(all_chains)

    #
    def get_atom(atoms, name):
        for a in atoms:
            if (a.name.strip().lower() == name.strip().lower()):
                return a.xyz

    residue_groups_i = list(c.residue_groups())
    residue_groups_j = list(c.residue_groups())
    residue_groups_k = list()  # standard aa residues only

    #
    def dist(r1, r2):
        return math.sqrt((r1[0] - r2[0])**2 + (r1[1] - r2[1])**2 +
                         (r1[2] - r2[2])**2)

    #
    def grow_chain(residue_groups_j, chunk, ci):
        n_linked = 0
        for rgj in residue_groups_j:
            nj = get_atom(atoms=rgj.atoms(), name="N")
            if (nj is None): continue
            d_ci_nj = dist(ci, nj)
            if (d_ci_nj < link_max and d_ci_nj > link_min):
                n_linked += 1
                chunk.append(rgj)
                residue_groups_j.remove(rgj)
                break
        return n_linked, rgj

    # Find all isolated single residues, and also save starters
    starters = []
    for rgi in residue_groups_i:
        ci = get_atom(atoms=rgi.atoms(), name="C")
        ni = get_atom(atoms=rgi.atoms(), name="N")
        if (ci is None or ni is None):  # collect non-proteins
            c = iotbx.pdb.hierarchy.chain(id=all_chains.next())
            c.append_residue_group(rgi)
            super_sphere_hierarchy.models()[0].append_chain(c)
            continue
        residue_groups_k.append(rgi)
        c_linked = 0
        n_linked = 0
        for rgj in residue_groups_j:
            cj = get_atom(atoms=rgj.atoms(), name="C")
            nj = get_atom(atoms=rgj.atoms(), name="N")
            if (cj is None or nj is None): continue
            d_ci_nj = dist(ci, nj)
            d_ni_cj = dist(ni, cj)
            if (d_ci_nj < link_max and d_ci_nj > link_min):
                n_linked += 1
            if (d_ni_cj < link_max and d_ni_cj > link_min):
                c_linked += 1
        assert c_linked in [1, 0], c_linked  # Either linked or not!
        assert n_linked in [1, 0], n_linked  # Either linked or not!
        if (c_linked == 0 and n_linked == 0):  # isolated single residue
            c = iotbx.pdb.hierarchy.chain(id=all_chains.next())
            rgi.link_to_previous = True
            c.append_residue_group(rgi)
            super_sphere_hierarchy.models()[0].append_chain(c)
        elif (c_linked == 0):  # collect c-terminal residues
            starters.append(rgi)
    # Grow continuous chains from remaining residues starting from c-terminals
    for rgi in starters:
        ci = get_atom(atoms=rgi.atoms(), name="C")
        chunk = [rgi]
        n_linked = 1
        while n_linked == 1:
            n_linked, rgj = grow_chain(residue_groups_k, chunk, ci)
            if (n_linked == 1):
                ci = get_atom(atoms=rgj.atoms(), name="C")
        c = iotbx.pdb.hierarchy.chain(id=all_chains.next())
        for i, rg in enumerate(chunk):
            rg.resseq = "%4d" % i
            c.append_residue_group(rg)
        super_sphere_hierarchy.models()[0].append_chain(c)
    #
    box = uctbx.non_crystallographic_unit_cell_with_the_sites_in_its_center(
        sites_cart=super_sphere_hierarchy.atoms().extract_xyz(),
        buffer_layer=10)
    cs_super_sphere = box.crystal_symmetry()
    return group_args(hierarchy=super_sphere_hierarchy,
                      crystal_symmetry=cs_super_sphere,
                      selection_r=r)
  def __init__(self,
               pdb_input,
               cif_objects=None,
               params=None,
               log=sys.stdout,
               verbose=True):
    t_0 = time()
    self.pdb_input = pdb_input
    self.cif_objects = cif_objects
    self.params = params
    self.log = log
    self.verbose = verbose

    self.rmsd_from_start = None
    self.init_model_statistics = None
    self.after_ss_idealization = None
    self.after_loop_idealization = None
    self.after_rotamer_fixing = None
    self.final_model_statistics = None
    self.reference_map = None

    self.whole_grm = None
    self.master_grm = None
    self.working_grm = None

    self.mon_lib_srv = None
    self.ener_lib = None
    self.rotamer_manager = None
    self.rama_manager = rama_eval()

    self.original_hierarchy = None # original pdb_h, without any processing
    self.original_boxed_hierarchy = None # original and boxed (if needed)
    self.whole_pdb_h = None # boxed with processing (AC trimming, H trimming,...)
    self.master_pdb_h = None # master copy in case of NCS
    self.working_pdb_h = None # one to use for fixing (master_pdb_h or working_pdb_h)

    # various checks, shifts, trims
    self.cs = self.pdb_input.crystal_symmetry()
    # check self.cs (copy-paste from secondary_sturcure_restraints)
    corrupted_cs = False
    if self.cs is not None:
      if [self.cs.unit_cell(), self.cs.space_group()].count(None) > 0:
        corrupted_cs = True
        self.cs = None
      elif self.cs.unit_cell().volume() < 10:
        corrupted_cs = True
        self.cs = None

    self.original_hierarchy = self.pdb_input.construct_hierarchy()
    # couple checks if pdb_h is ok
    o_c = self.original_hierarchy.overall_counts()
    o_c.raise_duplicate_atom_labels_if_necessary()
    o_c.raise_residue_groups_with_multiple_resnames_using_same_altloc_if_necessary()
    o_c.raise_chains_with_mix_of_proper_and_improper_alt_conf_if_necessary()
    o_c.raise_improper_alt_conf_if_necessary()
    if len(self.original_hierarchy.models()) > 1:
      raise Sorry("Multi model files are not supported")
    ca_only_present = False
    for c in self.original_hierarchy.only_model().chains():
      if c.is_ca_only():
        ca_only_present = True
    if ca_only_present:
      raise Sorry("Don't support models with chains containing only CA atoms.")

    self.original_boxed_hierarchy = self.original_hierarchy.deep_copy()
    self.original_boxed_hierarchy.reset_atom_i_seqs()
    self.shift_vector = None
    if self.cs is None:
      if corrupted_cs:
        print >> self.log, "Symmetry information is corrupted, "
      else:
        print >> self.log, "Symmetry information was not found, "
      print >> self.log, "putting molecule in P1 box."
      self.log.flush()
      from cctbx import uctbx
      atoms = self.original_boxed_hierarchy.atoms()
      box = uctbx.non_crystallographic_unit_cell_with_the_sites_in_its_center(
        sites_cart=atoms.extract_xyz(),
        buffer_layer=3)
      atoms.set_xyz(new_xyz=box.sites_cart)
      self.cs = box.crystal_symmetry()
      self.shift_vector = box.shift_vector

    # self.original_boxed_hierarchy.write_pdb_file(file_name="original_boxed_h.pdb")
    if self.shift_vector is not None:
      write_whole_pdb_file(
          file_name="%s_boxed.pdb" % self.params.output_prefix,
          pdb_hierarchy=self.original_boxed_hierarchy,
          crystal_symmetry=self.cs,
          ss_annotation=self.pdb_input.extract_secondary_structure())

    asc = self.original_boxed_hierarchy.atom_selection_cache()
    if self.params.trim_alternative_conformations:
      sel = asc.selection("altloc ' '")
      self.whole_pdb_h = self.original_boxed_hierarchy.select(sel).deep_copy()
      print >> self.log, "Atoms in original/working model: %d/%d" % (
          self.original_boxed_hierarchy.atoms_size(), self.whole_pdb_h.atoms_size())
    else:
      self.whole_pdb_h = self.original_boxed_hierarchy.deep_copy()
      # self.whole_pdb_h.reset_atom_i_seqs()
    # Trimming hydrogens
    # Many intermediate variables are needed due to strange behavior of
    # selections described in
    # iotbx/pdb/tst_hierarchy.py:exercise_selection_and_deep_copy()
    asc2 = self.whole_pdb_h.atom_selection_cache()
    h_sel = asc2.selection("not (element H or element D)")
    temp_h = self.whole_pdb_h.select(h_sel)
    self.whole_pdb_h = temp_h.deep_copy()
    self.whole_pdb_h.reset_atom_i_seqs()
    self.init_model_statistics = geometry_no_grm(
        pdb_hierarchy=iotbx.pdb.input(
          source_info=None,
          lines=self.whole_pdb_h.as_pdb_string()).construct_hierarchy(),
        molprobity_scores=True)
    self.time_for_init = time()-t_0
Example #26
0
def stats(model, prefix, no_ticks=True):
    # Get rid of H, multi-model, no-protein and single-atom residue models
    if (model.percent_of_single_atom_residues() > 20):
        return None
    sel = model.selection(string="protein")
    if (sel.count(True) == 0):
        return None
    ssr = "protein and not (element H or element D or resname UNX or resname UNK or resname UNL)"
    sel = model.selection(string=ssr)
    model = model.select(sel)
    if (len(model.get_hierarchy().models()) > 1):
        return None
    # Add H; this looses CRYST1 !
    rr = run_reduce_with_timeout(
        stdin_lines=model.get_hierarchy().as_pdb_string().splitlines(),
        file_name=None,
        parameters="-oh -his -flip -keep -allalt -pen9999 -",
        override_auto_timeout_with=None)
    # Create model; this is a single-model pure protein with new H added
    pdb_inp = iotbx.pdb.input(source_info=None, lines=rr.stdout_lines)
    model = mmtbx.model.manager(model_input=None,
                                build_grm=True,
                                pdb_hierarchy=pdb_inp.construct_hierarchy(),
                                process_input=True,
                                log=null_out())
    box = uctbx.non_crystallographic_unit_cell_with_the_sites_in_its_center(
        sites_cart=model.get_sites_cart(), buffer_layer=5)
    model.set_sites_cart(box.sites_cart)
    model._crystal_symmetry = box.crystal_symmetry()
    #
    N = 10
    SS = get_ss_selections(hierarchy=model.get_hierarchy())
    HB_all = find(
        model=model.select(flex.bool(model.size(), True)),
        a_DHA_cutoff=90).get_params_as_arrays(replace_with_empty_threshold=N)
    HB_alpha = find(
        model=model.select(SS.both.h_sel),
        a_DHA_cutoff=90).get_params_as_arrays(replace_with_empty_threshold=N)
    HB_beta = find(
        model=model.select(SS.both.s_sel),
        a_DHA_cutoff=90).get_params_as_arrays(replace_with_empty_threshold=N)
    print(HB_all.d_HA.size())
    result_dict = {}
    result_dict["all"] = HB_all
    result_dict["alpha"] = HB_alpha
    result_dict["beta"] = HB_beta
    #  result_dict["loop"]  = get_selected(sel=loop_sel)
    # Load histograms for reference high-resolution d_HA and a_DHA
    pkl_fn = libtbx.env.find_in_repositories(
        relative_path="mmtbx") + "/nci/d_HA_and_a_DHA_high_res.pkl"
    assert os.path.isfile(pkl_fn)
    ref = easy_pickle.load(pkl_fn)
    #
    import matplotlib as mpl
    mpl.use('Agg')
    import matplotlib.pyplot as plt
    fig = plt.figure(figsize=(10, 10))
    kwargs = dict(histtype='bar', bins=20, range=[1.6, 3.0], alpha=.8)
    for j, it in enumerate([["alpha", 1], ["beta", 3], ["all", 5]]):
        key, i = it
        ax = plt.subplot(int("32%d" % i))
        if (no_ticks):
            #ax.set_xticks([])
            ax.set_yticks([])
        if (j in [0, 1]):
            ax.tick_params(bottom=False)
            ax.set_xticklabels([])
        ax.tick_params(axis="x", labelsize=12)
        ax.tick_params(axis="y", labelsize=12, left=False, pad=-2)
        ax.text(0.98,
                0.92,
                key,
                size=12,
                horizontalalignment='right',
                transform=ax.transAxes)
        HB = result_dict[key]
        if HB is None: continue
        w1 = np.ones_like(HB.d_HA) / HB.d_HA.size()
        ax.hist(HB.d_HA, color="orangered", weights=w1, rwidth=0.3, **kwargs)
        #
        start, end1, end2 = 0, max(ref.distances[key].vals), \
          round(max(ref.distances[key].vals),2)
        if (not no_ticks):
            plt.yticks([0.01, end1], ["0", end2],
                       visible=True,
                       rotation="horizontal")

        if (key == "alpha"): plt.ylim(0, end2 + 0.02)
        elif (key == "beta"): plt.ylim(0, end2 + 0.02)
        elif (key == "all"): plt.ylim(0, end2 + 0.02)
        else: assert 0
        #
        if (j == 0): ax.set_title("Distance", size=15)
        bins = list(flex.double(ref.distances[key].bins))
        ax.bar(bins, ref.distances[key].vals, alpha=.3, width=0.07)
    #
    kwargs = dict(histtype='bar', bins=20, range=[90, 180], alpha=.8)
    for j, it in enumerate([["alpha", 2], ["beta", 4], ["all", 6]]):
        key, i = it
        ax = plt.subplot(int("32%d" % i))
        if (j in [0, 1]):
            ax.tick_params(bottom=False)
            ax.set_xticklabels([])
        if (no_ticks):
            #ax.set_xticks([])
            ax.set_yticks([])
        ax.tick_params(axis="x", labelsize=12)
        ax.tick_params(axis="y", labelsize=12, left=False, pad=-2)
        ax.text(0.98,
                0.92,
                key,
                size=12,
                horizontalalignment='right',
                transform=ax.transAxes)

        ax.text(0.98,
                0.92,
                key,
                size=12,
                horizontalalignment='right',
                transform=ax.transAxes)
        #if(j in [0,1]): ax.plot_params(bottom=False)
        HB = result_dict[key]
        if HB is None: continue
        w1 = np.ones_like(HB.a_DHA) / HB.a_DHA.size()
        ax.hist(HB.a_DHA, color="orangered", weights=w1, rwidth=0.3, **kwargs)
        #
        start, end1, end2 = 0, max(ref.angles[key].vals), \
          round(max(ref.angles[key].vals),2)
        if (not no_ticks):
            plt.yticks([0.01, end1], ["0", end2],
                       visible=True,
                       rotation="horizontal")

        if (key == "alpha"): plt.ylim(0, end2 + 0.02)
        elif (key == "beta"): plt.ylim(0, end2 + 0.02)
        elif (key == "all"): plt.ylim(0, end2 + 0.02)
        else: assert 0
        #
        if (j == 0): ax.set_title("Angle", size=15)
        ax.bar(ref.angles[key].bins, ref.angles[key].vals, width=4.5, alpha=.3)
    plt.subplots_adjust(wspace=0.12, hspace=0.025)
    if (no_ticks):
        plt.subplots_adjust(wspace=0.025, hspace=0.025)
    #fig.savefig("%s.png"%prefix, dpi=1000)
    fig.savefig("%s.pdf" % prefix)