示例#1
0
    def show_summary(self, out=None):
        if out is None: out = sys.stdout
        n_total = self.n_cif + self.n_hkl
        print >> out, "Number of cif: %7i" % self.n_cif
        print >> out, "Number of hkl: %7i" % self.n_hkl
        print >> out, "Number of hkl+cif pairs: ", self.n_hkl_cif_pairs
        print >> out

        print >> out, "%i parsing error%s" % plural_s(len(self.parsing_errors))
        print >> out, "%i exception%s" % plural_s(len(self.build_errors))
        print >> out, "%i ignored exception%s" % plural_s(
            len(self.ignored_errors))
        print >> out, "%i skipping" % len(self.skipped)
        print >> out
        rev = get_svn_revision()
        cod_path = os.environ.get("COD_SVN_WORKING_COPY")
        cod_rev = None
        if cod_path is not None:
            cod_rev = get_svn_revision(path=cod_path)
        build_tag = get_build_tag()
        if cod_rev is not None:
            print >> out, "COD svn revision: %i" % cod_rev
        if rev is not None:
            print >> out, "cctbx svn revision: %i" % rev
        if build_tag is not None:
            print >> out, "cctbx build tag: ", build_tag
  def show_summary(self, out=None):
    if out is None: out = sys.stdout
    n_total = self.n_cif + self.n_hkl
    print >> out, "Number of cif: %7i" % self.n_cif
    print >> out, "Number of hkl: %7i" % self.n_hkl
    print >> out, "Number of hkl+cif pairs: ", self.n_hkl_cif_pairs
    print >> out

    print >> out, "%i parsing error%s" % plural_s(len(self.parsing_errors))
    print >> out, "%i exception%s" % plural_s(len(self.build_errors))
    print >> out, "%i ignored exception%s" % plural_s(len(self.ignored_errors))
    print >> out, "%i skipping" % len(self.skipped)
    print >> out
    rev = get_svn_revision()
    cod_path = os.environ.get("COD_SVN_WORKING_COPY")
    cod_rev = None
    if cod_path is not None:
      cod_rev = get_svn_revision(path=cod_path)
    build_tag = get_build_tag()
    if cod_rev is not None:
      print >> out, "COD svn revision: %i" %cod_rev
    if rev is not None:
      print >> out, "cctbx svn revision: %i" %rev
    if build_tag is not None:
      print >> out, "cctbx build tag: ", build_tag
示例#3
0
 def show_summary(O, vertex_labels, out=None, prefix=""):
     from libtbx.utils import xlen, plural_s
     import sys
     if (out is None): out = sys.stdout
     if (vertex_labels is None):
         fmt = "%%0%dd" % len(str(max(0, O.n_vertices - 1)))
         vertex_labels = [fmt % i for i in range(O.n_vertices)]
     else:
         assert len(vertex_labels) == O.n_vertices
     print(prefix + "number of vertices:", O.n_vertices, file=out)
     print(prefix + "number of edges:", xlen(O.edge_list), file=out)
     if (O.find_cluster_loop_repeats is None):
         print(prefix + "find cluster loops: None", file=out)
     else:
         print(prefix+"find cluster loops: %d repeat%s" % \
           plural_s(O.find_cluster_loop_repeats), file=out)
     cm = O.cluster_manager
     cm.show_summary(out=out, prefix=prefix)
     if (cm.fixed_hinges is not None):
         for i, j in cm.fixed_hinges:
             print(prefix + "tardy fixed hinge:",
                   vertex_labels[i],
                   file=out)
             print(prefix + "                  ",
                   vertex_labels[j],
                   file=out)
     return O
def run(args, command_name=libtbx.env.dispatcher_name):
    parser = argparse.ArgumentParser(
        prog=command_name,
        usage='%s pdb_file "atom_selection" [...]' % command_name)
    parser.add_argument("file_name",
                        nargs=1,
                        help="File name of the model file")
    parser.add_argument(
        "inselections",
        help="Atom selection strings",
        nargs='+',
    )
    parser.add_argument("--write-pdb-file",
                        action="store",
                        help="write selected atoms to new PDB file",
                        default=None)
    parser.add_argument(
        "--cryst1-replacement-buffer-layer",
        action="store",
        type=float,
        help="replace CRYST1 with pseudo unit cell covering the selected"
        " atoms plus a surrounding buffer layer",
        default=None)
    co = parser.parse_args(args)
    mon_lib_srv = server.server()
    ener_lib = server.ener_lib()
    processed_pdb_file = pdb_interpretation.process(mon_lib_srv=mon_lib_srv,
                                                    ener_lib=ener_lib,
                                                    file_name=co.file_name[0],
                                                    log=sys.stdout)
    print
    acp = processed_pdb_file.all_chain_proxies

    selection_cache = acp.pdb_hierarchy.atom_selection_cache()
    atoms = acp.pdb_atoms
    all_bsel = flex.bool(atoms.size(), False)
    for selection_string in co.inselections:
        print selection_string
        isel = acp.iselection(string=selection_string, cache=selection_cache)
        all_bsel.set_selected(isel, True)
        if (not co.write_pdb_file):
            print "  %d atom%s selected" % plural_s(isel.size())
            for atom in atoms.select(isel):
                print "    %s" % atom.format_atom_record()
    print
    if (co.write_pdb_file):
        print "Writing file:", show_string(co.write_pdb_file)
        sel_hierarchy = acp.pdb_hierarchy.select(all_bsel)
        if (co.cryst1_replacement_buffer_layer is None):
            crystal_symmetry = acp.special_position_settings
        else:
            import cctbx.crystal
            crystal_symmetry = cctbx.crystal.non_crystallographic_symmetry(
                sites_cart=sel_hierarchy.atoms().extract_xyz(),
                buffer_layer=co.cryst1_replacement_buffer_layer)
        write_whole_pdb_file(file_name=co.write_pdb_file,
                             processed_pdb_file=processed_pdb_file,
                             pdb_hierarchy=sel_hierarchy,
                             crystal_symmetry=crystal_symmetry)
        print
示例#5
0
  def __init__(self, f_obs, asu_contents, e_statistics=False):
    assert f_obs.is_real_array()
    self.info = f_obs.info()
    f_obs_selected = f_obs.select(f_obs.data() > 0)
    f_obs_selected.use_binning_of(f_obs)
    # compute <fobs^2> in resolution shells
    self.mean_fobs_sq = f_obs_selected.mean_sq(
      use_binning=True,
      use_multiplicities=True).data[1:-1]
    n_none = self.mean_fobs_sq.count(None)
    if (n_none > 0):
      error_message = "wilson_plot error: %d empty bin%s:" % plural_s(n_none)
      if (self.info is not None):
        error_message += "\n  Info: " + str(self.info)
      error_message += "\n  Number of bins: %d" % len(self.mean_fobs_sq)
      error_message += "\n  Number of f_obs > 0: %d" % (
        f_obs_selected.indices().size())
      error_message += "\n  Number of f_obs <= 0: %d" % (
        f_obs.indices().size() - f_obs_selected.indices().size())
      raise RuntimeError(error_message)
    self.mean_fobs_sq = flex.double(self.mean_fobs_sq)
    # compute <s^2> = <(sin(theta)/lambda)^2> in resolution shells
    stol_sq = f_obs_selected.sin_theta_over_lambda_sq()
    stol_sq.use_binner_of(f_obs_selected)
    self.mean_stol_sq = flex.double(stol_sq.mean(
      use_binning=True,
      use_multiplicities=True).data[1:-1])
    # cache scattering factor info
    gaussians = {}
    for chemical_type in asu_contents.keys():
      gaussians[chemical_type] = eltbx.xray_scattering.wk1995(
        chemical_type).fetch()
    # compute expected f_calc^2 in resolution shells
    self.expected_f_sq = flex.double()
    for stol_sq in self.mean_stol_sq:
      sum_fj_sq = 0
      for chemical_type, n_atoms in asu_contents.items():
        f0 = gaussians[chemical_type].at_stol_sq(stol_sq)
        sum_fj_sq += f0 * f0 * n_atoms
      self.expected_f_sq.append(sum_fj_sq)
    self.expected_f_sq *= f_obs_selected.space_group().order_z() \
                        * f_obs_selected.space_group().n_ltr()
    # fit to straight line
    self.x = self.mean_stol_sq
    self.y = flex.log(self.mean_fobs_sq / self.expected_f_sq)
    fit = flex.linear_regression(self.x, self.y)
    assert fit.is_well_defined()
    self.fit_y_intercept = fit.y_intercept()
    self.fit_slope = fit.slope()
    self.wilson_intensity_scale_factor = math.exp(self.fit_y_intercept) # intensity scale factor
    self.wilson_k = math.sqrt(self.wilson_intensity_scale_factor) # conversion to amplitude scale factor
    self.wilson_b = -self.fit_slope / 2
    self.fit_correlation = flex.linear_correlation(self.x,self.y).coefficient()

    if e_statistics:
      normalised = f_obs_selected.normalised_amplitudes(asu_contents, self)
      self.normalised_f_obs = normalised.array()
      self.mean_e_sq_minus_1 = normalised.mean_e_sq_minus_1()
      self.percent_e_sq_gt_2 = normalised.percent_e_sq_gt_2()
