def run(params):
    log_out = multi_out()
    log_out.register("log", open(params.logfile, "w"), atexit_send_to=None)
    log_out.register("stdout", sys.stdout)

    libtbx.phil.parse(master_params_str).format(params).show(out=log_out, prefix=" ")

    xac_files = read_path_list(params.lstin, only_exists=True, err_out=log_out)

    if len(xac_files) == 0:
        print >>log_out, "No (existing) files in the list: %s" % params.lstin
        return
    
    if params.method == "brehm_diederichs":
        rb = BrehmDiederichs(xac_files, max_delta=params.max_delta,
                             d_min=params.d_min, min_ios=params.min_ios,
                             nproc=params.nproc, log_out=log_out)
    elif params.method == "selective_breeding":
        rb = KabschSelectiveBreeding(xac_files, max_delta=params.max_delta,
                                     d_min=params.d_min, min_ios=params.min_ios,
                                     nproc=params.nproc, log_out=log_out)
    elif params.method == "reference":
        rb = ReferenceBased(xac_files, params.reference_file, max_delta=params.max_delta,
                            d_min=params.d_min, min_ios=params.min_ios,
                            nproc=params.nproc, log_out=log_out)
    else:
        raise "Unknown method: %s" % params.method

    rb.assign_operators()
    new_files = rb.modify_xds_ascii_files()

    lstout = os.path.splitext(os.path.basename(params.lstin))[0]+"_reindexed.lst"
    ofs = open(lstout, "w")
    ofs.write("\n".join(new_files)+"\n")
    ofs.close()
    print >>log_out, "Reindexing done. For merging, use %s instead!" % lstout

    if params.method == "brehm_diederichs":
        print >>log_out, """
CCTBX-implementation (by Richard Gildea) of the "algorithm 2" of the following paper was used.
For publication, please cite:
 Brehm, W. and Diederichs, K. Breaking the indexing ambiguity in serial crystallography.
 Acta Cryst. (2014). D70, 101-109
 http://dx.doi.org/10.1107/S1399004713025431"""
    elif params.method == "selective_breeding":
        print >>log_out, """
def run(params):
    log_out = multi_out()
    log_out.register("log", open(params.logfile, "w"), atexit_send_to=None)
    log_out.register("stdout", sys.stdout)

    libtbx.phil.parse(master_params_str).format(params).show(out=log_out,
                                                             prefix=" ")

    xac_files = read_path_list(params.lstin, only_exists=True, err_out=log_out)

    if len(xac_files) == 0:
        print >> log_out, "No (existing) files in the list: %s" % params.lstin
        return

    if params.method == "brehm_diederichs":
        rb = BrehmDiederichs(xac_files,
                             max_delta=params.max_delta,
                             d_min=params.d_min,
                             min_ios=params.min_ios,
                             nproc=params.nproc,
                             log_out=log_out)
    elif params.method == "selective_breeding":
        rb = KabschSelectiveBreeding(xac_files,
                                     max_delta=params.max_delta,
                                     d_min=params.d_min,
                                     min_ios=params.min_ios,
                                     nproc=params.nproc,
                                     log_out=log_out)
    elif params.method == "reference":
        import iotbx.file_reader

        ref_file = iotbx.file_reader.any_file(params.reference_file)
        if ref_file.file_type == "hkl":
            ref_arrays = ref_file.file_server.miller_arrays
            if not ref_arrays:
                raise "No arrays in reference file"
            if params.reference_label is not None:
                ref_arrays = filter(
                    lambda x: params.reference_label in x.info().labels,
                    ref_arrays)
                if not ref_arrays:
                    raise "No arrays matched to specified label (%s)" % params.reference_label
                ref_array = ref_arrays[0].as_intensity_array()
            else:
                ref_array = None
                for array in ref_arrays:
                    if array.is_xray_intensity_array():
                        ref_array = array
                        print >> log_out, "Using %s as reference data" % array.info(
                        ).label_string()
                        break
                    elif array.is_xray_amplitude_array():
                        ref_array = array.f_as_f_sq()
                        print >> log_out, "Using %s as reference data" % array.info(
                        ).label_string()
                        break
        elif ref_file.file_type == "pdb":
            import mmtbx.utils
            xrs = ref_file.file_content.xray_structure_simple()
            fmodel_params = mmtbx.command_line.fmodel.fmodel_from_xray_structure_master_params.extract(
            )
            fmodel_params.fmodel.k_sol = 0.35
            fmodel_params.fmodel.b_sol = 50
            fmodel_params.high_resolution = params.d_min
            ref_array = mmtbx.utils.fmodel_from_xray_structure(
                xray_structure=xrs,
                params=fmodel_params).f_model.as_intensity_array()
        else:
            raise "input file type invalid"

        if ref_array is None:
            raise "suitable reference data not found"

        rb = ReferenceBased(xac_files,
                            ref_array,
                            max_delta=params.max_delta,
                            d_min=params.d_min,
                            min_ios=params.min_ios,
                            nproc=params.nproc,
                            log_out=log_out)
    else:
        raise "Unknown method: %s" % params.method

    if rb.bad_files:
        print "%s: %d bad files are included:" % (
            "WARNING" if params.skip_bad_files else "ERROR", len(rb.bad_files))
        for f in rb.bad_files:
            print "  %s" % f
        if not params.skip_bad_files:
            print
            print "You may want to change d_min= or min_ios= parameters to include these files."
            print "Alternatively, specify skip_bad_files=true to ignore these files (they are not included in output files)"
            return

    if params.method == "selective_breeding":
        rb.assign_operators(max_cycle=params.max_cycles)
    else:
        rb.assign_operators()

    rb.show_assign_summary()

    if params.dry_run:
        print >> log_out, "This is dry-run. Exiting here."
    else:
        out_prefix = os.path.splitext(os.path.basename(params.lstin))[0]

        ofs_cell = open(out_prefix + "_reindexed_cells.dat", "w")
        new_files = rb.modify_xds_ascii_files(cells_dat_out=ofs_cell)

        lstout = out_prefix + "_reindexed.lst"
        ofs = open(lstout, "w")
        ofs.write("\n".join(new_files) + "\n")
        ofs.close()
        print >> log_out, "Reindexing done. For merging, use %s instead!" % lstout

    if params.method == "brehm_diederichs":
        print >> log_out, """
