Runs the bafRegress function using SGE.

    :param filenames: the set of all sample files.
    :param out_prefix: the output prefix.
    :param extract_filename: the name of the markers to extract.
    :param freq_filename: the name of the file containing the frequency.
    :param options: the other options.

    :type filenames: set
    :type out_prefix: str
    :type extract_filename: str
    :type freq_filename: str
    :type options: argparse.Namespace

def run_bafRegress_sge(filenames, out_prefix, extract_filename, freq_filename,
                       options):
    """Runs the bafRegress function using SGE.

    :param filenames: the set of all sample files.
    :param out_prefix: the output prefix.
    :param extract_filename: the name of the markers to extract.
    :param freq_filename: the name of the file containing the frequency.
    :param options: the other options.

    :type filenames: set
    :type out_prefix: str
    :type extract_filename: str
    :type freq_filename: str
    :type options: argparse.Namespace

    """
    # Checks the environment variable for DRMAA package
    if "DRMAA_LIBRARY_PATH" not in os.environ:
        msg = "could not load drmaa: set DRMAA_LIBRARY_PATH"
        raise ProgramError(msg)

    # Import the drmaa package
    try:
        import drmaa
    except ImportError:
        raise ProgramError("drmaa is not install, install drmaa")

    # Initializing a session
    s = drmaa.Session()
    s.initialize()

    # The base command
    base_command = [
        "bafRegress.py",
        "estimate",
        "--freqfile", freq_filename,
        "--freqcol", "2,5",
        "--extract", extract_filename,
        "--colsample", options.colsample,
        "--colmarker", options.colmarker,
        "--colbaf", options.colbaf,
        "--colab1", options.colab1,
        "--colab2", options.colab2,
    ]

    # Creating chunks
    chunks = [
        list(filenames)[i: i+options.sample_per_run_for_sge]
        for i in range(0, len(filenames), options.sample_per_run_for_sge)
    ]

    # Run for each sub task...
    job_ids = []
    job_templates = []
    for i, chunk in enumerate(chunks):
        # Creating the final command
        command = [pipes.quote(token) for token in base_command + chunk]
        command.append(
            "> {}.bafRegress_{}".format(pipes.quote(out_prefix), i+1),
        )
        command = " ".join(command)

        # Creating the job template
        jt = s.createJobTemplate()
        jt.remoteCommand = "bash"
        jt.workingDirectory = os.getcwd()
        jt.jobEnvironment = os.environ
        jt.args = ["-c", command]
        jt.jobName = "_bafRegress_{}".format(i+1)

        # Cluster specifics
        if options.sge_walltime is not None:
            jt.hardWallclockTimeLimit = options.sge_walltime
        if options.sge_nodes is not None:
            native_spec = "-l nodes={}:ppn={}".format(options.sge_nodes[0],
                                                      options.sge_nodes[1])
            jt.nativeSpecification = native_spec

        job_ids.append(s.runJob(jt))
        job_templates.append(jt)

    # Waiting for the jobs to finish
    had_problems = []
    for job_id in job_ids:
        ret_val = s.wait(job_id, drmaa.Session.TIMEOUT_WAIT_FOREVER)
        had_problems.append(ret_val.exitStatus == 0)

    # Deleting the jobs
    for jt in job_templates:
        s.deleteJobTemplate(jt)

    # Closing the session
    s.exit()

    # Checking for problems
    if not all(had_problems):
        raise ProgramError("Some SGE jobs had errors...")

    # Merging the output files
    o_file = None
    try:
        o_file = open(out_prefix + ".bafRegress", "w")
    except IOError:
        msg = "{}: cannot write file".format(out_prefix + ".bafRegress")
        raise ProgramError(msg)

    to_remove = set()
    for i in range(len(chunks)):
        filename = out_prefix + ".bafRegress_{}".format(i+1)
        if not os.path.isfile(filename):
            raise ProgramError("{}: no such file".format(filename))
        to_remove.add(filename)

        with open(filename, "r") as i_file:
            if i == 0:
                # First file, we write everything
                o_file.write(i_file.read())
                continue

            for j, line in enumerate(i_file):
                # Skipping first line
                if j == 0:
                    continue
                o_file.write(line)

    for filename in to_remove:
        # Deleting the chunk
        os.remove(filename)

    # Closing
    o_file.close()