예제 #1
0
    def run(self):
        t_0 = time()
        self.ann = self.model.get_ss_annotation()
        self._setup_model_h()
        self.model.set_restraint_objects(self.model_h.get_restraint_objects())

        self.model.process_input_model(make_restraints=True)
        # set SS restratins
        self.set_ss_restraints(self.ann)

        self.model.setup_ncs_constraints_groups()

        self.init_model_statistics = self.get_statistics(self.model)

        #
        # Cablam idealization
        #
        if self.params.debug:
            self.shift_and_write_result(model=self.model, fname_suffix="start")
            self.shift_and_write_result(model=self.model_h,
                                        fname_suffix="start_h")
        self.params.cablam_idealization.find_ss_after_fixes = False
        ci_results = cablam_idealization(
            model=self.model,
            params=self.params.cablam_idealization,
            log=self.log).get_results()
        self.model = ci_results.model
        self.after_cablam_statistics = self.get_statistics(self.model)
        if self.params.debug:
            self.shift_and_write_result(model=self.model,
                                        fname_suffix="cablam_id")

        # Here we are preparing maps if needed.
        if self.user_supplied_map is not None:
            self.prepare_user_map()

        if self.reference_map is None and self.params.use_map_for_reference:
            self.prepare_reference_map_3()

        if self.params.run_minimization_first:
            # running simple minimization and updating all
            # self.master, self.working, etc...
            # self.whole_pdb_h.reset_atom_i_seqs()
            if self.init_ref_map is None:
                self.prepare_init_reference_map()
            print("Minimization first", file=self.log)
            self.minimize(
                model=self.model,
                original_pdb_h=self.original_hierarchy,
                excl_string_selection=None,  # don't need if we have map
                reference_map=self.init_ref_map,
            )
            self.init_gm_model_statistics = self.get_statistics(self.model)
            if self.params.debug:
                self.shift_and_write_result(model=self.model,
                                            fname_suffix="init_gm")

        if (self.init_gm_model_statistics is not None
                and self.init_gm_model_statistics.ramachandran.outliers == 0
                and self.init_gm_model_statistics.omega.twisted_general <= 0.01
                and self.init_gm_model_statistics.omega.twisted_proline <= 0.01
                and self.init_gm_model_statistics.omega.cis_general <= 0.01
                and self.init_gm_model_statistics.omega.cis_proline <= 0.01
                and self.init_gm_model_statistics.rotamer.outliers <= 0.01):
            print("Simple minimization was enough", file=self.log)
            # Early exit!!!
            self.shift_and_write_result(model=self.model,
                                        fname_suffix="all_idealized")
            if self.params.output_model_h:
                self.shift_and_write_result(model=self.model_h,
                                            fname_suffix="all_idealized_h")

            self.final_model_statistics = self.get_statistics(self.model)
            # self.original_boxed_hierarchy.write_pdb_file(file_name="original_boxed_end.pdb")
            self.time_for_run = time() - t_0
            if self.params.output_pkl:
                easy_pickle.dump(file_name="%s.pkl" %
                                 self.params.output_prefix,
                                 obj=self.get_stats_obj())
            return

        self.filtered_whole_ann = None
        if self.ann is not None:
            self.filtered_whole_ann = self.ann.deep_copy()
            print("Original SS annotation", file=self.log)
            print(self.ann.as_pdb_str(), file=self.log)
            if self.params.filter_input_ss:
                self.filtered_whole_ann = self.ann.filter_annotation(
                    hierarchy=self.model.get_hierarchy(),
                    asc=self.model.get_atom_selection_cache())
            print("Filtered SS annotation", file=self.log)
            print(self.filtered_whole_ann.as_pdb_str(), file=self.log)
            self.model.set_ss_annotation(self.filtered_whole_ann)

        # getting grm with SS restraints
        self.update_ss_in_grm(self.filtered_whole_ann)

        if (self.ann is None
                or self.ann.get_n_helices() + self.ann.get_n_sheets() == 0
                or not self.params.ss_idealization.enabled):
            print(
                "No secondary structure annotations found or SS idealization is disabled.",
                file=self.log)
            print("Secondary structure substitution step will be skipped",
                  file=self.log)
            self.log.flush()
            # here we want to do geometry minimization anyway!
            negate_selection = None
            if self.reference_map is None:
                outlier_selection_txt = mmtbx.building.loop_closure.utils. \
                  rama_score_selection(self.model.get_hierarchy(), self.model.get_ramachandran_manager(), "outlier",1)
                print("outlier_selection_txt",
                      outlier_selection_txt,
                      file=self.log)
                negate_selection = "all"
                if outlier_selection_txt != "" and outlier_selection_txt is not None:
                    negate_selection = "not (%s)" % outlier_selection_txt
            # if self.params.run_minimization_first:
            # self.minimize(
            #     model=self.model,
            #     original_pdb_h=self.whole_pdb_h,
            #     ncs_restraints_group_list=self.filtered_ncs_restr_group_list,
            #     excl_string_selection=negate_selection,
            #     reference_map=self.reference_map)
            # self.original_boxed_hierarchy.write_pdb_file(file_name="original_boxed_h_1.pdb")
        else:
            if self.params.debug:
                self.params.ss_idealization.file_name_before_regularization = \
                    "%s_ss_before_reg.pdb" % self.params.output_prefix
            self.params.ss_idealization.skip_good_ss_elements = True
            ssb.substitute_ss(model=self.model,
                              params=self.params.ss_idealization,
                              reference_map=self.master_map,
                              log=self.log)
            self.log.flush()

        self.after_ss_idealization = self.get_statistics(self.model)
        self.shift_and_write_result(model=self.model,
                                    fname_suffix="ss_ideal_stat")

        # Write resulting pdb file.
        if self.params.debug:
            self.shift_and_write_result(
                model=self.model,
                fname_suffix="ss_ideal",
            )
        # self.params.loop_idealization.minimize_whole = not self.model.ncs_constraints_present() and self.params.loop_idealization.minimize_whole
        self.params.loop_idealization.debug = self.params.debug or self.params.loop_idealization.debug
        # self.params.loop_idealization.enabled = False
        # self.params.loop_idealization.variant_search_level = 0
        print("Starting loop idealization", file=self.log)
        loop_ideal = loop_idealization(self.model,
                                       params=self.params.loop_idealization,
                                       reference_map=self.master_map,
                                       log=self.log,
                                       verbose=True)
        self.log.flush()
        if self.params.debug:
            self.shift_and_write_result(model=self.model,
                                        fname_suffix="rama_ideal")
        self.after_loop_idealization = self.get_statistics(self.model)

        # fixing remaining rotamer outliers
        if (self.params.additionally_fix_rotamer_outliers
                and self.after_loop_idealization.rotamer.outliers > 0.004):
            self.idealize_rotamers()

        self.after_rotamer_fixing = self.get_statistics(self.model)
        ref_hierarchy_for_final_gm = self.original_boxed_hierarchy
        if not self.params.use_starting_model_for_final_gm:
            ref_hierarchy_for_final_gm = self.model.get_hierarchy().deep_copy()
        ref_hierarchy_for_final_gm.reset_atom_i_seqs()

        if self.model.ncs_constraints_present():
            print("Using ncs", file=self.log)
            # assert 0
        else:
            print("Not using ncs", file=self.log)
            # assert 0

        # need to update SS manager for the whole model here.
        if self.params.use_ss_restraints:
            ss_params = sec_str_master_phil.fetch().extract()
            ss_params.secondary_structure.protein.remove_outliers = not self.params.ss_idealization.enabled
            self.set_ss_restraints(ss_annotation=self.filtered_whole_ann,
                                   params=ss_params.secondary_structure)
        if self.params.run_minimization_last:
            print("loop_ideal.ref_exclusion_selection",
                  loop_ideal.ref_exclusion_selection,
                  file=self.log)
            print("Minimizing whole model", file=self.log)
            self.minimize(
                model=self.model,
                original_pdb_h=ref_hierarchy_for_final_gm,
                excl_string_selection=loop_ideal.ref_exclusion_selection,
                reference_map=self.reference_map)
        self.shift_and_write_result(model=self.model,
                                    fname_suffix="all_idealized")
        if self.params.output_model_h:
            self.shift_and_write_result(model=self.model_h,
                                        fname_suffix="all_idealized_h")

        self.final_model_statistics = self.get_statistics(self.model)
        self.time_for_run = time() - t_0
        if self.params.output_pkl or self.params.debug:
            easy_pickle.dump(file_name="%s.pkl" % self.params.output_prefix,
                             obj=self.get_stats_obj())