示例#6
0
    def run(self):
        # I'm guessing self.data_manager, self.params and self.logger
        # are already defined here...

        # this must be mmtbx.model.manager?
        model = self.data_manager.get_model()
        atoms = model.get_atoms()
        all_bsel = flex.bool(atoms.size(), False)
        for selection_string in self.params.atom_selection_program.inselection:
            print("Selecting '%s'" % selection_string, file=self.logger)
            isel = model.iselection(string=selection_string)
            all_bsel.set_selected(isel, True)
            if self.params.atom_selection_program.write_pdb_file is None:
                print("  %d atom%s selected" % plural_s(isel.size()),
                      file=self.logger)
                for atom in atoms.select(isel):
                    print("    %s" % atom.format_atom_record(),
                          file=self.logger)
        print("", file=self.logger)
        if self.params.atom_selection_program.write_pdb_file is not None:
            print("Writing file:",
                  show_string(
                      self.params.atom_selection_program.write_pdb_file),
                  file=self.logger)
            ss_ann = model.get_ss_annotation()
            if not model.crystal_symmetry() or \
              (not model.crystal_symmetry().unit_cell()):
                model = shift_and_box_model(model, shift_model=False)
            selected_model = model.select(all_bsel)
            if (ss_ann is not None):
                selected_model.set_ss_annotation(ss_ann.\
                    filter_annotation(
                        hierarchy=selected_model.get_hierarchy(),
                        asc=selected_model.get_atom_selection_cache(),
                        remove_short_annotations=False,
                        remove_3_10_helices=False,
                        remove_empty_annotations=True,
                        concatenate_consecutive_helices=False,
                        split_helices_with_prolines=False,
                        filter_sheets_with_long_hbonds=False))
            if self.params.atom_selection_program.cryst1_replacement_buffer_layer is not None:
                box = uctbx.non_crystallographic_unit_cell_with_the_sites_in_its_center(
                    sites_cart=selected_model.get_atoms().extract_xyz(),
                    buffer_layer=self.params.atom_selection_program.
                    cryst1_replacement_buffer_layer)
                sp = crystal.special_position_settings(box.crystal_symmetry())
                sites_frac = box.sites_frac()
                xrs_box = selected_model.get_xray_structure(
                ).replace_sites_frac(box.sites_frac())
                xray_structure_box = xray.structure(sp, xrs_box.scatterers())
                selected_model.set_xray_structure(xray_structure_box)
            pdb_str = selected_model.model_as_pdb()
            f = open(self.params.atom_selection_program.write_pdb_file, 'w')
            f.write(pdb_str)
            f.close()
            print("", file=self.logger)
def run(args, command_name=libtbx.env.dispatcher_name):
    parser = argparse.ArgumentParser(
        prog=command_name,
        usage='%s pdb_file "atom_selection" [...]' % command_name)
    parser.add_argument("file_name",
                        nargs=1,
                        help="File name of the model file")
    parser.add_argument(
        "inselections",
        help="Atom selection strings",
        nargs='+',
    )
    parser.add_argument("--write-pdb-file",
                        action="store",
                        help="write selected atoms to new PDB file",
                        default=None)
    parser.add_argument(
        "--cryst1-replacement-buffer-layer",
        action="store",
        type=float,
        help="replace CRYST1 with pseudo unit cell covering the selected"
        " atoms plus a surrounding buffer layer",
        default=None)
    co = parser.parse_args(args)
    pdb_inp = iotbx.pdb.input(file_name=co.file_name[0])
    model = mmtbx.model.manager(model_input=pdb_inp, process_input=True)
    atoms = model.get_atoms()
    all_bsel = flex.bool(atoms.size(), False)
    for selection_string in co.inselections:
        print selection_string
        isel = model.iselection(selstr=selection_string)
        all_bsel.set_selected(isel, True)
        if (not co.write_pdb_file):
            print "  %d atom%s selected" % plural_s(isel.size())
            for atom in atoms.select(isel):
                print "    %s" % atom.format_atom_record()
    print
    if (co.write_pdb_file):
        print "Writing file:", show_string(co.write_pdb_file)
        selected_model = model.select(all_bsel)
        if (co.cryst1_replacement_buffer_layer is not None):
            box = uctbx.non_crystallographic_unit_cell_with_the_sites_in_its_center(
                sites_cart=selected_model.get_atoms().extract_xyz(),
                buffer_layer=co.cryst1_replacement_buffer_layer)
            sp = crystal.special_position_settings(box.crystal_symmetry())
            sites_frac = box.sites_frac()
            xrs_box = selected_model.get_xray_structure().replace_sites_frac(
                box.sites_frac())
            xray_structure_box = xray.structure(sp, xrs_box.scatterers())
            selected_model.set_xray_structure(xray_structure_box)
        pdb_str = selected_model.model_as_pdb()
        f = open(co.write_pdb_file, 'w')
        f.write(pdb_str)
        f.close()
        print
def run_dps(args):
  imageset, spots_mm, max_cell, params = args

  detector = imageset.get_detector()
  beam = imageset.get_beam()
  goniometer = imageset.get_goniometer()
  scan = imageset.get_scan()

  from rstbx.indexing_api.lattice import DPS_primitive_lattice
  # max_cell: max possible cell in Angstroms; set to None, determine from data
  # recommended_grid_sampling_rad: grid sampling in radians; guess for now

  DPS = DPS_primitive_lattice(max_cell=max_cell,
                              recommended_grid_sampling_rad=None,
                              horizon_phil=params)

  from scitbx import matrix
  DPS.S0_vector = matrix.col(beam.get_s0())
  DPS.inv_wave = 1./beam.get_wavelength()
  if goniometer is None:
    DPS.axis = matrix.col((1,0,0))
  else:
    DPS.axis = matrix.col(goniometer.get_rotation_axis())
  DPS.set_detector(detector)

  # transform input into what Nick needs
  # i.e., construct a flex.vec3 double consisting of mm spots, phi in degrees

  data = flex.vec3_double()
  for spot in spots_mm:
    data.append((spot['xyzobs.mm.value'][0],
                 spot['xyzobs.mm.value'][1],
                 spot['xyzobs.mm.value'][2]*180./math.pi))

  #from matplotlib import pyplot as plt
  #plt.plot([spot.centroid_position[0] for spot in spots_mm] , [spot.centroid_position[1] for spot in spots_mm], 'ro')
  #plt.show()

  logger.info("Running DPS using %i reflections" %len(data))

  DPS.index(raw_spot_input=data,
            panel_addresses=flex.int([s['panel'] for s in spots_mm]))
  solutions = DPS.getSolutions()
  from libtbx.utils import plural_s
  logger.info("Found %i solution%s with max unit cell %.2f Angstroms." %(
    len(solutions), plural_s(len(solutions))[1], DPS.amax))
  if len(solutions) < 3:
    from libtbx.utils import Sorry
    raise Sorry("Not enough solutions: found %i, need at least 3" %(
      len(solutions)))
  return dict(solutions=flex.vec3_double(
    [s.dvec for s in solutions]), amax=DPS.amax)
示例#9
0
    def __init__(self, f_obs, asu_contents, e_statistics=False):
        assert f_obs.is_real_array()
        self.info = f_obs.info()
        f_obs_selected = f_obs.select(f_obs.data() > 0)
        f_obs_selected.use_binning_of(f_obs)
        # compute <fobs^2> in resolution shells
        self.mean_fobs_sq = f_obs_selected.mean_sq(use_binning=True, use_multiplicities=True).data[1:-1]
        n_none = self.mean_fobs_sq.count(None)
        if n_none > 0:
            error_message = "wilson_plot error: %d empty bin%s:" % plural_s(n_none)
            if self.info is not None:
                error_message += "\n  Info: " + str(self.info)
            error_message += "\n  Number of bins: %d" % len(self.mean_fobs_sq)
            error_message += "\n  Number of f_obs > 0: %d" % (f_obs_selected.indices().size())
            error_message += "\n  Number of f_obs <= 0: %d" % (f_obs.indices().size() - f_obs_selected.indices().size())
            raise RuntimeError(error_message)
        self.mean_fobs_sq = flex.double(self.mean_fobs_sq)
        # compute <s^2> = <(sin(theta)/lambda)^2> in resolution shells
        stol_sq = f_obs_selected.sin_theta_over_lambda_sq()
        stol_sq.use_binner_of(f_obs_selected)
        self.mean_stol_sq = flex.double(stol_sq.mean(use_binning=True, use_multiplicities=True).data[1:-1])
        # cache scattering factor info
        gaussians = {}
        for chemical_type in asu_contents.keys():
            gaussians[chemical_type] = eltbx.xray_scattering.wk1995(chemical_type).fetch()
        # compute expected f_calc^2 in resolution shells
        self.expected_f_sq = flex.double()
        for stol_sq in self.mean_stol_sq:
            sum_fj_sq = 0
            for chemical_type, n_atoms in asu_contents.items():
                f0 = gaussians[chemical_type].at_stol_sq(stol_sq)
                sum_fj_sq += f0 * f0 * n_atoms
            self.expected_f_sq.append(sum_fj_sq)
        self.expected_f_sq *= f_obs_selected.space_group().order_z() * f_obs_selected.space_group().n_ltr()
        # fit to straight line
        self.x = self.mean_stol_sq
        self.y = flex.log(self.mean_fobs_sq / self.expected_f_sq)
        fit = flex.linear_regression(self.x, self.y)
        assert fit.is_well_defined()
        self.fit_y_intercept = fit.y_intercept()
        self.fit_slope = fit.slope()
        self.wilson_intensity_scale_factor = math.exp(self.fit_y_intercept)  # intensity scale factor
        self.wilson_k = math.sqrt(self.wilson_intensity_scale_factor)  # conversion to amplitude scale factor
        self.wilson_b = -self.fit_slope / 2
        self.fit_correlation = flex.linear_correlation(self.x, self.y).coefficient()

        if e_statistics:
            normalised = f_obs_selected.normalised_amplitudes(asu_contents, self)
            self.normalised_f_obs = normalised.array()
            self.mean_e_sq_minus_1 = normalised.mean_e_sq_minus_1()
            self.percent_e_sq_gt_2 = normalised.percent_e_sq_gt_2()
示例#10
0
def run_dps(args):
    imageset, spots_mm, max_cell, params = args

    detector = imageset.get_detector()
    beam = imageset.get_beam()
    goniometer = imageset.get_goniometer()

    # max_cell: max possible cell in Angstroms; set to None, determine from data
    # recommended_grid_sampling_rad: grid sampling in radians; guess for now

    DPS = DPS_primitive_lattice(max_cell=max_cell,
                                recommended_grid_sampling_rad=None,
                                horizon_phil=params)

    DPS.S0_vector = matrix.col(beam.get_s0())
    DPS.inv_wave = 1.0 / beam.get_wavelength()
    if goniometer is None:
        DPS.axis = matrix.col((1, 0, 0))
    else:
        DPS.axis = matrix.col(goniometer.get_rotation_axis())
    DPS.set_detector(detector)

    # transform input into what Nick needs
    # i.e., construct a flex.vec3 double consisting of mm spots, phi in degrees

    data = flex.vec3_double()
    for spot in spots_mm.rows():
        data.append((
            spot["xyzobs.mm.value"][0],
            spot["xyzobs.mm.value"][1],
            spot["xyzobs.mm.value"][2] * 180.0 / math.pi,
        ))

    logger.info("Running DPS using %i reflections" % len(data))

    DPS.index(
        raw_spot_input=data,
        panel_addresses=flex.int(s["panel"] for s in spots_mm.rows()),
    )
    solutions = DPS.getSolutions()

    logger.info("Found %i solution%s with max unit cell %.2f Angstroms." %
                (len(solutions), plural_s(len(solutions))[1], DPS.amax))
    if len(solutions) < 3:

        raise Sorry("Not enough solutions: found %i, need at least 3" %
                    (len(solutions)))
    return dict(solutions=flex.vec3_double([s.dvec for s in solutions]),
                amax=DPS.amax)
