def format_seq(seq, start, end, pad, retain_end=True):
    """Makes each sequence end when the first `end` or `pad` is encountered."""
    formatted_seq = []
    for id in seq:
        if id == start:
            continue
        if id == end:
            if retain_end:
                formatted_seq.append(id)
            break
        if id == pad:
            break
        formatted_seq.append(id)
    return formatted_seq