Example #1
0
  def __init__(
            self,
            fmodels,
            model,
            all_params,
            group_adp_selections   = None,
            group_adp_selections_h = None,
            group_adp_params       = group_adp_master_params.extract(),
            tls_selections         = None,
            nm_selections          = None,
            tls_params             = tls_master_params.extract(),
            nm_params              = nm_master_params.extract(),
            individual_adp_params  = individual_adp_master_params.extract(),
            adp_restraints_params  = adp_restraints_master_params.extract(),
            refine_adp_individual  = None,
            refine_adp_group       = None,
            refine_tls             = None,
            refine_nm              = None,
            tan_b_iso_max          = None,
            restraints_manager     = None,
            target_weights         = None,
            macro_cycle            = None,
            log                    = None,
            h_params               = None,
            nproc                  = None):
    global time_adp_refinement_py
    scatterers = fmodels.fmodel_xray().xray_structure.scatterers()
    timer = user_plus_sys_time()
    if(log is None): log = sys.stdout
    tan_u_iso = False
    param = 0
    if(tan_b_iso_max > 0.0):
       tan_u_iso = True
       param = int(tan_b_iso_max)
    if(macro_cycle == 1):
       offset = True
    else:
       offset = False

    if(refine_tls):
       print_statistics.make_sub_header(text = "TLS refinement",
                                        out  = log)
       tls_sel_st = flex.size_t()
       for ts in tls_selections:
         tls_sel_st.extend(ts)
       tls_sel_bool = flex.bool(scatterers.size(), flex.size_t(tls_sel_st))
       ### totally ad hoc fix
       tmp_site_t = flex.size_t()
       for gs in group_adp_selections:
         for gs_ in gs:
           tmp_site_t.append(gs_)
       ###
       if(macro_cycle == 1 or tmp_site_t.size() != scatterers.size()):
          gbr_selections = []
          for s in tls_selections:
            gbr_selections.append(s)
       else:
          gbr_selections = []
          for gs in group_adp_selections:
            gbr_selection = flex.size_t()
            for gs_ in gs:
              if(tls_sel_bool[gs_]):
                gbr_selection.append(gs_)
            if(gbr_selection.size() > 0):
              gbr_selections.append(gbr_selection)
       gbr_selections_one_arr = flex.size_t()
       for gbs in gbr_selections:
         gbr_selections_one_arr.extend(gbs)
       scatterers = fmodels.fmodel_xray().xray_structure.scatterers()
       for gbr_selection in gbr_selections_one_arr:
         scatterers[gbr_selection].flags.set_use_u_iso(True)
       group_b_manager = mmtbx.refinement.group.manager(
          fmodel                   = fmodels.fmodel_xray(),
          selections               = gbr_selections,
          convergence_test         = group_adp_params.convergence_test,
          max_number_of_iterations = 50,
          number_of_macro_cycles   = 1,
          refine_adp               = True,
          log                      = log)
       scatterers = fmodels.fmodel_xray().xray_structure.scatterers()
       for tls_selection_ in tls_selections:
         for tls_selection__ in tls_selection_:
           scatterers[tls_selection__].flags.set_use_u_aniso(True)
       model.show_groups(tls = True, out = log)
       current_target_name = fmodels.fmodel_xray().target_name
       fmodels.fmodel_xray().update(target_name = "ls_wunit_k1")
       tools.split_u(fmodels.fmodel_xray().xray_structure, tls_selections, offset)
       self.tls_refinement_manager = tools.tls_refinement(
          fmodel                      = fmodels.fmodel_xray(),
          model                       = model,
          selections                  = tls_selections,
          selections_1d               = tls_sel_st,
          refine_T                    = tls_params.refine_T,
          refine_L                    = tls_params.refine_L,
          refine_S                    = tls_params.refine_S,
          number_of_macro_cycles      = tls_params.number_of_macro_cycles,
          max_number_of_iterations    = tls_params.max_number_of_iterations,
          start_tls_value             = tls_params.start_tls_value,
          run_finite_differences_test = tls_params.run_finite_differences_test,
          eps                         = tls_params.eps,
          out                         = log,
          macro_cycle = macro_cycle,
          verbose = tls_params.verbose)
       fmodels.fmodel_xray().update(target_name = current_target_name)
       fmodels.update_xray_structure(
            xray_structure = self.tls_refinement_manager.fmodel.xray_structure,
            update_f_calc  = True)
       model.xray_structure = fmodels.fmodel_xray().xray_structure
    if(refine_nm):
       print_statistics.make_sub_header(text = "NM refinement",
                                        out  = log)
       nm_sel_st = flex.size_t()
       for ns in nm_selections:
         nm_sel_st.extend(ns)
       scatterers = fmodels.fmodel_xray().xray_structure.scatterers()
       for nm_selection_ in nm_selections:
         for nm_selection__ in nm_selection_:
           scatterers[nm_selection__].flags.set_use_u_aniso(True)
       model.show_groups(nm = True, out = log)
       current_target_name = fmodels.fmodel_xray().target_name
