Extract significant SNPs in the fisher file.

    :param prefix: the prefix of the input file.

    :type prefix: str

    Reads a list of significant markers (``prefix.assoc.fisher``) after plate
    bias analysis with Plink. Writes a file (``prefix.significant_SNPs.txt``)
    containing those significant markers.

def extractSignificantSNPs(prefix):
    """Extract significant SNPs in the fisher file.

    :param prefix: the prefix of the input file.

    :type prefix: str

    Reads a list of significant markers (``prefix.assoc.fisher``) after plate
    bias analysis with Plink. Writes a file (``prefix.significant_SNPs.txt``)
    containing those significant markers.

    """
    # The list of all assoc files
    fileNames = glob.glob(prefix + ".*.assoc.fisher")

    # The object to save an assoc row
    AssocRow = namedtuple("AssocRow",
                          ["chrom", "pos", "name", "p", "odds", "plate"])

    snpNames = set()
    assocResults = []
    for fileName in fileNames:
        # Getting the plate name
        plateName = re.search(r"/plate_bias\.(\S+)\.assoc\.fisher$", fileName)
        plateName = plateName.group(1) if plateName else "unknown"

        try:
            with open(fileName, 'r') as inputFile:
                headerIndex = None
                for line in inputFile:
                    row = createRowFromPlinkSpacedOutput(line)

                    if headerIndex is None:
                        # This is the header line
                        headerIndex = dict([
                            (row[i], i) for i in xrange(len(row))
                        ])
                        for name in ("CHR", "SNP", "BP", "P", "OR"):
                            if name not in headerIndex:
                                msg = "{}: missing column {}".format(
                                    fileName,
                                    name,
                                )
                                raise ProgramError(msg)

                    else:
                        snpName = row[headerIndex["SNP"]]
                        snpNames.add(snpName)
                        assocResults.append(AssocRow(
                            chrom=row[headerIndex["CHR"]],
                            pos=row[headerIndex["BP"]],
                            name=snpName,
                            p=row[headerIndex["P"]],
                            odds=row[headerIndex["OR"]],
                            plate=plateName,
                        ))

        except IOError:
            msg = "%(fileName)s: no such file" % locals()
            raise ProgramError(msg)

    # The output file
    outputFileName = prefix + ".significant_SNPs.txt"
    outputFile = None
    try:
        outputFile = open(outputFileName, "w")
    except IOError:
        msg = "%(outputFileName)s: can't write file" % locals()
        raise ProgramError(msg)

    if len(snpNames) > 0:
        print >>outputFile, "\n".join(snpNames)

    # Closing the file
    outputFile.close()

    return assocResults