Example #1
0
def get_map_model_managers():
  # Set up source data
  from iotbx.map_model_manager import map_model_manager
  mmm = map_model_manager()
  mmm.generate_map(wrapping=True)
  second_model=mmm.model().deep_copy()
  mmm.box_all_maps_around_model_and_shift_origin()
  mmm.map_manager().set_wrapping(False)
  mmm.set_log(sys.stdout)

  #now get a second one

  from scitbx import matrix
  r=matrix.sqr((-0.8090,0.5878,0.0000,
   -0.5878,-0.8090,-0.0000,
   0.0000,-0.0000,1.0000,))
  t = matrix.col((100,0,0))
  new_sites_cart = r.elems*mmm.model().get_sites_cart() + t.elems
  second_model.set_sites_cart(new_sites_cart)
  from cctbx.maptbx.box import shift_and_box_model
  second_model = shift_and_box_model(second_model)

  second_mmm = map_model_manager(model=second_model)
  second_mmm.generate_map(model=second_model,wrapping=True)
  second_mmm.box_all_maps_around_model_and_shift_origin(box_cushion=10)
  second_mmm.map_manager().set_wrapping(False)
  second_mmm.set_log(sys.stdout)

  print(mmm.model().get_sites_cart()[0])
  print(second_mmm.model().get_sites_cart()[0])
  return mmm, second_mmm
Example #2
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)
Example #3
0
def read_map_and_model(file_name_1, file_name_2):
    '''
    Identify which file is map and which is model, read in and
    create map_model_manager
  '''

    map_file_name = None
    model_file_name = None
    for f in [file_name_1, file_name_2]:
        for ending in ['.ccp4', '.mrc', '.map']:
            if f.endswith(ending):
                map_file_name = f
        for ending in ['.pdb', '.cif']:
            if f.endswith(ending):
                model_file_name = f
    if not map_file_name or not model_file_name:
        raise Sorry("Unable to identify map and model from %s and %s" %
                    (file_name_1, file_name_2))

    from iotbx.data_manager import DataManager
    from iotbx.map_model_manager import map_model_manager
    dm = DataManager()
    dm.process_real_map_file(map_file_name)
    mm = dm.get_real_map(map_file_name)

    dm.process_model_file(model_file_name)
    model = dm.get_model(model_file_name)
    mam = map_model_manager(model=model, map_manager=mm)
    return mam
def run(args, log=sys.stdout):
  print("-"*79, file=log)
  print(legend, file=log)
  print("-"*79, file=log)
  inputs = mmtbx.utils.process_command_line_args(args = args,
    master_params = master_params(),
    suppress_symmetry_related_errors = True)
  params = inputs.params.extract()
  # model
  broadcast(m="Input PDB:", log=log)
  file_names = inputs.pdb_file_names
  if(len(file_names) != 1): raise Sorry("PDB file has to given.")
  from iotbx.data_manager import DataManager
  dm = DataManager()
  dm.set_overwrite(True)
  model = dm.get_model(file_names[0])

  # map
  broadcast(m="Input map:", log=log)
  if(inputs.ccp4_map is None): raise Sorry("Map file has to given.")

  from iotbx.map_model_manager import map_model_manager
  mam = map_model_manager(model = model, map_manager = inputs.ccp4_map,
     wrapping = params.wrapping,
     ignore_symmetry_conflicts = params.ignore_symmetry_conflicts)

  mam.model().setup_scattering_dictionaries(
     scattering_table=params.scattering_table)
  mam.model().get_xray_structure().show_summary(f=log, prefix="  ")
  inputs.ccp4_map.show_summary(prefix="  ")

  # estimate resolution
  d_min = params.resolution
  if(d_min is None):
    raise Sorry("Map resolution must be given.")
  print("  d_min: %6.4f"%d_min, file=log)
  #
  result_obj = compdiff(
    map_data_obs = mam.map_manager().map_data(), # NOTE this will always wrap map
    xrs          = mam.model().get_xray_structure(),
    d_min        = d_min,
    vector_map   = False)

  output_map_manager=mam.map_manager().customized_copy(
      map_data=result_obj.map_result)
  dm.write_real_map_file(output_map_manager, "map_model_difference_1.ccp4")

  #
  result_obj = compdiff(
    map_data_obs = mam.map_manager().map_data(),
    xrs          = mam.model().get_xray_structure(),
    d_min        = d_min,
    vector_map   = True)

  output_map_manager=mam.map_manager().customized_copy(
      map_data=result_obj.map_result)
  dm.write_real_map_file(output_map_manager, "map_model_difference_2.ccp4")
def test_01(method='model_sharpen', expected_results=None):

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

    # Read in data

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

    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)

    mmm = map_model_manager(model=model,
                            map_manager_1=mm.deep_copy(),
                            map_manager_2=mm.deep_copy(),
                            ncs_object=ncs,
                            wrapping=False)
    mmm.add_map_manager_by_id(map_id='external_map',
                              map_manager=mmm.map_manager().deep_copy())
    mmm.set_resolution(3)
    mmm.set_log(sys.stdout)

    dc = mmm.deep_copy()

    sharpen_method = getattr(mmm, method)

    # sharpen by method (can be model_sharpen, half_map_sharpen or
    #     external_sharpen)

    mmm = dc.deep_copy()
    sharpen_method(anisotropic_sharpen=False, n_bins=10)
    assert mmm.map_model_cc() > 0.9
    sharpen_method(anisotropic_sharpen=False, n_bins=10, local_sharpen=True)
    assert mmm.map_model_cc() > 0.9
    sharpen_method(anisotropic_sharpen=True, n_bins=10)
    assert mmm.map_model_cc() > 0.9
    sharpen_method(anisotropic_sharpen=True,
                   n_bins=10,
                   local_sharpen=True,
                   n_boxes=1)
    assert mmm.map_model_cc() > 0.9
Example #6
0
    def as_map_model_manager(self):
        '''
      Return map_model_manager object with contents of this class (not a deepcopy)

    '''
        from iotbx.map_model_manager import map_model_manager
        mmm = map_model_manager(
            map_manager=self.map_manager(),
            model=self.model(),
        )
        # Keep track of the gridding in this boxing.
        mmm.set_gridding_first(self.gridding_first)
        mmm.set_gridding_last(self.gridding_last)
        return mmm
Example #7
0
    def validate(self):
        assert not None in [self.model, self.params, self.out]
        if (self.model is None):
            raise Sorry("Model is required.")
        if (self.miller_array is None and self.map_inp is None):
            raise Sorry("Map or map coefficients are required.")
        # Sanity check for crystal symmetry
        if (self.map_inp is not None and self.model is not None):
            self.base = map_model_manager(map_manager=self.map_inp,
                                          model=self.model,
                                          ignore_symmetry_conflicts=self.
                                          params.ignore_symmetry_conflicts)

            self.cs_consensus = self.base.crystal_symmetry()
        else:
            self.base = None
Example #8
0
def read_map_and_model(file_name_1,
                       file_name_2,
                       regression_directory=None,
                       prefix=None):
    '''
    Identify which file is map and which is model, read in and
    create map_model_manager
    If regression_directory is specified, look there for these files, assuming
    prefix of $PHENIX/modules/phenix_regression/
  '''

    if regression_directory and not prefix:
        import libtbx.load_env
        prefix = libtbx.env.under_dist(module_name="phenix_regression",
                                       path=regression_directory,
                                       test=os.path.isdir)

    if prefix:
        file_name_1 = os.path.join(prefix, file_name_1)
        file_name_2 = os.path.join(prefix, file_name_2)

    map_file_name = None
    model_file_name = None
    for f in [file_name_1, file_name_2]:
        for ending in ['.ccp4', '.mrc', '.map']:
            if f.endswith(ending):
                map_file_name = f
        for ending in ['.pdb', '.cif']:
            if f.endswith(ending):
                model_file_name = f
    if not map_file_name or not model_file_name:
        raise Sorry("Unable to guess map and model from %s and %s" %
                    (file_name_1, file_name_2))

    from iotbx.data_manager import DataManager
    from iotbx.map_model_manager import map_model_manager
    dm = DataManager()
    dm.process_real_map_file(map_file_name)
    mm = dm.get_real_map(map_file_name)

    dm.process_model_file(model_file_name)
    model = dm.get_model(model_file_name)
    mam = map_model_manager(model=model, map_manager=mm)
    return mam
def tst_01():

  print (" test utilities in create_models_or_maps")
  flex.set_random_seed(0)
  random.seed(0)

  model=generate_model()
  assert approx_equal (
    model.get_sites_cart()[0],
    (14.476, 10.57, 8.342))

  s_model=shake_model(model,shake=2)
  assert approx_equal (
    s_model.get_sites_cart()[0],
    (14.162085804614943, 11.403509966523153, 6.450881839681677))
  map_coeffs=generate_map_coefficients(model=model)
  assert approx_equal(map_coeffs.data()[0],
     (3.70494534745-0.185333495539j))
  map_manager=generate_map(map_coeffs=map_coeffs)

  mm_2= generate_map(map_coeffs=map_coeffs,
      d_min=3.5,
      gridding=map_manager.map_data().all(),
      wrapping=True,
      origin_shift_grid_units=(100,0,0),
      low_resolution_fourier_noise_fraction=1,
      high_resolution_fourier_noise_fraction=1,
      low_resolution_real_space_noise_fraction=1,
      high_resolution_real_space_noise_fraction=1,
      )
  assert approx_equal (mm_2.map_data()[323], -0.00339192976463)

  mm_2.shift_origin()
  model.set_shift_cart(mm_2.shift_cart())
  mam=map_model_manager(map_manager=mm_2,model=model)
  mam.write_map('map.mrc')
  mam.write_model('model.pdb')

  new_mam=read_map_and_model('map.mrc','model.pdb')
  new_mam_2=read_map_and_model('model.pdb','map.mrc')
  assert new_mam.map_manager().cc_to_other_map_manager(new_mam_2.map_manager())==1
