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")
Example #2
0
def test_01():

    # Source data

    data_dir = os.path.dirname(os.path.abspath(__file__))
    data_ccp4 = os.path.join(data_dir, 'data', 'non_zero_origin_map.ccp4')
    data_pdb = os.path.join(data_dir, 'data', 'non_zero_origin_model.pdb')
    data_ncs_spec = os.path.join(data_dir, 'data',
                                 'non_zero_origin_ncs_spec.ncs_spec')

    # DataManager

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

    # Read in map and model and ncs

    map_file = data_ccp4
    dm.process_real_map_file(map_file)
    mm = dm.get_real_map(map_file)

    model_file = data_pdb
    dm.process_model_file(model_file)
    model = dm.get_model(model_file)

    ncs_file = data_ncs_spec
    dm.process_ncs_spec_file(ncs_file)
    ncs = dm.get_ncs_spec(ncs_file)

    ncs_dc = ncs.deep_copy()

    mmmn = match_map_model_ncs()
    mmmn.add_map_manager(mm)
    mmmn.add_model(model)
    mmmn.add_ncs_object(ncs)

    # Save it
    mmmn_dc = mmmn.deep_copy()

    # Make sure we can add an ncs object that is either shifted or not
    mmmn_dcdc = mmmn.deep_copy()
    new_mmmn = match_map_model_ncs()
    new_mmmn.add_map_manager(mmmn_dcdc.map_manager())
    new_mmmn.add_model(mmmn_dcdc.model())
    new_mmmn.add_ncs_object(mmmn_dcdc.ncs_object())
    assert new_mmmn.ncs_object().shift_cart() == new_mmmn.map_manager(
    ).shift_cart()

    mmmn_dcdc = mmmn.deep_copy()
    new_mmmn = match_map_model_ncs()
    new_mmmn.add_map_manager(mmmn_dcdc.map_manager())
    new_mmmn.add_model(mmmn_dcdc.model())
    new_mmmn.add_ncs_object(ncs_dc)
    assert new_mmmn.ncs_object().shift_cart() == new_mmmn.map_manager(
    ).shift_cart()

    original_ncs = mmmn.ncs_object()
    assert approx_equal(
        (24.0528, 11.5833, 20.0004),
        tuple(original_ncs.ncs_groups()[0].translations_orth()[-1]),
        eps=0.1)

    assert tuple(mmmn._map_manager.origin_shift_grid_units) == (0, 0, 0)

    # Shift origin to (0,0,0)
    mmmn = mmmn_dc.deep_copy()  # fresh version of match_map_model_ncs
    mmmn.shift_origin()
    new_ncs = mmmn.ncs_object()
    assert tuple(mmmn._map_manager.origin_shift_grid_units) == (100, 100, 100)

    mmmn.write_model('s.pdb')
    mmmn.write_map('s.mrc')

    shifted_ncs = mmmn.ncs_object()
    assert approx_equal(
        (-153.758, -74.044, -127.487),
        tuple(shifted_ncs.ncs_groups()[0].translations_orth()[-1]),
        eps=0.1)

    # Shift a model and shift it back

    mmmn = mmmn_dc.deep_copy()  # fresh version of match_map_model_ncs
    model = mmmn.model()
    shifted_model = mmmn.shift_model_to_match_working_map(model=model)
    model_in_original_position = mmmn.shift_model_to_match_original_map(
        model=shifted_model)
    assert (approx_equal(
        model.get_sites_cart(),  # not a copy
        shifted_model.get_sites_cart()))
    assert approx_equal(model.get_sites_cart(),
                        model_in_original_position.get_sites_cart())

    # test data_manager map_model_manager
    generated_mmm = dm.get_map_model_manager()
    print(generated_mmm)
    assert (isinstance(generated_mmm, map_model_manager))

    # Generate a map and model

    import sys
    mmm = map_model_manager(log=sys.stdout)
    mmm.generate_map()
    model = mmm.model()
    mm = mmm.map_manager()
    assert approx_equal(model.get_sites_cart()[0], (14.476, 10.57, 8.34),
                        eps=0.01)
    assert approx_equal(mm.map_data()[10, 10, 10], -0.0506, eps=0.001)
    # Save it
    mmm_dc = mmm.deep_copy()

    # Create model from sites
    mmm_sites = mmm_dc.deep_copy()
    from scitbx.array_family import flex
    sites_cart = flex.vec3_double()
    sites_cart.append((3, 4, 5))
    mmm_sites.model_from_sites_cart(sites_cart=sites_cart,
                                    model_id='new_model')
    assert mmm_sites.get_model_by_id('new_model').get_sites_cart()[0] == (3, 4,
                                                                          5)

    # Set crystal_symmetry and unit_cell_crystal_symmetry and shift_cart
    # Box and shift the map_model_manager so we have new coordinate system
    mmm_sites.box_all_maps_around_model_and_shift_origin()
    new_model = mmm_sites.get_model_by_id('new_model')
    assert approx_equal(
        (3.747033333333334, 4.723075000000001, 5.0),
        mmm_sites.get_model_by_id('new_model').get_sites_cart()[0])

    # arbitrarily set unit_cell crystal symmetry of model to
    #  match crystal_symmetry. First have to set shift_cart to None
    new_model.set_shift_cart(shift_cart=None)
    new_model.set_unit_cell_crystal_symmetry_and_shift_cart()
    assert new_model.crystal_symmetry() != mmm_sites.crystal_symmetry()

    # now set crystal symmetries and shift cart of model to match the manager
    mmm_sites.set_model_symmetries_and_shift_cart_to_match_map(new_model)
    assert new_model.crystal_symmetry().is_similar_symmetry(
        mmm_sites.crystal_symmetry())
    assert new_model.unit_cell_crystal_symmetry().is_similar_symmetry(
        mmm_sites.unit_cell_crystal_symmetry())
    assert new_model.shift_cart() == mmm_sites.shift_cart()

    # Import hierarchy into a model and set symmetries and shift to match
    mmm_sites.model_from_hierarchy(hierarchy=mmm_sites.model().get_hierarchy(),
                                   model_id='model_from_hierarchy')
    assert mmm_sites.get_model_by_id('model_from_hierarchy').model_as_pdb() \
       == mmm_sites.get_model_by_id('model').model_as_pdb()

    # Check on wrapping
    assert not mm.wrapping(
    )  # this one should not wrap because it is zero at edges

    # Make a new one with no buffer so it is not zero at edges
    mmm = map_model_manager()
    mmm.generate_map(box_cushion=0)
    mm = mmm.map_manager()
    # check its compatibility with wrapping
    assert mm.is_consistent_with_wrapping()
    mmm.show_summary()

    # now box it
    sel = mmm.model().selection("resseq 221:221")
    new_model = mmm.model().deep_copy().select(sel)
    new_mmm = map_model_manager(model=new_model, map_manager=mm.deep_copy())
    new_mmm.box_all_maps_around_model_and_shift_origin()
    new_mm = new_mmm.map_manager()

    assert not new_mm.wrapping()
    assert not new_mm.is_consistent_with_wrapping()

    # now box it with selection
    new_mmm_1 = map_model_manager(model=mmm.model().deep_copy(),
                                  map_manager=mm.deep_copy())
    new_mmm_1.box_all_maps_around_model_and_shift_origin(
        selection_string="resseq 221:221")
    new_mm_1 = new_mmm_1.map_manager()

    assert not new_mm_1.wrapping()
    assert not new_mm_1.is_consistent_with_wrapping()
    assert new_mm_1.map_data().all() == new_mm.map_data().all()

    # create map_model_manager with just half-maps
    mm1 = mm.deep_copy()
    mm2 = mm.deep_copy()
    map_data = mm2.map_data()
    map_data += 1.
    new_mmm = map_model_manager(model=mmm.model().deep_copy(),
                                map_manager_1=mm1,
                                map_manager_2=mm2)
    assert new_mmm._map_dict.get(
        'map_manager') is None  # should not be any yet
    assert approx_equal(new_mmm.map_manager().map_data()[232],
                        mm.deep_copy().map_data()[232] + 0.5)
    assert new_mmm._map_dict.get(
        'map_manager') is not None  # now should be there

    # generate map data from a model
    mm1 = mm.deep_copy()
    mm2 = mm.deep_copy()
    new_mmm = map_model_manager(model=mmm.model().deep_copy(), map_manager=mm1)
    mmm.generate_map(model=mmm.model())
    mm = mmm.map_manager()
    mmm.show_summary()

    # check get_map_model_manager function
    dm = DataManager(['model'])
    assert not hasattr(dm, 'get_map_model_manager')
    dm = DataManager(['real_map'])
    assert not hasattr(dm, 'get_map_model_manager')
    dm = DataManager(['sequence'])
    assert not hasattr(dm, 'get_map_model_manager')
    dm = DataManager(['model', 'real_map'])
    assert hasattr(dm, 'get_map_model_manager')

    # usage
    dm.get_map_model_manager(model_file=data_pdb, map_files=data_ccp4)
    dm.get_map_model_manager(model_file=data_pdb, map_files=[data_ccp4])
    dm.get_map_model_manager(model_file=data_pdb,
                             map_files=[data_ccp4, data_ccp4, data_ccp4])
    dm.get_map_model_manager(model_file=data_pdb,
                             map_files=data_ccp4,
                             ignore_symmetry_conflicts=True)

    # errors
    try:
        dm.get_map_model_manager(model_file=data_pdb,
                                 map_files=data_ccp4,
                                 from_phil=True)
    except Sorry as e:
        assert 'from_phil is set to True' in str(e)
    try:
        dm.get_map_model_manager(model_file=data_pdb,
                                 map_files=data_ccp4,
                                 abc=123)
    except TypeError as e:
        assert 'unexpected keyword argument' in str(e)
    try:
        dm.get_map_model_manager(model_file=data_pdb,
                                 map_files=[data_ccp4, data_ccp4])
    except Sorry as e:
        assert '1 full map and 2 half maps' in str(e)

    # PHIL
    class test_program(ProgramTemplate):
        master_phil_str = '''
include scope iotbx.map_model_manager.map_model_phil_str
'''

    working_phil_str = '''
  map_model {
    full_map = %s
    half_map = %s
    half_map = s.mrc
    model = %s
  }
''' % (data_ccp4, data_ccp4, data_pdb)

    master_phil = parse(test_program.master_phil_str, process_includes=True)
    working_phil = master_phil.fetch(parse(working_phil_str))
    tp = test_program(dm, working_phil.extract())

    try:
        dm.get_map_model_manager(from_phil=True)
    except Exception as e:
        assert 'ignore_symmetry_conflicts' in str(e)
    try:
        dm.get_map_model_manager(from_phil=True,
                                 ignore_symmetry_conflicts=True)
    except AssertionError:
        pass