CCTBX-implementation (by Richard Gildea) of the "algorithm 2" of the following paper was used.
For publication, please cite:
 Brehm, W. and Diederichs, K. Breaking the indexing ambiguity in serial crystallography.
 Acta Cryst. (2014). D70, 101-109
 http://dx.doi.org/10.1107/S1399004713025431"""
    elif params.method == "selective_breeding":
        print >> log_out, """
def run(params):
    log_out = multi_out()
    log_out.register("log", open(params.logfile, "w"), atexit_send_to=None)
    log_out.register("stdout", sys.stdout)

    libtbx.phil.parse(master_params_str).format(params).show(out=log_out, prefix=" ")

    xac_files = read_path_list(params.lstin, only_exists=True, err_out=log_out)

    if len(xac_files) == 0:
        print >>log_out, "No (existing) files in the list: %s" % params.lstin
        return
    
    if params.method == "brehm_diederichs":
        rb = BrehmDiederichs(xac_files, max_delta=params.max_delta,
                             d_min=params.d_min, min_ios=params.min_ios,
                             nproc=params.nproc, log_out=log_out)
    elif params.method == "selective_breeding":
        rb = KabschSelectiveBreeding(xac_files, max_delta=params.max_delta,
                                     d_min=params.d_min, min_ios=params.min_ios,
                                     nproc=params.nproc, log_out=log_out)
    elif params.method == "reference":
        import iotbx.file_reader

        ref_file = iotbx.file_reader.any_file(params.reference_file)
        if ref_file.file_type == "hkl":
            ref_arrays = ref_file.file_server.miller_arrays
            if not ref_arrays:
                raise "No arrays in reference file"
            if params.reference_label is not None:
                ref_arrays = filter(lambda x: params.reference_label in x.info().labels, ref_arrays)
                if not ref_arrays: raise "No arrays matched to specified label (%s)" % params.reference_label
                ref_array = ref_arrays[0].as_intensity_array()
            else:
                ref_array = None
                for array in ref_arrays:
                    if array.is_xray_intensity_array():
                        ref_array = array
                        print >>log_out, "Using %s as reference data" % array.info().label_string()
                        break
                    elif array.is_xray_amplitude_array():
                        ref_array = array.f_as_f_sq()
                        print >>log_out, "Using %s as reference data" % array.info().label_string()
                        break
        elif ref_file.file_type == "pdb":
            import mmtbx.utils
            xrs = ref_file.file_content.xray_structure_simple()
            fmodel_params = mmtbx.command_line.fmodel.fmodel_from_xray_structure_master_params.extract()
            fmodel_params.fmodel.k_sol = 0.35
            fmodel_params.fmodel.b_sol = 50
            fmodel_params.high_resolution = params.d_min
            ref_array = mmtbx.utils.fmodel_from_xray_structure(xray_structure=xrs, params=fmodel_params).f_model.as_intensity_array()
        else:
            raise "input file type invalid"

        if ref_array is None:
            raise "suitable reference data not found"

        rb = ReferenceBased(xac_files, ref_array, max_delta=params.max_delta,
                            d_min=params.d_min, min_ios=params.min_ios,
                            nproc=params.nproc, log_out=log_out)
    else:
        raise "Unknown method: %s" % params.method

    if params.method == "selective_breeding":
        rb.assign_operators(max_cycle=params.max_cycles)
    else:
        rb.assign_operators()

    out_prefix = os.path.splitext(os.path.basename(params.lstin))[0]

    ofs_cell = open(out_prefix+"_reindexed_cells.dat", "w")
    new_files = rb.modify_xds_ascii_files(cells_dat_out=ofs_cell)

    lstout = out_prefix + "_reindexed.lst"
    ofs = open(lstout, "w")
    ofs.write("\n".join(new_files)+"\n")
    ofs.close()
    print >>log_out, "Reindexing done. For merging, use %s instead!" % lstout

    if params.method == "brehm_diederichs":
        print >>log_out, """
