Ejemplo n.º 1
0
 def read_ncs_file(self, file_name=None, log=sys.stdout):
     # Read in an NCS file and make sure its symmetry is similar to others
     from mmtbx.ncs.ncs import ncs
     ncs_object = ncs()
     ncs_object.read_ncs(file_name=file_name, log=log)
     if ncs_object.max_operators() < 2:
         self.ncs_object.set_unit_ncs()
     self.add_ncs_object(ncs_object)
Ejemplo n.º 2
0
 def _try_as_ncs (self) :
   from mmtbx.ncs.ncs import ncs
   from libtbx.utils import null_out
   ncs_object=ncs()
   try: # see if we can read biomtr records
     pdb_inp=iotbx.pdb.input(file_name=self.file_name)
     ncs_object.ncs_from_pdb_input_BIOMT(pdb_inp=pdb_inp,log=null_out(),
       quiet=True)
   except Exception,e: # try as regular ncs object
     ncs_object.read_ncs(file_name=self.file_name,log=sys.stdout,quiet=True)
Ejemplo n.º 3
0
 def _try_as_ncs (self) :
   from mmtbx.ncs.ncs import ncs
   from libtbx.utils import null_out
   ncs_object=ncs()
   try: # see if we can read biomtr records
     pdb_inp=iotbx.pdb.input(file_name=self.file_name)
     ncs_object.ncs_from_pdb_input_BIOMT(pdb_inp=pdb_inp,log=null_out(),
       quiet=True)
   except Exception,e: # try as regular ncs object
     ncs_object.read_ncs(file_name=self.file_name,log=sys.stdout,quiet=True)
Ejemplo n.º 4
0
 def get_results(self):
   from libtbx import group_args
   if not self.ncs_object:
     from mmtbx.ncs.ncs import ncs
     self.ncs_object=ncs()
     self.score=None
     self.cc=None
   return group_args(
    cc = self.cc,
    ncs_object = self.ncs_object,
    ncs_name = str(self.ncs_object.get_ncs_name()),
    ncs_operators = self.ncs_object.max_operators(),
    score=self.score,
   )
Ejemplo n.º 5
0
  def deep_copy(self,change_of_basis_operator=None,unit_cell=None,
      coordinate_offset=None,
      new_unit_cell=None):  # make a copy
    from mmtbx.ncs.ncs import ncs

    # make new ncs object with same overall params as this one:
    new=ncs(exclude_h=self._exclude_h,exclude_d=self._exclude_d)
    new.source_info=self.source_info
    new._ncs_read=self._ncs_read

    # deep_copy over all the ncs groups:
    for ncs_group in self._ncs_groups:
      new._ncs_groups.append(ncs_group.deep_copy(
         change_of_basis_operator=change_of_basis_operator,
         coordinate_offset=coordinate_offset,
         unit_cell=unit_cell,new_unit_cell=new_unit_cell))
    return new
def exercise(file_name=None, pdb_file_name = None, map_file_name = None ,
    split_pdb_file_name = None,
    out = sys.stdout):

  # Set up source data

  if not os.path.isfile(file_name):
    raise Sorry("Missing the file: %s" %(file_name)+"\n")

  print ("Reading from %s" %(file_name))
  from iotbx.map_manager import map_manager
  m = map_manager(file_name)

  print ("Header information from %s:" %(file_name))
  m.show_summary(out = out)

  map_data = m.map_data().deep_copy()
  crystal_symmetry = m.crystal_symmetry()
  unit_cell_parameters = m.crystal_symmetry().unit_cell().parameters()

  print ("\nMap origin: %s Extent %s"  %( map_data.origin(), map_data.all()))
  print ("Original unit cell, not just unit cell of part in this file): %s" %(
     str(unit_cell_parameters)))

  grid_point = (1, 2, 3)
  if map_data.origin() !=  (0, 0, 0): # make sure it is inside
    from scitbx.matrix import col
    grid_point = tuple (col(grid_point)+col(map_data.origin()))
  print ("\nValue of map_data at grid point %s: %.3f" %(str(grid_point),
    map_data[grid_point]))
  print ("Map data is %s" %(type(map_data)))

  random_position = (10, 5, 7.9)
  point_frac = crystal_symmetry.unit_cell().fractionalize(random_position)
  value_at_point_frac = map_data.eight_point_interpolation(point_frac)
  print ("Value of map_data at coordinates %s: %.3f" %(
      str(random_position), value_at_point_frac))

  map_data_as_float = map_data.as_float()
  print ("Map data as float is %s" %(type(map_data_as_float)))


  # make a little model
  sites_cart = flex.vec3_double( ((8, 10, 12), (14, 15, 16)))
  model = model_manager.from_sites_cart(
         atom_name = ' CA ',
         resname = 'ALA',
         chain_id = 'A',
         b_iso = 30.,
         occ = 1.,
         scatterer = 'C',
         sites_cart = sites_cart,
         crystal_symmetry = crystal_symmetry)


  # Move map and a model to place origin at (0, 0, 0)
  # map data is new copy but model is shifted in place.

  from iotbx.map_model_manager import map_model_manager
  mam = map_model_manager(
          map_manager =  m,
          model     = model.deep_copy(),
    )

  # Read in map and model and split up
  dm = DataManager()
  aa = dm.get_map_model_manager(model_file=pdb_file_name,
    map_files=map_file_name)
  bb = dm.get_map_model_manager(model_file=split_pdb_file_name,
    map_files=map_file_name)

  for selection_method in ['by_chain', 'by_segment','supplied_selections',
      'boxes']:
    if selection_method == 'boxes':
      choices = [True, False]
    else:
      choices = [True]
    if selection_method == 'by_chain':
      mask_choices = [True,False]
    else:
      mask_choices = [False]
    for select_final_boxes_based_on_model in choices:
      for skip_empty_boxes in choices:
        for mask_choice in mask_choices:
          if mask_choice: # use split model
            a=bb.deep_copy()
          else: # usual
            a=aa.deep_copy()
          print ("\nRunning split_up_map_and_model with \n"+
            "select_final_boxes_based_on_model="+
           "%s   skip_empty_boxes=%s selection_method=%s" %(
            select_final_boxes_based_on_model,skip_empty_boxes,selection_method))

          if selection_method == 'by_chain':
            print ("Mask around unused atoms: %s" %(mask_choice))
            box_info = a.split_up_map_and_model_by_chain(
              mask_around_unselected_atoms=mask_choice)
          elif selection_method == 'by_segment':
            box_info = a.split_up_map_and_model_by_segment()
          elif selection_method == 'supplied_selections':
            selection = a.model().selection('all')
            box_info = a.split_up_map_and_model_by_supplied_selections(
              selection_list = [selection])
          elif selection_method == 'boxes':
            box_info = a.split_up_map_and_model_by_boxes(
              skip_empty_boxes = skip_empty_boxes,
              select_final_boxes_based_on_model =
                select_final_boxes_based_on_model)
          print (selection_method,skip_empty_boxes,
              len(box_info.selection_list),
              box_info.selection_list[0].count(True))
          assert (selection_method,skip_empty_boxes,
              len(box_info.selection_list),
              box_info.selection_list[0].count(True)) in [
                ('by_chain',True,3,19),
                ("by_chain",True,1,86,),
                ("by_segment",True,1,86,),
                ("supplied_selections",True,1,86,),
                ("boxes",True,13,1,),
                ("boxes",False,36,0,),
                ("boxes",True,13,1,),
                ("boxes",False,36,0,),
                ]




          # Change the coordinates in one box
          small_model = box_info.mmm_list[0].model()
          small_sites_cart = small_model.get_sites_cart()
          from scitbx.matrix import col
          small_sites_cart += col((1,0,0))
          small_model.set_crystal_symmetry_and_sites_cart(
            sites_cart = small_sites_cart,
            crystal_symmetry = small_model.crystal_symmetry())
          # Put everything back together
          a.merge_split_maps_and_models(box_info = box_info)


  mam.box_all_maps_around_model_and_shift_origin()

  shifted_crystal_symmetry = mam.model().crystal_symmetry()
  shifted_model = mam.model()
  shifted_map_data = mam.map_data()

  print ("\nOriginal map origin (grid units):", map_data.origin())
  print ("Original model:\n", model.model_as_pdb())

  print ("Shifted map origin:", shifted_map_data.origin())
  print ("Shifted model:\n", shifted_model.model_as_pdb())


  # Save the map_model manager
  mam_dc=mam.deep_copy()
  print ("dc",mam)
  print ("dc mam_dc",mam_dc)

  # Mask map around atoms
  mam=mam_dc.deep_copy()
  print ("dc mam_dc dc",mam_dc)
  print (mam)
  mam.mask_all_maps_around_atoms(mask_atoms_atom_radius = 3,
     set_outside_to_mean_inside=True, soft_mask=False)
  print ("Mean before masking", mam.map_data().as_1d().min_max_mean().mean)
  assert approx_equal(mam.map_data().as_1d().min_max_mean().mean,
      -0.0585683621466)
  print ("Max before masking", mam.map_data().as_1d().min_max_mean().max)
  assert approx_equal(mam.map_data().as_1d().min_max_mean().max,
      -0.0585683621466)

  # Mask map around atoms, with soft mask
  mam=mam_dc.deep_copy()
  mam.mask_all_maps_around_atoms(mask_atoms_atom_radius = 3, soft_mask = True,
    soft_mask_radius = 5, set_outside_to_mean_inside=True)
  print ("Mean after first masking", mam.map_data().as_1d().min_max_mean().mean)
  assert approx_equal(mam.map_data().as_1d().min_max_mean().mean,
      -0.00177661714805)
  print ("Max after first masking", mam.map_data().as_1d().min_max_mean().max)
  assert approx_equal(mam.map_data().as_1d().min_max_mean().max,
       0.236853733659)

  # Mask map around atoms again
  mam.mask_all_maps_around_atoms(mask_atoms_atom_radius = 3,
     set_outside_to_mean_inside = True, soft_mask=False)
  print ("Mean after second masking", mam.map_data().as_1d().min_max_mean().mean)
  assert approx_equal(mam.map_data().as_1d().min_max_mean().mean,
     -0.0585683621466)
  print ("Max after second masking", mam.map_data().as_1d().min_max_mean().max)
  assert approx_equal(mam.map_data().as_1d().min_max_mean().max,
      -0.0585683621466)

  # Mask around edges
  mam=mam_dc.deep_copy()
  mam.mask_all_maps_around_edges( soft_mask_radius = 3)
  print ("Mean after masking edges", mam.map_data().as_1d().min_max_mean().mean)
  assert approx_equal(mam.map_data().as_1d().min_max_mean().mean,
      0.0155055604192)
  print ("Max after masking edges", mam.map_data().as_1d().min_max_mean().max)
  assert approx_equal(mam.map_data().as_1d().min_max_mean().max,
      0.249827131629)


  print ("\nWriting map_data and model in shifted position (origin at 0, 0, 0)")

  output_file_name = 'shifted_map.ccp4'
  print ("Writing to %s" %(output_file_name))
  mrcfile.write_ccp4_map(
      file_name = output_file_name,
      crystal_symmetry = shifted_crystal_symmetry,
      map_data = shifted_map_data, )

  output_file_name = 'shifted_model.pdb'
  f = open(output_file_name, 'w')
  print (shifted_model.model_as_pdb(), file=f)
  f.close()


  print ("\nWriting map_data and model in original position (origin at %s)" %(
      str(mam.map_manager().origin_shift_grid_units)))

  output_file_name = 'new_map_original_position.ccp4'
  print ("Writing to %s" %(output_file_name))
  mrcfile.write_ccp4_map(
      file_name = output_file_name,
      crystal_symmetry = shifted_crystal_symmetry,
      map_data = shifted_map_data,
      origin_shift_grid_units = mam.map_manager().origin_shift_grid_units)
  print (shifted_model.model_as_pdb())
  output_pdb_file_name = 'new_model_original_position.pdb'
  f = open(output_pdb_file_name, 'w')
  print (shifted_model.model_as_pdb(), file=f)
  f.close()

  # Write as mmcif
  output_cif_file_name = 'new_model_original_position.cif'
  f = open(output_cif_file_name, 'w')
  print (shifted_model.model_as_mmcif(),file = f)
  f.close()


  # Read the new map and model
  import iotbx.pdb
  new_model =  model_manager(
     model_input = iotbx.pdb.input(
         source_info = None,
         lines = flex.split_lines(open(output_pdb_file_name).read())),
         crystal_symmetry = crystal_symmetry)
  assert new_model.model_as_pdb() == model.model_as_pdb()

  new_model_from_cif =  model_manager(
     model_input = iotbx.pdb.input(
         source_info = None,
         lines = flex.split_lines(open(output_cif_file_name).read())),
         crystal_symmetry = crystal_symmetry)
  assert new_model_from_cif.model_as_pdb() == model.model_as_pdb()

  # Read and box the original file again in case we modified m in any
  #   previous tests
  m = map_manager(file_name)
  mam=map_model_manager(model=model.deep_copy(),map_manager=m)
  mam.box_all_maps_around_model_and_shift_origin()

  file_name = output_file_name
  print ("Reading from %s" %(file_name))
  new_map = iotbx.mrcfile.map_reader(file_name = file_name, verbose = False)
  new_map.data = new_map.data.shift_origin()
  print ("Header information from %s:" %(file_name))
  new_map.show_summary(out = out)
  assert new_map.map_data().origin() == mam.map_manager().map_data().origin()
  assert new_map.crystal_symmetry().is_similar_symmetry(mam.map_manager().crystal_symmetry())

  # make a map_model_manager with lots of maps and model and ncs
  from mmtbx.ncs.ncs import ncs
  ncs_object=ncs()
  ncs_object.set_unit_ncs()
  mam = map_model_manager(
          map_manager =  m,
          ncs_object =  ncs_object,
          map_manager_1 =  m.deep_copy(),
          map_manager_2 =  m.deep_copy(),
          extra_model_list =  [model.deep_copy(),model.deep_copy()],
          extra_model_id_list = ["model_1","model_2"],
          extra_map_manager_list =  [m.deep_copy(),m.deep_copy()],
          extra_map_manager_id_list = ["extra_1","extra_2"],
          model     = model.deep_copy(),
    )


  # make a map_model_manager with lots of maps and model and ncs and run
  # with wrapping and ignore_symmetry_conflicts on
  from mmtbx.ncs.ncs import ncs
  ncs_object=ncs()
  ncs_object.set_unit_ncs()
  m.set_ncs_object(ncs_object.deep_copy())
  mam2 = map_model_manager(
          map_manager =  m.deep_copy(),
          ncs_object =  ncs_object.deep_copy(),
          map_manager_1 =  m.deep_copy(),
          map_manager_2 =  m.deep_copy(),
          extra_model_list =  [model.deep_copy(),model.deep_copy()],
          extra_model_id_list = ["model_1","model_2"],
          extra_map_manager_list =  [m.deep_copy(),m.deep_copy()],
          extra_map_manager_id_list = ["extra_1","extra_2"],
          model     = model.deep_copy(),
          ignore_symmetry_conflicts = True,
          wrapping = m.wrapping(),
    )
  assert mam.map_manager().is_similar(mam2.map_manager())
  assert mam.map_manager().is_similar(mam2.map_manager_1())
  for m in mam2.map_managers():
    assert mam.map_manager().is_similar(m)
  assert mam.model().shift_cart() == mam2.model().shift_cart()
  assert mam.model().shift_cart() == mam2.get_model_by_id('model_2').shift_cart()



  print ("OK")
