Example #1
0
def run(fmodel, model, log, params=None):
    print_statistics.make_header("Fit water hydrogens into residual map",
                                 out=log)
    if (params is None):
        params = all_master_params().extract()
    print_statistics.make_sub_header("find peak-candidates", out=log)
    peaks = find_hydrogen_peaks(fmodel=fmodel,
                                pdb_atoms=model.get_atoms(),
                                params=params,
                                log=log)
    waters_and_peaks = extract_hoh_peaks(
        peaks=peaks,
        pdb_hierarchy=model.get_hierarchy(),
        pdb_atoms=model.get_atoms(),
        xray_structure=model.get_xray_structure())
    print_statistics.make_sub_header("6D rigid body fit of HOH", out=log)
    print("Fit quality:", file=log)
    for water_and_peaks in waters_and_peaks:
        fit_water(water_and_peaks=water_and_peaks,
                  xray_structure=model.get_xray_structure(),
                  params=params,
                  log=log)
    # adjust ADP for H
    # TODO mrt: probably H bfactors should be equal to those
    # of the bonded atom
    u_isos = model.get_xray_structure().extract_u_iso_or_u_equiv()
    u_iso_mean = flex.mean(u_isos)
    sel_big = u_isos > u_iso_mean * 2
    hd_sel = model.get_hd_selection()
    sel_big.set_selected(~hd_sel, False)
    model.get_xray_structure().set_u_iso(value=u_iso_mean, selection=sel_big)
Example #2
0
def run(fmodel, model, log, params = None):
  print_statistics.make_header("Fit water hydrogens into residual map",
    out = log)
  if(params is None):
    params = all_master_params().extract()
  print_statistics.make_sub_header("find peak-candidates", out = log)
  peaks = find_hydrogen_peaks(
    fmodel = fmodel,
    pdb_atoms = model.pdb_atoms,
    params = params,
    log = log)
  waters_and_peaks = extract_hoh_peaks(
    peaks = peaks,
    pdb_hierarchy = model.pdb_hierarchy(),
    pdb_atoms = model.pdb_atoms,
    xray_structure = model.xray_structure)
  print_statistics.make_sub_header("6D rigid body fit of HOH", out = log)
  print >> log, "Fit quality:"
  for water_and_peaks in waters_and_peaks:
    fit_water(water_and_peaks = water_and_peaks,
              xray_structure  = model.xray_structure,
              params          = params,
              log             = log)
  # adjust ADP for H
  # TODO mrt: probably H bfactors should be equal to those
  # of the bonded atom
  u_isos = model.xray_structure.extract_u_iso_or_u_equiv()
  u_iso_mean = flex.mean(u_isos)
  sel_big = u_isos > u_iso_mean*2
  hd_sel = model.xray_structure.hd_selection()
  sel_big.set_selected(~hd_sel, False)
  model.xray_structure.set_u_iso(value = u_iso_mean, selection = sel_big)
Example #3
0
 def show_comprehensive(self, message = ""):
   """
   ...
   """
   from mmtbx.refinement import print_statistics
   print_statistics.make_sub_header("X-ray data", out = self.log)
   if(self.fmodel_x is not None):
     self.fmodel_xray().info().show_all(header = message, out = self.log)
   if(self.fmodel_n is not None):
     print_statistics.make_sub_header("Neutron data", out = self.log)
     self.fmodel_neutron().info().show_all(header = message, out = self.log)
Example #4
0
 def show_comprehensive(self, message=""):
     """
 ...
 """
     from mmtbx.refinement import print_statistics
     print_statistics.make_sub_header("X-ray data", out=self.log)
     if (self.fmodel_x is not None):
         self.fmodel_xray().info().show_all(header=message, out=self.log)
     if (self.fmodel_n is not None):
         print_statistics.make_sub_header("Neutron data", out=self.log)
         self.fmodel_neutron().info().show_all(header=message, out=self.log)
def manager(params,
            target_weights,
            all_params,
            macro_cycle,
            h_params,
            fmodels,
            model,
            out = None,
            states_collector=None,
            callback=None):
  if(out is None): out = sys.stdout
  if (states_collector is not None) :
    assert hasattr(states_collector, "add")
  print_statistics.make_header("simulated annealing refinement", out = out)
  model.set_refine_individual_sites()
  fmodel = fmodels.fmodel_xray() # XXX use only xray data
  fmodel.xray_structure = model.xray_structure # XXX use only xray data
  if (params.max_number_of_iterations >= 0):
    print_statistics.make_sub_header(
      "lbfgs minimization: before simulated annealing", out = out)
    is_neutron_scat_table = False
    if(all_params.main.scattering_table == "neutron"):
      is_neutron_scat_table = True
    import scitbx.lbfgs
    minimized = mmtbx.refinement.minimization.lbfgs(
      restraints_manager       = model.restraints_manager,
      refine_xyz               = True,
      fmodels                  = fmodels,
      is_neutron_scat_table    = is_neutron_scat_table,
      model                    = model,
      lbfgs_termination_params = scitbx.lbfgs.termination_parameters(
        max_iterations = params.max_number_of_iterations),
      target_weights           = target_weights,
      h_params                 = h_params,
      verbose                  = 0)
  fmodel.update_xray_structure(xray_structure = model.xray_structure,
                               update_f_calc  = True,
                               update_f_mask  = True)
  print_statistics.make_header("simulated annealing", out = out)
  wx = target_weights.xyz_weights_result.wx * \
    target_weights.xyz_weights_result.wx_scale
  run(
    params = params,
    restraints_manager = model.restraints_manager,
    fmodel             = fmodel,
    wx                 = wx,
    wc                 = target_weights.xyz_weights_result.w,
    states_collector   = states_collector,
    callback           = callback,
    log                = out)
Example #6
0
def manager(params,
            target_weights,
            all_params,
            macro_cycle,
            h_params,
            fmodels,
            model,
            out=None,
            states_collector=None,
            callback=None):
    if (out is None): out = sys.stdout
    if (states_collector is not None):
        assert hasattr(states_collector, "add")
    print_statistics.make_header("simulated annealing refinement", out=out)
    model.set_refine_individual_sites()
    fmodel = fmodels.fmodel_xray()  # XXX use only xray data
    fmodel.xray_structure = model.get_xray_structure(
    )  # XXX use only xray data
    if (params.max_number_of_iterations >= 0):
        print_statistics.make_sub_header(
            "lbfgs minimization: before simulated annealing", out=out)
        is_neutron_scat_table = False
        if (all_params.main.scattering_table == "neutron"):
            is_neutron_scat_table = True
        import scitbx.lbfgs
        minimized = mmtbx.refinement.minimization.lbfgs(
            restraints_manager=model.restraints_manager,
            refine_xyz=True,
            fmodels=fmodels,
            is_neutron_scat_table=is_neutron_scat_table,
            model=model,
            lbfgs_termination_params=scitbx.lbfgs.termination_parameters(
                max_iterations=params.max_number_of_iterations),
            target_weights=target_weights,
            h_params=h_params,
            verbose=0)
    fmodel.update_xray_structure(xray_structure=model.get_xray_structure(),
                                 update_f_calc=True,
                                 update_f_mask=True)
    print_statistics.make_header("simulated annealing", out=out)
    wx = target_weights.xyz_weights_result.wx * \
      target_weights.xyz_weights_result.wx_scale
    run(params=params,
        restraints_manager=model.restraints_manager,
        fmodel=fmodel,
        wx=wx,
        wc=target_weights.xyz_weights_result.w,
        states_collector=states_collector,
        callback=callback,
        log=out)
Example #7
0
def show_highest_peaks_and_deepest_holes(fmodel,
                                         pdb_atoms,
                                         map_type,
                                         map_cutoff_plus,
                                         map_cutoff_minus,
                                         log = None):
  if(log is None): log = sys.stdout
  print_statistics.make_header(
    "residual map %s: highest peaks and deepst holes"%map_type, out = log)
  fp_params = master_params.extract()
  fp_params.map_next_to_model.min_model_peak_dist = 0.7
  fp_params.map_next_to_model.min_peak_peak_dist = 0.7
  fp_params.peak_search.min_cross_distance = 0.7
  fp_params.map_next_to_model.use_hydrogens = True
  for par in [(map_cutoff_plus,"peaks"), (map_cutoff_minus,"holes")]:
    print_statistics.make_sub_header(par[1], out = log)
    result = manager(fmodel     = fmodel,
                     map_type   = "mFobs-DFmodel",
                     map_cutoff = par[0],
                     params     = fp_params,
                     log        = log)
    result.peaks_mapped()
    result.show_mapped(pdb_atoms = pdb_atoms)
Example #8
0
def show_highest_peaks_and_deepest_holes(fmodel,
                                         pdb_atoms,
                                         map_type,
                                         map_cutoff_plus,
                                         map_cutoff_minus,
                                         log=None):
    if (log is None): log = sys.stdout
    print_statistics.make_header(
        "residual map %s: highest peaks and deepst holes" % map_type, out=log)
    fp_params = master_params.extract()
    fp_params.map_next_to_model.min_model_peak_dist = 0.7
    fp_params.map_next_to_model.min_peak_peak_dist = 0.7
    fp_params.peak_search.min_cross_distance = 0.7
    fp_params.map_next_to_model.use_hydrogens = True
    for par in [(map_cutoff_plus, "peaks"), (map_cutoff_minus, "holes")]:
        print_statistics.make_sub_header(par[1], out=log)
        result = manager(fmodel=fmodel,
                         map_type="mFobs-DFmodel",
                         map_cutoff=par[0],
                         params=fp_params,
                         log=log)
        result.peaks_mapped()
        result.show_mapped(pdb_atoms=pdb_atoms)
