Exemple #1
0
 def __init__(self,
              u_cart,
              u_iso=None,
              use_u_aniso=None,
              sites_cart=None,
              adp_similarity_proxies=None,
              rigid_bond_proxies=None,
              isotropic_adp_proxies=None,
              compute_gradients=True,
              gradients_aniso_cart=None,
              gradients_iso=None,
              disable_asu_cache=False,
              normalization=False):
     adopt_init_args(self, locals())
     self.number_of_restraints = 0
     self.residual_sum = 0
     self.normalization_factor = None
     if (adp_similarity_proxies is not None):
         assert u_iso is not None and use_u_aniso is not None
     if (rigid_bond_proxies is not None): assert sites_cart is not None
     if (sites_cart is not None): assert sites_cart.size() == u_cart.size()
     if (u_iso is not None): assert u_iso.size() == u_cart.size()
     if (use_u_aniso is not None):
         assert use_u_aniso.size() == u_cart.size()
     if (compute_gradients):
         if (self.gradients_aniso_cart is None):
             self.gradients_aniso_cart = flex.sym_mat3_double(
                 sites_cart.size(), [0, 0, 0, 0, 0, 0])
         else:
             assert self.gradients_aniso_cart.size() == sites_cart.size()
         if (u_iso is not None and self.gradients_iso is None):
             self.gradients_iso = flex.double(sites_cart.size(), 0)
         elif (u_iso is not None):
             assert self.gradients_iso.size() == sites_cart.size()
     if (adp_similarity_proxies is None):
         self.n_adp_similarity_proxies = None
         self.adp_similarity_residual_sum = 0
     else:
         self.n_adp_similarity_proxies = len(adp_similarity_proxies)
         self.adp_similarity_residual_sum = adp_restraints.adp_similarity_residual_sum(
             u_cart=u_cart,
             u_iso=u_iso,
             use_u_aniso=use_u_aniso,
             proxies=adp_similarity_proxies,
             gradients_aniso_cart=self.gradients_aniso_cart,
             gradients_iso=self.gradients_iso)
         self.number_of_restraints += 6 * self.n_adp_similarity_proxies
         self.residual_sum += self.adp_similarity_residual_sum
     if (rigid_bond_proxies is None):
         self.n_rigid_bond_proxies = None
         self.rigid_bond_residual_sum = 0
     else:
         self.n_rigid_bond_proxies = len(rigid_bond_proxies)
         self.rigid_bond_residual_sum = adp_restraints.rigid_bond_residual_sum(
             sites_cart=sites_cart,
             u_cart=u_cart,
             proxies=rigid_bond_proxies,
             gradients_aniso_cart=self.gradients_aniso_cart)
         self.number_of_restraints += self.n_rigid_bond_proxies
         self.residual_sum += self.rigid_bond_residual_sum
     if (isotropic_adp_proxies is None):
         self.n_isotropic_adp_proxies = None
         self.isotropic_adp_residual_sum = 0
     else:
         self.n_isotropic_adp_proxies = len(isotropic_adp_proxies)
         self.isotropic_adp_residual_sum = adp_restraints.isotropic_adp_residual_sum(
             u_cart=u_cart,
             proxies=isotropic_adp_proxies,
             gradients_aniso_cart=self.gradients_aniso_cart)
         self.number_of_restraints += self.n_isotropic_adp_proxies
         self.residual_sum += self.isotropic_adp_residual_sum
     self.finalize_target_and_gradients()
