Example #1
0
 def __init__(self,
              map_data,
              xray_structure,
              ncs_restraints_group_list,
              real_space_gradients_delta,
              refine_selection=None,
              use_ncs_constraints=True,
              restraints_manager=None,
              data_weight=None,
              refine_sites=False,
              refine_transformations=False):
     adopt_init_args(self, locals())
     self.refine_selection = nu.get_refine_selection(
         refine_selection=self.refine_selection,
         number_of_atoms=self.xray_structure.sites_cart().size())
     self.extended_ncs_selection = nu.get_extended_ncs_selection(
         ncs_restraints_group_list=ncs_restraints_group_list,
         refine_selection=self.refine_selection)
     self.unit_cell = self.xray_structure.unit_cell()
     # get selection to refine
     asu_size = xray_structure.scatterers().size()
     if refine_sites:
         # Use all atoms to refine
         self.selection = flex.bool(asu_size, refine_selection)
     elif refine_transformations:
         # use only NCS related atoms (Without the Master NCS copy)
         self.selection = nu.get_ncs_related_selection(
             ncs_restraints_group_list=ncs_restraints_group_list,
             asu_size=asu_size)
Example #2
0
 def __init__(self,
              fmodel,
              ncs_restraints_group_list,
              refine_selection=None,
              use_ncs_constraints=True,
              restraints_manager=None,
              data_weight=None,
              refine_sites=False,
              refine_u_iso=False,
              refine_transformations=False,
              iso_restraints=None,
              use_hd=False):
     adopt_init_args(self, locals())
     asu_size = self.fmodel.xray_structure.sites_cart().size()
     self.refine_selection = nu.get_refine_selection(
         refine_selection=self.refine_selection, number_of_atoms=asu_size)
     self.extended_ncs_selection = nu.get_extended_ncs_selection(
         ncs_restraints_group_list=ncs_restraints_group_list,
         refine_selection=self.refine_selection)
     self.fmodel.xray_structure.scatterers().flags_set_grads(state=False)
     self.x_target_functor = self.fmodel.target_functor()
     self.xray_structure = self.fmodel.xray_structure
     if self.refine_sites:
         xray.set_scatterer_grad_flags(
             scatterers=self.fmodel.xray_structure.scatterers(), site=True)
     elif self.refine_u_iso:
         xray.set_scatterer_grad_flags(
             scatterers=self.fmodel.xray_structure.scatterers(), u_iso=True)
     elif self.refine_transformations:
         xray.set_scatterer_grad_flags(
             scatterers=self.fmodel.xray_structure.scatterers(), site=True)
 def __init__(
       self,
       map_data,
       xray_structure,
       ncs_restraints_group_list,
       real_space_gradients_delta,
       refine_selection=None,
       use_ncs_constraints=True,
       restraints_manager=None,
       data_weight=None,
       refine_sites=False,
       refine_transformations=False):
   adopt_init_args(self, locals())
   self.refine_selection = nu.get_refine_selection(
     refine_selection=self.refine_selection,
     number_of_atoms=self.xray_structure.sites_cart().size())
   self.extended_ncs_selection = nu.get_extended_ncs_selection(
     ncs_restraints_group_list=ncs_restraints_group_list,
     refine_selection=self.refine_selection)
   self.unit_cell = self.xray_structure.unit_cell()
   # get selection to refine
   asu_size = xray_structure.scatterers().size()
   if refine_sites:
     # Use all atoms to refine
     self.selection = flex.bool(asu_size, refine_selection)
   elif refine_transformations:
     # use only NCS related atoms (Without the Master NCS copy)
     self.selection = nu.get_ncs_related_selection(
       ncs_restraints_group_list=ncs_restraints_group_list,
       asu_size=asu_size)
