@staticmethod
def cigar_prefix_length(cigar, reference_bases):
    """
		Given a prefix of length reference_bases relative to the reference, how
		long is the prefix of the read? In other words: If reference_bases on
		the reference are consumed, how many bases on the query does that
		correspond to?

		If the position is within or at the end of an insertion (which do not
		consume bases on the reference), then the number of bases up to the
		beginning of the insertion is reported.

		Return a pair (reference_bases, query_bases) where the value for
		reference_bases may be smaller than the requested one if the CIGAR does
		not cover enough reference bases.

		Reference skips (N operators) are treated as the end of the read. That
		is, no positions beyond a reference skip are reported.
		"""
    ref_pos = 0
    query_pos = 0
    for op, length in cigar:
        if op in (0, 7, 8):
            ref_pos += length
            query_pos += length
            if ref_pos >= reference_bases:
                return reference_bases, query_pos + reference_bases - ref_pos
        elif op == 2:
            ref_pos += length
            if ref_pos >= reference_bases:
                return reference_bases, query_pos
        elif op == 1:
            query_pos += length
        elif op == 4 or op == 5:
            pass
        elif op == 3:
            return reference_bases, query_pos
        else:
            assert False, 'unknown CIGAR operator'
    assert ref_pos < reference_bases
    return ref_pos, query_pos