示例#11
0
def run_dps(experiment, spots_mm, max_cell):
    # max_cell: max possible cell in Angstroms; set to None, determine from data
    # recommended_grid_sampling_rad: grid sampling in radians; guess for now

    horizon_phil = iotbx.phil.parse(input_string=indexing_api_defs).extract()
    DPS = DPS_primitive_lattice(max_cell=max_cell,
                                recommended_grid_sampling_rad=None,
                                horizon_phil=horizon_phil)

    DPS.S0_vector = matrix.col(experiment.beam.get_s0())
    DPS.inv_wave = 1.0 / experiment.beam.get_wavelength()
    if experiment.goniometer is None:
        DPS.axis = matrix.col((1, 0, 0))
    else:
        DPS.axis = matrix.col(experiment.goniometer.get_rotation_axis())
    DPS.set_detector(experiment.detector)

    # transform input into what DPS needs
    # i.e., construct a flex.vec3 double consisting of mm spots, phi in degrees
    data = flex.vec3_double()
    for spot in spots_mm.rows():
        data.append((
            spot["xyzobs.mm.value"][0],
            spot["xyzobs.mm.value"][1],
            spot["xyzobs.mm.value"][2] * 180.0 / math.pi,
        ))

    logger.info("Running DPS using %i reflections", len(data))

    DPS.index(
        raw_spot_input=data,
        panel_addresses=flex.int(s["panel"] for s in spots_mm.rows()),
    )
    solutions = DPS.getSolutions()

    logger.info(
        "Found %i solution%s with max unit cell %.2f Angstroms.",
        len(solutions),
        plural_s(len(solutions))[1],
        DPS.amax,
    )

    # There must be at least 3 solutions to make a set, otherwise return empty result
    if len(solutions) < 3:
        return {}
    return {
        "solutions": flex.vec3_double(s.dvec for s in solutions),
        "amax": DPS.amax
    }
示例#12
0
 def show_summary(O, out=None, prefix=""):
     from libtbx.utils import xlen, plural_s
     import sys
     if (out is None): out = sys.stdout
     print >> out, prefix+"number of fixed vertex lists:", \
       len(O.fixed_vertex_lists)
     print >> out, prefix+"number of fixed vertices:", \
       sum([len(fixed_vertices) for fixed_vertices in O.fixed_vertex_lists])
     print >> out, prefix + "number of clusters:", len(O.clusters)
     print >> out, prefix+"merge clusters with multiple connections: %d pass%s"\
       % plural_s(O.merge_clusters_with_multiple_connections_passes, "es")
     print >> out, prefix + "number of hinge edges:", xlen(O.hinge_edges)
     print >> out, prefix + "number of loop edges:", xlen(O.loop_edges)
     print >> out, prefix+"number of loop edge bendings:", \
       xlen(O.loop_edge_bendings)
     print >> out, prefix + "number of fixed hinges:", xlen(O.fixed_hinges)
     return O
示例#13
0
 def show_summary(O, out=None, prefix=""):
   from libtbx.utils import xlen, plural_s
   import sys
   if (out is None): out = sys.stdout
   print >> out, prefix+"number of fixed vertex lists:", \
     len(O.fixed_vertex_lists)
   print >> out, prefix+"number of fixed vertices:", \
     sum([len(fixed_vertices) for fixed_vertices in O.fixed_vertex_lists])
   print >> out, prefix+"number of clusters:", len(O.clusters)
   print >> out, prefix+"merge clusters with multiple connections: %d pass%s"\
     % plural_s(O.merge_clusters_with_multiple_connections_passes, "es")
   print >> out, prefix+"number of hinge edges:", xlen(O.hinge_edges)
   print >> out, prefix+"number of loop edges:", xlen(O.loop_edges)
   print >> out, prefix+"number of loop edge bendings:", \
     xlen(O.loop_edge_bendings)
   print >> out, prefix+"number of fixed hinges:", xlen(O.fixed_hinges)
   return O
示例#14
0
    def run(self):
        # I'm guessing self.data_manager, self.params and self.logger
        # are already defined here...

        # this must be mmtbx.model.manager?
        model = self.data_manager.get_model()
        atoms = model.get_atoms()
        all_bsel = flex.bool(atoms.size(), False)
        for selection_string in self.params.atom_selection_program.inselection:
            print("Selecting '%s'" % selection_string, file=self.logger)
            isel = model.iselection(selstr=selection_string)
            all_bsel.set_selected(isel, True)
            if self.params.atom_selection_program.write_pdb_file is None:
                print("  %d atom%s selected" % plural_s(isel.size()),
                      file=self.logger)
                for atom in atoms.select(isel):
                    print("    %s" % atom.format_atom_record(),
                          file=self.logger)
        print("", file=self.logger)
        if self.params.atom_selection_program.write_pdb_file is not None:
            print("Writing file:",
                  show_string(
                      self.params.atom_selection_program.write_pdb_file),
                  file=self.logger)
            selected_model = model.select(all_bsel)
            if self.params.atom_selection_program.cryst1_replacement_buffer_layer is not None:
                box = uctbx.non_crystallographic_unit_cell_with_the_sites_in_its_center(
                    sites_cart=selected_model.get_atoms().extract_xyz(),
                    buffer_layer=self.params.atom_selection_program.
                    cryst1_replacement_buffer_layer)
                sp = crystal.special_position_settings(box.crystal_symmetry())
                sites_frac = box.sites_frac()
                xrs_box = selected_model.get_xray_structure(
                ).replace_sites_frac(box.sites_frac())
                xray_structure_box = xray.structure(sp, xrs_box.scatterers())
                selected_model.set_xray_structure(xray_structure_box)
            pdb_str = selected_model.model_as_pdb()
            f = open(self.params.atom_selection_program.write_pdb_file, 'w')
            f.write(pdb_str)
            f.close()
            print("", file=self.logger)
示例#15
0
def process(params, pickle_file_name):
    cod_id = op.basename(pickle_file_name).split(".", 1)[0]
    print "cod_id:", cod_id
    c_obs, structure_prep, edge_list = easy_pickle.load(
        file_name=pickle_file_name)
    changes = structure_prep.make_scatterer_labels_shelx_compatible_in_place()
    if (params.sites_mod_short):
        structure_prep = structure_prep.sites_mod_short()
    from iotbx.shelx import fvar_encoding
    structure_prep = \
      fvar_encoding.move_sites_if_necessary_for_shelx_fvar_encoding(
        xray_structure=structure_prep)
    structure_prep.show_summary().show_scatterers()
    if (len(changes) != 0):
        from libtbx.utils import plural_s
        print "INFO: %d atom name%s changed for compatibility with SHELXL:" \
          % plural_s(len(changes))
        for change in changes:
            print '  changed: "%s" -> "%s"' % change
    structure_prep.scattering_type_registry(table="it1992").show()
    fvar_encoding.dev_build_shelx76_fvars(structure_prep)  # only an exercise
    print "." * 79
    #
    if (len(params.optimizers) == 0):
        return
    #
    assert c_obs.is_xray_intensity_array() or c_obs.is_xray_amplitude_array()
    if (c_obs.is_xray_intensity_array()):
        i_obs = c_obs
        f_obs = c_obs.f_sq_as_f(algorithm="xtal_3_7")
    else:
        f_obs = c_obs
        i_obs = c_obs.f_as_f_sq(algorithm="shelxl")
    process_continue(params=params,
                     cod_id=cod_id,
                     c_obs=c_obs,
                     i_obs=i_obs,
                     f_obs=f_obs,
                     structure_prep=structure_prep)
