Example #1
0
  def map_inside_unit_cell(self,unit_cell=None):
    # map all the operators inside the unit cell.  Must be supplied
    assert unit_cell is not None
    if len(self._centers)==0: return

    new_centers=[]
    new_translations_orth=[]
    from scitbx.math import  matrix
    #rotation matrices do not change, just translations
    # find the identity:
    first_coordinate_offset=None
    for center,trans_orth,ncs_rota_matr in zip (
       self._centers, self._translations_orth,self._rota_matrices):
      if is_identity(ncs_rota_matr,trans_orth):
        first_coordinate_offset=matrix.col(self.offset_inside_cell(
          center,unit_cell=unit_cell))
        break
    if first_coordinate_offset is None:
      raise Sorry("Identity not found in NCS matrices?")
    for center,trans_orth,ncs_rota_matr in zip (
       self._centers, self._translations_orth,self._rota_matrices):
      coordinate_offset=self.offset_inside_cell(center,unit_cell=unit_cell)

      new_centers.append(matrix.col(center)+matrix.col(coordinate_offset))
      #  T'=T - R x_i + x_1
      delta = matrix.col(ncs_rota_matr * coordinate_offset)
      t_prime=matrix.col(trans_orth) - delta + first_coordinate_offset
      new_translations_orth.append(t_prime)

    self._centers=new_centers
    self._translations_orth=new_translations_orth
Example #2
0
  def save_oper(self):
     # decide if there is anything to save:

     have_oper=True
     for item in (self._trans,self._rota_matrix,self._center):
       if not item:
          have_oper=False
     if self._rota_matrix and len(self._rota_matrix)!=3:
          raise Sorry("Cannot interpret this NCS file (rotations not understood)")
     if self._trans and len(self._trans)!=3:
          raise Sorry("Cannot interpret this NCS file (translations not understood)")
     have_something=False
     if have_oper or self._rmsd or self._residues_in_common:
       have_something=True
     if not have_something: return
     self._n_ncs_oper+=1
     if have_oper:
       from scitbx import matrix
       self._ncs_trans_orth.append(matrix.col(self._trans))
       self._ncs_rota_matr.append(matrix.sqr(
         self._rota_matrix[0]+self._rota_matrix[1]+self._rota_matrix[2] ))
       self._ncs_center_orth.append(matrix.col(self._center))
     else:
       self._ncs_trans_orth.append(None)
       self._ncs_rota_matr.append(None)
       self._ncs_center_orth.append(None)
     self._rmsd_list.append(self._rmsd)
     self._residues_in_common_list.append(self._residues_in_common)
     self._list_of_resseq_list.append(self._resseq_list)
     self._group.append(self._chain)

     self.init_oper()
Example #3
0
 def offset_inside_cell(self,center,unit_cell):
   # put the center inside (0,1)
   from scitbx.math import  matrix
   c=matrix.col(center)
   c_frac=unit_cell.fractionalize(c)
   offset_frac=[]
   for x in c_frac:
    offset_frac.append(self.offset_inside_zero_one(x))
   return unit_cell.orthogonalize(matrix.col(offset_frac))
Example #4
0
 def apply_cob_to_vector(self,vector=None,
        change_of_basis_operator=None,
        coordinate_offset=None,
        unit_cell=None,new_unit_cell=None):
   if coordinate_offset is not None:
     from scitbx.math import  matrix
     new_vector=matrix.col(vector)+matrix.col(coordinate_offset)
   elif change_of_basis_operator:
     frac=unit_cell.fractionalize(vector)
     new_frac = change_of_basis_operator.c() * frac
     new_vector=new_unit_cell.orthogonalize(new_frac)
   return new_vector
Example #5
0
def get_map_and_model(params=None,
    map_data=None,
    crystal_symmetry=None,
    pdb_inp=None,
    ncs_obj=None,
    half_map_data_list=None,
    map_coords_inside_cell=True,
    get_map_labels=None,
    out=sys.stdout):

  acc=None # accessor used to shift map back to original location if desired
  origin_frac=(0,0,0)
  map_labels=None
  if map_data and crystal_symmetry:
    original_crystal_symmetry=crystal_symmetry
    original_unit_cell_grid=None
    acc=map_data.accessor()
    shift_needed = not \
       (map_data.focus_size_1d() > 0 and map_data.nd() == 3 and
        map_data.is_0_based())
    if(shift_needed):
      origin_shift=(
        map_data.origin()[0]/map_data.all()[0],
        map_data.origin()[1]/map_data.all()[1],
        map_data.origin()[2]/map_data.all()[2])
      origin_frac=origin_shift  # NOTE: fraction of NEW cell
      map_data = map_data.shift_origin()
    else:
      origin_frac=(0.,0.,0.)

  elif params.input_files.map_file:
    print >>out,"\nReading map from %s\n" %( params.input_files.map_file)
    from cctbx.maptbx.segment_and_split_map import get_map_object
    map_data,space_group,unit_cell,crystal_symmetry,origin_frac,acc,\
        original_crystal_symmetry,original_unit_cell_grid,map_labels=\
      get_map_object(file_name=params.input_files.map_file,
      must_allow_sharpening=(not params.control.ignore_map_limitations),
      get_map_labels=True,out=out)
    map_data=map_data.as_double()
    if origin_frac != (0,0,0) and acc is None:
      print >>out,"\nWARNING: Unable to place output map at position of "+\
        "input map though input map has non-zero origin at %s\n" %(
        str(origin_frac))

  elif params.input_files.map_coeffs_file:
    map_coeffs=get_map_coeffs_from_file(
      map_coeffs_file=params.input_files.map_coeffs_file,
      map_coeffs_labels=params.input_files.map_coeffs_labels)

    if not map_coeffs:
      raise Sorry("Could not get map coeffs from %s with labels %s" %(
        params.input_files.map_coeffs_file,params.input_files.map_coeffs_labels))
    print >>out,"Map coefficients read from %s with labels %s" %(
         params.input_files.map_coeffs_file,
         str(params.input_files.map_coeffs_labels))
    crystal_symmetry=map_coeffs.crystal_symmetry()
    from cctbx.maptbx.segment_and_split_map import get_map_from_map_coeffs
    map_data=get_map_from_map_coeffs(
      map_coeffs=map_coeffs,crystal_symmetry=crystal_symmetry)
    acc=map_data.accessor()
    original_crystal_symmetry=crystal_symmetry
    original_unit_cell_grid=None
    if not params.crystal_info.resolution:
      params.crystal_info.resolution=map_coeffs.d_min()
      print >>out,"Resolution from map_coeffs is %7.2f A" %(
          params.crystal_info.resolution)
  else:
    raise Sorry("Need ccp4 map or map_coeffs")

  if params.input_files.half_map_file:
    if len(params.input_files.half_map_file) != 2:
      raise Sorry("Please supply zero or two half_map files")
    half_map_data_list=[]
    from cctbx.maptbx.segment_and_split_map import get_map_object
    for file_name in params.input_files.half_map_file:
      print >>out,"\nReading half-map from %s\n" %(file_name)
      half_map_data,half_map_space_group,half_map_unit_cell,\
        half_map_crystal_symmetry,half_map_origin_frac,half_map_acc,\
         half_map_original_crystal_symmetry,half_map_original_unit_cell_grid=\
        get_map_object(file_name=file_name,out=out)
      half_map_data=half_map_data.as_double()
      assert half_map_crystal_symmetry.is_similar_symmetry(crystal_symmetry)

      half_map_data_list.append(half_map_data)

  if params.crystal_info.resolution is None:
    raise Sorry("Need resolution if map is supplied")

  if params.crystal_info.resolution >= 10:
    print >>out,"\n** WARNING: auto_sharpen is designed for maps at a "+\
      "resolution of about 4.5 A\nor better.  Sharpening may be"+\
      "poor at %7.0f A" %(params.crystal_info.resolution)


  if params.input_files.pdb_file and not pdb_inp: # get model
    model_file=params.input_files.pdb_file
    if not os.path.isfile(model_file):
      raise Sorry("Missing the model file: %s" %(model_file))
    pdb_inp=iotbx.pdb.input(file_name=model_file)
  if pdb_inp: # XXX added 2019-05-05
    if origin_frac != (0,0,0):
      print >>out,"Shifting model by %s" %(str(origin_frac))
      from cctbx.maptbx.segment_and_split_map import \
         apply_shift_to_pdb_hierarchy
      origin_shift=crystal_symmetry.unit_cell().orthogonalize(
         (-origin_frac[0],-origin_frac[1],-origin_frac[2]))
      pdb_inp=apply_shift_to_pdb_hierarchy(
       origin_shift=origin_shift,
       crystal_symmetry=crystal_symmetry,
       pdb_hierarchy=pdb_inp.construct_hierarchy(),
       out=out).as_pdb_input()
    if map_coords_inside_cell:
      # put inside (0,1)
      pdb_inp=map_inside_cell(pdb_inp,crystal_symmetry=crystal_symmetry)

  if params.input_files.ncs_file and not ncs_obj: # NCS
    from cctbx.maptbx.segment_and_split_map import get_ncs
    ncs_obj,dummy_tracking_data=get_ncs(params,out=out)
    if origin_frac != (0,0,0):
      origin_shift=crystal_symmetry.unit_cell().orthogonalize(
         (-origin_frac[0],-origin_frac[1],-origin_frac[2]))
      print >>out,"Shifting NCS by (%7.2f,%7.2f,%7.2f) " %((origin_shift))
      from scitbx.math import  matrix
      ncs_obj=ncs_obj.coordinate_offset(
       coordinate_offset=matrix.col(origin_shift))

  if get_map_labels:
    return pdb_inp,map_data,half_map_data_list,ncs_obj,crystal_symmetry,acc,\
       original_crystal_symmetry,original_unit_cell_grid,map_labels
  else:
    return pdb_inp,map_data,half_map_data_list,ncs_obj,crystal_symmetry,acc,\
       original_crystal_symmetry,original_unit_cell_grid
Example #6
0
def run(args, crystal_symmetry=None, log=None):
  h = "phenix.map_box: extract box with model and map around selected atoms"
  if(log is None): log = sys.stdout
  print_statistics.make_header(h, out=log)
  default_message="""\

%s.

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

or

  phenix.map_box map.ccp4 density_select=True

Parameters:"""%h
  if(len(args) == 0):
    print default_message
    master_phil.show(prefix="  ")
    return
  inputs = mmtbx.utils.process_command_line_args(args = args,
    cmd_cs=crystal_symmetry,
    master_params = master_phil)
  params = inputs.params.extract()
  # PDB file
  if params.pdb_file and not inputs.pdb_file_names:
    inputs.pdb_file_names=[params.pdb_file]
  if(len(inputs.pdb_file_names)!=1 and not params.density_select):
    raise Sorry("PDB file is needed unless density_select is set.")
  print_statistics.make_sub_header("pdb model", out=log)
  if len(inputs.pdb_file_names)>0:
    pdb_inp = iotbx.pdb.input(file_name=inputs.pdb_file_names[0])
    pdb_hierarchy = pdb_inp.construct_hierarchy()
    pdb_atoms = pdb_hierarchy.atoms()
    pdb_atoms.reset_i_seq()
  else:
    pdb_hierarchy=None
  # Map or map coefficients
  map_coeff = None
  if(inputs.ccp4_map is None):
    if(len(inputs.reflection_file_names)!=1):
      raise Sorry("Map or map coefficients file is needed.")
    map_coeff = reflection_file_utils.extract_miller_array_from_file(
      file_name = inputs.reflection_file_names[0],
      label     = params.label,
      type      = "complex",
      log       = log)
    fft_map = map_coeff.fft_map(resolution_factor=params.resolution_factor)
    fft_map.apply_sigma_scaling()
    map_data = fft_map.real_map_unpadded()
    map_or_map_coeffs_prefix=os.path.basename(
       inputs.reflection_file_names[0][:-4])
  else:
    print_statistics.make_sub_header("CCP4 map", out=log)
    ccp4_map = inputs.ccp4_map
    ccp4_map.show_summary(prefix="  ")
    map_data = ccp4_map.map_data()
    if inputs.ccp4_map_file_name.endswith(".ccp4"):
      map_or_map_coeffs_prefix=os.path.basename(
       inputs.ccp4_map_file_name[:-5])
    else:
      map_or_map_coeffs_prefix=os.path.basename(
       inputs.ccp4_map_file_name[:-4])
  #
  if len(inputs.pdb_file_names)>0:
    output_prefix=os.path.basename(inputs.pdb_file_names[0])[:-4]
  else:
    output_prefix=map_or_map_coeffs_prefix

  if not pdb_hierarchy: # get an empty hierarchy
    from cctbx.array_family import flex
    pdb_hierarchy=iotbx.pdb.input(
      source_info='',lines=flex.split_lines('')).construct_hierarchy()
  xray_structure = pdb_hierarchy.extract_xray_structure(
    crystal_symmetry=inputs.crystal_symmetry)
  xray_structure.show_summary(f=log)
  #
  selection = pdb_hierarchy.atom_selection_cache().selection(
    string = params.selection)
  if selection.size():
    print_statistics.make_sub_header("atom selection", out=log)
    print >> log, "Selection string: selection='%s'"%params.selection
    print >> log, \
        "  selects %d atoms from total %d atoms."%(selection.count(True),
        selection.size())
  sites_cart_all = xray_structure.sites_cart()
  sites_cart = sites_cart_all.select(selection)
  selection = xray_structure.selection_within(
    radius    = params.selection_radius,
    selection = selection)