def exercise(file_name, out=sys.stdout):

    # Set up source data
    if not os.path.isfile(file_name):
        raise Sorry("Missing the file: %s" % (file_name) + "\n")

    print("Reading from %s" % (file_name))
    from iotbx.map_manager import map_manager

    m = map_manager(file_name)

    # make a little model
    sites_cart = flex.vec3_double(((8, 10, 12), (14, 15, 16)))
    model = model_manager.from_sites_cart(
        atom_name=' CA ',
        resname='ALA',
        chain_id='A',
        b_iso=30.,
        occ=1.,
        scatterer='C',
        sites_cart=sites_cart,
        crystal_symmetry=m.crystal_symmetry())

    # make a map_model_manager with lots of maps and model and ncs
    from iotbx.map_model_manager import map_model_manager

    from mmtbx.ncs.ncs import ncs
    ncs_object = ncs()
    ncs_object.set_unit_ncs()
    mask_mm = m.deep_copy()
    mask_mm.set_is_mask(True)
    mam = map_model_manager(
        map_manager=m,
        ncs_object=ncs_object,
        map_manager_1=m.deep_copy(),
        map_manager_2=m.deep_copy(),
        extra_map_manager_list=[m.deep_copy(),
                                m.deep_copy(),
                                m.deep_copy()],
        extra_map_manager_id_list=["extra_1", "extra_2", "map_manager_mask"],
        model=model.deep_copy(),
    )
    print(mam.map_manager())
    print(mam.model())
    print(mam.map_manager_1())
    print(mam.map_manager_2())
    print(mam.map_manager_mask())
    print(mam.map_manager().ncs_object())
    all_map_names = mam.map_id_list()
    for id in all_map_names:
        print("Map_manager %s: %s " % (id, mam.get_map_manager_by_id(id)))

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

    # Create a model with ncs
    from iotbx.regression.ncs.tst_ncs import pdb_str_5
    file_name = 'tst_mam.pdb'
    f = open(file_name, 'w')
    print(pdb_str_5, file=f)
    f.close()

    # Generate map data from this model (it has ncs)
    mmm = map_model_manager()
    mmm.generate_map(box_cushion=0, file_name=file_name, n_residues=500)
    ncs_mam = mmm.deep_copy()
    ncs_mam_copy = mmm.deep_copy()

    # Make sure this model has 126 sites (42 sites times 3-fold ncs)
    assert ncs_mam.model().get_sites_cart().size() == 126
    assert approx_equal(ncs_mam.model().get_sites_cart()[0],
                        (23.560999999999996, 8.159, 10.660000000000002))

    # Get just unique part (42 sites)
    unique_mam = ncs_mam.extract_all_maps_around_model(
        select_unique_by_ncs=True)
    assert unique_mam.model().get_sites_cart().size() == 42
    assert approx_equal(unique_mam.model().get_sites_cart()[0],
                        (18.740916666666664, 13.1794, 16.10544))

    # Make sure that the extraction did not change the original but does change
    #   the extracted part
    assert (unique_mam.model().get_sites_cart()[0] !=
            ncs_mam.model().get_sites_cart()[0]
            )  # it was a deep copy so original stays

    # Shift back the extracted part and make sure it matches the original now
    shifted_back_unique_model = mmm.get_model_from_other(
        unique_mam.deep_copy())
    assert approx_equal(shifted_back_unique_model.get_sites_cart()[0],
                        (23.560999999999996, 8.158999999999997, 10.66))

    # Change the extracted model
    sites_cart = unique_mam.model().get_sites_cart()
    sites_cart[0] = (1, 1, 1)
    unique_mam.model().get_hierarchy().atoms().set_xyz(sites_cart)
    # Note; setting xyz in hierarchy does not set xrs by itself. do that now:
    unique_mam.model().set_sites_cart_from_hierarchy(multiply_ncs=False)

    # Make sure we really changed it
    assert approx_equal(unique_mam.model().get_sites_cart()[0], (1, 1, 1))

    # Now propagate all the changes in this unique part to entire original model
    #   using NCS
    ncs_mam.propagate_model_from_other(other=unique_mam,
                                       model_id='model',
                                       other_model_id='model')
    # ...and check that copy 1 and copy 2 both change
    assert approx_equal(
        ncs_mam.model().get_sites_cart()[0],
        (5.820083333333333, -4.020400000000001, -4.445440000000001))
    assert approx_equal(
        ncs_mam.model().get_sites_cart()[42],
        (38.41904613024224, 17.233251085893276, 2.5547442135142524))

    # Find ncs from map or model
    nn = ncs_mam_copy
    nn.write_map('ncs.ccp4')
    nn.write_model('ncs.pdb')
    ncs_object = nn.get_ncs_from_model()
    dm.write_ncs_spec_file(ncs_object, 'ncs.ncs_spec')
    print("NCS from map", ncs_object)
    nn.set_ncs_object(ncs_object)
    print("NCS now: ", nn.ncs_object())
    nn.get_ncs_from_map(ncs_object=ncs_object)
    print("ncs cc:", nn.ncs_cc())
    assert approx_equal(nn.ncs_cc(), 0.961915979834, eps=0.01)

    # Make a deep_copy
    dc = mam.deep_copy()
    new_mam = mam.deep_copy()
    assert mam.map_manager().map_data()[0] == new_mam.map_manager().map_data(
    )[0]

    # Make a customized_copy
    new_mam = mam.customized_copy(model_dict={'model': mam.model()})
    assert new_mam.model() is mam.model()
    assert not new_mam.map_dict() is mam.map_dict()

    new_mam = mam.customized_copy(model_dict={'model': mam.model()},
                                  map_dict=mam.map_dict())
    assert new_mam.model() is mam.model()
    assert new_mam.map_dict() is mam.map_dict()
    print(mam)

    # Add a map
    mam = dc.deep_copy()
    print(mam.map_id_list())
    assert len(mam.map_id_list()) == 6
    mam.add_map_manager_by_id(mam.map_manager().deep_copy(), 'new_map_manager')
    print(mam.map_id_list())
    assert len(mam.map_id_list()) == 7

    # duplicate a map
    mam = dc.deep_copy()
    print(mam.map_id_list())
    assert len(mam.map_id_list()) == 6
    mam.duplicate_map_manager('map_manager', 'new_map_manager')
    print(mam.map_id_list())
    assert len(mam.map_id_list()) == 7

    # resolution_filter a map
    mam = dc.deep_copy()
    print(mam.map_id_list())
    mam.duplicate_map_manager('map_manager', 'new_map_manager')
    mam.resolution_filter(map_id='new_map_manager', d_min=3.5, d_max=6)

    # Add a model
    mam = dc.deep_copy()
    print(mam.model_id_list())
    assert len(mam.model_id_list()) == 1
    mam.add_model_by_id(mam.model().deep_copy(), 'new_model')
    print(mam.model_id_list())
    assert len(mam.model_id_list()) == 2

    # Initialize a map
    mam1 = new_mam.deep_copy()
    mam1.initialize_maps(map_value=6)
    assert mam1.map_manager().map_data()[225] == 6

    # Create mask around density and apply to all maps
    mam1 = new_mam.deep_copy()
    mam1.mask_all_maps_around_density(
        solvent_content=0.5,
        soft_mask=True,
    )
    s = (mam1.get_map_manager_by_id('mask').map_data() > 0.5)
    assert approx_equal((s.count(True), s.size()), (1024, 2048))

    # Create mask around edges and apply to all maps
    mam1 = new_mam.deep_copy()
    mam1.mask_all_maps_around_edges()
    s = (mam1.get_map_manager_by_id('mask').map_data() > 0.5)
    assert approx_equal((s.count(True), s.size()), (1176, 2048))

    # Create a soft mask around model and apply to all maps
    new_mam.mask_all_maps_around_atoms(mask_atoms_atom_radius=8,
                                       soft_mask=True)
    s = (new_mam.get_map_manager_by_id('mask').map_data() > 0.5)
    assert approx_equal((s.count(True), s.size()), (1944, 2048))

    # Create a soft mask around model and do not do anything with it
    new_mam.create_mask_around_atoms(mask_atoms_atom_radius=8, soft_mask=True)
    s = (new_mam.get_map_manager_by_id('mask').map_data() > 0.5)
    assert approx_equal((s.count(True), s.size()), (1944, 2048))

    # Create a soft mask around model and do not do anything with it, wrapping =true
    dummy_mam = new_mam.deep_copy()
    dummy_mam.map_manager().set_wrapping(True)
    dummy_mam.create_mask_around_atoms(mask_atoms_atom_radius=8,
                                       soft_mask=True)
    s = (dummy_mam.get_map_manager_by_id('mask').map_data() > 0.5)
    assert approx_equal((s.count(True), s.size()), (1944, 2048))

    # Create a sharp mask around model and do not do anything with it
    new_mam.create_mask_around_atoms(soft_mask=False, mask_atoms_atom_radius=8)
    s = (new_mam.get_map_manager_by_id('mask').map_data() > 0.5)
    assert approx_equal((s.count(True), s.size()), (138, 2048))

    # Mask around edges and do not do anything with it
    mam = dc.deep_copy()
    mam.create_mask_around_edges()
    s = (mam.get_map_manager_by_id('mask').map_data() > 0.5)
    assert approx_equal((s.count(True), s.size()), (1176, 2048))

    # Mask around density and to not do anything with it
    mam = dc.deep_copy()
    mam.create_mask_around_density(soft_mask=False)
    s = (mam.get_map_manager_by_id('mask').map_data() > 0.5)
    assert approx_equal((s.count(True), s.size()), (1000, 2048))

    # Apply the current mask to one map
    mam.apply_mask_to_map('map_manager')
    s = (mam.map_manager().map_data() > 0.)
    assert approx_equal((s.count(True), s.size()), (640, 2048))
    s = (mam.map_manager().map_data() != 0.)
    assert approx_equal((s.count(True), s.size()), (1000, 2048))
    assert approx_equal((mam.map_manager().map_data()[225]), -0.0418027862906)

    # Apply any mask to one map
    mam.apply_mask_to_map('map_manager', mask_id='mask')
    s = (mam.map_manager().map_data() > 0.)
    assert approx_equal((s.count(True), s.size()), (640, 2048))
    s = (mam.map_manager().map_data() != 0.)
    assert approx_equal((s.count(True), s.size()), (1000, 2048))
    assert approx_equal((mam.map_manager().map_data()[225]), -0.0418027862906)

    # Apply the mask to all maps
    mam.apply_mask_to_maps()
    s = (mam.map_manager().map_data() > 0.)
    assert approx_equal((s.count(True), s.size()), (640, 2048))
    s = (mam.map_manager().map_data() != 0.)
    assert approx_equal((s.count(True), s.size()), (1000, 2048))
    assert approx_equal((mam.map_manager().map_data()[225]), -0.0418027862906)

    # Apply the mask to all maps, setting outside value to mean inside
    mam.apply_mask_to_maps(set_outside_to_mean_inside=True)
    s = (mam.map_manager().map_data() > 0.)
    assert approx_equal((s.count(True), s.size()), (1688, 2048))
    s = (mam.map_manager().map_data() != 0.)
    assert approx_equal((s.count(True), s.size()), (2048, 2048))
    assert approx_equal((mam.map_manager().map_data()[2047]), -0.0759598612785)
    s = (mam.get_map_manager_by_id('mask').map_data() > 0).as_1d()
    inside = mam.map_manager().map_data().as_1d().select(s)
    outside = mam.map_manager().map_data().as_1d().select(~s)
    assert approx_equal(
        (inside.min_max_mean().max, outside.min_max_mean().max),
        (0.335603952408, 0.0239064293122))

    # Make a new map and model, get mam and box with selection
    mmm = map_model_manager()
    mmm.generate_map(box_cushion=0, wrapping=True)
    mam = mmm
    mam_dc = mam.deep_copy()

    new_mm_1 = mam.map_manager()
    assert approx_equal((mmm.map_data().all(), new_mm_1.map_data().all()),
                        ((18, 25, 20), (18, 25, 20)))

    # Get local fsc or randomized map
    dc = mam_dc.deep_copy()
    dc.map_manager().set_wrapping(False)
    map_coeffs = dc.map_manager().map_as_fourier_coefficients(d_min=3)
    from cctbx.development.create_models_or_maps import generate_map
    new_mm_1 = generate_map(map_coeffs=map_coeffs,
                            d_min=3,
                            low_resolution_real_space_noise_fraction=1,
                            high_resolution_real_space_noise_fraction=50,
                            map_manager=dc.map_manager(),
                            random_seed=124321)
    new_mm_2 = generate_map(map_coeffs=map_coeffs,
                            d_min=3,
                            low_resolution_real_space_noise_fraction=1,
                            high_resolution_real_space_noise_fraction=50,
                            map_manager=dc.map_manager(),
                            random_seed=734119)
    dc.add_map_manager_by_id(new_mm_1, 'map_manager_1')
    dc.add_map_manager_by_id(new_mm_2, 'map_manager_2')
    cc = dc.map_map_cc()
    fsc_curve = dc.map_map_fsc()
    dc.set_log(sys.stdout)
    dc.local_fsc(n_boxes=1)

    # Get map-map FSC
    dc = mam_dc.deep_copy()
    dc.duplicate_map_manager(map_id='map_manager', new_map_id='filtered')
    dc.resolution_filter(d_min=3.5, d_max=10, map_id='filtered')
    dc.create_mask_around_atoms()
    fsc_curve = dc.map_map_fsc(map_id_1='map_manager',
                               map_id_2='filtered',
                               mask_id='mask',
                               resolution=3.5,
                               fsc_cutoff=0.97)
    assert approx_equal(fsc_curve.d_min, 3.91175024213, eps=0.01)
    assert approx_equal(fsc_curve.fsc.fsc[-1], 0.695137718033)

    # Get map-map CC
    dc = mam_dc.deep_copy()
    dc.duplicate_map_manager(map_id='map_manager', new_map_id='filtered')
    dc.resolution_filter(d_min=3.5, d_max=6, map_id='filtered')
    cc = dc.map_map_cc('map_manager', 'filtered')
    assert approx_equal(cc, 0.706499206126)

    # Get map-map CC with mask
    dc = mam_dc.deep_copy()
    dc.duplicate_map_manager(map_id='map_manager', new_map_id='filtered')
    dc.create_mask_around_density(mask_id='filtered')
    cc = dc.map_map_cc('map_manager', 'filtered', mask_id='mask')
    assert approx_equal(cc, 0.411247493741)

    # box around model
    mam = mam_dc.deep_copy()
    mam.box_all_maps_around_model_and_shift_origin(
        selection_string="resseq 221:221")
    new_mm_1 = mam.map_manager()
    assert approx_equal((mmm.map_data().all(), new_mm_1.map_data().all()),
                        ((18, 25, 20), (24, 20, 20)))

    # extract_around_model (get new mam)
    new_mam_dc = mam_dc.extract_all_maps_around_model(
        selection_string="resseq 221:221")
    new_mm_1a = new_mam_dc.map_manager()
    assert approx_equal((mmm.map_data().all(), new_mm_1a.map_data().all()),
                        ((18, 25, 20), (24, 20, 20)))
    assert approx_equal(new_mm_1.map_data(), new_mm_1a.map_data())

    # box around_density
    mam2 = mam_dc.deep_copy()
    mam2.box_all_maps_around_density_and_shift_origin(box_cushion=0)
    new_mm_2 = mam2.map_manager()
    assert approx_equal((mmm.map_data().all(), new_mm_2.map_data().all()),
                        ((18, 25, 20), (16, 23, 18)))

    # extract_around_density (get new mam)
    mam2 = mam_dc.deep_copy()
    mam2_b = mam2.extract_all_maps_around_density(box_cushion=0)
    new_mm_2 = mam2_b.map_manager()
    assert approx_equal((mmm.map_data().all(), new_mm_2.map_data().all()),
                        ((18, 25, 20), (16, 23, 18)))

    # Repeat as map_model_manager:
    mmm = mam_dc.as_map_model_manager().deep_copy()
    mmm.box_all_maps_around_model_and_shift_origin(
        selection_string="resseq 221:221")
    new_mm_1a = mmm.map_manager()
    assert approx_equal((mmm.map_data().all(), new_mm_1a.map_data().all()),
                        ((24, 20, 20), (24, 20, 20)))
    assert approx_equal(new_mm_1.map_data(), new_mm_1a.map_data())

    # box around density
    mam.box_all_maps_around_density_and_shift_origin(box_cushion=0)
    new_mm_1 = mam.map_manager()
    assert approx_equal((mmm.map_data().all(), new_mm_1.map_data().all()),
                        ((24, 20, 20), (22, 18, 18)))

    # extract around density (get new mam)
    mam1 = mam_dc.deep_copy()
    mam1.extract_all_maps_around_density(box_cushion=0)
    new_mm_1 = mam1.map_manager()
    assert approx_equal((mmm.map_data().all(), new_mm_1.map_data().all()),
                        ((24, 20, 20), (18, 25, 20)))

    # create mask around density, then box around mask (i.e., box around density)
    mam.create_mask_around_density(soft_mask=False)
    mam.box_all_maps_around_mask_and_shift_origin(box_cushion=3)
    new_mm_1 = mam.map_manager()
    assert approx_equal((mmm.map_data().all(), new_mm_1.map_data().all()),
                        ((24, 20, 20), (22, 18, 18)))

    # box with bounds
    mam.box_all_maps_with_bounds_and_shift_origin(lower_bounds=(10, 10, 10),
                                                  upper_bounds=(15, 15, 15))
    new_mm_1 = mam.map_manager()
    assert approx_equal((mmm.map_data().all(), new_mm_1.map_data().all()),
                        ((24, 20, 20), (6, 6, 6)))

    # extract with bounds
    mam = mam_dc.deep_copy()
    mam_1 = mam.extract_all_maps_with_bounds(lower_bounds=(10, 10, 10),
                                             upper_bounds=(15, 15, 15))
    new_mm_1 = mam_1.map_manager()
    assert approx_equal((mmm.map_data().all(), new_mm_1.map_data().all()),
                        ((24, 20, 20), (6, 6, 6)))

    # box with unique
    mam = mam_dc.deep_copy()
    mam.box_all_maps_around_unique_and_shift_origin(molecular_mass=2500,
                                                    resolution=3)
    new_mm_1 = mam.map_manager()
    assert approx_equal((mmm.map_data().all(), new_mm_1.map_data().all()),
                        ((24, 20, 20), (18, 25, 20)))

    # extract with unique
    mam = mam_dc.deep_copy()
    mam_1 = mam.extract_all_maps_around_unique(molecular_mass=2500,
                                               resolution=3)
    new_mm_1 = mam_1.map_manager()
    assert approx_equal((mmm.map_data().all(), new_mm_1.map_data().all()),
                        ((24, 20, 20), (18, 25, 20)))

    # extract a box and then restore model into same reference as current mam
    mam = mam_dc.deep_copy()
    mam.box_all_maps_with_bounds_and_shift_origin(lower_bounds=(2, 2, 2),
                                                  upper_bounds=(17, 17, 17))
    print("mam:",
          mam.model().get_sites_cart()[0],
          mam.map_manager().origin_is_zero())
    # extract a box
    box_mam = mam.extract_all_maps_with_bounds(lower_bounds=(10, 10, 10),
                                               upper_bounds=(15, 15, 15))
    box_model = box_mam.model()
    matched_box_model = mam.get_model_from_other(box_mam)
    assert approx_equal(matched_box_model.get_sites_cart()[0],
                        mam.model().get_sites_cart()[0])

    # Convert a map to fourier coefficients
    mam = mam_dc.deep_copy()
    ma = mam.map_as_fourier_coefficients(d_min=3)
    assert approx_equal(ma.d_min(), 3.01655042414)

    mam.add_map_from_fourier_coefficients(ma, map_id='new_map_manager')
    cc = flex.linear_correlation(
        mam.get_map_manager_by_id('map_manager').map_data().as_1d(),
        mam.get_map_manager_by_id(
            'new_map_manager').map_data().as_1d()).coefficient()
    assert (cc >= 0.99)

    # Get map-model CC
    dc = mam_dc.extract_all_maps_around_model(
        selection_string="(name ca or name cb or name c or name o) " +
        "and resseq 221:221",
        box_cushion=0)
    cc = dc.map_model_cc(resolution=3)
    assert approx_equal(cc, 0.450025539936)

    # Remove model outside map
    dc.remove_model_outside_map(boundary=0)
    assert (mam_dc.model().get_sites_cart().size(),
            dc.model().get_sites_cart().size()) == (86, 4)

    # shift a model to match the map
    dc = mam_dc.extract_all_maps_around_model(
        selection_string="(name ca or name cb or name c or name o) " +
        "and resseq 221:221",
        box_cushion=0)
    actual_model = dc.model().deep_copy()
    working_model = dc.model().deep_copy()
    working_model.set_shift_cart((0, 0, 0))
    working_model.set_sites_cart(working_model.get_sites_cart() -
                                 actual_model.shift_cart())
    dc.shift_any_model_to_match(working_model)
    assert approx_equal(actual_model.get_sites_cart()[0],
                        working_model.get_sites_cart()[0])
