Beispiel #1
0
def get_random_structure_and_map(
    use_static_structure=False,
    random_seed=171413,
):

    if use_static_structure:
        mmm = map_model_manager()
        mmm.generate_map()
        return group_args(model=mmm.model(), mm=mmm.map_manager())
    import random
    random.seed(random_seed)
    i = random.randint(1, 714717)
    flex.set_random_seed(i)

    xrs = random_structure.xray_structure(
        space_group_info=space_group_info(19),
        volume_per_atom=25.,
        elements=('C', 'N', 'O', 'H') * 10,
        min_distance=1.5)
    fc = xrs.structure_factors(d_min=2).f_calc()
    fft_map = fc.fft_map(resolution_factor=0.25)
    fft_map.apply_volume_scaling()
    ph = iotbx.pdb.input(source_info=None,
                         lines=xrs.as_pdb_file()).construct_hierarchy()
    ph.atoms().set_xyz(xrs.sites_cart())
    map_data = fft_map.real_map_unpadded()
    mm = map_manager(unit_cell_grid=map_data.accessor().all(),
                     unit_cell_crystal_symmetry=fc.crystal_symmetry(),
                     origin_shift_grid_units=(0, 0, 0),
                     map_data=map_data)
    model = mmtbx.model.manager(model_input=None,
                                pdb_hierarchy=ph,
                                crystal_symmetry=fc.crystal_symmetry())
    return group_args(model=model, mm=mm)
Beispiel #2
0
def get_map_manager(
    map_data,
    wrapping,
    unit_cell_dimensions=None,
    crystal_symmetry=None,
):
    '''
      Get a minimal map_manager in SG p1 from any map_data and
      if unit_cell_dimensions.  Assume cell angles are 90,90,90
      Shift origin to (0,0,0)
  '''
    assert unit_cell_dimensions or crystal_symmetry
    assert isinstance(wrapping, bool)
    from iotbx.map_manager import map_manager
    map_data = map_data.shift_origin()

    if not crystal_symmetry:
        from cctbx import crystal
        crystal_symmetry = crystal.symmetry(
            tuple(list(unit_cell_dimensions[:3]) + [90, 90, 90]), 1)
    mm = map_manager(map_data=map_data,
                     unit_cell_grid=map_data.all(),
                     unit_cell_crystal_symmetry=crystal_symmetry,
                     origin_shift_grid_units=(0, 0, 0),
                     wrapping=wrapping)
    return mm
Beispiel #3
0
 def read_map(self, file_name=None, log=sys.stdout):
     # Read in a map and make sure its symmetry is similar to others
     mm = map_manager(file_name)
     self.add_map_manager(mm, log=log)
Beispiel #4
0
def test_01():

  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_map.ccp4')

  dm = DataManager(['miller_array','real_map', 'phil'])
  dm.set_overwrite(True)
  dm.process_real_map_file(data_ccp4)

  # test writing and reading file
  mm = dm.get_real_map()
  mm.shift_origin()
  mm.show_summary()
  dm.write_map_with_map_manager(mm, filename='test_map_manager.ccp4', overwrite=True)

  # get map_data
  map_data=mm.map_data()
  assert approx_equal(map_data[15,10,19], 0.38,eps=0.01)

  # get crystal_symmetry
  cs=mm.crystal_symmetry()
  assert approx_equal(cs.unit_cell().parameters()[0] ,22.41,eps=0.01)

  # and full cell symmetry
  full_cs=mm.unit_cell_crystal_symmetry()
  assert approx_equal(full_cs.unit_cell().parameters()[0] ,149.4066,eps=0.01)

  # write map directly:
  mm.write_map('test_direct.ccp4')

  # read back directly
  new_mm=map_manager('test_direct.ccp4')
  assert (not new_mm.is_similar(mm))

  new_mm.shift_origin()
  assert mm.is_similar(new_mm)

  # deep_copy
  new_mm=mm.deep_copy()
  assert new_mm.is_similar(mm)

  # customized_copy
  new_mm=mm.customized_copy(map_data=mm.map_data().deep_copy())
  assert new_mm.is_similar(mm)


  # Initialize with parameters
  mm_para=map_manager(
     unit_cell_grid= mm.unit_cell_grid,
     unit_cell_crystal_symmetry= mm.unit_cell_crystal_symmetry(),
     origin_shift_grid_units= mm.origin_shift_grid_units,
     map_data=mm.map_data())
  assert mm_para.is_similar(mm)

  # Adjust origin and gridding:
  mm_read=map_manager(data_ccp4)
  mm_read.set_origin_and_gridding((10,10,10),gridding=(100,100,100))
  assert (not mm_read.is_similar(mm))
  assert (not mm_read.already_shifted())

  # Adjust origin and gridding should fail if origin already shifted:
  mm_read=map_manager(data_ccp4)
  mm_read.shift_origin()
  mm_read.set_origin_and_gridding((10,10,10),gridding=(100,100,100))
  assert (mm_read.is_similar(mm))  # not shifted as it failed
  assert (mm_read.already_shifted())

  # Set input_file name
  mm_read.set_input_file_name('test input_file')
  assert mm_read.input_file_name=='test input_file'

  # Set program name
  mm_read.set_program_name('test program')
  assert mm_read.program_name=='test program'

  # Set limitation
  mm_read.add_limitation('map_is_sharpened')
  assert mm_read.limitations==['map_is_sharpened']

  # Add a label
  mm_read.add_label('TEST LABEL')
  assert mm_read.labels[0]=='TEST LABEL'
  mm_read.write_map('map_with_labels.mrc')
  new_mm=map_manager('map_with_labels.mrc')
  assert 'TEST LABEL' in new_mm.labels
  assert new_mm.is_in_limitations('map_is_sharpened')
  assert new_mm.labels[0].find('test program')>-1

  # Read a map directly
  mm_read=map_manager(data_ccp4)
  mm_read.shift_origin()
  assert mm_read.is_similar(mm)

  # Set log
  import sys
  mm.set_log(sys.stdout)

  # Add map_data
  mm_read.replace_map_data(map_data=mm.map_data().deep_copy())
  assert mm_read.is_similar(mm)



  dm.process_real_map_file('test_map_manager.ccp4')
  new_mm=dm.get_real_map('test_map_manager.ccp4')
  new_mm.show_summary()
  assert (not new_mm.is_similar(mm))
  new_mm.shift_origin()
  new_mm.show_summary()
  assert new_mm.is_similar(mm)
  os.remove('test_map_manager.ccp4')

  # Convert to map coeffs, write out, read back, convert back to map

  map_coeffs = mm.map_as_fourier_coefficients(high_resolution = 3)
  mtz_dataset = map_coeffs.as_mtz_dataset(column_root_label='F')
  mtz_object=mtz_dataset.mtz_object()
  dm.write_miller_array_file(mtz_object, filename="map_coeffs.mtz")
  # Note these Fourier coeffs correspond to working map (not original position)

  array_labels=dm.get_miller_array_labels("map_coeffs.mtz")
  labels=array_labels[0]
  dm.get_reflection_file_server(filenames=["map_coeffs.mtz"],labels=[labels])
  miller_arrays=dm.get_miller_arrays()
  new_map_coeffs=miller_arrays[0]
  map_data_from_map_coeffs=mm.fourier_coefficients_as_map(
      map_coeffs=new_map_coeffs)

  mm_from_map_coeffs=mm.customized_copy(map_data=map_data_from_map_coeffs)
  assert mm_from_map_coeffs.is_similar(mm)
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])
Beispiel #7
0
 def _try_as_ccp4_map(self):
     from iotbx.map_manager import map_manager
     from libtbx.utils import null_out
     map_object = map_manager(file_name=str(self.file_name), log=null_out())
     self._file_type = "ccp4_map"
     self._file_object = map_object
