import re


def mdz_parser(mdz_str):
    """
    Takes the MD:Z filed string and returns a list of SNP positions and the SNP base
    :param mdz_str: the string from MD:Z field of the SAM file for a read
    :return: List of (pos, SNP_base) tuples. Please note that pos is 0-based;
    therefore, pos 0 points to the first base in the sequence.
    """

    # 'MD:Z:15A15A9G15C42'
    # Removing the MD:Z: prefix from the string
    mdz_str = mdz_str[5:]
    # Splitting into alphabetical and numerical parts
    mdz_parsed = filter(None, re.split(r'(\d+)', mdz_str))
    # mdz_parsed = re.findall(r"[^\W\d_]+|\d+", mdz_str)

    # Return list
    pos_snps = []
    pos = 0
    for item in mdz_parsed:
        if item.isalpha():
            for base in item:
                pos_snps.append((pos, base))
                pos += 1
        elif item.isnumeric():
            pos += int(item)
        else:
            print("ERROR: Invalid MD:Z field in SAM file: {}".format(item))

    return pos_snps