Example #10
0
    def get_polder_diff_map(self, f_obs, r_free_flags, f_calc, f_mask,
                            model_selected, box_cushion):
        fmodel = mmtbx.f_model.manager(f_obs=f_obs,
                                       r_free_flags=r_free_flags,
                                       f_calc=f_calc,
                                       f_mask=f_mask)
        fmodel.update_all_scales(remove_outliers=False)
        mc_diff = map_tools.electron_density_map(
            fmodel=fmodel).map_coefficients(map_type="mFo-DFc",
                                            isotropize=True,
                                            fill_missing=False)
        fft_map = miller.fft_map(crystal_gridding=self.crystal_gridding,
                                 fourier_coefficients=mc_diff)
        fft_map.apply_sigma_scaling()
        mm = fft_map.as_map_manager()

        from iotbx.map_model_manager import map_model_manager
        inputs = map_model_manager(
            model=model_selected.deep_copy(),
            map_manager=mm,
        )  # no need to allow ignore_symmetry_conflicts
        return inputs
Example #11
0
def run():
    result = flex.bool()
    for sgn in range(1, 231):
        group = space_group_info(sgn)
        xrs = random_structure.xray_structure(space_group_info=group,
                                              volume_per_atom=25.,
                                              general_positions_only=False,
                                              elements=('C', 'N', 'O', 'H') *
                                              30,
                                              min_distance=1.0)
        sgt = xrs.space_group().type()
        fc = xrs.structure_factors(d_min=2).f_calc()
        fft_map = fc.fft_map(symmetry_flags=maptbx.use_space_group_symmetry,
                             resolution_factor=1. / 3)
        map_data = fft_map.real_map_unpadded()
        n_real = map_data.all()
        mm = iotbx.map_manager.map_manager(
            map_data=map_data,
            unit_cell_grid=map_data.accessor().all(),
            unit_cell_crystal_symmetry=xrs.crystal_symmetry(),
            origin_shift_grid_units=[0, 0, 0],
            wrapping=True)
        assert mm.is_consistent_with_wrapping() in [True, None]
        assert mm.map_data().all() == n_real
        new_mm = mm.as_full_size_map()
        assert new_mm.map_data().all() == n_real

        # Now cut off edges and should not work:
        from iotbx.map_model_manager import map_model_manager
        mmm = map_model_manager(map_manager=mm)
        upper_bounds = tuple([x - 1 for x in mm.map_data().all()])
        mmm.box_all_maps_with_bounds_and_shift_origin(
            lower_bounds=(1, 1, 1), upper_bounds=upper_bounds)
        mm = mmm.map_manager()
        assert mm.is_consistent_with_wrapping() in [None, False]
        assert mm.map_data().all() != n_real
        new_mm = mm.as_full_size_map()
        assert new_mm.map_data().all() == n_real
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])
Example #13
0
def exercise_around_model():

    from cctbx.maptbx.box import make_list_symmetric
    a = [3, 4, 5, 3, 9, 1, 6, 3, 2, 5, 6, 6]
    new_a = make_list_symmetric(a)
    from scitbx.array_family import flex
    aa = flex.double(a)
    new_aa = flex.double(new_a)
    assert (aa.size(), new_aa.size()) == (12, 12)
    assert aa.min_max_mean().mean == new_aa.min_max_mean().mean
    print(a, new_a)

    a = [3, 4, 5, 3, 8, 1, 6, 7, 3, 2, 5, 6, 6]
    new_a = make_list_symmetric(a)
    from scitbx.array_family import flex
    aa = flex.double(a)
    new_aa = flex.double(new_a)
    print(a, new_a)
    assert (aa.size(), new_aa.size()) == (13, 13)
    assert aa.min_max_mean().mean == new_aa.min_max_mean().mean

    mam = get_random_structure_and_map(use_static_structure=True)

    map_data_orig = mam.mm.map_data().deep_copy()
    sites_frac_orig = mam.model.get_sites_frac().deep_copy()
    sites_cart_orig = mam.model.get_sites_cart().deep_copy()
    cs_orig = mam.model.crystal_symmetry()

    box = cctbx.maptbx.box.around_model(map_manager=mam.mm,
                                        model=mam.model.deep_copy(),
                                        box_cushion=10,
                                        wrapping=True)
    new_mm1 = box.map_manager()
    new_mm2 = box.apply_to_map(map_manager=mam.mm.deep_copy())
    assert approx_equal(new_mm1.map_data(), new_mm2.map_data())

    new_model1 = box.model()
    new_model2 = box.apply_to_model(model=mam.model.deep_copy())
    assert new_model1.crystal_symmetry().is_similar_symmetry(
        new_model2.crystal_symmetry())
    assert new_model1.crystal_symmetry().is_similar_symmetry(
        box.crystal_symmetry)

    assert approx_equal(new_model1.get_sites_cart()[0],
                        (19.705233333333336, 15.631525, 13.5040625))
    # make sure things did change
    assert new_mm2.map_data().size() != map_data_orig.size()

    # make sure things are changed in-place and are therefore different from start
    assert box.map_manager().map_data().size() != map_data_orig.size()
    assert box.model().get_sites_frac() != sites_frac_orig
    assert box.model().get_sites_cart() != sites_cart_orig
    assert (not cs_orig.is_similar_symmetry(box.model().crystal_symmetry()))

    # make sure box, model and map_manager remember original crystal symmetry
    assert cs_orig.is_similar_symmetry(
        box.map_manager().unit_cell_crystal_symmetry())
    assert cs_orig.is_similar_symmetry(
        box.map_manager().unit_cell_crystal_symmetry())

    assert approx_equal(
        box.model().shift_cart(),
        [5.229233333333334, 5.061524999999999, 5.162062499999999])

    assert box.model().unit_cell_crystal_symmetry().is_similar_symmetry(
        cs_orig)
    assert (not box.model().crystal_symmetry().is_similar_symmetry(cs_orig))

    assert approx_equal(
        box.model()._figure_out_hierarchy_to_output(
            do_not_shift_back=False).atoms().extract_xyz()[0],
        (14.476, 10.57, 8.342))

    # make sure we can stack shifts
    sel = box.model().selection("resseq 219:219")
    m_small = box.model().select(selection=sel)

    assert approx_equal(box.model().shift_cart(), m_small.shift_cart())

    # Now box again:
    small_box = cctbx.maptbx.box.around_model(map_manager=mam.mm,
                                              model=mam.model.deep_copy(),
                                              box_cushion=5,
                                              wrapping=True)

    # Make sure nothing was zeroed out in this map (wrapping = True)
    assert new_mm1.map_data().as_1d().count(0) == 0

    # Now without wrapping...
    box = cctbx.maptbx.box.around_model(map_manager=mam.mm,
                                        model=mam.model.deep_copy(),
                                        box_cushion=10,
                                        wrapping=False)

    # make sure things are changed in-place and are therefore different from start
    assert box.map_manager().map_data().size() != map_data_orig.size()
    assert box.model().get_sites_frac() != sites_frac_orig
    assert box.model().get_sites_cart() != sites_cart_orig
    assert (not cs_orig.is_similar_symmetry(box.model().crystal_symmetry()))

    # make sure box, model and map_manager remember original crystal symmetry
    assert cs_orig.is_similar_symmetry(
        box.model().unit_cell_crystal_symmetry())
    assert cs_orig.is_similar_symmetry(
        box.map_manager().unit_cell_crystal_symmetry())

    assert box.map_manager().map_data().as_1d().count(0) == 81264

    # Now specify bounds directly
    new_box = cctbx.maptbx.box.with_bounds(map_manager=mam.mm.deep_copy(),
                                           lower_bounds=(-7, -7, -7),
                                           upper_bounds=(37, 47, 39),
                                           wrapping=False)

    new_model = new_box.apply_to_model(mam.model.deep_copy())
    # make sure things are changed in-place and are therefore different from start
    assert new_box.map_manager().map_data().size() != map_data_orig.size()
    assert new_model.get_sites_frac() != sites_frac_orig
    assert new_model.get_sites_cart() != sites_cart_orig
    assert (not cs_orig.is_similar_symmetry(new_model.crystal_symmetry()))

    # make sure box, model and map_manager remember original crystal symmetry
    assert cs_orig.is_similar_symmetry(
        box.model().unit_cell_crystal_symmetry())
    assert cs_orig.is_similar_symmetry(
        box.map_manager().unit_cell_crystal_symmetry())

    assert box.map_manager().map_data().as_1d().count(0) == 81264

    # Now specify bounds directly and init with model
    box = cctbx.maptbx.box.with_bounds(map_manager=mam.mm.deep_copy(),
                                       lower_bounds=(-7, -7, -7),
                                       upper_bounds=(37, 47, 39),
                                       wrapping=False,
                                       model=mam.model.deep_copy())

    new_model = box.model()
    # make sure things are changed in-place and are therefore different from start
    assert box.map_manager().map_data().size() != map_data_orig.size()
    assert new_model.get_sites_frac() != sites_frac_orig
    assert new_model.get_sites_cart() != sites_cart_orig
    assert (not cs_orig.is_similar_symmetry(new_model.crystal_symmetry()))

    # make sure box, model and map_manager remember original crystal symmetry
    assert cs_orig.is_similar_symmetry(
        box.model().unit_cell_crystal_symmetry())
    assert cs_orig.is_similar_symmetry(
        box.map_manager().unit_cell_crystal_symmetry())

    assert box.map_manager().map_data().as_1d().count(0) == 81264

    # Extract using around_unique

    data_dir = os.path.dirname(os.path.abspath(__file__))
    data_ccp4 = os.path.join(data_dir, 'data', 'D7.ccp4')
    data_ncs = os.path.join(data_dir, 'data', 'D7.ncs_spec')
    data_seq = os.path.join(data_dir, 'data', 'D7.seq')

    dm = DataManager(['real_map', 'phil', 'ncs_spec', 'sequence'])
    dm.process_real_map_file(data_ccp4)
    mm = dm.get_real_map(data_ccp4)

    dm.process_ncs_spec_file(data_ncs)
    ncs_obj = dm.get_ncs_spec(data_ncs)

    dm.process_sequence_file(data_seq)
    sequence = dm.get_sequence(data_seq)
    sequence_as_text = sequence[0].sequence

    map_model_mgr = map_model_manager(map_manager=mm, ncs_object=ncs_obj)
    mm = map_model_mgr.map_manager()
    mm.show_summary()

    box = cctbx.maptbx.box.around_unique(
        map_manager=mm.deep_copy(),
        resolution=3,
        box_cushion=1,
        sequence=sequence_as_text,
        soft_mask=True,
        wrapping=False,
    )

    box.map_manager().write_map('new_box.ccp4')

    # run again from map_manager

    map_model_mgr.box_all_maps_around_unique_and_shift_origin(
        resolution=3,
        box_cushion=1,
        sequence=sequence_as_text,
        soft_mask=True,
    )

    # Get bounds around density
    box = cctbx.maptbx.box.around_density(map_manager=mam.mm.deep_copy(),
                                          wrapping=False)

    # Create a mask

    mm = mam.mm.deep_copy()

    mm.create_mask_around_density(
        resolution=3,
        molecular_mass=2100,
        sequence="GAVAGA",
        solvent_content=0.5,
    )
    mask_mm = mm.get_mask_as_map_manager()
    assert approx_equal(
        (mask_mm.map_data().count(0), mask_mm.map_data().count(1),
         mask_mm.map_data().size()), (19184, 19216, 38400))

    # Box around the mask
    box = cctbx.maptbx.box.around_mask(
        map_manager=mam.mm.deep_copy(),
        mask_as_map_manager=mask_mm,
        wrapping=False,
    )

    assert (box.gridding_first, box.gridding_last) == ([0, 0, 0], [29, 39, 31])

    # Box around the mask with cubic box
    box = cctbx.maptbx.box.around_mask(
        map_manager=mam.mm.deep_copy(),
        mask_as_map_manager=mask_mm,
        use_cubic_boxing=True,
        wrapping=False,
    )

    assert (box.gridding_first, box.gridding_last) == ([1, 6, 2], [30, 35, 31])

    #
    # IF you are about to change this - THINK TWICE!
    #
    from libtbx.introspection import getfullargspec
    r = getfullargspec(cctbx.maptbx.box.around_model.__init__)
    assert r.args == [
        'self', 'map_manager', 'model', 'box_cushion', 'wrapping',
        'model_can_be_outside_bounds', 'stay_inside_current_map',
        'use_cubic_boxing', 'require_match_unit_cell_crystal_symmetry', 'log'
    ], r.args
    r = getfullargspec(cctbx.maptbx.box.with_bounds.__init__)
    assert r.args == [
        'self', 'map_manager', 'lower_bounds', 'upper_bounds', 'model',
        'wrapping', 'model_can_be_outside_bounds', 'stay_inside_current_map',
        'use_cubic_boxing', 'log'
    ], r.args

    print("OK")