Exemple #2
0
def exercise_rigid_bond():
  i_seqs = (1,2)
  weight = 1
  p = adp_restraints.rigid_bond_proxy(i_seqs=i_seqs,weight=weight)
  assert p.i_seqs == i_seqs
  assert p.weight == weight
  sites = ((1,2,3),(2,3,4))
  u_cart = ((1,2,3,4,5,6), (3,4,5,6,7,8))
  expected_gradients = ((-4, -4, -4, -8, -8, -8), (4, 4, 4, 8, 8, 8))
  r = adp_restraints.rigid_bond(sites=sites, u_cart=u_cart, weight=weight)
  assert r.weight == weight
  assert approx_equal(r.delta_z(), -6)
  assert approx_equal(r.residual(), 36)
  assert approx_equal(r.gradients(), expected_gradients)
  sites_cart = flex.vec3_double(((1,2,3),(2,5,4),(3,4,5)))
  u_cart = flex.sym_mat3_double(((1,2,3,4,5,6),
                                 (2,3,3,5,7,7),
                                 (3,4,5,3,7,8)))
  r = adp_restraints.rigid_bond(
    adp_restraint_params(sites_cart=sites_cart, u_cart=u_cart),
    proxy=p)
  assert approx_equal(r.weight, weight)
  unit_cell = uctbx.unit_cell([15,25,30,90,90,90])
  sites_frac = unit_cell.fractionalize(sites_cart=sites_cart)
  u_star = flex.sym_mat3_double([
    adptbx.u_cart_as_u_star(unit_cell, u_cart_i)
    for u_cart_i in u_cart])
  pair = adp_restraints.rigid_bond_pair(sites_frac[1],
                                     sites_frac[2],
                                     u_star[1],
                                     u_star[2],
                                     unit_cell)
  assert approx_equal(pair.delta_z(), abs(r.delta_z()))
  assert approx_equal(pair.z_12(), r.z_12())
  assert approx_equal(pair.z_21(), r.z_21())
  #
  gradients_aniso_cart = flex.sym_mat3_double(sites_cart.size(), (0,0,0,0,0,0))
  gradients_iso = flex.double(sites_cart.size(), 0)
  proxies = adp_restraints.shared_rigid_bond_proxy([p,p])
  params = adp_restraint_params(sites_cart=sites_cart, u_cart=u_cart)
  residuals = adp_restraints.rigid_bond_residuals(params, proxies=proxies)
  assert approx_equal(residuals, (r.residual(),r.residual()))
  deltas = adp_restraints.rigid_bond_deltas(params, proxies=proxies)
  assert approx_equal(deltas, (r.delta_z(),r.delta_z()))
  residual_sum = adp_restraints.rigid_bond_residual_sum(
    params=params,
    proxies=proxies,
    gradients_aniso_cart=gradients_aniso_cart)
  assert approx_equal(residual_sum, 2 * r.residual())
  for g,e in zip(gradients_aniso_cart[1:3], r.gradients()):
    assert approx_equal(g, matrix.col(e)*2)
  fd_grads_aniso, fd_grads_iso = finite_difference_gradients(
    restraint_type=adp_restraints.rigid_bond,
    proxy=p,
    sites_cart=sites_cart,
    u_cart=u_cart)
  for g,e in zip(gradients_aniso_cart, fd_grads_aniso):
    assert approx_equal(g, matrix.col(e)*2)
  #
  # check frame invariance of residual
  #
  u_cart_1 = matrix.sym(sym_mat3=(0.1,0.2,0.05,0.03,0.02,0.01))
  u_cart_2 = matrix.sym(sym_mat3=(0.21,0.32,0.11,0.02,0.02,0.07))
  u_cart = (u_cart_1.as_sym_mat3(),u_cart_2.as_sym_mat3())
  site_cart_1 = matrix.col((1,2,3))
  site_cart_2 = matrix.col((3,1,4.2))
  sites = (tuple(site_cart_1),tuple(site_cart_2))
  a = adp_restraints.rigid_bond(sites=sites, u_cart=u_cart, weight=1)
  expected_residual = a.residual()
  gen = flex.mersenne_twister()
  for i in range(20):
    R = matrix.rec(gen.random_double_r3_rotation_matrix(),(3,3))
    u_cart_1_rot = R * u_cart_1 * R.transpose()
    u_cart_2_rot = R * u_cart_2 * R.transpose()
    u_cart = (u_cart_1_rot.as_sym_mat3(),u_cart_2_rot.as_sym_mat3())
    site_cart_1_rot = R * site_cart_1
    site_cart_2_rot = R * site_cart_2
    sites = (tuple(site_cart_1_rot),tuple(site_cart_2_rot))
    a = adp_restraints.rigid_bond(
      sites=sites, u_cart=u_cart,
      weight=1)
    assert approx_equal(a.residual(), expected_residual)
