Пример #1
0
def find_map_peaks(params, strip_xrs, log):
    #Adapted from mmtbx fine_peaks.py and
    #phenix find_peaks_holes.py, simplified to just give
    #coords and map levels for clustered peaks in difference map
    print >> log, "_" * 79
    print >> log, "Finding Difference Map Peaks:"
    map_coeff = reflection_file_utils.extract_miller_array_from_file(
        file_name=params.input.input_map.map_coeff_file,
        label=params.input.input_map.map_diff_label,
        type="complex",
        log=null_log)
    peaks_result = find_peaks.manager(
        fmodel=fake_fmodel(strip_xrs),
        map_type=None,
        map_cutoff=params.input.parameters.peak_pick_cutoff,
        params=params.peak_search,
        use_all_data=True,
        map_coeffs=map_coeff,
        log=log)
    peaks_result.peaks_mapped()  #cluter/arrange found peaks?
    peaks = peaks_result.peaks()  #returns heights,coords(frac)
    unit_cell = strip_xrs.unit_cell()  #need cell for cartesian
    peaks.sites = unit_cell.orthogonalize(peaks.sites)

    return peaks
def exercise_extract_miller_array_from_file():
  from iotbx import reflection_file_utils as rfu
  from libtbx.test_utils import approx_equal
  log = null_out()
  sorry_counts = 0
  crystal_symmetry = crystal.symmetry(
    unit_cell=(30,31,32,85,95,100),
    space_group_symbol="P 1")
  miller_set = miller.build_set(
    crystal_symmetry=crystal_symmetry,
    anomalous_flag=False,
    d_min=3)
  size = miller_set.indices().size()
  a1 = miller_set.array(
    data=flex.hendrickson_lattman(size, (1,1,1,1)))
  a2 = miller_set.array(data=flex.double(size, 2))
  a3 = miller_set.array(data=flex.double(size, 3))
  a4 = miller_set.array(data=flex.complex_double(size, 4+4j))
  a5 = miller_set.array(data=flex.complex_double(size, 5+5j))
  #
  mtz_dataset = a1.as_mtz_dataset(column_root_label="A1")
  mtz_dataset.mtz_object().write("tmp.mtz")
  ma = rfu.extract_miller_array_from_file(file_name="tmp.mtz", log=log)
  assert type(ma.data()) == flex.hendrickson_lattman
  #
  mtz_dataset = a5.as_mtz_dataset(column_root_label="A5")
  mtz_dataset.mtz_object().write("tmp.mtz")
  ma = rfu.extract_miller_array_from_file(file_name="tmp.mtz", log=log)
  assert type(ma.data()) == flex.complex_double
  #
  for tp in [None, "complex"]:
    mtz_dataset = a4.as_mtz_dataset(column_root_label="A4")
    mtz_dataset.add_miller_array(
      miller_array=a5, column_root_label="A5")
    mtz_dataset.mtz_object().write("tmp.mtz")
    try:
      rfu.extract_miller_array_from_file(file_name="tmp.mtz",type=tp, log=log)
    except Sorry, e:
      assert ("Multiple choices available." in str(e))
      sorry_counts += 1
Пример #3
0
 def make_fofc_map(self):
     # FOFC map of the whole original asu
     map_coeff = reflection_file_utils.extract_miller_array_from_file(
         file_name=self.map_file,
         label="FOFCWT,PHFOFCWT",
         type="complex",
         log=null_log)
     map_sym = map_coeff.crystal_symmetry()
     fft_map = map_coeff.fft_map(resolution_factor=0.25)
     mapsig = np.nanstd(fft_map.real_map_unpadded().as_numpy_array())
     fft_map.apply_sigma_scaling()
     self.fofc_map_data = fft_map.real_map_unpadded()
     return mapsig
