<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def chooseBestSnps(tped, snps, trueCompletion, trueConcordance, prefix):
 """Choose the best duplicates according to the completion and concordance. :param tped: a representation of the ``tped`` of duplicated markers. :param snps: the position of the duplicated markers in the ``tped``. :param trueCompletion: the completion of each markers. :param trueConcordance: the pairwise concordance of each markers. :param prefix: the prefix of the output files. :type tped: numpy.array :type snps: dict :type trueCompletion: numpy.array :type trueConcordance: dict :type prefix: str :returns: a tuple containing the chosen indexes (:py:class:`dict`) as the first element, the completion (:py:class:`numpy.array`) as the second element, and the concordance (:py:class:`dict`) as last element. It creates two output files: ``prefix.chosen_snps.info`` and ``prefix.not_chosen_snps.info``. The first one contains the markers that were chosen for completion, and the second one, the markers that weren't. It starts by computing the completion of each markers (dividing the number of calls divided by the total number of genotypes). Then, for each of the duplicated markers, we choose the best one according to completion and concordance (see explanation in :py:func:`DupSamples.duplicated_samples.chooseBestDuplicates` for more details). """

    # The output files
    chosenFile = None
    try:
        chosenFile = open(prefix + ".chosen_snps.info", "w")
    except IOError:
        msg = "%(prefix)s.chosen_snps.info: can't write file" % locals()
        raise ProgramError(msg)

    excludedFile = None
    try:
        excludedFile = open(prefix + ".not_chosen_snps.info", "w")
    except IOError:
        msg = "%(prefix)s.not_chosen_snps.info: can't write file" % locals()
        raise ProgramError(msg)

    # Computing the completion
    completion = np.true_divide(trueCompletion[0], trueCompletion[1])

    # For each duplicated SNPs
    chosenIndexes = {}
    snpConcordance = {}
    for snp, indexes in snps.iteritems():
        # Getting the completion for those duplicated SNPs
        currCompletion = completion[indexes]

        # Sorting those completion
        sortedCompletionInsexes = np.argsort(currCompletion)

        # Getting the concordance
        concordance = np.true_divide(trueConcordance[snp][0],
                                     trueConcordance[snp][1])

        currConcordance = [[] for i in xrange(len(indexes))]
        for i in xrange(len(indexes)):
            indexToKeep = list(set(range(len(indexes))) - set([i]))
            currConcordance[i] = np.mean(concordance[i, indexToKeep])
        currConcordance = np.array(currConcordance)
        if snp not in snpConcordance:
            snpConcordance[snp] = currConcordance

        # Sorting the concordance
        sortedConcordanceIndexes = np.argsort(currConcordance)

        # Trying to find the best duplicate to keep
        nbToCheck = 1
        chosenIndex = None
        while nbToCheck <= len(indexes):
            # Getting the `nbToCheck` best value (higher to lower)
            completionValue = currCompletion[
                sortedCompletionInsexes[nbToCheck*-1]
            ]
            concordanceValue = currConcordance[
                sortedConcordanceIndexes[nbToCheck*-1]
            ]

            # Getting the indexes to consider
            completionToConsider = set(
                np.where(currCompletion >= completionValue)[0]
            )
            concordanceToConsider = set(
                np.where(currConcordance >= concordanceValue)[0]
            )

            # Getting the intersection of the indexes
            toConsider = concordanceToConsider & completionToConsider
            if len(toConsider) >= 1:
                chosenIndex = random.choice(list(toConsider))
                break
            nbToCheck += 1

        if chosenIndex is None:
            msg = "Could not choose the best snp ID"
            raise ProgramError(msg)

        # Printing the chosen SNPs
        print >>chosenFile, tped[indexes[chosenIndex], 1]

        # Printing the excluded SNPs
        for i, index in enumerate(indexes):
            if i != chosenIndex:
                print >>excludedFile, tped[index, 1]

        chosenIndexes[snp] = indexes[chosenIndex]

    # Closing the output files
    chosenFile.close()
    excludedFile.close()

    return chosenIndexes, completion, snpConcordance