Esempio n. 1
0
def randomize_completeness2(map_coeffs, crystal_gridding, n_cycles=10):
    map_filter = None
    from mmtbx.maps import fem
    for cycle in xrange(n_cycles):
        if (cycle > 0):
            mc = map_coeffs.select(flex.random_bool(map_coeffs.size(), 0.99))
        else:
            mc = map_coeffs.deep_copy()
        m = get_map(map_coeffs=mc, crystal_gridding=crystal_gridding)
        maptbx.reset(data=m,
                     substitute_value=0.0,
                     less_than_threshold=0.5,
                     greater_than_threshold=-9999,
                     use_and=True)
        m = maptbx.volume_scale(map=m, n_bins=10000).map_data()
        if (map_filter is not None):
            map_filter = maptbx.volume_scale(map=map_filter,
                                             n_bins=10000).map_data()
        map_filter = fem.intersection(m1=map_filter,
                                      m2=m,
                                      thresholds=flex.double(
                                          [i / 100. for i in range(0, 55, 5)]),
                                      average=True)
    #map_filter = map_filter.set_selected(map_filter< 0.5, 0)
    #map_filter = map_filter.set_selected(map_filter>=0.5, 1)
    return map_filter
Esempio n. 2
0
def run(args):
    show_citation()
    if (len(args) != 1):
        raise Sorry("Need to provide CCP4 formatted map file.")
    # map
    try:
        ccp4_map = iotbx.mrcfile.map_reader(file_name=args[0])
    except Exception:  # XXX should probably be RuntimeError?
        raise Sorry("Not a valid file (provide CCP4 formatted map file).")
    cs = crystal.symmetry(ccp4_map.unit_cell().parameters(),
                          ccp4_map.space_group_number)
    m = ccp4_map.data.as_double()
    # show general statistics
    show_overall_statistics(m=m, header="Map basic info (%s):" % args[0])
    # HE
    m_he = maptbx.volume_scale(map=m, n_bins=10000).map_data()
    show_overall_statistics(m=m_he, header="Rank-scaled (HE) map info:")
    #
    iotbx.mrcfile.write_ccp4_map(
        file_name=args[0] + "_rank_scaled.ccp4",
        unit_cell=cs.unit_cell(),
        space_group=cs.space_group(),
        #gridding_first=(0,0,0),# This causes a bug (map gets shifted)
        #gridding_last=n_real,  # This causes a bug (map gets shifted)
        map_data=m_he,
        labels=flex.std_string([""]))
Esempio n. 3
0
def run(args):
    show_citation()
    if len(args) != 1:
        raise Sorry("Need to provide CCP4 formatted map file.")
    # map
    try:
        ccp4_map = iotbx.ccp4_map.map_reader(file_name=args[0])
    except Exception:  # XXX should probably be RuntimeError?
        raise Sorry("Not a valid file (provide CCP4 formatted map file).")
    cs = crystal.symmetry(ccp4_map.unit_cell().parameters(), ccp4_map.space_group_number)
    m = ccp4_map.data.as_double()
    # show general statistics
    show_overall_statistics(m=m, header="Map basic info (%s):" % args[0])
    # HE
    m_he = maptbx.volume_scale(map=m, n_bins=10000).map_data()
    show_overall_statistics(m=m_he, header="Rank-scaled (HE) map info:")
    #
    iotbx.ccp4_map.write_ccp4_map(
        file_name=args[0] + "_rank_scaled.ccp4",
        unit_cell=cs.unit_cell(),
        space_group=cs.space_group(),
        # gridding_first=(0,0,0),# This causes a bug (map gets shifted)
        # gridding_last=n_real,  # This causes a bug (map gets shifted)
        map_data=m_he,
        labels=flex.std_string([""]),
    )
Esempio n. 4
0
def exercise_cc_peak():
  def get_map():
    av = [random.random() for i in xrange(10*20*30)]
    m = flex.double(av)
    m = m-flex.min(m)
    m = m/flex.max(m)
    m.resize(flex.grid((10,20,30)))
    return m
  m1 = get_map()
  m2 = get_map()
  for t in range(0,11):
    t=t/10.
    ccp=maptbx.cc_peak(map_1=m1, map_2=m2, cutoff=t)
  #
  sites_frac = flex.vec3_double([
    (0.50,0.50,0.50)])
  from cctbx import xray
  xray_structure = xray.structure(
    crystal_symmetry=crystal.symmetry(
      unit_cell=(5,5,5,90,90,90),
      space_group_symbol="P1"),
    scatterers=flex.xray_scatterer([
      xray.scatterer(label=str(i), scattering_type="C", site=site_frac)
        for i,site_frac in enumerate(sites_frac)]))
  fc1 = xray_structure.structure_factors(d_min=1.6).f_calc()
  fc2 = xray_structure.structure_factors(d_min=1.7).f_calc()
  for t in range(0,11):
    t=t/10.
    ccp=maptbx.cc_peak(map_coeffs_1=fc1, map_coeffs_2=fc2, cutoff=t)
  #
  m1_he = maptbx.volume_scale(map = m1,  n_bins = 10000).map_data()
  m2_he = maptbx.volume_scale(map = m2,  n_bins = 10000).map_data()
  cutoffs = flex.double([i/20. for i in range(1,20)])
  df = maptbx.discrepancy_function(map_1=m1_he, map_2=m2_he, cutoffs=cutoffs)
  #
  fc1 = xray_structure.structure_factors(d_min=2.2).f_calc()
  fc2 = xray_structure.structure_factors(d_min=2.2).f_calc()
  for t in range(0,10):
    t=t/10.
    ccp=maptbx.cc_peak(map_coeffs_1=fc1, map_coeffs_2=fc2, cutoff=t)
    assert approx_equal(ccp, 1)
  # 1D case
  m1_he_1d = maptbx.volume_scale_1d(map = m1.as_1d(),  n_bins = 10000).map_data()
  m2_he_1d = maptbx.volume_scale_1d(map = m2.as_1d(),  n_bins = 10000).map_data()
  df_1d = maptbx.discrepancy_function(
    map_1=m1_he_1d, map_2=m2_he_1d, cutoffs=cutoffs)
  assert approx_equal(df, df_1d)