def exercise_syminfo_lib_pdb_cryst1_recycling():
  # this call is to build _syminfo_lib_cache
  assert extract_from_symmetry_lib.ccp4_symbol(
    space_group_info=sgtbx.space_group_info("P 1"),
    lib_name="syminfo.lib") == "P 1"
  #
  import iotbx.pdb.cryst1_interpretation
  n_need_more_special = 0
  for number in xrange(1,230+1):
    for hall,ccp4_symbol in \
          extract_from_symmetry_lib._syminfo_lib_cache[number]:
      sgi_hall = sgtbx.space_group_info(symbol="Hall: "+hall)
      if (sgi_hall.group().is_centric()):
        continue
      sgroup = iotbx.pdb.format_cryst1_sgroup(space_group_info=sgi_hall)
      if (len(sgroup) > 11):
        print "ccp4 syminfo.lib setting leads to pdb CRYST1 overflow:",\
          ccp4_symbol, sgroup
      cs = sgi_hall.any_compatible_crystal_symmetry(volume=1000)
      pdb_str = iotbx.pdb.format_cryst1_record(crystal_symmetry=cs)
      cs2 = iotbx.pdb.cryst1_interpretation.crystal_symmetry(
        cryst1_record=pdb_str)
      if (cs2.space_group_info() is None):
        if (n_need_more_special == 0): print
        print '"%s": "Hall: %s",' % (
          ccp4_symbol.replace(" ", "").upper(), hall)
        n_need_more_special += 1
      else:
        assert cs2.is_similar_symmetry(other=cs)
      if (ccp4io_adaptbx is not None):
        exercise_mmdb_cryst1_interpretation(
          sgi_hall=sgi_hall, pdb_str=pdb_str)
  if (n_need_more_special != 0):
    print
    from libtbx.utils import plural_s
    raise RuntimeError("""\
Please edit iotbx/pdb/cryst1_interpretation.py:
  Add the %d line%s above with "Hall:" to the "special" dictionary.
""" % plural_s(n_need_more_special))
def exercise_syminfo_lib_pdb_cryst1_recycling():
    # this call is to build _syminfo_lib_cache
    assert extract_from_symmetry_lib.ccp4_symbol(
        space_group_info=sgtbx.space_group_info("P 1"),
        lib_name="syminfo.lib") == "P 1"
    #
    import iotbx.pdb.cryst1_interpretation
    n_need_more_special = 0
    for number in range(1, 230 + 1):
        for hall,ccp4_symbol in \
              extract_from_symmetry_lib._syminfo_lib_cache[number]:
            sgi_hall = sgtbx.space_group_info(symbol="Hall: " + hall)
            if (sgi_hall.group().is_centric()):
                continue
            sgroup = iotbx.pdb.format_cryst1_sgroup(space_group_info=sgi_hall)
            if (len(sgroup) > 11):
                print("ccp4 syminfo.lib setting leads to pdb CRYST1 overflow:",\
                  ccp4_symbol, sgroup)
            cs = sgi_hall.any_compatible_crystal_symmetry(volume=1000)
            pdb_str = iotbx.pdb.format_cryst1_record(crystal_symmetry=cs)
            cs2 = iotbx.pdb.cryst1_interpretation.crystal_symmetry(
                cryst1_record=pdb_str)
            if (cs2.space_group_info() is None):
                if (n_need_more_special == 0): print()
                print('"%s": "Hall: %s",' %
                      (ccp4_symbol.replace(" ", "").upper(), hall))
                n_need_more_special += 1
            else:
                assert cs2.is_similar_symmetry(other=cs)
            if (ccp4io_adaptbx is not None):
                exercise_mmdb_cryst1_interpretation(sgi_hall=sgi_hall,
                                                    pdb_str=pdb_str)
    if (n_need_more_special != 0):
        print()
        from libtbx.utils import plural_s
        raise RuntimeError("""\
Please edit iotbx/pdb/cryst1_interpretation.py:
  Add the %d line%s above with "Hall:" to the "special" dictionary.
""" % plural_s(n_need_more_special))
示例#18
0
def process(params, pickle_file_name):
  cod_id = op.basename(pickle_file_name).split(".",1)[0]
  print "cod_id:", cod_id
  c_obs, structure_prep, edge_list = easy_pickle.load(
    file_name=pickle_file_name)
  changes = structure_prep.make_scatterer_labels_shelx_compatible_in_place()
  if (params.sites_mod_short):
    structure_prep = structure_prep.sites_mod_short()
  from iotbx.shelx import fvar_encoding
  structure_prep = \
    fvar_encoding.move_sites_if_necessary_for_shelx_fvar_encoding(
      xray_structure=structure_prep)
  structure_prep.show_summary().show_scatterers()
  if (len(changes) != 0):
    from libtbx.utils import plural_s
    print "INFO: %d atom name%s changed for compatibility with SHELXL:" \
      % plural_s(len(changes))
    for change in changes:
      print '  changed: "%s" -> "%s"' % change
  structure_prep.scattering_type_registry(table="it1992").show()
  fvar_encoding.dev_build_shelx76_fvars(structure_prep) # only an exercise
  print "."*79
  #
  if (len(params.optimizers) == 0):
    return
  #
  assert c_obs.is_xray_intensity_array() or c_obs.is_xray_amplitude_array()
  if (c_obs.is_xray_intensity_array()):
    i_obs = c_obs
    f_obs = c_obs.f_sq_as_f(algorithm="xtal_3_7")
  else:
    f_obs = c_obs
    i_obs = c_obs.f_as_f_sq(algorithm="shelxl")
  process_continue(
    params=params,
    cod_id=cod_id,
    c_obs=c_obs, i_obs=i_obs, f_obs=f_obs,
    structure_prep=structure_prep)
示例#19
0
 def show_summary(O, vertex_labels, out=None, prefix=""):
   from libtbx.utils import xlen, plural_s
   import sys
   if (out is None): out = sys.stdout
   if (vertex_labels is None):
     fmt = "%%0%dd" % len(str(max(0, O.n_vertices-1)))
     vertex_labels = [fmt % i for i in xrange(O.n_vertices)]
   else:
     assert len(vertex_labels) == O.n_vertices
   print >> out, prefix+"number of vertices:", O.n_vertices
   print >> out, prefix+"number of edges:", xlen(O.edge_list)
   if (O.find_cluster_loop_repeats is None):
     print >> out, prefix+"find cluster loops: None"
   else:
     print >> out, prefix+"find cluster loops: %d repeat%s" % \
       plural_s(O.find_cluster_loop_repeats)
   cm = O.cluster_manager
   cm.show_summary(out=out, prefix=prefix)
   if (cm.fixed_hinges is not None):
     for i,j in cm.fixed_hinges:
       print >> out, prefix+"tardy fixed hinge:", vertex_labels[i]
       print >> out, prefix+"                  ", vertex_labels[j]
   return O
示例#20
0
def run(args, command_name=libtbx.env.dispatcher_name):
    if (len(args) == 0): args = ["--help"]
    command_line = (option_parser(
        usage='%s pdb_file "atom_selection" [...]' % command_name).option(
            None,
            "--write_pdb_file",
            action="store",
            type="string",
            default=None,
            help="write selected atoms to new PDB file",
            metavar="FILE"
        ).option(
            None,
            "--cryst1_replacement_buffer_layer",
            action="store",
            type="float",
            default=None,
            help="replace CRYST1 with pseudo unit cell covering the selected"
            " atoms plus a surrounding buffer layer",
            metavar="WIDTH")).process(args=args, min_nargs=2)
    co = command_line.options
    mon_lib_srv = server.server()
    ener_lib = server.ener_lib()
    processed_pdb_file = pdb_interpretation.process(
        mon_lib_srv=mon_lib_srv,
        ener_lib=ener_lib,
        file_name=command_line.args[0],
        log=sys.stdout)
    print
    acp = processed_pdb_file.all_chain_proxies

    hierarchy = acp.pdb_hierarchy
    asc = hierarchy.atom_selection_cache()
    sel = asc.selection(string="chain 'A' and resid 1 through 8 and icode ' '")
    h1 = hierarchy.select(sel)  # keep original hierarchy too
    print h1.as_pdb_string()

    selection_cache = acp.pdb_hierarchy.atom_selection_cache()
    atoms = acp.pdb_atoms
    all_bsel = flex.bool(atoms.size(), False)
    for selection_string in command_line.args[1:]:
        print selection_string
        isel = acp.iselection(string=selection_string, cache=selection_cache)
        all_bsel.set_selected(isel, True)
        if (not co.write_pdb_file):
            print "  %d atom%s selected" % plural_s(isel.size())
            for atom in atoms.select(isel):
                print "    %s" % atom.format_atom_record()
    print
    if (co.write_pdb_file):
        print "Writing file:", show_string(co.write_pdb_file)
        sel_hierarchy = acp.pdb_hierarchy.select(all_bsel)
        if (co.cryst1_replacement_buffer_layer is None):
            crystal_symmetry = acp.special_position_settings
        else:
            import cctbx.crystal
            crystal_symmetry = cctbx.crystal.non_crystallographic_symmetry(
                sites_cart=sel_hierarchy.atoms().extract_xyz(),
                buffer_layer=co.cryst1_replacement_buffer_layer)
        write_whole_pdb_file(file_name=co.write_pdb_file,
                             processed_pdb_file=processed_pdb_file,
                             pdb_hierarchy=sel_hierarchy,
                             crystal_symmetry=crystal_symmetry)
        print
    else:
      l = line.strip().replace(" ","")
      if (l == "){-endblockparameterdefinition-}"):
        lines_out.append(line)
        end_block_parameter_definition = True
      else:
        line_out = line
        for p in parameter_names:
          if (l.startswith("{===>}%s=" % p) and l.endswith(";")):
            parameters_found[p] += 1
            line_out = '{===>} %s=%s;' % (p, vars()[p])
            if (line_out != line): parameters_changed.append(p)
            break
        lines_out.append(line_out)
  if (parameters_found.values().count(1) != 7):
    raise Sorry("Unexpected set of variable names in %s:\n  counts: %s" % (
      show_string(target), str(parameters_found)))
  elif (len(parameters_changed) == 0):
    print "Info: no changes, %s was not modified." % show_string(target)
  else:
    string_out = "\n".join(lines_out)
    print "Info: %d change%s" % plural_s(len(parameters_changed)), \
      "(%s)," % ", ".join(parameters_changed), \
      "writing modified file %s." % show_string(target)
    try: print >> open(target, "w"), string_out
    except IOError, e:
      raise Sorry("Error writing file %s (%s)" % (show_string(target), str(e)))

if (__name__ == "__main__"):
  run(sys.argv[1:])
示例#22
0
def unit_cell_info(sub_clusters):
    """
  Print unit cell information for a list of clusters.

  :param sub_clusters: a list of cluster objects
  :return: a string containing median unit cells, standard deviations and
   point group composition of each cluster.
  """
    from libtbx.utils import plural_s
    # 3. print out some information that is useful.
    out_str = "\n\n{:<16} {:<8} {:<13} {:<13} {:<13} {:<12} {:<12} {:<12}{:<8}\n".format(
        "Cluster_id", "N_xtals", "Med_a", "Med_b", "Med_c", "Med_alpha",
        "Med_beta", "Med_gamma", "Delta(deg)")
    singletons = []
    for cluster in sub_clusters:
        if len(cluster.members) != 1:
            # New approach, takes niggli setting of the cluster median and converts
            # back to reference setting for cluster report. Fixes cctbx#97.
            from cctbx import crystal
            from cctbx.uctbx import unit_cell
            from cctbx.sgtbx.lattice_symmetry import metric_subgroups

            input_symmetry = crystal.symmetry(unit_cell=unit_cell(
                cluster.medians[0:6]),
                                              space_group_symbol="P 1")
            groups = metric_subgroups(
                input_symmetry,
                3.00,
                enforce_max_delta_for_generated_two_folds=True)
            group = groups.result_groups[0]
            # suppress stdout output for now
            from StringIO import StringIO
            SS = StringIO()
            import sys
            sys.stdout = SS
            group['best_subsym'].space_group_info().show_summary()
            sys.stdout = sys.__stdout__
            print "                       Unit cell:", group[
                'best_subsym'].unit_cell()
            uc_params_conv = group['best_subsym'].unit_cell().parameters()

            sorted_pg_comp = sorted(cluster.pg_composition.items(),
                                    key=lambda x: -1 * x[1])
            pg_strings = [
                "{} in {}".format(pg[1], pg[0]) for pg in sorted_pg_comp
            ]
            point_group_string = ", ".join(pg_strings) + "."
            out_str += point_group_string
            out_str += ("\n{:<16} {:<8} {:<6.2f}({:<5.2f}) {:<6.2f}({:<5.2f})"
                        " {:<6.2f}({:<5.2f}) {:<6.2f}({:<4.2f}) {:<6.2f}"
                        "({:<4.2f}) {:<6.2f}({:<4.2f})").format(
                            cluster.cname, len(cluster.members),
                            cluster.medians[0], cluster.stdevs[0],
                            cluster.medians[1], cluster.stdevs[1],
                            cluster.medians[2], cluster.stdevs[2],
                            cluster.medians[3], cluster.stdevs[3],
                            cluster.medians[4], cluster.stdevs[4],
                            cluster.medians[5], cluster.stdevs[5])
            out_str += ("\n{:>24}  {:<6.2f}{:<7} {:<6.2f}{:<7}"
                        " {:<6.2f}{:<7} {:<6.2f}{:<6} {:<6.2f}"
                        "{:<6} {:<6.2f}{:<6}  {:<6.2}").format(
                            SS.getvalue().strip()[13:], uc_params_conv[0], "",
                            uc_params_conv[1], "", uc_params_conv[2], "",
                            uc_params_conv[3], "", uc_params_conv[4], "",
                            uc_params_conv[5], "",
                            group["max_angular_difference"]) + "\n\n"

        else:
            singletons.append("".join([
                ("{:<14} {:<11.2f} {:<11.2f} {:<11.2f}"
                 "{:<12.1f} {:<12.1f} {:<12.1f}").format(
                     cluster.pg_composition.keys()[0],
                     cluster.members[0].uc[0], cluster.members[0].uc[1],
                     cluster.members[0].uc[2], cluster.members[0].uc[3],
                     cluster.members[0].uc[4], cluster.members[0].uc[5]), '\n'
            ]))
    out_str += "\nStandard deviations are in brackets."
    explanation = """\nEach cluster:
Input lattice count, with integration Bravais setting space group.
Cluster median with Niggli cell parameters (std dev in brackets).
Highest possible metric symmetry and unit cell using LePage (J Appl Cryst 1982, 15:255) method, maximum delta 3deg."""
    out_str += explanation
    singleton_str = "\n%i singleton%s:" % plural_s(len(singletons))
    singleton_str += "\n\n{:<14} {:<11} {:<11} {:<11}{:<12} {:<12} {:<12}\n".format(
        "Point group", "a", "b", "c", "alpha", "beta", "gamma")
    singleton_str += "".join(singletons)
    n_clusters = len(sub_clusters) - len(singletons)
    out_str = "\n%i cluster%s:" % plural_s(n_clusters) + out_str
    return singleton_str + out_str
