def rank_counts(counts_by_key):
    """Rank keys in a dict by integer count values

    If there are 7 ties for the highest count, then all 7 keys
    (e.g. genes) are rank #1. The next highest key gets rank #8.
    """
    # First get `count`s and number of times that `count` appears
    occurences_by_count = dict([])
    for key in counts_by_key:
        count = counts_by_key[key]
        occurences_by_count[count] = occurences_by_count.get(count, 0) + 1

    sorted_occurences = sorted(occurences_by_count, reverse=True)
    # print("sorted occurences_by_count: " + str(sorted_occurences))

    # Calculate ranks, accounting for ties
    ranks_by_count = dict([])
    rank_counter = 1
    for count in sorted_occurences:
        ranks_by_count[count] = rank_counter
        rank_counter = rank_counter + occurences_by_count[count]
    # print("cite mapping to rank: " + str(ranks_by_count))

    # Put it into a new dict for simplicity
    ranks_by_key = {}
    for key in counts_by_key:
        count = counts_by_key[key]
        ranks_by_key[key] = ranks_by_count[count]

    return ranks_by_key