Ejemplo n.º 8
0
    selection = pdb_hierarchy.atom_selection_cache().selection(
        string=params.selection)
    if selection.size():
        print_statistics.make_sub_header("atom selection", out=log)
        print >> log, "Selection string: selection='%s'" % params.selection
        print >> log, \
            "  selects %d atoms from total %d atoms."%(selection.count(True),
            selection.size())
    sites_cart_all = xray_structure.sites_cart()
    sites_cart = sites_cart_all.select(selection)
    selection = xray_structure.selection_within(radius=params.selection_radius,
                                                selection=selection)

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

    # Get sequence if extract_unique is set
    sequence = None
    if params.extract_unique:
        if params.sequence_file:
Ejemplo n.º 9
0
def run(args, crystal_symmetry=None,
     ncs_object=None,
     pdb_hierarchy=None,
     map_data=None,
     mask_data=None,
     half_map_data_list=None,
     half_map_labels_list=None,
     lower_bounds=None,
     upper_bounds=None,
     write_output_files=True,
     log=None):
  h = "phenix.map_box: extract box with model and map around selected atoms"
  if(log is None): log = sys.stdout
  print_statistics.make_header(h, out=log)
  default_message="""\

%s.

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

or

  phenix.map_box map.ccp4 density_select=True

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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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



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

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

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

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

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

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

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

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

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

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

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

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

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



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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  print(file=log)
  return box
Ejemplo n.º 10
0
    def get_ncs_info_as_spec(self,
                             exclude_h=None,
                             exclude_d=None,
                             stem=None,
                             write_ncs_domain_pdb=False,
                             log=None):
        """
    XXX This function should be transfered to mmtbx/ncs/ncs.py:ncs class as
    its classmethod, because it creates an object and this is the task of
    a constructor. And it definetely should be decoupled from file creation!


    Returns ncs spec object and can prints ncs info in a ncs_spec,
    format_all_for_resolve or format_all_for_phenix_refine format

    Note that while ncs_groups can master ncs can be comprised from several
    chains, the spec groups can not. So groups with multiple chains in the
    master selection are splitted

    Note that spec format does not support insertions notation
    for example "resseq 49" will include "resid 49" and "resid 49A"

    Args:
      write: (bool) when False, will not write to file or print
      exclude_h,exclude_d : parameters of the ncs object
    Return:
      spec_object
    """
        log = log or self.log
        if not stem: stem = ''
        else: stem += '_'
        spec_object = ncs.ncs(exclude_h=exclude_h, exclude_d=exclude_d)
        xyz = self.truncated_hierarchy.atoms().extract_xyz()
        #===============================================================
        # New implementation
        # Here we have original i_seqs already in ncs_restraints_group_list,
        # so we should use self.hierarchy for everything
        xyz = self.hierarchy.atoms().extract_xyz()
        assert self.ncs_restraints_group_list is not None
        splitted_nrgl = self.ncs_restraints_group_list.split_by_chains(
            hierarchy=self.hierarchy)
        for i_group, group in enumerate(splitted_nrgl):
            center_orth = []
            rotations = []
            translations = []
            # chain id
            chain_id_list = []
            # this is [ [[1, 2] [5, 6]] ]
            residue_range_list = []
            rmsd_list = []
            # number of residues
            residues_count = []

            # Putting master in:
            center_orth.append(get_center_orth(xyz, group.master_iselection))
            rotations.append(matrix.sqr([1, 0, 0, 0, 1, 0, 0, 0, 1]))
            translations.append(matrix.col([0, 0, 0]))
            chain_id, ranges, count = get_chain_and_ranges(
                self.hierarchy.select(group.master_iselection))
            chain_id_list.append(chain_id)
            residue_range_list.append(ranges)
            residues_count.append(count)
            rmsd_list.append(0)
            for c in group.copies:
                center_orth.append(get_center_orth(xyz, c.iselection))
                # in spec files transform is copy -> master, not master -> copy
                r, t = inverse_transform(c.r, c.t)
                rotations.append(r)
                translations.append(t)

                chain_id, ranges, count = get_chain_and_ranges(
                    self.hierarchy.select(c.iselection))
                chain_id_list.append(chain_id)
                residue_range_list.append(ranges)
                residues_count.append(count)
                rmsd_list.append(c.rmsd)
            # XXX This should be consistent with full_file_name parameter in
            # simple_ncs_from_pdb.py: create_ncs_domain_pdb_files()
            # This is here just because we need to output filename of the domain
            # into the spec file if pdb file is going to be created...
            ncs_domain_pdb = None
            if write_ncs_domain_pdb:
                ncs_domain_pdb = stem + 'group_' + str(i_group + 1) + '.pdb'
            spec_object.import_ncs_group(
                center_orth=center_orth,
                ncs_rota_matr=rotations,
                trans_orth=translations,
                rmsd_list=rmsd_list,
                chain_residue_id=[chain_id_list, residue_range_list],
                residues_in_common_list=residues_count,
                ncs_domain_pdb=ncs_domain_pdb)
        spec_object._ncs_obj = self
        return spec_object
Ejemplo n.º 11
0
def run(args, log=sys.stdout, as_gui_program=False):
    if (len(args) == 0):
        parsed = defaults(log=log)
        parsed.show(prefix="  ", out=log)
        return
    command_line = (option_parser().enable_symmetry_comprehensive().option(
        "-q",
        "--quiet",
        action="store_true",
        default=False,
        help="suppress output").option("--output_plots",
                                       action="store_true",
                                       default=False)).process(args=args)
    parsed = defaults(log=log)
    processed_args = mmtbx.utils.process_command_line_args(
        args=command_line.args,
        cmd_cs=command_line.symmetry,
        master_params=parsed,
        log=log,
        suppress_symmetry_related_errors=True)
    processed_args.params.show(out=log)
    params = processed_args.params.extract().density_modification
    output_plots = command_line.options.output_plots

    crystal_symmetry = crystal.symmetry(
        unit_cell=params.input.unit_cell,
        space_group_info=params.input.space_group)
    reflection_files = {}
    for rfn in (params.input.reflection_data.file_name,
                params.input.experimental_phases.file_name,
                params.input.map_coefficients.file_name):
        if os.path.isfile(str(rfn)) and rfn not in reflection_files:
            reflection_files.setdefault(
                rfn,
                iotbx.reflection_file_reader.any_reflection_file(
                    file_name=rfn, ensure_read_access=False))
    # TODO is reflection_files a dict ?
    server = iotbx.reflection_file_utils.reflection_file_server(
        crystal_symmetry=crystal_symmetry,
        reflection_files=list(reflection_files.values()))
    fo = mmtbx.utils.determine_data_and_flags(
        server,
        parameters=params.input.reflection_data,
        extract_r_free_flags=False,
        log=log).f_obs
    hl_coeffs = mmtbx.utils.determine_experimental_phases(
        server,
        params.input.experimental_phases,
        log=log,
        parameter_scope="",
        working_point_group=None,
        symmetry_safety_check=True,
        ignore_all_zeros=True)
    if params.input.map_coefficients.file_name is not None:
        map_coeffs = server.get_phases_deg(
            file_name=params.input.map_coefficients.file_name,
            labels=params.input.map_coefficients.labels,
            convert_to_phases_if_necessary=False,
            original_phase_units=None,
            parameter_scope="",
            parameter_name="labels").map_to_asu()
    else:
        map_coeffs = None
    ncs_object = None
    if params.input.ncs_file_name is not None:
        ncs_object = ncs.ncs()
        ncs_object.read_ncs(params.input.ncs_file_name)
        ncs_object.display_all(log=log)

    fo = fo.map_to_asu()
    hl_coeffs = hl_coeffs.map_to_asu()

    fo = fo.eliminate_sys_absent().average_bijvoet_mates()
    hl_coeffs = hl_coeffs.eliminate_sys_absent().average_bijvoet_mates()

    model_map = None
    model_map_coeffs = None
    if len(processed_args.pdb_file_names):
        pdb_inp = mmtbx.utils.pdb_inp_from_multiple_files(
            pdb_files=processed_args.pdb_file_names, log=log)
        xs = pdb_inp.xray_structure_simple()
        fo_, hl_ = fo, hl_coeffs
        if params.change_basis_to_niggli_cell:
            change_of_basis_op = xs.change_of_basis_op_to_niggli_cell()
            xs = xs.change_basis(change_of_basis_op)
            fo_ = fo_.change_basis(change_of_basis_op).map_to_asu()
            hl_ = hl_.change_basis(change_of_basis_op).map_to_asu()
        #fo_, hl_ = fo_.common_sets(hl_)
        fmodel_refined = mmtbx.utils.fmodel_simple(
            f_obs=fo_,
            scattering_table=
            "wk1995",  #XXX pva: 1) neutrons? 2) move up as a parameter.
            xray_structures=[xs],
            bulk_solvent_correction=True,
            anisotropic_scaling=True,
            r_free_flags=fo_.array(data=flex.bool(fo_.size(), False)))
        fmodel_refined.update(abcd=hl_)

        master_phil = mmtbx.maps.map_and_map_coeff_master_params()
        map_params = master_phil.fetch(
            iotbx.phil.parse("""\
