def maf_iterator(fn, index_friendly=False,<EOL>yield_class=MultipleSequenceAlignment, yield_kw_args={},<EOL>verbose=False):
try:<EOL><INDENT>fh = open(fn)<EOL><DEDENT>except (TypeError):<EOL><INDENT>fh = fn<EOL><DEDENT>if index_friendly:<EOL><INDENT>fh = iter(fh.readline, '<STR_LIT>')<EOL><DEDENT>sequences = []<EOL>meta_data = {}<EOL>for line in fh:<EOL><INDENT>line = line.strip()<EOL>if line == "<STR_LIT>" or line[<NUM_LIT:0>] == "<STR_LIT:#>":<EOL><INDENT>continue<EOL><DEDENT>parts = line.split()<EOL>line_type = parts[<NUM_LIT:0>].strip()<EOL>if line_type == A_LINE:<EOL><INDENT>if sequences != []:<EOL><INDENT>meta_data[SEQ_ORDER_KEY] = [s.name for s in sequences]<EOL>kw_args = merge_dictionaries({"<STR_LIT>": sequences,<EOL>"<STR_LIT>": meta_data}, yield_kw_args)<EOL>yield yield_class(**kw_args)<EOL><DEDENT>sequences = []<EOL>meta_data = {}<EOL>for i in range(<NUM_LIT:1>, len(parts)):<EOL><INDENT>assert(parts[i].count("<STR_LIT:=>") == <NUM_LIT:1>)<EOL>piv = parts[i].find("<STR_LIT:=>")<EOL>meta_data[parts[i][:piv]] = parts[i][piv + <NUM_LIT:1>:]<EOL><DEDENT><DEDENT>elif line_type == S_LINE:<EOL><INDENT>sequences.append(__build_sequence(parts))<EOL><DEDENT>elif line_type == I_LINE:<EOL><INDENT>if len(sequences) < <NUM_LIT:1>:<EOL><INDENT>raise MAFError("<STR_LIT>" +<EOL>"<STR_LIT>")<EOL><DEDENT>__annotate_sequence_with_context(sequences[-<NUM_LIT:1>], parts)<EOL><DEDENT>elif line_type == E_LINE:<EOL><INDENT>sequences.append(__build_unknown_sequence(parts))<EOL><DEDENT>elif line_type == Q_LINE:<EOL><INDENT>if len(sequences) < <NUM_LIT:1>:<EOL><INDENT>raise MAFError("<STR_LIT>" +<EOL>"<STR_LIT>")<EOL><DEDENT>__annotate_sequence_with_quality(sequences[-<NUM_LIT:1>], parts)<EOL><DEDENT>else:<EOL><INDENT>raise MAFError("<STR_LIT>" + line)<EOL><DEDENT><DEDENT>if sequences != []:<EOL><INDENT>meta_data[SEQ_ORDER_KEY] = [s.name for s in sequences]<EOL>kw_args = merge_dictionaries({"<STR_LIT>": sequences,<EOL>"<STR_LIT>": meta_data},<EOL>yield_kw_args)<EOL>yield yield_class(**kw_args)<EOL><DEDENT>
Iterate of MAF format file and yield <yield_class> objects for each block.

MAF files are arranged in blocks. Each block is a multiple alignment. Within
a block, the first character of a line indicates what kind of line it is:

a -- key-value pair meta data for block; one per block, should be first line
s -- a sequence line; see __build_sequence() for details.
i -- always come after s lines, and contain information about the context of
     the sequence; see __annotate_sequence_with_context() for details.
e -- indicates that there is no aligning sequence for a species, but that
     there are blocks before and after this one that do align. See
     __build_unknown_sequence() for details.
q -- quality information about an aligned base in a species. See
     __annotate_sequence_with_quality()

:param yield_class:   yield objects returned by this function/constructor;
                      must accept key-word args of 'sequences' and
                      'meta_data' which are list and dictionary respectively.
                      Default is MultipleSequenceAlignment
:param yield_kw_args: extra keyword args to pass to 'yield_class'