Check for duplicated samples in a tfam/tped file.

    :param argString: the options

    :type argString: list

    Here are the steps for the duplicated samples step.

    1.  Prints the options.
    2.  Reads the ``tfam`` file (:py:func:`readTFAM`).
    3.  Separate the duplicated samples from the unique samples
        (:py:func:`findDuplicates`).
    4.  Writes the unique samples into a file named
        ``prefix.unique_samples.tfam`` (:py:func:`printUniqueTFAM`).
    5.  Reads the ``tped`` file and write into ``prefix.unique_samples.tped``
        the pedigree file for the unique samples (:py:func:`processTPED`).
        Saves in memory the pedigree for the duplicated samples. Updates the
        indexes of the duplicated samples.
    6.  If there are no duplicated samples, simply copies the files
        ``prefix.unique_samples`` (``tped`` and ``tfam``) to
        ``prefix.final.tfam`` and ``prefix..final.tped``, respectively.
    7.  Computes the completion (for each of the duplicated samples) and the
        concordance of each sample pairs (:py:func:`computeStatistics`).
    8.  Prints statistics (concordance and completion)
        (:py:func:`printStatistics`).
    9.  We print the concordance matrix for each duplicated samples
        (:py:func:`printConcordance`).
    10. We print the ``tped`` and the ``tfam`` file for the duplicated samples
        (``prefix.duplicated_samples``)
        (:py:func:`printDuplicatedTPEDandTFAM`).
    11. Choose the best of each duplicates (to keep and to complete) according
        to completion and concordance (:py:func:`chooseBestDuplicates`).
    12. Creates a unique ``tped`` and ``tfam`` from the duplicated samples by
        completing the best chosen one with the other samples
        (:py:func:`createAndCleanTPED`).
    13. Merge the two tfiles together (``prefix.unique_samples`` and
        ``prefix.chosen_samples``) to create the final dataset
        (``prefix.final``) (:py:func:`addToTPEDandTFAM`).

def main(argString=None):
    """Check for duplicated samples in a tfam/tped file.

    :param argString: the options

    :type argString: list

    Here are the steps for the duplicated samples step.

    1.  Prints the options.
    2.  Reads the ``tfam`` file (:py:func:`readTFAM`).
    3.  Separate the duplicated samples from the unique samples
        (:py:func:`findDuplicates`).
    4.  Writes the unique samples into a file named
        ``prefix.unique_samples.tfam`` (:py:func:`printUniqueTFAM`).
    5.  Reads the ``tped`` file and write into ``prefix.unique_samples.tped``
        the pedigree file for the unique samples (:py:func:`processTPED`).
        Saves in memory the pedigree for the duplicated samples. Updates the
        indexes of the duplicated samples.
    6.  If there are no duplicated samples, simply copies the files
        ``prefix.unique_samples`` (``tped`` and ``tfam``) to
        ``prefix.final.tfam`` and ``prefix..final.tped``, respectively.
    7.  Computes the completion (for each of the duplicated samples) and the
        concordance of each sample pairs (:py:func:`computeStatistics`).
    8.  Prints statistics (concordance and completion)
        (:py:func:`printStatistics`).
    9.  We print the concordance matrix for each duplicated samples
        (:py:func:`printConcordance`).
    10. We print the ``tped`` and the ``tfam`` file for the duplicated samples
        (``prefix.duplicated_samples``)
        (:py:func:`printDuplicatedTPEDandTFAM`).
    11. Choose the best of each duplicates (to keep and to complete) according
        to completion and concordance (:py:func:`chooseBestDuplicates`).
    12. Creates a unique ``tped`` and ``tfam`` from the duplicated samples by
        completing the best chosen one with the other samples
        (:py:func:`createAndCleanTPED`).
    13. Merge the two tfiles together (``prefix.unique_samples`` and
        ``prefix.chosen_samples``) to create the final dataset
        (``prefix.final``) (:py:func:`addToTPEDandTFAM`).

    """
    # Getting and checking the options
    args = parseArgs(argString)
    checkArgs(args)

    logger.info("Options used:")
    for key, value in vars(args).iteritems():
        logger.info("  --{} {}".format(key.replace("_", "-"), value))

    # Reading the tfam file
    logger.info("Reading TFAM")
    tfam = readTFAM(args.tfile + ".tfam")

    # Find duplicated samples
    logger.info("Finding duplicated samples")
    uniqueSamples, duplicatedSamples = findDuplicates(tfam)

    # Prints the unique tfam
    logger.info("Creating TFAM for unique samples")
    printUniqueTFAM(tfam, uniqueSamples, args.out)

    # Process the TPED file
    logger.info("Reading TPED (and creating TPED for unique samples)")
    tped, tpedSamples = processTPED(uniqueSamples, duplicatedSamples,
                                    args.tfile + ".tped", args.out)

    if len(duplicatedSamples) == 0:
        logger.info("There are no duplicates in {}.tfam".format(args.tfile))
        # There are no duplicated samples
        try:
            shutil.copy(args.out + ".unique_samples.tfam",
                        args.out + ".final.tfam")
        except IOError:
            msg = "%s.unique_samples.tfam: can't copy to " \
                  "%s.final.tfam" % (args.out, args.out)
            raise ProgramError(msg)
        try:
            shutil.copy(args.out + ".unique_samples.tped",
                        args.out + ".final.tped")
        except IOError:
            msg = "%s.unique_samples.tped: can't copy to " \
                  "%s.final.tped" % (args.out, args.out)
            raise ProgramError(msg)

    else:
        # We continue
        # Compute statistics
        logger.info("Computing the completion and concordance of duplicated "
                    "samples")
        completion, concordance = computeStatistics(tped, tfam, tpedSamples,
                                                    duplicatedSamples,
                                                    args.out)

        # Print the statistics
        logger.info("Printing the statistics")
        completion_percentage = printStatistics(completion, concordance,
                                                tpedSamples, duplicatedSamples,
                                                args.out)

        # Print the concordance file
        logger.info("Printing the concordance file")
        concordance_percentage = printConcordance(concordance, args.out)

        # Print the duplicated TFAM and TPED
        logger.info("Creating TPED and TFAM for duplicated samples")
        printDuplicatedTPEDandTFAM(tped, tfam, tpedSamples, duplicatedSamples,
                                   args.out)

        # Choose the best duplicates
        logger.info("Choosing the best duplicates")
        chosenSamples, comp, conc = chooseBestDuplicates(
            tped,
            tpedSamples,
            duplicatedSamples,
            completion_percentage,
            concordance_percentage,
            args.out,
        )

        # Clean the genotype of the chosen samples
        logger.info("Cleaning and creating unique TPED and TFAM from "
                    "duplicated samples")
        newTPED, newTFAM = createAndCleanTPED(
            tped,
            tfam,
            tpedSamples,
            duplicatedSamples,
            chosenSamples,
            args.out,
            comp,
            args.sample_completion_threshold,
            conc,
            args.sample_concordance_threshold,
        )

        # Add the chosen TPED and TFAM
        logger.info("Creating final TPED and TFAM file")
        addToTPEDandTFAM(newTPED, newTFAM, args.out,
                         args.out + ".unique_samples")