Example #14
0
def exercise():
    """Test prepare_map_for_docking using data with known errors."""

    # Generate two half-maps with same anisotropic signal, independent anisotropic
    # noise. Test to see how well optimal map coefficients are estimated.

    # Start by working out how large the padding will have to be so that
    # starting automatically-generated map will be large enough to contain
    # sphere with room to spare around model.
    n_residues = 25
    d_min = 2.5
    from cctbx.development.create_models_or_maps import generate_model
    test_model = generate_model(n_residues=n_residues)
    sites_cart = test_model.get_sites_cart()
    cart_min = flex.double(sites_cart.min())
    cart_max = flex.double(sites_cart.max())
    box_centre = (cart_min + cart_max) / 2
    dsqrmax = flex.max((sites_cart - tuple(box_centre)).norms())**2
    model_radius = math.sqrt(dsqrmax)
    min_model_extent = flex.min(cart_max - cart_min)
    pad_to_allow_cube = model_radius - min_model_extent / 2
    # Extra space needed for eventual masking
    boundary_to_smoothing_ratio = 2
    soft_mask_radius = d_min
    padding = soft_mask_radius * boundary_to_smoothing_ratio
    box_cushion = padding + pad_to_allow_cube + d_min  # A bit extra

    # Make map in box big enough to cut out cube containing sphere
    mmm = map_model_manager()
    mmm.generate_map(n_residues=n_residues,
                     d_min=d_min,
                     k_sol=0.1,
                     b_sol=50.,
                     box_cushion=box_cushion)
    # Keep copy of perfect map for tests of success
    mm_start = mmm.map_manager().deep_copy()
    mmm.add_map_manager_by_id(mm_start, 'perfect_map')
    model = mmm.model()
    sites_cart = model.get_sites_cart()
    cart_min = flex.double(sites_cart.min())
    cart_max = flex.double(sites_cart.max())

    # Turn starting map into map coeffs for the signal
    ucpars = mmm.map_manager().unit_cell().parameters()
    d_max = max(ucpars[0], ucpars[1], ucpars[2])
    start_map_coeffs = mmm.map_as_fourier_coefficients(d_min=d_min,
                                                       d_max=d_max)

    # Apply anisotropic scaling to map coeffs
    b_target = (100., 200., 300., -50., 50., 100.)
    u_star_s = adptbx.u_cart_as_u_star(start_map_coeffs.unit_cell(),
                                       adptbx.b_as_u(b_target))
    # b_model = (30.,30.,30.,0.,0.,0.)  # All atoms in model have B=30
    # b_expected = list((flex.double(b_target) + flex.double(b_model)))
    scaled_map_coeffs = start_map_coeffs.apply_debye_waller_factors(
        u_star=u_star_s)

    # Generate map coefficient errors for first half-map from complex normal
    # distribution
    b_target_e = (0., 0., 0., -50., -50., 100.)  # Anisotropy for error terms
    u_star_e = adptbx.u_cart_as_u_star(start_map_coeffs.unit_cell(),
                                       adptbx.b_as_u(b_target_e))
    se_target = 10.  # Target for SigmaE variance term
    rsigma = math.sqrt(se_target / 2.)
    jj = 0. + 1.j  # Define I for generating complex numbers
    random_complexes1 = flex.complex_double()
    ncoeffs = start_map_coeffs.size()
    random.seed(123457)  # Make runs reproducible
    for i in range(ncoeffs):
        random_complexes1.append(
            random.gauss(0., rsigma) + random.gauss(0., rsigma) * jj)
    rc1_miller = start_map_coeffs.customized_copy(data=random_complexes1)
    mc1_delta = rc1_miller.apply_debye_waller_factors(u_star=u_star_e)
    map1_coeffs = scaled_map_coeffs.customized_copy(
        data=scaled_map_coeffs.data() + mc1_delta.data())

    # Repeat for second half map with independent errors from same distribution
    random_complexes2 = flex.complex_double()
    for i in range(ncoeffs):
        random_complexes2.append(
            random.gauss(0., rsigma) + random.gauss(0., rsigma) * jj)
    rc2_miller = start_map_coeffs.customized_copy(data=random_complexes2)
    mc2_delta = rc2_miller.apply_debye_waller_factors(u_star=u_star_e)
    map2_coeffs = scaled_map_coeffs.customized_copy(
        data=scaled_map_coeffs.data() + mc2_delta.data())

    # mmm.write_model("fake_map.pdb")
    mmm.add_map_from_fourier_coefficients(map1_coeffs, map_id='map_manager_1')
    mmm.add_map_from_fourier_coefficients(map2_coeffs, map_id='map_manager_2')
    # Replace original map_manager with mean of half-maps
    mm_mean_data = (mmm.map_manager_1().map_data() +
                    mmm.map_manager_2().map_data()) / 2
    mmm.map_manager().set_map_data(map_data=mm_mean_data)
    # Add mask map for ordered component of map
    protein_mw = n_residues * 110.  # MW from model would be better...
    nucleic_mw = None
    mask_id = 'ordered_volume_mask'
    add_ordered_volume_mask(mmm,
                            d_min,
                            protein_mw=protein_mw,
                            nucleic_mw=nucleic_mw,
                            map_id_out=mask_id)
    box_centre = tuple(flex.double((ucpars[0], ucpars[1], ucpars[2])) / 2)
    # Now refine to assess parameters describing map errors
    results = assess_cryoem_errors(mmm,
                                   d_min,
                                   sphere_cent=tuple(box_centre),
                                   radius=model_radius + d_min,
                                   verbosity=0)

    # resultsdict = results.resultsdict
    # b_refined_a = resultsdict["a_baniso"]
    # print("\nIdeal A tensor as Baniso: ", b_expected)
    # print("Refined A tensor as Baniso", b_refined_a)

    # Note that all maps have been cut out with a spherical mask, so compare using these
    new_mmm = results.new_mmm
    perfect_mapCC = new_mmm.map_model_cc(map_id='perfect_map')
    mapCC = new_mmm.map_model_cc(map_id='map_manager_wtd')  # Achieved map
    start_mapCC = new_mmm.map_model_cc(
    )  # Starting map with noise and anisotropy

    mc_perfect = new_mmm.map_as_fourier_coefficients(d_min=d_min,
                                                     d_max=d_max,
                                                     map_id='perfect_map')
    mc_achieved = new_mmm.map_as_fourier_coefficients(d_min=d_min,
                                                      d_max=d_max,
                                                      map_id='map_manager_wtd')

    # Compare with results using theoretically perfect error parameters to compute
    # ideal map coefficients.
    sigmaS_terms = flex.pow2(get_power_spectrum(
        mc_perfect))  # Actual signal power before anisotropy
    mc_start = new_mmm.map_as_fourier_coefficients(d_min=d_min, d_max=d_max)
    eE_ideal = mc_start.deep_copy()
    ones_array = flex.double(eE_ideal.size(), 1)
    all_ones = eE_ideal.customized_copy(data=ones_array)
    u_star_s2 = tuple(flex.double(u_star_s) *
                      2.)  # Square anisotropy for signal power calc
    sigmaS_terms = sigmaS_terms * all_ones.apply_debye_waller_factors(
        u_star=u_star_s2).data()  # Corrected for anisotropy

    u_star_e2 = tuple(flex.double(u_star_e) * 2.)
    sigmaE_terms = all_ones.apply_debye_waller_factors(
        u_star=u_star_e2).data() * se_target

    scale_terms = 1. / flex.sqrt(sigmaS_terms + sigmaE_terms / 2.)
    dobs_terms = 1. / flex.sqrt(1. + sigmaE_terms / (2 * sigmaS_terms))
    mc_ideal = eE_ideal.customized_copy(data=eE_ideal.data() * scale_terms *
                                        dobs_terms)
    # write_mtz(mc_achieved,"achieved_map.mtz","achieved")
    # write_mtz(mc_ideal,"ideal_map.mtz","ideal")

    mapCC_ideal_achieved = mc_ideal.map_correlation(other=mc_achieved)
    # print("CC between ideal and achieved maps:",mapCC_ideal_achieved)
    assert (mapCC_ideal_achieved > 0.92)
    new_mmm.add_map_from_fourier_coefficients(mc_ideal, map_id='ideal_map')
    ideal_mapCC = new_mmm.map_model_cc(map_id='ideal_map')
    # print("Perfect, starting, ideal and achieved mapCC: ", perfect_mapCC, start_mapCC, ideal_mapCC, mapCC)
    assert (mapCC > 0.98 * ideal_mapCC)
  def get_statistics(self):
    if self.json_data['success_composition'] is False:
      return
    make_sub_header('Get statistics', out=self.logger)
    self.save_json()
    success = True
    self.json_data['success_statistics'] = None

    sc, r_sc = None, None
    self.pickle_data.min_reso = None
    self.pickle_data.d_inv = None
    self.pickle_data.r_d_inv = None
    self.pickle_data.d_inv_half = None
    self.pickle_data.fsc_model = None
    self.pickle_data.r_fsc_model = None
    self.pickle_data.fsc_half = None

    if self.json_data['restraints_file']:
      cif_file = self.json_data['restraints_file']
      cif_objects = monomer_library.server.read_cif(file_name=to_str(cif_file))
      cif_objects = [(cif_file, cif_objects)]
    else: cif_objects = None

    for p, fn in zip(('', 'r_'),
      [self.json_data['pdb_file_updated'], self.json_data['pdb_file_refined']]):
    # TODO which file to use for initial!!
      if (not fn or not os.path.isfile(fn)): continue

      if p=='': print('Initial model', file=self.logger)
      if p=='r_': print('\nRefined model', file=self.logger)

      dm = DataManager()

      dm.process_real_map_file(self.json_data['map_file'])
      map_inp = dm.get_real_map(self.json_data['map_file'])
      #map_inp   = iotbx.ccp4_map.map_reader(file_name = self.json_data['map_file'])
      map_inp_1, map_inp_2 = None, None
      if self.json_data['map_file_1'] is not None:
        #map_inp_1 = iotbx.ccp4_map.map_reader(file_name = self.json_data['map_file_1'])
        dm.process_real_map_file(self.json_data['map_file_1'])
        map_inp_1 = dm.get_real_map(self.json_data['map_file_1'])
        #map_data_1 = map_inp_1.map_data()
      if self.json_data['map_file_2'] is not None:
        dm.process_real_map_file(self.json_data['map_file_2'])
        map_inp_2 = dm.get_real_map(self.json_data['map_file_2'])
        #map_inp_2 = iotbx.ccp4_map.map_reader(file_name = self.json_data['map_file_2'])
        #map_data_2 = map_inp_2.map_data()

      print('\tGet model class with restraints...', file=self.logger)
      pdb_inp = iotbx.pdb.input(file_name = fn)
      try:
        model = mmtbx.model.manager(
          model_input       = pdb_inp,
          restraint_objects = cif_objects,
          build_grm         = True,
          stop_for_unknowns = False,
          crystal_symmetry  = map_inp.crystal_symmetry(),
          log               = null_out())
      except Exception as e:
        #success = False
        self.json_data['success_statistics'] = False
        msg = traceback.format_exc()
        print(msg, file=self.logger)
        self.write_log(
          step = 'Statistics: Model class (with restraints) from pdb ',
          msg  = msg)
        self.save_json()
        continue

      if not success: continue

      # Emringer
      if self.resolution < 4.0:
        print('\tCalculate Emringer score', file=self.logger)
        try:
          emringer_score = self.get_emringer_score(
            model   = model.deep_copy(),
            map_inp = map_inp.deep_copy())
          self.json_data[p+'emringer'] = emringer_score
        except Exception as e:
          msg = traceback.format_exc()
          print(msg, file=self.logger)
          self.write_log(step = 'EMRinger failed ', msg = msg)
          # TODO: save as success_statistics False?

      # Rama-z score
      try:
        rama_z_score = rama_z.rama_z(
        models = [model],
        log = self.logger)
        z_scores = rama_z_score.get_z_scores()
        self.json_data[p+'z_score'] = z_scores['W'][0]
      except Exception as e:
        msg = traceback.format_exc()
        print(msg, file=self.logger)
        self.write_log(step = 'Rama z-score failed ', msg = msg)

