Example #1
0
def exercise_approx_equal():
  assert approx_equal(1., 1. + 1e-11)
  assert approx_equal(1+1j, 0.997+1.004j, eps=1e-2)
  assert approx_equal(1, 0.997+0.004j, eps=1e-2)
  assert approx_equal(1+0.003j, 0.997, eps=1e-2)
  assert approx_equal([ 2.5, 3.4+5.8j, 7.89],
                      [ 2.4+0.1j, 3.5+5.9j, 7.90], eps=0.2)
Example #2
0
def exercise_3():
  missing = check_external_dependencies(['scipy', 'sklearn', 'networkx'])
  if len(missing):
    print ("Skipping exercise_3: missing dependencies" +
           " %s" * len(missing)) %(tuple(missing))
    return
  # thaumatin
  data_dir = os.path.join(dials_regression, "indexing_test_data", "i04_weak_data")
  pickle_path = os.path.join(data_dir, "full.pickle")
  sweep_path = os.path.join(data_dir, "datablock_orig.json")
  extra_args = ["cluster_analysis_search=True",
                "n_macro_cycles=3",
                "bin_size_fraction=0.25",
                "reciprocal_space_grid.d_min=4"]
  expected_unit_cell = uctbx.unit_cell(
    (58, 58, 150, 90, 90, 90))
  expected_rmsds = (0.05, 0.041, 0.0004)

  # now enforce symmetry
  extra_args.append("known_symmetry.space_group=P4")
  expected_hall_symbol = ' P 4'

  result = run_one_indexing(pickle_path, sweep_path, extra_args, expected_unit_cell,
                            expected_rmsds, expected_hall_symbol)

  a, b, c = result.crystal_model.get_real_space_vectors()
  assert approx_equal(a.length(), b.length())
  assert c.length() > b.length()
  assert approx_equal(a.angle(b, deg=True), 90)
  assert approx_equal(b.angle(c, deg=True), 90)
  assert approx_equal(c.angle(a, deg=True), 90)
Example #3
0
def exercise_00():
  file = libtbx.env.find_in_repositories(relative_path=
    "chem_data/polygon_data/all_mvd.pickle", test=os.path.isfile)
  database_dict = easy_pickle.load(file)
  #
  i_1l2h = list(database_dict["pdb_code"]).index("1l2h")
  found = 0
  for key in database_dict.keys():
    #print key, ": ",database_dict[key][i_1l2h]
    #
    value = database_dict[key][i_1l2h]
    if(key == "unit_cell"):
      assert "(53.89, 53.89, 77.36, 90, 90, 90)" == value.strip()
      found += 1
    if(key == "high_resolution"):
      assert approx_equal(1.54, float(value))
      found += 1
    if(key == "pdb_header_tls"):
      assert "false (n_groups: 0)" == value.strip()
      found += 1
    if(key == "test_set_size"):
      assert approx_equal(0.0476, float(value),0002)
      found += 1
    if(key == "twinned"):
      assert "h,-k,-l" == value.strip()
      found += 1
  #
  assert found == 5
def test_fit():
  x1_obs=[]
  x2_obs=[]
  x1m=1.0
  x2m=3.0
  a=1
  b=2
  c=-0.5
  y_obs=[]

  for ii in range(10):
    for jj in range(10):
      x1_obs.append(ii-5)
      x2_obs.append(jj-5)
      y_obs.append(
        a*(( (ii-5.0)-x1m )**2.0) +
        b*(( (jj-5.0)-x2m )**2.0) +
        2.0*c*( (ii-5.0)-x1m )*( (jj-5.0)-x2m )
        )

  fit =fqf.fit_quadratic_function_2d_data( x1_obs, x2_obs, y_obs  )
  assert approx_equal( fit.x1m, x1m, eps=1e-5)
  assert approx_equal( fit.x2m, x2m, eps=1e-5)
  assert approx_equal( fit.a, a, eps=1e-5)
  assert approx_equal( fit.b, b, eps=1e-5)
  assert approx_equal( fit.c, c, eps=1e-5)
  print 'OK'
Example #5
0
def assert_equal_data_and_sigmas(array_1, array_2):
  a, b = array_1.map_to_asu().common_sets(array_2.map_to_asu())
  assert a.indices().all_eq(b.indices())
  from libtbx.test_utils import approx_equal
  assert approx_equal(a.data(), b.data())
  if (not array_1.is_xray_reconstructed_amplitude_array()):
    assert approx_equal(a.sigmas(), b.sigmas())
Example #6
0
def check_adp_set_b_iso(
      cmd, xrsp_init, output, selection, selection_str, verbose,
      tolerance=1.e-3):
  remove_files(output)
  run_command(command=cmd, verbose=verbose)
  xrsp = xray_structure_plus(file_name = output)
  assert approx_equal(xrsp.occ,        xrsp_init.occ,tolerance)
  assert approx_equal(xrsp.sites_cart, xrsp_init.sites_cart,tolerance)
  assert approx_equal(xrsp.use_u_iso,  xrsp_init.use_u_iso,tolerance)
  assert approx_equal(xrsp.use_u_aniso,xrsp_init.use_u_aniso,tolerance)
  assert approx_equal(xrsp.u_iso_not_used,  xrsp_init.u_iso_not_used,tolerance)
  assert approx_equal(xrsp.u_cart_not_used,xrsp_init.u_cart_not_used,tolerance)
  if(selection_str is None):
    assert not_approx_equal(xrsp.u_iso_used,  xrsp_init.u_iso_used,tolerance)
    for ucart in xrsp.u_cart:
      b_iso = adptbx.u_as_b(adptbx.u_cart_as_u_iso(ucart))
      if b_iso > 0: assert approx_equal(b_iso, 10, 0.005)
      else: assert approx_equal(b_iso, -78.956, 0.005)
  else:
    arg1 = xrsp.u_iso_used.select(selection.select(xrsp.use_u_iso))
    arg2 = xrsp_init.u_iso_used.select(selection.select(xrsp_init.use_u_iso))
    if(arg1.size() > 0): assert not_approx_equal(arg1, arg2,tolerance)
    for ucart in xrsp.u_cart:
      b_iso = adptbx.u_as_b(adptbx.u_cart_as_u_iso(ucart))
      if b_iso > 0: assert approx_equal(b_iso, 10, 0.005)
      else: assert approx_equal(b_iso, -78.956, 0.005)
Example #7
0
  def tst_import_beam_centre(self):
    from glob import glob
    import os
    from libtbx import easy_run
    from dxtbx.serialize import load

    # Find the image files
    image_files = glob(os.path.join(self.path, "centroid*.cbf"))
    image_files = ' '.join(image_files)

    # provide mosflm beam centre to dials.import
    cmd = 'dials.import %s mosflm_beam_centre=100,200 output.datablock=mosflm_beam_centre.json' %image_files
    easy_run.fully_buffered(cmd)
    assert os.path.exists("mosflm_beam_centre.json")
    datablock = load.datablock("mosflm_beam_centre.json")[0]
    imgset = datablock.extract_imagesets()[0]
    beam_centre = imgset.get_detector()[0].get_beam_centre(imgset.get_beam().get_s0())
    assert approx_equal(beam_centre, (200,100))

    # provide an alternative datablock.json to get geometry from
    cmd = 'dials.import %s reference_geometry=mosflm_beam_centre.json output.datablock=mosflm_beam_centre2.json' %image_files
    easy_run.fully_buffered(cmd)
    assert os.path.exists("mosflm_beam_centre2.json")
    datablock = load.datablock("mosflm_beam_centre2.json")[0]
    imgset = datablock.extract_imagesets()[0]
    beam_centre = imgset.get_detector()[0].get_beam_centre(imgset.get_beam().get_s0())
    assert approx_equal(beam_centre, (200,100))

    print 'OK'
def exercise_match_bijvoet_mates():
  h0 = flex.miller_index(((1,2,3), (-1,-2,-3), (2,3,4), (-2,-3,-4), (3,4,5)))
  d0 = flex.double((1,2,3,4,5))
  bm = miller.match_bijvoet_mates(
    sgtbx.space_group_type(),
    h0)
  bm = miller.match_bijvoet_mates(
    sgtbx.reciprocal_space_asu(sgtbx.space_group_type()),
    h0)
  bm = miller.match_bijvoet_mates(
    h0)
  assert tuple(bm.pairs()) == ((0,1), (2,3))
  assert tuple(bm.singles("+")) == (4,)
  assert tuple(bm.singles("-")) == ()
  assert bm.n_singles() != 0
  assert tuple(bm.pairs_hemisphere_selection("+")) == (0, 2)
  assert tuple(bm.pairs_hemisphere_selection("-")) == (1, 3)
  assert tuple(bm.singles_hemisphere_selection("+")) == (4,)
  assert tuple(bm.singles_hemisphere_selection("-")) == ()
  assert tuple(bm.miller_indices_in_hemisphere("+")) == ((1,2,3), (2,3,4))
  assert tuple(bm.miller_indices_in_hemisphere("-")) == ((-1,-2,-3),(-2,-3,-4))
  assert approx_equal(tuple(bm.minus(d0)), (-1, -1))
  assert approx_equal(tuple(bm.additive_sigmas(d0)),
                      [math.sqrt(x*x+y*y) for x,y in ((1,2), (3,4))])
  assert approx_equal(tuple(bm.average(d0)), (3/2., 7/2.))
  h0.append((1,2,3))
  try: miller.match_bijvoet_mates(h0)
  except Exception: pass
  else: raise Exception_expected
Example #9
0
 def check_f_calc_derivs():
     eps = 1e-6
     g_fin = flex.complex_double()
     c_fin = flex.vec3_double()
     for ih in xrange(f_calc.size()):
         c_orig = f_calc[ih]
         g_fin_ab = []
         c_fin_ab = []
         for iab in [0, 1]:
             fs = []
             gs = []
             for signed_eps in [eps, -eps]:
                 if iab == 0:
                     f_calc[ih] = complex(c_orig.real + signed_eps, c_orig.imag)
                 else:
                     f_calc[ih] = complex(c_orig.real, c_orig.imag + signed_eps)
                 trg_eps = r1.target(f_obs=f_obs, f_calc=f_calc)
                 fs.append(trg_eps.t)
                 gs.append(trg_eps.f_calc_gradients[ih])
             g_fin_ab.append((fs[0] - fs[1]) / (2 * eps))
             c_fin_ab.append((gs[0] - gs[1]) / (2 * eps))
         g_fin.append(complex(*g_fin_ab))
         assert approx_equal(c_fin_ab[0].imag, c_fin_ab[1].real)
         c_fin.append((c_fin_ab[0].real, c_fin_ab[1].imag, c_fin_ab[0].imag))
         f_calc[ih] = c_orig
     for pn, f, a in zip(g_fin.part_names(), g_fin.parts(), trg.f_calc_gradients.parts()):
         print >> log, "g fin %s:" % pn, numstr(f)
         print >> log, "  ana %s:" % pn, numstr(a)
     assert approx_equal(trg.f_calc_gradients, g_fin)
     for pn, f, a in zip(["aa", "bb", "ab"], c_fin.parts(), trg.f_calc_hessians.parts()):
         print >> log, "c fin %s:" % pn, numstr(f)
         print >> log, "  ana %s:" % pn, numstr(a)
     assert approx_equal(trg.f_calc_hessians, c_fin)
Example #10
0
def exercise():
  t = henke.table("SI")
  assert t.label() == "Si"
  assert t.atomic_number() == 14
  f = t.at_angstrom(2)
  assert f.is_valid_fp()
  assert f.is_valid_fdp()
  assert f.is_valid()
  from cctbx import factor_kev_angstrom
  assert approx_equal(f.fp(), t.at_kev(factor_kev_angstrom / 2).fp())
  assert approx_equal(f.fdp(), t.at_kev(factor_kev_angstrom / 2).fdp())
  assert approx_equal(f.fp(), t.at_ev(1000 * factor_kev_angstrom / 2).fp())
  assert approx_equal(f.fdp(), t.at_ev(1000 * factor_kev_angstrom / 2).fdp())
  c = f.as_complex()
  assert c.real == f.fp()
  assert c.imag == f.fdp()
  verify(t, 10.0, -9999.00, 4.00688)
  verify(t, 29.3, 4.04139-14, 0.371742)
  verify(t, 30000.0, 14.0266-14, 0.0228459)
  n = 0
  for t in henke.table_iterator():
    n += 1
    if (n == 1):
      assert t.label() == "H"
    elif (n == 92):
      assert t.label() == "U"
    u = henke.table(t.label())
    assert u.label() == t.label()
  assert n == 92
Example #11
0
def exercise_two_loop_recursion(fgh):
  x0 = flex.double([3.0, -4.0])
  g0 = fgh.g(x0)
  h0 = flex.double([[1,0],[0,1]])
  memory = []
  hg0 = bfgs.hg_two_loop_recursion(memory, hk0=h0, gk=g0)
  assert approx_equal(hg0, h0.matrix_multiply(g0))
  #
  x1 = x0 - 1/3 * hg0
  g1 = fgh.g(x1)
  h1 = bfgs.h_update(hk=h0, sk=x1-x0, yk=g1-g0)
  memory.append(bfgs.memory_element(s=x1-x0, y=g1-g0))
  hg1 = bfgs.hg_two_loop_recursion(memory, hk0=h0, gk=g1)
  assert approx_equal(hg1, h1.matrix_multiply(g1))
  #
  x2 = x1 - 1/5 * hg1
  g2 = fgh.g(x2)
  h2 = bfgs.h_update(hk=h1, sk=x2-x1, yk=g2-g1)
  memory.append(bfgs.memory_element(s=x2-x1, y=g2-g1))
  hg2 = bfgs.hg_two_loop_recursion(memory, hk0=h0, gk=g2)
  assert approx_equal(hg2, h2.matrix_multiply(g2))
  #
  x3 = x2 - 3/8 * hg2
  g3 = fgh.g(x3)
  h3 = bfgs.h_update(hk=h2, sk=x3-x2, yk=g3-g2)
  memory.append(bfgs.memory_element(s=x3-x2, y=g3-g2))
  hg3 = bfgs.hg_two_loop_recursion(memory, hk0=h0, gk=g3)
  assert approx_equal(hg3, h3.matrix_multiply(g3))
Example #12
0
def exercise_d_metrical_matrix_d_params():
  def finite_differences(unit_cell, eps=1e-6):
    grads = []
    for i in range(6):
      params = list(unit_cell.parameters())
      params[i] += eps
      uc = uctbx.unit_cell(parameters=params)
      qm = matrix.col(uc.metrical_matrix())
      params[i] -= 2*eps
      uc = uctbx.unit_cell(parameters=params)
      qp = matrix.col(uc.metrical_matrix())
      dq = (qm-qp)/(2*eps)
      grads.extend(list(dq))
    grads = flex.double(grads)
    grads.resize(flex.grid((6,6)))
    return grads.matrix_transpose()
  from cctbx import sgtbx
  p1 = sgtbx.space_group_info('P1')
  uc = p1.any_compatible_unit_cell(27)
  grads = uc.d_metrical_matrix_d_params()
  fd_grads = finite_differences(uc)
  assert approx_equal(grads, fd_grads)
  sgi = sgtbx.space_group_info('I-4')
  uc = sgi.any_compatible_unit_cell(volume=18000)
  grads = uc.d_metrical_matrix_d_params()
  fd_grads = finite_differences(uc)
  assert approx_equal(grads, fd_grads)
def exercise_linear_normal_equations():
  py_eqs = [ ( 1, (-1,  0,  0),  1),
             ( 2, ( 2, -1,  0),  3),
             (-1, ( 0,  2,  1),  2),
             (-2, ( 0,  1,  0), -2),
             ]

  eqs_0 = normal_eqns.linear_ls(3)
  for b, a, w in py_eqs:
    eqs_0.add_equation(right_hand_side=b,
                       design_matrix_row=flex.double(a),
                       weight=w)

  eqs_1 = normal_eqns.linear_ls(3)
  b = flex.double()
  w = flex.double()
  a = sparse.matrix(len(py_eqs), 3)
  for i, (b_, a_, w_) in enumerate(py_eqs):
    b.append(b_)
    w.append(w_)
    for j in xrange(3):
      if a_[j]: a[i, j] = a_[j]
  eqs_1.add_equations(right_hand_side=b, design_matrix=a, weights=w)

  assert approx_equal(
    eqs_0.normal_matrix_packed_u(), eqs_1.normal_matrix_packed_u(), eps=1e-15)
  assert approx_equal(
    eqs_0.right_hand_side(), eqs_1.right_hand_side(), eps=1e-15)
  assert approx_equal(
    list(eqs_0.normal_matrix_packed_u()), [ 13, -6, 0, 9, 4, 2 ], eps=1e-15)
  assert approx_equal(
    list(eqs_0.right_hand_side()), [ 11, -6, -2 ], eps=1e-15)
def exercise_geo_reduce_for_tardy(mon_lib_srv, ener_lib, file_name, expected_bond_counts, expected_dihedral_counts):
    file_path = libtbx.env.find_in_repositories(
        relative_path="phenix_regression/tardy_action/" + file_name, test=os.path.isfile
    )
    if file_path is None:
        print 'Skipping exercise_geo_reduce_for_tardy("%s"):' " input file not available" % file_name
        return
    log = StringIO()
    processed_pdb_file = monomer_library.pdb_interpretation.process(
        mon_lib_srv=mon_lib_srv, ener_lib=ener_lib, file_name=file_path, log=log
    )
    geo = processed_pdb_file.geometry_restraints_manager()
    sites_cart = processed_pdb_file.all_chain_proxies.sites_cart_exact()
    tardy_tree = geo.construct_tardy_tree(sites_cart=sites_cart)
    reduced_geo = geo.reduce_for_tardy(tardy_tree=tardy_tree)
    bond_counts = (
        geo.pair_proxies(sites_cart=sites_cart).bond_proxies.n_total(),
        reduced_geo.pair_proxies(sites_cart=sites_cart).bond_proxies.n_total(),
    )
    dihedral_counts = (geo.get_dihedral_proxies().size(), reduced_geo.get_dihedral_proxies().size())
    assert approx_equal(bond_counts, expected_bond_counts)
    assert approx_equal(dihedral_counts, expected_dihedral_counts)
    proxy_i_seqs_red = {}
    for proxy in reduced_geo.dihedral_proxies:
        proxy_i_seqs_red[proxy.i_seqs] = proxy
    assert len(proxy_i_seqs_red) == dihedral_counts[1]
    awl = list(processed_pdb_file.all_chain_proxies.pdb_hierarchy.atoms_with_labels())
    for proxy in geo.get_dihedral_proxies():
        if not proxy_i_seqs_red.has_key(proxy.i_seqs):
            sigma = 1 / proxy.weight ** 0.5
            if sigma > 10:
                assert awl[proxy.i_seqs[0]].resname in ["PRO", "CYS"]
Example #15
0
def exercise_fast_minimum_reduction():
  mr = uctbx.fast_minimum_reduction(uctbx.unit_cell((1,1,1,90,90,90)))
  assert mr.iteration_limit() == 100
  assert mr.multiplier_significant_change_test() == 16
  assert mr.min_n_no_significant_change() == 2
  mr = uctbx.fast_minimum_reduction(uctbx.unit_cell((1,1,1,90,90,90)), 90)
  assert mr.iteration_limit() == 90
  assert mr.multiplier_significant_change_test() == 16
  assert mr.min_n_no_significant_change() == 2
  mr = uctbx.fast_minimum_reduction(uctbx.unit_cell((1,1,1,90,90,90)), 90,8)
  assert mr.iteration_limit() == 90
  assert mr.multiplier_significant_change_test() == 8
  assert mr.min_n_no_significant_change() == 2
  mr = uctbx.fast_minimum_reduction(uctbx.unit_cell((1,1,1,90,90,90)), 90,8,4)
  assert mr.iteration_limit() == 90
  assert mr.multiplier_significant_change_test() == 8
  assert mr.min_n_no_significant_change() == 4
  mr = uctbx.fast_minimum_reduction(uctbx.unit_cell((2,3,5,80,90,100)))
  assert approx_equal(mr.as_gruber_matrix(),(4,9,25,-5.209445,0,-2.083778))
  assert approx_equal(mr.as_niggli_matrix(),(4,9,25,-5.209445/2,0,-2.083778/2))
  assert approx_equal(mr.as_sym_mat3(),(4,9,25,-2.083778/2,0,-5.209445/2))
  assert mr.as_unit_cell().is_similar_to(uctbx.unit_cell((2,3,5,100,90,100)))
  assert approx_equal(mr.r_inv(), (-1,0,0,0,-1,0,0,0,1))
  assert mr.n_iterations() == 1
  assert not mr.termination_due_to_significant_change_test()
  assert mr.type() == 2
  mr = uctbx.fast_minimum_reduction(uctbx.unit_cell((5,3,2,50,120,130)), 8)
  assert mr.n_iterations() == 8
  assert not mr.termination_due_to_significant_change_test()
  try:
    uctbx.fast_minimum_reduction(uctbx.unit_cell((5,3,2,50,120,130)), 2, 7)
  except RuntimeError, e:
    assert str(e) == "cctbx Error: Iteration limit exceeded."
Example #16
0
def exercise_f_model_no_scales(symbol = "C 2"):
  random.seed(0)
  flex.set_random_seed(0)
  x = random_structure.xray_structure(
    space_group_info       = sgtbx.space_group_info(symbol=symbol),
    elements               =(("O","N","C")*5+("H",)*10),
    volume_per_atom        = 200,
    min_distance           = 1.5,
    general_positions_only = True,
    random_u_iso           = True,
    random_occupancy       = False)
  f_obs = abs(x.structure_factors(d_min = 1.5, algorithm="fft").f_calc())
  x.shake_sites_in_place(mean_distance=1)
  k_iso = flex.double(f_obs.data().size(), 2)
  k_aniso = flex.double(f_obs.data().size(), 3)
  fmodel = mmtbx.f_model.manager(
    xray_structure = x,
    k_isotropic    = k_iso,
    k_anisotropic  = k_aniso,
    f_obs          = f_obs)
  fc = abs(fmodel.f_calc()).data()
  fm = abs(fmodel.f_model()).data()
  fmns = abs(fmodel.f_model_no_scales()).data()
  assert approx_equal(flex.mean(fm/fc), 6)
  assert approx_equal(flex.mean(fmns/fc), 1)
def exercise(prefix="tst_models_to_from_chains"):
  # inputs
  expected_n = 5
  xrs = iotbx.pdb.input(source_info=None, lines=pdb_str).xray_structure_simple()
  input_file_name = "%s.pdb"%prefix
  of = open(input_file_name,"w")
  print >> of, pdb_str
  of.close()
  mi = flex.miller_index(((0,0,1), ))
  ms = miller.set(xrs.crystal_symmetry(), mi,  anomalous_flag=False)
  complete_set = ms.complete_set(d_min=3)
  fc = complete_set.structure_factors_from_scatterers(
    xray_structure=xrs).f_calc()
  # models -> chains
  easy_run.call("phenix.models_as_chains %s"%input_file_name)
  pdb_inp = iotbx.pdb.input(file_name="chains_"+input_file_name)
  h = pdb_inp.construct_hierarchy()
  assert len(list(h.chains()))==expected_n
  assert len(list(h.models()))==1
  xrs_c = pdb_inp.xray_structure_simple()
  fc_c = complete_set.structure_factors_from_scatterers(
    xray_structure=xrs_c).f_calc()
  #
  easy_run.call("phenix.chains_as_models chains_%s"%input_file_name)
  pdb_inp = iotbx.pdb.input(file_name="models_chains_"+input_file_name)
  h = pdb_inp.construct_hierarchy()
  assert len(list(h.chains()))==expected_n
  assert len(list(h.models()))==expected_n
  xrs_m = pdb_inp.xray_structure_simple()
  fc_m = complete_set.structure_factors_from_scatterers(
    xray_structure=xrs_m).f_calc()
  #
  assert approx_equal(0, r(fc, fc_c)  )
  assert approx_equal(0, r(fc, fc_m)  )
  assert approx_equal(0, r(fc_c, fc_m))
Example #18
0
def exercise():
  """ Compare simple mask (mmtbx/utils.h) with classic mask. Does not account
      for site multiplicity due to symmetry. Also, this is an alternative
      exercise for asu mask.
  """
  for i_pdb, pdb_str in enumerate([pdb_str_1, pdb_str_2, pdb_str_3]):
    for solvent_radius in [0, 1.0]:
      print "file %d solvent_raius: %3.1f"%(i_pdb, solvent_radius)
      xrs = iotbx.pdb.input(source_info = None,
        lines = pdb_str).xray_structure_simple()
      mp = mmtbx.masks.mask_master_params.extract()
      mp.solvent_radius=solvent_radius
      mp.shrink_truncation_radius=0.0
      mmtbx_masks_asu_mask_obj = mmtbx.masks.asu_mask(
        xray_structure = xrs.expand_to_p1(sites_mod_positive=True), # Must be P1
        d_min          = 1,
        mask_params    = mp)
      mask_data = mmtbx_masks_asu_mask_obj.mask_data_whole_uc()
      c1o,c0o,s,csf = mask_data.count(1), mask_data.count(0), mask_data.size(),\
        mmtbx_masks_asu_mask_obj.asu_mask.contact_surface_fraction
      assert c1o+c0o==s
      f1o, f0o = c1o*100./s, c0o*100./s
      print "  old", f1o, f0o, csf
      mask_data = maptbx.mask(
        xray_structure = xrs, # Expanded to P1 internally
        n_real         = mask_data.focus(),
        solvent_radius = solvent_radius)
      c1n, c0n, s = mask_data.count(1), mask_data.count(0), mask_data.size()
      assert c1n+c0n==s
      f1n, f0n = c1n*100./s, c0n*100./s
      print "  new:", f1n, f0n
      assert approx_equal(f1o, f1n, 1)
      assert approx_equal(f0o, f0n, 1)
      assert approx_equal(f1o, csf*100, 1)
Example #19
0
def exercise_ellipsoid(n_trials=100, n_sub_trials=10):
  from gltbx import quadrics
  rnd = random.Random(0)
  for i in xrange(n_trials):
    centre = matrix.col([ rnd.random() for k in xrange(3) ])
    half_lengths = matrix.col([ 0.1 + rnd.random() for k in xrange(3) ])
    r = scitbx.math.euler_angles_as_matrix(
      [ rnd.uniform(0, 360) for i in xrange(3) ], deg=True)
    metrics = r * matrix.diag([ x**2 for x in half_lengths ]) * r.transpose()
    t = quadrics.ellipsoid_to_sphere_transform(centre, metrics.as_sym_mat3())
    assert approx_equal(t.translation_part(), centre)
    m = matrix.sqr(t.linear_part())
    assert m.determinant() > 0
    for j in xrange(n_sub_trials):
      y = matrix.col([ rnd.random() for k in xrange(3) ])
      c_y = y.transpose() * y
      x = m*y
      c_x = x.transpose() * metrics.inverse() * x
      assert approx_equal(c_x, c_y)
  r = scitbx.math.euler_angles_as_matrix((30, 115, 260), deg=True)
  centre = matrix.col((-1, 2, 3))
  metrics = r * matrix.diag((-1, 0.1, 1)) * r.transpose()
  t = quadrics.ellipsoid_to_sphere_transform(centre, metrics.as_sym_mat3())
  assert t.non_positive_definite()
  x = r * matrix.col((1,0,0))
  assert x.transpose() * metrics.inverse() * x > 0
