def processTPEDandTFAM(tped, tfam, prefix):
    """Process the TPED and TFAM files.

    :param tped: the name of the ``tped`` file.
    :param tfam: the name of the ``tfam`` file.
    :param prefix: the prefix of the output files.

    :type tped: str
    :type tfam: str
    :type prefix: str

    Copies the original ``tfam`` file into ``prefix.tfam``. Then, it reads the
    ``tped`` file and keeps in memory two sets containing the markers which are
    all failed or which contains only heterozygous genotypes.

    It creates two output files, ``prefix.allFailed`` and ``prefix.allHetero``,
    containing the markers that are all failed and are all heterozygous,
    respectively.

    .. note::
        All heterozygous markers located on the mitochondrial chromosome are
        not remove.

    """
    # Copying the tfam file
    try:
        shutil.copy(tfam, prefix + ".tfam")
    except IOError:
        msg = "%s: can't write file" % prefix + ".tfam"
        raise ProgramError(msg)

    try:
        outputFile = open(prefix + ".tped", "w")
    except IOError:
        msg = "%s: can't write to file" % prefix + ".tped"
        raise ProgramError(msg)

    # The name of the bad SNPs
    allFailed = set()
    allHetero = set()

    with open(tped, 'r') as inputFile:
        for line in inputFile:
            row = line.rstrip("\r\n").split("\t")
            snpInfo = row[:4]
            chromosome = snpInfo[0]
            genotypes = np.array([i.upper() for i in row[4:]])

            # Testing the genotypes
            uniqueGenotypes = np.unique(genotypes)
            if len(uniqueGenotypes) == 1:
                # We have only one kind of genotype, either all homo, all
                # hetero or all no call
                if uniqueGenotypes[0] == "0 0":
                    # This one is a no call
                    allFailed.add(snpInfo[1])
                elif len(set(uniqueGenotypes[0].split(" "))) == 2:
                    # There are two different alleles, hence, hetero
                    if chromosome not in {"26", "MT"}:
                        # The SNP is not on a mitochondrial chromosome
                        # (because we want to keep those)
                        allHetero.add(snpInfo[1])

            # If the SNP is good (neither in allFailed or allHetero), we keep
            # the SNP
            if (snpInfo[1] not in allFailed) and (snpInfo[1] not in allHetero):
                print >>outputFile, "\t".join(row)

    outputFile.close()

    # Now printing the summary files
    # The SNPs with no calls
    fileName = prefix + ".allFailed"
    try:
        with open(fileName, 'w') as outputFile:
            if len(allFailed) > 0:
                print >>outputFile, "\n".join(allFailed)
    except IOError:
        msg = "%(fileName)s: can't write to file" % locals()
        raise ProgramError(msg)

    # The SNPs with only hetero calls
    fileName = prefix + ".allHetero"
    try:
        with open(fileName, 'w') as outputFile:
            if len(allHetero) > 0:
                print >>outputFile, "\n".join(allHetero)
    except IOError:
        msg = "%(fileName)s: can't write to file" % locals()
        raise ProgramError(msg)
def processTPEDandTFAM ( tped , tfam , prefix ) : try : shutil . copy ( tfam , prefix + ".tfam" ) except IOError : msg = "%s: can't write file" % prefix + ".tfam" raise ProgramError ( msg ) try : outputFile = open ( prefix + ".tped" , "w" ) except IOError : msg = "%s: can't write to file" % prefix + ".tped" raise ProgramError ( msg ) allFailed = set ( ) allHetero = set ( ) with open ( tped , 'r' ) as inputFile : for line in inputFile : row = line . rstrip ( "\r\n" ) . split ( "\t" ) snpInfo = row [ : 4 ] chromosome = snpInfo [ 0 ] genotypes = np . array ( [ i . upper ( ) for i in row [ 4 : ] ] ) uniqueGenotypes = np . unique ( genotypes ) if len ( uniqueGenotypes ) == 1 : if uniqueGenotypes [ 0 ] == "0 0" : allFailed . add ( snpInfo [ 1 ] ) elif len ( set ( uniqueGenotypes [ 0 ] . split ( " " ) ) ) == 2 : if chromosome not in { "26" , "MT" } : allHetero . add ( snpInfo [ 1 ] ) if ( snpInfo [ 1 ] not in allFailed ) and ( snpInfo [ 1 ] not in allHetero ) : print >> outputFile , "\t" . join ( row ) outputFile . close ( ) fileName = prefix + ".allFailed" try : with open ( fileName , 'w' ) as outputFile : if len ( allFailed ) > 0 : print >> outputFile , "\n" . join ( allFailed ) except IOError : msg = "%(fileName)s: can't write to file" % locals ( ) raise ProgramError ( msg ) fileName = prefix + ".allHetero" try : with open ( fileName , 'w' ) as outputFile : if len ( allHetero ) > 0 : print >> outputFile , "\n" . join ( allHetero ) except IOError : msg = "%(fileName)s: can't write to file" % locals ( ) raise ProgramError ( msg )
Process the TPED and TFAM files.

    :param tped: the name of the ``tped`` file.
    :param tfam: the name of the ``tfam`` file.
    :param prefix: the prefix of the output files.

    :type tped: str
    :type tfam: str
    :type prefix: str

    Copies the original ``tfam`` file into ``prefix.tfam``. Then, it reads the
    ``tped`` file and keeps in memory two sets containing the markers which are
    all failed or which contains only heterozygous genotypes.

    It creates two output files, ``prefix.allFailed`` and ``prefix.allHetero``,
    containing the markers that are all failed and are all heterozygous,
    respectively.

    .. note::
        All heterozygous markers located on the mitochondrial chromosome are
        not remove.