def chooseBestDuplicates(tped, samples, oldSamples, completion,
                         concordance_all, prefix):
    chosenFile = None
    try:
        chosenFile = open(prefix + ".chosen_samples.info", "w")
    except IOError:
        msg = "%(prefix)s.chosen_samples.info: can't write file" % locals()
        raise ProgramError(msg)
    print >>chosenFile, "\t".join(["origIndex", "dupIndex", "famID", "indID"])
    excludedFile = None
    try:
        excludedFile = open(prefix + ".excluded_samples.info", "w")
    except IOError:
        msg = "%(prefix)s.excluded_samples.info: can't write file" % locals()
        raise ProgramError(msg)
    print >>excludedFile, "\t".join(["origIndex", "dupIndex", "famID",
                                     "indID"])
    chosenIndexes = {}
    sampleConcordance = {}
    for sample, indexes in samples.iteritems():
        currCompletion = completion[indexes]
        sortedCompletionIndexes = np.argsort(currCompletion)
        concordance = concordance_all[sample]
        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 sample not in sampleConcordance:
            sampleConcordance[sample] = currConcordance
        sortedConcordanceIndexes = np.argsort(currConcordance)
        nbToCheck = 1
        chosenIndex = None
        while nbToCheck <= len(indexes):
            completionValue = currCompletion[
                sortedCompletionIndexes[nbToCheck*-1]
            ]
            concordanceValue = currConcordance[
                sortedConcordanceIndexes[nbToCheck*-1]
            ]
            completionToConsider = set(
                np.where(currCompletion >= completionValue)[0]
            )
            concordanceToConsider = set(
                np.where(currConcordance >= concordanceValue)[0])
            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 sample ID for {}".format(sample)
            raise ProgramError(msg)
        print >>chosenFile, "\t".join([str(oldSamples[sample][chosenIndex]+1),
                                       str(indexes[chosenIndex]+1), sample[0],
                                       sample[1]])
        for i, index in enumerate(indexes):
            if i != chosenIndex:
                print >>excludedFile, "\t".join([str(oldSamples[sample][i]+1),
                                                 str(index+1), sample[0],
                                                 sample[1]])
        chosenIndexes[sample] = indexes[chosenIndex]
    chosenFile.close()
    excludedFile.close()
    return chosenIndexes, completion, sampleConcordance