Example #9
0
def find_tls(params,
             pdb_hierarchy,
             xray_structure,
             return_as_list=False,
             ignore_pdb_header_groups=False,
             out=None):
    """
  !!! WARNING! incoming xray_structure here gets converted to
  isotropic B-factors IN PLACE.
  """
    if (out is None):
        out = sys.stdout
    print_statistics.make_header("Analyzing inputs", out=out)
    if (params.random_seed is None):
        params.random_seed = flex.get_random_seed()
    random.seed(params.random_seed)
    flex.set_random_seed(params.random_seed)
    xray_structure.convert_to_isotropic()
    sites_cart = xray_structure.sites_cart()
    u_cart = None
    u_iso = xray_structure.extract_u_iso_or_u_equiv()  #*adptbx.u_as_b(1.) # ?
    bad_i_seqs = check_adp(u_iso=u_iso, out=out)
    if (bad_i_seqs is not None):
        atoms = pdb_hierarchy.atoms()
        bad_atom_strings = []
        for i_seq in bad_i_seqs[:10]:
            atom_str = atoms[i_seq].format_atom_record()
            bad_atom_strings.append(atom_str)
        if (len(bad_i_seqs) > 10):
            bad_atom_strings.append("... (remaining %d not shown)" %
                                    (len(bad_i_seqs) - 10))
        raise Sorry(
            ("%d atoms in the model contain isotropic B-factors <= 0:\n" +
             "\n".join(bad_atom_strings)) % (len(bad_i_seqs)))
    #
    ssm = mmtbx.secondary_structure.manager(pdb_hierarchy=pdb_hierarchy,
                                            sec_str_from_pdb_file=None,
                                            params=None,
                                            log=out)
    alpha_h_selection = ssm.helix_selection()
    secondary_structure_selection = ssm.helix_selection() | \
        ssm.beta_selection() | ssm.base_pair_selection()
    if (u_cart is not None):
        assert secondary_structure_selection.size() == u_cart.size()
    else:
        assert secondary_structure_selection.size() == u_iso.size()
    ssm.show_summary(log=out)
    chains_and_residue_selections, secondary_structure_selection = chains_and_atoms(
        pdb_hierarchy=pdb_hierarchy,
        secondary_structure_selection=secondary_structure_selection,
        out=out)
    chains_and_permutations = []
    chains_and_atom_selection_strings = []
    print_statistics.make_header("Processing chains", out=out)
    if (params.nproc is None):
        params.nproc = 1
    for crs in chains_and_residue_selections:
        print_statistics.make_sub_header("Processing chain '%s'" % crs[0],
                                         out=out)
        chain_selection = chain_selection_from_residues(crs[1])
        groups, perms = get_model_partitioning(
            residues=crs[1],
            secondary_structure_selection=secondary_structure_selection,
            out=out)
        #
        if (len(perms) == 1):
            print("  Whole chain is considered as one TLS group.", file=out)
            chains_and_atom_selection_strings.append([
                crs[0],
                permutations_as_atom_selection_string(groups, perms[0])
            ])
        else:
            print("  Fitting TLS matrices...", file=out)
            dic = {}
            target_best = 1.e+9
            if (params.nproc is Auto) or (params.nproc > 1):
                process_perms = analyze_permutations(groups=groups,
                                                     sites_cart=sites_cart,
                                                     u_cart=u_cart,
                                                     u_iso=u_iso)
                from libtbx import easy_mp
                stdout_and_targets = easy_mp.pool_map(
                    processes=params.nproc,
                    fixed_func=process_perms,
                    args=perms,
                    chunksize=100,
                    func_wrapper="buffer_stdout_stderr")
                targets = [t for so, t in stdout_and_targets]
                for (perm, target) in zip(perms, targets):
                    dic.setdefault(len(perm), []).append([target, perm])
            else:
                for i_perm, perm in enumerate(perms):
                    if i_perm % 500 == 0:
                        print("    ...perm %d of %d" % (i_perm, len(perms)),
                              file=out)
                    selections = tls_group_selections(groups, perm)
                    target = 0
                    for selection in selections:
                        mo = tls_refinery(u_cart=u_cart,
                                          u_iso=u_iso,
                                          sites_cart=sites_cart,
                                          selection=selection)
                        target += mo.f
                    dic.setdefault(len(perm), []).append([target, perm])
                #print "    perm %d of %d: target=%8.3f (TLS groups: %s), permutation:"%(
                #  i_perm, len(perms),target,len(perm)),perm
            print("    Best fits:", file=out)
            print("      No. of         Targets", file=out)
            print("      groups   best   rand.pick  diff.  score permutation",
                  file=out)
            score_best = -1.e+9
            perm_choice = None
            for k, v in six.iteritems(dic):
                t_best = v[0][0]
                perm_best = v[0][1]
                for v_ in v:
                    if (v_[0] < t_best):
                        t_best = v_[0]
                        perm_best = v_[1]
                if (u_cart is not None):
                    u_cart_ = u_cart.select(chain_selection)
                else:
                    u_cart_ = None
                if (u_iso is not None):
                    u_iso_ = u_iso.select(chain_selection)
                else:
                    u_iso_ = None
                r = tls_refinery_random_groups(
                    u_cart=u_cart_,
                    u_iso=u_iso_,
                    sites_cart=sites_cart.select(chain_selection),
                    n_groups=k)
                score = (r - t_best) / (r + t_best) * 100.
                print("         %3d %6.3f      %6.3f %6.2f %6.3f" %
                      (k, t_best, r, r - t_best, score),
                      perm_best,
                      file=out)
                if (score > score_best):
                    score_best = score
                    perm_choice = perm_best[:]
            #
            chains_and_permutations.append([crs[0], perm_choice])
            chains_and_atom_selection_strings.append([
                crs[0],
                permutations_as_atom_selection_string(groups, perm_choice)
            ])
            #
    print_statistics.make_header("SUMMARY", out=out)
    #print "Optimal TLS groups:"
    #for chain_and_permutation in chains_and_permutations:
    #  print chain_and_permutation
    #print
    print("TLS atom selections for phenix.refine:", file=out)
    groups_out = StringIO()
    selection_strings = []
    print("refinement.refine.adp {", file=groups_out)
    for r in chains_and_atom_selection_strings:
        prefix = "chain '%s'" % r[0]
        if (len(r[1]) > 0 and len(r[1:]) > 0):
            prefix += " and "
            for r_ in r[1:]:
                for r__ in r_:
                    if (len(r__) > 0):
                        group_selection = prefix + "(%s)" % r__
                        print("  tls = \"%s\"" % group_selection,
                              file=groups_out)
                        selection_strings.append("%s" % group_selection)
        else:
            print("  tls = \"%s\"" % prefix, file=groups_out)
            selection_strings.append("%s" % prefix)
    print("}", file=groups_out)
    print(groups_out.getvalue(), file=out)
    print(file=out)
    #XXX
    if 0:
        merge_groups_by_connectivity(pdb_hierarchy=pdb_hierarchy,
                                     xray_structure=xray_structure,
                                     selection_strings=selection_strings)
    #XXX
    if (len(selection_strings) > 0):
        total_target = total_score(pdb_hierarchy=pdb_hierarchy,
                                   sites_cart=sites_cart,
                                   u_iso=u_iso,
                                   selection_strings=selection_strings)
        print(
            "Overall best total target for automatically found groups: %10.1f"
            % total_target,
            file=out)
        print(file=out)
    if (return_as_list):
        return selection_strings
    else:
        return groups_out.getvalue()
Example #10
0
  def __init__(
            self,
            fmodels,
            model,
            all_params,
            group_adp_selections   = None,
            group_adp_selections_h = None,
            group_adp_params       = None,
            tls_selections         = None,
            tls_params             = tls_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,
            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
    if(group_adp_params is None):
      group_adp_params = group_adp_master_params.extract()
    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,
          use_restraints           = False, #XXX do not use in TLS refinement for now
          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.set_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,
        use_restraints           = group_adp_params.use_restraints,
        restraints_weight        = group_adp_params.restraints_weight,
        refine_adp               = True,
        log                      = log)
    time_adp_refinement_py += timer.elapsed()
Example #11
0
 def __init__(
           self,
           model,
           fmodels,
           target_weights,
           individual_adp_params,
           adp_restraints_params,
           h_params,
           log,
           all_params,
           nproc=None):
   adopt_init_args(self, locals())
   d_min = fmodels.fmodel_xray().f_obs().d_min()
   # initialize with defaults...
   if(target_weights is not None):
     import mmtbx.refinement.weights_params
     wcp = mmtbx.refinement.weights_params.tw_customizations_params.extract()
     for w_s_c in wcp.weight_selection_criteria:
       if(d_min >= w_s_c.d_min and d_min < w_s_c.d_max):
         r_free_range_width = w_s_c.r_free_range_width
         r_free_r_work_gap = w_s_c.r_free_minus_r_work
         mean_diff_b_iso_bonded_fraction = w_s_c.mean_diff_b_iso_bonded_fraction
         min_diff_b_iso_bonded = w_s_c.min_diff_b_iso_bonded
         break
     # ...then customize
     wsc = all_params.target_weights.weight_selection_criteria
     if(wsc.r_free_minus_r_work is not None):
       r_free_r_work_gap = wsc.r_free_minus_r_work
     if(wsc.r_free_range_width is not None):
       r_free_range_width = wsc.r_free_range_width
     if(wsc.mean_diff_b_iso_bonded_fraction is not None):
       mean_diff_b_iso_bonded_fraction = wsc.mean_diff_b_iso_bonded_fraction
     if(wsc.min_diff_b_iso_bonded is not None):
       min_diff_b_iso_bonded = wsc.min_diff_b_iso_bonded
   #
   print_statistics.make_sub_header(text="Individual ADP refinement", out = log)
   assert fmodels.fmodel_xray().xray_structure is model.get_xray_structure()
   #
   fmodels.create_target_functors()
   assert approx_equal(self.fmodels.fmodel_xray().target_w(),
     self.fmodels.target_functor_result_xray(
       compute_gradients=False).target_work())
   rw     = flex.double()
   rf     = flex.double()
   rfrw   = flex.double()
   deltab = flex.double()
   w      = flex.double()
   if(self.target_weights is not None):
     fmth ="    R-FACTORS      <Bi-Bj>  <B>   WEIGHT       TARGETS"
     print(fmth, file=self.log)
     print(" work  free  delta                           data restr", file=self.log)
   else:
     print("Unresrained refinement...", file=self.log)
   self.save_scatterers = self.fmodels.fmodel_xray().xray_structure.\
       deep_copy_scatterers().scatterers()
   if(self.target_weights is not None):
     default_weight = self.target_weights.adp_weights_result.wx*\
         self.target_weights.adp_weights_result.wx_scale
     if(self.target_weights.twp.optimize_adp_weight):
       wx_scale = [0.03,0.125,0.5,1.,1.5,2.,2.5,3.,3.5,4.,4.5,5.]
       trial_weights = list( flex.double(wx_scale)*self.target_weights.adp_weights_result.wx )
       self.wx_scale = 1
     else:
       trial_weights = [self.target_weights.adp_weights_result.wx]
       self.wx_scale = self.target_weights.adp_weights_result.wx_scale
   else:
     default_weight = 1
     trial_weights = [1]
     self.wx_scale = 1
   self.show(weight=default_weight)
   trial_results = []
   if nproc is None:
     nproc =  all_params.main.nproc
   parallel = False
   if (len(trial_weights) > 1) and ((nproc is Auto) or (nproc > 1)):
     parallel = True
     from libtbx import easy_mp
     stdout_and_results = easy_mp.pool_map(
       processes=nproc,
       fixed_func=self.try_weight,
       args=trial_weights,
       func_wrapper="buffer_stdout_stderr") # XXX safer for phenix GUI
     trial_results = [ r for so, r in stdout_and_results ]
   else :
     for weight in trial_weights:
       result = self.try_weight(weight, print_stats=True)
       trial_results.append(result)
   for result in trial_results :
     if(result is not None) and (result.r_work is not None):
       if (parallel):
         result.show(out=self.log)
       rw     .append(result.r_work)
       rf     .append(result.r_free)
       rfrw   .append(result.r_gap)
       deltab .append(result.delta_b)
       w      .append(result.weight)
   #
   if(len(trial_weights)>1 and rw.size()>0):
     # filter by rfree-rwork
     rw,rf,rfrw,deltab,w = self.score(rw=rw,rf=rf,rfrw=rfrw,deltab=deltab,w=w,
       score_target=rfrw,score_target_value=r_free_r_work_gap,
       secondary_target=deltab)
     # filter by rfree
     rw,rf,rfrw,deltab,w = self.score(rw=rw,rf=rf,rfrw=rfrw,deltab=deltab,w=w,
       score_target=rf,score_target_value=flex.min(rf)+r_free_range_width)
     # filter by <Bi-Bj>
     delta_b_target = max(min_diff_b_iso_bonded, flex.mean(self.fmodels.
       fmodel_xray().xray_structure.extract_u_iso_or_u_equiv()*
         adptbx.u_as_b(1))*mean_diff_b_iso_bonded_fraction)
     print("  max suggested <Bi-Bj> for this run: %7.2f"%delta_b_target, file=log)
     print("  max allowed Rfree-Rwork gap: %5.1f"%r_free_r_work_gap, file=log)
     print("  range of equivalent Rfree: %5.1f"%r_free_range_width, file=log)
     rw,rf,rfrw,deltab,w = self.score(rw=rw,rf=rf,rfrw=rfrw,deltab=deltab,w=w,
       score_target=deltab,score_target_value=delta_b_target)
     # select the result with lowest rfree
     sel = flex.sort_permutation(rf)
     rw,rf,rfrw,deltab,w= self.select(
       rw=rw,rf=rf,rfrw=rfrw,deltab=deltab,w=w,sel=sel)
     #
     w_best = w[0]
     rw_best = rw[0]
     print("Best ADP weight: %8.3f"%w_best, file=self.log)
     #
     self.target_weights.adp_weights_result.wx = w_best
     self.target_weights.adp_weights_result.wx_scale = 1
     best_u_star = None
     best_u_iso = None
     for result in trial_results :
       if(abs(result.weight-w_best)<=1.e-8):
         best_u_star = result.u_star
         best_u_iso = result.u_iso
         break
     if(best_u_iso is None) : # XXX this probably shouldn't happen...
       self.fmodels.fmodel_xray().xray_structure.replace_scatterers(
         self.save_scatterers.deep_copy())
     else :
       assert (best_u_star is not None)
       xrs = self.fmodels.fmodel_xray().xray_structure
       xrs.set_u_iso(values=best_u_iso)
       xrs.scatterers().set_u_star(best_u_star)
       new_u_iso = xrs.scatterers().extract_u_iso()
       assert (new_u_iso.all_eq(best_u_iso))
     self.fmodels.update_xray_structure(
       xray_structure = self.fmodels.fmodel_xray().xray_structure,
       update_f_calc  = True)
     print("Accepted refinement result:", file=self.log)
     # reset alpha/beta parameters - if this is not done, the assertion
     # below will fail
     fmodels.create_target_functors()
     if(self.fmodels.fmodel_neutron() is None):
       assert approx_equal(self.fmodels.fmodel_xray().r_work()*100, rw_best,
         eps=0.001)
       # this needs to be done again again, just in case
       fmodels.create_target_functors()
     self.show(weight=w_best)
   self.fmodels.fmodel_xray().xray_structure.tidy_us()
   self.fmodels.update_xray_structure(
     xray_structure = self.fmodels.fmodel_xray().xray_structure,
     update_f_calc  = True)
   fmodels.create_target_functors()
   assert approx_equal(self.fmodels.fmodel_xray().target_w(),
      self.fmodels.target_functor_result_xray(
        compute_gradients=False).target_work())
   self.model.set_xray_structure(self.fmodels.fmodel_xray().xray_structure)
