def cleancigar(cigar):
    '''
    Cleans a CIGAR alignment to remove zero length operations

    >>> cigar_tostr(cleancigar(cigar_fromstr('31M0N52M18S')))
    '83M18S'

    '''
    newcigar = []

    changed = False
    zero = False

    for op, size in cigar:
        if size > 0:
            if zero and newcigar:
                if newcigar[-1][0] == op:
                    newsize = newcigar[-1][1] + size
                    newcigar[-1] = (op, newsize)
                zero = 0
            else:
                newcigar.append((op, size))
        else:
            changed = True
            zero = True

    if changed:
        return newcigar

    return None
dataset/ETHPy150Open ngsutils/ngsutils/ngsutils/bam/__init__.py/cleancigar/original