Beispiel #1
0
  def __init__(self,
      model,
      target_map,
      refine_ncs_operators=False,
      number_of_cycles=1,
      log=None):
    from mmtbx.refinement.geometry_minimization import add_rotamer_restraints
    from mmtbx.refinement.minimization_monitor import minimization_monitor
    self.model = model
    self.log = log
    print >> self.log, "Minimizing using reference map..."
    self.log.flush()

    # copy-paste from cctbx_project/mmtbx/refinement/geometry_minimization.py:
    # minimize_wrapper_for_ramachandran
    self.model.get_restraints_manager().geometry.pair_proxies(
        sites_cart=self.model.get_sites_cart())

    ncs_restraints_group_list = self.model.get_ncs_groups()
    if ncs_restraints_group_list is None:
      ncs_restraints_group_list = []
    ncs_groups=None
    if len(ncs_restraints_group_list) > 0:
      ncs_groups=ncs_restraints_group_list

    min_monitor = minimization_monitor(
        number_of_cycles=number_of_cycles,
        max_number_of_cycles=20,
        mode="no_outliers")
    selection_real_space = None
    import mmtbx.refinement.real_space.weight
    self.w = 1
    print >> log, "number_of_cycles", number_of_cycles
    print >> log, "Stats before minimization:"
    ms = self.model.geometry_statistics()
    ms.show(log=log)

    while min_monitor.need_more_cycles():
      print >> self.log, "Cycle number", min_monitor.get_current_cycle_n()
      self.model.get_restraints_manager().geometry.update_ramachandran_restraints_phi_psi_targets(
          sites_cart=self.model.get_sites_cart())
      print >> self.log, "  Updating rotamer restraints..."
      add_rotamer_restraints(
        pdb_hierarchy      = self.model.get_hierarchy(),
        restraints_manager = self.model.get_restraints_manager(),
        selection          = None,
        sigma              = 5,
        mode               = "fix_outliers",
        accept_allowed     = False,
        mon_lib_srv        = self.model.get_mon_lib_srv(),
        rotamer_manager    = self.model.get_rotamer_manager())
      self.model.set_sites_cart_from_hierarchy()

      if min_monitor.need_weight_optimization():
        # if self.w is None:
        print >> self.log, "  Determining weight..."
        self.log.flush()
        self.weight = mmtbx.refinement.real_space.weight.run(
            map_data                    = target_map,
            xray_structure              = self.model.get_xray_structure(),
            pdb_hierarchy               = self.model.get_hierarchy(),
            geometry_restraints_manager = self.model.get_restraints_manager(),
            rms_bonds_limit             = 0.015,
            rms_angles_limit            = 1.0,
            ncs_groups                  = ncs_groups)

        # division is to put more weight onto restraints. Checked. Works.
        self.w = self.weight.weight/3.0
        # self.w = self.weight.weight/15.0
        # self.w = 0
        # self.w = self.weight.weight
        for s in self.weight.msg_strings:
          print >> self.log, s

      if ncs_restraints_group_list is None or len(ncs_restraints_group_list)==0:
        #No NCS
        print >> self.log, "  Minimizing..."
        print >> self.log, "     with weight %f" % self.w
        self.log.flush()
        refine_object = simple(
            target_map                  = target_map,
            selection                   = None,
            max_iterations              = 150,
            geometry_restraints_manager = self.model.get_restraints_manager().geometry,
            selection_real_space        = selection_real_space,
            states_accumulator          = None,
            ncs_groups                  = ncs_groups)
        refine_object.refine(weight = self.w, xray_structure = self.model.get_xray_structure())
        self.rmsd_bonds_final, self.rmsd_angles_final = refine_object.rmsds()
        print >> log, "RMSDS:", self.rmsd_bonds_final, self.rmsd_angles_final
        # print >> log, "sizes:", len(refine_object.sites_cart()), len(self.xrs.scatterers())
        self.model.set_sites_cart(refine_object.sites_cart(), update_grm=True)
        # print >> log, "sizes", self.xrs.scatterers()
      else:
        # Yes NCS
        # copy-paste from macro_cycle_real_space.py
        # !!! Don't rearrange NCS groups here because master was just fixed!
        # import mmtbx.ncs.ncs_utils as nu
        # nu.get_list_of_best_ncs_copy_map_correlation(
        #     ncs_groups     = ncs_restraints_group_list,
        #     xray_structure = self.model.get_xray_structure(),
        #     map_data       = target_map,
        #     d_min          = 3)
        print >> self.log, "  Minimizing... (NCS)"
        tfg_obj = mmtbx.refinement.minimization_ncs_constraints.\
          target_function_and_grads_real_space(
            map_data                   = target_map,
            xray_structure             = self.model.get_xray_structure(),
            ncs_restraints_group_list  = ncs_restraints_group_list,
            refine_selection           = None,
            real_space_gradients_delta = 1,
            restraints_manager         = self.model.get_restraints_manager(),
            data_weight                = self.w,
            refine_sites               = True)
        minimized = mmtbx.refinement.minimization_ncs_constraints.lbfgs(
          target_and_grads_object      = tfg_obj,
          xray_structure               = self.model.get_xray_structure(),
          ncs_restraints_group_list    = ncs_restraints_group_list,
          refine_selection             = None,
          finite_grad_differences_test = False,
          max_iterations               = 100,
          refine_sites                 = True)
        self.model.set_sites_cart(tfg_obj.xray_structure.sites_cart())
      ncs_restraints_group_list.recalculate_ncs_transforms(self.model.get_sites_cart())
      ms = self.model.geometry_statistics()
      min_monitor.save_cycle_results(geometry=ms)
      ms.show(log=log)
  def __init__(self,
      pdb_h,
      xrs,
      target_map,
      grm=None,
      ncs_restraints_group_list=[],
      mon_lib_srv=None,
      rotamer_manager=None,
      ss_annotation=None,
      refine_ncs_operators=False,
      number_of_cycles=1,
      log=None):
    assert grm is not None
    from mmtbx.refinement.geometry_minimization import add_rotamer_restraints
    from mmtbx.model_statistics import geometry_no_grm
    from mmtbx.refinement.minimization_monitor import minimization_monitor
    self.pdb_h = pdb_h
    self.xrs = xrs
    self.log = log
    # self.cs = self.xrs.crystal_symmetry()
    self.cs = crystal.symmetry(
        unit_cell=self.xrs.crystal_symmetry().unit_cell(),
        space_group=1)
    print >> self.log, "Minimizing using reference map..."
    self.log.flush()
    self.grm = grm
    # copy-paste from cctbx_project/mmtbx/refinement/geometry_minimization.py:
    # minimize_wrapper_for_ramachandran

    self.grm.geometry.pair_proxies(
        sites_cart=self.pdb_h.atoms().extract_xyz())
    if self.grm.geometry.ramachandran_manager is not None:
      self.grm.geometry.ramachandran_manager.update_phi_psi_targets(
          sites_cart=self.pdb_h.atoms().extract_xyz())

    ncs_groups=None
    if len(ncs_restraints_group_list) > 0:
      ncs_groups=ncs_restraints_group_list

    if rotamer_manager is None:
      from mmtbx.rotamer.rotamer_eval import RotamerEval
      rotamer_manager = RotamerEval(mon_lib_srv=mon_lib_srv)

    # self.pdb_h.write_pdb_file(file_name="rsr_before_rot_fix.pdb",
    #     crystal_symmetry=self.xrs.crystal_symmetry())
    # STOP()

    # selection_real_space = xrs.backbone_selection() # XXX What is it???
    min_monitor = minimization_monitor(
        number_of_cycles=number_of_cycles,
        max_number_of_cycles=20,
        mode="no_outliers")
    selection_real_space = None
    import mmtbx.refinement.real_space.weight
    self.w = 1
    print >> log, "number_of_cycles", number_of_cycles
    print >> log, "Stats before minimization:"
    ms = geometry_no_grm(
        pdb_hierarchy=self.pdb_h,
        molprobity_scores=True)
    print >> self.log, ms.format_molprobity_scores(prefix="    ")

    while min_monitor.need_more_cycles():
      # for x in xrange(number_of_cycles):
      print >> self.log, "Cycle number", min_monitor.get_current_cycle_n()
      print >> self.log, "  Updating rotamer restraints..."
      self.pdb_h, grm = add_rotamer_restraints(
        pdb_hierarchy      = self.pdb_h,
        restraints_manager = grm,
        selection          = None,
        sigma              = 5,
        mode               = "fix_outliers",
        accept_allowed     = False,
        mon_lib_srv        = mon_lib_srv,
        rotamer_manager    = rotamer_manager)
      self.xrs = self.pdb_h.extract_xray_structure(crystal_symmetry=self.cs)
      # self.pdb_h.write_pdb_file(file_name="rsr_after_rot_fix.pdb",
      #     crystal_symmetry=self.xrs.crystal_symmetry())
      # if True:
      if ncs_restraints_group_list is None or len(ncs_restraints_group_list)==0:
        #No NCS
        if min_monitor.need_weight_optimization():
          # if self.w is None:
          print >> self.log, "  Determining weight..."
          self.log.flush()
          self.weight = mmtbx.refinement.real_space.weight.run(
              map_data                    = target_map,
              xray_structure              = self.xrs,
              pdb_hierarchy               = self.pdb_h,
              geometry_restraints_manager = grm,
              rms_bonds_limit             = 0.015,
              rms_angles_limit            = 1.0)
          # for s in self.weight.msg_strings:
          #   print >> self.log, s

          # division is to put more weight onto restraints. Checked. Works.
          self.w = self.weight.weight/3.0
          # self.w = self.weight.weight/15.0
          # self.w = 0
          # self.w = self.weight.weight
          # print >> self.log, self.w
        print >> self.log, "  Minimizing..."
        print >> self.log, "     with weight %f" % self.w
        self.log.flush()
        refine_object = simple(
            target_map                  = target_map,
            selection                   = None,
            max_iterations              = 150,
            geometry_restraints_manager = grm.geometry,
            selection_real_space        = selection_real_space,
            states_accumulator          = None,
            ncs_groups                  = ncs_groups)
        refine_object.refine(weight = self.w, xray_structure = self.xrs)
        self.rmsd_bonds_final, self.rmsd_angles_final = refine_object.rmsds()
        print >> log, "RMSDS:", self.rmsd_bonds_final, self.rmsd_angles_final
        # print >> log, "sizes:", len(refine_object.sites_cart()), len(self.xrs.scatterers())
        self.xrs=self.xrs.replace_sites_cart(
            new_sites=refine_object.sites_cart(), selection=None)
        # print >> log, "sizes", self.xrs.scatterers()
      else:
        # Yes NCS
        # copy-paste from macro_cycle_real_space.py
        import mmtbx.ncs.ncs_utils as nu
        nu.get_list_of_best_ncs_copy_map_correlation(
            ncs_groups     = ncs_restraints_group_list,
            xray_structure = self.xrs,
            map_data       = target_map,
            d_min          = 3)
        if min_monitor.need_weight_optimization():
          # if self.w is None:
          print >> self.log, "  Determining weight... (NCS)",
          self.weight = mmtbx.refinement.real_space.weight.run(
              map_data                    = target_map,
              xray_structure              = self.xrs,#.select(sel_master),
              pdb_hierarchy               = self.pdb_h,#.select(sel_master),
              geometry_restraints_manager = grm,
              rms_bonds_limit             = 0.01,
              rms_angles_limit            = 1.0,
              ncs_groups                  = ncs_restraints_group_list)
          # division supposed to put more weight onto restraints. Need checking.
          self.w = self.weight.weight/3.0
          for s in self.weight.msg_strings:
            print >> self.log, s
        print >> self.log, "  Minimizing... (NCS)"
        tfg_obj = mmtbx.refinement.minimization_ncs_constraints.\
          target_function_and_grads_real_space(
            map_data                   = target_map,
            xray_structure             = self.xrs,
            ncs_restraints_group_list  = ncs_restraints_group_list,
            refine_selection           = None,
            real_space_gradients_delta = 1,
            restraints_manager         = grm,
            data_weight                = self.w,
            refine_sites               = True)
        minimized = mmtbx.refinement.minimization_ncs_constraints.lbfgs(
          target_and_grads_object      = tfg_obj,
          xray_structure               = self.xrs,
          ncs_restraints_group_list    = ncs_restraints_group_list,
          refine_selection             = None,
          finite_grad_differences_test = False,
          max_iterations               = 100,
          refine_sites                 = True)
        self.xrs = tfg_obj.xray_structure
          # self.structure_monitor.update(
          #   xray_structure = tfg_obj.xray_structure,
          #   accept_as_is   = True)
      self.pdb_h.adopt_xray_structure(self.xrs)
      ms = geometry_no_grm(
          pdb_hierarchy=self.pdb_h,
          molprobity_scores=True)
      min_monitor.save_cycle_results(geometry=ms)
      print >> self.log, ms.format_molprobity_scores(prefix="    ")
    def __init__(self,
                 model,
                 target_map,
                 refine_ncs_operators=False,
                 number_of_cycles=1,
                 cycles_to_converge=2,
                 log=None):

        # completely new way of doing this. using RSR macro-cycle
        # for test compatibility:
        print("Minimizing using reference map...", file=log)
        if model.ncs_constraints_present():
            print("  Minimizing... (NCS)", file=log)
        else:
            print("  Minimizing...", file=log)
        from phenix.refinement.macro_cycle_real_space import run as rsr_mc_run
        import scitbx.math
        from phenix.command_line.real_space_refine import extract_rigid_body_selections
        from phenix.command_line.real_space_refine import master_params as rsr_master_params
        from mmtbx.refinement.real_space.utils import target_map as rsr_target_map
        import mmtbx.idealized_aa_residues.rotamer_manager
        sin_cos_table = scitbx.math.sin_cos_table(n=10000)
        params = rsr_master_params().extract()
        params.pdb_interpretation = model._pdb_interpretation_params.pdb_interpretation
        params.refinement.run = "minimization_global+local_grid_search"
        params.refine_ncs_operators = False
        params.output.write_all_states = True
        rotamer_manager = mmtbx.idealized_aa_residues.rotamer_manager.load(
            rotamers="favored")

        rigid_body_selections = extract_rigid_body_selections(
            params=params,
            ncs_groups=model.get_ncs_groups(),
            pdb_hierarchy=model.get_hierarchy())
        rsr_tm = rsr_target_map(map_data=target_map,
                                xray_structure=model.get_xray_structure(),
                                d_min=3.8,
                                atom_radius=params.refinement.atom_radius)
        res = rsr_mc_run(params=params,
                         model=model,
                         target_map=rsr_tm,
                         log=log,
                         ncs_groups=model.get_ncs_groups(),
                         rotamer_manager=rotamer_manager,
                         sin_cos_table=sin_cos_table,
                         rigid_body_selections=rigid_body_selections)
        model.set_sites_cart_from_hierarchy(res.model.get_hierarchy())
        res.structure_monitor.states_collector.write(
            file_name="rsr_all_states.pdb")
        return
        # end ===================================================

        from mmtbx.refinement.geometry_minimization import add_rotamer_restraints
        from mmtbx.refinement.minimization_monitor import minimization_monitor
        self.model = model
        self.log = log
        print("Minimizing using reference map...", file=self.log)
        self.log.flush()

        # copy-paste from cctbx_project/mmtbx/refinement/geometry_minimization.py:
        # minimize_wrapper_for_ramachandran
        self.model.get_restraints_manager().geometry.pair_proxies(
            sites_cart=self.model.get_sites_cart())

        ncs_restraints_group_list = self.model.get_ncs_groups()
        if ncs_restraints_group_list is None:
            ncs_restraints_group_list = []
        ncs_groups = None
        if len(ncs_restraints_group_list) > 0:
            ncs_groups = ncs_restraints_group_list

        min_monitor = minimization_monitor(
            number_of_cycles=number_of_cycles,
            max_number_of_cycles=20,
            cycles_to_converge=cycles_to_converge,
            mode="no_outliers")
        selection_real_space = None
        import mmtbx.refinement.real_space.weight
        self.w = 1
        print("number_of_cycles", number_of_cycles, file=log)
        print("Stats before minimization:", file=log)
        ms = self.model.geometry_statistics()
        ms.show(log=log)

        while min_monitor.need_more_cycles():
            print("Cycle number",
                  min_monitor.get_current_cycle_n(),
                  file=self.log)
            self.model.get_restraints_manager().geometry.\
                update_ramachandran_restraints_phi_psi_targets(
                    hierarchy=self.model.get_hierarchy())
            print("  Updating rotamer restraints...", file=self.log)
            add_rotamer_restraints(
                pdb_hierarchy=self.model.get_hierarchy(),
                restraints_manager=self.model.get_restraints_manager(),
                selection=None,
                sigma=5,
                mode="fix_outliers",
                accept_allowed=False,
                mon_lib_srv=self.model.get_mon_lib_srv(),
                rotamer_manager=self.model.get_rotamer_manager())
            self.model.set_sites_cart_from_hierarchy()

            if min_monitor.need_weight_optimization():
                # if self.w is None:
                print("  Determining weight...", file=self.log)
                self.log.flush()
                self.weight = mmtbx.refinement.real_space.weight.run(
                    map_data=target_map,
                    xray_structure=self.model.get_xray_structure(),
                    pdb_hierarchy=self.model.get_hierarchy(),
                    geometry_restraints_manager=self.model.
                    get_restraints_manager(),
                    rms_bonds_limit=0.015,
                    rms_angles_limit=1.0,
                    ncs_groups=ncs_groups)

                # division is to put more weight onto restraints. Checked. Works.
                self.w = self.weight.weight / 3.0
                # self.w = self.weight.weight/15.0
                # self.w = 0
                # self.w = self.weight.weight
                for s in self.weight.msg_strings:
                    print(s, file=self.log)

            if ncs_restraints_group_list is None or len(
                    ncs_restraints_group_list) == 0:
                #No NCS
                print("  Minimizing...", file=self.log)
                print("     with weight %f" % self.w, file=self.log)
                self.log.flush()
                refine_object = simple(
                    target_map=target_map,
                    selection=None,
                    max_iterations=150,
                    geometry_restraints_manager=self.model.
                    get_restraints_manager().geometry,
                    selection_real_space=selection_real_space,
                    states_accumulator=None,
                    ncs_groups=ncs_groups)
                refine_object.refine(
                    weight=self.w,
                    xray_structure=self.model.get_xray_structure())
                self.rmsd_bonds_final, self.rmsd_angles_final = refine_object.rmsds(
                )
                print("RMSDS:",
                      self.rmsd_bonds_final,
                      self.rmsd_angles_final,
                      file=log)
                # print >> log, "sizes:", len(refine_object.sites_cart()), len(self.xrs.scatterers())
                self.model.set_sites_cart(refine_object.sites_cart(),
                                          update_grm=True)
                # print >> log, "sizes", self.xrs.scatterers()
            else:
                # Yes NCS
                # copy-paste from macro_cycle_real_space.py
                # !!! Don't rearrange NCS groups here because master was just fixed!
                # import mmtbx.ncs.ncs_utils as nu
                # nu.get_list_of_best_ncs_copy_map_correlation(
                #     ncs_groups     = ncs_restraints_group_list,
                #     xray_structure = self.model.get_xray_structure(),
                #     map_data       = target_map,
                #     d_min          = 3)
                print("  Minimizing... (NCS)", file=self.log)
                tfg_obj = mmtbx.refinement.minimization_ncs_constraints.\
                  target_function_and_grads_real_space(
                    map_data                   = target_map,
                    xray_structure             = self.model.get_xray_structure(),
                    ncs_restraints_group_list  = ncs_restraints_group_list,
                    refine_selection           = None,
                    real_space_gradients_delta = 1,
                    restraints_manager         = self.model.get_restraints_manager(),
                    data_weight                = self.w,
                    refine_sites               = True)
                minimized = mmtbx.refinement.minimization_ncs_constraints.lbfgs(
                    target_and_grads_object=tfg_obj,
                    xray_structure=self.model.get_xray_structure(),
                    ncs_restraints_group_list=ncs_restraints_group_list,
                    refine_selection=None,
                    finite_grad_differences_test=False,
                    max_iterations=100,
                    refine_sites=True)
                self.model.set_sites_cart(tfg_obj.xray_structure.sites_cart())
            ncs_restraints_group_list.recalculate_ncs_transforms(
                self.model.get_sites_cart())
            ms = self.model.geometry_statistics()
            min_monitor.save_cycle_results(geometry=ms)
            ms.show(log=log)
  def __init__(self,
      pdb_h,
      xrs,
      target_map,
      grm=None,
      ncs_restraints_group_list=[],
      mon_lib_srv=None,
      rotamer_manager=None,
      ss_annotation=None,
      refine_ncs_operators=False,
      number_of_cycles=1,
      log=None):
    from mmtbx.refinement.geometry_minimization import add_rotamer_restraints
    from mmtbx.model_statistics import geometry_no_grm
    self.pdb_h = pdb_h
    self.xrs = xrs
    self.log = log
    self.cs = self.xrs.crystal_symmetry()
    print >> self.log, "Minimizing using reference map..."
    self.log.flush()
    self.grm = grm
    # create a new one
    # copy-paste from cctbx_project/mmtbx/refinement/geometry_minimization.py:
    # minimize_wrapper_for_ramachandran
    if self.grm is None:
      from mmtbx.monomer_library.pdb_interpretation import grand_master_phil_str
      from mmtbx.geometry_restraints import reference
      from mmtbx.command_line.geometry_minimization import \
          get_geometry_restraints_manager
      from libtbx.utils import null_out
      from scitbx.array_family import flex
      import mmtbx.utils
      if self.log is None:
        self.log = null_out()
      params_line = grand_master_phil_str
      import iotbx.phil
      params = iotbx.phil.parse(
          input_string=params_line, process_includes=True).extract()
      params.pdb_interpretation.clash_guard.nonbonded_distance_threshold=None
      params.pdb_interpretation.peptide_link.ramachandran_restraints = True
      params.pdb_interpretation.peptide_link.oldfield.weight_scale=3
      params.pdb_interpretation.peptide_link.oldfield.plot_cutoff=0.03
      params.pdb_interpretation.nonbonded_weight = 500
      params.pdb_interpretation.c_beta_restraints=True
      params.pdb_interpretation.max_reasonable_bond_distance = None
      params.pdb_interpretation.peptide_link.apply_peptide_plane = True
      params.pdb_interpretation.ncs_search.enabled = True
      params.pdb_interpretation.restraints_library.rdl = True
      processed_pdb_files_srv = mmtbx.utils.\
          process_pdb_file_srv(
              crystal_symmetry= self.cs,
              pdb_interpretation_params = params.pdb_interpretation,
              stop_for_unknowns         = False,
              log=self.log,
              cif_objects=None)
      processed_pdb_file, junk = processed_pdb_files_srv.\
          process_pdb_files(raw_records=flex.split_lines(self.pdb_h.as_pdb_string()))
      mon_lib_srv = processed_pdb_files_srv.mon_lib_srv
      ener_lib = processed_pdb_files_srv.ener_lib
      ncs_restraints_group_list = []
      if processed_pdb_file.ncs_obj is not None:
        ncs_restraints_group_list = processed_pdb_file.ncs_obj.get_ncs_restraints_group_list()
      grm = get_geometry_restraints_manager(
          processed_pdb_file, xrs, params=params)
      # dealing with SS
      if ss_annotation is not None:
        from mmtbx.secondary_structure import manager
        ss_manager = manager(
            pdb_hierarchy=self.pdb_h,
            geometry_restraints_manager=grm.geometry,
            sec_str_from_pdb_file=ss_annotation,
            params=None,
            mon_lib_srv=mon_lib_srv,
            verbose=-1,
            log=self.log)
        grm.geometry.set_secondary_structure_restraints(
            ss_manager=ss_manager,
            hierarchy=self.pdb_h,
            log=self.log)
    else:
      self.grm.geometry.pair_proxies(
          sites_cart=self.pdb_h.atoms().extract_xyz())
      if self.grm.geometry.ramachandran_manager is not None:
        self.grm.geometry.ramachandran_manager.update_phi_psi_targets(
            sites_cart=self.pdb_h.atoms().extract_xyz())

    ncs_groups=None
    if len(ncs_restraints_group_list) > 0:
      ncs_groups=ncs_restraints_group_list

    if rotamer_manager is None:
      from mmtbx.rotamer.rotamer_eval import RotamerEval
      rotamer_manager = RotamerEval(mon_lib_srv=mon_lib_srv)

    self.pdb_h.write_pdb_file(file_name="rsr_before_rot_fix.pdb",
        crystal_symmetry=self.xrs.crystal_symmetry())
    # STOP()

    selection_real_space = xrs.backbone_selection()
    # selection_real_space = None
    import mmtbx.refinement.real_space.weight
    self.w = None
    for x in xrange(number_of_cycles):
      print >> self.log, "  Updating rotamer restraints..."
      self.pdb_h, grm = add_rotamer_restraints(
        pdb_hierarchy      = self.pdb_h,
        restraints_manager = grm,
        selection          = None,
        sigma              = 5,
        mode               = "fix_outliers",
        accept_allowed     = False,
        mon_lib_srv        = mon_lib_srv,
        rotamer_manager    = rotamer_manager)
      self.xrs = self.pdb_h.extract_xray_structure(crystal_symmetry=self.cs)
      self.pdb_h.write_pdb_file(file_name="rsr_after_rot_fix.pdb",
          crystal_symmetry=self.xrs.crystal_symmetry())
      # if True:
      if ncs_restraints_group_list is None or len(ncs_restraints_group_list)==0:
        #No NCS
        if self.w is None:
          print >> self.log, "  Determining weight..."
          self.log.flush()
          self.weight = mmtbx.refinement.real_space.weight.run(
              map_data                    = target_map,
              xray_structure              = self.xrs,
              pdb_hierarchy               = self.pdb_h,
              geometry_restraints_manager = grm,
              rms_bonds_limit             = 0.015,
              rms_angles_limit            = 1.0)
          # division is to put more weight onto restraints. Checked. Works.
          self.w = self.weight.weight/3.0
          # self.w = self.weight.weight
          # self.w =2
          # print >> self.log, self.w
          for s in self.weight.msg_strings:
            print >> self.log, s
        print >> self.log, "  Minimizing..."
        print >> self.log, "     with weight %f" % self.w
        self.log.flush()
        refine_object = simple(
            target_map                  = target_map,
            selection                   = None,
            max_iterations              = 150,
            geometry_restraints_manager = grm.geometry,
            selection_real_space        = selection_real_space,
            states_accumulator          = None,
            ncs_groups                  = ncs_groups)
        refine_object.refine(weight = self.w, xray_structure = self.xrs)
        self.rmsd_bonds_final, self.rmsd_angles_final = refine_object.rmsds()
        print >> log, "RMSDS:", self.rmsd_bonds_final, self.rmsd_angles_final
        # print >> log, "sizes:", len(refine_object.sites_cart()), len(self.xrs.scatterers())
        self.xrs=self.xrs.replace_sites_cart(
            new_sites=refine_object.sites_cart(), selection=None)
        # print >> log, "sizes", self.xrs.scatterers()
      else:
        # Yes NCS
        # copy-paste from macro_cycle_real_space.py
        import mmtbx.ncs.ncs_utils as nu
        nu.get_list_of_best_ncs_copy_map_correlation(
            ncs_groups     = ncs_restraints_group_list,
            xray_structure = self.xrs,
            map_data       = target_map,
            d_min          = 3)
        if self.w is None:
          print >> self.log, "  Determining weight... (NCS)",
          self.weight = mmtbx.refinement.real_space.weight.run(
              map_data                    = target_map,
              xray_structure              = self.xrs,#.select(sel_master),
              pdb_hierarchy               = self.pdb_h,#.select(sel_master),
              geometry_restraints_manager = grm,
              rms_bonds_limit             = 0.01,
              rms_angles_limit            = 1.0,
              ncs_groups                  = ncs_restraints_group_list)
          # division supposed to put more weight onto restraints. Need checking.
          self.w = self.weight.weight/3.0
          for s in self.weight.msg_strings:
            print >> self.log, s
        print >> self.log, "  Minimizing... (NCS)"

        actions = [[True, False], ]
        if refine_ncs_operators:
          actions = [[False, True], [True, False]]
        for action in actions:
          refine_sites, refine_transformations = action
          tfg_obj = mmtbx.refinement.minimization_ncs_constraints.\
            target_function_and_grads_real_space(
              map_data                   = target_map,
              xray_structure             = self.xrs,
              ncs_restraints_group_list  = ncs_restraints_group_list,
              refine_selection           = None,
              real_space_gradients_delta = 1,
              restraints_manager         = grm,
              data_weight                = self.w,
              refine_sites               = refine_sites,
              refine_transformations     = refine_transformations)
          minimized = mmtbx.refinement.minimization_ncs_constraints.lbfgs(
            target_and_grads_object      = tfg_obj,
            xray_structure               = self.xrs,
            ncs_restraints_group_list    = ncs_restraints_group_list,
            refine_selection             = None,
            finite_grad_differences_test = False,
            max_iterations               = 100,
            refine_sites                 = refine_sites,
            refine_transformations       = refine_transformations)
          self.xrs = tfg_obj.xray_structure
          # self.structure_monitor.update(
          #   xray_structure = tfg_obj.xray_structure,
          #   accept_as_is   = True)
      self.pdb_h.adopt_xray_structure(self.xrs)
      # ms = geometry_no_grm(
      #     pdb_hierarchy=self.pdb_h,
      #     molprobity_scores=True)
      # print >> self.log, ms.format_molprobity_scores(prefix="    ")


    # print >> log, "pdb_h", self.pdb_h.atoms_size()
    self.pdb_h.write_pdb_file("after_map_min.pdb")