Process the TPED file.

    :param uniqueSNPs: the unique markers.
    :param mapF: a representation of the ``map`` file.
    :param fileName: the name of the ``tped`` file.
    :param tfam: the name of the ``tfam`` file.
    :param prefix: the prefix of all the files.

    :type uniqueSNPs: dict
    :type mapF: list
    :type fileName: str
    :type tfam: str
    :type prefix: str

    :returns: a tuple with the representation of the ``tped`` file
              (:py:class:`numpy.array`) as first element, and the updated
              position of the duplicated markers in the ``tped``
              representation.

    Copies the ``tfam`` file into ``prefix.unique_snps.tfam``. While reading
    the ``tped`` file, creates a new one (``prefix.unique_snps.tped``)
    containing only unique markers.

def processTPED(uniqueSNPs, mapF, fileName, tfam, prefix):
    """Process the TPED file.

    :param uniqueSNPs: the unique markers.
    :param mapF: a representation of the ``map`` file.
    :param fileName: the name of the ``tped`` file.
    :param tfam: the name of the ``tfam`` file.
    :param prefix: the prefix of all the files.

    :type uniqueSNPs: dict
    :type mapF: list
    :type fileName: str
    :type tfam: str
    :type prefix: str

    :returns: a tuple with the representation of the ``tped`` file
              (:py:class:`numpy.array`) as first element, and the updated
              position of the duplicated markers in the ``tped``
              representation.

    Copies the ``tfam`` file into ``prefix.unique_snps.tfam``. While reading
    the ``tped`` file, creates a new one (``prefix.unique_snps.tped``)
    containing only unique markers.

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

    tped = []
    updatedSNPs = defaultdict(list)
    outputFile = None
    try:
        outputFile = open(prefix + ".unique_snps.tped", "w")
    except IOError:
        msg = "%s: can't write to file" % prefix + ".unique_snps.tped"
        raise ProgramError(msg)
    nbSNP = 0
    with open(fileName, 'r') as inputFile:
        for line in inputFile:
            nbSNP += 1
            row = line.rstrip("\r\n").split("\t")
            snpInfo = row[:4]
            genotype = [i.upper() for i in row[4:]]

            chromosome = snpInfo[0]
            position = snpInfo[3]

            if (chromosome, position) in uniqueSNPs:
                # Printing the new TPED file (unique SNPs only)
                print >>outputFile, "\t".join(snpInfo + genotype)
            else:
                # Saving the TPED file (duplicated samples only)
                currPos = len(tped)
                tped.append(tuple(snpInfo + genotype))
                updatedSNPs[(chromosome, position)].append(currPos)
    outputFile.close()

    if len(mapF) != nbSNP:
        msg = "%(fileName)s: no the same number of SNPs than MAP " \
              "file" % locals()
        raise ProgramError(msg)

    tped = np.array(tped)

    return tped, updatedSNPs