Example #20
0
def exercise () :
  from libtbx.test_utils import approx_equal
  try :
    import numpy
  except ImportError :
    test_numpy = False
    print "Numpy not installed, will skip array conversion."
  else :
    test_numpy = True
  # ramachandran
  z_data = get_rotarama_data(pos_type="general",
    convert_to_numpy_array=test_numpy)
  z_data = get_rotarama_data(pos_type="pre-proline",
    convert_to_numpy_array=test_numpy)
  # rotamer
  z_data = get_rotarama_data(residue_type="arg",
    db="rota",
    convert_to_numpy_array=test_numpy)
  z_data = get_rotarama_data(residue_type="phe",
    db="rota",
    convert_to_numpy_array=test_numpy)
  atom_info = decode_atom_str(" OD2 ASP A  14L")
  assert (atom_info.name == " OD2") and (atom_info.resname == "ASP")
  assert (atom_info.altloc == " ") and (atom_info.chain_id == "A")
  assert (atom_info.resid == "  14L") and (atom_info.resseq == "14")
  mpscore = molprobity_score(48.0, 9.95, 86.44) # 2hr0
  assert approx_equal(mpscore, 3.55, eps=0.01)
  mpscore = molprobity_score(215.8, 17.99, 52.18) # 3mku
  assert approx_equal(mpscore, 4.71, eps=0.01)
def exercise_tensor_constraints_core(crystal_symmetry):
  from cctbx import crystal
  from cctbx import adptbx
  from scitbx import matrix
  site_symmetry = crystal.special_position_settings(
    crystal_symmetry).site_symmetry(site=(0,0,0))
  unit_cell = crystal_symmetry.unit_cell()
  group = crystal_symmetry.space_group()
  assert site_symmetry.n_matrices() == group.order_p()
  for reciprocal_space in [False, True]:
    c_tensor_constraints = sgtbx.tensor_rank_2_constraints(
      space_group=group,
      reciprocal_space=reciprocal_space).row_echelon_form()
    p_tensor_constraints = python_tensor_constraints(
      self=group, reciprocal_space=reciprocal_space)
    assert c_tensor_constraints.all_eq(p_tensor_constraints)
  adp_constraints = group.adp_constraints()
  u_cart_p1 = adptbx.random_u_cart()
  u_star_p1 = adptbx.u_cart_as_u_star(unit_cell, u_cart_p1)
  u_star = site_symmetry.average_u_star(u_star_p1)
  f = unit_cell.volume()**(2/3.)
  assert approx_equal(
    list(matrix.col(group.average_u_star(u_star=u_star_p1))*f),
    list(matrix.col(u_star)*f))
  independent_params = adp_constraints.independent_params(u_star)
  assert adp_constraints.n_independent_params() == len(independent_params)
  assert adp_constraints.n_independent_params() \
       + adp_constraints.n_dependent_params() == 6
  u_star_vfy = adp_constraints.all_params(independent_params)
  u_cart = adptbx.u_star_as_u_cart(unit_cell, u_star)
  u_cart_vfy = adptbx.u_star_as_u_cart(unit_cell, list(u_star_vfy))
  assert approx_equal(u_cart_vfy, u_cart)
Example #22
0
 def __init__(self, xray_structure, k_anisotropic, k_masks, ss):
   self.xray_structure = xray_structure
   self.k_anisotropic  = k_anisotropic
   self.k_masks        = k_masks
   self.ss             = ss
   #
   k_total = self.k_anisotropic
   r = scitbx.math.gaussian_fit_1d_analytical(x=flex.sqrt(self.ss), y=k_total)
   k,b = r.a, r.b
   #
   k,b,r = mmtbx.bulk_solvent.fit_k_exp_b_to_k_total(k_total, self.ss, k, b)
   k_exp_overall, b_exp_overall = None,None
   if(r<0.7): k_exp_overall, b_exp_overall = k,b
   if(self.xray_structure is None): return None
   b_adj = 0
   if([k_exp_overall, b_exp_overall].count(None)==0 and k != 0):
     bs1 = self.xray_structure.extract_u_iso_or_u_equiv()*adptbx.u_as_b(1.)
     def split(b_trace, xray_structure):
       b_min = xray_structure.min_u_cart_eigenvalue()*adptbx.u_as_b(1.)
       b_res = min(0, b_min + b_trace+1.e-6)
       b_adj = b_trace-b_res
       xray_structure.shift_us(b_shift = b_adj)
       return b_adj, b_res
     b_adj,b_res=split(b_trace=b_exp_overall,xray_structure=self.xray_structure)
     k_new = k_exp_overall*flex.exp(-self.ss*b_adj)
     bs2 = self.xray_structure.extract_u_iso_or_u_equiv()*adptbx.u_as_b(1.)
     diff = bs2-bs1
     assert approx_equal(flex.min(diff), flex.max(diff))
     assert approx_equal(flex.max(diff), b_adj)
     self.k_anisotropic = self.k_anisotropic/k_new
     self.k_masks = [m*flex.exp(-self.ss*b_adj) for m in self.k_masks]
Example #23
0
def exercise_emringer_residue_scan():
  pdb_file = libtbx.env.find_in_repositories(
    relative_path="phenix_regression/mmtbx/em_ringer/tst_emringer_model.pdb",
    test=os.path.isfile)
  map_file = libtbx.env.find_in_repositories(
    relative_path="phenix_regression/mmtbx/em_ringer/tst_emringer_map.ccp4",
    test=os.path.isfile)
  assert (not None in [pdb_file, map_file])
  results, scoring, rolling = emringer.run([pdb_file, map_file], out=null_out())
  # Make sure the right number of residues (22 out of 28) get scanned
  assert len(results)==22
  modelled_list = [290.742121792,192.844056257,45.4781110306,294.247825632,303.618891108,58.7694040824,331.70068496,46.7136045049,290.167261226,304.261231829,282.651244586,268.729721112,195.972333785,305.321933311,314.81066224,286.028424514,311.180807466,313.004918133,296.67781565,296.949191638,169.644245088,192.496265164]
  peak_list = [270,180,260,75,305,30,310,90,265,270,270,240,280,260,310,285,295,100,260,165,155,200]
  peak_rhos = [0.175600306502,0.351591946536,0.206238983746,0.3269057296,0.68375562882,0.251143527693,0.29106077218,0.199922124642,0.298461589197,0.563313760047,0.412696803251,0.511080434089,0.310001828446,0.228239176285,0.563148497472,0.490755919184,0.200978032127,0.274929619102,0.299229846335,0.179215798655,0.150783734124,0.210869945593]
  for i in range(22):
    # Make sure the modelled angle is correctly read
    assert approx_equal(results[i]._angles[1].angle_current, modelled_list[i])
    # Make sure the peak is chosen correctly
    assert approx_equal(results[i]._angles[1].peak_chi, peak_list[i])
    # Make sure the peak rhos are correct
    assert approx_equal(results[i]._angles[1].peak_rho, peak_rhos[i])
  results, scoring2, rolling2 = emringer.run([pdb_file, map_file, "rolling_window_threshold=0.5"], out=null_out())
  assert rolling.threshold == 0
  assert rolling2.threshold == 0.5
  #print rolling.results_a[0]
  #print rolling2.results_a[0]
  # just making sure this doesn't break!
  results, scoring2, rolling = emringer.run([pdb_file, map_file, "sampling_angle=2"], out=null_out())
Example #24
0
def exercise_1(grid_step,
               radius,
               shell,
               a,
               b,
               buffer_layer,
               site_cart):
  xray_structure = xray_structure_of_one_atom(site_cart    = site_cart,
                                              buffer_layer = buffer_layer,
                                              a            = a,
                                              b            = b)
  sampled_density = mmtbx.real_space.sampled_model_density(
                                            xray_structure = xray_structure,
                                            grid_step      = grid_step)
  site_frac = xray_structure.unit_cell().fractionalization_matrix()*site_cart
  assert approx_equal(flex.max(sampled_density.data()),
              sampled_density.data().value_at_closest_grid_point(site_frac[0]))

  around_atom_obj = mmtbx.real_space.around_atom(
                           unit_cell = xray_structure.unit_cell(),
                           map_data  = sampled_density.data(),
                           radius    = radius,
                           shell     = shell,
                           site_frac = list(xray_structure.sites_frac())[0])
  data_exact = around_atom_obj.data()
  dist_exact = around_atom_obj.distances()
  approx_obj = maptbx.one_gaussian_peak_approximation(
                                           data_at_grid_points    = data_exact,
                                           distances              = dist_exact,
                                           use_weights            = False,
                                           optimize_cutoff_radius = True)
  assert approx_equal(approx_obj.a_reciprocal_space(), 6.0, 1.e-3)
  assert approx_equal(approx_obj.b_reciprocal_space(), 3.0, 1.e-3)
  assert approx_obj.gof() < 0.3
  assert approx_obj.cutoff_radius() < radius
 def check_f_derivs():
   g_ana = trg.f_gradients
   c_ana = trg.f_hessians
   eps = 1e-6
   g_fin = flex.complex_double()
   c_fin = flex.vec3_double()
   for ih in xrange(i_calc.size()):
     c_orig = f_calc[ih]
     g_fin_ab = []
     c_fin_ab = []
     for iab in [0,1]:
       fs = []
       gs = []
       for signed_eps in [eps, -eps]:
         if (iab == 0):
           f_calc[ih] = complex(c_orig.real + signed_eps, c_orig.imag)
         else:
           f_calc[ih] = complex(c_orig.real, c_orig.imag + signed_eps)
         trg_eps = kwt2(
           f_obs=f_obs, i_obs=i_obs, i_sig=i_sig,
           f_calc=f_calc, i_calc=None, wa=wa, wb=wb)
         fs.append(trg_eps.target)
         gs.append(trg_eps.f_gradients[ih])
       g_fin_ab.append((fs[0]-fs[1])/(2*eps))
       c_fin_ab.append((gs[0]-gs[1])/(2*eps))
     g_fin.append(complex(*g_fin_ab))
     assert approx_equal(c_fin_ab[0].imag, c_fin_ab[1].real)
     c_fin.append((c_fin_ab[0].real, c_fin_ab[1].imag, c_fin_ab[0].imag))
     f_calc[ih] = c_orig
   assert approx_equal(g_ana, g_fin)
   assert approx_equal(c_ana, c_fin)
 def step_h(self, V_L, b_o):
   """
   Three uncorrelated translations.
   """
   print_step("Step h:", self.log)
   V_M = b_o.R_PL * V_L * b_o.R_PL.transpose()
   self.show_matrix(x=V_M, title="V_M ")
   es = self.eigen_system_default_handler(m=V_M)
   v_x, v_y, v_z = es.x, es.y, es.z
   lam_u,lam_v,lam_w = es.vals
   self.show_vector(x=v_x, title="v_x")
   self.show_vector(x=v_y, title="v_y")
   self.show_vector(x=v_z, title="v_z")
   assert approx_equal(v_x.dot(v_y), 0)
   assert approx_equal(v_y.dot(v_z), 0)
   assert approx_equal(v_z.dot(v_x), 0)
   R_MV = matrix.sqr([
     v_x[0], v_y[0], v_z[0],
     v_x[1], v_y[1], v_z[1],
     v_x[2], v_y[2], v_z[2]])
   self.show_matrix(x=R_MV, title="R_MV")
   V_V = matrix.sym(sym_mat3=[lam_u, lam_v, lam_w, 0,0,0])
   self.show_matrix(x=V_V, title="V_V")
   assert approx_equal(V_V, R_MV.transpose() * V_M * R_MV) # formula (20)
   return group_args(
     v_x   = v_x,
     v_y   = v_y,
     v_z   = v_z,
     V_M   = V_M,
     V_V   = V_V,
     R_MV  = R_MV)
Example #27
0
def exercise_pair_registry_adp_iso():
  mersenne_twister = flex.mersenne_twister(seed=0)
  for n_seq in xrange(2,20):
    registry = ncs.restraints.pair_registry(n_seq=n_seq, n_ncs=n_seq)
    for j_seq in xrange(1,n_seq):
      assert registry.enter(i_seq=0, j_seq=j_seq, j_ncs=j_seq) == (0, 0)
    selection_pairs = registry.selection_pairs()
    for j in xrange(1,n_seq):
      assert zip(*selection_pairs[j-1]) == [(0,j)]
    weight = 2.134
    average_power = 0.589
    u_isos = mersenne_twister.random_double(size=n_seq) + 1.e-3
    gradients_in = mersenne_twister.random_double(size=n_seq)
    gradients = gradients_in.deep_copy()
    registry_residual_sum = registry.adp_iso_residual_sum(
      weight=weight,
      average_power=average_power,
      u_isos=u_isos,
      u_average_min=1.e-6,
      gradients=gradients)
    gradients -= gradients_in
    assert approx_equal(
      registry_residual_sum,
      adp_iso_residual_sum(
        weight=weight, average_power=average_power, u_isos=u_isos))
    assert approx_equal(
      gradients,
      adp_iso_analytical_gradients(
        weight=weight, average_power=average_power, u_isos=u_isos))
Example #28
0
 def apply_back_trace_of_overall_exp_scale_matrix(self, xray_structure=None):
   k,b=self.overall_isotropic_kb_estimate()
   k_total = self.core.k_isotropic * self.core.k_anisotropic * \
     self.core.k_isotropic_exp
   k,b,r = mmtbx.bulk_solvent.fit_k_exp_b_to_k_total(k_total, self.ss, k, b)
   if(r<0.7): self.k_exp_overall,self.b_exp_overall = k,b
   if(xray_structure is None): return None
   b_adj = 0
   if([self.k_exp_overall,self.b_exp_overall].count(None)==0 and k != 0):
     bs1 = xray_structure.extract_u_iso_or_u_equiv()*adptbx.u_as_b(1.)
     def split(b_trace, xray_structure):
       b_min = xray_structure.min_u_cart_eigenvalue()*adptbx.u_as_b(1.)
       b_res = min(0, b_min + b_trace+1.e-6)
       b_adj = b_trace-b_res
       xray_structure.shift_us(b_shift = b_adj)
       return b_adj, b_res
     b_adj,b_res=split(b_trace=self.b_exp_overall,xray_structure=xray_structure)
     k_new = self.k_exp_overall*flex.exp(-self.ss*b_adj)
     bs2 = xray_structure.extract_u_iso_or_u_equiv()*adptbx.u_as_b(1.)
     diff = bs2-bs1
     assert approx_equal(flex.min(diff), flex.max(diff))
     assert approx_equal(flex.max(diff), b_adj)
     self.core = self.core.update(
       k_isotropic = self.core.k_isotropic,
       k_isotropic_exp = self.core.k_isotropic_exp/k_new,
       k_masks = [m*flex.exp(-self.ss*b_adj) for m in self.core.k_masks])
   return group_args(
     xray_structure = xray_structure,
     k_isotropic    = self.k_isotropic(),
     k_anisotropic  = self.k_anisotropic(),
     k_mask         = self.k_masks(),
     b_adj          = b_adj)
Example #29
0
 def run_once():
   buffers = easy_run.fully_buffered(command=run_cmd)
   if (len(buffers.stderr_lines) != 3):
     print "v"*79
     print "\n".join(buffers.stderr_lines)
     print "^"*79
     raise RuntimeError(
       "Unexpected number of output lines"
       " (3 expected; acutal output see above).")
   if (n_scatt == 0):
     pass
   elif (n_scatt <= 10 and n_refl <= 100):
     assert len(buffers.stdout_lines) == n_scatt + n_refl
   else:
     assert len(buffers.stdout_lines) == 1
     max_a, max_b = [float(s) for s in buffers.stdout_lines[0].split()]
   if (check_max_a_b):
     if (n_scatt == 2000 and n_refl == 20000):
       assert approx_equal(max_a, 35.047157, eps=1e-4)
       assert approx_equal(max_b, 25.212738, eps=1e-4)
     elif (n_scatt == 100 and n_refl == 1000):
       assert approx_equal(max_a,  4.493645, eps=1e-4)
       assert approx_equal(max_b, 10.515532, eps=1e-4)
     elif (n_scatt <= 10 and n_refl <= 100):
       if (libtbx.env.has_module(name="cctbx")):
         compare_with_cctbx_structure_factors(
           n_scatt=n_scatt,
           n_refl=n_refl,
           output_lines=buffers.stdout_lines)
     else:
       raise RuntimeError, (max_a, max_b)
   utime = float(buffers.stderr_lines[1].split()[1])
   utimes.append(utime)
   print "sample utime: %.2f" % utime
   sys.stdout.flush()
Example #30
0
def tst_gauss_hermite_engine():
  # test with known values
  ghe = sm.gauss_hermite_engine(4)
  x_ams_55    = [0.524647623275290, 1.650680123885785]
  w_ams_55    = [0.8049140900055  , 0.08131283544725]
  wexs_ams_55 = [1.0599644828950  , 1.2402258176958]
  x_this = ghe.x()[0:2]
  w_this = ghe.w()[0:2]
  for x,xx in zip( x_this, x_ams_55):
    assert approx_equal( x, xx, eps=1e-8 )
  for w,ww in zip( w_this, w_ams_55):
    assert approx_equal( w, ww, eps=1e-8 )

  # test a large order set of number
  for n in range(2,29):
    ghe = sm.gauss_hermite_engine(n)
    x_this = ghe.x()
    step = 0.5/math.sqrt(n*1.0)
    for ix in range( x_this.size() ):
      f = ghe.f( x_this[ix] )[0]
      assert approx_equal(f,0,eps=1e-5)
      # check the uniqueness of each point
      for jj in  range( x_this.size() ):
        if jj != ix:
          assert ( math.fabs(x_this[ix]-x_this[jj]) >= step )
Example #31
0
        for a_box, a_output, n_box, n_output in zip(box_abc, output_abc,
                                                    output_box.map_box.all(),
                                                    output_unit_cell_grid):
            expected_output_abc.append(a_box * n_output / n_box)
            box_spacing.append(a_box / n_box)
            if output_crystal_symmetry:
                output_spacing.append(a_output / n_output)
            else:
                output_spacing.append(a_box / n_box)

        if output_crystal_symmetry:  # make sure it is compatible...
            r0 = expected_output_abc[0] / output_abc[0]
            r1 = expected_output_abc[1] / output_abc[1]
            r2 = expected_output_abc[2] / output_abc[2]
            from libtbx.test_utils import approx_equal
            if not approx_equal(r0, r1, eps=0.001) or not approx_equal(
                    r0, r2, eps=0.001):
                print >>log,"WARNING: output_unit_cell and cell_grid will "+\
                  "change ratio of grid spacing.\nOld spacings: "+\
                 "(%.2f, %.2f, %.2f) A " %(tuple(box_spacing))+\
                "\nNew spacings:  (%.2f, %.2f, %.2f) A \n" %(tuple(output_spacing))
        else:
            output_abc = expected_output_abc

        from cctbx import crystal
        output_crystal_symmetry = crystal.symmetry(unit_cell=list(output_abc) +
                                                   [90, 90, 90],
                                                   space_group="P1")
        print >>log, \
          "Output unit cell will be:  (%.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n"%(
           tuple(output_crystal_symmetry.unit_cell().parameters()))
Example #32
0
def verify(table, wave_length, fp, fdp):
    fp_fdp = table.at_angstrom(wave_length)
    assert approx_equal(fp_fdp.fp(), fp)
    assert approx_equal(fp_fdp.fdp(), fdp)
 def __init__(self,
              refiner,
              xray_structure,
              start_trial_weight_value=50.,
              weight_sample_rate=10,
              rms_bonds_limit=0.03,
              rms_angles_limit=3.0,
              optimize_weight=True):
     self.rms_angles_start = None
     self.rms_bonds_start = None
     self.refiner = refiner
     self.weight_start = start_trial_weight_value
     sites_cart_start = xray_structure.sites_cart()
     self.rms_bonds_start, self.rms_angles_start  = \
       self.rmsds(sites_cart=xray_structure.sites_cart())
     self.weight_sample_rate = weight_sample_rate
     # results
     self.weight_final = None
     self.sites_cart_result = None
     self.rms_bonds_final, self.rms_angles_final = None, None
     #
     pool = {}
     bonds = flex.double()
     angles = flex.double()
     weights = flex.double()
     #
     weight = start_trial_weight_value
     weight_last = weight
     self.adjust_weight_sample_rate(weight=weight)
     if (optimize_weight):
         while True:
             self.rmsds(sites_cart=sites_cart_start)  # DUMMY
             self.adjust_weight_sample_rate(weight=weight_last)
             tmp = xray_structure.deep_copy_scatterers()
             #tmp.shake_sites_in_place(
             #  rms_difference = None,
             #  mean_distance  = 0.5)
             refiner.refine(
                 xray_structure=
                 tmp,  #xray_structure.deep_copy_scatterers(), # XXX
                 weight=weight)
             sites_cart_result = refiner.sites_cart()
             bd, ad = self.rmsds(sites_cart=sites_cart_result)
             bonds.append(bd)
             angles.append(ad)
             weights.append(weight)
             pool.setdefault(weight, []).append(
                 [sites_cart_result.deep_copy(), bd, ad])
             if (refiner.geometry_restraints_manager is None): break
             weight_last = weight
             if (ad > rms_angles_limit or bd > rms_bonds_limit):
                 weight -= self.weight_sample_rate
             else:
                 weight += self.weight_sample_rate
             if (weight < 0 or abs(weight) < 1.e-6):
                 self.adjust_weight_sample_rate(weight=weight)
                 weight = weight_last
                 weight -= self.weight_sample_rate
             #print ">>> ", "%8.4f %8.4f"%(weight, weight_last), "%6.4f %5.2f"%(bd, ad),\
             #  self.weight_sample_rate, "  f (start/final):", refiner.refined.f_start, refiner.refined.f_final
             if ((weight < 0 or weight > 1000) or weight in weights): break
             l = bonds.size() - 1
             if (bonds.size() > 5 and abs(bonds[l] - bonds[l - 1]) < 0.0005
                     and abs(bonds[l] - bonds[l - 2]) < 0.0005
                     and abs(bonds[l] - bonds[l - 3]) < 0.0005
                     and abs(bonds[l] - bonds[l - 4]) < 0.0005
                     and abs(bonds[l] - bonds[l - 5]) < 0.0005):
                 break
     else:
         refiner.refine(
             xray_structure=xray_structure.deep_copy_scatterers(),  # XXX
             weight=weight)
         sites_cart_result = refiner.sites_cart()
     # select results
     if (optimize_weight):
         delta = bonds - rms_bonds_limit
         ind = (delta == flex.max_default(delta.select(delta <= 0),
                                          flex.min(delta))).iselection()[0]
         self.weight_final = weights[ind]
         self.sites_cart_result = pool[self.weight_final][0][0]
         self.rms_bonds_final,self.rms_angles_final = \
           self.rmsds(sites_cart=self.sites_cart_result)
         assert approx_equal(pool[self.weight_final][0][2], angles[ind])
         assert approx_equal(pool[self.weight_final][0][1], bonds[ind])
         assert approx_equal(self.rms_angles_final, angles[ind])
         assert approx_equal(self.rms_bonds_final, bonds[ind])
     else:
         self.weight_final = self.weight_start
         self.sites_cart_result = sites_cart_result
