Find the overlapping SNPs in 4 different data sets.

def findOverlappingSNPsWithGoldStandard(prefix, gold_prefixe, out_prefix,
                                        use_marker_names=False):
    """Find the overlapping SNPs in 4 different data sets."""
    # Reading the main file
    sourceSnpToExtract = {}
    if use_marker_names:
        sourceSnpToExtract = set()
    duplicates = set()
    try:
        with open(prefix + ".bim", "r") as inputFile:
            for line in inputFile:
                row = line.rstrip("\r\n").split("\t")
                chromosome = row[0]
                position = row[3]
                snpName = row[1]

                if use_marker_names:
                    sourceSnpToExtract.add(snpName)
                else:
                    if (chromosome, position) not in sourceSnpToExtract:
                        sourceSnpToExtract[(chromosome, position)] = snpName
                    else:
                        # It's a duplicate
                        duplicates.add((chromosome, position))
    except IOError:
        msg = "%s.bim: no such file" % prefix
        raise ProgramError(msg)

    # Removing duplicates from the list
    if not use_marker_names:
        logger.info("  - There are {} duplicated markers "
                    "in {};".format(len(duplicates), prefix + ".bim"))
        logger.info("  - removing them for simplicity...")
        for snpID in duplicates:
            del sourceSnpToExtract[snpID]

    # Reading the Gold standard files
    goldSnpToExtract = {}
    if use_marker_names:
        goldSnpToExtract = set()
    with open(gold_prefixe + ".bim", "r") as inputFile:
        for line in inputFile:
            row = line.rstrip("\r\n").split("\t")
            chromosome = row[0]
            position = row[3]
            snpName = row[1]

            if use_marker_names:
                if snpName in sourceSnpToExtract:
                    goldSnpToExtract.add(snpName)
            else:
                if (chromosome, position) in sourceSnpToExtract:
                    # We want this SNP
                    goldSnpToExtract[(chromosome, position)] = snpName

    # Printing the names of the SNPs to extract
    goldOutputFile = None
    try:
        goldOutputFile = open(out_prefix + ".gold_snp_to_extract", "w")
    except IOError:
        msg = "%(out_prefix)s.goldSnpToExtract: can't write file" % locals()
        raise ProgramError(msg)

    sourceOutputFile = None
    try:
        sourceOutputFile = open(out_prefix + ".source_snp_to_extract", "w")
    except IOError:
        msg = "%(out_prefix)s.sourceSnpToExtract: can't write file" % locals()
        raise ProgramError(msg)

    changeNameOutputFile = None
    try:
        changeNameOutputFile = open(out_prefix + ".update_names", "w")
    except IOError:
        msg = "%(out_prefix)s.updateNames: can't write file" % locals()
        raise ProgramError(msg)

    # Writing the file
    if use_marker_names:
        for snpName in goldSnpToExtract:
            print >>sourceOutputFile, snpName
            print >>goldOutputFile, snpName
    else:
        for snpID in goldSnpToExtract.iterkeys():
            print >>sourceOutputFile, sourceSnpToExtract[snpID]
            print >>goldOutputFile, goldSnpToExtract[snpID]
            print >>changeNameOutputFile, "\t".join([
                goldSnpToExtract[snpID],
                sourceSnpToExtract[snpID],
            ])

    # Closing the output file
    goldOutputFile.close()
    sourceOutputFile.close()
    changeNameOutputFile.close()