#
  if params.density_select:
    print_statistics.make_sub_header(
    "Extracting box around selected density and writing output files", out=log)
  else:
   print_statistics.make_sub_header(
    "Extracting box around selected atoms and writing output files", out=log)
  #
  box = mmtbx.utils.extract_box_around_model_and_map(
    xray_structure   = xray_structure,
    map_data         = map_data.as_double(),
    box_cushion      = params.box_cushion,
    selection        = selection,
    density_select   = params.density_select,
    threshold        = params.density_select_threshold)

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

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

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

  if(params.output_file_name_prefix is None):
    file_name = "%s_box.pdb"%output_prefix
  else: file_name = "%s.pdb"%params.output_file_name_prefix
  ph_box = pdb_hierarchy.select(selection)
  ph_box.adopt_xray_structure(box.xray_structure_box)
  ph_box.write_pdb_file(file_name=file_name, crystal_symmetry =
    box.xray_structure_box.crystal_symmetry())

  if("ccp4" in params.output_format):
    if(params.output_file_name_prefix is None):
      file_name = "%s_box.ccp4"%output_prefix
    else: file_name = "%s.ccp4"%params.output_file_name_prefix
    print >> log, "writing map to CCP4 formatted file:   %s"%file_name
    box.write_ccp4_map(file_name=file_name)
  if("xplor" in params.output_format):
    if(params.output_file_name_prefix is None):
      file_name = "%s_box.xplor"%output_prefix
    else: file_name = "%s.xplor"%params.output_file_name_prefix
    print >> log, "writing map to X-plor formatted file: %s"%file_name
    box.write_xplor_map(file_name=file_name)
  if("mtz" in params.output_format):
    if(params.output_file_name_prefix is None):
      file_name = "%s_box.mtz"%output_prefix
    else: file_name = "%s.mtz"%params.output_file_name_prefix
    print >> log, "writing map coefficients to MTZ file: %s"%file_name
    if(map_coeff is not None): d_min = map_coeff.d_min()
    else:
      d_min = maptbx.d_min_from_map(map_data=box.map_box,
        unit_cell=box.xray_structure_box.unit_cell())
    box.map_coefficients(d_min=d_min,
      resolution_factor=params.resolution_factor, file_name=file_name)

  if params.ncs_file:

    if(params.output_file_name_prefix is None):
      output_ncs_file = "%s_box.ncs_spec"%output_prefix
    else: output_ncs_file = "%s.ncs_spec"%params.output_file_name_prefix
    print >>log,"\nOffsetting NCS in %s and writing to %s" %(
       params.ncs_file,output_ncs_file)
    from mmtbx.ncs.ncs import ncs
    ncs_object=ncs()
    ncs_object.read_ncs(params.ncs_file,log=log)
    ncs_object.display_all(log=log)
    if not ncs_object or ncs_object.max_operators()<1:
      print >>log,"Skipping...no NCS available"
    elif box.total_shift_cart:
      from scitbx.math import  matrix
      print >>log,"Shifting NCS operators "+\
        "based on coordinate shift of (%7.1f,%7.1f,%7.1f)" %(
        tuple(box.total_shift_cart))
      ncs_object=ncs_object.coordinate_offset(
       coordinate_offset=matrix.col(box.total_shift_cart))
      ncs_object.display_all(log=log)
    ncs_object.format_all_for_group_specification(
       file_name=output_ncs_file)
    box.ncs_object=ncs_object
  else:
    box.ncs_object=None
  print >> log
  return box
Example #7
0
def reindex_utils(args):
  if len(args)==0:
    print_help()
  elif ( "--help" in args ):
    print_help()
  elif ( "--h" in args ):
    print_help()
  elif ("-h" in args ):
    print_help()
  else:
    log = multi_out()
    if (not "--quiet" in args):
      log.register(label="stdout", file_object=sys.stdout)
    string_buffer = StringIO()
    string_buffer_plots = StringIO()
    log.register(label="log_buffer", file_object=string_buffer)

    phil_objects = []
    argument_interpreter = master_params.command_line_argument_interpreter(
      home_scope="map_coefs")

    print >> log, "#phil __OFF__"
    print >> log, "================="
    print >> log, "    REINDEX      "
    print >> log, "A reindexing tool"
    print >> log, "================="
    print >> log


    for arg in args:
      command_line_params = None
      arg_is_processed = False
      # is it a file?
      if (os.path.isfile(arg)): ## is this a file name?
        # check if it is a phil file
        try:
          command_line_params = iotbx.phil.parse(file_name=arg)
          if command_line_params is not None:
            phil_objects.append(command_line_params)
            arg_is_processed = True
        except KeyboardInterrupt: raise
        except Exception : pass
      else:
        try:
          command_line_params = argument_interpreter.process(arg=arg)
          if command_line_params is not None:
            phil_objects.append(command_line_params)
            arg_is_processed = True
        except KeyboardInterrupt: raise
        except Exception : pass

      if not arg_is_processed:
        print >> log, "##----------------------------------------------##"
        print >> log, "## Unknown file or keyword:", arg
        print >> log, "##----------------------------------------------##"
        print >> log
        raise Sorry("Unknown file or keyword: %s" % arg)

    effective_params = master_params.fetch(sources=phil_objects)
    params_root = effective_params.extract()
    params = params_root.reindex_utils

    # now get the unit cell from the files
    hkl_xs = None
    pdb_xs = None
    if params.input.xray_data.file_name is not None:
      hkl_xs = crystal_symmetry_from_any.extract_from(
        file_name=params.input.xray_data.file_name)
    if params.input.model.file_name is not None:
      pdb_xs = crystal_symmetry_from_any.extract_from(
        file_name=params.input.model.file_name)

    phil_xs = crystal.symmetry(
      unit_cell=params.input.unit_cell,
      space_group_info=params.input.space_group  )


    combined_xs = crystal.select_crystal_symmetry(
      None,phil_xs, [pdb_xs],[hkl_xs])

    # inject the unit cell and symmetry in the phil scope please
    params.input.unit_cell = combined_xs.unit_cell()
    params.input.space_group = combined_xs.space_group_info()

    print >> log, "#phil __ON__"
    new_params =  master_params.format(python_object=params_root)
    new_params.show(out=log)
    print >> log, "#phil __END__"

    if params.input.unit_cell is None:
      raise Sorry("unit cell not specified")
    if params.input.space_group is None:
      raise Sorry("space group not specified")

    #-----------------------------------------------------------
    #
    # step 1: read in the reflection file
    #
    miller_array = None
    if  params.input.xray_data.file_name is not None:
      phil_xs = crystal.symmetry(
        unit_cell=params.input.unit_cell,
        space_group_info=params.input.space_group  )

      xray_data_server =  reflection_file_utils.reflection_file_server(
        crystal_symmetry = phil_xs,
        force_symmetry = True,
        reflection_files=[])

      miller_array = xray_data_server.get_xray_data(
        file_name = params.input.xray_data.file_name,
        labels = params.input.xray_data.labels,
        ignore_all_zeros = True,
        parameter_scope = 'reindex_utils.input.xray_data',
        parameter_name = 'labels'
        )

      info = miller_array.info()

      miller_array = miller_array.map_to_asu()

      miller_array = miller_array.select(
        miller_array.indices() != (0,0,0))

      miller_array = miller_array.select(
        miller_array.data() > 0 )
      if  miller_array.sigmas() is not None:
        miller_array = miller_array.select(
          miller_array.sigmas() > 0 )

      if (miller_array.is_xray_intensity_array()):
        miller_array = miller_array.f_sq_as_f()
      elif (miller_array.is_complex_array()):
        miller_array = abs(miller_array)

      miller_array.set_info(info)
      print >> log
      print >> log, "Summary info of observed data"
      print >> log, "============================="
      miller_array.show_summary(f=log)
      print >> log


    #----------------------------------------------------------------
    # Step 2: get an xray structure from the PDB file
    #
    pdb_model = None

    if params.input.model.file_name is not None:
      pdb_model = iotbx.pdb.input(
        file_name=params.input.model.file_name)
      model = pdb_model.xray_structure_simple(crystal_symmetry=phil_xs)
      print >> log, "Atomic model summary"
      print >> log, "===================="
      model.show_summary()
      print >> log

    if params.parameters.action=="reindex":
      #----------------------------------------------------------------
      # step 3: get the reindex laws
      to_niggli    = phil_xs.change_of_basis_op_to_niggli_cell()
      to_reference = phil_xs.change_of_basis_op_to_reference_setting()
      to_inverse   = phil_xs.change_of_basis_op_to_inverse_hand()
      cb_op = None
      pr = params.parameters.reindex
      if (pr.standard_laws == "niggli"):
        cb_op = to_niggli
      elif (pr.standard_laws == "reference_setting"):
        cb_op = to_reference
      elif (pr.standard_laws == "invert"):
        cb_op = to_inverse
      else:
        cb_op = sgtbx.change_of_basis_op(pr.user_supplied_law)

      if cb_op is None:
        raise Sorry("No change of basis operation is supplied.")
      if params.parameters.inverse:
        cb_op = cb_op.inverse()

      print >> log, "Supplied reindexing law:"
      print >> log, "========================"
      print >> log, "hkl notation: ", cb_op.as_hkl()
      print >> log, "xyz notation: ", cb_op.as_xyz()
      print >> log, "abc notation: ", cb_op.as_abc()
      #----------------------------------------------------------------
      # step 4: do the reindexing
      #
      # step 4a: first do the miller array object
      new_miller_array = None
      if miller_array is not None:
        new_miller_array = miller_array.change_basis( cb_op )
      #
      # step 4b: the xray structure
      new_model = None
      if pdb_model is not None:
        new_model = model.change_basis( cb_op )

      #----------------------------------------------------------------
      # step 5a: write the new mtz file
      print >> log
      print >> log, "The data and model have been reindexed"
      print >> log, "--------------------------------------"
      print >> log
      print >> log, "Writing output files...."
      if miller_array is not None:
        print >> log, "writing mtz file with name %s" % (params.output.hklout)
        mtz_dataset = new_miller_array.as_mtz_dataset(
          column_root_label="FOBS")
        mtz_dataset.mtz_object().write(file_name=params.output.hklout)

      #step 5b: write the new pdb file
      if new_model is not None:
        pdb_file = open(params.output.xyzout, 'w')
        print >> log, "Wring pdb file to: %s" % params.output.xyzout
        write_as_pdb_file(
          input_pdb = pdb_model,
          input_xray_structure = new_model,
          out = pdb_file,
          chain_id_increment = params.parameters.chain_id_increment,
          additional_remark = "Generated by mmtbx reindex")

        print >> pdb_file, "END"
        pdb_file.close()
      if ( [miller_array,new_model]).count(None)==2:
        print >>log, "No input reflection of coordinate files have been given"

    if params.parameters.action=="operator":
      rt_mx = sgtbx.rt_mx(
        params.parameters.apply_operator.user_supplied_operator,t_den=12*8 )
      if params.parameters.inverse:
        rt_mx = rt_mx.inverse()
      print >> log
      print >> log, "Applied operator : ", rt_mx.as_xyz()
      print >> log

      sites = model.sites_frac()
      new_sites = flex.vec3_double()
      for site in sites:
        new_site = rt_mx.r()*matrix.col(site)
        new_site = flex.double(new_site)+flex.double( rt_mx.t().as_double() )
        new_sites.append( tuple(new_site) )
      new_model = model.deep_copy_scatterers()

      new_model.set_sites_frac( new_sites )
      # write the new [pdb file please
      pdb_file = open(params.output.xyzout, 'w')
      print >> log, "Wring pdb file to: %s" % params.output.xyzout
      if params.parameters.apply_operator.concatenate_model:
        write_as_pdb_file(
          input_pdb = pdb_model,
          input_xray_structure = model,
          out = pdb_file,
          chain_id_increment = 0,
          additional_remark = None,
          print_cryst_and_scale=True)

      write_as_pdb_file(
        input_pdb = pdb_model,
        input_xray_structure = new_model,
        out = pdb_file,
        chain_id_increment = params.parameters.chain_id_increment,
        additional_remark = None,
        print_cryst_and_scale=False)

      print >> pdb_file, "END"
      pdb_file.close()

    if params.parameters.action=="manipulate_pdb":
      #rest all the b values
      if params.parameters.manipulate_pdb.set_b:
        b_iso = params.reindex_utils.parameters.manipulate_pdb.b_iso
        new_model = model.set_b_iso( value = b_iso )
        print >> log
        print >> log, "All B-values have been set to %5.3f"%(b_iso)
        print >> log, "Writing PDB file %s"%(params.output.xyzout)
        print >> log

      pdb_file = open(params.output.xyzout, 'w')
      write_as_pdb_file(
        input_pdb = pdb_model,
        input_xray_structure = new_model,
        out = pdb_file,
        chain_id_increment = 0,
        additional_remark = None,
        print_cryst_and_scale=True)
      print >> pdb_file, "END"
      pdb_file.close()

    #write the logfile
    logger = open(params.output.logfile, 'w')
    print >> log, "Writing log file with name %s" % params.output.logfile
    print >> log
    print >> logger, string_buffer.getvalue()