map_coefficients {
  map_type = 2mFo-DFc
  isotropize = True
}
""")).extract().map_coefficients[0]
        model_map_coeffs = mmtbx.maps.map_coefficients_from_fmodel(
            fmodel=fmodel_refined, params=map_params)
        model_map = model_map_coeffs.fft_map(
            resolution_factor=params.grid_resolution_factor).real_map_unpadded(
            )

    import time

    t0 = time.time()
    dm = density_modify(params,
                        fo,
                        hl_coeffs,
                        ncs_object=ncs_object,
                        map_coeffs=map_coeffs,
                        model_map_coeffs=model_map_coeffs,
                        log=log,
                        as_gui_program=as_gui_program)
    time_dm = time.time() - t0
    print("Time taken for density modification: %.2fs" % time_dm, file=log)
    # run cns
    if 0:
        from cctbx.development import cns_density_modification
        cns_result = cns_density_modification.run(params, fo, hl_coeffs)
        print(cns_result.modified_map.all())
        print(dm.map.all())
        dm_map_coeffs = dm.map_coeffs_in_original_setting
        from cctbx import maptbx, miller
        crystal_gridding = maptbx.crystal_gridding(
            dm_map_coeffs.unit_cell(),
            space_group_info=dm_map_coeffs.space_group().info(),
            pre_determined_n_real=cns_result.modified_map.all())
        dm_map = miller.fft_map(crystal_gridding,
                                dm_map_coeffs).apply_sigma_scaling()
        corr = flex.linear_correlation(cns_result.modified_map.as_1d(),
                                       dm_map.real_map_unpadded().as_1d())
        print("CNS dm/mmtbx dm correlation:")
        corr.show_summary()
        if dm.model_map_coeffs is not None:
            model_map = miller.fft_map(
                crystal_gridding,
                dm.miller_array_in_original_setting(
                    dm.model_map_coeffs)).apply_sigma_scaling()
            corr = flex.linear_correlation(
                cns_result.modified_map.as_1d(),
                model_map.real_map_unpadded().as_1d())
            print("CNS dm/model correlation:")
            corr.show_summary()

    if output_plots:
        plots_to_make = (
            "fom",
            "skewness",
            "r1_factor",
            "r1_factor_fom",
            "mean_solvent_density",
            "mean_protein_density",
            "f000_over_v",
            "k_flip",
            "rms_solvent_density",
            "rms_protein_density",
            "standard_deviation_local_rms",
            "mean_delta_phi",
            "mean_delta_phi_initial",
        )
        from matplotlib.backends.backend_pdf import PdfPages
        from libtbx import pyplot

        stats = dm.get_stats()
        pdf = PdfPages("density_modification.pdf")

        if len(dm.correlation_coeffs) > 1:
            if 0:
                start_coeffs, model_coeffs = dm.map_coeffs_start.common_sets(
                    model_map_coeffs)
                model_phases = model_coeffs.phases(deg=True).data()
                exptl_phases = nearest_phase(
                    model_phases,
                    start_coeffs.phases(deg=True).data(),
                    deg=True)
                corr = flex.linear_correlation(exptl_phases, model_phases)
                corr.show_summary()
                fig = pyplot.figure()
                ax = fig.add_subplot(1, 1, 1)
                ax.set_title("phases start")
                ax.set_xlabel("Experimental phases")
                ax.set_ylabel("Phases from refined model")
                ax.scatter(exptl_phases, model_phases, marker="x", s=10)
                pdf.savefig(fig)
                #
                dm_coeffs, model_coeffs = dm.map_coeffs.common_sets(
                    model_map_coeffs)
                model_phases = model_coeffs.phases(deg=True).data()
                dm_phases = nearest_phase(model_phases,
                                          dm_coeffs.phases(deg=True).data(),
                                          deg=True)
                corr = flex.linear_correlation(dm_phases, model_phases)
                corr.show_summary()
                fig = pyplot.figure()
                ax = fig.add_subplot(1, 1, 1)
                ax.set_title("phases dm")
                ax.set_xlabel("Phases from density modification")
                ax.set_ylabel("Phases from refined model")
                ax.scatter(dm_phases, model_phases, marker="x", s=10)
                pdf.savefig(fig)
            #
            data = dm.correlation_coeffs
            fig = pyplot.figure()
            ax = fig.add_subplot(1, 1, 1)
            ax.set_title("correlation coefficient")
            ax.plot(list(range(1, dm.i_cycle + 2)), data)
            pdf.savefig(fig)
            #
            data = dm.mean_phase_errors
            fig = pyplot.figure()
            ax = fig.add_subplot(1, 1, 1)
            ax.set_title("Mean effective phase errors")
            ax.plot(list(range(1, dm.i_cycle + 2)), data)
            pdf.savefig(fig)

        for plot in plots_to_make:
            data = [
                getattr(stats.get_cycle_stats(i), plot)
                for i in range(1, dm.i_cycle + 2)
            ]
            fig = pyplot.figure()
            ax = fig.add_subplot(1, 1, 1)
            ax.set_title(plot.replace("_", " "))
            ax.plot(list(range(1, dm.i_cycle + 2)), data)
            pdf.savefig(fig)

        data = [
            stats.get_cycle_stats(i).rms_solvent_density /
            stats.get_cycle_stats(i).rms_protein_density
            for i in range(1, dm.i_cycle + 2)
        ]
        fig = pyplot.figure()
        ax = fig.add_subplot(1, 1, 1)
        ax.set_title("RMS solvent/protein density ratio")
        ax.plot(list(range(1, dm.i_cycle + 2)), data)
        pdf.savefig(fig)

        pdf.close()

    dm_map_coeffs = dm.map_coeffs_in_original_setting
    dm_hl_coeffs = dm.hl_coeffs_in_original_setting

    # output map if requested
    map_params = params.output.map
    if map_params.file_name is not None:
        fft_map = dm_map_coeffs.fft_map(
            resolution_factor=params.grid_resolution_factor)
        if map_params.scale == "sigma":
            fft_map.apply_sigma_scaling()
        else:
            fft_map.apply_volume_scaling()
        gridding_first = gridding_last = None
        title_lines = []
        if map_params.format == "xplor":
            fft_map.as_xplor_map(file_name=map_params.file_name,
                                 title_lines=title_lines,
                                 gridding_first=gridding_first,
                                 gridding_last=gridding_last)
        else:
            fft_map.as_ccp4_map(file_name=map_params.file_name,
                                gridding_first=gridding_first,
                                gridding_last=gridding_last,
                                labels=title_lines)

    # output map coefficients if requested
    mtz_params = params.output.mtz

    # Decide if we are going to actually write the mtz
    if mtz_params.file_name is not None:
        orig_fom, final_fom = dm.start_and_end_fom()
        if mtz_params.skip_output_if_worse and final_fom < orig_fom:
            ok_to_write_mtz = False
            print(
                "Not writing out mtz. Final FOM (%7.3f) worse than start (%7.3f)"
                % (final_fom, orig_fom))
        else:  # usual
            ok_to_write_mtz = True
    else:
        ok_to_write_mtz = True

    if mtz_params.file_name is not None and ok_to_write_mtz:
        label_decorator = iotbx.mtz.ccp4_label_decorator()
        fo = dm.miller_array_in_original_setting(
            dm.f_obs_complete).common_set(dm_map_coeffs)
        mtz_dataset = fo.as_mtz_dataset(column_root_label="F",
                                        label_decorator=label_decorator)
        mtz_dataset.add_miller_array(dm_map_coeffs,
                                     column_root_label="FWT",
                                     label_decorator=label_decorator)
        phase_source = dm.miller_array_in_original_setting(
            dm.phase_source).common_set(dm_map_coeffs)
        mtz_dataset.add_miller_array(
            phase_source.array(data=flex.abs(phase_source.data())),
            column_root_label="FOM",
            column_types='W',
            label_decorator=label_decorator)
        mtz_dataset.add_miller_array(
            phase_source.array(data=phase_source.phases(deg=True).data()),
            column_root_label="PHIB",
            column_types='P',
            label_decorator=None)
        if mtz_params.output_hendrickson_lattman_coefficients:
            mtz_dataset.add_miller_array(dm_hl_coeffs,
                                         column_root_label="HL",
                                         label_decorator=label_decorator)
        mtz_dataset.mtz_object().write(mtz_params.file_name)

    return result(map_file=map_params.file_name,
                  mtz_file=mtz_params.file_name,
                  stats=dm.get_stats())
Ejemplo n.º 12
0
def get_ncs_obj(file_name,out=sys.stdout):
  from mmtbx.ncs.ncs import ncs
  ncs_object=ncs()
  ncs_object.read_ncs(file_name=file_name,log=out)
  return ncs_object
Ejemplo n.º 13
0
def run(args,
        crystal_symmetry=None,
        ncs_object=None,
        pdb_hierarchy=None,
        map_data=None,
        lower_bounds=None,
        upper_bounds=None,
        write_output_files=True,
        log=None):
    h = "phenix.map_box: extract box with model and map around selected atoms"
    if (log is None): log = sys.stdout
    print_statistics.make_header(h, out=log)
    default_message = """\

