def _select_best_indel(indels):
    """Select the highest quality indel, based on the quality,
    prefering low earlier positions above later positions in
    case of ties."""
    def _indel_by_quality_and_position(indel):
        # The negative position is used to select the first
        # of equally quality indels
        return (float(indel.qual), -indel.pos)

    return max(indels, key = _indel_by_quality_and_position)