def region_pos_to_genomic_pos(name, start, cigar):
    '''
        converts a junction position to a genomic location given a junction
        ref name, the junction position, and the cigar alignment.

        returns: (genomic ref, genomic pos, genomic cigar)

    >>> region_pos_to_genomic_pos('chr1:1000-1050,2000-2050,3000-4000', 25, [(0, 100)])
    ('chr1', 1025, [(0, 25), (3, 950), (0, 50), (3, 950), (0, 25)])

    >>> region_pos_to_genomic_pos('chr1:1000-1050,1050-1200', 25, [(0, 100)])
    ('chr1', 1025, [(0, 25), (3, 0), (0, 75)])

    >>> region_pos_to_genomic_pos('chr3R:17630851-17630897,17634338-17634384', 17, [(0, 39)])
    ('chr3R', 17630868, [(0, 29), (3, 3441), (0, 10)])

    >>> region_pos_to_genomic_pos('chr1:1000-1050,2000-2050', 25, [(4, 25), (0, 50), (4, 25)])
    ('chr1', 1025, [(4, 25), (0, 25), (3, 950), (0, 25), (4, 25)])

    >>> region_pos_to_genomic_pos('chr1:1000-1050,2000-2050', 25, [(5, 25), (0, 75)])
    ('chr1', 1025, [(5, 25), (0, 25), (3, 950), (0, 50)])

    >>> region_pos_to_genomic_pos('chr1:1000-1050,2000-2050', 25, [(5, 25), (0, 75)])
    ('chr1', 1025, [(5, 25), (0, 25), (3, 950), (0, 50)])

    >>> region_pos_to_genomic_pos('chr7:16829153-16829246,16829246-16829339', 62, cigar_fromstr('83M18S'))
    ('chr7', 16829215, [(0, 31), (3, 0), (0, 52), (4, 18)])


    '''

    if name in __region_cache:
        chrom, fragments = __region_cache[name]
    else:
        c1 = name.split(':')
        chrom = c1[0]

        fragments = []
        for fragment in c1[1].split(','):
            s, e = fragment.split('-')
            fragments.append((int(s), int(e)))

        __region_cache[name] = (chrom, fragments)

    chr_cigar = []
    chr_start = fragments[0][0]

    read_start = int(start)

    frag_idx = 0
    frag_start = 0
    frag_end = 0

    for i, (s, e) in enumerate(fragments):
        if chr_start + read_start < e:
            chr_start += read_start
            frag_idx = i
            frag_start = s
            frag_end = e
            break

        else:
            chr_start += (e - s)
            read_start -= (e - s)

    cur_pos = chr_start

    for op, length in cigar:
        if op in [1, 4, 5]:
            chr_cigar.append((op, length))

        elif op in [0, 2]:
            if frag_end <= cur_pos + length:
                cur_pos += length
                chr_cigar.append((op, length))

            else:
                while cur_pos + length > frag_end:
                    if frag_end - cur_pos > 0:
                        chr_cigar.append((op, frag_end - cur_pos))
                        length -= (frag_end - cur_pos)
                    cur_pos = frag_end
                    frag_idx += 1
                    if len(fragments) <= frag_idx:
                        print 'ERROR converting: ', name, fragments 
                        return (chrom, 0, chr_cigar)
                    frag_start, frag_end = fragments[frag_idx]
                    chr_cigar.append((3, frag_start - cur_pos))
                    cur_pos = frag_start

                cur_pos = cur_pos + length
                chr_cigar.append((op, length))
        else:
            print "Unsupported CIGAR operation (%s)" % bam_cigar[op]
            sys.exit(1)

    return (chrom, chr_start, chr_cigar)
dataset/ETHPy150Open ngsutils/ngsutils/ngsutils/bam/__init__.py/region_pos_to_genomic_pos/swapped operands of `<=`