def get_map_from_map(map_file_object, params, xrs, log):
  print("Processing input CCP4 map file...", file=log)
  map_data = map_file_object.file_content.map_data()
  try:
    # map_cs = map_content.file_object.crystal_symmetry()
    map_cs = map_file_object.crystal_symmetry()
  except NotImplementedError as e:
    pass
  print("Input map min,max,mean: %7.3f %7.3f %7.3f"%\
      map_data.as_1d().min_max_mean().as_tuple(), file=log)
  if map_cs.space_group().type().number() not in [0,1]:
    print(map_cs.space_group().type().number())
    raise Sorry("Only P1 group for maps is supported.")
  map_data = map_data - flex.mean(map_data)
  sd = map_data.sample_standard_deviation()
  map_data = map_data/sd
  print("Rescaled map min,max,mean: %7.3f %7.3f %7.3f"%\
    map_data.as_1d().min_max_mean().as_tuple(), file=log)
  map_file_object.file_content.show_summary(prefix="  ")
  shift_manager = mmtbx.utils.extract_box_around_model_and_map(
      xray_structure = xrs,
      map_data       = map_data.deep_copy(),
      box_cushion    = 5)
  sys.stdout.flush()
  xray_structure = shift_manager.xray_structure_box
  crystal_symmetry = xray_structure.crystal_symmetry()
  map_data = shift_manager.map_box

  if params.mask_and_he_map:
    print("Masking and histogram equalizing...", file=log)
    import boost.python
    cctbx_maptbx_ext = boost.python.import_ext("cctbx_maptbx_ext")
    xrs_p1 = xray_structure.expand_to_p1(sites_mod_positive=True)
    radii = flex.double(xrs_p1.scatterers().size(), 5.0)
    mask = cctbx_maptbx_ext.mask(
      sites_frac                  = xrs_p1.sites_frac(),
      unit_cell                   = xrs_p1.unit_cell(),
      n_real                      = map_data.all(),
      mask_value_inside_molecule  = 1,
      mask_value_outside_molecule = 0,
      radii                       = radii)
    map_data = mask*map_data
    from phenix.command_line.real_space_refine import write_ccp4_map
    write_ccp4_map(o=xray_structure.crystal_symmetry(), file_name="junk_mask.map",
     map_data=mask)
    del mask
    map_data = maptbx.volume_scale(map = map_data, n_bins = 10000).map_data()
    write_ccp4_map(o=xray_structure.crystal_symmetry(), file_name="junk_map.map",
     map_data=map_data)
  return map_data, map_cs, shift_manager
Esempio n. 6
0
def randomize_completeness2(map_coeffs, crystal_gridding, n_cycles=10):
  map_filter = None
  from mmtbx.maps import fem
  for cycle in xrange(n_cycles):
    if(cycle>0):
      mc = map_coeffs.select(flex.random_bool(map_coeffs.size(), 0.99))
    else:
      mc = map_coeffs.deep_copy()
    m = get_map(map_coeffs=mc, crystal_gridding=crystal_gridding)
    maptbx.reset(
      data=m,
      substitute_value=0.0,
      less_than_threshold=0.5,
      greater_than_threshold=-9999,
      use_and=True)
    m  = maptbx.volume_scale(map = m,  n_bins = 10000).map_data()
    if(map_filter is not None):
      map_filter  = maptbx.volume_scale(map = map_filter,  n_bins = 10000).map_data()
    map_filter = fem.intersection(m1=map_filter, m2=m,
      thresholds=flex.double([i/100. for i in range(0,55,5)]),
      average=True)
  #map_filter = map_filter.set_selected(map_filter< 0.5, 0)
  #map_filter = map_filter.set_selected(map_filter>=0.5, 1)
  return map_filter
Esempio n. 7
0
def run(args):
    show_citation()
    if (len(args) != 1):
        raise Sorry("Need to provide CCP4 formatted map file.")
    # map
    dm = DataManager()
    dm.set_overwrite(True)
    map_manager = dm.get_real_map(args[0])
    map_manager.shift_origin()

    cs = map_manager.crystal_symmetry()
    m = map_manager.map_data().as_double()
    # show general statistics
    show_overall_statistics(m=m, header="Map basic info (%s):" % args[0])
    # HE
    m_he = maptbx.volume_scale(map=m, n_bins=10000).map_data()
    show_overall_statistics(m=m_he, header="Rank-scaled (HE) map info:")
    #
    file_name = args[0] + "_rank_scaled.ccp4"
    he_map_manager = map_manager.customized_copy(map_data=m_he)
    he_map_manager.add_label("Histogram-equalized map")
    dm.write_real_map_file(he_map_manager, file_name)