#      base = map_and_model.input(
#        map_manager   = map_inp,
#        map_manager_1 = map_inp_1,
#        map_manager_2 = map_inp_2,
#        model         = model,
#        box           = True)

      checked = map_model_manager(
        map_manager         = map_inp,
        map_manager_1       = map_inp_1,
        map_manager_2       = map_inp_2,
        model      = model,
        wrapping   = None)
      checked.box_all_maps_around_model_and_shift_origin()

      params = validation_cryoem.master_params().extract()
      params.resolution = self.resolution
      params.slim = False

      if (p == ''):
        print('\tGet map parameters...', file=self.logger)
        self.get_map_parameters(base = checked)

      print('\tRun Mtriage...', file=self.logger)
      try:
        o = validation_cryoem.validation(
          model      = checked.model(),
          map_data   = checked.map_data(),
          map_data_1 = checked.map_data_1(),
          map_data_2 = checked.map_data_2(),
          params     = params)
      except Exception as e:
        # success = False
        self.json_data['success_statistics'] = False
        msg = traceback.format_exc()
        print(msg, file=self.logger)
        self.write_log(step = 'Statistics ', msg = msg)
        self.save_json()
        continue

      print('\tStore info in pkl...', file=self.logger)
      o_dict = self.fill_dictionary(o)
      if (p == ''):
        self.json_data['o'] = o_dict
        sc = o.model.get_sites_cart()
      elif (p == 'r_'):
        self.json_data['o_refined'] = o_dict
        r_sc = o.model.get_sites_cart()
      if o is not None:
        if o.model_vs_data is not None:
          if o.model_vs_data.cc is not None:
            if o.model_vs_data.cc.cc_per_chain is not None:
              if (p == ''):
                self.pickle_data.cc_per_chain = o.model_vs_data.cc.cc_per_chain
              if (p == 'r_'):
                self.pickle_data.r_cc_per_chain = o.model_vs_data.cc.cc_per_chain
            if o.model_vs_data.cc.cc_per_residue is not None:
              if (p == ''):
                self.pickle_data.cc_per_residue = o.model_vs_data.cc.cc_per_residue
              if (p == 'r_'):
                self.pickle_data.r_cc_per_residue = o.model_vs_data.cc.cc_per_residue
        if o.model_stats.geometry.ramachandran.ramalyze is not None:
          rl = o.model_stats.geometry.ramachandran.ramalyze
          if (p == ''): self.pickle_data.ramalyze = rl
          if (p == 'r_'): self.pickle_data.r_ramalyze = rl
      # values for the curve go up to very high reso, get highest reso limit
#      if o is not None:
#        reso_list = [self.json_data['d_pdb'],self.json_data['d_cif'],
#          self.json_data['d_map'], self.json_data['o']['d99'],
#          self.json_data['o']['d_model'], self.json_data['o']['d_fsc']]
#      else:
#        reso_list = [self.json_data['d_pdb'],self.json_data['d_cif'],
#          self.json_data['d_map']]
#      min_reso = min([elem for elem in reso_list if elem is not None])
#      self.pickle_data.min_reso = min_reso
      min_reso = self.resolution
      self.pickle_data.min_reso = self.resolution
      # apply some arbitrary buffer
      min_reso = min_reso - 0.5
      if o is not None:
        masked = o.data_stats.masked
        if (masked.fsc_curve_model is not None):
          if ((masked.fsc_curve_model.d_inv is not None) and
              (masked.fsc_curve_model.fsc is not None)):
            if (p == ''):
              self.pickle_data.d_inv = masked.fsc_curve_model.d_inv
              self.pickle_data.fsc_model   = masked.fsc_curve_model.fsc
            if (p == 'r_'):
              self.pickle_data.r_d_inv = masked.fsc_curve_model.d_inv
              self.pickle_data.r_fsc_model   = masked.fsc_curve_model.fsc
        if (p == ''):
          if (masked.fsc_curve is not None):
            if (masked.fsc_curve.fsc is not None):
              if ((masked.fsc_curve.fsc.d_inv is not None) and
                  (masked.fsc_curve.fsc.fsc is not None)):
                self.pickle_data.d_inv_half = masked.fsc_curve.fsc.d_inv
                self.pickle_data.fsc_half   = masked.fsc_curve.fsc.fsc

      if o is not None:
        print("\nd99                  :", o.data_stats.masked.d99)
        print("d_model              :", o.data_stats.masked.d_model)
        print("d_fsc                :", o.data_stats.masked.d_fsc)
        print("ramachandran.outliers:", o.model_stats.geometry.ramachandran.outliers)
        print("cc_mask              :", o.model_vs_data.cc.cc_mask)
        print("cc_box               :", o.model_vs_data.cc.cc_box)

    if sc is not None and r_sc is not None:
      self.json_data['rmsd'] = sc.rms_difference(r_sc)

    if self.json_data['success_statistics'] is not False:
      self.json_data['success_statistics'] = success