Example #8
0
def run(args,
        crystal_symmetry=None,
        pdb_hierarchy=None,
        map_data=None,
        write_output_files=True,
        log=None):
    h = "phenix.map_box: extract box with model and map around selected atoms"
    if (log is None): log = sys.stdout
    print_statistics.make_header(h, out=log)
    default_message = """\

%s.

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

or

  phenix.map_box map.ccp4 density_select=True

Parameters:""" % h
    if (len(args) == 0 and not pdb_hierarchy):
        print default_message
        master_phil.show(prefix="  ")
        return
    inputs = mmtbx.utils.process_command_line_args(args=args,
                                                   cmd_cs=crystal_symmetry,
                                                   master_params=master_phil)
    params = inputs.params.extract()
    # PDB file
    if params.pdb_file and not inputs.pdb_file_names and not pdb_hierarchy:
        inputs.pdb_file_names = [params.pdb_file]
    if (len(inputs.pdb_file_names) != 1 and not params.density_select
            and not pdb_hierarchy):
        raise Sorry("PDB file is needed unless density_select is set.")
    print_statistics.make_sub_header("pdb model", out=log)
    if len(inputs.pdb_file_names) > 0:
        pdb_inp = iotbx.pdb.input(file_name=inputs.pdb_file_names[0])
        pdb_hierarchy = pdb_inp.construct_hierarchy()
    if pdb_hierarchy:
        pdb_atoms = pdb_hierarchy.atoms()
        pdb_atoms.reset_i_seq()
    else:
        pdb_hierarchy = None
    # Map or map coefficients
    map_coeff = None
    if (not map_data):
        # read first mtz file
        if ((len(inputs.reflection_file_names) > 0)
                or (params.map_coefficients_file is not None)):
            # file in phil takes precedent
            if (params.map_coefficients_file is not None):
                if (len(inputs.reflection_file_names) == 0):
                    inputs.reflection_file_names.append(
                        params.map_coefficients_file)
                else:
                    inputs.reflection_file_names[
                        0] = params.map_coefficients_file
            map_coeff = reflection_file_utils.extract_miller_array_from_file(
                file_name=inputs.reflection_file_names[0],
                label=params.label,
                type="complex",
                log=log)
            fft_map = map_coeff.fft_map(
                resolution_factor=params.resolution_factor)
            fft_map.apply_sigma_scaling()
            map_data = fft_map.real_map_unpadded()
            map_or_map_coeffs_prefix = os.path.basename(
                inputs.reflection_file_names[0][:-4])
        # or read CCP4 map
        elif ((inputs.ccp4_map is not None)
              or (params.ccp4_map_file is not None)):
            if (params.ccp4_map_file is not None):
                af = any_file(params.ccp4_map_file)
                if (af.file_type == 'ccp4_map'):
                    inputs.ccp4_map = af.file_content
                    inputs.ccp4_map_file_name = params.ccp4_map_file
            print_statistics.make_sub_header("CCP4 map", out=log)
            ccp4_map = inputs.ccp4_map
            ccp4_map.show_summary(prefix="  ", out=log)
            map_data = ccp4_map.data  #map_data()
            if inputs.ccp4_map_file_name.endswith(".ccp4"):
                map_or_map_coeffs_prefix = os.path.basename(
                    inputs.ccp4_map_file_name[:-5])
            else:
                map_or_map_coeffs_prefix = os.path.basename(
                    inputs.ccp4_map_file_name[:-4])
    else:  # have map_data
        map_or_map_coeffs_prefix = None

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

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

    if not pdb_hierarchy:  # get an empty hierarchy
        from cctbx.array_family import flex
        pdb_hierarchy = iotbx.pdb.input(
            source_info='', lines=flex.split_lines('')).construct_hierarchy()
    xray_structure = pdb_hierarchy.extract_xray_structure(
        crystal_symmetry=inputs.crystal_symmetry)
    xray_structure.show_summary(f=log)
    #
    selection = pdb_hierarchy.atom_selection_cache().selection(
        string=params.selection)
    if selection.size():
        print_statistics.make_sub_header("atom selection", out=log)
        print >> log, "Selection string: selection='%s'" % params.selection
        print >> log, \
            "  selects %d atoms from total %d atoms."%(selection.count(True),
            selection.size())
    sites_cart_all = xray_structure.sites_cart()
    sites_cart = sites_cart_all.select(selection)
    selection = xray_structure.selection_within(radius=params.selection_radius,
                                                selection=selection)
    #
    if params.density_select:
        print_statistics.make_sub_header(
            "Extracting box around selected density and writing output files",
            out=log)
    else:
        print_statistics.make_sub_header(
            "Extracting box around selected atoms and writing output files",
            out=log)
    #
    if params.value_outside_atoms == 'mean':
        print >> log, "\nValue outside atoms mask will be set to mean inside mask"
    if params.get_half_height_width:
        print >> log, "\nHalf width at half height will be used to id boundaries"

    box = mmtbx.utils.extract_box_around_model_and_map(
        xray_structure=xray_structure,
        map_data=map_data.as_double(),
        box_cushion=params.box_cushion,
        selection=selection,
        density_select=params.density_select,
        threshold=params.density_select_threshold,
        get_half_height_width=params.get_half_height_width,
        mask_atoms=params.mask_atoms,
        soft_mask=params.soft_mask,
        soft_mask_radius=params.soft_mask_radius,
        mask_atoms_atom_radius=params.mask_atoms_atom_radius,
        value_outside_atoms=params.value_outside_atoms,
    )

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

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

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

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

    box.hierarchy = ph_box
    if not write_output_files:
        return box

    if (params.output_file_name_prefix is None):
        file_name = "%s_box.pdb" % output_prefix
    else:
        file_name = "%s.pdb" % params.output_file_name_prefix
    ph_box = pdb_hierarchy.select(selection)
    ph_box.adopt_xray_structure(box.xray_structure_box)
    ph_box.write_pdb_file(
        file_name=file_name,
        crystal_symmetry=box.xray_structure_box.crystal_symmetry())

    if ("ccp4" in params.output_format):
        if (params.output_file_name_prefix is None):
            file_name = "%s_box.ccp4" % output_prefix
        else:
            file_name = "%s.ccp4" % params.output_file_name_prefix
        print >> log, "writing map to CCP4 formatted file:   %s" % file_name
        box.write_ccp4_map(file_name=file_name)
    if ("xplor" in params.output_format):
        if (params.output_file_name_prefix is None):
            file_name = "%s_box.xplor" % output_prefix
        else:
            file_name = "%s.xplor" % params.output_file_name_prefix
        print >> log, "writing map to X-plor formatted file: %s" % file_name
        box.write_xplor_map(file_name=file_name)
    if ("mtz" in params.output_format):
        if (params.output_file_name_prefix is None):
            file_name = "%s_box.mtz" % output_prefix
        else:
            file_name = "%s.mtz" % params.output_file_name_prefix
        print >> log, "writing map coefficients to MTZ file: %s" % file_name
        if (map_coeff is not None): d_min = map_coeff.d_min()
        else:
            d_min = maptbx.d_min_from_map(
                map_data=box.map_box,
                unit_cell=box.xray_structure_box.unit_cell())
        box.map_coefficients(d_min=d_min,
                             resolution_factor=params.resolution_factor,
                             file_name=file_name)

    if params.ncs_file:

        if (params.output_file_name_prefix is None):
            output_ncs_file = "%s_box.ncs_spec" % output_prefix
        else:
            output_ncs_file = "%s.ncs_spec" % params.output_file_name_prefix
        print >> log, "\nOffsetting NCS in %s and writing to %s" % (
            params.ncs_file, output_ncs_file)
        from mmtbx.ncs.ncs import ncs
        ncs_object = ncs()
        ncs_object.read_ncs(params.ncs_file, log=log)
        ncs_object.display_all(log=log)
        if not ncs_object or ncs_object.max_operators() < 1:
            print >> log, "Skipping...no NCS available"
        elif box.total_shift_cart:
            from scitbx.math import matrix
            print >>log,"Shifting NCS operators "+\
              "based on coordinate shift of (%7.1f,%7.1f,%7.1f)" %(
              tuple(box.total_shift_cart))
            ncs_object = ncs_object.coordinate_offset(
                coordinate_offset=matrix.col(box.total_shift_cart))
            ncs_object.display_all(log=log)
        ncs_object.format_all_for_group_specification(
            file_name=output_ncs_file)
        box.ncs_object = ncs_object
    else:
        box.ncs_object = None
    print >> log
    return box