%s.

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

or

  phenix.map_box map.ccp4 density_select=True

Parameters:""" % h
    if (len(args) == 0 and not pdb_hierarchy):
        print default_message
        master_phil.show(prefix="  ")
        return
    inputs = mmtbx.utils.process_command_line_args(args=args,
                                                   cmd_cs=crystal_symmetry,
                                                   master_params=master_phil)
    params = inputs.params.extract()
    master_phil.format(python_object=params).show(out=log)

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

    # PDB file
    if params.pdb_file and not inputs.pdb_file_names and not pdb_hierarchy:
        inputs.pdb_file_names = [params.pdb_file]
    if (len(inputs.pdb_file_names) != 1 and not params.density_select
            and not pdb_hierarchy and not params.keep_map_size
            and not params.upper_bounds and not params.extract_unique):
        raise Sorry("PDB file is needed unless extract_unique, " +
                    "density_select, keep_map_size \nor bounds are set .")
    if (len(inputs.pdb_file_names)!=1 and not pdb_hierarchy and \
         (params.mask_atoms or params.soft_mask )):
        raise Sorry("PDB file is needed for mask_atoms or soft_mask")
    if (params.density_select and params.keep_map_size):
        raise Sorry("Cannot set both density_select and keep_map_size")
    if (params.density_select and params.upper_bounds):
        raise Sorry("Cannot set both density_select and bounds")
    if (params.keep_map_size and params.upper_bounds):
        raise Sorry("Cannot set both keep_map_size and bounds")
    if (params.upper_bounds and not params.lower_bounds):
        raise Sorry("Please set lower_bounds if you set upper_bounds")
    if (params.extract_unique and not params.resolution):
        raise Sorry("Please set resolution for extract_unique")
    print_statistics.make_sub_header("pdb model", out=log)
    if len(inputs.pdb_file_names) > 0:
        pdb_inp = iotbx.pdb.input(file_name=inputs.pdb_file_names[0])
        pdb_hierarchy = pdb_inp.construct_hierarchy()
    if pdb_hierarchy:
        pdb_atoms = pdb_hierarchy.atoms()
        pdb_atoms.reset_i_seq()
    else:
        pdb_hierarchy = None
    # Map or map coefficients
    map_coeff = None
    if (not map_data):
        # read first mtz file
        if ((len(inputs.reflection_file_names) > 0)
                or (params.map_coefficients_file is not None)):
            # file in phil takes precedent
            if (params.map_coefficients_file is not None):
                if (len(inputs.reflection_file_names) == 0):
                    inputs.reflection_file_names.append(
                        params.map_coefficients_file)
                else:
                    inputs.reflection_file_names[
                        0] = params.map_coefficients_file
            map_coeff = reflection_file_utils.extract_miller_array_from_file(
                file_name=inputs.reflection_file_names[0],
                label=params.label,
                type="complex",
                log=log)
            if not crystal_symmetry:
                crystal_symmetry = map_coeff.crystal_symmetry()
            fft_map = map_coeff.fft_map(
                resolution_factor=params.resolution_factor)
            fft_map.apply_sigma_scaling()
            map_data = fft_map.real_map_unpadded()
            map_or_map_coeffs_prefix = os.path.basename(
                inputs.reflection_file_names[0][:-4])
        # or read CCP4 map
        elif ((inputs.ccp4_map is not None)
              or (params.ccp4_map_file is not None)):
            if (params.ccp4_map_file is not None):
                af = any_file(params.ccp4_map_file)
                if (af.file_type == 'ccp4_map'):
                    inputs.ccp4_map = af.file_content
                    inputs.ccp4_map_file_name = params.ccp4_map_file
            print_statistics.make_sub_header("CCP4 map", out=log)
            ccp4_map = inputs.ccp4_map
            ccp4_map.show_summary(prefix="  ", out=log)
            if not crystal_symmetry:
                crystal_symmetry = ccp4_map.crystal_symmetry()
            map_data = ccp4_map.data  #map_data()
            if inputs.ccp4_map_file_name.endswith(".ccp4"):
                map_or_map_coeffs_prefix = os.path.basename(
                    inputs.ccp4_map_file_name[:-5])
            else:
                map_or_map_coeffs_prefix = os.path.basename(
                    inputs.ccp4_map_file_name[:-4])
    else:  # have map_data
        map_or_map_coeffs_prefix = None

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

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

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

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

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

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

        if sequence and not params.molecular_mass:
            # get molecular mass from sequence
            from iotbx.bioinformatics import text_from_chains_matching_chain_type
            if params.chain_type in [None, 'PROTEIN']:
                n_protein = len(
                    text_from_chains_matching_chain_type(text=sequence,
                                                         chain_type='PROTEIN'))
            else:
                n_protein = 0
            if params.chain_type in [None, 'RNA']:
                n_rna = len(
                    text_from_chains_matching_chain_type(text=sequence,
                                                         chain_type='RNA'))
            else:
                n_rna = 0
            if params.chain_type in [None, 'DNA']:
                n_dna = len(
                    text_from_chains_matching_chain_type(text=sequence,
                                                         chain_type='DNA'))
            else:
                n_dna = 0
            params.molecular_mass = n_protein * 110 + (n_rna + n_dna) * 330
        elif not params.molecular_mass:
            raise Sorry(
                "Need a sequence file or molecular mass for extract_unique")
    else:
        molecular_mass = None
#
    if params.density_select:
        print_statistics.make_sub_header(
            "Extracting box around selected density and writing output files",
            out=log)
    else:
        print_statistics.make_sub_header(
            "Extracting box around selected atoms and writing output files",
            out=log)
    #
    if params.value_outside_atoms == 'mean':
        print >> log, "\nValue outside atoms mask will be set to mean inside mask"
    if params.get_half_height_width and params.density_select:
        print >> log, "\nHalf width at half height will be used to id boundaries"
    if params.soft_mask and sites_cart_all.size() > 0:
        print >> log, "\nSoft mask will be applied to model-based mask"
    if params.keep_map_size:
        print >> log, "\nEntire map will be kept (not cutting out region)"
    if params.restrict_map_size:
        print >> log, "\nOutput map will be within input map"
    if params.lower_bounds and params.upper_bounds:
        print >> log, "Bounds for cut out map are (%s,%s,%s) to (%s,%s,%s)" % (
            tuple(list(params.lower_bounds) + list(params.upper_bounds)))

    box = mmtbx.utils.extract_box_around_model_and_map(
        xray_structure=xray_structure,
        map_data=map_data.as_double(),
        box_cushion=params.box_cushion,
        selection=selection,
        density_select=params.density_select,
        threshold=params.density_select_threshold,
        get_half_height_width=params.get_half_height_width,
        mask_atoms=params.mask_atoms,
        soft_mask=params.soft_mask,
        soft_mask_radius=params.soft_mask_radius,
        mask_atoms_atom_radius=params.mask_atoms_atom_radius,
        value_outside_atoms=params.value_outside_atoms,
        keep_map_size=params.keep_map_size,
        restrict_map_size=params.restrict_map_size,
        lower_bounds=params.lower_bounds,
        upper_bounds=params.upper_bounds,
        extract_unique=params.extract_unique,
        chain_type=params.chain_type,
        sequence=sequence,
        solvent_content=params.solvent_content,
        molecular_mass=params.molecular_mass,
        resolution=params.resolution,
        ncs_object=ncs_object,
        symmetry=params.symmetry,
    )

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

    if (inputs and inputs.crystal_symmetry and inputs.ccp4_map
            and inputs.crystal_symmetry.unit_cell().parameters()
            and inputs.ccp4_map.unit_cell_parameters) and (
                inputs.crystal_symmetry.unit_cell().parameters() !=
                inputs.ccp4_map.unit_cell_parameters):
        print >> log, "\nNOTE: Mismatch of unit cell parameters from CCP4 map:"
        print >>log,"Unit cell from CCP4 map 'unit cell parameters': "+\
          "%.1f, %.1f, %.1f, %.1f, %.1f, %.1f)" %tuple(
            inputs.ccp4_map.unit_cell_parameters)
        print >>log,"Unit cell from CCP4 map 'map grid':             "+\
          "%.1f, %.1f, %.1f, %.1f, %.1f, %.1f)" %tuple(
           inputs.crystal_symmetry.unit_cell().parameters())
        print >>log,"\nInterpreting this as the 'unit cell parameters' was "+\
          "original map \ndimension and 'map grid' is the "+\
          "portion actually in the map that was supplied here.\n"
        box.unit_cell_parameters_from_ccp4_map = inputs.ccp4_map.unit_cell_parameters
        box.unit_cell_parameters_deduced_from_map_grid=\
           inputs.crystal_symmetry.unit_cell().parameters()

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

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

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

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

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

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

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

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

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

            if (params.output_file_name_prefix is None):
                file_name = "%s_box.pdb" % output_prefix
            else:
                file_name = "%s.pdb" % params.output_file_name_prefix

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

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

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

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

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

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

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

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

        # Write xplor map.  Shift back if keep_origin=True
        if ("xplor" in params.output_format):
            if (params.output_file_name_prefix is None):
                file_name = "%s_box.xplor" % output_prefix
            else:
                file_name = "%s.xplor" % params.output_file_name_prefix
            if params.keep_origin:
                print >> log, "Writing boxed map with box unit_cell and original "+\
                  "position\n    to X-plor formatted file: %s"%file_name
            else:
                print >> log, "Writing box_map shifted to (0,0,0) to X-plor "+\
                  "formatted file: %s"%file_name
            box.write_xplor_map(file_name=file_name,
                                shift_back=params.keep_origin)

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

            if params.keep_origin:
                print >> log, "Writing map coefficients with box_map unit_cell"+\
                  " but position matching\n   "+\
                  " original position to MTZ file: %s"%file_name
            else:
                print >> log, "Writing box_map coefficients shifted to (0,0,0) "+\
                   "to MTZ file: %s"%file_name
            if (map_coeff is not None):
                d_min = map_coeff.d_min()
            elif params.resolution is not None:
                d_min = params.resolution
            else:
                d_min = maptbx.d_min_from_map(
                    map_data=box.map_box,
                    unit_cell=box.xray_structure_box.unit_cell())
            box.map_coefficients(d_min=d_min,
                                 resolution_factor=params.resolution_factor,
                                 file_name=file_name,
                                 shift_back=params.keep_origin)

    print >> log
    return box
Ejemplo n.º 14
0
def run(args, crystal_symmetry=None,
     ncs_object=None,
     pdb_hierarchy=None,
     map_data=None,
     lower_bounds=None,
     upper_bounds=None,
     write_output_files=True,
     log=None):
  h = "phenix.map_box: extract box with model and map around selected atoms"
  if(log is None): log = sys.stdout
  print_statistics.make_header(h, out=log)
  default_message="""\

