Append a tfile to another, creating a new one.

    :param tped: the ``tped`` that will be appended to the other one.
    :param tfam: the ``tfam`` that will be appended to the other one.
    :param prefix: the prefix of all the files.
    :param toAddPrefix: the prefix of the final file.

    :type tped: :py:class:`numpy.array`
    :type tfam: :py:class:`numpy.array`
    :type prefix: str
    :type toAddPrefix: str

    Here are the steps of this function:

    1. Writes the ``tped`` into ``prefix.chosen_samples.tped``.
    2. Writes the ``tfam`` into ``prefix.chosen_samples.tfam``.
    3. Copies the previous ``tfam`` (``toAddPrefix.tfam``) into the final
       ``tfam`` (``prefix.final.tfam``).
    4. Append the ``tfam`` to the final ``tfam`` (``prefix.final.tfam``).
    5. Reads the previous ``tped`` (``toAddPrefix.tped``) and append the new
       ``tped`` to it, writing the final one (``prefix.final.tped``).

    .. warning::
        The ``tped`` and ``tfam`` variables need to contain at least one
        sample.

def addToTPEDandTFAM(tped, tfam, prefix, toAddPrefix):
    """Append a tfile to another, creating a new one.

    :param tped: the ``tped`` that will be appended to the other one.
    :param tfam: the ``tfam`` that will be appended to the other one.
    :param prefix: the prefix of all the files.
    :param toAddPrefix: the prefix of the final file.

    :type tped: :py:class:`numpy.array`
    :type tfam: :py:class:`numpy.array`
    :type prefix: str
    :type toAddPrefix: str

    Here are the steps of this function:

    1. Writes the ``tped`` into ``prefix.chosen_samples.tped``.
    2. Writes the ``tfam`` into ``prefix.chosen_samples.tfam``.
    3. Copies the previous ``tfam`` (``toAddPrefix.tfam``) into the final
       ``tfam`` (``prefix.final.tfam``).
    4. Append the ``tfam`` to the final ``tfam`` (``prefix.final.tfam``).
    5. Reads the previous ``tped`` (``toAddPrefix.tped``) and append the new
       ``tped`` to it, writing the final one (``prefix.final.tped``).

    .. warning::
        The ``tped`` and ``tfam`` variables need to contain at least one
        sample.

    """
    # First, writing the chosen tped
    tpedFile = None
    try:
        tpedFile = open(prefix + ".chosen_samples.tped", "w")
    except IOError:
        msg = "%(prefix)s.chosen_samples.tped: can't write file" % locals()
        raise ProgramError(msg)
    for row in tped:
        print >>tpedFile, "\t".join(row)
    tpedFile.close()

    # Second, writing the chosen tfam
    tfamFile = None
    try:
        tfamFile = open(prefix + ".chosen_samples.tfam", "w")
    except IOError:
        msg = "%(prefix)s.chosen_samples.tfam: can't write file" % locals()
        raise ProgramError(msg)
    for row in tfam:
        print >>tfamFile, "\t".join(row)
    tfamFile.close()

    # The final TFAM file (copying and appending)
    try:
        shutil.copyfile(toAddPrefix + ".tfam", prefix + ".final.tfam")
    except IOError:
        msg = "%(toAddPrefix)s.tfam: can't copy to " \
              "%(prefix)s.final.tfam" % locals()
        raise ProgramError(msg)
    newTFAM = None
    try:
        newTFAM = open(prefix + ".final.tfam", "a")
    except IOError:
        msg = "%(prefix)s.final.tfam: can't append" % locals()
        raise ProgramError(msg)
    for row in tfam:
        print >>newTFAM, "\t".join(row)
    newTFAM.close()

    # The final TPED file
    newTPED = None
    try:
        newTPED = open(prefix + ".final.tped", "w")
    except IOError:
        msg = "%(prefix)s.final.tped: can't write file" % locals()
        raise ProgramError(msg)
    try:
        with open(toAddPrefix + ".tped", "r") as inputFile:
            for i, line in enumerate(inputFile):
                row = line.rstrip("\r\n").split("\t")
                originalSNP = row[1]
                toAdd = tped[i]
                if originalSNP != toAdd[1]:
                    msg = "%(toAddPrefix)s.tped: not good sort" % locals()
                    raise ProgramError(msg)
                row.extend(list(toAdd[4:]))
                print >>newTPED, "\t".join(row)
    except IOError:
        msg = "%(toAddPrefix)s.tped: no such file" % locals()
        raise ProgramError(msg)
    newTPED.close()