Example #9
0
def get_map_and_model(params=None,
                      map_data=None,
                      crystal_symmetry=None,
                      pdb_inp=None,
                      ncs_obj=None,
                      half_map_data_list=None,
                      out=sys.stdout):

    acc = None  # accessor used to shift map back to original location if desired
    origin_frac = (0, 0, 0)
    acc = None
    if map_data and crystal_symmetry:
        pass  # we are set

    elif params.input_files.map_file:
        print >> out, "\nReading map from %s\n" % (params.input_files.map_file)
        from cctbx.maptbx.segment_and_split_map import get_map_object
        map_data,space_group,unit_cell,crystal_symmetry,origin_frac,acc=\
          get_map_object(file_name=params.input_files.map_file,out=out)
        map_data = map_data.as_double()
        if origin_frac != (0, 0, 0) and acc is None:
            print >>out,"\nWARNING: Unable to place output map at position of "+\
              "input map though input map has non-zero origin at %s\n" %(
              str(origin_frac))

    elif params.input_files.map_coeffs_file:
        map_coeffs = get_map_coeffs_from_file(
            map_coeffs_file=params.input_files.map_coeffs_file,
            map_coeffs_labels=params.input_files.map_coeffs_labels)

        if not map_coeffs:
            raise Sorry("Could not get map coeffs from %s with labels %s" %
                        (params.input_files.map_coeffs_file,
                         params.input_files.map_coeffs_labels))
        print >> out, "Map coefficients read from %s with labels %s" % (
            params.input_files.map_coeffs_file,
            str(params.input_files.map_coeffs_labels))
        crystal_symmetry = map_coeffs.crystal_symmetry()
        from cctbx.maptbx.segment_and_split_map import get_map_from_map_coeffs
        map_data = get_map_from_map_coeffs(map_coeffs=map_coeffs,
                                           crystal_symmetry=crystal_symmetry)
        acc = map_data.accessor()
        if not params.crystal_info.resolution:
            params.crystal_info.resolution = map_coeffs.d_min()
            print >> out, "Resolution from map_coeffs is %7.2f A" % (
                params.crystal_info.resolution)
    else:
        raise Sorry("Need ccp4 map or map_coeffs")

    if params.input_files.half_map_file:
        if len(params.input_files.half_map_file) != 2:
            raise Sorry("Please supply zero or two half_map files")
        half_map_data_list = []
        from cctbx.maptbx.segment_and_split_map import get_map_object
        for file_name in params.input_files.half_map_file:
            print >> out, "\nReading half-map from %s\n" % (file_name)
            half_map_data,half_map_space_group,half_map_unit_cell,\
              half_map_crystal_symmetry,half_map_origin_frac,half_map_acc=\
              get_map_object(file_name=file_name,out=out)
            half_map_data = half_map_data.as_double()
            assert half_map_crystal_symmetry.is_similar_symmetry(
                crystal_symmetry)

            half_map_data_list.append(half_map_data)

    if params.crystal_info.resolution is None:
        raise Sorry("Need resolution if map is supplied")

    if params.input_files.pdb_file and not pdb_inp:  # get model
        model_file = params.input_files.pdb_file
        if not os.path.isfile(model_file):
            raise Sorry("Missing the model file: %s" % (model_file))
        pdb_inp = iotbx.pdb.input(file_name=model_file)
        if origin_frac != (0, 0, 0):
            print >> out, "Shifting model by %s" % (str(origin_frac))
            from cctbx.maptbx.segment_and_split_map import \
               apply_shift_to_pdb_hierarchy
            origin_shift = crystal_symmetry.unit_cell().orthogonalize(
                (-origin_frac[0], -origin_frac[1], -origin_frac[2]))
            pdb_inp = apply_shift_to_pdb_hierarchy(
                origin_shift=origin_shift,
                crystal_symmetry=crystal_symmetry,
                pdb_hierarchy=pdb_inp.construct_hierarchy(),
                out=out).as_pdb_input()

    if params.input_files.ncs_file and not ncs_obj:  # NCS
        from cctbx.maptbx.segment_and_split_map import get_ncs
        ncs_obj, dummy_tracking_data = get_ncs(params, out=out)
        if origin_frac != (0, 0, 0):
            origin_shift = crystal_symmetry.unit_cell().orthogonalize(
                (-origin_frac[0], -origin_frac[1], -origin_frac[2]))
            print >> out, "Shifting NCS by (%7.2f,%7.2f,%7.2f) " % (
                (origin_shift))
            from scitbx.math import matrix
            ncs_obj = ncs_obj.coordinate_offset(
                coordinate_offset=matrix.col(origin_shift))

    return pdb_inp, map_data, half_map_data_list, ncs_obj, crystal_symmetry, acc
Example #10
0
def run(args, command_name="mmtbx.super"):
  if (len(args) == 0):
    print "usage: %s fixed.pdb moving.pdb [parameter=value ...]" % command_name
    return

  print "#"
  print "#                       ", command_name
  print "#"
  print "# A lightweight sequence-based structure superposition tool."
  print "#"
  print "#"

  phil_objects = []
  argument_interpreter = master_params.command_line_argument_interpreter(
    home_scope="super")
  fixed_pdb_file_name = None
  moving_pdb_file_name = None
  for arg in args:
    if (os.path.isfile(arg)):
      if (fixed_pdb_file_name is None): fixed_pdb_file_name = arg
      elif (moving_pdb_file_name is None): moving_pdb_file_name = arg
      else: raise Sorry("Too many file names.")
    else:
      try: command_line_params = argument_interpreter.process(arg=arg)
      except KeyboardInterrupt: raise
      except Exception: raise Sorry("Unknown file or keyword: %s" % arg)
      else: phil_objects.append(command_line_params)

  working_params = master_params.fetch(sources=phil_objects)
  params = working_params.extract()

  def raise_missing(what):
      raise Sorry("""\
Missing file name for %(what)s structure:
  Please add
    %(what)s=file_name
  to the command line to specify the %(what)s structure.""" % vars())

  if (fixed_pdb_file_name is None):
    if (params.super.fixed is None): raise_missing("fixed")
  else:
    params.super.fixed = fixed_pdb_file_name
  if (moving_pdb_file_name is None):
    if (params.super.moving is None): raise_missing("moving")
  else:
    params.super.moving = moving_pdb_file_name

  print "#Parameters used:"
  print "#phil __ON__"
  print
  working_params = master_params.format(python_object=params)
  working_params.show()
  print
  print "#phil __OFF__"
  print

  print "Reading fixed structure:", params.super.fixed
  fixed_pdb = iotbx.pdb.input(file_name=params.super.fixed)
  print
  print "Reading moving structure:", params.super.moving
  moving_pdb = iotbx.pdb.input(file_name=params.super.moving)
  print

  fixed_seq, fixed_sites, fixed_site_flags = extract_sequence_and_sites(
    pdb_input=fixed_pdb)
  moving_seq, moving_sites, moving_site_flags = extract_sequence_and_sites(
    pdb_input=moving_pdb)

  print "Computing sequence alignment..."
  align_obj = mmtbx.alignment.align(
    seq_a=fixed_seq,
    seq_b=moving_seq,
    gap_opening_penalty=params.super.gap_opening_penalty,
    gap_extension_penalty=params.super.gap_extension_penalty,
    similarity_function=params.super.similarity_matrix,
    style=params.super.alignment_style)
  print "done."
  print

  alignment = align_obj.extract_alignment()
  matches = alignment.matches()
  equal = matches.count("|")
  similar = matches.count("*")
  total = len(alignment.a) - alignment.a.count("-")
  alignment.pretty_print(
    matches=matches,
    block_size=50,
    n_block=1,
    top_name="fixed",
    bottom_name="moving",
    comment="""\
The alignment used in the superposition is shown below.

The sequence identity (fraction of | symbols) is %4.1f%%
of the aligned length of the fixed molecule sequence.

The sequence similarity (fraction of | and * symbols) is %4.1f%%
of the aligned length of the fixed molecule sequence.
""" % (100.*equal/max(1,total), 100.*(equal+similar)/max(1,total)))

  fixed_sites_sel = flex.vec3_double()
  moving_sites_sel = flex.vec3_double()
  for ia,ib,m in zip(alignment.i_seqs_a, alignment.i_seqs_b, matches):
    if (m not in ["|", "*"]): continue
    if (fixed_site_flags[ia] and moving_site_flags[ib]):
      fixed_sites_sel.append(fixed_sites[ia])
      moving_sites_sel.append(moving_sites[ib])

  print "Performing least-squares superposition of C-alpha atom pairs:"
  print "  Number of C-alpha atoms pairs in matching residues"
  print "  indicated by | or * above:", fixed_sites_sel.size()
  if (fixed_sites_sel.size() == 0):
    raise Sorry("No matching C-alpha atoms.")
  lsq_fit = superpose.least_squares_fit(
    reference_sites=fixed_sites_sel,
    other_sites=moving_sites_sel)
  rmsd = fixed_sites_sel.rms_difference(lsq_fit.other_sites_best_fit())
  print "  RMSD between the aligned C-alpha atoms: %.3f" % rmsd
  print

  print "Writing moved pdb to file: %s" % params.super.moved
  pdb_hierarchy = moving_pdb.construct_hierarchy()
  for atom in pdb_hierarchy.atoms():
    atom.xyz = lsq_fit.r * matrix.col(atom.xyz) + lsq_fit.t
  pdb_hierarchy.write_pdb_file(file_name=params.super.moved, append_end=True)
  print
