def test_compare_rotation_and_translation(self):
    print 'Running ',sys._getframe().f_code.co_name
    pdb_inp1 = iotbx.pdb.input(source_info=None, lines=test_pdb)
    pdb_inp2 = iotbx.pdb.input(source_info=None, lines=test_cif)
    trans_obj1 = ncs.input(
        hierarchy=pdb_inp1.construct_hierarchy(),
        transform_info=pdb_inp1.process_MTRIX_records())
    trans_obj2 = ncs.input(
        hierarchy=pdb_inp2.construct_hierarchy(),
        transform_info=pdb_inp2.process_MTRIX_records())

    # trans_obj1 = ncs.input(pdb_string=test_pdb)
    # trans_obj2 = ncs.input(cif_string=test_cif)
    #
    nrg1 = trans_obj1.get_ncs_restraints_group_list()
    nrg2 = trans_obj2.get_ncs_restraints_group_list()

    x1 = nu.concatenate_rot_tran(ncs_restraints_group_list=nrg1)
    x2 = nu.concatenate_rot_tran(ncs_restraints_group_list=nrg2)

    x = (x1 - x2).as_double()
    self.assertEqual(x.min_max_mean().as_tuple(), (0,0,0))
    #
    pdb_inp = iotbx.pdb.input(source_info=None, lines=test_cif)
    transform_info = pdb_inp.process_MTRIX_records()
    results = transform_info.as_pdb_string()

    pdb_inp = iotbx.pdb.input(source_info=None, lines=test_pdb)
    transform_info = pdb_inp.process_MTRIX_records()
    expected = transform_info.as_pdb_string()

    self.assertEqual(results,expected)
Ejemplo n.º 2
0
 def test_concatenate_rot_tran(self):
   """ Verify correct concatenation of rotation and translations """
   # print sys._getframe().f_code.co_name
   results = nu.concatenate_rot_tran(self.tr_obj1)
   expected = flex.double([
     -0.40177529, 1.20019851, 2.64221706, 0.5, -0.5, 0.0,
     2.24044161,  1.57079633, 0.0,        0.0,  0.0, 0.0])
   assert approx_equal(results,expected,1.0e-4)
Ejemplo n.º 3
0
 def test_concatenate_rot_tran(self):
     """ Verify correct concatenation of rotation and translations """
     # print sys._getframe().f_code.co_name
     results = nu.concatenate_rot_tran(self.tr_obj1)
     expected = flex.double([
         -0.40177529, 1.20019851, 2.64221706, 0.5, -0.5, 0.0, 2.24044161,
         1.57079633, 0.0, 0.0, 0.0, 0.0
     ])
     assert approx_equal(results, expected, 1.0e-4)
Ejemplo n.º 4
0
    def test_update_x(self):
        """    Verify that transforms are getting updated    """
        # print sys._getframe().f_code.co_name
        x1 = nu.concatenate_rot_tran(self.tr_obj1)
        x2 = nu.shake_transformations(x=x1,
                                      shake_angles_sigma=0.035,
                                      shake_translation_sigma=0.5)
        # update with shaken parameters
        self.tr_obj1 = nu.update_rot_tran(x=x2, transforms_obj=self.tr_obj1)
        self.ncs_restraints_group_list = nu.update_rot_tran(
            x=x2, ncs_restraints_group_list=self.ncs_restraints_group_list)

        x3 = nu.concatenate_rot_tran(
            ncs_restraints_group_list=self.ncs_restraints_group_list)
        x4 = nu.concatenate_rot_tran(transforms_obj=self.tr_obj1)
        assert abs(sum(list(x3 - x4))) < 1.0e-3

        the, psi, phi = x2[6:9]
        rot = scitbx.rigid_body.rb_mat_xyz(the=the,
                                           psi=psi,
                                           phi=phi,
                                           deg=False)
        a3 = rot.rot_mat()

        the, psi, phi = x4[6:9]
        rot = scitbx.rigid_body.rb_mat_xyz(the=the,
                                           psi=psi,
                                           phi=phi,
                                           deg=False)
        a4 = rot.rot_mat()
        assert abs(sum(list(a3 - a4))) < 1.0e-3

        # test that update_rot_tran dose not unintentionally change x
        round_val = 3
        r_elems = []
        for rec in self.ncs_restraints_group_list:
            for c in rec.copies:
                r = c.r.round(round_val)
                r_elems.append(r.elems)
