Consensus by simple plurality, unweighted.

    Resolves ties by two heuristics:

    1. Prefer the aa that follows the preceding consensus aa type most often
       in the original sequences.
    2. Finally, prefer the less-common aa type.

def make_simple_col_consensus(bg_freqs):
    """Consensus by simple plurality, unweighted.

    Resolves ties by two heuristics:

    1. Prefer the aa that follows the preceding consensus aa type most often
       in the original sequences.
    2. Finally, prefer the less-common aa type.
    """
    # Hack: use default kwargs to persist across iterations
    def col_consensus(col, prev_col=[], prev_char=[]):
        # Count the amino acid types in this column
        aa_counts = sequtils.aa_frequencies(col)
        assert aa_counts, "Column is all gaps! That's not allowed."
        # Take the most common residue(s)
        best_char, best_score = max(aa_counts.iteritems(),
                                    key=lambda kv: kv[1])
        # Resolve ties
        ties = [aa for aa in aa_counts if aa_counts[aa] == best_score]
        if len(ties) > 1:
            # Breaker #1: most common after the prev. consensus char
            # Resolve a tied col by restricting to rows where the preceding
            # char is the consensus type for that (preceding) col
            if prev_char and prev_col:
                mc_next = Counter(
                        [b for a, b in zip(prev_col, col)
                            if a == prev_char[0] and b in ties]
                        ).most_common()
                ties_next = [x[0] for x in mc_next
                        if x[1] == mc_next[0][1]]
                if ties_next:
                    ties = ties_next
            if len(ties) > 1:
                # Breaker #2: lowest overall residue frequency
                ties.sort(key=lambda aa: bg_freqs[aa])
            best_char = ties[0]
        else:
            assert best_char == ties[0], \
                    'WTF %s != %s[0]' % (best_char, ties)
        # Save values for tie-breaker #1
        prev_col[:] = col
        prev_char[:] = best_char
        return best_char
    return col_consensus