Example #34
0
def exercise(space_group_info,
             const_gaussian,
             negative_gaussian,
             anomalous_flag,
             allow_mix,
             use_u_iso,
             use_u_aniso,
             d_min=1.,
             resolution_factor=1. / 3,
             max_prime=5,
             quality_factor=100,
             wing_cutoff=1.e-6,
             exp_table_one_over_step_size=-100,
             force_complex=False,
             verbose=0):
    if (const_gaussian):
        elements = ["const"] * 8
    elif (negative_gaussian):
        elements = ["H"] * 8
    else:
        elements = ["N", "C", "C", "O", "N", "C", "C", "O"]
    if (random.random() < 0.5):
        random_f_prime_scale = 0.6
    else:
        random_f_prime_scale = 0
    structure = random_structure.xray_structure(
        space_group_info,
        elements=elements,
        random_f_prime_d_min=1,
        random_f_prime_scale=random_f_prime_scale,
        random_f_double_prime=anomalous_flag,
        use_u_aniso=True,
        use_u_iso=False,
        random_u_cart_scale=0.3,
        random_u_iso=True,
        random_occupancy=True)
    random_structure.random_modify_adp_and_adp_flags_2(
        scatterers=structure.scatterers(),
        use_u_iso=use_u_iso,
        use_u_aniso=use_u_aniso,
        allow_mix=allow_mix,
        random_u_iso_scale=0.3,
        random_u_iso_min=0.0)
    sampled_density_must_be_positive = True
    if (negative_gaussian):
        reg = structure.scattering_type_registry(
            custom_dict={"H": eltbx.xray_scattering.gaussian(-1)})
        assert reg.gaussian("H").n_terms() == 0
        assert reg.gaussian("H").c() == -1
        sampled_density_must_be_positive = False
    elif (not const_gaussian and random.random() < 0.5):
        if (random.random() < 0.5):
            sampled_density_must_be_positive = False
        assign_custom_gaussians(
            structure, negative_a=not sampled_density_must_be_positive)
    f_direct = structure.structure_factors(anomalous_flag=anomalous_flag,
                                           d_min=d_min,
                                           algorithm="direct").f_calc()
    crystal_gridding = f_direct.crystal_gridding(
        resolution_factor=resolution_factor, d_min=d_min, max_prime=max_prime)
    assert crystal_gridding.symmetry_flags() is None
    rfft = fftpack.real_to_complex_3d(crystal_gridding.n_real())
    u_base = xray.calc_u_base(d_min, resolution_factor, quality_factor)
    omptbx.env.num_threads = libtbx.introspection.number_of_processors()
    sampled_density = xray.sampled_model_density(
        unit_cell=structure.unit_cell(),
        scatterers=structure.scatterers(),
        scattering_type_registry=structure.scattering_type_registry(),
        fft_n_real=rfft.n_real(),
        fft_m_real=rfft.m_real(),
        u_base=u_base,
        wing_cutoff=wing_cutoff,
        exp_table_one_over_step_size=exp_table_one_over_step_size,
        force_complex=force_complex,
        sampled_density_must_be_positive=sampled_density_must_be_positive,
        tolerance_positive_definite=1.e-5,
        use_u_base_as_u_extra=False)
    focus = sampled_density.real_map_unpadded().focus()
    all = sampled_density.real_map_unpadded().all()
    last = sampled_density.real_map_unpadded().last()
    assert approx_equal(focus, last)
    assert approx_equal(all, last)
    assert sampled_density.anomalous_flag() == (anomalous_flag
                                                or force_complex)
    if (0 or verbose):
        print "const_gaussian:", const_gaussian
        print "negative_gaussian:", negative_gaussian
        print "number of scatterers passed:", \
          sampled_density.n_scatterers_passed()
        print "number of contributing scatterers:", \
          sampled_density.n_contributing_scatterers()
        print "number of anomalous scatterers:", \
          sampled_density.n_anomalous_scatterers()
        print "wing_cutoff:", sampled_density.wing_cutoff()
        print "exp_table_one_over_step_size:", \
          sampled_density.exp_table_one_over_step_size()
        print "exp_table_size:", sampled_density.exp_table_size()
        print "max_sampling_box_edges:", sampled_density.max_sampling_box_edges(
        ),
        print "(%.4f, %.4f, %.4f)" % sampled_density.max_sampling_box_edges_frac(
        )
        if (not sampled_density.anomalous_flag()):
            print "map min:", flex.min(sampled_density.real_map())
            print "map max:", flex.max(sampled_density.real_map())
        else:
            print "map min:", flex.min(flex.real(
                sampled_density.complex_map())),
            print flex.min(flex.imag(sampled_density.complex_map()))
            print "map max:", flex.max(flex.real(
                sampled_density.complex_map())),
            print flex.max(flex.imag(sampled_density.complex_map()))
    if (not sampled_density.anomalous_flag() and negative_gaussian):
        assert flex.min(sampled_density.real_map()) < 0
        assert flex.max(sampled_density.real_map()) == 0
    if (not sampled_density.anomalous_flag()):
        map = sampled_density.real_map()
        assert map.all() == rfft.m_real()
        assert map.focus() == rfft.n_real()
        sf_map = rfft.forward(map)
        assert sf_map.all() == rfft.n_complex()
        assert sf_map.focus() == rfft.n_complex()
        collect_conj = True
    else:
        cfft = fftpack.complex_to_complex_3d(rfft.n_real())
        map = sampled_density.complex_map()
        assert map.all() == cfft.n()
        assert map.focus() == cfft.n()
        sf_map = cfft.backward(map)
        assert sf_map.all() == cfft.n()
        assert sf_map.focus() == cfft.n()
        collect_conj = False
    f_fft_data = maptbx.structure_factors.from_map(
        space_group=f_direct.space_group(),
        anomalous_flag=sampled_density.anomalous_flag(),
        miller_indices=f_direct.indices(),
        complex_map=sf_map,
        conjugate_flag=collect_conj).data()
    sampled_density.eliminate_u_extra_and_normalize(f_direct.indices(),
                                                    f_fft_data)
    structure_factor_utils.check_correlation("direct/fft_regression",
                                             f_direct.indices(),
                                             0,
                                             f_direct.data(),
                                             f_fft_data,
                                             min_corr_ampl=1 * 0.99,
                                             max_mean_w_phase_error=1 * 3.,
                                             verbose=verbose)
    f_fft = xray.structure_factors.from_scatterers(
        miller_set=f_direct,
        grid_resolution_factor=resolution_factor,
        quality_factor=quality_factor,
        wing_cutoff=wing_cutoff,
        exp_table_one_over_step_size=exp_table_one_over_step_size,
        sampled_density_must_be_positive=sampled_density_must_be_positive,
        max_prime=max_prime)(xray_structure=structure,
                             miller_set=f_direct,
                             algorithm="fft").f_calc()
    structure_factor_utils.check_correlation("direct/fft_xray",
                                             f_direct.indices(),
                                             0,
                                             f_direct.data(),
                                             f_fft.data(),
                                             min_corr_ampl=1 * 0.99,
                                             max_mean_w_phase_error=1 * 3.,
                                             verbose=verbose)
Example #35
0
def test_basics():
    # construct a simple structure whose sites and u_iso's are to be refined
    xs = xray.structure(crystal_symmetry=crystal.symmetry(
        unit_cell=(10, 10, 10, 90, 90, 90), space_group_symbol='hall: P 1'),
                        scatterers=flex.xray_scatterer((
                            xray.scatterer('C0', site=(0, -1 / 2, 0), u=0.1),
                            xray.scatterer('C1', site=(0, 1 / 2, 0), u=0.2),
                            xray.scatterer('C0a', site=(1 / 2, 0, 0), u=0.1),
                            xray.scatterer('C1a', site=(-1 / 2, 0, 0), u=0.2),
                        )))
    for sc in xs.scatterers():
        sc.flags.set_grad_site(True)
        sc.flags.set_grad_u_iso(True)

    # copy the original structure as a reference to test against later
    xs_ref = xs.deep_copy_scatterers()

    # mess up the symmetries that the forthcoming constraints shall impose
    c0, c1, c0a, c1a = xs.scatterers()
    c0a.site = (0, 0, 0)
    c1a.site = (1 / 2, 1 / 2, 1 / 2)
    c0a.u_iso = 0.5
    c1a.u_iso = 0.6

    # construct a reparametrisation for the following constraints:
    # (C0a, C1a) is the image of (C0, C1) through a rotation of 90 degrees
    # about the z-axis
    r = constraints.ext.reparametrisation(xs.unit_cell())
    sc_params = constraints.shared_scatterer_parameters(xs.scatterers())
    c0_site_param = r.add(constraints.independent_site_parameter, c0)
    sc_params[0].site = c0_site_param
    c1_site_param = r.add(constraints.independent_site_parameter, c1)
    sc_params[1].site = c1_site_param
    move_param = r.add(constraints.independent_small_6_vector_parameter,
                       (0, 0, 0, 0, 0, math.pi / 2))
    c0a_c1a_site_param = r.add(constraints.same_group_xyz,
                               scatterers=(c0a, c1a),
                               sites=(c0_site_param, c1_site_param),
                               alignment_matrix=matrix.identity(3),
                               shifts_and_angles=move_param)
    sc_params[2].site = sc_params[3].site = c0a_c1a_site_param
    c0_u_iso_param = r.add(constraints.independent_u_iso_parameter, c0)
    sc_params[0].u = c0_u_iso_param
    c1_u_iso_param = r.add(constraints.independent_u_iso_parameter, c1)
    sc_params[1].u = c1_u_iso_param
    c0a_c1a_u_iso_param = r.add(constraints.same_group_u_iso,
                                scatterers=(c0a, c1a),
                                u_isos=(c0_u_iso_param, c1_u_iso_param))
    sc_params[2].u = sc_params[3].u = c0a_c1a_u_iso_param
    r.finalise()

    # put the reparametrisation to work
    r.linearise()
    r.store()
    c0_ref, c1_ref, c0a_ref, c1a_ref = xs_ref.scatterers()
    assert approx_equal(c0a.site, c0a_ref.site, eps=1e-12)
    assert approx_equal(c1a.site, c1a_ref.site, eps=1e-12)
    assert approx_equal(c0a.u_iso, c0a_ref.u_iso, eps=1e-12)
    assert approx_equal(c1a.u_iso, c1a_ref.u_iso, eps=1e-12)

    # check that origin fixing restraints work in the presence of
    # that reparametrisation
    # this is a regression test as they used not to (bug reported by Oleg)
    from smtbx.refinement.restraints import origin_fixing_restraints
    from scitbx import lstbx
    orig_fixing = origin_fixing_restraints.homogeneous_weighting(
        xs.space_group())
    normal_eqn = lstbx.normal_eqns.ext.linear_ls(n_parameters=(3 + 1) * 2 + 6)
    jacobian_transpose_matching_grad_fc = r.jacobian_transpose_matching(
        sc_params.mapping_to_grad_fc())
    orig_fixing.add_to(normal_eqn, jacobian_transpose_matching_grad_fc,
                       sc_params)
Example #36
0
def exercise_spotfinder():
    if not libtbx.env.has_module("dials_regression"):
        print "Skipping exercise_spotfinder: dials_regression not present"
        return

    data_dir = libtbx.env.find_in_repositories(
        relative_path="dials_regression/centroid_test_data",
        test=os.path.isdir)
    template = glob(os.path.join(data_dir, "centroid*.cbf"))
    args = [
        "dials.find_spots", ' '.join(template),
        "output.reflections=spotfinder.pickle", "output.shoeboxes=True"
    ]
    result = easy_run.fully_buffered(command=" ".join(args)).raise_if_errors()
    assert os.path.exists("spotfinder.pickle")
    with open("spotfinder.pickle", "rb") as f:
        reflections = pickle.load(f)
        assert len(reflections) == 653, len(reflections)
        refl = reflections[0]
        assert approx_equal(refl['intensity.sum.value'], 42)
        assert approx_equal(refl['bbox'], (1398, 1400, 513, 515, 0, 1))
        assert approx_equal(refl['xyzobs.px.value'],
                            (1399.1190476190477, 514.2142857142857, 0.5))
        assert "shoebox" in reflections
    print 'OK'

    # now with a resolution filter
    args = [
        "dials.find_spots", "filter.d_min=2", "filter.d_max=15",
        ' '.join(template), "output.reflections=spotfinder.pickle",
        "output.shoeboxes=False"
    ]
    result = easy_run.fully_buffered(command=" ".join(args)).raise_if_errors()
    assert os.path.exists("spotfinder.pickle")
    with open("spotfinder.pickle", "rb") as f:
        reflections = pickle.load(f)
        assert len(reflections) == 467, len(reflections)
        assert "shoebox" not in reflections
    print 'OK'

    # now write a hot mask
    args = [
        "dials.find_spots", "write_hot_mask=True", ' '.join(template),
        "output.reflections=spotfinder.pickle", "output.shoeboxes=False"
    ]
    result = easy_run.fully_buffered(command=" ".join(args)).raise_if_errors()
    assert os.path.exists("spotfinder.pickle")
    with open("spotfinder.pickle", "rb") as f:
        reflections = pickle.load(f)
        assert len(reflections) == 653, len(reflections)
        assert "shoebox" not in reflections
    assert os.path.exists("hot_mask_0.pickle")
    with open("hot_mask_0.pickle", "rb") as f:
        mask = pickle.load(f)
        assert len(mask) == 1, len(mask)
        assert mask[0].count(False) == 12, mask[0].count(False)
    print 'OK'

    # now write a hot mask
    args = [
        "dials.find_spots", "write_hot_mask=True",
        "hot_mask_prefix=my_hot_mask", ' '.join(template),
        "output.reflections=spotfinder.pickle", "output.shoeboxes=False"
    ]
    result = easy_run.fully_buffered(command=" ".join(args)).raise_if_errors()
    assert os.path.exists("spotfinder.pickle")
    with open("spotfinder.pickle", "rb") as f:
        reflections = pickle.load(f)
        assert len(reflections) == 653, len(reflections)
        assert "shoebox" not in reflections
    assert os.path.exists("my_hot_mask_0.pickle")
    with open("my_hot_mask_0.pickle", "rb") as f:
        mask = pickle.load(f)
        assert len(mask) == 1, len(mask)
        assert mask[0].count(False) == 12, mask[0].count(False)
    print 'OK'

    # now with more generous parameters
    args = [
        "dials.find_spots", "min_spot_size=3", "max_separation=3",
        ' '.join(template), "output.reflections=spotfinder.pickle"
    ]
    result = easy_run.fully_buffered(command=" ".join(args)).raise_if_errors()
    assert os.path.exists("spotfinder.pickle")
    with open("spotfinder.pickle", "rb") as f:
        reflections = pickle.load(f)
        assert len(reflections) == 678, len(reflections)
    print 'OK'

    # Now with a user defined mask
    template = glob(os.path.join(data_dir, "centroid*.cbf"))
    args = [
        "dials.find_spots", ' '.join(template),
        "output.reflections=spotfinder.pickle", "output.shoeboxes=True",
        "lookup.mask=%s" % os.path.join(data_dir, "mask.pickle")
    ]
    result = easy_run.fully_buffered(command=" ".join(args)).raise_if_errors()
    assert os.path.exists("spotfinder.pickle")
    with open("spotfinder.pickle", "rb") as f:
        reflections = pickle.load(f)
        from dxtbx.datablock import DataBlockFactory
        datablocks = DataBlockFactory.from_json_file(
            os.path.join(data_dir, "datablock.json"))
        assert (len(datablocks) == 1)
        imageset = datablocks[0].extract_imagesets()[0]
        detector = imageset.get_detector()
        beam = imageset.get_beam()
        for x, y, z in reflections['xyzobs.px.value']:
            d = detector[0].get_resolution_at_pixel(beam.get_s0(), (x, y))
            assert (d >= 3)

    # Now with a user defined mask
    template = glob(os.path.join(data_dir, "centroid*.cbf"))
    args = [
        "dials.find_spots", ' '.join(template),
        "output.reflections=spotfinder.pickle", "output.shoeboxes=True",
        "region_of_interest=800,1200,800,1200"
    ]
    result = easy_run.fully_buffered(command=" ".join(args)).raise_if_errors()
    assert os.path.exists("spotfinder.pickle")
    with open("spotfinder.pickle", "rb") as f:
        reflections = pickle.load(f)
        x, y, z = reflections['xyzobs.px.value'].parts()
        assert x.all_ge(800)
        assert y.all_ge(800)
        assert x.all_lt(1200)
        assert y.all_lt(1200)

    print 'OK'

    # now with XFEL stills
    data_dir = libtbx.env.find_in_repositories(
        relative_path="dials_regression/spotfinding_test_data",
        test=os.path.isdir)
    template = os.path.join(data_dir, "idx-s00-20131106040302615.cbf")
    args = [
        "dials.find_spots", template, "output.reflections=spotfinder.pickle"
    ]
    result = easy_run.fully_buffered(command=" ".join(args)).raise_if_errors()
    assert os.path.exists("spotfinder.pickle")
    with open("spotfinder.pickle", "rb") as f:
        reflections = pickle.load(f)
        assert len(reflections) == 2643, len(reflections)
    print 'OK'
 def as_remark_200(self, wavelength=None):
     from libtbx.test_utils import approx_equal
     synchrotron = wl = "NULL"
     if (wavelength is not None):
         out = StringIO()
         # XXX somewhat risky...
         if (not approx_equal(wavelength, 1.5418, eps=0.01, out=out) and
                 not approx_equal(wavelength, 0.7107, eps=0.01, out=out)):
             synchrotron = "Y"
         else:
             synchrotron = "N"
         wl = "%.4f" % wavelength
     lines = []
     lines.append("")
     lines.append("EXPERIMENTAL DETAILS")
     lines.append(" EXPERIMENT TYPE                : X-RAY DIFFRACTION")
     lines.append(" DATE OF DATA COLLECTION        : NULL")
     lines.append(" TEMPERATURE           (KELVIN) : NULL")
     lines.append(" PH                             : NULL")
     lines.append(" NUMBER OF CRYSTALS USED        : NULL")
     lines.append("")
     lines.append(" SYNCHROTRON              (Y/N) : NULL")
     lines.append(" RADIATION SOURCE               : NULL")
     lines.append(" BEAMLINE                       : NULL")
     lines.append(" X-RAY GENERATOR MODEL          : NULL")
     lines.append(" MONOCHROMATIC OR LAUE    (M/L) : M")
     lines.append(" WAVELENGTH OR RANGE        (A) : %s" % wl)
     lines.append(" MONOCHROMATOR                  : NULL")
     lines.append(" OPTICS                         : NULL")
     lines.append("")
     lines.append(" DETECTOR TYPE                  : NULL")
     lines.append(" DETECTOR MANUFACTURER          : NULL")
     lines.append(" INTENSITY-INTEGRATION SOFTWARE : NULL")
     lines.append(" DATA SCALING SOFTWARE          : NULL")
     lines.append("")
     lines.append("OVERALL.")
     comp_overall = format_value("%.1f", self.overall.completeness * 100)
     mult_overall = format_value("%.1f", self.overall.mean_redundancy)
     rmerg_overall = format_value("%.5f", self.overall.r_merge)
     s2n_overall = format_value("%.4f", self.overall.i_over_sigma_mean)
     lines.append(" COMPLETENESS FOR RANGE     (%%) : %s" % comp_overall)
     lines.append(" DATA REDUNDANCY                : %s" % mult_overall)
     lines.append(" R MERGE                    (I) : %s" % rmerg_overall)
     lines.append(" R SYM                      (I) : NULL")
     lines.append(" <I/SIGMA(I)> FOR THE DATA SET  : %s" % s2n_overall)
     lines.append("")
     lines.append("IN THE HIGHEST RESOLUTION SHELL.")
     bin_stats = self.bins[-1]
     d_max = format_value("%.2f", bin_stats.d_max)
     d_min = format_value("%.2f", bin_stats.d_min)
     comp_lastbin = format_value("%.1f", bin_stats.completeness * 100)
     mult_lastbin = format_value("%.1f", bin_stats.mean_redundancy)
     rmerg_lastbin = format_value("%.5f", bin_stats.r_merge)
     s2n_lastbin = format_value("%.4f", bin_stats.i_over_sigma_mean)
     lines.append(" HIGHEST RESOLUTION SHELL, RANGE HIGH (A) : %s" % d_min)
     lines.append(" HIGHEST RESOLUTION SHELL, RANGE LOW  (A) : %s" % d_max)
     lines.append(" COMPLETENESS FOR SHELL     (%%) : %s" % comp_lastbin)
     lines.append(" DATA REDUNDANCY IN SHELL       : %s" % mult_lastbin)
     lines.append(" R MERGE FOR SHELL          (I) : %s" % rmerg_lastbin)
     lines.append(" R SYM FOR SHELL            (I) : NULL")
     lines.append(" <I/SIGMA(I)> FOR SHELL         : %s" % s2n_lastbin)
     lines.append("")
     remark_lines = ["REMARK 200 %s" % line for line in lines]
     return "\n".join(remark_lines)
Example #38
0
  def _is_hbond(self, item, fsc0):
    """
    Check if a nonbonded proxy is a H bond

    Parameters:
      item: list item of nonbonded_list
      fsc0: shell_sym_table
    """
    is_hbond = False

    i_seq          = item[1]
    j_seq          = item[2]
    model_distance = item[3]
    vdw_sum        = item[4]
    symop_str      = item[5]
    symop          = item[6]

    is_candidate = hbond.precheck(
      atoms = self.atoms,
      i = i_seq,
      j = j_seq,
      Hs = self.Hs,
      As = self.As,
      Ds = self.Ds,
      fsc0 = fsc0)

    if (not is_candidate):
      return is_hbond

    crystal_symmetry = self.model.crystal_symmetry()
    rt_mx_ji = None
    if symop is not None:
      rt_mx_ji = sgtbx.rt_mx(str(symop))
    #
    D, H, A, Y, atom_A, atom_H, atom_D = hbond.get_D_H_A_Y(
      i        = i_seq,
      j        = j_seq,
      Hs       = self.Hs,
      fsc0     = fsc0,
      rt_mx_ji = rt_mx_ji,
      fm       = crystal_symmetry.unit_cell().fractionalization_matrix(),
      om       = crystal_symmetry.unit_cell().orthogonalization_matrix(),
      atoms    = self.atoms)

    d_HA = A.distance(H)
    # something went wrong if the distances are not equal
    assert approx_equal(d_HA, model_distance, eps=0.1)
    d_DA = D.distance(A)
    a_DHA = H.angle(A, D, deg=True)

    # Values from Steiner, Angew. Chem. Int. Ed. 2002, 41, 48-76, Table 2
    # Modification: minimum angle is 110, not 90
    # TODO: do we want to adapt to acceptor element?
    # TODO: h_a_y angle could be interesting, too
#    if ((d_HA >= 1.2 and d_HA <= 2.2) and
#        (d_DA  >= 2.2 and d_DA <= 3.2) and
#        (a_DHA >= 110)):
    if ((d_HA >= self.d_HA_cutoff[0] and d_HA <= self.d_HA_cutoff[1]) and
        (d_DA  >= self.d_DA_cutoff[0] and d_DA <= self.d_DA_cutoff[1]) and
        (a_DHA >= self.a_DHA_cutoff)):
      is_hbond = True

      self._hbonds.add_hbond(
        hbond_tuple = (D.i_seq, H.i_seq, A.i_seq),
        hbond_info  = [d_HA, d_DA, a_DHA, symop_str, symop, vdw_sum])
      # TODO: if several atom_x, use the first one found
      #  (show shortest or both)
      #break

    return is_hbond
Example #39
0
def exercise(pdb_str, eps, idealize):
    mon_lib_srv = monomer_library.server.server()
    ener_lib = monomer_library.server.ener_lib()
    processed_pdb_file = monomer_library.pdb_interpretation.process(
        mon_lib_srv=mon_lib_srv,
        ener_lib=ener_lib,
        raw_records=pdb_str,
        force_symmetry=True)
    pdb_hierarchy = processed_pdb_file.all_chain_proxies.pdb_hierarchy
    xray_structure = processed_pdb_file.xray_structure()
    sites_cart = xray_structure.sites_cart()
    geometry = processed_pdb_file.geometry_restraints_manager(
        show_energies=False, plain_pairs_radius=5.0)

    es = geometry.energies_sites(sites_cart=sites_cart, compute_gradients=True)
    g_analytical = es.gradients
    #
    riding_h_manager = riding.manager(pdb_hierarchy=pdb_hierarchy,
                                      geometry_restraints=geometry)

    h_parameterization = riding_h_manager.h_parameterization

    riding_h_manager.idealize_hydrogens_inplace(pdb_hierarchy=pdb_hierarchy,
                                                xray_structure=xray_structure)
    #sites_cart = pdb_hierarchy.atoms().extract_xyz()
    sites_cart = xray_structure.sites_cart()

    #for i in g_analytical:
    #  print i
    #print '----------'
    g_analytical = geometry.energies_sites(sites_cart=sites_cart,
                                           compute_gradients=True).gradients
    modify_gradients.modify_gradients(sites_cart=sites_cart,
                                      h_parameterization=h_parameterization,
                                      grads=g_analytical)
    #
    ex = [eps, 0, 0]
    ey = [0, eps, 0]
    ez = [0, 0, eps]
    g_fd = flex.vec3_double()
    for i_site in xrange(sites_cart.size()):
        g_fd_i = []
        for e in [ex, ey, ez]:
            ts = []
            for sign in [-1, 1]:
                sites_cart_ = sites_cart.deep_copy()
                xray_structure_ = xray_structure.deep_copy_scatterers()
                sites_cart_[i_site] = [
                    sites_cart_[i_site][j] + e[j] * sign for j in xrange(3)
                ]
                xray_structure_.set_sites_cart(sites_cart_)
                # after shift, recalculate H position
                riding_h_manager.idealize_hydrogens_inplace(
                    xray_structure=xray_structure_)
                sites_cart_ = xray_structure_.sites_cart()
                ts.append(
                    geometry.energies_sites(sites_cart=sites_cart_,
                                            compute_gradients=False).target)
            g_fd_i.append((ts[1] - ts[0]) / (2 * eps))
        g_fd.append(g_fd_i)

    for g1, g2 in zip(g_analytical, g_fd):
        #print g1,g2
        assert approx_equal(g1, g2, 1.e-4)
Example #40
0
def run3(prefix):
    file_name = os.path.join(qr_unit_tests_data, "h_altconf_2.pdb")

    s_b_W1_A_str = "altloc A or resseq 95:97"
    s_f_W1_A_str = "resseq 87:93 or altloc A or resseq 95:97"

    s_b_W1_B_str = "altloc B or resseq 95:97"
    s_f_W1_B_str = "resseq 87:93 or altloc B or resseq 95:97"

    s_b_W1_AB_str = "resseq 95:97"
    s_f_W1_AB_str = "resseq 87:93 or resseq 95:97"

    s_b_W2_str = "altloc B or resseq 91:93"
    s_f_W2_str = "resseq 95:99 or altloc B or resseq 91:93"

    s_b_A_str = "resseq 90:93 or resseq 95:96"
    s_f_A_str = "altloc A or resseq 90:93 or resseq 95:96"

    s_b_B_str = "resseq 90:93 or resseq 95:96"
    s_f_B_str = "altloc B or resseq 90:93 or resseq 95:96"

    g, junk1, junk2 = get_grads(file_name=file_name,
                                sel_f_str="all",
                                sel_buffer_str="not all")
    gA_f, s_A_f, s_A_b = get_grads(file_name=file_name,
                                   sel_f_str=s_f_A_str,
                                   sel_buffer_str=s_b_A_str)
    gB_f, s_B_f, s_B_b = get_grads(file_name=file_name,
                                   sel_f_str=s_f_B_str,
                                   sel_buffer_str=s_b_B_str)
    gW1_A_f, s_W1_A_f, s_W1_A_b = get_grads(file_name=file_name,
                                            sel_f_str=s_f_W1_A_str,
                                            sel_buffer_str=s_b_W1_A_str)
    gW1_B_f, s_W1_B_f, s_W1_B_b = get_grads(file_name=file_name,
                                            sel_f_str=s_f_W1_B_str,
                                            sel_buffer_str=s_b_W1_B_str)
    gW1_AB_f, s_W1_AB_f, s_W1_AB_b = get_grads(file_name=file_name,
                                               sel_f_str=s_f_W1_AB_str,
                                               sel_buffer_str=s_b_W1_AB_str)
    gW2_f, s_W2_f, s_W2_b = get_grads(file_name=file_name,
                                      sel_f_str=s_f_W2_str,
                                      sel_buffer_str=s_b_W2_str)

    g = flex.vec3_double(g.size())
    g_W1_A = g.set_selected(s_W1_A_f, gW1_A_f)
    g_W1_A = g_W1_A.set_selected(s_W1_A_b, [0, 0, 0])

    g = flex.vec3_double(g.size())
    g_W1_B = g.set_selected(s_W1_B_f, gW1_B_f)
    g_W1_B = g_W1_B.set_selected(s_W1_B_b, [0, 0, 0])

    g = flex.vec3_double(g.size())
    g_W1_AB = g.set_selected(s_W1_AB_f, gW1_AB_f)
    g_W1_AB = g_W1_AB.set_selected(s_W1_AB_b, [0, 0, 0])

    g = flex.vec3_double(g.size())
    g_W2 = g.set_selected(s_W2_f, gW2_f)
    g_W2 = g_W2.set_selected(s_W2_b, [0, 0, 0])

    g = flex.vec3_double(g.size())
    g_A = g.set_selected(s_A_f, gA_f)
    g_A = g_A.set_selected(s_A_b, [0, 0, 0])

    g = flex.vec3_double(g.size())
    g_B = g.set_selected(s_B_f, gB_f)
    g_B = g_B.set_selected(s_B_b, [0, 0, 0])

    g1, junk1, junk2 = get_grads(file_name=file_name,
                                 sel_f_str="all",
                                 sel_buffer_str="not all")
    g2 = g_A + g_B + g_W1_A + g_W1_B + g_W2 - g_W1_AB

    diff = g1 - g2
    assert approx_equal(diff.max(), [0, 0, 0])
    if (0):
        print diff.max()
        for d in diff:
            print d