Ejemplo n.º 5
0
  def test_update_x(self):
    """    Verify that transforms are getting updated    """
    # print sys._getframe().f_code.co_name
    x1 = nu.concatenate_rot_tran(self.tr_obj1)
    x2 = nu.shake_transformations(
      x = x1,
      shake_angles_sigma=0.035,
      shake_translation_sigma=0.5)
    # update with shaken parameters
    self.tr_obj1 = nu.update_rot_tran(
      x=x2, transforms_obj=self.tr_obj1)
    self.ncs_restraints_group_list = nu.update_rot_tran(
      x=x2, ncs_restraints_group_list=self.ncs_restraints_group_list)

    x3 = nu.concatenate_rot_tran(
      ncs_restraints_group_list=self.ncs_restraints_group_list)
    x4 = nu.concatenate_rot_tran(
      transforms_obj=self.tr_obj1)
    assert abs(sum(list(x3-x4))) < 1.0e-3

    the,psi,phi =x2[6:9]
    rot = scitbx.rigid_body.rb_mat_xyz(
      the=the, psi=psi, phi=phi, deg=False)
    a3 = rot.rot_mat()

    the,psi,phi =x4[6:9]
    rot = scitbx.rigid_body.rb_mat_xyz(
      the=the, psi=psi, phi=phi, deg=False)
    a4 = rot.rot_mat()
    assert abs(sum(list(a3-a4))) < 1.0e-3

    # test that update_rot_tran dose not unintentionally change x
    round_val = 3
    r_elems = []
    for rec in self.ncs_restraints_group_list:
      for c in rec.copies:
        r = c.r.round(round_val)
        r_elems.append(r.elems)
  def test_compare_rotation_and_translation(self):
    print 'Running ',sys._getframe().f_code.co_name
    trans_obj1 = ncs.input(pdb_string=test_pdb)
    trans_obj2 = ncs.input(cif_string=test_cif)
    #
    nrg1 = trans_obj1.get_ncs_restraints_group_list()
    nrg2 = trans_obj2.get_ncs_restraints_group_list()

    x1 = nu.concatenate_rot_tran(ncs_restraints_group_list=nrg1)
    x2 = nu.concatenate_rot_tran(ncs_restraints_group_list=nrg2)

    x = (x1 - x2).as_double()
    self.assertEqual(x.min_max_mean().as_tuple(), (0,0,0))
    #
    pdb_hierarchy_inp = pdb.hierarchy.input(pdb_string=test_cif)
    transform_info = pdb_hierarchy_inp.input.process_mtrix_records()
    results = transform_info.as_pdb_string()

    pdb_hierarchy_inp = pdb.hierarchy.input(pdb_string=test_pdb)
    transform_info = pdb_hierarchy_inp.input.process_mtrix_records()
    expected = transform_info.as_pdb_string()

    self.assertEqual(results,expected)