예제 #2
0
  def run(self):
    t_0 = time()
    self.ann = self.model.get_ss_annotation()

    self.get_grm()
    self.model.setup_ncs_constraints_groups()

    self.init_model_statistics = self.get_statistics(self.model)

    #
    # Cablam idealization
    #
    self.params.cablam_idealization.find_ss_after_fixes = False
    ci_results = cablam_idealization(
        model=self.model,
        params=self.params.cablam_idealization,
        log=self.log).get_results()
    self.model = ci_results.model
    self.after_cablam_statistics = self.get_statistics(self.model)
    if self.params.debug:
      self.shift_and_write_result(
          model = self.model,
          fname_suffix="cablam_id")


    # Here we are preparing maps if needed.
    if self.user_supplied_map is not None:
      self.prepare_user_map()

    if self.reference_map is None and self.params.use_map_for_reference:
      self.prepare_reference_map_3()

    if self.params.run_minimization_first:
      # running simple minimization and updating all
      # self.master, self.working, etc...
      # self.whole_pdb_h.reset_atom_i_seqs()
      if self.init_ref_map is None:
        self.prepare_init_reference_map()
      print >> self.log, "Minimization first"
      self.minimize(
          model=self.model,
          original_pdb_h=self.original_hierarchy,
          excl_string_selection=None, # don't need if we have map
          reference_map=self.init_ref_map,
          )
      self.init_gm_model_statistics = self.get_statistics(self.model)
      if self.params.debug:
        self.shift_and_write_result(
            model = self.model,
            fname_suffix="init_gm")

    if (self.init_gm_model_statistics is not None
        and self.init_gm_model_statistics.ramachandran.outliers == 0
        and self.init_gm_model_statistics.omega.twisted_general <= 0.01
        and self.init_gm_model_statistics.omega.twisted_proline <= 0.01
        and self.init_gm_model_statistics.omega.cis_general <= 0.01
        and self.init_gm_model_statistics.omega.cis_proline <= 0.01
        and self.init_gm_model_statistics.rotamer.outliers <= 0.01):
      print >> self.log, "Simple minimization was enough"
      # Early exit!!!
      self.shift_and_write_result(
          model=self.model,
          fname_suffix="all_idealized")
      self.final_model_statistics = self.get_statistics(self.model)
      # self.original_boxed_hierarchy.write_pdb_file(file_name="original_boxed_end.pdb")
      self.time_for_run = time() - t_0
      if self.params.output_pkl:
        easy_pickle.dump(
            file_name="%s.pkl" % self.params.output_prefix,
            obj = self.get_stats_obj())
      return

    self.filtered_whole_ann = None
    if self.ann is not None:
      self.filtered_whole_ann = self.ann.deep_copy()
      print >> self.log, "Original SS annotation"
      print >> self.log, self.ann.as_pdb_str()
      if self.params.filter_input_ss:
        self.filtered_whole_ann = self.ann.filter_annotation(
            hierarchy=self.model.get_hierarchy(),
            asc=self.model.get_atom_selection_cache())
      print >> self.log, "Filtered SS annotation"
      print >> self.log, self.filtered_whole_ann.as_pdb_str()
      self.model.set_ss_annotation(self.filtered_whole_ann)

    # getting grm with SS restraints
    self.update_ss_in_grm(self.filtered_whole_ann)

    if (self.ann is None or
        self.ann.get_n_helices() + self.ann.get_n_sheets() == 0 or
        not self.params.ss_idealization.enabled):
      print >> self.log, "No secondary structure annotations found or SS idealization is disabled."
      print >> self.log, "Secondary structure substitution step will be skipped"
      self.log.flush()
      # here we want to do geometry minimization anyway!
      negate_selection = None
      if self.reference_map is None:
        outlier_selection_txt = mmtbx.building.loop_closure.utils. \
          rama_score_selection(self.model, self.model.get_ramachandran_manager(), "outlier",1)
        print >> self.log, "outlier_selection_txt", outlier_selection_txt
        negate_selection = "all"
        if outlier_selection_txt != "" and outlier_selection_txt is not None:
          negate_selection = "not (%s)" % outlier_selection_txt
      # if self.params.run_minimization_first:
      # self.minimize(
      #     model=self.model,
      #     original_pdb_h=self.whole_pdb_h,
      #     ncs_restraints_group_list=self.filtered_ncs_restr_group_list,
      #     excl_string_selection=negate_selection,
      #     reference_map=self.reference_map)
      # self.original_boxed_hierarchy.write_pdb_file(file_name="original_boxed_h_1.pdb")
    else:
      if self.params.debug:
        self.params.ss_idealization.file_name_before_regularization = \
            "%s_ss_before_reg.pdb" % self.params.output_prefix
      self.params.ss_idealization.skip_good_ss_elements = True
      ssb.substitute_ss(
          model = self.model,
          params=self.params.ss_idealization,
          verbose=self.params.verbose,
          reference_map=self.master_map,
          log=self.log)
      self.log.flush()

    self.after_ss_idealization = self.get_statistics(self.model)
    self.shift_and_write_result(
          model=self.model,
          fname_suffix="ss_ideal_stat")

    # Write resulting pdb file.
    if self.params.debug:
      self.shift_and_write_result(
          model=self.model,
          fname_suffix="ss_ideal",
          )
    # self.params.loop_idealization.minimize_whole = not self.model.ncs_constraints_present() and self.params.loop_idealization.minimize_whole
    self.params.loop_idealization.debug = self.params.debug or self.params.loop_idealization.debug
    # self.params.loop_idealization.enabled = False
    # self.params.loop_idealization.variant_search_level = 0
    print >> self.log, "Starting loop idealization"
    loop_ideal = loop_idealization(
        self.model,
        params=self.params.loop_idealization,
        reference_map=self.master_map,
        log=self.log,
        verbose=True)
    self.log.flush()
    if self.params.debug:
      self.shift_and_write_result(
          model = self.model,
          fname_suffix="rama_ideal")
    self.after_loop_idealization = self.get_statistics(self.model)

    # fixing remaining rotamer outliers
    if (self.params.additionally_fix_rotamer_outliers and
        self.after_loop_idealization.rotamer.outliers > 0.004):

      print >> self.log, "Fixing rotamers..."
      self.log.flush()
      if self.params.debug:
        self.shift_and_write_result(
          model = self.model,
          fname_suffix="just_before_rota")
      # run reduce
      assert (libtbx.env.has_module(name="reduce"))

      input_str = self.model.model_as_pdb(do_not_shift_back=True)
      build = "phenix.reduce" + " -quiet -build -allalt -NUC -"
      output = easy_run.fully_buffered(build,
                                   stdin_lines=input_str)
      p = mmtbx.model.manager.get_default_pdb_interpretation_params()
      p.pdb_interpretation.use_neutron_distances=True
      p.pdb_interpretation.ncs_search = self.params_for_model.pdb_interpretation.ncs_search
      p.pdb_interpretation.ncs_search.exclude_selection="water"
      h_input = iotbx.pdb.input(lines=output.stdout_lines, source_info=None)
      h_model = mmtbx.model.manager(model_input = h_input,
          process_input=True,
          restraint_objects=self.model._restraint_objects,
          pdb_interpretation_params=p)
      sel = h_model.get_hd_selection()
      h_model.setup_ncs_constraints_groups(filter_groups=True)
      h_model._update_master_sel()

      result = mmtbx.refinement.real_space.fit_residues.run(
          vdw_radii         = h_model.get_vdw_radii(),
          bselection        = h_model.get_master_selection(),
          pdb_hierarchy     = h_model.get_hierarchy(),
          crystal_symmetry  = self.model.crystal_symmetry(),
          map_data          = self.master_map,
          rotamer_manager   = mmtbx.idealized_aa_residues.rotamer_manager.load(),
          sin_cos_table     = scitbx.math.sin_cos_table(n=10000),
          backbone_sample   = False,
          mon_lib_srv       = h_model.get_mon_lib_srv(),
          log               = self.log)
      self.model.set_sites_cart(
          sites_cart = result.pdb_hierarchy.select(~sel).atoms().extract_xyz(),
          update_grm = True)
      self.model.set_sites_cart_from_hierarchy(multiply_ncs=True)
    if self.params.debug:
      self.shift_and_write_result(
          model = self.model,
          fname_suffix="rota_ideal")

    self.after_rotamer_fixing = self.get_statistics(self.model)
    ref_hierarchy_for_final_gm = self.original_boxed_hierarchy
    if not self.params.use_starting_model_for_final_gm:
      ref_hierarchy_for_final_gm = self.model.get_hierarchy().deep_copy()
    ref_hierarchy_for_final_gm.reset_atom_i_seqs()

    if self.model.ncs_constraints_present():
      print >> self.log, "Using ncs"
      # assert 0
    else:
      print >> self.log, "Not using ncs"
      # assert 0

    # need to update SS manager for the whole model here.
    if self.params.use_ss_restraints:
      ss_params = sec_str_master_phil.fetch().extract()
      ss_params.secondary_structure.protein.remove_outliers = not self.params.ss_idealization.enabled
      self.set_ss_restraints(
          ss_annotation=self.filtered_whole_ann,
          params=ss_params.secondary_structure)
    if self.params.run_minimization_last:
      print >> self.log, "loop_ideal.ref_exclusion_selection", loop_ideal.ref_exclusion_selection
      print >> self.log, "Minimizing whole model"
      self.minimize(
          model = self.model,
          original_pdb_h=ref_hierarchy_for_final_gm,
          excl_string_selection=loop_ideal.ref_exclusion_selection,
          reference_map = self.reference_map)
    self.shift_and_write_result(
        model = self.model,
        fname_suffix="all_idealized")
    self.final_model_statistics = self.get_statistics(self.model)
    self.time_for_run = time() - t_0
    if self.params.output_pkl or self.params.debug:
      easy_pickle.dump(
          file_name="%s.pkl" % self.params.output_prefix,
          obj = self.get_stats_obj())