示例#1
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)
示例#2
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)
示例#3
0
 def test_update_rot_tran(self):
   """
   Verify correct conversion from angles and translation
   to rotation matrices and translations """
   # print sys._getframe().f_code.co_name
   x = 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])
   self.tr_obj1 = nu.update_rot_tran(
     x=x,transforms_obj=self.tr_obj1)
   rot_results, tran_results = nu.get_rotation_translation_as_list(
     transforms_obj=self.tr_obj1)
   rot_expected = [self.rotation1, self.rotation2]
   tran_expected = [self.translation1,self.translation2]
   assert approx_equal(tran_results,tran_expected,1.0e-4)
   assert approx_equal(rot_results,rot_expected,1.0e-4)
示例#4
0
 def test_update_rot_tran(self):
     """
 Verify correct conversion from angles and translation
 to rotation matrices and translations """
     # print sys._getframe().f_code.co_name
     x = 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
     ])
     self.tr_obj1 = nu.update_rot_tran(x=x, transforms_obj=self.tr_obj1)
     rot_results, tran_results = nu.get_rotation_translation_as_list(
         transforms_obj=self.tr_obj1)
     rot_expected = [self.rotation1, self.rotation2]
     tran_expected = [self.translation1, self.translation2]
     assert approx_equal(tran_results, tran_expected, 1.0e-4)
     assert approx_equal(rot_results, rot_expected, 1.0e-4)
示例#5
0
 def update_xray_structure(self, x=None):
     """
 Update xray_structure with refined parameters, then update
 fmodel object with updated xray_structure.
 """
     if not x: x = self.x
     if self.refine_transformations:
         # update the ncs_restraint_groups transforms
         self.ncs_restraints_group_list = nu.update_rot_tran(
             x=x, ncs_restraints_group_list=self.ncs_restraints_group_list)
         # Use the new transformations to create the ASU
         x_ncs = nu.get_ncs_sites_cart(self).as_double()
         x_asu = self.refinable_params_one_ncs_to_asu(x_ncs)
         self.xray_structure.set_sites_cart(
             sites_cart=flex.vec3_double(x_asu))
     elif self.refine_sites:
         x_asu = self.refinable_params_one_ncs_to_asu(x)
         self.xray_structure.set_sites_cart(
             sites_cart=flex.vec3_double(x_asu))
     elif self.refine_u_iso:
         x_asu = self.refinable_params_one_ncs_to_asu()
         self.xray_structure.set_u_iso(values=x_asu)
     return self.xray_structure
 def update_xray_structure(self, x=None):
   """
   Update xray_structure with refined parameters, then update
   fmodel object with updated xray_structure.
   """
   if not x: x = self.x
   if self.refine_transformations:
     # update the ncs_restraint_groups transforms
     self.ncs_restraints_group_list = nu.update_rot_tran(
       x=x, ncs_restraints_group_list=self.ncs_restraints_group_list)
     # Use the new transformations to create the ASU
     x_ncs = nu.get_ncs_sites_cart(self).as_double()
     x_asu = self.refinable_params_one_ncs_to_asu(x_ncs)
     self.xray_structure.set_sites_cart(
       sites_cart = flex.vec3_double(x_asu))
   elif self.refine_sites:
     x_asu = self.refinable_params_one_ncs_to_asu(x)
     self.xray_structure.set_sites_cart(
       sites_cart = flex.vec3_double(x_asu))
   elif self.refine_u_iso:
     x_asu = self.refinable_params_one_ncs_to_asu()
     self.xray_structure.set_u_iso(values = x_asu)
   return self.xray_structure
示例#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")
 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")