Example #12
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 run(args,
        crystal_symmetry=None,
        ncs_object=None,
        pdb_hierarchy=None,
        map_data=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
    inputs = mmtbx.utils.process_command_line_args(args=args,
                                                   cmd_cs=crystal_symmetry,
                                                   master_params=master_phil)
    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 pdb_hierarchy and not params.keep_map_size
            and not params.upper_bounds and not params.extract_unique):
        raise Sorry("PDB file is needed unless extract_unique, " +
                    "density_select, keep_map_size \nor bounds are set .")
    if (len(inputs.pdb_file_names)!=1 and not pdb_hierarchy and \
         (params.mask_atoms or params.soft_mask )):
        raise Sorry("PDB file is needed for mask_atoms or soft_mask")
    if (params.density_select and params.keep_map_size):
        raise Sorry("Cannot set both density_select and keep_map_size")
    if (params.density_select and params.upper_bounds):
        raise Sorry("Cannot set both density_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 and not params.resolution):
        raise Sorry("Please set resolution for extract_unique")
    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
    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)):
            if (params.ccp4_map_file is not None):
                af = any_file(params.ccp4_map_file)
                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.data  #map_data()
            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 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)
    #
    selection = pdb_hierarchy.atom_selection_cache().selection(
        string=params.selection)
    if selection.size():
        print_statistics.make_sub_header("atom selection", out=log)
        print >> log, "Selection string: selection='%s'" % params.selection
        print >> log, \
            "  selects %d atoms from total %d atoms."%(selection.count(True),
            selection.size())
    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 >> log, "Total of %s operators read" % (
                ncs_object.max_operators())
    if not ncs_object or ncs_object.max_operators() < 1:
        print >> log, "No symmetry available"
    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:
        if params.sequence_file:
            if n_ops > 1:  # get unique part of sequence and multiply
                remove_duplicates = True
            else:
                remove_duplicates = False
            from iotbx.bioinformatics import get_sequences
            sequence = n_ops * (" ".join(
                get_sequences(file_name=params.sequence_file,
                              remove_duplicates=remove_duplicates)))

        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_protein * 110 + (n_rna + n_dna) * 330
        elif not params.molecular_mass:
            raise Sorry(
                "Need a sequence file or molecular mass for extract_unique")
    else:
        molecular_mass = None
#
    if params.density_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 >> log, "\nValue outside atoms mask will be set to mean inside mask"
    if params.get_half_height_width and params.density_select:
        print >> log, "\nHalf width at half height will be used to id boundaries"
    if params.soft_mask and sites_cart_all.size() > 0:
        print >> log, "\nSoft mask will be applied to model-based mask"
    if params.keep_map_size:
        print >> log, "\nEntire map will be kept (not cutting out region)"
    if params.restrict_map_size:
        print >> log, "\nOutput map will be within input map"
    if params.lower_bounds and params.upper_bounds:
        print >> log, "Bounds for cut out map are (%s,%s,%s) to (%s,%s,%s)" % (
            tuple(list(params.lower_bounds) + list(params.upper_bounds)))

    box = mmtbx.utils.extract_box_around_model_and_map(
        xray_structure=xray_structure,
        map_data=map_data.as_double(),
        box_cushion=params.box_cushion,
        selection=selection,
        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,
        extract_unique=params.extract_unique,
        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,
    )

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

    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 >> log, "\nNOTE: Mismatch of unit cell parameters from CCP4 map:"
        print >>log,"Unit cell from CCP4 map 'unit cell parameters': "+\
          "%.1f, %.1f, %.1f, %.1f, %.1f, %.1f)" %tuple(
            inputs.ccp4_map.unit_cell_parameters)
        print >>log,"Unit cell from CCP4 map 'map grid':             "+\
          "%.1f, %.1f, %.1f, %.1f, %.1f, %.1f)" %tuple(
           inputs.crystal_symmetry.unit_cell().parameters())
        print >>log,"\nInterpreting this as the 'unit cell parameters' was "+\
          "original map \ndimension and 'map grid' is the "+\
          "portion actually in the map that was supplied here.\n"
        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

    # ncs_object is original
    #  box.ncs_object is shifted by shift_cart

    print >> log, "Box cell dimensions: (%.2f, %.2f, %.2f) A" % (
        box.box_crystal_symmetry.unit_cell().parameters()[:3])
    if params.keep_origin:
        print >> log, "Box origin is at grid position of : (%d, %d, %d) " % (
            tuple(box.origin_shift_grid_units(reverse=True)))
        print >> log, "Box origin is at coordinates: (%.2f, %.2f, %.2f) A" % (
            tuple(-col(box.shift_cart)))

    if box.pdb_outside_box_msg:
        print >> log, box.pdb_outside_box_msg

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

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

    if (not params.keep_origin):
        if box.shift_cart:
            print >>log,\
              "Final coordinate shift for output files: (%.2f,%.2f,%.2f) A" %(
              tuple(box.shift_cart))
        else:
            print >>log,"\nOutput files are in same location as original: origin "+\
              "is at (0,0,0)"
    else:  # keep_origin
        print >> log, "\nOutput files are in same location as original, just cut out."
        print >> log, "Note that output maps are only valid in the cut out region.\n"

    if params.keep_origin:
        ph_box_original_location = ph_box.deep_copy()
        sites_cart = box.shift_sites_cart_back(
            box.xray_structure_box.sites_cart())
        xrs_offset = ph_box_original_location.extract_xray_structure(
            crystal_symmetry=box.xray_structure_box.crystal_symmetry(
            )).replace_sites_cart(new_sites=sites_cart)
        ph_box_original_location.adopt_xray_structure(xrs_offset)
        box.hierarchy_original_location = ph_box_original_location
    else:
        box.hierarchy_original_location = None

    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

            if params.keep_origin:  # Keeping origin
                print >> log, "Writing boxed PDB with box unit cell and in "+\
                  "original\n    position to:   %s"%(
                  file_name)
                ph_box_original_location.write_pdb_file(
                    file_name=file_name,
                    crystal_symmetry=box.xray_structure_box.crystal_symmetry())

            else:  # write box PDB in box cell
                print >> log, "Writing shifted boxed PDB to file:   %s" % file_name
                ph_box.write_pdb_file(
                    file_name=file_name,
                    crystal_symmetry=box.xray_structure_box.crystal_symmetry())

        # Write NCS file if NCS
        if ncs_object and 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

            if params.keep_origin:
                if params.symmetry_file:
                    print >> log, "\nDuplicating symmetry in %s and writing to %s" % (
                        params.symmetry_file, output_symmetry_file)
                else:
                    print >> log, "\nWriting symmetry to %s" % (
                        output_symmetry_file)
                ncs_object.format_all_for_group_specification(
                    file_name=output_symmetry_file)

            else:
                print >> log, "\nOffsetting symmetry in %s and writing to %s" % (
                    params.symmetry_file, output_symmetry_file)
                box.ncs_object.format_all_for_group_specification(
                    file_name=output_symmetry_file)

        # Write ccp4 map.  Shift back to original location if keep_origin=True
        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

            if params.keep_origin:
                print >> log, "Writing boxed map with box unit_cell and "+\
                   "original\n    position to CCP4 formatted file:   %s"%file_name
            else:
                print >> log, "Writing box map shifted to (0,0,0) to CCP4 "+\
                   "formatted file:   %s"%file_name

            box.write_ccp4_map(file_name=file_name,
                               shift_back=params.keep_origin)

        # 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
            if params.keep_origin:
                print >> log, "Writing boxed map with box unit_cell and original "+\
                  "position\n    to X-plor formatted file: %s"%file_name
            else:
                print >> log, "Writing box_map shifted to (0,0,0) to X-plor "+\
                  "formatted file: %s"%file_name
            box.write_xplor_map(file_name=file_name,
                                shift_back=params.keep_origin)

        # 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

            if params.keep_origin:
                print >> log, "Writing map coefficients with box_map unit_cell"+\
                  " but position matching\n   "+\
                  " original position to MTZ file: %s"%file_name
            else:
                print >> log, "Writing box_map coefficients shifted to (0,0,0) "+\
                   "to MTZ file: %s"%file_name
            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=box.map_box,
                    unit_cell=box.xray_structure_box.unit_cell())
            box.map_coefficients(d_min=d_min,
                                 resolution_factor=params.resolution_factor,
                                 file_name=file_name,
                                 shift_back=params.keep_origin)

    print >> log
    return box
Example #14
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)
            nm_sel_bool = flex.bool(scatterers.size(), flex.size_t(nm_sel_st))
            ### totally ad hoc fix
            tmp_site_t = flex.size_t()
            for gs in group_adp_selections:
                #         print gs.size()
                for gs_ in gs:
                    tmp_site_t.append(gs_)
            ###
            if (macro_cycle == 1 or tmp_site_t.size() != scatterers.size()):
                gbr_selections = []
                #          print "gbr selections are based on nm selections"
                for s in nm_selections:
                    gbr_selections.append(s)
            else:
                gbr_selections = []
                for gs in group_adp_selections:
                    gbr_selection = flex.size_t()
                    for gs_ in gs:
                        if (nm_sel_bool[gs_]):
                            gbr_selection.append(gs_)
                    if (gbr_selection.size() > 0):
                        gbr_selections.append(gbr_selection)
            #print "group length ", len(group_adp_selections)
            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)
            if nm_params.group_b_refine:
                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 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
            if (macro_cycle <= nm_params.stabilize_cycle):
                tools.split_u(fmodels.fmodel_xray().xray_structure,
                              gbr_selections, offset)
            elif (macro_cycle <= nm_params.stabilize_cycle):
                u_eq = model.xray_structure_initial.extract_u_iso_or_u_equiv()
                nmtools.split_u(u_eq,
                                fmodels.fmodel_xray().xray_structure,
                                nm_selections, offset)
            else:
                u_eq = model.xray_structure.extract_u_iso_or_u_equiv()
                nmtools.split_u(u_eq,
                                fmodels.fmodel_xray().xray_structure,
                                nm_selections, offset)
            if not nm_params.update_target_function:
                fmodels.fmodel_xray().update(target_name="ls_wunit_k1")
            update_eigenvec = False
            if nm_params.update_evec and macro_cycle % nm_params.update_evec_per_cycle == 0:
                update_eigenvec = True
            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,
                fsub_evecin=nm_params.fsub_evecin,
                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,
                update_evec=update_eigenvec,
                enmcalc_path=nm_params.enmcalc_path,
                start_xs_value=nm_params.start_xs_value,
                zero_mode_corr=nm_params.zero_mode_corr,
                fsub_ref=nm_params.fsub_ref,
                fsub_n_modes=nm_params.fsub_n_modes,
                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


#       from mmtbx.command_line import find_tls_groups
#       tls_params = find_tls_groups.master_phil.fetch().extract()
#       tls_params.nproc = 1
#       tls_selections_str = find_tls_groups.find_tls(
#               params = tls_params,
#               pdb_inp = None,
#               pdb_hierarchy = model.pdb_hierarchy(),
#               xray_structure = model.xray_structure,
#               return_as_list = True,
#               out = log)
#       tls_selections = utils.get_atom_selections(
#               all_chain_proxies = model.all_chain_proxies,
#               selection_strings = tls_selections_str,
#               xray_structure = model.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()
Example #15
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.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.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) ):
      if (params.ccp4_map_file is not None):
        af = any_file(params.ccp4_map_file)
        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.data #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.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 #16
0
def run(args, crystal_symmetry=None, 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):
    print default_message
    master_phil.show(prefix="  ")
    return
  inputs = mmtbx.utils.process_command_line_args(args = args,
    cmd_cs=crystal_symmetry,
    master_params = master_phil)
  params = inputs.params.extract()
  # PDB file
  if params.pdb_file and not inputs.pdb_file_names:
    inputs.pdb_file_names=[params.pdb_file]
  if(len(inputs.pdb_file_names)!=1 and not params.density_select):
    raise Sorry("PDB file is needed unless density_select 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()
    pdb_atoms = pdb_hierarchy.atoms()
    pdb_atoms.reset_i_seq()
  else:
    pdb_hierarchy=None
  # Map or map coefficients
  map_coeff = None
  if(inputs.ccp4_map is None):
    if(len(inputs.reflection_file_names)!=1):
      raise Sorry("Map or map coefficients file is needed.")
    map_coeff = reflection_file_utils.extract_miller_array_from_file(
      file_name = inputs.reflection_file_names[0],
      label     = params.label,
      type      = "complex",
      log       = log)
    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])
  else:
    print_statistics.make_sub_header("CCP4 map", out=log)
    ccp4_map = inputs.ccp4_map
    ccp4_map.show_summary(prefix="  ")
    map_data = ccp4_map.map_data()
    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])
  #
  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)
  #
  selection = pdb_hierarchy.atom_selection_cache().selection(
    string = params.selection)
  if selection.size():
    print_statistics.make_sub_header("atom selection", out=log)
    print >> log, "Selection string: selection='%s'"%params.selection
    print >> log, \
        "  selects %d atoms from total %d atoms."%(selection.count(True),
        selection.size())
  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 params.density_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)
  #
  box = mmtbx.utils.extract_box_around_model_and_map(
    xray_structure   = xray_structure,
    map_data         = map_data.as_double(),
    box_cushion      = params.box_cushion,
    selection        = selection,
    density_select   = params.density_select,
    threshold        = params.density_select_threshold)

  if box.initial_shift_cart:
    print >>log,"\nInitial coordinate shift will be (%.1f,%.1f,%.1f)\n" %(
     box.initial_shift_cart)
  if box.total_shift_cart:
    print >>log,"Final coordinate shift: (%.1f,%.1f,%.1f)" %(
      box.total_shift_cart)

  print >>log,"Final cell dimensions: (%.1f,%.1f,%.1f)\n" %(
      box.box_crystal_symmetry.unit_cell().parameters()[:3])

  if box.pdb_outside_box_msg:
    print >> log, box.pdb_outside_box_msg

  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_box = pdb_hierarchy.select(selection)
  ph_box.adopt_xray_structure(box.xray_structure_box)
  ph_box.write_pdb_file(file_name=file_name, crystal_symmetry =
    box.xray_structure_box.crystal_symmetry())

  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
    print >> log, "writing map to CCP4 formatted file:   %s"%file_name
    box.write_ccp4_map(file_name=file_name)
  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
    print >> log, "writing map to X-plor formatted file: %s"%file_name
    box.write_xplor_map(file_name=file_name)
  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 >> log, "writing map coefficients to MTZ file: %s"%file_name
    if(map_coeff is not None): d_min = map_coeff.d_min()
    else:
      d_min = maptbx.d_min_from_map(map_data=box.map_box,
        unit_cell=box.xray_structure_box.unit_cell())
    box.map_coefficients(d_min=d_min,
      resolution_factor=params.resolution_factor, file_name=file_name)

  if params.ncs_file:

    if(params.output_file_name_prefix is None):
      output_ncs_file = "%s_box.ncs_spec"%output_prefix
    else: output_ncs_file = "%s.ncs_spec"%params.output_file_name_prefix
    print >>log,"\nOffsetting NCS in %s and writing to %s" %(
       params.ncs_file,output_ncs_file)
    from mmtbx.ncs.ncs import ncs
    ncs_object=ncs()
    ncs_object.read_ncs(params.ncs_file,log=log)
    ncs_object.display_all(log=log)
    if not ncs_object or ncs_object.max_operators()<1:
      print >>log,"Skipping...no NCS available"
    elif box.total_shift_cart:
      from scitbx.math import  matrix
      print >>log,"Shifting NCS operators "+\
        "based on coordinate shift of (%7.1f,%7.1f,%7.1f)" %(
        tuple(box.total_shift_cart))
      ncs_object=ncs_object.coordinate_offset(
       coordinate_offset=matrix.col(box.total_shift_cart))
      ncs_object.display_all(log=log)
    ncs_object.format_all_for_group_specification(
       file_name=output_ncs_file)
    box.ncs_object=ncs_object
  else:
    box.ncs_object=None
  print >> log
  return box
