from typing import Iterable
from typing import Tuple
from typing import Counter


def filter_mutation_set_by_position(mutation_sets: Iterable[Tuple[Tuple[int, int], ...]], limit: int = 10):
  """Return a filtered mutation set, where each position is used a maximum of `limit` times."""
  filtered_mutation_sets = []
  position_counter = Counter()
  for mutation_set in mutation_sets:
    positions = [m[0] for m in mutation_set]
    if any([position_counter[position] >= limit for position in positions]):
      continue
    else:
      position_counter.update(positions)
      filtered_mutation_sets.append(mutation_set)
  return filtered_mutation_sets