Beispiel #8
0
def test_01():

    # Source data (map and model)

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

    # Read in map data with data_manager
    dm = DataManager(['real_map'])
    dm.set_overwrite(True)

    # Next step uses map_manager to do the actual reading
    dm.process_real_map_file(data_ccp4)
    mm = dm.get_real_map()

    # Shift the origin of the map; starts at (100,100,100)
    print(mm.map_data().origin())
    assert mm.map_data().origin() == (100, 100, 100)
    assert mm.origin_shift_grid_units == (0, 0, 0)
    mm.shift_origin()
    assert mm.map_data().origin() == (0, 0, 0)
    assert mm.origin_shift_grid_units == (100, 100, 100)
    mm.show_summary()

    # test cc_to_other_map
    assert mm.cc_to_other_map_manager(mm) == 1

    # test writing and reading file
    dm.write_real_map_file(mm,
                           filename='test_map_manager.ccp4',
                           overwrite=True)
    dm.process_real_map_file('test_map_manager.ccp4')
    new_mm = dm.get_real_map('test_map_manager.ccp4')
    os.remove('test_map_manager.ccp4')
    new_mm.shift_origin()
    # Check whether gridding and crystal_symmetry are similar for mm, new_mm
    assert new_mm.is_similar(mm)
    assert approx_equal(new_mm.map_data()[3125], mm.map_data()[3125])

    # test writing and reading file without shifting origin
    dm = DataManager(['real_map'])
    dm.set_overwrite(True)
    dm.process_real_map_file(data_ccp4)
    mm = dm.get_real_map()
    mm.show_summary()
    dm.write_real_map_file(mm,
                           filename='test_map_manager.ccp4',
                           overwrite=True)
    new_mm = map_manager('test_map_manager.ccp4')
    assert (new_mm.is_similar(mm))
    new_mm.shift_origin()
    assert (not new_mm.is_similar(mm))

    # get map_data
    dm = DataManager(['real_map'])
    dm.set_overwrite(True)
    dm.process_real_map_file(data_ccp4)
    mm = dm.get_real_map()
    mm.shift_origin()
    map_data = mm.map_data()
    assert approx_equal(map_data[15, 10, 19], 0.38, eps=0.01)

    # get crystal_symmetry
    cs = mm.crystal_symmetry()
    assert approx_equal(cs.unit_cell().parameters()[0], 22.41, eps=0.01)

    # and full cell symmetry
    full_cs = mm.unit_cell_crystal_symmetry()
    assert approx_equal(full_cs.unit_cell().parameters()[0],
                        149.4066,
                        eps=0.01)

    # write map directly:
    mm.write_map('test_direct.ccp4')

    # read back directly
    new_mm = map_manager('test_direct.ccp4')
    assert (not new_mm.is_similar(mm))

    new_mm.shift_origin()
    assert mm.is_similar(new_mm)
    assert approx_equal(new_mm.map_data()[3125], mm.map_data()[3125])

    # deep_copy
    new_mm = mm.deep_copy()
    assert new_mm.is_similar(mm)
    assert approx_equal(new_mm.map_data()[3125], mm.map_data()[3125])

    # deep_copy a map without shifting origin
    # Make a DataManager that can write a map coeffs file too
    dm = DataManager(['miller_array', 'real_map'])
    dm.set_overwrite(True)
    dm.process_real_map_file(data_ccp4)
    omm = dm.get_real_map()
    omm.show_summary()
    new_omm = omm.deep_copy()
    assert new_omm.is_similar(omm)
    assert (not new_omm.is_similar(mm))

    # customized_copy
    new_mm = mm.customized_copy(map_data=mm.map_data().deep_copy())
    assert new_mm.is_similar(mm)

    # Initialize with parameters
    mm_para = map_manager(
        unit_cell_grid=mm.unit_cell_grid,
        unit_cell_crystal_symmetry=mm.unit_cell_crystal_symmetry(),
        origin_shift_grid_units=mm.origin_shift_grid_units,
        map_data=mm.map_data(),
        wrapping=False)
    assert mm_para.is_similar(mm)

    # Adjust origin and gridding:
    mm_read = map_manager(data_ccp4)
    mm_read.shift_origin()
    mm.show_summary()
    mm_read.show_summary()
    mm_read.set_original_origin_and_gridding((10, 10, 10),
                                             gridding=(100, 100, 100))
    mm_read.show_summary()
    assert (not mm_read.is_similar(mm))
    assert (mm_read.origin_is_zero())

    # Set program name
    mm_read.set_program_name('test program')
    assert mm_read.program_name == 'test program'

    # Set limitation
    mm_read.add_limitation('map_is_sharpened')
    assert mm_read.limitations == ['map_is_sharpened']

    # Add a label
    mm_read.add_label('TEST LABEL')
    assert mm_read.labels[0] == 'TEST LABEL'
    mm_read.write_map('map_with_labels.mrc')
    new_mm = map_manager('map_with_labels.mrc')
    assert 'TEST LABEL' in new_mm.labels
    assert new_mm.is_in_limitations('map_is_sharpened')
    assert new_mm.labels[0].find('test program') > -1

    # change the cell dimensions
    mm_read = map_manager(data_ccp4)
    mm_read.shift_origin()
    assert mm_read.is_similar(mm)
    assert approx_equal(mm_read.pixel_sizes(), (0.7470, 0.7231, 0.7374),
                        eps=0.001)
    from cctbx import crystal
    new_uc_params = list(
        mm_read.unit_cell_crystal_symmetry().unit_cell().parameters())
    new_uc_params[0] += 10
    new_cs = crystal.symmetry(new_uc_params, 1)
    mm_read.set_unit_cell_crystal_symmetry(new_cs)
    assert not mm_read.crystal_symmetry().is_similar_symmetry(
        mm.crystal_symmetry())
    assert not mm_read.is_similar(mm)
    mm_read.show_summary()
    assert approx_equal(mm_read.pixel_sizes(), (0.7970, 0.7231, 0.7374),
                        eps=0.001)

    # Read a map directly
    mm_read = map_manager(data_ccp4)
    mm_read.shift_origin()
    assert mm_read.is_similar(mm)

    # Set log
    import sys
    mm.set_log(sys.stdout)

    # Add map_data
    new_mm = mm_read.customized_copy(map_data=mm.map_data().deep_copy())
    assert new_mm.is_similar(mm)

    # replace data
    new_mm.set_map_data(map_data=mm.map_data().deep_copy())
    assert new_mm.is_similar(mm)

    # create a full-sized map from this one
    mm_full_size = mm_read.deep_copy().as_full_size_map()
    assert not mm_full_size.is_similar(mm_read)
    print(mm_full_size.map_data().origin(), mm_read.map_data().origin())
    print(mm_full_size.map_data().all(), mm_read.map_data().all())

    # Apply a mask to edges of a map
    assert approx_equal(new_mm.map_data().as_1d().min_max_mean().max,
                        mm.map_data().as_1d().min_max_mean().max)
    assert approx_equal((new_mm.map_data()[0], mm.map_data()[0]), (0.0, 0.0))
    new_mm.create_mask_around_edges(soft_mask_radius=3)
    new_mm.soft_mask(soft_mask_radius=3)
    assert approx_equal(new_mm.map_data().as_1d().min_max_mean().max,
                        mm.map_data().as_1d().min_max_mean().max)
    new_mm.apply_mask(set_outside_to_mean_inside=True)
    assert approx_equal((new_mm.map_data()[0], mm.map_data()[0]),
                        (0.0116267086024, 0.0))

    dm.process_real_map_file('test_map_manager.ccp4')
    new_mm = dm.get_real_map('test_map_manager.ccp4')
    new_mm.show_summary()
    assert (not new_mm.is_similar(mm))
    new_mm.shift_origin()
    new_mm.show_summary()
    assert new_mm.is_similar(mm)
    os.remove('test_map_manager.ccp4')

    # Check origin_shifts
    print(new_mm.origin_shift_grid_units)
    print(new_mm.shift_cart())
    assert approx_equal(new_mm.origin_shift_grid_units, (100, 100, 100))
    assert approx_equal(
        new_mm.shift_cart(),
        (-74.70333099365234, -72.30750274658205, -73.7437515258789))

    # Convert to map coeffs, write out, read back, convert back to map

    map_coeffs = mm.map_as_fourier_coefficients(d_min=3)
    mtz_dataset = map_coeffs.as_mtz_dataset(column_root_label='F')
    mtz_object = mtz_dataset.mtz_object()
    dm.write_miller_array_file(mtz_object, filename="map_coeffs.mtz")
    # Note these Fourier coeffs correspond to working map (not original position)

    array_labels = dm.get_miller_array_labels("map_coeffs.mtz")
    labels = array_labels[0]
    dm.get_reflection_file_server(filenames=["map_coeffs.mtz"],
                                  labels=[labels])
    miller_arrays = dm.get_miller_arrays()
    new_map_coeffs = miller_arrays[0]
    mm_from_map_coeffs = mm.fourier_coefficients_as_map_manager(
        map_coeffs=new_map_coeffs)

    assert mm_from_map_coeffs.is_similar(mm)

    # Find map symmetry in a map
    data_d7 = os.path.join(data_dir, 'data', 'D7.ccp4')
    dm = DataManager(['real_map', 'model'])
    dm.process_real_map_file(data_d7)
    dm.process_model_file(data_pdb)
    mm = dm.get_real_map(data_d7)
    model = dm.get_model(data_pdb)
    mm.shift_origin()
    mm.set_original_origin_and_gridding(original_origin=(0, 0, 0))

    # Box it so it is not so easy to find symmetry
    from cctbx.maptbx.box import with_bounds
    box = with_bounds(mm, lower_bounds=(2, 2, 2), upper_bounds=(43, 43, 43))
    new_mm = box.map_manager()
    new_mm.find_map_symmetry(symmetry='d7',
                             min_ncs_cc=0.8,
                             include_helical_symmetry=False)
    ncs_obj = new_mm.ncs_object()
    assert ncs_obj is not None
    print("NCS: ", new_mm.ncs_object().as_ncs_spec_string())
    another_mm = map_manager(
        unit_cell_grid=new_mm.unit_cell_grid,
        unit_cell_crystal_symmetry=new_mm.unit_cell_crystal_symmetry(),
        origin_shift_grid_units=new_mm.origin_shift_grid_units,
        map_data=new_mm.map_data(),
        ncs_object=ncs_obj,
        wrapping=False)
    assert another_mm.is_similar(new_mm)
    assert ncs_obj.is_similar_ncs_object(another_mm.ncs_object())
    assert new_mm.is_similar(another_mm)

    # Adjust model and ncs symmetry to match this map
    assert model.shift_cart() is None
    new_mm.set_model_symmetries_and_shift_cart_to_match_map(model)
    assert approx_equal(
        model.shift_cart(),
        (-0.888888888888889, -0.8888888888888891, -0.888888888888889))

    assert new_mm.is_compatible_ncs_object(ncs_obj)
    ncs_obj.set_shift_cart((0, 0, 0))
    assert not new_mm.is_compatible_ncs_object(ncs_obj)

    new_mm.set_ncs_object_shift_cart_to_match_map(ncs_obj)
    new_mm.set_ncs_object(ncs_obj)
    assert new_mm.is_compatible_ncs_object(new_mm.ncs_object())
    new_mm.show_summary()

    new_mm.shift_origin(desired_origin=(11, 1, 1))
    print(new_mm.shift_cart(), new_mm.ncs_object().shift_cart())
    assert new_mm.is_compatible_ncs_object(new_mm.ncs_object())
    new_mm.shift_origin()
    assert new_mm.is_compatible_ncs_object(new_mm.ncs_object())

    # filter a map
    dm = DataManager()
    mm = dm.get_real_map(data_d7)

    low_pass_filtered = mm.deep_copy()
    low_pass_filtered.resolution_filter(d_min=2.5)

    high_pass_filtered = mm.deep_copy()
    high_pass_filtered.resolution_filter(d_max=2.5)

    gaussian = mm.deep_copy()
    gaussian.gaussian_filter(smoothing_radius=1)

    binary = mm.deep_copy()
    binary.binary_filter(threshold=0.5)

    assert approx_equal(
        (mm.map_data().as_1d()[1073],
         low_pass_filtered.map_data().as_1d()[1073],
         high_pass_filtered.map_data().as_1d()[1073],
         gaussian.map_data().as_1d()[1073], binary.map_data().as_1d()[1073]),
        (0.0171344596893, 0.0227163900537, -0.0072717454565, 0.0149086679298,
         0.0))

    info = mm.get_density_along_line((5, 5, 5), (10, 10, 10))
    assert approx_equal([info.along_density_values[4]] +
                        list(info.along_sites[4]),
                        [-0.562231123447, 8.0, 8.0, 8.0])
    from iotbx.map_model_manager import map_model_manager
    extra_map_manager_id_list = [
        "low_pass_filtered", "high_pass_filtered", "gaussian", "binary"
    ]

    expected_cc = [
        0.999920243317, 0.0129365545729, 0.971491994253, 0.733986499746
    ]
    mam = map_model_manager(
        map_manager=mm,
        extra_map_manager_list=[
            low_pass_filtered, high_pass_filtered, gaussian, binary
        ],
        extra_map_manager_id_list=extra_map_manager_id_list,
    )
    for other_id, cc in zip(extra_map_manager_id_list, expected_cc):
        assert approx_equal(
            cc, mam.map_map_cc(map_id='map_manager', other_map_id=other_id))