Example #16
0
def RunProbeTests(inFileName):

    #========================================================================
    # Call the test functions for the libraries we test.

    ret = probeext.DotSpheres_test()
    assert len(ret) == 0, "DotSpheres_test() failed: " + ret

    ret = probeext.SpatialQuery_test()
    assert len(ret) == 0, "SpatialQuery_test() failed: " + ret

    ret = probeext.Scoring_test()
    assert len(ret) == 0, "Scoring_test() failed: " + ret

    AtomTypes.Test()
    Helpers.Test()

    #========================================================================
    # Now ensure that we can use the C++-wrapped classes as intended to make sure
    # that the wrapping code or parameters have not changed.

    #========================================================================
    # Make sure we can get at the DotSphere objects and their methods
    cache = probeext.DotSphereCache(10)
    sphere1 = cache.get_sphere(1)
    dots = sphere1.dots()

    #========================================================================
    # Make sure we can fill in an ExtraAtomInfoList and pass it to scoring
    # Generate an example data model with a small molecule in it
    if inFileName is not None and len(inFileName) > 0:
        # Read a model from a file using the DataManager
        dm = DataManager()
        dm.process_model_file(inFileName)
        model = dm.get_model(inFileName)
    else:
        # Generate a small-molecule model using the map model manager
        mmm = map_model_manager(
        )  #   get an initialized instance of the map_model_manager
        mmm.generate_map(
        )  #   get a model from a generated small library model and calculate a map for it
        model = mmm.model()  #   get the model

    # Fix up bogus unit cell when it occurs by checking crystal symmetry.
    cs = model.crystal_symmetry()
    if (cs is None) or (cs.unit_cell() is None):
        model = shift_and_box_model(model=model)

    # Get the list of all atoms in the model
    atoms = model.get_atoms()

    # Get the bonding information we'll need to exclude our bonded neighbors.
    try:
        p = mmtbx.model.manager.get_default_pdb_interpretation_params()
        model.process(make_restraints=True,
                      pdb_interpretation_params=p)  # make restraints
        geometry = model.get_restraints_manager().geometry
        sites_cart = model.get_sites_cart()  # cartesian coordinates
        bond_proxies_simple, asu = \
            geometry.get_all_bond_proxies(sites_cart = sites_cart)
    except Exception as e:
        raise Exception("Could not get bonding information for input file: " +
                        str(e))
    bondedNeighbors = Helpers.getBondedNeighborLists(atoms,
                                                     bond_proxies_simple)

    # Traverse the hierarchy and look up the extra data to be filled in.
    ret = Helpers.getExtraAtomInfo(model)
    extra = ret.extraAtomInfo

    # Construct a SpatialQuery and fill in the atoms.  Ensure that we can make a
    # query within 1000 Angstroms of the origin.
    sq = probeext.SpatialQuery(atoms)
    nb = sq.neighbors((0, 0, 0), 0, 1000)

    # Construct a DotScorer object.
    # Find the radius of each atom in the structure and construct dot spheres for
    # them. Find the atoms that are bonded to them and add them to an excluded list.
    # Then compute the score for each of them and report the summed score over the
    # whole molecule the way that Reduce will.
    ds = probeext.DotScorer(extra)
    total = 0
    badBumpTotal = 0
    for a in atoms:
        rad = extra.getMappingFor(a).vdwRadius
        assert rad > 0, "Invalid radius for atom look-up: " + a.name + " rad = " + str(
            rad)
        sphere = cache.get_sphere(rad)

        # Excluded atoms that are bonded to me or to one of my neightbors.
        # It has the side effect of excluding myself if I have any neighbors.
        # Construct as a set to avoid duplicates.
        exclude = set()
        for n in bondedNeighbors[a]:
            exclude.add(n)
            for n2 in bondedNeighbors[n]:
                exclude.add(n2)
        exclude = list(exclude)

        dots = sphere.dots()
        res = ds.score_dots(a, 1.0, sq, rad * 3, 0.25, exclude, sphere.dots(),
                            sphere.density(), False)
        total += res.totalScore()
        if res.hasBadBump:
            badBumpTotal += 1

    # Test calling the single-dot checking code as will be used by Probe to make sure
    # all of the Python linkage is working
    dotOffset = [1, 0, 0]
    check = ds.check_dot(atoms[0], dotOffset, 1, atoms, [atoms[0]])
    overlapType = check.overlapType

    # Test calling the interaction_type method to be sure Python linkage is working
    interactionType = ds.interaction_type(check.overlapType, check.gap)
Example #17
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))
Example #18
0
                (self.d_max - self.d_min)))

    def get_i_bin_below(self, d_value):
        return max(0, min(self.n_bins - 2, int(self.get_i_bin_real(d_value))))

    def get_i_bin_above(self, d_value):
        return max(1, min(self.n_bins - 1, 1 + self.get_i_bin_below(d_value)))

    def exercise(self):
        print("Exercising approx_amplitude_vs_resolution")

        for d_value in range(20):
            print("D-min %s  rms FC  %s " % (d_value, aavr.get_value(d_value)))

        for d_min, d_max in ((1.1, 1.3), (2.7, 3.0), (3.5, 3.6), (10, 18)):
            print("D_min %s D_max %s  RMS FC: %s " %
                  (d_min, d_max, self.get_expected_value_in_bin(d_min, d_max)))


if __name__ == "__main__":
    aavr = approx_amplitude_vs_resolution()
    aavr.exercise()

    from iotbx.map_model_manager import map_model_manager
    mmm = map_model_manager()
    mmm.generate_map()
    new_aavr = approx_amplitude_vs_resolution(d_min=3,
                                              n_bins=20,
                                              map_model_manager=mmm)
    print(list(new_aavr.get_target_scale_factors()))
Example #19
0
def Test(inFileName = None):

  #========================================================================
  # Make sure we can fill in mmtbx.probe.ExtraAtomInfoList info.
  # Generate an example data model with a small molecule in it unless we
  # were given a file name on the command line.
  if inFileName is not None and len(inFileName) > 0:
    # Read a model from a file using the DataManager
    dm = DataManager()
    dm.process_model_file(inFileName)
    model = dm.get_model(inFileName)
  else:
    # Generate a small-molecule model using the map model manager
    mmm=map_model_manager()         #   get an initialized instance of the map_model_manager
    mmm.generate_map()              #   get a model from a generated small library model and calculate a map for it
    model = mmm.model()             #   get the model

  # Fill in an ExtraAtomInfoList with an entry for each atom in the hierarchy.
  # We first find the largest i_seq sequence number in the model and reserve that
  # many entries so we will always be able to fill in the entry for an atom.
  atoms = model.get_atoms()
  maxI = atoms[0].i_seq
  for a in atoms:
    if a.i_seq > maxI:
      maxI = a.i_seq
  extra = []
  for i in range(maxI+1):
    extra.append(probe.ExtraAtomInfo())

  # Traverse the hierarchy and look up the extra data to be filled in.
  # Get a list of all the atoms in the chain while we're at it
  at = AtomTypes()
  ph = model.get_hierarchy()
  for m in ph.models():
    for chain in m.chains():
      for rg in chain.residue_groups():
        for ag in rg.atom_groups():
          for a in ag.atoms():
            ei, warn = at.FindProbeExtraAtomInfo(a)
            extra[a.i_seq] = ei
            # User code should test for and print warnings
            #if len(warn) > 0:
            #  print(warn)

  #========================================================================
  # Find an Oxygen atom and ask for its radii with explicit Hydrogen, implicit Hydrogen,
  # and Nuclear radii.
  o = None
  ph = model.get_hierarchy()
  for a in ph.models()[0].atoms():
    if a.element.strip() == 'O':
      o = a
  assert o is not None, "AtomTypes.Test(): Could not find Oxygen (internal test failure)"
  explicitH = AtomTypes(useNeutronDistances = False,
                        useImplicitHydrogenDistances = False).FindProbeExtraAtomInfo(o)[0].vdwRadius
  implicitH = AtomTypes(useNeutronDistances = False,
                        useImplicitHydrogenDistances = True).FindProbeExtraAtomInfo(o)[0].vdwRadius
  neutronH = AtomTypes(useNeutronDistances = True,
                        useImplicitHydrogenDistances = False).FindProbeExtraAtomInfo(o)[0].vdwRadius
  assert explicitH != implicitH, "AtomTypes.Test(): Implicit and explicit Oxygen radii did not differ as expected"

  #========================================================================
  # Check MaximumVDWRadius, calling it twice to make sure both the cached and non-cached
  # results work.
  for i in range(2):
    assert at.MaximumVDWRadius() == 2.5, "AtomTypes.Test(): Unexpected MaximumVDWRadius(): got "+str(MaximumVDWRadius())+", expected 2.5"

  #========================================================================
  # Check IsAromatic() to ensure it gives results when expected and not when not.
  aromaticChecks = [
      ['PHE', 'CE2', True],
      ['  U', 'HN3', True],
      ['ASN',   'O', False]
    ]
  for a in aromaticChecks:
    assert IsAromatic(a[0],a[1]) == a[2], "AtomTypes.Test(): {} {} not marked as aromatic {}".format(a[0],a[1],a[2])
