def substitute(sequence, offset, ref, alt):
    """Mutate a sequence by substituting given `alt` at instead of `ref` at the
    given `position`.

    Parameters
    ----------
    sequence : sequence
        String of amino acids or DNA bases

    offset : int
        Base 0 offset from start of `sequence`

    ref : sequence or str
        What do we expect to find at the position?

    alt : sequence or str
        Alternate sequence to insert
    """
    n_ref = len(ref)
    sequence_ref = sequence[offset:offset + n_ref]
    assert str(sequence_ref) == str(ref), \
        "Reference %s at offset %d != expected reference %s" % \
        (sequence_ref, offset, ref)
    prefix = sequence[:offset]
    suffix = sequence[offset + n_ref:]
    return prefix + alt + suffix