def read_alignment(out_sam, loci, seqs, out_file):
hits = defaultdict(list)<EOL>with open(out_file, "<STR_LIT:w>") as out_handle:<EOL><INDENT>samfile = pysam.Samfile(out_sam, "<STR_LIT:r>")<EOL>for a in samfile.fetch():<EOL><INDENT>if not a.is_unmapped:<EOL><INDENT>nm = int([t[<NUM_LIT:1>] for t in a.tags if t[<NUM_LIT:0>] == "<STR_LIT>"][<NUM_LIT:0>])<EOL>a = makeBED(a)<EOL>if not a:<EOL><INDENT>continue<EOL><DEDENT>ref, locus = get_loci(samfile.getrname(int(a.chr)), loci)<EOL>hits[a.name].append((nm, "<STR_LIT>" % (a.name, a.name.split("<STR_LIT:->")[<NUM_LIT:0>], locus, ref, a.start, a.end)))<EOL><DEDENT><DEDENT>for hit in hits.values():<EOL><INDENT>nm = hit[<NUM_LIT:0>][<NUM_LIT:0>]<EOL>for l in hit:<EOL><INDENT>if nm == l[<NUM_LIT:0>]:<EOL><INDENT>print(l[<NUM_LIT:1>], file=out_handle)<EOL><DEDENT><DEDENT><DEDENT><DEDENT>return out_file<EOL>
read which seqs map to which loci and
    return a tab separated file