Exemple #3
0
def exercise_rigid_bond():
  i_seqs = (1,2)
  weight = 1
  p = adp_restraints.rigid_bond_proxy(i_seqs=i_seqs,weight=weight)
  assert p.i_seqs == i_seqs
  assert p.weight == weight
  sites = ((1,2,3),(2,3,4))
  u_cart = ((1,2,3,4,5,6), (3,4,5,6,7,8))
  expected_gradients = ((-4, -4, -4, -8, -8, -8), (4, 4, 4, 8, 8, 8))
  r = adp_restraints.rigid_bond(sites=sites, u_cart=u_cart, weight=weight)
  assert r.weight == weight
  assert approx_equal(r.delta_z(), -6)
  assert approx_equal(r.residual(), 36)
  assert approx_equal(r.gradients(), expected_gradients)
  sites_cart = flex.vec3_double(((1,2,3),(2,5,4),(3,4,5)))
  u_cart = flex.sym_mat3_double(((1,2,3,4,5,6),
                                 (2,3,3,5,7,7),
                                 (3,4,5,3,7,8)))
  r = adp_restraints.rigid_bond(
    adp_restraint_params(sites_cart=sites_cart, u_cart=u_cart),
    proxy=p)
  assert approx_equal(r.weight, weight)
  unit_cell = uctbx.unit_cell([15,25,30,90,90,90])
  sites_frac = unit_cell.fractionalize(sites_cart=sites_cart)
  u_star = flex.sym_mat3_double([
    adptbx.u_cart_as_u_star(unit_cell, u_cart_i)
    for u_cart_i in u_cart])
  pair = adp_restraints.rigid_bond_pair(sites_frac[1],
                                     sites_frac[2],
                                     u_star[1],
                                     u_star[2],
                                     unit_cell)
  assert approx_equal(pair.delta_z(), abs(r.delta_z()))
  assert approx_equal(pair.z_12(), r.z_12())
  assert approx_equal(pair.z_21(), r.z_21())
  #
  gradients_aniso_cart = flex.sym_mat3_double(sites_cart.size(), (0,0,0,0,0,0))
  gradients_iso = flex.double(sites_cart.size(), 0)
  proxies = adp_restraints.shared_rigid_bond_proxy([p,p])
  params = adp_restraint_params(sites_cart=sites_cart, u_cart=u_cart)
  residuals = adp_restraints.rigid_bond_residuals(params, proxies=proxies)
  assert approx_equal(residuals, (r.residual(),r.residual()))
  deltas = adp_restraints.rigid_bond_deltas(params, proxies=proxies)
  assert approx_equal(deltas, (r.delta_z(),r.delta_z()))
  residual_sum = adp_restraints.rigid_bond_residual_sum(
    params=params,
    proxies=proxies,
    gradients_aniso_cart=gradients_aniso_cart)
  assert approx_equal(residual_sum, 2 * r.residual())
  for g,e in zip(gradients_aniso_cart[1:3], r.gradients()):
    assert approx_equal(g, matrix.col(e)*2)
  fd_grads_aniso, fd_grads_iso = finite_difference_gradients(
    restraint_type=adp_restraints.rigid_bond,
    proxy=p,
    sites_cart=sites_cart,
    u_cart=u_cart)
  for g,e in zip(gradients_aniso_cart, fd_grads_aniso):
    assert approx_equal(g, matrix.col(e)*2)
  #
  # check frame invariance of residual
  #
  u_cart_1 = matrix.sym(sym_mat3=(0.1,0.2,0.05,0.03,0.02,0.01))
  u_cart_2 = matrix.sym(sym_mat3=(0.21,0.32,0.11,0.02,0.02,0.07))
  u_cart = (u_cart_1.as_sym_mat3(),u_cart_2.as_sym_mat3())
  site_cart_1 = matrix.col((1,2,3))
  site_cart_2 = matrix.col((3,1,4.2))
  sites = (tuple(site_cart_1),tuple(site_cart_2))
  a = adp_restraints.rigid_bond(sites=sites, u_cart=u_cart, weight=1)
  expected_residual = a.residual()
  gen = flex.mersenne_twister()
  for i in range(20):
    R = matrix.rec(gen.random_double_r3_rotation_matrix(),(3,3))
    u_cart_1_rot = R * u_cart_1 * R.transpose()
    u_cart_2_rot = R * u_cart_2 * R.transpose()
    u_cart = (u_cart_1_rot.as_sym_mat3(),u_cart_2_rot.as_sym_mat3())
    site_cart_1_rot = R * site_cart_1
    site_cart_2_rot = R * site_cart_2
    sites = (tuple(site_cart_1_rot),tuple(site_cart_2_rot))
    a = adp_restraints.rigid_bond(
      sites=sites, u_cart=u_cart,
      weight=1)
    assert approx_equal(a.residual(), expected_residual)
