Calculate the amino acid frequencies in a set of SeqRecords.

    Weights for each sequence in the alignment can be given as a list/tuple,
    usually calculated with the sequence_weights function. For convenience, you
    can also pass "weights=True" and the weights will be calculated with
    sequence_weights here.

def aa_counts(aln, weights=None, gap_chars='-.'):
    """Calculate the amino acid frequencies in a set of SeqRecords.

    Weights for each sequence in the alignment can be given as a list/tuple,
    usually calculated with the sequence_weights function. For convenience, you
    can also pass "weights=True" and the weights will be calculated with
    sequence_weights here.
    """
    if weights is None:
        counts = Counter()
        for rec in aln:
            seq_counts = Counter(str(rec.seq))
            counts.update(seq_counts)
    else:
        if weights == True:
            # For convenience
            weights = sequence_weights(aln)
        else:
            assert len(weights) == len(aln), (
                "Length mismatch: weights = %d, alignment = %d"
                % (len(weights), len(aln)))
        counts = defaultdict(float)
        for col in zip(*aln):
            for aa, wt in zip(col, weights):
                counts[aa] += wt

    # Don't count gaps
    for gap_char in gap_chars:
        if gap_char in counts:
            del counts[gap_char]
    return counts