Esempio n. 8
0
    def __init__(self,
                 f_obs,
                 r_free_flags,
                 xray_structure,
                 use_resolve,
                 use_omit,
                 use_max_map,
                 sharp,
                 use_unsharp_masking,
                 resolution_factor,
                 n_inner_loop=10,
                 n_outer_loop=10,
                 log=None):
        adopt_init_args(self, locals())
        if (self.log is None): self.log = sys.stdout
        print("Start FEM...", file=self.log)
        self.prepare_f_obs_and_flags()
        self.mc_orig = self.compute_original_map()
        self.b_overall = None
        print("Create un-sharpened fmodel...", file=self.log)
        self.fmodel_nosharp = self.create_fmodel(show=True).deep_copy()
        if (self.sharp): self.remove_common_isotropic_adp()
        print("Create fmodel...", file=self.log)
        self.fmodel = self.create_fmodel(show=True)
        self.crystal_gridding = self.f_obs.crystal_gridding(
            d_min=self.fmodel.f_obs().d_min(),
            symmetry_flags=maptbx.use_space_group_symmetry,
            resolution_factor=self.resolution_factor)
        # initial maps
        print("Compute initial maps...", file=self.log)
        self.mc = map_tools.electron_density_map(
            fmodel=self.fmodel).map_coefficients(map_type="2mFo-DFc",
                                                 isotropize=True,
                                                 fill_missing=False)
        self.mc_fill = map_tools.electron_density_map(
            fmodel=self.fmodel).map_coefficients(map_type="2mFo-DFc",
                                                 isotropize=True,
                                                 fill_missing=True)
        print("Finding typical atom volumes...", file=self.log)
        self.selection = good_atoms_selection(
            crystal_gridding=self.crystal_gridding,
            map_coeffs=self.mc_fill,
            xray_structure=self.fmodel.xray_structure)
        # main FEM loop
        m = self.outer_loop()
        m = low_volume_density_elimination(m=m,
                                           fmodel=self.fmodel,
                                           selection=self.selection)
        self.zero_below_threshold(m=m)
        # HE
        m = maptbx.volume_scale(map=m, n_bins=10000).map_data()
        # create Fourier ripples filter
        sel = m < 0.5
        msk = m.set_selected(sel, 0)
        sel = msk >= 0.5
        msk = msk.set_selected(sel, 1)

        #XXX    maptbx.sharpen(map_data=m, index_span=2, n_averages=1,
        #XXX          allow_negatives=False)

        # Use Resolve filter
        m_resolve = self.resolve_filter_map()
        if (m_resolve is not None): m = m * m_resolve
        # Use OMIT
        if (self.use_omit):
            comit = mmtbx.maps.composite_omit_map.run(
                fmodel=self.fmodel_nosharp,
                crystal_gridding=self.crystal_gridding,
                box_size_as_fraction=0.2,
                max_boxes=2000,
                neutral_volume_box_cushion_width=2,
                full_resolution_map=True,
                log=self.log)
            omit_map = comit.as_p1_map()
            #ccp4_map(cg=self.crystal_gridding, file_name="omit1.ccp4", map_data=omit_map)
            omit_map = low_volume_density_elimination(m=omit_map,
                                                      fmodel=self.fmodel,
                                                      selection=self.selection,
                                                      end=16)
            #ccp4_map(cg=self.crystal_gridding, file_name="omit2.ccp4", map_data=omit_map)
            sel = omit_map < 1.5
            omit_map = omit_map.set_selected(sel, 0)
            sel = omit_map >= 1.5
            omit_map = omit_map.set_selected(sel, 1)
            m = m * omit_map
            #
            # Extra filter: seems to ne redundant, TBD.
            #
            #omit_mc = comit.result_as_sf()
            #omit_map = get_map(mc=omit_mc, cg=self.crystal_gridding)
            #omit_map = low_volume_density_elimination(m=omit_map, fmodel=self.fmodel, selection=self.selection)
            #ccp4_map(cg=self.crystal_gridding, file_name="omit2.ccp4", map_data=omit_map)
            #sel      = omit_map<0.5
            #omit_map = omit_map.set_selected(sel, 0)
            #sel      = omit_map>=0.5
            #omit_map = omit_map.set_selected(sel, 1)
            #m = m * omit_map

        #
        self.mc_result = self.mc_fill.structure_factors_from_map(
            map=m, use_scale=True, anomalous_flag=False, use_sg=False)
        if (self.sharp):
            self.mc_result = mmtbx.maps.b_factor_sharpening_by_map_kurtosis_maximization(
                map_coeffs=self.mc_result, show=True, b_only=False)
        self.map_result = get_map(mc=self.mc_result,
                                  cg=self.crystal_gridding) * msk
