# Path: dark/sam.py
# CONSUMES_REFERENCE = {CMATCH, CDEL, CREF_SKIP, CEQUAL, CDIFF}


from pysam import CINS, CSOFT_CLIP, CHARD_CLIP
from dark.sam import CONSUMES_REFERENCE
                     'non-hard-clipped bases.')


def insertionOffset(offset, pairs, cigarOperations):
    """
    Get the insertion offset in the reference for a base that is part of an
    insertion.

    @param offset: The offset (in C{pairs}) of the soft-clipped base (which
       has a to-be-determined offset in the reference).
    @param pairs: A C{list} of C{int} (queryOffset, referenceOffset) pairs
       for a pysam C{pysam.AlignedSegment}, as returned by
       C{read.get_aligned_pairs} where the read is returned by the pysam
       C{fetch} method.
    @param cigarOperations: A C{list} of pysam CIGAR operations.
    @return: A 2-tuple containing a C{bool} and the C{int} reference offset of
        the base after the insertion. The C{bool} indicates whether we needed
        to look back in the sequence to find a reference base.
    """
    assert cigarOperations[offset] == CINS
    assert pairs[offset][1] is None

    # Look back.
    for pair, cigarOperation in zip(pairs[offset::-1],
                                    cigarOperations[offset::-1]):
        if cigarOperation != CINS:
            _, referenceOffset = pair
            # If we have a reference offset, then we've found our
            # answer. Otherwise, check as above.
            if referenceOffset is None:

# Path: dark/sam.py
# CONSUMES_REFERENCE = {CMATCH, CDEL, CREF_SKIP, CEQUAL, CDIFF}


# Path: dark/cigar.py
from pysam import CINS, CSOFT_CLIP, CHARD_CLIP
from dark.sam import CONSUMES_REFERENCE

                     'non-hard-clipped bases.')


def insertionOffset(offset, pairs, cigarOperations):
    """
    Get the insertion offset in the reference for a base that is part of an
    insertion.

    @param offset: The offset (in C{pairs}) of the soft-clipped base (which
       has a to-be-determined offset in the reference).
    @param pairs: A C{list} of C{int} (queryOffset, referenceOffset) pairs
       for a pysam C{pysam.AlignedSegment}, as returned by
       C{read.get_aligned_pairs} where the read is returned by the pysam
       C{fetch} method.
    @param cigarOperations: A C{list} of pysam CIGAR operations.
    @return: A 2-tuple containing a C{bool} and the C{int} reference offset of
        the base after the insertion. The C{bool} indicates whether we needed
        to look back in the sequence to find a reference base.
    """
    assert cigarOperations[offset] == CINS
    assert pairs[offset][1] is None

    # Look back.
    for pair, cigarOperation in zip(pairs[offset::-1],
                                    cigarOperations[offset::-1]):
        if cigarOperation != CINS:
            _, referenceOffset = pair
            # If we have a reference offset, then we've found our
            # answer. Otherwise, check as above.
            if referenceOffset is None:

                assert cigarOperation not in CONSUMES_REFERENCE