Example #11
0
def run(args, command_name="phenix.xmanip"):
    if (len(args) == 0 or "--help" in args or "--h" in args or "-h" in args):
        print_help(command_name=command_name)
    else:
        log = multi_out()
        if (not "--quiet" in args):
            log.register(label="stdout", file_object=sys.stdout)
        string_buffer = StringIO()
        string_buffer_plots = StringIO()
        log.register(label="log_buffer", file_object=string_buffer)

        phil_objects = []
        argument_interpreter = master_params.command_line_argument_interpreter(
            home_scope="map_coefs")

        print("#phil __OFF__", file=log)
        print("==========================", file=log)
        print("          XMANIP          ", file=log)
        print("reindexing and other tasks", file=log)
        print("==========================", file=log)
        print(file=log)

        for arg in args:
            command_line_params = None
            arg_is_processed = False
            # is it a file?
            if (os.path.isfile(arg)):  ## is this a file name?
                # check if it is a phil file
                try:
                    command_line_params = iotbx.phil.parse(file_name=arg)
                    if command_line_params is not None:
                        phil_objects.append(command_line_params)
                        arg_is_processed = True
                except KeyboardInterrupt:
                    raise
                except Exception:
                    pass
            else:
                try:
                    command_line_params = argument_interpreter.process(arg=arg)
                    if command_line_params is not None:
                        phil_objects.append(command_line_params)
                        arg_is_processed = True
                except KeyboardInterrupt:
                    raise
                except Exception:
                    pass

            if not arg_is_processed:
                print("##----------------------------------------------##",
                      file=log)
                print("## Unknown file or keyword:", arg, file=log)
                print("##----------------------------------------------##",
                      file=log)
                print(file=log)
                raise Sorry("Unknown file or keyword: %s" % arg)

        effective_params = master_params.fetch(sources=phil_objects)
        params = effective_params.extract()

        # now get the unit cell from the files
        hkl_xs = []
        pdb_xs = None

        #multiple file names are allowed
        for xray_data in params.xmanip.input.xray_data:
            if xray_data.file_name is not None:
                hkl_xs.append(
                    crystal_symmetry_from_any.extract_from(
                        file_name=xray_data.file_name))

        if params.xmanip.input.model.file_name is not None:
            pdb_xs = crystal_symmetry_from_any.extract_from(
                file_name=params.xmanip.input.model.file_name)

        phil_xs = crystal.symmetry(
            unit_cell=params.xmanip.input.unit_cell,
            space_group_info=params.xmanip.input.space_group)

        combined_xs = crystal.select_crystal_symmetry(None, phil_xs, [pdb_xs],
                                                      hkl_xs)
        if combined_xs is not None:
            # inject the unit cell and symmetry in the phil scope please
            params.xmanip.input.unit_cell = combined_xs.unit_cell()
            params.xmanip.input.space_group = \
              sgtbx.space_group_info( group = combined_xs.space_group() )

        print("#phil __ON__", file=log)
        new_params = master_params.format(python_object=params)
        new_params.show(out=log)
        print("#phil __END__", file=log)

        if params.xmanip.input.unit_cell is None:
            raise Sorry("unit cell not specified")
        if params.xmanip.input.space_group is None:
            raise Sorry("space group not specified")

        #-----------------------------------------------------------
        #
        # step 1: read in the reflection file
        #

        miller_arrays = []
        labels = []
        label_appendix = []
        write_it = []
        names = {}

        if len(params.xmanip.input.xray_data) > 0:

            phil_xs = crystal.symmetry(
                unit_cell=params.xmanip.input.unit_cell,
                space_group_info=params.xmanip.input.space_group)

            xray_data_server = reflection_file_utils.reflection_file_server(
                crystal_symmetry=phil_xs,
                force_symmetry=True,
                reflection_files=[])

            count = 0
            for xray_data in params.xmanip.input.xray_data:
                if xray_data.file_name is not None:
                    miller_array = None
                    miller_array = read_data(xray_data.file_name,
                                             xray_data.labels, phil_xs)
                    print(file=log)
                    print("Summary info of observed data", file=log)
                    print("=============================", file=log)
                    if miller_array is None:
                        raise Sorry("Failed to read data. see errors above")
                    miller_array.show_summary(f=log)
                    print(file=log)

                    miller_arrays.append(miller_array)
                    labels.append(miller_array.info().labels)
                    label_appendix.append(xray_data.label_appendix)

                    this_name = "COL_" + str(count)
                    if xray_data.name is not None:
                        this_name = xray_data.name
                    #check if this name is allready used
                    if this_name in names:
                        raise Sorry(
                            "Non unique dataset name. Please change the input script"
                        )
                    names.update({this_name: count})
                    count += 1

                    write_it.append(xray_data.write_out)

            output_label_root = construct_output_labels(labels, label_appendix)
            for ii in range(len(labels)):
                test = 0
                for jj in range(ii + 1, len(labels)):
                    for lab_name1, lab_name2 in zip(labels[ii], labels[jj]):
                        if lab_name1 == lab_name2:
                            test += 1
                    if test == 2:
                        print(
                            "\n***** You are trying to import the data with label(s) %s more then one time. ***** \n"
                            % (str(labels[ii])),
                            file=log)
            for ii in range(len(output_label_root)):
                for jj in range(ii + 1, len(output_label_root)):
                    if output_label_root[ii] == output_label_root[jj]:
                        if write_it[ii]:
                            if write_it[jj]:
                                print("Output label roots:", file=log)
                                print(output_label_root, file=log)
                                raise Sorry(
                                    "Output labels are not unique. Modify input."
                                )

        #----------------------------------------------------------------
        # Step 2: get an xray structure from the PDB file
        #
        pdb_model = None
        model = None
        if params.xmanip.input.model.file_name is not None:
            pdb_model = iotbx.pdb.input(
                file_name=params.xmanip.input.model.file_name)
            model = pdb_model.xray_structure_simple(crystal_symmetry=phil_xs)
            print("Atomic model summary", file=log)
            print("====================", file=log)
            model.show_summary(f=log)
            print(file=log)

        write_miller_array = False
        write_pdb_file = False
        # define some output holder thingamebobs
        new_miller_arrays = []
        new_model = None

        #manipulate miller arrays
        if params.xmanip.parameters.action == "manipulate_miller":
            write_miller_array = True
            new_miller = xmanip_tasks.manipulate_miller(
                names, miller_arrays, model,
                params.xmanip.parameters.manipulate_miller, log)
            miller_arrays.append(new_miller)
            # not very smart to rely here on a phil defintion defined in another file
            tmp_root = params.xmanip.parameters.manipulate_miller.output_label_root
            if tmp_root is None:
                tmp_root = "UNSPECIFIED"
            output_label_root.append(tmp_root)
            write_it.append(True)

        if params.xmanip.parameters.action == "reindex":
            write_miller_array = True
            #----------------------------------------------------------------
            # step 3: get the reindex laws
            phil_xs.show_summary()
            to_niggli = phil_xs.change_of_basis_op_to_niggli_cell()
            to_reference = phil_xs.change_of_basis_op_to_reference_setting()
            to_inverse = phil_xs.change_of_basis_op_to_inverse_hand()
            to_primitive = phil_xs.change_of_basis_op_to_primitive_setting()
            cb_op = None
            if (params.xmanip.parameters.reindex.standard_laws == "niggli"):
                cb_op = to_niggli
            if (params.xmanip.parameters.reindex.standard_laws ==
                    "reference_setting"):
                cb_op = to_reference
            if (params.xmanip.parameters.reindex.standard_laws == "invert"):
                cb_op = to_inverse
            if (params.xmanip.parameters.reindex.standard_laws ==
                    "user_supplied"):
                cb_op = sgtbx.change_of_basis_op(
                    params.xmanip.parameters.reindex.user_supplied_law)
            if (params.xmanip.parameters.reindex.standard_laws ==
                    "primitive_setting"):
                cb_op = to_primitive

            if cb_op is None:
                raise Sorry("No change of basis operation is supplied.")

            print("Supplied reindexing law:", file=log)
            print("========================", file=log)
            print("hkl notation: ", cb_op.as_hkl(), file=log)
            print("xyz notation: ", cb_op.as_xyz(), file=log)
            print("abc notation: ", cb_op.as_abc(), file=log)
            #----------------------------------------------------------------
            # step 4: do the reindexing
            #
            # step 4a: first do the miller array object
            #new_miller_arrays = []
            for miller_array in miller_arrays:
                new_miller_array = None
                if miller_array is not None:
                    new_miller_array = miller_array.change_basis(cb_op)
                    new_miller_arrays.append(new_miller_array)
            #
            # step 4b: the xray structure
            if pdb_model is not None:
                write_pdb_file = True
                new_model = model.change_basis(cb_op)

        if write_miller_array:
            if len(new_miller_arrays) == 0:
                new_miller_arrays = miller_arrays
            #----------------------------------------------------------------
            print(file=log)
            print("The data has been reindexed/manipulated", file=log)
            print("--------------------------------------", file=log)
            print(file=log)
            print("Writing output files....", file=log)

            mtz_dataset = None
            if len(new_miller_arrays) > 0:
                first = 0
                for item in range(len(write_it)):
                    if write_it[item]:
                        first = item
                        if new_miller_arrays[first] is not None:
                            break

                if new_miller_arrays[first] is not None:
                    tmp = new_miller_arrays[first].map_to_asu()
                    mtz_dataset = tmp.as_mtz_dataset(
                        column_root_label=output_label_root[first])

            if mtz_dataset is not None:
                for miller_array, new_root in zip(
                        new_miller_arrays[first + 1:],
                        output_label_root[first + 1:]):
                    if miller_array is not None:
                        mtz_dataset = mtz_dataset.add_miller_array(
                            miller_array=miller_array,
                            column_root_label=new_root)

                print("Writing mtz file with name %s" %
                      (params.xmanip.output.hklout),
                      file=log)
                mtz_dataset.mtz_object().write(
                    file_name=params.xmanip.output.hklout)

            #step 5b: write the new pdb file
            if new_model is not None:
                pdb_file = open(params.xmanip.output.xyzout, 'w')
                print("Wring pdb file to: %s" % (params.xmanip.output.xyzout),
                      file=log)
                write_as_pdb_file(input_pdb=pdb_model,
                                  input_xray_structure=new_model,
                                  out=pdb_file,
                                  chain_id_increment=0,
                                  additional_remark="Generated by %s" %
                                  command_name)

                pdb_file.close()
            if ([miller_array, new_model]).count(None) == 2:
                print(
                    "No input reflection of coordinate files have been given",
                    file=log)

        if params.xmanip.parameters.action == "manipulate_pdb":
            if params.xmanip.parameters.manipulate_pdb.task == "apply_operator":
                rt_mx = None
                if params.xmanip.parameters.manipulate_pdb.apply_operator.standard_operators == "user_supplied_operator":
                    rt_mx = sgtbx.rt_mx(
                        params.xmanip.parameters.manipulate_pdb.apply_operator.
                        user_supplied_operator,
                        t_den=12 * 8)
                    print("Applied operator : ", rt_mx.as_xyz(), file=log)
                if params.xmanip.parameters.manipulate_pdb.apply_operator.standard_operators == \
                     "user_supplied_cartesian_rotation_matrix":
                    rt = params.xmanip.parameters.manipulate_pdb.apply_operator.user_supplied_cartesian_rotation_matrix
                    tmp_r = None
                    tmp_t = None
                    if "," in rt.r:
                        tmp_r = rt.r.split(',')
                    else:
                        tmp_r = rt.r.split(' ')
                    if "," in rt.r:
                        tmp_t = rt.t.split(',')
                    else:
                        tmp_t = rt.t.split(' ')
                    tmp_tmp_r = []
                    tmp_tmp_t = []
                    for item in tmp_r:
                        tmp_tmp_r.append(float(item))
                    if len(tmp_tmp_r) != 9:
                        raise Sorry(
                            "Invalid rotation matrix. Please check input: %s" %
                            (rt.r))
                    for item in tmp_t:
                        tmp_tmp_t.append(float(item))
                    if len(tmp_tmp_t) != 3:
                        raise Sorry(
                            "Invalid translational vector. Please check input: %s"
                            % (rt.t))
                    tmp_tmp_t = (tmp_tmp_t)
                    rt_mx = quick_rt_mx(tmp_tmp_r, tmp_tmp_t)
                    print("User supplied cartesian matrix and vector: ",
                          file=log)
                    rt_mx.show()
                    o = matrix.sqr(
                        model.unit_cell().orthogonalization_matrix())
                    tmp_r = o.inverse() * rt_mx.r() * o
                    tmp_t = o.inverse() * matrix.col(list(rt_mx.t()))
                    print(file=log)
                    print("Operator in fractional coordinates: ", file=log)
                    rt_mx = quick_rt_mx(r=tmp_r.as_float(), t=list(tmp_t))
                    rt_mx.show(out=log)
                    print(file=log)

                if params.xmanip.parameters.manipulate_pdb.apply_operator.invert:
                    rt_mx = rt_mx.inverse()
                    print(file=log)
                    print("Taking inverse of given operator", file=log)
                    print(file=log)

                sites = model.sites_frac()
                new_sites = flex.vec3_double()
                for site in sites:
                    new_site = rt_mx.r() * matrix.col(site)
                    new_site = flex.double(new_site) + flex.double(
                        rt_mx.t().as_double())
                    new_sites.append(tuple(new_site))
                new_model = model.deep_copy_scatterers()

                new_model.set_sites_frac(new_sites)
                # write the new [pdb file please
                pdb_file = open(params.xmanip.output.xyzout, 'w')
                print("Wring pdb file to: %s" % (params.xmanip.output.xyzout),
                      file=log)
                if params.xmanip.parameters.manipulate_pdb.apply_operator.concatenate_model:
                    write_as_pdb_file(input_pdb=pdb_model,
                                      input_xray_structure=model,
                                      out=pdb_file,
                                      chain_id_increment=0,
                                      additional_remark=None,
                                      print_cryst_and_scale=True)

                write_as_pdb_file(
                    input_pdb=pdb_model,
                    input_xray_structure=new_model,
                    out=pdb_file,
                    chain_id_increment=params.xmanip.parameters.manipulate_pdb.
                    apply_operator.chain_id_increment,
                    additional_remark=None,
                    print_cryst_and_scale=False)

                pdb_file.close()

            if params.xmanip.parameters.manipulate_pdb.task == "set_b":
                #rest all the b values
                if params.xmanip.parameters.manipulate_pdb.set_b:
                    b_iso = params.xmanip.parameters.manipulate_pdb.b_iso
                    new_model = model.set_b_iso(value=b_iso)
                    print(file=log)
                    print("All B-values have been set to %5.3f" % (b_iso),
                          file=log)
                    print("Writing PDB file %s" %
                          (params.xmanip.output.xyzout),
                          file=log)
                    print(file=log)

                pdb_file = open(params.xmanip.output.xyzout, 'w')
                write_as_pdb_file(input_pdb=pdb_model,
                                  input_xray_structure=new_model,
                                  out=pdb_file,
                                  chain_id_increment=0,
                                  additional_remark=None,
                                  print_cryst_and_scale=True)
                pdb_file.close()

        #write the logfile
        logger = open(params.xmanip.output.logfile, 'w')
        print("Writing log file with name %s  " %
              (params.xmanip.output.logfile),
              file=log)
        print(string_buffer.getvalue()[0:len(string_buffer.getvalue()) - 1],
              file=logger)  #avoid a newline at the end ...
        logger.close()