Esempio n. 9
0
def run(args, out=sys.stdout, validated=False):
    show_citation(out=out)
    if (len(args) == 0):
        master_phil.show(out=out)
        print('\nUsage: phenix.map_comparison <CCP4> <CCP4>\n',\
          '       phenix.map_comparison <CCP4> <MTZ> mtz_label_1=<label>\n',\
          '       phenix.map_comparison <MTZ 1> mtz_label_1=<label 1> <MTZ 2> mtz_label_2=<label 2>\n', file=out)
        sys.exit()

    # process arguments
    params = None
    input_attributes = ['map_1', 'mtz_1', 'map_2', 'mtz_2']
    try:  # automatic parsing
        params = phil.process_command_line_with_files(
            args=args, master_phil=master_phil).work.extract()
    except Exception:  # map_file_def only handles one map phil
        from libtbx.phil.command_line import argument_interpreter
        arg_int = argument_interpreter(master_phil=master_phil)
        command_line_args = list()
        map_files = list()
        for arg in args:
            if (os.path.isfile(arg)):
                map_files.append(arg)
            else:
                command_line_args.append(arg_int.process(arg))
        params = master_phil.fetch(sources=command_line_args).extract()

        # check if more files are necessary
        n_defined = 0
        for attribute in input_attributes:
            if (getattr(params.input, attribute) is not None):
                n_defined += 1

        # matches files to phil scope, stops once there is sufficient data
        for map_file in map_files:
            if (n_defined < 2):
                current_map = file_reader.any_file(map_file)
                if (current_map.file_type == 'ccp4_map'):
                    n_defined += 1
                    if (params.input.map_1 is None):
                        params.input.map_1 = map_file
                    elif (params.input.map_2 is None):
                        params.input.map_2 = map_file
                elif (current_map.file_type == 'hkl'):
                    n_defined += 1
                    if (params.input.mtz_1 is None):
                        params.input.mtz_1 = map_file
                    elif (params.input.mtz_2 is None):
                        params.input.mtz_2 = map_file
            else:
                print('WARNING: only the first two files are used', file=out)
                break

    # validate arguments (GUI sets validated to true, no need to run again)
    assert (params is not None)
    if (not validated):
        validate_params(params)

    # ---------------------------------------------------------------------------
    # check if maps need to be generated from mtz
    n_maps = 0
    maps = list()
    map_names = list()
    for attribute in input_attributes:
        filename = getattr(params.input, attribute)
        if (filename is not None):
            map_names.append(filename)
            current_map = file_reader.any_file(filename)
            maps.append(current_map)
            if (current_map.file_type == 'ccp4_map'):
                n_maps += 1

    # construct maps, if necessary
    crystal_gridding = None
    m1 = None
    m2 = None

    # 1 map, 1 mtz file
    if (n_maps == 1):
        for current_map in maps:
            if (current_map.file_type == 'ccp4_map'):
                uc = current_map.file_object.unit_cell()
                sg_info = space_group_info(
                    current_map.file_object.space_group_number)
                n_real = current_map.file_object.unit_cell_grid
                crystal_gridding = maptbx.crystal_gridding(
                    uc, space_group_info=sg_info, pre_determined_n_real=n_real)
                m1 = current_map.file_object.map_data()
        if (crystal_gridding is not None):
            label = None
            for attribute in [('mtz_1', 'mtz_label_1'),
                              ('mtz_2', 'mtz_label_2')]:
                filename = getattr(params.input, attribute[0])
                label = getattr(params.input, attribute[1])
                if ((filename is not None) and (label is not None)):
                    break
            # labels will match currently open mtz file
            for current_map in maps:
                if (current_map.file_type == 'hkl'):
                    m2 = miller.fft_map(
                        crystal_gridding=crystal_gridding,
                        fourier_coefficients=current_map.file_server.
                        get_miller_array(
                            label)).apply_sigma_scaling().real_map_unpadded()
        else:
            raise Sorry('Gridding is not defined.')

    # 2 mtz files
    elif (n_maps == 0):
        crystal_symmetry = get_crystal_symmetry(maps[0])
        d_min = min(get_d_min(maps[0]), get_d_min(maps[1]))
        crystal_gridding = maptbx.crystal_gridding(
            crystal_symmetry.unit_cell(),
            d_min=d_min,
            resolution_factor=params.options.resolution_factor,
            space_group_info=crystal_symmetry.space_group_info())
        m1 = miller.fft_map(
            crystal_gridding=crystal_gridding,
            fourier_coefficients=maps[0].file_server.get_miller_array(
                params.input.mtz_label_1)).apply_sigma_scaling(
                ).real_map_unpadded()
        m2 = miller.fft_map(
            crystal_gridding=crystal_gridding,
            fourier_coefficients=maps[1].file_server.get_miller_array(
                params.input.mtz_label_2)).apply_sigma_scaling(
                ).real_map_unpadded()

    # 2 maps
    else:
        m1 = maps[0].file_object.map_data()
        m2 = maps[1].file_object.map_data()

    # ---------------------------------------------------------------------------
    # analyze maps
    assert ((m1 is not None) and (m2 is not None))

    # show general statistics
    s1 = maptbx.more_statistics(m1)
    s2 = maptbx.more_statistics(m2)
    show_overall_statistics(out=out, s=s1, header="Map 1 (%s):" % map_names[0])
    show_overall_statistics(out=out, s=s2, header="Map 2 (%s):" % map_names[1])
    cc_input_maps = flex.linear_correlation(x=m1.as_1d(),
                                            y=m2.as_1d()).coefficient()
    print("CC, input maps: %6.4f" % cc_input_maps, file=out)

    # compute CCpeak
    cc_peaks = list()
    m1_he = maptbx.volume_scale(map=m1, n_bins=10000).map_data()
    m2_he = maptbx.volume_scale(map=m2, n_bins=10000).map_data()
    cc_quantile = flex.linear_correlation(x=m1_he.as_1d(),
                                          y=m2_he.as_1d()).coefficient()
    print("CC, quantile rank-scaled (histogram equalized) maps: %6.4f" % \
      cc_quantile, file=out)
    print("Peak correlation:", file=out)
    print("  cutoff  CCpeak", file=out)
    cutoffs = [i / 100.
               for i in range(1, 90)] + [i / 1000 for i in range(900, 1000)]
    for cutoff in cutoffs:
        cc_peak = maptbx.cc_peak(map_1=m1_he, map_2=m2_he, cutoff=cutoff)
        print("  %3.2f   %7.4f" % (cutoff, cc_peak), file=out)
        cc_peaks.append((cutoff, cc_peak))

    # compute discrepancy function (D-function)
    discrepancies = list()
    cutoffs = flex.double(cutoffs)
    df = maptbx.discrepancy_function(map_1=m1_he, map_2=m2_he, cutoffs=cutoffs)
    print("Discrepancy function:", file=out)
    print("  cutoff  D", file=out)
    for c, d in zip(cutoffs, df):
        print("  %3.2f   %7.4f" % (c, d), file=out)
        discrepancies.append((c, d))

    # compute and output histograms
    h1 = maptbx.histogram(map=m1, n_bins=10000)
    h2 = maptbx.histogram(map=m2, n_bins=10000)
    print("Map histograms:", file=out)
    print("Map 1 (%s)     Map 2 (%s)"%\
      (params.input.map_1,params.input.map_2), file=out)
    print("(map_value,cdf,frequency) <> (map_value,cdf,frequency)", file=out)
    for a1, c1, v1, a2, c2, v2 in zip(h1.arguments(), h1.c_values(),
                                      h1.values(), h2.arguments(),
                                      h2.c_values(), h2.values()):
        print("(%9.5f %9.5f %9.5f) <> (%9.5f %9.5f %9.5f)"%\
          (a1,c1,v1, a2,c2,v2), file=out)

    # store results
    s1_dict = create_statistics_dict(s=s1)
    s2_dict = create_statistics_dict(s=s2)
    results = dict()
    inputs = list()
    for attribute in input_attributes:
        filename = getattr(params.input, attribute)
        if (filename is not None):
            inputs.append(filename)
    assert (len(inputs) == 2)
    results['map_files'] = inputs
    results['map_statistics'] = (s1_dict, s2_dict)
    results['cc_input_maps'] = cc_input_maps
    results['cc_quantile'] = cc_quantile
    results['cc_peaks'] = cc_peaks
    results['discrepancies'] = discrepancies
    # TODO, verify h1,h2 are not dicts, e.g. .values is py2/3 compat. I assume it is here
    results['map_histograms'] = ((h1.arguments(), h1.c_values(), h1.values()),
                                 (h2.arguments(), h2.c_values(), h2.values()))

    return results