def run(
      args,
      command_name="phenix.reflection_file_converter",
      simply_return_all_miller_arrays=False):
  command_line = (option_parser(
    usage="%s [options] reflection_file ..." % command_name,
    description="Example: %s w1.sca --mtz ." % command_name)
    .enable_symmetry_comprehensive()
    .option(None, "--weak_symmetry",
      action="store_true",
      default=False,
      help="symmetry on command line is weaker than symmetry found in files")
    .enable_resolutions()
    .option(None, "--label",
      action="store",
      type="string",
      help="Substring of reflection data label or number",
      metavar="STRING")
    .option(None, "--non_anomalous",
      action="store_true",
      default=False,
      help="Averages Bijvoet mates to obtain a non-anomalous array")
    .option(None, "--r_free_label",
      action="store",
      type="string",
      help="Substring of reflection data label or number",
      metavar="STRING")
    .option(None, "--r_free_test_flag_value",
      action="store",
      type="int",
      help="Value in R-free array indicating assignment to free set.",
      metavar="FLOAT")
    .option(None, "--generate_r_free_flags",
      action="store_true",
      default=False,
      help="Generates a new array of random R-free flags"
           " (MTZ and CNS output only).")
    .option(None, "--use_lattice_symmetry_in_r_free_flag_generation",
      dest="use_lattice_symmetry_in_r_free_flag_generation",
      action="store_true",
      default=True,
      help="group twin/pseudo symmetry related reflections together"
           " in r-free set (this is the default).")
    .option(None, "--no_lattice_symmetry_in_r_free_flag_generation",
      dest="use_lattice_symmetry_in_r_free_flag_generation",
      action="store_false",
      help="opposite of --use-lattice-symmetry-in-r-free-flag-generation")
    .option(None, "--r_free_flags_fraction",
      action="store",
      default=0.10,
      type="float",
      help="Target fraction free/work reflections (default: 0.10).",
      metavar="FLOAT")
    .option(None, "--r_free_flags_max_free",
      action="store",
      default=2000,
      type="int",
      help="Maximum number of free reflections (default: 2000).",
      metavar="INT")
    .option(None, "--r_free_flags_format",
      choices=("cns", "ccp4", "shelx"),
      default="cns",
      help="Convention for generating R-free flags",
      metavar="cns|ccp4")
    .option(None, "--output_r_free_label",
      action="store",
      type="string",
      help="Label for newly generated R-free flags (defaults to R-free-flags)",
      default="R-free-flags",
      metavar="STRING")
    .option(None, "--random_seed",
      action="store",
      type="int",
      help="Seed for random number generator (affects generation of"
           " R-free flags).",
      metavar="INT")
    .option(None, "--change_of_basis",
      action="store",
      type="string",
      help="Change-of-basis operator: h,k,l or x,y,z"
           " or to_reference_setting, to_primitive_setting, to_niggli_cell,"
           " to_inverse_hand",
      metavar="STRING")
    .option(None, "--eliminate_invalid_indices",
      action="store_true",
      default=False,
      help="Remove indices which are invalid given the change of basis desired")
    .option(None, "--expand_to_p1",
      action="store_true",
      default=False,
      help="Generates all symmetrically equivalent reflections."
           " The space group symmetry is reset to P1."
           " May be used in combination with --change_to_space_group to"
           " lower the symmetry.")
    .option(None, "--change_to_space_group",
      action="store",
      type="string",
      help="Changes the space group and merges equivalent reflections"
           " if necessary",
      metavar="SYMBOL|NUMBER")
    .option(None, "--write_mtz_amplitudes",
      action="store_true",
      default=False,
      help="Converts intensities to amplitudes before writing MTZ format;"
           " requires --mtz_root_label")
    .option(None, "--write_mtz_intensities",
      action="store_true",
      default=False,
      help="Converts amplitudes to intensities before writing MTZ format;"
           " requires --mtz_root_label")
    .option(None,"--remove_negatives",
      action="store_true",
      default=False,
      help="Remove negative intensities or amplitudes from the data set" )
    .option(None,"--massage_intensities",
      action="store_true",
      default=False,
      help="'Treat' negative intensities to get a positive amplitude."
           " |Fnew| = sqrt((Io+sqrt(Io**2 +2sigma**2))/2.0). Requires"
           " intensities as input and the flags --mtz,"
           " --write_mtz_amplitudes and --mtz_root_label.")
    .option(None, "--scale_max",
      action="store",
      type="float",
      help="Scales data such that the maximum is equal to the given value",
      metavar="FLOAT")
    .option(None, "--scale_factor",
      action="store",
      type="float",
      help="Multiplies data with the given factor",
      metavar="FLOAT")
    .option(None, "--sca",
      action="store",
      type="string",
      help=
        "write data to Scalepack FILE ('--sca .' copies name of input file)",
      metavar="FILE")
    .option(None, "--mtz",
      action="store",
      type="string",
      help="write data to MTZ FILE ('--mtz .' copies name of input file)",
      metavar="FILE")
    .option(None, "--mtz_root_label",
      action="store",
      type="string",
      help="Root label for MTZ file (e.g. Fobs)",
      metavar="STRING")
    .option(None, "--cns",
      action="store",
      type="string",
      help="write data to CNS FILE ('--cns .' copies name of input file)",
      metavar="FILE")
    .option(None, "--shelx",
      action="store",
      type="string",
      help="write data to SHELX FILE ('--shelx .' copies name of input file)",
      metavar="FILE")
  ).process(args=args)
  co = command_line.options
  if (co.random_seed is not None):
    random.seed(co.random_seed)
    flex.set_random_seed(value=co.random_seed)
  if (    co.write_mtz_amplitudes
      and co.write_mtz_intensities):
    print
    print "--write_mtz_amplitudes and --write_mtz_intensities" \
          " are mutually exclusive."
    print
    return None
  if (   co.write_mtz_amplitudes
      or co.write_mtz_intensities):
    if (co.mtz_root_label is None):
      print
      print "--write_mtz_amplitudes and --write_mtz_intensities" \
            " require --mtz_root_label."
      print
      return None
  if (    co.scale_max is not None
      and co.scale_factor is not None):
    print
    print "--scale_max and --scale_factor are mutually exclusive."
    print
    return None
  if (len(command_line.args) == 0):
    command_line.parser.show_help()
    return None
  all_miller_arrays = reflection_file_reader.collect_arrays(
    file_names=command_line.args,
    crystal_symmetry=None,
    force_symmetry=False,
    merge_equivalents=False,
    discard_arrays=False,
    verbose=1)
  if (simply_return_all_miller_arrays):
    return all_miller_arrays
  if (len(all_miller_arrays) == 0):
    print
    print "No reflection data found in input file%s." % (
      plural_s(len(command_line.args))[1])
    print
    return None
  label_table = reflection_file_utils.label_table(
    miller_arrays=all_miller_arrays)
  selected_array = label_table.select_array(
    label=co.label, command_line_switch="--label")
  if (selected_array is None): return None
  r_free_flags = None
  r_free_info = None
  if (co.r_free_label is not None):
    r_free_flags = label_table.match_data_label(
      label=co.r_free_label,
      command_line_switch="--r_free_label")
    if (r_free_flags is None):
      return None
    r_free_info = str(r_free_flags.info())
    if (not r_free_flags.is_bool_array()):
      test_flag_value = reflection_file_utils.get_r_free_flags_scores(
        miller_arrays=[r_free_flags],
        test_flag_value=co.r_free_test_flag_value).test_flag_values[0]
      if (test_flag_value is None):
        if (co.r_free_test_flag_value is None):
          raise Sorry(
            "Cannot automatically determine r_free_test_flag_value."
            " Please use --r_free_test_flag_value to specify a value.")
        else:
          raise Sorry("Invalid --r_free_test_flag_value.")
      r_free_flags = r_free_flags.customized_copy(
        data=(r_free_flags.data() == test_flag_value))
  print "Selected data:"
  print " ", selected_array.info()
  print "  Observation type:", selected_array.observation_type()
  print
  if (r_free_info is not None):
    print "R-free flags:"
    print " ", r_free_info
    print
  processed_array = selected_array.customized_copy(
    crystal_symmetry=selected_array.join_symmetry(
      other_symmetry=command_line.symmetry,
      force=not co.weak_symmetry)).set_observation_type(
        selected_array.observation_type())
  if (r_free_flags is not None):
    r_free_flags = r_free_flags.customized_copy(
      crystal_symmetry=processed_array)
  print "Input crystal symmetry:"
  crystal.symmetry.show_summary(processed_array, prefix="  ")
  print
  if (processed_array.unit_cell() is None):
    command_line.parser.show_help()
    print "Unit cell parameters unknown. Please use --symmetry or --unit_cell."
    print
    return None
  if (processed_array.space_group_info() is None):
    command_line.parser.show_help()
    print "Space group unknown. Please use --symmetry or --space_group."
    print
    return None
  if (r_free_flags is not None):
    r_free_flags = r_free_flags.customized_copy(
      crystal_symmetry=processed_array)
  if (co.change_of_basis is not None):
    processed_array, cb_op = processed_array.apply_change_of_basis(
      change_of_basis=co.change_of_basis,
      eliminate_invalid_indices=co.eliminate_invalid_indices)
    if (r_free_flags is not None):
      r_free_flags = r_free_flags.change_basis(cb_op=cb_op)
  if (not processed_array.is_unique_set_under_symmetry()):
    print "Merging symmetry-equivalent values:"
    merged = processed_array.merge_equivalents()
    merged.show_summary(prefix="  ")
    print
    processed_array = merged.array()
    del merged
    processed_array.show_comprehensive_summary(prefix="  ")
    print
  if (r_free_flags is not None
      and not r_free_flags.is_unique_set_under_symmetry()):
    print "Merging symmetry-equivalent R-free flags:"
    merged = r_free_flags.merge_equivalents()
    merged.show_summary(prefix="  ")
    print
    r_free_flags = merged.array()
    del merged
    r_free_flags.show_comprehensive_summary(prefix="  ")
    print
  if (co.expand_to_p1):
    print "Expanding symmetry and resetting space group to P1:"
    if (r_free_flags is not None):
      raise Sorry(
        "--expand_to_p1 not supported for arrays of R-free flags.")
    processed_array = processed_array.expand_to_p1()
    processed_array.show_comprehensive_summary(prefix="  ")
    print
  if (co.change_to_space_group is not None):
    if (r_free_flags is not None):
      raise Sorry(
        "--change_to_space_group not supported for arrays of R-free flags.")
    new_space_group_info = sgtbx.space_group_info(
      symbol=co.change_to_space_group)
    print "Change to space group:", new_space_group_info
    new_crystal_symmetry = crystal.symmetry(
      unit_cell=processed_array.unit_cell(),
      space_group_info=new_space_group_info,
      assert_is_compatible_unit_cell=False)
    if (not new_crystal_symmetry.unit_cell()
              .is_similar_to(processed_array.unit_cell())):
      print "  *************"
      print "  W A R N I N G"
      print "  *************"
      print "  Unit cell parameters adapted to new space group symmetry are"
      print "  significantly different from input unit cell parameters:"
      print "      Input unit cell parameters:", \
        processed_array.unit_cell()
      print "    Adapted unit cell parameters:", \
        new_crystal_symmetry.unit_cell()
    processed_array = processed_array.customized_copy(
      crystal_symmetry=new_crystal_symmetry)
    print
    if (not processed_array.is_unique_set_under_symmetry()):
      print "  Merging values symmetry-equivalent under new symmetry:"
      merged = processed_array.merge_equivalents()
      merged.show_summary(prefix="    ")
      print
      processed_array = merged.array()
      del merged
      processed_array.show_comprehensive_summary(prefix="    ")
      print
  if (processed_array.anomalous_flag() and co.non_anomalous):
    print "Converting data array from anomalous to non-anomalous."
    if (not processed_array.is_xray_intensity_array()):
      processed_array = processed_array.average_bijvoet_mates()
    else:
      processed_array = processed_array.f_sq_as_f()
      processed_array = processed_array.average_bijvoet_mates()
      processed_array = processed_array.f_as_f_sq()
      processed_array.set_observation_type_xray_intensity()
  if (r_free_flags is not None
      and r_free_flags.anomalous_flag()
      and co.non_anomalous):
    print "Converting R-free flags from anomalous to non-anomalous."
    r_free_flags = r_free_flags.average_bijvoet_mates()
  d_max = co.low_resolution
  d_min = co.resolution
  if (d_max is not None or d_min is not None):
    if (d_max is not None):
      print "Applying low resolution cutoff: d_max=%.6g" % d_max
    if (d_min is not None):
      print "Applying high resolution cutoff: d_min=%.6g" % d_min
    processed_array = processed_array.resolution_filter(
      d_max=d_max, d_min=d_min)
    print "Number of reflections:", processed_array.indices().size()
    print
  if (co.scale_max is not None):
    print "Scaling data such that the maximum value is: %.6g" % co.scale_max
    processed_array = processed_array.apply_scaling(target_max=co.scale_max)
    print
  if (co.scale_factor is not None):
    print "Multiplying data with the factor: %.6g" % co.scale_factor
    processed_array = processed_array.apply_scaling(factor=co.scale_factor)
    print

  if (([co.remove_negatives, co.massage_intensities]).count(True) == 2):
    raise Sorry(
      "It is not possible to use --remove_negatives and"
      " --massage_intensities at the same time.")

  if (co.remove_negatives):
    if processed_array.is_real_array():
      print "Removing negatives items"
      processed_array = processed_array.select(
        processed_array.data() > 0)
      if processed_array.sigmas() is not None:
        processed_array = processed_array.select(
          processed_array.sigmas() > 0)
    else:
      raise Sorry("--remove_negatives not applicable to complex data arrays.")

  if (co.massage_intensities):
    if processed_array.is_real_array():
      if processed_array.is_xray_intensity_array():
        if (co.mtz is not None):
          if (co.write_mtz_amplitudes):
            print "The supplied intensities will be used to estimate"
            print " amplitudes in the following way:  "
            print " Fobs = Sqrt[ (Iobs + Sqrt(Iobs**2 + 2sigmaIobs**2))/2 ]"
            print " Sigmas are estimated in a similar manner."
            print
            processed_array = processed_array.enforce_positive_amplitudes()
          else:
            raise Sorry(
              "--write_mtz_amplitudes has to be specified when using"
              " --massage_intensities")
        else:
          raise Sorry("--mtz has to be used when using --massage_intensities")
      else:
        raise Sorry(
          "Intensities must be supplied when using the option"
          " --massage_intensities")
    else:
      raise Sorry(
        "--massage_intensities not applicable to complex data arrays.")

  if (not co.generate_r_free_flags):
    if (r_free_flags is None):
      r_free_info = []
    else:
      if (r_free_flags.anomalous_flag() != processed_array.anomalous_flag()):
        if (processed_array.anomalous_flag()): is_not = ("", " not")
        else:                                  is_not = (" not", "")
        raise Sorry(
          "The data array is%s anomalous but the R-free array is%s.\n"
            % is_not
          + "  Please try --non_anomalous.")
      r_free_info = ["R-free flags source: " + r_free_info]
      if (not r_free_flags.indices().all_eq(processed_array.indices())):
        processed_array = processed_array.map_to_asu()
        r_free_flags = r_free_flags.map_to_asu().common_set(processed_array)
        n_missing_r_free_flags = processed_array.indices().size() \
                               - r_free_flags.indices().size()
        if (n_missing_r_free_flags != 0):
          raise Sorry("R-free flags not compatible with data array:"
           " missing flag for %d reflections selected for output." %
             n_missing_r_free_flags)
  else:
    if (r_free_flags is not None):
      raise Sorry(
        "--r_free_label and --generate_r_free_flags are mutually exclusive.")
    print "Generating a new array of R-free flags:"
    r_free_flags = processed_array.generate_r_free_flags(
      fraction=co.r_free_flags_fraction,
      max_free=co.r_free_flags_max_free,
      use_lattice_symmetry=co.use_lattice_symmetry_in_r_free_flag_generation,
      format=co.r_free_flags_format)
    test_flag_value = True
    if (co.r_free_flags_format == "ccp4") :
      test_flag_value = 0
    elif (co.r_free_flags_format == "shelx") :
      test_flag_value = -1
    r_free_as_bool = r_free_flags.customized_copy(
      data=r_free_flags.data()==test_flag_value)
    r_free_info = [
      "R-free flags generated by %s:" % command_name]
    r_free_info.append("  "+date_and_time())
    r_free_info.append("  fraction: %.6g" % co.r_free_flags_fraction)
    r_free_info.append("  max_free: %s" % str(co.r_free_flags_max_free))
    r_free_info.append("  size of work set: %d" %
      r_free_as_bool.data().count(False))
    r_free_info.append("  size of free set: %d" %
      r_free_as_bool.data().count(True))
    r_free_info_str = StringIO()
    r_free_as_bool.show_r_free_flags_info(prefix="  ", out=r_free_info_str)
    if (co.r_free_flags_format == "ccp4") :
      r_free_info.append("  convention: CCP4 (test=0, work=1-%d)" %
        flex.max(r_free_flags.data()))
    elif (co.r_free_flags_format == "shelx") :
      r_free_info.append("  convention: SHELXL (test=-1, work=1)")
    else :
      r_free_info.append("  convention: CNS/X-PLOR (test=1, work=0)")
    print "\n".join(r_free_info[2:4])
    print r_free_info[-1]
    print r_free_info_str.getvalue()
    print

  n_output_files = 0
  if (co.sca is not None):
    if (co.generate_r_free_flags):
      raise Sorry("Cannot write R-free flags to Scalepack file.")
    file_name = reflection_file_utils.construct_output_file_name(
      input_file_names=[selected_array.info().source],
      user_file_name=co.sca,
      file_type_label="Scalepack",
      file_extension="sca")
    print "Writing Scalepack file:", file_name
    iotbx.scalepack.merge.write(
      file_name=file_name,
      miller_array=processed_array)
    n_output_files += 1
    print
  if (co.mtz is not None):
    file_name = reflection_file_utils.construct_output_file_name(
      input_file_names=[selected_array.info().source],
      user_file_name=co.mtz,
      file_type_label="MTZ",
      file_extension="mtz")
    print "Writing MTZ file:", file_name
    mtz_history_buffer = flex.std_string()
    mtz_history_buffer.append(date_and_time())
    mtz_history_buffer.append("> program: %s" % command_name)
    mtz_history_buffer.append("> input file name: %s" %
      os.path.basename(selected_array.info().source))
    mtz_history_buffer.append("> input directory: %s" %
      os.path.dirname(os.path.abspath(selected_array.info().source)))
    mtz_history_buffer.append("> input labels: %s" %
      selected_array.info().label_string())
    mtz_output_array = processed_array
    if (co.write_mtz_amplitudes):
      if (not mtz_output_array.is_xray_amplitude_array()):
        print "  Converting intensities to amplitudes."
        mtz_output_array = mtz_output_array.f_sq_as_f()
        mtz_history_buffer.append("> Intensities converted to amplitudes.")
    elif (co.write_mtz_intensities):
      if (not mtz_output_array.is_xray_intensity_array()):
        print "  Converting amplitudes to intensities."
        mtz_output_array = mtz_output_array.f_as_f_sq()
        mtz_history_buffer.append("> Amplitudes converted to intensities.")
    column_root_label = co.mtz_root_label
    if (column_root_label is None):
      # XXX 2013-03-29: preserve original root label by default
      # XXX 2014-12-16: skip trailing "(+)" in root_label if anomalous
      column_root_label = selected_array.info().labels[0]
    column_root_label=remove_anomalous_suffix_if_necessary(
      miller_array=selected_array,
      column_root_label=column_root_label)
    mtz_dataset = mtz_output_array.as_mtz_dataset(
      column_root_label=column_root_label)
    del mtz_output_array
    if (r_free_flags is not None):
      mtz_dataset.add_miller_array(
        miller_array=r_free_flags,
        column_root_label=co.output_r_free_label)
      for line in r_free_info:
        mtz_history_buffer.append("> " + line)
    mtz_history_buffer.append("> output file name: %s" %
      os.path.basename(file_name))
    mtz_history_buffer.append("> output directory: %s" %
      os.path.dirname(os.path.abspath(file_name)))
    mtz_object = mtz_dataset.mtz_object()
    mtz_object.add_history(mtz_history_buffer)
    mtz_object.write(file_name=file_name)
    n_output_files += 1
    print
  if (co.cns is not None):
    file_name = reflection_file_utils.construct_output_file_name(
      input_file_names=[selected_array.info().source],
      user_file_name=co.cns,
      file_type_label="CNS",
      file_extension="cns")
    print "Writing CNS file:", file_name
    processed_array.export_as_cns_hkl(
      file_object=open(file_name, "w"),
      file_name=file_name,
      info=["source of data: "+str(selected_array.info())] + r_free_info,
      r_free_flags=r_free_flags)
    n_output_files += 1
    print
  if (co.shelx is not None):
    if (co.generate_r_free_flags):
      raise Sorry("Cannot write R-free flags to SHELX file.")
    file_name = reflection_file_utils.construct_output_file_name(
      input_file_names=[selected_array.info().source],
      user_file_name=co.shelx,
      file_type_label="SHELX",
      file_extension="shelx")
    print "Writing SHELX file:", file_name
    processed_array.as_amplitude_array().export_as_shelx_hklf(
      open(file_name, "w"))
    n_output_files += 1
    print
  if (n_output_files == 0):
    command_line.parser.show_help()
    print "Please specify at least one output file format,",
    print "e.g. --mtz, --sca, etc."
    print
    return None
  return processed_array
