def blast_to_cigar(query_seq, match_seq, subject_seq, cigar_age='new'):
    """converts BLAST alignment into a old or new CIGAR line

    Args:
        query_seq (str): Aligned portion of query sequence

        match_seq (str): Alignment sequence

        subject_seq (str): Aligned portion of subject/reference sequence

        cigar_age (str): ['old', 'new'] CIGAR format to use, new is highly
            detailed while old is fairly minimalistic

    Returns:
        str: CIGAR string

    Raises:
        ValueError: If query_seq, match_seq, and match_seq not same length

    Examples:
        >>> query = 'AAGGG--CCTTGTA'
        >>> subject = 'AAGCCTTCCAGGTA'
        >>> alignment_old = '|||||  |||||||'
        >>> alignment_new = 'AAG++  CC++GTA'
        >>> blast_to_cigar(query, alignment_new, subject)
        3=2X2D2=2X3=
        >>> blast_to_cigar(query, alignment_old, subject, cigar_age='old')
        5M2D7M
    """
    if not len(query_seq) == len(match_seq) or not len(query_seq) == len(
        subject_seq) or not len(subject_seq) == len(match_seq):
        raise ValueError(
            'query_seq, match_seq, and subject_seq not same lengths.')
    cigar_line_raw = []
    for query, match, subject in zip(query_seq, match_seq, subject_seq):
        if query == '-':
            cigar_line_raw.append('D')
            continue
        elif subject == '-':
            cigar_line_raw.append('I')
            continue
        elif match == '+' or match == '|' or match.isalpha():
            if match != '+' and cigar_age == 'new':
                cigar_line_raw.append('=')
                continue
            elif match == '+' and cigar_age == 'new':
                cigar_line_raw.append('X')
                continue
            else:
                cigar_line_raw.append('M')
                continue
        elif cigar_age == 'new':
            cigar_line_raw.append('X')
            continue
        else:
            cigar_line_raw.append('M')
    cigar_line = []
    last_position = ''
    repeats = 1
    cigar_len = len(cigar_line_raw)
    for letter in enumerate(cigar_line_raw):
        if letter[1] == last_position:
            repeats += 1
        else:
            if repeats != 1:
                cigar_line.append(str(repeats))
                repeats = 1
            cigar_line.append(last_position)
        if letter[0] == cigar_len - 1:
            if repeats != 1:
                cigar_line.append(str(repeats))
                repeats = 1
            cigar_line.append(letter[1])
        last_position = letter[1]
    return ''.join(cigar_line)