Esempio n. 10
0
  def __init__(
        self,
        f_obs,
        r_free_flags,
        xray_structure,
        use_resolve,
        use_omit,
        use_max_map,
        sharp,
        use_unsharp_masking,
        resolution_factor,
        n_inner_loop = 10,
        n_outer_loop = 10,
        log          = None):
    adopt_init_args(self, locals())
    if(self.log is None): self.log = sys.stdout
    print >> self.log, "Start FEM..."
    self.prepare_f_obs_and_flags()
    self.mc_orig = self.compute_original_map()
    self.b_overall = None
    print >> self.log, "Create un-sharpened fmodel..."
    self.fmodel_nosharp = self.create_fmodel(show=True).deep_copy()
    if(self.sharp): self.remove_common_isotropic_adp()
    print >> self.log, "Create fmodel..."
    self.fmodel = self.create_fmodel(show=True)
    self.crystal_gridding = self.f_obs.crystal_gridding(
      d_min             = self.fmodel.f_obs().d_min(),
      symmetry_flags    = maptbx.use_space_group_symmetry,
      resolution_factor = self.resolution_factor)
    # initial maps
    print >> self.log, "Compute initial maps..."
    self.mc = map_tools.electron_density_map(
      fmodel=self.fmodel).map_coefficients(
        map_type     = "2mFo-DFc",
        isotropize   = True,
        fill_missing = False)
    self.mc_fill = map_tools.electron_density_map(
      fmodel=self.fmodel).map_coefficients(
        map_type     = "2mFo-DFc",
        isotropize   = True,
        fill_missing = True)
    print >> self.log, "Finding typical atom volumes..."
    self.selection = good_atoms_selection(
      crystal_gridding = self.crystal_gridding,
      map_coeffs       = self.mc_fill,
      xray_structure   = self.fmodel.xray_structure)
    # main FEM loop
    m = self.outer_loop()
    m = low_volume_density_elimination(m=m, fmodel=self.fmodel,
      selection=self.selection)
    self.zero_below_threshold(m = m)
    # HE
    m = maptbx.volume_scale(map = m,  n_bins = 10000).map_data()
    # create Fourier ripples filter
    sel = m<0.5
    msk = m.set_selected(sel, 0)
    sel = msk>=0.5
    msk = msk.set_selected(sel, 1)