Example #17
0
           "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"
        new_output_format = []
        for x in params.output_format:
            if x != 'mtz':
                new_output_format.append(x)
        params.output_format = new_output_format

    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
    if (not map_data):
        # read first mtz file
        if ((len(inputs.reflection_file_names) > 0)
Example #18
0
 def __init__(self, fmodel,
                    selections = None,
                    params     = None,
                    r_initial  = None,
                    t_initial  = None,
                    bss        = None,
                    log        = None,
                    monitors   = None):
   global time_rigid_body_total
   self.params = params
   save_original_target_name = fmodel.target_name
   save_bss_anisotropic_scaling = None
   if(bss is not None):
     save_bss_anisotropic_scaling = bss.anisotropic_scaling
   timer_rigid_body_total = user_plus_sys_time()
   save_r_work = fmodel.r_work()
   save_r_free = fmodel.r_free()
   save_xray_structure = fmodel.xray_structure.deep_copy_scatterers()
   if(log is None): log = sys.stdout
   if(selections is None):
     selections = []
     selections.append(flex.bool(
       fmodel.xray_structure.scatterers().size(), True).iselection())
   else: assert len(selections) > 0
   fmodel.xray_structure.scatterers().flags_set_grads(state=False)
   fmodel.xray_structure.scatterers().flags_set_grad_site(
     iselection = flex.bool(fmodel.xray_structure.scatterers().size(), True
     ).iselection())
   self.total_rotation = []
   self.total_translation = []
   for item in selections:
       self.total_rotation.append(flex.double(3,0))
       self.total_translation.append(flex.double(3,0))
   if(r_initial is None):
      r_initial = []
      for item in selections:
          r_initial.append(flex.double(3,0))
   if(t_initial is None):
      t_initial = []
      for item in selections:
          t_initial.append(flex.double(3,0))
   fmodel_copy = fmodel.deep_copy()
   if(fmodel_copy.mask_params is not None):
      fmodel_copy.mask_params.verbose = -1
   d_mins, target_names = split_resolution_range(
     d_spacings                    = fmodel_copy.f_obs_work().d_spacings().data(),
     n_bodies                      = len(selections),
     target                        = params.target,
     target_auto_switch_resolution = params.target_auto_switch_resolution,
     n_ref_first                   = params.min_number_of_reflections,
     multi_body_factor_n_ref_first = params.multi_body_factor,
     d_low                         = params.max_low_high_res_limit,
     d_high                        = params.high_resolution,
     number_of_zones               = params.number_of_zones,
     zone_exponent                 = params.zone_exponent,
     log                           = log)
   print >> log
   if (fmodel.target_name != target_names[0]):
     fmodel.update(target_name=target_names[0])
   self.show(fmodel = fmodel,
             r_mat  = self.total_rotation,
             t_vec  = self.total_translation,
             header = "Start",
             out    = log)
   if (params.number_of_zones == 1 or monitors is None):
     monitors_call_back_handler = None
   else:
     monitors_call_back_handler = monitors.call_back_handler
     if (monitors_call_back_handler is not None):
       monitors_call_back_handler(
         monitor=None, model=None, fmodel=fmodel, method="rigid_body")
   for res,target_name in zip(d_mins, target_names):
       xrs = fmodel_copy.xray_structure.deep_copy_scatterers()
       fmodel_copy = fmodel.resolution_filter(d_min = res)
       if (fmodel_copy.target_name != target_name):
         fmodel_copy.update(target_name=target_name)
       d_max_min = fmodel_copy.f_obs_work().d_max_min()
       line = "Refinement at resolution: "+\
                str("%7.2f"%d_max_min[0]).strip() + " - " \
              + str("%6.2f"%d_max_min[1]).strip() \
              + " target=" + fmodel_copy.target_name
       print_statistics.make_sub_header(line, out = log)
       fmodel_copy.update_xray_structure(xray_structure = xrs,
                                         update_f_calc  = True)
       rworks = flex.double()
       if(len(d_mins) == 1):
          n_rigid_body_minimizer_cycles = 1
       else:
          n_rigid_body_minimizer_cycles = min(int(res),4)
       for i_macro_cycle in xrange(n_rigid_body_minimizer_cycles):
           if(bss is not None and params.bulk_solvent_and_scale):
              if(fmodel_copy.f_obs().d_min() > 3.0):
                 bss.anisotropic_scaling=False
              fast=True
              if(bss.mode=="slow"): fast=False
              fmodel_copy.update_all_scales(
                update_f_part1       = False,
                params               = bss,
                fast                 = fast,
                log                  = log,
                remove_outliers      = False,
                optimize_mask        = False,
                refine_hd_scattering = False)
              if(fmodel_copy.f_obs().d_min() > 3.0):
                 assert save_bss_anisotropic_scaling is not None
                 bss.anisotropic_scaling = save_bss_anisotropic_scaling
                 bss.minimization_b_cart = save_bss_anisotropic_scaling
           minimized = rigid_body_minimizer(
             fmodel                 = fmodel_copy,
             selections             = selections,
             r_initial              = r_initial,
             t_initial              = t_initial,
             refine_r               = params.refine_rotation,
             refine_t               = params.refine_translation,
             max_iterations         = params.max_iterations,
             euler_angle_convention = params.euler_angle_convention,
             lbfgs_maxfev = params.lbfgs_line_search_max_function_evaluations)
           rotation_matrices = []
           translation_vectors = []
           for i in xrange(len(selections)):
               self.total_rotation[i] += flex.double(minimized.r_min[i])
               self.total_translation[i] += flex.double(minimized.t_min[i])
               rot_obj = scitbx.rigid_body.euler(
                 phi        = minimized.r_min[i][0],
                 psi        = minimized.r_min[i][1],
                 the        = minimized.r_min[i][2],
                 convention = params.euler_angle_convention)
               rotation_matrices.append(rot_obj.rot_mat())
               translation_vectors.append(minimized.t_min[i])
           new_xrs = apply_transformation(
                        xray_structure      = minimized.fmodel.xray_structure,
                        rotation_matrices   = rotation_matrices,
                        translation_vectors = translation_vectors,
                        selections          = selections)
           fmodel_copy.update_xray_structure(xray_structure = new_xrs,
                                             update_f_calc  = True,
                                             update_f_mask  = True)
           rwork = minimized.fmodel.r_work()
           rfree = minimized.fmodel.r_free()
           assert approx_equal(rwork, fmodel_copy.r_work())
       fmodel.update_xray_structure(
         xray_structure = fmodel_copy.xray_structure,
         update_f_calc  = True,
         update_f_mask  = True)
       if(bss is not None and params.bulk_solvent_and_scale):
         fast=True
         if(bss.mode=="slow"): fast=False
         fmodel_copy.update_all_scales(
           update_f_part1       = False,
           params               = bss,
           fast                 = fast,
           log                  = log,
           remove_outliers      = False,
           optimize_mask        = False,
           refine_hd_scattering = False)
       self.show(fmodel = fmodel,
                 r_mat  = self.total_rotation,
                 t_vec  = self.total_translation,
                 header = "Rigid body refinement",
                 out    = log)
       if (monitors_call_back_handler is not None):
         monitors_call_back_handler(
           monitor=None, model=None, fmodel=fmodel, method="rigid_body")
   if(bss is not None and params.bulk_solvent_and_scale):
     fast=True
     if(bss.mode=="slow"): fast=False
     fmodel_copy.update_all_scales(
       update_f_part1       = False,
       params               = bss,
       fast                 = fast,
       log                  = log,
       remove_outliers      = False,
       optimize_mask        = False,
       refine_hd_scattering = False)
   print >> log
   self.show(fmodel = fmodel,
             r_mat  = self.total_rotation,
             t_vec  = self.total_translation,
             header = "Rigid body end",
             out    = log)
   print >> log
   self.evaluate_after_end(fmodel, save_r_work, save_r_free,
     save_xray_structure, log)
   self.fmodel = fmodel
   self.fmodel.update(target_name = save_original_target_name)
   time_rigid_body_total += timer_rigid_body_total.elapsed()
Example #19
0
def run(fmodel,
        geometry_restraints_manager,
        pdb_hierarchy,
        solvent_selection,
        optimize_hd=False,
        log=None,
        params=None):
    if (log is None): log = sys.stdout
    if (params is None): params = master_params().extract()
    print_statistics.make_sub_header(text="Fitting sidechains", out=log)
    mon_lib_srv = mmtbx.monomer_library.server.server()
    if (params.use_dihedral_restraints):
        sel = flex.bool(fmodel.xray_structure.scatterers().size(), True)
        geometry_restraints_manager = geometry_restraints_manager.select(sel)
        geometry_restraints_manager.remove_dihedrals_in_place(sel)
    restraints_manager = mmtbx.restraints.manager(
        geometry=geometry_restraints_manager, normalization=True)
    model = mmtbx.model.manager(restraints_manager=restraints_manager,
                                xray_structure=fmodel.xray_structure,
                                pdb_hierarchy=pdb_hierarchy)
    backbone_selections = model.backbone_selections()
    if (params.ignore_water_when_move_sidechains):
        selection = ~model.solvent_selection()
    else:
        selection = flex.bool(model.xray_structure.scatterers().size(), True)
    selection &= backbone_selections
    fmt = "Macro-cycle %2d: r_work=%6.4f r_free=%6.4f"
    print >> log, fmt % (0, fmodel.r_work(), fmodel.r_free())
    for macro_cycle in range(1, params.number_of_macro_cycles + 1):
        target_map_data, fft_map_1 = get_map_data(
            fmodel=fmodel,
            map_type=params.target_map,
            exclude_free_r_reflections=params.exclude_free_r_reflections)
        model_map_data, fft_map_2 = get_map_data(fmodel=fmodel,
                                                 map_type=params.model_map)
        residual_map_data, fft_map_3 = get_map_data(
            fmodel=fmodel,
            map_type=params.residual_map,
            exclude_free_r_reflections=params.exclude_free_r_reflections)
        if (params.filter_residual_map_value
                is not None):  #XXX use filtering....
            map_sel = flex.abs(
                residual_map_data) < params.filter_residual_map_value
            residual_map_data = residual_map_data.set_selected(map_sel, 0)
        if (params.filter_2fofc_map is not None):
            map_sel = flex.abs(target_map_data) < params.filter_2fofc_map
            target_map_data = target_map_data.set_selected(map_sel, 0)
        rsr_manager = refiner(
            pdb_hierarchy=pdb_hierarchy,
            target_map=target_map_data,
            geometry_restraints_manager=geometry_restraints_manager,
            real_space_target_weight=params.residue_iteration.
            real_space_refine_target_weight,
            real_space_gradients_delta=fmodel.f_obs().d_min() / 4,
            max_iterations=params.residue_iteration.
            real_space_refine_max_iterations)
        residue_rsr_monitor = residue_iteration(
            pdb_hierarchy=pdb_hierarchy,
            xray_structure=fmodel.xray_structure,
            selection=selection,
            target_map_data=target_map_data,
            model_map_data=model_map_data,
            residual_map_data=residual_map_data,
            mon_lib_srv=mon_lib_srv,
            rsr_manager=rsr_manager,
            optimize_hd=optimize_hd,
            params=params.residue_iteration,
            log=log)
        fmodel.update_xray_structure(update_f_calc=True, update_f_mask=True)
        print >> log, "1:", fmt % (macro_cycle, fmodel.r_work(),
                                   fmodel.r_free())
        del target_map_data, model_map_data, residual_map_data, fft_map_1, fft_map_2, fft_map_3
        if (params.validate_change):
            validate_changes(fmodel=fmodel,
                             residue_rsr_monitor=residue_rsr_monitor,
                             validate_method=None,
                             log=log)
Example #20
0
 def __init__(
           self,
           model,
           fmodels,
           target_weights,
           individual_adp_params,
           adp_restraints_params,
           h_params,
           log,
           all_params,
           nproc=None):
   adopt_init_args(self, locals())
   d_min = fmodels.fmodel_xray().f_obs().d_min()
   # initialize with defaults...
   if(target_weights is not None):
     import mmtbx.refinement.weights_params
     wcp = mmtbx.refinement.weights_params.tw_customizations_params.extract()
     for w_s_c in wcp.weight_selection_criteria:
       if(d_min >= w_s_c.d_min and d_min < w_s_c.d_max):
         r_free_range_width = w_s_c.r_free_range_width
         r_free_r_work_gap = w_s_c.r_free_minus_r_work
         mean_diff_b_iso_bonded_fraction = w_s_c.mean_diff_b_iso_bonded_fraction
         min_diff_b_iso_bonded = w_s_c.min_diff_b_iso_bonded
         break
     # ...then customize
     wsc = all_params.target_weights.weight_selection_criteria
     if(wsc.r_free_minus_r_work is not None):
       r_free_r_work_gap = wsc.r_free_minus_r_work
     if(wsc.r_free_range_width is not None):
       r_free_range_width = wsc.r_free_range_width
     if(wsc.mean_diff_b_iso_bonded_fraction is not None):
       mean_diff_b_iso_bonded_fraction = wsc.mean_diff_b_iso_bonded_fraction
     if(wsc.min_diff_b_iso_bonded is not None):
       min_diff_b_iso_bonded = wsc.min_diff_b_iso_bonded
   #
   print_statistics.make_sub_header(text="Individual ADP refinement", out = log)
   assert fmodels.fmodel_xray().xray_structure is model.xray_structure
   #
   fmodels.create_target_functors()
   assert approx_equal(self.fmodels.fmodel_xray().target_w(),
     self.fmodels.target_functor_result_xray(
       compute_gradients=False).target_work())
   rw     = flex.double()
   rf     = flex.double()
   rfrw   = flex.double()
   deltab = flex.double()
   w      = flex.double()
   if(self.target_weights is not None):
     fmth ="    R-FACTORS      <Bi-Bj>  <B>   WEIGHT       TARGETS"
     print >> self.log, fmth
     print >> self.log, " work  free  delta                           data restr"
   else:
     print >> self.log, "Unresrained refinement..."
   self.save_scatterers = self.fmodels.fmodel_xray().xray_structure.\
       deep_copy_scatterers().scatterers()
   if(self.target_weights is not None):
     default_weight = self.target_weights.adp_weights_result.wx*\
         self.target_weights.adp_weights_result.wx_scale
     if(self.target_weights.twp.optimize_adp_weight):
       wx_scale = [0.03,0.125,0.5,1.,1.5,2.,2.5,3.,3.5,4.,4.5,5.]
       trial_weights = list( flex.double(wx_scale)*self.target_weights.adp_weights_result.wx )
       self.wx_scale = 1
     else:
       trial_weights = [self.target_weights.adp_weights_result.wx]
       self.wx_scale = self.target_weights.adp_weights_result.wx_scale
   else:
     default_weight = 1
     trial_weights = [1]
     self.wx_scale = 1
   self.show(weight=default_weight)
   trial_results = []
   if nproc is None:
     nproc =  all_params.main.nproc
   parallel = False
   if (len(trial_weights) > 1) and ((nproc is Auto) or (nproc > 1)) :
     parallel = True
     from libtbx import easy_mp
     stdout_and_results = easy_mp.pool_map(
       processes=nproc,
       fixed_func=self.try_weight,
       args=trial_weights,
       func_wrapper="buffer_stdout_stderr") # XXX safer for phenix GUI
     trial_results = [ r for so, r in stdout_and_results ]
   else :
     for weight in trial_weights:
       result = self.try_weight(weight, print_stats=True)
       trial_results.append(result)
   for result in trial_results :
     if(result is not None) and (result.r_work is not None) :
       if (parallel) :
         result.show(out=self.log)
       rw     .append(result.r_work)
       rf     .append(result.r_free)
       rfrw   .append(result.r_gap)
       deltab .append(result.delta_b)
       w      .append(result.weight)
   #
   if(len(trial_weights)>1):
     # filter by rfree-rwork
     rw,rf,rfrw,deltab,w = self.score(rw=rw,rf=rf,rfrw=rfrw,deltab=deltab,w=w,
       score_target=rfrw,score_target_value=r_free_r_work_gap,
       secondary_target=deltab)
     # filter by rfree
     rw,rf,rfrw,deltab,w = self.score(rw=rw,rf=rf,rfrw=rfrw,deltab=deltab,w=w,
       score_target=rf,score_target_value=flex.min(rf)+r_free_range_width)
     # filter by <Bi-Bj>
     delta_b_target = max(min_diff_b_iso_bonded, flex.mean(self.fmodels.
       fmodel_xray().xray_structure.extract_u_iso_or_u_equiv()*
         adptbx.u_as_b(1))*mean_diff_b_iso_bonded_fraction)
     print >> log, "  max suggested <Bi-Bj> for this run: %7.2f"%delta_b_target
     print >> log, "  max allowed Rfree-Rwork gap: %5.1f"%r_free_r_work_gap
     print >> log, "  range of equivalent Rfree: %5.1f"%r_free_range_width
     rw,rf,rfrw,deltab,w = self.score(rw=rw,rf=rf,rfrw=rfrw,deltab=deltab,w=w,
       score_target=deltab,score_target_value=delta_b_target)
     # select the result with lowest rfree
     sel = flex.sort_permutation(rf)
     rw,rf,rfrw,deltab,w= self.select(
       rw=rw,rf=rf,rfrw=rfrw,deltab=deltab,w=w,sel=sel)
     #
     w_best = w[0]
     rw_best = rw[0]
     print >> self.log, "Best ADP weight: %8.3f"%w_best
     #
     self.target_weights.adp_weights_result.wx = w_best
     self.target_weights.adp_weights_result.wx_scale = 1
     best_u_star = None
     best_u_iso = None
     for result in trial_results :
       if(abs(result.weight-w_best)<=1.e-8) :
         best_u_star = result.u_star
         best_u_iso = result.u_iso
         break
     if(best_u_iso is None) : # XXX this probably shouldn't happen...
       self.fmodels.fmodel_xray().xray_structure.replace_scatterers(
         self.save_scatterers.deep_copy())
     else :
       assert (best_u_star is not None)
       xrs = self.fmodels.fmodel_xray().xray_structure
       xrs.set_u_iso(values=best_u_iso)
       xrs.scatterers().set_u_star(best_u_star)
       new_u_iso = xrs.scatterers().extract_u_iso()
       assert (new_u_iso.all_eq(best_u_iso))
     self.fmodels.update_xray_structure(
       xray_structure = self.fmodels.fmodel_xray().xray_structure,
       update_f_calc  = True)
     print >> self.log, "Accepted refinement result:"
     # reset alpha/beta parameters - if this is not done, the assertion
     # below will fail
     fmodels.create_target_functors()
     if(self.fmodels.fmodel_neutron() is None):
       assert approx_equal(self.fmodels.fmodel_xray().r_work()*100, rw_best,
         eps=0.001)
       # this needs to be done again again, just in case
       fmodels.create_target_functors()
     self.show(weight=w_best)
   self.fmodels.fmodel_xray().xray_structure.tidy_us()
   assert approx_equal(self.fmodels.fmodel_xray().target_w(),
      self.fmodels.target_functor_result_xray(
        compute_gradients=False).target_work())
   self.model.xray_structure = self.fmodels.fmodel_xray().xray_structure
Example #21
0
  def __init__(
            self,
            fmodels,
            model,
            params,
            target_weights,
            macro_cycle,
            ncs_manager=None,
            log=None):
    if log is None:
      log = sys.stdout
    # self.ncs_manager = ncs_manager
    self.nproc = params.main.nproc
    if self.nproc is Auto:
      self.nproc = 1
    self.verbose = params.den.verbose
    self.log = log
    self.fmodels = fmodels
    self.model = model
    self.params = params
    self.target_weights = target_weights
    self.adp_refinement_manager = None
    self.macro_cycle = macro_cycle
    self.tan_b_iso_max = 0
    self.random_seed = params.main.random_seed
    den_manager = model.restraints_manager. \
      geometry.den_manager
    print_statistics.make_header("DEN refinement", out=self.log)
    pdb_hierarchy = self.model.get_hierarchy(sync_with_xray_structure=True)
    if den_manager.get_n_proxies() == 0:
      print_statistics.make_sub_header(
        "DEN restraint nework", out = self.log)
      den_manager.build_den_proxies(pdb_hierarchy=pdb_hierarchy)
      den_manager.build_den_restraints()
      den_manager.show_den_summary(
        sites_cart=self.model.get_sites_cart())
    if den_manager.params.output_kinemage:
      den_manager.output_kinemage(
        self.model.get_sites_cart())
    print_statistics.make_sub_header(
      "coordinate minimization before annealing", out=self.log)
    self.minimize(ca_only=self.params.den.minimize_c_alpha_only)
    self.save_scatterers_local = fmodels.fmodel_xray().\
      xray_structure.deep_copy_scatterers().scatterers()
    #DEN refinement start, turn on
    if params.den.optimize:
      grid = den_manager.get_optimization_grid()
      print >> log, \
        "Running DEN torsion optimization on %d processors..." % \
        params.main.nproc
    else:
      grid = [(params.den.gamma, params.den.weight)]
    grid_results = []
    grid_so = []

    if "torsion" in params.den.annealing_type:
      print >> self.log, "Running torsion simulated annealing"
      if ( (params.den.optimize) and
           ( (self.nproc is Auto) or (self.nproc > 1) )):
        stdout_and_results = easy_mp.pool_map(
          processes=params.main.nproc,
          fixed_func=self.try_den_weight_torsion,
          args=grid,
          func_wrapper="buffer_stdout_stderr")
        for so, r in stdout_and_results:
          if (r is None):
            raise RuntimeError(("DEN weight optimization failed:"+
              "\n%s\nThis is a "+
              "serious error; please contact [email protected].") % so)
          grid_so.append(so)
          grid_results.append(r)
      else:
        for grid_pair in grid:
          result = self.try_den_weight_torsion(
                     grid_pair=grid_pair)
          grid_results.append(result)
      self.show_den_opt_summary_torsion(grid_results)
    elif "cartesian" in params.den.annealing_type:
      print >> self.log, "Running Cartesian simulated annealing"
      if ( (params.den.optimize) and
           ( (self.nproc is Auto) or (self.nproc > 1) )):
        stdout_and_results = easy_mp.pool_map(
          processes=params.main.nproc,
          fixed_func=self.try_den_weight_cartesian,
          args=grid,
          func_wrapper="buffer_stdout_stderr")
        for so, r in stdout_and_results:
          if (r is None):
            raise RuntimeError(("DEN weight optimization failed:"+
              "\n%s\nThis is a "+
              "serious error; please contact [email protected].") % so)
          grid_so.append(so)
          grid_results.append(r)
      else:
        for grid_pair in grid:
          result = self.try_den_weight_cartesian(
                     grid_pair=grid_pair)
          grid_results.append(result)
      self.show_den_opt_summary_cartesian(grid_results)
    else:
      raise "error in DEN annealing type"
    low_r_free = 1.0
    best_xray_structure = None
    best_eq_distances = None
    best_gamma =  None
    best_weight = None
    best_so_i = None
    for i, result in enumerate(grid_results):
      cur_r_free = result[2]
      if cur_r_free < low_r_free:
        low_r_free = cur_r_free
        best_gamma = result[0]
        best_weight = result[1]
        best_xray_structure = result[3]
        best_eq_distances = result[4]
        best_so_i = i
    assert best_xray_structure is not None
    if params.den.optimize:
      print >> self.log, "\nbest gamma: %.1f" % best_gamma
      print >> self.log, "best weight: %.1f\n" % best_weight
      if params.den.verbose:
        if len(grid_so) >= (best_so_i+1):
          print >> self.log, "\nBest annealing results:\n"
          print >> self.log, grid_so[best_so_i]
    fmodels.fmodel_xray().xray_structure.replace_scatterers(
      best_xray_structure.deep_copy())
    fmodels.update_xray_structure(
      xray_structure = fmodels.fmodel_xray().xray_structure,
      update_f_calc  = True)
    utils.assert_xray_structures_equal(
      x1 = fmodels.fmodel_xray().xray_structure,
      x2 = model.get_xray_structure())
    model.restraints_manager.geometry.\
      den_manager.import_eq_distances(eq_distances=best_eq_distances)
    self.model.torsion_ncs_restraints_update(log=self.log)
Example #22
0
def find_tls (params,
              pdb_inp,
              pdb_hierarchy,
              xray_structure,
              return_as_list=False,
              ignore_pdb_header_groups=False,
              out=None) :
  if (out is None) :
    out = sys.stdout
  print_statistics.make_header("Analyzing inputs", out=out)
  if (params.random_seed is None) :
    params.random_seed = flex.get_random_seed()
  random.seed(params.random_seed)
  flex.set_random_seed(params.random_seed)
  xray_structure.convert_to_isotropic()
  sites_cart = xray_structure.sites_cart()
  u_cart = None
  u_iso  = xray_structure.extract_u_iso_or_u_equiv()#*adptbx.u_as_b(1.) # ?
  bad_i_seqs = check_adp(u_iso=u_iso, out=out)
  if (bad_i_seqs is not None) :
    atoms = pdb_hierarchy.atoms()
    bad_atom_strings = []
    for i_seq in bad_i_seqs[:10] :
      atom_str = atoms[i_seq].format_atom_record()
      bad_atom_strings.append(atom_str)
    if (len(bad_i_seqs) > 10) :
      bad_atom_strings.append("... (remaining %d not shown)" %
        (len(bad_i_seqs)-10))
    raise Sorry(("%d atoms in the model contain isotropic B-factors <= 0:\n"+
      "\n".join(bad_atom_strings)) % (len(bad_i_seqs)))
  #
  ssm = mmtbx.secondary_structure.manager(
    pdb_hierarchy                = pdb_hierarchy,
    sec_str_from_pdb_file        = None,
    params                       = None)
  alpha_h_selection = ssm.alpha_selection()
  secondary_structure_selection = ssm.alpha_selection() | \
      ssm.beta_selection() | ssm.base_pair_selection()
  if(u_cart is not None):
    assert secondary_structure_selection.size() == u_cart.size()
  else:
    assert secondary_structure_selection.size() == u_iso.size()
  ssm.show_summary(log=out)
  chains_and_residue_selections, secondary_structure_selection = chains_and_atoms(
    pdb_hierarchy                 = pdb_hierarchy,
    secondary_structure_selection = secondary_structure_selection,
    out                           = out)
  chains_and_permutations = []
  chains_and_atom_selection_strings = []
  print_statistics.make_header("Processing chains", out=out)
  if (params.nproc is None) :
    params.nproc = 1
  for crs in chains_and_residue_selections:
    print_statistics.make_sub_header("Processing chain '%s'"%crs[0],
      out=out)
    chain_selection = chain_selection_from_residues(crs[1])
    groups, perms = get_model_partitioning(residues = crs[1],
      secondary_structure_selection = secondary_structure_selection,
      out = out)
    #
    #print
    #selection_arrays = sels_as_selection_arrays(sels = groups)
    #merge_groups_by_connectivity(
    #  pdb_hierarchy     = pdb_hierarchy,
    #  xray_structure    = xray_structure,
    #  selection_arrays  = selection_arrays)
    #assert 0
    #
    if(len(perms)==1):
      print >> out, "  Whole chain is considered as one TLS group."
      chains_and_atom_selection_strings.append([crs[0],
        permutations_as_atom_selection_string(groups, perms[0])])
    else:
      print >> out, "  Fitting TLS matrices..."
      dic = {}
      target_best = 1.e+9
      if (params.nproc is Auto) or (params.nproc > 1) :
        process_perms = analyze_permutations(
          groups=groups,
          sites_cart=sites_cart,
          u_cart=u_cart,
          u_iso=u_iso)
        from libtbx import easy_mp
        stdout_and_targets = easy_mp.pool_map(
          processes=params.nproc,
          fixed_func=process_perms,
          args=perms,
          chunksize=100,
          func_wrapper="buffer_stdout_stderr")
        targets = [ t for so, t in stdout_and_targets ]
        for (perm, target) in zip(perms, targets) :
          dic.setdefault(len(perm), []).append([target,perm])
      else :
        for i_perm, perm in enumerate(perms):
          if i_perm%500==0:
            print >> out, "    ...perm %d of %d"%(i_perm, len(perms))
          selections = tls_group_selections(groups, perm)
          target = 0
          for selection in selections:
            mo = tls_refinery(
              u_cart     = u_cart,
              u_iso      = u_iso,
              sites_cart = sites_cart,
              selection  = selection)
            target += mo.f
          dic.setdefault(len(perm), []).append([target,perm])
        #print "    perm %d of %d: target=%8.3f (TLS groups: %s), permutation:"%(
        #  i_perm, len(perms),target,len(perm)),perm
      print >> out, "    Best fits:"
      print >> out, "      No. of         Targets"
      print >> out, "      groups   best   rand.pick  diff.  score permutation"
      score_best = -1.e+9
      perm_choice = None
      for k, v in zip(dic.keys(),dic.values()):
        t_best = v[0][0]
        perm_best = v[0][1]
        for v_ in v:
          if(v_[0]<t_best):
            t_best = v_[0]
            perm_best = v_[1]
        if(u_cart is not None):
          u_cart_ = u_cart.select(chain_selection)
        else: u_cart_ = None
        if(u_iso is not None):
          u_iso_ = u_iso.select(chain_selection)
        else: u_iso_ = None
        r = tls_refinery_random_groups(
          u_cart     = u_cart_,
          u_iso      = u_iso_,
          sites_cart = sites_cart.select(chain_selection),
          n_groups   = k)
        score = (r-t_best)/(r+t_best)*100.
        print >> out, "         %3d %6.3f      %6.3f %6.2f %6.3f"%(
          k,t_best, r, r-t_best, score), perm_best
        if(score > score_best):
          score_best = score
          perm_choice = perm_best[:]
      #
      chains_and_permutations.append([crs[0],perm_choice])
      chains_and_atom_selection_strings.append([crs[0],
        permutations_as_atom_selection_string(groups, perm_choice)])
      #
  if (pdb_inp is not None) and (not ignore_pdb_header_groups) :
    external_tls_selections = external_tls(
      pdb_inp       = pdb_inp,
      pdb_hierarchy = pdb_hierarchy,
      sites_cart    = sites_cart,
      u_iso         = u_iso,
      out           = out)
  print_statistics.make_header("SUMMARY", out=out)
  #print "Optimal TLS groups:"
  #for chain_and_permutation in chains_and_permutations:
  #  print chain_and_permutation
  #print
  print >> out, "TLS atom selections for phenix.refine:"
  groups_out = cStringIO.StringIO()
  selection_strings = []
  print >> groups_out, "refinement.refine.adp {"
  for r in chains_and_atom_selection_strings:
    prefix = "chain '%s'"%r[0]
    if(len(r[1])>0 and len(r[1:])>0):
      prefix += " and "
      for r_ in r[1:]:
        for r__ in r_:
          if(len(r__)>0):
            group_selection = prefix+"(%s)"%r__
            print >> groups_out, "  tls = \"%s\"" % group_selection
            selection_strings.append("%s" % group_selection)
    else:
      print >> groups_out, "  tls = \"%s\"" % prefix
      selection_strings.append("%s" % prefix)
  print >> groups_out, "}"
  print >> out, groups_out.getvalue()
  print >> out
  #XXX
  if 0:
    merge_groups_by_connectivity(
      pdb_hierarchy     = pdb_hierarchy,
      xray_structure    = xray_structure,
      selection_strings = selection_strings)
  #XXX
  if(len(selection_strings)>0):
    total_target = total_score(
      pdb_hierarchy     = pdb_hierarchy,
      sites_cart        = sites_cart,
      u_iso             = u_iso,
      selection_strings = selection_strings)
    print >> out, "Overall best total target for automatically found groups: %10.1f"%total_target
    print >> out
  if (return_as_list) :
    return selection_strings
  else :
    return groups_out.getvalue()
Example #23
0
def run(fmodel,
        geometry_restraints_manager,
        pdb_hierarchy,
        solvent_selection,
        optimize_hd = False,
        log = None,
        params = None):
  if(log is None): log = sys.stdout
  if(params is None): params = master_params().extract()
  print_statistics.make_sub_header(text="Fitting sidechains", out=log)
  mon_lib_srv = mmtbx.monomer_library.server.server()
  if(params.use_dihedral_restraints):
    sel = flex.bool(fmodel.xray_structure.scatterers().size(), True)
    geometry_restraints_manager = geometry_restraints_manager.select(sel)
    geometry_restraints_manager.remove_dihedrals_in_place(sel)
  restraints_manager = mmtbx.restraints.manager(
    geometry      = geometry_restraints_manager,
    normalization = True)
  model = mmtbx.model.manager(
    restraints_manager = restraints_manager,
    xray_structure = fmodel.xray_structure,
    pdb_hierarchy = pdb_hierarchy)
  backbone_selections = model.backbone_selections()
  if(params.ignore_water_when_move_sidechains):
    selection = ~model.solvent_selection()
  else:
    selection = flex.bool(model.xray_structure.scatterers().size(), True)
  selection &= backbone_selections
  fmt = "Macro-cycle %2d: r_work=%6.4f r_free=%6.4f"
  print >> log, fmt%(0, fmodel.r_work(), fmodel.r_free())
  for macro_cycle in range(1,params.number_of_macro_cycles+1):
    target_map_data, fft_map_1 = get_map_data(fmodel = fmodel,
      map_type = params.target_map,
      exclude_free_r_reflections = params.exclude_free_r_reflections)
    model_map_data,fft_map_2 = get_map_data(fmodel = fmodel,
      map_type = params.model_map)
    residual_map_data,fft_map_3 = get_map_data(fmodel = fmodel,
      map_type = params.residual_map,
      exclude_free_r_reflections = params.exclude_free_r_reflections)
    if(params.filter_residual_map_value is not None): #XXX use filtering....
      map_sel = flex.abs(residual_map_data) < params.filter_residual_map_value
      residual_map_data = residual_map_data.set_selected(map_sel, 0)
    if(params.filter_2fofc_map is not None):
      map_sel = flex.abs(target_map_data) < params.filter_2fofc_map
      target_map_data = target_map_data.set_selected(map_sel, 0)
    rsr_manager = refiner(
      pdb_hierarchy               = pdb_hierarchy,
      target_map                  = target_map_data,
      geometry_restraints_manager = geometry_restraints_manager,
      real_space_target_weight    = params.residue_iteration.real_space_refine_target_weight,
      real_space_gradients_delta  = fmodel.f_obs().d_min()/4,
      max_iterations              = params.residue_iteration.real_space_refine_max_iterations)
    residue_rsr_monitor = residue_iteration(
      pdb_hierarchy     = pdb_hierarchy,
      xray_structure    = fmodel.xray_structure,
      selection         = selection,
      target_map_data   = target_map_data,
      model_map_data    = model_map_data,
      residual_map_data = residual_map_data,
      mon_lib_srv       = mon_lib_srv,
      rsr_manager       = rsr_manager,
      optimize_hd       = optimize_hd,
      params            = params.residue_iteration,
      log               = log)
    fmodel.update_xray_structure(update_f_calc=True, update_f_mask=True)
    print >> log, "1:", fmt%(macro_cycle, fmodel.r_work(), fmodel.r_free())
    del target_map_data, model_map_data, residual_map_data, fft_map_1, fft_map_2, fft_map_3
    if(params.validate_change):
      validate_changes(fmodel = fmodel,
                       residue_rsr_monitor = residue_rsr_monitor,
                       validate_method=None,
                       log = log)
Example #24
0
def run(args, crystal_symmetry=None,
     ncs_object=None,
     pdb_hierarchy=None,
     map_data=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
  inputs = mmtbx.utils.process_command_line_args(args = args,
    cmd_cs=crystal_symmetry,
    master_params = master_phil)
  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
    pdb_hierarchy and not params.keep_map_size and not params.upper_bounds
     and not params.extract_unique):
    raise Sorry("PDB file is needed unless extract_unique, "+
      "density_select, keep_map_size \nor bounds are set .")
  if (len(inputs.pdb_file_names)!=1 and not pdb_hierarchy and \
       (params.mask_atoms or params.soft_mask )):
    raise Sorry("PDB file is needed for mask_atoms or soft_mask")
  if (params.density_select and params.keep_map_size):
    raise Sorry("Cannot set both density_select and keep_map_size")
  if (params.density_select and params.upper_bounds):
    raise Sorry("Cannot set both density_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 and not params.resolution):
    raise Sorry("Please set resolution for extract_unique")
  if (write_output_files) and ("mtz" in params.output_format) and (
       (params.keep_origin) and (not params.keep_map_size)):
    raise Sorry("Please set output_format=ccp4 to skip mtz or set "+\
      "keep_origin=False or keep_map_size=True")

  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 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
  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) ):
      if (params.ccp4_map_file is not None):
        af = any_file(params.ccp4_map_file)
        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.data #map_data()
      input_unit_cell_grid=ccp4_map.unit_cell_grid
      input_unit_cell=ccp4_map.unit_cell_parameters

      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.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)
  #
  selection = pdb_hierarchy.atom_selection_cache().selection(
    string = params.selection)
  if selection.size():
    print_statistics.make_sub_header("atom selection", out=log)
    print >> log, "Selection string: selection='%s'"%params.selection
    print >> log, \
        "  selects %d atoms from total %d atoms."%(selection.count(True),
        selection.size())
  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 >>log,"Total of %s operators read" %(ncs_object.max_operators())
  if not ncs_object or ncs_object.max_operators()<1:
      print >>log,"No symmetry available"
  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:
    if params.sequence_file:
      if n_ops > 1: # get unique part of sequence and multiply
        remove_duplicates=True
      else:
        remove_duplicates=False
      from iotbx.bioinformatics import get_sequences
      sequence=n_ops * (" ".join(get_sequences(file_name=params.sequence_file,
        remove_duplicates=remove_duplicates)))

    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_protein*110+(n_rna+n_dna)*330
    elif not params.molecular_mass:
      raise Sorry("Need a sequence file or molecular mass for extract_unique")
  else:
    molecular_mass=None
