The main function of the module..

    Here are the steps for duplicated samples:

    1.  Prints the options.
    2.  Reads the ``map`` file to gather marker's position
        (:py:func:`readMAP`).
    3.  Reads the ``tfam`` file (:py:func:`readTFAM`).
    4.  Finds the unique markers using the ``map`` file
        (:py:func:`findUniques`).
    5.  Process the ``tped`` file. Write a file containing unique markers in
        ``prefix.unique_snps`` (``tfam`` and ``tped``). Keep in memory
        information about the duplicated markers (``tped``)
        (:py:func:`processTPED`).
    6.  If there are no duplicated markers, the file ``prefix.unique_snps``
        (``tped`` and ``tfam``) are copied to ``prefix.final``.
    7.  If there are duplicated markers, print a ``tped`` and ``tfam`` file
        containing the duplicated markers
        (:py:func:`printDuplicatedTPEDandTFAM`).
    8.  Computes the frequency of the duplicated markers (using Plink) and read
        the output file (:py:func:`computeFrequency`).
    9.  Computes the concordance and pairwise completion of each of the
        duplicated markers (:py:func:`computeStatistics`).
    10. Prints the problematic duplicated markers with a file containing the
        summary of the statistics (completion and pairwise concordance)
        (:py:func:`printProblems`).
    11. Print the pairwise concordance in a file (matrices)
        (:py:func:`printConcordance`).
    12. Choose the best duplicated markers using concordance and completion
        (:py:func:`chooseBestSnps`).
    13. Completes the chosen markers with the remaining duplicated markers
        (:py:func:`createAndCleanTPED`).
    14. Creates the final ``tped`` file, containing the unique markers, the
        chosen duplicated markers that were completed, and the problematic
        duplicated markers (for further analysis). This set excludes markers
        that were used for completing the chosen ones
        (:py:func:`createFinalTPEDandTFAM`).

def main(argString=None):
    """The main function of the module..

    Here are the steps for duplicated samples:

    1.  Prints the options.
    2.  Reads the ``map`` file to gather marker's position
        (:py:func:`readMAP`).
    3.  Reads the ``tfam`` file (:py:func:`readTFAM`).
    4.  Finds the unique markers using the ``map`` file
        (:py:func:`findUniques`).
    5.  Process the ``tped`` file. Write a file containing unique markers in
        ``prefix.unique_snps`` (``tfam`` and ``tped``). Keep in memory
        information about the duplicated markers (``tped``)
        (:py:func:`processTPED`).
    6.  If there are no duplicated markers, the file ``prefix.unique_snps``
        (``tped`` and ``tfam``) are copied to ``prefix.final``.
    7.  If there are duplicated markers, print a ``tped`` and ``tfam`` file
        containing the duplicated markers
        (:py:func:`printDuplicatedTPEDandTFAM`).
    8.  Computes the frequency of the duplicated markers (using Plink) and read
        the output file (:py:func:`computeFrequency`).
    9.  Computes the concordance and pairwise completion of each of the
        duplicated markers (:py:func:`computeStatistics`).
    10. Prints the problematic duplicated markers with a file containing the
        summary of the statistics (completion and pairwise concordance)
        (:py:func:`printProblems`).
    11. Print the pairwise concordance in a file (matrices)
        (:py:func:`printConcordance`).
    12. Choose the best duplicated markers using concordance and completion
        (:py:func:`chooseBestSnps`).
    13. Completes the chosen markers with the remaining duplicated markers
        (:py:func:`createAndCleanTPED`).
    14. Creates the final ``tped`` file, containing the unique markers, the
        chosen duplicated markers that were completed, and the problematic
        duplicated markers (for further analysis). This set excludes markers
        that were used for completing the chosen ones
        (:py:func:`createFinalTPEDandTFAM`).

    """
    # 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 map file
    logger.info("Reading MAP file")
    mapF = readMAP(args.tfile + ".map", args.out)

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

    # Find unique snps
    logger.info("Finding unique SNPs")
    uniqueSNPs = findUniques(mapF)

    # Process the TPED file
    logger.info("Reading TPED file")
    tped, duplicatedSNPs = processTPED(uniqueSNPs, mapF,
                                       args.tfile + ".tped",
                                       args.tfile + ".tfam", args.out)

    if len(tped) == 0:
        logger.info("There are no duplicated SNPs")
        logger.info("  - Creating final TFAM")
        # Copying the files
        # The TFAM
        try:
            shutil.copy(args.out + ".unique_snps.tfam",
                        args.out + ".final.tfam")
        except IOError:
            msg = "%s.unique_snps.tfam: can't copy file to " \
                  "%s.final.tfam" % (args.out, args.out)
            raise ProgramError(msg)

        # The TPED
        logger.info("  - Creating final TPED")
        try:
            shutil.copy(args.out + ".unique_snps.tped",
                        args.out + ".final.tped")
        except IOError:
            msg = "%s.unique_snps.tped: can't copy file to " \
                  "%s.final.tped" % (args.out, args.out)
            raise ProgramError(msg)

    else:
        # We print the TPED and TFAM for the duplicated SNPs
        logger.info("Printing duplicated SNPs TPED and TFAM files")
        printDuplicatedTPEDandTFAM(tped, args.tfile + ".tfam", args.out)

        # Computing the frequency of the duplicated SNPs
        logger.info("Computing duplicated SNPs' frequencies")
        dupSNPsFreq = computeFrequency(args.out + ".duplicated_snps",
                                       args.out)

        # Compute statistics
        logger.info("Computing concordance and completion of duplicated SNPs")
        completion, concordance = computeStatistics(tped, tfam, duplicatedSNPs)

        # Print the statistics
        logger.info("Printing duplicated SNPs summary file and finding errors "
                    "within duplicates")
        snpsToComplete = printProblems(completion, concordance, tped,
                                       duplicatedSNPs, dupSNPsFreq, args.out,
                                       args.frequency_difference)

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

        # Choose the best SNP
        logger.info("Choosing best SNP for each duplicates")
        chosenSNPs, comp, conc = chooseBestSnps(tped, duplicatedSNPs,
                                                completion, concordance,
                                                args.out)

        # Complete the SNPs
        logger.info("Completing chosen duplicates (removing discordant "
                    "genotypes)")
        newTPED, snpToRemove = createAndCleanTPED(
            tped,
            tfam,
            duplicatedSNPs,
            args.out,
            chosenSNPs,
            comp,
            conc,
            snpsToComplete,
            args.tfile + ".tfam",
            args.snp_completion_threshold,
            args.snp_concordance_threshold,
        )

        # Creates the final tped
        logger.info("Writing final TPED and TFAM file")
        createFinalTPEDandTFAM(newTPED, args.out + ".unique_snps",
                               args.out, snpToRemove)