#       fmodels.fmodel_xray().update(target_name = "ls_wunit_k1")
#       tools.split_u(fmodels.fmodel_xray().xray_structure, tls_selections, offset)
       self.nm_refinement_manager = nmtools.nm_refinement(
          fmodel                      = fmodels.fmodel_xray(),
          model                       = model,
          selections                  = nm_selections,
          selections_1d               = nm_sel_st,
          n_modes                     = nm_params.n_modes,
          number_of_macro_cycles      = nm_params.number_of_macro_cycles,
          max_number_of_iterations    = nm_params.max_number_of_iterations,
          weight_nmre		      = nm_params.weight_nmre,
          evecin                      = nm_params.evecin,
          evalin                      = nm_params.evalin,
	  nm_params_filename          = nm_params.nm_params_filename,
          zero_mode_flag              = nm_params.zero_mode_flag,
          zero_mode_input_flag        = nm_params.zero_mode_input_flag,
          start_xs_value              = nm_params.start_xs_value,
          run_finite_differences_test = nm_params.run_finite_differences_test,
          eps                         = nm_params.eps,
          out                         = log,
          macro_cycle = macro_cycle,
          verbose = nm_params.verbose)
       fmodels.fmodel_xray().update(target_name = current_target_name)
       fmodels.update_xray_structure(
            xray_structure = self.nm_refinement_manager.fmodel.xray_structure,
            update_f_calc  = True)
       model.xray_structure = fmodels.fmodel_xray().xray_structure

    if(refine_adp_individual):
       refine_adp(
         model                 = model,
         fmodels               = fmodels,
         target_weights        = target_weights,
         individual_adp_params = individual_adp_params,
         adp_restraints_params = adp_restraints_params,
         h_params              = h_params,
         log                   = log,
         all_params            = all_params,
         nproc                 = nproc)

    if(refine_adp_group):
       print_statistics.make_sub_header(text= "group isotropic ADP refinement",
                                        out = log)
       group_b_manager = mmtbx.refinement.group.manager(
          fmodel                   = fmodels.fmodel_xray(),
          selections               = group_adp_selections,
          convergence_test         = group_adp_params.convergence_test,
          max_number_of_iterations = group_adp_params.max_number_of_iterations,
          number_of_macro_cycles   = group_adp_params.number_of_macro_cycles,
          run_finite_differences_test = group_adp_params.run_finite_differences_test,
          refine_adp               = True,
          log                      = log)
    time_adp_refinement_py += timer.elapsed()
def exercise_2(eps = 1.e-6):
###> Get started from PDB
  mon_lib_srv = monomer_library.server.server()
  ener_lib = monomer_library.server.ener_lib()
  pdb_file = libtbx.env.find_in_repositories(
    relative_path="phenix_regression/pdb/phe_abc_tlsanl_out_geometry_minimized.pdb",
    test=os.path.isfile)
  processed_pdb_file = monomer_library.pdb_interpretation.process(
                                       mon_lib_srv               = mon_lib_srv,
                                       ener_lib                  = ener_lib,
                                       file_name                 = pdb_file,
                                       raw_records               = None,
                                       force_symmetry            = True)
  xray_structure = processed_pdb_file.xray_structure()
  xray_structure.scattering_type_registry(table = "wk1995")
  xray_structure.convert_to_isotropic()
  u_iso_start = xray_structure.extract_u_iso_or_u_equiv()
  xray_structure.convert_to_anisotropic()
  selections = []
  selection_strings = ["chain A", "chain B", "chain C"]
  for string in selection_strings:
      selections.append(processed_pdb_file.all_chain_proxies.selection(
                                                              string = string))
################
  geometry = processed_pdb_file.geometry_restraints_manager(
                                                    show_energies      = False,
                                                    plain_pairs_radius = 5.0)
  restraints_manager = mmtbx.restraints.manager(geometry      = geometry,
                                                normalization = False)
  selection = flex.bool(xray_structure.scatterers().size(), True)
  class refinement_flags: pass
  refinement_flags.adp_tls = selections
  tls_groups = tools.tls_groups(selection_strings = selection_strings)
  tlsos = tools.generate_tlsos(
          selections     = refinement_flags.adp_tls,
          xray_structure = xray_structure,
          value          = 0.0)
  tls_groups.tlsos = tlsos
  model = mmtbx.model.manager(
    refinement_flags = refinement_flags,
    restraints_manager = restraints_manager,
    xray_structure = xray_structure,
    tls_groups = tls_groups,
    pdb_hierarchy = processed_pdb_file.all_chain_proxies.pdb_hierarchy)