#
  if params.density_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 >>log,"\nValue outside atoms mask will be set to mean inside mask"
  if params.get_half_height_width and params.density_select:
    print >>log,"\nHalf width at half height will be used to id boundaries"
  if params.soft_mask and sites_cart_all.size()>0:
    print >>log,"\nSoft mask will be applied to model-based mask"
  if params.keep_map_size:
    print >>log,"\nEntire map will be kept (not cutting out region)"
  if params.restrict_map_size:
    print >>log,"\nOutput map will be within input map"
  if params.lower_bounds and params.upper_bounds:
    print >>log,"Bounds for cut out map are (%s,%s,%s) to (%s,%s,%s)" %(
     tuple(list(params.lower_bounds)+list(params.upper_bounds)))

  box = mmtbx.utils.extract_box_around_model_and_map(
    xray_structure   = xray_structure,
    map_data         = map_data.as_double(),
    box_cushion      = params.box_cushion,
    selection        = selection,
    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,
    extract_unique        = params.extract_unique,
    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,

    )

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

  if (inputs and  # XXX fix or remove this
    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 >>log,"\nNOTE: Mismatch of unit cell parameters from CCP4 map:"
    print >>log,"Unit cell from CCP4 map 'unit cell parameters': "+\
      "%.1f, %.1f, %.1f, %.1f, %.1f, %.1f)" %tuple(
        inputs.ccp4_map.unit_cell_parameters)
    print >>log,"Unit cell from CCP4 map 'map grid':             "+\
      "%.1f, %.1f, %.1f, %.1f, %.1f, %.1f)" %tuple(
       inputs.crystal_symmetry.unit_cell().parameters())
    print >>log,"\nInterpreting this as the 'unit cell parameters' was "+\
      "original map \ndimension and 'map grid' is the "+\
      "portion actually in the map that was supplied here.\n"
    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 >> log, box.pdb_outside_box_msg

  # 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 >>log,"\nBox cell dimensions: (%.2f, %.2f, %.2f) A" %(
      box.box_crystal_symmetry.unit_cell().parameters()[:3])
  if box.shift_cart:
     print>>log,"Working origin moved from grid position of"+\
        ": (%d, %d, %d) to (0,0,0) " %(
        tuple(box.origin_shift_grid_units(reverse=True)))
     print>>log,"Working origin moved from  coordinates of:"+\
        " (%.2f, %.2f, %.2f) A to (0,0,0)\n" %(
         tuple(-col(box.shift_cart)))

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

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

  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 >>log,"Setting output unit cell parameters and unit cell grid to"+\
      " match\ninput map file"

  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 >>log,\
       "Output unit cell set to: %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)" %tuple(
        output_crystal_symmetry.unit_cell().parameters())
  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 >>log,\
       "Output unit cell grid set to: (%s, %s, %s)" %tuple(
        output_unit_cell_grid)

    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 >>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()))

  else:
    output_unit_cell_grid = map_data=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 >> log, "Writing boxed PDB with box unit cell to %s" %(
          file_name)

    # 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 >>log,"\nWriting symmetry to %s" %( output_symmetry_file)

    # 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
     output_box.write_ccp4_map(file_name=file_name,
       output_crystal_symmetry=output_crystal_symmetry,
       output_unit_cell_grid=output_unit_cell_grid,
       shift_back=shift_back)
     print >> log, "Writing boxed map "+\
          "to CCP4 formatted file:   %s"%file_name

    # 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 >> log, "Writing boxed map "+\
         "to X-plor formatted file: %s"%file_name

    # 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 >> log, "Writing map coefficients "+\
         "to MTZ file: %s"%file_name
     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,
       resolution_factor=params.resolution_factor, file_name=file_name,
       shift_back=shift_back)

  print >> log
  return box