def exercise_3():
    #test torsion restraints
    for use_reference in ['True', 'False', 'top_out', 'None']:
        pdb_inp = iotbx.pdb.input(lines=flex.std_string(
            pdb_str_2.splitlines()),
                                  source_info=None)
        model = manager(model_input=pdb_inp, log=null_out())
        grm = model.get_restraints_manager().geometry
        xrs2 = model.get_xray_structure()
        awl2 = model.get_hierarchy().atoms_with_labels()
        pdb2 = model.get_hierarchy()
        pdb_inp3 = iotbx.pdb.input(source_info=None, lines=pdb_str_3)
        xrs3 = pdb_inp3.xray_structure_simple()
        ph3 = pdb_inp3.construct_hierarchy()
        ph3.atoms().reset_i_seq()
        awl3 = ph3.atoms_with_labels()
        sites_cart_reference = flex.vec3_double()
        selection = flex.size_t()
        min_selection = flex.size_t()
        reference_names = [
            "N", "CA", "CB", "CG", "CD", "NE", "CZ", "NH1", "NH2"
        ]
        minimize_names = ["CG", "CD", "NE", "CZ", "NH1", "NH2"]
        for a2, a3 in zip(tuple(awl2), tuple(awl3)):
            assert a2.resname == a3.resname
            assert a2.name == a3.name
            assert a2.i_seq == a3.i_seq
            if (a2.resname == "ARG" and a2.name.strip() in reference_names):
                selection.append(a2.i_seq)
                sites_cart_reference.append(a3.xyz)
                if a2.name.strip() in minimize_names:
                    min_selection.append(a2.i_seq)
        assert selection.size() == len(reference_names)
        selection_bool = flex.bool(xrs2.scatterers().size(), min_selection)
        if (use_reference == 'True'):
            grm.add_chi_torsion_restraints_in_place(
                pdb_hierarchy=pdb2,
                sites_cart=sites_cart_reference,
                selection=selection,
                sigma=2.5)
        elif (use_reference == 'top_out'):
            grm.add_chi_torsion_restraints_in_place(
                pdb_hierarchy=pdb2,
                sites_cart=sites_cart_reference,
                selection=selection,
                sigma=2.5,
                limit=180.0,
                top_out_potential=True)
        elif (use_reference == 'None'):
            grm.add_chi_torsion_restraints_in_place(
                pdb_hierarchy=pdb2,
                sites_cart=sites_cart_reference,
                selection=selection,
                sigma=2.5)
            grm.remove_chi_torsion_restraints_in_place(selection=selection)
        d1 = flex.mean(
            flex.sqrt((xrs2.sites_cart().select(min_selection) -
                       xrs3.sites_cart().select(min_selection)).dot()))
        print "distance start (use_reference: %s): %6.4f" % (
            str(use_reference), d1)
        assert d1 > 4.0
        assert approx_equal(
            flex.max(
                flex.sqrt((xrs2.sites_cart().select(~selection_bool) -
                           xrs3.sites_cart().select(~selection_bool)).dot())),
            0)
        from cctbx import geometry_restraints
        import mmtbx.refinement.geometry_minimization
        import scitbx.lbfgs
        grf = geometry_restraints.flags.flags(default=True)
        grf.nonbonded = False
        sites_cart = xrs2.sites_cart()
        minimized = mmtbx.refinement.geometry_minimization.lbfgs(
            sites_cart=sites_cart,
            correct_special_position_tolerance=1.0,
            geometry_restraints_manager=grm,
            sites_cart_selection=flex.bool(sites_cart.size(), min_selection),
            geometry_restraints_flags=grf,
            lbfgs_termination_params=scitbx.lbfgs.termination_parameters(
                max_iterations=5000))
        xrs2.set_sites_cart(sites_cart=sites_cart)
        d2 = flex.mean(
            flex.sqrt((xrs2.sites_cart().select(min_selection) -
                       xrs3.sites_cart().select(min_selection)).dot()))
        print "distance final (use_reference: %s): %6.4f" % (
            str(use_reference), d2)
        if (use_reference in ['True', 'top_out']): assert d2 < 0.02, d2
        else: assert d2 > 4.0, d2
        assert approx_equal(
            flex.max(
                flex.sqrt((xrs2.sites_cart().select(~selection_bool) -
                           xrs3.sites_cart().select(~selection_bool)).dot())),
            0)
    #test torsion manipulation
    grm.remove_chi_torsion_restraints_in_place()
    grm.remove_chi_torsion_restraints_in_place()
    sites_cart_reference = []
    selections_reference = []
    for model in pdb2.models():
        for chain in model.chains():
            for residue in chain.residues():
                sites_cart_reference.append(residue.atoms().extract_xyz())
                selections_reference.append(residue.atoms().extract_i_seq())

    #one residue at a time (effectively chi angles only)
    for sites_cart, selection in zip(sites_cart_reference,
                                     selections_reference):
        grm.add_chi_torsion_restraints_in_place(pdb_hierarchy=pdb2,
                                                sites_cart=sites_cart,
                                                selection=selection)
    assert grm.get_n_chi_torsion_proixes() == 6
    grm.remove_chi_torsion_restraints_in_place()

    #all sites at once, chi angles only
    sites_cart = xrs2.sites_cart()
    grm.add_chi_torsion_restraints_in_place(pdb_hierarchy=pdb2,
                                            sites_cart=sites_cart,
                                            selection=None,
                                            chi_angles_only=True)
    assert grm.get_n_chi_torsion_proixes() == 6

    #all sites at once, all torsions
    grm.add_chi_torsion_restraints_in_place(pdb_hierarchy=pdb2,
                                            sites_cart=sites_cart,
                                            selection=None,
                                            chi_angles_only=False)
    # grm.get_chi_torsion_proxies().show_sorted(
    #     by_value='residual',
    #     sites_cart=sites_cart,
    #     site_labels=[atom.id_str() for atom in pdb2.atoms()])
    assert grm.get_n_chi_torsion_proixes(
    ) == 12, grm.get_n_chi_torsion_proixes()
def test(dials_regression):
  from scitbx import matrix
  from libtbx.phil import parse
  from libtbx.test_utils import approx_equal
  from scitbx.array_family import flex

  # Get modules to build models and minimiser using PHIL
  from dials.test.algorithms.refinement import setup_geometry
  from dials.test.algorithms.refinement import setup_minimiser

  from dials.algorithms.refinement.parameterisation.crystal_parameters import \
      CrystalOrientationParameterisation, CrystalUnitCellParameterisation

  # Symmetry constrained parameterisation for the unit cell
  from cctbx.uctbx import unit_cell
  from rstbx.symmetry.constraints.parameter_reduction import \
      symmetrize_reduce_enlarge

  DEG2RAD = math.pi/180.0
  RAD2DEG = 180.0/math.pi

  master_phil = parse("""
      include scope dials.test.algorithms.refinement.geometry_phil
      include scope dials.test.algorithms.refinement.minimiser_phil
      """, process_includes=True)

  # make cell more oblique
  args=["a.direction.close_to.sd=5","b.direction.close_to.sd=5","c.direction.close_to.sd=5"]
  models = setup_geometry.Extract(master_phil, cmdline_args = args)
  crystal = models.crystal

  # a hexagonal crystal is a good test case for behaviour of oblique cells
  do_hexagonal = True
  if do_hexagonal:
    from dxtbx.model.experiment_list import ExperimentListFactory
    experiments = ExperimentListFactory.from_json_file(
        os.path.join(dials_regression, "refinement_test_data", "multi_stills", "combined_experiments.json"),
        check_format=False)
    crystal = experiments[0].crystal

  # derive finite difference gradients of various quantities wrt each param
  def check_fd_gradients(parameterisation):
    mp = parameterisation
    p_vals = mp.get_param_vals()
    deltas = [1.e-7 for p in p_vals]
    assert len(deltas) == len(p_vals)
    fd_grad = []

    # get matrix to unset rotations of unit cell vectors
    Ut = matrix.sqr(mp.get_model().get_U()).transpose()

    for i, delta in enumerate(deltas):
      val = p_vals[i]

      p_vals[i] -= delta / 2.
      mp.set_param_vals(p_vals)
      rev_uc = mp.get_model().get_unit_cell().parameters()
      rev_vec = mp.get_model().get_real_space_vectors()
      rev_vec = [Ut * vec for vec in rev_vec]
      rev_B = matrix.sqr(mp.get_model().get_B())
      rev_O = rev_B.transpose().inverse()

      p_vals[i] += delta
      mp.set_param_vals(p_vals)
      fwd_uc = mp.get_model().get_unit_cell().parameters()
      fwd_vec = mp.get_model().get_real_space_vectors()
      fwd_vec = [Ut * vec for vec in fwd_vec]
      fwd_B = matrix.sqr(mp.get_model().get_B())
      fwd_O = fwd_B.transpose().inverse()

      fd_uc = [(f - r) / delta for f,r in zip(fwd_uc, rev_uc)]
      fd_vec = [(f - r) / delta for f,r in zip(fwd_vec, rev_vec)]
      fd_B = (fwd_B - rev_B) / delta
      fd_O = (fwd_O - rev_O) / delta

      fd_grad.append({'da_dp':fd_uc[0],
                      'db_dp':fd_uc[1],
                      'dc_dp':fd_uc[2],
                      'daa_dp':fd_uc[3],
                      'dbb_dp':fd_uc[4],
                      'dcc_dp':fd_uc[5],
                      'davec_dp':fd_vec[0],
                      'dbvec_dp':fd_vec[1],
                      'dcvec_dp':fd_vec[2],
                      'dB_dp':fd_B,
                      'dO_dp':fd_O})

      p_vals[i] = val

    # return to the initial state
    mp.set_param_vals(p_vals)

    return fd_grad

  xlo_param = CrystalOrientationParameterisation(crystal)
  xluc_param = CrystalUnitCellParameterisation(crystal)

  from dials.algorithms.refinement.restraints.restraints import SingleUnitCellTie
  uct = SingleUnitCellTie(xluc_param, [None]*6, [None]*6)

  from scitbx.math import angle_derivative_wrt_vectors

  B = matrix.sqr(crystal.get_B())
  O = (B.transpose()).inverse()
  a, b, c, aa, bb, cc = crystal.get_unit_cell().parameters()
  aa *= DEG2RAD
  bb *= DEG2RAD
  cc *= DEG2RAD
  Ut = matrix.sqr(crystal.get_U()).transpose()
  avec, bvec, cvec = [Ut * vec for vec in crystal.get_real_space_vectors()]

  # calculate d[B^T]/dp
  dB_dp = xluc_param.get_ds_dp()
  dBT_dp = [dB.transpose() for dB in dB_dp]

  # calculate d[O]/dp
  dO_dp = [-O * dBT * O for dBT in dBT_dp]

  # function to get analytical derivative of angles wrt vectors
  def dangle(u, v):
    return [matrix.col(e) for e in angle_derivative_wrt_vectors(u,v)]

  dalpha_db, dalpha_dc = dangle(bvec, cvec)
  dbeta_da, dbeta_dc = dangle(avec, cvec)
  dgamma_da, dgamma_db = dangle(avec, bvec)

  # get all FD derivatives
  fd_grad = check_fd_gradients(xluc_param)

  # look at each parameter
  for i, dO in enumerate(dO_dp):

    #print
    #print "***** PARAMETER {0} *****".format(i)

    #print "dB_dp analytical"
    #print dB_dp[i]
    #print "dB_dp FD"
    #print fd_grad[i]['dB_dp']
    #print

    # dB_dp is good. What about dO_dp?

    #print "O MATRIX"
    #print "dO_dp analytical"
    #print dO.round(6)
    #print "dO_dp FD"
    #print fd_grad[i]['dO_dp'].round(6)
    #print
    assert approx_equal(dO, fd_grad[i]['dO_dp'])

    # extract derivatives of each unit cell vector wrt p
    dav_dp, dbv_dp, dcv_dp = dO.transpose().as_list_of_lists()
    dav_dp = matrix.col(dav_dp)
    dbv_dp = matrix.col(dbv_dp)
    dcv_dp = matrix.col(dcv_dp)

    # check these are correct vs FD
    #print "CELL VECTORS"
    #diff = dav_dp - fd_grad[i]['davec_dp']
    #print 2 * diff.length() / (dav_dp.length() + fd_grad[i]['davec_dp'].length()) * 100
    #print 'davec_dp analytical: {0:.5f} {1:.5f} {2:.5f}'.format(*dav_dp.elems)
    #print 'davec_dp finite diff: {0:.5f} {1:.5f} {2:.5f}'.format(*fd_grad[i]['davec_dp'].elems)
    assert approx_equal(dav_dp, fd_grad[i]['davec_dp'])

    #diff = dbv_dp - fd_grad[i]['dbvec_dp']
    #print 2 * diff.length() / (dbv_dp.length() + fd_grad[i]['dbvec_dp'].length()) * 100
    #print 'dbvec_dp analytical: {0:.5f} {1:.5f} {2:.5f}'.format(*dbv_dp.elems)
    #print 'dbvec_dp finite diff: {0:.5f} {1:.5f} {2:.5f}'.format(*fd_grad[i]['dbvec_dp'].elems)
    assert approx_equal(dbv_dp, fd_grad[i]['dbvec_dp'])

    #diff = dcv_dp - fd_grad[i]['dcvec_dp']
    #print 2 * diff.length() / (dcv_dp.length() + fd_grad[i]['dcvec_dp'].length()) * 100
    #print 'dcvec_dp analytical: {0:.5f} {1:.5f} {2:.5f}'.format(*dcv_dp.elems)
    #print 'dcvec_dp finite diff: {0:.5f} {1:.5f} {2:.5f}'.format(*fd_grad[i]['dcvec_dp'].elems)
    #print
    assert approx_equal(dcv_dp, fd_grad[i]['dcvec_dp'])

    #print "CELL LENGTHS"
    da_dp = 1./a * avec.dot(dav_dp)
    #print "d[a]/dp{2} analytical: {0:.5f} FD: {1:.5f}".format(da_dp, fd_grad[i]['da_dp'], i)
    assert approx_equal(da_dp, fd_grad[i]['da_dp'])

    db_dp = 1./b * bvec.dot(dbv_dp)
    #print "d[b]/dp{2} analytical: {0:.5f} FD: {1:.5f}".format(db_dp, fd_grad[i]['db_dp'], i)
    assert approx_equal(db_dp, fd_grad[i]['db_dp'])

    dc_dp = 1./c * cvec.dot(dcv_dp)
    #print "d[c]/dp{2} analytical: {0:.5f} FD: {1:.5f}".format(dc_dp, fd_grad[i]['dc_dp'], i)
    assert approx_equal(dc_dp, fd_grad[i]['dc_dp'])

    #print
    #print "CELL ANGLES"

    daa_dp = RAD2DEG * (dbv_dp.dot(dalpha_db) + dcv_dp.dot(dalpha_dc))
    dbb_dp = RAD2DEG * (dav_dp.dot(dbeta_da) + dcv_dp.dot(dbeta_dc))
    dcc_dp = RAD2DEG * (dav_dp.dot(dgamma_da) + dbv_dp.dot(dgamma_db))

    #print "d[alpha]/dp{2} analytical: {0:.5f} FD: {1:.5f}".format(daa_dp, fd_grad[i]['daa_dp'], i)
    #print "d[beta]/dp{2} analytical: {0:.5f} FD: {1:.5f}".format(dbb_dp, fd_grad[i]['dbb_dp'], i)
    #print "d[gamma]/dp{2} analytical: {0:.5f} FD: {1:.5f}".format(dcc_dp, fd_grad[i]['dcc_dp'], i)
    assert approx_equal(daa_dp, fd_grad[i]['daa_dp'])
    assert approx_equal(dbb_dp, fd_grad[i]['dbb_dp'])
    assert approx_equal(dcc_dp, fd_grad[i]['dcc_dp'])
Example #43
0
def test_01():

    # Source data

    data_dir = os.path.dirname(os.path.abspath(__file__))
    data_ccp4 = os.path.join(data_dir, 'data', 'non_zero_origin_map.ccp4')
    data_pdb = os.path.join(data_dir, 'data', 'non_zero_origin_model.pdb')
    data_ncs_spec = os.path.join(data_dir, 'data',
                                 'non_zero_origin_ncs_spec.ncs_spec')

    # DataManager

    dm = DataManager(['ncs_spec', 'model', 'real_map', 'phil'])
    dm.set_overwrite(True)

    # Read in map and model and ncs

    map_file = data_ccp4
    dm.process_real_map_file(map_file)
    mm = dm.get_real_map(map_file)

    model_file = data_pdb
    dm.process_model_file(model_file)
    model = dm.get_model(model_file)

    ncs_file = data_ncs_spec
    dm.process_ncs_spec_file(ncs_file)
    ncs = dm.get_ncs_spec(ncs_file)

    ncs_dc = ncs.deep_copy()

    mmmn = match_map_model_ncs()
    mmmn.add_map_manager(mm)
    mmmn.add_model(model)
    mmmn.add_ncs_object(ncs)

    # Save it
    mmmn_dc = mmmn.deep_copy()

    # Test creating mmm from model:
    mmm_from_model = model.as_map_model_manager(create_model_map=False)
    mmm_from_model = model.as_map_model_manager(create_model_map=True,
                                                resolution=5)
    assert mmm_from_model.map_manager() is not None

    # Make sure we can add an ncs object that is either shifted or not
    mmmn_dcdc = mmmn.deep_copy()
    new_mmmn = match_map_model_ncs()
    new_mmmn.add_map_manager(mmmn_dcdc.map_manager())
    new_mmmn.add_model(mmmn_dcdc.model())
    new_mmmn.add_ncs_object(mmmn_dcdc.ncs_object())
    assert new_mmmn.ncs_object().shift_cart() == new_mmmn.map_manager(
    ).shift_cart()

    mmmn_dcdc = mmmn.deep_copy()
    new_mmmn = match_map_model_ncs()
    new_mmmn.add_map_manager(mmmn_dcdc.map_manager())
    new_mmmn.add_model(mmmn_dcdc.model())
    new_mmmn.add_ncs_object(ncs_dc)
    assert new_mmmn.ncs_object().shift_cart() == new_mmmn.map_manager(
    ).shift_cart()

    original_ncs = mmmn.ncs_object()
    assert approx_equal(
        (24.0528, 11.5833, 20.0004),
        tuple(original_ncs.ncs_groups()[0].translations_orth()[-1]),
        eps=0.1)

    assert tuple(mmmn._map_manager.origin_shift_grid_units) == (0, 0, 0)

    # Shift origin to (0,0,0)
    mmmn = mmmn_dc.deep_copy()  # fresh version of match_map_model_ncs
    mmmn.shift_origin()
    new_ncs = mmmn.ncs_object()
    assert tuple(mmmn._map_manager.origin_shift_grid_units) == (100, 100, 100)

    mmmn.write_model('s.pdb')
    mmmn.write_map('s.mrc')

    shifted_ncs = mmmn.ncs_object()
    assert approx_equal(
        (-153.758, -74.044, -127.487),
        tuple(shifted_ncs.ncs_groups()[0].translations_orth()[-1]),
        eps=0.1)

    # Shift a model and shift it back

    mmmn = mmmn_dc.deep_copy()  # fresh version of match_map_model_ncs
    model = mmmn.model()
    shifted_model = mmmn.shift_model_to_match_working_map(model=model)
    model_in_original_position = mmmn.shift_model_to_match_original_map(
        model=shifted_model)
    assert (approx_equal(
        model.get_sites_cart(),  # not a copy
        shifted_model.get_sites_cart()))
    assert approx_equal(model.get_sites_cart(),
                        model_in_original_position.get_sites_cart())

    # test data_manager map_model_manager
    generated_mmm = dm.get_map_model_manager()
    print(generated_mmm)
    assert (isinstance(generated_mmm, map_model_manager))

    # Generate a map and model

    import sys
    mmm = map_model_manager(log=sys.stdout)
    mmm.generate_map()
    model = mmm.model()
    mm = mmm.map_manager()
    assert approx_equal(model.get_sites_cart()[0], (14.476, 10.57, 8.34),
                        eps=0.01)
    assert approx_equal(mm.map_data()[10, 10, 10], -0.0506, eps=0.001)
    # Save it
    mmm_dc = mmm.deep_copy()

    # Create model from sites
    mmm_sites = mmm_dc.deep_copy()
    from scitbx.array_family import flex
    sites_cart = flex.vec3_double()
    sites_cart.append((3, 4, 5))
    mmm_sites.model_from_sites_cart(sites_cart=sites_cart,
                                    model_id='new_model')
    assert mmm_sites.get_model_by_id('new_model').get_sites_cart()[0] == (3, 4,
                                                                          5)
    ph_sites = mmm_sites.get_model_by_id('new_model').get_hierarchy()
    text_sites = mmm_sites.get_model_by_id('new_model').model_as_pdb()

    # Create model from hierarchy
    mmm_sites = mmm_dc.deep_copy()
    mmm_sites.model_from_hierarchy(hierarchy=ph_sites, model_id='new_model')
    assert mmm_sites.get_model_by_id('new_model').get_sites_cart()[0] == (3, 4,
                                                                          5)

    # Create model from text
    mmm_sites = mmm_dc.deep_copy()
    mmm_sites.model_from_text(text=text_sites, model_id='new_model')
    assert mmm_sites.get_model_by_id('new_model').get_sites_cart()[0] == (3, 4,
                                                                          5)

    # Set crystal_symmetry and unit_cell_crystal_symmetry and shift_cart
    # Box and shift the map_model_manager so we have new coordinate system
    mmm_sites.box_all_maps_around_model_and_shift_origin(box_cushion=4.5)
    new_model = mmm_sites.get_model_by_id('new_model')
    assert approx_equal(
        (3., 4., 5.0),
        mmm_sites.get_model_by_id('new_model').get_sites_cart()[0])

    # arbitrarily set unit_cell crystal symmetry of model to
    #  match crystal_symmetry. First have to set shift_cart to None
    new_model.set_shift_cart(shift_cart=None)
    new_model.set_unit_cell_crystal_symmetry_and_shift_cart()
    assert new_model.crystal_symmetry() != mmm_sites.crystal_symmetry()

    # now set crystal symmetries and shift cart of model to match the manager
    mmm_sites.set_model_symmetries_and_shift_cart_to_match_map(new_model)
    assert new_model.crystal_symmetry().is_similar_symmetry(
        mmm_sites.crystal_symmetry())
    assert new_model.unit_cell_crystal_symmetry().is_similar_symmetry(
        mmm_sites.unit_cell_crystal_symmetry())
    assert new_model.shift_cart() == mmm_sites.shift_cart()

    # Import hierarchy into a model and set symmetries and shift to match
    mmm_sites.model_from_hierarchy(hierarchy=mmm_sites.model().get_hierarchy(),
                                   model_id='model_from_hierarchy')
    assert mmm_sites.get_model_by_id('model_from_hierarchy').model_as_pdb() \
       == mmm_sites.get_model_by_id('model').model_as_pdb()

    # Check on wrapping
    assert not mm.wrapping(
    )  # this one should not wrap because it is zero at edges

    # Make a new one with no buffer so it is not zero at edges
    mmm = map_model_manager()
    mmm.generate_map(box_cushion=0)
    mm = mmm.map_manager()
    # check its compatibility with wrapping
    assert mm.is_consistent_with_wrapping()
    mmm.show_summary()

    # now box it
    sel = mmm.model().selection("resseq 221:221")
    new_model = mmm.model().deep_copy().select(sel)
    new_mmm = map_model_manager(model=new_model, map_manager=mm.deep_copy())
    new_mmm.box_all_maps_around_model_and_shift_origin()
    new_mm = new_mmm.map_manager()

    assert not new_mm.wrapping()
    assert not new_mm.is_consistent_with_wrapping()

    # now box it with selection
    new_mmm_1 = map_model_manager(model=mmm.model().deep_copy(),
                                  map_manager=mm.deep_copy())
    new_mmm_1.box_all_maps_around_model_and_shift_origin(
        selection_string="resseq 221:221")
    new_mm_1 = new_mmm_1.map_manager()

    assert not new_mm_1.wrapping()
    assert not new_mm_1.is_consistent_with_wrapping()
    assert new_mm_1.map_data().all() == new_mm.map_data().all()

    # create map_model_manager with just half-maps
    mm1 = mm.deep_copy()
    mm2 = mm.deep_copy()
    map_data = mm2.map_data()
    map_data += 1.
    new_mmm = map_model_manager(model=mmm.model().deep_copy(),
                                map_manager_1=mm1,
                                map_manager_2=mm2)
    assert new_mmm._map_dict.get(
        'map_manager') is None  # should not be any yet
    assert approx_equal(new_mmm.map_manager().map_data()[232],
                        mm.deep_copy().map_data()[232] + 0.5)
    assert new_mmm._map_dict.get(
        'map_manager') is not None  # now should be there

    # generate map data from a model
    mm1 = mm.deep_copy()
    mm2 = mm.deep_copy()
    new_mmm = map_model_manager(model=mmm.model().deep_copy(), map_manager=mm1)
    mmm.generate_map(model=mmm.model())
    mm = mmm.map_manager()
    mmm.show_summary()

    # check get_map_model_manager function
    dm = DataManager(['model'])
    assert not hasattr(dm, 'get_map_model_manager')
    dm = DataManager(['real_map'])
    assert not hasattr(dm, 'get_map_model_manager')
    dm = DataManager(['sequence'])
    assert not hasattr(dm, 'get_map_model_manager')
    dm = DataManager(['model', 'real_map'])
    assert hasattr(dm, 'get_map_model_manager')

    # usage
    dm.get_map_model_manager(model_file=data_pdb, map_files=data_ccp4)
    dm.get_map_model_manager(model_file=data_pdb, map_files=[data_ccp4])
    dm.get_map_model_manager(model_file=data_pdb,
                             map_files=[data_ccp4, data_ccp4, data_ccp4])
    dm.get_map_model_manager(model_file=data_pdb,
                             map_files=data_ccp4,
                             ignore_symmetry_conflicts=True)

    # errors
    try:
        dm.get_map_model_manager(model_file=data_pdb,
                                 map_files=data_ccp4,
                                 from_phil=True)
    except Sorry as e:
        assert 'from_phil is set to True' in str(e)
    try:
        dm.get_map_model_manager(model_file=data_pdb,
                                 map_files=data_ccp4,
                                 abc=123)
    except TypeError as e:
        assert 'unexpected keyword argument' in str(e)
    try:
        dm.get_map_model_manager(model_file=data_pdb,
                                 map_files=[data_ccp4, data_ccp4])
    except Sorry as e:
        assert '1 full map and 2 half maps' in str(e)

    # PHIL
    class test_program(ProgramTemplate):
        master_phil_str = '''
include scope iotbx.map_model_manager.map_model_phil_str
'''

    working_phil_str = '''
  map_model {
    full_map = %s
    half_map = %s
    half_map = s.mrc
    model = %s
  }
''' % (data_ccp4, data_ccp4, data_pdb)

    master_phil = parse(test_program.master_phil_str, process_includes=True)
    working_phil = master_phil.fetch(parse(working_phil_str))
    tp = test_program(dm, working_phil.extract())

    try:
        dm.get_map_model_manager(from_phil=True)
    except Exception as e:
        assert 'ignore_symmetry_conflicts' in str(e)
    try:
        dm.get_map_model_manager(from_phil=True,
                                 ignore_symmetry_conflicts=True)
    except AssertionError:
        pass