#XXX    maptbx.sharpen(map_data=m, index_span=2, n_averages=1,
#XXX          allow_negatives=False)


    # Use Resolve filter
    m_resolve = self.resolve_filter_map()
    if(m_resolve is not None): m = m * m_resolve
    # Use OMIT
    if(self.use_omit):
      comit = mmtbx.maps.composite_omit_map.run(
        fmodel                           = self.fmodel_nosharp,
        crystal_gridding                 = self.crystal_gridding,
        box_size_as_fraction             = 0.2,
        max_boxes                        = 2000,
        neutral_volume_box_cushion_width = 2,
        full_resolution_map              = True,
        log                              = self.log)
      omit_map = comit.as_p1_map()
      #ccp4_map(cg=self.crystal_gridding, file_name="omit1.ccp4", map_data=omit_map)
      omit_map = low_volume_density_elimination(m=omit_map, fmodel=self.fmodel,
        selection=self.selection,end=16)
      #ccp4_map(cg=self.crystal_gridding, file_name="omit2.ccp4", map_data=omit_map)
      sel      = omit_map<1.5
      omit_map = omit_map.set_selected(sel, 0)
      sel      = omit_map>=1.5
      omit_map = omit_map.set_selected(sel, 1)
      m = m * omit_map
      #
      # Extra filter: seems to ne redundant, TBD.
      #
      #omit_mc = comit.result_as_sf()
      #omit_map = get_map(mc=omit_mc, cg=self.crystal_gridding)
      #omit_map = low_volume_density_elimination(m=omit_map, fmodel=self.fmodel, selection=self.selection)
      #ccp4_map(cg=self.crystal_gridding, file_name="omit2.ccp4", map_data=omit_map)
      #sel      = omit_map<0.5
      #omit_map = omit_map.set_selected(sel, 0)
      #sel      = omit_map>=0.5
      #omit_map = omit_map.set_selected(sel, 1)
      #m = m * omit_map

    #
    self.mc_result = self.mc_fill.structure_factors_from_map(
      map            = m,
      use_scale      = True,
      anomalous_flag = False,
      use_sg         = False)
    if(self.sharp):
      self.mc_result=mmtbx.maps.b_factor_sharpening_by_map_kurtosis_maximization(
        map_coeffs=self.mc_result, show=True, b_only=False)
    self.map_result = get_map(mc=self.mc_result, cg=self.crystal_gridding)*msk
