Runs step4 (clean mind).

    :param in_prefix: the prefix of the input files.
    :param in_type: the type of the input files.
    :param out_prefix: the output prefix.
    :param base_dir: the output directory.
    :param options: the options needed.

    :type in_prefix: str
    :type in_type: str
    :type out_prefix: str
    :type base_dir: str
    :type options: list

    :returns: a tuple containing the prefix of the output files (the input
              prefix for the next script) and the type of the output files
              (``bfile``).

    This function calls the
    :py:mod:`pyGenClean.SampleMissingness.sample_missingness` module. The
    required file type for this module is either a ``bfile`` or a ``tfile``,
    hence the need to use the :py:func:`check_input_files` to check if the file
    input file type is the good one, or to create it if needed.

def run_sample_missingness(in_prefix, in_type, out_prefix, base_dir, options):
    """Runs step4 (clean mind).

    :param in_prefix: the prefix of the input files.
    :param in_type: the type of the input files.
    :param out_prefix: the output prefix.
    :param base_dir: the output directory.
    :param options: the options needed.

    :type in_prefix: str
    :type in_type: str
    :type out_prefix: str
    :type base_dir: str
    :type options: list

    :returns: a tuple containing the prefix of the output files (the input
              prefix for the next script) and the type of the output files
              (``bfile``).

    This function calls the
    :py:mod:`pyGenClean.SampleMissingness.sample_missingness` module. The
    required file type for this module is either a ``bfile`` or a ``tfile``,
    hence the need to use the :py:func:`check_input_files` to check if the file
    input file type is the good one, or to create it if needed.

    """
    # Creating the output directory
    os.mkdir(out_prefix)

    # We are looking at what we have
    required_type = "tfile"
    if in_type == "bfile":
        required_type = "bfile"
    check_input_files(in_prefix, in_type, required_type)

    # We need to inject the name of the input file and the name of the output
    # prefix
    script_prefix = os.path.join(out_prefix, "clean_mind")
    options += ["--ifile", in_prefix,
                "--out", script_prefix]
    if required_type == "bfile":
        options.append("--is-bfile")

    # We run the script
    try:
        sample_missingness.main(options)
    except sample_missingness.ProgramError as e:
        msg = "sample_missingness: {}".format(e)
        raise ProgramError(msg)

    # We want to modify the description, so that it contains the option used
    desc = sample_missingness.desc
    mind_value = sample_missingness.parser.get_default("mind")
    if "--mind" in options:
        # Since we already run the script, we know that the mind option is a
        # valid float
        mind_value = options[options.index("--mind") + 1]
    if desc.endswith("."):
        desc = desc[:-1] + r" (${}={}$).".format(latex_template.texttt("mind"),
                                                 mind_value)
    long_description = sample_missingness.long_desc.format(
        success_rate="{:.1f}".format((1-float(mind_value)) * 100),
    )

    # We want to save in a file the samples that were removed
    # There is one file to look at, which contains only one row, the name of
    # the samples:
    #     - prefix.irem (file will exists only if samples were removed)
    nb_samples = 0
    o_filename = os.path.join(base_dir, "excluded_samples.txt")
    i_filename = script_prefix + ".irem"
    if os.path.isfile(i_filename):
        # True, so sample were removed
        with open(i_filename, "r") as i_file, open(o_filename, "a") as o_file:
            for line in i_file:
                nb_samples += 1
                print >>o_file, line.rstrip("\r\n") + "\tmind {}".format(
                    mind_value,
                )

    # We write a LaTeX summary
    latex_file = os.path.join(script_prefix + ".summary.tex")
    try:
        with open(latex_file, "w") as o_file:
            print >>o_file, latex_template.subsection(
                sample_missingness.pretty_name,
            )
            text = ("Using a {} threshold of {} ({} keeping only samples with "
                    r"a missing rate $\leq {}$), {:,d} sample{} {} excluded "
                    "from the dataset.".format(
                        latex_template.texttt("mind"),
                        mind_value,
                        latex_template.textit("i.e."),
                        mind_value,
                        nb_samples,
                        "s" if nb_samples > 1 else "",
                        "were" if nb_samples > 1 else "was",
                    ))
            print >>o_file, latex_template.wrap_lines(text)

    except IOError:
        msg = "{}: cannot write LaTeX summary".format(latex_file)
        raise ProgramError(msg)

    # Writing the summary results
    with open(os.path.join(base_dir, "results_summary.txt"), "a") as o_file:
        print >>o_file, "# {}".format(script_prefix)
        print >>o_file, ("Number of samples with missing rate higher "
                         "than {t}\t{nb:,d}\t\t-{nb:,d}".format(
                            t=mind_value,
                            nb=nb_samples,
                         ))
        print >>o_file, "---"

    # We know this step does produce a new data set (bfile), so we return it
    return _StepResult(
        next_file=os.path.join(out_prefix, "clean_mind"),
        next_file_type="bfile",
        latex_summary=latex_file,
        description=desc,
        long_description=long_description,
        graph_path=None,
    )