def alignmentPositions(alignment):
    """
    Extracts start position and length from an alignment

    Arguments:
      alignment : tuples of (operation, length) for each alignment operation.

    Returns:
      dict : query (q) and reference (r) start (0-based) and length information with keys
             {q_start, q_length, r_start, r_length}.
    """
    result = {'q_start': 0, 'q_length': 0, 'r_start': 0, 'r_length': 0}
    if alignment[0][0] == 'S':
        result['q_start'] = alignment[0][1]
    if alignment[0][0] == 'N':
        result['r_start'] = alignment[0][1]
    elif alignment[0][0] == 'S' and alignment[1][0] == 'N':
        result['r_start'] = alignment[1][1]
    for x, i in alignment:
        if x in ('M', '=', 'X'):
            result['r_length'] += i
            result['q_length'] += i
        elif x == 'D':
            result['r_length'] += i
        elif x == 'I':
            result['q_length'] += i
    return result
