def _read_bam(bam_fn, precursors):
mode = "<STR_LIT:r>" if bam_fn.endswith("<STR_LIT>") else "<STR_LIT:rb>"<EOL>handle = pysam.Samfile(bam_fn, mode)<EOL>reads = defaultdict(realign)<EOL>for line in handle:<EOL><INDENT>chrom = handle.getrname(line.reference_id)<EOL>query_name = line.query_name<EOL>if query_name not in reads:<EOL><INDENT>reads[query_name].sequence = line.query_sequence<EOL><DEDENT>iso = isomir()<EOL>iso.align = line<EOL>iso.start = line.reference_start<EOL>iso.subs, iso.add = _realign(reads[query_name].sequence, precursors[chrom], line.reference_start)<EOL>reads[query_name].set_precursor(chrom, iso)<EOL><DEDENT>reads = _clean_hits(reads)<EOL>return reads<EOL>
read bam file and perform realignment of hits