Example #12
0
  def copy_rot_trans(self,list_of_matrices,list_of_translations,
      change_of_basis_operator=None,
      coordinate_offset=None,
      unit_cell=None,new_unit_cell=None):
    # if change_of_basis_operator is None, then return copy of what we have
    from copy import deepcopy
    from scitbx.math import  matrix
    new_list_of_matrices=[]
    new_list_of_translations=[]
    if change_of_basis_operator is not None:
      a=  matrix.sqr(new_unit_cell.orthogonalization_matrix()) \
        * change_of_basis_operator.c().as_rational().r \
        * matrix.sqr(unit_cell.fractionalization_matrix())
      a_inv=a.inverse()
    else:
      a=None
    for ncs_r,ncs_t in zip(list_of_matrices,list_of_translations):
      if change_of_basis_operator is None and coordinate_offset is None:
        new_list_of_matrices.append(deepcopy(ncs_r))
        new_list_of_translations.append(deepcopy(ncs_t))
      elif coordinate_offset is not None:
        # TT 2015-11-02 special case of below where cob is just a translation
        # R' =  R
        # T' =  T + t - R t
        new_list_of_matrices.append(deepcopy(ncs_r))  # these are the same
        from scitbx import matrix
        delta = ncs_r * coordinate_offset
        t_prime=matrix.col(ncs_t) + \
          matrix.col(coordinate_offset) - matrix.col(delta)
        new_list_of_translations.append(t_prime)

      else:
        # tt 2011-10-02
        # Formula for conversion of NCS rotation matrix and translation
        # relating two points in coordinate system to a second coordinate system
        # The change-of-basis operator is new_x = a x + t
        # The NCS operator is y = R x + T (in original coordinate system)
        # Then if NCS operator in new coordinate system is y' = R' x' + T':
        # R' = a R a_inv
        # T' = a T + t - a R a_inv t = transformed T minus R' * t
        #
        # Derivation:
        # x' and y' (values in new coordinate system) can be written:
        #   x'=a x + t
        #   y'=a y + t
        # Or rewriting:
        #   x= a_inv (x' - t)
        #   y= a_inv (y' - t)
        # Then as y = R x + T  (in original coordinate system), we can say:
        #   a_inv (y' - t) = R (a_inv (x' - t) ) + T
        # Or...
        #   y' = [a R a_inv] x' - [a R a_inv ] t + t + a t
        # So that:
        #   R' = a R a_inv
        #   T' = a T + t - a R a_inv t = transformed T minus R' * t

        # matrices are a ncs_r a_inv
        ncs_r_prime=a * ncs_r * a_inv
        new_list_of_matrices.append(ncs_r_prime)

        # translation vectors are partly a * ncs_t + t (the cob operator)
        frac=unit_cell.fractionalize(ncs_t)
        new_frac = change_of_basis_operator.c() * frac
        new_ncs_t=new_unit_cell.orthogonalize(new_frac)
        # translation vectors depend on the change of basis and the rotation
        # as well as the change-of-basis operator
        t_as_col=change_of_basis_operator.c().t().as_rational().as_float()
        # the basis translation in orig coordinate system
        cob_trans=unit_cell.orthogonalize(t_as_col)
        # correction for the basis translation in new coordinate system
        delta = ncs_r_prime * cob_trans
        t_prime=matrix.col(new_ncs_t) - matrix.col(delta)
        new_list_of_translations.append(t_prime)

    return new_list_of_matrices,new_list_of_translations
Example #13
0
  def extend_helix_operators(self,z_range=None):
    assert self._have_helical_symmetry
    # extend the operators to go from -z_range to z_range
    from copy import deepcopy
    rota_matrices_inv_sav=deepcopy(self.rota_matrices_inv())
    translations_orth_inv_sav=deepcopy(self.translations_orth_inv())
    # only apply centers if some existing ones are not zero
    have_non_zero_c=False
    from scitbx import matrix
    for c in self._centers:
      if list(c)!=[0,0,0]:
        have_non_zero_c=True
        break

    sort_list=[]
    for r,t,c in zip(rota_matrices_inv_sav,translations_orth_inv_sav,self._centers):
      z_value=t[2]
      sort_list.append([z_value,r,t,c])
    sort_list.sort()
    z_first,r_first,t_first,c_first=sort_list[0]
    z_last,r_last,t_last,c_last=sort_list[-1]
    z_next_to_last,r_next_to_last,t_next_to_last,c_next_to_last=sort_list[-2]
    z_translation=self.get_helix_z_translation()
    if not z_translation:
      from libtbx.utils import Sorry
      raise Sorry("Cannot apply extension of helical NCS operators with no"+
        " Z-translation")

    # Figure out which direction to add single shifts to each end
    r_forwards,t_forwards,r_reverse,t_reverse=self.get_forwards_reverse_helix(
      r1=r_next_to_last,t1=t_next_to_last,r2=r_last,t2=t_last)

    # Add on at end until we get to z_max (or z_min if z_translation<0)
    r_list=[r_last]
    t_list=[t_last]
    c_list=[c_last]
    while 1:
      new_r = r_forwards*r_list[-1]
      new_t= (r_forwards * t_list[-1]) + t_forwards
      new_c= (r_forwards*c_list[-1])+t_forwards
      if is_in_range(new_t[2],-z_range,z_range):
        r_list.append(new_r)
        t_list.append(new_t)
        c_list.append(new_c)
      else:
        break
    rota_matrices_inv=rota_matrices_inv_sav+r_list[1:]
    translations_orth_inv=translations_orth_inv_sav+t_list[1:]
    centers=self._centers+c_list[1:]

    # and for other end
    r_list=[r_first]
    t_list=[t_first]
    c_list=[c_first]
    while 1:
      new_r = r_reverse*r_list[-1]
      new_t= (r_reverse * t_list[-1]) + t_reverse
      new_c= (r_reverse*c_list[-1])+t_reverse
      if is_in_range(new_t[2],-z_range,z_range):
        r_list.append(new_r)
        t_list.append(new_t)
        c_list.append(new_c)
      else:
        break
    rota_matrices_inv+=r_list[1:]
    translations_orth_inv+=t_list[1:]
    centers+=c_list[1:]
    # Now we have a new set...invert and save them
    self._n_ncs_oper=len(rota_matrices_inv)
    self._rota_matrices=[]
    self._translations_orth=[]
    self._centers=[]
    for r_inv,t_inv,c in zip(rota_matrices_inv,translations_orth_inv,centers):
      r_std=r_inv.inverse()
      t_std=-1.*r_std*t_inv
      self._rota_matrices.append(r_std)
      self._translations_orth.append(t_std)
      if have_non_zero_c:
        self._centers.append(c)
      else:
        self._centers.append(matrix.col((0,0,0)))

    self.delete_inv()
Example #14
0
def run(args, command_name="mmtbx.super"):
    if len(args) == 0:
        print "usage: %s fixed.pdb moving.pdb [parameter=value ...]" % command_name
        return

    print "#"
    print "#                       ", command_name
    print "#"
    print "# A lightweight sequence-based structure superposition tool."
    print "#"
    print "#"

    phil_objects = []
    argument_interpreter = master_params.command_line_argument_interpreter(home_scope="super")
    fixed_pdb_file_name = None
    moving_pdb_file_name = None
    for arg in args:
        if os.path.isfile(arg):
            if fixed_pdb_file_name is None:
                fixed_pdb_file_name = arg
            elif moving_pdb_file_name is None:
                moving_pdb_file_name = arg
            else:
                raise Sorry("Too many file names.")
        else:
            try:
                command_line_params = argument_interpreter.process(arg=arg)
            except KeyboardInterrupt:
                raise
            except Exception:
                raise Sorry("Unknown file or keyword: %s" % arg)
            else:
                phil_objects.append(command_line_params)

    working_params = master_params.fetch(sources=phil_objects)
    params = working_params.extract()

    def raise_missing(what):
        raise Sorry(
            """\
Missing file name for %(what)s structure:
  Please add
    %(what)s=file_name
  to the command line to specify the %(what)s structure."""
            % vars()
        )

    if fixed_pdb_file_name is None:
        if params.super.fixed is None:
            raise_missing("fixed")
    else:
        params.super.fixed = fixed_pdb_file_name
    if moving_pdb_file_name is None:
        if params.super.moving is None:
            raise_missing("moving")
    else:
        params.super.moving = moving_pdb_file_name

    print "#Parameters used:"
    print "#phil __ON__"
    print
    working_params = master_params.format(python_object=params)
    working_params.show()
    print
    print "#phil __OFF__"
    print

    print "Reading fixed structure:", params.super.fixed
    fixed_pdb = iotbx.pdb.input(file_name=params.super.fixed)
    print
    print "Reading moving structure:", params.super.moving
    moving_pdb = iotbx.pdb.input(file_name=params.super.moving)
    print

    fixed_seq, fixed_sites, fixed_site_flags = extract_sequence_and_sites(pdb_input=fixed_pdb)
    moving_seq, moving_sites, moving_site_flags = extract_sequence_and_sites(pdb_input=moving_pdb)

    print "Computing sequence alignment..."
    align_obj = mmtbx.alignment.align(
        seq_a=fixed_seq,
        seq_b=moving_seq,
        gap_opening_penalty=params.super.gap_opening_penalty,
        gap_extension_penalty=params.super.gap_extension_penalty,
        similarity_function=params.super.similarity_matrix,
        style=params.super.alignment_style,
    )
    print "done."
    print

    alignment = align_obj.extract_alignment()
    matches = alignment.matches()
    equal = matches.count("|")
    similar = matches.count("*")
    total = len(alignment.a) - alignment.a.count("-")
    alignment.pretty_print(
        matches=matches,
        block_size=50,
        n_block=1,
        top_name="fixed",
        bottom_name="moving",
        comment="""\
The alignment used in the superposition is shown below.

The sequence identity (fraction of | symbols) is %4.1f%%
of the aligned length of the fixed molecule sequence.

The sequence similarity (fraction of | and * symbols) is %4.1f%%
of the aligned length of the fixed molecule sequence.
"""
        % (100.0 * equal / max(1, total), 100.0 * (equal + similar) / max(1, total)),
    )

    fixed_sites_sel = flex.vec3_double()
    moving_sites_sel = flex.vec3_double()
    for ia, ib, m in zip(alignment.i_seqs_a, alignment.i_seqs_b, matches):
        if m not in ["|", "*"]:
            continue
        if fixed_site_flags[ia] and moving_site_flags[ib]:
            fixed_sites_sel.append(fixed_sites[ia])
            moving_sites_sel.append(moving_sites[ib])

    print "Performing least-squares superposition of C-alpha atom pairs:"
    print "  Number of C-alpha atoms pairs in matching residues"
    print "  indicated by | or * above:", fixed_sites_sel.size()
    if fixed_sites_sel.size() == 0:
        raise Sorry("No matching C-alpha atoms.")
    lsq_fit = superpose.least_squares_fit(reference_sites=fixed_sites_sel, other_sites=moving_sites_sel)
    rmsd = fixed_sites_sel.rms_difference(lsq_fit.other_sites_best_fit())
    print "  RMSD between the aligned C-alpha atoms: %.3f" % rmsd
    print

    print "Writing moved pdb to file: %s" % params.super.moved
    pdb_hierarchy = moving_pdb.construct_hierarchy()
    for atom in pdb_hierarchy.atoms():
        atom.xyz = lsq_fit.r * matrix.col(atom.xyz) + lsq_fit.t
    pdb_hierarchy.write_pdb_file(file_name=params.super.moved, append_end=True)
    print
