def __init__(self,
              restraints_manager,
              pdb_hierarchy,
              correct_special_position_tolerance,
              ncs_restraints_group_list      = [],
              max_number_of_iterations       = 500,
              number_of_macro_cycles         = 5,
              selection                      = None,
              bond                           = False,
              nonbonded                      = False,
              angle                          = False,
              dihedral                       = False,
              chirality                      = False,
              planarity                      = False,
              parallelity                    = False,
              rmsd_bonds_termination_cutoff  = 0,
              rmsd_angles_termination_cutoff = 0,
              alternate_nonbonded_off_on     = False,
              cdl                            = False,
              rdl                            = False,
              correct_hydrogens              = False,
              fix_rotamer_outliers           = True,
              allow_allowed_rotamers         = True,
              states_collector               = None,
              log                            = None,
              mon_lib_srv                    = None):
   self.log = log
   if self.log is None:
     self.log = sys.stdout
   self.pdb_hierarchy = pdb_hierarchy
   self.minimized = None
   self.mon_lib_srv = mon_lib_srv
   if self.mon_lib_srv is None:
     self.mon_lib_srv = monomer_library.server.server()
   self.restraints_manager = restraints_manager
   assert max_number_of_iterations+number_of_macro_cycles > 0
   assert [bond,nonbonded,angle,dihedral,chirality,planarity,
           parallelity].count(False) < 7
   self.cdl_proxies = None
   self.rdl_proxies = None
   self.rotamer_manager = None
   if fix_rotamer_outliers:
     from mmtbx.rotamer.rotamer_eval import RotamerEval
     self.rotamer_manager = RotamerEval(mon_lib_srv=self.mon_lib_srv)
   if(cdl):
     from mmtbx.conformation_dependent_library.cdl_setup import setup_restraints
     self.cdl_proxies = setup_restraints(self.restraints_manager.geometry)
   self.correct_hydrogens = correct_hydrogens
   if(alternate_nonbonded_off_on and number_of_macro_cycles % 2 != 0):
     number_of_macro_cycles += 1
   import scitbx.lbfgs
   lbfgs_termination_params = scitbx.lbfgs.termination_parameters(
     max_iterations = max_number_of_iterations)
   exception_handling_params = scitbx.lbfgs.exception_handling_parameters(
     ignore_line_search_failed_step_at_lower_bound = True)
   geometry_restraints_flags = geometry_restraints.flags.flags(
     bond               = bond,
     nonbonded          = nonbonded,
     angle              = angle,
     dihedral           = dihedral,
     chirality          = chirality,
     planarity          = planarity,
     parallelity        = parallelity,
     reference_coordinate = True,
     reference_dihedral = True,
     bond_similarity    = True,
     ramachandran_restraints = True)
   self.update_cdl_restraints()
   self.show()
   for i_macro_cycle in xrange(number_of_macro_cycles):
     print >> self.log, "  macro-cycle:", i_macro_cycle
     self.restraints_manager.geometry.update_ramachandran_restraints_phi_psi_targets(
       sites_cart=self.pdb_hierarchy.atoms().extract_xyz())
     if(alternate_nonbonded_off_on and i_macro_cycle<=number_of_macro_cycles/2):
       geometry_restraints_flags.nonbonded = bool(i_macro_cycle % 2)
     self.correct_hydrogen_geometries(self.log)
     self.update_cdl_restraints(macro_cycle=i_macro_cycle)
     if(fix_rotamer_outliers):
       self.pdb_hierarchy, self.restraints_manager = add_rotamer_restraints(
         pdb_hierarchy      = self.pdb_hierarchy,
         restraints_manager = self.restraints_manager,
         selection          = selection,
         sigma              = 10,
         mode               = "fix_outliers",
         accept_allowed     = allow_allowed_rotamers,
         mon_lib_srv        = self.mon_lib_srv,
         rotamer_manager    = self.rotamer_manager)
     sites_cart = self.pdb_hierarchy.atoms().extract_xyz()
     if rdl:
       self.updaterdl(prefix="Update RDL restraints")
     # self.pdb_hierarchy.write_pdb_file("after_fix_%d.pdb" % i_macro_cycle)
     # self.restraints_manager.write_geo_file(
     #     sites_cart=self.pdb_hierarchy.atoms().extract_xyz(),
     #     site_labels= [atom.id_str() for atom in self.pdb_hierarchy.atoms()],
     #     file_name="after_fix_%d.geo" % i_macro_cycle)
     if (ncs_restraints_group_list is not None
         and len(ncs_restraints_group_list)) > 0:
       # do ncs minimization
       print >> self.log, "Using NCS constraints."
       xrs = self.pdb_hierarchy.extract_xray_structure().deep_copy_scatterers()
       refine_selection = flex.size_t(xrange(xrs.scatterers().size()))
       tfg_obj = mmtbx.refinement.minimization_ncs_constraints.\
           target_function_and_grads_geometry_minimization(
               xray_structure=xrs,
               ncs_restraints_group_list=ncs_restraints_group_list,
               refine_selection=refine_selection,
               restraints_manager=self.restraints_manager.geometry,
               refine_sites=True,
               refine_transformations=False,
               )
       minimized = mmtbx.refinement.minimization_ncs_constraints.lbfgs(
         target_and_grads_object      = tfg_obj,
         xray_structure               = xrs,
         ncs_restraints_group_list    = ncs_restraints_group_list,
         refine_selection             = refine_selection,
         finite_grad_differences_test = False,
         max_iterations               = max_number_of_iterations,
         refine_sites                 = True,
         refine_transformations       = False)
       self.pdb_hierarchy.adopt_xray_structure(xrs)
     else:
       self.minimized = lbfgs(
         sites_cart                      = sites_cart,
         correct_special_position_tolerance=correct_special_position_tolerance,
         geometry_restraints_manager     = restraints_manager.geometry,
         geometry_restraints_flags       = geometry_restraints_flags,
         lbfgs_termination_params        = lbfgs_termination_params,
         lbfgs_exception_handling_params = exception_handling_params,
         sites_cart_selection            = selection,
         rmsd_bonds_termination_cutoff   = rmsd_bonds_termination_cutoff,
         rmsd_angles_termination_cutoff  = rmsd_angles_termination_cutoff,
         states_collector                = states_collector,
         site_labels                     = None)
       self.pdb_hierarchy.atoms().set_xyz(sites_cart)
     self.show()
     self.log.flush()
     geometry_restraints_flags.nonbonded = nonbonded
     lbfgs_termination_params = scitbx.lbfgs.termination_parameters(
         max_iterations = max_number_of_iterations)
 def __init__(
     self,
     restraints_manager,
     pdb_hierarchy,
     correct_special_position_tolerance,
     riding_h_manager=None,
     ncs_restraints_group_list=[],  # These are actually for NCS CONSTRAINTS!
     max_number_of_iterations=500,
     number_of_macro_cycles=5,
     selection=None,
     bond=False,
     nonbonded=False,
     angle=False,
     dihedral=False,
     chirality=False,
     planarity=False,
     parallelity=False,
     rmsd_bonds_termination_cutoff=0,
     rmsd_angles_termination_cutoff=0,
     alternate_nonbonded_off_on=False,
     cdl=False,
     rdl=False,
     correct_hydrogens=False,
     fix_rotamer_outliers=True,
     allow_allowed_rotamers=True,
     states_collector=None,
     log=None,
     mon_lib_srv=None,
     ias_selection=None,
 ):
     self.log = log
     if self.log is None:
         self.log = sys.stdout
     self.pdb_hierarchy = pdb_hierarchy
     self.ias_selection = ias_selection
     self.minimized = None
     self.mon_lib_srv = mon_lib_srv
     if self.mon_lib_srv is None:
         self.mon_lib_srv = monomer_library.server.server()
     self.restraints_manager = restraints_manager
     assert max_number_of_iterations + number_of_macro_cycles > 0
     assert [
         bond, nonbonded, angle, dihedral, chirality, planarity, parallelity
     ].count(False) < 7
     self.cdl_proxies = None
     self.rdl_proxies = None
     self.rotamer_manager = None
     if fix_rotamer_outliers:
         from mmtbx.rotamer.rotamer_eval import RotamerEval
         self.rotamer_manager = RotamerEval(mon_lib_srv=self.mon_lib_srv)
     if (cdl):
         from mmtbx.conformation_dependent_library.cdl_setup import setup_restraints
         self.cdl_proxies = setup_restraints(
             self.restraints_manager.geometry)
     self.correct_hydrogens = correct_hydrogens
     if (alternate_nonbonded_off_on and number_of_macro_cycles % 2 != 0):
         number_of_macro_cycles += 1
     import scitbx.lbfgs
     lbfgs_termination_params = scitbx.lbfgs.termination_parameters(
         max_iterations=max_number_of_iterations)
     exception_handling_params = scitbx.lbfgs.exception_handling_parameters(
         ignore_line_search_failed_step_at_lower_bound=True)
     geometry_restraints_flags = geometry_restraints.flags.flags(
         bond=bond,
         nonbonded=nonbonded,
         angle=angle,
         dihedral=dihedral,
         chirality=chirality,
         planarity=planarity,
         parallelity=parallelity,
         reference_coordinate=True,
         reference_dihedral=True,
         bond_similarity=True,
         ramachandran_restraints=True)
     self.update_cdl_restraints()
     self.show()
     for i_macro_cycle in xrange(number_of_macro_cycles):
         print >> self.log, "  macro-cycle:", i_macro_cycle
         self.restraints_manager.geometry.update_ramachandran_restraints_phi_psi_targets(
             sites_cart=self.pdb_hierarchy.atoms().extract_xyz())
         if (alternate_nonbonded_off_on
                 and i_macro_cycle <= number_of_macro_cycles / 2):
             geometry_restraints_flags.nonbonded = bool(i_macro_cycle % 2)
         self.update_cdl_restraints(macro_cycle=i_macro_cycle)
         if (fix_rotamer_outliers):
             self.pdb_hierarchy, self.restraints_manager = add_rotamer_restraints(
                 pdb_hierarchy=self.pdb_hierarchy,
                 restraints_manager=self.restraints_manager,
                 selection=selection,
                 sigma=10,
                 mode="fix_outliers",
                 accept_allowed=allow_allowed_rotamers,
                 mon_lib_srv=self.mon_lib_srv,
                 rotamer_manager=self.rotamer_manager)
         sites_cart = self.pdb_hierarchy.atoms().extract_xyz()
         if rdl:
             self.updaterdl(prefix="Update RDL restraints")
         if (ncs_restraints_group_list is not None
                 and len(ncs_restraints_group_list)) > 0:
             # do ncs minimization
             print >> self.log, "Using NCS constraints."
             xrs = self.pdb_hierarchy.extract_xray_structure(
             ).deep_copy_scatterers()
             refine_selection = flex.size_t(xrange(xrs.scatterers().size()))
             tfg_obj = mmtbx.refinement.minimization_ncs_constraints.\
                 target_function_and_grads_geometry_minimization(
                     xray_structure=xrs,
                     ncs_restraints_group_list=ncs_restraints_group_list, # CONSTRAINTS
                     refine_selection=refine_selection,
                     restraints_manager=self.restraints_manager.geometry,
                     refine_sites=True,
                     refine_transformations=False,
                     )
             minimized = mmtbx.refinement.minimization_ncs_constraints.lbfgs(
                 target_and_grads_object=tfg_obj,
                 xray_structure=xrs,
                 ncs_restraints_group_list=
                 ncs_restraints_group_list,  # CONSTRAINTS
                 refine_selection=refine_selection,
                 finite_grad_differences_test=False,
                 max_iterations=max_number_of_iterations,
                 refine_sites=True,
                 refine_transformations=False)
             self.pdb_hierarchy.adopt_xray_structure(xrs)
         else:
             sites_cart_orig = sites_cart.deep_copy()
             if ias_selection is not None and ias_selection.count(True) > 0:
                 sites_cart = sites_cart.select(~ias_selection)
             self.minimized = lbfgs(
                 sites_cart=sites_cart,
                 riding_h_manager=riding_h_manager,
                 correct_special_position_tolerance=
                 correct_special_position_tolerance,
                 geometry_restraints_manager=restraints_manager.geometry,
                 geometry_restraints_flags=geometry_restraints_flags,
                 lbfgs_termination_params=lbfgs_termination_params,
                 lbfgs_exception_handling_params=exception_handling_params,
                 sites_cart_selection=selection,
                 rmsd_bonds_termination_cutoff=rmsd_bonds_termination_cutoff,
                 rmsd_angles_termination_cutoff=
                 rmsd_angles_termination_cutoff,
                 states_collector=states_collector,
                 site_labels=None)
             if (ias_selection is not None):
                 for i_seq, ias_s in enumerate(
                         ias_selection
                 ):  # assumes that IAS appended to the back
                     if (not ias_s):
                         sites_cart_orig[i_seq] = sites_cart[i_seq]
             else:
                 sites_cart_orig = sites_cart
             self.pdb_hierarchy.atoms().set_xyz(sites_cart_orig)
         self.show()
         self.log.flush()
         geometry_restraints_flags.nonbonded = nonbonded
         lbfgs_termination_params = scitbx.lbfgs.termination_parameters(
             max_iterations=max_number_of_iterations)
 def __init__(self,
              restraints_manager,
              pdb_hierarchy,
              correct_special_position_tolerance,
              max_number_of_iterations       = 500,
              number_of_macro_cycles         = 5,
              selection                      = None,
              bond                           = False,
              nonbonded                      = False,
              angle                          = False,
              dihedral                       = False,
              chirality                      = False,
              planarity                      = False,
              parallelity                    = False,
              rmsd_bonds_termination_cutoff  = 0,
              rmsd_angles_termination_cutoff = 0,
              alternate_nonbonded_off_on     = False,
              cdl                            = False,
              correct_hydrogens              = False,
              fix_rotamer_outliers           = True,
              states_collector               = None,
              log                            = None):
   self.log = log
   if self.log is None:
     self.log = sys.stdout
   self.pdb_hierarchy = pdb_hierarchy
   self.minimized = None
   self.restraints_manager = restraints_manager
   assert max_number_of_iterations+number_of_macro_cycles > 0
   assert [bond,nonbonded,angle,dihedral,chirality,planarity,
           parallelity].count(False) < 7
   self.cdl_proxies = None
   if(cdl):
     from mmtbx.conformation_dependent_library.cdl_setup import setup_restraints
     self.cdl_proxies = setup_restraints(restraints_manager.geometry)
   self.correct_hydrogens = correct_hydrogens
   if(alternate_nonbonded_off_on and number_of_macro_cycles % 2 != 0):
     number_of_macro_cycles += 1
   import scitbx.lbfgs
   lbfgs_termination_params = scitbx.lbfgs.termination_parameters(
     max_iterations = max_number_of_iterations)
   exception_handling_params = scitbx.lbfgs.exception_handling_parameters(
     ignore_line_search_failed_step_at_lower_bound = True)
   geometry_restraints_flags = geometry_restraints.flags.flags(
     bond               = bond,
     nonbonded          = nonbonded,
     angle              = angle,
     dihedral           = dihedral,
     chirality          = chirality,
     planarity          = planarity,
     parallelity        = parallelity,
     reference_coordinate = True,
     reference_dihedral = True,
     bond_similarity    = True,
     ramachandran_restraints = True)
   self.update_cdl_restraints()
   self.show()
   for i_macro_cycle in xrange(number_of_macro_cycles):
     print >> self.log, "  macro-cycle:", i_macro_cycle
     if(alternate_nonbonded_off_on and i_macro_cycle<=number_of_macro_cycles/2):
       geometry_restraints_flags.nonbonded = bool(i_macro_cycle % 2)
     self.correct_hydrogen_geometries(log)
     self.update_cdl_restraints(macro_cycle=i_macro_cycle)
     if(fix_rotamer_outliers):
       self.pdb_hierarchy, self.restraints_manager = add_rotamer_restraints(
         pdb_hierarchy      = self.pdb_hierarchy,
         restraints_manager = self.restraints_manager,
         selection          = selection,
         sigma              = 10,
         mode               = "fix_outliers")
     sites_cart = self.pdb_hierarchy.atoms().extract_xyz()
     self.minimized = lbfgs(
       sites_cart                      = sites_cart,
       correct_special_position_tolerance=correct_special_position_tolerance,
       geometry_restraints_manager     = restraints_manager.geometry,
       geometry_restraints_flags       = geometry_restraints_flags,
       lbfgs_termination_params        = lbfgs_termination_params,
       lbfgs_exception_handling_params = exception_handling_params,
       sites_cart_selection            = selection,
       rmsd_bonds_termination_cutoff   = rmsd_bonds_termination_cutoff,
       rmsd_angles_termination_cutoff  = rmsd_angles_termination_cutoff,
       states_collector                = states_collector,
       site_labels                     = None)
     self.pdb_hierarchy.atoms().set_xyz(sites_cart)
     self.show()
     geometry_restraints_flags.nonbonded = nonbonded
     lbfgs_termination_params = scitbx.lbfgs.termination_parameters(
         max_iterations = max_number_of_iterations)