Process the TPED file.

    :param uniqueSamples: the position of unique samples.
    :param duplicatedSamples: the position of duplicated samples.
    :param fileName: the name of the file.
    :param prefix: the prefix of all the files.

    :type uniqueSamples: dict
    :type duplicatedSamples: collections.defaultdict
    :type fileName: str
    :type prefix: str

    :returns: a tuple containing the ``tped`` (:py:class:`numpy.array`) as
              first element, and the updated positions of the duplicated
              samples (:py:class:`dict`)

    Reads the entire ``tped`` and prints another one containing only unique
    samples (``prefix.unique_samples.tped``). It then creates a
    :py:class:`numpy.array` containing the duplicated samples.

def processTPED(uniqueSamples, duplicatedSamples, fileName, prefix):
    """Process the TPED file.

    :param uniqueSamples: the position of unique samples.
    :param duplicatedSamples: the position of duplicated samples.
    :param fileName: the name of the file.
    :param prefix: the prefix of all the files.

    :type uniqueSamples: dict
    :type duplicatedSamples: collections.defaultdict
    :type fileName: str
    :type prefix: str

    :returns: a tuple containing the ``tped`` (:py:class:`numpy.array`) as
              first element, and the updated positions of the duplicated
              samples (:py:class:`dict`)

    Reads the entire ``tped`` and prints another one containing only unique
    samples (``prefix.unique_samples.tped``). It then creates a
    :py:class:`numpy.array` containing the duplicated samples.

    """
    # Getting the indexes
    uI = sorted(uniqueSamples.values())
    dI = []
    for item in duplicatedSamples.itervalues():
        dI.extend(item)
    dI.sort()

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

            # Printing the new TPED file (unique samples only)
            print >>outputFile, "\t".join(snpInfo + [genotype[i] for i in uI])

            # Saving the TPED file (duplicated samples only)
            tped.append(tuple(snpInfo + [genotype[i] for i in dI]))

    # Closing the output file
    outputFile.close()

    # Updating the positions
    updatedSamples = {}
    for sampleID in duplicatedSamples:
        positions = duplicatedSamples[sampleID]
        newPositions = range(len(positions))
        for i, pos in enumerate(positions):
            newPositions[i] = dI.index(pos)
        updatedSamples[sampleID] = newPositions

    tped = np.array(tped)

    return tped, updatedSamples