%s.

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

or

  phenix.map_box map.ccp4 density_select=True

Parameters:"""%h
  if(len(args) == 0 and not pdb_hierarchy):
    print default_message
    master_phil.show(prefix="  ")
    return
  inputs = mmtbx.utils.process_command_line_args(args = args,
    cmd_cs=crystal_symmetry,
    master_params = master_phil)
  params = inputs.params.extract()
  master_phil.format(python_object=params).show(out=log)

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

  # PDB file
  if params.pdb_file and not inputs.pdb_file_names and not pdb_hierarchy:
    inputs.pdb_file_names=[params.pdb_file]
  if(len(inputs.pdb_file_names)!=1 and not params.density_select and not
    pdb_hierarchy and not params.keep_map_size and not params.upper_bounds
     and not params.extract_unique):
    raise Sorry("PDB file is needed unless extract_unique, "+
      "density_select, keep_map_size \nor bounds are set .")
  if (len(inputs.pdb_file_names)!=1 and not pdb_hierarchy and \
       (params.mask_atoms or params.soft_mask )):
    raise Sorry("PDB file is needed for mask_atoms or soft_mask")
  if (params.density_select and params.keep_map_size):
    raise Sorry("Cannot set both density_select and keep_map_size")
  if (params.density_select and params.upper_bounds):
    raise Sorry("Cannot set both density_select and bounds")
  if (params.keep_map_size and params.upper_bounds):
    raise Sorry("Cannot set both keep_map_size and bounds")
  if (params.upper_bounds and not params.lower_bounds):
    raise Sorry("Please set lower_bounds if you set upper_bounds")
  if (params.extract_unique and not params.resolution):
    raise Sorry("Please set resolution for extract_unique")
  if (write_output_files) and ("mtz" in params.output_format) and (
       (params.keep_origin) and (not params.keep_map_size)):
    raise Sorry("Please set output_format=ccp4 to skip mtz or set "+\
      "keep_origin=False or keep_map_size=True")

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

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

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


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

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



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

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

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

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

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

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

    if sequence and not params.molecular_mass:
      # get molecular mass from sequence
      from iotbx.bioinformatics import text_from_chains_matching_chain_type
      if params.chain_type in [None,'PROTEIN']:
        n_protein=len(text_from_chains_matching_chain_type(
          text=sequence,chain_type='PROTEIN'))
      else:
        n_protein=0
      if params.chain_type in [None,'RNA']:
        n_rna=len(text_from_chains_matching_chain_type(
          text=sequence,chain_type='RNA'))
      else:
        n_rna=0
      if params.chain_type in [None,'DNA']:
        n_dna=len(text_from_chains_matching_chain_type(
         text=sequence,chain_type='DNA'))
      else:
        n_dna=0
      params.molecular_mass=n_protein*110+(n_rna+n_dna)*330
    elif not params.molecular_mass:
      raise Sorry("Need a sequence file or molecular mass for extract_unique")
  else:
    molecular_mass=None
#
  if params.density_select:
    print_statistics.make_sub_header(
    "Extracting box around selected density and writing output files", out=log)
  else:
   print_statistics.make_sub_header(
    "Extracting box around selected atoms and writing output files", out=log)
  #
  if params.value_outside_atoms=='mean':
    print >>log,"\nValue outside atoms mask will be set to mean inside mask"
  if params.get_half_height_width and params.density_select:
    print >>log,"\nHalf width at half height will be used to id boundaries"
  if params.soft_mask and sites_cart_all.size()>0:
    print >>log,"\nSoft mask will be applied to model-based mask"
  if params.keep_map_size:
    print >>log,"\nEntire map will be kept (not cutting out region)"
  if params.restrict_map_size:
    print >>log,"\nOutput map will be within input map"
  if params.lower_bounds and params.upper_bounds:
    print >>log,"Bounds for cut out map are (%s,%s,%s) to (%s,%s,%s)" %(
     tuple(list(params.lower_bounds)+list(params.upper_bounds)))

  box = mmtbx.utils.extract_box_around_model_and_map(
    xray_structure   = xray_structure,
    map_data         = map_data.as_double(),
    box_cushion      = params.box_cushion,
    selection        = selection,
    density_select   = params.density_select,
    threshold        = params.density_select_threshold,
    get_half_height_width = params.get_half_height_width,
    mask_atoms       = params.mask_atoms,
    soft_mask        = params.soft_mask,
    soft_mask_radius = params.soft_mask_radius,
    mask_atoms_atom_radius = params.mask_atoms_atom_radius,
    value_outside_atoms = params.value_outside_atoms,
    keep_map_size         = params.keep_map_size,
    restrict_map_size     = params.restrict_map_size,
    lower_bounds          = params.lower_bounds,
    upper_bounds          = params.upper_bounds,
    extract_unique        = params.extract_unique,
    chain_type            = params.chain_type,
    sequence              = sequence,
    solvent_content       = params.solvent_content,
    molecular_mass        = params.molecular_mass,
    resolution            = params.resolution,
    ncs_object            = ncs_object,
    symmetry              = params.symmetry,

    )

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

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

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



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

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

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

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

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

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

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


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

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

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

  ph_output_box_output_location = ph_box.deep_copy()
  if output_box.shift_cart:  # shift coordinates and NCS back by shift_cart
    # NOTE output_box.shift_cart could be different than box.shift_cart if
    #  there is a target position for the origin and it is not the same as the
    #  original origin.
    sites_cart = output_box.shift_sites_cart_back(
      output_box.xray_structure_box.sites_cart())
    xrs_offset = ph_output_box_output_location.extract_xray_structure(
        crystal_symmetry=output_box.xray_structure_box.crystal_symmetry()
          ).replace_sites_cart(new_sites = sites_cart)
    ph_output_box_output_location.adopt_xray_structure(xrs_offset)

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

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

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

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

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

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

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

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

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

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

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

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

    # Write ccp4 map.
    if("ccp4" in params.output_format):
     if(params.output_file_name_prefix is None):
       file_name = "%s_box.ccp4"%output_prefix
     else: file_name = "%s.ccp4"%params.output_file_name_prefix
     output_box.write_ccp4_map(file_name=file_name,
       output_crystal_symmetry=output_crystal_symmetry,
       output_unit_cell_grid=output_unit_cell_grid,
       shift_back=shift_back)
     print >> log, "Writing boxed map "+\
          "to CCP4 formatted file:   %s"%file_name

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

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

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

  print >> log
  return box
Ejemplo n.º 15
0
def run(args, log = sys.stdout, as_gui_program=False):
  if(len(args)==0):
    parsed = defaults(log=log)
    parsed.show(prefix="  ", out=log)
    return
  command_line = (option_parser()
                  .enable_symmetry_comprehensive()
                  .option("-q", "--quiet",
                          action="store_true",
                          default=False,
                          help="suppress output")
                  .option("--output_plots",
                          action="store_true",
                          default=False)
                  ).process(args=args)
  parsed = defaults(log=log)
  processed_args = mmtbx.utils.process_command_line_args(
    args=command_line.args,
    cmd_cs=command_line.symmetry,
    master_params=parsed,
    log=log,
    suppress_symmetry_related_errors=True)
  processed_args.params.show(out=log)
  params = processed_args.params.extract().density_modification
  output_plots = command_line.options.output_plots

  crystal_symmetry = crystal.symmetry(
    unit_cell=params.input.unit_cell,
    space_group_info=params.input.space_group)
  reflection_files = {}
  for rfn in (params.input.reflection_data.file_name,
              params.input.experimental_phases.file_name,
              params.input.map_coefficients.file_name):
    if os.path.isfile(str(rfn)) and rfn not in reflection_files:
      reflection_files.setdefault(
        rfn, iotbx.reflection_file_reader.any_reflection_file(
          file_name=rfn, ensure_read_access=False))
  server = iotbx.reflection_file_utils.reflection_file_server(
    crystal_symmetry=crystal_symmetry,
    reflection_files=reflection_files.values())
  fo = mmtbx.utils.determine_data_and_flags(
    server,
    parameters=params.input.reflection_data,
    extract_r_free_flags=False,log=log).f_obs
  hl_coeffs = mmtbx.utils.determine_experimental_phases(
    server,
    params.input.experimental_phases,
    log=log,
    parameter_scope="",
    working_point_group=None,
    symmetry_safety_check=True,
    ignore_all_zeros=True)
  if params.input.map_coefficients.file_name is not None:
    map_coeffs = server.get_phases_deg(
      file_name=params.input.map_coefficients.file_name,
      labels=params.input.map_coefficients.labels,
      convert_to_phases_if_necessary=False,
      original_phase_units=None,
      parameter_scope="",
      parameter_name="labels").map_to_asu()
  else:
    map_coeffs = None
  ncs_object = None
  if params.input.ncs_file_name is not None:
    ncs_object = ncs.ncs()
    ncs_object.read_ncs(params.input.ncs_file_name)
    ncs_object.display_all(log=log)

  fo = fo.map_to_asu()
  hl_coeffs = hl_coeffs.map_to_asu()

  fo = fo.eliminate_sys_absent().average_bijvoet_mates()
  hl_coeffs = hl_coeffs.eliminate_sys_absent().average_bijvoet_mates()

  model_map = None
  model_map_coeffs = None
  if len(processed_args.pdb_file_names):
    pdb_file = mmtbx.utils.pdb_file(
      pdb_file_names=processed_args.pdb_file_names)
    xs = pdb_file.pdb_inp.xray_structure_simple()
    fo_, hl_ = fo, hl_coeffs
    if params.change_basis_to_niggli_cell:
      change_of_basis_op = xs.change_of_basis_op_to_niggli_cell()
      xs = xs.change_basis(change_of_basis_op)
      fo_ = fo_.change_basis(change_of_basis_op).map_to_asu()
      hl_ = hl_.change_basis(change_of_basis_op).map_to_asu()
    #fo_, hl_ = fo_.common_sets(hl_)
    fmodel_refined = mmtbx.utils.fmodel_simple(
      f_obs=fo_,
      scattering_table="wk1995",#XXX pva: 1) neutrons? 2) move up as a parameter.
      xray_structures=[xs],
      bulk_solvent_correction=True,
      anisotropic_scaling=True,
      r_free_flags=fo_.array(data=flex.bool(fo_.size(), False)))
    fmodel_refined.update(abcd=hl_)

    master_phil = mmtbx.maps.map_and_map_coeff_master_params()
    map_params = master_phil.fetch(iotbx.phil.parse("""\