Example #4
0
 def test_ncs_selection(self):
   """
   verify that extended_ncs_selection, which include the master ncs copy and
   the portion of the protein we want to refine.
   """
   # print sys._getframe().f_code.co_name
   refine_selection = flex.size_t(range(30))
   result = nu.get_extended_ncs_selection(
     ncs_restraints_group_list=self.ncs_restraints_group_list,
     refine_selection=refine_selection)
   expected = [0, 1, 2, 3, 4, 5, 6, 21, 22, 23, 24, 25, 26, 27, 28, 29]
   assert list(result) == expected
Example #5
0
 def test_ncs_selection(self):
     """
 verify that extended_ncs_selection, which include the master ncs copy and
 the portion of the protein we want to refine.
 """
     # print sys._getframe().f_code.co_name
     refine_selection = flex.size_t(range(30))
     result = nu.get_extended_ncs_selection(
         ncs_restraints_group_list=self.ncs_restraints_group_list,
         refine_selection=refine_selection)
     expected = [0, 1, 2, 3, 4, 5, 6, 21, 22, 23, 24, 25, 26, 27, 28, 29]
     assert list(result) == expected
Example #6
0
 def __init__(self,
              xray_structure,
              ncs_restraints_group_list,
              refine_selection=None,
              use_ncs_constraints=True,
              restraints_manager=None,
              refine_sites=False,
              refine_transformations=False):
     adopt_init_args(self, locals())
     self.refine_selection = nu.get_refine_selection(
         refine_selection=self.refine_selection,
         number_of_atoms=self.xray_structure.sites_cart().size())
     self.extended_ncs_selection = nu.get_extended_ncs_selection(
         ncs_restraints_group_list=ncs_restraints_group_list,
         refine_selection=self.refine_selection)
     self.unit_cell = self.xray_structure.unit_cell()
 def __init__(
       self,
       xray_structure,
       ncs_restraints_group_list,
       refine_selection=None,
       use_ncs_constraints=True,
       restraints_manager=None,
       refine_sites=False,
       refine_transformations=False):
   adopt_init_args(self, locals())
   self.refine_selection = nu.get_refine_selection(
     refine_selection=self.refine_selection,
     number_of_atoms=self.xray_structure.sites_cart().size())
   self.extended_ncs_selection = nu.get_extended_ncs_selection(
     ncs_restraints_group_list=ncs_restraints_group_list,
     refine_selection=self.refine_selection)
   self.unit_cell = self.xray_structure.unit_cell()
 def __init__(self,
              map_data,
              xray_structure,
              ncs_restraints_group_list,
              real_space_gradients_delta,
              refine_selection=None,
              use_ncs_constraints=True,
              restraints_manager=None,
              data_weight=None,
              refine_sites=False):
     adopt_init_args(self, locals())
     self.refine_selection = nu.get_refine_selection(
         refine_selection=self.refine_selection,
         number_of_atoms=self.xray_structure.sites_cart().size())
     self.extended_ncs_selection = nu.get_extended_ncs_selection(
         ncs_restraints_group_list=ncs_restraints_group_list,
         refine_selection=self.refine_selection)
     self.unit_cell = self.xray_structure.unit_cell()
     # get selection to refine
     asu_size = xray_structure.scatterers().size()
     self.selection = flex.bool(asu_size, refine_selection)
 def test_grads_one_ncs_to_asu(self):
   # No more NAGs in NCS selection
   # print sys._getframe().f_code.co_name
   pdb_inp = iotbx.pdb.input(lines=test_pdb_1,source_info=None)
   ncs_inp = ncs.input(hierarchy=pdb_inp.construct_hierarchy(),
       exclude_selection=None)
   pdb_inp = iotbx.pdb.input(source_info=None, lines=test_pdb_1)
   ph = pdb_inp.construct_hierarchy()
   xrs =  pdb_inp.xray_structure_simple()
   #
   nrgl = ncs_inp.get_ncs_restraints_group_list()
   asu_length = ncs_inp.total_asu_length
   #
   refine_selection = nu.get_refine_selection(number_of_atoms=asu_length)
   extended_ncs_selection =  nu.get_extended_ncs_selection(
     ncs_restraints_group_list=nrgl,
     refine_selection=refine_selection)
   #
   self.assertEqual(asu_length, ph.atoms_size())
   self.assertEqual(asu_length, 18)
   #
   xrs_one_ncs_copy = xrs.select(extended_ncs_selection)
   master_grad = xrs_one_ncs_copy.extract_u_iso_or_u_equiv()
   #
   g = grads_one_ncs_to_asu(
     ncs_restraints_group_list=nrgl,
     total_asu_length=asu_length,
     extended_ncs_selection=extended_ncs_selection,
     master_grad=master_grad)
   #
   self.assertEqual(g.size(),18)
   masters = [[0,1,12],[2, 3, 13]]
   copies = [[[4, 5, 14],[8, 9, 16]],[[6, 7, 15],[10, 11, 17]]]
   for m,cs in zip(masters,copies):
     ml = list(g.select(flex.size_t(m)))
     for c in cs:
       cl = list(g.select(flex.size_t(c)))
       self.assertEqual(ml,cl)
 def __init__(
       self,
       fmodel,
       ncs_restraints_group_list,
       refine_selection=None,
       use_ncs_constraints=True,
       restraints_manager=None,
       data_weight=None,
       refine_sites=False,
       refine_u_iso=False,
       refine_transformations=False,
       iso_restraints = None,
       use_hd         = False):
   adopt_init_args(self, locals())
   asu_size = self.fmodel.xray_structure.sites_cart().size()
   self.refine_selection = nu.get_refine_selection(
     refine_selection=self.refine_selection,
     number_of_atoms=asu_size)
   self.extended_ncs_selection = nu.get_extended_ncs_selection(
     ncs_restraints_group_list=ncs_restraints_group_list,
     refine_selection=self.refine_selection)
   self.fmodel.xray_structure.scatterers().flags_set_grads(state=False)
   self.x_target_functor = self.fmodel.target_functor()
   self.xray_structure = self.fmodel.xray_structure
   if self.refine_sites:
     xray.set_scatterer_grad_flags(
       scatterers = self.fmodel.xray_structure.scatterers(),
       site       = True)
   elif self.refine_u_iso:
     xray.set_scatterer_grad_flags(
       scatterers = self.fmodel.xray_structure.scatterers(),
       u_iso      = True)
   elif self.refine_transformations:
     xray.set_scatterer_grad_flags(
       scatterers = self.fmodel.xray_structure.scatterers(),
       site       = True)
 def test_grads_one_ncs_to_asu(self):
     # No more NAGs in NCS selection
     # print sys._getframe().f_code.co_name
     pdb_inp = iotbx.pdb.input(lines=test_pdb_1, source_info=None)
     ncs_inp = ncs.input(hierarchy=pdb_inp.construct_hierarchy(),
                         exclude_selection=None)
     pdb_inp = iotbx.pdb.input(source_info=None, lines=test_pdb_1)
     ph = pdb_inp.construct_hierarchy()
     xrs = pdb_inp.xray_structure_simple()
     #
     nrgl = ncs_inp.get_ncs_restraints_group_list()
     asu_length = ncs_inp.total_asu_length
     #
     refine_selection = nu.get_refine_selection(number_of_atoms=asu_length)
     extended_ncs_selection = nu.get_extended_ncs_selection(
         ncs_restraints_group_list=nrgl, refine_selection=refine_selection)
     #
     self.assertEqual(asu_length, ph.atoms_size())
     self.assertEqual(asu_length, 18)
     #
     xrs_one_ncs_copy = xrs.select(extended_ncs_selection)
     master_grad = xrs_one_ncs_copy.extract_u_iso_or_u_equiv()
     #
     g = grads_one_ncs_to_asu(ncs_restraints_group_list=nrgl,
                              total_asu_length=asu_length,
                              extended_ncs_selection=extended_ncs_selection,
                              master_grad=master_grad)
     #
     self.assertEqual(g.size(), 18)
     masters = [[0, 1, 12], [2, 3, 13]]
     copies = [[[4, 5, 14], [8, 9, 16]], [[6, 7, 15], [10, 11, 17]]]
     for m, cs in zip(masters, copies):
         ml = list(g.select(flex.size_t(m)))
         for c in cs:
             cl = list(g.select(flex.size_t(c)))
             self.assertEqual(ml, cl)
