def blocks(aln, threshold=<NUM_LIT:0.5>, weights=None):
assert len(aln)<EOL>if weights == False:<EOL><INDENT>def pct_nongaps(col):<EOL><INDENT>return <NUM_LIT:1> - (float(col.count('<STR_LIT:->')) / len(col))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if weights in (None, True):<EOL><INDENT>weights = sequence_weights(aln, '<STR_LIT>')<EOL><DEDENT>def pct_nongaps(col):<EOL><INDENT>assert len(col) == len(weights)<EOL>ngaps = sum(wt * (c == '<STR_LIT:->')<EOL>for wt, c in zip(weights, col))<EOL>return <NUM_LIT:1> - (ngaps / len(col))<EOL><DEDENT><DEDENT>seqstrs = [str(rec.seq) for rec in aln]<EOL>clean_cols = [col for col in zip(*seqstrs)<EOL>if pct_nongaps(col) >= threshold]<EOL>alphabet = aln[<NUM_LIT:0>].seq.alphabet<EOL>clean_seqs = [Seq('<STR_LIT>'.join(row), alphabet)<EOL>for row in zip(*clean_cols)]<EOL>clean_recs = []<EOL>for rec, seq in zip(aln, clean_seqs):<EOL><INDENT>newrec = deepcopy(rec)<EOL>newrec.seq = seq<EOL>clean_recs.append(newrec)<EOL><DEDENT>return MultipleSeqAlignment(clean_recs, alphabet=alphabet)<EOL>
Remove gappy columns from an alignment.