Ejemplo n.º 7
0
def run(prefix="tst", d_min=1.0):
  """
  NCS constraints: xyz, adp, and operators.
  """
  pdb_file_name_answer = "%s_answer.pdb"%prefix
  of=open(pdb_file_name_answer, "w")
  print >> of, pdb_str_answer
  of.close()
  #
  pdb_file_name_poor = "%s_poor.pdb"%prefix
  of=open(pdb_file_name_poor, "w")
  print >> of, pdb_str_poor
  of.close()
  #
  pdb_inp_answer = iotbx.pdb.input(file_name=pdb_file_name_answer)
  ph_answer = pdb_inp_answer.construct_hierarchy()
  ph_answer.atoms().reset_i_seq()
  xrs_answer = pdb_inp_answer.xray_structure_simple()
  #
  pdb_inp_poor = iotbx.pdb.input(file_name=pdb_file_name_poor)
  ph_poor = pdb_inp_poor.construct_hierarchy()
  ph_poor_obj = iotbx.pdb.input(source_info=None, lines=pdb_str_poor2)
  ph_poor.atoms().reset_i_seq()
  xrs_poor = pdb_inp_poor.xray_structure_simple()
  #
  ppf = mmtbx.utils.process_pdb_file_srv(log=False).process_pdb_files(
    raw_records=pdb_str_poor.splitlines())[0]
  mmtbx.utils.assert_xray_structures_equal(
    x1=ppf.xray_structure(show_summary = False),
    x2=xrs_poor)
  restraints_manager = mmtbx.restraints.manager(
    geometry      = ppf.geometry_restraints_manager(show_energies = False),
    normalization = True)
  #
  fc = xrs_answer.structure_factors(d_min=d_min).f_calc()
  fft_map = fc.fft_map(resolution_factor = 0.25)
  fft_map.apply_sigma_scaling()
  map_data = fft_map.real_map_unpadded()
  #
  ncs_obj_poor = mmtbx.ncs.asu_ncs_converter(pdb_hierarchy = ph_answer,
    add_identity=True)
  ncs_obj_poor.write_pdb_file(file_name="asu.pdb",
    crystal_symmetry=xrs_poor.crystal_symmetry(), mode="asu")
  rm = ncs_obj_poor.back_rotation_matrices
  tv = ncs_obj_poor.back_translation_vectors
  ir = [rm[0]]
  it = [tv[0]]
  # create transformation object
  transforms_obj = ncs.input(
    hierarchy = ph_poor_obj.construct_hierarchy(),
    rotations=rm,
    translations=tv)
  x = nu.concatenate_rot_tran(transforms_obj=transforms_obj)
  x = nu.shake_transformations(
    x = x,
    shake_angles_sigma=1*math.pi/180,
    shake_translation_sigma=0.1)
  transforms_obj = nu.update_rot_tran(x=x, transforms_obj=transforms_obj)
  rm,tv = nu.get_rotation_translation_as_list(transforms_obj=transforms_obj)
  # just to see how result of shaking looks like
  rm_ = ir+rm
  tv_ = it+tv
  ncs_obj_poor.back_rotation_matrices=rm_
  ncs_obj_poor.back_translation_vectors=tv_
  ncs_obj_poor.update_sites_cart(
    sites_cart_master_ncs_copy=ncs_obj_poor.ph_first_chain.atoms().extract_xyz())
  ncs_obj_poor.write_pdb_file(file_name="asu2.pdb",
    crystal_symmetry=xrs_poor.crystal_symmetry(), mode="asu")
  transforms_obj = nu.update_transforms(transforms_obj,rm,tv)
  ncs_restraints_group_list = transforms_obj.get_ncs_restraints_group_list()
  refine_selection = flex.size_t(xrange(transforms_obj.total_asu_length))
  for i in xrange(5):
    data_weight = 1
    tfg_obj = mmtbx.refinement.minimization_ncs_constraints.\
      target_function_and_grads_real_space(
        map_data                   = map_data,
        xray_structure             = xrs_poor,
        ncs_restraints_group_list  = ncs_restraints_group_list,
        refine_selection           = refine_selection,
        real_space_gradients_delta = d_min/4,
        restraints_manager         = restraints_manager,
        data_weight                = data_weight,
        refine_sites               = True)
    minimized = mmtbx.refinement.minimization_ncs_constraints.lbfgs(
      target_and_grads_object      = tfg_obj,
      xray_structure               = xrs_poor,
      ncs_restraints_group_list    = ncs_restraints_group_list,
      refine_selection             = refine_selection,
      finite_grad_differences_test = False,
      max_iterations               = 60,
      refine_sites                 = True)
    xrs_poor = tfg_obj.xray_structure
    ph_poor.adopt_xray_structure(tfg_obj.xray_structure)
    #
    ncs_obj_poor = mmtbx.ncs.asu_ncs_converter(pdb_hierarchy = ph_poor,
      add_identity=False)
    rm,tv = nu.get_rotation_translation_as_list(
      ncs_restraints_group_list= ncs_restraints_group_list)
  #
  ph_poor.write_pdb_file(file_name="refined.pdb")