Example #25
0
 def __init__(self,
              fmodel,
              selections=None,
              params=None,
              r_initial=None,
              t_initial=None,
              bss=None,
              log=None,
              monitors=None):
     global time_rigid_body_total
     self.params = params
     save_original_target_name = fmodel.target_name
     save_bss_anisotropic_scaling = None
     if (bss is not None):
         save_bss_anisotropic_scaling = bss.anisotropic_scaling
     timer_rigid_body_total = user_plus_sys_time()
     save_r_work = fmodel.r_work()
     save_r_free = fmodel.r_free()
     save_xray_structure = fmodel.xray_structure.deep_copy_scatterers()
     if (log is None): log = sys.stdout
     if (selections is None):
         selections = []
         selections.append(
             flex.bool(fmodel.xray_structure.scatterers().size(),
                       True).iselection())
     else:
         assert len(selections) > 0
     fmodel.xray_structure.scatterers().flags_set_grads(state=False)
     fmodel.xray_structure.scatterers().flags_set_grad_site(
         iselection=flex.bool(fmodel.xray_structure.scatterers().size(),
                              True).iselection())
     self.total_rotation = []
     self.total_translation = []
     for item in selections:
         self.total_rotation.append(flex.double(3, 0))
         self.total_translation.append(flex.double(3, 0))
     if (r_initial is None):
         r_initial = []
         for item in selections:
             r_initial.append(flex.double(3, 0))
     if (t_initial is None):
         t_initial = []
         for item in selections:
             t_initial.append(flex.double(3, 0))
     fmodel_copy = fmodel.deep_copy()
     if (fmodel_copy.mask_params is not None):
         fmodel_copy.mask_params.verbose = -1
     d_mins, target_names = split_resolution_range(
         d_spacings=fmodel_copy.f_obs_work().d_spacings().data(),
         n_bodies=len(selections),
         target=params.target,
         target_auto_switch_resolution=params.target_auto_switch_resolution,
         n_ref_first=params.min_number_of_reflections,
         multi_body_factor_n_ref_first=params.multi_body_factor,
         d_low=params.max_low_high_res_limit,
         d_high=params.high_resolution,
         number_of_zones=params.number_of_zones,
         zone_exponent=params.zone_exponent,
         log=log)
     print(file=log)
     if (fmodel.target_name != target_names[0]):
         fmodel.update(target_name=target_names[0])
     self.show(fmodel=fmodel,
               r_mat=self.total_rotation,
               t_vec=self.total_translation,
               header="Start",
               out=log)
     if (params.number_of_zones == 1 or monitors is None):
         monitors_call_back_handler = None
     else:
         monitors_call_back_handler = monitors.call_back_handler
         if (monitors_call_back_handler is not None):
             monitors_call_back_handler(monitor=None,
                                        model=None,
                                        fmodel=fmodel,
                                        method="rigid_body")
     for res, target_name in zip(d_mins, target_names):
         xrs = fmodel_copy.xray_structure.deep_copy_scatterers()
         fmodel_copy = fmodel.resolution_filter(d_min=res)
         if (fmodel_copy.target_name != target_name):
             fmodel_copy.update(target_name=target_name)
         d_max_min = fmodel_copy.f_obs_work().d_max_min()
         line = "Refinement at resolution: "+\
                  str("%7.2f"%d_max_min[0]).strip() + " - " \
                + str("%6.2f"%d_max_min[1]).strip() \
                + " target=" + fmodel_copy.target_name
         print_statistics.make_sub_header(line, out=log)
         fmodel_copy.update_xray_structure(xray_structure=xrs,
                                           update_f_calc=True)
         rworks = flex.double()
         if (len(d_mins) == 1):
             n_rigid_body_minimizer_cycles = 1
         else:
             n_rigid_body_minimizer_cycles = min(int(res), 4)
         for i_macro_cycle in range(n_rigid_body_minimizer_cycles):
             if (bss is not None and params.bulk_solvent_and_scale):
                 if (fmodel_copy.f_obs().d_min() > 3.0):
                     bss.anisotropic_scaling = False
                 fast = True
                 if (bss.mode == "slow"): fast = False
                 fmodel_copy.update_all_scales(update_f_part1=False,
                                               params=bss,
                                               fast=fast,
                                               log=log,
                                               remove_outliers=False,
                                               optimize_mask=False,
                                               refine_hd_scattering=False)
                 if (fmodel_copy.f_obs().d_min() > 3.0):
                     assert save_bss_anisotropic_scaling is not None
                     bss.anisotropic_scaling = save_bss_anisotropic_scaling
                     bss.minimization_b_cart = save_bss_anisotropic_scaling
             minimized = rigid_body_minimizer(
                 fmodel=fmodel_copy,
                 selections=selections,
                 r_initial=r_initial,
                 t_initial=t_initial,
                 refine_r=params.refine_rotation,
                 refine_t=params.refine_translation,
                 max_iterations=params.max_iterations,
                 euler_angle_convention=params.euler_angle_convention,
                 lbfgs_maxfev=params.
                 lbfgs_line_search_max_function_evaluations)
             rotation_matrices = []
             translation_vectors = []
             for i in range(len(selections)):
                 self.total_rotation[i] += flex.double(minimized.r_min[i])
                 self.total_translation[i] += flex.double(
                     minimized.t_min[i])
                 rot_obj = scitbx.rigid_body.euler(
                     phi=minimized.r_min[i][0],
                     psi=minimized.r_min[i][1],
                     the=minimized.r_min[i][2],
                     convention=params.euler_angle_convention)
                 rotation_matrices.append(rot_obj.rot_mat())
                 translation_vectors.append(minimized.t_min[i])
             new_xrs = apply_transformation(
                 xray_structure=minimized.fmodel.xray_structure,
                 rotation_matrices=rotation_matrices,
                 translation_vectors=translation_vectors,
                 selections=selections)
             fmodel_copy.update_xray_structure(xray_structure=new_xrs,
                                               update_f_calc=True,
                                               update_f_mask=True)
             rwork = minimized.fmodel.r_work()
             rfree = minimized.fmodel.r_free()
             assert approx_equal(rwork, fmodel_copy.r_work())
         fmodel.update_xray_structure(
             xray_structure=fmodel_copy.xray_structure,
             update_f_calc=True,
             update_f_mask=True)
         if (bss is not None and params.bulk_solvent_and_scale):
             fast = True
             if (bss.mode == "slow"): fast = False
             fmodel_copy.update_all_scales(update_f_part1=False,
                                           params=bss,
                                           fast=fast,
                                           log=log,
                                           remove_outliers=False,
                                           optimize_mask=False,
                                           refine_hd_scattering=False)
         self.show(fmodel=fmodel,
                   r_mat=self.total_rotation,
                   t_vec=self.total_translation,
                   header="Rigid body refinement",
                   out=log)
         if (monitors_call_back_handler is not None):
             monitors_call_back_handler(monitor=None,
                                        model=None,
                                        fmodel=fmodel,
                                        method="rigid_body")
     if (bss is not None and params.bulk_solvent_and_scale):
         fast = True
         if (bss.mode == "slow"): fast = False
         fmodel_copy.update_all_scales(update_f_part1=False,
                                       params=bss,
                                       fast=fast,
                                       log=log,
                                       remove_outliers=False,
                                       optimize_mask=False,
                                       refine_hd_scattering=False)
     print(file=log)
     self.show(fmodel=fmodel,
               r_mat=self.total_rotation,
               t_vec=self.total_translation,
               header="Rigid body end",
               out=log)
     print(file=log)
     self.evaluate_after_end(fmodel, save_r_work, save_r_free,
                             save_xray_structure, log)
     self.fmodel = fmodel
     self.fmodel.update(target_name=save_original_target_name)
     time_rigid_body_total += timer_rigid_body_total.elapsed()
