Example #1
0
def compare_masks(struc, opts):
  tolerance = opts.tolerance
  resolution = opts.resolution
  solvent_radius = opts.solvent_radius
  shrink_radius = opts.shrink_radius
  verbose = opts.verbose
  cout.truncate(0)
  time_p1 = 0.0
  time_asu = 0.0
  time_orig = 0.0
  params = masks.mask_master_params.extract()
  params.ignore_hydrogens = False
  params.ignore_zero_occupancy_atoms = False
  params.solvent_radius = solvent_radius
  params.shrink_truncation_radius = shrink_radius
  fc = struc.structure_factors(d_min = resolution).f_calc()
  while fc.data().size() <= 3 :
    resolution /= 1.2
    assert resolution > 1.0E-3
    fc = struc.structure_factors( d_min = resolution).f_calc()
  print >>cout, "Resolution= ", resolution, "  solvent radius= ", \
      solvent_radius, "  shrink radius= ", shrink_radius,  "  Tolerance= ", \
      tolerance, "  Number of reflection= ", fc.data().size()
  struc.show_summary(cout)
  group = struc.space_group()
  print >>cout, "Cell volume= ", struc.unit_cell().volume(), \
    "  Group order= ", group.order_z(), " p= ", group.order_p()
  print >>cout, "Hall  symbol: ", group.type().hall_symbol()
  #check_group(group)

  tb = time.time()
  asu_mask = masks.atom_mask(
      unit_cell = struc.unit_cell(),
      group = struc.space_group(),
      resolution = fc.d_min(),
      grid_step_factor = params.grid_step_factor,
      solvent_radius = params.solvent_radius,
      shrink_truncation_radius = params.shrink_truncation_radius )
  te = time.time()
  time_asu += (te-tb)
  grid =  asu_mask.grid_size()
  print >>cout, "asu mask grid = ", grid
  zero_test(asu_mask, fc, tolerance = tolerance)
  radii = get_radii(struc)
  assert len(radii) == len(struc.sites_frac())
  tb = time.time()
  asu_mask.compute( struc.sites_frac(), radii )
  te = time.time()
  time_asu += (te-tb)
  print >>cout, "   n asu atoms= ", asu_mask.n_asu_atoms(), \
      "   has-enclosed= ", asu_mask.debug_has_enclosed_box
  tb = time.time()
  fm_asu = asu_mask.structure_factors( fc.indices() )
  fm_asu = fc.set().array( data = fm_asu )
  te = time.time()
  time_asu_sf = te-tb
  time_asu += (te-tb)
  # save files
  if not opts.save_files is None:
    tmp_file = open(opts.save_files + ".pdb", "w")
    print >>tmp_file, struc.as_pdb_file()
    tmp_file.close()
    asu_mask.xplor_write_map(opts.save_files + "_mask.map")
    asu_mask.xplor_write_map(opts.save_files + "_inverted_mask.map", 1, True)
    # also save structure factors
    import iotbx.mtz
    mtzo = iotbx.mtz.object()
    mtzo.set_title("mask test")
    mtzo.add_history(line="start")
    mtzo.set_space_group_info(fm_asu.space_group_info())
    mtzo.set_hkl_base(fm_asu.unit_cell())
    crystal = mtzo.add_crystal(
      name="mask_test_crystal",
      project_name="mask_test_project",
      unit_cell=fm_asu.unit_cell())
    dataset = crystal.add_dataset(
      name="mask_test_dataset",
      wavelength=1)
    assert dataset.add_miller_array(
      miller_array=fm_asu,
      column_root_label="F",
      #column_types=column_types
      ) is dataset
    mtzo.add_history(line="done")
    mtzo.write(opts.save_files + "_sf.mtz")
  #
  # ========= old mask =============
  #
  tb = time.time()
  struc_p1 = struc.expand_to_p1()
  te = time.time()
  time_p1_exp = (te-tb)
  time_p1 += (te-tb)
  fc_p1 = fc.deep_copy()
  fc_p1 = fc_p1.customized_copy(crystal_symmetry = struc_p1.crystal_symmetry())
  tb = time.time()
  blk_p1 = masks.bulk_solvent(
    xray_structure = struc_p1,
    gridding_n_real = grid,
    ignore_zero_occupancy_atoms = params.ignore_zero_occupancy_atoms,
    ignore_hydrogen_atoms = params.ignore_hydrogens,
    solvent_radius = params.solvent_radius,
    shrink_truncation_radius = params.shrink_truncation_radius)
  te = time.time()
  time_p1_msk = (te-tb)
  time_p1 += (te-tb)
  tb = time.time()
  fm_p1 = blk_p1.structure_factors( miller_set = fc_p1 )
  te = time.time()
  time_p1_sf = (te-tb)
  time_p1 += (te-tb)
  blk_p1.show_summary(cout)
  ### original mask
  tb = time.time()
  blk_o = masks.bulk_solvent(
    xray_structure = struc,
    gridding_n_real = grid,
    ignore_zero_occupancy_atoms = params.ignore_zero_occupancy_atoms,
    ignore_hydrogen_atoms = params.ignore_hydrogens,
    solvent_radius = params.solvent_radius,
    shrink_truncation_radius = params.shrink_truncation_radius)
  te = time.time()
  time_orig_msk = (te-tb)
  time_orig += (te-tb)
  tb = time.time()
  fm_o = blk_o.structure_factors( miller_set = fc )
  te = time.time()
  time_orig_sf = (te-tb)
  time_orig += (te-tb)
  print >>cout, "Number of reflections ::: Fm asu = ", fm_asu.data().size(), \
    "Fm P1 = ", fm_p1.data().size()
  print >>cout, "Time ( ms )    P1= ", time_p1*1000.0, "   orig= ", \
      time_orig*1000.0, "    asu= ", time_asu*1000.0
  print >>cout, "Times ( ms ) mask_asu= ", asu_mask.debug_mask_asu_time, \
      " atoms_to_asu= ", asu_mask.debug_atoms_to_asu_time, \
      " accessible= ", asu_mask.debug_accessible_time, \
      " contact= ", asu_mask.debug_contact_time, \
      " Fc= ", time_asu_sf*1000.0, \
      " fft= ", asu_mask.debug_fft_time
  print >>cout, "Times ( ms ) orig:  mask= ", time_orig_msk*1000.0, "  Fc=", \
      time_orig_sf*1000.0
  print >>cout, "Times ( ms ) p1 :  expand= ", time_p1_exp*1000.0, "  mask= ", \
      time_p1_msk*1000.0, "  Fc=", time_p1_sf*1000.0
  assert fm_asu.data().size() == fm_o.data().size()
  t_v1 = asu_mask.contact_surface_fraction
  t_v2 = blk_p1.contact_surface_fraction
  t_v3 = max( abs(t_v1), abs(t_v2) )
  if t_v3 > 1.0E-6:
    t_v4 = abs( t_v1 - t_v2 ) / t_v3
  else:
    t_v4 = 0.0
  if( t_v4>1.0E-6 ):
    if not opts.failed_file is None:
      tmp_file = open(opts.failed_file, "w")
      print >>tmp_file, struc.as_pdb_file()
      tmp_file.close()
    raise "Not equal solvent volume"

  assert approx_equal(
    asu_mask.contact_surface_fraction, blk_p1.contact_surface_fraction)
  assert approx_equal(
    asu_mask.accessible_surface_fraction, blk_p1.accessible_surface_fraction)
  assert is_below_limit(
    value=asu_mask.accessible_surface_fraction,
    limit=asu_mask.contact_surface_fraction)
  n_compared = compare_fc(fm_asu, fm_p1, tolerance = tolerance)
  assert n_compared == fm_asu.data().size(), \
    "N compared refls: "+str(n_compared) + " != " + str(fm_asu.data().size())
  assert n_compared >0
  if verbose:
    print cout.getvalue()
  # test that second calculation will produce the same results
  asu_mask.compute( struc.sites_frac(), radii )
  fm_asu2 = asu_mask.structure_factors( fc.indices() )
  fm_asu2 = fc.set().array( data = fm_asu2 )
  n_compared = compare_fc(fm_asu, fm_asu2, tolerance = tolerance)
  assert n_compared == fm_asu.data().size(), \
    "N compared refls: "+str(n_compared) + " != " + str(fm_asu.data().size())
  cout.truncate(0)