Example #15
0
def reindex_utils(args):
    if len(args) == 0:
        print_help()
    elif ("--help" in args):
        print_help()
    elif ("--h" in args):
        print_help()
    elif ("-h" in args):
        print_help()
    else:
        log = multi_out()
        if (not "--quiet" in args):
            log.register(label="stdout", file_object=sys.stdout)
        string_buffer = StringIO()
        string_buffer_plots = StringIO()
        log.register(label="log_buffer", file_object=string_buffer)

        phil_objects = []
        argument_interpreter = master_params.command_line_argument_interpreter(
            home_scope="map_coefs")

        print >> log, "#phil __OFF__"
        print >> log, "================="
        print >> log, "    REINDEX      "
        print >> log, "A reindexing tool"
        print >> log, "================="
        print >> log

        for arg in args:
            command_line_params = None
            arg_is_processed = False
            # is it a file?
            if (os.path.isfile(arg)):  ## is this a file name?
                # check if it is a phil file
                try:
                    command_line_params = iotbx.phil.parse(file_name=arg)
                    if command_line_params is not None:
                        phil_objects.append(command_line_params)
                        arg_is_processed = True
                except KeyboardInterrupt:
                    raise
                except Exception:
                    pass
            else:
                try:
                    command_line_params = argument_interpreter.process(arg=arg)
                    if command_line_params is not None:
                        phil_objects.append(command_line_params)
                        arg_is_processed = True
                except KeyboardInterrupt:
                    raise
                except Exception:
                    pass

            if not arg_is_processed:
                print >> log, "##----------------------------------------------##"
                print >> log, "## Unknown file or keyword:", arg
                print >> log, "##----------------------------------------------##"
                print >> log
                raise Sorry("Unknown file or keyword: %s" % arg)

        effective_params = master_params.fetch(sources=phil_objects)
        params_root = effective_params.extract()
        params = params_root.reindex_utils

        # now get the unit cell from the files
        hkl_xs = None
        pdb_xs = None
        if params.input.xray_data.file_name is not None:
            hkl_xs = crystal_symmetry_from_any.extract_from(
                file_name=params.input.xray_data.file_name)
        if params.input.model.file_name is not None:
            pdb_xs = crystal_symmetry_from_any.extract_from(
                file_name=params.input.model.file_name)

        phil_xs = crystal.symmetry(unit_cell=params.input.unit_cell,
                                   space_group_info=params.input.space_group)

        combined_xs = crystal.select_crystal_symmetry(None, phil_xs, [pdb_xs],
                                                      [hkl_xs])

        # inject the unit cell and symmetry in the phil scope please
        params.input.unit_cell = combined_xs.unit_cell()
        params.input.space_group = combined_xs.space_group_info()

        print >> log, "#phil __ON__"
        new_params = master_params.format(python_object=params_root)
        new_params.show(out=log)
        print >> log, "#phil __END__"

        if params.input.unit_cell is None:
            raise Sorry("unit cell not specified")
        if params.input.space_group is None:
            raise Sorry("space group not specified")

        #-----------------------------------------------------------
        #
        # step 1: read in the reflection file
        #
        miller_array = None
        if params.input.xray_data.file_name is not None:
            phil_xs = crystal.symmetry(
                unit_cell=params.input.unit_cell,
                space_group_info=params.input.space_group)

            xray_data_server = reflection_file_utils.reflection_file_server(
                crystal_symmetry=phil_xs,
                force_symmetry=True,
                reflection_files=[])

            miller_array = xray_data_server.get_xray_data(
                file_name=params.input.xray_data.file_name,
                labels=params.input.xray_data.labels,
                ignore_all_zeros=True,
                parameter_scope='reindex_utils.input.xray_data',
                parameter_name='labels')

            info = miller_array.info()

            miller_array = miller_array.map_to_asu()

            miller_array = miller_array.select(
                miller_array.indices() != (0, 0, 0))

            miller_array = miller_array.select(miller_array.data() > 0)
            if miller_array.sigmas() is not None:
                miller_array = miller_array.select(miller_array.sigmas() > 0)

            if (miller_array.is_xray_intensity_array()):
                miller_array = miller_array.f_sq_as_f()
            elif (miller_array.is_complex_array()):
                miller_array = abs(miller_array)

            miller_array.set_info(info)
            print >> log
            print >> log, "Summary info of observed data"
            print >> log, "============================="
            miller_array.show_summary(f=log)
            print >> log

        #----------------------------------------------------------------
        # Step 2: get an xray structure from the PDB file
        #
        pdb_model = None

        if params.input.model.file_name is not None:
            pdb_model = iotbx.pdb.input(file_name=params.input.model.file_name)
            model = pdb_model.xray_structure_simple(crystal_symmetry=phil_xs)
            print >> log, "Atomic model summary"
            print >> log, "===================="
            model.show_summary()
            print >> log

        if params.parameters.action == "reindex":
            #----------------------------------------------------------------
            # step 3: get the reindex laws
            to_niggli = phil_xs.change_of_basis_op_to_niggli_cell()
            to_reference = phil_xs.change_of_basis_op_to_reference_setting()
            to_inverse = phil_xs.change_of_basis_op_to_inverse_hand()
            cb_op = None
            pr = params.parameters.reindex
            if (pr.standard_laws == "niggli"):
                cb_op = to_niggli
            elif (pr.standard_laws == "reference_setting"):
                cb_op = to_reference
            elif (pr.standard_laws == "invert"):
                cb_op = to_inverse
            else:
                cb_op = sgtbx.change_of_basis_op(pr.user_supplied_law)

            if cb_op is None:
                raise Sorry("No change of basis operation is supplied.")
            if params.parameters.inverse:
                cb_op = cb_op.inverse()

            print >> log, "Supplied reindexing law:"
            print >> log, "========================"
            print >> log, "hkl notation: ", cb_op.as_hkl()
            print >> log, "xyz notation: ", cb_op.as_xyz()
            print >> log, "abc notation: ", cb_op.as_abc()
            #----------------------------------------------------------------
            # step 4: do the reindexing
            #
            # step 4a: first do the miller array object
            new_miller_array = None
            if miller_array is not None:
                new_miller_array = miller_array.change_basis(cb_op)
            #
            # step 4b: the xray structure
            new_model = None
            if pdb_model is not None:
                new_model = model.change_basis(cb_op)

            #----------------------------------------------------------------
            # step 5a: write the new mtz file
            print >> log
            print >> log, "The data and model have been reindexed"
            print >> log, "--------------------------------------"
            print >> log
            print >> log, "Writing output files...."
            if miller_array is not None:
                print >> log, "writing mtz file with name %s" % (
                    params.output.hklout)
                mtz_dataset = new_miller_array.as_mtz_dataset(
                    column_root_label="FOBS")
                mtz_dataset.mtz_object().write(file_name=params.output.hklout)

            #step 5b: write the new pdb file
            if new_model is not None:
                pdb_file = open(params.output.xyzout, 'w')
                print >> log, "Wring pdb file to: %s" % params.output.xyzout
                write_as_pdb_file(
                    input_pdb=pdb_model,
                    input_xray_structure=new_model,
                    out=pdb_file,
                    chain_id_increment=params.parameters.chain_id_increment,
                    additional_remark="Generated by mmtbx reindex")

                print >> pdb_file, "END"
                pdb_file.close()
            if ([miller_array, new_model]).count(None) == 2:
                print >> log, "No input reflection of coordinate files have been given"

        if params.parameters.action == "operator":
            rt_mx = sgtbx.rt_mx(
                params.parameters.apply_operator.user_supplied_operator,
                t_den=12 * 8)
            if params.parameters.inverse:
                rt_mx = rt_mx.inverse()
            print >> log
            print >> log, "Applied operator : ", rt_mx.as_xyz()
            print >> log

            sites = model.sites_frac()
            new_sites = flex.vec3_double()
            for site in sites:
                new_site = rt_mx.r() * matrix.col(site)
                new_site = flex.double(new_site) + flex.double(
                    rt_mx.t().as_double())
                new_sites.append(tuple(new_site))
            new_model = model.deep_copy_scatterers()

            new_model.set_sites_frac(new_sites)
            # write the new [pdb file please
            pdb_file = open(params.output.xyzout, 'w')
            print >> log, "Wring pdb file to: %s" % params.output.xyzout
            if params.parameters.apply_operator.concatenate_model:
                write_as_pdb_file(input_pdb=pdb_model,
                                  input_xray_structure=model,
                                  out=pdb_file,
                                  chain_id_increment=0,
                                  additional_remark=None,
                                  print_cryst_and_scale=True)

            write_as_pdb_file(
                input_pdb=pdb_model,
                input_xray_structure=new_model,
                out=pdb_file,
                chain_id_increment=params.parameters.chain_id_increment,
                additional_remark=None,
                print_cryst_and_scale=False)

            print >> pdb_file, "END"
            pdb_file.close()

        if params.parameters.action == "manipulate_pdb":
            #rest all the b values
            if params.parameters.manipulate_pdb.set_b:
                b_iso = params.reindex_utils.parameters.manipulate_pdb.b_iso
                new_model = model.set_b_iso(value=b_iso)
                print >> log
                print >> log, "All B-values have been set to %5.3f" % (b_iso)
                print >> log, "Writing PDB file %s" % (params.output.xyzout)
                print >> log

            pdb_file = open(params.output.xyzout, 'w')
            write_as_pdb_file(input_pdb=pdb_model,
                              input_xray_structure=new_model,
                              out=pdb_file,
                              chain_id_increment=0,
                              additional_remark=None,
                              print_cryst_and_scale=True)
            print >> pdb_file, "END"
            pdb_file.close()

        #write the logfile
        logger = open(params.output.logfile, 'w')
        print >> log, "Writing log file with name %s" % params.output.logfile
        print >> log
        print >> logger, string_buffer.getvalue()