def exercise_protein():
    pdb_file = libtbx.env.find_in_repositories(
        relative_path="phenix_regression/pdb/3ifk.pdb", test=op.isfile)
    hkl_file = libtbx.env.find_in_repositories(
        relative_path="phenix_regression/reflection_files/3ifk.mtz",
        test=op.isfile)
    if (pdb_file is None):
        print "phenix_regression not available, skipping."
        return
    args1 = [
        pdb_file,
        "outliers_only=True",
        "output.prefix=tst_molprobity",
        "--pickle",
        "flags.xtriage=True",
    ]
    result = molprobity.run(args=args1, out=null_out()).validation
    out1 = StringIO()
    result.show(out=out1)
    result = loads(dumps(result))
    out2 = StringIO()
    result.show(out=out2)
    assert (result.nqh_flips.n_outliers == 6)
    assert (not "RNA validation" in out2.getvalue())
    assert (out2.getvalue() == out1.getvalue())
    dump("tst_molprobity.pkl", result)
    mc = result.as_multi_criterion_view()
    assert (result.neutron_stats is None)
    mpscore = result.molprobity_score()
    # percentiles
    out4 = StringIO()
    result.show_summary(out=out4, show_percentiles=True)
    assert ("""  Clashscore            =  49.59 (percentile: 0.2)"""
            in out4.getvalue())
    # misc
    assert approx_equal(result.r_work(), 0.237)  # from PDB header
    assert approx_equal(result.r_free(), 0.293)  # from PDB header
    assert approx_equal(result.d_min(), 2.03)  # from PDB header
    assert (result.d_max_min() is None)
    assert approx_equal(result.rms_bonds(), 0.02585)
    assert approx_equal(result.rms_angles(), 2.356740)
    assert approx_equal(result.rama_favored(), 96.47059)
    assert (result.cbeta_outliers() == 10)
    assert approx_equal(result.molprobity_score(), 3.40, eps=0.01)
    summary = result.summarize()
    gui_fields = list(summary.iter_molprobity_gui_fields())
    assert (len(gui_fields) == 6)
    #result.show()
    assert (str(mc.data()[2]) == ' A   5  THR  rota,cb,clash')
    import mmtbx.validation.molprobity
    from iotbx import file_reader
    pdb_in = file_reader.any_file(pdb_file)
    hierarchy = pdb_in.file_object.hierarchy
    result = mmtbx.validation.molprobity.molprobity(pdb_hierarchy=hierarchy)
    out3 = StringIO()
    result.show_summary(out=out3)
    assert """\
  Ramachandran outliers =   1.76 %
                favored =  96.47 %
  Rotamer outliers      =  20.00 %
""" in out3.getvalue()
    # now with data
    args2 = args1 + [hkl_file, "--maps"]
    result, cmdline = molprobity.run(args=args2,
                                     out=null_out(),
                                     return_input_objects=True)
    out = StringIO()
    result.show(out=out)
    stats = result.get_statistics_for_phenix_gui()
    #print stats
    stats = result.get_polygon_statistics([
        "r_work", "r_free", "adp_mean_all", "angle_rmsd", "bond_rmsd",
        "clashscore"
    ])
    #print stats
    assert approx_equal(result.r_work(), 0.2276, eps=0.001)
    assert approx_equal(result.r_free(), 0.2805, eps=0.001)
    assert approx_equal(result.d_min(), 2.0302, eps=0.0001)
    assert approx_equal(result.d_max_min(), [34.546125, 2.0302], eps=0.0001)
    assert approx_equal(result.rms_bonds(), 0.02585)
    assert approx_equal(result.rms_angles(), 2.356740)
    assert approx_equal(result.rama_favored(), 96.47059)
    assert (result.cbeta_outliers() == 10)
    assert approx_equal(result.unit_cell().parameters(),
                        (55.285, 58.851, 67.115, 90, 90, 90))
    assert (str(result.space_group_info()) == "P 21 21 21")
    bins = result.fmodel_statistics_by_resolution()
    assert (len(bins) == 10)
    assert approx_equal(result.atoms_to_observations_ratio(),
                        0.09755,
                        eps=0.0001)
    assert approx_equal(result.b_iso_mean(), 31.11739)
    assert op.isfile("tst_molprobity_maps.mtz")
    bins = result.fmodel_statistics_by_resolution()
    #bins.show()
    bin_plot = result.fmodel_statistics_graph_data()
    lg = bin_plot.format_loggraph()
    # fake fmodel_neutron
    fmodel_neutron = cmdline.fmodel.deep_copy()
    result2 = mmtbx.validation.molprobity.molprobity(
        pdb_hierarchy=cmdline.pdb_hierarchy,
        fmodel=cmdline.fmodel,
        fmodel_neutron=fmodel_neutron,
        geometry_restraints_manager=cmdline.geometry,
        nuclear=True,
        keep_hydrogens=True)
    stats = result2.get_statistics_for_phenix_gui()
    assert ('R-work (neutron)' in [label for (label, stat) in stats])
Example #45
0
def exercise_gaussian_fit():

    # test fitting of a gaussian
    def do_gaussian_fit(scale, mu, sigma):
        start = mu - 6 * sigma
        stop = mu + 6 * sigma
        step = (stop - start) / 1000
        x = flex.double(frange(start, stop, step))
        y = scale * flex.exp(-flex.pow2(x - mu) / (2 * sigma**2))
        fit = curve_fitting.single_gaussian_fit(x, y)
        assert approx_equal(fit.a, scale, 1e-4)
        assert approx_equal(fit.b, mu, eps=1e-4)
        assert approx_equal(fit.c, sigma, eps=1e-4)

    for i in range(10):
        scale = random.random() * 1000
        sigma = (random.random() + 0.0001) * 10
        mu = (-1)**random.randint(0, 1) * random.random() * 1000
        functor = curve_fitting.gaussian(scale, mu, sigma)
        start = mu - 6 * sigma
        stop = mu + 6 * sigma
        step = (stop - start) / 1000
        x = flex.double(frange(start, stop, step))
        fd_grads = finite_differences(functor, x)
        assert approx_equal(functor.partial_derivatives(x), fd_grads, 1e-4)
        do_gaussian_fit(scale, mu, sigma)

    # if we take the log of a gaussian we can fit a parabola
    scale = 123
    mu = 3.2
    sigma = 0.1
    x = flex.double(frange(2, 4, 0.01))
    y = scale * flex.exp(-flex.pow2(x - mu) / (2 * sigma**2))
    # need to be careful to only use values of y > 0
    eps = 1e-15
    x = flex.double([x[i] for i in range(x.size()) if y[i] > eps])
    y = flex.double([y[i] for i in range(y.size()) if y[i] > eps])
    fit = curve_fitting.univariate_polynomial_fit(x, flex.log(y), degree=2)
    c, b, a = fit.params
    assert approx_equal(mu, -b / (2 * a))
    assert approx_equal(sigma * sigma, -1 / (2 * a))

    # test multiple gaussian fits
    gaussians = [
        curve_fitting.gaussian(0.3989538, 3.7499764, 0.7500268),
        curve_fitting.gaussian(0.7978957, 6.0000004, 0.5000078)
    ]
    x = flex.double(frange(0, 10, 0.1))
    y = flex.double(x.size())
    for i in range(len(gaussians)):
        g = gaussians[i]
        scale, mu, sigma = g.a, g.b, g.c
        y += g(x)

    starting_gaussians = [
        curve_fitting.gaussian(1, 4, 1),
        curve_fitting.gaussian(1, 5, 1)
    ]
    fit = curve_fitting.gaussian_fit(x, y, starting_gaussians)
    for g1, g2 in zip(gaussians, fit.gaussians):
        assert approx_equal(g1.a, g2.a, eps=1e-4)
        assert approx_equal(g1.b, g2.b, eps=1e-4)
        assert approx_equal(g1.c, g2.c, eps=1e-4)

    # use example of 5-gaussian fit from here:
    # http://research.stowers-institute.org/efg/R/Statistics/MixturesOfDistributions/index.htm
    gaussians = [
        curve_fitting.gaussian(0.10516252, 23.32727, 2.436638),
        curve_fitting.gaussian(0.46462715, 33.09053, 2.997594),
        curve_fitting.gaussian(0.29827916, 41.27244, 4.274585),
        curve_fitting.gaussian(0.08986616, 51.24468, 5.077521),
        curve_fitting.gaussian(0.04206501, 61.31818, 7.070303)
    ]

    x = flex.double(frange(0, 80, 0.1))
    y = flex.double(x.size())
    for i in range(len(gaussians)):
        g = gaussians[i]
        scale, mu, sigma = g.a, g.b, g.c
        y += g(x)

    termination_params = scitbx.lbfgs.termination_parameters(
        min_iterations=500)
    starting_gaussians = [
        curve_fitting.gaussian(1, 21, 2.1),
        curve_fitting.gaussian(1, 30, 2.8),
        curve_fitting.gaussian(1, 40, 2.2),
        curve_fitting.gaussian(1, 51, 1.2),
        curve_fitting.gaussian(1, 60, 2.3)
    ]
    fit = curve_fitting.gaussian_fit(x,
                                     y,
                                     starting_gaussians,
                                     termination_params=termination_params)
    y_calc = fit.compute_y_calc()
    assert approx_equal(y, y_calc, eps=1e-2)

    have_cma_es = libtbx.env.has_module("cma_es")
    if have_cma_es:
        fit = curve_fitting.cma_es_minimiser(starting_gaussians, x, y)
        y_calc = fit.compute_y_calc()
        assert approx_equal(y, y_calc, eps=5e-2)
def exercise_2():
    for use_reference in [True, False, None]:
        pdb_inp = iotbx.pdb.input(lines=flex.std_string(
            pdb_str_2.splitlines()),
                                  source_info=None)
        model = manager(model_input=pdb_inp, log=null_out())
        grm = model.get_restraints_manager().geometry
        xrs2 = model.get_xray_structure()
        awl2 = model.get_hierarchy().atoms_with_labels()
        pdb_inp3 = iotbx.pdb.input(source_info=None, lines=pdb_str_3)
        xrs3 = pdb_inp3.xray_structure_simple()
        ph3 = pdb_inp3.construct_hierarchy()
        ph3.atoms().reset_i_seq()
        awl3 = ph3.atoms_with_labels()
        sites_cart_reference = flex.vec3_double()
        selection = flex.size_t()
        reference_names = ["CG", "CD", "NE", "CZ", "NH1", "NH2"]
        for a2, a3 in zip(tuple(awl2), tuple(awl3)):
            assert a2.resname == a3.resname
            assert a2.name == a3.name
            assert a2.i_seq == a3.i_seq
            if (a2.resname == "ARG" and a2.name.strip() in reference_names):
                selection.append(a2.i_seq)
                sites_cart_reference.append(a3.xyz)
        assert selection.size() == len(reference_names)
        selection_bool = flex.bool(xrs2.scatterers().size(), selection)
        if (use_reference):
            grm.adopt_reference_coordinate_restraints_in_place(
                reference.add_coordinate_restraints(
                    sites_cart=sites_cart_reference,
                    selection=selection,
                    sigma=0.01))
        elif (use_reference is None):
            grm.adopt_reference_coordinate_restraints_in_place(
                reference.add_coordinate_restraints(
                    sites_cart=sites_cart_reference,
                    selection=selection,
                    sigma=0.01))
            grm.remove_reference_coordinate_restraints_in_place(
                selection=selection)
        d1 = flex.mean(
            flex.sqrt((xrs2.sites_cart().select(selection) -
                       xrs3.sites_cart().select(selection)).dot()))
        print "distance start (use_reference: %s): %6.4f" % (
            str(use_reference), d1)
        assert d1 > 4.0
        assert approx_equal(
            flex.max(
                flex.sqrt((xrs2.sites_cart().select(~selection_bool) -
                           xrs3.sites_cart().select(~selection_bool)).dot())),
            0)
        from cctbx import geometry_restraints
        import mmtbx.refinement.geometry_minimization
        import scitbx.lbfgs
        grf = geometry_restraints.flags.flags(default=True)
        sites_cart = xrs2.sites_cart()
        minimized = mmtbx.refinement.geometry_minimization.lbfgs(
            sites_cart=sites_cart,
            correct_special_position_tolerance=1.0,
            geometry_restraints_manager=grm,
            sites_cart_selection=flex.bool(sites_cart.size(), selection),
            geometry_restraints_flags=grf,
            lbfgs_termination_params=scitbx.lbfgs.termination_parameters(
                max_iterations=5000))
        xrs2.set_sites_cart(sites_cart=sites_cart)
        d2 = flex.mean(
            flex.sqrt((xrs2.sites_cart().select(selection) -
                       xrs3.sites_cart().select(selection)).dot()))
        print "distance final (use_reference: %s): %6.4f" % (
            str(use_reference), d2)
        if (use_reference): assert d2 < 0.005, "failed: %f<0.05" % d2
        else: assert d2 > 4.0, d2
        assert approx_equal(
            flex.max(
                flex.sqrt((xrs2.sites_cart().select(~selection_bool) -
                           xrs3.sites_cart().select(~selection_bool)).dot())),
            0)
def exercise_non_crystallographic_conserving_bonds_and_angles():
    sites_cart, geo = geometry_restraints.manager \
      .construct_non_crystallographic_conserving_bonds_and_angles(
        sites_cart=flex.vec3_double([
          (10.949, 12.815, 15.189),
          (10.405, 13.954, 15.917),
          (10.779, 15.262, 15.227),
          ( 9.916, 16.090, 14.936)]),
        edge_list_bonds=[(0, 1), (1, 2), (2, 3)],
        edge_list_angles=[(0, 2), (1, 3)])
    assert approx_equal(sites_cart, [(6.033, 5.000, 5.253),
                                     (5.489, 6.139, 5.981),
                                     (5.863, 7.447, 5.291),
                                     (5.000, 8.275, 5.000)])
    assert approx_equal(geo.energies_sites(sites_cart=sites_cart).target, 0)
    sites_cart_noise = flex.vec3_double([  # Just to make all residuals unique,
        (6.043, 5.030, 5.233),  # so that the sorted bond list below
        (5.469, 6.119, 5.941),  # has the same order on all platforms.
        (5.893, 7.487, 5.281),
        (5.040, 8.225, 5.020)
    ])
    sio = StringIO()
    geo.show_sorted(sites_cart=sites_cart_noise, f=sio)
    expected_first_part = """\
Bond restraints: 5
Sorted by residual:
bond 2
     3
  ideal  model  delta    sigma   weight residual
  1.231  1.158  0.073 1.00e-01 1.00e+02 5.35e-01
bond 1
     2
  ideal  model  delta    sigma   weight residual
  1.525  1.577 -0.052 1.00e-01 1.00e+02 2.66e-01
bond 1
     3
  ideal  model  delta    sigma   weight residual
  2.401  2.338  0.063 1.41e-01 5.00e+01 1.96e-01
bond 0
     1
  ideal  model  delta    sigma   weight residual
  1.457  1.420  0.037 1.00e-01 1.00e+02 1.37e-01
bond 0
     2
  ideal  model  delta    sigma   weight residual
  2.453  2.462 -0.009 1.41e-01 5.00e+01 3.92e-03

Bond-like restraints: 0
Sorted by residual:

Metal coordination restraints: 0
Sorted by residual:

"""
    assert not show_diff(
        sio.getvalue(), expected_first_part + """\
Nonbonded interactions: 0

""")
    #
    sites_cart, geo = geometry_restraints.manager \
      .construct_non_crystallographic_conserving_bonds_and_angles(
        sites_cart=flex.vec3_double([
          (10.949, 12.815, 15.189),
          (10.405, 13.954, 15.917),
          (10.779, 15.262, 15.227),
          ( 9.916, 16.090, 14.936),
          (10.749, 12.615, 15.389)]),
        edge_list_bonds=[(0, 1), (1, 2), (2, 3)],
        edge_list_angles=[(0, 2), (1, 3)])
    sites_cart_noise.append(sites_cart[-1])
    sio = StringIO()
    geo.show_sorted(sites_cart=sites_cart_noise, f=sio)
    assert not show_diff(
        sio.getvalue(), expected_first_part + """\
Nonbonded interactions: 2
Sorted by model distance:
nonbonded 0
          4
   model   vdw
   0.306 1.200
nonbonded 1
          4
   model   vdw
   1.274 1.200

""")
Example #48
0
def exercise_00(prefix="tst_polder_box"):
    """
  Test for phenix.polder using the compute_box=True option.
  """
    f = open("%s.pdb" % prefix, "w")
    f.write(pdb_str)
    f.close()
    f = open("%s_ligand.pdb" % prefix, "w")
    f.write(pdb_str + pdb_str_ligand)
    f.close()
    cmd = " ".join([
        "phenix.fmodel",
        "%s_ligand.pdb" % prefix, "high_res=2.0", "type=real", "label=f-obs",
        "k_sol=0.4", "b_sol=50",
        "output.file_name=%s.mtz" % prefix,
        "> %s.log" % prefix
    ])
    print(cmd)
    easy_run.call(cmd)
    #
    selection_string = '((resseq 65 and name CG) or (resseq 7 and name OH) or \
    resseq 183 or resseq 95 or (resseq 148 and name CG))'

    cmd = " ".join([
        "phenix.polder",
        "%s.pdb" % prefix,
        "%s.mtz" % prefix, "compute_box=True",
        'solvent_exclusion_mask_selection="%s" ' % selection_string,
        'mask_output=True',
        "> %s.log" % prefix
    ])
    print(cmd)
    easy_run.call(cmd)
    #
    miller_arrays = reflection_file_reader.any_reflection_file(
        file_name="tst_polder_box_polder_map_coeffs.mtz").as_miller_arrays()
    mc_polder, mc_bias_omit, mc_omit = [
        None,
    ] * 3
    for ma in miller_arrays:
        lbl = ma.info().label_string()
        if (lbl == "mFo-DFc_polder,PHImFo-DFc_polder"):
            mc_polder = ma.deep_copy()
        if (lbl == "mFo-DFc_bias_omit,PHImFo-DFc_bias_omit"):
            mc_bias_omit = ma.deep_copy()
        if (lbl == "mFo-DFc_omit,PHImFo-DFc_omit"):
            mc_omit = ma.deep_copy()
    assert [mc_polder, mc_omit].count(None) == 0
    cg = maptbx.crystal_gridding(unit_cell=mc_polder.unit_cell(),
                                 d_min=mc_polder.d_min(),
                                 resolution_factor=0.25,
                                 space_group_info=mc_polder.space_group_info())
    map_polder = get_map(cg=cg, mc=mc_polder)
    map_omit = get_map(cg=cg, mc=mc_omit)
    pdb_hierarchy = iotbx.pdb.input(source_info=None,
                                    lines=pdb_str).construct_hierarchy()
    sel = pdb_hierarchy.atom_selection_cache().selection(
        string=selection_string)
    sites_cart_lig = pdb_hierarchy.atoms().extract_xyz().select(sel)
    sites_frac_lig = mc_polder.unit_cell().fractionalize(sites_cart_lig)
    mp = get_map_stats(map=map_polder, sites_frac=sites_frac_lig)
    mo = get_map_stats(map=map_omit, sites_frac=sites_frac_lig)
    #
    mmm_mp = mp.min_max_mean().as_tuple()
    mmm_o = mo.min_max_mean().as_tuple()
    print("Polder map : %7.3f %7.3f %7.3f" % mmm_mp)
    print("Omit       : %7.3f %7.3f %7.3f" % mmm_o)
    #
    assert approx_equal(mmm_mp, [-1.932, 1.673, -0.021], eps=0.15)
    assert approx_equal(mmm_o, [-0.633, 0.326, -0.035], eps=0.15)