################
###> Get TLS <-> Ucart
  T_initial = []
  L_initial = []
  S_initial = []
  T_initial.append([0.11,0.22,0.33,0.12,0.13,0.23])
  L_initial.append([1.11,1.22,1.33,1.12,1.13,1.23])
  S_initial.append([0.11,0.12,0.13,0.21,0.22,0.23,0.31,0.32,-0.33])

  T_initial.append([0.22,0.44,0.66,0.24,0.26,0.46])
  L_initial.append([2.22,2.44,2.66,2.24,2.26,2.46])
  S_initial.append([0.22,0.24,0.26,0.42,0.44,0.46,0.62,0.64,-0.66])

  T_initial.append([0.33,0.66,0.99,0.36,0.39,0.69])
  L_initial.append([2.33,2.66,2.99,2.36,2.39,2.69])
  S_initial.append([0.22,0.24,0.26,0.42,0.44,0.46,0.62,0.64,-0.66])

  tlsosA = tools.generate_tlsos(selections     = selections,
                                xray_structure = xray_structure,
                                T              = T_initial,
                                L              = L_initial,
                                S              = S_initial)

  tlsos = tools.generate_tlsos(selections     = selections,
                               xray_structure = xray_structure,
                               T              = T_initial,
                               L              = L_initial,
                               S              = S_initial)
  tlsos = tools.make_tlso_compatible_with_u_positive_definite(
                  tlsos                                       = tlsos,
                  xray_structure                              = xray_structure.deep_copy_scatterers(),
                  selections                                  = selections,
                  max_iterations                              = 50,
                  number_of_u_nonpositive_definite            = 0,
                  eps                                         = eps,
                  number_of_macro_cycles_for_tls_from_uanisos = 30)

  u_cart_answer = tools.u_cart_from_tls(sites_cart = xray_structure.sites_cart(),
                                         selections = selections,
                                         tlsos      = tlsos)
  xray_structure.scatterers().set_u_cart(xray_structure.unit_cell(),
                                                                 u_cart_answer)

  assert approx_equal(u_cart_answer,
        xray_structure.scatterers().extract_u_cart(xray_structure.unit_cell()))


  tools.show_tls(tlsos = tlsos, text = "ANSWER")

###> Set up fmodel
  sfg_params = mmtbx.f_model.sf_and_grads_accuracy_master_params.extract()
  sfg_params.algorithm = "direct"
  sfg_params.cos_sin_table = False
  dummy = xray_structure.structure_factors(algorithm = sfg_params.algorithm,
                                           d_min     = 2.0).f_calc()
  f_obs = abs(dummy.structure_factors_from_scatterers(
                           xray_structure = xray_structure,
                           algorithm      = sfg_params.algorithm,
                           cos_sin_table  = sfg_params.cos_sin_table).f_calc())
  flags = f_obs.generate_r_free_flags(fraction=0.01, max_free=2000)

  fmodel = mmtbx.f_model.manager(xray_structure    = xray_structure,
                                 f_obs             = f_obs,
                                 r_free_flags      = flags,
                                 target_name       = "ls_wunit_k1",
                                 sf_and_grads_accuracy_params = sfg_params)
  fmodel.info(free_reflections_per_bin=250, max_number_of_bins=30).show_all()
  xray_structure.convert_to_isotropic()
  xray_structure.set_b_iso(value = 25.0)
  fmodel.update_xray_structure(xray_structure = xray_structure,
                               update_f_calc  = True)
  fmodel.info(free_reflections_per_bin=250, max_number_of_bins=30).show_all()
  print "*"*80
###> TLS refinement against xray data
  if (not "--comprehensive" in sys.argv[1:]):
          number_of_macro_cycles   = 1
          max_number_of_iterations = 3
  else:
          number_of_macro_cycles   = 100
          max_number_of_iterations = 50

  for start_tls_value in [None]:#[0.0, tlsosA, None]:
  #for start_tls_value in [None]:
      print " \n "+str(start_tls_value) + " \n "
      fmodel_cp = fmodel.deep_copy()
      #for sc in fmodel_cp.xray_structure.scatterers():
      #  sc.flags.set_use_u_aniso(True)
      fmodel_cp.xray_structure.convert_to_anisotropic()

      if(start_tls_value is None):
         run_finite_differences_test = True
      else: run_finite_differences_test = False
      model.xray_structure = fmodel_cp.xray_structure
      tls_refinement_manager = tools.tls_refinement(
                     fmodel                      = fmodel_cp,
                     model                       = model,
                     selections                  = selections,
                     selections_1d               = None,
                     refine_T                    = 1,
                     refine_L                    = 1,
                     refine_S                    = 1,
                     number_of_macro_cycles      = number_of_macro_cycles,
                     max_number_of_iterations    = max_number_of_iterations,
                     start_tls_value             = start_tls_value,
                     run_finite_differences_test = run_finite_differences_test,
                     eps                         = eps)
      u_cart = tls_refinement_manager.fmodel.xray_structure.scatterers().extract_u_cart(
                                                        xray_structure.unit_cell())
      if("--comprehensive" in sys.argv[1:]):
         format   = "%10.6f %10.6f %10.6f %10.6f %10.6f %10.6f"
         counter = 0
         if(start_tls_value == tlsosA): tolerance = 1.e-6
         else: tolerance = 0.02
         for m1,m2 in zip(u_cart_answer, u_cart):
             counter += 1
             if(counter < 10):
                print "1=" + format % (m1[0],m1[1],m1[2],m1[3],m1[4],m1[5])
                print "2=" + format % (m2[0],m2[1],m2[2],m2[3],m2[4],m2[5])
             assert approx_equal(m1,m2, tolerance)