Пример #4
0
def exercise_extract_miller_array_from_file():
    from iotbx import reflection_file_utils as rfu
    from libtbx.test_utils import approx_equal
    log = null_out()
    sorry_counts = 0
    crystal_symmetry = crystal.symmetry(unit_cell=(30, 31, 32, 85, 95, 100),
                                        space_group_symbol="P 1")
    miller_set = miller.build_set(crystal_symmetry=crystal_symmetry,
                                  anomalous_flag=False,
                                  d_min=3)
    size = miller_set.indices().size()
    a1 = miller_set.array(data=flex.hendrickson_lattman(size, (1, 1, 1, 1)))
    a2 = miller_set.array(data=flex.double(size, 2))
    a3 = miller_set.array(data=flex.double(size, 3))
    a4 = miller_set.array(data=flex.complex_double(size, 4 + 4j))
    a5 = miller_set.array(data=flex.complex_double(size, 5 + 5j))
    #
    mtz_dataset = a1.as_mtz_dataset(column_root_label="A1")
    mtz_dataset.mtz_object().write("tmp.mtz")
    ma = rfu.extract_miller_array_from_file(file_name="tmp.mtz", log=log)
    assert type(ma.data()) == flex.hendrickson_lattman
    #
    mtz_dataset = a5.as_mtz_dataset(column_root_label="A5")
    mtz_dataset.mtz_object().write("tmp.mtz")
    ma = rfu.extract_miller_array_from_file(file_name="tmp.mtz", log=log)
    assert type(ma.data()) == flex.complex_double
    #
    for tp in [None, "complex"]:
        mtz_dataset = a4.as_mtz_dataset(column_root_label="A4")
        mtz_dataset.add_miller_array(miller_array=a5, column_root_label="A5")
        mtz_dataset.mtz_object().write("tmp.mtz")
        try:
            rfu.extract_miller_array_from_file(file_name="tmp.mtz",
                                               type=tp,
                                               log=log)
        except Sorry, e:
            assert ("Multiple choices available." in str(e))
            sorry_counts += 1
Пример #5
0
 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)