Example #49
0
def run(args,
        crystal_symmetry=None,
        ncs_object=None,
        pdb_hierarchy=None,
        map_data=None,
        mask_data=None,
        half_map_data_list=None,
        half_map_labels_list=None,
        lower_bounds=None,
        upper_bounds=None,
        write_output_files=True,
        log=None):
    h = "phenix.map_box: extract box with model and map around selected atoms"
    if (log is None): log = sys.stdout
    print_statistics.make_header(h, out=log)
    default_message = """\

%s.

Usage:
  phenix.map_box model.pdb map_coefficients.mtz selection="chain A and resseq 1:10"

or

  phenix.map_box map.ccp4 density_select=True

Parameters:""" % h
    if (len(args) == 0 and not pdb_hierarchy):
        print(default_message)
        master_phil.show(prefix="  ")
        return

    # Process inputs ignoring symmetry conflicts just to get the value of
    #   ignore_symmetry_conflicts...

    inputs = mmtbx.utils.process_command_line_args(
        args=args,
        cmd_cs=crystal_symmetry,
        master_params=master_phil,
        suppress_symmetry_related_errors=True)
    params = inputs.params.extract()

    # Now process inputs for real and write a nice error message if necessary.
    try:
        inputs = mmtbx.utils.process_command_line_args(
            args=args,
            cmd_cs=crystal_symmetry,
            master_params=master_phil,
            suppress_symmetry_related_errors=params.ignore_symmetry_conflicts)
    except Exception as e:
        if str(e).find("symmetry mismatch ") > 1:
            raise Sorry(str(e) + "\nTry 'ignore_symmetry_conflicts=True'")
        else:
            raise e

    params = inputs.params.extract()
    master_phil.format(python_object=params).show(out=log)

    # Overwrite params with parameters in call if available
    if lower_bounds:
        params.lower_bounds = lower_bounds
    if upper_bounds:
        params.upper_bounds = upper_bounds

    # PDB file
    if params.pdb_file and not inputs.pdb_file_names and not pdb_hierarchy:
        inputs.pdb_file_names = [params.pdb_file]
    if (len(inputs.pdb_file_names) != 1 and not params.density_select
            and not params.mask_select and not pdb_hierarchy
            and not params.keep_map_size and not params.upper_bounds
            and not params.extract_unique
            and not params.bounds_match_this_file):
        raise Sorry(
            "PDB file is needed unless extract_unique, " +
            "density_select, mask_select, keep_map_size \nor bounds are set .")
    if (len(inputs.pdb_file_names)!=1 and not pdb_hierarchy and \
         (params.mask_atoms )):
        raise Sorry("PDB file is needed for mask_atoms")
    if params.soft_mask and (not params.resolution) and \
          (len(inputs.pdb_file_names)!=1 and not pdb_hierarchy):
        raise Sorry("Need resolution for soft_mask without PDB file")
    if ((params.density_select or params.mask_select)
            and params.keep_map_size):
        raise Sorry(
            "Cannot set both density_select/mask_select and keep_map_size")
    if ((params.density_select or params.mask_select) and params.upper_bounds):
        raise Sorry("Cannot set both density_select/mask_select and bounds")
    if (params.keep_map_size and params.upper_bounds):
        raise Sorry("Cannot set both keep_map_size and bounds")
    if (params.upper_bounds and not params.lower_bounds):
        raise Sorry("Please set lower_bounds if you set upper_bounds")
    if (params.extract_unique):
        if (not params.resolution):
            raise Sorry("Please set resolution for extract_unique")
        if (not params.symmetry) and (not params.symmetry_file) and \
            (not ncs_object):
            raise Sorry(
                "Please supply a symmetry file or symmetry for extract_unique (you "
                +
                "\ncan try symmetry=ALL if you do not know your symmetry or " +
                "symmetry=C1 if \nthere is none)")
            from mmtbx.ncs.ncs import ncs
            ncs_object = ncs()
            ncs_object.set_unit_ncs()

    if params.keep_input_unit_cell_and_grid and (
        (params.output_unit_cell_grid is not None) or
        (params.output_unit_cell is not None)):
        raise Sorry("If you set keep_input_unit_cell_and_grid then you cannot "+\
           "set \noutput_unit_cell_grid or output_unit_cell")

    if (write_output_files) and ("mtz" in params.output_format) and (
        (params.keep_origin) and (not params.keep_map_size)):
        print("\nNOTE: Skipping write of mtz file as keep_origin=True and \n"+\
           "keep_map_size is False\n")
        params.output_format = remove_element(params.output_format,
                                              element='mtz')

    if (write_output_files) and ("mtz" in params.output_format) and (
        (params.extract_unique)):
        print("\nNOTE: Skipping write of mtz file as extract_unique=True\n")
        params.output_format = remove_element(params.output_format,
                                              element='mtz')

    if params.output_origin_match_this_file or params.bounds_match_this_file:
        if params.output_origin_match_this_file:
            fn = params.output_origin_match_this_file
            if params.bounds_match_this_file:
                raise Sorry("Cannot match origin and bounds at same time")
        else:
            fn = params.bounds_match_this_file
        if not params.ccp4_map_file:
            raise Sorry(
                "Need to specify your input file with ccp4_map_file=xxx if you use "
                +
                "output_origin_match_this_file=xxxx or bounds_match_this_file=xxxx"
            )

        af = any_file(fn)
        if (af.file_type == 'ccp4_map'):
            origin = af.file_content.map_data().origin()
            if params.output_origin_match_this_file:
                params.output_origin_grid_units = origin
                print("Origin of (%s,%s,%s) taken from %s" %
                      (origin[0], origin[1], origin[2], fn))
            else:
                all = af.file_content.map_data().all()
                params.lower_bounds = origin
                print("Lower bounds of (%s,%s,%s) taken from %s" %
                      (params.lower_bounds[0], params.lower_bounds[1],
                       params.lower_bounds[2], fn))
                params.upper_bounds = list(
                    col(origin) + col(all) - col((1, 1, 1)))
                print("upper bounds of (%s,%s,%s) taken from %s" %
                      (params.upper_bounds[0], params.upper_bounds[1],
                       params.upper_bounds[2], fn))
                params.bounds_are_absolute = True
        else:
            raise Sorry("Unable to interpret %s as map file" % (fn))

    if params.output_origin_grid_units is not None and params.keep_origin:
        params.keep_origin = False
        print("Setting keep_origin=False as output_origin_grid_units is set")
    print_statistics.make_sub_header("pdb model", out=log)
    if len(inputs.pdb_file_names) > 0:
        pdb_inp = iotbx.pdb.input(file_name=inputs.pdb_file_names[0])
        pdb_hierarchy = pdb_inp.construct_hierarchy()
    if pdb_hierarchy:
        pdb_atoms = pdb_hierarchy.atoms()
        pdb_atoms.reset_i_seq()
    else:
        pdb_hierarchy = None
    # Map or map coefficients
    map_coeff = None
    input_unit_cell_grid = None
    input_unit_cell = None
    input_map_labels = None
    if (not map_data):
        # read first mtz file
        if ((len(inputs.reflection_file_names) > 0)
                or (params.map_coefficients_file is not None)):
            # file in phil takes precedent
            if (params.map_coefficients_file is not None):
                if (len(inputs.reflection_file_names) == 0):
                    inputs.reflection_file_names.append(
                        params.map_coefficients_file)
                else:
                    inputs.reflection_file_names[
                        0] = params.map_coefficients_file
            map_coeff = reflection_file_utils.extract_miller_array_from_file(
                file_name=inputs.reflection_file_names[0],
                label=params.label,
                type="complex",
                log=log)
            if not crystal_symmetry:
                crystal_symmetry = map_coeff.crystal_symmetry()
            fft_map = map_coeff.fft_map(
                resolution_factor=params.resolution_factor)
            fft_map.apply_sigma_scaling()
            map_data = fft_map.real_map_unpadded()
            map_or_map_coeffs_prefix = os.path.basename(
                inputs.reflection_file_names[0][:-4])
        # or read CCP4 map
        elif ((inputs.ccp4_map is not None)
              or (params.ccp4_map_file is not None)):
            print("ZZB", params.ccp4_map_file)
            if (params.ccp4_map_file is not None):
                af = any_file(params.ccp4_map_file)
                print("ZZC", af, dir(af), af.file_type, dir(af.file_content))
                if (af.file_type == 'ccp4_map'):
                    inputs.ccp4_map = af.file_content
                    inputs.ccp4_map_file_name = params.ccp4_map_file
            print_statistics.make_sub_header("CCP4 map", out=log)
            ccp4_map = inputs.ccp4_map
            ccp4_map.show_summary(prefix="  ", out=log)
            if not crystal_symmetry:
                crystal_symmetry = ccp4_map.crystal_symmetry()
            map_data = ccp4_map.map_data()
            input_unit_cell_grid = ccp4_map.unit_cell_grid
            input_unit_cell = ccp4_map.unit_cell().parameters()
            input_map_labels = ccp4_map.get_labels()

            if inputs.ccp4_map_file_name.endswith(".ccp4"):
                map_or_map_coeffs_prefix = os.path.basename(
                    inputs.ccp4_map_file_name[:-5])
            else:
                map_or_map_coeffs_prefix = os.path.basename(
                    inputs.ccp4_map_file_name[:-4])
    else:  # have map_data
        map_or_map_coeffs_prefix = None

    if params.half_map_list and (not half_map_data_list):
        if not params.extract_unique:
            raise Sorry("Can only use half_map_with extract_unique")
        print("Reading half-maps", params.half_map_list)
        half_map_data_list = []
        half_map_labels_list = []
        for fn in params.half_map_list:
            print("Reading half map from %s" % (fn), file=log)
            af = any_file(fn)
            print_statistics.make_sub_header("CCP4 map", out=log)
            h_ccp4_map = af.file_content
            h_ccp4_map.show_summary(prefix="  ", out=log)
            h_map_data = h_ccp4_map.map_data()
            half_map_data_list.append(h_map_data)
            half_map_labels_list.append(h_ccp4_map.get_labels())

    if params.map_scale_factor:
        print("Applying scale factor of %s to map data on read-in" %
              (params.map_scale_factor))
        map_data = map_data * params.map_scale_factor

    if params.output_origin_grid_units is not None:
        origin_to_match = tuple(params.output_origin_grid_units)
    else:
        origin_to_match = None

    if origin_to_match:
        sc = []
        for x, o, a in zip(crystal_symmetry.unit_cell().parameters()[:3],
                           origin_to_match, map_data.all()):
            sc.append(-x * o / a)
        shift_cart_for_origin_to_match = tuple(sc)
    else:
        origin_to_match = None
        shift_cart_for_origin_to_match = None

    if crystal_symmetry and not inputs.crystal_symmetry:
        inputs.crystal_symmetry = crystal_symmetry

    # final check that map_data exists
    if (map_data is None):
        raise Sorry("Map or map coefficients file is needed.")

    if len(inputs.pdb_file_names) > 0:
        output_prefix = os.path.basename(inputs.pdb_file_names[0])[:-4]
    else:
        output_prefix = map_or_map_coeffs_prefix

    if not pdb_hierarchy:  # get an empty hierarchy
        from cctbx.array_family import flex
        pdb_hierarchy = iotbx.pdb.input(
            source_info='', lines=flex.split_lines('')).construct_hierarchy()
    xray_structure = pdb_hierarchy.extract_xray_structure(
        crystal_symmetry=inputs.crystal_symmetry)
    xray_structure.show_summary(f=log)
    #
    if not params.selection: params.selection = "all"
    selection = pdb_hierarchy.atom_selection_cache().selection(
        string=params.selection)
    if selection.size():
        print_statistics.make_sub_header("atom selection", out=log)
        print("Selection string: selection='%s'" % params.selection, file=log)
        print("  selects %d atoms from total %d atoms." %
              (selection.count(True), selection.size()),
              file=log)
    sites_cart_all = xray_structure.sites_cart()
    sites_cart = sites_cart_all.select(selection)
    selection = xray_structure.selection_within(radius=params.selection_radius,
                                                selection=selection)

    if not ncs_object:
        from mmtbx.ncs.ncs import ncs
        ncs_object = ncs()
        if params.symmetry_file:
            ncs_object.read_ncs(params.symmetry_file, log=log)
            print("Total of %s operators read" % (ncs_object.max_operators()),
                  file=log)
    if not ncs_object or ncs_object.max_operators() < 1:
        print("No symmetry available", file=log)
    if ncs_object:
        n_ops = max(1, ncs_object.max_operators())
    else:
        n_ops = 1

    # Get sequence if extract_unique is set
    sequence = None
    if params.extract_unique or params.mask_select:
        if params.sequence_file:
            if n_ops > 1:  # get unique part of sequence
                remove_duplicates = True
            else:
                remove_duplicates = False
            from iotbx.bioinformatics import get_sequences
            sequence = (" ".join(
                get_sequences(file_name=params.sequence_file,
                              remove_duplicates=remove_duplicates)))

        if params.chain_type in ['None', None]: params.chain_type = None
        if sequence and not params.molecular_mass:
            # get molecular mass from sequence
            from iotbx.bioinformatics import text_from_chains_matching_chain_type
            if params.chain_type in [None, 'PROTEIN']:
                n_protein = len(
                    text_from_chains_matching_chain_type(text=sequence,
                                                         chain_type='PROTEIN'))
            else:
                n_protein = 0
            if params.chain_type in [None, 'RNA']:
                n_rna = len(
                    text_from_chains_matching_chain_type(text=sequence,
                                                         chain_type='RNA'))
            else:
                n_rna = 0
            if params.chain_type in [None, 'DNA']:
                n_dna = len(
                    text_from_chains_matching_chain_type(text=sequence,
                                                         chain_type='DNA'))
            else:
                n_dna = 0
            params.molecular_mass = n_ops * (n_protein * 110 +
                                             (n_rna + n_dna) * 330)
            print("\nEstimate of molecular mass is %.0f " %
                  (params.molecular_mass),
                  file=log)
    if params.density_select or params.mask_select:
        print_statistics.make_sub_header(
            "Extracting box around selected density and writing output files",
            out=log)
    else:
        print_statistics.make_sub_header(
            "Extracting box around selected atoms and writing output files",
            out=log)
    #
    if params.value_outside_atoms == 'mean':
        print("\nValue outside atoms mask will be set to mean inside mask",
              file=log)
    if params.get_half_height_width and params.density_select:
        print("\nHalf width at half height will be used to id boundaries",
              file=log)

    if params.soft_mask and sites_cart_all.size() > 0:
        print("\nSoft mask will be applied to model-based mask", file=log)
    elif params.soft_mask:
        print("\nSoft mask will be applied to outside of map box", file=log)
    if params.keep_map_size:
        print("\nEntire map will be kept (not cutting out region)", file=log)
    if params.restrict_map_size:
        print("\nOutput map will be within input map", file=log)
    if params.lower_bounds and params.upper_bounds:
        print("Bounds for cut out map are (%s,%s,%s) to (%s,%s,%s)" %
              (tuple(list(params.lower_bounds) + list(params.upper_bounds))),
              file=log)

    if mask_data:
        mask_data = mask_data.as_double()
    box = mmtbx.utils.extract_box_around_model_and_map(
        xray_structure=xray_structure,
        map_data=map_data.as_double(),
        mask_data=mask_data,
        box_cushion=params.box_cushion,
        selection=selection,
        mask_select=params.mask_select,
        density_select=params.density_select,
        threshold=params.density_select_threshold,
        get_half_height_width=params.get_half_height_width,
        mask_atoms=params.mask_atoms,
        soft_mask=params.soft_mask,
        soft_mask_radius=params.soft_mask_radius,
        mask_atoms_atom_radius=params.mask_atoms_atom_radius,
        value_outside_atoms=params.value_outside_atoms,
        keep_map_size=params.keep_map_size,
        restrict_map_size=params.restrict_map_size,
        lower_bounds=params.lower_bounds,
        upper_bounds=params.upper_bounds,
        bounds_are_absolute=params.bounds_are_absolute,
        zero_outside_original_map=params.zero_outside_original_map,
        extract_unique=params.extract_unique,
        target_ncs_au_file=params.target_ncs_au_file,
        regions_to_keep=params.regions_to_keep,
        box_buffer=params.box_buffer,
        soft_mask_extract_unique=params.soft_mask_extract_unique,
        mask_expand_ratio=params.mask_expand_ratio,
        keep_low_density=params.keep_low_density,
        chain_type=params.chain_type,
        sequence=sequence,
        solvent_content=params.solvent_content,
        molecular_mass=params.molecular_mass,
        resolution=params.resolution,
        ncs_object=ncs_object,
        symmetry=params.symmetry,
        half_map_data_list=half_map_data_list,
    )

    ph_box = pdb_hierarchy.select(selection)
    ph_box.adopt_xray_structure(box.xray_structure_box)
    box.hierarchy = ph_box

    if params.mask_select:
        print("\nSolvent content used in mask_select: %.3f " %
              (box.get_solvent_content()),
              file=log)
    if (inputs and inputs.crystal_symmetry and inputs.ccp4_map
            and inputs.crystal_symmetry.unit_cell().parameters()
            and inputs.ccp4_map.unit_cell().parameters()) and (
                inputs.crystal_symmetry.unit_cell().parameters() !=
                inputs.ccp4_map.unit_cell().parameters()):
        print("\nNOTE: Input CCP4 map is only part of unit cell:", file=log)
        print("Full unit cell ('unit cell parameters'): "+\
          "(%.1f, %.1f, %.1f, %.1f, %.1f, %.1f) A" %tuple(
            inputs.ccp4_map.unit_cell().parameters()), file=log)
        print("Size of CCP4 map 'map unit cell':        "+\
          "(%.1f, %.1f, %.1f, %.1f, %.1f, %.1f) A" %tuple(
           inputs.crystal_symmetry.unit_cell().parameters()), file=log)
        print("Full unit cell as grid units: (%s, %s, %s)" %
              (inputs.ccp4_map.unit_cell_grid),
              file=log)
        print("Map unit cell as grid units:  (%s, %s, %s)" % (map_data.all()),
              file=log)

        box.unit_cell_parameters_from_ccp4_map = inputs.ccp4_map.unit_cell(
        ).parameters()
        box.unit_cell_parameters_deduced_from_map_grid=\
           inputs.crystal_symmetry.unit_cell().parameters()

    else:
        box.unit_cell_parameters_from_ccp4_map = None
        box.unit_cell_parameters_deduced_from_map_grid = None

    if box.pdb_outside_box_msg:
        print(box.pdb_outside_box_msg, file=log)

    # NOTE: box object is always shifted to place origin at (0,0,0)

    # NOTE ON ORIGIN SHIFTS:  The shifts are applied locally here. The box
    #  object is not affected and always has the origin at (0,0,0)
    #  output_box is copy of box with shift_cart corresponding to the output
    #   files. Normally this is the same as the original shift_cart. However
    #   if user has specified a new output origin it will differ.

    # For output files ONLY:
    #  keep_origin==False leave origin at (0,0,0)
    #  keep_origin==True: we shift everything back to where it was,
    #  output_origin_grid_units=10,10,10: output origin is at (10,10,10)

    # ncs_object is original
    #  box.ncs_object is shifted by shift_cart
    #  output_box.ncs_object is shifted back by -new shift_cart

    # Additional note on output unit_cell and grid_units.
    # The ccp4-style output map can specify the unit cell and grid units
    #  corresponding to that cell.  This can be separate from the origin and
    #  number of grid points in the map as written.  If specified, write these
    #  out to the output ccp4 map and also use this unit cell for writing
    #  any output PDB files

    from copy import deepcopy
    output_box = deepcopy(box)  # won't use box below here except to return it

    print("\nBox cell dimensions: (%.2f, %.2f, %.2f) A" %
          (box.box_crystal_symmetry.unit_cell().parameters()[:3]),
          file=log)

    if box.shift_cart:
        print("Working origin moved from grid position of"+\
           ": (%d, %d, %d) to (0,0,0) " %(
           tuple(box.origin_shift_grid_units(reverse=True))), file=log)
        print("Working origin moved from  coordinates of:"+\
           " (%.2f, %.2f, %.2f) A to (0,0,0)\n" %(
            tuple(-col(box.shift_cart))), file=log)

    if (params.keep_origin):
        print("\nRestoring original position for output files", file=log)
        print("Origin will be at grid position of"+\
            ": (%d, %d, %d) " %(
            tuple(box.origin_shift_grid_units(reverse=True))), file=log)
        print("\nOutput files will be in same location as original",
              end=' ',
              file=log)
        if not params.keep_map_size:
            print("just cut out.", file=log)
        else:
            print("keeping entire map", file=log)
        print("Note that output maps are only valid in the cut out region.\n",
              file=log)

    else:
        if origin_to_match:
            output_box.shift_cart = shift_cart_for_origin_to_match
            if params.output_origin_grid_units:
                print("Output map origin to be shifted to match target",
                      file=log)
            print("Placing origin at grid point (%s, %s, %s)" %(
              origin_to_match)+"\n"+ \
              "Final coordinate shift for output files: (%.2f,%.2f,%.2f) A\n" %(
              tuple(col(output_box.shift_cart)-col(box.shift_cart))), file=log)
        elif box.shift_cart:
            output_box.shift_cart = (0, 0, 0)  # not shifting back
            print("Final origin will be at (0,0,0)", file=log)
            print(
                "Final coordinate shift for output files: (%.2f,%.2f,%.2f) A\n"
                % (tuple(col(output_box.shift_cart) - col(box.shift_cart))),
                file=log)
        else:
            print("\nOutput files are in same location as original and origin "+\
              "is at (0,0,0)\n", file=log)

    print("\nBox grid: (%s, %s, %s) " % (output_box.map_box.all()), file=log)
    ph_output_box_output_location = ph_box.deep_copy()
    if output_box.shift_cart:  # shift coordinates and NCS back by shift_cart
        # NOTE output_box.shift_cart could be different than box.shift_cart if
        #  there is a target position for the origin and it is not the same as the
        #  original origin.
        sites_cart = output_box.shift_sites_cart_back(
            output_box.xray_structure_box.sites_cart())
        xrs_offset = ph_output_box_output_location.extract_xray_structure(
            crystal_symmetry=output_box.xray_structure_box.crystal_symmetry(
            )).replace_sites_cart(new_sites=sites_cart)
        ph_output_box_output_location.adopt_xray_structure(xrs_offset)

        if output_box.ncs_object:
            output_box.ncs_object = output_box.ncs_object.coordinate_offset(
                tuple(-col(output_box.shift_cart)))
        shift_back = True
    else:
        shift_back = False

    if params.keep_input_unit_cell_and_grid and \
         (input_unit_cell_grid is not None) and \
         (input_unit_cell is not None):
        params.output_unit_cell = input_unit_cell
        params.output_unit_cell_grid = input_unit_cell_grid
        print("Setting output unit cell parameters and unit cell grid to"+\
          " match\ninput map file", file=log)

    if params.output_unit_cell:  # Set output unit cell parameters
        from cctbx import crystal
        output_crystal_symmetry = crystal.symmetry(
            unit_cell=params.output_unit_cell, space_group="P1")
        output_unit_cell = output_crystal_symmetry.unit_cell()
        print("Output unit cell set to: %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)" %
              tuple(output_crystal_symmetry.unit_cell().parameters()),
              file=log)
    else:
        output_crystal_symmetry = None

    # =============  Check/set output unit cell grid and cell parameters =======
    if params.output_unit_cell_grid or output_crystal_symmetry:
        if params.output_unit_cell_grid:
            output_unit_cell_grid = params.output_unit_cell_grid
        else:
            output_unit_cell_grid = output_box.map_box.all()
        print("Output unit cell grid set to: (%s, %s, %s)" %
              tuple(output_unit_cell_grid),
              file=log)

        expected_output_abc = []
        box_spacing = []
        output_spacing = []
        box_abc=output_box.xray_structure_box.\
             crystal_symmetry().unit_cell().parameters()[:3]
        if output_crystal_symmetry:
            output_abc = output_crystal_symmetry.unit_cell().parameters()[:3]
        else:
            output_abc = [None, None, None]
        for a_box, a_output, n_box, n_output in zip(box_abc, output_abc,
                                                    output_box.map_box.all(),
                                                    output_unit_cell_grid):
            expected_output_abc.append(a_box * n_output / n_box)
            box_spacing.append(a_box / n_box)
            if output_crystal_symmetry:
                output_spacing.append(a_output / n_output)
            else:
                output_spacing.append(a_box / n_box)

        if output_crystal_symmetry:  # make sure it is compatible...
            r0 = expected_output_abc[0] / output_abc[0]
            r1 = expected_output_abc[1] / output_abc[1]
            r2 = expected_output_abc[2] / output_abc[2]
            from libtbx.test_utils import approx_equal
            if not approx_equal(r0, r1, eps=0.001) or not approx_equal(
                    r0, r2, eps=0.001):
                print("WARNING: output_unit_cell and cell_grid will "+\
                  "change ratio of grid spacing.\nOld spacings: "+\
                 "(%.2f, %.2f, %.2f) A " %(tuple(box_spacing))+\
                "\nNew spacings:  (%.2f, %.2f, %.2f) A \n" %(tuple(output_spacing)), file=log)
        else:
            output_abc = expected_output_abc

        from cctbx import crystal
        output_crystal_symmetry = crystal.symmetry(unit_cell=list(output_abc) +
                                                   [90, 90, 90],
                                                   space_group="P1")
        print(
            "Output unit cell will be:  (%.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n"
            % (tuple(output_crystal_symmetry.unit_cell().parameters())),
            file=log)

    else:
        output_unit_cell_grid = output_box.map_box.all()
        output_crystal_symmetry = output_box.xray_structure_box.crystal_symmetry(
        )
    # ==========  Done check/set output unit cell grid and cell parameters =====

    if write_output_files:
        # Write PDB file
        if ph_box.overall_counts().n_residues > 0:

            if (params.output_file_name_prefix is None):
                file_name = "%s_box.pdb" % output_prefix
            else:
                file_name = "%s.pdb" % params.output_file_name_prefix
            ph_output_box_output_location.write_pdb_file(
                file_name=file_name, crystal_symmetry=output_crystal_symmetry)
            print("Writing boxed PDB with box unit cell to %s" % (file_name),
                  file=log)

        # Write NCS file if NCS
        if output_box.ncs_object and output_box.ncs_object.max_operators() > 0:
            if (params.output_file_name_prefix is None):
                output_symmetry_file = "%s_box.ncs_spec" % output_prefix
            else:
                output_symmetry_file = "%s.ncs_spec" % params.output_file_name_prefix
            output_box.ncs_object.format_all_for_group_specification(
                file_name=output_symmetry_file)

            print("\nWriting symmetry to %s" % (output_symmetry_file),
                  file=log)

        # Write ccp4 map.
        if ("ccp4" in params.output_format):
            if (params.output_file_name_prefix is None):
                file_name = "%s_box.ccp4" % output_prefix
            else:
                file_name = "%s.ccp4" % params.output_file_name_prefix
            from iotbx.mrcfile import create_output_labels
            if params.extract_unique:
                program_name = 'map_box using extract_unique'
                limitations = ["extract_unique"]
            else:
                program_name = 'map_box'
                limitations = []
            labels = create_output_labels(
                program_name=program_name,
                input_file_name=inputs.ccp4_map_file_name,
                input_labels=input_map_labels,
                limitations=limitations,
                output_labels=params.output_map_labels)
            output_box.write_ccp4_map(
                file_name=file_name,
                output_crystal_symmetry=output_crystal_symmetry,
                output_mean=params.output_ccp4_map_mean,
                output_sd=params.output_ccp4_map_sd,
                output_unit_cell_grid=output_unit_cell_grid,
                shift_back=shift_back,
                output_map_labels=labels,
                output_external_origin=params.output_external_origin)
            print("Writing boxed map "+\
                 "to CCP4 formatted file:   %s"%file_name, file=log)
            if not params.half_map_list:
                params.half_map_list = []
            if not output_box.map_box_half_map_list:
                output_box.map_box_half_map_list = []
            if not half_map_labels_list:
                half_map_labels_list = len(
                    output_box.map_box_half_map_list) * [None]
            for hm, labels, fn in zip(
                    output_box.map_box_half_map_list, half_map_labels_list,
                    params.half_map_list):  # half maps matching
                labels = create_output_labels(
                    program_name=program_name,
                    input_file_name=fn,
                    input_labels=labels,
                    limitations=limitations,
                    output_labels=params.output_map_labels)
                hm_fn = "%s_box.ccp4" % (".".join(
                    os.path.basename(fn).split(".")[:-1]))
                output_box.write_ccp4_map(
                    file_name=hm_fn,
                    map_data=hm,
                    output_crystal_symmetry=output_crystal_symmetry,
                    output_mean=params.output_ccp4_map_mean,
                    output_sd=params.output_ccp4_map_sd,
                    output_unit_cell_grid=output_unit_cell_grid,
                    shift_back=shift_back,
                    output_map_labels=labels,
                    output_external_origin=params.output_external_origin)
                print("Writing boxed half map to: %s " % (hm_fn), file=log)

        # Write xplor map.  Shift back if keep_origin=True
        if ("xplor" in params.output_format):
            if (params.output_file_name_prefix is None):
                file_name = "%s_box.xplor" % output_prefix
            else:
                file_name = "%s.xplor" % params.output_file_name_prefix
            output_box.write_xplor_map(
                file_name=file_name,
                output_crystal_symmetry=output_crystal_symmetry,
                output_unit_cell_grid=output_unit_cell_grid,
                shift_back=shift_back,
            )
            print("Writing boxed map "+\
                "to X-plor formatted file: %s"%file_name, file=log)

        # Write mtz map coeffs.  Shift back if keep_origin=True
        if ("mtz" in params.output_format):
            if (params.output_file_name_prefix is None):
                file_name = "%s_box.mtz" % output_prefix
            else:
                file_name = "%s.mtz" % params.output_file_name_prefix

            print("Writing map coefficients "+\
                "to MTZ file: %s"%file_name, file=log)
            if (map_coeff is not None):
                d_min = map_coeff.d_min()
            elif params.resolution is not None:
                d_min = params.resolution
            else:
                d_min = maptbx.d_min_from_map(
                    map_data=output_box.map_box,
                    unit_cell=output_box.xray_structure_box.unit_cell())
            output_box.map_coefficients(
                d_min=d_min,
                scale_max=params.scale_max,
                resolution_factor=params.resolution_factor,
                file_name=file_name,
                shift_back=shift_back)

    print(file=log)
    return box
Example #50
0
def exercise_header_misc(regression_pdb):
    pdb_inp = pdb.input(file_name=op.join(regression_pdb, "pdb1a1q.ent"))
    wavelength = pdb_inp.extract_wavelength()
    assert approx_equal(wavelength, 0.995)
    expt_type = pdb_inp.get_experiment_type()
    assert (expt_type == 'X-RAY DIFFRACTION')
Example #51
0
def exercise_2():
    hkl_file = libtbx.env.find_in_repositories(
        relative_path="phenix_regression/wizards/data/p9_se_w2.sca",
        test=os.path.isfile)
    if (hkl_file is None):
        warnings.warn("phenix_regression not available, skipping test")
        return
    hkl_in = file_reader.any_file(hkl_file).assert_file_type("hkl")
    i_obs_raw = hkl_in.file_object.as_miller_arrays(
        merge_equivalents=False,
        crystal_symmetry=crystal.symmetry(space_group_symbol="I4",
                                          unit_cell=(113.949, 113.949, 32.474,
                                                     90, 90, 90)))[0]
    i_obs = i_obs_raw.merge_equivalents().array()
    # completeness and data strength
    cstats = ds.i_sigi_completeness_stats(i_obs)
    d_min_cut = cstats.resolution_cut
    assert approx_equal(d_min_cut, 2.150815)
    ws = ds.wilson_scaling(miller_array=i_obs, n_residues=120)
    # outliers - this shouldn't actually work, since it requires additional
    # processing steps on the input data
    try:
        outliers = ds.possible_outliers(i_obs)
    except AssertionError:
        pass
    else:
        raise Exception_expected
    ######################################################################
    # OVERALL ANALYSIS
    pdb_file = libtbx.env.find_in_repositories(
        relative_path="phenix_examples/p9-build/p9.pdb", test=os.path.isfile)
    f_calc = None
    if (pdb_file is not None):
        pdb_in = file_reader.any_file(pdb_file).assert_file_type("pdb")
        hierarchy = pdb_in.file_object.hierarchy
        xrs = pdb_in.file_object.xray_structure_simple(crystal_symmetry=i_obs)
        f_calc = xrs.structure_factors(d_min=i_obs.d_min()).f_calc()
        f_calc = abs(f_calc).generate_bijvoet_mates()
        f_calc = f_calc.set_observation_type_xray_amplitude()
        i_obs, f_calc = i_obs.common_sets(other=f_calc)
        open("tmp_xtriage.pdb",
             "w").write(hierarchy.as_pdb_string(crystal_symmetry=i_obs))
        pdb_file = "tmp_xtriage.pdb"
    params = xtriage.master_params.extract()
    params.scaling.input.asu_contents.n_residues = 141
    result = xtriage.xtriage_analyses(miller_obs=i_obs,
                                      miller_calc=f_calc,
                                      params=params,
                                      unmerged_obs=i_obs_raw,
                                      text_out=open("logfile3.log",
                                                    "w"))  #sys.stdout)
    # XXX there appears to be some system-dependence here, hence sloppy limits
    assert (15.5 < result.aniso_b_min < 15.9)
    assert (10 < result.aniso_range_of_b < 11)
    # check relative Wilson
    if (pdb_file is not None):
        assert (result.relative_wilson is not None)
        # FIXME
        #assert (result.relative_wilson.n_outliers() == 34)
    #show_pickled_object_sizes(result)
    test_pickle_consistency_and_size(result)
    # XXX PDB validation server
    assert approx_equal(result.iso_b_wilson, 18.33, eps=0.1)
    assert approx_equal(result.aniso_b_ratio, 0.546, eps=0.1)
    assert (result.number_of_wilson_outliers == 0)
    assert approx_equal(result.l_test_mean_l, 0.493, eps=0.1)
    assert approx_equal(result.l_test_mean_l_squared, 0.326, eps=0.1)
    assert approx_equal(result.i_over_sigma_outer_shell, 3.25, eps=0.1)
    assert ("No significant pseudotranslation is detected"
            in result.patterson_verdict)
    # test consistency of output after pickling and unpickling
    try:
        from phenix_dev.phenix_cloud import xtriage_json
    except ImportError:
        pass
    else:
        json_out = xtriage_json.json_output("p9.sca")
        result.show(out=json_out)
        open("xtriage.json", "w").write(json_out.export())
    # unmerged data
    assert result.merging_stats is not None
    out = StringIO()
    result.merging_stats.show(out=out)
    assert ("R-merge: 0.073" in out.getvalue())
    assert approx_equal(result.estimate_d_min(min_i_over_sigma=10),
                        1.9645,
                        eps=0.001)
    # FIXME PDB doesn't actually have unit cell!
    # test detection of symmetry in reference file
    if (pdb_file is not None):
        args = [hkl_file, pdb_file]
        result = xtriage.run(args=args, out=null_out())
