def findFlippedSNPs(goldFrqFile1, sourceAlleles, outPrefix):
    """Find flipped SNPs and flip them in the data1."""
    goldAlleles = {}
    with open(goldFrqFile1, "r") as inputFile:
        headerIndex = None
        for i, line in enumerate(inputFile):
            row = createRowFromPlinkSpacedOutput(line)

            if i == 0:
                # This is the header
                headerIndex = dict([
                    (row[j], j) for j in xrange(len(row))
                ])

                # Checking the columns
                for columnName in ["SNP", "A1", "A2"]:
                    if columnName not in headerIndex:
                        msg = "%(fileName)s: no column named " \
                                "%(columnName)s" % locals()
                        raise ProgramError(msg)
            else:
                snpName = row[headerIndex["SNP"]]
                allele1 = row[headerIndex["A1"]]
                allele2 = row[headerIndex["A2"]]

                alleles = set([allele1, allele2])
                if "0" in alleles:
                    alleles.remove("0")

                goldAlleles[snpName] = alleles

    # Finding the SNPs to flip
    toFlipOutputFile = None
    try:
        toFlipOutputFile = open(outPrefix + ".snp_to_flip_in_reference", "w")
    except IOError:
        msg = "%(outPrefix)s.snp_to_flip_in_reference: can't write " \
              "file" % locals()
        raise ProgramError(msg)

    toRemoveOutputFile = None
    try:
        toRemoveOutputFile = open(outPrefix + ".snp_to_remove", "w")
    except IOError:
        msg = "%(outPrefix)s.snp_to_remove: can't write file" % locals()
        raise ProgramError(msg)

    toRemoveOutputFileExplanation = None
    try:
        toRemoveOutputFileExplanation = open(
            outPrefix + ".snp_to_remove.explanation",
            "w",
        )
        print >>toRemoveOutputFileExplanation, "\t".join(["Name", "Reason",
                                                          "Alleles 1",
                                                          "Alleles 2"])
    except IOError:
        msg = "%(outPrefix)s.snp_to_remove: can't write file" % locals()
        raise ProgramError(msg)

    for snpName in goldAlleles.iterkeys():
        alleles1 = goldAlleles[snpName]
        alleles2 = sourceAlleles[snpName]

        if (len(alleles1) == 2) and (len(alleles2) == 2):
            # Both are heterozygous
            if (({"A", "T"} == alleles1 and {"A", "T"} == alleles2) or
                    ({"C", "G"} == alleles1 and {"C", "G"} == alleles2)):
                # We can't flip those..., so we remove them
                print >>toRemoveOutputFile, snpName
                print >>toRemoveOutputFileExplanation, "\t".join([
                    snpName,
                    "Undetermined",
                    "".join(alleles1),
                    "".join(alleles2),
                ])
            else:
                if alleles1 != alleles2:
                    # Let's try the flip one
                    if flipGenotype(alleles1) == alleles2:
                        # We need to flip it
                        print >>toFlipOutputFile, snpName
                    else:
                        # Those SNP are discordant...
                        print >>toRemoveOutputFile, snpName
                        print >>toRemoveOutputFileExplanation, "\t".join([
                            snpName,
                            "Invalid",
                            "".join(alleles1),
                            "".join(alleles2),
                        ])
        else:
            # We want to remove this SNP, because there is at least one
            # homozygous individual
            print >>toRemoveOutputFile, snpName
            tmp_allele1 = "".join(alleles1)
            if len(alleles1) == 1:
                tmp_allele1 += tmp_allele1
            tmp_allele2 = "".join(alleles1)
            if len(alleles1) == 1:
                tmp_allele2 += tmp_allele2
            print >>toRemoveOutputFileExplanation, "\t".join([snpName,
                                                              "Homozygous",
                                                              tmp_allele1,
                                                              tmp_allele2])

    # Closing output files
    toFlipOutputFile.close()
    toRemoveOutputFile.close()
    toRemoveOutputFileExplanation.close()