Example #26
0
def run(args,
        crystal_symmetry=None,
        pdb_hierarchy=None,
        map_data=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
    inputs = mmtbx.utils.process_command_line_args(args=args,
                                                   cmd_cs=crystal_symmetry,
                                                   master_params=master_phil)
    params = inputs.params.extract()
    # 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 pdb_hierarchy):
        raise Sorry("PDB file is needed unless density_select 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
    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)
            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)):
            if (params.ccp4_map_file is not None):
                af = any_file(params.ccp4_map_file)
                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)
            map_data = ccp4_map.data  #map_data()
            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

    # 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)
    #
    selection = pdb_hierarchy.atom_selection_cache().selection(
        string=params.selection)
    if selection.size():
        print_statistics.make_sub_header("atom selection", out=log)
        print >> log, "Selection string: selection='%s'" % params.selection
        print >> log, \
            "  selects %d atoms from total %d atoms."%(selection.count(True),
            selection.size())
    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 params.density_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 >> log, "\nValue outside atoms mask will be set to mean inside mask"
    if params.get_half_height_width:
        print >> log, "\nHalf width at half height will be used to id boundaries"

    box = mmtbx.utils.extract_box_around_model_and_map(
        xray_structure=xray_structure,
        map_data=map_data.as_double(),
        box_cushion=params.box_cushion,
        selection=selection,
        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,
    )

    if box.initial_shift_cart:
        print >> log, "\nInitial coordinate shift will be (%.1f,%.1f,%.1f)\n" % (
            box.initial_shift_cart)
    if box.total_shift_cart:
        print >> log, "Final coordinate shift: (%.1f,%.1f,%.1f)" % (
            box.total_shift_cart)

    print >> log, "Final cell dimensions: (%.1f,%.1f,%.1f)\n" % (
        box.box_crystal_symmetry.unit_cell().parameters()[:3])

    if box.pdb_outside_box_msg:
        print >> log, box.pdb_outside_box_msg

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

    box.hierarchy = ph_box
    if not write_output_files:
        return box

    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_box = pdb_hierarchy.select(selection)
    ph_box.adopt_xray_structure(box.xray_structure_box)
    ph_box.write_pdb_file(
        file_name=file_name,
        crystal_symmetry=box.xray_structure_box.crystal_symmetry())

    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
        print >> log, "writing map to CCP4 formatted file:   %s" % file_name
        box.write_ccp4_map(file_name=file_name)
    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
        print >> log, "writing map to X-plor formatted file: %s" % file_name
        box.write_xplor_map(file_name=file_name)
    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 >> log, "writing map coefficients to MTZ file: %s" % file_name
        if (map_coeff is not None): d_min = map_coeff.d_min()
        else:
            d_min = maptbx.d_min_from_map(
                map_data=box.map_box,
                unit_cell=box.xray_structure_box.unit_cell())
        box.map_coefficients(d_min=d_min,
                             resolution_factor=params.resolution_factor,
                             file_name=file_name)

    if params.ncs_file:

        if (params.output_file_name_prefix is None):
            output_ncs_file = "%s_box.ncs_spec" % output_prefix
        else:
            output_ncs_file = "%s.ncs_spec" % params.output_file_name_prefix
        print >> log, "\nOffsetting NCS in %s and writing to %s" % (
            params.ncs_file, output_ncs_file)
        from mmtbx.ncs.ncs import ncs
        ncs_object = ncs()
        ncs_object.read_ncs(params.ncs_file, log=log)
        ncs_object.display_all(log=log)
        if not ncs_object or ncs_object.max_operators() < 1:
            print >> log, "Skipping...no NCS available"
        elif box.total_shift_cart:
            from scitbx.math import matrix
            print >>log,"Shifting NCS operators "+\
              "based on coordinate shift of (%7.1f,%7.1f,%7.1f)" %(
              tuple(box.total_shift_cart))
            ncs_object = ncs_object.coordinate_offset(
                coordinate_offset=matrix.col(box.total_shift_cart))
            ncs_object.display_all(log=log)
        ncs_object.format_all_for_group_specification(
            file_name=output_ncs_file)
        box.ncs_object = ncs_object
    else:
        box.ncs_object = None
    print >> log
    return box