Esempio n. 11
0
def run(args, out=sys.stdout, validated=False):
  show_citation(out=out)
  if (len(args) == 0):
    master_phil.show(out=out)
    print >> out,\
      '\nUsage: phenix.map_comparison <CCP4> <CCP4>\n',\
      '       phenix.map_comparison <CCP4> <MTZ> mtz_label_1=<label>\n',\
      '       phenix.map_comparison <MTZ 1> mtz_label_1=<label 1> <MTZ 2> mtz_label_2=<label 2>\n'
    sys.exit()

  # process arguments
  params = None
  input_attributes = ['map_1', 'mtz_1', 'map_2', 'mtz_2']
  try: # automatic parsing
    params = phil.process_command_line_with_files(
      args=args, master_phil=master_phil).work.extract()
  except Exception: # map_file_def only handles one map phil
    from libtbx.phil.command_line import argument_interpreter
    arg_int = argument_interpreter(master_phil=master_phil)
    command_line_args = list()
    map_files = list()
    for arg in args:
      if (os.path.isfile(arg)):
        map_files.append(arg)
      else:
        command_line_args.append(arg_int.process(arg))
    params = master_phil.fetch(sources=command_line_args).extract()

    # check if more files are necessary
    n_defined = 0
    for attribute in input_attributes:
      if (getattr(params.input, attribute) is not None):
        n_defined += 1

    # matches files to phil scope, stops once there is sufficient data
    for map_file in map_files:
      if (n_defined < 2):
        current_map = file_reader.any_file(map_file)
        if (current_map.file_type == 'ccp4_map'):
          n_defined += 1
          if (params.input.map_1 is None):
            params.input.map_1 = map_file
          elif (params.input.map_2 is None):
            params.input.map_2 = map_file
        elif (current_map.file_type == 'hkl'):
          n_defined += 1
          if (params.input.mtz_1 is None):
            params.input.mtz_1 = map_file
          elif (params.input.mtz_2 is None):
            params.input.mtz_2 = map_file
      else:
        print >> out, 'WARNING: only the first two files are used'
        break

  # validate arguments (GUI sets validated to true, no need to run again)
  assert (params is not None)
  if (not validated):
    validate_params(params)

  # ---------------------------------------------------------------------------
  # check if maps need to be generated from mtz
  n_maps = 0
  maps = list()
  map_names = list()
  for attribute in input_attributes:
    filename = getattr(params.input, attribute)
    if (filename is not None):
      map_names.append(filename)
      current_map = file_reader.any_file(filename)
      maps.append(current_map)
      if (current_map.file_type == 'ccp4_map'):
        n_maps += 1

  # construct maps, if necessary
  crystal_gridding = None
  m1 = None
  m2 = None

  # 1 map, 1 mtz file
  if (n_maps == 1):
    for current_map in maps:
      if (current_map.file_type == 'ccp4_map'):
        uc = current_map.file_object.unit_cell()
        sg_info = space_group_info(current_map.file_object.space_group_number)
        n_real = current_map.file_object.unit_cell_grid
        crystal_gridding = maptbx.crystal_gridding(
          uc, space_group_info=sg_info, pre_determined_n_real=n_real)
        m1 = current_map.file_object.map_data()
    if (crystal_gridding is not None):
      label = None
      for attribute in [('mtz_1', 'mtz_label_1'),
                        ('mtz_2', 'mtz_label_2')]:
        filename = getattr(params.input, attribute[0])
        label = getattr(params.input, attribute[1])
        if ( (filename is not None) and (label is not None) ):
          break
      # labels will match currently open mtz file
      for current_map in maps:
        if (current_map.file_type == 'hkl'):
          m2 = miller.fft_map(
            crystal_gridding=crystal_gridding,
            fourier_coefficients=current_map.file_server.get_miller_array(
              label)).apply_sigma_scaling().real_map_unpadded()
    else:
      raise Sorry('Gridding is not defined.')

  # 2 mtz files
  elif (n_maps == 0):
    crystal_symmetry = get_crystal_symmetry(maps[0])
    d_min = min(get_d_min(maps[0]), get_d_min(maps[1]))
    crystal_gridding = maptbx.crystal_gridding(
      crystal_symmetry.unit_cell(), d_min=d_min,
      resolution_factor=params.options.resolution_factor,
      space_group_info=crystal_symmetry.space_group_info())
    m1 = miller.fft_map(
      crystal_gridding=crystal_gridding,
      fourier_coefficients=maps[0].file_server.get_miller_array(
        params.input.mtz_label_1)).apply_sigma_scaling().real_map_unpadded()
    m2 = miller.fft_map(
      crystal_gridding=crystal_gridding,
      fourier_coefficients=maps[1].file_server.get_miller_array(
        params.input.mtz_label_2)).apply_sigma_scaling().real_map_unpadded()

  # 2 maps
  else:
    m1 = maps[0].file_object.map_data()
    m2 = maps[1].file_object.map_data()

  # ---------------------------------------------------------------------------
  # analyze maps
  assert ( (m1 is not None) and (m2 is not None) )

  # show general statistics
  s1 = maptbx.more_statistics(m1)
  s2 = maptbx.more_statistics(m2)
  show_overall_statistics(out=out, s=s1, header="Map 1 (%s):"%map_names[0])
  show_overall_statistics(out=out, s=s2, header="Map 2 (%s):"%map_names[1])
  cc_input_maps = flex.linear_correlation(x = m1.as_1d(),
                                          y = m2.as_1d()).coefficient()
  print >> out, "CC, input maps: %6.4f" % cc_input_maps

  # compute CCpeak
  cc_peaks = list()
  m1_he = maptbx.volume_scale(map = m1,  n_bins = 10000).map_data()
  m2_he = maptbx.volume_scale(map = m2,  n_bins = 10000).map_data()
  cc_quantile = flex.linear_correlation(x = m1_he.as_1d(),
                                        y = m2_he.as_1d()).coefficient()
  print >> out, "CC, quantile rank-scaled (histogram equalized) maps: %6.4f" % \
    cc_quantile
  print >> out, "Peak correlation:"
  print >> out, "  cutoff  CCpeak"
  cutoffs = [i/100.  for i in range(1,90)]+ [i/1000 for i in range(900,1000)]
  for cutoff in cutoffs:
    cc_peak = maptbx.cc_peak(map_1=m1_he, map_2=m2_he, cutoff=cutoff)
    print >> out, "  %3.2f   %7.4f" % (cutoff, cc_peak)
    cc_peaks.append((cutoff, cc_peak))

  # compute discrepancy function (D-function)
  discrepancies = list()
  cutoffs = flex.double(cutoffs)
  df = maptbx.discrepancy_function(map_1=m1_he, map_2=m2_he, cutoffs=cutoffs)
  print >> out, "Discrepancy function:"
  print >> out, "  cutoff  D"
  for c, d in zip(cutoffs, df):
    print >> out, "  %3.2f   %7.4f" % (c,d)
    discrepancies.append((c, d))

  # compute and output histograms
  h1 = maptbx.histogram(map=m1, n_bins=10000)
  h2 = maptbx.histogram(map=m2, n_bins=10000)
  print >> out, "Map histograms:"
  print >> out, "Map 1 (%s)     Map 2 (%s)"%\
    (params.input.map_1,params.input.map_2)
  print >> out, "(map_value,cdf,frequency) <> (map_value,cdf,frequency)"
  for a1,c1,v1, a2,c2,v2 in zip(h1.arguments(), h1.c_values(), h1.values(),
                                h2.arguments(), h2.c_values(), h2.values()):
    print >> out, "(%9.5f %9.5f %9.5f) <> (%9.5f %9.5f %9.5f)"%\
      (a1,c1,v1, a2,c2,v2)

  # store results
  s1_dict = create_statistics_dict(s=s1)
  s2_dict = create_statistics_dict(s=s2)
  results = dict()
  inputs = list()
  for attribute in input_attributes:
    filename = getattr(params.input,attribute)
    if (filename is not None):
      inputs.append(filename)
  assert (len(inputs) == 2)
  results['map_files'] = inputs
  results['map_statistics'] = (s1_dict, s2_dict)
  results['cc_input_maps'] = cc_input_maps
  results['cc_quantile'] = cc_quantile
  results['cc_peaks'] = cc_peaks
  results['discrepancies'] = discrepancies
  results['map_histograms'] = ( (h1.arguments(), h1.c_values(), h1.values()),
                                (h2.arguments(), h2.c_values(), h2.values()) )

  return results