Example #16
0
def run(args, command_name="phenix.xmanip"):
  if (len(args) == 0 or "--help" in args or "--h" in args or "-h" in args):
    print_help(command_name=command_name)
  else:
    log = multi_out()
    if (not "--quiet" in args):
      log.register(label="stdout", file_object=sys.stdout)
    string_buffer = StringIO()
    string_buffer_plots = StringIO()
    log.register(label="log_buffer", file_object=string_buffer)

    phil_objects = []
    argument_interpreter = master_params.command_line_argument_interpreter(
      home_scope="map_coefs")

    print >> log, "#phil __OFF__"
    print >> log, "=========================="
    print >> log, "          XMANIP          "
    print >> log, "reindexing and other tasks"
    print >> log, "=========================="
    print >> log


    for arg in args:
      command_line_params = None
      arg_is_processed = False
      # is it a file?
      if (os.path.isfile(arg)): ## is this a file name?
        # check if it is a phil file
        try:
          command_line_params = iotbx.phil.parse(file_name=arg)
          if command_line_params is not None:
            phil_objects.append(command_line_params)
            arg_is_processed = True
        except KeyboardInterrupt: raise
        except Exception : pass
      else:
        try:
          command_line_params = argument_interpreter.process(arg=arg)
          if command_line_params is not None:
            phil_objects.append(command_line_params)
            arg_is_processed = True
        except KeyboardInterrupt: raise
        except Exception : pass

      if not arg_is_processed:
        print >> log, "##----------------------------------------------##"
        print >> log, "## Unknown file or keyword:", arg
        print >> log, "##----------------------------------------------##"
        print >> log
        raise Sorry("Unknown file or keyword: %s" % arg)

    effective_params = master_params.fetch(sources=phil_objects)
    params = effective_params.extract()

    # now get the unit cell from the files
    hkl_xs = []
    pdb_xs = None

    #multiple file names are allowed
    for xray_data in params.xmanip.input.xray_data:
      if xray_data.file_name is not None:
        hkl_xs.append( crystal_symmetry_from_any.extract_from(
           file_name=xray_data.file_name) )

    if params.xmanip.input.model.file_name is not None:
      pdb_xs = crystal_symmetry_from_any.extract_from(
        file_name=params.xmanip.input.model.file_name)

    phil_xs = crystal.symmetry(
      unit_cell=params.xmanip.input.unit_cell,
      space_group_info=params.xmanip.input.space_group  )

    combined_xs = crystal.select_crystal_symmetry(
      None,phil_xs, [pdb_xs],hkl_xs)
    if combined_xs is not None:
      # inject the unit cell and symmetry in the phil scope please
      params.xmanip.input.unit_cell = combined_xs.unit_cell()
      params.xmanip.input.space_group = \
        sgtbx.space_group_info( group = combined_xs.space_group() )

    print >> log, "#phil __ON__"
    new_params =  master_params.format(python_object=params)
    new_params.show(out=log)
    print >> log, "#phil __END__"

    if params.xmanip.input.unit_cell is None:
      raise Sorry("unit cell not specified")
    if params.xmanip.input.space_group is None:
      raise Sorry("space group not specified")

    #-----------------------------------------------------------
    #
    # step 1: read in the reflection file
    #

    miller_arrays = []
    labels = []
    label_appendix = []
    write_it = []
    names = {}

    if len(params.xmanip.input.xray_data)>0:

      phil_xs = crystal.symmetry(
        unit_cell=params.xmanip.input.unit_cell,
        space_group_info=params.xmanip.input.space_group  )

      xray_data_server =  reflection_file_utils.reflection_file_server(
        crystal_symmetry = phil_xs,
        force_symmetry = True,
        reflection_files=[])

      count=0
      for xray_data in params.xmanip.input.xray_data:
        if xray_data.file_name is not None:
          miller_array = None
          miller_array = read_data(xray_data.file_name,
                                   xray_data.labels,
                                   phil_xs)
          print >> log
          print >> log, "Summary info of observed data"
          print >> log, "============================="
          if miller_array is None:
            raise Sorry("Failed to read data. see errors above" )
          miller_array.show_summary(f=log)
          print >> log

          miller_arrays.append( miller_array )
          labels.append( miller_array.info().labels )
          label_appendix.append( xray_data.label_appendix )

          this_name = "COL_"+str(count)
          if xray_data.name is not None:
            this_name = xray_data.name
          #check if this name is allready used
          if names.has_key( this_name ):
            raise Sorry( "Non unique dataset name. Please change the input script" )
          names.update( {this_name:count} )
          count += 1

          write_it.append( xray_data.write_out)

      output_label_root = construct_output_labels( labels, label_appendix )
      for ii in range(len(labels)):
        test=0
        for jj in range( ii+1,len(labels) ):
          for lab_name1, lab_name2 in zip(labels[ii],labels[jj]):
            if lab_name1==lab_name2:
              test+=1
          if test == 2:
            print >> log, "\n***** You are trying to import the data with label(s) %s more then one time. ***** \n"%(str(labels[ii]))
      for ii in range(len(output_label_root)):
        for jj in range(ii+1,len(output_label_root)):
          if output_label_root[ii]==output_label_root[jj]:
            if write_it[ii]:
              if write_it[jj]:
                print >> log, "Output label roots:"
                print >> log, output_label_root
                raise Sorry( "Output labels are not unique. Modify input." )



    #----------------------------------------------------------------
    # Step 2: get an xray structure from the PDB file
    #
    pdb_model = None
    model = None
    if params.xmanip.input.model.file_name is not None:
      pdb_model = iotbx.pdb.input(
        file_name=params.xmanip.input.model.file_name)
      model = pdb_model.xray_structure_simple(crystal_symmetry=phil_xs)
      print >> log, "Atomic model summary"
      print >> log, "===================="
      model.show_summary(f=log)
      print >> log


    write_miller_array = False
    write_pdb_file = False
    # define some output holder thingamebobs
    new_miller_arrays = []
    new_model = None

    #manipulate miller arrays
    if params.xmanip.parameters.action == "manipulate_miller":
      write_miller_array = True
      new_miller = xmanip_tasks.manipulate_miller(names,
                                                  miller_arrays,
                                                  model,
                                                  params.xmanip.parameters.manipulate_miller,
                                                  log )
      miller_arrays.append( new_miller )
      # not very smart to rely here on a phil defintion defined in another file
      tmp_root = params.xmanip.parameters.manipulate_miller.output_label_root
      if tmp_root is None:
        tmp_root = "UNSPECIFIED"
      output_label_root.append( tmp_root )
      write_it.append(True)




    if params.xmanip.parameters.action=="reindex":
      write_miller_array = True
      #----------------------------------------------------------------
      # step 3: get the reindex laws
      phil_xs.show_summary()
      to_niggli    = phil_xs.change_of_basis_op_to_niggli_cell()
      to_reference = phil_xs.change_of_basis_op_to_reference_setting()
      to_inverse   = phil_xs.change_of_basis_op_to_inverse_hand()
      to_primitive = phil_xs.change_of_basis_op_to_primitive_setting()
      cb_op = None
      if (params.xmanip.parameters.reindex.standard_laws == "niggli"):
        cb_op = to_niggli
      if (params.xmanip.parameters.reindex.standard_laws == "reference_setting"):
        cb_op = to_reference
      if (params.xmanip.parameters.reindex.standard_laws == "invert"):
        cb_op = to_inverse
      if (params.xmanip.parameters.reindex.standard_laws == "user_supplied"):
        cb_op = sgtbx.change_of_basis_op( params.xmanip.parameters.reindex.user_supplied_law )
      if (params.xmanip.parameters.reindex.standard_laws == "primitive_setting"):
        cb_op = to_primitive


      if cb_op is None:
        raise Sorry("No change of basis operation is supplied.")

      print >> log, "Supplied reindexing law:"
      print >> log, "========================"
      print >> log, "hkl notation: ", cb_op.as_hkl()
      print >> log, "xyz notation: ", cb_op.as_xyz()
      print >> log, "abc notation: ", cb_op.as_abc()
      #----------------------------------------------------------------
      # step 4: do the reindexing
      #
      # step 4a: first do the miller array object
      #new_miller_arrays = []
      for miller_array in miller_arrays:
        new_miller_array = None
        if miller_array is not None:
          new_miller_array = miller_array.change_basis( cb_op )
          new_miller_arrays.append( new_miller_array )
      #
      # step 4b: the xray structure
      if pdb_model is not None:
        write_pdb_file=True
        new_model = model.change_basis( cb_op )


    if write_miller_array:
      if len(new_miller_arrays)==0:
        new_miller_arrays = miller_arrays
      #----------------------------------------------------------------
      print >> log
      print >> log, "The data has been reindexed/manipulated"
      print >> log, "--------------------------------------"
      print >> log
      print >> log, "Writing output files...."

      mtz_dataset=None
      if len(new_miller_arrays)>0:
        first=0
        for item in range(len(write_it)):
          if write_it[item]:
            first=item
            if new_miller_arrays[ first ] is not None:
              break

        if new_miller_arrays[first] is not None:
          tmp = new_miller_arrays[first].map_to_asu()
          mtz_dataset = tmp.as_mtz_dataset(
            column_root_label=output_label_root[first])

      if mtz_dataset is not None:
        for miller_array, new_root in zip(new_miller_arrays[first+1:],
                                          output_label_root[first+1:]):
          if miller_array is not None:
            mtz_dataset = mtz_dataset.add_miller_array(
              miller_array = miller_array,
              column_root_label = new_root)

        print >> log, "Writing mtz file with name %s"%(params.xmanip.output.hklout)
        mtz_dataset.mtz_object().write(
          file_name=params.xmanip.output.hklout)

      #step 5b: write the new pdb file
      if new_model is not None:
        pdb_file = open( params.xmanip.output.xyzout, 'w')
        print >> log, "Wring pdb file to: %s"%(params.xmanip.output.xyzout)
        write_as_pdb_file(
          input_pdb = pdb_model,
          input_xray_structure = new_model,
          out = pdb_file,
          chain_id_increment= 0,
          additional_remark = "Generated by %s" % command_name)

        pdb_file.close()
      if ( [miller_array,new_model]).count(None)==2:
        print >>log, "No input reflection of coordinate files have been given"

    if params.xmanip.parameters.action=="manipulate_pdb":
      if params.xmanip.parameters.manipulate_pdb.task == "apply_operator":
        rt_mx = None
        if params.xmanip.parameters.manipulate_pdb.apply_operator.standard_operators == "user_supplied_operator":
          rt_mx = sgtbx.rt_mx(
            params.xmanip.parameters.manipulate_pdb.apply_operator.user_supplied_operator,t_den=12*8 )
          print >> log, "Applied operator : ", rt_mx.as_xyz()
        if params.xmanip.parameters.manipulate_pdb.apply_operator.standard_operators == \
             "user_supplied_cartesian_rotation_matrix":
          rt = params.xmanip.parameters.manipulate_pdb.apply_operator.user_supplied_cartesian_rotation_matrix
          tmp_r=None
          tmp_t=None
          if "," in rt.r:
            tmp_r = rt.r.split(',')
          else:
            tmp_r = rt.r.split(' ')
          if "," in rt.r:
            tmp_t = rt.t.split(',')
          else:
            tmp_t = rt.t.split(' ')
          tmp_tmp_r=[]
          tmp_tmp_t=[]
          for item in tmp_r:
            tmp_tmp_r.append( float(item) )
          if len(tmp_tmp_r)!=9:
            raise Sorry("Invalid rotation matrix. Please check input: %s"%(rt.r) )
          for item in tmp_t:
            tmp_tmp_t.append( float(item) )
          if len(tmp_tmp_t)!=3:
            raise Sorry("Invalid translational vector. Please check input: %s"%(rt.t) )
          tmp_tmp_t = (tmp_tmp_t)
          rt_mx = quick_rt_mx(tmp_tmp_r, tmp_tmp_t)
          print >> log, "User supplied cartesian matrix and vector: "
          rt_mx.show()
          o = matrix.sqr(model.unit_cell().orthogonalization_matrix())
          tmp_r = o.inverse()*rt_mx.r()*o
          tmp_t = o.inverse()*matrix.col(list(rt_mx.t()))
          print >> log
          print >> log, "Operator in fractional coordinates: "
          rt_mx = quick_rt_mx(r=tmp_r.as_float(), t=list(tmp_t))
          rt_mx.show(out=log)
          print >> log


        if params.xmanip.parameters.manipulate_pdb.apply_operator.invert:
          rt_mx = rt_mx.inverse()
          print >> log
          print >> log, "Taking inverse of given operator"
          print >> log

        sites = model.sites_frac()
        new_sites = flex.vec3_double()
        for site in sites:
          new_site = rt_mx.r()*matrix.col(site)
          new_site = flex.double(new_site)+flex.double( rt_mx.t().as_double() )
          new_sites.append( tuple(new_site) )
        new_model = model.deep_copy_scatterers()

        new_model.set_sites_frac( new_sites )
        # write the new [pdb file please
        pdb_file = open( params.xmanip.output.xyzout, 'w')
        print >> log, "Wring pdb file to: %s"%(params.xmanip.output.xyzout)
        if params.xmanip.parameters.manipulate_pdb.apply_operator.concatenate_model:
          write_as_pdb_file( input_pdb = pdb_model,
                             input_xray_structure = model,
                             out = pdb_file,
                             chain_id_increment = 0,
                             additional_remark = None,
                             print_cryst_and_scale=True )

        write_as_pdb_file( input_pdb = pdb_model,
                           input_xray_structure = new_model,
                           out = pdb_file,
                           chain_id_increment = params.xmanip.parameters.manipulate_pdb.apply_operator.chain_id_increment,
                           additional_remark = None,
                           print_cryst_and_scale=False )

        pdb_file.close()

      if params.xmanip.parameters.manipulate_pdb.task =="set_b":
        #rest all the b values
        if params.xmanip.parameters.manipulate_pdb.set_b:
          b_iso = params.xmanip.parameters.manipulate_pdb.b_iso
          new_model = model.set_b_iso( value = b_iso )
          print >> log
          print >> log, "All B-values have been set to %5.3f"%(b_iso)
          print >> log, "Writing PDB file %s"%(params.xmanip.output.xyzout)
          print >> log

        pdb_file = open( params.xmanip.output.xyzout, 'w')
        write_as_pdb_file( input_pdb = pdb_model,
                           input_xray_structure = new_model,
                           out = pdb_file,
                           chain_id_increment = 0,
                           additional_remark = None,
                           print_cryst_and_scale=True)
        pdb_file.close()

    #write the logfile
    logger = open( params.xmanip.output.logfile, 'w')
    print >> log, "Writing log file with name %s  "%(params.xmanip.output.logfile)
    print >> logger, string_buffer.getvalue()[0:len(string_buffer.getvalue())-1] #avoid a newline at the end ...
    logger.close()