Пример #6
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
Пример #7
0
def process_inputs(args, log=sys.stdout):
    print >> log, "-" * 79
    print >> log, "PProbe RUN at %s" % time.ctime()
    print >> log, "Processing all Inputs:"
    #process phils in order to not overwrite inputs with defaults
    #phil from above
    master_phil = phil.parse(master_params_str, process_includes=True)
    #map params from phenix defaults (phil)
    maps_phil = phil.parse(mmtbx.maps.map_and_map_coeff_params_str)
    search_phil = phil.parse(peak_search_param_str)

    #merge phil objects?
    total_phil = master_phil.fetch(sources=[maps_phil, search_phil])

    #inputs is somehow different -- object with specific params and lists of files
    #process after all phil?
    inputs = mmtbx.utils.process_command_line_args(args=args,
                                                   master_params=total_phil)

    #params object contains all command line parameters
    working_phil = inputs.params
    params = working_phil.extract()

    #check for master param dictionary
    if params.input.model_param.model_dict_file is None:
        params.input.model_param.model_dict_file = os.path.join(
            PProbe_dataio.get_script_directory(), "pprobe_master.dict")
    if not os.path.isfile(params.input.model_param.model_dict_file):
        print >> log, "WARNING -- param file not found!"
        print >> log, "--> trying pprobe_master.dict . . . "
        params.input.model_param.model_dict_file = "pprobe_master.dict"
        if not os.path.isfile(params.input.model_param.model_dict_file):
            raise Sorry("Master Param Dictionary %s not found!" %
                        params.input.model_param.model_dict_file)

    if params.pprobe.extract:
        #check for proper PDB input
        #count up PDB files found
        pdb_count = len(inputs.pdb_file_names)
        for pdbin in (params.input.pdb.model_pdb, params.input.pdb.strip_pdb,
                      params.input.pdb.peaks_pdb):
            if pdbin is not None:
                pdb_count = pdb_count + 1
        if (pdb_count == 1) and (len(inputs.pdb_file_names) == 1):
            #one vanilla pdb to be used as model
            params.input.pdb.model_pdb = inputs.pdb_file_names[0]
        elif (pdb_count == 3) and (len(inputs.pdb_file_names) == 0):
            pass  #three explicit PDBs, hopefully correct
        else:
            raise Sorry("\n\tInput 1 PDB for automatic stripping and peak finding \n"+\
                        "\tor all PDB files specifically, like so: \n"+\
                        "\tfor explicit input: \n"+\
                        "\t\tmodel_pdb=XXX.pdb strip_pdb=YYY.pdb peaks_pdb=ZZZ.pdb \n"+\
                        "\tfor automatic pdb generation: \n"+\
                        "\t\tXXX.pdb")

        #check for proper reflection file input
        reflection_files = inputs.reflection_files
        if (len(reflection_files) == 0):
            raise Sorry("Reflection data or map coefficients required")
        if (len(reflection_files) > 1):
            raise Sorry("Only one type of reflection data can be entered \n"+\
                        "Enter map coefficients with map_coeff_file=XXX.mtz \n"+\
                        "or structure factor files as XXX.(any supported)")
        else:
            params.input.reflection_data.reflection_file_name = reflection_files[
                0].file_name()

        #filename setup
        model_basename = os.path.basename(
            params.input.pdb.model_pdb.split(".")[0])
        if (len(model_basename) > 0
                and params.output.output_file_name_prefix is None):
            params.output.output_file_name_prefix = model_basename
        if params.input.input_map.map_coeff_file is not None:
            params.input.parameters.write_maps = False
        new_params = master_phil.format(python_object=params)
        #okay, see if we're where we want to be
        print >> log, "Runtime Parameters:"
        new_params.show()

        #DATA PROCESSING
        #setup model pdb (required and should be known)
        crystal_symmetry = check_symmetry(inputs, params, log)
        model_pdb_input = iotbx.pdb.input(file_name=params.input.pdb.model_pdb)
        model_hier = model_pdb_input.construct_hierarchy()
        model_hier.remove_hd()
        model_xrs = model_hier.extract_xray_structure(
            crystal_symmetry=crystal_symmetry)

        #strip pdb if needed,write result
        if (params.input.pdb.strip_pdb is
                None) and (params.input.parameters.map_omit_mode != "asis"):
            strip_xrs, strip_hier = create_strip_pdb(
                model_hier, model_xrs, params.input.parameters.map_omit_mode,
                log)
            strip_filename = params.output.output_file_name_prefix + "_pprobe_strip.pdb"
            print >> log, "Writing Strip PDB to: ", strip_filename
            strip_hier.write_pdb_file(file_name=strip_filename,
                                      crystal_symmetry=crystal_symmetry,
                                      append_end=True,
                                      anisou=False)
            params.input.pdb.strip_pdb = strip_filename
        elif params.input.parameters.map_omit_mode == "asis":
            strip_xrs, strip_hier = model_xrs, model_hier
            params.input.pdb.strip_pdb = params.input.pdb.model_pdb
        else:
            strip_pdb_input = iotbx.pdb.input(
                file_name=params.input.pdb.strip_pdb)
            strip_hier = strip_pdb_input.construct_hierarchy()
            strip_hier.remove_hd()
            strip_xrs = strip_hier.extract_xray_structure(
                crystal_symmetry=crystal_symmetry)

        #Make maps if map_coefficients not input,write out by default
        if (params.input.input_map.map_coeff_file is None):

            hkl_in = file_reader.any_file(
                params.input.reflection_data.reflection_file_name,
                force_type="hkl")
            hkl_in.assert_file_type("hkl")
            reflection_files = [hkl_in.file_object]
            f_obs, r_free_flags = setup_reflection_data(
                inputs, params, crystal_symmetry, reflection_files, log)
            #maps object is list of miller arrays
            maps = create_pprobe_maps(f_obs, r_free_flags, params, strip_xrs,
                                      strip_hier, log)
            map_fname = params.output.output_file_name_prefix + "_pprobe_maps.mtz"
            print >> log, "Writing PProbe maps to MTZ file: ", map_fname
            maps.write_mtz_file(map_fname)
            params.input.input_map.map_coeff_file = params.output.output_file_name_prefix + "_pprobe_maps.mtz"
        else:
            print "READING MAP FILE: ", params.input.input_map.map_coeff_file
            #setup input map coefficients

            map_coeff = reflection_file_utils.extract_miller_array_from_file(
                file_name=params.input.input_map.map_coeff_file,
                label=params.input.input_map.map_diff_label,
                type="complex",
                log=null_log)

            if params.input.parameters.score_res is None:
                params.input.parameters.score_res = map_coeff.d_min()
                print >> log, "  Determined Resolution Limit: %.2f" % params.input.parameters.score_res
                print >> log, "    -->Override with \"score_res=XXX\""
            map_fname = params.input.input_map.map_coeff_file

        # if peaks not input, find and write to pdb
        if params.input.pdb.peaks_pdb is None:
            if params.input.parameters.map_omit_mode != "valsol":
                peaks_result = find_map_peaks(params, strip_xrs, log)
                pdb_str = peaks_pdb_str(peaks_result)
                peak_pdb = iotbx.pdb.input(source_info=None,
                                           lines=flex.split_lines(pdb_str))
                peak_hier = peak_pdb.construct_hierarchy()
                peak_filename = params.output.output_file_name_prefix + "_pprobe_peaks.pdb"
                print >> log, "Writing Peaks to %s:" % peak_filename
                peak_hier.write_pdb_file(file_name=peak_filename,
                                         crystal_symmetry=crystal_symmetry,
                                         append_end=True,
                                         anisou=False)
                params.input.pdb.peaks_pdb = peak_filename
            else:
                peak_filename = params.output.output_file_name_prefix + "_pprobe_peaks.pdb"
                peak_xrs, peak_hier = create_sol_pdb(
                    model_hier, model_xrs,
                    params.input.parameters.map_omit_mode, log)
                print >> log, "Writing Peaks to %s:" % peak_filename
                peak_hier.write_pdb_file(file_name=peak_filename,
                                         crystal_symmetry=crystal_symmetry,
                                         append_end=True,
                                         anisou=False)
                params.input.pdb.peaks_pdb = peak_filename

        #Wrap up, display file names and info for manual input
        #save parameters for next stage
        new_phil = working_phil.format(python_object=params)
        phil_fname = params.output.output_file_name_prefix + "_pprobe.param"
        f = open(phil_fname, "w")
        f.write(new_phil.as_str())
        f.close()
        print >> log, "_" * 79
        print >> log, "Inputs Processed, final files:"
        print >> log, "   Model PDB: ", params.input.pdb.model_pdb
        print >> log, "   Strip PDB: ", params.input.pdb.strip_pdb
        print >> log, "   Peaks PDB: ", params.input.pdb.peaks_pdb
        print >> log, "   Map Coeff: ", map_fname
        print >> log, "   Resolution: %.2f" % params.input.parameters.score_res
        print >> log, "   Params: ", phil_fname
        #also return params
        return params
    else:  #only rescoring from pkl
        #filename setup
        pkl_basename = os.path.basename(
            params.input.data_pkl.peak_dict.split(".")[0])
        if (len(pkl_basename) > 0
                and params.output.output_file_name_prefix is None):
            params.output.output_file_name_prefix = pkl_basename
            pkl_file = params.input.data_pkl.peak_dict
        if not os.path.isfile(pkl_file):
            raise Sorry("\n\tPKL input requested but no file available\n"+\
                        "\t\t\t cannot find %s" % pkl_file)
        new_phil = working_phil.format(python_object=params)
        phil_fname = params.output.output_file_name_prefix + "_pprobe.param"
        f = open(phil_fname, "w")
        f.write(new_phil.as_str())
        f.close()

        new_params = master_phil.format(python_object=params)
        print >> log, "Runtime Parameters:"
        new_params.show()
        return params