CCTBX-implementation (by Richard Gildea) of the "algorithm 2" of the following paper was used.
For publication, please cite:
 Brehm, W. and Diederichs, K. Breaking the indexing ambiguity in serial crystallography.
 Acta Cryst. (2014). D70, 101-109
 http://dx.doi.org/10.1107/S1399004713025431"""
    elif params.method == "selective_breeding":
        print >>log_out, """
Exemple #4
0
"""
(c) RIKEN 2015. All rights reserved. 
Author: Keitaro Yamashita

This software is released under the new BSD License; see LICENSE.
"""
from yamtbx.dataproc.auto.multi_merging.resolve_reindex import BrehmDiederichs

if __name__ == "__main__":
    import sys
    lst = sys.argv[1]
    xac_files = map(lambda x: x.strip(), open(lst))

    rb = BrehmDiederichs(xac_files, log_out=sys.stdout)
    rb.assign_operators()
    rb.modify_xds_ascii_files()
"""
(c) RIKEN 2015. All rights reserved. 
Author: Keitaro Yamashita

This software is released under the new BSD License; see LICENSE.
"""
from yamtbx.dataproc.auto.multi_merging.resolve_reindex import BrehmDiederichs

if __name__ == "__main__":
    import sys

    lst = sys.argv[1]
    xac_files = map(lambda x: x.strip(), open(lst))

    rb = BrehmDiederichs(xac_files, log_out=sys.stdout)
    rb.assign_operators()
    rb.modify_xds_ascii_files()