Beispiel #9
0
def generate_map(
        output_map_file_name=None,
        map_coeffs=None,  # Required
        d_min=None,
        map_manager=None,  # source of info, not map
        gridding=None,
        wrapping=False,
        resolution_factor=None,
        origin_shift_grid_units=None,
        low_resolution_fourier_noise_fraction=0,
        high_resolution_fourier_noise_fraction=0,
        low_resolution_real_space_noise_fraction=0,
        high_resolution_real_space_noise_fraction=0,
        low_resolution_noise_cutoff=None,
        random_seed=None,
        log=sys.stdout):
    '''
      Generate map from map_coefficients and add noise in Fourier or real space

      This function typically accessed and tested through map_model_manager

      Summary:
      --------

      Calculate a map and optionally add noise to it.  Supply map
      coefficients (miller_array object) and types of noise to add,
      along with optional gridding (nx,ny,nz), and origin_shift_grid_units.
      Optionally create map coefficients from a model and optionally
      generate a model.

      Unique aspect of this noise generation is that it can be specified
      whether the noise is local in real space (every point in a map
      gets a random value before Fourier filtering), or local in Fourier
      space (every Fourier coefficient gets a complex random offset).
      Also the relative contribution of each type of noise vs resolution
      can be controlled.

      Parameters:
      -----------


      output_map_file_name (string, None):  Output map file (MRC/CCP4 format)
      map_coeffs (miller.array object, None) : map coefficients
      d_min(float):      high_resolution limit (A)
      gridding (tuple (nx,ny,nz), None):  Gridding of map (optional)
      origin_shift_grid_units (tuple (ix,iy,iz), None):  Move location of
          origin of resulting map to (ix,iy,iz) before writing out
      low_resolution_fourier_noise_fraction (float, 0): Low-res Fourier noise
      high_resolution_fourier_noise_fraction (float, 0): High-res Fourier noise
      low_resolution_real_space_noise_fraction(float, 0): Low-res
          real-space noise
      high_resolution_real_space_noise_fraction (float, 0): High-res
          real-space noise
      low_resolution_noise_cutoff (float, None):  Low resolution where noise
          starts to be added

  '''

    if random_seed:
        random_seed = int(random_seed)
        import random
        random.seed(random_seed)
        random_seed = random.randint(1, 714717)
        flex.set_random_seed(random_seed)

    if map_manager:
        origin_shift_grid_units = map_manager.origin_shift_grid_units
        gridding = map_manager.map_data().all()
        wrapping = map_manager.wrapping()
    if gridding:
        if type(gridding) in [type(
            (1, 2, 3)), type([1, 2, 3])] and type(gridding[0]) == type(1):
            pass  # already fine
        else:
            new_gridding = []
            for x in str(gridding).replace("(", "").replace(")", "").replace(
                    "[", "").replace("]", "").replace(",", "").split():
                new_gridding.append(int(x))
            gridding = new_gridding
    low_resolution_fourier_noise_fraction = float(
        low_resolution_fourier_noise_fraction)
    high_resolution_fourier_noise_fraction = float(
        high_resolution_fourier_noise_fraction)
    low_resolution_real_space_noise_fraction = float(
        low_resolution_real_space_noise_fraction)
    high_resolution_real_space_noise_fraction = float(
        high_resolution_real_space_noise_fraction)
    if low_resolution_noise_cutoff:
        low_resolution_noise_cutoff = float(low_resolution_noise_cutoff)

    if d_min:
        d_min = float(d_min)
        map_coeffs = map_coeffs.resolution_filter(d_min=d_min)

    # Calculate a map from Fourier coefficients:
    map_data = get_map_from_map_coeffs(
        map_coeffs=map_coeffs,
        crystal_symmetry=map_coeffs.crystal_symmetry(),
        n_real=gridding,
        resolution_factor=resolution_factor,
        apply_sigma_scaling=False)

    # Optionally add noise to this map as an additive noise map
    # Noise can be added in Fourier space (leads to correlated errors
    #    in real space)  or in real space (leads to correlated errors
    #    in Fourier space).
    # Noise is Fourier-weighted as function of resolution.
    # RMS noise to add at low-resolution (Fourier based noise) as fraction of RMS
    #   value in map at low-resolution is: low_resolution_fourier_noise_fraction
    # RMS Fourier high-res noise:is high_resolution_fourier_noise_fraction
    # RMS real-space low-res noise:is low_resolution_real_space_noise_fraction
    # RMS real-space high-res noise:is high_resolution_real_space_noise_fraction
    # Low-resolution where noise begins to be added is low_resolution_noise_cutoff

    if (low_resolution_fourier_noise_fraction
            or high_resolution_fourier_noise_fraction):
        fourier_noise_map = get_fourier_noise_map(
            n_real=map_data.all(),
            map_coeffs=map_coeffs,
            low_resolution_fourier_noise_fraction=
            low_resolution_fourier_noise_fraction,
            high_resolution_fourier_noise_fraction=
            high_resolution_fourier_noise_fraction,
            d_min=d_min,
            low_resolution_noise_cutoff=low_resolution_noise_cutoff,
            log=log)
    else:
        fourier_noise_map = None

    if (low_resolution_real_space_noise_fraction
            or high_resolution_real_space_noise_fraction):
        real_space_noise_map = get_real_space_noise_map(
            map_data=map_data,
            map_coeffs=map_coeffs,
            low_resolution_real_space_noise_fraction=
            low_resolution_real_space_noise_fraction,
            high_resolution_real_space_noise_fraction=
            high_resolution_real_space_noise_fraction,
            d_min=d_min,
            low_resolution_noise_cutoff=low_resolution_noise_cutoff,
            log=log)
    else:
        real_space_noise_map = None

    if fourier_noise_map:
        map_data += fourier_noise_map
    if real_space_noise_map:
        map_data += real_space_noise_map

    if map_manager:
        mm = map_manager.customized_copy(map_data=map_data)
    else:
        # Create a map_manager object directly (unusual use of map_manager)
        from iotbx.map_manager import map_manager
        mm = map_manager(
            map_data=map_data,
            unit_cell_grid=map_data.all(),
            unit_cell_crystal_symmetry=map_coeffs.crystal_symmetry(),
            origin_shift_grid_units=origin_shift_grid_units,
            wrapping=wrapping)

    if output_map_file_name:
        mm.write_map(output_map_file_name)
    else:
        print("Generated map with origin at %s and size of %s" %
              (mm.map_data().origin(), mm.map_data().all()),
              file=log)

    return mm