Ejemplo n.º 8
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 __init__(self,
              n_macro_cycle,
              sites,
              u_iso,
              transformations,
              finite_grad_differences_test,
              use_geometry_restraints,
              shake_site_mean_distance = 1.5,
              d_min = 2,
              shake_angles_sigma = 0.035,
              shake_translation_sigma = 0.5):
   """ create temp test files and data for tests """
   adopt_init_args(self, locals())
   self.test_files_names = [] # collect names of files for cleanup
   # 1 NCS copy: starting template to generate whole asu; place into P1 box
   pdb_inp = iotbx.pdb.input(source_info=None, lines=ncs_1_copy)
   pdb_obj = iotbx.pdb.hierarchy.input(pdb_string=ncs_1_copy)
   mtrix_object = pdb_inp.process_mtrix_records()
   ph = pdb_inp.construct_hierarchy()
   xrs = pdb_inp.xray_structure_simple()
   xrs_one_ncs = xrs.orthorhombic_unit_cell_around_centered_scatterers(
     buffer_size=8)
   ph.adopt_xray_structure(xrs_one_ncs)
   of = open("one_ncs_in_asu.pdb", "w")
   print >> of, mtrix_object.as_pdb_string()
   print >> of, ph.as_pdb_string(crystal_symmetry=xrs_one_ncs.crystal_symmetry())
   of.close()
   # 1 NCS copy -> full asu (expand NCS). This is the answer-structure
   m = multimer(file_name="one_ncs_in_asu.pdb",
                round_coordinates=False,
                reconstruction_type='cau',error_handle=True,eps=1e-2)
   assert m.number_of_transforms == 2, m.number_of_transforms
   xrs_asu = m.assembled_multimer.extract_xray_structure(
     crystal_symmetry = xrs_one_ncs.crystal_symmetry())
   m.write("full_asu.pdb")
   # force ASU none-rounded coordinates into xray structure
   xrs_asu.set_sites_cart(m.sites_cart())
   assert xrs_asu.crystal_symmetry().is_similar_symmetry(
     xrs_one_ncs.crystal_symmetry())
   # Generate Fobs from answer structure
   f_obs = abs(xrs_asu.structure_factors(d_min=d_min, algorithm="direct").f_calc())
   r_free_flags = f_obs.generate_r_free_flags()
   mtz_dataset = f_obs.as_mtz_dataset(column_root_label="F-obs")
   mtz_dataset.add_miller_array(
     miller_array=r_free_flags,
     column_root_label="R-free-flags")
   mtz_object = mtz_dataset.mtz_object()
   mtz_object.write(file_name = "data.mtz")
   # Shake structure - subject to refinement input
   xrs_shaken = xrs_one_ncs.deep_copy_scatterers()
   if sites: xrs_shaken.shake_sites_in_place(
     mean_distance=shake_site_mean_distance)
   if self.u_iso:
     u_random = flex.random_double(xrs_shaken.scatterers().size())
     xrs_shaken = xrs_shaken.set_u_iso(values=u_random)
   if self.transformations:
     transforms_obj = ncs.input(
     transform_info = mtrix_object,
     pdb_hierarchy_inp = pdb_obj)
     x = nu.concatenate_rot_tran(transforms_obj=transforms_obj)
     x = nu.shake_transformations(
       x = x,
       shake_angles_sigma=self.shake_angles_sigma,
       shake_translation_sigma=self.shake_translation_sigma)
     transforms_obj = nu.update_rot_tran(x=x,transforms_obj=transforms_obj)
     mtrix_object = transforms_obj.build_MTRIX_object()
   ph.adopt_xray_structure(xrs_shaken)
   of = open("one_ncs_in_asu_shaken.pdb", "w")
   print >> of, mtrix_object.as_pdb_string()
   print >> of, ph.as_pdb_string(crystal_symmetry=xrs.crystal_symmetry())
   of.close()
   self.f_obs = f_obs
   self.r_free_flags = r_free_flags
   self.xrs_one_ncs = xrs_one_ncs
   # Get restraints manager
   self.grm = None
   pdb_str = m.assembled_multimer.as_pdb_string(
     crystal_symmetry=xrs_one_ncs.crystal_symmetry())
   self.iso_restraints = None
   if(self.use_geometry_restraints):
     self.grm = nu.get_restraints_manager(pdb_string=pdb_str)
     if(self.u_iso):
       temp = mmtbx.refinement.adp_refinement.adp_restraints_master_params
       self.iso_restraints = temp.extract().iso