def run(args, command_name=libtbx.env.dispatcher_name):
  if (len(args) == 0): args = ["--help"]
  command_line = (option_parser(
    usage='%s pdb_file "atom_selection" [...]' % command_name)
    .option(None, "--write_pdb_file",
      action="store",
      type="string",
      default=None,
      help="write selected atoms to new PDB file",
      metavar="FILE")
    .option(None, "--cryst1_replacement_buffer_layer",
      action="store",
      type="float",
      default=None,
      help="replace CRYST1 with pseudo unit cell covering the selected"
        " atoms plus a surrounding buffer layer",
      metavar="WIDTH")
  ).process(args=args, min_nargs=2)
  co = command_line.options
  mon_lib_srv = server.server()
  ener_lib = server.ener_lib()
  processed_pdb_file = pdb_interpretation.process(
    mon_lib_srv=mon_lib_srv,
    ener_lib=ener_lib,
    file_name=command_line.args[0],
    log=sys.stdout)
  print
  acp = processed_pdb_file.all_chain_proxies

  hierarchy=acp.pdb_hierarchy
  asc=hierarchy.atom_selection_cache()
  sel=asc.selection(string = "chain 'A' and resid 1 through 8 and icode ' '")
  h1=hierarchy.select(sel)  # keep original hierarchy too
  print h1.as_pdb_string()


  selection_cache = acp.pdb_hierarchy.atom_selection_cache()
  atoms = acp.pdb_atoms
  all_bsel = flex.bool(atoms.size(), False)
  for selection_string in command_line.args[1:]:
    print selection_string
    isel = acp.iselection(string=selection_string, cache=selection_cache)
    all_bsel.set_selected(isel, True)
    if (not co.write_pdb_file):
      print "  %d atom%s selected" % plural_s(isel.size())
      for atom in atoms.select(isel):
        print "    %s" % atom.format_atom_record()
  print
  if (co.write_pdb_file):
    print "Writing file:", show_string(co.write_pdb_file)
    sel_hierarchy = acp.pdb_hierarchy.select(all_bsel)
    if (co.cryst1_replacement_buffer_layer is None):
      crystal_symmetry = acp.special_position_settings
    else:
      import cctbx.crystal
      crystal_symmetry = cctbx.crystal.non_crystallographic_symmetry(
        sites_cart=sel_hierarchy.atoms().extract_xyz(),
        buffer_layer=co.cryst1_replacement_buffer_layer)
    write_whole_pdb_file(
        file_name=co.write_pdb_file,
        processed_pdb_file=processed_pdb_file,
        pdb_hierarchy=sel_hierarchy,
        crystal_symmetry=crystal_symmetry)
    print