map_coefficients {
  map_type = 2mFo-DFc
  isotropize = True
}
""")).extract().map_coefficients[0]
    model_map_coeffs = mmtbx.maps.map_coefficients_from_fmodel(
      fmodel=fmodel_refined, params=map_params)
    model_map = model_map_coeffs.fft_map(
      resolution_factor=params.grid_resolution_factor).real_map_unpadded()

  import time

  t0 = time.time()
  dm = density_modify(
    params,
    fo,
    hl_coeffs,
    ncs_object=ncs_object,
    map_coeffs=map_coeffs,
    model_map_coeffs=model_map_coeffs,
    log=log,
    as_gui_program=as_gui_program)
  time_dm = time.time()-t0
  print >> log, "Time taken for density modification: %.2fs" %time_dm
  # run cns
  if 0:
    from cctbx.development import cns_density_modification
    cns_result = cns_density_modification.run(params, fo, hl_coeffs)
    print cns_result.modified_map.all()
    print dm.map.all()
    dm_map_coeffs = dm.map_coeffs_in_original_setting
    from cctbx import maptbx, miller
    crystal_gridding = maptbx.crystal_gridding(
      dm_map_coeffs.unit_cell(),
      space_group_info=dm_map_coeffs.space_group().info(),
      pre_determined_n_real=cns_result.modified_map.all())
    dm_map = miller.fft_map(crystal_gridding, dm_map_coeffs).apply_sigma_scaling()
    corr = flex.linear_correlation(cns_result.modified_map.as_1d(), dm_map.real_map_unpadded().as_1d())
    print "CNS dm/mmtbx dm correlation:"
    corr.show_summary()
    if dm.model_map_coeffs is not None:
      model_map = miller.fft_map(
        crystal_gridding,
        dm.miller_array_in_original_setting(dm.model_map_coeffs)).apply_sigma_scaling()
      corr = flex.linear_correlation(cns_result.modified_map.as_1d(), model_map.real_map_unpadded().as_1d())
      print "CNS dm/model correlation:"
      corr.show_summary()

  if output_plots:
    plots_to_make = (
      "fom", "skewness",
      "r1_factor", "r1_factor_fom", "mean_solvent_density", "mean_protein_density",
      "f000_over_v", "k_flip", "rms_solvent_density", "rms_protein_density",
      "standard_deviation_local_rms", "mean_delta_phi", "mean_delta_phi_initial",
      )
    from matplotlib.backends.backend_pdf import PdfPages
    from libtbx import pyplot

    stats = dm.get_stats()
    pdf = PdfPages("density_modification.pdf")

    if len(dm.correlation_coeffs) > 1:
      if 0:
        start_coeffs, model_coeffs = dm.map_coeffs_start.common_sets(model_map_coeffs)
        model_phases = model_coeffs.phases(deg=True).data()
        exptl_phases = nearest_phase(
          model_phases, start_coeffs.phases(deg=True).data(), deg=True)
        corr = flex.linear_correlation(exptl_phases, model_phases)
        corr.show_summary()
        fig = pyplot.figure()
        ax = fig.add_subplot(1,1,1)
        ax.set_title("phases start")
        ax.set_xlabel("Experimental phases")
        ax.set_ylabel("Phases from refined model")
        ax.scatter(exptl_phases,
                   model_phases,
                   marker="x", s=10)
        pdf.savefig(fig)
        #
        dm_coeffs, model_coeffs = dm.map_coeffs.common_sets(model_map_coeffs)
        model_phases = model_coeffs.phases(deg=True).data()
        dm_phases = nearest_phase(
          model_phases, dm_coeffs.phases(deg=True).data(), deg=True)
        corr = flex.linear_correlation(dm_phases, model_phases)
        corr.show_summary()
        fig = pyplot.figure()
        ax = fig.add_subplot(1,1,1)
        ax.set_title("phases dm")
        ax.set_xlabel("Phases from density modification")
        ax.set_ylabel("Phases from refined model")
        ax.scatter(dm_phases,
                   model_phases,
                   marker="x", s=10)
        pdf.savefig(fig)
      #
      data = dm.correlation_coeffs
      fig = pyplot.figure()
      ax = fig.add_subplot(1,1,1)
      ax.set_title("correlation coefficient")
      ax.plot(range(1, dm.i_cycle+2), data)
      pdf.savefig(fig)
      #
      data = dm.mean_phase_errors
      fig = pyplot.figure()
      ax = fig.add_subplot(1,1,1)
      ax.set_title("Mean effective phase errors")
      ax.plot(range(1, dm.i_cycle+2), data)
      pdf.savefig(fig)

    for plot in plots_to_make:
      data = [getattr(stats.get_cycle_stats(i), plot) for i in range(1, dm.i_cycle+2)]
      fig = pyplot.figure()
      ax = fig.add_subplot(1,1,1)
      ax.set_title(plot.replace("_", " "))
      ax.plot(range(1, dm.i_cycle+2), data)
      pdf.savefig(fig)

    data = [stats.get_cycle_stats(i).rms_solvent_density/
            stats.get_cycle_stats(i).rms_protein_density
            for i in range(1, dm.i_cycle+2)]
    fig = pyplot.figure()
    ax = fig.add_subplot(1,1,1)
    ax.set_title("RMS solvent/protein density ratio")
    ax.plot(range(1, dm.i_cycle+2), data)
    pdf.savefig(fig)

    pdf.close()

  dm_map_coeffs = dm.map_coeffs_in_original_setting
  dm_hl_coeffs = dm.hl_coeffs_in_original_setting

  # output map if requested
  map_params = params.output.map
  if map_params.file_name is not None:
    fft_map = dm_map_coeffs.fft_map(resolution_factor=params.grid_resolution_factor)
    if map_params.scale == "sigma":
      fft_map.apply_sigma_scaling()
    else:
      fft_map.apply_volume_scaling()
    gridding_first = gridding_last = None
    title_lines = []
    if map_params.format == "xplor":
      fft_map.as_xplor_map(
        file_name      = map_params.file_name,
        title_lines    = title_lines,
        gridding_first = gridding_first,
        gridding_last  = gridding_last)
    else :
      fft_map.as_ccp4_map(
        file_name      = map_params.file_name,
        gridding_first = gridding_first,
        gridding_last  = gridding_last,
        labels=title_lines)

  # output map coefficients if requested
  mtz_params = params.output.mtz

  # Decide if we are going to actually write the mtz
  if mtz_params.file_name is not None:
    orig_fom,final_fom=dm.start_and_end_fom()
    if mtz_params.skip_output_if_worse and final_fom < orig_fom:
      ok_to_write_mtz=False
      print "Not writing out mtz. Final FOM (%7.3f) worse than start (%7.3f)" %(
        final_fom,orig_fom)
    else:  # usual
      ok_to_write_mtz=True
  else:
      ok_to_write_mtz=True

  if mtz_params.file_name is not None and ok_to_write_mtz:
    label_decorator=iotbx.mtz.ccp4_label_decorator()
    fo = dm.miller_array_in_original_setting(dm.f_obs_complete).common_set(dm_map_coeffs)
    mtz_dataset = fo.as_mtz_dataset(
      column_root_label="F",
      label_decorator=label_decorator)
    mtz_dataset.add_miller_array(
      dm_map_coeffs,
      column_root_label="FWT",
      label_decorator=label_decorator)
    phase_source = dm.miller_array_in_original_setting(dm.phase_source).common_set(dm_map_coeffs)
    mtz_dataset.add_miller_array(
      phase_source.array(data=flex.abs(phase_source.data())),
      column_root_label="FOM",
      column_types='W',
      label_decorator=label_decorator)
    mtz_dataset.add_miller_array(
      phase_source.array(data=phase_source.phases(deg=True).data()),
      column_root_label="PHIB",
      column_types='P',
      label_decorator=None)
    if mtz_params.output_hendrickson_lattman_coefficients:
      mtz_dataset.add_miller_array(
        dm_hl_coeffs,
        column_root_label="HL",
        label_decorator=label_decorator)
    mtz_dataset.mtz_object().write(mtz_params.file_name)

  return result(
    map_file=map_params.file_name,
    mtz_file=mtz_params.file_name,
    stats=dm.get_stats())
Ejemplo n.º 16
0
def run(args, crystal_symmetry=None, log=None):
  h = "phenix.map_box: extract box with model and map around selected atoms"
  if(log is None): log = sys.stdout
  print_statistics.make_header(h, out=log)
  default_message="""\

