Extract a list of SNPs using Plink.

def extractSNPs(prefixes, snpToExtractFileNames, outPrefixes, runSGE):
    """Extract a list of SNPs using Plink."""
    s = None
    jobIDs = []
    jobTemplates = []
    if runSGE:
        # Add the environment variable for DRMAA package
        if "DRMAA_LIBRARY_PATH" not in os.environ:
            t = "/shares/data/sge/lib/lx24-amd64/libdrmaa.so.1.0"
            os.environ["DRMAA_LIBRARY_PATH"] = t

        # 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()

    for k, prefix in enumerate(prefixes):
        plinkCommand = ["plink", "--noweb", "--bfile", prefix, "--extract",
                        snpToExtractFileNames[k], "--make-bed", "--out",
                        outPrefixes[k]]

        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"

            # 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
        # Deleating 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)