Пример #8
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")
    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
Пример #9
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
   mtz_dataset = a4.as_mtz_dataset(column_root_label="A4")
   mtz_dataset.add_miller_array(
     miller_array=a5, column_root_label="A5")
   mtz_dataset.mtz_object().write("tmp.mtz")
   try:
     rfu.extract_miller_array_from_file(file_name="tmp.mtz",type=tp, log=log)
   except Sorry, e:
     assert ("Multiple choices available." in str(e))
     sorry_counts += 1
 #
 for tp in [None, "real"]:
   mtz_dataset = a2.as_mtz_dataset(column_root_label="A2")
   mtz_dataset.add_miller_array(
     miller_array=a3, column_root_label="A3")
   mtz_dataset.mtz_object().write("tmp.mtz")
   try: rfu.extract_miller_array_from_file(file_name="tmp.mtz",type=tp,log=log)
   except Sorry, e:
     assert ("Multiple choices available." in str(e))
     sorry_counts += 1
 #
 mtz_dataset = a3.as_mtz_dataset(column_root_label="A3")
 mtz_dataset.add_miller_array(
   miller_array=a4, column_root_label="A4")
 mtz_dataset.mtz_object().write("tmp.mtz")
 try: rfu.extract_miller_array_from_file(file_name="tmp.mtz",log=log)
 except Sorry, e:
   assert ("Multiple choices available." in str(e))
   sorry_counts += 1
 #
 mtz_dataset = a4.as_mtz_dataset(column_root_label="A4")
 mtz_dataset.add_miller_array(
Пример #11
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
Пример #12
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
Пример #13
0
def run(args, log):
    timer = user_plus_sys_time()
    format_usage_message(log=log)
    if (len(args) == 0): return
    parsed = master_params()
    inputs = mmtbx.utils.process_command_line_args(args=args,
                                                   master_params=parsed,
                                                   log=log)
    params = inputs.params.extract()
    broadcast(m="Input parameters", log=log)
    inputs.params.show(prefix="  ")
    ###
    xray_structure = None
    if (len(inputs.pdb_file_names) > 0):
        broadcast(m="Input model", log=log)
        assert len(inputs.pdb_file_names) == 1
        print >> log, "  file name:", inputs.pdb_file_names[0]
        xray_structure = iotbx.pdb.input(
            file_name=inputs.pdb_file_names[0]).xray_structure_simple()
        assert xray_structure is not None
        xray_structure.show_summary(prefix="  ", f=log)
        mmtbx.utils.setup_scattering_dictionaries(
            scattering_table=params.scattering_table,
            xray_structure=xray_structure,
            d_min=0.25)
        xray_structure.scattering_type_registry().show(prefix="  ", out=log)
    ###
    broadcast(m="Input reflection data", log=log)
    reff = inputs.reflection_file_names
    if (len(reff) > 1):
        raise Sorry("One reflection file should be provided.")
    elif (len(reff) == 0):
        if (params.hkl_file_name is None):
            raise Sorry("No reflection file provided.")
        else:
            reff = [params.hkl_file_name]
    map_coeffs = reflection_file_utils.extract_miller_array_from_file(
        file_name=reff[0], label=params.label, type="complex", log=log)
    assert map_coeffs is not None
    map_coeffs.show_comprehensive_summary(prefix="  ", f=log)
    ###
    broadcast(m="MEM calculations begin", log=log)
    f_000 = params.f_000
    solvent_fraction = params.solvent_fraction
    if (f_000 is None):
        f_000_obj = mmtbx.utils.f_000(
            xray_structure=xray_structure,
            unit_cell_volume=map_coeffs.unit_cell().volume(),
            solvent_fraction=params.solvent_fraction,
            mean_solvent_density=params.mean_solvent_density)
        f_000 = f_000_obj.f_000
        solvent_fraction = f_000_obj.solvent_fraction
    print >> log, "F(0,0,0): %12.6f" % f_000
    if (solvent_fraction is not None):
        print >> log, "solvent_fraction: %6.4f" % solvent_fraction
    result = mem.run(f=map_coeffs,
                     f_000=f_000,
                     lam=params.lam,
                     lambda_increment_factor=params.lambda_increment_factor,
                     resolution_factor=params.resolution_factor,
                     verbose=True,
                     start_map="min_shifted",
                     max_iterations=params.max_iterations,
                     use_modification=True,
                     beta=params.beta,
                     convergence_at_r_factor=params.convergence_at_r_factor,
                     xray_structure=xray_structure,
                     convergence_r_threshold=params.convergence_r_threshold,
                     log=log)
    ###
    broadcast(m="Output MEM map coefficients", log=log)
    ind = max(0, reff[0].rfind("."))
    ofn = params.output_file_name
    if (ofn is None):
        ofn = reff[0] + "_mem.mtz" if ind == 0 else reff[0][:ind] + "_mem.mtz"
    print >> log, "  Output file name:", ofn
    result.write_mtz_file(file_name=ofn,
                          column_root_label=params.column_root_label,
                          d_min=params.output_high_resolution)
    broadcast(m="All done", log=log)
    return os.path.abspath(ofn)
Пример #14
0
def run(args, log):
  timer = user_plus_sys_time()
  format_usage_message(log = log)
  if(len(args)==0): return
  parsed = master_params()
  inputs = mmtbx.utils.process_command_line_args(
    args=args, master_params=parsed, log=log)
  params = inputs.params.extract()
  broadcast(m="Input parameters", log = log)
  inputs.params.show(prefix="  ")
  ###
  xray_structure = None
  if(len(inputs.pdb_file_names)>0):
    broadcast(m="Input model", log = log)
    assert len(inputs.pdb_file_names) == 1
    print >> log, "  file name:", inputs.pdb_file_names[0]
    xray_structure = iotbx.pdb.input(
      file_name = inputs.pdb_file_names[0]).xray_structure_simple()
    assert xray_structure is not None
    xray_structure.show_summary(prefix="  ", f=log)
    mmtbx.utils.setup_scattering_dictionaries(
      scattering_table = params.scattering_table,
      xray_structure   = xray_structure,
      d_min            = 0.25)
    xray_structure.scattering_type_registry().show(prefix="  ", out = log)
  ###
  broadcast(m="Input reflection data", log = log)
  reff = inputs.reflection_file_names
  if(len(reff) > 1):
    raise Sorry("One reflection file should be provided.")
  elif(len(reff) == 0):
    if(params.hkl_file_name is None):
      raise Sorry("No reflection file provided.")
    else: reff = [params.hkl_file_name]
  map_coeffs = reflection_file_utils.extract_miller_array_from_file(
    file_name = reff[0],
    label     = params.label,
    type      = "complex",
    log       = log)
  assert map_coeffs is not None
  map_coeffs.show_comprehensive_summary(prefix="  ", f=log)
  ###
  broadcast(m="MEM calculations begin", log = log)
  f_000 = params.f_000
  solvent_fraction = params.solvent_fraction
  if(f_000 is None):
    f_000_obj = mmtbx.utils.f_000(
      xray_structure       = xray_structure,
      unit_cell_volume     = map_coeffs.unit_cell().volume(),
      solvent_fraction     = params.solvent_fraction,
      mean_solvent_density = params.mean_solvent_density)
    f_000 = f_000_obj.f_000
    solvent_fraction = f_000_obj.solvent_fraction
  print >> log, "F(0,0,0): %12.6f"%f_000
  if(solvent_fraction is not None):
    print >> log, "solvent_fraction: %6.4f" % solvent_fraction
  result = mem.run(
    f                       = map_coeffs,
    f_000                   = f_000,
    lam                     = params.lam,
    lambda_increment_factor = params.lambda_increment_factor,
    resolution_factor       = params.resolution_factor,
    verbose                 = True,
    start_map               = "min_shifted",
    max_iterations          = params.max_iterations,
    use_modification        = True,
    beta                    = params.beta,
    convergence_at_r_factor = params.convergence_at_r_factor,
    xray_structure          = xray_structure,
    convergence_r_threshold = params.convergence_r_threshold,
    log                     = log)
  ###
  broadcast(m="Output MEM map coefficients", log = log)
  ind = max(0,reff[0].rfind("."))
  ofn = params.output_file_name
  if (ofn is None) :
    ofn = reff[0]+"_mem.mtz" if ind==0 else reff[0][:ind]+"_mem.mtz"
  print >> log, "  Output file name:", ofn
  result.write_mtz_file(file_name = ofn,
    column_root_label=params.column_root_label,
    d_min=params.output_high_resolution)
  broadcast(m="All done", log=log)
  return os.path.abspath(ofn)
Пример #15
0
     mtz_dataset.mtz_object().write("tmp.mtz")
     try:
         rfu.extract_miller_array_from_file(file_name="tmp.mtz",
                                            type=tp,
                                            log=log)
     except Sorry, e:
         assert ("Multiple choices available." in str(e))
         sorry_counts += 1
 #
 for tp in [None, "real"]:
     mtz_dataset = a2.as_mtz_dataset(column_root_label="A2")
     mtz_dataset.add_miller_array(miller_array=a3, column_root_label="A3")
     mtz_dataset.mtz_object().write("tmp.mtz")
     try:
         rfu.extract_miller_array_from_file(file_name="tmp.mtz",
                                            type=tp,
                                            log=log)
     except Sorry, e:
         assert ("Multiple choices available." in str(e))
         sorry_counts += 1
 #
 mtz_dataset = a3.as_mtz_dataset(column_root_label="A3")
 mtz_dataset.add_miller_array(miller_array=a4, column_root_label="A4")
 mtz_dataset.mtz_object().write("tmp.mtz")
 try:
     rfu.extract_miller_array_from_file(file_name="tmp.mtz", log=log)
 except Sorry, e:
     assert ("Multiple choices available." in str(e))
     sorry_counts += 1
 #
 mtz_dataset = a4.as_mtz_dataset(column_root_label="A4")