Beispiel #10
0
def generate_map(map_coeffs=None,
                 high_resolution=3,
                 gridding=None,
                 origin_shift_grid_units=None,
                 low_resolution_fourier_noise_fraction=0,
                 high_resolution_fourier_noise_fraction=0,
                 low_resolution_real_space_noise_fraction=0,
                 high_resolution_real_space_noise_fraction=0,
                 output_map_file_name=None,
                 log=sys.stdout,
                 **pass_through_kw
                 ):  # pass_through_kw picks up all the keywords that are to be
    # passed to other routines
    '''
    generate_map

    Convenience method to calculate a map and
    optionally add noise to it.  Supply map coefficients (miller_array
    object) and types of noise to add, along with optional gridding (nx,ny,nz),
    and origin_shift_grid_units.

    Not implemented:
    Unique aspect of this noise generation is that it can be specified
    whether the noise is local in real space (every point in a map
    gets a random value before Fourier filtering), or local in Fourier
    space (every Fourier coefficient gets a complex random offset).
    Also the relative contribution of each type of noise vs resolution
    can be controlled.

    Full list of keywords that can be supplied. These affect
    generate_model, generate_map_coefficients and generate_map:

      model_file_name=None  # file to read model from
      n_residues=10,  # how many residues to include
      b_iso=30,  # what b_iso to set all the atoms to
      box_buffer=5,  # buffer around atoms
      start_res=None,  # residue to start with
      space_group_number=1,  # space group number for model and map
      output_model_file_name=None,  # file name for model (if any)
      random_seed=None,  # random seed for shake
      shake=None,  # rms offset for each atom if any
      scattering_table='electron',  # scattering table, electron n_gaussian
      gridding=None,  # optional gridding for map
      origin_shift_grid_units=None,  # optional origin for map
      low_resolution_fourier_noise_fraction=0, # fourier noise lowres
      high_resolution_fourier_noise_fraction=0, # hires
      low_resolution_real_space_noise_fraction=0, # real-space noise lowres
      high_resolution_real_space_noise_fraction=0, # real-space noise hires
      output_map_file_name=None,   # optional output map file namd

  '''

    if gridding:
        if type(gridding) == type([1, 2, 3]) and type(gridding[0]) == type(1):
            pass  # already fine
        else:
            gridding = []
            for x in str(gridding).replace(",", "").split():
                gridding.append(int(x))

    if not map_coeffs:  # get map coefficients
        map_coeffs = generate_map_coefficients(high_resolution=high_resolution,
                                               log=log,
                                               **pass_through_kw)

    if high_resolution:
        map_coeffs = map_coeffs.resolution_filter(d_min=high_resolution)

    # Calculate a map from Fourier coefficients:
    from cctbx.maptbx.segment_and_split_map import get_map_from_map_coeffs
    map_data = get_map_from_map_coeffs(
        map_coeffs=map_coeffs,
        crystal_symmetry=map_coeffs.crystal_symmetry(),
        n_real=gridding,
        apply_sigma_scaling=False)

    from iotbx.map_manager import map_manager
    mm = map_manager(map_data=map_data,
                     unit_cell_grid=map_data.all(),
                     unit_cell_parameters=map_coeffs.crystal_symmetry(
                     ).unit_cell().parameters(),
                     space_group_number=map_coeffs.crystal_symmetry().
                     space_group().info().symbol_and_number().split('(')[0],
                     origin_shift_grid_units=origin_shift_grid_units)

    if output_map_file_name:
        mm.write_map(output_map_file_name)
    else:
        print("Generated map with origin at %s and size of %s" %
              (mm.map_data().origin(), mm.map_data().all()),
              file=log)

    return mm