Example #12
0
 def run_test(self):
   # Refinement
   params = mmtbx.f_model.sf_and_grads_accuracy_master_params.extract()
   params.algorithm = "direct"
   # Get the xray_structure of the shaken ASU
   xrs_shaken_asu, dummy, transform_info, ph, ph2 = step_1(
     file_name="one_ncs_in_asu_shaken.pdb",
     crystal_symmetry=self.xrs_one_ncs.crystal_symmetry(),
     write_name='asu_shaken.pdb')
   tr_obj = iotbx.ncs.input(
     hierarchy = ph,
     transform_info = transform_info,
     exclude_selection=None)
   self.ncs_restraints_group_list = tr_obj.get_ncs_restraints_group_list()
   # refine both ncs related and not related atoms
   self.refine_selection = flex.size_t(range(tr_obj.total_asu_length))
   self.extended_ncs_selection = nu.get_extended_ncs_selection(
     ncs_restraints_group_list=tr_obj.get_ncs_restraints_group_list(),
     refine_selection=self.refine_selection)
   assert self.refine_selection.size() == 21, self.refine_selection.size()
   self.fmodel = mmtbx.f_model.manager(
     f_obs                        = self.f_obs,
     r_free_flags                 = self.r_free_flags,
     xray_structure               = xrs_shaken_asu,
     sf_and_grads_accuracy_params = params,
     target_name                  = "ls_wunit_k1")
   r_start = self.fmodel.r_work()
   assert r_start > 0.1, r_start
   print "start r_factor: %6.4f" % r_start
   for macro_cycle in xrange(self.n_macro_cycle):
     data_weight = None
     if(self.use_geometry_restraints):
       self.transformations=False
       data_weight = nu.get_weight(minimized_obj=self)
     target_and_grads_object = mmtbx.refinement.minimization_ncs_constraints.\
       target_function_and_grads_reciprocal_space(
         fmodel                 = self.fmodel,
         ncs_restraints_group_list = self.ncs_restraints_group_list,
         refine_selection     = self.refine_selection,
         restraints_manager     = self.grm,
         data_weight            = data_weight,
         refine_sites           = self.sites,
         refine_u_iso           = self.u_iso,
         iso_restraints         = self.iso_restraints)
     minimized = mmtbx.refinement.minimization_ncs_constraints.lbfgs(
       target_and_grads_object      = target_and_grads_object,
       xray_structure               = self.fmodel.xray_structure,
       ncs_restraints_group_list    = self.ncs_restraints_group_list,
       refine_selection             = self.refine_selection,
       finite_grad_differences_test = self.finite_grad_differences_test,
       max_iterations               = 100,
       refine_sites                 = self.sites,
       refine_u_iso                 = self.u_iso)
     refine_type = 'adp'*self.u_iso + 'sites'*self.sites
     outstr = "  macro_cycle {0:3} ({1})   r_factor: {2:6.4f}   " + \
           self.finite_grad_differences_test * \
           "finite_grad_difference_val: {3:.4f}"
     print outstr.format(
       macro_cycle, refine_type,self.fmodel.r_work(),
       minimized.finite_grad_difference_val)
     assert (minimized.finite_grad_difference_val < 1.0e-3)
     assert approx_equal(self.fmodel.r_work(), target_and_grads_object.fmodel.r_work())
     # break test if r_work is very small
     if target_and_grads_object.fmodel.r_work() < 1.0e-6: break
   # check results
   if(self.u_iso):
     assert approx_equal(self.fmodel.r_work(), 0, 1.e-5)
   elif(self.sites):
     if(self.use_geometry_restraints):
       assert approx_equal(self.fmodel.r_work(), 0, 0.00018)
     else:
       assert approx_equal(self.fmodel.r_work(), 0, 3.e-4)
   else: assert 0
   # output refined model
   xrs_refined = self.fmodel.xray_structure
   output_file_name = "refined_u_iso%s_sites%s.pdb"%(str(self.u_iso),
     str(self.sites))
   assert ph2.atoms().size() == self.fmodel.xray_structure.scatterers().size()
   ph2.atoms().set_xyz(self.fmodel.xray_structure.sites_cart())
   ph2.atoms().set_b(
     self.fmodel.xray_structure.extract_u_iso_or_u_equiv()*adptbx.u_as_b(1.))
   ph2.write_pdb_file(file_name = output_file_name,
     crystal_symmetry=self.fmodel.xray_structure.crystal_symmetry())
   self.test_files_names.append(output_file_name)
   # check final model
   if(not self.use_geometry_restraints):
     # XXX fix later for case self.use_geometry_restraints=True
     pdb_inp_answer = iotbx.pdb.input(source_info=None, lines=ncs_1_copy)
     pdb_inp_refined = iotbx.pdb.input(file_name=output_file_name)
     xrs1 = pdb_inp_answer.xray_structure_simple(
       crystal_symmetry=self.fmodel.xray_structure.crystal_symmetry())
     xrs2 = pdb_inp_refined.xray_structure_simple().select(
       minimized.extended_ncs_selection)
     print xrs1.crystal_symmetry().unit_cell().parameters()
     print xrs2.crystal_symmetry().unit_cell().parameters()
     mmtbx.utils.assert_xray_structures_equal(
       x1 = xrs1,
       x2 = xrs2,
       sites = False)
     delta = flex.vec3_double([xrs1.center_of_mass()]*xrs2.scatterers().size())-\
             flex.vec3_double([xrs2.center_of_mass()]*xrs2.scatterers().size())
     xrs2.set_sites_cart(sites_cart = xrs2.sites_cart()+delta)
     mmtbx.utils.assert_xray_structures_equal(
       x1 = xrs1,
       x2 = xrs2,
       eps=1.e-4)