Esempio n. 12
0
def run(args, validated=False):
  show_citation()
  if ( (len(args) == 0) or (len(args) > 2) ):
    print '\nUsage: phenix.map_comparison map_1=<first map> map_2=<second map>\n'
    sys.exit()

  # process arguments
  try: # automatic parsing
    params = phil.process_command_line_with_files(
      args=args, master_phil=master_phil).work.extract()
  except Exception: # map_file_def only handles one map phil
    from libtbx.phil.command_line import argument_interpreter
    arg_int = argument_interpreter(master_phil=master_phil)
    command_line_args = list()
    map_files = list()
    for arg in args:
      if (os.path.isfile(arg)):
        map_files.append(arg)
      else:
        command_line_args.append(arg_int.process(arg))
    params = master_phil.fetch(sources=command_line_args).extract()
    for map_file in map_files:
      if (params.input.map_1 is None):
        params.input.map_1 = map_file
      else:
        params.input.map_2 = map_file

  # validate arguments (GUI sets validated to true, no need to run again)
  if (not validated):
    validate_params(params)

  # ---------------------------------------------------------------------------
  # map 1
  ccp4_map_1 = iotbx.ccp4_map.map_reader(file_name=params.input.map_1)
  cs_1 = crystal.symmetry(ccp4_map_1.unit_cell().parameters(),
    ccp4_map_1.space_group_number)
  m1 = ccp4_map_1.map_data()

  # map 2
  ccp4_map_2 = iotbx.ccp4_map.map_reader(file_name=params.input.map_2)
  cs_2 = crystal.symmetry(ccp4_map_2.unit_cell().parameters(),
    ccp4_map_2.space_group_number)
  m2 = ccp4_map_2.map_data()

  # show general statistics
  s1 = maptbx.more_statistics(m1)
  s2 = maptbx.more_statistics(m2)
  show_overall_statistics(s=s1, header="Map 1 (%s):"%params.input.map_1)
  show_overall_statistics(s=s2, header="Map 2 (%s):"%params.input.map_2)
  cc_input_maps = flex.linear_correlation(x = m1.as_1d(),
                                          y = m2.as_1d()).coefficient()
  print "CC, input maps: %6.4f" % cc_input_maps

  # compute CCpeak
  cc_peaks = list()
  m1_he = maptbx.volume_scale(map = m1,  n_bins = 10000).map_data()
  m2_he = maptbx.volume_scale(map = m2,  n_bins = 10000).map_data()
  cc_quantile = flex.linear_correlation(x = m1_he.as_1d(),
                                        y = m2_he.as_1d()).coefficient()
  print "CC, quantile rank-scaled (histogram equalized) maps: %6.4f" % \
    cc_quantile
  print "Peak correlation:"
  print "  cutoff  CCpeak"
  for cutoff in [i/100. for i in range(0,100,5)]+[0.99, 1.0]:
    cc_peak = maptbx.cc_peak(map_1=m1_he, map_2=m2_he, cutoff=cutoff)
    print "  %3.2f   %7.4f" % (cutoff, cc_peak)
    cc_peaks.append((cutoff, cc_peak))

  # compute discrepancy function (D-function)
  discrepancies = list()
  cutoffs = flex.double([i/20. for i in range(1,20)])
  df = maptbx.discrepancy_function(map_1=m1_he, map_2=m2_he, cutoffs=cutoffs)
  print "Discrepancy function:"
  print "  cutoff  D"
  for c, d in zip(cutoffs, df):
    print "  %3.2f   %7.4f" % (c,d)
    discrepancies.append((c, d))

  # compute and output histograms
  h1 = maptbx.histogram(map=m1, n_bins=10000)
  h2 = maptbx.histogram(map=m2, n_bins=10000)
  print "Map histograms:"
  print "Map 1 (%s)     Map 2 (%s)"%(params.input.map_1,params.input.map_2)
  print "(map_value,cdf,frequency) <> (map_value,cdf,frequency)"
  for a1,c1,v1, a2,c2,v2 in zip(h1.arguments(), h1.c_values(), h1.values(),
                                h2.arguments(), h2.c_values(), h2.values()):
    print "(%9.5f %9.5f %9.5f) <> (%9.5f %9.5f %9.5f)"%(a1,c1,v1, a2,c2,v2)

  # store results
  s1_dict = create_statistics_dict(s1)
  s2_dict = create_statistics_dict(s2)
  results = dict()
  results['map_files'] = (params.input.map_1, params.input.map_2)
  results['map_statistics'] = (s1_dict, s2_dict)
  results['cc_input_maps'] = cc_input_maps
  results['cc_quantile'] = cc_quantile
  results['cc_peaks'] = cc_peaks
  results['discrepancies'] = discrepancies
  results['map_histograms'] = ( (h1.arguments(), h1.c_values(), h1.values()),
                                (h2.arguments(), h2.c_values(), h2.values()) )

  return results