示例#25
0
    def __init__(self,
                 mtz_object,
                 custom_cif_labels_dict=None,
                 log=None,
                 test_flag_value=None):

        self.cif_blocks = {'xray': None, 'neutron': None}

        if log is None: log = sys.stdout

        miller_arrays = mtz_object.as_miller_arrays()

        miller_arrays_as_cif_block = None

        input_observations_xray = None
        input_observations_neutron = None
        r_free_xray = None
        r_free_neutron = None
        f_obs_filtered_xray = None
        f_obs_filtered_neutron = None

        mtz_to_cif_labels_dict = {}
        mtz_to_cif_labels_dict.update(phenix_to_cif_labels_dict)
        mtz_to_cif_labels_dict.update(ccp4_to_cif_labels_dict)
        if custom_cif_labels_dict is not None:
            mtz_to_cif_labels_dict.update(custom_cif_labels_dict)

        unknown_mtz_labels = []

        for array in miller_arrays:
            labels = array.info().labels
            label = labels[0]
            if reflection_file_utils.looks_like_r_free_flags_info(
                    array.info()):
                if "(+)" in label:
                    array = array.average_bijvoet_mates()
                    labels = [label.replace("(+)", "")]
                if label.endswith(("neutron", "_N")):
                    r_free_neutron = array
                else:
                    r_free_xray = array
                continue  # deal with these later
            elif label.startswith("F-obs-filtered"):
                if label.endswith(("neutron", "_N")):
                    f_obs_filtered_neutron = array
                else:
                    f_obs_filtered_xray = array
            elif label.startswith("F-obs") or label.startswith("I-obs"):
                if label.strip("(+)").endswith(("neutron", "_N")):
                    input_observations_neutron = array
                else:
                    input_observations_xray = array
            #elif label.startswith("R-free-flags"):
            column_names = []
            for mtz_label in labels:
                cif_label = mtz_to_cif_label(mtz_to_cif_labels_dict, mtz_label)
                column_names.append(cif_label)
            if column_names.count(None) > 0:
                # I don't know what to do with this array
                for i, mtz_label in enumerate(labels):
                    if column_names[i] is None:
                        unknown_mtz_labels.append(mtz_label)
                continue
            assert column_names.count(None) == 0
            if labels[0].strip("(+)").endswith(("neutron", "_N")):
                data_type = "neutron"
            else:
                data_type = "xray"
            if column_names[0].startswith(
                ("_refln.F_meas", "_refln.F_squared_meas", "_refln.pdbx_F_",
                 "_refln.pdbx_I_")):
                if data_type == "neutron":
                    input_observations_neutron = array
                else:
                    input_observations_xray = array

            if self.cif_blocks.get(data_type) is None:
                self.cif_blocks[
                    data_type] = iotbx.cif.miller_arrays_as_cif_block(
                        array=array, column_names=column_names, format="mmcif")
            else:
                self.cif_blocks[data_type].add_miller_array(
                    array, column_names=column_names)

        if len(unknown_mtz_labels):
            print >> log, "Warning: Unknown mtz label%s: %s" % (plural_s(
                len(unknown_mtz_labels))[1], ", ".join(unknown_mtz_labels))
            print >> log, "  Use mtz_labels and cif_labels keywords to provide translation for custom labels."

        data_types = set(["xray"])
        if self.cif_blocks['neutron'] is not None:
            data_types.add("neutron")

        if input_observations_xray is None and f_obs_filtered_xray is not None:
            self.cif_blocks["xray"].add_miller_array(
                array=f_obs_filtered_xray,
                column_names=('_refln.F_meas_au', '_refln.F_meas_sigma_au'))
        if input_observations_neutron is None and f_obs_filtered_neutron is not None:
            self.cif_blocks["neutron"].add_miller_array(
                array=f_obs_filtered_neutron,
                column_names=('_refln.F_meas_au', '_refln.F_meas_sigma_au'))

        for data_type in data_types:
            if data_type == "xray":
                r_free = r_free_xray
                input_obs = input_observations_xray
                f_obs_filtered = f_obs_filtered_xray
                if (self.cif_blocks["xray"] is None and r_free_xray is not None
                        and self.cif_blocks["neutron"] is not None
                        and r_free_neutron is None):
                    r_free_neutron = r_free_xray
            elif data_type == "neutron":
                r_free = r_free_neutron
                input_obs = input_observations_neutron
                f_obs_filtered = f_obs_filtered_neutron
            if self.cif_blocks[data_type] is not None and r_free is not None:
                self.cif_blocks[data_type].add_miller_array(
                    array=r_free, column_name='_refln.pdbx_r_free_flag')

            if input_obs is None or r_free is None: continue
            if (test_flag_value is None):
                test_flag_value = reflection_file_utils.guess_r_free_flag_value(
                    miller_array=r_free)
            assert (test_flag_value is not None)
            refln_status = r_free.array(
                data=flex.std_string(r_free.size(), "."))
            input_obs_non_anom = input_obs.average_bijvoet_mates()
            match = r_free.match_indices(input_obs_non_anom)
            refln_status.data().set_selected(match.pair_selection(0), "o")
            refln_status.data().set_selected(r_free.data() == test_flag_value,
                                             "f")
            if f_obs_filtered is not None:
                f_obs_filtered_non_anom = f_obs_filtered.average_bijvoet_mates(
                )
                match = r_free.match_indices(f_obs_filtered_non_anom)
                refln_status.data().set_selected(match.single_selection(0),
                                                 "<")  # XXX
            self.cif_blocks[data_type].add_miller_array(
                array=refln_status, column_name="_refln.status")
                lines_out.append(line)
                end_block_parameter_definition = True
            else:
                line_out = line
                for p in parameter_names:
                    if (l.startswith("{===>}%s=" % p) and l.endswith(";")):
                        parameters_found[p] += 1
                        line_out = '{===>} %s=%s;' % (p, vars()[p])
                        if (line_out != line): parameters_changed.append(p)
                        break
                lines_out.append(line_out)
    if (parameters_found.values().count(1) != 7):
        raise Sorry("Unexpected set of variable names in %s:\n  counts: %s" %
                    (show_string(target), str(parameters_found)))
    elif (len(parameters_changed) == 0):
        print "Info: no changes, %s was not modified." % show_string(target)
    else:
        string_out = "\n".join(lines_out)
        print "Info: %d change%s" % plural_s(len(parameters_changed)), \
          "(%s)," % ", ".join(parameters_changed), \
          "writing modified file %s." % show_string(target)
        try:
            print >> open(target, "w"), string_out
        except IOError, e:
            raise Sorry("Error writing file %s (%s)" %
                        (show_string(target), str(e)))