Example #20
0
    def get_map_model_manager(self,
                              model_file=None,
                              map_files=None,
                              from_phil=False,
                              **kwargs):
        '''
    A convenience function for constructing a map_model_manager from the
    files in the DataManager.

    Parameters
    ==========
      model_file: str
        The name of the model file
      map_files: str or list
        The name(s) of the map files. If there is only one map, the name (str)
        is sufficient. A list is expected to have only 1 (full) or 2 (two half-
        maps) or 3 (full and 2 half-maps) map files. If three map files
        are in the list,
        the first map file is assumed to be the full map.
      from_phil: bool
        If set to True, the model and map names are retrieved from the
        standard PHIL names. The model_file and map_files parameters must
        be None if this parameter is set to True.
      **kwargs: keyworded arguments
        Extra keyworded arguments for map_model_manager constructor

    Return
    ======
      map_model_manager object
    '''

        # get filenames from PHIL
        if from_phil:
            if model_file is not None or map_files is not None:
                raise Sorry(
                    'If from_phil is set to True, model_file and map_files must be None.'
                )

            params = self._program.params
            map_model = None
            if hasattr(params, 'map_model'):
                map_model = params.map_model
            elif hasattr(params, 'input_files') and hasattr(
                    params.input_files, 'map_model'):
                map_model = params.input_files.map_model
            else:
                raise Sorry(
                    'Program does not have the "map_model" PHIL scope.')

            model_file = map_model.model

            map_files = []
            full_map = getattr(map_model, 'full_map', None)
            if full_map is not None:
                map_files.append(full_map)
            half_maps = getattr(map_model, 'half_map', None)
            if half_maps:
                if len(half_maps) != 2:
                    raise Sorry('Please provide 2 half-maps.')
                map_files += half_maps

        # If we didn't get anything, try looking directly at the
        #  available maps and models. If there are 1, 2 or 3 maps and 1 model,
        #  take them
        if (not model_file) and self.get_model_names() and \
             len(self.get_model_names()) == 1:
            model_file = self.get_default_model_name()
        if not map_files and self.get_real_map_names():
            if len(self.get_real_map_names()) == 1:
                map_files = self.get_default_real_map_name()
            elif len(self.get_real_map_names()) in [2, 3]:
                map_files = self.get_real_map_names()

        # check map_files argument
        mm = None
        mm_1 = None
        mm_2 = None
        if isinstance(map_files, list):
            if len(map_files) != 1 and len(map_files) != 2 and len(
                    map_files) != 3:
                msg = 'Please provide only 1 full map or 2 half maps or 1 ' +\
                 'full map and 2 half maps.\n Found:\n'
                for map_file in map_files:
                    msg += ('  {map_file}\n'.format(map_file=map_file))
                raise Sorry(msg)
            if len(map_files) == 1:
                mm = self.get_real_map(map_files[0])
            elif len(map_files) == 2:
                mm_1 = self.get_real_map(map_files[0])
                mm_2 = self.get_real_map(map_files[1])
            elif len(map_files) == 3:
                mm = self.get_real_map(map_files[0])
                mm_1 = self.get_real_map(map_files[1])
                mm_2 = self.get_real_map(map_files[2])
        elif map_files:
            mm = self.get_real_map(map_files)
        else:
            mm = None

        if model_file:
            model = self.get_model(model_file)
        else:
            model = None

        mmm = map_model_manager(model=model,
                                map_manager=mm,
                                map_manager_1=mm_1,
                                map_manager_2=mm_2,
                                **kwargs)

        # clean up so that another read of maps and model will read again (these
        # are shifted when map_model_manager is called)
        if isinstance(map_files, list):
            for file_name in map_files[:3]:
                if file_name and file_name in self.get_real_map_names():
                    self.remove_real_map(file_name)
        elif map_files:
            self.remove_real_map(map_files)
        if model_file:
            self.remove_model(model_file)

        return mmm
Example #21
0
    def __init__(self, model, fmodel, cc_min=0.8, molprobity_map_params=None):

        from iotbx.pdb.amino_acid_codes import one_letter_given_three_letter
        from mmtbx import real_space_correlation

        validation.__init__(self)

        pdb_hierarchy = model.get_hierarchy()
        crystal_symmetry = model.crystal_symmetry()

        # arrays for different components
        self.everything = list()
        self.protein = list()
        self.other = list()
        self.water = list()
        aa_codes = one_letter_given_three_letter

        # redo real_space_corelation.simple to use map objects instead of filenames
        self.overall_rsc = None
        rsc = None
        try:
            rsc_params = real_space_correlation.master_params().extract()
            rsc_params.detail = "residue"
            rsc_params.map_1.fill_missing_reflections = False
            rsc_params.map_2.fill_missing_reflections = False
            use_maps = False
            if (molprobity_map_params is not None):
                rsc_params.map_coefficients_file_name = \
                  molprobity_map_params.map_coefficients_file_name
                rsc_params.map_coefficients_label = \
                  molprobity_map_params.map_coefficients_label
                if (molprobity_map_params.map_file_name is not None):
                    use_maps = True
            # use mmtbx/command_line/map_model_cc.py for maps
            self.fsc = None
            if (use_maps):
                from iotbx.map_model_manager import map_model_manager
                from mmtbx.maps import map_model_cc
                #  XXX no longer exists from mmtbx.maps.import get_fsc
                from mmtbx.maps.mtriage import get_fsc  # XXX replaced above
                from iotbx.file_reader import any_file
                params = map_model_cc.master_params().extract()
                params.map_model_cc.resolution = molprobity_map_params.d_min
                map_object = any_file(
                    molprobity_map_params.map_file_name).file_object

                # check that model crystal symmetry matches map crystal symmetry
                mmi = map_model_manager(map_manager=map_object, model=model)

                rsc_object = map_model_cc.map_model_cc(
                    mmi.map_data(),
                    mmi.model().get_hierarchy(), mmi.crystal_symmetry(),
                    params.map_model_cc)
                rsc_object.validate()
                rsc_object.run()
                rsc = rsc_object.get_results()
                self.overall_rsc = (rsc.cc_mask, rsc.cc_volume, rsc.cc_peaks)

                self.fsc = get_fsc(mmi.map_data(), mmi.model(),
                                   params.map_model_cc)
                self.fsc.atom_radius = rsc.atom_radius
                rsc = rsc.cc_per_residue
            # mmtbx/real_space_correlation.py for X-ray/neutron data and map
            # coefficients
            else:
                self.overall_rsc, rsc = real_space_correlation.simple(
                    fmodel=fmodel,
                    pdb_hierarchy=pdb_hierarchy,
                    params=rsc_params,
                    log=null_out())
        except Exception as e:
            raise
        else:
            assert ((self.overall_rsc is not None) and (rsc is not None))
            for i, result_ in enumerate(rsc):
                if (use_maps
                    ):  # new rsc calculation (mmtbx/maps/model_map_cc.py)
                    result = residue_real_space(chain_id=result_.chain_id,
                                                resname=result_.resname,
                                                resseq=result_.resseq,
                                                icode=result_.icode,
                                                altloc="",
                                                score=result_.cc,
                                                b_iso=result_.b_iso_mean,
                                                occupancy=result_.occ_mean,
                                                outlier=result_.cc < cc_min,
                                                xyz=result_.xyz_mean)
                else:  # old rsc calculation (mmtbx/maps/real_space_correlation.py)
                    result = residue_real_space(
                        chain_id=result_.chain_id,
                        resname=result_.residue.resname,
                        resseq=result_.residue.resseq,
                        icode=result_.residue.icode,
                        altloc="",
                        score=result_.cc,
                        b_iso=result_.b,
                        occupancy=result_.occupancy,
                        fmodel=result_.map_value_1,
                        two_fofc=result_.map_value_2,
                        outlier=result_.cc < cc_min,
                        xyz=result_.residue.atoms().extract_xyz().mean())
                if result.is_outlier():
                    self.n_outliers += 1
                # XXX unlike other validation metrics, we always save the results for
                # the real-space correlation, since these are used as the basis for
                # the multi-criterion plot in Phenix.  The show() method will only
                # print outliers, however.
                if (result.resname != 'HOH'):  # water is handled by waters.py
                    self.everything.append(result)
                    if result.resname in one_letter_given_three_letter:
                        self.protein.append(result)
                    else:
                        self.other.append(result)
                self.everything += self.water
                self.results = self.protein
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')

    # Read in data

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

    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)

    mmm = map_model_manager(model=model,
                            map_manager_1=mm.deep_copy(),
                            map_manager_2=mm.deep_copy(),
                            ncs_object=ncs,
                            wrapping=False)
    mmm.add_map_manager_by_id(map_id='external_map',
                              map_manager=mmm.map_manager().deep_copy())
    mmm.set_resolution(3)
    mmm.set_log(sys.stdout)

    dc = mmm.deep_copy()

    # Model sharpening
    mmm = dc.deep_copy()
    tls_info = mmm.tls_from_map(
        n_bins=10,
        model_id='model',
        map_id='map_manager',
        iterations=1,
    )
    tlso = tls_info.tlso_list[0]
    print("t:", tlso.t)
    print("l:", tlso.l)
    print("s:", tlso.s)
    print("origin:", tlso.origin)

    assert approx_equal(
        tlso.t,
        (1.1665511122614693, 1.2026392186971397, 1.1654187623738737,
         -0.08474662045683597, -0.02260930304525043, 0.06492095346560478))
    assert approx_equal(tlso.l,
                        (-0.002162154945537812, -0.0023776908642138776,
                         0.0009748174775374614, -5.9732257180723945e-05,
                         -0.0001342760165428358, -9.055411066345411e-05))
    assert approx_equal(
        tlso.s,
        (3.409944886438518e-08, 6.0542707156228405e-09, -8.938076172958137e-09,
         4.8771411705994806e-09, -2.6247834187732072e-08,
         4.605012474599143e-09, 6.090471572948155e-10, 2.1790753409285795e-09,
         -7.851614684653208e-09))
    assert approx_equal(
        tlso.origin,
        (-64.70331931297399, -62.30573551948903, -63.743687240164604))

    print("TLS: ", tlso.t, tlso.l, tlso.s, tlso.origin)
