def get_ribo_counts(ribo_fileobj, transcript_name, read_lengths, read_offsets):
read_counts = {}<EOL>total_reads = <NUM_LIT:0><EOL>for record in ribo_fileobj.fetch(transcript_name):<EOL><INDENT>query_length = record.query_length<EOL>position_ref = record.pos + <NUM_LIT:1><EOL>for index, read_length in enumerate(read_lengths):<EOL><INDENT>position = position_ref  <EOL>if read_length == <NUM_LIT:0> or read_length == query_length:<EOL><INDENT>position += read_offsets[index]<EOL><DEDENT>else:<EOL><INDENT>continue<EOL><DEDENT>total_reads += <NUM_LIT:1><EOL>try:<EOL><INDENT>read_counts[position]<EOL><DEDENT>except KeyError:<EOL><INDENT>read_counts[position] = {<NUM_LIT:1>: <NUM_LIT:0>, <NUM_LIT:2>: <NUM_LIT:0>, <NUM_LIT:3>: <NUM_LIT:0>}<EOL><DEDENT>rem = position % <NUM_LIT:3><EOL>if rem == <NUM_LIT:0>:<EOL><INDENT>read_counts[position][<NUM_LIT:3>] += <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>read_counts[position][rem] += <NUM_LIT:1><EOL><DEDENT><DEDENT><DEDENT>log.debug('<STR_LIT>'.format(total_reads))<EOL>log.debug('<STR_LIT>'.format(transcript_name, read_counts))<EOL>return read_counts, total_reads<EOL>
For each mapped read of the given transcript in the BAM file
    (pysam AlignmentFile object), return the position (+1) and the
    corresponding frame (1, 2 or 3) to which it aligns.

    Keyword arguments:
    ribo_fileobj -- file object - BAM file opened using pysam AlignmentFile
    transcript_name -- Name of transcript to get counts for
    read_length (optional) -- If provided, get counts only for reads of this length.