Beispiel #11
0
def test_01():

    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_map.ccp4')

    dm = DataManager(['miller_array', 'real_map', 'phil'])
    dm.set_overwrite(True)
    dm.process_real_map_file(data_ccp4)

    # test writing and reading file
    mm = dm.get_real_map()
    mm.shift_origin()
    mm.show_summary()
    dm.write_real_map_file(mm,
                           filename='test_map_manager.ccp4',
                           overwrite=True)
    os.remove('test_map_manager.ccp4')

    # test writing and reading file without shifting origin
    dm = DataManager(['miller_array', 'real_map', 'phil'])
    dm.set_overwrite(True)
    dm.process_real_map_file(data_ccp4)
    mm = dm.get_real_map()
    mm.show_summary()
    dm.write_real_map_file(mm,
                           filename='test_map_manager.ccp4',
                           overwrite=True)

    new_mm = map_manager('test_map_manager.ccp4')
    assert (new_mm.is_similar(mm))
    new_mm.shift_origin()
    assert (not new_mm.is_similar(mm))

    # get map_data
    mm.shift_origin()
    map_data = mm.map_data()
    assert approx_equal(map_data[15, 10, 19], 0.38, eps=0.01)

    # get crystal_symmetry
    cs = mm.crystal_symmetry()
    assert approx_equal(cs.unit_cell().parameters()[0], 22.41, eps=0.01)

    # and full cell symmetry
    full_cs = mm.unit_cell_crystal_symmetry()
    assert approx_equal(full_cs.unit_cell().parameters()[0],
                        149.4066,
                        eps=0.01)

    # write map directly:
    mm.write_map('test_direct.ccp4')

    # read back directly
    new_mm = map_manager('test_direct.ccp4')
    assert (not new_mm.is_similar(mm))

    new_mm.shift_origin()
    assert mm.is_similar(new_mm)

    # deep_copy
    new_mm = mm.deep_copy()
    assert new_mm.is_similar(mm)

    # deep_copy a map without shifting origin
    dm = DataManager(['miller_array', 'real_map', 'phil'])
    dm.set_overwrite(True)
    dm.process_real_map_file(data_ccp4)
    omm = dm.get_real_map()
    omm.show_summary()
    new_omm = omm.deep_copy()
    assert new_omm.is_similar(omm)
    assert (not new_omm.is_similar(mm))

    # customized_copy
    new_mm = mm.customized_copy(map_data=mm.map_data().deep_copy())
    assert new_mm.is_similar(mm)

    # Initialize with parameters
    mm_para = map_manager(
        unit_cell_grid=mm.unit_cell_grid,
        unit_cell_crystal_symmetry=mm.unit_cell_crystal_symmetry(),
        origin_shift_grid_units=mm.origin_shift_grid_units,
        map_data=mm.map_data())
    assert mm_para.is_similar(mm)

    # Adjust origin and gridding:
    mm_read = map_manager(data_ccp4)
    mm_read.shift_origin()
    mm.show_summary()
    mm_read.show_summary()
    mm_read.set_original_origin_and_gridding((10, 10, 10),
                                             gridding=(100, 100, 100))
    mm_read.show_summary()
    assert (not mm_read.is_similar(mm))
    assert (mm_read.origin_is_zero())

    # Set program name
    mm_read.set_program_name('test program')
    assert mm_read.program_name == 'test program'

    # Set limitation
    mm_read.add_limitation('map_is_sharpened')
    assert mm_read.limitations == ['map_is_sharpened']

    # Add a label
    mm_read.add_label('TEST LABEL')
    assert mm_read.labels[0] == 'TEST LABEL'
    mm_read.write_map('map_with_labels.mrc')
    new_mm = map_manager('map_with_labels.mrc')
    assert 'TEST LABEL' in new_mm.labels
    assert new_mm.is_in_limitations('map_is_sharpened')
    assert new_mm.labels[0].find('test program') > -1

    # Read a map directly
    mm_read = map_manager(data_ccp4)
    mm_read.shift_origin()
    assert mm_read.is_similar(mm)

    # Set log
    import sys
    mm.set_log(sys.stdout)

    # Add map_data
    new_mm = mm_read.customized_copy(map_data=mm.map_data().deep_copy())
    assert new_mm.is_similar(mm)

    # replace data
    new_mm.set_map_data(map_data=mm.map_data().deep_copy())
    assert new_mm.is_similar(mm)

    # Apply a mask to edges of a map
    assert approx_equal(new_mm.map_data().as_1d().min_max_mean().max,
                        mm.map_data().as_1d().min_max_mean().max)
    assert approx_equal((new_mm.map_data()[0], mm.map_data()[0]), (0.0, 0.0))
    new_mm.create_mask_around_edges(soft_mask_radius=3)
    new_mm.soft_mask(soft_mask_radius=3)
    assert approx_equal(new_mm.map_data().as_1d().min_max_mean().max,
                        mm.map_data().as_1d().min_max_mean().max)
    new_mm.apply_mask(set_outside_to_mean_inside=True)
    assert approx_equal((new_mm.map_data()[0], mm.map_data()[0]),
                        (0.0116267086024, 0.0))

    dm.process_real_map_file('test_map_manager.ccp4')
    new_mm = dm.get_real_map('test_map_manager.ccp4')
    new_mm.show_summary()
    assert (not new_mm.is_similar(mm))
    new_mm.shift_origin()
    new_mm.show_summary()
    assert new_mm.is_similar(mm)
    os.remove('test_map_manager.ccp4')

    # Check origin_shifts
    print(new_mm.origin_shift_grid_units)
    print(new_mm.origin_shift_cart())
    assert approx_equal(new_mm.origin_shift_grid_units, (100, 100, 100))
    assert approx_equal(
        new_mm.origin_shift_cart(),
        (74.70333099365234, 72.30750274658205, 73.7437515258789))
    # Convert to map coeffs, write out, read back, convert back to map

    map_coeffs = mm.map_as_fourier_coefficients(high_resolution=3)
    mtz_dataset = map_coeffs.as_mtz_dataset(column_root_label='F')
    mtz_object = mtz_dataset.mtz_object()
    dm.write_miller_array_file(mtz_object, filename="map_coeffs.mtz")
    # Note these Fourier coeffs correspond to working map (not original position)

    array_labels = dm.get_miller_array_labels("map_coeffs.mtz")
    labels = array_labels[0]
    dm.get_reflection_file_server(filenames=["map_coeffs.mtz"],
                                  labels=[labels])
    miller_arrays = dm.get_miller_arrays()
    new_map_coeffs = miller_arrays[0]
    map_data_from_map_coeffs = mm.fourier_coefficients_as_map(
        map_coeffs=new_map_coeffs)

    mm_from_map_coeffs = mm.customized_copy(map_data=map_data_from_map_coeffs)
    assert mm_from_map_coeffs.is_similar(mm)