Exemple #4
0
 def __init__(self, u_cart,
                    u_iso=None,
                    use_u_aniso=None,
                    sites_cart=None,
                    adp_similarity_proxies=None,
                    rigid_bond_proxies=None,
                    isotropic_adp_proxies=None,
                    compute_gradients=True,
                    gradients_aniso_cart=None,
                    gradients_iso=None,
                    disable_asu_cache=False,
                    normalization=False):
   adopt_init_args(self, locals())
   self.number_of_restraints = 0
   self.residual_sum = 0
   self.normalization_factor = None
   if (adp_similarity_proxies is not None):
     assert u_iso is not None and use_u_aniso is not None
   if (rigid_bond_proxies is not None): assert sites_cart is not None
   if (sites_cart is not None): assert sites_cart.size() == u_cart.size()
   if (u_iso is not None): assert u_iso.size() == u_cart.size()
   if (use_u_aniso is not None): assert use_u_aniso.size() == u_cart.size()
   if (compute_gradients):
     if (self.gradients_aniso_cart is None):
       self.gradients_aniso_cart = flex.sym_mat3_double(
         sites_cart.size(), [0,0,0,0,0,0])
     else:
       assert self.gradients_aniso_cart.size() == sites_cart.size()
     if (u_iso is not None and self.gradients_iso is None):
       self.gradients_iso = flex.double(sites_cart.size(), 0)
     elif (u_iso is not None):
       assert self.gradients_iso.size() == sites_cart.size()
   if (adp_similarity_proxies is None):
     self.n_adp_similarity_proxies = None
     self.adp_similarity_residual_sum = 0
   else:
     self.n_adp_similarity_proxies = len(adp_similarity_proxies)
     self.adp_similarity_residual_sum = adp_restraints.adp_similarity_residual_sum(
       u_cart=u_cart,
       u_iso=u_iso,
       use_u_aniso=use_u_aniso,
       proxies=adp_similarity_proxies,
       gradients_aniso_cart=self.gradients_aniso_cart,
       gradients_iso=self.gradients_iso)
     self.number_of_restraints += 6 * self.n_adp_similarity_proxies
     self.residual_sum += self.adp_similarity_residual_sum
   if (rigid_bond_proxies is None):
     self.n_rigid_bond_proxies = None
     self.rigid_bond_residual_sum = 0
   else:
     self.n_rigid_bond_proxies = len(rigid_bond_proxies)
     self.rigid_bond_residual_sum = adp_restraints.rigid_bond_residual_sum(
       sites_cart=sites_cart,
       u_cart=u_cart,
       proxies=rigid_bond_proxies,
       gradients_aniso_cart=self.gradients_aniso_cart)
     self.number_of_restraints += self.n_rigid_bond_proxies
     self.residual_sum += self.rigid_bond_residual_sum
   if (isotropic_adp_proxies is None):
     self.n_isotropic_adp_proxies = None
     self.isotropic_adp_residual_sum = 0
   else:
     self.n_isotropic_adp_proxies = len(isotropic_adp_proxies)
     self.isotropic_adp_residual_sum = adp_restraints.isotropic_adp_residual_sum(
       u_cart=u_cart,
       proxies=isotropic_adp_proxies,
       gradients_aniso_cart=self.gradients_aniso_cart)
     self.number_of_restraints += self.n_isotropic_adp_proxies
     self.residual_sum += self.isotropic_adp_residual_sum
   self.finalize_target_and_gradients()