Example #2
0
def get_f_mask(xrs, ma, step, option=2, r_shrink=None, r_sol=None):
    #
    result = ma.deep_copy()
    sel = ma.d_spacings().data() >= 3
    ma = ma.select(sel)
    #
    crystal_gridding = maptbx.crystal_gridding(
        unit_cell=xrs.unit_cell(),
        space_group_info=xrs.space_group_info(),
        symmetry_flags=maptbx.use_space_group_symmetry,
        step=step)
    n_real = crystal_gridding.n_real()
    atom_radii = vdw_radii_from_xray_structure(xray_structure=xrs)
    mask_params = masks.mask_master_params.extract()
    grid_step_factor = ma.d_min() / step
    if (r_shrink is not None): mask_params.shrink_truncation_radius = r_shrink
    if (r_sol is not None): mask_params.solvent_radius = r_sol
    mask_params.grid_step_factor = grid_step_factor
    # 1
    if (option == 1):
        asu_mask = ext.atom_mask(
            unit_cell=xrs.unit_cell(),
            group=xrs.space_group(),
            resolution=ma.d_min(),
            grid_step_factor=grid_step_factor,
            solvent_radius=mask_params.solvent_radius,
            shrink_truncation_radius=mask_params.shrink_truncation_radius)
        asu_mask.compute(xrs.sites_frac(), atom_radii)
        fm_asu = asu_mask.structure_factors(ma.indices())
        f_mask = ma.set().array(data=fm_asu)
    # 2
    elif (option == 2):
        asu_mask = ext.atom_mask(
            unit_cell=xrs.unit_cell(),
            space_group=xrs.space_group(),
            gridding_n_real=n_real,
            solvent_radius=mask_params.solvent_radius,
            shrink_truncation_radius=mask_params.shrink_truncation_radius)
        asu_mask.compute(xrs.sites_frac(), atom_radii)
        fm_asu = asu_mask.structure_factors(ma.indices())
        f_mask = ma.set().array(data=fm_asu)
    # 3
    elif (option == 3):
        mask_p1 = mmtbx.masks.mask_from_xray_structure(
            xray_structure=xrs,
            p1=True,
            for_structure_factors=True,
            solvent_radius=mask_params.solvent_radius,
            shrink_truncation_radius=mask_params.shrink_truncation_radius,
            n_real=n_real,
            in_asu=False).mask_data
        maptbx.unpad_in_place(map=mask_p1)
        mask = asu_map_ext.asymmetric_map(
            xrs.crystal_symmetry().space_group().type(), mask_p1).data()
        f_mask = ma.structure_factors_from_asu_map(asu_map_data=mask,
                                                   n_real=n_real)
    # 4
    elif (option == 4):
        f_mask = masks.bulk_solvent(
            xray_structure=xrs,
            ignore_zero_occupancy_atoms=False,
            solvent_radius=mask_params.solvent_radius,
            shrink_truncation_radius=mask_params.shrink_truncation_radius,
            ignore_hydrogen_atoms=False,
            grid_step=step,
            atom_radii=atom_radii).structure_factors(miller_set=ma)
    elif (option == 5):
        o = mmtbx.masks.bulk_solvent(
            xray_structure=xrs,
            ignore_zero_occupancy_atoms=False,
            solvent_radius=mask_params.solvent_radius,
            shrink_truncation_radius=mask_params.shrink_truncation_radius,
            ignore_hydrogen_atoms=False,
            gridding_n_real=n_real,
            atom_radii=atom_radii)
        assert approx_equal(n_real, o.data.accessor().all())
        f_mask = o.structure_factors(ma)
    elif (option == 6):
        # XXX No control over n_real, so results with others don't match
        mask_manager = masks.manager(miller_array=ma,
                                     miller_array_twin=None,
                                     mask_params=mask_params)
        f_mask = mask_manager.shell_f_masks(xray_structure=xrs,
                                            force_update=True)[0]
    else:
        assert 0
    #
    data = flex.complex_double(result.indices().size(), 0)
    data = data.set_selected(sel, f_mask.data())
    result = result.array(data=data)
    return result