def run(prefix="tst", d_min=1.0):
  """
  NCS constraints: xyz, adp, and operators.
  """
  pdb_file_name_answer = "%s_answer.pdb"%prefix
  of=open(pdb_file_name_answer, "w")
  print >> of, pdb_str_answer
  of.close()
  #
  pdb_file_name_poor = "%s_poor.pdb"%prefix
  of=open(pdb_file_name_poor, "w")
  print >> of, pdb_str_poor
  of.close()
  #
  pdb_inp_answer = iotbx.pdb.input(file_name=pdb_file_name_answer)
  ph_answer = pdb_inp_answer.construct_hierarchy()
  ph_answer.atoms().reset_i_seq()
  xrs_answer = pdb_inp_answer.xray_structure_simple()
  #
  pdb_inp_poor = iotbx.pdb.input(file_name=pdb_file_name_poor)
  ph_poor = pdb_inp_poor.construct_hierarchy()
  ph_poor_obj = iotbx.pdb.hierarchy.input(pdb_string=pdb_str_poor2)
  ph_poor.atoms().reset_i_seq()
  xrs_poor = pdb_inp_poor.xray_structure_simple()
  #
  ppf = mmtbx.utils.process_pdb_file_srv(log=False).process_pdb_files(
    raw_records=pdb_str_poor.splitlines())[0]
  mmtbx.utils.assert_xray_structures_equal(
    x1=ppf.xray_structure(show_summary = False),
    x2=xrs_poor)
  restraints_manager = mmtbx.restraints.manager(
    geometry      = ppf.geometry_restraints_manager(show_energies = False),
    normalization = True)
  #
  fc = xrs_answer.structure_factors(d_min=d_min).f_calc()
  fft_map = fc.fft_map(resolution_factor = 0.25)
  fft_map.apply_sigma_scaling()
  map_data = fft_map.real_map_unpadded()
  #
  ncs_obj_poor = mmtbx.ncs.asu_ncs_converter(pdb_hierarchy = ph_answer,
    add_identity=True)
  ncs_obj_poor.write_pdb_file(file_name="asu.pdb",
    crystal_symmetry=xrs_poor.crystal_symmetry(), mode="asu")
  rm = ncs_obj_poor.back_rotation_matrices
  tv = ncs_obj_poor.back_translation_vectors
  ir = [rm[0]]
  it = [tv[0]]
  # create transformation object
  transforms_obj = ncs.input(
    pdb_hierarchy_inp = ph_poor_obj,
    rotations=rm,
    translations=tv)
  x = nu.concatenate_rot_tran(transforms_obj=transforms_obj)
  x = nu.shake_transformations(
    x = x,
    shake_angles_sigma=1*math.pi/180,
    shake_translation_sigma=0.1)
  transforms_obj = nu.update_rot_tran(x=x, transforms_obj=transforms_obj)
  rm,tv = nu.get_rotation_translation_as_list(transforms_obj=transforms_obj)
  # just to see how result of shaking looks like
  rm_ = ir+rm
  tv_ = it+tv
  ncs_obj_poor.back_rotation_matrices=rm_
  ncs_obj_poor.back_translation_vectors=tv_
  ncs_obj_poor.update_sites_cart(
    sites_cart_master_ncs_copy=ncs_obj_poor.ph_first_chain.atoms().extract_xyz())
  ncs_obj_poor.write_pdb_file(file_name="asu2.pdb",
    crystal_symmetry=xrs_poor.crystal_symmetry(), mode="asu")
  transforms_obj = nu.update_transforms(transforms_obj,rm,tv)
  ncs_restraints_group_list = transforms_obj.get_ncs_restraints_group_list()
  refine_selection = flex.size_t(xrange(transforms_obj.total_asu_length))
  for i in xrange(5):
    data_weight = 1
    for action in [[False, True], [True, False]]:
      refine_sites, refine_transformations = action
      tfg_obj = mmtbx.refinement.minimization_ncs_constraints.\
        target_function_and_grads_real_space(
          map_data                   = map_data,
          xray_structure             = xrs_poor,
          ncs_restraints_group_list  = ncs_restraints_group_list,
          refine_selection           = refine_selection,
          real_space_gradients_delta = d_min/4,
          restraints_manager         = restraints_manager,
          data_weight                = data_weight,
          refine_sites               = refine_sites,
          refine_transformations     = refine_transformations)
      minimized = mmtbx.refinement.minimization_ncs_constraints.lbfgs(
        target_and_grads_object      = tfg_obj,
        xray_structure               = xrs_poor,
        ncs_restraints_group_list    = ncs_restraints_group_list,
        refine_selection             = refine_selection,
        finite_grad_differences_test = False,
        max_iterations               = 60,
        refine_sites                 = refine_sites,
        refine_transformations       = refine_transformations)
      xrs_poor = tfg_obj.xray_structure
      ph_poor.adopt_xray_structure(tfg_obj.xray_structure)
      #
      ncs_obj_poor = mmtbx.ncs.asu_ncs_converter(pdb_hierarchy = ph_poor,
        add_identity=False)
      rm,tv = nu.get_rotation_translation_as_list(
        ncs_restraints_group_list= ncs_restraints_group_list)
  #
  ph_poor.write_pdb_file(file_name="refined.pdb")