示例#1
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)
 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)