%s.

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

or

  phenix.map_box map.ccp4 density_select=True

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

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

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

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

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

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

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

  if params.ncs_file:

    if(params.output_file_name_prefix is None):
      output_ncs_file = "%s_box.ncs_spec"%output_prefix
    else: output_ncs_file = "%s.ncs_spec"%params.output_file_name_prefix
    print >>log,"\nOffsetting NCS in %s and writing to %s" %(
       params.ncs_file,output_ncs_file)
    from mmtbx.ncs.ncs import ncs
    ncs_object=ncs()
    ncs_object.read_ncs(params.ncs_file,log=log)
    ncs_object.display_all(log=log)
    if not ncs_object or ncs_object.max_operators()<1:
      print >>log,"Skipping...no NCS available"
    elif box.total_shift_cart:
      from scitbx.math import  matrix
      print >>log,"Shifting NCS operators "+\
        "based on coordinate shift of (%7.1f,%7.1f,%7.1f)" %(
        tuple(box.total_shift_cart))
      ncs_object=ncs_object.coordinate_offset(
       coordinate_offset=matrix.col(box.total_shift_cart))
      ncs_object.display_all(log=log)
    ncs_object.format_all_for_group_specification(
       file_name=output_ncs_file)
    box.ncs_object=ncs_object
  else:
    box.ncs_object=None
  print >> log
  return box
Ejemplo n.º 17
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
Ejemplo n.º 18
0
    orth=unit_cell.orthogonalize(frac)
    trans_orth=-1.*ncs_rota_matr*orth
    return ncs_rota_matr,trans_orth
#####################################################



if __name__=="__main__":
  log=sys.stdout
  args=sys.argv[1:]
  if 'exercise' in args:
    file_name='TEST.NCS'
    f=open(file_name,'w')
    f.write(test_ncs_info)
    f.close()
    ncs_object=ncs()
    ncs_object.read_ncs(file_name,source_info=file_name)
    ncs_object.display_all()
    file2='TEST2.NCS'
    text=ncs_object.format_all_for_group_specification(file_name=file2)

    if not text or text != test_ncs_info:
     print "NOT OK ...please compare TEST.NCS (std) vs TEST2.NCS (output)"
     ff=open('txt.dat','w')
     ff.write(text)
     ff.close()
    else:
     print "OK"
  elif len(args)>0 and args[0] and os.path.isfile(args[0]):
    ncs_object=ncs()
    ncs_object.read_ncs(args[0],source_info=args[0])