Example #52
0
def test2():
    """Run scan-varying refinement, comparing RMSD table with expected values.
  This test automates what was manually done periodically and recorded in
  dials_regression/refinement_test_data/centroid/README.txt"""

    dials_regression = libtbx.env.find_in_repositories(
        relative_path="dials_regression", test=os.path.isdir)

    # use the i04_weak_data for this test
    data_dir = os.path.join(dials_regression, "refinement_test_data",
                            "centroid")
    experiments_path = os.path.join(data_dir,
                                    "experiments_XPARM_REGULARIZED.json")
    pickle_path = os.path.join(data_dir, "spot_all_xds.pickle")

    for pth in (experiments_path, pickle_path):
        assert os.path.exists(pth)

    # scan-static refinement first to get refined_experiments.json as start point
    cmd1 = ("dials.refine " + experiments_path + " " + pickle_path +
            " reflections_per_degree=50 "
            " outlier.algorithm=null close_to_spindle_cutoff=0.05")
    cmd2 = ("dials.refine refined_experiments.json " + pickle_path +
            " scan_varying=true output.history=history.pickle"
            " reflections_per_degree=50"
            " outlier.algorithm=null close_to_spindle_cutoff=0.05"
            " crystal.orientation.smoother.interval_width_degrees=36.0"
            " crystal.unit_cell.smoother.interval_width_degrees=36.0")

    # work in a temporary directory
    cwd = os.path.abspath(os.curdir)
    tmp_dir = open_tmp_directory(suffix="test_dials_refine")
    os.chdir(tmp_dir)
    try:
        print cmd1
        result1 = easy_run.fully_buffered(command=cmd1).raise_if_errors()
        print cmd2
        result2 = easy_run.fully_buffered(command=cmd2).raise_if_errors()
        # load and check results
        import cPickle as pickle
        history = pickle.load(open("history.pickle", "r"))

        expected_rmsds = [(0.088488398, 0.114583571, 0.001460382),
                          (0.080489334, 0.086406517, 0.001284069),
                          (0.078835086, 0.086052630, 0.001195882),
                          (0.077476911, 0.086194611, 0.001161143),
                          (0.076755840, 0.086090630, 0.001157239),
                          (0.076586376, 0.085939462, 0.001155641),
                          (0.076603722, 0.085878953, 0.001155065),
                          (0.076611382, 0.085862959, 0.001154863),
                          (0.076608732, 0.085856935, 0.001154384),
                          (0.076605731, 0.085852271, 0.001153858),
                          (0.076604576, 0.085852318, 0.001153643),
                          (0.076603981, 0.085854175, 0.001153594)]

        assert approx_equal(history['rmsd'], expected_rmsds)

        # check that the used_in_refinement flag got set correctly
        rt = flex.reflection_table.from_pickle('refined.pickle')
        uir = rt.get_flags(rt.flags.used_in_refinement)
        assert uir.count(True) == history['num_reflections'][-1]

    finally:
        os.chdir(cwd)

    print "OK"
    return
mydetector = models.detector
mygonio = models.goniometer
mycrystal = models.crystal
mybeam = models.beam

# Build a mock scan for a 180 degree sweep
sf = ScanFactory()
myscan = sf.make_scan(image_range=(1, 1800),
                      exposure_times=0.1,
                      oscillation=(0, 0.1),
                      epochs=range(1800),
                      deg=True)
sweep_range = myscan.get_oscillation_range(deg=False)
im_width = myscan.get_oscillation(deg=False)[1]
assert sweep_range == (0., pi)
assert approx_equal(im_width, 0.1 * pi / 180.)

# Build an experiment list
experiments = ExperimentList()
experiments.append(
    Experiment(beam=mybeam,
               detector=mydetector,
               goniometer=mygonio,
               scan=myscan,
               crystal=mycrystal,
               imageset=None))

###########################
# Parameterise the models #
###########################
Example #54
0
def exercise_crystal_model():

    real_space_a = matrix.col((10, 0, 0))
    real_space_b = matrix.col((0, 11, 0))
    real_space_c = matrix.col((0, 0, 12))
    model = crystal_model(real_space_a=(10, 0, 0),
                          real_space_b=(0, 11, 0),
                          real_space_c=(0, 0, 12),
                          space_group_symbol="P 1")
    assert isinstance(model.get_unit_cell(), uctbx.unit_cell)
    assert model.get_unit_cell().parameters() == (10.0, 11.0, 12.0, 90.0, 90.0,
                                                  90.0)
    assert approx_equal(model.get_A(),
                        (1 / 10, 0, 0, 0, 1 / 11, 0, 0, 0, 1 / 12))
    assert approx_equal(model.get_A().inverse(),
                        (10, 0, 0, 0, 11, 0, 0, 0, 12))
    assert approx_equal(model.get_B(), model.get_A())
    assert approx_equal(model.get_U(), (1, 0, 0, 0, 1, 0, 0, 0, 1))
    assert approx_equal(model.get_real_space_vectors(),
                        (real_space_a, real_space_b, real_space_c))
    assert approx_equal(model.get_mosaicity(), 0)

    model2 = crystal_model(real_space_a=(10, 0, 0),
                           real_space_b=(0, 11, 0),
                           real_space_c=(0, 0, 12),
                           space_group_symbol="P 1")
    assert model == model2
    model2.set_mosaicity(0.01)
    assert model != model2
    # rotate 45 degrees about x-axis
    R1 = matrix.sqr(
        (1, 0, 0, 0, math.cos(math.pi / 4), -math.sin(math.pi / 4), 0,
         math.sin(math.pi / 4), math.cos(math.pi / 4)))
    # rotate 30 degrees about y-axis
    R2 = matrix.sqr((math.cos(math.pi / 6), 0, math.sin(math.pi / 6), 0, 1, 0,
                     -math.sin(math.pi / 6), 0, math.cos(math.pi / 6)))
    # rotate 60 degrees about z-axis
    R3 = matrix.sqr((math.cos(math.pi / 3), -math.sin(math.pi / 3), 0,
                     math.sin(math.pi / 3), math.cos(math.pi / 3), 0, 0, 0, 1))
    R = R1 * R2 * R3
    model.set_U(R)
    # B is unchanged
    assert approx_equal(model.get_B(),
                        (1 / 10, 0, 0, 0, 1 / 11, 0, 0, 0, 1 / 12))
    assert approx_equal(model.get_U(), R)
    assert approx_equal(model.get_A(), model.get_U() * model.get_B())
    a_, b_, c_ = model.get_real_space_vectors()
    assert approx_equal(a_, R * real_space_a)
    assert approx_equal(b_, R * real_space_b)
    assert approx_equal(c_, R * real_space_c)
    s = StringIO()
    print >> s, model
    assert not show_diff(
        s.getvalue().replace("-0.0000", " 0.0000"), """\
Crystal:
    Unit cell: (10.000, 11.000, 12.000, 90.000, 90.000, 90.000)
    Space group: P 1
    U matrix:  {{ 0.4330, -0.7500,  0.5000},
                { 0.7891,  0.0474, -0.6124},
                { 0.4356,  0.6597,  0.6124}}
    B matrix:  {{ 0.1000,  0.0000,  0.0000},
                { 0.0000,  0.0909,  0.0000},
                { 0.0000,  0.0000,  0.0833}}
    A = UB:    {{ 0.0433, -0.0682,  0.0417},
                { 0.0789,  0.0043, -0.0510},
                { 0.0436,  0.0600,  0.0510}}

""")
    model.set_B((1 / 12, 0, 0, 0, 1 / 12, 0, 0, 0, 1 / 12))
    assert approx_equal(model.get_unit_cell().parameters(),
                        (12, 12, 12, 90, 90, 90))

    U = matrix.sqr((0.3455, -0.2589, -0.9020, 0.8914, 0.3909, 0.2293, 0.2933,
                    -0.8833, 0.3658))
    B = matrix.sqr((1 / 13, 0, 0, 0, 1 / 13, 0, 0, 0, 1 / 13))
    model.set_A(U * B)
    assert approx_equal(model.get_A(), U * B)
    assert approx_equal(model.get_U(), U, 1e-4)
    assert approx_equal(model.get_B(), B, 1e-5)

    model3 = crystal_model(real_space_a=(10, 0, 0),
                           real_space_b=(0, 11, 0),
                           real_space_c=(0, 0, 12),
                           space_group=sgtbx.space_group_info("P 222").group(),
                           mosaicity=0.01)
    assert model3.get_space_group().type().hall_symbol() == " P 2 2"
    assert model != model3
    #
    sgi_ref = sgtbx.space_group_info(number=230)
    model_ref = crystal_model(real_space_a=(44, 0, 0),
                              real_space_b=(0, 44, 0),
                              real_space_c=(0, 0, 44),
                              space_group=sgi_ref.group())
    assert approx_equal(model_ref.get_U(), (1, 0, 0, 0, 1, 0, 0, 0, 1))
    assert approx_equal(model_ref.get_B(),
                        (1 / 44, 0, 0, 0, 1 / 44, 0, 0, 0, 1 / 44))
    assert approx_equal(model_ref.get_A(), model_ref.get_B())
    assert approx_equal(model_ref.get_unit_cell().parameters(),
                        (44, 44, 44, 90, 90, 90))
    a_ref, b_ref, c_ref = model_ref.get_real_space_vectors()
    cb_op_to_primitive = sgi_ref.change_of_basis_op_to_primitive_setting()
    model_primitive = model_ref.change_basis(cb_op_to_primitive)
    cb_op_to_reference = model_primitive.get_space_group().info()\
      .change_of_basis_op_to_reference_setting()
    a_prim, b_prim, c_prim = model_primitive.get_real_space_vectors()
    #print cb_op_to_primitive.as_abc()
    ##'-1/2*a+1/2*b+1/2*c,1/2*a-1/2*b+1/2*c,1/2*a+1/2*b-1/2*c'
    assert approx_equal(a_prim, -1 / 2 * a_ref + 1 / 2 * b_ref + 1 / 2 * c_ref)
    assert approx_equal(b_prim, 1 / 2 * a_ref - 1 / 2 * b_ref + 1 / 2 * c_ref)
    assert approx_equal(c_prim, 1 / 2 * a_ref + 1 / 2 * b_ref - 1 / 2 * c_ref)
    #print cb_op_to_reference.as_abc()
    ##b+c,a+c,a+b
    assert approx_equal(a_ref, b_prim + c_prim)
    assert approx_equal(b_ref, a_prim + c_prim)
    assert approx_equal(c_ref, a_prim + b_prim)
    assert approx_equal(model_primitive.get_U(), [
        -0.5773502691896258, 0.40824829046386285, 0.7071067811865476,
        0.5773502691896257, -0.4082482904638631, 0.7071067811865476,
        0.5773502691896257, 0.8164965809277259, 0.0
    ])
    assert approx_equal(model_primitive.get_B(), [
        0.0262431940540739, 0.0, 0.0, 0.00927837023781507, 0.02783511071344521,
        0.0, 0.01607060866333063, 0.01607060866333063, 0.03214121732666125
    ])
    assert approx_equal(
        model_primitive.get_A(),
        (0, 1 / 44, 1 / 44, 1 / 44, 0, 1 / 44, 1 / 44, 1 / 44, 0))
    assert approx_equal(model_primitive.get_unit_cell().parameters(), [
        38.1051177665153, 38.1051177665153, 38.1051177665153,
        109.47122063449069, 109.47122063449069, 109.47122063449069
    ])
    assert model_ref != model_primitive
    model_ref_recycled = model_primitive.change_basis(cb_op_to_reference)
    assert approx_equal(model_ref.get_U(), model_ref_recycled.get_U())
    assert approx_equal(model_ref.get_B(), model_ref_recycled.get_B())
    assert approx_equal(model_ref.get_A(), model_ref_recycled.get_A())
    assert approx_equal(model_ref.get_unit_cell().parameters(),
                        model_ref_recycled.get_unit_cell().parameters())
    assert model_ref == model_ref_recycled
    #
    uc = uctbx.unit_cell(
        (58.2567, 58.1264, 39.7093, 46.9077, 46.8612, 62.1055))
    sg = sgtbx.space_group_info(symbol="P1").group()
    cs = crystal.symmetry(unit_cell=uc, space_group=sg)
    cb_op_to_minimum = cs.change_of_basis_op_to_minimum_cell()
    # the reciprocal matrix
    B = matrix.sqr(uc.fractionalization_matrix()).transpose()
    U = random_rotation()
    direct_matrix = (U * B).inverse()
    model = crystal_model(direct_matrix[:3],
                          direct_matrix[3:6],
                          direct_matrix[6:9],
                          space_group=sg)
    assert uc.is_similar_to(model.get_unit_cell())
    uc_minimum = uc.change_basis(cb_op_to_minimum)
    model_minimum = model.change_basis(cb_op_to_minimum)
    assert uc_minimum.is_similar_to(model_minimum.get_unit_cell())
    assert model_minimum != model
    model_minimum.update(model)
    assert model_minimum == model
    #
    from scitbx.math import euler_angles
    A_static = model.get_A()
    A_as_scan_points = [A_static]
    num_scan_points = 11
    for i in range(num_scan_points - 1):
        A_as_scan_points.append(
            A_as_scan_points[-1] *
            matrix.sqr(euler_angles.xyz_matrix(0.1, 0.2, 0.3)))
    model.set_A_at_scan_points(A_as_scan_points)
    model_minimum = model.change_basis(cb_op_to_minimum)
    assert model.num_scan_points == model_minimum.num_scan_points == num_scan_points
    M = matrix.sqr(cb_op_to_minimum.c_inv().r().transpose().as_double())
    M_inv = M.inverse()
    for i in range(num_scan_points):
        A_orig = model.get_A_at_scan_point(i)
        A_min = model_minimum.get_A_at_scan_point(i)
        assert A_min == A_orig * M_inv
Example #55
0
def exercise_flood_fill():
    uc = uctbx.unit_cell('10 10 10 90 90 90')
    for uc in (uctbx.unit_cell('10 10 10 90 90 90'),
               uctbx.unit_cell('9 10 11 87 91 95')):
        gridding = maptbx.crystal_gridding(unit_cell=uc,
                                           pre_determined_n_real=(5, 5, 5))
        corner_cube = (0, 4, 20, 24, 100, 104, 120, 124
                       )  # cube across all 8 corners
        channel = (12, 37, 38, 39, 42, 43, 62, 63, 67, 68, 87, 112)
        data = flex.int(flex.grid(gridding.n_real()))
        for i in (corner_cube + channel):
            data[i] = 1
        flood_fill = masks.flood_fill(data, uc)
        assert data.count(0) == 105
        for i in corner_cube:
            assert data[i] == 2
        for i in channel:
            assert data[i] == 3
        assert approx_equal(flood_fill.centres_of_mass(),
                            ((-0.5, -0.5, -0.5), (-2.5, 7 / 3, 2.5)))
        assert approx_equal(flood_fill.centres_of_mass_frac(),
                            ((-0.1, -0.1, -0.1), (-0.5, 7 / 15, 0.5)))
        assert approx_equal(
            flood_fill.centres_of_mass_cart(),
            uc.orthogonalize(flood_fill.centres_of_mass_frac()))
        assert flood_fill.n_voids() == 2
        assert approx_equal(flood_fill.grid_points_per_void(), (8, 12))
        if 0:
            from crys3d import wx_map_viewer
            wx_map_viewer.display(raw_map=data.as_double(),
                                  unit_cell=uc,
                                  wires=False)
        #
        gridding = maptbx.crystal_gridding(unit_cell=uc,
                                           pre_determined_n_real=(10, 10, 10))
        data = flex.int(flex.grid(gridding.n_real()))
        # parallelogram
        points = [(2, 4, 5), (3, 4, 5), (4, 4, 5), (5, 4, 5), (6, 4, 5),
                  (3, 5, 5), (4, 5, 5), (5, 5, 5), (6, 5, 5), (7, 5, 5),
                  (4, 6, 5), (5, 6, 5), (6, 6, 5), (7, 6, 5), (8, 6, 5)]
        points_frac = flex.vec3_double()
        for p in points:
            data[p] = 1
            points_frac.append([p[i] / gridding.n_real()[i] for i in range(3)])
        points_cart = uc.orthogonalize(points_frac)
        flood_fill = masks.flood_fill(data, uc)
        assert data.count(2) == 15
        assert approx_equal(flood_fill.centres_of_mass_frac(),
                            ((0.5, 0.5, 0.5), ))
        pai_cart = math.principal_axes_of_inertia(points=points_cart,
                                                  weights=flex.double(
                                                      points_cart.size(), 1.0))
        F = matrix.sqr(uc.fractionalization_matrix())
        O = matrix.sqr(uc.orthogonalization_matrix())
        assert approx_equal(pai_cart.center_of_mass(),
                            flood_fill.centres_of_mass_cart()[0])
        assert approx_equal(
            flood_fill.covariance_matrices_cart()[0],
            (F.transpose() *
             matrix.sym(sym_mat3=flood_fill.covariance_matrices_frac()[0]) *
             F).as_sym_mat3())
        assert approx_equal(pai_cart.inertia_tensor(),
                            flood_fill.inertia_tensors_cart()[0])
        assert approx_equal(pai_cart.eigensystem().vectors(),
                            flood_fill.eigensystems_cart()[0].vectors())
        assert approx_equal(pai_cart.eigensystem().values(),
                            flood_fill.eigensystems_cart()[0].values())
    return