Example #13
0
 def __init__(self,
              ncs_restraints_group_list,
              target_and_grads_object,
              xray_structure,
              refine_selection=None,
              finite_grad_differences_test=False,
              finite_grad_difference_val=0,
              max_iterations=35,
              refine_sites=False,
              refine_u_iso=False,
              refine_transformations=False):
     """
 NCS constrained ADP and coordinates refinement. Also refines NCS operators.
 """
     adopt_init_args(self,
                     args=locals(),
                     exclude=['ncs_restraints_group_list'])
     self.refine_selection = nu.get_refine_selection(
         refine_selection=self.refine_selection,
         number_of_atoms=self.xray_structure.sites_cart().size())
     self.use_ncs_constraints = target_and_grads_object.use_ncs_constraints
     self.ncs_restraints_group_list = nu.ncs_restraints_group_list_copy(
         ncs_restraints_group_list)
     self.ncs_groups_coordinates_centers = []
     self.extended_ncs_selection = nu.get_extended_ncs_selection(
         ncs_restraints_group_list=self.ncs_restraints_group_list,
         refine_selection=self.refine_selection)
     assert [
         self.refine_sites, self.refine_u_iso, self.refine_transformations
     ].count(True) == 1
     self.total_asu_length = len(xray_structure.sites_cart())
     traditional_convergence_test_eps = 1.0e-6
     if self.use_ncs_constraints:
         xray_structure_one_ncs_copy = xray_structure.select(
             self.extended_ncs_selection)
     else:
         xray_structure_one_ncs_copy = xray_structure.select(
             self.refine_selection)
     if self.refine_sites:
         self.x = xray_structure_one_ncs_copy.sites_cart().as_double()
     elif self.refine_u_iso:
         assert xray_structure_one_ncs_copy.scatterers().size() == \
           xray_structure_one_ncs_copy.use_u_iso().count(True)
         self.x = xray_structure_one_ncs_copy.extract_u_iso_or_u_equiv()
     elif self.refine_transformations:
         # move refinable parameters to coordinate center
         self.ncs_groups_coordinates_centers = nu.get_ncs_groups_centers(
             xray_structure=self.xray_structure,
             ncs_restraints_group_list=self.ncs_restraints_group_list)
         self.ncs_restraints_group_list = nu.shift_translation_to_center(
             shifts=self.ncs_groups_coordinates_centers,
             ncs_restraints_group_list=self.ncs_restraints_group_list)
         self.x = nu.concatenate_rot_tran(
             ncs_restraints_group_list=self.ncs_restraints_group_list)
     minimizer = scitbx.lbfgs.run(
         target_evaluator=self,
         termination_params=scitbx.lbfgs.termination_parameters(
             max_iterations=max_iterations,
             traditional_convergence_test_eps=
             traditional_convergence_test_eps),
         exception_handling_params=scitbx.lbfgs.
         exception_handling_parameters(
             ignore_line_search_failed_rounding_errors=True,
             ignore_line_search_failed_step_at_lower_bound=True,
             ignore_line_search_failed_maxfev=True))
     # change transforms to the original coordinate system
     if self.refine_transformations:
         self.ncs_restraints_group_list = nu.shift_translation_back_to_place(
             shifts=self.ncs_groups_coordinates_centers,
             ncs_restraints_group_list=self.ncs_restraints_group_list)
     if (getattr(self.target_and_grads_object, "finalize", None)):
         self.target_and_grads_object.finalize()
     # pass the refined ncs_restraints_group_list to original object
     for g1, g2 in zip(ncs_restraints_group_list,
                       self.ncs_restraints_group_list):
         for tr1, tr2 in zip(g1.copies, g2.copies):
             tr1.r = tr2.r
             tr1.t = tr2.t
 def __init__(self,
       ncs_restraints_group_list,
       target_and_grads_object,
       xray_structure,
       refine_selection             = None,
       finite_grad_differences_test = False,
       finite_grad_difference_val   = 0,
       max_iterations               = 35,
       refine_sites                 = False,
       refine_u_iso                 = False,
       refine_transformations       = False):
   """
   NCS constrained ADP and coordinates refinement. Also refines NCS operators.
   """
   adopt_init_args(self, args=locals(),exclude=['ncs_restraints_group_list'])
   self.refine_selection = nu.get_refine_selection(
     refine_selection=self.refine_selection,
     number_of_atoms=self.xray_structure.sites_cart().size())
   self.use_ncs_constraints = target_and_grads_object.use_ncs_constraints
   self.ncs_restraints_group_list = nu.ncs_restraints_group_list_copy(
     ncs_restraints_group_list)
   self.ncs_groups_coordinates_centers = []
   self.extended_ncs_selection = nu.get_extended_ncs_selection(
     ncs_restraints_group_list=self.ncs_restraints_group_list,
     refine_selection=self.refine_selection)
   assert [self.refine_sites,
           self.refine_u_iso, self.refine_transformations].count(True) == 1
   self.total_asu_length = len(xray_structure.sites_cart())
   traditional_convergence_test_eps = 1.0e-6
   if self.use_ncs_constraints:
     xray_structure_one_ncs_copy = xray_structure.select(
       self.extended_ncs_selection)
   else:
     xray_structure_one_ncs_copy = xray_structure.select(self.refine_selection)
   if self.refine_sites:
     self.x = xray_structure_one_ncs_copy.sites_cart().as_double()
   elif self.refine_u_iso:
     assert xray_structure_one_ncs_copy.scatterers().size() == \
       xray_structure_one_ncs_copy.use_u_iso().count(True)
     self.x = xray_structure_one_ncs_copy.extract_u_iso_or_u_equiv()
   elif self.refine_transformations:
     # move refinable parameters to coordinate center
     self.ncs_groups_coordinates_centers = nu.get_ncs_groups_centers(
       xray_structure=self.xray_structure,
       ncs_restraints_group_list=self.ncs_restraints_group_list)
     self.ncs_restraints_group_list = nu.shift_translation_to_center(
       shifts = self.ncs_groups_coordinates_centers,
       ncs_restraints_group_list = self.ncs_restraints_group_list)
     self.x = nu.concatenate_rot_tran(
       ncs_restraints_group_list=self.ncs_restraints_group_list)
   minimizer = scitbx.lbfgs.run(
     target_evaluator=self,
     termination_params=scitbx.lbfgs.termination_parameters(
       max_iterations=max_iterations,
       traditional_convergence_test_eps=traditional_convergence_test_eps),
     exception_handling_params=scitbx.lbfgs.exception_handling_parameters(
       ignore_line_search_failed_rounding_errors=True,
       ignore_line_search_failed_step_at_lower_bound=True,
       ignore_line_search_failed_maxfev=True))
   # change transforms to the original coordinate system
   if self.refine_transformations:
     self.ncs_restraints_group_list = nu.shift_translation_back_to_place(
         shifts = self.ncs_groups_coordinates_centers,
         ncs_restraints_group_list = self.ncs_restraints_group_list)
   if(getattr(self.target_and_grads_object, "finalize", None)):
     self.target_and_grads_object.finalize()
   # pass the refined ncs_restraints_group_list to original object
   for g1,g2 in zip(ncs_restraints_group_list,self.ncs_restraints_group_list):
     for tr1,tr2 in zip(g1.copies,g2.copies):
       tr1.r = tr2.r
       tr1.t = tr2.t
 def run_test(self):
   # Refinement
   params = mmtbx.f_model.sf_and_grads_accuracy_master_params.extract()
   params.algorithm = "direct"
   # Get the xray_structure of the shaken ASU
   m_shaken = multimer(
     file_name="one_ncs_in_asu_shaken.pdb",
     round_coordinates=False,
     reconstruction_type='cau',error_handle=True,eps=1e-2)
   xrs_shaken_asu = m_shaken.assembled_multimer.extract_xray_structure(
     crystal_symmetry=self.xrs_one_ncs.crystal_symmetry())
   # force non-rounded coordinates into xray structure
   xrs_shaken_asu.set_sites_cart(m_shaken.sites_cart())
   # Save the shaken ASU for inspection
   m_shaken.write(
     pdb_output_file_name='asu_shaken.pdb',
     crystal_symmetry=self.xrs_one_ncs.crystal_symmetry())
   tr_obj = m_shaken.transforms_obj
   self.ncs_restraints_group_list = tr_obj.get_ncs_restraints_group_list()
   # refine both ncs related and not related atoms
   self.refine_selection = flex.size_t(range(tr_obj.total_asu_length))
   self.extended_ncs_selection = nu.get_extended_ncs_selection(
     ncs_restraints_group_list=tr_obj.get_ncs_restraints_group_list(),
     refine_selection=self.refine_selection)
   assert self.refine_selection.size() == 21
   self.fmodel = mmtbx.f_model.manager(
     f_obs                        = self.f_obs,
     r_free_flags                 = self.r_free_flags,
     xray_structure               = xrs_shaken_asu,
     sf_and_grads_accuracy_params = params,
     target_name                  = "ls_wunit_k1")
   r_start = self.fmodel.r_work()
   assert r_start > 0.1, r_start
   print "start r_factor: %6.4f" % r_start
   for macro_cycle in xrange(self.n_macro_cycle):
     if self.transformations and \
             not self.ncs_restraints_group_list: continue
     data_weight = None
     if(self.use_geometry_restraints):
       data_weight = nu.get_weight(minimized_obj=self)
     target_and_grads_object = mmtbx.refinement.minimization_ncs_constraints.\
       target_function_and_grads_reciprocal_space(
         fmodel                 = self.fmodel,
         ncs_restraints_group_list = self.ncs_restraints_group_list,
         refine_selection     = self.refine_selection,
         restraints_manager     = self.grm,
         data_weight            = data_weight,
         refine_sites           = self.sites,
         refine_u_iso           = self.u_iso,
         refine_transformations = self.transformations,
         iso_restraints         = self.iso_restraints)
     minimized = mmtbx.refinement.minimization_ncs_constraints.lbfgs(
       target_and_grads_object      = target_and_grads_object,
       xray_structure               = self.fmodel.xray_structure,
       ncs_restraints_group_list    = self.ncs_restraints_group_list,
       refine_selection             = self.refine_selection,
       finite_grad_differences_test = self.finite_grad_differences_test,
       max_iterations               = 100,
       refine_sites                 = self.sites,
       refine_u_iso                 = self.u_iso,
       refine_transformations       = self.transformations)
     refine_type = 'adp'*self.u_iso + 'sites'*self.sites \
                   + 'transformation'*self.transformations
     outstr = "  macro_cycle {0:3} ({1})   r_factor: {2:6.4f}   " + \
           self.finite_grad_differences_test * \
           "finite_grad_difference_val: {3:.4f}"
     print outstr.format(
       macro_cycle, refine_type,self.fmodel.r_work(),
       minimized.finite_grad_difference_val)
     assert (minimized.finite_grad_difference_val < 1.0e-3)
     assert approx_equal(self.fmodel.r_work(), target_and_grads_object.fmodel.r_work())
     # break test if r_work is very small
     if target_and_grads_object.fmodel.r_work() < 1.0e-6: break
   # check results
   if(self.u_iso):
     assert approx_equal(self.fmodel.r_work(), 0, 1.e-5)
   elif(self.sites):
     if(self.use_geometry_restraints):
       assert approx_equal(self.fmodel.r_work(), 0, 0.00015)
     else:
       assert approx_equal(self.fmodel.r_work(), 0, 1.e-5)
   elif self.transformations:
       assert approx_equal(self.fmodel.r_work(), 0, 0.0001)
   else: assert 0
   # output refined model
   xrs_refined = self.fmodel.xray_structure
   m_shaken.assembled_multimer.adopt_xray_structure(self.fmodel.xray_structure)
   output_file_name = "refined_u_iso%s_sites%s.pdb"%(str(self.u_iso),
     str(self.sites))
   m_shaken.write(output_file_name)
   self.test_files_names.append(output_file_name)
   # check final model
   if(not self.use_geometry_restraints):
     # XXX fix later for case self.use_geometry_restraints=True
     pdb_inp_answer = iotbx.pdb.input(source_info=None, lines=ncs_1_copy)
     pdb_inp_refined = iotbx.pdb.input(file_name=output_file_name)
     xrs1 = pdb_inp_answer.xray_structure_simple()
     xrs2 = pdb_inp_refined.xray_structure_simple().select(
       minimized.extended_ncs_selection)
     mmtbx.utils.assert_xray_structures_equal(
       x1 = xrs1,
       x2 = xrs2,
       sites = False)
     delta = flex.vec3_double([xrs1.center_of_mass()]*xrs2.scatterers().size())-\
             flex.vec3_double([xrs2.center_of_mass()]*xrs2.scatterers().size())
     xrs2.set_sites_cart(sites_cart = xrs2.sites_cart()+delta)
     mmtbx.utils.assert_xray_structures_equal(
       x1 = xrs1,
       x2 = xrs2)