Example #23
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
Example #24
0
def exercise(out=sys.stdout):

    # test shift_aware_rt

    mmm1, mmm2 = get_map_model_managers()
    initial_shift_aware_rt_info = mmm1.shift_aware_rt_to_superpose_other(mmm2)
    initial_rt_info = initial_shift_aware_rt_info.working_rt_info(
        from_obj=mmm2, to_obj=mmm1)

    model_2 = mmm2.model().apply_selection_string("resseq 222:235")
    mmm2.set_model(model_2)
    shift_aware_rt_info = mmm1.shift_aware_rt_to_superpose_other(mmm2)
    rt_info = shift_aware_rt_info.working_rt_info(from_obj=mmm2, to_obj=mmm1)
    assert shift_aware_rt_info.is_similar(initial_shift_aware_rt_info,
                                          tol=0.002)

    shift_aware_rt = mmm1.shift_aware_rt(working_rt_info=rt_info,
                                         from_obj=mmm2,
                                         to_obj=mmm1)

    shift_aware_rt = mmm1.map_manager().shift_aware_rt(working_rt_info=rt_info,
                                                       from_obj=mmm2,
                                                       to_obj=mmm1)
    print(mmm1, mmm2)
    sites_cart_2 = mmm2.model().get_sites_cart()
    mapped_sites_cart = shift_aware_rt.apply_rt(sites_cart=sites_cart_2,
                                                from_obj=mmm2,
                                                to_obj=mmm1)
    assert approx_equal(
        mapped_sites_cart,
        mmm1.model().apply_selection_string("resseq 222:235").get_sites_cart(),
        eps=0.01)
    working_rt_info = shift_aware_rt.working_rt_info(from_obj=mmm2,
                                                     to_obj=mmm1)
    mapped_sites_cart = working_rt_info.r.elems * mmm2.model().get_sites_cart(
    ) + working_rt_info.t.elems
    assert approx_equal(
        mapped_sites_cart,
        mmm1.model().apply_selection_string("resseq 222:235").get_sites_cart(),
        eps=0.01)

    inverse_shift_aware_rt = shift_aware_rt.inverse()
    mapped_sites_cart = inverse_shift_aware_rt.apply_rt(
        sites_cart=mmm1.model().apply_selection_string(
            "resseq 222:235").get_sites_cart(),
        from_obj=mmm1,
        to_obj=mmm2)
    assert approx_equal(mapped_sites_cart,
                        mmm2.model().get_sites_cart(),
                        eps=0.01)

    mmm1, mmm2 = get_map_model_managers()

    # get r,t to map mmm2 model on mmm1 model
    shift_aware_rt_info = mmm1.shift_aware_rt_to_superpose_other(mmm2)
    rt_info = shift_aware_rt_info.working_rt_info(from_obj=mmm2, to_obj=mmm1)
    print(rt_info)

    # get mmm2 map superimposed on mmm1 map (in region where it is defined, zero
    #   outside that region)

    new_mm = mmm1.superposed_map_manager_from_other(other=mmm2)
    new_mm.write_map('super.ccp4')
    mmm1.write_map('orig.ccp4')
    mmm1.write_model('orig.pdb')

    new_mm = mmm1.superposed_map_manager_from_other(
        other=mmm2, selection_string="resseq 221:225")
    assert approx_equal(new_mm.map_map_cc(mmm1.map_manager()),
                        0.994645868918,
                        eps=0.01)
    new_mm.write_map('super_221-225.ccp4')

    new_mm = mmm1.superposed_map_manager_from_other(other=mmm2,
                                                    working_rt_info=rt_info)
    assert approx_equal(new_mm.map_map_cc(mmm1.map_manager()),
                        0.994645868918,
                        eps=0.01)
    new_mm.write_map('super_221-225.ccp4')

    # get a local resolution map (this one should look pretty constant!)
    mmm1.set_resolution(3)
    mmma = mmm1.deep_copy()
    model = mmm1.model()
    mmma.remove_model_by_id('model')
    mmmb = mmma.deep_copy()

    mmma.map_manager().randomize(random_seed=23412,
                                 d_min=3,
                                 high_resolution_fourier_noise_fraction=10,
                                 low_resolution_noise_cutoff=5)
    mmmb.map_manager().randomize(random_seed=887241,
                                 d_min=3,
                                 high_resolution_fourier_noise_fraction=10,
                                 low_resolution_noise_cutoff=5)
    assert approx_equal(mmma.map_manager().map_map_cc(mmmb.map_manager()),
                        0.16, 0.10)
    from iotbx.map_model_manager import map_model_manager
    model.set_b_iso(flex.double(model.get_sites_cart().size(), 0))
    local_mmm = map_model_manager(map_manager_1=mmma.map_manager(),
                                  map_manager_2=mmmb.map_manager(),
                                  model=model)
    local_mmm.set_resolution(3)
    local_mmm.local_fsc()

    from iotbx.data_manager import DataManager
    dm = DataManager()
    dm.set_overwrite(True)

    cc_before = local_mmm.map_model_cc()
    print("Working with randomized maps cc = ", cc_before)
    dc = local_mmm.deep_copy()
    dc.set_log(sys.stdout)
    cc_before = dc.map_model_cc()
    dc.half_map_sharpen(n_bins=15)
    cc_after = dc.map_model_cc(map_id='map_manager_scaled')
    print("CC before, after half map sharpen: ", cc_before, cc_after)
    assert approx_equal((cc_before, cc_after), (0.80, 0.80), eps=0.10)

    dc = local_mmm.deep_copy()
    dc.set_log(sys.stdout)
    cc_before = dc.map_model_cc()
    dc.model_sharpen(n_bins=15,
                     local_sharpen=False,
                     anisotropic_sharpen=False,
                     optimize_b_eff=False)
    cc_after = dc.map_model_cc(map_id='map_manager_scaled')
    print("CC before, after std model sharpen: ", cc_before, cc_after)
    assert approx_equal((cc_before, cc_after), (0.80, 0.90), eps=0.10)
    model_sharpened_mm = dc.get_map_manager_by_id(map_id='map_manager_scaled')

    dc = local_mmm.deep_copy()
    dc.set_log(sys.stdout)
    cc_before = dc.map_model_cc()
    dc.model_sharpen(local_sharpen=True, n_boxes=1, n_bins=15)
    cc_after = dc.map_model_cc(map_id='map_manager_scaled')
    print("CC before, after local model sharpen n_boxes=1: ", cc_before,
          cc_after)
    assert approx_equal((cc_before, cc_after), (0.80, 0.90), eps=0.10)
    model_sharpened_mm = dc.get_map_manager_by_id(map_id='map_manager_scaled')

    dc = local_mmm.deep_copy()
    dc.set_log(sys.stdout)
    dc.add_map_manager_by_id(model_sharpened_mm, 'external_map')
    cc_before = dc.map_map_cc(map_id='map_manager',
                              other_map_id='external_map')
    dc.external_sharpen(n_bins=15, map_id_external_map='external_map')
    print(dc)
    cc_after = dc.map_map_cc(map_id='map_manager_scaled',
                             other_map_id='external_map')
    print("CC before, after external sharpen n_boxes=1: ", cc_before, cc_after)
    assert approx_equal((cc_before, cc_after), (0.7, 0.95), eps=0.10)

    dc = local_mmm.deep_copy()
    dc.set_log(sys.stdout)
    dc.add_map_manager_by_id(model_sharpened_mm, 'external_map')
    cc_before = dc.map_map_cc(map_id='map_manager',
                              other_map_id='external_map')
    dc.external_sharpen(local_sharpen=True,
                        n_boxes=1,
                        n_bins=15,
                        map_id_external_map='external_map')
    cc_after = dc.map_map_cc(map_id='map_manager_scaled',
                             other_map_id='external_map')
    print("CC before, after external sharpen local n_boxes=1: ", cc_before,
          cc_after)
    assert approx_equal((cc_before, cc_after), (0.70, 0.95), eps=0.10)

    dc = local_mmm.deep_copy()
    dc.set_log(sys.stdout)
    dc._local_sharpen(map_id_scaled_list=['map_manager_scaled'],
                      map_id_to_be_scaled_list=['map_manager'],
                      n_bins=15,
                      n_boxes=1)
    cc = dc.map_model_cc()
    assert approx_equal(cc, 0.80, eps=0.1)

    # create a mask around density
    dc.create_mask_around_density(soft_mask=False)
    count = dc.get_map_manager_by_id('mask').map_data().count(1)
    print(count)
    assert 8000 < count < 14000
    dc.expand_mask(buffer_radius=2)
    count = dc.get_map_manager_by_id('mask').map_data().count(1)
    print(count)
    assert count == 1

    # Test mask and map info functions
    mmm1, mmm2 = get_map_model_managers()
    mmm1.create_mask_around_density(soft_mask=False)
    mask_info = mmm1.mask_info()
    map_info = mmm1.map_info()
    mask_info_by_id = mmm1.mask_info(mask_id='mask')
    map_info_by_id = mmm1.map_info(map_id='map_manager')
    assert mask_info() == mask_info_by_id()
    assert map_info() == map_info_by_id()
    assert approx_equal(mask_info.fraction_marked, 0.210091991342)
    assert approx_equal(map_info.fraction_above_sigma_cutoff, 0.0577876984127)

    # create a spherical mask around a point
    print("Spherical masks", )
    dc = mmm1.deep_copy()
    dc.mask_info()
    assert dc.mask_info().marked_points == 9318
    dc.create_spherical_mask()
    dc.mask_info()
    assert dc.mask_info().marked_points == 1286
    dc.create_spherical_mask(soft_mask_radius=1)
    dc.mask_info()
    assert dc.mask_info().marked_points == 8990
    dc.create_spherical_mask(soft_mask=False)
    dc.mask_info()
    assert dc.mask_info().marked_points == 1458
    dc.create_spherical_mask(mask_radius=4)
    dc.mask_info()
    assert dc.mask_info().marked_points == 914
    dc.create_spherical_mask(soft_mask=False, mask_radius=4)
    dc.mask_info()
    assert dc.mask_info().marked_points == 654
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 #26
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')

  # Read in data

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

  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)

  mmm=map_model_manager(
    model = model,
    map_manager_1 = mm.deep_copy(),
    map_manager_2 = mm.deep_copy(),
    ncs_object = ncs,
    wrapping = False)
  mmm.add_map_manager_by_id(
     map_id='external_map',map_manager=mmm.map_manager().deep_copy())
  mmm.set_resolution(3)
  mmm.set_log(sys.stdout)

  dc = mmm.deep_copy()


  # Model sharpening
  mmm = dc.deep_copy()
  tls_info=mmm.tls_from_map(n_bins=10,
    model_id = 'model',
    map_id = 'map_manager',
    iterations = 1,
   )
  tlso = tls_info.tlso_list[0]
  print ("t:", tlso.t)
  print ("l:", tlso.l)
  print ("s:", tlso.s)
  print ("origin:", tlso.origin)

  assert approx_equal(tlso.t,
(0.7920199173476214, 0.742281514408794, 0.7103008342583756, -0.05199687072329786, 0.04301326317889638, -0.0032498605215769945))
  assert approx_equal(tlso.l,
(-0.0004103603606922303, -0.00042929108338180655, 0.0001519656028327732, -2.8489076942333132e-06, -6.23198622708519e-05, 3.694504506269563e-05))
  assert approx_equal(tlso.s,
(5.562000065270741e-09, -5.108813278707348e-10, -4.132731326301999e-09, 7.925572910527853e-10, -2.7018794222798323e-09, 1.0742616538181975e-09, 7.501166703517675e-10, -1.0661760561475498e-09, -2.860120638319051e-09))
  assert approx_equal(tlso.origin,
(-64.703319312974, -62.30575036040377, -63.74368724016462))


  print("TLS: ",tlso.t,tlso.l,tlso.s,tlso.origin)