Example #27
0
  def __init__(
            self,
            fmodels,
            model,
            params,
            target_weights,
            macro_cycle,
            ncs_manager=None,
            log=None):
    if log is None:
      log = sys.stdout
    # self.ncs_manager = ncs_manager
    self.nproc = params.main.nproc
    if self.nproc is Auto:
      self.nproc = 1
    self.verbose = params.den.verbose
    self.log = log
    self.fmodels = fmodels
    self.model = model
    self.params = params
    self.target_weights = target_weights
    self.adp_refinement_manager = None
    self.macro_cycle = macro_cycle
    self.tan_b_iso_max = 0
    self.random_seed = params.main.random_seed
    den_manager = model.restraints_manager. \
      geometry.den_manager
    print_statistics.make_header("DEN refinement", out=self.log)
    pdb_hierarchy = self.model.pdb_hierarchy(sync_with_xray_structure=True)
    if den_manager.get_n_proxies() == 0:
      print_statistics.make_sub_header(
        "DEN restraint nework", out = self.log)
      den_manager.build_den_proxies(pdb_hierarchy=pdb_hierarchy)
      den_manager.build_den_restraints()
      den_manager.show_den_summary(
        sites_cart=self.model.xray_structure.sites_cart())
    if den_manager.params.output_kinemage:
      den_manager.output_kinemage(
        self.model.xray_structure.sites_cart())
    print_statistics.make_sub_header(
      "coordinate minimization before annealing", out=self.log)
    self.minimize(ca_only=self.params.den.minimize_c_alpha_only)
    self.save_scatterers_local = fmodels.fmodel_xray().\
      xray_structure.deep_copy_scatterers().scatterers()
    #DEN refinement start, turn on
    if params.den.optimize:
      grid = den_manager.get_optimization_grid()
      print >> log, \
        "Running DEN torsion optimization on %d processors..." % \
        params.main.nproc
    else:
      grid = [(params.den.gamma, params.den.weight)]
    grid_results = []
    grid_so = []

    if "torsion" in params.den.annealing_type:
      print >> self.log, "Running torsion simulated annealing"
      if ( (params.den.optimize) and
           ( (self.nproc is Auto) or (self.nproc > 1) )):
        stdout_and_results = easy_mp.pool_map(
          processes=params.main.nproc,
          fixed_func=self.try_den_weight_torsion,
          args=grid,
          func_wrapper="buffer_stdout_stderr")
        for so, r in stdout_and_results:
          if (r is None):
            raise RuntimeError(("DEN weight optimization failed:"+
              "\n%s\nThis is a "+
              "serious error; please contact [email protected].") % so)
          grid_so.append(so)
          grid_results.append(r)
      else:
        for grid_pair in grid:
          result = self.try_den_weight_torsion(
                     grid_pair=grid_pair)
          grid_results.append(result)
      self.show_den_opt_summary_torsion(grid_results)
    elif "cartesian" in params.den.annealing_type:
      print >> self.log, "Running Cartesian simulated annealing"
      if ( (params.den.optimize) and
           ( (self.nproc is Auto) or (self.nproc > 1) )):
        stdout_and_results = easy_mp.pool_map(
          processes=params.main.nproc,
          fixed_func=self.try_den_weight_cartesian,
          args=grid,
          func_wrapper="buffer_stdout_stderr")
        for so, r in stdout_and_results:
          if (r is None):
            raise RuntimeError(("DEN weight optimization failed:"+
              "\n%s\nThis is a "+
              "serious error; please contact [email protected].") % so)
          grid_so.append(so)
          grid_results.append(r)
      else:
        for grid_pair in grid:
          result = self.try_den_weight_cartesian(
                     grid_pair=grid_pair)
          grid_results.append(result)
      self.show_den_opt_summary_cartesian(grid_results)
    else:
      raise "error in DEN annealing type"
    low_r_free = 1.0
    best_xray_structure = None
    best_eq_distances = None
    best_gamma =  None
    best_weight = None
    best_so_i = None
    for i, result in enumerate(grid_results):
      cur_r_free = result[2]
      if cur_r_free < low_r_free:
        low_r_free = cur_r_free
        best_gamma = result[0]
        best_weight = result[1]
        best_xray_structure = result[3]
        best_eq_distances = result[4]
        best_so_i = i
    assert best_xray_structure is not None
    if params.den.optimize:
      print >> self.log, "\nbest gamma: %.1f" % best_gamma
      print >> self.log, "best weight: %.1f\n" % best_weight
      if params.den.verbose:
        if len(grid_so) >= (best_so_i+1):
          print >> self.log, "\nBest annealing results:\n"
          print >> self.log, grid_so[best_so_i]
    fmodels.fmodel_xray().xray_structure.replace_scatterers(
      best_xray_structure.deep_copy())
    fmodels.update_xray_structure(
      xray_structure = fmodels.fmodel_xray().xray_structure,
      update_f_calc  = True)
    utils.assert_xray_structures_equal(
      x1 = fmodels.fmodel_xray().xray_structure,
      x2 = model.xray_structure)
    model.restraints_manager.geometry.\
      den_manager.import_eq_distances(eq_distances=best_eq_distances)
    self.model.restraints_manager.geometry.update_dihedral_ncs_restraints(
        sites_cart=self.model.xray_structure.sites_cart(),
        pdb_hierarchy=self.model.pdb_hierarchy(sync_with_xray_structure=True),
        log=self.log)
Example #28
0
def run(
    fmodel,
    geometry_restraints_manager,
    pdb_hierarchy,
    solvent_selection,
    secondary_structure=None,
    params=None,
    optimize_hd=False,
    log=None,
):
    from mmtbx.refinement import print_statistics
    from mmtbx.secondary_structure import proteins
    import mmtbx.restraints
    import mmtbx.model
    from scitbx.array_family import flex

    if log is None:
        log = sys.stdout
    if params is None:
        params = master_params().extract()
    if secondary_structure is None:
        import mmtbx.secondary_structure

        secondary_structure = mmtbx.secondary_structure.manager(pdb_hierarchy=pdb_hierarchy)
    print_statistics.make_sub_header(text="Peptide bond flips", out=log)
    validator = ramachandran_validator()
    restraints_manager = mmtbx.restraints.manager(geometry=geometry_restraints_manager, normalization=True)
    model = mmtbx.model.manager(
        restraints_manager=restraints_manager, xray_structure=fmodel.xray_structure, pdb_hierarchy=pdb_hierarchy
    )
    backbone_selections = model.backbone_selections()
    if params.ignore_water_when_flipping:
        selection = ~model.solvent_selection()
    else:
        selection = flex.bool(model.xray_structure.scatterers().size(), True)
    selection &= backbone_selections
    fmt = "Macro-cycle %2d: r_work=%6.4f r_free=%6.4f"
    print >> log, fmt % (0, fmodel.r_work(), fmodel.r_free())
    n_mac = params.number_of_macro_cycles
    if n_mac is None:
        n_mac = 0
    for macro_cycle in range(1, n_mac + 1):
        target_map_data, fft_map_1 = fit_rotamers.get_map_data(fmodel=fmodel, map_type=params.target_map)
        model_map_data, fft_map_2 = fit_rotamers.get_map_data(fmodel=fmodel, map_type=params.model_map)
        residual_map_data, fft_map_3 = fit_rotamers.get_map_data(fmodel=fmodel, map_type=params.residual_map)
        if params.filter_residual_map_value is not None:  # XXX use filtering....
            map_sel = flex.abs(residual_map_data) < params.filter_residual_map_value
            residual_map_data = residual_map_data.set_selected(map_sel, 0)
        if params.filter_2fofc_map is not None:
            map_sel = flex.abs(target_map_data) < params.filter_2fofc_map
            target_map_data = target_map_data.set_selected(map_sel, 0)
        rsr_manager = fit_rotamers.refiner(
            pdb_hierarchy=pdb_hierarchy,
            target_map=target_map_data,
            geometry_restraints_manager=geometry_restraints_manager,
            real_space_target_weight=params.residue_iteration.real_space_refine_target_weight,
            real_space_gradients_delta=fmodel.f_obs().d_min() / 4,
            max_iterations=params.residue_iteration.real_space_refine_max_iterations,
        )
        secondary_structure.pdb_hierarchy = pdb_hierarchy
        alpha_selection = secondary_structure.helix_selection(alpha_only=True)
        if params.skip_approximate_helices:
            cache = pdb_hierarchy.atom_selection_cache()
            print >> log, "Looking for roughly helical residues. . ."
            find_helices = proteins.find_helices_simple(pdb_hierarchy)
            # find_helices.show(out=log)
            print >> log, ""
            ss_params = find_helices.as_restraint_groups()
            if ss_params is not None:
                for helix in ss_params.helix:
                    helix_selection = cache.selection(helix.selection)
                    alpha_selection |= helix_selection
        residue_rsr_monitor = residue_iteration(
            pdb_hierarchy=pdb_hierarchy,
            xray_structure=fmodel.xray_structure,
            selection=selection,
            alpha_selection=alpha_selection,
            target_map_data=target_map_data,
            model_map_data=model_map_data,
            residual_map_data=residual_map_data,
            rsr_manager=rsr_manager,
            params=params.residue_iteration,
            validator=validator,
            log=log,
        )
        fmodel.update_xray_structure(update_f_calc=True, update_f_mask=True)
        print >> log, "1:", fmt % (macro_cycle, fmodel.r_work(), fmodel.r_free())
        del target_map_data, model_map_data, residual_map_data, fft_map_1, fft_map_2, fft_map_3
        if params.real_space_refine_overall:
            pass
            # FIXME this method has completely disappeared...
            # fit_rotamers.rsr_overall(
            #  model=model,
            #  fmodel=fmodel,
            #  params=params,
            #  optimize_hd=optimize_hd,
            #  macro_cycle=macro_cycle,
            #  log=log)
            # print >> log, "1:", fmt%(macro_cycle, fmodel.r_work(), fmodel.r_free())
        if params.validate_change:
            fit_rotamers.validate_changes(
                fmodel=fmodel, residue_rsr_monitor=residue_rsr_monitor, validate_method=validator, log=log
            )
        pdb_hierarchy.atoms().set_xyz(fmodel.xray_structure.sites_cart())