Example #56
0
def exercise_1():
    pdb_raw = """\
CRYST1   23.000    6.666   25.000  90.00 107.08  90.00 P 1 21 1      2
ATOM      1  N   GLY A   1      -9.009   4.612   6.102  1.00 16.77           N
ATOM      2  CA  GLY A   1      -9.052   4.207   4.651  1.00 16.57           C
ATOM      3  C   GLY A   1      -8.015   3.140   4.419  1.00 16.16           C
ATOM      4  O   GLY A   1      -7.523   2.521   5.381  1.00 16.78           O
ATOM      5  N   ASN A   2      -7.656   2.923   3.155  1.00 15.02           N
ATOM      6  CA  ASN A   2      -6.522   2.038   2.831  1.00 14.10           C
ATOM      7  C   ASN A   2      -5.241   2.537   3.427  1.00 13.13           C
ATOM      8  O   ASN A   2      -4.978   3.742   3.426  1.00 11.91           O
ATOM      9  CB  ASN A   2      -6.346   1.881   1.341  1.00 15.38           C
ATOM     10  CG  ASN A   2      -7.584   1.342   0.692  1.00 14.08           C
ATOM     11  OD1 ASN A   2      -8.025   0.227   1.016  1.00 17.46           O
ATOM     12  ND2 ASN A   2      -8.204   2.155  -0.169  1.00 11.72           N
ATOM     13  N   ASN A   3      -4.438   1.590   3.905  1.00 12.26           N
ATOM     14  CA  ASN A   3      -3.193   1.904   4.589  1.00 11.74           C
ATOM     15  C   ASN A   3      -1.955   1.332   3.895  1.00 11.10           C
ATOM     16  O   ASN A   3      -1.872   0.119   3.648  1.00 10.42           O
ATOM     17  CB  ASN A   3      -3.259   1.378   6.042  1.00 12.15           C
ATOM     18  CG  ASN A   3      -2.006   1.739   6.861  1.00 12.82           C
ATOM     19  OD1 ASN A   3      -1.702   2.925   7.072  1.00 15.05           O
ATOM     20  ND2 ASN A   3      -1.271   0.715   7.306  1.00 13.48           N
ATOM     21  N   MET A   4      -1.005   2.228   3.598  1.00 10.29           N
ATOM     22  CA  MET A   4       0.384   1.888   3.199  1.00 10.53           C
ATOM     23  C   MET A   4       1.435   2.606   4.088  1.00 10.24           C
ATOM     24  O   MET A   4       1.547   3.843   4.115  1.00  8.86           O
ATOM     25  CB  MET A   4       0.616   2.241   1.729  1.00 20.00           C
ATOM     26  CG  MET A   4      -0.207   1.416   0.754  1.00 20.00           C
ATOM     27  SD  MET A   4       0.132  -0.349   0.876  1.00 20.00           S
ATOM     28  CE  MET A   4       1.822  -0.411   0.285  1.00 20.00           C
ATOM     29  N   GLN A   5       2.154   1.821   4.871  1.00 10.38           N
ATOM     30  CA  GLN A   5       3.270   2.361   5.640  1.00 11.39           C
ATOM     31  C   GLN A   5       4.594   1.768   5.172  1.00 11.52           C
ATOM     32  O   GLN A   5       4.768   0.546   5.054  1.00 12.05           O
ATOM     33  CB  GLN A   5       3.056   2.183   7.147  1.00 11.96           C
ATOM     34  CG  GLN A   5       1.829   2.950   7.647  1.00 10.81           C
ATOM     35  CD  GLN A   5       1.344   2.414   8.954  1.00 13.10           C
ATOM     36  OE1 GLN A   5       0.774   1.325   9.002  1.00 10.65           O
ATOM     37  NE2 GLN A   5       1.549   3.187  10.039  1.00 12.30           N
ATOM     38  N   ASN A   6       5.514   2.664   4.856  1.00 11.99           N
ATOM     39  CA  ASN A   6       6.831   2.310   4.318  1.00 12.30           C
ATOM     40  C   ASN A   6       7.854   2.761   5.324  1.00 13.40           C
ATOM     41  O   ASN A   6       8.219   3.943   5.374  1.00 13.92           O
ATOM     42  CB  ASN A   6       7.065   3.016   2.993  1.00 12.13           C
ATOM     43  CG  ASN A   6       5.961   2.735   2.003  1.00 12.77           C
ATOM     44  OD1 ASN A   6       5.798   1.604   1.551  1.00 14.27           O
ATOM     45  ND2 ASN A   6       5.195   3.747   1.679  1.00 10.07           N
ATOM     46  N   TYR A   7       8.292   1.817   6.147  1.00 14.70           N
ATOM     47  CA  TYR A   7       9.159   2.144   7.299  1.00 15.18           C
ATOM     48  C   TYR A   7      10.603   2.331   6.885  1.00 15.91           C
ATOM     49  O   TYR A   7      11.041   1.811   5.855  1.00 15.76           O
ATOM     50  CB  TYR A   7       9.061   1.065   8.369  1.00 15.35           C
ATOM     51  CG  TYR A   7       7.665   0.929   8.902  1.00 14.45           C
ATOM     52  CD1 TYR A   7       6.771   0.021   8.327  1.00 15.68           C
ATOM     53  CD2 TYR A   7       7.210   1.756   9.920  1.00 14.80           C
ATOM     54  CE1 TYR A   7       5.480  -0.094   8.796  1.00 13.46           C
ATOM     55  CE2 TYR A   7       5.904   1.649  10.416  1.00 14.33           C
ATOM     56  CZ  TYR A   7       5.047   0.729   9.831  1.00 15.09           C
ATOM     57  OH  TYR A   7       3.766   0.589  10.291  1.00 14.39           O
ATOM     58  OXT TYR A   7      11.358   2.999   7.612  1.00 17.49           O
TER      59      TYR A   7
HETATM    1 CA    CA A   8      10.431   1.858   3.216  1.00 30.00          CA
HETATM   60  O   HOH A   9      -6.471   5.227   7.124  1.00 22.62           O
HETATM   62  O   HOH A  10     -11.286   1.756  -1.468  1.00 17.08           O
HETATM   63  O   HOH A  11      11.808   4.179   9.970  1.00 23.99           O
HETATM   64  O   HOH A  12      13.605   1.327   9.198  1.00 26.17           O
HETATM   65  O   HOH A  13      -2.749   3.429  10.024  1.00 39.15           O
HETATM   66  O   HOH A  14      -1.500   0.682  10.967  1.00 43.49           O
END
"""
    pdb_file = "tst_xtriage_in.pdb"
    open(pdb_file, "w").write(pdb_raw)
    fmodel_args = [
        pdb_file,
        "high_resolution=1.5",
        "k_sol=0.35",
        "b_sol=20",
        "wavelength=1.54",
        "add_random_error_to_amplitudes_percent=3",
        "random_seed=12345",
        "output.type=real",
        "output.label=F",
        "output.file_name=tst_xtriage_fmodel.mtz",
    ]
    fmodel.run(args=fmodel_args, log=null_out())
    mtz_in = file_reader.any_file("tst_xtriage_fmodel.mtz").assert_file_type(
        "hkl")
    f_obs = mtz_in.file_server.miller_arrays[0].remove_cone(0.1)
    data = f_obs.data()
    # add some outliers
    #data[17] = 20
    #data[334] = 26
    #data[1908] = 13
    # and sigmas
    sigf = flex.double(f_obs.size(), 0.1) + (f_obs.data() * 0.03)
    f_obs = f_obs.customized_copy(sigmas=sigf)
    mtz_file = "tst_xtriage_in.mtz"
    f_obs.as_mtz_dataset(column_root_label="F").mtz_object().write(mtz_file)
    seq_file = "tst_xtriage_in.fa"
    open(seq_file, "w").write("> tst_xtriage\nGNNMQNY")

    # check with completeness_as_non_anomalous=True

    xtriage_args = [
        mtz_file,
        pdb_file,
        seq_file,
        "log=tst_xtriage_1.log",
        "l_test_dhkl=2,2,2",
        "completeness_as_non_anomalous=True",
    ]
    result = xtriage.run(args=xtriage_args, out=null_out())
    test_pickle_consistency_and_size(result)
    assert (result.matthews.n_copies == 1)
    assert (str(result.matthews.table) == """\
Solvent content analysis
Copies             Solvent content    Matthews coeff.    P(solvent content)
1                  0.472              2.33               1.000
""")
    data_strength = result.data_strength_and_completeness
    assert approx_equal(data_strength.data_strength.resolution_cut,
                        1.5351,
                        eps=0.001)
    out1 = data_strength.low_resolution_completeness.format()
    assert (out1 == """\
---------------------------------------------------------
| Resolution range  | N(obs)/N(possible) | Completeness |
---------------------------------------------------------
| 21.9858 - 10.4368 | [6/7]              | 0.857        |
| 10.4368 -  8.4369 | [3/3]              | 1.000        |
|  8.4369 -  7.4172 | [3/4]              | 0.750        |
|  7.4172 -  6.7606 | [4/4]              | 1.000        |
|  6.7606 -  6.2882 | [5/5]              | 1.000        |
|  6.2882 -  5.9252 | [3/4]              | 0.750        |
|  5.9252 -  5.6337 | [7/7]              | 1.000        |
|  5.6337 -  5.3922 | [5/5]              | 1.000        |
|  5.3922 -  5.1874 | [4/4]              | 1.000        |
|  5.1874 -  5.0106 | [4/4]              | 1.000        |
---------------------------------------------------------"""), out1
    # ANOMALOUS SIGNAL
    a_meas = result.anomalous_info.measurability
    assert approx_equal(a_meas.high_d_cut, 4.7636, eps=0.0001)
    assert approx_equal(a_meas.low_d_cut, 2.2357, eps=0.0001)
    # ABSOLUTE SCALING
    ws = result.wilson_scaling
    assert ("%.2f" % ws.iso_p_scale) == "0.66"
    assert ("%.2f" % ws.iso_b_wilson) == "14.51"
    # FIXME these may need to be adjusted for different hardware/OS
    assert approx_equal(ws.aniso_p_scale, 0.66106, eps=0.001)
    assert approx_equal(
        ws.aniso_u_star,
        [0.00034473, 0.00479983, 0.000287162, -0.0, 9.00962e-05, 0.0])
    assert approx_equal(ws.aniso_b_cart,
                        (13.218423, 16.840142, 12.948426, 1.0354e-15,
                         -0.0685311, -7.92862e-16))
    # convenience methods for GUI
    assert approx_equal(result.aniso_b_min, 12.948426)
    assert approx_equal(result.aniso_range_of_b, 3.891716)
    #
    assert approx_equal(
        ws.outlier_shell_table.data[0],  # d_spacing
        [9.865132, 8.369653, 4.863587, 4.648635, 3.126905, 1.729609])
    assert approx_equal(
        ws.outlier_shell_table.data[1],  # z_score
        [5.587749, 15.425036, 4.763399, 6.57819, 4.650204, 4.580195])
    assert (len(ws.outliers.acentric_outliers_table.data[0]) == 2)
    assert (ws.outliers.acentric_outliers_table.data[1] == [(0, -1, -1),
                                                            (0, 1, 1)])
    assert approx_equal(ws.outliers.acentric_outliers_table.data[2],
                        [3.440749, 3.253775])
    assert (ws.outliers.centric_outliers_table.data is None)
    assert (len(ws.ice_rings.table._rows) == 10)
    assert (ws.ice_rings.table._rows[0] ==
            ['    3.897', '     1.000', '   0.52', '   1.00']), \
            ws.ice_rings.table._rows[0]
    tw = result.twin_results
    wm = tw.wilson_moments
    out = StringIO()
    wm.show(out)
    assert not show_diff(
        out.getvalue(), """
                  ----------Wilson ratio and moments----------

Acentric reflections:


   <I^2>/<I>^2    :2.047   (untwinned: 2.000; perfect twin 1.500)
   <F>^2/<F^2>    :0.779   (untwinned: 0.785; perfect twin 0.885)
   <|E^2 - 1|>    :0.743   (untwinned: 0.736; perfect twin 0.541)

Centric reflections:


   <I^2>/<I>^2    :3.043   (untwinned: 3.000; perfect twin 2.000)
   <F>^2/<F^2>    :0.626   (untwinned: 0.637; perfect twin 0.785)
   <|E^2 - 1|>    :0.996   (untwinned: 0.968; perfect twin 0.736)

""")
    # XXX PDB validation server
    assert approx_equal(result.iso_b_wilson, 14.51, eps=0.1)
    assert approx_equal(result.aniso_b_ratio, 0.271, eps=0.1)
    assert (result.number_of_wilson_outliers == 2)
    assert approx_equal(result.l_test_mean_l, 0.481, eps=0.1)
    assert approx_equal(result.l_test_mean_l_squared, 0.322, eps=0.1)
    assert approx_equal(result.i_over_sigma_outer_shell, 10.64, eps=0.01)
    assert ("indicating pseudo-translationa" in result.patterson_verdict)
    # check relative Wilson
    # FIXME
    #result.relative_wilson.show()
    #assert (result.relative_wilson.n_outliers() == 0)
    #show_pickled_object_sizes(result)
    #

    # check with completeness_as_non_anomalous=False

    xtriage_args = [
        mtz_file,
        pdb_file,
        seq_file,
        "log=tst_xtriage_1.log",
        "l_test_dhkl=2,2,2",
        "completeness_as_non_anomalous=False",
    ]
    result = xtriage.run(args=xtriage_args, out=null_out())
    test_pickle_consistency_and_size(result)
    assert (result.matthews.n_copies == 1)
    assert (str(result.matthews.table) == """\
Solvent content analysis
Copies             Solvent content    Matthews coeff.    P(solvent content)
1                  0.472              2.33               1.000
""")
    data_strength = result.data_strength_and_completeness
    assert approx_equal(data_strength.data_strength.resolution_cut,
                        1.5351,
                        eps=0.001)
    out1 = data_strength.low_resolution_completeness.format()
    assert (out1 == """\
---------------------------------------------------------
| Resolution range  | N(obs)/N(possible) | Completeness |
---------------------------------------------------------
| 21.9858 - 10.4368 | [ 6/7 ]            | 0.857        |
| 10.4368 -  8.4369 | [ 3/3 ]            | 1.000        |
|  8.4369 -  7.4172 | [ 3/4 ]            | 0.750        |
|  7.4172 -  6.7606 | [ 4/4 ]            | 1.000        |
|  6.7606 -  6.2882 | [ 8/8 ]            | 1.000        |
|  6.2882 -  5.9252 | [ 4/5 ]            | 0.800        |
|  5.9252 -  5.6337 | [11/11]            | 1.000        |
|  5.6337 -  5.3922 | [ 7/7 ]            | 1.000        |
|  5.3922 -  5.1874 | [ 6/6 ]            | 1.000        |
|  5.1874 -  5.0106 | [ 7/7 ]            | 1.000        |
---------------------------------------------------------"""), out1
    # ANOMALOUS SIGNAL
    a_meas = result.anomalous_info.measurability
    assert approx_equal(a_meas.high_d_cut, 4.7636, eps=0.0001)
    assert approx_equal(a_meas.low_d_cut, 2.2357, eps=0.0001)
    # ABSOLUTE SCALING
    ws = result.wilson_scaling
    assert ("%.2f" % ws.iso_p_scale) == "0.66"
    assert ("%.2f" % ws.iso_b_wilson) == "14.51"
    # FIXME these may need to be adjusted for different hardware/OS
    assert approx_equal(ws.aniso_p_scale, 0.66106, eps=0.001)
    assert approx_equal(
        ws.aniso_u_star,
        [0.00034473, 0.00479983, 0.000287162, -0.0, 9.00962e-05, 0.0])
    assert approx_equal(ws.aniso_b_cart,
                        (13.218423, 16.840142, 12.948426, 1.0354e-15,
                         -0.0685311, -7.92862e-16))
    # convenience methods for GUI
    assert approx_equal(result.aniso_b_min, 12.948426)
    assert approx_equal(result.aniso_range_of_b, 3.891716)
    #
    assert approx_equal(
        ws.outlier_shell_table.data[0],  # d_spacing
        [9.865132, 8.369653, 4.863587, 4.648635, 3.126905, 1.729609])
    assert approx_equal(
        ws.outlier_shell_table.data[1],  # z_score
        [5.587749, 15.425036, 4.763399, 6.57819, 4.650204, 4.580195])
    assert (len(ws.outliers.acentric_outliers_table.data[0]) == 2)
    assert (ws.outliers.acentric_outliers_table.data[1] == [(0, -1, -1),
                                                            (0, 1, 1)])
    assert approx_equal(ws.outliers.acentric_outliers_table.data[2],
                        [3.440749, 3.253775])
    assert (ws.outliers.centric_outliers_table.data is None)
    assert (len(ws.ice_rings.table._rows) == 10)
    assert (ws.ice_rings.table._rows[0] ==
            ['    3.897', '     1.000', '   0.52', '   1.00']), \
            ws.ice_rings.table._rows[0]
    tw = result.twin_results
    wm = tw.wilson_moments
    out = StringIO()
    wm.show(out)
    assert not show_diff(
        out.getvalue(), """
                  ----------Wilson ratio and moments----------

Acentric reflections:


   <I^2>/<I>^2    :2.047   (untwinned: 2.000; perfect twin 1.500)
   <F>^2/<F^2>    :0.779   (untwinned: 0.785; perfect twin 0.885)
   <|E^2 - 1|>    :0.743   (untwinned: 0.736; perfect twin 0.541)

Centric reflections:


   <I^2>/<I>^2    :3.043   (untwinned: 3.000; perfect twin 2.000)
   <F>^2/<F^2>    :0.626   (untwinned: 0.637; perfect twin 0.785)
   <|E^2 - 1|>    :0.996   (untwinned: 0.968; perfect twin 0.736)

""")
    # XXX PDB validation server
    assert approx_equal(result.iso_b_wilson, 14.51, eps=0.1)
    assert approx_equal(result.aniso_b_ratio, 0.271, eps=0.1)
    assert (result.number_of_wilson_outliers == 2)
    assert approx_equal(result.l_test_mean_l, 0.481, eps=0.1)
    assert approx_equal(result.l_test_mean_l_squared, 0.322, eps=0.1)
    assert approx_equal(result.i_over_sigma_outer_shell, 10.64, eps=0.01)
    assert ("indicating pseudo-translationa" in result.patterson_verdict)
    # check relative Wilson
    # FIXME
    #result.relative_wilson.show()
    #assert (result.relative_wilson.n_outliers() == 0)
    #show_pickled_object_sizes(result)
    #
    # test without sigmas
    f_obs_2 = f_obs.customized_copy(sigmas=None)
    mtz_file = "tst_xtriage_in_2.mtz"
    f_obs_2.as_mtz_dataset(column_root_label="F").mtz_object().write(mtz_file)
    xtriage_args = [
        mtz_file,
        pdb_file,
        seq_file,
        "log=tst_xtriage_1.log",
    ]

    result = xtriage.run(args=xtriage_args, out=null_out())
    result.summarize_issues()
    # test in lower symmetry
    f_obs_3 = f_obs.expand_to_p1()
    mtz_file = "tst_xtriage_in_3.mtz"
    f_obs_3.as_mtz_dataset(column_root_label="F").mtz_object().write(mtz_file)
    xtriage_args = [
        mtz_file,
        seq_file,
        "log=tst_xtriage_2.log",
    ]
    result = xtriage.run(args=xtriage_args, out=null_out())
    assert ((
        1,
        'One or more symmetry operators suggest that the data has a higher crystallographic symmetry (P 2 1 1).',
        'Point group and R-factor analysis')
            in result.summarize_issues()._issues)
    # test with elliptical truncation
    f_obs_3 = f_obs.customized_copy(
        crystal_symmetry=crystal.symmetry((23, 5, 20, 90, 107.8, 90), "P 21"))
    f_obs_3 = f_obs_3.resolution_filter(d_min=1.5)
    f_obs_3 = f_obs_3.customized_copy(
        crystal_symmetry=f_obs.crystal_symmetry())
    reso = ds.analyze_resolution_limits(f_obs_3)
    out = StringIO()
    reso.show(out=out)
    assert ("max. difference between axes = 0.652" in out.getvalue()), \
      out.getvalue()
    assert ("elliptically truncated" in out.getvalue())
    # make sure the elliptical truncation detection still works in higher space
    # groups - we only need a miller.set for this
    miller_set = miller.build_set(crystal_symmetry=crystal.symmetry(
        (20, 20, 20, 90, 90, 90), "P422"),
                                  d_min=1.5,
                                  anomalous_flag=False)
    reso = ds.analyze_resolution_limits(miller_set)
    out = StringIO()
    reso.show(out=out)
    assert ("Resolution limits are within expected tolerances"
            in out.getvalue())
    # log binning
    out = StringIO()
    log_binned = ds.log_binned_completeness(f_obs_3)
    log_binned.show(out=out)
    assert ("""| 1.9724 - 1.5094  | 368/1230    | 29.9%        |"""
            in out.getvalue()), out.getvalue()
    # test with no acentrics
    cf = f_obs.centric_flags().data()
    centrics = f_obs.select(cf)
    acentrics = f_obs.select(~cf)
    mtz_file = "tst_xtriage_in_3.mtz"
    centrics.as_mtz_dataset(column_root_label="F").mtz_object().write(mtz_file)
    args = [
        mtz_file,
        pdb_file,
        seq_file,
        "log=tst_xtriage_3.log",
    ]
    try:
        xtriage.run(args=args, out=null_out())
    except Sorry:
        pass
    else:
        raise Exception_expected
    # with only a handful of acentrics
    sel = flex.bool(acentrics.size(), False)
    for i in range(10):
        sel[i] = True
    f_obs_4 = centrics.concatenate(acentrics.select(sel))
    f_obs_4.as_mtz_dataset(column_root_label="F").mtz_object().write(mtz_file)
    try:
        xtriage.run(args=args, out=null_out())
    except Sorry:
        pass
    else:
        raise Exception_expected
Example #57
0
def exercise_d_data_target_d_atomic_params():
  import iotbx.pdb
  import mmtbx.f_model
  from scitbx.array_family import flex
  good = """
CRYST1   15.000   15.000   15.000  90.00  90.00  90.00 P 212121
HETATM  115  O   HOH A  18       3.000   5.000   5.000  1.00 10.00           O
HETATM  115  O   HOH A  18       5.000   5.000   8.000  1.00 10.00           O
TER
END
  """
  bad = """
CRYST1   15.000   15.000   15.000  90.00  90.00  90.00 P 212121
HETATM  115  O   HOH A  18       3.000   5.000   5.000  0.50 10.00           O
HETATM  115  O   HOH A  19       5.000   5.000   8.000  0.90 10.00           O
TER
END
  """
  for update_f_part1 in [True, False]:
    pdb_inp = iotbx.pdb.input(source_info=None, lines=bad)
    xrs = pdb_inp.xray_structure_simple()
    xrs.scattering_type_registry(table = "wk1995")
    #
    xb = iotbx.pdb.input(source_info=None, lines=good).xray_structure_simple()
    xb.scattering_type_registry(table = "wk1995")
    f_obs = abs(xb.structure_factors(d_min=1,
      algorithm="direct").f_calc()).set_observation_type_xray_amplitude()
    #
    sfp = mmtbx.f_model.sf_and_grads_accuracy_master_params.extract()
    sfp.algorithm="direct"
    for target_name in ["ls_wunit_kunit", "ls_wunit_k1", "ml"]:
      print("%s"%target_name, "-"*50)
      fmodel = mmtbx.f_model.manager(
        f_obs                        = f_obs,
        xray_structure               = xrs.deep_copy_scatterers(),
        target_name                  = target_name,
        sf_and_grads_accuracy_params = sfp)
      fmodel.update_all_scales(update_f_part1=update_f_part1)
      alpha_beta = fmodel.alpha_beta()
      print("R-work: %6.4f"%fmodel.r_work())
      #
      tg = mmtbx.utils.experimental_data_target_and_gradients(fmodel = fmodel,
        alpha_beta=alpha_beta)
      tg.show()
      eps = 1.e-6
      # occupancies
      go = tg.grad_occ()
      for i in [0,1]:
        xrs1 = fmodel.xray_structure.deep_copy_scatterers()
        xrs2 = fmodel.xray_structure.deep_copy_scatterers()
        #
        xrs1.scatterers()[i].occupancy+=+eps
        tg.update_xray_structure(xray_structure=xrs1, alpha_beta=alpha_beta)
        t1 = tg.target()
        #
        xrs2.scatterers()[i].occupancy+=-eps
        tg.update_xray_structure(xray_structure=xrs2, alpha_beta=alpha_beta)
        t2 = tg.target()
        #
        gfd = (t1-t2)/(2*eps)
        print(gfd, go[i])
        assert approx_equal(go[i], gfd, 1.e-5)
      # sites_cart
      gc = tg.grad_sites_cart()
      uc = fmodel.xray_structure.unit_cell()
      for i in [0,1]:
        gfd = []
        for e in [(eps,0,0),(0,eps,0),(0,0,eps)]:
          xrs1 = fmodel.xray_structure.deep_copy_scatterers()
          xrs2 = fmodel.xray_structure.deep_copy_scatterers()
          #
          s1 = flex.vec3_double([uc.orthogonalize(xrs1.scatterers()[i].site)])
          s1 = s1+flex.vec3_double([e])
          xrs1.scatterers()[i].site = uc.fractionalize(s1[0])
          tg.update_xray_structure(xray_structure=xrs1, alpha_beta=alpha_beta)
          t1 = tg.target()
          #
          s2 = flex.vec3_double([uc.orthogonalize(xrs2.scatterers()[i].site)])
          s2 = s2-flex.vec3_double([e])
          xrs2.scatterers()[i].site = uc.fractionalize(s2[0])
          tg.update_xray_structure(xray_structure=xrs2, alpha_beta=alpha_beta)
          t2 = tg.target()
          #
          gfd.append( (t1-t2)/(2*eps) )
        print(gfd, list(gc[i]))
        assert approx_equal(gc[i], gfd, 1.e-5)
def run_group(symbol):
  group = space_group_info(symbol);
  print("\n==")
  elements = ('C', 'N', 'O', 'H')*11
  struc = random_structure.xray_structure(
    space_group_info = group,
    volume_per_atom = 25.,
    general_positions_only = False,
    elements = elements,
    min_distance = 1.0)
  struc.show_summary()
  d_min = 2.
  fc = struc.structure_factors(d_min=d_min).f_calc()
  symmetry_flags = maptbx.use_space_group_symmetry
  fftmap = fc.fft_map(symmetry_flags = symmetry_flags)
  grid_size = fftmap.real_map().accessor().focus()
  ###
  rm = fftmap.real_map().deep_copy()
  amap0  = asymmetric_map(struc.space_group().type(), rm)
  p1_map00 = amap0.symmetry_expanded_map()
  assert approx_equal(p1_map00, rm)
  #
  maptbx.unpad_in_place(rm)
  amap1  = asymmetric_map(struc.space_group().type(), rm)
  p1_map10 = amap1.symmetry_expanded_map()
  assert approx_equal(p1_map00, p1_map10)
  ###

  grid_tags = maptbx.grid_tags(grid_size)
  grid_tags.build(fftmap.space_group_info().type(), fftmap.symmetry_flags())
  grid_tags.verify(fftmap.real_map())

  print("FFT grid_size = ", grid_size)
  amap = asymmetric_map(struc.space_group().type(), fftmap.real_map())
  afc = amap.structure_factors(fc.indices())
  afftmap = amap.map_for_fft()
  print("whole cell map size: ", afftmap.accessor().focus())
  adata = amap.data()
  acc = adata.accessor()
  print("Asu map size: ", acc.origin(), " ", acc.last(), " ", acc.focus(), \
      " ", acc.all())
  df = flex.abs(afc - fc.data())
  r1 = flex.sum(df) / flex.sum(flex.abs(fc.data()))
  print("R1: ", r1)
  assert r1 < 1.e-5
  # just to prove to myself that I can shift origin to 000 and then reshape back
  adata = adata.shift_origin()
  adata.reshape(acc)
  #
  adata2 = adata.deep_copy()*2.
  amap2 = asymmetric_map(struc.space_group().type(), adata2, grid_size)
  afc2 = amap2.structure_factors(fc.indices())
  df2 = flex.abs(afc2*.5-fc.data())
  r12 = flex.sum(df2) / flex.sum(flex.abs(fc.data()))
  print("R1 again: ", r12)
  assert r12 < 1.e-5

  p1_map = amap.symmetry_expanded_map()
  assert p1_map.accessor().focus() == grid_size

  rel_tol = 1.e-6
  n = 0
  mean_rel_dif = 0.
  for (m1,m2) in zip(fftmap.real_map(),p1_map):
    dif = abs(m1-m2)
    av = 0.5*(abs(m1)+abs(m2))
    assert dif <= rel_tol * av, "%f not <= %f * %f" % (dif, rel_tol, av)
    if av != 0:
      mean_rel_dif = mean_rel_dif + dif/av
      n = n + 1
  mean_rel_dif = mean_rel_dif / n
  print("mean rel err: ", mean_rel_dif)
  assert mean_rel_dif < 1.e-6
Example #59
0
        print 'FAIL: dials_regression not configured'
        exit(0)

    orig_expt_json = os.path.join(
        dials_regression, "experiment_test_data/kappa_experiments.json")

    new_expt_json = os.path.join(os.getcwd(), 'modified_experiments.json')

    cmd = "dials.modify_geometry %s angles=10,20,30" % orig_expt_json
    result = easy_run.fully_buffered(cmd).raise_if_errors()

    from dxtbx.serialize import load
    assert os.path.exists(orig_expt_json), orig_expt_json
    orig_expt = load.experiment_list(orig_expt_json, check_format=False)
    assert os.path.exists(new_expt_json), new_expt_json
    new_expt = load.experiment_list(new_expt_json, check_format=False)

    orig_gonio = orig_expt.goniometers()[0]
    new_gonio = new_expt.goniometers()[0]
    assert approx_equal(orig_gonio.get_angles(), [0, 180, 0])
    assert approx_equal(new_gonio.get_angles(), [10, 20, 30])

    return


if __name__ == '__main__':
    from dials.test import cd_auto
    with cd_auto(__file__):
        run()
        print "OK"
Example #60
0
    def plot_samples_ix(O, stage, ix):
        p = O.params.plot_samples
        i_sc, x_type = O.x_info[ix]

        def f_calc_without_moving_scatterer():
            sc = O.xray_structure.scatterers()[i_sc]
            occ = sc.occupancy
            try:
                sc.occupancy = 0
                result = O.get_f_calc().data()
            finally:
                sc.occupancy = occ
            return result

        f_calc_fixed = f_calc_without_moving_scatterer()
        xs = O.xray_structure.select(flex.size_t([i_sc]))

        def f_calc_moving():
            return O.f_obs.structure_factors_from_scatterers(
                xray_structure=xs, algorithm="direct",
                cos_sin_table=False).f_calc().data()

        sc = xs.scatterers()[0]
        ss = xs.site_symmetry_table().get(0)

        def build_info_str():
            return "%s|%03d|%03d|%s|%s|occ=%.2f|%s|%s" % (
                O.plot_samples_id, ix, i_sc, sc.label, sc.scattering_type,
                sc.occupancy, ss.special_op_simplified(), x_type)

        info_str = build_info_str()
        print "plot_samples:", info_str
        sys.stdout.flush()
        ys = []

        def ys_append():
            tg = O.__get_tg(f_cbs=O.get_f_cbs(f_calc=O.f_obs.customized_copy(
                data=f_calc_moving() + f_calc_fixed)),
                            derivatives_depth=0,
                            target=O.ps_target,
                            weights=O.ps_weights)
            y = tg.target_work()
            ys.append(y)
            return y

        xyv = []
        if (x_type == "u"):
            assert p.u_min < p.u_max
            assert p.u_steps > 0
            for i_step in xrange(p.u_steps + 1):
                u = p.u_min + i_step / p.u_steps * (p.u_max - p.u_min)
                sc.u_iso = u
                y = ys_append()
                xyv.append((u, y, sc.u_iso))
        else:
            assert p.x_radius > 0
            assert p.x_steps > 0
            ss_constr = ss.site_constraints()
            ss_np = ss_constr.n_independent_params()
            ss_ip = "xyz".find(x_type)
            assert ss_ip >= 0
            ixx = ix - ss_ip
            indep = list(O.x[ixx:ixx + ss_np])
            i_inp = indep[ss_ip]
            from libtbx.test_utils import approx_equal
            assert approx_equal(ss_constr.all_params(independent_params=indep),
                                sc.site)
            site_inp = sc.site
            indep[ss_ip] = i_inp + 1
            sc.site = ss_constr.all_params(independent_params=indep)
            dist = xs.unit_cell().distance(sc.site, site_inp)
            assert dist != 0
            x_scale = p.x_radius / dist
            for i_step in xrange(-p.x_steps // 2, p.x_steps // 2 + 1):
                x = i_step / p.x_steps * 2 * x_scale
                indep[ss_ip] = i_inp + x
                sc.site = ss_constr.all_params(independent_params=indep)
                y = ys_append()
                dist = xs.unit_cell().distance(sc.site, site_inp)
                if (i_step < 0): dist *= -1
                xyv.append((dist, y, indep[ss_ip]))
        #
        base_name_plot_files = "%s_%03d_%s" % (p.file_prefix, ix, stage)

        def write_xyv():
            if (p.file_prefix is None): return
            f = open(base_name_plot_files + ".xy", "w")
            print >> f, "# %s" % info_str
            print >> f, "# %s" % str(xs.unit_cell())
            print >> f, "# %s" % str(xs.space_group_info().symbol_and_number())
            for x, y, v in xyv:
                print >> f, x, y, v

        write_xyv()
        if (len(p.pyplot) != 0):
            from libtbx import pyplot
            fig = pyplot.figure()
            ax = fig.add_subplot(1, 1, 1)
            x, y, v = zip(*xyv)
            ax.plot(x, y, "r-")
            x = O.x[ix]
            ax.plot([x, x], [min(ys), max(ys)], "k--")
            if (O.x_reference is not None):
                x = O.x_reference[ix]
                ax.plot([x, x], [min(ys), max(ys)], "r--")
            ax.set_title(info_str, fontsize=12)
            ax.axis([xyv[0][0], xyv[-1][0], None, None])

            def write_pdf():
                if (p.file_prefix is None): return
                fig.savefig(base_name_plot_files + ".pdf", bbox_inches="tight")

            if ("pdf" in p.pyplot):
                write_pdf()
            if ("gui" in p.pyplot):
                pyplot.show()