Extract a list of SNPs using Plink.

    :param snpToExtractFileNames: the name of the files which contains the
                                  markers to extract from the original data
                                  set.
    :param referencePrefixes: a list containing the three reference population
                              prefixes (the original data sets).
    :param popNames: a list containing the three reference population names.
    :param outPrefix: the prefix of the output file.
    :param runSGE: Whether using SGE or not.
    :param options: the options.

    :type snpToExtractFileNames: list
    :type referencePrefixes: list
    :type popNames: list
    :type outPrefix: str
    :type runSGE: boolean
    :type options: argparse.Namespace

    Using Plink, extract a set of markers from a list of prefixes.

def extractSNPs(snpToExtractFileNames, referencePrefixes, popNames, outPrefix,
                runSGE, options):
    """Extract a list of SNPs using Plink.

    :param snpToExtractFileNames: the name of the files which contains the
                                  markers to extract from the original data
                                  set.
    :param referencePrefixes: a list containing the three reference population
                              prefixes (the original data sets).
    :param popNames: a list containing the three reference population names.
    :param outPrefix: the prefix of the output file.
    :param runSGE: Whether using SGE or not.
    :param options: the options.

    :type snpToExtractFileNames: list
    :type referencePrefixes: list
    :type popNames: list
    :type outPrefix: str
    :type runSGE: boolean
    :type options: argparse.Namespace

    Using Plink, extract a set of markers from a list of prefixes.

    """
    s = None
    jobIDs = []
    jobTemplates = []
    if runSGE:
        # Add 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 python drmaa library
        try:
            import drmaa
        except ImportError:
            raise ProgramError("drmaa is not install, install drmaa")

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

    zipped = izip(popNames, referencePrefixes, snpToExtractFileNames)
    for popName, refPrefix, snpToExtractFileName in zipped:
        plinkCommand = ["plink", "--noweb", "--bfile", refPrefix, "--extract",
                        snpToExtractFileName, "--make-bed", "--out",
                        outPrefix + "." + popName]

        if runSGE:
            # We run using SGE
            # Creating the job template
            jt = s.createJobTemplate()
            jt.remoteCommand = plinkCommand[0]
            jt.workingDirectory = os.getcwd()
            jt.jobEnvironment = os.environ
            jt.args = plinkCommand[1:]
            jt.jobName = "_plink_extract_snps"

            # 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

            # Running the job
            jobID = s.runJob(jt)

            # Storing the job template and the job ID
            jobTemplates.append(jt)
            jobIDs.append(jobID)

        else:
            # We run normal
            runCommand(plinkCommand)

    if runSGE:
        # We wait for all the jobs to be over
        hadProblems = []
        for jobID in jobIDs:
            retVal = s.wait(jobID, drmaa.Session.TIMEOUT_WAIT_FOREVER)
            hadProblems.append(retVal.exitStatus == 0)

        # The jobs should be finished, so we clean everything
        # Deleting the job template, and exiting the session
        for jt in jobTemplates:
            s.deleteJobTemplate(jt)

        # Closing the connection
        s.exit()

        for hadProblem in hadProblems:
            if not hadProblem:
                msg = "Some SGE jobs had errors..."
                raise ProgramError(msg)