if (__name__ == "__main__"):
    run(sys.argv[1:])
示例#27
0
  def __init__(self, mtz_object, custom_cif_labels_dict=None, log=None,
      test_flag_value=None):

    self.cif_blocks = {
      'xray': None,
      'neutron': None
    }

    if log is None: log = sys.stdout

    miller_arrays = mtz_object.as_miller_arrays()

    miller_arrays_as_cif_block = None

    input_observations_xray = None
    input_observations_neutron = None
    r_free_xray = None
    r_free_neutron = None
    f_obs_filtered_xray = None
    f_obs_filtered_neutron = None

    mtz_to_cif_labels_dict = {}
    mtz_to_cif_labels_dict.update(phenix_to_cif_labels_dict)
    mtz_to_cif_labels_dict.update(ccp4_to_cif_labels_dict)
    if custom_cif_labels_dict is not None:
      mtz_to_cif_labels_dict.update(custom_cif_labels_dict)

    unknown_mtz_labels = []

    for array in miller_arrays:
      labels = array.info().labels
      label = labels[0]
      if reflection_file_utils.looks_like_r_free_flags_info(array.info()):
        if "(+)" in label:
          array = array.average_bijvoet_mates()
          labels = [label.replace("(+)", "")]
        if label.endswith(("neutron", "_N")):
          r_free_neutron = array
        else:
          r_free_xray = array
        continue # deal with these later
      elif label.startswith("F-obs-filtered"):
        if label.endswith(("neutron", "_N")):
          f_obs_filtered_neutron = array
        else:
          f_obs_filtered_xray = array
      elif label.startswith("F-obs") or label.startswith("I-obs"):
        if label.strip("(+)").endswith(("neutron", "_N")):
          input_observations_neutron = array
        else:
          input_observations_xray = array
      #elif label.startswith("R-free-flags"):
      column_names = []
      for mtz_label in labels:
        cif_label = mtz_to_cif_label(mtz_to_cif_labels_dict, mtz_label)
        column_names.append(cif_label)
      if column_names.count(None) > 0:
        # I don't know what to do with this array
        for i, mtz_label in enumerate(labels):
          if column_names[i] is None:
            unknown_mtz_labels.append(mtz_label)
        continue
      assert column_names.count(None) == 0
      if labels[0].strip("(+)").endswith(("neutron", "_N")):
        data_type = "neutron"
      else:
        data_type = "xray"
      if column_names[0].startswith(("_refln.F_meas",
                                     "_refln.F_squared_meas",
                                     "_refln.pdbx_F_",
                                     "_refln.pdbx_I_")):
        if data_type == "neutron":
          input_observations_neutron = array
        else:
          input_observations_xray = array

      if self.cif_blocks.get(data_type) is None:
        self.cif_blocks[data_type] = iotbx.cif.miller_arrays_as_cif_block(
          array=array, column_names=column_names, format="mmcif")
      else:
        self.cif_blocks[data_type].add_miller_array(array, column_names=column_names)

    if len(unknown_mtz_labels):
      print >> log, "Warning: Unknown mtz label%s: %s" %(
        plural_s(len(unknown_mtz_labels))[1], ", ".join(unknown_mtz_labels))
      print >> log, "  Use mtz_labels and cif_labels keywords to provide translation for custom labels."

    data_types = set(["xray"])
    if self.cif_blocks['neutron'] is not None:
      data_types.add("neutron")

    if input_observations_xray is None and f_obs_filtered_xray is not None:
      self.cif_blocks["xray"].add_miller_array(
        array=f_obs_filtered_xray,
        column_names=('_refln.F_meas_au','_refln.F_meas_sigma_au'))
    if input_observations_neutron is None and f_obs_filtered_neutron is not None:
      self.cif_blocks["neutron"].add_miller_array(
        array=f_obs_filtered_neutron,
        column_names=('_refln.F_meas_au','_refln.F_meas_sigma_au'))

    for data_type in data_types:
      if data_type == "xray":
        r_free = r_free_xray
        input_obs = input_observations_xray
        f_obs_filtered = f_obs_filtered_xray
        if (self.cif_blocks["xray"] is None and r_free_xray is not None and
            self.cif_blocks["neutron"] is not None and r_free_neutron is None):
          r_free_neutron = r_free_xray
      elif data_type == "neutron":
        r_free = r_free_neutron
        input_obs = input_observations_neutron
        f_obs_filtered = f_obs_filtered_neutron
      if self.cif_blocks[data_type] is not None and r_free is not None:
        self.cif_blocks[data_type].add_miller_array(
          array=r_free, column_name='_refln.pdbx_r_free_flag')

      if input_obs is None or r_free is None: continue
      if (test_flag_value is None) :
        test_flag_value = reflection_file_utils.guess_r_free_flag_value(
          miller_array=r_free)
      assert (test_flag_value is not None)
      refln_status = r_free.array(data=flex.std_string(r_free.size(), "."))
      input_obs_non_anom = input_obs.average_bijvoet_mates()
      match = r_free.match_indices(input_obs_non_anom)
      refln_status.data().set_selected(match.pair_selection(0), "o")
      refln_status.data().set_selected(r_free.data() == test_flag_value, "f")
      if f_obs_filtered is not None:
        f_obs_filtered_non_anom = f_obs_filtered.average_bijvoet_mates()
        match = r_free.match_indices(f_obs_filtered_non_anom)
        refln_status.data().set_selected(match.single_selection(0), "<") # XXX
      self.cif_blocks[data_type].add_miller_array(
        array=refln_status, column_name="_refln.status")
def run(args):
    if (len(args) != 2):
        raise Usage("""\
iotbx.cns.transfer_crystal_symmetry any_symmetry_source_file cns_input_file
  *********************************************
  NOTE: the cns_input_file is changed in place.
  *********************************************""")
    #
    for file_name in args:
        if (not os.path.exists(file_name)):
            raise Sorry("No such file: %s" % show_string(file_name))
    source, target = args
    crystal_symmetry = crystal_symmetry_from_any.extract_from(source)
    if (crystal_symmetry is None):
        raise Sorry("Unknown file format or unit cell and/or space group"
                    " missing from file: " + show_string(source))
    cns_space_group_symbol = cns.space_group_symbols.cns_format(
        space_group_info=crystal_symmetry.space_group_info())
    if (cns_space_group_symbol is None):
        raise Sorry("Space group not available in CNS: %s" %
                    show_string(str(crystal_symmetry.space_group_info())))
    sg = '"%s"' % cns_space_group_symbol
    a, b, c, alpha, beta, gamma = [
        "%.6g" % p for p in crystal_symmetry.unit_cell().parameters()
    ]
    parameter_names = ["sg", "a", "b", "c", "alpha", "beta", "gamma"]
    parameters_found = dict(zip(parameter_names, [0] * len(parameter_names)))
    parameters_changed = []
    lines_out = []
    detect_binary = detect_binary_file(monitor_initial=100)
    try:
        cns_inp = open(target).read().splitlines()
    except IOError as e:
        raise Sorry("Error reading file %s (%s)" %
                    (show_string(target), str(e)))
    end_block_parameter_definition = False
    for line in cns_inp:
        if (detect_binary is not None):
            is_binary = detect_binary.is_binary_file(block=line)
            if (is_binary is not None):
                if (is_binary):
                    raise Sorry("%s appears to be a binary file." %
                                show_string(target))
                detect_binary = None
        if (end_block_parameter_definition):
            lines_out.append(line)
        else:
            l = line.strip().replace(" ", "")
            if (l == "){-endblockparameterdefinition-}"):
                lines_out.append(line)
                end_block_parameter_definition = True
            else:
                line_out = line
                for p in parameter_names:
                    if (l.startswith("{===>}%s=" % p) and l.endswith(";")):
                        parameters_found[p] += 1
                        line_out = '{===>} %s=%s;' % (p, vars()[p])
                        if (line_out != line): parameters_changed.append(p)
                        break
                lines_out.append(line_out)
    if (list(parameters_found.values()).count(1) != 7):
        raise Sorry("Unexpected set of variable names in %s:\n  counts: %s" %
                    (show_string(target), str(parameters_found)))
    elif (len(parameters_changed) == 0):
        print("Info: no changes, %s was not modified." % show_string(target))
    else:
        string_out = "\n".join(lines_out)
        print("Info: %d change%s" % plural_s(len(parameters_changed)), \
          "(%s)," % ", ".join(parameters_changed), \
          "writing modified file %s." % show_string(target))
        try:
            print(string_out, file=open(target, "w"))
        except IOError as e:
            raise Sorry("Error writing file %s (%s)" %
                        (show_string(target), str(e)))