Beispiel #12
0
  def __init__(self,
      model,
      target_map,
      refine_ncs_operators=False,
      number_of_cycles=1,
      cycles_to_converge=2,
      min_mode='simple_cycles',
      resolution=3.,
      log=None):

    # completely new way of doing this. using RSR macro-cycle
    # for test compatibility:
    print("Minimizing using reference map...", file=log)
    if model.ncs_constraints_present():
      print("  Minimizing... (NCS)", file=log)
    else:
      print("  Minimizing...", file=log)
    from phenix.refinement.macro_cycle_real_space import run as rsr_mc_run
    import scitbx.math
    from phenix.refinement import rsr
    rsr_master_params = rsr.master_params_str
    import iotbx.phil
    rsr_master_params = iotbx.phil.parse(rsr_master_params, process_includes=True)
    import mmtbx.idealized_aa_residues.rotamer_manager
    sin_cos_table = scitbx.math.sin_cos_table(n=10000)
    params = rsr_master_params.extract()
    params.pdb_interpretation = model._pdb_interpretation_params.pdb_interpretation
    params.refinement.run = "minimization_global+local_grid_search"
    params.refine_ncs_operators=False
    params.refinement.macro_cycles = number_of_cycles
    params.resolution = resolution
    rotamer_manager = mmtbx.idealized_aa_residues.rotamer_manager.load(
        rotamers = "favored")
    rigid_body_selections = [] # no RBR here
    from iotbx import map_model_manager
    from iotbx import map_manager
    mm = map_manager.map_manager(
      map_data                   = target_map,
      unit_cell_grid             = target_map.all(),
      wrapping                   = False,
      unit_cell_crystal_symmetry = model.crystal_symmetry())
    mmm = map_model_manager.map_model_manager(
      model       = model,
      map_manager = mm)
    res = rsr_mc_run(
      params                = params,
      map_model_manager     = mmm,
      log                   = log,
      rotamer_manager       = rotamer_manager,
      sin_cos_table         = sin_cos_table,
      rigid_body_selections = rigid_body_selections)

    model.set_sites_cart_from_hierarchy(res.model.get_hierarchy())
    res.structure_monitor.states_collector.write(file_name="rsr_all_states.pdb")
    return





    # end ===================================================
    # Very sophisticated implementation. Need to investigate before removal.

    from mmtbx.refinement.geometry_minimization import add_rotamer_restraints
    from mmtbx.refinement.minimization_monitor import minimization_monitor
    self.model = model
    self.log = log
    print("Minimizing using reference map...", file=self.log)
    self.log.flush()

    # copy-paste from cctbx_project/mmtbx/refinement/geometry_minimization.py:
    # minimize_wrapper_for_ramachandran
    self.model.get_restraints_manager().geometry.pair_proxies(
        sites_cart=self.model.get_sites_cart())

    ncs_restraints_group_list = self.model.get_ncs_groups()
    if ncs_restraints_group_list is None:
      ncs_restraints_group_list = []
    ncs_groups=None
    if len(ncs_restraints_group_list) > 0:
      ncs_groups=ncs_restraints_group_list

    min_monitor = minimization_monitor(
        number_of_cycles=number_of_cycles,
        max_number_of_cycles=20,
        cycles_to_converge=cycles_to_converge,
        mode=min_mode)
    selection_real_space = None
    import mmtbx.refinement.real_space.weight
    self.w = 1
    print("number_of_cycles", number_of_cycles, file=log)
    print("Stats before minimization:", file=log)
    ms = self.model.geometry_statistics()
    ms.show(log=log)

    while min_monitor.need_more_cycles():
      print("Cycle number", min_monitor.get_current_cycle_n(), file=self.log)
      self.model.get_restraints_manager().geometry.\
          update_ramachandran_restraints_phi_psi_targets(
              hierarchy=self.model.get_hierarchy())
      print("  Updating rotamer restraints...", file=self.log)
      add_rotamer_restraints(
        pdb_hierarchy      = self.model.get_hierarchy(),
        restraints_manager = self.model.get_restraints_manager(),
        selection          = None,
        sigma              = 5,
        mode               = "fix_outliers",
        accept_allowed     = False,
        mon_lib_srv        = self.model.get_mon_lib_srv(),
        rotamer_manager    = self.model.get_rotamer_manager())
      self.model.set_sites_cart_from_hierarchy()

      if min_monitor.need_weight_optimization():
        # if self.w is None:
        print("  Determining weight...", file=self.log)
        self.log.flush()
        self.weight = mmtbx.refinement.real_space.weight.run(
            map_data                    = target_map,
            xray_structure              = self.model.get_xray_structure(),
            pdb_hierarchy               = self.model.get_hierarchy(),
            geometry_restraints_manager = self.model.get_restraints_manager(),
            rms_bonds_limit             = 0.015,
            rms_angles_limit            = 1.0,
            ncs_groups                  = ncs_groups)

        # division is to put more weight onto restraints. Checked. Works.
        self.w = self.weight.weight/3.0
        # self.w = self.weight.weight/15.0
        # self.w = 0
        # self.w = self.weight.weight
        for s in self.weight.msg_strings:
          print(s, file=self.log)

      if ncs_restraints_group_list is None or len(ncs_restraints_group_list)==0:
        #No NCS
        print("  Minimizing...", file=self.log)
        print("     with weight %f" % self.w, file=self.log)
        self.log.flush()
        refine_object = simple(
            target_map                  = target_map,
            selection                   = None,
            max_iterations              = 150,
            geometry_restraints_manager = self.model.get_restraints_manager().geometry,
            selection_real_space        = selection_real_space,
            states_accumulator          = None,
            ncs_groups                  = ncs_groups)
        refine_object.refine(weight = self.w, xray_structure = self.model.get_xray_structure())
        self.rmsd_bonds_final, self.rmsd_angles_final = refine_object.rmsds()
        print("RMSDS:", self.rmsd_bonds_final, self.rmsd_angles_final, file=log)
        # print >> log, "sizes:", len(refine_object.sites_cart()), len(self.xrs.scatterers())
        self.model.set_sites_cart(refine_object.sites_cart(), update_grm=True)
        # print >> log, "sizes", self.xrs.scatterers()
      else:
        # Yes NCS
        # copy-paste from macro_cycle_real_space.py
        # !!! Don't rearrange NCS groups here because master was just fixed!
        # import mmtbx.ncs.ncs_utils as nu
        # nu.get_list_of_best_ncs_copy_map_correlation(
        #     ncs_groups     = ncs_restraints_group_list,
        #     xray_structure = self.model.get_xray_structure(),
        #     map_data       = target_map,
        #     d_min          = 3)
        print("  Minimizing... (NCS)", file=self.log)
        tfg_obj = mmtbx.refinement.minimization_ncs_constraints.\
          target_function_and_grads_real_space(
            map_data                   = target_map,
            xray_structure             = self.model.get_xray_structure(),
            ncs_restraints_group_list  = ncs_restraints_group_list,
            refine_selection           = None,
            real_space_gradients_delta = 1,
            restraints_manager         = self.model.get_restraints_manager(),
            data_weight                = self.w,
            refine_sites               = True)
        minimized = mmtbx.refinement.minimization_ncs_constraints.lbfgs(
          target_and_grads_object      = tfg_obj,
          xray_structure               = self.model.get_xray_structure(),
          ncs_restraints_group_list    = ncs_restraints_group_list,
          refine_selection             = None,
          finite_grad_differences_test = False,
          max_iterations               = 100,
          refine_sites                 = True)
        self.model.set_sites_cart(tfg_obj.xray_structure.sites_cart())
      ncs_restraints_group_list.recalculate_ncs_transforms(self.model.get_sites_cart())
      ms = self.model.geometry_statistics()
      min_monitor.save_cycle_results(geometry=ms)
      ms.show(log=log)