Example #27
0
def run(args, out=None, verbose=True):
  t0 = time.time()
  if (out is None) : out = sys.stdout
  from iotbx import file_reader
  import iotbx.phil
  cmdline = iotbx.phil.process_command_line_with_files(
    args=args,
    master_phil=master_phil,
    pdb_file_def="model",
    reflection_file_def="map_coeffs",
    map_file_def="map_file",
    cif_file_def="cif_file",
    usage_string="""\
mmtbx.ringer model.pdb map_coeffs.mtz [cif_file ...] [options]

%s
""" % __doc__)
  cmdline.work.show()
  params = cmdline.work.extract()
  validate_params(params)
  pdb_in = file_reader.any_file(params.model, force_type="pdb")
  pdb_in.check_file_type("pdb")

  pdb_inp = iotbx.pdb.input(file_name=params.model)
  model = mmtbx.model.manager(
    model_input      = pdb_inp)
  crystal_symmetry_model = model.crystal_symmetry()
  if crystal_symmetry_model is not None:
    crystal_symmetry_model.show_summary()

  hierarchy = model.get_hierarchy()
  map_coeffs = map_inp = difference_map_coeffs = None
  map_data, unit_cell = None, None
  # get miller array if map coefficients are provided
  if (params.map_coeffs is not None):
    mtz_in = file_reader.any_file(params.map_coeffs, force_type="hkl")
    mtz_in.check_file_type("hkl")
    best_guess = None
    best_labels = []
    all_labels = []
    for array in mtz_in.file_server.miller_arrays :
      if (array.is_complex_array()):
        labels = array.info().label_string()
        if (labels == params.map_label):
          map_coeffs = array
        elif (labels == params.difference_map_label):
          difference_map_coeffs = array
        else :
          if (params.map_label is None):
            all_labels.append(labels)
            if (labels.startswith("2FOFCWT") or labels.startswith("2mFoDFc") or
                labels.startswith("FWT")):
              best_guess = array
              best_labels.append(labels)
          if (params.difference_map_label is None):
            if (labels.startswith("FOFCWT") or labels.startswith("DELFWT")):
              difference_map_coeffs = array
    if (map_coeffs is None):
      if (len(all_labels) == 0):
        raise Sorry("No valid (pre-weighted) map coefficients found in file.")
      elif (best_guess is None):
        raise Sorry("Couldn't automatically determine appropriate map labels. "+
          "Choices:\n  %s" % "  \n".join(all_labels))
      elif (len(best_labels) > 1):
        raise Sorry("Multiple appropriate map coefficients found in file. "+
          "Choices:\n  %s" % "\n  ".join(best_labels))
      map_coeffs = best_guess
      print("  Guessing %s for input map coefficients" % best_labels[0], file=out)
  # get map_inp object and do sanity checks if map is provided
  else :
    ccp4_map_in = file_reader.any_file(params.map_file, force_type="ccp4_map")
    ccp4_map_in.check_file_type("ccp4_map")
    map_inp = ccp4_map_in.file_object
    base = map_model_manager(
      map_manager               = map_inp,
      model            = model,
      ignore_symmetry_conflicts = params.ignore_symmetry_conflicts)
    cs_consensus = base.crystal_symmetry()
    hierarchy = base.model().get_hierarchy()
    map_data = base.map_data()
    unit_cell = map_inp.grid_unit_cell()

  hierarchy.atoms().reset_i_seq()

  make_header("Iterating over residues", out=out)
  t1 = time.time()
  results = iterate_over_residues(
    pdb_hierarchy=hierarchy,
    map_coeffs=map_coeffs,
    difference_map_coeffs=difference_map_coeffs,
    map_data  = map_data,
    unit_cell = unit_cell,
    params=params,
    log=out).results
  t2 = time.time()
  if (verbose):
    print("Time excluding I/O: %8.1fs" % (t2 - t1), file=out)
    print("Overall runtime:    %8.1fs" % (t2 - t0), file=out)
  if (params.output_base is None):
    pdb_base = os.path.basename(params.model)
    params.output_base = os.path.splitext(pdb_base)[0] + "_ringer"
  easy_pickle.dump("%s.pkl" % params.output_base, results)
  print("Wrote %s.pkl" % params.output_base, file=out)
  csv = "\n".join([ r.format_csv() for r in results ])
  open("%s.csv" % params.output_base, "w").write(csv)
  print("Wrote %s.csv" % params.output_base, file=out)
  print("\nReference:", file=out)
  print("""\
  Lang PT, Ng HL, Fraser JS, Corn JE, Echols N, Sales M, Holton JM, Alber T.
  Automated electron-density sampling reveals widespread conformational
  polymorphism in proteins. Protein Sci. 2010 Jul;19(7):1420-31. PubMed PMID:
  20499387""", file=out)
  if (params.gui):
    run_app(results)
  else :
    return results
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_model.pdb')
    data_ncs_spec = os.path.join(data_dir, 'data',
                                 'non_zero_origin_ncs_spec.ncs_spec')

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

    # Read in map and model

    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)

    mmm = map_model_manager()
    mmm.add_map_manager(mm)
    mmm.add_model(model)
    mmm.add_ncs_object(ncs)

    original_ncs = mmm.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(mmm._map_manager.origin_shift_grid_units) == (0, 0, 0)

    # Shift origin to (0,0,0)
    mmm.shift_origin()
    assert tuple(mmm._map_manager.origin_shift_grid_units) == (100, 100, 100)

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

    shifted_ncs = mmm.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

    model = mmm.model()
    shifted_model = mmm.shift_model_to_match_working_map(model=model)
    model_in_original_position = mmm.shift_model_to_match_original_map(
        model=shifted_model)
    assert (not approx_equal(
        model.get_sites_cart(), shifted_model.get_sites_cart(), out=None))
    assert approx_equal(model.get_sites_cart(),
                        model_in_original_position.get_sites_cart())

    # Generate a map and model

    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.0195, eps=0.001)
Example #29
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')

    # Read in data

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

    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)

    mmm = map_model_manager(model=model,
                            map_manager_1=mm.deep_copy(),
                            map_manager_2=mm.deep_copy(),
                            ncs_object=ncs,
                            wrapping=False)
    mmm.add_map_manager_by_id(map_id='external_map',
                              map_manager=mmm.map_manager().deep_copy())
    mmm.set_resolution(3)
    mmm.set_log(sys.stdout)

    dc = mmm.deep_copy()

    # Model sharpening
    mmm = dc.deep_copy()
    tls_info = mmm.tls_from_map(
        n_bins=10,
        model_id='model',
        map_id='map_manager',
        iterations=1,
    )
    tlso = tls_info.tlso_list[0]
    print("t:", tlso.t)
    print("l:", tlso.l)
    print("s:", tlso.s)
    print("origin:", tlso.origin)

    assert approx_equal(
        tlso.t,
        (1.180418902258779, 1.1747521845606608, 1.178996799712174,
         -0.08474662674769494, -0.022609295693646402, 0.0649209491344932))
    assert approx_equal(
        tlso.l,
        (-0.002159404807991249, -0.002107964765763024, 0.0008301439376854558,
         -5.973347993775719e-05, -0.000134276871934738, -9.05515898670584e-05))
    assert approx_equal(
        tlso.s,
        (2.9348223335616302e-08, 5.52441087256425e-09, -5.382459681103171e-09,
         4.3530347434547015e-09, -2.3559464233595e-08, 4.217968590464982e-09,
         -4.380707049750269e-09, 1.9232725033868253e-09,
         -5.788759082799497e-09))
    assert approx_equal(
        tlso.origin,
        (-64.70331931297399, -62.30573551948903, -63.743687240164604))

    print("TLS: ", tlso.t, tlso.l, tlso.s, tlso.origin)
Example #30
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')

    # Read in data

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

    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)

    mmm = map_model_manager(model=model,
                            map_manager_1=mm.deep_copy(),
                            map_manager_2=mm.deep_copy(),
                            ncs_object=ncs,
                            wrapping=False)
    mmm.add_map_manager_by_id(map_id='external_map',
                              map_manager=mmm.map_manager().deep_copy())
    mmm.set_resolution(3)
    mmm.set_log(sys.stdout)

    dc = mmm.deep_copy()

    # Model sharpening
    mmm = dc.deep_copy()
    tls_info = mmm.tls_from_map(
        n_bins=10,
        model_id='model',
        map_id='map_manager',
        iterations=1,
    )
    tlso = tls_info.tlso_list[0]
    print("t:", tlso.t)
    print("l:", tlso.l)
    print("s:", tlso.s)
    print("origin:", tlso.origin)
    assert approx_equal(
        tlso.t,
        (0.6518723599417712, 0.6807846368236251, 0.6161941485135081,
         -0.04791178588048965, -0.0014794039180157132, 0.032774655367019095))
    assert approx_equal(tlso.l,
                        (-0.00020092054127279798, -9.557441568256003e-05,
                         -1.711699526358822e-05, -4.8893794663274e-05,
                         -2.026091444762368e-05, -2.194054393244713e-05))
    assert approx_equal(
        tlso.s,
        (1.3393493443427932e-09, 3.660975429526433e-10, -6.07051859483934e-10,
         4.1665859321329886e-10, -1.35856164931005e-09, 5.409502867516901e-10,
         -5.4005830782848e-10, -1.2017586805521653e-09,
         1.9212302485258283e-11))
    assert approx_equal(
        tlso.origin,
        (-64.70331931297407, -62.305747062422725, -63.74368724016457))

    print("TLS: ", tlso.t, tlso.l, tlso.s, tlso.origin)