def consensus(aln, weights=None, gap_threshold=0.5, simple=False, trim_ends=True):
    if simple:
        col_consensus = make_simple_col_consensus(alnutils.aa_frequencies(aln))
        def is_majority_gap(col):
            return (float(col.count('-')) / len(col) >= gap_threshold)
    else:
        if weights is None:
            seq_weights = alnutils.sequence_weights(aln, 'avg1')
        else:
            seq_weights = weights
        aa_frequencies = alnutils.aa_frequencies(aln, weights=seq_weights)
        col_consensus = make_entropy_col_consensus(aa_frequencies)
        def is_majority_gap(col):
            gap_count = 0.0
            for wt, char in zip(seq_weights, col):
                if char == '-':
                    gap_count += wt
            return (gap_count / sum(seq_weights) >= gap_threshold)
    def col_wise_consensus(columns):
        if not trim_ends:
            in_left_end = True
            maybe_right_tail = []
        for col in columns:
            if all(c.islower() for c in col if c not in '.-'):
                yield '-'
                continue
            if any(c.islower() for c in col):
                logging.warn('Mixed lowercase and uppercase letters in a '
                        'column: ' + ''.join(col))
                col = map(str.upper, col)
            is_gap = is_majority_gap(col)
            if not trim_ends:
                if in_left_end:
                    if not is_gap:
                        in_left_end = False
                    is_gap = False
            if is_gap and trim_ends:
                yield '-'
                continue
            cons_char = col_consensus(col)
            if trim_ends:
                yield cons_char
            else:
                if is_gap:
                    maybe_right_tail.append(cons_char)
                else:
                    for char in maybe_right_tail:
                        yield '-'
                    maybe_right_tail = []
                    yield cons_char
        if not trim_ends:
            for char in maybe_right_tail:
                yield char
    return ''.join(col_wise_consensus(zip(*aln)))